xref: /aosp_15_r20/external/skia/tools/trace/SkPerfettoTrace.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google LLC
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 SkPerfettoTrace_DEFINED
9 #define SkPerfettoTrace_DEFINED
10 
11 #include "include/utils/SkEventTracer.h"
12 #include "tools/trace/EventTracingPriv.h"
13 #include "perfetto.h"
14 
15 PERFETTO_DEFINE_CATEGORIES();
16 
17 /**
18  * This class is used to support Perfetto tracing. It hooks into the SkEventTracer system.
19  */
20 class SkPerfettoTrace : public SkEventTracer {
21 public:
22     SkPerfettoTrace();
23     ~SkPerfettoTrace() override;
24 
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 
36     void updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
37                                   const char* name,
38                                   SkEventTracer::Handle handle) override;
39 
40     const uint8_t* getCategoryGroupEnabled(const char* name) override;
41 
42     const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override;
43 
44     void newTracingSection(const char* name) override;
45 
46 private:
47     SkPerfettoTrace(const SkPerfettoTrace&) = delete;
48     SkPerfettoTrace& operator=(const SkPerfettoTrace&) = delete;
49     SkEventTracingCategories fCategories;
50     std::unique_ptr<perfetto::TracingSession> tracingSession;
51     int fd{-1};
52 
53     /** Store the perfetto trace file output path, name, and extension separately. This isolation
54      * of name components becomes useful when splitting traces up by sections, where we want to
55      * alter the base file name but keep the trace output path and file extension the same.
56      */
57     std::string fOutputPath;
58     std::string fOutputFileExtension;
59     std::string fCurrentSessionFullOutputPath;
60 
61     void openNewTracingSession(const std::string& baseFileName);
62     void closeTracingSession();
63 
64     /** Overloaded private methods to initiate a trace event with 0-2 arguments. Perfetto supports
65      * adding an arbitrary number of debug annotations or arguments, but the existing Skia trace
66      * structure only supports 0-2 so that is all we accommodate.
67      */
68     void triggerTraceEvent(const uint8_t* categoryEnabledFlag, const char* eventName);
69     void triggerTraceEvent(const uint8_t* categoryEnabledFlag, const char* eventName,
70                            const char* arg1Name, const uint8_t& arg1Type, const uint64_t& arg1Val);
71     void triggerTraceEvent(const uint8_t* categoryEnabledFlag, const char* eventName,
72                            const char* arg1Name, const uint8_t& arg1Type, const uint64_t& arg1Val,
73                            const char* arg2Name, const uint8_t& arg2Type, const uint64_t& arg2Val);
74 };
75 
76 #endif
77