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 12 #include "lldb/DataFormatters/TypeSummary.h" 13 #include "lldb/Utility/ConstString.h" 14 #include "lldb/Utility/RealpathPrefixes.h" 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 27 namespace lldb_private { 28 29 using StatsClock = std::chrono::high_resolution_clock; 30 using StatsTimepoint = std::chrono::time_point<StatsClock>; 31 class SummaryStatistics; 32 // Declaring here as there is no private forward 33 typedef std::shared_ptr<SummaryStatistics> SummaryStatisticsSP; 34 35 class StatsDuration { 36 public: 37 using Duration = std::chrono::duration<double>; 38 get()39 Duration get() const { 40 return Duration(InternalDuration(value.load(std::memory_order_relaxed))); 41 } Duration()42 operator Duration() const { return get(); } 43 reset()44 void reset() { value.store(0, std::memory_order_relaxed); } 45 46 StatsDuration &operator+=(Duration dur) { 47 value.fetch_add(std::chrono::duration_cast<InternalDuration>(dur).count(), 48 std::memory_order_relaxed); 49 return *this; 50 } 51 52 private: 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. 76 class ElapsedTime { 77 public: 78 /// Set to the start time when the object is created. 79 StatsTimepoint m_start_time; 80 /// Elapsed time in seconds to increment when this object goes out of scope. 81 StatsDuration &m_elapsed_time; 82 83 public: ElapsedTime(StatsDuration & opt_time)84 ElapsedTime(StatsDuration &opt_time) : m_elapsed_time(opt_time) { 85 m_start_time = StatsClock::now(); 86 } ~ElapsedTime()87 ~ElapsedTime() { 88 StatsClock::duration elapsed = StatsClock::now() - m_start_time; 89 m_elapsed_time += elapsed; 90 } 91 }; 92 93 /// A class to count time for plugins 94 class StatisticsMap { 95 public: add(llvm::StringRef key,double value)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 } merge(StatisticsMap map_to_merge)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. 114 struct StatsSuccessFail { StatsSuccessFailStatsSuccessFail115 StatsSuccessFail(llvm::StringRef n) : name(n.str()) {} 116 NotifySuccessStatsSuccessFail117 void NotifySuccess() { ++successes; } NotifyFailureStatsSuccessFail118 void NotifyFailure() { ++failures; } 119 120 llvm::json::Value ToJSON() const; 121 std::string name; 122 uint32_t successes = 0; 123 uint32_t failures = 0; 124 }; 125 126 /// A class that represents statistics for a since lldb_private::Module. 127 struct ModuleStats { 128 llvm::json::Value ToJSON() const; 129 intptr_t identifier; 130 std::string path; 131 std::string uuid; 132 std::string triple; 133 // Path separate debug info file, or empty if none. 134 std::string symfile_path; 135 // If the debug info is contained in multiple files where each one is 136 // represented as a separate lldb_private::Module, then these are the 137 // identifiers of these modules in the global module list. This allows us to 138 // track down all of the stats that contribute to this module. 139 std::vector<intptr_t> symfile_modules; 140 llvm::StringMap<llvm::json::Value> type_system_stats; 141 StatisticsMap symbol_locator_time; 142 double symtab_parse_time = 0.0; 143 double symtab_index_time = 0.0; 144 uint32_t symtab_symbol_count = 0; 145 double debug_parse_time = 0.0; 146 double debug_index_time = 0.0; 147 uint64_t debug_info_size = 0; 148 bool symtab_loaded_from_cache = false; 149 bool symtab_saved_to_cache = false; 150 bool debug_info_index_loaded_from_cache = false; 151 bool debug_info_index_saved_to_cache = false; 152 bool debug_info_enabled = true; 153 bool symtab_stripped = false; 154 bool debug_info_had_variable_errors = false; 155 bool debug_info_had_incomplete_types = false; 156 uint32_t dwo_file_count = 0; 157 uint32_t loaded_dwo_file_count = 0; 158 }; 159 160 struct ConstStringStats { 161 llvm::json::Value ToJSON() const; 162 ConstString::MemoryStats stats = ConstString::GetMemoryStats(); 163 }; 164 165 struct StatisticsOptions { 166 public: SetSummaryOnlyStatisticsOptions167 void SetSummaryOnly(bool value) { m_summary_only = value; } GetSummaryOnlyStatisticsOptions168 bool GetSummaryOnly() const { return m_summary_only.value_or(false); } 169 SetLoadAllDebugInfoStatisticsOptions170 void SetLoadAllDebugInfo(bool value) { m_load_all_debug_info = value; } GetLoadAllDebugInfoStatisticsOptions171 bool GetLoadAllDebugInfo() const { 172 return m_load_all_debug_info.value_or(false); 173 } 174 SetIncludeTargetsStatisticsOptions175 void SetIncludeTargets(bool value) { m_include_targets = value; } GetIncludeTargetsStatisticsOptions176 bool GetIncludeTargets() const { 177 if (m_include_targets.has_value()) 178 return m_include_targets.value(); 179 // Default to true in both default mode and summary mode. 180 return true; 181 } 182 SetIncludeModulesStatisticsOptions183 void SetIncludeModules(bool value) { m_include_modules = value; } GetIncludeModulesStatisticsOptions184 bool GetIncludeModules() const { 185 if (m_include_modules.has_value()) 186 return m_include_modules.value(); 187 // `m_include_modules` has no value set, so return a value based on 188 // `m_summary_only`. 189 return !GetSummaryOnly(); 190 } 191 SetIncludeTranscriptStatisticsOptions192 void SetIncludeTranscript(bool value) { m_include_transcript = value; } GetIncludeTranscriptStatisticsOptions193 bool GetIncludeTranscript() const { 194 return m_include_transcript.value_or(false); 195 } 196 SetIncludePluginsStatisticsOptions197 void SetIncludePlugins(bool value) { m_include_plugins = value; } GetIncludePluginsStatisticsOptions198 bool GetIncludePlugins() const { 199 if (m_include_plugins.has_value()) 200 return m_include_plugins.value(); 201 // Default to true in both default mode and summary mode. 202 return true; 203 } 204 205 private: 206 std::optional<bool> m_summary_only; 207 std::optional<bool> m_load_all_debug_info; 208 std::optional<bool> m_include_targets; 209 std::optional<bool> m_include_modules; 210 std::optional<bool> m_include_transcript; 211 std::optional<bool> m_include_plugins; 212 }; 213 214 /// A class that represents statistics about a TypeSummaryProviders invocations 215 /// \note All members of this class need to be accessed in a thread safe manner 216 class SummaryStatistics { 217 public: SummaryStatistics(std::string name,std::string impl_type)218 explicit SummaryStatistics(std::string name, std::string impl_type) 219 : m_total_time(), m_impl_type(std::move(impl_type)), 220 m_name(std::move(name)), m_count(0) {} 221 GetName()222 std::string GetName() const { return m_name; }; GetTotalTime()223 double GetTotalTime() const { return m_total_time.get().count(); } 224 GetSummaryCount()225 uint64_t GetSummaryCount() const { 226 return m_count.load(std::memory_order_relaxed); 227 } 228 GetDurationReference()229 StatsDuration &GetDurationReference() { return m_total_time; }; 230 GetSummaryKindName()231 std::string GetSummaryKindName() const { return m_impl_type; } 232 233 llvm::json::Value ToJSON() const; 234 Reset()235 void Reset() { m_total_time.reset(); } 236 237 /// Basic RAII class to increment the summary count when the call is complete. 238 class SummaryInvocation { 239 public: SummaryInvocation(SummaryStatisticsSP summary_stats)240 SummaryInvocation(SummaryStatisticsSP summary_stats) 241 : m_stats(summary_stats), 242 m_elapsed_time(summary_stats->GetDurationReference()) {} ~SummaryInvocation()243 ~SummaryInvocation() { m_stats->OnInvoked(); } 244 245 /// Delete the copy constructor and assignment operator to prevent 246 /// accidental double counting. 247 /// @{ 248 SummaryInvocation(const SummaryInvocation &) = delete; 249 SummaryInvocation &operator=(const SummaryInvocation &) = delete; 250 /// @} 251 252 private: 253 SummaryStatisticsSP m_stats; 254 ElapsedTime m_elapsed_time; 255 }; 256 257 private: OnInvoked()258 void OnInvoked() noexcept { m_count.fetch_add(1, std::memory_order_relaxed); } 259 lldb_private::StatsDuration m_total_time; 260 const std::string m_impl_type; 261 const std::string m_name; 262 std::atomic<uint64_t> m_count; 263 }; 264 265 /// A class that wraps a std::map of SummaryStatistics objects behind a mutex. 266 class SummaryStatisticsCache { 267 public: 268 /// Get the SummaryStatistics object for a given provider name, or insert 269 /// if statistics for that provider is not in the map. 270 SummaryStatisticsSP GetSummaryStatisticsForProvider(lldb_private::TypeSummaryImpl & provider)271 GetSummaryStatisticsForProvider(lldb_private::TypeSummaryImpl &provider) { 272 std::lock_guard<std::mutex> guard(m_map_mutex); 273 if (auto iterator = m_summary_stats_map.find(provider.GetName()); 274 iterator != m_summary_stats_map.end()) 275 return iterator->second; 276 277 auto it = m_summary_stats_map.try_emplace( 278 provider.GetName(), 279 std::make_shared<SummaryStatistics>(provider.GetName(), 280 provider.GetSummaryKindName())); 281 return it.first->second; 282 } 283 284 llvm::json::Value ToJSON(); 285 286 void Reset(); 287 288 private: 289 llvm::StringMap<SummaryStatisticsSP> m_summary_stats_map; 290 std::mutex m_map_mutex; 291 }; 292 293 /// A class that represents statistics for a since lldb_private::Target. 294 class TargetStats { 295 public: 296 llvm::json::Value ToJSON(Target &target, 297 const lldb_private::StatisticsOptions &options); 298 299 void SetLaunchOrAttachTime(); 300 void SetFirstPrivateStopTime(); 301 void SetFirstPublicStopTime(); 302 void IncreaseSourceMapDeduceCount(); 303 void IncreaseSourceRealpathAttemptCount(uint32_t count); 304 void IncreaseSourceRealpathCompatibleCount(uint32_t count); 305 GetCreateTime()306 StatsDuration &GetCreateTime() { return m_create_time; } GetExpressionStats()307 StatsSuccessFail &GetExpressionStats() { return m_expr_eval; } GetFrameVariableStats()308 StatsSuccessFail &GetFrameVariableStats() { return m_frame_var; } 309 void Reset(Target &target); 310 311 protected: 312 StatsDuration m_create_time; 313 std::optional<StatsTimepoint> m_launch_or_attach_time; 314 std::optional<StatsTimepoint> m_first_private_stop_time; 315 std::optional<StatsTimepoint> m_first_public_stop_time; 316 StatsSuccessFail m_expr_eval{"expressionEvaluation"}; 317 StatsSuccessFail m_frame_var{"frameVariable"}; 318 std::vector<intptr_t> m_module_identifiers; 319 uint32_t m_source_map_deduce_count = 0; 320 uint32_t m_source_realpath_attempt_count = 0; 321 uint32_t m_source_realpath_compatible_count = 0; 322 void CollectStats(Target &target); 323 }; 324 325 class DebuggerStats { 326 public: SetCollectingStats(bool enable)327 static void SetCollectingStats(bool enable) { g_collecting_stats = enable; } GetCollectingStats()328 static bool GetCollectingStats() { return g_collecting_stats; } 329 330 /// Get metrics associated with one or all targets in a debugger in JSON 331 /// format. 332 /// 333 /// \param debugger 334 /// The debugger to get the target list from if \a target is NULL. 335 /// 336 /// \param target 337 /// The single target to emit statistics for if non NULL, otherwise dump 338 /// statistics only for the specified target. 339 /// 340 /// \param summary_only 341 /// If true, only report high level summary statistics without 342 /// targets/modules/breakpoints etc.. details. 343 /// 344 /// \return 345 /// Returns a JSON value that contains all target metrics. 346 static llvm::json::Value 347 ReportStatistics(Debugger &debugger, Target *target, 348 const lldb_private::StatisticsOptions &options); 349 350 /// Reset metrics associated with one or all targets in a debugger. 351 /// 352 /// \param debugger 353 /// The debugger to reset the target list from if \a target is NULL. 354 /// 355 /// \param target 356 /// The target to reset statistics for, or if null, reset statistics 357 /// for all targets 358 static void ResetStatistics(Debugger &debugger, Target *target); 359 360 protected: 361 // Collecting stats can be set to true to collect stats that are expensive 362 // to collect. By default all stats that are cheap to collect are enabled. 363 // This settings is here to maintain compatibility with "statistics enable" 364 // and "statistics disable". 365 static bool g_collecting_stats; 366 }; 367 368 } // namespace lldb_private 369 370 #endif // LLDB_TARGET_STATISTICS_H 371