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> |
| 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 | |
| 27 | void registerWithPerfetto(bool test) { |
| 28 | internal::registerWithPerfetto(test); |
| 29 | } |
| 30 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 31 | void traceBegin(uint64_t category, const char* name) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 32 | struct PerfettoTeCategory* perfettoTeCategory = |
| 33 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 34 | |
| 35 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 36 | atrace_begin(category, name); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 37 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 38 | internal::perfettoTraceBegin(*perfettoTeCategory, name); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 42 | void traceEnd(uint64_t category) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 43 | struct PerfettoTeCategory* perfettoTeCategory = |
| 44 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 45 | |
| 46 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 47 | atrace_end(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 48 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 49 | internal::perfettoTraceEnd(*perfettoTeCategory); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 53 | void traceAsyncBegin(uint64_t category, const char* name, int32_t cookie) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 54 | struct PerfettoTeCategory* perfettoTeCategory = |
| 55 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 56 | |
| 57 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 58 | atrace_async_begin(category, name, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 59 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 60 | internal::perfettoTraceAsyncBegin(*perfettoTeCategory, name, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 64 | void traceAsyncEnd(uint64_t category, const char* name, int32_t cookie) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 65 | struct PerfettoTeCategory* perfettoTeCategory = |
| 66 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 67 | |
| 68 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 69 | atrace_async_end(category, name, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 70 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 71 | internal::perfettoTraceAsyncEnd(*perfettoTeCategory, name, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 75 | void traceAsyncBeginForTrack(uint64_t category, const char* name, |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 76 | const char* trackName, int32_t cookie) { |
| 77 | struct PerfettoTeCategory* perfettoTeCategory = |
| 78 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 79 | |
| 80 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 81 | atrace_async_for_track_begin(category, trackName, name, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 82 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 83 | internal::perfettoTraceAsyncBeginForTrack(*perfettoTeCategory, name, trackName, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 87 | void traceAsyncEndForTrack(uint64_t category, const char* trackName, |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 88 | int32_t cookie) { |
| 89 | struct PerfettoTeCategory* perfettoTeCategory = |
| 90 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 91 | |
| 92 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 93 | atrace_async_for_track_end(category, trackName, cookie); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 94 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 95 | internal::perfettoTraceAsyncEndForTrack(*perfettoTeCategory, trackName, cookie); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 99 | void traceInstant(uint64_t category, const char* name) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 100 | struct PerfettoTeCategory* perfettoTeCategory = |
| 101 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 102 | |
| 103 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 104 | atrace_instant(category, name); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 105 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 106 | internal::perfettoTraceInstant(*perfettoTeCategory, name); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 110 | void traceInstantForTrack(uint64_t category, const char* trackName, |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 111 | const char* name) { |
| 112 | struct PerfettoTeCategory* perfettoTeCategory = |
| 113 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 114 | |
| 115 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 116 | atrace_instant_for_track(category, trackName, name); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 117 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 118 | internal::perfettoTraceInstantForTrack(*perfettoTeCategory, trackName, name); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 122 | void traceCounter(uint64_t category, const char* name, int64_t value) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 123 | struct PerfettoTeCategory* perfettoTeCategory = |
| 124 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 125 | |
| 126 | if (internal::shouldPreferAtrace(perfettoTeCategory, category)) { |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 127 | atrace_int64(category, name, value); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 128 | } else if (internal::isPerfettoCategoryEnabled(perfettoTeCategory)) { |
| 129 | internal::perfettoTraceCounter(*perfettoTeCategory, name, value); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Zimuzo Ezeozue | 68a0d27 | 2024-03-19 09:52:41 +0000 | [diff] [blame] | 133 | bool isTagEnabled(uint64_t category) { |
| 134 | struct PerfettoTeCategory* perfettoTeCategory = |
| 135 | internal::toPerfettoCategory(category); |
Zimuzo Ezeozue | 32bd152 | 2024-05-09 03:25:54 +0100 | [diff] [blame] | 136 | return internal::isPerfettoCategoryEnabled(perfettoTeCategory) |
| 137 | || atrace_is_tag_enabled(category); |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 138 | } |
Biswarup Pal | a0ae40f | 2023-11-15 13:30:48 +0000 | [diff] [blame] | 139 | } // namespace tracing_perfetto |