1 /*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "tracing_perfetto.h"
18
19 #include <cutils/trace.h>
20
21 #include "perfetto/public/te_category_macros.h"
22 #include "trace_categories.h"
23 #include "tracing_perfetto_internal.h"
24
25 namespace tracing_perfetto {
26
registerWithPerfetto(bool test)27 void registerWithPerfetto(bool test) {
28 internal::registerWithPerfetto(test);
29 }
30
traceBegin(uint64_t category,const char * name)31 Result traceBegin(uint64_t category, const char* name) {
32 struct PerfettoTeCategory* perfettoTeCategory =
33 internal::toPerfettoCategory(category);
34 if (perfettoTeCategory != nullptr) {
35 return internal::perfettoTraceBegin(*perfettoTeCategory, name);
36 } else {
37 atrace_begin(category, name);
38 return Result::SUCCESS;
39 }
40 }
41
traceEnd(uint64_t category)42 Result traceEnd(uint64_t category) {
43 struct PerfettoTeCategory* perfettoTeCategory =
44 internal::toPerfettoCategory(category);
45 if (perfettoTeCategory != nullptr) {
46 return internal::perfettoTraceEnd(*perfettoTeCategory);
47 } else {
48 atrace_end(category);
49 return Result::SUCCESS;
50 }
51 }
52
traceAsyncBegin(uint64_t category,const char * name,int32_t cookie)53 Result traceAsyncBegin(uint64_t category, const char* name, int32_t cookie) {
54 struct PerfettoTeCategory* perfettoTeCategory =
55 internal::toPerfettoCategory(category);
56 if (perfettoTeCategory != nullptr) {
57 return internal::perfettoTraceAsyncBegin(*perfettoTeCategory, name, cookie);
58 } else {
59 atrace_async_begin(category, name, cookie);
60 return Result::SUCCESS;
61 }
62 }
63
traceAsyncEnd(uint64_t category,const char * name,int32_t cookie)64 Result traceAsyncEnd(uint64_t category, const char* name, int32_t cookie) {
65 struct PerfettoTeCategory* perfettoTeCategory =
66 internal::toPerfettoCategory(category);
67 if (perfettoTeCategory != nullptr) {
68 return internal::perfettoTraceAsyncEnd(*perfettoTeCategory, name, cookie);
69 } else {
70 atrace_async_end(category, name, cookie);
71 return Result::SUCCESS;
72 }
73 }
74
traceAsyncBeginForTrack(uint64_t category,const char * name,const char * trackName,int32_t cookie)75 Result traceAsyncBeginForTrack(uint64_t category, const char* name,
76 const char* trackName, int32_t cookie) {
77 struct PerfettoTeCategory* perfettoTeCategory =
78 internal::toPerfettoCategory(category);
79 if (perfettoTeCategory != nullptr) {
80 return internal::perfettoTraceAsyncBeginForTrack(*perfettoTeCategory, name, trackName, cookie);
81 } else {
82 atrace_async_for_track_begin(category, trackName, name, cookie);
83 return Result::SUCCESS;
84 }
85 }
86
traceAsyncEndForTrack(uint64_t category,const char * trackName,int32_t cookie)87 Result traceAsyncEndForTrack(uint64_t category, const char* trackName,
88 int32_t cookie) {
89 struct PerfettoTeCategory* perfettoTeCategory =
90 internal::toPerfettoCategory(category);
91 if (perfettoTeCategory != nullptr) {
92 return internal::perfettoTraceAsyncEndForTrack(*perfettoTeCategory, trackName, cookie);
93 } else {
94 atrace_async_for_track_end(category, trackName, cookie);
95 return Result::SUCCESS;
96 }
97 }
98
traceInstant(uint64_t category,const char * name)99 Result traceInstant(uint64_t category, const char* name) {
100 struct PerfettoTeCategory* perfettoTeCategory =
101 internal::toPerfettoCategory(category);
102 if (perfettoTeCategory != nullptr) {
103 return internal::perfettoTraceInstant(*perfettoTeCategory, name);
104 } else {
105 atrace_instant(category, name);
106 return Result::SUCCESS;
107 }
108 }
109
traceInstantForTrack(uint64_t category,const char * trackName,const char * name)110 Result traceInstantForTrack(uint64_t category, const char* trackName,
111 const char* name) {
112 struct PerfettoTeCategory* perfettoTeCategory =
113 internal::toPerfettoCategory(category);
114 if (perfettoTeCategory != nullptr) {
115 return internal::perfettoTraceInstantForTrack(*perfettoTeCategory, trackName, name);
116 } else {
117 atrace_instant_for_track(category, trackName, name);
118 return Result::SUCCESS;
119 }
120 }
121
traceCounter(uint64_t category,const char * name,int64_t value)122 Result traceCounter(uint64_t category, const char* name, int64_t value) {
123 struct PerfettoTeCategory* perfettoTeCategory =
124 internal::toPerfettoCategory(category);
125 if (perfettoTeCategory != nullptr) {
126 return internal::perfettoTraceCounter(*perfettoTeCategory, name, value);
127 } else {
128 atrace_int64(category, name, value);
129 return Result::SUCCESS;
130 }
131 }
132
isTagEnabled(uint64_t category)133 bool isTagEnabled(uint64_t category) {
134 struct PerfettoTeCategory* perfettoTeCategory =
135 internal::toPerfettoCategory(category);
136 if (perfettoTeCategory != nullptr) {
137 return true;
138 } else {
139 return (atrace_get_enabled_tags() & category) != 0;
140 }
141 }
142
143 } // namespace tracing_perfetto
144