Skip to content

Commit 7c13550

Browse files
committed
[TimeProfiler] Fix some style issues. NFC
Reviewed By: broadwaylamb, russell.gallop Differential Revision: https://reviews.llvm.org/D78153
1 parent cd5d5ce commit 7c13550

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

llvm/lib/Support/TimeProfiler.cpp

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Support/TimeProfiler.h"
14+
#include "llvm/ADT/STLExtras.h"
1415
#include "llvm/ADT/StringMap.h"
1516
#include "llvm/Support/CommandLine.h"
1617
#include "llvm/Support/JSON.h"
@@ -26,18 +27,14 @@
2627
using namespace std::chrono;
2728
using namespace llvm;
2829

29-
namespace {
30-
std::mutex Mu;
30+
static std::mutex Mu;
3131
// List of all instances
32-
std::vector<TimeTraceProfiler *>
33-
ThreadTimeTraceProfilerInstances; // guarded by Mu
32+
static std::vector<TimeTraceProfiler *>
33+
ThreadTimeTraceProfilerInstances; // GUARDED_BY(Mu)
3434
// Per Thread instance
35-
LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
36-
} // namespace
37-
38-
namespace llvm {
35+
static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
3936

40-
TimeTraceProfiler *getTimeTraceProfilerInstance() {
37+
TimeTraceProfiler *llvm::getTimeTraceProfilerInstance() {
4138
return TimeTraceProfilerInstance;
4239
}
4340

@@ -47,6 +44,7 @@ typedef std::pair<size_t, DurationType> CountAndDurationType;
4744
typedef std::pair<std::string, CountAndDurationType>
4845
NameAndCountAndDurationType;
4946

47+
namespace {
5048
struct Entry {
5149
const TimePointType Start;
5250
TimePointType End;
@@ -72,8 +70,9 @@ struct Entry {
7270
.count();
7371
}
7472
};
73+
} // namespace
7574

76-
struct TimeTraceProfiler {
75+
struct llvm::TimeTraceProfiler {
7776
TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "")
7877
: StartTime(steady_clock::now()), ProcName(ProcName),
7978
Tid(llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {}
@@ -85,7 +84,7 @@ struct TimeTraceProfiler {
8584

8685
void end() {
8786
assert(!Stack.empty() && "Must call begin() first");
88-
auto &E = Stack.back();
87+
Entry &E = Stack.back();
8988
E.End = steady_clock::now();
9089

9190
// Check that end times monotonically increase.
@@ -120,15 +119,14 @@ struct TimeTraceProfiler {
120119

121120
// Write events from this TimeTraceProfilerInstance and
122121
// ThreadTimeTraceProfilerInstances.
123-
void Write(raw_pwrite_stream &OS) {
122+
void write(raw_pwrite_stream &OS) {
124123
// Acquire Mutex as reading ThreadTimeTraceProfilerInstances.
125124
std::lock_guard<std::mutex> Lock(Mu);
126125
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");
132130

133131
json::OStream J(OS);
134132
J.objectBegin();
@@ -152,22 +150,18 @@ struct TimeTraceProfiler {
152150
}
153151
});
154152
};
155-
for (const auto &E : Entries) {
153+
for (const Entry &E : Entries)
156154
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)
160157
writeEvent(E, TTP->Tid);
161-
}
162-
}
163158

164159
// Emit totals by section name as additional "thread" events, sorted from
165160
// longest one.
166161
// Find highest used thread id.
167162
uint64_t MaxTid = this->Tid;
168-
for (const auto &TTP : ThreadTimeTraceProfilerInstances) {
163+
for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
169164
MaxTid = std::max(MaxTid, TTP->Tid);
170-
}
171165

172166
// Combine all CountAndTotalPerName from threads into one.
173167
StringMap<CountAndDurationType> AllCountAndTotalPerName;
@@ -178,29 +172,25 @@ struct TimeTraceProfiler {
178172
CountAndTotal.first += Value.first;
179173
CountAndTotal.second += Value.second;
180174
};
181-
for (const auto &Stat : CountAndTotalPerName) {
175+
for (const auto &Stat : CountAndTotalPerName)
182176
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)
186179
combineStat(Stat);
187-
}
188-
}
189180

190181
std::vector<NameAndCountAndDurationType> SortedTotals;
191182
SortedTotals.reserve(AllCountAndTotalPerName.size());
192183
for (const auto &Total : AllCountAndTotalPerName)
193184
SortedTotals.emplace_back(std::string(Total.getKey()), Total.getValue());
194185

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+
});
200190

