Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 2 | // Copyright (C) 2019 Google Inc. |
| 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 | #include "base/Tracing.h" |
| 16 | |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 17 | #include "perfetto-tracing-only.h" |
| 18 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace base { |
| 26 | |
| 27 | const bool* tracingDisabledPtr = nullptr; |
| 28 | |
| 29 | void initializeTracing() { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 30 | virtualdeviceperfetto::initialize(&tracingDisabledPtr); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void enableTracing() { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 34 | if (virtualdeviceperfetto::queryTraceConfig().tracingDisabled) { |
| 35 | virtualdeviceperfetto::enableTracing(); |
| 36 | } |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void disableTracing() { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 40 | if (!virtualdeviceperfetto::queryTraceConfig().tracingDisabled) { |
| 41 | virtualdeviceperfetto::disableTracing(); |
| 42 | } |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool shouldEnableTracing() { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 46 | return !(virtualdeviceperfetto::queryTraceConfig().tracingDisabled); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), true )) |
| 51 | # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), false )) |
| 52 | #else |
| 53 | # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), 1 )) |
| 54 | # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), 0 )) |
| 55 | #endif |
| 56 | |
| 57 | __attribute__((always_inline)) void beginTrace(const char* name) { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 58 | if (CC_LIKELY(*tracingDisabledPtr)) return; |
| 59 | virtualdeviceperfetto::beginTrace(name); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | __attribute__((always_inline)) void endTrace() { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 63 | if (CC_LIKELY(*tracingDisabledPtr)) return; |
| 64 | virtualdeviceperfetto::endTrace(); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | __attribute__((always_inline)) void traceCounter(const char* name, int64_t value) { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 68 | if (CC_LIKELY(*tracingDisabledPtr)) return; |
| 69 | virtualdeviceperfetto::traceCounter(name, value); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | ScopedTrace::ScopedTrace(const char* name) { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 73 | if (CC_LIKELY(*tracingDisabledPtr)) return; |
| 74 | virtualdeviceperfetto::beginTrace(name); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ScopedTrace::~ScopedTrace() { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 78 | if (CC_LIKELY(*tracingDisabledPtr)) return; |
| 79 | virtualdeviceperfetto::endTrace(); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void setGuestTime(uint64_t t) { |
Doug Horn | 8de2047 | 2020-12-09 13:33:50 -0800 | [diff] [blame] | 83 | virtualdeviceperfetto::setGuestTime(t); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace base |
| 87 | } // namespace android |