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 #pragma once
16 
17 #include <inttypes.h>
18 
19 #if defined(AEMU_TRACING_SHARED) && defined(_MSC_VER)
20 #if defined(TRACING_EXPORTS)
21 #define TRACING_API __declspec(dllexport)
22 #define TRACING_API_TEMPLATE_DECLARE
23 #define TRACING_API_TEMPLATE_DEFINE __declspec(dllexport)
24 #else
25 #define TRACING_API __declspec(dllimport)
26 #define TRACING_API_TEMPLATE_DECLARE
27 #define TRACING_API_TEMPLATE_DEFINE __declspec(dllimport)
28 #endif  // defined(TRACING_EXPORTS)
29 #elif defined(AEMU_UI_SHARED)
30 #define TRACING_API __attribute__((visibility("default")))
31 #define TRACING_API_TEMPLATE_DECLARE __attribute__((visibility("default")))
32 #define TRACING_API_TEMPLATE_DEFINE
33 #else
34 #define TRACING_API
35 #define TRACING_API_TEMPLATE_DECLARE
36 #define TRACING_API_TEMPLATE_DEFINE
37 #endif
38 // Library to perform tracing. Talks to platform-specific
39 // tracing libraries.
40 namespace android {
41 namespace base {
42 
43 // New tracing API that talks to an underlying tracing library, possibly perfetto.
44 //
45 // Sets up global state useful for tracing.
46 TRACING_API void initializeTracing();
47 
48 // Enable/disable tracing
49 TRACING_API void enableTracing();
50 TRACING_API void disableTracing();
51 
52 // Set the time of traces on the host to be at this guest time.
53 // Not needed if we assume timestamps can be transferrable (e.g.,
54 // when RDTSC with raw passthrough is used)
55 TRACING_API void setGuestTime(uint64_t guestTime);
56 
57 // Record a counter of some kind.
58 TRACING_API void traceCounter(const char* tag, int64_t value);
59 
60 TRACING_API void beginTrace(const char* name);
61 TRACING_API void endTrace();
62 
63 class TRACING_API ScopedTrace {
64 public:
65     ScopedTrace(const char* name);
66     ~ScopedTrace();
67 };
68 
69 class ScopedTraceDerived : public ScopedTrace {
70 public:
71     void* member = nullptr;
72 };
73 
74 void setGuestTime(uint64_t t);
75 
76 void enableTracing();
77 void disableTracing();
78 
79 bool shouldEnableTracing();
80 
81 void traceCounter(const char* name, int64_t value);
82 
83 } // namespace base
84 } // namespace android
85 
86 #define __AEMU_GENSYM2(x,y) x##y
87 #define __AEMU_GENSYM1(x,y) __AEMU_GENSYM2(x,y)
88 #define AEMU_GENSYM(x) __AEMU_GENSYM1(x,__COUNTER__)
89 
90 #define AEMU_SCOPED_TRACE(tag) __attribute__ ((unused)) android::base::ScopedTrace AEMU_GENSYM(aemuScopedTrace_)(tag)
91 #define AEMU_SCOPED_TRACE_CALL() AEMU_SCOPED_TRACE(__func__)
92 #define AEMU_SCOPED_THRESHOLD_TRACE_CALL()
93 #define AEMU_SCOPED_THRESHOLD_TRACE(...)
94