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