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;
169 bool symtab_stripped = false;
173};
174
179
181public:
182 void SetSummaryOnly(bool value) { m_summary_only = value; }
183 bool GetSummaryOnly() const { return m_summary_only.value_or(false); }
184
185 void SetLoadAllDebugInfo(bool value) { m_load_all_debug_info = value; }
186 bool GetLoadAllDebugInfo() const {
187 return m_load_all_debug_info.value_or(false);
188 }
189
190 void SetIncludeTargets(bool value) { m_include_targets = value; }
191 bool GetIncludeTargets() const {
192 if (m_include_targets.has_value())
193 return m_include_targets.value();
194 // Default to true in both default mode and summary mode.
195 return true;
196 }
197
198 void SetIncludeModules(bool value) { m_include_modules = value; }
199 bool GetIncludeModules() const {
200 if (m_include_modules.has_value())
201 return m_include_modules.value();
202 // `m_include_modules` has no value set, so return a value based on
203 // `m_summary_only`.
204 return !GetSummaryOnly();
205 }
206
207 void SetIncludeTranscript(bool value) { m_include_transcript = value; }
208 bool GetIncludeTranscript() const {
209 return m_include_transcript.value_or(false);
210 }
211
212 void SetIncludePlugins(bool value) { m_include_plugins = value; }
213 bool GetIncludePlugins() const {
214 if (m_include_plugins.has_value())
215 return m_include_plugins.value();
216 // Default to true in both default mode and summary mode.
217 return true;
218 }
219
220private:
221 std::optional<bool> m_summary_only;
222 std::optional<bool> m_load_all_debug_info;
223 std::optional<bool> m_include_targets;
224 std::optional<bool> m_include_modules;
225 std::optional<bool> m_include_transcript;
226 std::optional<bool> m_include_plugins;
227};
228
229/// A class that represents statistics about a TypeSummaryProviders invocations
230/// \note All members of this class need to be accessed in a thread safe manner
232public:
233 explicit SummaryStatistics(std::string name, std::string impl_type)
234 : m_total_time(), m_impl_type(std::move(impl_type)),
235 m_name(std::move(name)), m_count(0) {}
236
237 std::string GetName() const { return m_name; };
238 double GetTotalTime() const { return m_total_time.get().count(); }
239
240 uint64_t GetSummaryCount() const {
241 return m_count.load(std::memory_order_relaxed);
242 }
243
245
246 std::string GetSummaryKindName() const { return m_impl_type; }
247
248 llvm::json::Value ToJSON() const;
249
250 void Reset() { m_total_time.reset(); }
251
252 /// Basic RAII class to increment the summary count when the call is complete.
254 public:
256 : m_stats(summary_stats),
257 m_elapsed_time(summary_stats->GetDurationReference()) {}
258 ~SummaryInvocation() { m_stats->OnInvoked(); }
259
260 /// Delete the copy constructor and assignment operator to prevent
261 /// accidental double counting.
262 /// @{
265 /// @}
266
267 private:
270 };
271
272private:
273 void OnInvoked() noexcept { m_count.fetch_add(1, std::memory_order_relaxed); }
275 const std::string m_impl_type;
276 const std::string m_name;
277 std::atomic<uint64_t> m_count;
278};
279
280/// A class that wraps a std::map of SummaryStatistics objects behind a mutex.
282public:
283 /// Get the SummaryStatistics object for a given provider name, or insert
284 /// if statistics for that provider is not in the map.
287 std::lock_guard<std::mutex> guard(m_map_mutex);
288 if (auto iterator = m_summary_stats_map.find(provider.GetName());
289 iterator != m_summary_stats_map.end())
290 return iterator->second;
291
292 auto it = m_summary_stats_map.try_emplace(
293 provider.GetName(),
294 std::make_shared<SummaryStatistics>(provider.GetName(),
295 provider.GetSummaryKindName()));
296 return it.first->second;
297 }
298
299 llvm::json::Value ToJSON();
300
301 void Reset();
302
303private:
304 llvm::StringMap<SummaryStatisticsSP> m_summary_stats_map;
305 std::mutex m_map_mutex;
306};
307
308/// A class that represents statistics for a since lldb_private::Target.
341
343public:
344 static void SetCollectingStats(bool enable) { g_collecting_stats = enable; }
345 static bool GetCollectingStats() { return g_collecting_stats; }
346
347 /// Get metrics associated with one or all targets in a debugger in JSON
348 /// format.
349 ///
350 /// \param debugger
351 /// The debugger to get the target list from if \a target is NULL.
352 ///
353 /// \param target
354 /// The single target to emit statistics for if non NULL, otherwise dump
355 /// statistics only for the specified target.
356 ///
357 /// \param summary_only
358 /// If true, only report high level summary statistics without
359 /// targets/modules/breakpoints etc.. details.
360 ///
361 /// \return
362 /// Returns a JSON value that contains all target metrics.
363 static llvm::json::Value
364 ReportStatistics(Debugger &debugger, Target *target,
365 const lldb_private::StatisticsOptions &options);
366
367 /// Reset metrics associated with one or all targets in a debugger.
368 ///
369 /// \param debugger
370 /// The debugger to reset the target list from if \a target is NULL.
371 ///
372 /// \param target
373 /// The target to reset statistics for, or if null, reset statistics
374 /// for all targets
375 static void ResetStatistics(Debugger &debugger, Target *target);
376
377protected:
378 // Collecting stats can be set to true to collect stats that are expensive
379 // to collect. By default all stats that are cheap to collect are enabled.
380 // This settings is here to maintain compatibility with "statistics enable"
381 // and "statistics disable".
383};
384
385} // namespace lldb_private
386
387#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:344
static bool GetCollectingStats()
Definition Statistics.h:345
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:100
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:281
llvm::StringMap< SummaryStatisticsSP > m_summary_stats_map
Definition Statistics.h:304
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:286
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:255
A class that represents statistics about a TypeSummaryProviders invocations.
Definition Statistics.h:231
const std::string m_impl_type
Definition Statistics.h:275
llvm::json::Value ToJSON() const
std::string GetSummaryKindName() const
Definition Statistics.h:246
SummaryStatistics(std::string name, std::string impl_type)
Definition Statistics.h:233
std::atomic< uint64_t > m_count
Definition Statistics.h:277
std::string GetName() const
Definition Statistics.h:237
uint64_t GetSummaryCount() const
Definition Statistics.h:240
StatsDuration & GetDurationReference()
Definition Statistics.h:244
lldb_private::StatsDuration m_total_time
Definition Statistics.h:274
A class that represents statistics for a since lldb_private::Target.
Definition Statistics.h:309
void Reset(Target &target)
StatsSuccessFail m_frame_var
Definition Statistics.h:334
std::optional< StatsTimepoint > m_launch_or_attach_time
Definition Statistics.h:330
void CollectStats(Target &target)
StatsSuccessFail & GetFrameVariableStats()
Definition Statistics.h:324
llvm::json::Value ToJSON(Target &target, const lldb_private::StatisticsOptions &options)
StatsDuration m_create_time
Definition Statistics.h:328
std::optional< StatsTimepoint > m_first_public_stop_time
Definition Statistics.h:332
StatsDuration m_load_core_time
Definition Statistics.h:329
void IncreaseSourceRealpathAttemptCount(uint32_t count)
StatsDuration & GetLoadCoreTime()
Definition Statistics.h:322
StatsDuration & GetCreateTime()
Definition Statistics.h:321
uint32_t m_source_realpath_compatible_count
Definition Statistics.h:338
StatsSuccessFail & GetExpressionStats()
Definition Statistics.h:323
StatsSuccessFail m_expr_eval
Definition Statistics.h:333
std::vector< intptr_t > m_module_identifiers
Definition Statistics.h:335
uint32_t m_source_realpath_attempt_count
Definition Statistics.h:337
void IncreaseSourceRealpathCompatibleCount(uint32_t count)
std::optional< StatsTimepoint > m_first_private_stop_time
Definition Statistics.h:331
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:177
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:212
std::optional< bool > m_load_all_debug_info
Definition Statistics.h:222
void SetIncludeTranscript(bool value)
Definition Statistics.h:207
void SetSummaryOnly(bool value)
Definition Statistics.h:182
std::optional< bool > m_include_plugins
Definition Statistics.h:226
std::optional< bool > m_include_modules
Definition Statistics.h:224
std::optional< bool > m_include_transcript
Definition Statistics.h:225
void SetIncludeModules(bool value)
Definition Statistics.h:198
std::optional< bool > m_summary_only
Definition Statistics.h:221
std::optional< bool > m_include_targets
Definition Statistics.h:223
void SetLoadAllDebugInfo(bool value)
Definition Statistics.h:185
void SetIncludeTargets(bool value)
Definition Statistics.h:190
A class to count success/fail statistics.
Definition Statistics.h:114
llvm::json::Value ToJSON() const