• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()29 inline 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)38 inline void ATraceBegin(const char* name) {
39   PaletteTraceBegin(name);
40 }
41 
ATraceEnd()42 inline void ATraceEnd() {
43   PaletteTraceEnd();
44 }
45 
ATraceIntegerValue(const char * name,int32_t value)46 inline 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(ScopedTrace&&) = default;
64 
~ScopedTrace()65   ~ScopedTrace() {
66     ATraceEnd();
67   }
68 };
69 
70 // Helper for the SCOPED_TRACE macro. Do not use directly.
71 class ScopedTraceNoStart {
72  public:
ScopedTraceNoStart()73   ScopedTraceNoStart() {
74   }
75 
~ScopedTraceNoStart()76   ~ScopedTraceNoStart() {
77     ATraceEnd();
78   }
79 
80   // Message helper for the macro. Do not use directly.
81   class ScopedTraceMessageHelper {
82    public:
ScopedTraceMessageHelper()83     ScopedTraceMessageHelper() {
84     }
~ScopedTraceMessageHelper()85     ~ScopedTraceMessageHelper() {
86       ATraceBegin(buffer_.str().c_str());
87     }
88 
stream()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_
105