Phosphor
phosphor-internal.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  */
22 #include <atomic>
23 
24 /*
25  * Generates a variable name that will be unique per-line for the given prefix
26  */
27 #define PHOSPHOR_INTERNAL_UID3(a, b) \
28  phosphor_internal_ ##a##_##b
29 
30 #define PHOSPHOR_INTERNAL_UID2(a, b) \
31  PHOSPHOR_INTERNAL_UID3(a, b)
32 
33 #define PHOSPHOR_INTERNAL_UID(prefix) \
34  PHOSPHOR_INTERNAL_UID2(prefix, __LINE__)
35 
36 /*
37  * Sets up the category status variables
38  *
39  * This makes a slight optimisation by storing the result of loading
40  * category_enabled into category_enabled_temp which saves a second
41  * load in the calling macro.
42  */
43 #define PHOSPHOR_INTERNAL_CATEGORY_INFO \
44  static std::atomic<const phosphor::AtomicCategoryStatus*> \
45  PHOSPHOR_INTERNAL_UID(category_enabled); \
46  const phosphor::AtomicCategoryStatus* PHOSPHOR_INTERNAL_UID(category_enabled_temp) \
47  = PHOSPHOR_INTERNAL_UID(category_enabled).load(std::memory_order_acquire); \
48 
49 #define PHOSPHOR_INTERNAL_INITIALIZE_TPI(tpi_name, category, name, argA, argB) \
50  constexpr static phosphor::tracepoint_info PHOSPHOR_INTERNAL_UID(tpi_name) = { \
51  category, \
52  name, \
53  {{argA, argB}} \
54  }; \
55 
56 #define PHOSPHOR_INTERNAL_INITIALIZE_TRACEPOINT(category, name, argA, argB) \
57  PHOSPHOR_INTERNAL_INITIALIZE_TPI(tpi, category, name, argA, argB); \
58  if (unlikely(!PHOSPHOR_INTERNAL_UID(category_enabled_temp))) { \
59  PHOSPHOR_INTERNAL_UID(category_enabled_temp) = &PHOSPHOR_INSTANCE.getCategoryStatus(category); \
60  PHOSPHOR_INTERNAL_UID(category_enabled).store( \
61  PHOSPHOR_INTERNAL_UID(category_enabled_temp), std::memory_order_release); \
62  } \
63 
64 /*
65  * Traces an event of a specified type with one or more arguments
66  *
67  * This compares '!=' to disabled instead of '==' to enabled as it allows
68  * for comparison to 0 rather than comparison to 1 which saves an instruction
69  * on the disabled path when compiled.
70  */
71 #define PHOSPHOR_INTERNAL_TRACE_EVENT(category, name, argA, argB, type, ...) \
72  PHOSPHOR_INTERNAL_CATEGORY_INFO \
73  PHOSPHOR_INTERNAL_INITIALIZE_TRACEPOINT(category, name, argA, argB) \
74  if (PHOSPHOR_INTERNAL_UID(category_enabled_temp)->load(std::memory_order_acquire) \
75  != phosphor::CategoryStatus::Disabled) { \
76  PHOSPHOR_INSTANCE.logEvent(&PHOSPHOR_INTERNAL_UID(tpi), type, __VA_ARGS__); \
77  }
78 
79 /*
80  * Traces an event of a specified type with zero arguments
81  */
82 #define PHOSPHOR_INTERNAL_TRACE_EVENT0(category, name, type) \
83  PHOSPHOR_INTERNAL_CATEGORY_INFO \
84  PHOSPHOR_INTERNAL_INITIALIZE_TRACEPOINT(category, name, "arg1", "arg2") \
85  if (PHOSPHOR_INTERNAL_UID(category_enabled_temp)->load(std::memory_order_relaxed) \
86  != phosphor::CategoryStatus::Disabled) { \
87  PHOSPHOR_INSTANCE.logEvent(&PHOSPHOR_INTERNAL_UID(tpi), type); \
88  }
89 
90 /*
91  * For when additional trace events are created in the same scope.
92  * Therefore the tpi name must be made unique i.e. second_tpi, third_tpi.
93  * Note PHOSPHOR_INTERNAL_CATEGORY_INFO need not be called as the
94  * category_enabled and category_enabled_temp are defined when creating the
95  * first trace event.
96  */
97 #define PHOSPHOR_INTERNAL_ADDITIONAL_TRACE_EVENT0(tpi_name, category, name, type) \
98  PHOSPHOR_INTERNAL_INITIALIZE_TPI(tpi_name, category, name, "arg1", "arg2") \
99  if (PHOSPHOR_INTERNAL_UID(category_enabled_temp)->load(std::memory_order_relaxed) \
100  != phosphor::CategoryStatus::Disabled) { \
101  PHOSPHOR_INSTANCE.logEvent(&PHOSPHOR_INTERNAL_UID(tpi_name), type); \
102  }