1 //===-- Statistics.cpp ----------------------------------------------------===// 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 #include "lldb/Target/Statistics.h" 10 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Symbol/SymbolFile.h" 14 #include "lldb/Target/Target.h" 15 16 using namespace lldb; 17 using namespace lldb_private; 18 using namespace llvm; 19 20 static void EmplaceSafeString(llvm::json::Object &obj, llvm::StringRef key, 21 const std::string &str) { 22 if (str.empty()) 23 return; 24 if (LLVM_LIKELY(llvm::json::isUTF8(str))) 25 obj.try_emplace(key, str); 26 else 27 obj.try_emplace(key, llvm::json::fixUTF8(str)); 28 } 29 30 json::Value StatsSuccessFail::ToJSON() const { 31 return json::Object{{"successes", successes}, {"failures", failures}}; 32 } 33 34 static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end) { 35 StatsDuration elapsed = end.time_since_epoch() - start.time_since_epoch(); 36 return elapsed.count(); 37 } 38 39 void TargetStats::CollectStats(Target &target) { 40 m_module_identifiers.clear(); 41 for (ModuleSP module_sp : target.GetImages().Modules()) 42 m_module_identifiers.emplace_back((intptr_t)module_sp.get()); 43 } 44 45 json::Value ModuleStats::ToJSON() const { 46 json::Object module; 47 EmplaceSafeString(module, "path", path); 48 EmplaceSafeString(module, "uuid", uuid); 49 EmplaceSafeString(module, "triple", triple); 50 module.try_emplace("identifier", identifier); 51 module.try_emplace("symbolTableParseTime", symtab_parse_time); 52 module.try_emplace("symbolTableIndexTime", symtab_index_time); 53 return module; 54 } 55 56 json::Value TargetStats::ToJSON(Target &target) { 57 CollectStats(target); 58 59 json::Array json_module_uuid_array; 60 for (auto module_identifier : m_module_identifiers) 61 json_module_uuid_array.emplace_back(module_identifier); 62 63 json::Object target_metrics_json{ 64 {m_expr_eval.name, m_expr_eval.ToJSON()}, 65 {m_frame_var.name, m_frame_var.ToJSON()}, 66 {"moduleIdentifiers", std::move(json_module_uuid_array)}}; 67 68 if (m_launch_or_attach_time && m_first_private_stop_time) { 69 double elapsed_time = 70 elapsed(*m_launch_or_attach_time, *m_first_private_stop_time); 71 target_metrics_json.try_emplace("launchOrAttachTime", elapsed_time); 72 } 73 if (m_launch_or_attach_time && m_first_public_stop_time) { 74 double elapsed_time = 75 elapsed(*m_launch_or_attach_time, *m_first_public_stop_time); 76 target_metrics_json.try_emplace("firstStopTime", elapsed_time); 77 } 78 target_metrics_json.try_emplace("targetCreateTime", m_create_time.count()); 79 80 return target_metrics_json; 81 } 82 83 void TargetStats::SetLaunchOrAttachTime() { 84 m_launch_or_attach_time = StatsClock::now(); 85 m_first_private_stop_time = llvm::None; 86 } 87 88 void TargetStats::SetFirstPrivateStopTime() { 89 // Launching and attaching has many paths depending on if synchronous mode 90 // was used or if we are stopping at the entry point or not. Only set the 91 // first stop time if it hasn't already been set. 92 if (!m_first_private_stop_time) 93 m_first_private_stop_time = StatsClock::now(); 94 } 95 96 void TargetStats::SetFirstPublicStopTime() { 97 // Launching and attaching has many paths depending on if synchronous mode 98 // was used or if we are stopping at the entry point or not. Only set the 99 // first stop time if it hasn't already been set. 100 if (!m_first_public_stop_time) 101 m_first_public_stop_time = StatsClock::now(); 102 } 103 104 bool DebuggerStats::g_collecting_stats = false; 105 106 llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger, 107 Target *target) { 108 json::Array json_targets; 109 json::Array json_modules; 110 double symtab_parse_time = 0.0; 111 double symtab_index_time = 0.0; 112 if (target) { 113 json_targets.emplace_back(target->ReportStatistics()); 114 } else { 115 for (const auto &target : debugger.GetTargetList().Targets()) 116 json_targets.emplace_back(target->ReportStatistics()); 117 } 118 std::vector<ModuleStats> modules; 119 std::lock_guard<std::recursive_mutex> guard( 120 Module::GetAllocationModuleCollectionMutex()); 121 const size_t num_modules = Module::GetNumberAllocatedModules(); 122 ModuleSP module_sp; 123 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 124 Module *module = Module::GetAllocatedModuleAtIndex(image_idx); 125 ModuleStats module_stat; 126 module_stat.identifier = (intptr_t)module; 127 module_stat.path = module->GetFileSpec().GetPath(); 128 module_stat.uuid = module->GetUUID().GetAsString(); 129 module_stat.triple = module->GetArchitecture().GetTriple().str(); 130 module_stat.symtab_parse_time = module->GetSymtabParseTime().count(); 131 module_stat.symtab_index_time = module->GetSymtabIndexTime().count(); 132 symtab_parse_time += module_stat.symtab_parse_time; 133 symtab_index_time += module_stat.symtab_index_time; 134 json_modules.emplace_back(module_stat.ToJSON()); 135 } 136 137 json::Object global_stats{ 138 {"targets", std::move(json_targets)}, 139 {"modules", std::move(json_modules)}, 140 {"totalSymbolTableParseTime", symtab_parse_time}, 141 {"totalSymbolTableIndexTime", symtab_index_time}, 142 }; 143 return std::move(global_stats); 144 } 145