Phosphor
phosphor.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2016 Couchbase, Inc
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "phosphor-internal.h"
21 #include "trace_log.h"
22 
73 #define PHOSPHOR_INSTANCE phosphor::TraceLog::getInstance()
74 
79 #define PHOSPHOR_PTR(arg) arg
80 
86 #define PHOSPHOR_INLINE_STR(arg) phosphor::inline_zstring<8>(arg)
87 #define PHOSPHOR_INLINE_STR_N(arg, len) phosphor::inline_zstring<8>(arg, len)
88 
89 #if !defined(PHOSPHOR_DISABLED)
90 #define PHOSPHOR_DISABLED 0
91 #endif
92 
93 #define PHOSPHOR_ENABLED !PHOSPHOR_DISABLED
94 
95 #if !PHOSPHOR_DISABLED
96 
112 #define TRACE_EVENT_START(category, name, ...) \
113  PHOSPHOR_INTERNAL_TRACE_EVENT( \
114  category, \
115  name, \
116  "arg1", \
117  "arg2", \
118  phosphor::TraceEvent::Type::SyncStart, \
119  __VA_ARGS__)
120 
121 #define TRACE_EVENT_START0(category, name) \
122  PHOSPHOR_INTERNAL_TRACE_EVENT0( \
123  category, name, phosphor::TraceEvent::Type::SyncStart)
124 
125 #define TRACE_EVENT_END(category, name, ...) \
126  PHOSPHOR_INTERNAL_TRACE_EVENT( \
127  category, \
128  name, \
129  "arg1_end", \
130  "arg2_end", \
131  phosphor::TraceEvent::Type::SyncEnd, \
132  __VA_ARGS__)
133 
134 #define TRACE_EVENT_END0(category, name) \
135  PHOSPHOR_INTERNAL_TRACE_EVENT0( \
136  category, name, phosphor::TraceEvent::Type::SyncEnd)
137 
138 #define TRACE_EVENT_START1(category, name, arg1_name, arg1) \
139  PHOSPHOR_INTERNAL_TRACE_EVENT( \
140  category, \
141  name, \
142  arg1_name, \
143  "", \
144  phosphor::TraceEvent::Type::SyncStart, \
145  arg1)
146 
147 #define TRACE_EVENT_END1(category, name, arg1_name, arg1) \
148  PHOSPHOR_INTERNAL_TRACE_EVENT( \
149  category, \
150  name, \
151  arg1_name, \
152  "", \
153  phosphor::TraceEvent::Type::SyncEnd, \
154  arg1)
155 
156 #define TRACE_EVENT_START2(category, name, arg1_name, arg1, arg2_name, arg2) \
157  PHOSPHOR_INTERNAL_TRACE_EVENT( \
158  category, \
159  name, \
160  arg1_name, \
161  arg2_name, \
162  phosphor::TraceEvent::Type::SyncStart, \
163  arg1, \
164  arg2)
165 
166 #define TRACE_EVENT_END2(category, name, arg1_name, arg1, arg2_name, arg2) \
167  PHOSPHOR_INTERNAL_TRACE_EVENT( \
168  category, \
169  name, \
170  arg1_name, \
171  arg2_name, \
172  phosphor::TraceEvent::Type::SyncEnd, \
173  arg1, \
174  arg2)
175 
216 #define TRACE_EVENT(category, name, ...) \
217  static constexpr const char* PHOSPHOR_INTERNAL_UID(nme) = name; \
218  TRACE_EVENT_START(category, name, __VA_ARGS__); \
219  struct PHOSPHOR_INTERNAL_UID(scoped_trace_t) { \
220  ~PHOSPHOR_INTERNAL_UID(scoped_trace_t)() { \
221  TRACE_EVENT_END0(category, PHOSPHOR_INTERNAL_UID(nme)); \
222  } \
223  } PHOSPHOR_INTERNAL_UID(scoped_trace_inst);
224 
225 #define TRACE_EVENT0(category, name) \
226  static constexpr const char* PHOSPHOR_INTERNAL_UID(nme) = name; \
227  TRACE_EVENT_START0(category, name); \
228  struct PHOSPHOR_INTERNAL_UID(scoped_trace_t) { \
229  ~PHOSPHOR_INTERNAL_UID(scoped_trace_t)() { \
230  TRACE_EVENT_END0(category, PHOSPHOR_INTERNAL_UID(nme));\
231  } \
232  } PHOSPHOR_INTERNAL_UID(scoped_trace_inst);
233 
234 #define TRACE_EVENT1(category, name, arg1_name, arg1) \
235  static constexpr const char* PHOSPHOR_INTERNAL_UID(nme) = name; \
236  TRACE_EVENT_START1(category, name, arg1_name, arg1); \
237  struct PHOSPHOR_INTERNAL_UID(scoped_trace_t) { \
238  ~PHOSPHOR_INTERNAL_UID(scoped_trace_t)() { \
239  TRACE_EVENT_END0(category, PHOSPHOR_INTERNAL_UID(nme)); \
240  } \
241  } PHOSPHOR_INTERNAL_UID(scoped_trace_inst);
242 
243 #define TRACE_EVENT2(category, name, arg1_name, arg1, arg2_name, arg2) \
244  static constexpr const char* PHOSPHOR_INTERNAL_UID(nme) = name; \
245  TRACE_EVENT_START2(category, name, arg1_name, arg1, arg2_name, arg2); \
246  struct PHOSPHOR_INTERNAL_UID(scoped_trace_t) { \
247  ~PHOSPHOR_INTERNAL_UID(scoped_trace_t)() { \
248  TRACE_EVENT_END0(category, PHOSPHOR_INTERNAL_UID(nme)); \
249  } \
250  } PHOSPHOR_INTERNAL_UID(scoped_trace_inst);
251 
252 #define TRACE_FUNCTION(category, ...) \
253  TRACE_EVENT(category, __func__, __VA_ARGS__)
254 
255 #define TRACE_FUNCTION0(category) \
256  TRACE_EVENT0(category, __func__)
257 
258 #define TRACE_FUNCTION1(category, arg1_name, arg1) \
259  TRACE_EVENT1(category, __func__, arg1_name, arg1)
260 
261 #define TRACE_FUNCTION2(category, arg1_name, arg1, arg2_name, arg2) \
262  TRACE_EVENT2(category, __func__, arg1_name, arg1, arg2_name, arg2)
263 
264 #define TRACE_LOCKGUARD(mutex, category, name) \
265  static constexpr const char* PHOSPHOR_INTERNAL_UID(nme) = name ".held"; \
266  static decltype(mutex)& PHOSPHOR_INTERNAL_UID(lk)(mutex); \
267  TRACE_EVENT_START0(category, name ".acquire"); \
268  PHOSPHOR_INTERNAL_UID(lk).lock(); \
269  PHOSPHOR_INTERNAL_ADDITIONAL_TRACE_EVENT0( \
270  second_tpi, category, name ".acquire", phosphor::TraceEvent::Type::SyncEnd); \
271  PHOSPHOR_INTERNAL_ADDITIONAL_TRACE_EVENT0( \
272  third_tpi, category, name ".held", phosphor::TraceEvent::Type::SyncStart); \
273  struct PHOSPHOR_INTERNAL_UID(scoped_trace_t) { \
274  ~PHOSPHOR_INTERNAL_UID(scoped_trace_t)() { \
275  PHOSPHOR_INTERNAL_UID(lk).unlock(); \
276  TRACE_EVENT_END0(category, PHOSPHOR_INTERNAL_UID(nme)); \
277  } \
278  } PHOSPHOR_INTERNAL_UID(scoped_trace_inst);
279 
298 #define TRACE_ASYNC_START(category, name, id, ...) \
299  PHOSPHOR_INTERNAL_TRACE_EVENT( \
300  category, \
301  name, \
302  "id", \
303  "arg1", \
304  phosphor::TraceEvent::Type::AsyncStart, \
305  id, \
306  __VA_ARGS__)
307 
308 #define TRACE_ASYNC_START0(category, name, id) \
309  PHOSPHOR_INTERNAL_TRACE_EVENT( \
310  category, \
311  name, \
312  "id", \
313  "", \
314  phosphor::TraceEvent::Type::AsyncStart, \
315  id)
316 
317 #define TRACE_ASYNC_START1(category, name, id, arg1_name, arg1) \
318  PHOSPHOR_INTERNAL_TRACE_EVENT( \
319  category, \
320  name, \
321  "id", \
322  arg1_name, \
323  phosphor::TraceEvent::Type::AsyncStart, \
324  id, \
325  arg1)
326 
327 #define TRACE_ASYNC_END(category, name, id, ...) \
328  PHOSPHOR_INTERNAL_TRACE_EVENT( \
329  category, \
330  name, \
331  "id_end", \
332  "arg1_end", \
333  phosphor::TraceEvent::Type::AsyncEnd, \
334  id, \
335  __VA_ARGS__)
336 
337 #define TRACE_ASYNC_END0(category, name, id) \
338  PHOSPHOR_INTERNAL_TRACE_EVENT( \
339  category, \
340  name, \
341  "id_end", \
342  "", \
343  phosphor::TraceEvent::Type::AsyncEnd, \
344  id)
345 
346 #define TRACE_ASYNC_END1(category, name, id, arg1_name, arg1) \
347  PHOSPHOR_INTERNAL_TRACE_EVENT( \
348  category, \
349  name, \
350  "id_end", \
351  arg1_name, \
352  phosphor::TraceEvent::Type::AsyncEnd, \
353  id, \
354  arg1)
355 
369 #define TRACE_INSTANT(category, name, ...) \
370  PHOSPHOR_INTERNAL_TRACE_EVENT( \
371  category, \
372  name, \
373  "arg1", \
374  "arg2", \
375  phosphor::TraceEvent::Type::Instant, \
376  __VA_ARGS__)
377 
378 #define TRACE_INSTANT0(category, name) \
379  PHOSPHOR_INTERNAL_TRACE_EVENT0( \
380  category, name, phosphor::TraceEvent::Type::Instant)
381 
382 #define TRACE_INSTANT1(category, name, arg1_name, arg1) \
383  PHOSPHOR_INTERNAL_TRACE_EVENT( \
384  category, \
385  name, \
386  arg1_name, \
387  "", \
388  phosphor::TraceEvent::Type::Instant, \
389  arg1)
390 
391 #define TRACE_INSTANT2(category, name, arg1_name, arg1, arg2_name, arg2) \
392  PHOSPHOR_INTERNAL_TRACE_EVENT( \
393  category, \
394  name, \
395  arg1_name, \
396  arg2_name, \
397  phosphor::TraceEvent::Type::Instant, \
398  arg1, \
399  arg2)
400 
415 #define TRACE_GLOBAL(category, name, ...) \
416  PHOSPHOR_INTERNAL_TRACE_EVENT( \
417  category, \
418  name, \
419  "arg1", \
420  "arg2", \
421  phosphor::TraceEvent::Type::GlobalInstant, \
422  __VA_ARGS__)
423 
424 #define TRACE_GLOBAL0(category, name) \
425  PHOSPHOR_INTERNAL_TRACE_EVENT0( \
426  category, name, phosphor::TraceEvent::Type::GlobalInstant)
427 
428 
429 #define TRACE_GLOBAL1(category, name, arg1_name, arg1) \
430  PHOSPHOR_INTERNAL_TRACE_EVENT( \
431  category, \
432  name, \
433  arg1_name, \
434  "", \
435  phosphor::TraceEvent::Type::GlobalInstant, \
436  arg1)
437 
438 #define TRACE_GLOBAL2(category, name, arg1_name, arg1, arg2_name, arg2) \
439  PHOSPHOR_INTERNAL_TRACE_EVENT( \
440  category, \
441  name, \
442  arg1_name, \
443  arg2_name, \
444  phosphor::TraceEvent::Type::GlobalInstant, \
445  arg1, \
446  arg2)
447 
449 #else // if: defined(PHOSPHOR_DISABLED) && PHOSPHOR_DISABLED != 0
450 
451 #define TRACE_EVENT_START(category, name, ...)
452 #define TRACE_EVENT_START0(category, name)
453 #define TRACE_EVENT_START1(category, name, arg1_name, arg1)
454 #define TRACE_EVENT_START2(category, name, arg1_name, arg1, arg2_name, arg2)
455 #define TRACE_EVENT_END(category, name, ...)
456 #define TRACE_EVENT_END0(category, name)
457 #define TRACE_EVENT_END1(category, name, arg1_name, arg1)
458 #define TRACE_EVENT_END2(category, name, arg1_name, arg1, arg2_name, arg2)
459 
460 #define TRACE_EVENT(category, name, ...)
461 #define TRACE_EVENT0(category, name)
462 #define TRACE_EVENT1(category, name, arg1_name, arg1)
463 #define TRACE_EVENT2(category, name, arg1_name, arg1, arg2_name, arg2)
464 
465 #define TRACE_FUNCTION(category, ...)
466 #define TRACE_FUNCTION0(category)
467 #define TRACE_FUNCTION1(category, name, arg1_name, arg1)
468 #define TRACE_FUNCTION2(category, name, arg1_name, arg1, arg2_name, arg2)
469 
470 #define TRACE_LOCKGUARD(mutex, category, name)
471 
472 #define TRACE_ASYNC_START(category, name, id, ...)
473 #define TRACE_ASYNC_START0(category, name, id)
474 #define TRACE_ASYNC_START1(category, name, id, arg1_name, arg1)
475 #define TRACE_ASYNC_END(category, name, id, ...)
476 #define TRACE_ASYNC_END0(category, name, id)
477 #define TRACE_ASYNC_END1(category, name, id, arg1_name, arg1)
478 
479 #define TRACE_INSTANT(category, name, ...)
480 #define TRACE_INSTANT0(category, name)
481 #define TRACE_INSTANT1(category, name, arg1_name, arg1)
482 #define TRACE_INSTANT2(category, name, arg1_name, arg1, arg2_name, arg2)
483 
484 #define TRACE_GLOBAL(category, name, ...)
485 #define TRACE_GLOBAL0(category, name)
486 #define TRACE_GLOBAL1(category, name, arg1_name, arg1)
487 #define TRACE_GLOBAL2(category, name, arg1_name, arg1, arg2_name, arg2)
488 
489 #endif // PHOSPHOR_DISABLED