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 const size_t ExpectedEntryCount = 93 Entries.size() + CountAndTotalPerName.size() + 1; 94 Events.reserve(ExpectedEntryCount); 95 96 // Emit all events for the main flame graph. 97 for (const auto &E : Entries) { 98 auto StartUs = duration_cast<microseconds>(E.Start - StartTime).count(); 99 auto DurUs = duration_cast<microseconds>(E.Duration).count(); 100 101 Events.emplace_back(json::Object{ 102 {"pid", 1}, 103 {"tid", 0}, 104 {"ph", "X"}, 105 {"ts", StartUs}, 106 {"dur", DurUs}, 107 {"name", E.Name}, 108 {"args", json::Object{{"detail", E.Detail}}}, 109 }); 110 } 111 112 // Emit totals by section name as additional "thread" events, sorted from 113 // longest one. 114 int Tid = 1; 115 std::vector<NameAndCountAndDurationType> SortedTotals; 116 SortedTotals.reserve(CountAndTotalPerName.size()); 117 for (const auto &E : CountAndTotalPerName) 118 SortedTotals.emplace_back(E.getKey(), E.getValue()); 119 120 llvm::sort(SortedTotals.begin(), SortedTotals.end(), 121 [](const NameAndCountAndDurationType &A, 122 const NameAndCountAndDurationType &B) { 123 return A.second.second > B.second.second; 124 }); 125 for (const auto &E : SortedTotals) { 126 auto DurUs = duration_cast<microseconds>(E.second.second).count(); 127 auto Count = CountAndTotalPerName[E.first].first; 128 129 Events.emplace_back(json::Object{ 130 {"pid", 1}, 131 {"tid", Tid}, 132 {"ph", "X"}, 133 {"ts", 0}, 134 {"dur", DurUs}, 135 {"name", "Total " + E.first}, 136 {"args", json::Object{{"count", static_cast<int64_t>(Count)}, 137 {"avg ms", 138 static_cast<int64_t>(DurUs / Count / 1000)}}}, 139 }); 140 141 ++Tid; 142 } 143 144 // Emit metadata event with process name. 145 Events.emplace_back(json::Object{ 146 {"cat", ""}, 147 {"pid", 1}, 148 {"tid", 0}, 149 {"ts", 0}, 150 {"ph", "M"}, 151 {"name", "process_name"}, 152 {"args", json::Object{{"name", "clang"}}}, 153 }); 154 155 assert(Events.size() == ExpectedEntryCount && "Size prediction failed!"); 156 157 OS << formatv("{0:2}", json::Value(json::Object( 158 {{"traceEvents", std::move(Events)}}))); 159 } 160 161 SmallVector<Entry, 16> Stack; 162 SmallVector<Entry, 128> Entries; 163 StringMap<CountAndDurationType> CountAndTotalPerName; 164 time_point<steady_clock> StartTime; 165 }; 166 167 void timeTraceProfilerInitialize() { 168 assert(TimeTraceProfilerInstance == nullptr && 169 "Profiler should not be initialized"); 170 TimeTraceProfilerInstance = new TimeTraceProfiler(); 171 } 172 173 void timeTraceProfilerCleanup() { 174 delete TimeTraceProfilerInstance; 175 TimeTraceProfilerInstance = nullptr; 176 } 177 178 void timeTraceProfilerWrite(raw_pwrite_stream &OS) { 179 assert(TimeTraceProfilerInstance != nullptr && 180 "Profiler object can't be null"); 181 TimeTraceProfilerInstance->Write(OS); 182 } 183 184 void timeTraceProfilerBegin(StringRef Name, StringRef Detail) { 185 if (TimeTraceProfilerInstance != nullptr) 186 TimeTraceProfilerInstance->begin(Name, [&]() { return Detail; }); 187 } 188 189 void timeTraceProfilerBegin(StringRef Name, 190 llvm::function_ref<std::string()> Detail) { 191 if (TimeTraceProfilerInstance != nullptr) 192 TimeTraceProfilerInstance->begin(Name, Detail); 193 } 194 195 void timeTraceProfilerEnd() { 196 if (TimeTraceProfilerInstance != nullptr) 197 TimeTraceProfilerInstance->end(); 198 } 199 200 } // namespace llvm 201