File: trace.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (82 lines) | stat: -rw-r--r-- 2,251 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef ANDROID_PDX_TRACE_H_
#define ANDROID_PDX_TRACE_H_

#include <array>

#include <utils/Trace.h>

// Enables internal tracing in libpdx. This is disabled by default to avoid
// spamming the trace buffers during normal trace activities. libpdx must be
// built with this set to true to enable internal tracing.
#ifndef PDX_LIB_TRACE_ENABLED
#define PDX_LIB_TRACE_ENABLED false
#endif

namespace android {
namespace pdx {

// Utility to generate scoped tracers with arguments.
class ScopedTraceArgs {
 public:
  template <typename... Args>
  ScopedTraceArgs(uint64_t tag, const char* format, Args&&... args)
      : tag_{tag} {
    if (atrace_is_tag_enabled(tag_)) {
      std::array<char, 1024> buffer;
      snprintf(buffer.data(), buffer.size(), format,
               std::forward<Args>(args)...);
      atrace_begin(tag_, buffer.data());
    }
  }

  ~ScopedTraceArgs() { atrace_end(tag_); }

 private:
  uint64_t tag_;

  ScopedTraceArgs(const ScopedTraceArgs&) = delete;
  void operator=(const ScopedTraceArgs&) = delete;
};

// Utility to generate scoped tracers.
class ScopedTrace {
 public:
  template <typename... Args>
  ScopedTrace(uint64_t tag, bool enabled, const char* name)
      : tag_{tag}, enabled_{enabled} {
    if (enabled_)
      atrace_begin(tag_, name);
  }

  ~ScopedTrace() {
    if (enabled_)
      atrace_end(tag_);
  }

 private:
  uint64_t tag_;
  bool enabled_;

  ScopedTrace(const ScopedTrace&) = delete;
  void operator=(const ScopedTrace&) = delete;
};

}  // namespace pdx
}  // namespace android

// Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro
// defined in utils/Trace.h.
#define PDX_TRACE_FORMAT(format, ...)                         \
  ::android::pdx::ScopedTraceArgs PASTE(__tracer, __LINE__) { \
    ATRACE_TAG, format, ##__VA_ARGS__                         \
  }

// TODO(eieio): Rename this to PDX_LIB_TRACE_NAME() for internal use by libpdx
// and rename internal uses inside the library. This version is only enabled
// when PDX_LIB_TRACE_ENABLED is true.
#define PDX_TRACE_NAME(name)                              \
  ::android::pdx::ScopedTrace PASTE(__tracer, __LINE__) { \
    ATRACE_TAG, PDX_LIB_TRACE_ENABLED, name               \
  }

#endif  // ANDROID_PDX_TRACE_H_