SkTraceEvent.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright (c) 2014 Google Inc.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file.
  5. // This header file defines implementation details of how the trace macros in
  6. // SkTraceEventCommon.h collect and store trace events. Anything not
  7. // implementation-specific should go in SkTraceEventCommon.h instead of here.
  8. #ifndef SkTraceEvent_DEFINED
  9. #define SkTraceEvent_DEFINED
  10. #include "include/utils/SkEventTracer.h"
  11. #include "src/core/SkTraceEventCommon.h"
  12. #include <atomic>
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // Implementation specific tracing API definitions.
  15. // Makes it easier to add traces with a simple TRACE_EVENT0("skia", TRACE_FUNC).
  16. #if defined(_MSC_VER)
  17. #define TRACE_FUNC __FUNCSIG__
  18. #else
  19. #define TRACE_FUNC __PRETTY_FUNCTION__
  20. #endif
  21. // By default, const char* argument values are assumed to have long-lived scope
  22. // and will not be copied. Use this macro to force a const char* to be copied.
  23. #define TRACE_STR_COPY(str) \
  24. skia::tracing_internals::TraceStringWithCopy(str)
  25. #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
  26. *INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
  27. (SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags | \
  28. SkEventTracer::kEnabledForEventCallback_CategoryGroupEnabledFlags)
  29. // Get a pointer to the enabled state of the given trace category. Only
  30. // long-lived literal strings should be given as the category group. The
  31. // returned pointer can be held permanently in a local static for example. If
  32. // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
  33. // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
  34. // between the load of the tracing state and the call to
  35. // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
  36. // for best performance when tracing is disabled.
  37. // const uint8_t*
  38. // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
  39. #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
  40. SkEventTracer::GetInstance()->getCategoryGroupEnabled
  41. // Add a trace event to the platform tracing system.
  42. // SkEventTracer::Handle TRACE_EVENT_API_ADD_TRACE_EVENT(
  43. // char phase,
  44. // const uint8_t* category_group_enabled,
  45. // const char* name,
  46. // uint64_t id,
  47. // int num_args,
  48. // const char** arg_names,
  49. // const uint8_t* arg_types,
  50. // const uint64_t* arg_values,
  51. // unsigned char flags)
  52. #define TRACE_EVENT_API_ADD_TRACE_EVENT \
  53. SkEventTracer::GetInstance()->addTraceEvent
  54. // Set the duration field of a COMPLETE trace event.
  55. // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
  56. // const uint8_t* category_group_enabled,
  57. // const char* name,
  58. // SkEventTracer::Handle id)
  59. #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
  60. SkEventTracer::GetInstance()->updateTraceEventDuration
  61. // Defines visibility for classes in trace_event.h
  62. #define TRACE_EVENT_API_CLASS_EXPORT SK_API
  63. // We prepend this string to all category names, so that ALL Skia trace events are
  64. // disabled by default when tracing in Chrome.
  65. #define TRACE_CATEGORY_PREFIX "disabled-by-default-"
  66. ////////////////////////////////////////////////////////////////////////////////
  67. // Implementation detail: trace event macros create temporary variables
  68. // to keep instrumentation overhead low. These macros give each temporary
  69. // variable a unique name based on the line number to prevent name collisions.
  70. #define INTERNAL_TRACE_EVENT_UID3(a,b) \
  71. trace_event_unique_##a##b
  72. #define INTERNAL_TRACE_EVENT_UID2(a,b) \
  73. INTERNAL_TRACE_EVENT_UID3(a,b)
  74. #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
  75. INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
  76. // Implementation detail: internal macro to create static category.
  77. // No barriers are needed, because this code is designed to operate safely
  78. // even when the unsigned char* points to garbage data (which may be the case
  79. // on processors without cache coherency).
  80. #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
  81. category_group, atomic, category_group_enabled) \
  82. category_group_enabled = \
  83. reinterpret_cast<const uint8_t*>(atomic.load(std::memory_order_relaxed)); \
  84. if (!category_group_enabled) { \
  85. category_group_enabled = TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
  86. atomic.store(reinterpret_cast<intptr_t>(category_group_enabled), \
  87. std::memory_order_relaxed); \
  88. }
  89. #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
  90. static std::atomic<intptr_t> INTERNAL_TRACE_EVENT_UID(atomic){0}; \
  91. const uint8_t* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
  92. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
  93. TRACE_CATEGORY_PREFIX category_group, \
  94. INTERNAL_TRACE_EVENT_UID(atomic), \
  95. INTERNAL_TRACE_EVENT_UID(category_group_enabled));
  96. // Implementation detail: internal macro to create static category and add
  97. // event if the category is enabled.
  98. #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
  99. do { \
  100. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  101. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
  102. skia::tracing_internals::AddTraceEvent( \
  103. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  104. skia::tracing_internals::kNoEventId, flags, ##__VA_ARGS__); \
  105. } \
  106. } while (0)
  107. // Implementation detail: internal macro to create static category and add
  108. // event if the category is enabled.
  109. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
  110. flags, ...) \
  111. do { \
  112. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  113. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
  114. unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
  115. skia::tracing_internals::TraceID trace_event_trace_id( \
  116. id, &trace_event_flags); \
  117. skia::tracing_internals::AddTraceEvent( \
  118. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
  119. name, trace_event_trace_id.data(), trace_event_flags, \
  120. ##__VA_ARGS__); \
  121. } \
  122. } while (0)
  123. // Implementation detail: internal macro to create static category and add begin
  124. // event if the category is enabled. Also adds the end event when the scope
  125. // ends.
  126. #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
  127. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  128. skia::tracing_internals::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
  129. do { \
  130. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
  131. SkEventTracer::Handle h = skia::tracing_internals::AddTraceEvent( \
  132. TRACE_EVENT_PHASE_COMPLETE, \
  133. INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
  134. name, skia::tracing_internals::kNoEventId, \
  135. TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
  136. INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
  137. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
  138. } \
  139. } while (0)
  140. namespace skia {
  141. namespace tracing_internals {
  142. // Specify these values when the corresponding argument of AddTraceEvent is not
  143. // used.
  144. const int kZeroNumArgs = 0;
  145. const uint64_t kNoEventId = 0;
  146. // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
  147. // are by default mangled with the Process ID so that they are unlikely to
  148. // collide when the same pointer is used on different processes.
  149. class TraceID {
  150. public:
  151. TraceID(const void* id, unsigned char* flags)
  152. : data_(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(id))) {
  153. *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
  154. }
  155. TraceID(uint64_t id, unsigned char* flags)
  156. : data_(id) { (void)flags; }
  157. TraceID(unsigned int id, unsigned char* flags)
  158. : data_(id) { (void)flags; }
  159. TraceID(unsigned short id, unsigned char* flags)
  160. : data_(id) { (void)flags; }
  161. TraceID(unsigned char id, unsigned char* flags)
  162. : data_(id) { (void)flags; }
  163. TraceID(long long id, unsigned char* flags)
  164. : data_(static_cast<uint64_t>(id)) { (void)flags; }
  165. TraceID(long id, unsigned char* flags)
  166. : data_(static_cast<uint64_t>(id)) { (void)flags; }
  167. TraceID(int id, unsigned char* flags)
  168. : data_(static_cast<uint64_t>(id)) { (void)flags; }
  169. TraceID(short id, unsigned char* flags)
  170. : data_(static_cast<uint64_t>(id)) { (void)flags; }
  171. TraceID(signed char id, unsigned char* flags)
  172. : data_(static_cast<uint64_t>(id)) { (void)flags; }
  173. uint64_t data() const { return data_; }
  174. private:
  175. uint64_t data_;
  176. };
  177. // Simple union to store various types as uint64_t.
  178. union TraceValueUnion {
  179. bool as_bool;
  180. uint64_t as_uint;
  181. long long as_int;
  182. double as_double;
  183. const void* as_pointer;
  184. const char* as_string;
  185. };
  186. // Simple container for const char* that should be copied instead of retained.
  187. class TraceStringWithCopy {
  188. public:
  189. explicit TraceStringWithCopy(const char* str) : str_(str) {}
  190. operator const char* () const { return str_; }
  191. private:
  192. const char* str_;
  193. };
  194. // Define SetTraceValue for each allowed type. It stores the type and
  195. // value in the return arguments. This allows this API to avoid declaring any
  196. // structures so that it is portable to third_party libraries.
  197. #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
  198. union_member, \
  199. value_type_id) \
  200. static inline void SetTraceValue( \
  201. actual_type arg, \
  202. unsigned char* type, \
  203. uint64_t* value) { \
  204. TraceValueUnion type_value; \
  205. type_value.union_member = arg; \
  206. *type = value_type_id; \
  207. *value = type_value.as_uint; \
  208. }
  209. // Simpler form for int types that can be safely casted.
  210. #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
  211. value_type_id) \
  212. static inline void SetTraceValue( \
  213. actual_type arg, \
  214. unsigned char* type, \
  215. uint64_t* value) { \
  216. *type = value_type_id; \
  217. *value = static_cast<uint64_t>(arg); \
  218. }
  219. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(uint64_t, TRACE_VALUE_TYPE_UINT)
  220. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
  221. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
  222. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
  223. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
  224. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
  225. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
  226. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
  227. INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
  228. INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
  229. INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
  230. INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer, TRACE_VALUE_TYPE_POINTER)
  231. INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string, TRACE_VALUE_TYPE_STRING)
  232. INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
  233. TRACE_VALUE_TYPE_COPY_STRING)
  234. #undef INTERNAL_DECLARE_SET_TRACE_VALUE
  235. #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
  236. // These AddTraceEvent and AddTraceEvent template
  237. // functions are defined here instead of in the macro, because the arg_values
  238. // could be temporary objects, such as std::string. In order to store
  239. // pointers to the internal c_str and pass through to the tracing API,
  240. // the arg_values must live throughout these procedures.
  241. static inline SkEventTracer::Handle
  242. AddTraceEvent(
  243. char phase,
  244. const uint8_t* category_group_enabled,
  245. const char* name,
  246. uint64_t id,
  247. unsigned char flags) {
  248. return TRACE_EVENT_API_ADD_TRACE_EVENT(
  249. phase, category_group_enabled, name, id,
  250. kZeroNumArgs, nullptr, nullptr, nullptr, flags);
  251. }
  252. template<class ARG1_TYPE>
  253. static inline SkEventTracer::Handle
  254. AddTraceEvent(
  255. char phase,
  256. const uint8_t* category_group_enabled,
  257. const char* name,
  258. uint64_t id,
  259. unsigned char flags,
  260. const char* arg1_name,
  261. const ARG1_TYPE& arg1_val) {
  262. const int num_args = 1;
  263. uint8_t arg_types[1];
  264. uint64_t arg_values[1];
  265. SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
  266. return TRACE_EVENT_API_ADD_TRACE_EVENT(
  267. phase, category_group_enabled, name, id,
  268. num_args, &arg1_name, arg_types, arg_values, flags);
  269. }
  270. template<class ARG1_TYPE, class ARG2_TYPE>
  271. static inline SkEventTracer::Handle
  272. AddTraceEvent(
  273. char phase,
  274. const uint8_t* category_group_enabled,
  275. const char* name,
  276. uint64_t id,
  277. unsigned char flags,
  278. const char* arg1_name,
  279. const ARG1_TYPE& arg1_val,
  280. const char* arg2_name,
  281. const ARG2_TYPE& arg2_val) {
  282. const int num_args = 2;
  283. const char* arg_names[2] = { arg1_name, arg2_name };
  284. unsigned char arg_types[2];
  285. uint64_t arg_values[2];
  286. SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
  287. SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
  288. return TRACE_EVENT_API_ADD_TRACE_EVENT(
  289. phase, category_group_enabled, name, id,
  290. num_args, arg_names, arg_types, arg_values, flags);
  291. }
  292. // Used by TRACE_EVENTx macros. Do not use directly.
  293. class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
  294. public:
  295. // Note: members of data_ intentionally left uninitialized. See Initialize.
  296. ScopedTracer() : p_data_(nullptr) {}
  297. ~ScopedTracer() {
  298. if (p_data_ && *data_.category_group_enabled)
  299. TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
  300. data_.category_group_enabled, data_.name, data_.event_handle);
  301. }
  302. void Initialize(const uint8_t* category_group_enabled,
  303. const char* name,
  304. SkEventTracer::Handle event_handle) {
  305. data_.category_group_enabled = category_group_enabled;
  306. data_.name = name;
  307. data_.event_handle = event_handle;
  308. p_data_ = &data_;
  309. }
  310. private:
  311. // This Data struct workaround is to avoid initializing all the members
  312. // in Data during construction of this object, since this object is always
  313. // constructed, even when tracing is disabled. If the members of Data were
  314. // members of this class instead, compiler warnings occur about potential
  315. // uninitialized accesses.
  316. struct Data {
  317. const uint8_t* category_group_enabled;
  318. const char* name;
  319. SkEventTracer::Handle event_handle;
  320. };
  321. Data* p_data_;
  322. Data data_;
  323. };
  324. } // namespace tracing_internals
  325. } // namespace skia
  326. #endif