1 /* 2 * 3 * Copyright 2015 gRPC authors. 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 19 #ifndef GRPC_CORE_LIB_PROFILING_TIMERS_H 20 #define GRPC_CORE_LIB_PROFILING_TIMERS_H 21 22 void gpr_timers_global_init(void); 23 void gpr_timers_global_destroy(void); 24 25 void gpr_timer_add_mark(const char* tagstr, int important, const char* file, 26 int line); 27 void gpr_timer_begin(const char* tagstr, int important, const char* file, 28 int line); 29 void gpr_timer_end(const char* tagstr, int important, const char* file, 30 int line); 31 32 void gpr_timers_set_log_filename(const char* filename); 33 34 void gpr_timer_set_enabled(int enabled); 35 36 #if !(defined(GRPC_STAP_PROFILER) + defined(GRPC_BASIC_PROFILER) + \ 37 defined(GRPC_CUSTOM_PROFILER)) 38 /* No profiling. No-op all the things. */ 39 #define GPR_TIMER_MARK(tag, important) \ 40 do { \ 41 } while (0) 42 43 #define GPR_TIMER_SCOPE(tag, important) \ 44 do { \ 45 } while (0) 46 47 #else /* at least one profiler requested... */ 48 /* ... hopefully only one. */ 49 #if defined(GRPC_STAP_PROFILER) && defined(GRPC_BASIC_PROFILER) 50 #error "GRPC_STAP_PROFILER and GRPC_BASIC_PROFILER are mutually exclusive." 51 #endif 52 #if defined(GRPC_STAP_PROFILER) && defined(GRPC_CUSTOM_PROFILER) 53 #error "GRPC_STAP_PROFILER and GRPC_CUSTOM_PROFILER are mutually exclusive." 54 #endif 55 #if defined(GRPC_CUSTOM_PROFILER) && defined(GRPC_BASIC_PROFILER) 56 #error "GRPC_CUSTOM_PROFILER and GRPC_BASIC_PROFILER are mutually exclusive." 57 #endif 58 59 /* Generic profiling interface. */ 60 #define GPR_TIMER_MARK(tag, important) \ 61 gpr_timer_add_mark(tag, important, __FILE__, __LINE__); 62 63 #ifdef GRPC_STAP_PROFILER 64 /* Empty placeholder for now. */ 65 #endif /* GRPC_STAP_PROFILER */ 66 67 #ifdef GRPC_BASIC_PROFILER 68 /* Empty placeholder for now. */ 69 #endif /* GRPC_BASIC_PROFILER */ 70 71 namespace grpc { 72 class ProfileScope { 73 public: ProfileScope(const char * desc,bool important,const char * file,int line)74 ProfileScope(const char* desc, bool important, const char* file, int line) 75 : desc_(desc) { 76 gpr_timer_begin(desc_, important ? 1 : 0, file, line); 77 } ~ProfileScope()78 ~ProfileScope() { gpr_timer_end(desc_, 0, "n/a", 0); } 79 80 private: 81 const char* const desc_; 82 }; 83 } // namespace grpc 84 85 #define GPR_TIMER_SCOPE_NAME_INTERNAL(prefix, line) prefix##line 86 #define GPR_TIMER_SCOPE_NAME(prefix, line) \ 87 GPR_TIMER_SCOPE_NAME_INTERNAL(prefix, line) 88 #define GPR_TIMER_SCOPE(tag, important) \ 89 ::grpc::ProfileScope GPR_TIMER_SCOPE_NAME(_profile_scope_, __LINE__)( \ 90 (tag), (important), __FILE__, __LINE__) 91 92 #endif /* at least one profiler requested. */ 93 94 #endif /* GRPC_CORE_LIB_PROFILING_TIMERS_H */ 95