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 module.try_emplace("debugInfoParseTime", debug_parse_time); 54 module.try_emplace("debugInfoIndexTime", debug_index_time); 55 module.try_emplace("debugInfoByteSize", (int64_t)debug_info_size); 56 return module; 57 } 58 59 json::Value TargetStats::ToJSON(Target &target) { 60 CollectStats(target); 61 62 json::Array json_module_uuid_array; 63 for (auto module_identifier : m_module_identifiers) 64 json_module_uuid_array.emplace_back(module_identifier); 65 66 json::Object target_metrics_json{ 67 {m_expr_eval.name, m_expr_eval.ToJSON()}, 68 {m_frame_var.name, m_frame_var.ToJSON()}, 69 {"moduleIdentifiers", std::move(json_module_uuid_array)}}; 70 71 if (m_launch_or_attach_time && m_first_private_stop_time) { 72 double elapsed_time = 73 elapsed(*m_launch_or_attach_time, *m_first_private_stop_time); 74 target_metrics_json.try_emplace("launchOrAttachTime", elapsed_time); 75 } 76 if (m_launch_or_attach_time && m_first_public_stop_time) { 77 double elapsed_time = 78 elapsed(*m_launch_or_attach_time, *m_first_public_stop_time); 79 target_metrics_json.try_emplace("firstStopTime", elapsed_time); 80 } 81 target_metrics_json.try_emplace("targetCreateTime", m_create_time.count()); 82 83 json::Array breakpoints_array; 84 double totalBreakpointResolveTime = 0.0; 85 // Rport both the normal breakpoint list and the internal breakpoint list. 86 for (int i = 0; i < 2; ++i) { 87 BreakpointList &breakpoints = target.GetBreakpointList(i == 1); 88 std::unique_lock<std::recursive_mutex> lock; 89 breakpoints.GetListMutex(lock); 90 size_t num_breakpoints = breakpoints.GetSize(); 91 for (size_t i = 0; i < num_breakpoints; i++) { 92 Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get(); 93 breakpoints_array.push_back(bp->GetStatistics()); 94 totalBreakpointResolveTime += bp->GetResolveTime().count(); 95 } 96 } 97 98 target_metrics_json.try_emplace("breakpoints", std::move(breakpoints_array)); 99 target_metrics_json.try_emplace("totalBreakpointResolveTime", 100 totalBreakpointResolveTime); 101 102 return target_metrics_json; 103 } 104 105 void TargetStats::SetLaunchOrAttachTime() { 106 m_launch_or_attach_time = StatsClock::now(); 107 m_first_private_stop_time = llvm::None; 108 } 109 110 void TargetStats::SetFirstPrivateStopTime() { 111 // Launching and attaching has many paths depending on if synchronous mode 112 // was used or if we are stopping at the entry point or not. Only set the 113 // first stop time if it hasn't already been set. 114 if (!m_first_private_stop_time) 115 m_first_private_stop_time = StatsClock::now(); 116 } 117 118 void TargetStats::SetFirstPublicStopTime() { 119 // Launching and attaching has many paths depending on if synchronous mode 120 // was used or if we are stopping at the entry point or not. Only set the 121 // first stop time if it hasn't already been set. 122 if (!m_first_public_stop_time) 123 m_first_public_stop_time = StatsClock::now(); 124 } 125 126 bool DebuggerStats::g_collecting_stats = false; 127 128 llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger, 129 Target *target) { 130 json::Array json_targets; 131 json::Array json_modules; 132 double symtab_parse_time = 0.0; 133 double symtab_index_time = 0.0; 134 double debug_parse_time = 0.0; 135 double debug_index_time = 0.0; 136 uint64_t debug_info_size = 0; 137 if (target) { 138 json_targets.emplace_back(target->ReportStatistics()); 139 } else { 140 for (const auto &target : debugger.GetTargetList().Targets()) 141 json_targets.emplace_back(target->ReportStatistics()); 142 } 143 std::vector<ModuleStats> modules; 144 std::lock_guard<std::recursive_mutex> guard( 145 Module::GetAllocationModuleCollectionMutex()); 146 const size_t num_modules = Module::GetNumberAllocatedModules(); 147 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 148 Module *module = Module::GetAllocatedModuleAtIndex(image_idx); 149 ModuleStats module_stat; 150 module_stat.identifier = (intptr_t)module; 151 module_stat.path = module->GetFileSpec().GetPath(); 152 if (ConstString object_name = module->GetObjectName()) { 153 module_stat.path.append(1, '('); 154 module_stat.path.append(object_name.GetStringRef().str()); 155 module_stat.path.append(1, ')'); 156 } 157 module_stat.uuid = module->GetUUID().GetAsString(); 158 module_stat.triple = module->GetArchitecture().GetTriple().str(); 159 module_stat.symtab_parse_time = module->GetSymtabParseTime().count(); 160 module_stat.symtab_index_time = module->GetSymtabIndexTime().count(); 161 SymbolFile *sym_file = module->GetSymbolFile(); 162 if (sym_file) { 163 module_stat.debug_index_time = sym_file->GetDebugInfoIndexTime().count(); 164 module_stat.debug_parse_time = sym_file->GetDebugInfoParseTime().count(); 165 module_stat.debug_info_size = sym_file->GetDebugInfoSize(); 166 } 167 symtab_parse_time += module_stat.symtab_parse_time; 168 symtab_index_time += module_stat.symtab_index_time; 169 debug_parse_time += module_stat.debug_parse_time; 170 debug_index_time += module_stat.debug_index_time; 171 debug_info_size += module_stat.debug_info_size; 172 json_modules.emplace_back(module_stat.ToJSON()); 173 } 174 175 json::Object global_stats{ 176 {"targets", std::move(json_targets)}, 177 {"modules", std::move(json_modules)}, 178 {"totalSymbolTableParseTime", symtab_parse_time}, 179 {"totalSymbolTableIndexTime", symtab_index_time}, 180 {"totalDebugInfoParseTime", debug_parse_time}, 181 {"totalDebugInfoIndexTime", debug_index_time}, 182 {"totalDebugInfoByteSize", debug_info_size}, 183 }; 184 return std::move(global_stats); 185 } 186