aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/profiling/timers.h
blob: 7f02b4bf84936a67dfcd885d5e0adfd61e008f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef GRPC_CORE_LIB_PROFILING_TIMERS_H
#define GRPC_CORE_LIB_PROFILING_TIMERS_H

#ifdef __cplusplus
extern "C" {
#endif

void gpr_timers_global_init(void);
void gpr_timers_global_destroy(void);

void gpr_timer_add_mark(const char *tagstr, int important, const char *file,
                        int line);
void gpr_timer_begin(const char *tagstr, int important, const char *file,
                     int line);
void gpr_timer_end(const char *tagstr, int important, const char *file,
                   int line);

void gpr_timers_set_log_filename(const char *filename);

void gpr_timer_set_enabled(int enabled);

#if !(defined(GRPC_STAP_PROFILER) + defined(GRPC_BASIC_PROFILER) + \
      defined(GRPC_CUSTOM_PROFILER))
/* No profiling. No-op all the things. */
#define GPR_TIMER_MARK(tag, important) \
  do {                                 \
  } while (0)

#define GPR_TIMER_BEGIN(tag, important) \
  do {                                  \
  } while (0)

#define GPR_TIMER_END(tag, important) \
  do {                                \
  } while (0)

#else /* at least one profiler requested... */
/* ... hopefully only one. */
#if defined(GRPC_STAP_PROFILER) && defined(GRPC_BASIC_PROFILER)
#error "GRPC_STAP_PROFILER and GRPC_BASIC_PROFILER are mutually exclusive."
#endif
#if defined(GRPC_STAP_PROFILER) && defined(GRPC_CUSTOM_PROFILER)
#error "GRPC_STAP_PROFILER and GRPC_CUSTOM_PROFILER are mutually exclusive."
#endif
#if defined(GRPC_CUSTOM_PROFILER) && defined(GRPC_BASIC_PROFILER)
#error "GRPC_CUSTOM_PROFILER and GRPC_BASIC_PROFILER are mutually exclusive."
#endif

/* Generic profiling interface. */
#define GPR_TIMER_MARK(tag, important) \
  gpr_timer_add_mark(tag, important, __FILE__, __LINE__);

#define GPR_TIMER_BEGIN(tag, important) \
  gpr_timer_begin(tag, important, __FILE__, __LINE__);

#define GPR_TIMER_END(tag, important) \
  gpr_timer_end(tag, important, __FILE__, __LINE__);

#ifdef GRPC_STAP_PROFILER
/* Empty placeholder for now. */
#endif /* GRPC_STAP_PROFILER */

#ifdef GRPC_BASIC_PROFILER
/* Empty placeholder for now. */
#endif /* GRPC_BASIC_PROFILER */

#endif /* at least one profiler requested. */

#ifdef __cplusplus
}

#if (defined(GRPC_STAP_PROFILER) + defined(GRPC_BASIC_PROFILER) + \
     defined(GRPC_CUSTOM_PROFILER))
namespace grpc {
class ProfileScope {
 public:
  ProfileScope(const char *desc, bool important, const char *file, int line)
      : desc_(desc) {
    gpr_timer_begin(desc_, important ? 1 : 0, file, line);
  }
  ~ProfileScope() { gpr_timer_end(desc_, 0, "n/a", 0); }

 private:
  const char *const desc_;
};
}  // namespace grpc

#define GPR_TIMER_SCOPE(tag, important)                                        \
  ::grpc::ProfileScope _profile_scope_##__LINE__((tag), (important), __FILE__, \
                                                 __LINE__)
#else
#define GPR_TIMER_SCOPE(tag, important) \
  do {                                  \
  } while (false)
#endif
#endif

#endif /* GRPC_CORE_LIB_PROFILING_TIMERS_H */