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 | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 13 | #include "SkSpinlock.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 14 | #include "SkString.h" |
Brian Osman | b6705c2 | 2017-08-01 10:23:38 -0400 | [diff] [blame] | 15 | #include "SkTHash.h" |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 16 | |
Brian Osman | 69fd008 | 2017-08-09 09:25:39 -0400 | [diff] [blame] | 17 | class SkJSONWriter; |
| 18 | |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 19 | /** |
| 20 | * A SkEventTracer implementation that logs events to JSON for viewing with chrome://tracing. |
| 21 | */ |
| 22 | class SkChromeTracingTracer : public SkEventTracer { |
| 23 | public: |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 24 | SkChromeTracingTracer(const char* filename); |
| 25 | ~SkChromeTracingTracer() override; |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 26 | |
| 27 | SkEventTracer::Handle addTraceEvent(char phase, |
| 28 | const uint8_t* categoryEnabledFlag, |
| 29 | const char* name, |
| 30 | uint64_t id, |
| 31 | int numArgs, |
| 32 | const char** argNames, |
| 33 | const uint8_t* argTypes, |
| 34 | const uint64_t* argValues, |
| 35 | uint8_t flags) override; |
| 36 | |
| 37 | void updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| 38 | const char* name, |
| 39 | SkEventTracer::Handle handle) override; |
| 40 | |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 41 | const uint8_t* getCategoryGroupEnabled(const char* name) override { |
| 42 | return fCategories.getCategoryGroupEnabled(name); |
| 43 | } |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 44 | |
| 45 | const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override { |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 46 | return fCategories.getCategoryGroupName(categoryEnabledFlag); |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | private: |
| 50 | void flush(); |
| 51 | |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 52 | enum { |
Brian Osman | c7f2676 | 2017-08-16 10:31:29 -0400 | [diff] [blame^] | 53 | // Events are variable size, but most commonly 56 bytes, assuming 64-bit pointers and |
| 54 | // reasonable packing. This is a first guess at a number that balances memory usage vs. |
| 55 | // time overhead of allocating blocks. |
| 56 | kBlockSize = 512 * 1024, |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
Brian Osman | c7f2676 | 2017-08-16 10:31:29 -0400 | [diff] [blame^] | 59 | typedef std::unique_ptr<uint8_t[]> BlockPtr; |
| 60 | struct TraceEventBlock { |
| 61 | BlockPtr fBlock; |
| 62 | int fEventsInBlock; |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
Brian Osman | c7f2676 | 2017-08-16 10:31:29 -0400 | [diff] [blame^] | 65 | void createBlock(); |
Brian Osman | b6705c2 | 2017-08-01 10:23:38 -0400 | [diff] [blame] | 66 | |
Brian Osman | c7f2676 | 2017-08-16 10:31:29 -0400 | [diff] [blame^] | 67 | Handle appendEvent(const void* data, size_t size); |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 68 | |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 69 | SkString fFilename; |
Brian Osman | bc8150f | 2017-07-24 11:38:01 -0400 | [diff] [blame] | 70 | SkSpinlock fMutex; |
Brian Osman | 65e4c61 | 2017-07-21 11:06:24 -0400 | [diff] [blame] | 71 | SkEventTracingCategories fCategories; |
Brian Osman | c7f2676 | 2017-08-16 10:31:29 -0400 | [diff] [blame^] | 72 | |
| 73 | TraceEventBlock fCurBlock; |
| 74 | size_t fCurBlockUsed; |
| 75 | |
| 76 | SkTArray<TraceEventBlock> fBlocks; |
Brian Osman | 53136aa | 2017-07-20 15:43:35 -0400 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | #endif |