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 | #ifndef SkChromeTracingTracer_DEFINED |
| 9 | #define SkChromeTracingTracer_DEFINED |
| 10 | |
| 11 | #include "SkEventTracer.h" |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 12 | #include "SkEventTracingPriv.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 13 | #include "SkJSONCPP.h" |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 14 | #include "SkSpinlock.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 15 | #include "SkString.h" |
| 16 | |
| 17 | /** |
| 18 | * A SkEventTracer implementation that logs events to JSON for viewing with chrome://tracing. |
| 19 | */ |
| 20 | class SkChromeTracingTracer : public SkEventTracer { |
| 21 | public: |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 22 | SkChromeTracingTracer(const char* filename); |
| 23 | ~SkChromeTracingTracer() override; |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 24 | |
| 25 | SkEventTracer::Handle addTraceEvent(char phase, |
| 26 | const uint8_t* categoryEnabledFlag, |
| 27 | const char* name, |
| 28 | uint64_t id, |
| 29 | int numArgs, |
| 30 | const char** argNames, |
| 31 | const uint8_t* argTypes, |
| 32 | const uint64_t* argValues, |
| 33 | uint8_t flags) override; |
| 34 | |
| 35 | void updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| 36 | const char* name, |
| 37 | SkEventTracer::Handle handle) override; |
| 38 | |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 39 | const uint8_t* getCategoryGroupEnabled(const char* name) override { |
| 40 | return fCategories.getCategoryGroupEnabled(name); |
| 41 | } |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 42 | |
| 43 | const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override { |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 44 | return fCategories.getCategoryGroupName(categoryEnabledFlag); |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private: |
| 48 | void flush(); |
| 49 | |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 50 | enum { |
| 51 | // Events are currently 80 bytes, assuming 64-bit pointers and reasonable packing. |
| 52 | // This is a first guess at a number that balances memory usage vs. time overhead of |
| 53 | // allocating blocks. |
| 54 | kEventsPerBlock = 10000, |
| 55 | |
| 56 | // Our current tracing macros only support up to 2 arguments |
| 57 | kMaxArgs = 2, |
| 58 | }; |
| 59 | |
| 60 | struct TraceEvent { |
| 61 | // Fields are ordered to minimize size due to alignment |
| 62 | char fPhase; |
| 63 | uint8_t fNumArgs; |
| 64 | uint8_t fArgTypes[kMaxArgs]; |
| 65 | |
| 66 | const char* fName; |
| 67 | const char* fCategory; |
| 68 | uint64_t fClockBegin; |
| 69 | uint64_t fClockEnd; |
| 70 | SkThreadID fThreadID; |
| 71 | |
| 72 | const char* fArgNames[kMaxArgs]; |
| 73 | uint64_t fArgValues[kMaxArgs]; |
| 74 | }; |
| 75 | |
| 76 | typedef std::unique_ptr<TraceEvent[]> BlockPtr; |
| 77 | BlockPtr createBlock(); |
| 78 | TraceEvent* appendEvent(const TraceEvent&); |
| 79 | Json::Value traceEventToJson(const TraceEvent&); |
| 80 | |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 81 | SkString fFilename; |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 82 | SkSpinlock fMutex; |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 83 | SkEventTracingCategories fCategories; |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 84 | BlockPtr fCurBlock; |
| 85 | int fEventsInCurBlock; |
| 86 | SkTArray<BlockPtr> fBlocks; |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #endif |