201191
// Report totals on separate threads of tracing file.
202192
uint64_t TotalTid = MaxTid + 1;
203-
for (const auto &Total : SortedTotals) {
193+
for (const NameAndCountAndDurationType &Total : SortedTotals) {
204194
auto DurUs = duration_cast<microseconds>(Total.second.second).count();
205195
auto Count = AllCountAndTotalPerName[Total.first].first;
206196

@@ -247,8 +237,8 @@ struct TimeTraceProfiler {
247237
const unsigned TimeTraceGranularity;
248238
};
249239

250-
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
251-
StringRef ProcName) {
240+
void llvm::timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
241+
StringRef ProcName) {
252242
assert(TimeTraceProfilerInstance == nullptr &&
253243
"Profiler should not be initialized");
254244
TimeTraceProfilerInstance = new TimeTraceProfiler(
@@ -257,7 +247,7 @@ void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
257247

258248
// Removes all TimeTraceProfilerInstances.
259249
// Called from main thread.
260-
void timeTraceProfilerCleanup() {
250+
void llvm::timeTraceProfilerCleanup() {
261251
delete TimeTraceProfilerInstance;
262252
std::lock_guard<std::mutex> Lock(Mu);
263253
for (auto TTP : ThreadTimeTraceProfilerInstances)
@@ -267,20 +257,20 @@ void timeTraceProfilerCleanup() {
267257

268258
// Finish TimeTraceProfilerInstance on a worker thread.
269259
// This doesn't remove the instance, just moves the pointer to global vector.
270-
void timeTraceProfilerFinishThread() {
260+
void llvm::timeTraceProfilerFinishThread() {
271261
std::lock_guard<std::mutex> Lock(Mu);
272262
ThreadTimeTraceProfilerInstances.push_back(TimeTraceProfilerInstance);
273263
TimeTraceProfilerInstance = nullptr;
274264
}
275265

276-
void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
266+
void llvm::timeTraceProfilerWrite(raw_pwrite_stream &OS) {
277267
assert(TimeTraceProfilerInstance != nullptr &&
278268
"Profiler object can't be null");
279-
TimeTraceProfilerInstance->Write(OS);
269+
TimeTraceProfilerInstance->write(OS);
280270
}
281271

282-
Error timeTraceProfilerWrite(StringRef PreferredFileName,
283-
StringRef FallbackFileName) {
272+
Error llvm::timeTraceProfilerWrite(StringRef PreferredFileName,
273+
StringRef FallbackFileName) {
284274
assert(TimeTraceProfilerInstance != nullptr &&
285275
"Profiler object can't be null");
286276

@@ -299,21 +289,19 @@ Error timeTraceProfilerWrite(StringRef PreferredFileName,
299289
return Error::success();
300290
}
301291

302-
void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
292+
void llvm::timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
303293
if (TimeTraceProfilerInstance != nullptr)
304294
TimeTraceProfilerInstance->begin(std::string(Name),
305295
[&]() { return std::string(Detail); });
306296
}
307297

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) {
310300
if (TimeTraceProfilerInstance != nullptr)
311301
TimeTraceProfilerInstance->begin(std::string(Name), Detail);
312302
}
313303

314-
void timeTraceProfilerEnd() {
304+
void llvm::timeTraceProfilerEnd() {
315305
if (TimeTraceProfilerInstance != nullptr)
316306
TimeTraceProfilerInstance->end();
317307
}
318-
319-
} // namespace llvm

0 commit comments

Comments
 (0)