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 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 27 namespace art { 28 ATraceEnabled()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 ATraceBegin(const char * name)38inline void ATraceBegin(const char* name) { 39 PaletteTraceBegin(name); 40 } 41 ATraceEnd()42inline void ATraceEnd() { 43 PaletteTraceEnd(); 44 } 45 ATraceIntegerValue(const char * name,int32_t value)46inline void ATraceIntegerValue(const char* name, int32_t value) { 47 PaletteTraceIntegerValue(name, value); 48 } 49 50 class ScopedTrace { 51 public: ScopedTrace(const char * name)52 explicit ScopedTrace(const char* name) { 53 ATraceBegin(name); 54 } 55 template <typename Fn> ScopedTrace(Fn fn)56 explicit ScopedTrace(Fn fn) { 57 if (UNLIKELY(ATraceEnabled())) { 58 ATraceBegin(fn().c_str()); 59 } 60 } 61 ScopedTrace(const std::string & name)62 explicit ScopedTrace(const std::string& name) : ScopedTrace(name.c_str()) {} 63 ~ScopedTrace()64 ~ScopedTrace() { 65 ATraceEnd(); 66 } 67 }; 68 69 // Helper for the SCOPED_TRACE macro. Do not use directly. 70 class ScopedTraceNoStart { 71 public: ScopedTraceNoStart()72 ScopedTraceNoStart() { 73 } 74 ~ScopedTraceNoStart()75 ~ScopedTraceNoStart() { 76 ATraceEnd(); 77 } 78 79 // Message helper for the macro. Do not use directly. 80 class ScopedTraceMessageHelper { 81 public: ScopedTraceMessageHelper()82 ScopedTraceMessageHelper() { 83 } ~ScopedTraceMessageHelper()84 ~ScopedTraceMessageHelper() { 85 ATraceBegin(buffer_.str().c_str()); 86 } 87 stream()88 std::ostream& stream() { 89 return buffer_; 90 } 91 92 private: 93 std::ostringstream buffer_; 94 }; 95 }; 96 97 #define SCOPED_TRACE \ 98 ::art::ScopedTraceNoStart APPEND_TOKENS_AFTER_EVAL(trace, __LINE__) ; \ 99 (ATraceEnabled()) && ::art::ScopedTraceNoStart::ScopedTraceMessageHelper().stream() 100 101 } // namespace art 102 103 #endif // ART_LIBARTBASE_BASE_SYSTRACE_H_ 104