Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1 | /* |
| 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 Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_SYSTRACE_H_ |
| 18 | #define ART_LIBARTBASE_BASE_SYSTRACE_H_ |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 19 | |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 20 | #include <sstream> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Andreas Gampe | dfebbac | 2017-12-06 14:17:22 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
Andreas Gampe | 837f3f0 | 2018-04-23 15:50:00 -0700 | [diff] [blame] | 24 | #include "macros.h" |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 25 | #include "palette/palette.h" |
Andreas Gampe | dfebbac | 2017-12-06 14:17:22 -0800 | [diff] [blame] | 26 | |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 29 | inline 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 | |
| 38 | inline void ATraceBegin(const char* name) { |
| 39 | PaletteTraceBegin(name); |
| 40 | } |
| 41 | |
| 42 | inline void ATraceEnd() { |
| 43 | PaletteTraceEnd(); |
| 44 | } |
| 45 | |
| 46 | inline void ATraceIntegerValue(const char* name, int32_t value) { |
| 47 | PaletteTraceIntegerValue(name, value); |
| 48 | } |
| 49 | |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 50 | class ScopedTrace { |
| 51 | public: |
| 52 | explicit ScopedTrace(const char* name) { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 53 | ATraceBegin(name); |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 54 | } |
Andreas Gampe | dfebbac | 2017-12-06 14:17:22 -0800 | [diff] [blame] | 55 | template <typename Fn> |
| 56 | explicit ScopedTrace(Fn fn) { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 57 | if (UNLIKELY(ATraceEnabled())) { |
| 58 | ATraceBegin(fn().c_str()); |
Andreas Gampe | dfebbac | 2017-12-06 14:17:22 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 61 | |
| 62 | explicit ScopedTrace(const std::string& name) : ScopedTrace(name.c_str()) {} |
| 63 | |
| 64 | ~ScopedTrace() { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 65 | ATraceEnd(); |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 66 | } |
| 67 | }; |
| 68 | |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 69 | // Helper for the SCOPED_TRACE macro. Do not use directly. |
| 70 | class ScopedTraceNoStart { |
| 71 | public: |
| 72 | ScopedTraceNoStart() { |
| 73 | } |
| 74 | |
| 75 | ~ScopedTraceNoStart() { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 76 | ATraceEnd(); |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Message helper for the macro. Do not use directly. |
| 80 | class ScopedTraceMessageHelper { |
| 81 | public: |
| 82 | ScopedTraceMessageHelper() { |
| 83 | } |
| 84 | ~ScopedTraceMessageHelper() { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 85 | ATraceBegin(buffer_.str().c_str()); |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::ostream& stream() { |
| 89 | return buffer_; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | std::ostringstream buffer_; |
| 94 | }; |
| 95 | }; |
| 96 | |
| 97 | #define SCOPED_TRACE \ |
Andreas Gampe | 837f3f0 | 2018-04-23 15:50:00 -0700 | [diff] [blame] | 98 | ::art::ScopedTraceNoStart APPEND_TOKENS_AFTER_EVAL(trace, __LINE__) ; \ |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 99 | (ATraceEnabled()) && ::art::ScopedTraceNoStart::ScopedTraceMessageHelper().stream() |
Andreas Gampe | dfebbac | 2017-12-06 14:17:22 -0800 | [diff] [blame] | 100 | |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 101 | } // namespace art |
| 102 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 103 | #endif // ART_LIBARTBASE_BASE_SYSTRACE_H_ |