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 TEST_CPP_MICROBENCHMARKS_COUNTERS_H 20 #define TEST_CPP_MICROBENCHMARKS_COUNTERS_H 21 22 #include <sstream> 23 #include <vector> 24 25 #include <grpc/support/port_platform.h> 26 #include "src/core/lib/debug/stats.h" 27 #include "test/core/util/memory_counters.h" 28 29 #include <benchmark/benchmark.h> 30 #include <grpcpp/impl/grpc_library.h> 31 32 class Library { 33 public: get()34 static Library& get() { 35 static Library lib; 36 return lib; 37 } 38 rq()39 grpc_resource_quota* rq() { return rq_; } 40 41 private: Library()42 Library() { 43 #ifdef GPR_LOW_LEVEL_COUNTERS 44 grpc_memory_counters_init(); 45 #endif 46 init_lib_.init(); 47 rq_ = grpc_resource_quota_create("bm"); 48 } 49 ~Library()50 ~Library() { init_lib_.shutdown(); } 51 52 grpc::internal::GrpcLibrary init_lib_; 53 grpc_resource_quota* rq_; 54 }; 55 56 #ifdef GPR_LOW_LEVEL_COUNTERS 57 extern gpr_atm gpr_mu_locks; 58 extern gpr_atm gpr_counter_atm_cas; 59 extern gpr_atm gpr_counter_atm_add; 60 extern gpr_atm gpr_now_call_count; 61 #endif 62 63 class TrackCounters { 64 public: TrackCounters()65 TrackCounters() { grpc_stats_collect(&stats_begin_); } ~TrackCounters()66 virtual ~TrackCounters() {} 67 virtual void Finish(benchmark::State& state); 68 virtual void AddLabel(const grpc::string& label); 69 virtual void AddToLabel(std::ostream& out, benchmark::State& state); 70 71 private: 72 grpc_stats_data stats_begin_; 73 std::vector<grpc::string> labels_; 74 #ifdef GPR_LOW_LEVEL_COUNTERS 75 const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); 76 const size_t atm_cas_at_start_ = 77 gpr_atm_no_barrier_load(&gpr_counter_atm_cas); 78 const size_t atm_add_at_start_ = 79 gpr_atm_no_barrier_load(&gpr_counter_atm_add); 80 const size_t now_calls_at_start_ = 81 gpr_atm_no_barrier_load(&gpr_now_call_count); 82 grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot(); 83 #endif 84 }; 85 86 #endif 87