00001 #ifndef MINITRACE_H
00002 #define MINITRACE_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <inttypes.h>
00025
00026 #define MTR_ENABLED
00027
00028
00029
00030
00031
00032
00033
00034
00035 #define INTERNAL_MINITRACE_BUFFER_SIZE 1000000
00036
00037
00038
00039 namespace minitrace {
00040
00041
00042
00043 void mtr_init(const char *json_file);
00044
00045
00046 void mtr_shutdown();
00047
00048
00049
00050
00051 void mtr_start();
00052 void mtr_stop();
00053
00054
00055 void mtr_flush();
00056
00057
00058 int64_t mtr_time_usec();
00059
00060
00061
00062 void mtr_register_sigint_handler();
00063
00064
00065
00066
00067
00068 const char *mtr_pool_string(const char *str);
00069
00070
00071 typedef enum {
00072 MTR_ARG_TYPE_NONE = 0,
00073 MTR_ARG_TYPE_INT = 1,
00074
00075
00076 MTR_ARG_TYPE_STRING_CONST = 8,
00077 MTR_ARG_TYPE_STRING_COPY = 9,
00078
00079 } mtr_arg_type;
00080
00081
00082
00083 #define MTR_MAX_ARGS 1
00084
00085
00086 void internal_mtr_raw_event(const char *category, const char *name, char ph, void *id);
00087 void internal_mtr_raw_event_arg(const char *category, const char *name, char ph, void *id, mtr_arg_type arg_type, const char *arg_name, void *arg_value);
00088
00089 #ifdef MTR_ENABLED
00090
00091
00092
00093
00094
00095
00096 #define MTR_BEGIN(c, n) internal_mtr_raw_event(c, n, 'B', nullptr)
00097 #define MTR_END(c, n) internal_mtr_raw_event(c, n, 'E', nullptr)
00098 #define MTR_SCOPE(c, n) MTRScopedTrace ____mtr_scope(c, n)
00099 #define MTR_SCOPE_LIMIT(c, n, l) MTRScopedTraceLimit ____mtr_scope(c, n, l)
00100
00101
00102 #define MTR_START(c, n, id) internal_mtr_raw_event(c, n, 'S', (void *)(id))
00103 #define MTR_STEP(c, n, id, step) internal_mtr_raw_event_arg(c, n, 'T', (void *)(id), MTR_ARG_TYPE_STRING_CONST, "step", (void *)(step))
00104 #define MTR_FINISH(c, n, id) internal_mtr_raw_event(c, n, 'F', (void *)(id))
00105
00106
00107 #define MTR_FLOW_START(c, n, id) internal_mtr_raw_event(c, n, 's', (void *)(id))
00108 #define MTR_FLOW_STEP(c, n, id, step) internal_mtr_raw_event_arg(c, n, 't', (void *)(id), MTR_ARG_TYPE_STRING_CONST, "step", (void *)(step))
00109 #define MTR_FLOW_FINISH(c, n, id) internal_mtr_raw_event(c, n, 'f', (void *)(id))
00110
00111
00112
00113
00114
00115
00116
00117
00118 #define MTR_BEGIN_C(c, n, aname, astrval) internal_mtr_raw_event_arg(c, n, 'B', 0, MTR_ARG_TYPE_STRING_CONST, aname, (void *)(astrval))
00119 #define MTR_END_C(c, n, aname, astrval) internal_mtr_raw_event_arg(c, n, 'E', 0, MTR_ARG_TYPE_STRING_CONST, aname, (void *)(astrval))
00120 #define MTR_SCOPE_C(c, n, aname, astrval) MTRScopedTraceArg ____mtr_scope(c, n, MTR_ARG_TYPE_STRING_CONST, aname, (void *)(astrval))
00121
00122 #define MTR_BEGIN_S(c, n, aname, astrval) internal_mtr_raw_event_arg(c, n, 'B', 0, MTR_ARG_TYPE_STRING_COPY, aname, (void *)(astrval))
00123 #define MTR_END_S(c, n, aname, astrval) internal_mtr_raw_event_arg(c, n, 'E', 0, MTR_ARG_TYPE_STRING_COPY, aname, (void *)(astrval))
00124 #define MTR_SCOPE_S(c, n, aname, astrval) MTRScopedTraceArg ____mtr_scope(c, n, MTR_ARG_TYPE_STRING_COPY, aname, (void *)(astrval))
00125
00126 #define MTR_BEGIN_I(c, n, aname, aintval) internal_mtr_raw_event_arg(c, n, 'B', 0, MTR_ARG_TYPE_INT, aname, (void*)(intptr_t)(aintval))
00127 #define MTR_END_I(c, n, aname, aintval) internal_mtr_raw_event_arg(c, n, 'E', 0, MTR_ARG_TYPE_INT, aname, (void*)(intptr_t)(aintval))
00128 #define MTR_SCOPE_I(c, n, aname, aintval) MTRScopedTraceArg ____mtr_scope(c, n, MTR_ARG_TYPE_INT, aname, (void*)(intptr_t)(aintval))
00129
00130
00131 #define MTR_INSTANT(c, n) internal_mtr_raw_event(c, n, 'I', nullptr)
00132 #define MTR_INSTANT_C(c, n, aname, astrval) internal_mtr_raw_event(c, n, 'I', 0, MTR_ARG_TYPE_STRING_CONST, aname, (void *)(astrval))
00133 #define MTR_INSTANT_I(c, n, aname, aintval) internal_mtr_raw_event(c, n, 'I', 0, MTR_ARG_TYPE_INT, aname, (void *)(aintval))
00134
00135
00136 #define MTR_COUNTER(c, n, val) internal_mtr_raw_event_arg(c, n, 'C', 0, MTR_ARG_TYPE_INT, n, (void *)(intptr_t)(val))
00137
00138
00139
00140 #define MTR_META_PROCESS_NAME(n) internal_mtr_raw_event_arg("", "process_name", 'M', 0, MTR_ARG_TYPE_STRING_COPY, "name", (void *)(n))
00141 #define MTR_META_THREAD_NAME(n) internal_mtr_raw_event_arg("", "thread_name", 'M', 0, MTR_ARG_TYPE_STRING_COPY, "name", (void *)(n))
00142 #define MTR_META_THREAD_SORT_INDEX(i) internal_mtr_raw_event_arg("", "thread_sort_index", 'M', 0, MTR_ARG_TYPE_INT, "sort_index", (void *)(i))
00143
00144 #else
00145
00146 #define MTR_BEGIN(c, n)
00147 #define MTR_END(c, n)
00148 #define MTR_SCOPE(c, n)
00149 #define MTR_START(c, n, id)
00150 #define MTR_STEP(c, n, id, step)
00151 #define MTR_FINISH(c, n, id)
00152 #define MTR_FLOW_START(c, n, id)
00153 #define MTR_FLOW_STEP(c, n, id, step)
00154 #define MTR_FLOW_FINISH(c, n, id)
00155 #define MTR_INSTANT(c, n)
00156
00157 #define MTR_BEGIN_C(c, n, aname, astrval)
00158 #define MTR_END_C(c, n, aname, astrval)
00159 #define MTR_SCOPE_C(c, n, aname, astrval)
00160
00161 #define MTR_BEGIN_S(c, n, aname, astrval)
00162 #define MTR_END_S(c, n, aname, astrval)
00163 #define MTR_SCOPE_S(c, n, aname, astrval)
00164
00165 #define MTR_BEGIN_I(c, n, aname, aintval)
00166 #define MTR_END_I(c, n, aname, aintval)
00167 #define MTR_SCOPE_I(c, n, aname, aintval)
00168
00169 #define MTR_INSTANT(c, n)
00170 #define MTR_INSTANT_C(c, n, aname, astrval)
00171 #define MTR_INSTANT_I(c, n, aname, aintval)
00172
00173
00174 #define MTR_COUNTER(c, n, val)
00175
00176
00177
00178 #define MTR_META_PROCESS_NAME(n)
00179
00180 #define MTR_META_THREAD_NAME(n)
00181 #define MTR_META_THREAD_SORT_INDEX(i)
00182
00183 #endif
00184
00185
00186
00187 #define MTR_BEGIN_FUNC() MTR_BEGIN(__FILE__, __FUNCTION__)
00188 #define MTR_END_FUNC() MTR_END(__FILE__, __FUNCTION__)
00189 #define MTR_SCOPE_FUNC() MTR_SCOPE(__FILE__, __FUNCTION__)
00190 #define MTR_INSTANT_FUNC() MTR_INSTANT(__FILE__, __FUNCTION__)
00191 #define MTR_SCOPE_FUNC_LIMIT_S(l) MTRScopedTraceLimit ____mtr_scope(__FILE__, __FUNCTION__, l)
00192 #define MTR_SCOPE_FUNC_LIMIT_MS(l) MTRScopedTraceLimit ____mtr_scope(__FILE__, __FUNCTION__, 1)
00193
00194
00195 #define MTR_BEGIN_FUNC_S(aname, arg) MTR_BEGIN_S(__FILE__, __FUNCTION__, aname, arg)
00196 #define MTR_END_FUNC_S(aname, arg) MTR_END_S(__FILE__, __FUNCTION__, aname, arg)
00197 #define MTR_SCOPE_FUNC_S(aname, arg) MTR_SCOPE_S(__FILE__, __FUNCTION__, aname, arg)
00198
00199 #define MTR_BEGIN_FUNC_C(aname, arg) MTR_BEGIN_C(__FILE__, __FUNCTION__, aname, arg)
00200 #define MTR_END_FUNC_C(aname, arg) MTR_END_C(__FILE__, __FUNCTION__, aname, arg)
00201 #define MTR_SCOPE_FUNC_C(aname, arg) MTR_SCOPE_C(__FILE__, __FUNCTION__, aname, arg)
00202
00203 #define MTR_BEGIN_FUNC_I(aname, arg) MTR_BEGIN_I(__FILE__, __FUNCTION__, aname, arg)
00204 #define MTR_END_FUNC_I(aname, arg) MTR_END_I(__FILE__, __FUNCTION__, aname, arg)
00205 #define MTR_SCOPE_FUNC_I(aname, arg) MTR_SCOPE_I(__FILE__, __FUNCTION__, aname, arg)
00206
00207
00208 #ifdef MTR_ENABLED
00209
00210 class MTRScopedTrace {
00211 public:
00212 MTRScopedTrace(const char *category, const char *name)
00213 : category_(category), name_(name) {
00214 start_time_ = mtr_time_usec();
00215 }
00216 ~MTRScopedTrace() {
00217 internal_mtr_raw_event(category_, name_, 'X', &start_time_);
00218 }
00219
00220 private:
00221 const char *category_;
00222 const char *name_;
00223 int64_t start_time_;
00224 };
00225
00226
00227
00228 class MTRScopedTraceLimit {
00229 public:
00230 MTRScopedTraceLimit(const char *category, const char *name, double limit_s)
00231 : category_(category), name_(name), limit_(limit_s) {
00232 start_time_ = mtr_time_usec();
00233 }
00234 ~MTRScopedTraceLimit() {
00235 int64_t end_time = mtr_time_usec();
00236 if (end_time - start_time_ >= limit_) {
00237 internal_mtr_raw_event(category_, name_, 'X', &start_time_);
00238 }
00239 }
00240
00241 private:
00242 const char *category_;
00243 const char *name_;
00244 double start_time_;
00245 double limit_;
00246 };
00247
00248 class MTRScopedTraceArg {
00249 public:
00250 MTRScopedTraceArg(const char *category, const char *name, mtr_arg_type arg_type, const char *arg_name, void *arg_value)
00251 : category_(category), name_(name) {
00252 internal_mtr_raw_event_arg(category, name, 'B', 0, arg_type, arg_name, arg_value);
00253 }
00254 ~MTRScopedTraceArg() {
00255 internal_mtr_raw_event(category_, name_, 'E', 0);
00256 }
00257
00258 private:
00259 const char *category_;
00260 const char *name_;
00261 };
00262
00263 #endif
00264
00265 }
00266
00267 #endif