@@ -30,7 +30,7 @@ namespace {
30
30
31
31
using std::chrono::duration;
32
32
using std::chrono::duration_cast;
33
- using std::chrono::microseconds ;
33
+ using std::chrono::nanoseconds ;
34
34
using std::chrono::steady_clock;
35
35
using std::chrono::system_clock;
36
36
using std::chrono::time_point;
@@ -76,18 +76,18 @@ struct TimeTraceProfilerEntry {
76
76
: Start(std::move(S)), End(std::move(E)), Name(std::move(N)),
77
77
Detail (std::move(Dt)) {}
78
78
79
- // Calculate timings for FlameGraph. Cast time points to microsecond precision
79
+ // Calculate timings for FlameGraph. Cast time points to nanosecond precision
80
80
// rather than casting duration. This avoids truncation issues causing inner
81
81
// scopes overruning outer scopes.
82
- ClockType::rep getFlameGraphStartUs (TimePointType StartTime) const {
83
- return (time_point_cast<microseconds >(Start) -
84
- time_point_cast<microseconds >(StartTime))
82
+ ClockType::rep getFlameGraphStartNs (TimePointType StartTime) const {
83
+ return (time_point_cast<nanoseconds >(Start) -
84
+ time_point_cast<nanoseconds >(StartTime))
85
85
.count ();
86
86
}
87
87
88
- ClockType::rep getFlameGraphDurUs () const {
89
- return (time_point_cast<microseconds >(End) -
90
- time_point_cast<microseconds >(Start))
88
+ ClockType::rep getFlameGraphDurNs () const {
89
+ return (time_point_cast<nanoseconds >(End) -
90
+ time_point_cast<nanoseconds >(Start))
91
91
.count ();
92
92
}
93
93
};
@@ -114,16 +114,16 @@ struct llvm::TimeTraceProfiler {
114
114
115
115
// Check that end times monotonically increase.
116
116
assert ((Entries.empty () ||
117
- (E.getFlameGraphStartUs (StartTime) + E.getFlameGraphDurUs () >=
118
- Entries.back ().getFlameGraphStartUs (StartTime) +
119
- Entries.back ().getFlameGraphDurUs ())) &&
117
+ (E.getFlameGraphStartNs (StartTime) + E.getFlameGraphDurNs () >=
118
+ Entries.back ().getFlameGraphStartNs (StartTime) +
119
+ Entries.back ().getFlameGraphDurNs ())) &&
120
120
" TimeProfiler scope ended earlier than previous scope" );
121
121
122
122
// Calculate duration at full precision for overall counts.
123
123
DurationType Duration = E.End - E.Start ;
124
124
125
125
// Only include sections longer or equal to TimeTraceGranularity msec.
126
- if (duration_cast<microseconds >(Duration).count () >= TimeTraceGranularity)
126
+ if (duration_cast<nanoseconds >(Duration).count () >= TimeTraceGranularity)
127
127
Entries.emplace_back (E);
128
128
129
129
// Track total time taken by each "name", but only the topmost levels of
@@ -162,15 +162,15 @@ struct llvm::TimeTraceProfiler {
162
162
163
163
// Emit all events for the main flame graph.
164
164
auto writeEvent = [&](const auto &E, uint64_t Tid) {
165
- auto StartUs = E.getFlameGraphStartUs (StartTime);
166
- auto DurUs = E.getFlameGraphDurUs ();
165
+ auto StartNs = E.getFlameGraphStartNs (StartTime);
166
+ auto DurNs = E.getFlameGraphDurNs ();
167
167
168
168
J.object ([&] {
169
169
J.attribute (" pid" , Pid);
170
170
J.attribute (" tid" , int64_t (Tid));
171
171
J.attribute (" ph" , " X" );
172
- J.attribute (" ts" , StartUs );
173
- J.attribute (" dur" , DurUs );
172
+ J.attribute (" ts" , StartNs / 1000 );
173
+ J.attribute (" dur" , DurNs / 1000 );
174
174
J.attribute (" name" , E.Name );
175
175
if (!E.Detail .empty ()) {
176
176
J.attributeObject (" args" , [&] { J.attribute (" detail" , E.Detail ); });
@@ -218,19 +218,19 @@ struct llvm::TimeTraceProfiler {
218
218
// Report totals on separate threads of tracing file.
219
219
uint64_t TotalTid = MaxTid + 1 ;
220
220
for (const NameAndCountAndDurationType &Total : SortedTotals) {
221
- auto DurUs = duration_cast<microseconds >(Total.second .second ).count ();
221
+ auto DurNs = duration_cast<nanoseconds >(Total.second .second ).count ();
222
222
auto Count = AllCountAndTotalPerName[Total.first ].first ;
223
223
224
224
J.object ([&] {
225
225
J.attribute (" pid" , Pid);
226
226
J.attribute (" tid" , int64_t (TotalTid));
227
227
J.attribute (" ph" , " X" );
228
228
J.attribute (" ts" , 0 );
229
- J.attribute (" dur" , DurUs );
230
- J.attribute (" name" , " Total " + Total.first );
229
+ J.attribute (" dur" , DurNs / 1000 );
230
+ J.attribute (" name" , " Total: " + Total.first );
231
231
J.attributeObject (" args" , [&] {
232
232
J.attribute (" count" , int64_t (Count));
233
- J.attribute (" avg ms " , int64_t (DurUs / Count / 1000 ));
233
+ J.attribute (" avg us " , int64_t (DurNs / Count / 1000 ));
234
234
});
235
235
});
236
236
@@ -261,10 +261,10 @@ struct llvm::TimeTraceProfiler {
261
261
// Emit the absolute time when this TimeProfiler started.
262
262
// This can be used to combine the profiling data from
263
263
// multiple processes and preserve actual time intervals.
264
- J.attribute (" beginningOfTime" ,
265
- time_point_cast<microseconds>(BeginningOfTime )
266
- . time_since_epoch ()
267
- . count () );
264
+ J.attribute (" beginningOfTime" , time_point_cast<nanoseconds>(BeginningOfTime)
265
+ . time_since_epoch ( )
266
+ . count () /
267
+ 1000 );
268
268
269
269
J.objectEnd ();
270
270
}
@@ -281,7 +281,7 @@ struct llvm::TimeTraceProfiler {
281
281
SmallString<0 > ThreadName;
282
282
const uint64_t Tid;
283
283
284
- // Minimum time granularity (in microseconds )
284
+ // Minimum time granularity (in nanoseconds )
285
285
const unsigned TimeTraceGranularity;
286
286
};
287
287
0 commit comments