LLDB mainline
Statistics.h
Go to the documentation of this file.
1//===-- Statistics.h --------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_TARGET_STATISTICS_H
10#define LLDB_TARGET_STATISTICS_H
11
15#include "lldb/Utility/Stream.h"
16#include "lldb/lldb-forward.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/Support/JSON.h"
19#include <atomic>
20#include <chrono>
21#include <mutex>
22#include <optional>
23#include <ratio>
24#include <string>
25#include <vector>
26
27namespace lldb_private {
28
29using StatsClock = std::chrono::high_resolution_clock;
30using StatsTimepoint = std::chrono::time_point<StatsClock>;
32// Declaring here as there is no private forward
33typedef std::shared_ptr<SummaryStatistics> SummaryStatisticsSP;
34
36public:
37 using Duration = std::chrono::duration<double>;
38
39 Duration get() const {
40 return Duration(InternalDuration(value.load(std::memory_order_relaxed)));
41 }
42 operator Duration() const { return get(); }
43
44 void reset() { value.store(0, std::memory_order_relaxed); }
45
47 value.fetch_add(std::chrono::duration_cast<InternalDuration>(dur).count(),
48 std::memory_order_relaxed);
49 return *this;
50 }
51
52private:
53 using InternalDuration = std::chrono::duration<uint64_t, std::micro>;
54 std::atomic<uint64_t> value{0};
55};
56
57/// A class that measures elapsed time in an exception safe way.
58///
59/// This is a RAII class is designed to help gather timing statistics within
60/// LLDB where objects have optional Duration variables that get updated with
61/// elapsed times. This helps LLDB measure statistics for many things that are
62/// then reported in LLDB commands.
63///
64/// Objects that need to measure elapsed times should have a variable of type
65/// "StatsDuration m_time_xxx;" which can then be used in the constructor of
66/// this class inside a scope that wants to measure something:
67///
68/// ElapsedTime elapsed(m_time_xxx);
69/// // Do some work
70///
71/// This class will increment the m_time_xxx variable with the elapsed time
72/// when the object goes out of scope. The "m_time_xxx" variable will be
73/// incremented when the class goes out of scope. This allows a variable to
74/// measure something that might happen in stages at different times, like
75/// resolving a breakpoint each time a new shared library is loaded.
77public:
78 /// Set to the start time when the object is created.
80 /// Elapsed time in seconds to increment when this object goes out of scope.
82
83public:
84 ElapsedTime(StatsDuration &opt_time) : m_elapsed_time(opt_time) {
85 m_start_time = StatsClock::now();
86 }
88 StatsClock::duration elapsed = StatsClock::now() - m_start_time;
90 }
91};
92
93/// A class to count time for plugins
95public:
96 void add(llvm::StringRef key, double value) {
97 if (key.empty())
98 return;
99 auto it = map.find(key);
100 if (it == map.end())
101 map.try_emplace(key, value);
102 else
103 it->second += value;
104 }
105 void merge(StatisticsMap map_to_merge) {
106 for (const auto &entry : map_to_merge.map) {
107 add(entry.first(), entry.second);
108 }
109 }
110 llvm::StringMap<double> map;
111};
112
113/// A class to count success/fail statistics.
117
118 llvm::json::Value ToJSON() const;
119 uint32_t successes = 0;
120 uint32_t failures = 0;
121};
122
123/// Holds statistics about DWO (Debug With Object) files.
124struct DWOStats {
126 uint32_t dwo_file_count = 0;
127 uint32_t dwo_error_count = 0;
128
135
136 friend DWOStats operator+(DWOStats lhs, const DWOStats &rhs) {
137 lhs += rhs;
138 return lhs;
139 }
140};
141
142/// A class that represents statistics for a since lldb_private::Module.
144 llvm::json::Value ToJSON() const;
145 intptr_t identifier;
146 std::string path;
147 std::string uuid;
148 std::string triple;
149 // Path separate debug info file, or empty if none.
150 std::string symfile_path;
151 // If the debug info is contained in multiple files where each one is
152 // represented as a separate lldb_private::Module, then these are the
153 // identifiers of these modules in the global module list. This allows us to
154 // track down all of the stats that contribute to this module.
155 std::vector<intptr_t> symfile_modules;
156 llvm::StringMap<llvm::json::Value> type_system_stats;
158 double symtab_parse_time = 0.0;
159 double symtab_index_time = 0.0;
161 double debug_parse_time = 0.0;
162 double debug_index_time = 0.0;
163 uint64_t debug_info_size = 0;
171 bool symtab_stripped = false;
175};
176
181
183public:
184 void SetSummaryOnly(bool value) { m_summary_only = value; }
185 bool GetSummaryOnly() const { return m_summary_only.value_or(false); }
186
187 void SetLoadAllDebugInfo(bool value) { m_load_all_debug_info = value; }
188 bool GetLoadAllDebugInfo() const {
189 return m_load_all_debug_info.value_or(false);
190 }
191
192 void SetIncludeTargets(bool value) { m_include_targets = value; }
193 bool GetIncludeTargets() const {
194 if (m_include_targets.has_value())
195 return m_include_targets.value();
196 // Default to true in both default mode and summary mode.
197 return true;
198 }
199
200 void SetIncludeModules(bool value) { m_include_modules = value; }
201 bool GetIncludeModules() const {
202 if (m_include_modules.has_value())
203 return m_include_modules.value();
204 // `m_include_modules` has no value set, so return a value based on
205 // `m_summary_only`.
206 return !GetSummaryOnly();
207 }
208
209 void SetIncludeTranscript(bool value) { m_include_transcript = value; }
210 bool GetIncludeTranscript() const {
211 return m_include_transcript.value_or(false);
212 }
213
214 void SetIncludePlugins(bool value) { m_include_plugins = value; }
215 bool GetIncludePlugins() const {
216 if (m_include_plugins.has_value())
217 return m_include_plugins.value();
218 // Default to true in both default mode and summary mode.
219 return true;
220 }
221
222private:
223 std::optional<bool> m_summary_only;
224 std::optional<bool> m_load_all_debug_info;
225 std::optional<bool> m_include_targets;
226 std::optional<bool> m_include_modules;
227 std::optional<bool> m_include_transcript;
228 std::optional<bool> m_include_plugins;
229};
230
231/// A class that represents statistics about a TypeSummaryProviders invocations
232/// \note All members of this class need to be accessed in a thread safe manner
234public:
235 explicit SummaryStatistics(std::string name, std::string impl_type)
236 : m_total_time(), m_impl_type(std::move(impl_type)),
237 m_name(std::move(name)), m_count(0) {}
238
239 std::string GetName() const { return m_name; };
240 double GetTotalTime() const { return m_total_time.get().count(); }
241
242 uint64_t GetSummaryCount() const {
243 return m_count.load(std::memory_order_relaxed);
244 }
245
247
248 std::string GetSummaryKindName() const { return m_impl_type; }
249
250 llvm::json::Value ToJSON() const;
251
252 void Reset() { m_total_time.reset(); }
253
254 /// Basic RAII class to increment the summary count when the call is complete.
256 public:
258 : m_stats(summary_stats),
259 m_elapsed_time(summary_stats->GetDurationReference()) {}
260 ~SummaryInvocation() { m_stats->OnInvoked(); }
261
262 /// Delete the copy constructor and assignment operator to prevent
263 /// accidental double counting.
264 /// @{
267 /// @}
268
269 private:
272 };
273
274private:
275 void OnInvoked() noexcept { m_count.fetch_add(1, std::memory_order_relaxed); }
277 const std::string m_impl_type;
278 const std::string m_name;
279 std::atomic<uint64_t> m_count;
280};
281
282/// A class that wraps a std::map of SummaryStatistics objects behind a mutex.
284public:
285 /// Get the SummaryStatistics object for a given provider name, or insert
286 /// if statistics for that provider is not in the map.
289 std::lock_guard<std::mutex> guard(m_map_mutex);
290 if (auto iterator = m_summary_stats_map.find(provider.GetName());
291 iterator != m_summary_stats_map.end())
292 return iterator->second;
293
294 auto it = m_summary_stats_map.try_emplace(
295 provider.GetName(),
296 std::make_shared<SummaryStatistics>(provider.GetName(),
297 provider.GetSummaryKindName()));
298 return it.first->second;
299 }
300
301 llvm::json::Value ToJSON();
302
303 void Reset();
304
305private:
306 llvm::StringMap<SummaryStatisticsSP> m_summary_stats_map;
307 std::mutex m_map_mutex;
308};
309
310/// A class that represents statistics for a since lldb_private::Target.
343
345public:
346 static void SetCollectingStats(bool enable) { g_collecting_stats = enable; }
347 static bool GetCollectingStats() { return g_collecting_stats; }
348
349 /// Get metrics associated with one or all targets in a debugger in JSON
350 /// format.
351 ///
352 /// \param debugger
353 /// The debugger to get the target list from if \a target is NULL.
354 ///
355 /// \param target
356 /// The single target to emit statistics for if non NULL, otherwise dump
357 /// statistics only for the specified target.
358 ///
359 /// \param summary_only
360 /// If true, only report high level summary statistics without
361 /// targets/modules/breakpoints etc.. details.
362 ///
363 /// \return
364 /// Returns a JSON value that contains all target metrics.
365 static llvm::json::Value
366 ReportStatistics(Debugger &debugger, Target *target,
367 const lldb_private::StatisticsOptions &options);
368
369 /// Reset metrics associated with one or all targets in a debugger.
370 ///
371 /// \param debugger
372 /// The debugger to reset the target list from if \a target is NULL.
373 ///
374 /// \param target
375 /// The target to reset statistics for, or if null, reset statistics
376 /// for all targets
377 static void ResetStatistics(Debugger &debugger, Target *target);
378
379protected:
380 // Collecting stats can be set to true to collect stats that are expensive
381 // to collect. By default all stats that are cheap to collect are enabled.
382 // This settings is here to maintain compatibility with "statistics enable"
383 // and "statistics disable".
385};
386
387} // namespace lldb_private
388
389#endif // LLDB_TARGET_STATISTICS_H
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
static MemoryStats GetMemoryStats()
static void SetCollectingStats(bool enable)
Definition Statistics.h:346
static bool GetCollectingStats()
Definition Statistics.h:347
static void ResetStatistics(Debugger &debugger, Target *target)
Reset metrics associated with one or all targets in a debugger.
static llvm::json::Value ReportStatistics(Debugger &debugger, Target *target, const lldb_private::StatisticsOptions &options)
Get metrics associated with one or all targets in a debugger in JSON format.
A class to manage flag bits.
Definition Debugger.h:106
A class that measures elapsed time in an exception safe way.
Definition Statistics.h:76
StatsDuration & m_elapsed_time
Elapsed time in seconds to increment when this object goes out of scope.
Definition Statistics.h:81
ElapsedTime(StatsDuration &opt_time)
Definition Statistics.h:84
StatsTimepoint m_start_time
Set to the start time when the object is created.
Definition Statistics.h:79
A class to count time for plugins.
Definition Statistics.h:94
llvm::StringMap< double > map
Definition Statistics.h:110
void add(llvm::StringRef key, double value)
Definition Statistics.h:96
void merge(StatisticsMap map_to_merge)
Definition Statistics.h:105
std::atomic< uint64_t > value
Definition Statistics.h:54
std::chrono::duration< double > Duration
Definition Statistics.h:37
StatsDuration & operator+=(Duration dur)
Definition Statistics.h:46
std::chrono::duration< uint64_t, std::micro > InternalDuration
Definition Statistics.h:53
A class that wraps a std::map of SummaryStatistics objects behind a mutex.
Definition Statistics.h:283
llvm::StringMap< SummaryStatisticsSP > m_summary_stats_map
Definition Statistics.h:306
SummaryStatisticsSP GetSummaryStatisticsForProvider(lldb_private::TypeSummaryImpl &provider)
Get the SummaryStatistics object for a given provider name, or insert if statistics for that provider...
Definition Statistics.h:288
SummaryInvocation & operator=(const SummaryInvocation &)=delete
SummaryInvocation(const SummaryInvocation &)=delete
Delete the copy constructor and assignment operator to prevent accidental double counting.
SummaryInvocation(SummaryStatisticsSP summary_stats)
Definition Statistics.h:257
A class that represents statistics about a TypeSummaryProviders invocations.
Definition Statistics.h:233
const std::string m_impl_type
Definition Statistics.h:277
llvm::json::Value ToJSON() const
std::string GetSummaryKindName() const
Definition Statistics.h:248
SummaryStatistics(std::string name, std::string impl_type)
Definition Statistics.h:235
std::atomic< uint64_t > m_count
Definition Statistics.h:279
std::string GetName() const
Definition Statistics.h:239
uint64_t GetSummaryCount() const
Definition Statistics.h:242
StatsDuration & GetDurationReference()
Definition Statistics.h:246
lldb_private::StatsDuration m_total_time
Definition Statistics.h:276
A class that represents statistics for a since lldb_private::Target.
Definition Statistics.h:311
void Reset(Target &target)
StatsSuccessFail m_frame_var
Definition Statistics.h:336
std::optional< StatsTimepoint > m_launch_or_attach_time
Definition Statistics.h:332
void CollectStats(Target &target)
StatsSuccessFail & GetFrameVariableStats()
Definition Statistics.h:326
llvm::json::Value ToJSON(Target &target, const lldb_private::StatisticsOptions &options)
StatsDuration m_create_time
Definition Statistics.h:330
std::optional< StatsTimepoint > m_first_public_stop_time
Definition Statistics.h:334
StatsDuration m_load_core_time
Definition Statistics.h:331
void IncreaseSourceRealpathAttemptCount(uint32_t count)
StatsDuration & GetLoadCoreTime()
Definition Statistics.h:324
StatsDuration & GetCreateTime()
Definition Statistics.h:323
uint32_t m_source_realpath_compatible_count
Definition Statistics.h:340
StatsSuccessFail & GetExpressionStats()
Definition Statistics.h:325
StatsSuccessFail m_expr_eval
Definition Statistics.h:335
std::vector< intptr_t > m_module_identifiers
Definition Statistics.h:337
uint32_t m_source_realpath_attempt_count
Definition Statistics.h:339
void IncreaseSourceRealpathCompatibleCount(uint32_t count)
std::optional< StatsTimepoint > m_first_private_stop_time
Definition Statistics.h:333
virtual std::string GetName()=0
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
virtual std::string GetSummaryKindName()
Get the name of the kind of Summary Provider, either c++, summary string, script or python.
A class that represents a running process on the host machine.
std::chrono::high_resolution_clock StatsClock
Definition Statistics.h:29
std::shared_ptr< SummaryStatistics > SummaryStatisticsSP
Definition Statistics.h:33
std::chrono::time_point< StatsClock > StatsTimepoint
Definition Statistics.h:30
ConstString::MemoryStats stats
Definition Statistics.h:179
llvm::json::Value ToJSON() const
Holds statistics about DWO (Debug With Object) files.
Definition Statistics.h:124
friend DWOStats operator+(DWOStats lhs, const DWOStats &rhs)
Definition Statistics.h:136
DWOStats & operator+=(const DWOStats &rhs)
Definition Statistics.h:129
uint32_t loaded_dwo_file_count
Definition Statistics.h:125
A class that represents statistics for a since lldb_private::Module.
Definition Statistics.h:143
llvm::json::Value ToJSON() const
llvm::StringMap< llvm::json::Value > type_system_stats
Definition Statistics.h:156
StatisticsMap symbol_locator_time
Definition Statistics.h:157
std::vector< intptr_t > symfile_modules
Definition Statistics.h:155
void SetIncludePlugins(bool value)
Definition Statistics.h:214
std::optional< bool > m_load_all_debug_info
Definition Statistics.h:224
void SetIncludeTranscript(bool value)
Definition Statistics.h:209
void SetSummaryOnly(bool value)
Definition Statistics.h:184
std::optional< bool > m_include_plugins
Definition Statistics.h:228
std::optional< bool > m_include_modules
Definition Statistics.h:226
std::optional< bool > m_include_transcript
Definition Statistics.h:227
void SetIncludeModules(bool value)
Definition Statistics.h:200
std::optional< bool > m_summary_only
Definition Statistics.h:223
std::optional< bool > m_include_targets
Definition Statistics.h:225
void SetLoadAllDebugInfo(bool value)
Definition Statistics.h:187
void SetIncludeTargets(bool value)
Definition Statistics.h:192
A class to count success/fail statistics.
Definition Statistics.h:114
llvm::json::Value ToJSON() const