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