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