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