Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 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 "SkEventTracingPriv.h" |
| 9 | |
| 10 | #include "SkATrace.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 11 | #include "SkChromeTracingTracer.h" |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 12 | #include "SkCommandLineFlags.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 13 | #include "SkDebugfTracer.h" |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 14 | #include "SkEventTracer.h" |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 15 | #include "SkTraceEvent.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 16 | |
| 17 | DEFINE_string(trace, "", |
| 18 | "Log trace events in one of several modes:\n" |
| 19 | " debugf : Show events using SkDebugf\n" |
| 20 | " atrace : Send events to Android ATrace\n" |
| 21 | " <filename> : Any other string is interpreted as a filename. Writes\n" |
| 22 | " trace events to specified file as JSON, for viewing\n" |
| 23 | " with chrome://tracing"); |
| 24 | |
Brian Osman | 1e09686 | 2017-07-26 15:50:37 -0400 | [diff] [blame] | 25 | DEFINE_string(traceMatch, "", |
| 26 | "Filter which categories are traced.\n" |
| 27 | "Uses same format as --match\n"); |
| 28 | |
Mike Klein | 3bf0042 | 2017-07-24 14:27:18 -0400 | [diff] [blame] | 29 | void initializeEventTracingForTools(const char* traceFlag) { |
| 30 | if (!traceFlag) { |
| 31 | if (FLAGS_trace.isEmpty()) { |
| 32 | return; |
| 33 | } |
| 34 | traceFlag = FLAGS_trace[0]; |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 37 | SkEventTracer* eventTracer = nullptr; |
| 38 | if (0 == strcmp(traceFlag, "atrace")) { |
| 39 | eventTracer = new SkATrace(); |
| 40 | } else if (0 == strcmp(traceFlag, "debugf")) { |
| 41 | eventTracer = new SkDebugfTracer(); |
| 42 | } else { |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 43 | eventTracer = new SkChromeTracingTracer(traceFlag); |
| 44 | } |
| 45 | |
| 46 | SkAssertResult(SkEventTracer::SetInstance(eventTracer)); |
| 47 | } |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 48 | |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 49 | uint8_t* SkEventTracingCategories::getCategoryGroupEnabled(const char* name) { |
| 50 | static_assert(0 == offsetof(CategoryState, fEnabled), "CategoryState"); |
| 51 | |
| 52 | // We ignore the "disabled-by-default-" prefix in our internal tools |
| 53 | if (SkStrStartsWith(name, TRACE_CATEGORY_PREFIX)) { |
| 54 | name += strlen(TRACE_CATEGORY_PREFIX); |
| 55 | } |
| 56 | |
| 57 | // Chrome's implementation of this API does a two-phase lookup (once without a lock, then again |
| 58 | // with a lock. But the tracing macros avoid calling these functions more than once per site, |
| 59 | // so just do something simple (and easier to reason about): |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 60 | SkAutoMutexAcquire lock(&fMutex); |
| 61 | for (int i = 0; i < fNumCategories; ++i) { |
| 62 | if (0 == strcmp(name, fCategories[i].fName)) { |
| 63 | return reinterpret_cast<uint8_t*>(&fCategories[i]); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (fNumCategories >= kMaxCategories) { |
| 68 | SkDEBUGFAIL("Exhausted event tracing categories. Increase kMaxCategories."); |
| 69 | return reinterpret_cast<uint8_t*>(&fCategories[0]); |
| 70 | } |
| 71 | |
Brian Osman | 1e09686 | 2017-07-26 15:50:37 -0400 | [diff] [blame] | 72 | fCategories[fNumCategories].fEnabled = SkCommandLineFlags::ShouldSkip(FLAGS_traceMatch, name) |
| 73 | ? 0 : SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags; |
| 74 | |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 75 | fCategories[fNumCategories].fName = name; |
| 76 | return reinterpret_cast<uint8_t*>(&fCategories[fNumCategories++]); |
| 77 | } |
| 78 | |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 79 | const char* SkEventTracingCategories::getCategoryGroupName(const uint8_t* categoryEnabledFlag) { |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 80 | if (categoryEnabledFlag) { |
| 81 | return reinterpret_cast<const CategoryState*>(categoryEnabledFlag)->fName; |
| 82 | } |
| 83 | return nullptr; |
| 84 | } |