forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatistic.cpp
767 lines (705 loc) · 25.5 KB
/
Statistic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//===--- Statistic.cpp - Swift unified stats reporting --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "clang/AST/Decl.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/Timer.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/SIL/SILFunction.h"
#include "swift/Driver/DependencyGraph.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Config/config.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/raw_ostream.h"
#include <chrono>
#include <limits>
#if LLVM_ON_UNIX
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_PROC_PID_RUSAGE
#include <libproc.h>
#endif
#ifdef HAVE_MALLOC_MALLOC_H
#include <malloc/malloc.h>
#endif
namespace swift {
using namespace llvm;
using namespace llvm::sys;
static int64_t
getChildrenMaxResidentSetSize() {
#if defined(HAVE_GETRUSAGE) && !defined(__HAIKU__)
struct rusage RU;
::getrusage(RUSAGE_CHILDREN, &RU);
int64_t M = static_cast<int64_t>(RU.ru_maxrss);
if (M < 0) {
M = std::numeric_limits<int64_t>::max();
} else {
#ifndef __APPLE__
// Apple systems report bytes; everything else appears to report KB.
M <<= 10;
#endif
}
return M;
#else
return 0;
#endif
}
static std::string
makeFileName(StringRef Prefix,
StringRef ProgramName,
StringRef AuxName,
StringRef Suffix) {
std::string tmp;
raw_string_ostream stream(tmp);
auto now = std::chrono::system_clock::now();
auto dur = now.time_since_epoch();
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(dur);
stream << Prefix
<< "-" << usec.count()
<< "-" << ProgramName
<< "-" << AuxName
<< "-" << Process::GetRandomNumber()
<< "." << Suffix;
return stream.str();
}
static std::string
makeStatsFileName(StringRef ProgramName,
StringRef AuxName) {
return makeFileName("stats", ProgramName, AuxName, "json");
}
static std::string
makeTraceFileName(StringRef ProgramName,
StringRef AuxName) {
return makeFileName("trace", ProgramName, AuxName, "csv");
}
static std::string
makeProfileDirName(StringRef ProgramName,
StringRef AuxName) {
return makeFileName("profile", ProgramName, AuxName, "dir");
}
// LLVM's statistics-reporting machinery is sensitive to filenames containing
// YAML-quote-requiring characters, which occur surprisingly often in the wild;
// we only need a recognizable and likely-unique name for a target here, not an
// exact filename, so we go with a crude approximation. Furthermore, to avoid
// parse ambiguities when "demangling" counters and filenames we exclude hyphens
// and slashes.
static std::string
cleanName(StringRef n) {
std::string tmp;
for (auto c : n) {
if (('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
(c == '.'))
tmp += c;
else
tmp += '_';
}
return tmp;
}
static std::string
auxName(StringRef ModuleName,
StringRef InputName,
StringRef TripleName,
StringRef OutputType,
StringRef OptType) {
if (InputName.empty()) {
InputName = "all";
}
// Dispose of path prefix, which might make composite name too long.
InputName = path::filename(InputName);
if (OptType.empty()) {
OptType = "Onone";
}
if (!OutputType.empty() && OutputType.front() == '.') {
OutputType = OutputType.substr(1);
}
if (!OptType.empty() && OptType.front() == '-') {
OptType = OptType.substr(1);
}
return (cleanName(ModuleName)
+ "-" + cleanName(InputName)
+ "-" + cleanName(TripleName)
+ "-" + cleanName(OutputType)
+ "-" + cleanName(OptType));
}
class UnifiedStatsReporter::RecursionSafeTimers {
struct RecursionSafeTimer {
llvm::Optional<SharedTimer> Timer;
size_t RecursionDepth;
};
StringMap<RecursionSafeTimer> Timers;
public:
void beginTimer(StringRef Name) {
RecursionSafeTimer &T = Timers[Name];
if (T.RecursionDepth == 0) {
T.Timer.emplace(Name);
}
T.RecursionDepth++;
}
void endTimer(StringRef Name) {
auto I = Timers.find(Name);
assert(I != Timers.end());
RecursionSafeTimer &T = I->getValue();
assert(T.RecursionDepth != 0);
T.RecursionDepth--;
if (T.RecursionDepth == 0) {
T.Timer.reset();
}
}
};
class StatsProfiler {
struct Node {
int64_t SelfCount;
using Key = std::tuple<StringRef, const void *,
const UnifiedStatsReporter::TraceFormatter *>;
Node *Parent;
DenseMap<Key, std::unique_ptr<Node>> Children;
Node(Node *P=nullptr) : SelfCount(0), Parent(P)
{}
void print(std::vector<Key> &Context, raw_ostream &OS) const {
StringRef delim;
if (!(SelfCount == 0 || Context.empty())) {
for (auto const &K : Context) {
StringRef Name;
const void* Entity;
const UnifiedStatsReporter::TraceFormatter *Formatter;
std::tie(Name, Entity, Formatter) = K;
OS << delim << Name;
if (Formatter && Entity) {
OS << ' ';
Formatter->traceName(Entity, OS);
}
delim = ";";
}
OS << ' ' << SelfCount << '\n';
}
for (auto const &I : Children) {
Context.push_back(I.getFirst());
I.getSecond()->print(Context, OS);
Context.pop_back();
}
}
Node *getChild(StringRef Name,
const void *Entity,
const UnifiedStatsReporter::TraceFormatter *TF) {
Key K(Name, Entity, TF);
auto I = Children.find(K);
if (I != Children.end()) {
return I->getSecond().get();
} else {
auto N = llvm::make_unique<Node>(this);
auto P = N.get();
Children.insert(std::make_pair(K, std::move(N)));
return P;
}
}
};
Node Root;
Node *Curr;
public:
StatsProfiler()
: Curr(&Root)
{}
StatsProfiler(StatsProfiler const &Other) = delete;
StatsProfiler& operator=(const StatsProfiler&) = delete;
void print(raw_ostream &OS) const {
std::vector<Node::Key> Context;
Root.print(Context, OS);
}
void printToFile(StringRef Dirname, StringRef Filename) const {
SmallString<256> Path(Dirname);
llvm::sys::path::append(Path, Filename);
std::error_code EC;
raw_fd_ostream Stream(Path, EC, fs::F_Append | fs::F_Text);
if (EC) {
llvm::errs() << "Error opening profile file '"
<< Path << "' for writing\n";
return;
}
print(Stream);
}
void profileEvent(StringRef Name,
double DeltaSeconds,
bool IsEntry,
const void *Entity=nullptr,
const UnifiedStatsReporter::TraceFormatter *TF=nullptr) {
int64_t DeltaUSec = int64_t(1000000.0 * DeltaSeconds);
profileEvent(Name, DeltaUSec, IsEntry, Entity, TF);
}
void profileEvent(StringRef Name,
int64_t Delta,
bool IsEntry,
const void *Entity=nullptr,
const UnifiedStatsReporter::TraceFormatter *TF=nullptr) {
assert(Curr);
Curr->SelfCount += Delta;
if (IsEntry) {
Node *Child = Curr->getChild(Name, Entity, TF);
assert(Child);
assert(Child->Parent == Curr);
Curr = Child;
} else {
Curr = Curr->Parent;
assert(Curr);
}
}
};
struct UnifiedStatsReporter::StatsProfilers
{
// Timerecord of last update.
llvm::TimeRecord LastUpdated;
// One profiler for each time category.
StatsProfiler UserTime;
StatsProfiler SystemTime;
StatsProfiler ProcessTime;
StatsProfiler WallTime;
// Then one profiler for each frontend statistic.
#define FRONTEND_STATISTIC(TY, NAME) StatsProfiler NAME;
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
StatsProfilers()
: LastUpdated(llvm::TimeRecord::getCurrentTime())
{}
};
UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
StringRef ModuleName,
StringRef InputName,
StringRef TripleName,
StringRef OutputType,
StringRef OptType,
StringRef Directory,
SourceManager *SM,
clang::SourceManager *CSM,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities)
: UnifiedStatsReporter(ProgramName,
auxName(ModuleName,
InputName,
TripleName,
OutputType,
OptType),
Directory,
SM, CSM,
TraceEvents, ProfileEvents, ProfileEntities)
{
}
UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
StringRef AuxName,
StringRef Directory,
SourceManager *SM,
clang::SourceManager *CSM,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities)
: currentProcessExitStatusSet(false),
currentProcessExitStatus(EXIT_FAILURE),
StatsFilename(Directory),
TraceFilename(Directory),
ProfileDirname(Directory),
StartedTime(llvm::TimeRecord::getCurrentTime()),
MainThreadID(std::this_thread::get_id()),
Timer(make_unique<NamedRegionTimer>(AuxName,
"Building Target",
ProgramName, "Running Program")),
SourceMgr(SM),
ClangSourceMgr(CSM),
RecursiveTimers(llvm::make_unique<RecursionSafeTimers>())
{
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
path::append(ProfileDirname, makeProfileDirName(ProgramName, AuxName));
EnableStatistics(/*PrintOnExit=*/false);
SharedTimer::enableCompilationTimers();
if (TraceEvents || ProfileEvents || ProfileEntities)
LastTracedFrontendCounters.emplace();
if (TraceEvents)
FrontendStatsEvents.emplace();
if (ProfileEvents)
EventProfilers = make_unique<StatsProfilers>();
if (ProfileEntities)
EntityProfilers = make_unique<StatsProfilers>();
}
UnifiedStatsReporter::AlwaysOnDriverCounters &
UnifiedStatsReporter::getDriverCounters()
{
if (!DriverCounters)
DriverCounters.emplace();
return *DriverCounters;
}
UnifiedStatsReporter::AlwaysOnFrontendCounters &
UnifiedStatsReporter::getFrontendCounters()
{
if (!FrontendCounters)
FrontendCounters.emplace();
return *FrontendCounters;
}
void
UnifiedStatsReporter::noteCurrentProcessExitStatus(int status) {
assert(MainThreadID == std::this_thread::get_id());
assert(!currentProcessExitStatusSet);
currentProcessExitStatusSet = true;
currentProcessExitStatus = status;
}
void
UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
if (FrontendCounters) {
auto &C = getFrontendCounters();
#define FRONTEND_STATISTIC(TY, NAME) \
do { \
static Statistic Stat = {#TY, #NAME, #NAME, {0}, {false}}; \
Stat += (C).NAME; \
} while (0);
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
}
if (DriverCounters) {
auto &C = getDriverCounters();
#define DRIVER_STATISTIC(NAME) \
do { \
static Statistic Stat = {"Driver", #NAME, #NAME, {0}, {false}}; \
Stat += (C).NAME; \
} while (0);
#include "swift/Basic/Statistics.def"
#undef DRIVER_STATISTIC
}
}
void
UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
// Adapted from llvm::PrintStatisticsJSON
OS << "{\n";
const char *delim = "";
if (FrontendCounters) {
auto &C = getFrontendCounters();
#define FRONTEND_STATISTIC(TY, NAME) \
do { \
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
delim = ",\n"; \
} while (0);
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
}
if (DriverCounters) {
auto &C = getDriverCounters();
#define DRIVER_STATISTIC(NAME) \
do { \
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
delim = ",\n"; \
} while (0);
#include "swift/Basic/Statistics.def"
#undef DRIVER_STATISTIC
}
// Print timers.
TimerGroup::printAllJSONValues(OS, delim);
TimerGroup::clearAll();
OS << "\n}\n";
OS.flush();
}
FrontendStatsTracer::FrontendStatsTracer(
UnifiedStatsReporter *Reporter, StringRef EventName, const void *Entity,
const UnifiedStatsReporter::TraceFormatter *Formatter)
: Reporter(Reporter), SavedTime(), EventName(EventName), Entity(Entity),
Formatter(Formatter) {
if (Reporter) {
SavedTime = llvm::TimeRecord::getCurrentTime();
Reporter->saveAnyFrontendStatsEvents(*this, true);
}
}
FrontendStatsTracer::FrontendStatsTracer() = default;
FrontendStatsTracer&
FrontendStatsTracer::operator=(FrontendStatsTracer&& other)
{
Reporter = other.Reporter;
SavedTime = other.SavedTime;
EventName = other.EventName;
Entity = other.Entity;
Formatter = other.Formatter;
other.Reporter = nullptr;
return *this;
}
FrontendStatsTracer::FrontendStatsTracer(FrontendStatsTracer&& other)
: Reporter(other.Reporter),
SavedTime(other.SavedTime),
EventName(other.EventName),
Entity(other.Entity),
Formatter(other.Formatter)
{
other.Reporter = nullptr;
}
FrontendStatsTracer::~FrontendStatsTracer()
{
if (Reporter)
Reporter->saveAnyFrontendStatsEvents(*this, false);
}
// Copy any interesting process-wide resource accounting stats to
// associated fields in the provided AlwaysOnFrontendCounters.
void updateProcessWideFrontendCounters(
UnifiedStatsReporter::AlwaysOnFrontendCounters &C) {
#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
struct rusage_info_v4 ru;
if (0 == proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru)) {
C.NumInstructionsExecuted = ru.ri_instructions;
}
#endif
#if defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
// On Darwin we have a lifetime max that's maintained by malloc we can
// just directly query, even if we only make one query on shutdown.
malloc_statistics_t Stats;
malloc_zone_statistics(malloc_default_zone(), &Stats);
C.MaxMallocUsage = (int64_t)Stats.max_size_in_use;
#else
// If we don't have a malloc-tracked max-usage counter, we have to rely
// on taking the max over current-usage samples while running and hoping
// we get called often enough. This will happen when profiling/tracing,
// but not while doing single-query-on-shutdown collection.
C.MaxMallocUsage = std::max(C.MaxMallocUsage,
(int64_t)llvm::sys::Process::GetMallocUsage());
#endif
}
static inline void
saveEvent(StringRef StatName,
int64_t Curr, int64_t Last,
uint64_t NowUS, uint64_t LiveUS,
std::vector<UnifiedStatsReporter::FrontendStatsEvent> &Events,
FrontendStatsTracer const& T,
bool IsEntry) {
int64_t Delta = Curr - Last;
if (Delta != 0) {
Events.emplace_back(UnifiedStatsReporter::FrontendStatsEvent{
NowUS, LiveUS, IsEntry, T.EventName, StatName, Delta, Curr,
T.Entity, T.Formatter});
}
}
void
UnifiedStatsReporter::saveAnyFrontendStatsEvents(
FrontendStatsTracer const& T,
bool IsEntry)
{
assert(MainThreadID == std::this_thread::get_id());
// First make a note in the recursion-safe timers; these
// are active anytime UnifiedStatsReporter is active.
if (IsEntry) {
RecursiveTimers->beginTimer(T.EventName);
} else {
RecursiveTimers->endTimer(T.EventName);
}
// If we don't have a saved entry to form deltas against in the trace buffer
// or profilers, we're not tracing or profiling: return early.
if (!LastTracedFrontendCounters)
return;
auto Now = llvm::TimeRecord::getCurrentTime();
auto &Curr = getFrontendCounters();
auto &Last = *LastTracedFrontendCounters;
updateProcessWideFrontendCounters(Curr);
if (EventProfilers) {
auto TimeDelta = Now;
TimeDelta -= EventProfilers->LastUpdated;
EventProfilers->UserTime.profileEvent(T.EventName,
TimeDelta.getUserTime(),
IsEntry);
EventProfilers->SystemTime.profileEvent(T.EventName,
TimeDelta.getSystemTime(),
IsEntry);
EventProfilers->ProcessTime.profileEvent(T.EventName,
TimeDelta.getProcessTime(),
IsEntry);
EventProfilers->WallTime.profileEvent(T.EventName,
TimeDelta.getWallTime(),
IsEntry);
#define FRONTEND_STATISTIC(TY, N) \
EventProfilers->N.profileEvent(T.EventName, Curr.N - Last.N, IsEntry);
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
EventProfilers->LastUpdated = Now;
}
if (EntityProfilers) {
auto TimeDelta = Now;
TimeDelta -= EntityProfilers->LastUpdated;
EntityProfilers->UserTime.profileEvent(T.EventName,
TimeDelta.getUserTime(),
IsEntry, T.Entity, T.Formatter);
EntityProfilers->SystemTime.profileEvent(T.EventName,
TimeDelta.getSystemTime(),
IsEntry, T.Entity, T.Formatter);
EntityProfilers->ProcessTime.profileEvent(T.EventName,
TimeDelta.getProcessTime(),
IsEntry, T.Entity, T.Formatter);
EntityProfilers->WallTime.profileEvent(T.EventName,
TimeDelta.getWallTime(),
IsEntry, T.Entity, T.Formatter);
#define FRONTEND_STATISTIC(TY, N) \
EntityProfilers->N.profileEvent(T.EventName, Curr.N - Last.N, IsEntry, \
T.Entity, T.Formatter);
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
EntityProfilers->LastUpdated = Now;
}
if (FrontendStatsEvents) {
auto StartUS = uint64_t(1000000.0 * T.SavedTime.getProcessTime());
auto NowUS = uint64_t(1000000.0 * Now.getProcessTime());
auto LiveUS = IsEntry ? 0 : NowUS - StartUS;
auto &Events = *FrontendStatsEvents;
#define FRONTEND_STATISTIC(TY, N) \
saveEvent(#TY "." #N, Curr.N, Last.N, NowUS, LiveUS, Events, T, IsEntry);
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
}
// Save all counters (changed or otherwise).
Last = Curr;
}
UnifiedStatsReporter::TraceFormatter::~TraceFormatter() {}
UnifiedStatsReporter::~UnifiedStatsReporter()
{
assert(MainThreadID == std::this_thread::get_id());
// If nobody's marked this process as successful yet,
// mark it as failing.
if (currentProcessExitStatus != EXIT_SUCCESS) {
if (FrontendCounters) {
auto &C = getFrontendCounters();
C.NumProcessFailures++;
} else {
auto &C = getDriverCounters();
C.NumProcessFailures++;
}
}
if (FrontendCounters)
updateProcessWideFrontendCounters(getFrontendCounters());
// NB: Timer needs to be Optional<> because it needs to be destructed early;
// LLVM will complain about double-stopping a timer if you tear down a
// NamedRegionTimer after printing all timers. The printing routines were
// designed with more of a global-scope, run-at-process-exit in mind, which
// we're repurposing a bit here.
Timer.reset();
// We currently do this by manual TimeRecord keeping because LLVM has decided
// not to allow access to the Timers inside NamedRegionTimers.
auto ElapsedTime = llvm::TimeRecord::getCurrentTime();
ElapsedTime -= StartedTime;
if (DriverCounters) {
auto &C = getDriverCounters();
C.ChildrenMaxRSS = getChildrenMaxResidentSetSize();
}
if (FrontendCounters) {
auto &C = getFrontendCounters();
// Convenience calculation for crude top-level "absolute speed".
if (C.NumSourceLines != 0 && ElapsedTime.getProcessTime() != 0.0)
C.NumSourceLinesPerSecond = (int64_t) (((double)C.NumSourceLines) /
ElapsedTime.getProcessTime());
}
std::error_code EC;
raw_fd_ostream ostream(StatsFilename, EC, fs::F_Append | fs::F_Text);
if (EC) {
llvm::errs() << "Error opening -stats-output-dir file '"
<< StatsFilename << "' for writing\n";
return;
}
// We change behavior here depending on whether -DLLVM_ENABLE_STATS and/or
// assertions were on in this build; this is somewhat subtle, but turning on
// all stats for all of LLVM and clang is a bit more expensive and intrusive
// than we want to be in release builds.
//
// - If enabled: we copy all of our "always-on" local stats into LLVM's
// global statistics list, and ask LLVM to manage the printing of them.
//
// - If disabled: we still have our "always-on" local stats to write, and
// LLVM's global _timers_ were still enabled (they're runtime-enabled, not
// compile-time) so we sequence printing our own stats and LLVM's timers
// manually.
#if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
publishAlwaysOnStatsToLLVM();
PrintStatisticsJSON(ostream);
TimerGroup::clearAll();
#else
printAlwaysOnStatsAndTimers(ostream);
#endif
flushTracesAndProfiles();
}
void
UnifiedStatsReporter::flushTracesAndProfiles() {
if (FrontendStatsEvents && SourceMgr) {
std::error_code EC;
raw_fd_ostream tstream(TraceFilename, EC, fs::F_Append | fs::F_Text);
if (EC) {
llvm::errs() << "Error opening -trace-stats-events file '"
<< TraceFilename << "' for writing\n";
return;
}
tstream << "Time,Live,IsEntry,EventName,CounterName,"
<< "CounterDelta,CounterValue,EntityName,EntityRange\n";
for (auto const &E : *FrontendStatsEvents) {
tstream << E.TimeUSec << ','
<< E.LiveUSec << ','
<< (E.IsEntry ? "\"entry\"," : "\"exit\",")
<< '"' << E.EventName << '"' << ','
<< '"' << E.CounterName << '"' << ','
<< E.CounterDelta << ','
<< E.CounterValue << ',';
tstream << '"';
if (E.Formatter)
E.Formatter->traceName(E.Entity, tstream);
tstream << '"' << ',';
tstream << '"';
if (E.Formatter)
E.Formatter->traceLoc(E.Entity, SourceMgr, ClangSourceMgr, tstream);
tstream << '"' << '\n';
}
}
if (EventProfilers || EntityProfilers) {
std::error_code EC = llvm::sys::fs::create_directories(ProfileDirname);
if (EC) {
llvm::errs() << "Failed to create directory '" << ProfileDirname << "': "
<< EC.message() << "\n";
return;
}
if (EventProfilers) {
auto D = ProfileDirname;
EventProfilers->UserTime.printToFile(D, "Time.User.events");
EventProfilers->SystemTime.printToFile(D, "Time.System.events");
EventProfilers->ProcessTime.printToFile(D, "Time.Process.events");
EventProfilers->WallTime.printToFile(D, "Time.Wall.events");
#define FRONTEND_STATISTIC(TY, NAME) \
EventProfilers->NAME.printToFile(ProfileDirname, \
#TY "." #NAME ".events");
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
}
if (EntityProfilers) {
auto D = ProfileDirname;
EntityProfilers->UserTime.printToFile(D, "Time.User.entities");
EntityProfilers->SystemTime.printToFile(D, "Time.System.entities");
EntityProfilers->ProcessTime.printToFile(D, "Time.Process.entities");
EntityProfilers->WallTime.printToFile(D, "Time.Wall.entities");
#define FRONTEND_STATISTIC(TY, NAME) \
EntityProfilers->NAME.printToFile(ProfileDirname, \
#TY "." #NAME ".entities");
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
}
}
LastTracedFrontendCounters.reset();
FrontendStatsEvents.reset();
EventProfilers.reset();
EntityProfilers.reset();
}
} // namespace swift