blob: 987031641bfe1a018921cf1cf4dafbd4b94bb216 [file] [log] [blame]
Brian Osman53136aa2017-07-20 15:43:35 -04001/*
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 Osman65e4c612017-07-21 11:06:24 -040012#include "SkEventTracingPriv.h"
Brian Osman53136aa2017-07-20 15:43:35 -040013#include "SkJSONCPP.h"
Brian Osmanbc8150f2017-07-24 11:38:01 -040014#include "SkSpinlock.h"
Brian Osman53136aa2017-07-20 15:43:35 -040015#include "SkString.h"
Brian Osmanb6705c22017-08-01 10:23:38 -040016#include "SkTHash.h"
Brian Osman53136aa2017-07-20 15:43:35 -040017
18/**
19 * A SkEventTracer implementation that logs events to JSON for viewing with chrome://tracing.
20 */
21class SkChromeTracingTracer : public SkEventTracer {
22public:
Brian Osmanbc8150f2017-07-24 11:38:01 -040023 SkChromeTracingTracer(const char* filename);
24 ~SkChromeTracingTracer() override;
Brian Osman53136aa2017-07-20 15:43:35 -040025
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 Osman65e4c612017-07-21 11:06:24 -040040 const uint8_t* getCategoryGroupEnabled(const char* name) override {
41 return fCategories.getCategoryGroupEnabled(name);
42 }
Brian Osman53136aa2017-07-20 15:43:35 -040043
44 const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override {
Brian Osman65e4c612017-07-21 11:06:24 -040045 return fCategories.getCategoryGroupName(categoryEnabledFlag);
Brian Osman53136aa2017-07-20 15:43:35 -040046 }
47
48private:
49 void flush();
50
Brian Osmanbc8150f2017-07-24 11:38:01 -040051 enum {
Brian Osmanb6705c22017-08-01 10:23:38 -040052 // Events are currently 88 bytes, assuming 64-bit pointers and reasonable packing.
Brian Osmanbc8150f2017-07-24 11:38:01 -040053 // 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 Osmanb6705c22017-08-01 10:23:38 -040069 uint64_t fID;
Brian Osmanbc8150f2017-07-24 11:38:01 -040070 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 Osmanb6705c22017-08-01 10:23:38 -040080
81 typedef SkTHashMap<uint64_t, const char*> BaseTypeResolver;
Brian Osmanbc8150f2017-07-24 11:38:01 -040082 TraceEvent* appendEvent(const TraceEvent&);
Brian Osmanb6705c22017-08-01 10:23:38 -040083 Json::Value traceEventToJson(const TraceEvent&, BaseTypeResolver* baseTypeResolver);
Brian Osmanbc8150f2017-07-24 11:38:01 -040084
Brian Osman53136aa2017-07-20 15:43:35 -040085 SkString fFilename;
Brian Osmanbc8150f2017-07-24 11:38:01 -040086 SkSpinlock fMutex;
Brian Osman65e4c612017-07-21 11:06:24 -040087 SkEventTracingCategories fCategories;
Brian Osmanbc8150f2017-07-24 11:38:01 -040088 BlockPtr fCurBlock;
89 int fEventsInCurBlock;
90 SkTArray<BlockPtr> fBlocks;
Brian Osman53136aa2017-07-20 15:43:35 -040091};
92
93#endif