| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 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/EventTracingPriv.h" |
| 9 |
|
| 10 |
#include "include/utils/SkEventTracer.h" |
| 11 |
#include "src/core/SkATrace.h" |
| 12 |
#include "src/core/SkTraceEvent.h" |
| 13 |
#include "tools/flags/CommandLineFlags.h" |
| 14 |
#include "tools/trace/ChromeTracingTracer.h" |
| 15 |
#include "tools/trace/SkDebugfTracer.h" |
| 16 |
|
| 17 |
// SkPerfettoTrace is only relevant when Perfetto is requested, and for in-process tracing. It is |
| 18 |
// incompatible with the alternate "direct macro override" approach to using Perfetto, which is |
| 19 |
// currently used for SK_BUILD_FOR_ANDROID_FRAMEWORK. Skia's Perfetto integration is currently in |
| 20 |
// in a transitionary period, see go/skia-perfetto for details. |
| 21 |
#if defined(SK_USE_PERFETTO) |
| 22 |
#if defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO) |
| 23 |
#error "SK_USE_PERFETTO and SK_ANDROID_FRAMEWORK_USE_PERFETTO are mutually exclusive" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "tools/trace/SkPerfettoTrace.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
static DEFINE_string(trace, |
| 30 |
"", |
| 31 |
"Log trace events in one of several modes:\n" |
| 32 |
" debugf : Show events using SkDebugf\n" |
| 33 |
" atrace : Send events to Android ATrace\n" |
| 34 |
" perfetto : Send events to Perfetto (Linux, Android, and Mac only)\n" |
| 35 |
" <filename> : Any other string is interpreted as a filename. Writes\n" |
| 36 |
" trace events to specified file as JSON, for viewing\n" |
| 37 |
" with chrome://tracing"); |
| 38 |
|
| 39 |
static DEFINE_string(traceMatch, |
| 40 |
"", |
| 41 |
"Filter which categories are traced.\n" |
| 42 |
"Uses same format as --match\n"); |
| 43 |
|
| 44 |
void initializeEventTracingForTools(const char* traceFlag) { |
| 45 |
if (!traceFlag) { |
| 46 |
if (FLAGS_trace.isEmpty()) { |
| 47 |
return; |
| 48 |
} |
| 49 |
traceFlag = FLAGS_trace[0]; |
| 50 |
} |
| 51 |
|
| 52 |
SkEventTracer* eventTracer = nullptr; |
| 53 |
if (0 == strcmp(traceFlag, "atrace")) { |
| 54 |
eventTracer = new SkATrace(); |
| 55 |
} else if (0 == strcmp(traceFlag, "debugf")) { |
| 56 |
eventTracer = new SkDebugfTracer(); |
| 57 |
} else if (0 == strcmp(traceFlag, "perfetto")) { |
| 58 |
#if defined(SK_USE_PERFETTO) |
| 59 |
eventTracer = new SkPerfettoTrace(); |
| 60 |
#else |
| 61 |
// TODO(b/259248961): update this explanation (and associated docs) as the Perfetto |
| 62 |
// transition progresses. |
| 63 |
SkDebugf("Perfetto is not enabled (SK_USE_PERFETTO is false). Perfetto tracing will not " |
| 64 |
"be performed.\nTracing tools with Perfetto is only enabled for Linux, Android, " |
| 65 |
"and Mac.\n"); |
| 66 |
return; |
| 67 |
#endif |
| 68 |
} |
| 69 |
else { |
| 70 |
eventTracer = new ChromeTracingTracer(traceFlag); |
| 71 |
} |
| 72 |
|
| 73 |
SkAssertResult(SkEventTracer::SetInstance(eventTracer)); |
| 74 |
} |
| 75 |
|
| 76 |
uint8_t* SkEventTracingCategories::getCategoryGroupEnabled(const char* name) { |
| 77 |
static_assert(0 == offsetof(CategoryState, fEnabled), "CategoryState"); |
| 78 |
|
| 79 |
// We ignore the "disabled-by-default-" prefix in our internal tools |
| 80 |
if (SkStrStartsWith(name, TRACE_CATEGORY_PREFIX)) { |
| 81 |
name += strlen(TRACE_CATEGORY_PREFIX); |
| 82 |
} |
| 83 |
|
| 84 |
// Chrome's implementation of this API does a two-phase lookup (once without a lock, then again |
| 85 |
// with a lock. But the tracing macros avoid calling these functions more than once per site, |
| 86 |
// so just do something simple (and easier to reason about): |
| 87 |
SkAutoMutexExclusive lock(fMutex); |
| 88 |
for (int i = 0; i < fNumCategories; ++i) { |
| 89 |
if (0 == strcmp(name, fCategories[i].fName)) { |
| 90 |
return reinterpret_cast<uint8_t*>(&fCategories[i]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (fNumCategories >= kMaxCategories) { |
| 95 |
SkDEBUGFAIL("Exhausted event tracing categories. Increase kMaxCategories."); |
| 96 |
return reinterpret_cast<uint8_t*>(&fCategories[0]); |
| 97 |
} |
| 98 |
|
| 99 |
fCategories[fNumCategories].fEnabled = |
| 100 |
CommandLineFlags::ShouldSkip(FLAGS_traceMatch, name) |
| 101 |
? 0 |
| 102 |
: SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags; |
| 103 |
|
| 104 |
fCategories[fNumCategories].fName = name; |
| 105 |
return reinterpret_cast<uint8_t*>(&fCategories[fNumCategories++]); |
| 106 |
} |
| 107 |
|
| 108 |
const char* SkEventTracingCategories::getCategoryGroupName(const uint8_t* categoryEnabledFlag) { |
| 109 |
if (categoryEnabledFlag) { |
| 110 |
return reinterpret_cast<const CategoryState*>(categoryEnabledFlag)->fName; |
| 111 |
} |
| 112 |
return nullptr; |
| 113 |
} |