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
base / trace_event / trace_event_impl.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/trace_event_impl.h"
#include <stddef.h>
#include <sstream>
#include "base/format_macros.h"
#include "base/json/string_escape.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/process/process_handle.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_log.h"
#include "base/trace_event/traced_value.h"
#include "build/build_config.h"
// Define static storage for trace event categories (see
// PERFETTO_DEFINE_CATEGORIES).
PERFETTO_TRACK_EVENT_STATIC_STORAGE_IN_NAMESPACE_WITH_ATTRS(base, BASE_EXPORT);
namespace perfetto {
namespace legacy {
template <>
perfetto::ThreadTrack ConvertThreadId(const ::base::PlatformThreadId& thread) {
return perfetto::ThreadTrack::ForThread(thread);
}
} // namespace legacy
TraceTimestamp
TraceTimestampTraits<::base::TimeTicks>::ConvertTimestampToTraceTimeNs(
const ::base::TimeTicks& ticks) {
return {static_cast<uint32_t>(::base::TrackEvent::GetTraceClockId()),
static_cast<uint64_t>(ticks.since_origin().InNanoseconds())};
}
namespace internal {
void WriteDebugAnnotation(protos::pbzero::DebugAnnotation* annotation,
::base::TimeTicks ticks) {
annotation->set_int_value(ticks.since_origin().InMilliseconds());
}
void WriteDebugAnnotation(protos::pbzero::DebugAnnotation* annotation,
::base::Time time) {
annotation->set_int_value(time.since_origin().InMilliseconds());
}
} // namespace internal
} // namespace perfetto
namespace base {
namespace trace_event {
bool ConvertableToTraceFormat::AppendToProto(ProtoAppender* appender) const {
return false;
}
// See comment for name TraceEvent::scope_ definition.
static_assert(trace_event_internal::kGlobalScope == nullptr,
"Invalid TraceEvent::scope default initializer value");
TraceEvent::TraceEvent() = default;
TraceEvent::TraceEvent(PlatformThreadId thread_id,
TimeTicks timestamp,
ThreadTicks thread_timestamp,
char phase,
const unsigned char* category_group_enabled,
const char* name,
const char* scope,
unsigned long long id,
TraceArguments* args,
unsigned int flags)
: timestamp_(timestamp),
thread_timestamp_(thread_timestamp),
scope_(scope),
id_(id),
category_group_enabled_(category_group_enabled),
name_(name),
thread_id_(thread_id),
flags_(flags),
phase_(phase) {
InitArgs(args);
}
TraceEvent::~TraceEvent() = default;
TraceEvent::TraceEvent(TraceEvent&& other) noexcept = default;
TraceEvent& TraceEvent::operator=(TraceEvent&& other) noexcept = default;
void TraceEvent::Reset() {
// Only reset fields that won't be initialized in Reset(int, ...), or that may
// hold references to other objects.
duration_ = TimeDelta::FromInternalValue(-1);
args_.Reset();
parameter_copy_storage_.Reset();
}
void TraceEvent::Reset(PlatformThreadId thread_id,
TimeTicks timestamp,
ThreadTicks thread_timestamp,
char phase,
const unsigned char* category_group_enabled,
const char* name,
const char* scope,
unsigned long long id,
TraceArguments* args,
unsigned int flags) {
Reset();
timestamp_ = timestamp;
thread_timestamp_ = thread_timestamp;
scope_ = scope;
id_ = id;
category_group_enabled_ = category_group_enabled;
name_ = name;
thread_id_ = thread_id;
flags_ = flags;
phase_ = phase;
InitArgs(args);
}
void TraceEvent::InitArgs(TraceArguments* args) {
if (args)
args_ = std::move(*args);
args_.CopyStringsTo(¶meter_copy_storage_,
!!(flags_ & TRACE_EVENT_FLAG_COPY), &name_, &scope_);
}
void TraceEvent::UpdateDuration(const TimeTicks& now,
const ThreadTicks& thread_now) {
DCHECK_EQ(duration_.ToInternalValue(), -1);
duration_ = now - timestamp_;
// |thread_timestamp_| can be empty if the thread ticks clock wasn't
// initialized when it was recorded.
if (thread_timestamp_ != ThreadTicks())
thread_duration_ = thread_now - thread_timestamp_;
}
void TraceEvent::AppendAsJSON(
std::string* out,
const ArgumentFilterPredicate& argument_filter_predicate) const {
int64_t time_int64 = timestamp_.ToInternalValue();
ProcessId process_id;
PlatformThreadId thread_id;
if ((flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID) &&
process_id_ != kNullProcessId) {
process_id = process_id_;
thread_id = static_cast<PlatformThreadId>(-1);
} else {
process_id = TraceLog::GetInstance()->process_id();
thread_id = thread_id_;
}
const char* category_group_name =
TraceLog::GetCategoryGroupName(category_group_enabled_);
// Category group checked at category creation time.
DCHECK(!strchr(name_, '"'));
StringAppendF(out,
"{\"pid\":%i,\"tid\":%i,\"ts\":%" PRId64
",\"ph\":\"%c\",\"cat\":\"%s\",\"name\":",
static_cast<int>(process_id), static_cast<int>(thread_id),
time_int64, phase_, category_group_name);
EscapeJSONString(name_, true, out);
*out += ",\"args\":";
// Output argument names and values, stop at first NULL argument name.
// TODO(oysteine): The dual predicates here is a bit ugly; if the filtering
// capabilities need to grow even more precise we should rethink this
// approach
ArgumentNameFilterPredicate argument_name_filter_predicate;
bool strip_args =
arg_size() > 0 && arg_name(0) && !argument_filter_predicate.is_null() &&
!argument_filter_predicate.Run(category_group_name, name_,
&argument_name_filter_predicate);
if (strip_args) {
*out += "\"__stripped__\"";
} else {
*out += "{";
for (size_t i = 0; i < arg_size() && arg_name(i); ++i) {
if (i > 0)
*out += ",";
*out += "\"";
*out += arg_name(i);
*out += "\":";
if (argument_name_filter_predicate.is_null() ||
argument_name_filter_predicate.Run(arg_name(i))) {
arg_value(i).AppendAsJSON(arg_type(i), out);
} else {
*out += "\"__stripped__\"";
}
}
*out += "}";
}
if (phase_ == TRACE_EVENT_PHASE_COMPLETE) {
int64_t duration = duration_.ToInternalValue();
if (duration != -1)
StringAppendF(out, ",\"dur\":%" PRId64, duration);
if (!thread_timestamp_.is_null()) {
int64_t thread_duration = thread_duration_.ToInternalValue();
if (thread_duration != -1)
StringAppendF(out, ",\"tdur\":%" PRId64, thread_duration);
}
}
// Output tts if thread_timestamp is valid.
if (!thread_timestamp_.is_null()) {
int64_t thread_time_int64 = thread_timestamp_.ToInternalValue();
StringAppendF(out, ",\"tts\":%" PRId64, thread_time_int64);
}
// Output async tts marker field if flag is set.
if (flags_ & TRACE_EVENT_FLAG_ASYNC_TTS) {
StringAppendF(out, ", \"use_async_tts\":1");
}
// If id_ is set, print it out as a hex string so we don't loose any
// bits (it might be a 64-bit pointer).
unsigned int id_flags_ = flags_ & (TRACE_EVENT_FLAG_HAS_ID |
TRACE_EVENT_FLAG_HAS_LOCAL_ID |
TRACE_EVENT_FLAG_HAS_GLOBAL_ID);
if (id_flags_) {
if (scope_ != trace_event_internal::kGlobalScope)
StringAppendF(out, ",\"scope\":\"%s\"", scope_);
switch (id_flags_) {
case TRACE_EVENT_FLAG_HAS_ID:
StringAppendF(out, ",\"id\":\"0x%" PRIx64 "\"",
static_cast<uint64_t>(id_));
break;
case TRACE_EVENT_FLAG_HAS_LOCAL_ID:
StringAppendF(out, ",\"id2\":{\"local\":\"0x%" PRIx64 "\"}",
static_cast<uint64_t>(id_));
break;
case TRACE_EVENT_FLAG_HAS_GLOBAL_ID:
StringAppendF(out, ",\"id2\":{\"global\":\"0x%" PRIx64 "\"}",
static_cast<uint64_t>(id_));
break;
default:
NOTREACHED() << "More than one of the ID flags are set";
}
}
if (flags_ & TRACE_EVENT_FLAG_BIND_TO_ENCLOSING)
StringAppendF(out, ",\"bp\":\"e\"");
if ((flags_ & TRACE_EVENT_FLAG_FLOW_OUT) ||
(flags_ & TRACE_EVENT_FLAG_FLOW_IN)) {
StringAppendF(out, ",\"bind_id\":\"0x%" PRIx64 "\"",
static_cast<uint64_t>(0));
}
if (flags_ & TRACE_EVENT_FLAG_FLOW_IN)
StringAppendF(out, ",\"flow_in\":true");
if (flags_ & TRACE_EVENT_FLAG_FLOW_OUT)
StringAppendF(out, ",\"flow_out\":true");
// Instant events also output their scope.
if (phase_ == TRACE_EVENT_PHASE_INSTANT) {
char scope = '?';
switch (flags_ & TRACE_EVENT_FLAG_SCOPE_MASK) {
case TRACE_EVENT_SCOPE_GLOBAL:
scope = TRACE_EVENT_SCOPE_NAME_GLOBAL;
break;
case TRACE_EVENT_SCOPE_PROCESS:
scope = TRACE_EVENT_SCOPE_NAME_PROCESS;
break;
case TRACE_EVENT_SCOPE_THREAD:
scope = TRACE_EVENT_SCOPE_NAME_THREAD;
break;
}
StringAppendF(out, ",\"s\":\"%c\"", scope);
}
*out += "}";
}
void TraceEvent::AppendPrettyPrinted(std::ostringstream* out) const {
*out << name_ << "[";
*out << TraceLog::GetCategoryGroupName(category_group_enabled_);
*out << "]";
if (arg_size() > 0 && arg_name(0)) {
*out << ", {";
for (size_t i = 0; i < arg_size() && arg_name(i); ++i) {
if (i > 0)
*out << ", ";
*out << arg_name(i) << ":";
std::string value_as_text;
arg_value(i).AppendAsJSON(arg_type(i), &value_as_text);
*out << value_as_text;
}
*out << "}";
}
}
} // namespace trace_event
} // namespace base