• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2024 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #ifndef ATRACE_TAG
21 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
22 #endif
23 
24 #include <cutils/trace.h>
25 #include <tracing_perfetto.h>
26 
27 // prevent using atrace directly, calls should go through tracing_perfetto lib
28 #undef ATRACE_ENABLED
29 #undef ATRACE_BEGIN
30 #undef ATRACE_END
31 #undef ATRACE_ASYNC_BEGIN
32 #undef ATRACE_ASYNC_END
33 #undef ATRACE_ASYNC_FOR_TRACK_BEGIN
34 #undef ATRACE_ASYNC_FOR_TRACK_END
35 #undef ATRACE_INSTANT
36 #undef ATRACE_INSTANT_FOR_TRACK
37 #undef ATRACE_INT
38 #undef ATRACE_INT64
39 #undef ATRACE_CALL
40 #undef ATRACE_NAME
41 #undef ATRACE_FORMAT
42 #undef ATRACE_FORMAT_INSTANT
43 
44 #define SFTRACE_ENABLED() ::tracing_perfetto::isTagEnabled(ATRACE_TAG)
45 #define SFTRACE_BEGIN(name) ::tracing_perfetto::traceBegin(ATRACE_TAG, name)
46 #define SFTRACE_END() ::tracing_perfetto::traceEnd(ATRACE_TAG)
47 #define SFTRACE_ASYNC_BEGIN(name, cookie) \
48     ::tracing_perfetto::traceAsyncBegin(ATRACE_TAG, name, cookie)
49 #define SFTRACE_ASYNC_END(name, cookie) ::tracing_perfetto::traceAsyncEnd(ATRACE_TAG, name, cookie)
50 #define SFTRACE_ASYNC_FOR_TRACK_BEGIN(track_name, name, cookie) \
51     ::tracing_perfetto::traceAsyncBeginForTrack(ATRACE_TAG, name, track_name, cookie)
52 #define SFTRACE_ASYNC_FOR_TRACK_END(track_name, cookie) \
53     ::tracing_perfetto::traceAsyncEndForTrack(ATRACE_TAG, track_name, cookie)
54 #define SFTRACE_INSTANT(name) ::tracing_perfetto::traceInstant(ATRACE_TAG, name)
55 #define SFTRACE_FORMAT_INSTANT(fmt, ...) \
56     ::tracing_perfetto::traceFormatInstant(ATRACE_TAG, fmt, ##__VA_ARGS__)
57 #define SFTRACE_INSTANT_FOR_TRACK(trackName, name) \
58     ::tracing_perfetto::traceInstantForTrack(ATRACE_TAG, trackName, name)
59 #define SFTRACE_INT(name, value) ::tracing_perfetto::traceCounter32(ATRACE_TAG, name, value)
60 #define SFTRACE_INT64(name, value) ::tracing_perfetto::traceCounter(ATRACE_TAG, name, value)
61 
62 // SFTRACE_NAME traces from its location until the end of its enclosing scope.
63 #define _PASTE(x, y) x##y
64 #define PASTE(x, y) _PASTE(x, y)
65 #define SFTRACE_NAME(name) ::android::ScopedTrace PASTE(___tracer, __LINE__)(name)
66 // SFTRACE_CALL is an SFTRACE_NAME that uses the current function name.
67 #define SFTRACE_CALL() SFTRACE_NAME(__FUNCTION__)
68 #define SFTRACE_NAME_FOR_TRACK(trackName, name) \
69     ::android::ScopedTraceForTrack PASTE(___tracer, __LINE__)(trackName, name)
70 
71 #define SFTRACE_FORMAT(fmt, ...) \
72     ::android::ScopedTrace PASTE(___tracer, __LINE__)(fmt, ##__VA_ARGS__)
73 
74 #define ALOGE_AND_TRACE(fmt, ...)                   \
75     do {                                            \
76         ALOGE(fmt, ##__VA_ARGS__);                  \
77         SFTRACE_FORMAT_INSTANT(fmt, ##__VA_ARGS__); \
78     } while (false)
79 
80 namespace android {
81 
82 class ScopedTrace {
83 public:
84     template <typename... Args>
ScopedTrace(const char * fmt,Args &&...args)85     inline ScopedTrace(const char* fmt, Args&&... args) {
86         ::tracing_perfetto::traceFormatBegin(ATRACE_TAG, fmt, std::forward<Args>(args)...);
87     }
ScopedTrace(const char * name)88     inline ScopedTrace(const char* name) { SFTRACE_BEGIN(name); }
~ScopedTrace()89     inline ~ScopedTrace() { SFTRACE_END(); }
90 };
91 
92 class ScopedTraceForTrack {
93 public:
ScopedTraceForTrack(const char * trackName,const char * name)94     inline ScopedTraceForTrack(const char* trackName, const char* name)
95           : mCookie(getUniqueCookie()), mTrackName(trackName) {
96         SFTRACE_ASYNC_FOR_TRACK_BEGIN(mTrackName, name, mCookie);
97     }
~ScopedTraceForTrack()98     inline ~ScopedTraceForTrack() { SFTRACE_ASYNC_FOR_TRACK_END(mTrackName, mCookie); }
99 
100 private:
getUniqueCookie()101     static int32_t getUniqueCookie() {
102         static std::atomic<int32_t> sUniqueCookie = 1000;
103         return sUniqueCookie++;
104     }
105     int32_t mCookie;
106     const char* mTrackName;
107 };
108 
109 } // namespace android
110