Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 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> |
Vishnu Nair | 47b6e68 | 2024-07-12 18:46:07 +0000 | [diff] [blame] | 20 | #include <cstdarg> |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 21 | |
| 22 | #include "perfetto/public/te_category_macros.h" |
| 23 | #include "trace_categories.h" |
| 24 | #include "tracing_perfetto_internal.h" |
| 25 | |
| 26 | namespace tracing_perfetto { |
| 27 | |
| 28 | void registerWithPerfetto(bool test) { |
| 29 | internal::registerWithPerfetto(test); |
| 30 | } |
| 31 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 32 | void traceBegin(uint64_t category, const char* name) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 33 | struct PerfettoTeCategory* perfettoTeCategory = |
| 34 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 35 | |
| 36 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 37 | atrace_begin(category, name); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 38 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 39 | internal::perfettoTraceBegin(*perfettoTeCategory, name); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Vishnu Nair | 47b6e68 | 2024-07-12 18:46:07 +0000 | [diff] [blame] | 43 | void traceFormatBegin(uint64_t category, const char* fmt, ...) { |
| 44 | struct PerfettoTeCategory* perfettoTeCategory = |
| 45 | internal::toPerfettoCategory(category); |
| 46 | const bool preferAtrace = internal::shouldPreferAtrace(perfettoTeCategory, category); |
| 47 | const bool preferPerfetto = internal::isPerfettoCategoryEnabled(perfettoTeCategory); |
| 48 | if (CC_LIKELY(!(preferAtrace || preferPerfetto))) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const int BUFFER_SIZE = 256; |
| 53 | va_list ap; |
| 54 | char buf[BUFFER_SIZE]; |
| 55 | |
| 56 | va_start(ap, fmt); |
| 57 | vsnprintf(buf, BUFFER_SIZE, fmt, ap); |
| 58 | va_end(ap); |
| 59 | |
| 60 | |
| 61 | if (preferAtrace) { |
| 62 | atrace_begin(category, buf); |
| 63 | } else if (preferPerfetto) { |
| 64 | internal::perfettoTraceBegin(*perfettoTeCategory, buf); |
| 65 | } |
| 66 | } |
| 67 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 68 | void traceEnd(uint64_t category) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 69 | struct PerfettoTeCategory* perfettoTeCategory = |
| 70 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 71 | |
| 72 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 73 | atrace_end(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 74 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 75 | internal::perfettoTraceEnd(*perfettoTeCategory); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 79 | void traceAsyncBegin(uint64_t category, const char* name, int32_t cookie) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 80 | struct PerfettoTeCategory* perfettoTeCategory = |
| 81 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 82 | |
| 83 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 84 | atrace_async_begin(category, name, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 85 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 86 | internal::perfettoTraceAsyncBegin(*perfettoTeCategory, name, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 90 | void traceAsyncEnd(uint64_t category, const char* name, int32_t cookie) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 91 | struct PerfettoTeCategory* perfettoTeCategory = |
| 92 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 93 | |
| 94 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 95 | atrace_async_end(category, name, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 96 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 97 | internal::perfettoTraceAsyncEnd(*perfettoTeCategory, name, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 101 | void traceAsyncBeginForTrack(uint64_t category, const char* name, |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 102 | const char* trackName, int32_t cookie) { |
| 103 | struct PerfettoTeCategory* perfettoTeCategory = |
| 104 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 105 | |
| 106 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 107 | atrace_async_for_track_begin(category, trackName, name, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 108 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 109 | internal::perfettoTraceAsyncBeginForTrack(*perfettoTeCategory, name, trackName, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 113 | void traceAsyncEndForTrack(uint64_t category, const char* trackName, |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 114 | int32_t cookie) { |
| 115 | struct PerfettoTeCategory* perfettoTeCategory = |
| 116 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 117 | |
| 118 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 119 | atrace_async_for_track_end(category, trackName, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 120 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 121 | internal::perfettoTraceAsyncEndForTrack(*perfettoTeCategory, trackName, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 125 | void traceInstant(uint64_t category, const char* name) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 126 | struct PerfettoTeCategory* perfettoTeCategory = |
| 127 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 128 | |
| 129 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 130 | atrace_instant(category, name); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 131 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 132 | internal::perfettoTraceInstant(*perfettoTeCategory, name); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Vishnu Nair | 47b6e68 | 2024-07-12 18:46:07 +0000 | [diff] [blame] | 136 | void traceFormatInstant(uint64_t category, const char* fmt, ...) { |
| 137 | struct PerfettoTeCategory* perfettoTeCategory = |
| 138 | internal::toPerfettoCategory(category); |
| 139 | const bool preferAtrace = internal::shouldPreferAtrace(perfettoTeCategory, category); |
| 140 | const bool preferPerfetto = internal::isPerfettoCategoryEnabled(perfettoTeCategory); |
| 141 | if (CC_LIKELY(!(preferAtrace || preferPerfetto))) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | const int BUFFER_SIZE = 256; |
| 146 | va_list ap; |
| 147 | char buf[BUFFER_SIZE]; |
| 148 | |
| 149 | va_start(ap, fmt); |
| 150 | vsnprintf(buf, BUFFER_SIZE, fmt, ap); |
| 151 | va_end(ap); |
| 152 | |
| 153 | if (preferAtrace) { |
| 154 | atrace_instant(category, buf); |
| 155 | } else if (preferPerfetto) { |
| 156 | internal::perfettoTraceInstant(*perfettoTeCategory, buf); |
| 157 | } |
| 158 | } |
| 159 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 160 | void traceInstantForTrack(uint64_t category, const char* trackName, |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 161 | const char* name) { |
| 162 | struct PerfettoTeCategory* perfettoTeCategory = |
| 163 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 164 | |
| 165 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 166 | atrace_instant_for_track(category, trackName, name); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 167 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 168 | internal::perfettoTraceInstantForTrack(*perfettoTeCategory, trackName, name); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 172 | void traceCounter(uint64_t category, const char* name, int64_t value) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 173 | struct PerfettoTeCategory* perfettoTeCategory = |
| 174 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 175 | |
| 176 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 177 | atrace_int64(category, name, value); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 178 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 179 | internal::perfettoTraceCounter(*perfettoTeCategory, name, value); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Vishnu Nair | 47b6e68 | 2024-07-12 18:46:07 +0000 | [diff] [blame] | 183 | void traceCounter32(uint64_t category, const char* name, int32_t value) { |
| 184 | struct PerfettoTeCategory* perfettoTeCategory = internal::toPerfettoCategory(category); |
| 185 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
| 186 | atrace_int(category, name, value); |
| 187 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 188 | internal::perfettoTraceCounter(*perfettoTeCategory, name, |
| 189 | static_cast<int64_t>(value)); |
| 190 | } |
| 191 | } |
| 192 | |
Zimuzo Ezeozue | 68a0d27 | 2024-03-19 09:52:41 +0000 | [diff] [blame] | 193 | bool isTagEnabled(uint64_t category) { |
| 194 | struct PerfettoTeCategory* perfettoTeCategory = |
| 195 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 196 | return internal::isPerfettoCategoryEnabled(perfettoTeCategory) |
| 197 | || atrace_is_tag_enabled(category); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 198 | } |
Vishnu Nair | 47b6e68 | 2024-07-12 18:46:07 +0000 | [diff] [blame] | 199 | |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 200 | } // namespace tracing_perfetto |