11
11
// ===----------------------------------------------------------------------===//
12
12
13
13
#include " llvm/Support/TimeProfiler.h"
14
+ #include " llvm/ADT/STLExtras.h"
14
15
#include " llvm/ADT/StringMap.h"
15
16
#include " llvm/Support/CommandLine.h"
16
17
#include " llvm/Support/JSON.h"
26
27
using namespace std ::chrono;
27
28
using namespace llvm ;
28
29
29
- namespace {
30
- std::mutex Mu;
30
+ static std::mutex Mu;
31
31
// List of all instances
32
- std::vector<TimeTraceProfiler *>
33
- ThreadTimeTraceProfilerInstances; // guarded by Mu
32
+ static std::vector<TimeTraceProfiler *>
33
+ ThreadTimeTraceProfilerInstances; // GUARDED_BY(Mu)
34
34
// Per Thread instance
35
- LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr ;
36
- } // namespace
37
-
38
- namespace llvm {
35
+ static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr ;
39
36
40
- TimeTraceProfiler *getTimeTraceProfilerInstance () {
37
+ TimeTraceProfiler *llvm:: getTimeTraceProfilerInstance () {
41
38
return TimeTraceProfilerInstance;
42
39
}
43
40
@@ -47,6 +44,7 @@ typedef std::pair<size_t, DurationType> CountAndDurationType;
47
44
typedef std::pair<std::string, CountAndDurationType>
48
45
NameAndCountAndDurationType;
49
46
47
+ namespace {
50
48
struct Entry {
51
49
const TimePointType Start;
52
50
TimePointType End;
@@ -72,8 +70,9 @@ struct Entry {
72
70
.count ();
73
71
}
74
72
};
73
+ } // namespace
75
74
76
- struct TimeTraceProfiler {
75
+ struct llvm :: TimeTraceProfiler {
77
76
TimeTraceProfiler (unsigned TimeTraceGranularity = 0 , StringRef ProcName = " " )
78
77
: StartTime(steady_clock::now()), ProcName(ProcName),
79
78
Tid (llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {}
@@ -85,7 +84,7 @@ struct TimeTraceProfiler {
85
84
86
85
void end () {
87
86
assert (!Stack.empty () && " Must call begin() first" );
88
- auto &E = Stack.back ();
87
+ Entry &E = Stack.back ();
89
88
E.End = steady_clock::now ();
90
89
91
90
// Check that end times monotonically increase.
@@ -120,15 +119,14 @@ struct TimeTraceProfiler {
120
119
121
120
// Write events from this TimeTraceProfilerInstance and
122
121
// ThreadTimeTraceProfilerInstances.
123
- void Write (raw_pwrite_stream &OS) {
122
+ void write (raw_pwrite_stream &OS) {
124
123
// Acquire Mutex as reading ThreadTimeTraceProfilerInstances.
125
124
std::lock_guard<std::mutex> Lock (Mu);
126
125
assert (Stack.empty () &&
127
- " All profiler sections should be ended when calling Write" );
128
- assert (std::all_of (ThreadTimeTraceProfilerInstances.begin (),
129
- ThreadTimeTraceProfilerInstances.end (),
130
- [](const auto &TTP) { return TTP->Stack .empty (); }) &&
131
- " All profiler sections should be ended when calling Write" );
126
+ " All profiler sections should be ended when calling write" );
127
+ assert (llvm::all_of (ThreadTimeTraceProfilerInstances,
128
+ [](const auto &TTP) { return TTP->Stack .empty (); }) &&
129
+ " All profiler sections should be ended when calling write" );
132
130
133
131
json::OStream J (OS);
134
132
J.objectBegin ();
@@ -152,22 +150,18 @@ struct TimeTraceProfiler {
152
150
}
153
151
});
154
152
};
155
- for (const auto &E : Entries) {
153
+ for (const Entry &E : Entries)
156
154
writeEvent (E, this ->Tid );
157
- }
158
- for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
159
- for (const auto &E : TTP->Entries ) {
155
+ for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
156
+ for (const Entry &E : TTP->Entries )
160
157
writeEvent (E, TTP->Tid );
161
- }
162
- }
163
158
164
159
// Emit totals by section name as additional "thread" events, sorted from
165
160
// longest one.
166
161
// Find highest used thread id.
167
162
uint64_t MaxTid = this ->Tid ;
168
- for (const auto & TTP : ThreadTimeTraceProfilerInstances) {
163
+ for (const TimeTraceProfiler * TTP : ThreadTimeTraceProfilerInstances)
169
164
MaxTid = std::max (MaxTid, TTP->Tid );
170
- }
171
165
172
166
// Combine all CountAndTotalPerName from threads into one.
173
167
StringMap<CountAndDurationType> AllCountAndTotalPerName;
@@ -178,29 +172,25 @@ struct TimeTraceProfiler {
178
172
CountAndTotal.first += Value.first ;
179
173
CountAndTotal.second += Value.second ;
180
174
};
181
- for (const auto &Stat : CountAndTotalPerName) {
175
+ for (const auto &Stat : CountAndTotalPerName)
182
176
combineStat (Stat);
183
- }
184
- for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
185
- for (const auto &Stat : TTP->CountAndTotalPerName ) {
177
+ for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
178
+ for (const auto &Stat : TTP->CountAndTotalPerName )
186
179
combineStat (Stat);
187
- }
188
- }
189
180
190
181
std::vector<NameAndCountAndDurationType> SortedTotals;
191
182
SortedTotals.reserve (AllCountAndTotalPerName.size ());
192
183
for (const auto &Total : AllCountAndTotalPerName)
193
184
SortedTotals.emplace_back (std::string (Total.getKey ()), Total.getValue ());
194
185
195
- llvm::sort (SortedTotals.begin (), SortedTotals.end (),
196
- [](const NameAndCountAndDurationType &A,
197
- const NameAndCountAndDurationType &B) {
198
- return A.second .second > B.second .second ;
199
- });
186
+ llvm::sort (SortedTotals, [](const NameAndCountAndDurationType &A,
187
+ const NameAndCountAndDurationType &B) {
188
+ return A.second .second > B.second .second ;
189
+ });
200
190
201
191
// Report totals on separate threads of tracing file.
202
192
uint64_t TotalTid = MaxTid + 1 ;
203
- for (const auto &Total : SortedTotals) {
193
+ for (const NameAndCountAndDurationType &Total : SortedTotals) {
204
194
auto DurUs = duration_cast<microseconds>(Total.second .second ).count ();
205
195
auto Count = AllCountAndTotalPerName[Total.first ].first ;
206
196
@@ -247,8 +237,8 @@ struct TimeTraceProfiler {
247
237
const unsigned TimeTraceGranularity;
248
238
};
249
239
250
- void timeTraceProfilerInitialize (unsigned TimeTraceGranularity,
251
- StringRef ProcName) {
240
+ void llvm:: timeTraceProfilerInitialize (unsigned TimeTraceGranularity,
241
+ StringRef ProcName) {
252
242
assert (TimeTraceProfilerInstance == nullptr &&
253
243
" Profiler should not be initialized" );
254
244
TimeTraceProfilerInstance = new TimeTraceProfiler (
@@ -257,7 +247,7 @@ void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
257
247
258
248
// Removes all TimeTraceProfilerInstances.
259
249
// Called from main thread.
260
- void timeTraceProfilerCleanup () {
250
+ void llvm:: timeTraceProfilerCleanup () {
261
251
delete TimeTraceProfilerInstance;
262
252
std::lock_guard<std::mutex> Lock (Mu);
263
253
for (auto TTP : ThreadTimeTraceProfilerInstances)
@@ -267,20 +257,20 @@ void timeTraceProfilerCleanup() {
267
257
268
258
// Finish TimeTraceProfilerInstance on a worker thread.
269
259
// This doesn't remove the instance, just moves the pointer to global vector.
270
- void timeTraceProfilerFinishThread () {
260
+ void llvm:: timeTraceProfilerFinishThread () {
271
261
std::lock_guard<std::mutex> Lock (Mu);
272
262
ThreadTimeTraceProfilerInstances.push_back (TimeTraceProfilerInstance);
273
263
TimeTraceProfilerInstance = nullptr ;
274
264
}
275
265
276
- void timeTraceProfilerWrite (raw_pwrite_stream &OS) {
266
+ void llvm:: timeTraceProfilerWrite (raw_pwrite_stream &OS) {
277
267
assert (TimeTraceProfilerInstance != nullptr &&
278
268
" Profiler object can't be null" );
279
- TimeTraceProfilerInstance->Write (OS);
269
+ TimeTraceProfilerInstance->write (OS);
280
270
}
281
271
282
- Error timeTraceProfilerWrite (StringRef PreferredFileName,
283
- StringRef FallbackFileName) {
272
+ Error llvm:: timeTraceProfilerWrite (StringRef PreferredFileName,
273
+ StringRef FallbackFileName) {
284
274
assert (TimeTraceProfilerInstance != nullptr &&
285
275
" Profiler object can't be null" );
286
276
@@ -299,21 +289,19 @@ Error timeTraceProfilerWrite(StringRef PreferredFileName,
299
289
return Error::success ();
300
290
}
301
291
302
- void timeTraceProfilerBegin (StringRef Name, StringRef Detail) {
292
+ void llvm:: timeTraceProfilerBegin (StringRef Name, StringRef Detail) {
303
293
if (TimeTraceProfilerInstance != nullptr )
304
294
TimeTraceProfilerInstance->begin (std::string (Name),
305
295
[&]() { return std::string (Detail); });
306
296
}
307
297
308
- void timeTraceProfilerBegin (StringRef Name,
309
- llvm::function_ref<std::string()> Detail) {
298
+ void llvm:: timeTraceProfilerBegin (StringRef Name,
299
+ llvm::function_ref<std::string()> Detail) {
310
300
if (TimeTraceProfilerInstance != nullptr )
311
301
TimeTraceProfilerInstance->begin (std::string (Name), Detail);
312
302
}
313
303
314
- void timeTraceProfilerEnd () {
304
+ void llvm:: timeTraceProfilerEnd () {
315
305
if (TimeTraceProfilerInstance != nullptr )
316
306
TimeTraceProfilerInstance->end ();
317
307
}
318
-
319
- } // namespace llvm
0 commit comments