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/Support/JSON.h"
16 #include <atomic>
17 #include <chrono>
18 #include <ratio>
19 #include <string>
20 #include <vector>
21 
22 namespace lldb_private {
23 
24 using StatsClock = std::chrono::high_resolution_clock;
25 using StatsTimepoint = std::chrono::time_point<StatsClock>;
26 
28 public:
29  using Duration = std::chrono::duration<double>;
30 
31  Duration get() const {
32  return Duration(InternalDuration(value.load(std::memory_order_relaxed)));
33  }
34  operator Duration() const { return get(); }
35 
37  value.fetch_add(std::chrono::duration_cast<InternalDuration>(dur).count(),
38  std::memory_order_relaxed);
39  return *this;
40  }
41 
42 private:
43  using InternalDuration = std::chrono::duration<uint64_t, std::micro>;
44  std::atomic<uint64_t> value{0};
45 };
46 
47 /// A class that measures elapsed time in an exception safe way.
48 ///
49 /// This is a RAII class is designed to help gather timing statistics within
50 /// LLDB where objects have optional Duration variables that get updated with
51 /// elapsed times. This helps LLDB measure statistics for many things that are
52 /// then reported in LLDB commands.
53 ///
54 /// Objects that need to measure elapsed times should have a variable of type
55 /// "StatsDuration m_time_xxx;" which can then be used in the constructor of
56 /// this class inside a scope that wants to measure something:
57 ///
58 /// ElapsedTime elapsed(m_time_xxx);
59 /// // Do some work
60 ///
61 /// This class will increment the m_time_xxx variable with the elapsed time
62 /// when the object goes out of scope. The "m_time_xxx" variable will be
63 /// incremented when the class goes out of scope. This allows a variable to
64 /// measure something that might happen in stages at different times, like
65 /// resolving a breakpoint each time a new shared library is loaded.
66 class ElapsedTime {
67 public:
68  /// Set to the start time when the object is created.
70  /// Elapsed time in seconds to increment when this object goes out of scope.
72 
73 public:
74  ElapsedTime(StatsDuration &opt_time) : m_elapsed_time(opt_time) {
75  m_start_time = StatsClock::now();
76  }
78  StatsClock::duration elapsed = StatsClock::now() - m_start_time;
80  }
81 };
82 
83 /// A class to count success/fail statistics.
85  StatsSuccessFail(llvm::StringRef n) : name(n.str()) {}
86 
87  void NotifySuccess() { ++successes; }
88  void NotifyFailure() { ++failures; }
89 
90  llvm::json::Value ToJSON() const;
94 };
95 
96 /// A class that represents statistics for a since lldb_private::Module.
97 struct ModuleStats {
98  llvm::json::Value ToJSON() const;
99  intptr_t identifier;
103  // Path separate debug info file, or empty if none.
105  // If the debug info is contained in multiple files where each one is
106  // represented as a separate lldb_private::Module, then these are the
107  // identifiers of these modules in the global module list. This allows us to
108  // track down all of the stats that contribute to this module.
109  std::vector<intptr_t> symfile_modules;
110  double symtab_parse_time = 0.0;
111  double symtab_index_time = 0.0;
112  double debug_parse_time = 0.0;
113  double debug_index_time = 0.0;
114  uint64_t debug_info_size = 0;
116  bool symtab_saved_to_cache = false;
119  bool debug_info_enabled = true;
120  bool symtab_stripped = false;
121 };
122 
124  llvm::json::Value ToJSON() const;
126 };
127 
128 /// A class that represents statistics for a since lldb_private::Target.
129 class TargetStats {
130 public:
131  llvm::json::Value ToJSON(Target &target);
132 
133  void SetLaunchOrAttachTime();
135  void SetFirstPublicStopTime();
136 
140 
141 protected:
143  llvm::Optional<StatsTimepoint> m_launch_or_attach_time;
144  llvm::Optional<StatsTimepoint> m_first_private_stop_time;
145  llvm::Optional<StatsTimepoint> m_first_public_stop_time;
146  StatsSuccessFail m_expr_eval{"expressionEvaluation"};
147  StatsSuccessFail m_frame_var{"frameVariable"};
148  std::vector<intptr_t> m_module_identifiers;
149  void CollectStats(Target &target);
150 };
151 
153 public:
154  static void SetCollectingStats(bool enable) { g_collecting_stats = enable; }
155  static bool GetCollectingStats() { return g_collecting_stats; }
156 
157  /// Get metrics associated with one or all targets in a debugger in JSON
158  /// format.
159  ///
160  /// \param debugger
161  /// The debugger to get the target list from if \a target is NULL.
162  ///
163  /// \param target
164  /// The single target to emit statistics for if non NULL, otherwise dump
165  /// statistics only for the specified target.
166  ///
167  /// \return
168  /// Returns a JSON value that contains all target metrics.
169  static llvm::json::Value ReportStatistics(Debugger &debugger, Target *target);
170 
171 protected:
172  // Collecting stats can be set to true to collect stats that are expensive
173  // to collect. By default all stats that are cheap to collect are enabled.
174  // This settings is here to maintain compatibility with "statistics enable"
175  // and "statistics disable".
176  static bool g_collecting_stats;
177 };
178 
179 } // namespace lldb_private
180 
181 #endif // LLDB_TARGET_STATISTICS_H
lldb_private::ModuleStats::ToJSON
llvm::json::Value ToJSON() const
Definition: Statistics.cpp:48
lldb_private::StatsSuccessFail::ToJSON
llvm::json::Value ToJSON() const
Definition: Statistics.cpp:32
lldb_private::DebuggerStats::g_collecting_stats
static bool g_collecting_stats
Definition: Statistics.h:176
lldb_private::ModuleStats::debug_info_index_loaded_from_cache
bool debug_info_index_loaded_from_cache
Definition: Statistics.h:117
lldb_private::TargetStats::GetExpressionStats
StatsSuccessFail & GetExpressionStats()
Definition: Statistics.h:138
lldb_private::ModuleStats::debug_info_index_saved_to_cache
bool debug_info_index_saved_to_cache
Definition: Statistics.h:118
lldb_private::DebuggerStats::SetCollectingStats
static void SetCollectingStats(bool enable)
Definition: Statistics.h:154
lldb_private::TargetStats::GetCreateTime
StatsDuration & GetCreateTime()
Definition: Statistics.h:137
lldb_private::StatsDuration::Duration
std::chrono::duration< double > Duration
Definition: Statistics.h:29
lldb_private::TargetStats::m_frame_var
StatsSuccessFail m_frame_var
Definition: Statistics.h:147
lldb_private::ElapsedTime
A class that measures elapsed time in an exception safe way.
Definition: Statistics.h:66
lldb_private::TargetStats
A class that represents statistics for a since lldb_private::Target.
Definition: Statistics.h:129
lldb_private::ModuleStats::symtab_stripped
bool symtab_stripped
Definition: Statistics.h:120
lldb_private::ModuleStats::symtab_saved_to_cache
bool symtab_saved_to_cache
Definition: Statistics.h:116
lldb_private::StatsDuration::operator+=
StatsDuration & operator+=(Duration dur)
Definition: Statistics.h:36
lldb_private::ModuleStats::triple
std::string triple
Definition: Statistics.h:102
lldb_private::ElapsedTime::~ElapsedTime
~ElapsedTime()
Definition: Statistics.h:77
lldb_private::StatsSuccessFail::successes
uint32_t successes
Definition: Statistics.h:92
lldb_private::StatsDuration::InternalDuration
std::chrono::duration< uint64_t, std::micro > InternalDuration
Definition: Statistics.h:43
lldb_private::TargetStats::m_launch_or_attach_time
llvm::Optional< StatsTimepoint > m_launch_or_attach_time
Definition: Statistics.h:143
lldb_private::ConstString::MemoryStats
Definition: ConstString.h:398
lldb_private::ModuleStats::symtab_loaded_from_cache
bool symtab_loaded_from_cache
Definition: Statistics.h:115
lldb_private::StatsDuration
Definition: Statistics.h:27
lldb_private::ModuleStats::identifier
intptr_t identifier
Definition: Statistics.h:99
lldb_private::ModuleStats::debug_parse_time
double debug_parse_time
Definition: Statistics.h:112
lldb_private::Target
Definition: Target.h:469
lldb_private::ConstStringStats
Definition: Statistics.h:123
lldb_private::ModuleStats::symtab_index_time
double symtab_index_time
Definition: Statistics.h:111
elapsed
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
Definition: Statistics.cpp:36
lldb_private::TargetStats::ToJSON
llvm::json::Value ToJSON(Target &target)
Definition: Statistics.cpp:87
lldb_private::ModuleStats
A class that represents statistics for a since lldb_private::Module.
Definition: Statistics.h:97
lldb_private::ModuleStats::symfile_modules
std::vector< intptr_t > symfile_modules
Definition: Statistics.h:109
lldb_private::ModuleStats::symfile_path
std::string symfile_path
Definition: Statistics.h:104
lldb_private::ModuleStats::debug_info_enabled
bool debug_info_enabled
Definition: Statistics.h:119
lldb_private::StatsClock
std::chrono::high_resolution_clock StatsClock
Definition: Statistics.h:24
lldb_private::StatsSuccessFail
A class to count success/fail statistics.
Definition: Statistics.h:84
lldb_private::TargetStats::SetFirstPublicStopTime
void SetFirstPublicStopTime()
Definition: Statistics.cpp:156
lldb_private::Debugger
Definition: Debugger.h:74
lldb_private::TargetStats::m_module_identifiers
std::vector< intptr_t > m_module_identifiers
Definition: Statistics.h:148
lldb_private::ModuleStats::debug_info_size
uint64_t debug_info_size
Definition: Statistics.h:114
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::DebuggerStats::GetCollectingStats
static bool GetCollectingStats()
Definition: Statistics.h:155
lldb_private::DebuggerStats::ReportStatistics
static llvm::json::Value ReportStatistics(Debugger &debugger, Target *target)
Get metrics associated with one or all targets in a debugger in JSON format.
Definition: Statistics.cpp:166
lldb_private::StatsSuccessFail::StatsSuccessFail
StatsSuccessFail(llvm::StringRef n)
Definition: Statistics.h:85
lldb_private::ModuleStats::uuid
std::string uuid
Definition: Statistics.h:101
lldb_private::StatsTimepoint
std::chrono::time_point< StatsClock > StatsTimepoint
Definition: Statistics.h:25
lldb_private::ElapsedTime::m_start_time
StatsTimepoint m_start_time
Set to the start time when the object is created.
Definition: Statistics.h:69
lldb_private::TargetStats::GetFrameVariableStats
StatsSuccessFail & GetFrameVariableStats()
Definition: Statistics.h:139
uint32_t
lldb_private::StatsSuccessFail::name
std::string name
Definition: Statistics.h:91
lldb_private::StatsSuccessFail::NotifyFailure
void NotifyFailure()
Definition: Statistics.h:88
lldb_private::StatsDuration::value
std::atomic< uint64_t > value
Definition: Statistics.h:44
lldb_private::TargetStats::SetFirstPrivateStopTime
void SetFirstPrivateStopTime()
Definition: Statistics.cpp:148
lldb_private::ModuleStats::path
std::string path
Definition: Statistics.h:100
lldb_private::DebuggerStats
Definition: Statistics.h:152
lldb_private::ModuleStats::symtab_parse_time
double symtab_parse_time
Definition: Statistics.h:110
lldb_private::ConstString::GetMemoryStats
static MemoryStats GetMemoryStats()
Definition: ConstString.cpp:329
lldb_private::StatsDuration::get
Duration get() const
Definition: Statistics.h:31
lldb_private::ConstStringStats::stats
ConstString::MemoryStats stats
Definition: Statistics.h:125
lldb_private::ElapsedTime::ElapsedTime
ElapsedTime(StatsDuration &opt_time)
Definition: Statistics.h:74
lldb_private::TargetStats::m_expr_eval
StatsSuccessFail m_expr_eval
Definition: Statistics.h:146
lldb_private::ElapsedTime::m_elapsed_time
StatsDuration & m_elapsed_time
Elapsed time in seconds to increment when this object goes out of scope.
Definition: Statistics.h:71
lldb-forward.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ConstStringStats::ToJSON
llvm::json::Value ToJSON() const
Definition: Statistics.cpp:79
ConstString.h
lldb_private::ModuleStats::debug_index_time
double debug_index_time
Definition: Statistics.h:113
Stream.h
lldb_private::TargetStats::m_first_public_stop_time
llvm::Optional< StatsTimepoint > m_first_public_stop_time
Definition: Statistics.h:145
lldb_private::TargetStats::CollectStats
void CollectStats(Target &target)
Definition: Statistics.cpp:42
lldb_private::StatsSuccessFail::failures
uint32_t failures
Definition: Statistics.h:93
lldb_private::StatsSuccessFail::NotifySuccess
void NotifySuccess()
Definition: Statistics.h:87
lldb_private::TargetStats::m_create_time
StatsDuration m_create_time
Definition: Statistics.h:142
lldb_private::TargetStats::m_first_private_stop_time
llvm::Optional< StatsTimepoint > m_first_private_stop_time
Definition: Statistics.h:144
lldb_private::TargetStats::SetLaunchOrAttachTime
void SetLaunchOrAttachTime()
Definition: Statistics.cpp:143