stats.h 2.5 KB

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