blob: 30bba49f117a9a01a30df0ee8342358e6eb47e20 [file] [log] [blame]
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -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
David Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_SYSTRACE_H_
18#define ART_LIBARTBASE_BASE_SYSTRACE_H_
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080019
Andreas Gampec6548162017-12-08 12:15:22 -080020#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <string>
22
Andreas Gampedfebbac2017-12-06 14:17:22 -080023#include "android-base/stringprintf.h"
Andreas Gampe837f3f02018-04-23 15:50:00 -070024#include "macros.h"
Orion Hodson119733d2019-01-30 15:14:41 +000025#include "palette/palette.h"
Andreas Gampedfebbac2017-12-06 14:17:22 -080026
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080027namespace art {
28
Orion Hodson119733d2019-01-30 15:14:41 +000029inline bool ATraceEnabled() {
30 int enabled = 0;
31 if (UNLIKELY(PaletteTraceEnabled(&enabled) == PaletteStatus::kOkay && enabled != 0)) {
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
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080050class ScopedTrace {
51 public:
52 explicit ScopedTrace(const char* name) {
Orion Hodson119733d2019-01-30 15:14:41 +000053 ATraceBegin(name);
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080054 }
Andreas Gampedfebbac2017-12-06 14:17:22 -080055 template <typename Fn>
56 explicit ScopedTrace(Fn fn) {
Orion Hodson119733d2019-01-30 15:14:41 +000057 if (UNLIKELY(ATraceEnabled())) {
58 ATraceBegin(fn().c_str());
Andreas Gampedfebbac2017-12-06 14:17:22 -080059 }
60 }
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080061
62 explicit ScopedTrace(const std::string& name) : ScopedTrace(name.c_str()) {}
63
64 ~ScopedTrace() {
Orion Hodson119733d2019-01-30 15:14:41 +000065 ATraceEnd();
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080066 }
67};
68
Andreas Gampec6548162017-12-08 12:15:22 -080069// Helper for the SCOPED_TRACE macro. Do not use directly.
70class ScopedTraceNoStart {
71 public:
72 ScopedTraceNoStart() {
73 }
74
75 ~ScopedTraceNoStart() {
Orion Hodson119733d2019-01-30 15:14:41 +000076 ATraceEnd();
Andreas Gampec6548162017-12-08 12:15:22 -080077 }
78
79 // Message helper for the macro. Do not use directly.
80 class ScopedTraceMessageHelper {
81 public:
82 ScopedTraceMessageHelper() {
83 }
84 ~ScopedTraceMessageHelper() {
Orion Hodson119733d2019-01-30 15:14:41 +000085 ATraceBegin(buffer_.str().c_str());
Andreas Gampec6548162017-12-08 12:15:22 -080086 }
87
88 std::ostream& stream() {
89 return buffer_;
90 }
91
92 private:
93 std::ostringstream buffer_;
94 };
95};
96
97#define SCOPED_TRACE \
Andreas Gampe837f3f02018-04-23 15:50:00 -070098 ::art::ScopedTraceNoStart APPEND_TOKENS_AFTER_EVAL(trace, __LINE__) ; \
Orion Hodson119733d2019-01-30 15:14:41 +000099 (ATraceEnabled()) && ::art::ScopedTraceNoStart::ScopedTraceMessageHelper().stream()
Andreas Gampedfebbac2017-12-06 14:17:22 -0800100
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -0800101} // namespace art
102
David Sehrc431b9d2018-03-02 12:01:51 -0800103#endif // ART_LIBARTBASE_BASE_SYSTRACE_H_