blob: 6e5e0e013a453c0dd961c7c46d37c2c02ddb21ed [file] [log] [blame]
Fairphone ODM25c12f52023-12-15 17:24:06 +08001/*
2 * Copyright (C) 2016 The Android Open Source Project
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 */
16
17#ifndef ART_LIBARTBASE_BASE_SYSTRACE_H_
18#define ART_LIBARTBASE_BASE_SYSTRACE_H_
19
20#include <sstream>
21#include <string>
22
23#include "android-base/stringprintf.h"
24#include "macros.h"
25#include "palette/palette.h"
26
27namespace art {
28
29inline bool ATraceEnabled() {
30 bool enabled = false;
31 if (UNLIKELY(PaletteTraceEnabled(&enabled) == PALETTE_STATUS_OK && enabled)) {
32 return true;
33 } else {
34 return false;
35 }
36}
37
38inline void ATraceBegin(const char* name) {
39 PaletteTraceBegin(name);
40}
41
42inline void ATraceEnd() {
43 PaletteTraceEnd();
44}
45
46inline void ATraceIntegerValue(const char* name, int32_t value) {
47 PaletteTraceIntegerValue(name, value);
48}
49
50class ScopedTrace {
51 public:
52 explicit ScopedTrace(const char* name) {
53 ATraceBegin(name);
54 }
55 template <typename Fn>
56 explicit ScopedTrace(Fn fn) {
57 if (UNLIKELY(ATraceEnabled())) {
58 ATraceBegin(fn().c_str());
59 }
60 }
61
62 explicit ScopedTrace(const std::string& name) : ScopedTrace(name.c_str()) {}
63 ScopedTrace(ScopedTrace&&) = default;
64
65 ~ScopedTrace() {
66 ATraceEnd();
67 }
68};
69
70// Helper for the SCOPED_TRACE macro. Do not use directly.
71class ScopedTraceNoStart {
72 public:
73 ScopedTraceNoStart() {
74 }
75
76 ~ScopedTraceNoStart() {
77 ATraceEnd();
78 }
79
80 // Message helper for the macro. Do not use directly.
81 class ScopedTraceMessageHelper {
82 public:
83 ScopedTraceMessageHelper() {
84 }
85 ~ScopedTraceMessageHelper() {
86 ATraceBegin(buffer_.str().c_str());
87 }
88
89 std::ostream& stream() {
90 return buffer_;
91 }
92
93 private:
94 std::ostringstream buffer_;
95 };
96};
97
98#define SCOPED_TRACE \
99 ::art::ScopedTraceNoStart APPEND_TOKENS_AFTER_EVAL(trace, __LINE__) ; \
100 (ATraceEnabled()) && ::art::ScopedTraceNoStart::ScopedTraceMessageHelper().stream()
101
102} // namespace art
103
104#endif // ART_LIBARTBASE_BASE_SYSTRACE_H_