xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/common/include/common/trace.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 
69 #define SFTRACE_FORMAT(fmt, ...) \
70     ::android::ScopedTrace PASTE(___tracer, __LINE__)(fmt, ##__VA_ARGS__)
71 
72 #define ALOGE_AND_TRACE(fmt, ...)                   \
73     do {                                            \
74         ALOGE(fmt, ##__VA_ARGS__);                  \
75         SFTRACE_FORMAT_INSTANT(fmt, ##__VA_ARGS__); \
76     } while (false)
77 
78 namespace android {
79 
80 class ScopedTrace {
81 public:
82     template <typename... Args>
ScopedTrace(const char * fmt,Args &&...args)83     inline ScopedTrace(const char* fmt, Args&&... args) {
84         ::tracing_perfetto::traceFormatBegin(ATRACE_TAG, fmt, std::forward<Args>(args)...);
85     }
ScopedTrace(const char * name)86     inline ScopedTrace(const char* name) { SFTRACE_BEGIN(name); }
~ScopedTrace()87     inline ~ScopedTrace() { SFTRACE_END(); }
88 };
89 
90 } // namespace android
91