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/Process.h" 15 #include "lldb/Target/Target.h" 16 #include "lldb/Target/UnixSignals.h" 17 18 using namespace lldb; 19 using namespace lldb_private; 20 using namespace llvm; 21 22 static void EmplaceSafeString(llvm::json::Object &obj, llvm::StringRef key, 23 const std::string &str) { 24 if (str.empty()) 25 return; 26 if (LLVM_LIKELY(llvm::json::isUTF8(str))) 27 obj.try_emplace(key, str); 28 else 29 obj.try_emplace(key, llvm::json::fixUTF8(str)); 30 } 31 32 json::Value StatsSuccessFail::ToJSON() const { 33 return json::Object{{"successes", successes}, {"failures", failures}}; 34 } 35 36 static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end) { 37 StatsDuration elapsed = end.time_since_epoch() - start.time_since_epoch(); 38 return elapsed.count(); 39 } 40 41 void TargetStats::CollectStats(Target &target) { 42 m_module_identifiers.clear(); 43 for (ModuleSP module_sp : target.GetImages().Modules()) 44 m_module_identifiers.emplace_back((intptr_t)module_sp.get()); 45 } 46 47 json::Value ModuleStats::ToJSON() const { 48 json::Object module; 49 EmplaceSafeString(module, "path", path); 50 EmplaceSafeString(module, "uuid", uuid); 51 EmplaceSafeString(module, "triple", triple); 52 module.try_emplace("identifier", identifier); 53 module.try_emplace("symbolTableParseTime", symtab_parse_time); 54 module.try_emplace("symbolTableIndexTime", symtab_index_time); 55 module.try_emplace("debugInfoParseTime", debug_parse_time); 56 module.try_emplace("debugInfoIndexTime", debug_index_time); 57 module.try_emplace("debugInfoByteSize", (int64_t)debug_info_size); 58 return module; 59 } 60 61 json::Value TargetStats::ToJSON(Target &target) { 62 CollectStats(target); 63 64 json::Array json_module_uuid_array; 65 for (auto module_identifier : m_module_identifiers) 66 json_module_uuid_array.emplace_back(module_identifier); 67 68 json::Object target_metrics_json{ 69 {m_expr_eval.name, m_expr_eval.ToJSON()}, 70 {m_frame_var.name, m_frame_var.ToJSON()}, 71 {"moduleIdentifiers", std::move(json_module_uuid_array)}}; 72 73 if (m_launch_or_attach_time && m_first_private_stop_time) { 74 double elapsed_time = 75 elapsed(*m_launch_or_attach_time, *m_first_private_stop_time); 76 target_metrics_json.try_emplace("launchOrAttachTime", elapsed_time); 77 } 78 if (m_launch_or_attach_time && m_first_public_stop_time) { 79 double elapsed_time = 80 elapsed(*m_launch_or_attach_time, *m_first_public_stop_time); 81 target_metrics_json.try_emplace("firstStopTime", elapsed_time); 82 } 83 target_metrics_json.try_emplace("targetCreateTime", m_create_time.count()); 84 85 json::Array breakpoints_array; 86 double totalBreakpointResolveTime = 0.0; 87 // Rport both the normal breakpoint list and the internal breakpoint list. 88 for (int i = 0; i < 2; ++i) { 89 BreakpointList &breakpoints = target.GetBreakpointList(i == 1); 90 std::unique_lock<std::recursive_mutex> lock; 91 breakpoints.GetListMutex(lock); 92 size_t num_breakpoints = breakpoints.GetSize(); 93 for (size_t i = 0; i < num_breakpoints; i++) { 94 Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get(); 95 breakpoints_array.push_back(bp->GetStatistics()); 96 totalBreakpointResolveTime += bp->GetResolveTime().count(); 97 } 98 } 99 100 ProcessSP process_sp = target.GetProcessSP(); 101 if (process_sp) { 102 UnixSignalsSP unix_signals_sp = process_sp->GetUnixSignals(); 103 if (unix_signals_sp) 104 target_metrics_json.try_emplace("signals", 105 unix_signals_sp->GetHitCountStatistics()); 106 uint32_t stop_id = process_sp->GetStopID(); 107 target_metrics_json.try_emplace("stopCount", stop_id); 108 } 109 target_metrics_json.try_emplace("breakpoints", std::move(breakpoints_array)); 110 target_metrics_json.try_emplace("totalBreakpointResolveTime", 111 totalBreakpointResolveTime); 112 113 return target_metrics_json; 114 } 115 116 void TargetStats::SetLaunchOrAttachTime() { 117 m_launch_or_attach_time = StatsClock::now(); 118 m_first_private_stop_time = llvm::None; 119 } 120 121 void TargetStats::SetFirstPrivateStopTime() { 122 // Launching and attaching has many paths depending on if synchronous mode 123 // was used or if we are stopping at the entry point or not. Only set the 124 // first stop time if it hasn't already been set. 125 if (!m_first_private_stop_time) 126 m_first_private_stop_time = StatsClock::now(); 127 } 128 129 void TargetStats::SetFirstPublicStopTime() { 130 // Launching and attaching has many paths depending on if synchronous mode 131 // was used or if we are stopping at the entry point or not. Only set the 132 // first stop time if it hasn't already been set. 133 if (!m_first_public_stop_time) 134 m_first_public_stop_time = StatsClock::now(); 135 } 136 137 bool DebuggerStats::g_collecting_stats = false; 138 139 llvm::json::Value DebuggerStats::ReportStatistics(Debugger &debugger, 140 Target *target) { 141 json::Array json_targets; 142 json::Array json_modules; 143 double symtab_parse_time = 0.0; 144 double symtab_index_time = 0.0; 145 double debug_parse_time = 0.0; 146 double debug_index_time = 0.0; 147 uint64_t debug_info_size = 0; 148 if (target) { 149 json_targets.emplace_back(target->ReportStatistics()); 150 } else { 151 for (const auto &target : debugger.GetTargetList().Targets()) 152 json_targets.emplace_back(target->ReportStatistics()); 153 } 154 std::vector<ModuleStats> modules; 155 std::lock_guard<std::recursive_mutex> guard( 156 Module::GetAllocationModuleCollectionMutex()); 157 const size_t num_modules = Module::GetNumberAllocatedModules(); 158 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 159 Module *module = Module::GetAllocatedModuleAtIndex(image_idx); 160 ModuleStats module_stat; 161 module_stat.identifier = (intptr_t)module; 162 module_stat.path = module->GetFileSpec().GetPath(); 163 if (ConstString object_name = module->GetObjectName()) { 164 module_stat.path.append(1, '('); 165 module_stat.path.append(object_name.GetStringRef().str()); 166 module_stat.path.append(1, ')'); 167 } 168 module_stat.uuid = module->GetUUID().GetAsString(); 169 module_stat.triple = module->GetArchitecture().GetTriple().str(); 170 module_stat.symtab_parse_time = module->GetSymtabParseTime().count(); 171 module_stat.symtab_index_time = module->GetSymtabIndexTime().count(); 172 SymbolFile *sym_file = module->GetSymbolFile(); 173 if (sym_file) { 174 module_stat.debug_index_time = sym_file->GetDebugInfoIndexTime().count(); 175 module_stat.debug_parse_time = sym_file->GetDebugInfoParseTime().count(); 176 module_stat.debug_info_size = sym_file->GetDebugInfoSize(); 177 } 178 symtab_parse_time += module_stat.symtab_parse_time; 179 symtab_index_time += module_stat.symtab_index_time; 180 debug_parse_time += module_stat.debug_parse_time; 181 debug_index_time += module_stat.debug_index_time; 182 debug_info_size += module_stat.debug_info_size; 183 json_modules.emplace_back(module_stat.ToJSON()); 184 } 185 186 json::Object global_stats{ 187 {"targets", std::move(json_targets)}, 188 {"modules", std::move(json_modules)}, 189 {"totalSymbolTableParseTime", symtab_parse_time}, 190 {"totalSymbolTableIndexTime", symtab_index_time}, 191 {"totalDebugInfoParseTime", debug_parse_time}, 192 {"totalDebugInfoIndexTime", debug_index_time}, 193 {"totalDebugInfoByteSize", debug_info_size}, 194 }; 195 return std::move(global_stats); 196 } 197