blob: 4a749081b93acab8b7af343766f690b8901926c7 [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"
16
17/**
18 * A SkEventTracer implementation that logs events to JSON for viewing with chrome://tracing.
19 */
20class SkChromeTracingTracer : public SkEventTracer {
21public:
Brian Osmanbc8150f2017-07-24 11:38:01 -040022 SkChromeTracingTracer(const char* filename);
23 ~SkChromeTracingTracer() override;
Brian Osman53136aa2017-07-20 15:43:35 -040024
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 Osman65e4c612017-07-21 11:06:24 -040039 const uint8_t* getCategoryGroupEnabled(const char* name) override {
40 return fCategories.getCategoryGroupEnabled(name);
41 }
Brian Osman53136aa2017-07-20 15:43:35 -040042
43 const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override {
Brian Osman65e4c612017-07-21 11:06:24 -040044 return fCategories.getCategoryGroupName(categoryEnabledFlag);
Brian Osman53136aa2017-07-20 15:43:35 -040045 }
46
47private:
48 void flush();
49
Brian Osmanbc8150f2017-07-24 11:38:01 -040050 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 Osman53136aa2017-07-20 15:43:35 -040081 SkString fFilename;
Brian Osmanbc8150f2017-07-24 11:38:01 -040082 SkSpinlock fMutex;
Brian Osman65e4c612017-07-21 11:06:24 -040083 SkEventTracingCategories fCategories;
Brian Osmanbc8150f2017-07-24 11:38:01 -040084 BlockPtr fCurBlock;
85 int fEventsInCurBlock;
86 SkTArray<BlockPtr> fBlocks;
Brian Osman53136aa2017-07-20 15:43:35 -040087};
88
89#endif