| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "tools/trace/SkPerfettoTrace.h" |
| 9 |
|
| 10 |
#include <fcntl.h> |
| 11 |
#include <fstream> |
| 12 |
#include "src/core/SkTraceEvent.h" |
| 13 |
#include "tools/flags/CommandLineFlags.h" |
| 14 |
|
| 15 |
PERFETTO_TRACK_EVENT_STATIC_STORAGE(); |
| 16 |
|
| 17 |
static DEFINE_string(perfettoOutputDir, "./", |
| 18 |
"Output directory for perfetto trace file(s).\n" |
| 19 |
"Note: not the name of the file itself.\n" |
| 20 |
"Will only have an effect if perfetto tracing is enabled. See --trace."); |
| 21 |
static DEFINE_string(perfettoOutputFileName, "trace", |
| 22 |
"Output file name (excluding path and file extension) for the perfetto trace" |
| 23 |
"file.\nNote: When splitting trace files by benchmark (see " |
| 24 |
"--splitPerfettoTracesByBenchmark), file name will be determined by the " |
| 25 |
"benchmark name.\n" |
| 26 |
"Will only have an effect if perfetto tracing is enabled. See --trace."); |
| 27 |
static DEFINE_string(perfettoOutputFileExtension, ".perfetto-trace", |
| 28 |
"Output file extension for perfetto trace file(s).\n" |
| 29 |
"Will only have an effect if perfetto tracing is enabled. See --trace."); |
| 30 |
static DEFINE_bool(longPerfettoTrace, false, |
| 31 |
"Perfetto within Skia is optimized for tracing performance of 'smaller' traces" |
| 32 |
"(~10 seconds or less). Set this flag to true to optimize for longer tracing" |
| 33 |
"sessions.\n" |
| 34 |
"Will only have an effect if perfetto tracing is enabled. See --trace."); |
| 35 |
|
| 36 |
SkPerfettoTrace::SkPerfettoTrace() { |
| 37 |
fOutputPath = FLAGS_perfettoOutputDir[0]; |
| 38 |
fOutputFileExtension = FLAGS_perfettoOutputFileExtension[0]; |
| 39 |
this->openNewTracingSession(FLAGS_perfettoOutputFileName[0]); |
| 40 |
} |
| 41 |
|
| 42 |
SkPerfettoTrace::~SkPerfettoTrace() { |
| 43 |
this->closeTracingSession(); |
| 44 |
} |
| 45 |
|
| 46 |
void SkPerfettoTrace::openNewTracingSession(const std::string& baseFileName) { |
| 47 |
perfetto::TracingInitArgs args; |
| 48 |
/* Store the current tracing session's output file path as a member attribute so it can |
| 49 |
* be referenced when closing a tracing session (needed for short traces where writing to |
| 50 |
* the output file occurs at the end of all tracing). */ |
| 51 |
fCurrentSessionFullOutputPath = fOutputPath + baseFileName + fOutputFileExtension; |
| 52 |
|
| 53 |
/* Enable using only the in-process backend (recording only within the app itself). This is as |
| 54 |
* opposed to additionally including perfetto::kSystemBackend, which uses a Perfetto daemon. */ |
| 55 |
args.backends |= perfetto::kInProcessBackend; |
| 56 |
|
| 57 |
if (FLAGS_longPerfettoTrace) { |
| 58 |
/* Set the shared memory buffer size higher than the default of 256 KB to |
| 59 |
reduce trace writer packet loss occurrences associated with larger traces. */ |
| 60 |
args.shmem_size_hint_kb = 2000; |
| 61 |
} |
| 62 |
perfetto::Tracing::Initialize(args); |
| 63 |
perfetto::TrackEvent::Register(); |
| 64 |
|
| 65 |
// Set up event tracing configuration. |
| 66 |
perfetto::protos::gen::TrackEventConfig track_event_cfg; |
| 67 |
perfetto::TraceConfig cfg; |
| 68 |
|
| 69 |
/* Set the central memory buffer size - will record up to this amount of data. */ |
| 70 |
cfg.add_buffers()->set_size_kb(32000); |
| 71 |
|
| 72 |
if (FLAGS_longPerfettoTrace) { |
| 73 |
/* Enable continuous file writing/"streaming mode" to output trace data throughout the |
| 74 |
* program instead of one large dump at the end. */ |
| 75 |
cfg.set_write_into_file(true); |
| 76 |
/* If set to a value other than the default, set how often trace data gets written to the |
| 77 |
* output file. */ |
| 78 |
cfg.set_file_write_period_ms(5000); |
| 79 |
/* Force periodic commitment of shared memory buffer pages to the central buffer. |
| 80 |
* Helps prevent out-of-order event slices with long traces. */ |
| 81 |
cfg.set_flush_period_ms(10000); |
| 82 |
} |
| 83 |
|
| 84 |
auto* ds_cfg = cfg.add_data_sources()->mutable_config(); |
| 85 |
ds_cfg->set_name("track_event"); |
| 86 |
ds_cfg->set_track_event_config_raw(track_event_cfg.SerializeAsString()); |
| 87 |
|
| 88 |
// Begin a tracing session. |
| 89 |
tracingSession = perfetto::Tracing::NewTrace(); |
| 90 |
if (FLAGS_longPerfettoTrace) { |
| 91 |
fd = open(fCurrentSessionFullOutputPath.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0600); |
| 92 |
tracingSession->Setup(cfg, fd); |
| 93 |
} else { |
| 94 |
tracingSession->Setup(cfg); |
| 95 |
} |
| 96 |
tracingSession->StartBlocking(); |
| 97 |
} |
| 98 |
|
| 99 |
void SkPerfettoTrace::closeTracingSession() { |
| 100 |
perfetto::TrackEvent::Flush(); |
| 101 |
tracingSession->StopBlocking(); |
| 102 |
if (!FLAGS_longPerfettoTrace) { |
| 103 |
std::vector<char> trace_data(tracingSession->ReadTraceBlocking()); |
| 104 |
std::ofstream output; |
| 105 |
output.open(fCurrentSessionFullOutputPath, std::ios::out | std::ios::binary); |
| 106 |
output.write(&trace_data[0], trace_data.size()); |
| 107 |
output.close(); |
| 108 |
} else { |
| 109 |
close(fd); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
SkEventTracer::Handle SkPerfettoTrace::addTraceEvent(char phase, |
| 114 |
const uint8_t* categoryEnabledFlag, |
| 115 |
const char* name, |
| 116 |
uint64_t id, |
| 117 |
int numArgs, |
| 118 |
const char** argNames, |
| 119 |
const uint8_t* argTypes, |
| 120 |
const uint64_t* argValues, |
| 121 |
uint8_t flags) { |
| 122 |
perfetto::DynamicCategory category{ this->getCategoryGroupName(categoryEnabledFlag) }; |
| 123 |
if (TRACE_EVENT_PHASE_COMPLETE == phase || |
| 124 |
TRACE_EVENT_PHASE_INSTANT == phase) { |
| 125 |
switch (numArgs) { |
| 126 |
case 0: { |
| 127 |
this->triggerTraceEvent(categoryEnabledFlag, name); |
| 128 |
break; |
| 129 |
} |
| 130 |
case 1: { |
| 131 |
this->triggerTraceEvent(categoryEnabledFlag, name, argNames[0], argTypes[0], |
| 132 |
argValues[0]); |
| 133 |
break; |
| 134 |
} |
| 135 |
case 2: { |
| 136 |
this->triggerTraceEvent(categoryEnabledFlag, name, argNames[0], argTypes[0], |
| 137 |
argValues[0], argNames[1], argTypes[1], argValues[1]); |
| 138 |
break; |
| 139 |
} |
| 140 |
} |
| 141 |
} else if (TRACE_EVENT_PHASE_END == phase) { |
| 142 |
TRACE_EVENT_END(category); |
| 143 |
} |
| 144 |
|
| 145 |
if (TRACE_EVENT_PHASE_INSTANT == phase) { |
| 146 |
TRACE_EVENT_END(category); |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
void SkPerfettoTrace::updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| 152 |
const char* name, |
| 153 |
SkEventTracer::Handle handle) { |
| 154 |
// This is only ever called from a scoped trace event, so we will just end the event. |
| 155 |
perfetto::DynamicCategory category{ this->getCategoryGroupName(categoryEnabledFlag) }; |
| 156 |
TRACE_EVENT_END(category); |
| 157 |
} |
| 158 |
|
| 159 |
const uint8_t* SkPerfettoTrace::getCategoryGroupEnabled(const char* name) { |
| 160 |
return fCategories.getCategoryGroupEnabled(name); |
| 161 |
} |
| 162 |
|
| 163 |
const char* SkPerfettoTrace::getCategoryGroupName(const uint8_t* categoryEnabledFlag) { |
| 164 |
return fCategories.getCategoryGroupName(categoryEnabledFlag); |
| 165 |
} |
| 166 |
|
| 167 |
void SkPerfettoTrace::triggerTraceEvent(const uint8_t* categoryEnabledFlag, |
| 168 |
const char* eventName) { |
| 169 |
perfetto::DynamicCategory category{ this->getCategoryGroupName(categoryEnabledFlag) }; |
| 170 |
TRACE_EVENT_BEGIN(category, nullptr, [&](perfetto::EventContext ctx) { |
| 171 |
ctx.event()->set_name(eventName); |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
void SkPerfettoTrace::triggerTraceEvent(const uint8_t* categoryEnabledFlag, const char* eventName, |
| 176 |
const char* arg1Name, const uint8_t& arg1Type, |
| 177 |
const uint64_t& arg1Val) { |
| 178 |
perfetto::DynamicCategory category{ this->getCategoryGroupName(categoryEnabledFlag) }; |
| 179 |
|
| 180 |
switch (arg1Type) { |
| 181 |
case TRACE_VALUE_TYPE_BOOL: { |
| 182 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, SkToBool(arg1Val), |
| 183 |
[&](perfetto::EventContext ctx) { |
| 184 |
ctx.event()->set_name(eventName); }); |
| 185 |
break; |
| 186 |
} |
| 187 |
case TRACE_VALUE_TYPE_UINT: { |
| 188 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, |
| 189 |
[&](perfetto::EventContext ctx) { |
| 190 |
ctx.event()->set_name(eventName); }); |
| 191 |
break; |
| 192 |
} |
| 193 |
case TRACE_VALUE_TYPE_INT: { |
| 194 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, static_cast<int64_t>(arg1Val), |
| 195 |
[&](perfetto::EventContext ctx) { |
| 196 |
ctx.event()->set_name(eventName); }); |
| 197 |
break; |
| 198 |
} |
| 199 |
case TRACE_VALUE_TYPE_DOUBLE: { |
| 200 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, sk_bit_cast<double>(arg1Val), |
| 201 |
[&](perfetto::EventContext ctx) { |
| 202 |
ctx.event()->set_name(eventName); }); |
| 203 |
break; |
| 204 |
} |
| 205 |
case TRACE_VALUE_TYPE_POINTER: { |
| 206 |
TRACE_EVENT_BEGIN(category, nullptr, |
| 207 |
arg1Name, skia_private::TraceValueAsPointer(arg1Val), |
| 208 |
[&](perfetto::EventContext ctx) { |
| 209 |
ctx.event()->set_name(eventName); }); |
| 210 |
break; |
| 211 |
} |
| 212 |
case TRACE_VALUE_TYPE_COPY_STRING: [[fallthrough]]; // Perfetto always copies string data |
| 213 |
case TRACE_VALUE_TYPE_STRING: { |
| 214 |
TRACE_EVENT_BEGIN(category, nullptr, |
| 215 |
arg1Name, skia_private::TraceValueAsString(arg1Val), |
| 216 |
[&](perfetto::EventContext ctx) { |
| 217 |
ctx.event()->set_name(eventName); }); |
| 218 |
break; |
| 219 |
} |
| 220 |
default: { |
| 221 |
SkUNREACHABLE; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
namespace { |
| 227 |
/* Define a template to help handle all the possible TRACE_EVENT_BEGIN macro call |
| 228 |
* combinations with 2 arguments of all the types supported by SetTraceValue. |
| 229 |
*/ |
| 230 |
template <typename T> |
| 231 |
void begin_event_with_second_arg(const char * categoryName, const char* eventName, |
| 232 |
const char* arg1Name, T arg1Val, const char* arg2Name, |
| 233 |
const uint8_t& arg2Type, const uint64_t& arg2Val) { |
| 234 |
perfetto::DynamicCategory category{categoryName}; |
| 235 |
|
| 236 |
switch (arg2Type) { |
| 237 |
case TRACE_VALUE_TYPE_BOOL: { |
| 238 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, arg2Name, SkToBool(arg2Val), |
| 239 |
[&](perfetto::EventContext ctx) { |
| 240 |
ctx.event()->set_name(eventName); }); |
| 241 |
break; |
| 242 |
} |
| 243 |
case TRACE_VALUE_TYPE_UINT: { |
| 244 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, arg2Name, arg2Val, |
| 245 |
[&](perfetto::EventContext ctx) { |
| 246 |
ctx.event()->set_name(eventName); }); |
| 247 |
break; |
| 248 |
} |
| 249 |
case TRACE_VALUE_TYPE_INT: { |
| 250 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, |
| 251 |
arg2Name, static_cast<int64_t>(arg2Val), |
| 252 |
[&](perfetto::EventContext ctx) { |
| 253 |
ctx.event()->set_name(eventName); }); |
| 254 |
break; |
| 255 |
} |
| 256 |
case TRACE_VALUE_TYPE_DOUBLE: { |
| 257 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, |
| 258 |
arg2Name, sk_bit_cast<double>(arg2Val), |
| 259 |
[&](perfetto::EventContext ctx) { |
| 260 |
ctx.event()->set_name(eventName); }); |
| 261 |
break; |
| 262 |
} |
| 263 |
case TRACE_VALUE_TYPE_POINTER: { |
| 264 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, |
| 265 |
arg2Name, skia_private::TraceValueAsPointer(arg2Val), |
| 266 |
[&](perfetto::EventContext ctx) { |
| 267 |
ctx.event()->set_name(eventName); }); |
| 268 |
break; |
| 269 |
} |
| 270 |
case TRACE_VALUE_TYPE_COPY_STRING: [[fallthrough]]; |
| 271 |
case TRACE_VALUE_TYPE_STRING: { |
| 272 |
TRACE_EVENT_BEGIN(category, nullptr, arg1Name, arg1Val, |
| 273 |
arg2Name, skia_private::TraceValueAsString(arg2Val), |
| 274 |
[&](perfetto::EventContext ctx) { |
| 275 |
ctx.event()->set_name(eventName); }); |
| 276 |
break; |
| 277 |
} |
| 278 |
default: { |
| 279 |
SkUNREACHABLE; |
| 280 |
break; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} // anonymous namespace |
| 285 |
|
| 286 |
void SkPerfettoTrace::triggerTraceEvent(const uint8_t* categoryEnabledFlag, |
| 287 |
const char* eventName, const char* arg1Name, |
| 288 |
const uint8_t& arg1Type, const uint64_t& arg1Val, |
| 289 |
const char* arg2Name, const uint8_t& arg2Type, |
| 290 |
const uint64_t& arg2Val) { |
| 291 |
|
| 292 |
const char * category{ this->getCategoryGroupName(categoryEnabledFlag) }; |
| 293 |
|
| 294 |
switch (arg1Type) { |
| 295 |
case TRACE_VALUE_TYPE_BOOL: { |
| 296 |
begin_event_with_second_arg(category, eventName, arg1Name, SkToBool(arg1Val), |
| 297 |
arg2Name, arg2Type, arg2Val); |
| 298 |
break; |
| 299 |
} |
| 300 |
case TRACE_VALUE_TYPE_UINT: { |
| 301 |
begin_event_with_second_arg(category, eventName, arg1Name, arg1Val, |
| 302 |
arg2Name, arg2Type, arg2Val); |
| 303 |
break; |
| 304 |
} |
| 305 |
case TRACE_VALUE_TYPE_INT: { |
| 306 |
begin_event_with_second_arg(category, eventName, |
| 307 |
arg1Name, static_cast<int64_t>(arg1Val), |
| 308 |
arg2Name, arg2Type, arg2Val); |
| 309 |
break; |
| 310 |
} |
| 311 |
case TRACE_VALUE_TYPE_DOUBLE: { |
| 312 |
begin_event_with_second_arg(category, eventName, arg1Name, sk_bit_cast<double>(arg1Val), |
| 313 |
arg2Name, arg2Type, arg2Val); |
| 314 |
break; |
| 315 |
} |
| 316 |
case TRACE_VALUE_TYPE_POINTER: { |
| 317 |
begin_event_with_second_arg(category, eventName, |
| 318 |
arg1Name, skia_private::TraceValueAsPointer(arg1Val), |
| 319 |
arg2Name, arg2Type, arg2Val); |
| 320 |
break; |
| 321 |
} |
| 322 |
case TRACE_VALUE_TYPE_COPY_STRING: [[fallthrough]]; |
| 323 |
case TRACE_VALUE_TYPE_STRING: { |
| 324 |
begin_event_with_second_arg(category, eventName, |
| 325 |
arg1Name, skia_private::TraceValueAsString(arg1Val), |
| 326 |
arg2Name, arg2Type, arg2Val); |
| 327 |
break; |
| 328 |
} |
| 329 |
default: { |
| 330 |
SkUNREACHABLE; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
void SkPerfettoTrace::newTracingSection(const char* name) { |
| 336 |
if (perfetto::Tracing::IsInitialized()) { |
| 337 |
this->closeTracingSession(); |
| 338 |
} |
| 339 |
this->openNewTracingSession(name); |
| 340 |
} |