1 /* 2 * 3 * Copyright 2017 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_DEBUG_STATS_H 20 #define GRPC_CORE_LIB_DEBUG_STATS_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/support/atm.h> 25 #include "src/core/lib/debug/stats_data.h" 26 #include "src/core/lib/iomgr/exec_ctx.h" 27 28 typedef struct grpc_stats_data { 29 gpr_atm counters[GRPC_STATS_COUNTER_COUNT]; 30 gpr_atm histograms[GRPC_STATS_HISTOGRAM_BUCKETS]; 31 } grpc_stats_data; 32 33 extern grpc_stats_data* grpc_stats_per_cpu_storage; 34 35 #define GRPC_THREAD_STATS_DATA() \ 36 (&grpc_stats_per_cpu_storage[grpc_core::ExecCtx::Get()->starting_cpu()]) 37 38 /* Only collect stats if GRPC_COLLECT_STATS is defined or it is a debug build. 39 */ 40 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) 41 #define GRPC_STATS_INC_COUNTER(ctr) \ 42 (gpr_atm_no_barrier_fetch_add(&GRPC_THREAD_STATS_DATA()->counters[(ctr)], 1)) 43 44 #define GRPC_STATS_INC_HISTOGRAM(histogram, index) \ 45 (gpr_atm_no_barrier_fetch_add( \ 46 &GRPC_THREAD_STATS_DATA()->histograms[histogram##_FIRST_SLOT + (index)], \ 47 1)) 48 #else /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ 49 #define GRPC_STATS_INC_COUNTER(ctr) 50 #define GRPC_STATS_INC_HISTOGRAM(histogram, index) 51 #endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */ 52 53 void grpc_stats_init(void); 54 void grpc_stats_shutdown(void); 55 void grpc_stats_collect(grpc_stats_data* output); 56 // c = b-a 57 void grpc_stats_diff(const grpc_stats_data* b, const grpc_stats_data* a, 58 grpc_stats_data* c); 59 char* grpc_stats_data_as_json(const grpc_stats_data* data); 60 int grpc_stats_histo_find_bucket_slow(int value, const int* table, 61 int table_size); 62 double grpc_stats_histo_percentile(const grpc_stats_data* data, 63 grpc_stats_histograms histogram, 64 double percentile); 65 size_t grpc_stats_histo_count(const grpc_stats_data* data, 66 grpc_stats_histograms histogram); 67 68 #endif 69