1 //===-- TimeProfiler.cpp - Hierarchical Time Profiler ---------------------===// 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 // This file implements hierarchical time profiler. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/TimeProfiler.h" 14 #include "llvm/ADT/StringMap.h" 15 #include "llvm/Support/CommandLine.h" 16 #include "llvm/Support/FileSystem.h" 17 #include "llvm/Support/JSON.h" 18 #include <cassert> 19 #include <chrono> 20 #include <string> 21 #include <vector> 22 23 using namespace std::chrono; 24 25 namespace llvm { 26 27 static cl::opt<unsigned> TimeTraceGranularity( 28 "time-trace-granularity", 29 cl::desc( 30 "Minimum time granularity (in microseconds) traced by time profiler"), 31 cl::init(500)); 32 33 TimeTraceProfiler *TimeTraceProfilerInstance = nullptr; 34 35 typedef duration<steady_clock::rep, steady_clock::period> DurationType; 36 typedef std::pair<size_t, DurationType> CountAndDurationType; 37 typedef std::pair<std::string, CountAndDurationType> 38 NameAndCountAndDurationType; 39 40 struct Entry { 41 time_point<steady_clock> Start; 42 DurationType Duration; 43 std::string Name; 44 std::string Detail; 45 46 Entry(time_point<steady_clock> &&S, DurationType &&D, std::string &&N, 47 std::string &&Dt) 48 : Start(std::move(S)), Duration(std::move(D)), Name(std::move(N)), 49 Detail(std::move(Dt)){}; 50 }; 51 52 struct TimeTraceProfiler { 53 TimeTraceProfiler() { 54 StartTime = steady_clock::now(); 55 } 56 57 void begin(std::string Name, llvm::function_ref<std::string()> Detail) { 58 Stack.emplace_back(steady_clock::now(), DurationType{}, std::move(Name), 59 Detail()); 60 } 61 62 void end() { 63 assert(!Stack.empty() && "Must call begin() first"); 64 auto &E = Stack.back(); 65 E.Duration = steady_clock::now() - E.Start; 66 67 // Only include sections longer than TimeTraceGranularity msec. 68 if (duration_cast<microseconds>(E.Duration).count() > TimeTraceGranularity) 69 Entries.emplace_back(E); 70 71 // Track total time taken by each "name", but only the topmost levels of 72 // them; e.g. if there's a template instantiation that instantiates other 73 // templates from within, we only want to add the topmost one. "topmost" 74 // happens to be the ones that don't have any currently open entries above 75 // itself. 76 if (std::find_if(++Stack.rbegin(), Stack.rend(), [&](const Entry &Val) { 77 return Val.Name == E.Name; 78 }) == Stack.rend()) { 79 auto &CountAndTotal = CountAndTotalPerName[E.Name]; 80 CountAndTotal.first++; 81 CountAndTotal.second += E.Duration; 82 } 83 84 Stack.pop_back(); 85 } 86 87 void Write(raw_pwrite_stream &OS) { 88 assert(Stack.empty() && 89 "All profiler sections should be ended when calling Write"); 90 91 json::Array Events; 92 93 // Emit all events for the main flame graph. 94 for (const auto &E : Entries) { 95 auto StartUs = duration_cast<microseconds>(E.Start - StartTime).count(); 96 auto DurUs = duration_cast<microseconds>(E.Duration).count(); 97 98 Events.emplace_back(json::Object{ 99 {"pid", 1}, 100 {"tid", 0}, 101 {"ph", "X"}, 102 {"ts", StartUs}, 103 {"dur", DurUs}, 104 {"name", E.Name}, 105 {"args", json::Object{{"detail", E.Detail}}}, 106 }); 107 } 108 109 // Emit totals by section name as additional "thread" events, sorted from 110 // longest one. 111 int Tid = 1; 112 std::vector<NameAndCountAndDurationType> SortedTotals; 113 SortedTotals.reserve(CountAndTotalPerName.size()); 114 for (const auto &E : CountAndTotalPerName) 115 SortedTotals.emplace_back(E.getKey(), E.getValue()); 116 117 llvm::sort(SortedTotals.begin(), SortedTotals.end(), 118 [](const NameAndCountAndDurationType &A, 119 const NameAndCountAndDurationType &B) { 120 return A.second.second > B.second.second; 121 }); 122 for (const auto &E : SortedTotals) { 123 auto DurUs = duration_cast<microseconds>(E.second.second).count(); 124 auto Count = CountAndTotalPerName[E.first].first; 125 126 Events.emplace_back(json::Object{ 127 {"pid", 1}, 128 {"tid", Tid}, 129 {"ph", "X"}, 130 {"ts", 0}, 131 {"dur", DurUs}, 132 {"name", "Total " + E.first}, 133 {"args", json::Object{{"count", static_cast<int64_t>(Count)}, 134 {"avg ms", 135 static_cast<int64_t>(DurUs / Count / 1000)}}}, 136 }); 137 138 ++Tid; 139 } 140 141 // Emit metadata event with process name. 142 Events.emplace_back(json::Object{ 143 {"cat", ""}, 144 {"pid", 1}, 145 {"tid", 0}, 146 {"ts", 0}, 147 {"ph", "M"}, 148 {"name", "process_name"}, 149 {"args", json::Object{{"name", "clang"}}}, 150 }); 151 152 OS << formatv("{0:2}", json::Value(json::Object( 153 {{"traceEvents", std::move(Events)}}))); 154 } 155 156 SmallVector<Entry, 16> Stack; 157 SmallVector<Entry, 128> Entries; 158 StringMap<CountAndDurationType> CountAndTotalPerName; 159 time_point<steady_clock> StartTime; 160 }; 161 162 void timeTraceProfilerInitialize() { 163 assert(TimeTraceProfilerInstance == nullptr && 164 "Profiler should not be initialized"); 165 TimeTraceProfilerInstance = new TimeTraceProfiler(); 166 } 167 168 void timeTraceProfilerCleanup() { 169 delete TimeTraceProfilerInstance; 170 TimeTraceProfilerInstance = nullptr; 171 } 172 173 void timeTraceProfilerWrite(raw_pwrite_stream &OS) { 174 assert(TimeTraceProfilerInstance != nullptr && 175 "Profiler object can't be null"); 176 TimeTraceProfilerInstance->Write(OS); 177 } 178 179 void timeTraceProfilerBegin(StringRef Name, StringRef Detail) { 180 if (TimeTraceProfilerInstance != nullptr) 181 TimeTraceProfilerInstance->begin(Name, [&]() { return Detail; }); 182 } 183 184 void timeTraceProfilerBegin(StringRef Name, 185 llvm::function_ref<std::string()> Detail) { 186 if (TimeTraceProfilerInstance != nullptr) 187 TimeTraceProfilerInstance->begin(Name, Detail); 188 } 189 190 void timeTraceProfilerEnd() { 191 if (TimeTraceProfilerInstance != nullptr) 192 TimeTraceProfilerInstance->end(); 193 } 194 195 } // namespace llvm 196