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
17 #include "perfetto-tracing-only.h"
18
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
initializeTracing()29 void initializeTracing() {
30 virtualdeviceperfetto::initialize(&tracingDisabledPtr);
31 }
32
enableTracing()33 void enableTracing() {
34 if (virtualdeviceperfetto::queryTraceConfig().tracingDisabled) {
35 virtualdeviceperfetto::enableTracing();
36 }
37 }
38
disableTracing()39 void disableTracing() {
40 if (!virtualdeviceperfetto::queryTraceConfig().tracingDisabled) {
41 virtualdeviceperfetto::disableTracing();
42 }
43 }
44
shouldEnableTracing()45 bool shouldEnableTracing() {
46 return !(virtualdeviceperfetto::queryTraceConfig().tracingDisabled);
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
beginTrace(const char * name)57 __attribute__((always_inline)) void beginTrace(const char* name) {
58 if (CC_LIKELY(*tracingDisabledPtr)) return;
59 virtualdeviceperfetto::beginTrace(name);
60 }
61
endTrace()62 __attribute__((always_inline)) void endTrace() {
63 if (CC_LIKELY(*tracingDisabledPtr)) return;
64 virtualdeviceperfetto::endTrace();
65 }
66
traceCounter(const char * name,int64_t value)67 __attribute__((always_inline)) void traceCounter(const char* name, int64_t value) {
68 if (CC_LIKELY(*tracingDisabledPtr)) return;
69 virtualdeviceperfetto::traceCounter(name, value);
70 }
71
ScopedTrace(const char * name)72 ScopedTrace::ScopedTrace(const char* name) {
73 if (CC_LIKELY(*tracingDisabledPtr)) return;
74 virtualdeviceperfetto::beginTrace(name);
75 }
76
~ScopedTrace()77 ScopedTrace::~ScopedTrace() {
78 if (CC_LIKELY(*tracingDisabledPtr)) return;
79 virtualdeviceperfetto::endTrace();
80 }
81
setGuestTime(uint64_t t)82 void setGuestTime(uint64_t t) {
83 virtualdeviceperfetto::setGuestTime(t);
84 }
85
86 } // namespace base
87 } // namespace android
88