OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Trace events are for tracking application performance and resource usage. | 5 // Trace events are for tracking application performance and resource usage. |
6 // Macros are provided to track: | 6 // Macros are provided to track: |
7 // Begin and end of function calls | 7 // Begin and end of function calls |
8 // Counters | 8 // Counters |
9 // | 9 // |
10 // Events are issued against categories. Whereas LOG's | 10 // Events are issued against categories. Whereas LOG's |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 #include <vector> | 153 #include <vector> |
154 | 154 |
155 #include "base/callback.h" | 155 #include "base/callback.h" |
156 #include "base/hash_tables.h" | 156 #include "base/hash_tables.h" |
157 #include "base/memory/ref_counted_memory.h" | 157 #include "base/memory/ref_counted_memory.h" |
158 #include "base/string_util.h" | 158 #include "base/string_util.h" |
159 #include "base/synchronization/lock.h" | 159 #include "base/synchronization/lock.h" |
160 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 160 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
161 #include "base/timer.h" | 161 #include "base/timer.h" |
162 | 162 |
| 163 //////////////////////////////////////////////////////////////////////////////// |
| 164 /// Customize these defines to call through to platform tracing APIs. |
| 165 |
| 166 // volatile bool* |
| 167 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) |
| 168 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ |
| 169 base::debug::TraceLog::GetCategoryEnabled |
| 170 |
| 171 // Returns the threshold_begin_id used by TRACE_IF_LONGER_THAN macros. |
| 172 // int TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 173 // char phase, |
| 174 // const volatile bool* category_enabled, |
| 175 // const char* name, |
| 176 // uint64 id, |
| 177 // int num_args, |
| 178 // const char** arg_names, |
| 179 // const uint8* arg_types, |
| 180 // const uint64* arg_values, |
| 181 // int threshold_begin_id, |
| 182 // int64 threshold, |
| 183 // uint8 flags) |
| 184 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ |
| 185 base::debug::TraceLog::GetInstance()->AddTraceEvent |
| 186 |
| 187 // void TRACE_EVENT_API_ADD_COUNTER_EVENT( |
| 188 // const volatile bool* category_enabled, |
| 189 // const char* name, |
| 190 // uint64 id, |
| 191 // const char* arg1_name, int32 arg1_val, |
| 192 // const char* arg2_name, int32 arg2_val, |
| 193 // uint8 flags) |
| 194 #define TRACE_EVENT_API_ADD_COUNTER_EVENT \ |
| 195 base::debug::TraceLog::GetInstance()->AddCounterEvent |
| 196 |
| 197 // Mangle |pointer| with a process ID hash so that if |pointer| occurs on more |
| 198 // than one process, it will not collide in the trace data. |
| 199 // uint64 TRACE_EVENT_API_GET_ID_FROM_POINTER(void* pointer) |
| 200 #define TRACE_EVENT_API_GET_ID_FROM_POINTER \ |
| 201 base::debug::TraceLog::GetInstance()->GetInterProcessID |
| 202 |
| 203 //////////////////////////////////////////////////////////////////////////////// |
| 204 |
163 // By default, const char* argument values are assumed to have long-lived scope | 205 // By default, const char* argument values are assumed to have long-lived scope |
164 // and will not be copied. Use this macro to force a const char* to be copied. | 206 // and will not be copied. Use this macro to force a const char* to be copied. |
165 #define TRACE_STR_COPY(str) base::debug::TraceValue::StringWithCopy(str) | 207 #define TRACE_STR_COPY(str) \ |
| 208 trace_event_internal::TraceStringWithCopy(str) |
166 | 209 |
167 // Older style trace macros with explicit id and extra data | 210 // Older style trace macros with explicit id and extra data |
168 // Only these macros result in publishing data to ETW as currently implemented. | 211 // Only these macros result in publishing data to ETW as currently implemented. |
169 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 212 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
170 base::debug::TraceLog::AddTraceEventEtw( \ | 213 base::debug::TraceLog::AddTraceEventEtw( \ |
171 TRACE_EVENT_PHASE_BEGIN, \ | 214 TRACE_EVENT_PHASE_BEGIN, \ |
172 name, reinterpret_cast<const void*>(id), extra) | 215 name, reinterpret_cast<const void*>(id), extra) |
173 | 216 |
174 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 217 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
175 base::debug::TraceLog::AddTraceEventEtw( \ | 218 base::debug::TraceLog::AddTraceEventEtw( \ |
176 TRACE_EVENT_PHASE_END, \ | 219 TRACE_EVENT_PHASE_END, \ |
177 name, reinterpret_cast<const void*>(id), extra) | 220 name, reinterpret_cast<const void*>(id), extra) |
178 | 221 |
179 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ | 222 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ |
180 base::debug::TraceLog::AddTraceEventEtw( \ | 223 base::debug::TraceLog::AddTraceEventEtw( \ |
181 TRACE_EVENT_PHASE_INSTANT, \ | 224 TRACE_EVENT_PHASE_INSTANT, \ |
182 name, reinterpret_cast<const void*>(id), extra) | 225 name, reinterpret_cast<const void*>(id), extra) |
183 | 226 |
184 // Records a pair of begin and end events called "name" for the current | 227 // Records a pair of begin and end events called "name" for the current |
185 // scope, with 0, 1 or 2 associated arguments. If the category is not | 228 // scope, with 0, 1 or 2 associated arguments. If the category is not |
186 // enabled, then this does nothing. | 229 // enabled, then this does nothing. |
187 // - category and name strings must have application lifetime (statics or | 230 // - category and name strings must have application lifetime (statics or |
188 // literals). They may not include " chars. | 231 // literals). They may not include " chars. |
189 #define TRACE_EVENT0(category, name) \ | 232 #define TRACE_EVENT0(category, name) \ |
190 TRACE_EVENT1(category, name, NULL, 0) | 233 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) |
191 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | 234 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ |
192 TRACE_EVENT2(category, name, arg1_name, arg1_val, NULL, 0) | 235 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) |
193 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 236 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
194 INTERNAL_TRACE_EVENT_ADD_SCOPED( \ | 237 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \ |
195 category, name, arg1_name, arg1_val, arg2_name, arg2_val) | 238 arg2_name, arg2_val) |
196 | 239 |
197 // Same as TRACE_EVENT except that they are not included in official builds. | 240 // Same as TRACE_EVENT except that they are not included in official builds. |
198 #ifdef OFFICIAL_BUILD | 241 #ifdef OFFICIAL_BUILD |
199 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0 | 242 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0 |
200 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0 | 243 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0 |
201 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ | 244 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ |
202 arg2_name, arg2_val) (void)0 | 245 arg2_name, arg2_val) (void)0 |
203 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0 | 246 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0 |
204 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | 247 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ |
205 (void)0 | 248 (void)0 |
(...skipping 16 matching lines...) Expand all Loading... |
222 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | 265 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ |
223 arg2_name, arg2_val) | 266 arg2_name, arg2_val) |
224 #endif | 267 #endif |
225 | 268 |
226 // Records a single event called "name" immediately, with 0, 1 or 2 | 269 // Records a single event called "name" immediately, with 0, 1 or 2 |
227 // associated arguments. If the category is not enabled, then this | 270 // associated arguments. If the category is not enabled, then this |
228 // does nothing. | 271 // does nothing. |
229 // - category and name strings must have application lifetime (statics or | 272 // - category and name strings must have application lifetime (statics or |
230 // literals). They may not include " chars. | 273 // literals). They may not include " chars. |
231 #define TRACE_EVENT_INSTANT0(category, name) \ | 274 #define TRACE_EVENT_INSTANT0(category, name) \ |
232 TRACE_EVENT_INSTANT1(category, name, NULL, 0) | 275 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 276 category, name, TRACE_EVENT_FLAG_NONE) |
233 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | 277 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ |
234 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, NULL, 0) | 278 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 279 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) |
235 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | 280 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ |
236 arg2_name, arg2_val) \ | 281 arg2_name, arg2_val) \ |
237 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | 282 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
238 category, name, arg1_name, arg1_val, arg2_name, arg2_val, \ | 283 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ |
239 TRACE_EVENT_FLAG_NONE) | 284 arg2_name, arg2_val) |
240 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ | 285 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ |
241 TRACE_EVENT_COPY_INSTANT1(category, name, NULL, 0) | 286 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 287 category, name, TRACE_EVENT_FLAG_COPY) |
242 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ | 288 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ |
243 TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, NULL, 0) | 289 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 290 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) |
244 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ | 291 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ |
245 arg2_name, arg2_val) \ | 292 arg2_name, arg2_val) \ |
246 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | 293 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
247 category, name, \ | 294 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
248 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 295 arg2_name, arg2_val) |
249 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
250 TRACE_EVENT_FLAG_COPY) | |
251 | 296 |
252 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 | 297 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 |
253 // associated arguments. If the category is not enabled, then this | 298 // associated arguments. If the category is not enabled, then this |
254 // does nothing. | 299 // does nothing. |
255 // - category and name strings must have application lifetime (statics or | 300 // - category and name strings must have application lifetime (statics or |
256 // literals). They may not include " chars. | 301 // literals). They may not include " chars. |
257 #define TRACE_EVENT_BEGIN0(category, name) \ | 302 #define TRACE_EVENT_BEGIN0(category, name) \ |
258 TRACE_EVENT_BEGIN1(category, name, NULL, 0) | 303 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 304 category, name, TRACE_EVENT_FLAG_NONE) |
259 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ | 305 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ |
260 TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, NULL, 0) | 306 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 307 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) |
261 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ | 308 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ |
262 arg2_name, arg2_val) \ | 309 arg2_name, arg2_val) \ |
263 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | 310 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
264 category, name, arg1_name, arg1_val, arg2_name, arg2_val, \ | 311 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ |
265 TRACE_EVENT_FLAG_NONE) | 312 arg2_name, arg2_val) |
266 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ | 313 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ |
267 TRACE_EVENT_COPY_BEGIN1(category, name, NULL, 0) | 314 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 315 category, name, TRACE_EVENT_FLAG_COPY) |
268 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ | 316 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ |
269 TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, NULL, 0) | 317 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 318 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) |
270 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ | 319 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ |
271 arg2_name, arg2_val) \ | 320 arg2_name, arg2_val) \ |
272 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | 321 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
273 category, name, \ | 322 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
274 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 323 arg2_name, arg2_val) |
275 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
276 TRACE_EVENT_FLAG_COPY) | |
277 | 324 |
278 // Records a single END event for "name" immediately. If the category | 325 // Records a single END event for "name" immediately. If the category |
279 // is not enabled, then this does nothing. | 326 // is not enabled, then this does nothing. |
280 // - category and name strings must have application lifetime (statics or | 327 // - category and name strings must have application lifetime (statics or |
281 // literals). They may not include " chars. | 328 // literals). They may not include " chars. |
282 #define TRACE_EVENT_END0(category, name) \ | 329 #define TRACE_EVENT_END0(category, name) \ |
283 TRACE_EVENT_END1(category, name, NULL, 0) | 330 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 331 category, name, TRACE_EVENT_FLAG_NONE) |
284 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ | 332 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ |
285 TRACE_EVENT_END2(category, name, arg1_name, arg1_val, NULL, 0) | 333 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 334 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) |
286 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ | 335 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ |
287 arg2_name, arg2_val) \ | 336 arg2_name, arg2_val) \ |
288 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | 337 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
289 category, name, arg1_name, arg1_val, arg2_name, arg2_val, \ | 338 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ |
290 TRACE_EVENT_FLAG_NONE) | 339 arg2_name, arg2_val) |
291 #define TRACE_EVENT_COPY_END0(category, name) \ | 340 #define TRACE_EVENT_COPY_END0(category, name) \ |
292 TRACE_EVENT_COPY_END1(category, name, NULL, 0) | 341 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 342 category, name, TRACE_EVENT_FLAG_COPY) |
293 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ | 343 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ |
294 TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, NULL, 0) | 344 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 345 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) |
295 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ | 346 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ |
296 arg2_name, arg2_val) \ | 347 arg2_name, arg2_val) \ |
297 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | 348 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
298 category, name, \ | 349 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
299 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 350 arg2_name, arg2_val) |
300 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
301 TRACE_EVENT_FLAG_COPY) | |
302 | 351 |
303 // Time threshold event: | 352 // Time threshold event: |
304 // Only record the event if the duration is greater than the specified | 353 // Only record the event if the duration is greater than the specified |
305 // threshold_us (time in microseconds). | 354 // threshold_us (time in microseconds). |
306 // Records a pair of begin and end events called "name" for the current | 355 // Records a pair of begin and end events called "name" for the current |
307 // scope, with 0, 1 or 2 associated arguments. If the category is not | 356 // scope, with 0, 1 or 2 associated arguments. If the category is not |
308 // enabled, then this does nothing. | 357 // enabled, then this does nothing. |
309 // - category and name strings must have application lifetime (statics or | 358 // - category and name strings must have application lifetime (statics or |
310 // literals). They may not include " chars. | 359 // literals). They may not include " chars. |
311 #define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \ | 360 #define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \ |
312 TRACE_EVENT_IF_LONGER_THAN1(threshold_us, category, name, NULL, 0) | 361 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, category, name) |
313 #define TRACE_EVENT_IF_LONGER_THAN1( \ | 362 #define TRACE_EVENT_IF_LONGER_THAN1( \ |
314 threshold_us, category, name, arg1_name, arg1_val) \ | 363 threshold_us, category, name, arg1_name, arg1_val) \ |
315 TRACE_EVENT_IF_LONGER_THAN2(threshold_us, category, name, \ | 364 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ |
316 arg1_name, arg1_val, NULL, 0) | 365 threshold_us, category, name, arg1_name, arg1_val) |
317 #define TRACE_EVENT_IF_LONGER_THAN2( \ | 366 #define TRACE_EVENT_IF_LONGER_THAN2( \ |
318 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 367 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
319 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, \ | 368 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ |
320 category, name, arg1_name, arg1_val, arg2_name, arg2_val) | 369 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) |
321 | 370 |
322 // Records the value of a counter called "name" immediately. Value | 371 // Records the value of a counter called "name" immediately. Value |
323 // must be representable as a 32 bit integer. | 372 // must be representable as a 32 bit integer. |
324 // - category and name strings must have application lifetime (statics or | 373 // - category and name strings must have application lifetime (statics or |
325 // literals). They may not include " chars. | 374 // literals). They may not include " chars. |
326 #define TRACE_COUNTER1(category, name, value) \ | 375 #define TRACE_COUNTER1(category, name, value) \ |
327 TRACE_COUNTER2(category, name, "value", value, NULL, 0) | 376 TRACE_COUNTER2(category, name, "value", value, NULL, 0) |
328 #define TRACE_COPY_COUNTER1(category, name, value) \ | 377 #define TRACE_COPY_COUNTER1(category, name, value) \ |
329 TRACE_COPY_COUNTER2(category, name, "value", value, NULL, 0) | 378 TRACE_COPY_COUNTER2(category, name, "value", value, NULL, 0) |
330 | 379 |
331 // Records the values of a multi-parted counter called "name" immediately. | 380 // Records the values of a multi-parted counter called "name" immediately. |
332 // The UI will treat value1 and value2 as parts of a whole, displaying their | 381 // The UI will treat value1 and value2 as parts of a whole, displaying their |
333 // values as a stacked-bar chart. | 382 // values as a stacked-bar chart. |
334 // - category and name strings must have application lifetime (statics or | 383 // - category and name strings must have application lifetime (statics or |
335 // literals). They may not include " chars. | 384 // literals). They may not include " chars. |
336 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ | 385 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ |
337 value2_name, value2_val) \ | 386 value2_name, value2_val) \ |
338 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 387 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ |
339 category, name, 0, value1_name, value1_val, value2_name, value2_val, \ | 388 category, name, trace_event_internal::kNoEventId, \ |
| 389 value1_name, value1_val, value2_name, value2_val, \ |
340 TRACE_EVENT_FLAG_NONE) | 390 TRACE_EVENT_FLAG_NONE) |
341 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ | 391 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ |
342 value2_name, value2_val) \ | 392 value2_name, value2_val) \ |
343 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 393 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ |
344 category, name, 0, \ | 394 category, name, trace_event_internal::kNoEventId, \ |
345 value1_name, value1_val, \ | 395 value1_name, value1_val, value2_name, value2_val, \ |
346 value2_name, value2_val, \ | |
347 TRACE_EVENT_FLAG_COPY) | 396 TRACE_EVENT_FLAG_COPY) |
348 | 397 |
349 // Records the value of a counter called "name" immediately. Value | 398 // Records the value of a counter called "name" immediately. Value |
350 // must be representable as a 32 bit integer. | 399 // must be representable as a 32 bit integer. |
351 // - category and name strings must have application lifetime (statics or | 400 // - category and name strings must have application lifetime (statics or |
352 // literals). They may not include " chars. | 401 // literals). They may not include " chars. |
353 // - |id| is used to disambiguate counters with the same name. It must either | 402 // - |id| is used to disambiguate counters with the same name. It must either |
354 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 403 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
355 // will be xored with a hash of the process ID so that the same pointer on | 404 // will be xored with a hash of the process ID so that the same pointer on |
356 // two different processes will not collide. | 405 // two different processes will not collide. |
(...skipping 28 matching lines...) Expand all Loading... |
385 // Records a single START event called "name" immediately, with 0, 1 or 2 | 434 // Records a single START event called "name" immediately, with 0, 1 or 2 |
386 // associated arguments. If the category is not enabled, then this | 435 // associated arguments. If the category is not enabled, then this |
387 // does nothing. | 436 // does nothing. |
388 // - category and name strings must have application lifetime (statics or | 437 // - category and name strings must have application lifetime (statics or |
389 // literals). They may not include " chars. | 438 // literals). They may not include " chars. |
390 // - |id| is used to match the START event with the FINISH event. It must either | 439 // - |id| is used to match the START event with the FINISH event. It must either |
391 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 440 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
392 // will be xored with a hash of the process ID so that the same pointer on | 441 // will be xored with a hash of the process ID so that the same pointer on |
393 // two different processes will not collide. | 442 // two different processes will not collide. |
394 #define TRACE_EVENT_START0(category, name, id) \ | 443 #define TRACE_EVENT_START0(category, name, id) \ |
395 TRACE_EVENT_START1(category, name, id, NULL, 0) | 444 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 445 category, name, id, TRACE_EVENT_FLAG_HAS_ID) |
396 #define TRACE_EVENT_START1(category, name, id, arg1_name, arg1_val) \ | 446 #define TRACE_EVENT_START1(category, name, id, arg1_name, arg1_val) \ |
397 TRACE_EVENT_START2(category, name, id, arg1_name, arg1_val, NULL, 0) | 447 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 448 category, name, id, TRACE_EVENT_FLAG_HAS_ID, arg1_name, arg1_val) |
398 #define TRACE_EVENT_START2(category, name, id, arg1_name, arg1_val, \ | 449 #define TRACE_EVENT_START2(category, name, id, arg1_name, arg1_val, \ |
399 arg2_name, arg2_val) \ | 450 arg2_name, arg2_val) \ |
400 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ | 451 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
401 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, \ | 452 category, name, id, TRACE_EVENT_FLAG_HAS_ID, \ |
402 TRACE_EVENT_FLAG_HAS_ID) | 453 arg1_name, arg1_val, arg2_name, arg2_val) |
403 #define TRACE_EVENT_COPY_START0(category, name, id) \ | 454 #define TRACE_EVENT_COPY_START0(category, name, id) \ |
404 TRACE_EVENT_COPY_START1(category, name, id, NULL, 0) | 455 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 456 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) |
405 #define TRACE_EVENT_COPY_START1(category, name, id, arg1_name, arg1_val) \ | 457 #define TRACE_EVENT_COPY_START1(category, name, id, arg1_name, arg1_val) \ |
406 TRACE_EVENT_COPY_START2(category, name, id, arg1_name, arg1_val, NULL, 0) | 458 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 459 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
| 460 arg1_name, arg1_val) |
407 #define TRACE_EVENT_COPY_START2(category, name, id, arg1_name, arg1_val, \ | 461 #define TRACE_EVENT_COPY_START2(category, name, id, arg1_name, arg1_val, \ |
408 arg2_name, arg2_val) \ | 462 arg2_name, arg2_val) \ |
409 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ | 463 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
410 category, name, id, \ | 464 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
411 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 465 arg1_name, arg1_val, arg2_name, arg2_val) |
412 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
413 TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) | |
414 | 466 |
415 // Records a single FINISH event for "name" immediately. If the category | 467 // Records a single FINISH event for "name" immediately. If the category |
416 // is not enabled, then this does nothing. | 468 // is not enabled, then this does nothing. |
417 #define TRACE_EVENT_FINISH0(category, name, id) \ | 469 #define TRACE_EVENT_FINISH0(category, name, id) \ |
418 TRACE_EVENT_FINISH1(category, name, id, NULL, 0) | 470 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 471 category, name, id, TRACE_EVENT_FLAG_HAS_ID) |
419 #define TRACE_EVENT_FINISH1(category, name, id, arg1_name, arg1_val) \ | 472 #define TRACE_EVENT_FINISH1(category, name, id, arg1_name, arg1_val) \ |
420 TRACE_EVENT_FINISH2(category, name, id, arg1_name, arg1_val, NULL, 0) | 473 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 474 category, name, id, TRACE_EVENT_FLAG_HAS_ID, arg1_name, arg1_val) |
421 #define TRACE_EVENT_FINISH2(category, name, id, arg1_name, arg1_val, \ | 475 #define TRACE_EVENT_FINISH2(category, name, id, arg1_name, arg1_val, \ |
422 arg2_name, arg2_val) \ | 476 arg2_name, arg2_val) \ |
423 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 477 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
424 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, \ | 478 category, name, id, TRACE_EVENT_FLAG_HAS_ID, \ |
425 TRACE_EVENT_FLAG_HAS_ID) | 479 arg1_name, arg1_val, arg2_name, arg2_val) |
426 #define TRACE_EVENT_COPY_FINISH0(category, name, id) \ | 480 #define TRACE_EVENT_COPY_FINISH0(category, name, id) \ |
427 TRACE_EVENT_COPY_FINISH1(category, name, id, NULL, 0) | 481 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 482 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) |
428 #define TRACE_EVENT_COPY_FINISH1(category, name, id, arg1_name, arg1_val) \ | 483 #define TRACE_EVENT_COPY_FINISH1(category, name, id, arg1_name, arg1_val) \ |
429 TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, NULL, 0) | 484 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 485 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
| 486 arg1_name, arg1_val) |
430 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ | 487 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ |
431 arg2_name, arg2_val) \ | 488 arg2_name, arg2_val) \ |
432 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 489 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
433 category, name, id, \ | 490 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
434 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 491 arg1_name, arg1_val, arg2_name, arg2_val) |
435 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
436 TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) | |
437 | 492 |
438 | 493 |
439 // Implementation detail: trace event macros create temporary variables | 494 // Implementation detail: trace event macros create temporary variables |
440 // to keep instrumentation overhead low. These macros give each temporary | 495 // to keep instrumentation overhead low. These macros give each temporary |
441 // variable a unique name based on the line number to prevent name collissions. | 496 // variable a unique name based on the line number to prevent name collissions. |
442 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | 497 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ |
443 trace_event_unique_##a##b | 498 trace_event_unique_##a##b |
444 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | 499 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ |
445 INTERNAL_TRACE_EVENT_UID3(a,b) | 500 INTERNAL_TRACE_EVENT_UID3(a,b) |
446 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | 501 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ |
447 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | 502 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) |
448 | 503 |
449 // Implementation detail: internal macro to create static category. | 504 // Implementation detail: internal macro to create static category. |
450 // - ANNOTATE_BENIGN_RACE, see Thread Safety above. | 505 // - ANNOTATE_BENIGN_RACE, see Thread Safety above. |
451 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ | 506 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ |
452 static const base::debug::TraceCategory* \ | 507 static const volatile bool* \ |
453 INTERNAL_TRACE_EVENT_UID(catstatic) = NULL; \ | 508 INTERNAL_TRACE_EVENT_UID(catstatic) = NULL; \ |
454 ANNOTATE_BENIGN_RACE(&INTERNAL_TRACE_EVENT_UID(catstatic), \ | 509 ANNOTATE_BENIGN_RACE(&INTERNAL_TRACE_EVENT_UID(catstatic), \ |
455 "trace_event category"); \ | 510 "trace_event category"); \ |
456 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) \ | 511 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) \ |
457 INTERNAL_TRACE_EVENT_UID(catstatic) = \ | 512 INTERNAL_TRACE_EVENT_UID(catstatic) = \ |
458 base::debug::TraceLog::GetCategory(category); | 513 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); |
459 | 514 |
460 // Implementation detail: internal macro to create static category and add | 515 // Implementation detail: internal macro to create static category and add |
461 // event if the category is enabled. | 516 // event if the category is enabled. |
462 #define INTERNAL_TRACE_EVENT_ADD( \ | 517 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ |
463 phase, category, name, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | |
464 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 518 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
465 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 519 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
466 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 520 trace_event_internal::AddTraceEvent( \ |
467 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | 521 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \ |
468 name, 0, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, flags); \ | 522 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \ |
469 } | 523 } |
470 | 524 |
471 // Implementation detail: internal macro to create static category and | 525 // Implementation detail: internal macro to create static category and |
472 // add the counter event if it is enabled. | 526 // add the counter event if it is enabled. |
473 #define INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 527 #define INTERNAL_TRACE_EVENT_ADD_COUNTER( \ |
474 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | 528 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ |
475 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 529 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
476 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 530 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
477 base::debug::TraceLog::GetInstance()->AddCounterEvent( \ | 531 TRACE_EVENT_API_ADD_COUNTER_EVENT( \ |
478 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 532 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
479 name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags); \ | 533 name, trace_event_internal::TraceID(id).data(), \ |
| 534 arg1_name, arg1_val, arg2_name, arg2_val, flags); \ |
480 } | 535 } |
481 | 536 |
482 // Implementation detail: internal macro to create static category and add begin | 537 // Implementation detail: internal macro to create static category and add begin |
483 // event if the category is enabled. Also adds the end event when the scope | 538 // event if the category is enabled. Also adds the end event when the scope |
484 // ends. | 539 // ends. |
485 #define INTERNAL_TRACE_EVENT_ADD_SCOPED( \ | 540 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ |
486 category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
487 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 541 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
488 base::debug::internal::TraceEndOnScopeClose \ | 542 trace_event_internal::TraceEndOnScopeClose \ |
489 INTERNAL_TRACE_EVENT_UID(profileScope); \ | 543 INTERNAL_TRACE_EVENT_UID(profileScope); \ |
490 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 544 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
491 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 545 trace_event_internal::AddTraceEvent( \ |
492 TRACE_EVENT_PHASE_BEGIN, \ | 546 TRACE_EVENT_PHASE_BEGIN, \ |
493 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 547 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
494 name, 0, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, \ | 548 name, trace_event_internal::kNoEventId, \ |
495 TRACE_EVENT_FLAG_NONE); \ | 549 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ |
496 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | 550 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ |
497 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ | 551 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ |
498 } | 552 } |
499 | 553 |
500 // Implementation detail: internal macro to create static category and add begin | 554 // Implementation detail: internal macro to create static category and add begin |
501 // event if the category is enabled. Also adds the end event when the scope | 555 // event if the category is enabled. Also adds the end event when the scope |
502 // ends. If the elapsed time is < threshold time, the begin/end pair is erased. | 556 // ends. If the elapsed time is < threshold time, the begin/end pair is erased. |
503 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \ | 557 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \ |
504 category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 558 category, name, ...) \ |
505 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 559 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
506 base::debug::internal::TraceEndOnScopeCloseThreshold \ | 560 trace_event_internal::TraceEndOnScopeCloseThreshold \ |
507 INTERNAL_TRACE_EVENT_UID(profileScope); \ | 561 INTERNAL_TRACE_EVENT_UID(profileScope); \ |
508 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 562 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
509 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \ | 563 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \ |
510 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 564 trace_event_internal::AddTraceEvent( \ |
511 TRACE_EVENT_PHASE_BEGIN, \ | 565 TRACE_EVENT_PHASE_BEGIN, \ |
512 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 566 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
513 name, 0, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, \ | 567 name, trace_event_internal::kNoEventId, \ |
514 TRACE_EVENT_FLAG_NONE); \ | 568 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ |
515 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | 569 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ |
516 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | 570 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ |
517 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ | 571 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ |
518 } | 572 } |
519 | 573 |
520 // Implementation detail: internal macro to create static category and add | 574 // Implementation detail: internal macro to create static category and add |
521 // event if the category is enabled. | 575 // event if the category is enabled. |
522 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, \ | 576 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ |
523 arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | 577 ...) \ |
524 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 578 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
525 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 579 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
526 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 580 trace_event_internal::AddTraceEvent( \ |
527 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | 581 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ |
528 name, id, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, flags); \ | 582 name, trace_event_internal::TraceID(id).data(), flags, \ |
| 583 ##__VA_ARGS__); \ |
529 } | 584 } |
530 | 585 |
531 template <typename Type> | 586 template <typename Type> |
532 struct StaticMemorySingletonTraits; | 587 struct StaticMemorySingletonTraits; |
533 | 588 |
534 namespace base { | 589 namespace base { |
535 | 590 |
536 class RefCountedString; | 591 class RefCountedString; |
537 | 592 |
538 namespace debug { | 593 namespace debug { |
539 | 594 |
540 // Categories allow enabling/disabling of streams of trace events | 595 const int kTraceMaxNumArgs = 2; |
541 struct TraceCategory { | |
542 const char* name; | |
543 volatile bool enabled; | |
544 }; | |
545 | 596 |
546 const size_t kTraceMaxNumArgs = 2; | 597 // Notes regarding the following definitions: |
| 598 // New values can be added and propagated to third party libraries, but existing |
| 599 // definitions must never be changed, because third party libraries may use old |
| 600 // definitions. |
547 | 601 |
548 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | 602 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. |
549 typedef char TraceEventPhase; | 603 #define TRACE_EVENT_PHASE_BEGIN ('B') |
550 #define TRACE_EVENT_PHASE_BEGIN (base::debug::TraceEventPhase('B')) | 604 #define TRACE_EVENT_PHASE_END ('E') |
551 #define TRACE_EVENT_PHASE_END (base::debug::TraceEventPhase('E')) | 605 #define TRACE_EVENT_PHASE_INSTANT ('I') |
552 #define TRACE_EVENT_PHASE_INSTANT (base::debug::TraceEventPhase('I')) | 606 #define TRACE_EVENT_PHASE_START ('S') |
553 #define TRACE_EVENT_PHASE_START (base::debug::TraceEventPhase('S')) | 607 #define TRACE_EVENT_PHASE_FINISH ('F') |
554 #define TRACE_EVENT_PHASE_FINISH (base::debug::TraceEventPhase('F')) | 608 #define TRACE_EVENT_PHASE_METADATA ('M') |
555 #define TRACE_EVENT_PHASE_METADATA (base::debug::TraceEventPhase('M')) | 609 #define TRACE_EVENT_PHASE_COUNTER ('C') |
556 #define TRACE_EVENT_PHASE_COUNTER (base::debug::TraceEventPhase('C')) | |
557 | 610 |
558 typedef uint8 TraceEventFlags; | 611 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. |
559 #define TRACE_EVENT_FLAG_NONE (base::debug::TraceEventFlags(0)) | 612 #define TRACE_EVENT_FLAG_NONE (uint8(0)) |
560 #define TRACE_EVENT_FLAG_COPY (base::debug::TraceEventFlags(1<<0)) | 613 #define TRACE_EVENT_FLAG_COPY (uint8(1<<0)) |
561 #define TRACE_EVENT_FLAG_HAS_ID (base::debug::TraceEventFlags(1<<1)) | 614 #define TRACE_EVENT_FLAG_HAS_ID (uint8(1<<1)) |
562 | 615 |
563 // Simple union of values. This is much lighter weight than base::Value, which | 616 // Type values for identifying types in the TraceValue union. |
564 // requires dynamic allocation and a vtable. To keep the trace runtime overhead | 617 #define TRACE_VALUE_TYPE_BOOL (uint8(1)) |
565 // low, we want constant size storage here. | 618 #define TRACE_VALUE_TYPE_UINT (uint8(2)) |
566 class BASE_EXPORT TraceValue { | 619 #define TRACE_VALUE_TYPE_INT (uint8(3)) |
| 620 #define TRACE_VALUE_TYPE_DOUBLE (uint8(4)) |
| 621 #define TRACE_VALUE_TYPE_POINTER (uint8(5)) |
| 622 #define TRACE_VALUE_TYPE_STRING (uint8(6)) |
| 623 #define TRACE_VALUE_TYPE_COPY_STRING (uint8(7)) |
| 624 |
| 625 // Output records are "Events" and can be obtained via the |
| 626 // OutputCallback whenever the tracing system decides to flush. This |
| 627 // can happen at any time, on any thread, or you can programatically |
| 628 // force it to happen. |
| 629 class BASE_EXPORT TraceEvent { |
567 public: | 630 public: |
568 enum Type { | 631 union TraceValue { |
569 TRACE_TYPE_UNDEFINED, | |
570 TRACE_TYPE_BOOL, | |
571 TRACE_TYPE_UINT, | |
572 TRACE_TYPE_INT, | |
573 TRACE_TYPE_DOUBLE, | |
574 TRACE_TYPE_POINTER, | |
575 TRACE_TYPE_STRING, | |
576 TRACE_TYPE_STATIC_STRING | |
577 }; | |
578 | |
579 TraceValue() : type_(TRACE_TYPE_UNDEFINED) { | |
580 value_.as_uint = 0ull; | |
581 } | |
582 TraceValue(bool rhs) : type_(TRACE_TYPE_BOOL) { | |
583 value_.as_bool = rhs; | |
584 } | |
585 TraceValue(uint64 rhs) : type_(TRACE_TYPE_UINT) { | |
586 value_.as_uint = rhs; | |
587 } | |
588 TraceValue(uint32 rhs) : type_(TRACE_TYPE_UINT) { | |
589 value_.as_uint = rhs; | |
590 } | |
591 TraceValue(uint16 rhs) : type_(TRACE_TYPE_UINT) { | |
592 value_.as_uint = rhs; | |
593 } | |
594 TraceValue(uint8 rhs) : type_(TRACE_TYPE_UINT) { | |
595 value_.as_uint = rhs; | |
596 } | |
597 TraceValue(int64 rhs) : type_(TRACE_TYPE_INT) { | |
598 value_.as_int = rhs; | |
599 } | |
600 TraceValue(int32 rhs) : type_(TRACE_TYPE_INT) { | |
601 value_.as_int = rhs; | |
602 } | |
603 TraceValue(int16 rhs) : type_(TRACE_TYPE_INT) { | |
604 value_.as_int = rhs; | |
605 } | |
606 TraceValue(int8 rhs) : type_(TRACE_TYPE_INT) { | |
607 value_.as_int = rhs; | |
608 } | |
609 TraceValue(double rhs) : type_(TRACE_TYPE_DOUBLE) { | |
610 value_.as_double = rhs; | |
611 } | |
612 TraceValue(const void* rhs) : type_(TRACE_TYPE_POINTER) { | |
613 value_.as_pointer = rhs; | |
614 } | |
615 TraceValue(const std::string& rhs) : type_(TRACE_TYPE_STRING) { | |
616 value_.as_string = rhs.c_str(); | |
617 } | |
618 TraceValue(const char* rhs) : type_(TRACE_TYPE_STATIC_STRING) { | |
619 value_.as_string = rhs; | |
620 } | |
621 | |
622 static TraceValue StringWithCopy(const char* rhs) { | |
623 TraceValue value(rhs); | |
624 if (rhs) | |
625 value.type_ = TRACE_TYPE_STRING; | |
626 return value; | |
627 } | |
628 | |
629 static TraceValue ForceCopy(const TraceValue& rhs) { | |
630 TraceValue value(rhs); | |
631 if (value.type_ == TRACE_TYPE_STATIC_STRING && value.as_string()) | |
632 value.type_ = TRACE_TYPE_STRING; | |
633 return value; | |
634 } | |
635 | |
636 bool is_string() const { | |
637 return type_ == TRACE_TYPE_STRING || type_ == TRACE_TYPE_STATIC_STRING; | |
638 } | |
639 | |
640 void AppendAsJSON(std::string* out) const; | |
641 | |
642 Type type() const { | |
643 return type_; | |
644 } | |
645 uint64 as_uint() const { | |
646 DCHECK_EQ(TRACE_TYPE_UINT, type_); | |
647 return value_.as_uint; | |
648 } | |
649 bool as_bool() const { | |
650 DCHECK_EQ(TRACE_TYPE_BOOL, type_); | |
651 return value_.as_bool; | |
652 } | |
653 int64 as_int() const { | |
654 DCHECK_EQ(TRACE_TYPE_INT, type_); | |
655 return value_.as_int; | |
656 } | |
657 double as_double() const { | |
658 DCHECK_EQ(TRACE_TYPE_DOUBLE, type_); | |
659 return value_.as_double; | |
660 } | |
661 const void* as_pointer() const { | |
662 DCHECK_EQ(TRACE_TYPE_POINTER, type_); | |
663 return value_.as_pointer; | |
664 } | |
665 const char* as_string() const { | |
666 DCHECK(is_string()); | |
667 return value_.as_string; | |
668 } | |
669 const char** as_assignable_string() { | |
670 DCHECK_EQ(TRACE_TYPE_STRING, type_); | |
671 return &value_.as_string; | |
672 } | |
673 | |
674 private: | |
675 union Value { | |
676 bool as_bool; | 632 bool as_bool; |
677 uint64 as_uint; | 633 uint64 as_uint; |
678 int64 as_int; | 634 int64 as_int; |
679 double as_double; | 635 double as_double; |
680 const void* as_pointer; | 636 const void* as_pointer; |
681 const char* as_string; | 637 const char* as_string; |
682 }; | 638 }; |
683 | 639 |
684 Type type_; | |
685 Value value_; | |
686 }; | |
687 | |
688 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | |
689 // are mangled with the Process ID so that they are unlikely to collide when the | |
690 // same pointer is used on different processes. | |
691 class BASE_EXPORT TraceID { | |
692 public: | |
693 TraceID() : data_(0u) {} | |
694 TraceID(void* rhs); | |
695 TraceID(unsigned long long rhs) : data_(static_cast<uint64>(rhs)) {} | |
696 TraceID(unsigned long rhs) : data_(static_cast<uint64>(rhs)) {} | |
697 TraceID(unsigned int rhs) : data_(static_cast<uint64>(rhs)) {} | |
698 TraceID(long long rhs) : data_(static_cast<uint64>(rhs)) {} | |
699 TraceID(long rhs) : data_(static_cast<uint64>(rhs)) {} | |
700 TraceID(int rhs) : data_(static_cast<uint64>(rhs)) {} | |
701 | |
702 uint64 data() const { return data_; } | |
703 | |
704 private: | |
705 uint64 data_; | |
706 }; | |
707 | |
708 // Output records are "Events" and can be obtained via the | |
709 // OutputCallback whenever the tracing system decides to flush. This | |
710 // can happen at any time, on any thread, or you can programatically | |
711 // force it to happen. | |
712 class BASE_EXPORT TraceEvent { | |
713 public: | |
714 TraceEvent(); | 640 TraceEvent(); |
715 TraceEvent(int thread_id, | 641 TraceEvent(int thread_id, |
716 TimeTicks timestamp, | 642 TimeTicks timestamp, |
717 TraceEventPhase phase, | 643 char phase, |
718 const TraceCategory* category, | 644 const volatile bool* category_enabled, |
719 const char* name, | 645 const char* name, |
720 TraceID id, | 646 uint64 id, |
721 const char* arg1_name, const TraceValue& arg1_val, | 647 int num_args, |
722 const char* arg2_name, const TraceValue& arg2_val, | 648 const char** arg_names, |
723 TraceEventFlags flags); | 649 const uint8* arg_types, |
| 650 const uint64* arg_values, |
| 651 uint8 flags); |
724 ~TraceEvent(); | 652 ~TraceEvent(); |
725 | 653 |
726 static char GetPhaseChar(TraceEventPhase phase) { | |
727 return static_cast<char>(phase); | |
728 } | |
729 | |
730 static TraceEventPhase GetPhase(const char* phase) { | |
731 return static_cast<TraceEventPhase>(*phase); | |
732 } | |
733 | |
734 // Serialize event data to JSON | 654 // Serialize event data to JSON |
735 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 655 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
736 size_t start, | 656 size_t start, |
737 size_t count, | 657 size_t count, |
738 std::string* out); | 658 std::string* out); |
739 void AppendAsJSON(std::string* out) const; | 659 void AppendAsJSON(std::string* out) const; |
740 | 660 |
741 TimeTicks timestamp() const { return timestamp_; } | 661 TimeTicks timestamp() const { return timestamp_; } |
742 | 662 |
743 // Exposed for unittesting: | 663 // Exposed for unittesting: |
744 | 664 |
745 const base::RefCountedString* parameter_copy_storage() const { | 665 const base::RefCountedString* parameter_copy_storage() const { |
746 return parameter_copy_storage_.get(); | 666 return parameter_copy_storage_.get(); |
747 } | 667 } |
748 | 668 |
749 const char* name() const { return name_; } | 669 const char* name() const { return name_; } |
750 | 670 |
751 private: | 671 private: |
752 // Note: these are ordered by size (largest first) for optimal packing. | 672 // Note: these are ordered by size (largest first) for optimal packing. |
753 TimeTicks timestamp_; | 673 TimeTicks timestamp_; |
754 // id_ can be used to store phase-specific data. | 674 // id_ can be used to store phase-specific data. |
755 TraceID id_; | 675 uint64 id_; |
756 TraceValue arg_values_[kTraceMaxNumArgs]; | 676 TraceValue arg_values_[kTraceMaxNumArgs]; |
757 const char* arg_names_[kTraceMaxNumArgs]; | 677 const char* arg_names_[kTraceMaxNumArgs]; |
758 const TraceCategory* category_; | 678 const volatile bool* category_enabled_; |
759 const char* name_; | 679 const char* name_; |
760 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 680 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
761 int thread_id_; | 681 int thread_id_; |
762 TraceEventPhase phase_; | 682 char phase_; |
763 TraceEventFlags flags_; | 683 uint8 flags_; |
| 684 uint8 arg_types_[kTraceMaxNumArgs]; |
764 }; | 685 }; |
765 | 686 |
766 | 687 |
767 // TraceResultBuffer collects and converts trace fragments returned by TraceLog | 688 // TraceResultBuffer collects and converts trace fragments returned by TraceLog |
768 // to JSON output. | 689 // to JSON output. |
769 class BASE_EXPORT TraceResultBuffer { | 690 class BASE_EXPORT TraceResultBuffer { |
770 public: | 691 public: |
771 typedef base::Callback<void(const std::string&)> OutputCallback; | 692 typedef base::Callback<void(const std::string&)> OutputCallback; |
772 | 693 |
773 // If you don't need to stream JSON chunks out efficiently, and just want to | 694 // If you don't need to stream JSON chunks out efficiently, and just want to |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 // The trace buffer does not flush dynamically, so when it fills up, | 784 // The trace buffer does not flush dynamically, so when it fills up, |
864 // subsequent trace events will be dropped. This callback is generated when | 785 // subsequent trace events will be dropped. This callback is generated when |
865 // the trace buffer is full. The callback must be thread safe. | 786 // the trace buffer is full. The callback must be thread safe. |
866 typedef base::Callback<void(void)> BufferFullCallback; | 787 typedef base::Callback<void(void)> BufferFullCallback; |
867 void SetBufferFullCallback(const BufferFullCallback& cb); | 788 void SetBufferFullCallback(const BufferFullCallback& cb); |
868 | 789 |
869 // Flushes all logged data to the callback. | 790 // Flushes all logged data to the callback. |
870 void Flush(); | 791 void Flush(); |
871 | 792 |
872 // Called by TRACE_EVENT* macros, don't call this directly. | 793 // Called by TRACE_EVENT* macros, don't call this directly. |
873 static const TraceCategory* GetCategory(const char* name); | 794 static const volatile bool* GetCategoryEnabled(const char* name); |
| 795 static const char* GetCategoryName(const volatile bool* category_enabled); |
874 | 796 |
875 // Called by TRACE_EVENT* macros, don't call this directly. | 797 // Called by TRACE_EVENT* macros, don't call this directly. |
876 // Returns the index in the internal vector of the event if it was added, or | 798 // Returns the index in the internal vector of the event if it was added, or |
877 // -1 if the event was not added. | 799 // -1 if the event was not added. |
878 // On end events, the return value of the begin event can be specified along | 800 // On end events, the return value of the begin event can be specified along |
879 // with a threshold in microseconds. If the elapsed time between begin and end | 801 // with a threshold in microseconds. If the elapsed time between begin and end |
880 // is less than the threshold, the begin/end event pair is dropped. | 802 // is less than the threshold, the begin/end event pair is dropped. |
881 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied | 803 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied |
882 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. | 804 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. |
883 int AddTraceEvent(TraceEventPhase phase, | 805 int AddTraceEvent(char phase, |
884 const TraceCategory* category, | 806 const volatile bool* category_enabled, |
885 const char* name, | 807 const char* name, |
886 TraceID id, | 808 uint64 id, |
887 const char* arg1_name, TraceValue arg1_val, | 809 int num_args, |
888 const char* arg2_name, TraceValue arg2_val, | 810 const char** arg_names, |
| 811 const uint8* arg_types, |
| 812 const uint64* arg_values, |
889 int threshold_begin_id, | 813 int threshold_begin_id, |
890 int64 threshold, | 814 int64 threshold, |
891 TraceEventFlags flags); | 815 uint8 flags); |
892 static void AddTraceEventEtw(TraceEventPhase phase, | 816 static void AddTraceEventEtw(char phase, |
893 const char* name, | 817 const char* name, |
894 const void* id, | 818 const void* id, |
895 const char* extra); | 819 const char* extra); |
896 static void AddTraceEventEtw(TraceEventPhase phase, | 820 static void AddTraceEventEtw(char phase, |
897 const char* name, | 821 const char* name, |
898 const void* id, | 822 const void* id, |
899 const std::string& extra); | 823 const std::string& extra); |
900 | 824 |
901 // A wrapper around AddTraceEvent used by TRACE_COUNTERx macros | 825 // A wrapper around AddTraceEvent used by TRACE_COUNTERx macros |
902 // that allows only integer values for the counters. | 826 // that allows only integer values for the counters. |
903 int AddCounterEvent(const TraceCategory* category, | 827 void AddCounterEvent(const volatile bool* category_enabled, |
904 const char* name, | 828 const char* name, |
905 TraceID id, | 829 uint64 id, |
906 const char* arg1_name, int32 arg1_val, | 830 const char* arg1_name, int32 arg1_val, |
907 const char* arg2_name, int32 arg2_val, | 831 const char* arg2_name, int32 arg2_val, |
908 TraceEventFlags flags); | 832 uint8 flags); |
909 | 833 |
910 // Mangle |id| with a hash based on the process ID so that if |id| occurs on | 834 // Mangle |ptr| with a hash based on the process ID so that if |ptr| occurs on |
911 // more than one process, it will not collide. | 835 // more than one process, it will not collide. |
912 uint64 GetIntraProcessID(uint64 id) const { return id ^ process_id_hash_; } | 836 uint64 GetInterProcessID(void* ptr) const { |
| 837 return static_cast<uint64>(reinterpret_cast<uintptr_t>(ptr)) ^ |
| 838 process_id_hash_; |
| 839 } |
913 | 840 |
914 int process_id() const { return process_id_; } | 841 int process_id() const { return process_id_; } |
915 | 842 |
916 // Exposed for unittesting: | 843 // Exposed for unittesting: |
917 | 844 |
918 // Allows deleting our singleton instance. | 845 // Allows deleting our singleton instance. |
919 static void DeleteForTesting(); | 846 static void DeleteForTesting(); |
920 | 847 |
921 // Allows resurrecting our singleton instance post-AtExit processing. | 848 // Allows resurrecting our singleton instance post-AtExit processing. |
922 static void Resurrect(); | 849 static void Resurrect(); |
923 | 850 |
924 // Allow tests to inspect TraceEvents. | 851 // Allow tests to inspect TraceEvents. |
925 size_t GetEventsSize() const { return logged_events_.size(); } | 852 size_t GetEventsSize() const { return logged_events_.size(); } |
926 const TraceEvent& GetEventAt(size_t index) const { | 853 const TraceEvent& GetEventAt(size_t index) const { |
927 DCHECK(index < logged_events_.size()); | 854 DCHECK(index < logged_events_.size()); |
928 return logged_events_[index]; | 855 return logged_events_[index]; |
929 } | 856 } |
930 | 857 |
931 void SetProcessID(int process_id); | 858 void SetProcessID(int process_id); |
932 | 859 |
933 private: | 860 private: |
934 // This allows constructor and destructor to be private and usable only | 861 // This allows constructor and destructor to be private and usable only |
935 // by the Singleton class. | 862 // by the Singleton class. |
936 friend struct StaticMemorySingletonTraits<TraceLog>; | 863 friend struct StaticMemorySingletonTraits<TraceLog>; |
937 | 864 |
938 TraceLog(); | 865 TraceLog(); |
939 ~TraceLog(); | 866 ~TraceLog(); |
940 const TraceCategory* GetCategoryInternal(const char* name); | 867 const volatile bool* GetCategoryEnabledInternal(const char* name); |
941 void AddThreadNameMetadataEvents(); | 868 void AddThreadNameMetadataEvents(); |
942 void AddClockSyncMetadataEvents(); | 869 void AddClockSyncMetadataEvents(); |
943 | 870 |
944 // TODO(nduca): switch to per-thread trace buffers to reduce thread | 871 // TODO(nduca): switch to per-thread trace buffers to reduce thread |
945 // synchronization. | 872 // synchronization. |
946 Lock lock_; | 873 Lock lock_; |
947 bool enabled_; | 874 bool enabled_; |
948 OutputCallback output_callback_; | 875 OutputCallback output_callback_; |
949 BufferFullCallback buffer_full_callback_; | 876 BufferFullCallback buffer_full_callback_; |
950 std::vector<TraceEvent> logged_events_; | 877 std::vector<TraceEvent> logged_events_; |
951 std::vector<std::string> included_categories_; | 878 std::vector<std::string> included_categories_; |
952 std::vector<std::string> excluded_categories_; | 879 std::vector<std::string> excluded_categories_; |
953 | 880 |
954 base::hash_map<int, std::string> thread_names_; | 881 base::hash_map<int, std::string> thread_names_; |
955 | 882 |
956 // XORed with TraceID to make it unlikely to collide with other processes. | 883 // XORed with TraceID to make it unlikely to collide with other processes. |
957 uint64 process_id_hash_; | 884 uint64 process_id_hash_; |
958 | 885 |
959 int process_id_; | 886 int process_id_; |
960 | 887 |
961 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 888 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
962 }; | 889 }; |
963 | 890 |
964 namespace internal { | 891 } // namespace debug |
| 892 } // namespace base |
| 893 |
| 894 namespace trace_event_internal { |
| 895 |
| 896 // Specify these values when the corresponding argument of AddTraceEvent is not |
| 897 // used. |
| 898 const int kZeroNumArgs = 0; |
| 899 const int kNoThreshholdBeginId = -1; |
| 900 const int64 kNoThresholdValue = 0; |
| 901 const uint64 kNoEventId = 0; |
| 902 |
| 903 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers |
| 904 // are mangled with the Process ID so that they are unlikely to collide when the |
| 905 // same pointer is used on different processes. |
| 906 class BASE_EXPORT TraceID { |
| 907 public: |
| 908 explicit TraceID(void* rhs) : |
| 909 data_(TRACE_EVENT_API_GET_ID_FROM_POINTER(rhs)) {} |
| 910 explicit TraceID(unsigned long long rhs) : data_(static_cast<uint64>(rhs)) {} |
| 911 explicit TraceID(unsigned long rhs) : data_(static_cast<uint64>(rhs)) {} |
| 912 explicit TraceID(unsigned int rhs) : data_(static_cast<uint64>(rhs)) {} |
| 913 explicit TraceID(long long rhs) : data_(static_cast<uint64>(rhs)) {} |
| 914 explicit TraceID(long rhs) : data_(static_cast<uint64>(rhs)) {} |
| 915 explicit TraceID(int rhs) : data_(static_cast<uint64>(rhs)) {} |
| 916 |
| 917 uint64 data() const { return data_; } |
| 918 |
| 919 private: |
| 920 uint64 data_; |
| 921 }; |
| 922 |
| 923 // Simple union to store various types as uint64. |
| 924 union TraceValueUnion { |
| 925 bool as_bool; |
| 926 uint64 as_uint; |
| 927 int64 as_int; |
| 928 double as_double; |
| 929 const void* as_pointer; |
| 930 const char* as_string; |
| 931 }; |
| 932 |
| 933 // Simple container for const char* that should be copied instead of retained. |
| 934 class TraceStringWithCopy { |
| 935 public: |
| 936 explicit TraceStringWithCopy(const char* str) : str_(str) {} |
| 937 operator const char* () const { return str_; } |
| 938 private: |
| 939 const char* str_; |
| 940 }; |
| 941 |
| 942 // Define SetTraceValue for each allowed type. It stores the type and |
| 943 // value in the return arguments. This allows this API to avoid declaring any |
| 944 // structures so that it is portable to third_party libraries. |
| 945 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \ |
| 946 union_member, \ |
| 947 value_type_id) \ |
| 948 static inline void SetTraceValue(actual_type arg, \ |
| 949 uint8* type, \ |
| 950 uint64* value) { \ |
| 951 TraceValueUnion type_value; \ |
| 952 type_value.union_member = arg; \ |
| 953 *type = value_type_id; \ |
| 954 *value = type_value.as_uint; \ |
| 955 } |
| 956 |
| 957 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL) |
| 958 INTERNAL_DECLARE_SET_TRACE_VALUE(uint64, as_uint, TRACE_VALUE_TYPE_UINT) |
| 959 INTERNAL_DECLARE_SET_TRACE_VALUE(uint32, as_uint, TRACE_VALUE_TYPE_UINT) |
| 960 INTERNAL_DECLARE_SET_TRACE_VALUE(uint16, as_uint, TRACE_VALUE_TYPE_UINT) |
| 961 INTERNAL_DECLARE_SET_TRACE_VALUE(uint8, as_uint, TRACE_VALUE_TYPE_UINT) |
| 962 INTERNAL_DECLARE_SET_TRACE_VALUE(int64, as_int, TRACE_VALUE_TYPE_INT) |
| 963 INTERNAL_DECLARE_SET_TRACE_VALUE(int32, as_int, TRACE_VALUE_TYPE_INT) |
| 964 INTERNAL_DECLARE_SET_TRACE_VALUE(int16, as_int, TRACE_VALUE_TYPE_INT) |
| 965 INTERNAL_DECLARE_SET_TRACE_VALUE(int8, as_int, TRACE_VALUE_TYPE_INT) |
| 966 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE) |
| 967 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer, |
| 968 TRACE_VALUE_TYPE_POINTER) |
| 969 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string, |
| 970 TRACE_VALUE_TYPE_STRING) |
| 971 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string, |
| 972 TRACE_VALUE_TYPE_COPY_STRING) |
| 973 |
| 974 // std::string version of SetTraceValue so that trace arguments can be strings. |
| 975 static inline void SetTraceValue(const std::string& arg, |
| 976 uint8* type, |
| 977 uint64* value) { |
| 978 TraceValueUnion type_value; |
| 979 type_value.as_string = arg.c_str(); |
| 980 *type = TRACE_VALUE_TYPE_COPY_STRING; |
| 981 *value = type_value.as_uint; |
| 982 } |
| 983 |
| 984 // These AddTraceEvent template functions are defined here instead of in the |
| 985 // macro, because the arg_values could be temporary objects, such as |
| 986 // std::string. In order to store pointers to the internal c_str and pass |
| 987 // through to the tracing API, the arg_values must live throughout |
| 988 // these procedures. |
| 989 |
| 990 static inline int AddTraceEvent(char phase, |
| 991 const volatile bool* category_enabled, |
| 992 const char* name, |
| 993 uint64 id, |
| 994 uint8 flags) { |
| 995 return TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 996 phase, category_enabled, name, id, |
| 997 kZeroNumArgs, NULL, NULL, NULL, |
| 998 kNoThreshholdBeginId, kNoThresholdValue, flags); |
| 999 } |
| 1000 |
| 1001 template<class ARG1_TYPE> |
| 1002 static inline int AddTraceEvent(char phase, |
| 1003 const volatile bool* category_enabled, |
| 1004 const char* name, |
| 1005 uint64 id, |
| 1006 uint8 flags, |
| 1007 const char* arg1_name, |
| 1008 const ARG1_TYPE& arg1_val) { |
| 1009 const int num_args = 1; |
| 1010 uint8 arg_types[1]; |
| 1011 uint64 arg_values[1]; |
| 1012 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); |
| 1013 return TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 1014 phase, category_enabled, name, id, |
| 1015 num_args, &arg1_name, arg_types, arg_values, |
| 1016 kNoThreshholdBeginId, kNoThresholdValue, flags); |
| 1017 } |
| 1018 |
| 1019 template<class ARG1_TYPE, class ARG2_TYPE> |
| 1020 static inline int AddTraceEvent(char phase, |
| 1021 const volatile bool* category_enabled, |
| 1022 const char* name, |
| 1023 uint64 id, |
| 1024 uint8 flags, |
| 1025 const char* arg1_name, |
| 1026 const ARG1_TYPE& arg1_val, |
| 1027 const char* arg2_name, |
| 1028 const ARG2_TYPE& arg2_val) { |
| 1029 const int num_args = 2; |
| 1030 const char* arg_names[2] = { arg1_name, arg2_name }; |
| 1031 uint8 arg_types[2]; |
| 1032 uint64 arg_values[2]; |
| 1033 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); |
| 1034 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); |
| 1035 return TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 1036 phase, category_enabled, name, id, |
| 1037 num_args, arg_names, arg_types, arg_values, |
| 1038 kNoThreshholdBeginId, kNoThresholdValue, flags); |
| 1039 } |
965 | 1040 |
966 // Used by TRACE_EVENTx macro. Do not use directly. | 1041 // Used by TRACE_EVENTx macro. Do not use directly. |
967 class BASE_EXPORT TraceEndOnScopeClose { | 1042 class BASE_EXPORT TraceEndOnScopeClose { |
968 public: | 1043 public: |
969 // Note: members of data_ intentionally left uninitialized. See Initialize. | 1044 // Note: members of data_ intentionally left uninitialized. See Initialize. |
970 TraceEndOnScopeClose() : p_data_(NULL) {} | 1045 TraceEndOnScopeClose() : p_data_(NULL) {} |
971 ~TraceEndOnScopeClose() { | 1046 ~TraceEndOnScopeClose() { |
972 if (p_data_) | 1047 if (p_data_) |
973 AddEventIfEnabled(); | 1048 AddEventIfEnabled(); |
974 } | 1049 } |
975 | 1050 |
976 void Initialize(const TraceCategory* category, | 1051 void Initialize(const volatile bool* category_enabled, |
977 const char* name); | 1052 const char* name); |
978 | 1053 |
979 private: | 1054 private: |
980 // Add the end event if the category is still enabled. | 1055 // Add the end event if the category is still enabled. |
981 void AddEventIfEnabled(); | 1056 void AddEventIfEnabled(); |
982 | 1057 |
983 // This Data struct workaround is to avoid initializing all the members | 1058 // This Data struct workaround is to avoid initializing all the members |
984 // in Data during construction of this object, since this object is always | 1059 // in Data during construction of this object, since this object is always |
985 // constructed, even when tracing is disabled. If the members of Data were | 1060 // constructed, even when tracing is disabled. If the members of Data were |
986 // members of this class instead, compiler warnings occur about potential | 1061 // members of this class instead, compiler warnings occur about potential |
987 // uninitialized accesses. | 1062 // uninitialized accesses. |
988 struct Data { | 1063 struct Data { |
989 const TraceCategory* category; | 1064 const volatile bool* category_enabled; |
990 const char* name; | 1065 const char* name; |
991 }; | 1066 }; |
992 Data* p_data_; | 1067 Data* p_data_; |
993 Data data_; | 1068 Data data_; |
994 }; | 1069 }; |
995 | 1070 |
996 // Used by TRACE_EVENTx macro. Do not use directly. | 1071 // Used by TRACE_EVENTx macro. Do not use directly. |
997 class BASE_EXPORT TraceEndOnScopeCloseThreshold { | 1072 class BASE_EXPORT TraceEndOnScopeCloseThreshold { |
998 public: | 1073 public: |
999 // Note: members of data_ intentionally left uninitialized. See Initialize. | 1074 // Note: members of data_ intentionally left uninitialized. See Initialize. |
1000 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} | 1075 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} |
1001 ~TraceEndOnScopeCloseThreshold() { | 1076 ~TraceEndOnScopeCloseThreshold() { |
1002 if (p_data_) | 1077 if (p_data_) |
1003 AddEventIfEnabled(); | 1078 AddEventIfEnabled(); |
1004 } | 1079 } |
1005 | 1080 |
1006 // Called by macros only when tracing is enabled at the point when the begin | 1081 // Called by macros only when tracing is enabled at the point when the begin |
1007 // event is added. | 1082 // event is added. |
1008 void Initialize(const TraceCategory* category, | 1083 void Initialize(const volatile bool* category_enabled, |
1009 const char* name, | 1084 const char* name, |
1010 int threshold_begin_id, | 1085 int threshold_begin_id, |
1011 int64 threshold); | 1086 int64 threshold); |
1012 | 1087 |
1013 private: | 1088 private: |
1014 // Add the end event if the category is still enabled. | 1089 // Add the end event if the category is still enabled. |
1015 void AddEventIfEnabled(); | 1090 void AddEventIfEnabled(); |
1016 | 1091 |
1017 // This Data struct workaround is to avoid initializing all the members | 1092 // This Data struct workaround is to avoid initializing all the members |
1018 // in Data during construction of this object, since this object is always | 1093 // in Data during construction of this object, since this object is always |
1019 // constructed, even when tracing is disabled. If the members of Data were | 1094 // constructed, even when tracing is disabled. If the members of Data were |
1020 // members of this class instead, compiler warnings occur about potential | 1095 // members of this class instead, compiler warnings occur about potential |
1021 // uninitialized accesses. | 1096 // uninitialized accesses. |
1022 struct Data { | 1097 struct Data { |
1023 int64 threshold; | 1098 int64 threshold; |
1024 const TraceCategory* category; | 1099 const volatile bool* category_enabled; |
1025 const char* name; | 1100 const char* name; |
1026 int threshold_begin_id; | 1101 int threshold_begin_id; |
1027 }; | 1102 }; |
1028 Data* p_data_; | 1103 Data* p_data_; |
1029 Data data_; | 1104 Data data_; |
1030 }; | 1105 }; |
1031 | 1106 |
1032 } // namespace internal | 1107 } // namespace trace_event_internal |
1033 | |
1034 } // namespace debug | |
1035 } // namespace base | |
1036 | 1108 |
1037 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 1109 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
OLD | NEW |