blob: fae8c15b6e3d53bc4802dca9ae05233858c450e4 [file] [log] [blame]
Anton Afanasyevd880de22019-03-30 08:42:48 +00001//===-- TimeProfiler.cpp - Hierarchical Time Profiler ---------------------===//
2//
Anton Afanasyev26536722019-04-15 21:02:47 +00003// 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 Afanasyevd880de22019-03-30 08:42:48 +00006//
7//===----------------------------------------------------------------------===//
8//
Anton Afanasyev26536722019-04-15 21:02:47 +00009// This file implements hierarchical time profiler.
Anton Afanasyevd880de22019-03-30 08:42:48 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/TimeProfiler.h"
Anton Afanasyev03c3e0d2019-04-09 12:18:44 +000014#include "llvm/ADT/StringMap.h"
Anton Afanasyev26536722019-04-15 21:02:47 +000015#include "llvm/Support/CommandLine.h"
Anton Afanasyevd880de22019-03-30 08:42:48 +000016#include "llvm/Support/FileSystem.h"
Anton Afanasyev6547d512019-04-16 06:35:07 +000017#include "llvm/Support/JSON.h"
Anton Afanasyevd880de22019-03-30 08:42:48 +000018#include <cassert>
19#include <chrono>
20#include <string>
Anton Afanasyevd880de22019-03-30 08:42:48 +000021#include <vector>
22
23using namespace std::chrono;
24
25namespace llvm {
26
27TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
28
Anton Afanasyevd880de22019-03-30 08:42:48 +000029typedef duration<steady_clock::rep, steady_clock::period> DurationType;
Anton Afanasyev03c3e0d2019-04-09 12:18:44 +000030typedef std::pair<size_t, DurationType> CountAndDurationType;
31typedef std::pair<std::string, CountAndDurationType>
32 NameAndCountAndDurationType;
Anton Afanasyevd880de22019-03-30 08:42:48 +000033
34struct Entry {
35 time_point<steady_clock> Start;
36 DurationType Duration;
37 std::string Name;
38 std::string Detail;
Anton Afanasyev26536722019-04-15 21:02:47 +000039
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 Afanasyevd880de22019-03-30 08:42:48 +000044};
45
46struct TimeTraceProfiler {
47 TimeTraceProfiler() {
Anton Afanasyevd880de22019-03-30 08:42:48 +000048 StartTime = steady_clock::now();
49 }
50
51 void begin(std::string Name, llvm::function_ref<std::string()> Detail) {
Anton Afanasyev26536722019-04-15 21:02:47 +000052 Stack.emplace_back(steady_clock::now(), DurationType{}, std::move(Name),
53 Detail());
Anton Afanasyevd880de22019-03-30 08:42:48 +000054 }
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 Afanasyev3f3a2572019-08-19 22:58:26 +000061 // Only include sections longer or equal to TimeTraceGranularity msec.
62 if (duration_cast<microseconds>(E.Duration).count() >= TimeTraceGranularity)
Anton Afanasyevd880de22019-03-30 08:42:48 +000063 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 Afanasyev03c3e0d2019-04-09 12:18:44 +000073 auto &CountAndTotal = CountAndTotalPerName[E.Name];
74 CountAndTotal.first++;
75 CountAndTotal.second += E.Duration;
Anton Afanasyevd880de22019-03-30 08:42:48 +000076 }
77
78 Stack.pop_back();
79 }
80
Anton Afanasyev26536722019-04-15 21:02:47 +000081 void Write(raw_pwrite_stream &OS) {
Anton Afanasyevd880de22019-03-30 08:42:48 +000082 assert(Stack.empty() &&
83 "All profiler sections should be ended when calling Write");
Sam McCalla7edcfb2019-04-25 12:51:42 +000084 json::OStream J(OS);
85 J.objectBegin();
86 J.attributeBegin("traceEvents");
87 J.arrayBegin();
Anton Afanasyevd880de22019-03-30 08:42:48 +000088
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 Afanasyev6547d512019-04-16 06:35:07 +000093
Sam McCalla7edcfb2019-04-25 12:51:42 +000094 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 Afanasyev6547d512019-04-16 06:35:07 +0000102 });
Anton Afanasyevd880de22019-03-30 08:42:48 +0000103 }
104
105 // Emit totals by section name as additional "thread" events, sorted from
106 // longest one.
107 int Tid = 1;
Anton Afanasyev03c3e0d2019-04-09 12:18:44 +0000108 std::vector<NameAndCountAndDurationType> SortedTotals;
109 SortedTotals.reserve(CountAndTotalPerName.size());
Anton Afanasyev26536722019-04-15 21:02:47 +0000110 for (const auto &E : CountAndTotalPerName)
Anton Afanasyev03c3e0d2019-04-09 12:18:44 +0000111 SortedTotals.emplace_back(E.getKey(), E.getValue());
Anton Afanasyev26536722019-04-15 21:02:47 +0000112
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 Afanasyevd880de22019-03-30 08:42:48 +0000118 for (const auto &E : SortedTotals) {
Anton Afanasyev03c3e0d2019-04-09 12:18:44 +0000119 auto DurUs = duration_cast<microseconds>(E.second.second).count();
120 auto Count = CountAndTotalPerName[E.first].first;
Anton Afanasyev6547d512019-04-16 06:35:07 +0000121
Sam McCalla7edcfb2019-04-25 12:51:42 +0000122 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 Afanasyev6547d512019-04-16 06:35:07 +0000133 });
134
Anton Afanasyevd880de22019-03-30 08:42:48 +0000135 ++Tid;
136 }
137
138 // Emit metadata event with process name.
Sam McCalla7edcfb2019-04-25 12:51:42 +0000139 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 Afanasyev6547d512019-04-16 06:35:07 +0000147 });
148
Sam McCalla7edcfb2019-04-25 12:51:42 +0000149 J.arrayEnd();
150 J.attributeEnd();
151 J.objectEnd();
Anton Afanasyevd880de22019-03-30 08:42:48 +0000152 }
153
Anton Afanasyev26536722019-04-15 21:02:47 +0000154 SmallVector<Entry, 16> Stack;
155 SmallVector<Entry, 128> Entries;
Anton Afanasyev03c3e0d2019-04-09 12:18:44 +0000156 StringMap<CountAndDurationType> CountAndTotalPerName;
Anton Afanasyevd880de22019-03-30 08:42:48 +0000157 time_point<steady_clock> StartTime;
Anton Afanasyev4fdcabf2019-07-24 14:55:40 +0000158
159 // Minimum time granularity (in microseconds)
160 unsigned TimeTraceGranularity;
Anton Afanasyevd880de22019-03-30 08:42:48 +0000161};
162
Anton Afanasyev4fdcabf2019-07-24 14:55:40 +0000163void timeTraceProfilerInitialize(unsigned TimeTraceGranularity) {
Anton Afanasyevd880de22019-03-30 08:42:48 +0000164 assert(TimeTraceProfilerInstance == nullptr &&
165 "Profiler should not be initialized");
166 TimeTraceProfilerInstance = new TimeTraceProfiler();
Anton Afanasyev4fdcabf2019-07-24 14:55:40 +0000167 TimeTraceProfilerInstance->TimeTraceGranularity = TimeTraceGranularity;
Anton Afanasyevd880de22019-03-30 08:42:48 +0000168}
169
170void timeTraceProfilerCleanup() {
171 delete TimeTraceProfilerInstance;
172 TimeTraceProfilerInstance = nullptr;
173}
174
Anton Afanasyev26536722019-04-15 21:02:47 +0000175void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
Anton Afanasyevd880de22019-03-30 08:42:48 +0000176 assert(TimeTraceProfilerInstance != nullptr &&
177 "Profiler object can't be null");
178 TimeTraceProfilerInstance->Write(OS);
179}
180
181void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
182 if (TimeTraceProfilerInstance != nullptr)
183 TimeTraceProfilerInstance->begin(Name, [&]() { return Detail; });
184}
185
186void timeTraceProfilerBegin(StringRef Name,
187 llvm::function_ref<std::string()> Detail) {
188 if (TimeTraceProfilerInstance != nullptr)
189 TimeTraceProfilerInstance->begin(Name, Detail);
190}
191
192void timeTraceProfilerEnd() {
193 if (TimeTraceProfilerInstance != nullptr)
194 TimeTraceProfilerInstance->end();
195}
196
197} // namespace llvm