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 "aemu/base/Tracing.h"
16 
17 #ifdef USE_PERFETTO_TRACING
18 #include "perfetto-tracing-only.h"
19 #endif
20 
21 #include <string>
22 #include <vector>
23 
24 #include <fcntl.h>
25 
26 namespace android {
27 namespace base {
28 
29 const bool* tracingDisabledPtr = nullptr;
30 
initializeTracing()31 void initializeTracing() {
32 #ifdef USE_PERFETTO_TRACING
33     virtualdeviceperfetto::initialize(&tracingDisabledPtr);
34 #endif
35 }
36 
enableTracing()37 void enableTracing() {
38 #ifdef USE_PERFETTO_TRACING
39     if (virtualdeviceperfetto::queryTraceConfig().tracingDisabled) {
40         virtualdeviceperfetto::enableTracing();
41     }
42 #endif
43 }
44 
disableTracing()45 void disableTracing() {
46 #ifdef USE_PERFETTO_TRACING
47     if (!virtualdeviceperfetto::queryTraceConfig().tracingDisabled) {
48         virtualdeviceperfetto::disableTracing();
49     }
50 #endif
51 }
52 
shouldEnableTracing()53 bool shouldEnableTracing() {
54 #ifdef USE_PERFETTO_TRACING
55     return !(virtualdeviceperfetto::queryTraceConfig().tracingDisabled);
56 #else
57     return false;
58 #endif
59 }
60 
61 #ifdef __cplusplus
62 #   define CC_LIKELY( exp )    (__builtin_expect( !!(exp), true ))
63 #   define CC_UNLIKELY( exp )  (__builtin_expect( !!(exp), false ))
64 #else
65 #   define CC_LIKELY( exp )    (__builtin_expect( !!(exp), 1 ))
66 #   define CC_UNLIKELY( exp )  (__builtin_expect( !!(exp), 0 ))
67 #endif
68 
beginTrace(const char * name)69 __attribute__((always_inline)) void beginTrace(const char* name) {
70     if (CC_LIKELY(!tracingDisabledPtr)) return;
71 #ifdef USE_PERFETTO_TRACING
72     virtualdeviceperfetto::beginTrace(name);
73 #endif
74 }
75 
endTrace()76 __attribute__((always_inline)) void endTrace() {
77     if (CC_LIKELY(!tracingDisabledPtr)) return;
78 #ifdef USE_PERFETTO_TRACING
79     virtualdeviceperfetto::endTrace();
80 #endif
81 }
82 
traceCounter(const char * name,int64_t value)83 __attribute__((always_inline)) void traceCounter(const char* name, int64_t value) {
84     if (CC_LIKELY(!tracingDisabledPtr)) return;
85 #ifdef USE_PERFETTO_TRACING
86     virtualdeviceperfetto::traceCounter(name, value);
87 #endif
88 }
89 
ScopedTrace(const char * name)90 ScopedTrace::ScopedTrace(const char* name) {
91     if (CC_LIKELY(!tracingDisabledPtr)) return;
92 #ifdef USE_PERFETTO_TRACING
93     virtualdeviceperfetto::beginTrace(name);
94 #endif
95 }
96 
~ScopedTrace()97 ScopedTrace::~ScopedTrace() {
98     if (CC_LIKELY(!tracingDisabledPtr)) return;
99 #ifdef USE_PERFETTO_TRACING
100     virtualdeviceperfetto::endTrace();
101 #endif
102 }
103 
setGuestTime(uint64_t t)104 void setGuestTime(uint64_t t) {
105 #ifdef USE_PERFETTO_TRACING
106     virtualdeviceperfetto::setGuestTime(t);
107 #endif
108 }
109 
110 } // namespace base
111 } // namespace android
112