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 LibraryInitializer { 33 public: 34 LibraryInitializer(); 35 ~LibraryInitializer(); 36 rq()37 grpc_resource_quota* rq() { return rq_; } 38 39 static LibraryInitializer& get(); 40 41 private: 42 grpc::internal::GrpcLibrary init_lib_; 43 grpc_resource_quota* rq_; 44 }; 45 46 #ifdef GPR_LOW_LEVEL_COUNTERS 47 extern gpr_atm gpr_mu_locks; 48 extern gpr_atm gpr_counter_atm_cas; 49 extern gpr_atm gpr_counter_atm_add; 50 extern gpr_atm gpr_now_call_count; 51 #endif 52 53 class TrackCounters { 54 public: TrackCounters()55 TrackCounters() { grpc_stats_collect(&stats_begin_); } ~TrackCounters()56 virtual ~TrackCounters() {} 57 virtual void Finish(benchmark::State& state); 58 virtual void AddLabel(const std::string& label); 59 virtual void AddToLabel(std::ostream& out, benchmark::State& state); 60 61 private: 62 grpc_stats_data stats_begin_; 63 std::vector<std::string> labels_; 64 #ifdef GPR_LOW_LEVEL_COUNTERS 65 const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); 66 const size_t atm_cas_at_start_ = 67 gpr_atm_no_barrier_load(&gpr_counter_atm_cas); 68 const size_t atm_add_at_start_ = 69 gpr_atm_no_barrier_load(&gpr_counter_atm_add); 70 const size_t now_calls_at_start_ = 71 gpr_atm_no_barrier_load(&gpr_now_call_count); 72 grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot(); 73 #endif 74 }; 75 76 #endif 77