27
27
#include " llvm/Support/YAMLTraits.h"
28
28
#include " llvm/Support/raw_ostream.h"
29
29
#include < limits>
30
+ #include < optional>
30
31
31
32
#if HAVE_UNISTD_H
32
33
#include < unistd.h>
39
40
using namespace llvm ;
40
41
41
42
// ===----------------------------------------------------------------------===//
42
- // Forward declarations for Managed Timer Globals (mtg) getters.
43
+ // Forward declarations for Managed Timer Globals getters.
43
44
//
44
45
// Globals have been placed at the end of the file to restrict direct
45
46
// access. Use of getters also has the benefit of making it a bit more explicit
@@ -49,29 +50,20 @@ namespace {
49
50
class Name2PairMap ;
50
51
}
51
52
52
- namespace mtg {
53
- static std::string &LibSupportInfoOutputFilename ();
54
- static const std::string &InfoOutputFilename ();
55
- static bool TrackSpace ();
56
- static bool SortTimers ();
57
- static SignpostEmitter &Signposts ();
58
- static sys::SmartMutex<true > &TimerLock ();
59
- static TimerGroup &DefaultTimerGroup ();
60
- static TimerGroup *claimDefaultTimerGroup ();
61
- static Name2PairMap &NamedGroupedTimers ();
62
- } // namespace mtg
53
+ static std::string &libSupportInfoOutputFilename ();
54
+ static bool trackSpace ();
55
+ static bool sortTimers ();
56
+ static SignpostEmitter &signposts ();
57
+ static sys::SmartMutex<true > &timerLock ();
58
+ static TimerGroup &defaultTimerGroup ();
59
+ static Name2PairMap &namedGroupedTimers ();
63
60
64
61
// ===----------------------------------------------------------------------===//
65
62
//
66
63
// ===----------------------------------------------------------------------===//
67
- void llvm::initTimerOptions () {
68
- mtg::TrackSpace ();
69
- mtg::InfoOutputFilename ();
70
- mtg::SortTimers ();
71
- }
72
64
73
65
std::unique_ptr<raw_ostream> llvm::CreateInfoOutputFile () {
74
- const std::string &OutputFilename = mtg::LibSupportInfoOutputFilename ();
66
+ const std::string &OutputFilename = libSupportInfoOutputFilename ();
75
67
if (OutputFilename.empty ())
76
68
return std::make_unique<raw_fd_ostream>(2 , false ); // stderr.
77
69
if (OutputFilename == " -" )
@@ -97,7 +89,7 @@ std::unique_ptr<raw_ostream> llvm::CreateInfoOutputFile() {
97
89
// ===----------------------------------------------------------------------===//
98
90
99
91
void Timer::init (StringRef TimerName, StringRef TimerDescription) {
100
- init (TimerName, TimerDescription, mtg::DefaultTimerGroup ());
92
+ init (TimerName, TimerDescription, defaultTimerGroup ());
101
93
}
102
94
103
95
void Timer::init (StringRef TimerName, StringRef TimerDescription,
@@ -116,7 +108,7 @@ Timer::~Timer() {
116
108
}
117
109
118
110
static inline size_t getMemUsage () {
119
- if (!mtg::TrackSpace ())
111
+ if (!trackSpace ())
120
112
return 0 ;
121
113
return sys::Process::GetMallocUsage ();
122
114
}
@@ -157,7 +149,7 @@ TimeRecord TimeRecord::getCurrentTime(bool Start) {
157
149
void Timer::startTimer () {
158
150
assert (!Running && " Cannot start a running timer" );
159
151
Running = Triggered = true ;
160
- mtg::Signposts ().startInterval (this , getName ());
152
+ signposts ().startInterval (this , getName ());
161
153
StartTime = TimeRecord::getCurrentTime (true );
162
154
}
163
155
@@ -166,7 +158,7 @@ void Timer::stopTimer() {
166
158
Running = false ;
167
159
Time += TimeRecord::getCurrentTime (false );
168
160
Time -= StartTime;
169
- mtg::Signposts ().endInterval (this , getName ());
161
+ signposts ().endInterval (this , getName ());
170
162
}
171
163
172
164
void Timer::clear () {
@@ -218,7 +210,7 @@ class Name2PairMap {
218
210
219
211
Timer &get (StringRef Name, StringRef Description, StringRef GroupName,
220
212
StringRef GroupDescription) {
221
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
213
+ sys::SmartScopedLock<true > L (timerLock ());
222
214
223
215
std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
224
216
@@ -237,17 +229,17 @@ class Name2PairMap {
237
229
NamedRegionTimer::NamedRegionTimer (StringRef Name, StringRef Description,
238
230
StringRef GroupName,
239
231
StringRef GroupDescription, bool Enabled)
240
- : TimeRegion(!Enabled ? nullptr
241
- : & mtg::NamedGroupedTimers ().get(Name, Description,
242
- GroupName,
243
- GroupDescription)) {}
232
+ : TimeRegion(!Enabled
233
+ ? nullptr
234
+ : & namedGroupedTimers ().get(Name, Description, GroupName,
235
+ GroupDescription)) {}
244
236
245
237
// ===----------------------------------------------------------------------===//
246
238
// TimerGroup Implementation
247
239
// ===----------------------------------------------------------------------===//
248
240
249
241
// / This is the global list of TimerGroups, maintained by the TimerGroup
250
- // / ctor/dtor and is protected by the TimerLock lock.
242
+ // / ctor/dtor and is protected by the timerLock lock.
251
243
static TimerGroup *TimerGroupList = nullptr ;
252
244
253
245
TimerGroup::TimerGroup (StringRef Name, StringRef Description,
@@ -264,7 +256,7 @@ TimerGroup::TimerGroup(StringRef Name, StringRef Description,
264
256
}
265
257
266
258
TimerGroup::TimerGroup (StringRef Name, StringRef Description)
267
- : TimerGroup(Name, Description, mtg::TimerLock ()) {}
259
+ : TimerGroup(Name, Description, timerLock ()) {}
268
260
269
261
TimerGroup::TimerGroup (StringRef Name, StringRef Description,
270
262
const StringMap<TimeRecord > &Records)
@@ -283,15 +275,15 @@ TimerGroup::~TimerGroup() {
283
275
removeTimer (*FirstTimer);
284
276
285
277
// Remove the group from the TimerGroupList.
286
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
278
+ sys::SmartScopedLock<true > L (timerLock ());
287
279
*Prev = Next;
288
280
if (Next)
289
281
Next->Prev = Prev;
290
282
}
291
283
292
284
293
285
void TimerGroup::removeTimer (Timer &T) {
294
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
286
+ sys::SmartScopedLock<true > L (timerLock ());
295
287
296
288
// If the timer was started, move its data to TimersToPrint.
297
289
if (T.hasTriggered ())
@@ -314,7 +306,7 @@ void TimerGroup::removeTimer(Timer &T) {
314
306
}
315
307
316
308
void TimerGroup::addTimer (Timer &T) {
317
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
309
+ sys::SmartScopedLock<true > L (timerLock ());
318
310
319
311
// Add the timer to our list.
320
312
if (FirstTimer)
@@ -326,7 +318,7 @@ void TimerGroup::addTimer(Timer &T) {
326
318
327
319
void TimerGroup::PrintQueuedTimers (raw_ostream &OS) {
328
320
// Perhaps sort the timers in descending order by amount of time taken.
329
- if (mtg::SortTimers ())
321
+ if (sortTimers ())
330
322
llvm::sort (TimersToPrint);
331
323
332
324
TimeRecord Total;
@@ -344,7 +336,7 @@ void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
344
336
// If this is not an collection of ungrouped times, print the total time.
345
337
// Ungrouped timers don't really make sense to add up. We still print the
346
338
// TOTAL line to make the percentages make sense.
347
- if (this != &mtg::DefaultTimerGroup ())
339
+ if (this != &defaultTimerGroup ())
348
340
OS << format (" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n " ,
349
341
Total.getProcessTime (), Total.getWallTime ());
350
342
OS << ' \n ' ;
@@ -396,7 +388,7 @@ void TimerGroup::prepareToPrintList(bool ResetTime) {
396
388
void TimerGroup::print (raw_ostream &OS, bool ResetAfterPrint) {
397
389
{
398
390
// After preparing the timers we can free the lock
399
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
391
+ sys::SmartScopedLock<true > L (timerLock ());
400
392
prepareToPrintList (ResetAfterPrint);
401
393
}
402
394
@@ -406,20 +398,20 @@ void TimerGroup::print(raw_ostream &OS, bool ResetAfterPrint) {
406
398
}
407
399
408
400
void TimerGroup::clear () {
409
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
401
+ sys::SmartScopedLock<true > L (timerLock ());
410
402
for (Timer *T = FirstTimer; T; T = T->Next )
411
403
T->clear ();
412
404
}
413
405
414
406
void TimerGroup::printAll (raw_ostream &OS) {
415
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
407
+ sys::SmartScopedLock<true > L (timerLock ());
416
408
417
409
for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next )
418
410
TG->print (OS);
419
411
}
420
412
421
413
void TimerGroup::clearAll () {
422
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
414
+ sys::SmartScopedLock<true > L (timerLock ());
423
415
for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next )
424
416
TG->clear ();
425
417
}
@@ -436,7 +428,7 @@ void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
436
428
}
437
429
438
430
const char *TimerGroup::printJSONValues (raw_ostream &OS, const char *delim) {
439
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
431
+ sys::SmartScopedLock<true > L (timerLock ());
440
432
441
433
prepareToPrintList (false );
442
434
for (const PrintRecord &R : TimersToPrint) {
@@ -463,21 +455,12 @@ const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
463
455
}
464
456
465
457
const char *TimerGroup::printAllJSONValues (raw_ostream &OS, const char *delim) {
466
- sys::SmartScopedLock<true > L (mtg::TimerLock ());
458
+ sys::SmartScopedLock<true > L (timerLock ());
467
459
for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next )
468
460
delim = TG->printJSONValues (OS, delim);
469
461
return delim;
470
462
}
471
463
472
- void TimerGroup::constructForStatistics () {
473
- mtg::LibSupportInfoOutputFilename ();
474
- mtg::NamedGroupedTimers ();
475
- }
476
-
477
- std::unique_ptr<TimerGroup> TimerGroup::aquireDefaultGroup () {
478
- return std::unique_ptr<TimerGroup>(mtg::claimDefaultTimerGroup ());
479
- }
480
-
481
464
// ===----------------------------------------------------------------------===//
482
465
// Timer Globals
483
466
//
@@ -499,83 +482,59 @@ std::unique_ptr<TimerGroup> TimerGroup::aquireDefaultGroup() {
499
482
class llvm ::TimerGlobals {
500
483
public:
501
484
std::string LibSupportInfoOutputFilename;
502
- cl::opt<std::string, true > InfoOutputFilename;
503
- cl::opt<bool > TrackSpace;
504
- cl::opt<bool > SortTimers;
485
+ cl::opt<std::string, true > InfoOutputFilename{
486
+ " info-output-file" , cl::value_desc (" filename" ),
487
+ cl::desc (" File to append -stats and -timer output to" ), cl::Hidden,
488
+ cl::location (LibSupportInfoOutputFilename)};
489
+ cl::opt<bool > TrackSpace{
490
+ " track-memory" ,
491
+ cl::desc (" Enable -time-passes memory tracking (this may be slow)" ),
492
+ cl::Hidden};
493
+ cl::opt<bool > SortTimers{
494
+ " sort-timers" ,
495
+ cl::desc (" In the report, sort the timers in each group in wall clock"
496
+ " time order" ),
497
+ cl::init (true ), cl::Hidden};
498
+
499
+ sys::SmartMutex<true > TimerLock;
500
+ TimerGroup DefaultTimerGroup{" misc" , " Miscellaneous Ungrouped Timers" ,
501
+ TimerLock};
502
+ SignpostEmitter Signposts;
505
503
506
- private:
507
504
// Order of these members and initialization below is important. For example
508
- // the DefaultTimerGroup uses the TimerLock . Most of these also depend on the
505
+ // the defaultTimerGroup uses the timerLock . Most of these also depend on the
509
506
// options above.
510
507
std::once_flag InitDeferredFlag;
511
- std::unique_ptr<SignpostEmitter> SignpostsPtr;
512
- std::unique_ptr<sys::SmartMutex<true >> TimerLockPtr;
513
- std::unique_ptr<TimerGroup> DefaultTimerGroupPtr;
514
- std::unique_ptr<Name2PairMap> NamedGroupedTimersPtr;
508
+ std::optional<Name2PairMap> NamedGroupedTimersPtr;
509
+
515
510
TimerGlobals &initDeferred () {
516
- std::call_once (InitDeferredFlag, [this ]() {
517
- SignpostsPtr = std::make_unique<SignpostEmitter>();
518
- TimerLockPtr = std::make_unique<sys::SmartMutex<true >>();
519
- DefaultTimerGroupPtr.reset (new TimerGroup (
520
- " misc" , " Miscellaneous Ungrouped Timers" , *TimerLockPtr));
521
- NamedGroupedTimersPtr = std::make_unique<Name2PairMap>();
522
- });
511
+ std::call_once (InitDeferredFlag,
512
+ [this ]() { NamedGroupedTimersPtr.emplace (); });
523
513
return *this ;
524
514
}
525
-
526
- public:
527
- SignpostEmitter &Signposts () { return *initDeferred ().SignpostsPtr ; }
528
- sys::SmartMutex<true > &TimerLock () { return *initDeferred ().TimerLockPtr ; }
529
- TimerGroup &DefaultTimerGroup () {
530
- return *initDeferred ().DefaultTimerGroupPtr ;
531
- }
532
- TimerGroup *claimDefaultTimerGroup () {
533
- return initDeferred ().DefaultTimerGroupPtr .release ();
534
- }
535
- Name2PairMap &NamedGroupedTimers () {
536
- return *initDeferred ().NamedGroupedTimersPtr ;
537
- }
538
-
539
- public:
540
- TimerGlobals ()
541
- : InfoOutputFilename(
542
- " info-output-file" , cl::value_desc(" filename" ),
543
- cl::desc (" File to append -stats and -timer output to" ), cl::Hidden,
544
- cl::location(LibSupportInfoOutputFilename)),
545
- TrackSpace(
546
- " track-memory" ,
547
- cl::desc (" Enable -time-passes memory tracking (this may be slow)" ),
548
- cl::Hidden),
549
- SortTimers(
550
- " sort-timers" ,
551
- cl::desc (
552
- " In the report, sort the timers in each group in wall clock"
553
- " time order" ),
554
- cl::init(true ), cl::Hidden) {}
555
515
};
556
516
557
517
static ManagedStatic<TimerGlobals> ManagedTimerGlobals;
558
518
559
- static std::string &mtg::LibSupportInfoOutputFilename () {
519
+ static std::string &libSupportInfoOutputFilename () {
560
520
return ManagedTimerGlobals->LibSupportInfoOutputFilename ;
561
521
}
562
- static const std::string &mtg::InfoOutputFilename () {
563
- return ManagedTimerGlobals->InfoOutputFilename .getValue ();
564
- }
565
- static bool mtg::TrackSpace () { return ManagedTimerGlobals->TrackSpace ; }
566
- static bool mtg::SortTimers () { return ManagedTimerGlobals->SortTimers ; }
567
- static SignpostEmitter &mtg::Signposts () {
568
- return ManagedTimerGlobals->Signposts ();
522
+ static bool trackSpace () { return ManagedTimerGlobals->TrackSpace ; }
523
+ static bool sortTimers () { return ManagedTimerGlobals->SortTimers ; }
524
+ static SignpostEmitter &signposts () { return ManagedTimerGlobals->Signposts ; }
525
+ static sys::SmartMutex<true > &timerLock () {
526
+ return ManagedTimerGlobals->TimerLock ;
569
527
}
570
- static sys::SmartMutex< true > & mtg::TimerLock () {
571
- return ManagedTimerGlobals->TimerLock () ;
528
+ static TimerGroup & defaultTimerGroup () {
529
+ return ManagedTimerGlobals->DefaultTimerGroup ;
572
530
}
573
- static TimerGroup & mtg::DefaultTimerGroup () {
574
- return ManagedTimerGlobals->DefaultTimerGroup () ;
531
+ static Name2PairMap & namedGroupedTimers () {
532
+ return * ManagedTimerGlobals->initDeferred (). NamedGroupedTimersPtr ;
575
533
}
576
- static TimerGroup *mtg::claimDefaultTimerGroup () {
577
- return ManagedTimerGlobals->claimDefaultTimerGroup ();
578
- }
579
- static Name2PairMap &mtg::NamedGroupedTimers () {
580
- return ManagedTimerGlobals->NamedGroupedTimers ();
534
+
535
+ void llvm::initTimerOptions () { *ManagedTimerGlobals; }
536
+ void TimerGroup::constructForStatistics () {
537
+ ManagedTimerGlobals->initDeferred ();
581
538
}
539
+
540
+ void *TimerGroup::acquireTimerGlobals () { return ManagedTimerGlobals.claim (); }
0 commit comments