blob: 34fbbae393bca2a59a716c5eb3ec54c1ffb481e9 [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 Osmanbc8150f2017-07-24 11:38:01 -040013#include "SkSpinlock.h"
Brian Osman53136aa2017-07-20 15:43:35 -040014#include "SkString.h"
Brian Osmanb6705c22017-08-01 10:23:38 -040015#include "SkTHash.h"
Brian Osman53136aa2017-07-20 15:43:35 -040016
Brian Osman69fd0082017-08-09 09:25:39 -040017class SkJSONWriter;
18
Brian Osman53136aa2017-07-20 15:43:35 -040019/**
20 * A SkEventTracer implementation that logs events to JSON for viewing with chrome://tracing.
21 */
22class SkChromeTracingTracer : public SkEventTracer {
23public:
Brian Osmanbc8150f2017-07-24 11:38:01 -040024 SkChromeTracingTracer(const char* filename);
25 ~SkChromeTracingTracer() override;
Brian Osman53136aa2017-07-20 15:43:35 -040026
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 Osman65e4c612017-07-21 11:06:24 -040041 const uint8_t* getCategoryGroupEnabled(const char* name) override {
42 return fCategories.getCategoryGroupEnabled(name);
43 }
Brian Osman53136aa2017-07-20 15:43:35 -040044
45 const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override {
Brian Osman65e4c612017-07-21 11:06:24 -040046 return fCategories.getCategoryGroupName(categoryEnabledFlag);
Brian Osman53136aa2017-07-20 15:43:35 -040047 }
48
49private:
50 void flush();
51
Brian Osmanbc8150f2017-07-24 11:38:01 -040052 enum {
Brian Osmanc7f26762017-08-16 10:31:29 -040053 // 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 Osmanbc8150f2017-07-24 11:38:01 -040057 };
58
Brian Osmanc7f26762017-08-16 10:31:29 -040059 typedef std::unique_ptr<uint8_t[]> BlockPtr;
60 struct TraceEventBlock {
61 BlockPtr fBlock;
62 int fEventsInBlock;
Brian Osmanbc8150f2017-07-24 11:38:01 -040063 };
64
Brian Osmanc7f26762017-08-16 10:31:29 -040065 void createBlock();
Brian Osmanb6705c22017-08-01 10:23:38 -040066
Brian Osmanc7f26762017-08-16 10:31:29 -040067 Handle appendEvent(const void* data, size_t size);
Brian Osmanbc8150f2017-07-24 11:38:01 -040068
Brian Osman53136aa2017-07-20 15:43:35 -040069 SkString fFilename;
Brian Osmanbc8150f2017-07-24 11:38:01 -040070 SkSpinlock fMutex;
Brian Osman65e4c612017-07-21 11:06:24 -040071 SkEventTracingCategories fCategories;
Brian Osmanc7f26762017-08-16 10:31:29 -040072
73 TraceEventBlock fCurBlock;
74 size_t fCurBlockUsed;
75
76 SkTArray<TraceEventBlock> fBlocks;
Brian Osman53136aa2017-07-20 15:43:35 -040077};
78
79#endif