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
13#include "lldb/Utility/Stream.h"
14#include "lldb/lldb-forward.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/Support/JSON.h"
17#include <atomic>
18#include <chrono>
19#include <optional>
20#include <ratio>
21#include <string>
22#include <vector>
23
24namespace lldb_private {
25
26using StatsClock = std::chrono::high_resolution_clock;
27using StatsTimepoint = std::chrono::time_point<StatsClock>;
28
30public:
31 using Duration = std::chrono::duration<double>;
32
33 Duration get() const {
34 return Duration(InternalDuration(value.load(std::memory_order_relaxed)));
35 }
36 operator Duration() const { return get(); }
37
39 value.fetch_add(std::chrono::duration_cast<InternalDuration>(dur).count(),
40 std::memory_order_relaxed);
41 return *this;
42 }
43
44private:
45 using InternalDuration = std::chrono::duration<uint64_t, std::micro>;
46 std::atomic<uint64_t> value{0};
47};
48
49/// A class that measures elapsed time in an exception safe way.
50///
51/// This is a RAII class is designed to help gather timing statistics within
52/// LLDB where objects have optional Duration variables that get updated with
53/// elapsed times. This helps LLDB measure statistics for many things that are
54/// then reported in LLDB commands.
55///
56/// Objects that need to measure elapsed times should have a variable of type
57/// "StatsDuration m_time_xxx;" which can then be used in the constructor of
58/// this class inside a scope that wants to measure something:
59///
60/// ElapsedTime elapsed(m_time_xxx);
61/// // Do some work
62///
63/// This class will increment the m_time_xxx variable with the elapsed time
64/// when the object goes out of scope. The "m_time_xxx" variable will be
65/// incremented when the class goes out of scope. This allows a variable to
66/// measure something that might happen in stages at different times, like
67/// resolving a breakpoint each time a new shared library is loaded.
69public:
70 /// Set to the start time when the object is created.
72 /// Elapsed time in seconds to increment when this object goes out of scope.
74
75public:
76 ElapsedTime(StatsDuration &opt_time) : m_elapsed_time(opt_time) {
77 m_start_time = StatsClock::now();
78 }
80 StatsClock::duration elapsed = StatsClock::now() - m_start_time;
82 }
83};
84
85/// A class to count success/fail statistics.
87 StatsSuccessFail(llvm::StringRef n) : name(n.str()) {}
88
90 void NotifyFailure() { ++failures; }
91
92 llvm::json::Value ToJSON() const;
93 std::string name;
94 uint32_t successes = 0;
95 uint32_t failures = 0;
96};
97
98/// A class that represents statistics for a since lldb_private::Module.
100 llvm::json::Value ToJSON() const;
101 intptr_t identifier;
102 std::string path;
103 std::string uuid;
104 std::string triple;
105 // Path separate debug info file, or empty if none.
106 std::string symfile_path;
107 // If the debug info is contained in multiple files where each one is
108 // represented as a separate lldb_private::Module, then these are the
109 // identifiers of these modules in the global module list. This allows us to
110 // track down all of the stats that contribute to this module.
111 std::vector<intptr_t> symfile_modules;
112 llvm::StringMap<llvm::json::Value> type_system_stats;
113 double symtab_parse_time = 0.0;
114 double symtab_index_time = 0.0;
115 double debug_parse_time = 0.0;
116 double debug_index_time = 0.0;
117 uint64_t debug_info_size = 0;
123 bool symtab_stripped = false;
126};
127
129 llvm::json::Value ToJSON() const;
131};
132
134public:
135 void SetSummaryOnly(bool value) { m_summary_only = value; }
136 bool GetSummaryOnly() const { return m_summary_only.value_or(false); }
137
138 void SetLoadAllDebugInfo(bool value) { m_load_all_debug_info = value; }
139 bool GetLoadAllDebugInfo() const {
140 return m_load_all_debug_info.value_or(false);
141 }
142
143 void SetIncludeTargets(bool value) { m_include_targets = value; }
144 bool GetIncludeTargets() const {
145 if (m_include_targets.has_value())
146 return m_include_targets.value();
147 // Default to true in both default mode and summary mode.
148 return true;
149 }
150
151 void SetIncludeModules(bool value) { m_include_modules = value; }
152 bool GetIncludeModules() const {
153 if (m_include_modules.has_value())
154 return m_include_modules.value();
155 // `m_include_modules` has no value set, so return a value based on
156 // `m_summary_only`.
157 return !GetSummaryOnly();
158 }
159
160 void SetIncludeTranscript(bool value) { m_include_transcript = value; }
161 bool GetIncludeTranscript() const {
162 if (m_include_transcript.has_value())
163 return m_include_transcript.value();
164 // `m_include_transcript` has no value set, so return a value based on
165 // `m_summary_only`.
166 return !GetSummaryOnly();
167 }
168
169private:
170 std::optional<bool> m_summary_only;
171 std::optional<bool> m_load_all_debug_info;
172 std::optional<bool> m_include_targets;
173 std::optional<bool> m_include_modules;
174 std::optional<bool> m_include_transcript;
175};
176
177/// A class that represents statistics for a since lldb_private::Target.
179public:
180 llvm::json::Value ToJSON(Target &target,
181 const lldb_private::StatisticsOptions &options);
182
187
191
192protected:
194 std::optional<StatsTimepoint> m_launch_or_attach_time;
195 std::optional<StatsTimepoint> m_first_private_stop_time;
196 std::optional<StatsTimepoint> m_first_public_stop_time;
197 StatsSuccessFail m_expr_eval{"expressionEvaluation"};
199 std::vector<intptr_t> m_module_identifiers;
201 void CollectStats(Target &target);
202};
203
205public:
206 static void SetCollectingStats(bool enable) { g_collecting_stats = enable; }
207 static bool GetCollectingStats() { return g_collecting_stats; }
208
209 /// Get metrics associated with one or all targets in a debugger in JSON
210 /// format.
211 ///
212 /// \param debugger
213 /// The debugger to get the target list from if \a target is NULL.
214 ///
215 /// \param target
216 /// The single target to emit statistics for if non NULL, otherwise dump
217 /// statistics only for the specified target.
218 ///
219 /// \param summary_only
220 /// If true, only report high level summary statistics without
221 /// targets/modules/breakpoints etc.. details.
222 ///
223 /// \return
224 /// Returns a JSON value that contains all target metrics.
225 static llvm::json::Value
226 ReportStatistics(Debugger &debugger, Target *target,
227 const lldb_private::StatisticsOptions &options);
228
229protected:
230 // Collecting stats can be set to true to collect stats that are expensive
231 // to collect. By default all stats that are cheap to collect are enabled.
232 // This settings is here to maintain compatibility with "statistics enable"
233 // and "statistics disable".
235};
236
237} // namespace lldb_private
238
239#endif // LLDB_TARGET_STATISTICS_H
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
Definition: Statistics.cpp:39
static MemoryStats GetMemoryStats()
static void SetCollectingStats(bool enable)
Definition: Statistics.h:206
static bool GetCollectingStats()
Definition: Statistics.h:207
static bool g_collecting_stats
Definition: Statistics.h:234
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.
Definition: Statistics.cpp:225
A class to manage flag bits.
Definition: Debugger.h:80
A class that measures elapsed time in an exception safe way.
Definition: Statistics.h:68
StatsDuration & m_elapsed_time
Elapsed time in seconds to increment when this object goes out of scope.
Definition: Statistics.h:73
ElapsedTime(StatsDuration &opt_time)
Definition: Statistics.h:76
StatsTimepoint m_start_time
Set to the start time when the object is created.
Definition: Statistics.h:71
Duration get() const
Definition: Statistics.h:33
std::atomic< uint64_t > value
Definition: Statistics.h:46
std::chrono::duration< double > Duration
Definition: Statistics.h:31
StatsDuration & operator+=(Duration dur)
Definition: Statistics.h:38
std::chrono::duration< uint64_t, std::micro > InternalDuration
Definition: Statistics.h:45
A class that represents statistics for a since lldb_private::Target.
Definition: Statistics.h:178
StatsSuccessFail m_frame_var
Definition: Statistics.h:198
std::optional< StatsTimepoint > m_launch_or_attach_time
Definition: Statistics.h:194
uint32_t m_source_map_deduce_count
Definition: Statistics.h:200
void CollectStats(Target &target)
Definition: Statistics.cpp:45
StatsSuccessFail & GetFrameVariableStats()
Definition: Statistics.h:190
llvm::json::Value ToJSON(Target &target, const lldb_private::StatisticsOptions &options)
Definition: Statistics.cpp:106
StatsDuration m_create_time
Definition: Statistics.h:193
std::optional< StatsTimepoint > m_first_public_stop_time
Definition: Statistics.h:196
StatsDuration & GetCreateTime()
Definition: Statistics.h:188
StatsSuccessFail & GetExpressionStats()
Definition: Statistics.h:189
StatsSuccessFail m_expr_eval
Definition: Statistics.h:197
std::vector< intptr_t > m_module_identifiers
Definition: Statistics.h:199
std::optional< StatsTimepoint > m_first_private_stop_time
Definition: Statistics.h:195
A class that represents a running process on the host machine.
std::chrono::time_point< StatsClock > StatsTimepoint
Definition: Statistics.h:27
std::chrono::high_resolution_clock StatsClock
Definition: Statistics.h:26
ConstString::MemoryStats stats
Definition: Statistics.h:130
llvm::json::Value ToJSON() const
Definition: Statistics.cpp:97
A class that represents statistics for a since lldb_private::Module.
Definition: Statistics.h:99
llvm::json::Value ToJSON() const
Definition: Statistics.cpp:51
llvm::StringMap< llvm::json::Value > type_system_stats
Definition: Statistics.h:112
std::vector< intptr_t > symfile_modules
Definition: Statistics.h:111
std::optional< bool > m_load_all_debug_info
Definition: Statistics.h:171
void SetIncludeTranscript(bool value)
Definition: Statistics.h:160
void SetSummaryOnly(bool value)
Definition: Statistics.h:135
std::optional< bool > m_include_modules
Definition: Statistics.h:173
std::optional< bool > m_include_transcript
Definition: Statistics.h:174
void SetIncludeModules(bool value)
Definition: Statistics.h:151
std::optional< bool > m_summary_only
Definition: Statistics.h:170
std::optional< bool > m_include_targets
Definition: Statistics.h:172
void SetLoadAllDebugInfo(bool value)
Definition: Statistics.h:138
void SetIncludeTargets(bool value)
Definition: Statistics.h:143
A class to count success/fail statistics.
Definition: Statistics.h:86
StatsSuccessFail(llvm::StringRef n)
Definition: Statistics.h:87
llvm::json::Value ToJSON() const
Definition: Statistics.cpp:35