Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2017 gRPC authors. |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef TEST_CPP_MICROBENCHMARKS_COUNTERS_H |
| 20 | #define TEST_CPP_MICROBENCHMARKS_COUNTERS_H |
| 21 | |
| 22 | #include <sstream> |
| 23 | |
| 24 | extern "C" { |
| 25 | #include <grpc/support/port_platform.h> |
Craig Tiller | b893046 | 2017-07-19 16:24:19 -0700 | [diff] [blame] | 26 | #include "src/core/lib/debug/stats.h" |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 27 | #include "test/core/util/memory_counters.h" |
| 28 | } |
| 29 | |
Craig Tiller | af11581 | 2017-03-09 15:09:34 -0800 | [diff] [blame] | 30 | #include <benchmark/benchmark.h> |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 31 | #include <grpc++/impl/grpc_library.h> |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 32 | |
| 33 | class Library { |
| 34 | public: |
| 35 | static Library& get() { |
| 36 | static Library lib; |
| 37 | return lib; |
| 38 | } |
| 39 | |
| 40 | grpc_resource_quota* rq() { return rq_; } |
| 41 | |
| 42 | private: |
| 43 | Library() { |
Craig Tiller | a08b11f | 2017-03-10 20:34:08 -0800 | [diff] [blame] | 44 | #ifdef GPR_LOW_LEVEL_COUNTERS |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 45 | grpc_memory_counters_init(); |
Craig Tiller | a08b11f | 2017-03-10 20:34:08 -0800 | [diff] [blame] | 46 | #endif |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 47 | init_lib_.init(); |
| 48 | rq_ = grpc_resource_quota_create("bm"); |
| 49 | } |
| 50 | |
| 51 | ~Library() { init_lib_.shutdown(); } |
| 52 | |
| 53 | grpc::internal::GrpcLibrary init_lib_; |
| 54 | grpc_resource_quota* rq_; |
| 55 | }; |
| 56 | |
| 57 | #ifdef GPR_LOW_LEVEL_COUNTERS |
| 58 | extern "C" gpr_atm gpr_mu_locks; |
| 59 | extern "C" gpr_atm gpr_counter_atm_cas; |
| 60 | extern "C" gpr_atm gpr_counter_atm_add; |
Craig Tiller | e1523e9 | 2017-03-14 21:10:42 -0700 | [diff] [blame] | 61 | extern "C" gpr_atm gpr_now_call_count; |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 62 | #endif |
| 63 | |
| 64 | class TrackCounters { |
| 65 | public: |
Craig Tiller | b893046 | 2017-07-19 16:24:19 -0700 | [diff] [blame] | 66 | TrackCounters() { grpc_stats_collect(&stats_begin_); } |
Craig Tiller | f09ec59 | 2017-03-03 16:52:37 -0800 | [diff] [blame] | 67 | virtual void Finish(benchmark::State& state); |
| 68 | virtual void AddToLabel(std::ostream& out, benchmark::State& state); |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
Craig Tiller | b893046 | 2017-07-19 16:24:19 -0700 | [diff] [blame] | 71 | grpc_stats_data stats_begin_; |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 72 | #ifdef GPR_LOW_LEVEL_COUNTERS |
| 73 | const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); |
| 74 | const size_t atm_cas_at_start_ = |
| 75 | gpr_atm_no_barrier_load(&gpr_counter_atm_cas); |
| 76 | const size_t atm_add_at_start_ = |
| 77 | gpr_atm_no_barrier_load(&gpr_counter_atm_add); |
Craig Tiller | e1523e9 | 2017-03-14 21:10:42 -0700 | [diff] [blame] | 78 | const size_t now_calls_at_start_ = |
| 79 | gpr_atm_no_barrier_load(&gpr_now_call_count); |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 80 | grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot(); |
Craig Tiller | 71c2864 | 2017-03-09 08:03:27 -0800 | [diff] [blame] | 81 | #endif |
Craig Tiller | 5e32153 | 2017-03-03 08:50:25 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | #endif |