blob: 07dd6117098dda008c608fd431dcc75315a650a5 [file] [log] [blame]
Craig Tiller5e321532017-03-03 08:50:25 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2017 gRPC authors.
Craig Tiller5e321532017-03-03 08:50:25 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Tiller5e321532017-03-03 08:50:25 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller5e321532017-03-03 08:50:25 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Tiller5e321532017-03-03 08:50:25 -080016 *
17 */
18
19#ifndef TEST_CPP_MICROBENCHMARKS_COUNTERS_H
20#define TEST_CPP_MICROBENCHMARKS_COUNTERS_H
21
22#include <sstream>
23
24extern "C" {
25#include <grpc/support/port_platform.h>
Craig Tillerb8930462017-07-19 16:24:19 -070026#include "src/core/lib/debug/stats.h"
Craig Tiller5e321532017-03-03 08:50:25 -080027#include "test/core/util/memory_counters.h"
28}
29
Craig Tilleraf115812017-03-09 15:09:34 -080030#include <benchmark/benchmark.h>
Craig Tiller5e321532017-03-03 08:50:25 -080031#include <grpc++/impl/grpc_library.h>
Craig Tiller5e321532017-03-03 08:50:25 -080032
33class 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 Tillera08b11f2017-03-10 20:34:08 -080044#ifdef GPR_LOW_LEVEL_COUNTERS
Craig Tiller5e321532017-03-03 08:50:25 -080045 grpc_memory_counters_init();
Craig Tillera08b11f2017-03-10 20:34:08 -080046#endif
Craig Tiller5e321532017-03-03 08:50:25 -080047 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
58extern "C" gpr_atm gpr_mu_locks;
59extern "C" gpr_atm gpr_counter_atm_cas;
60extern "C" gpr_atm gpr_counter_atm_add;
Craig Tillere1523e92017-03-14 21:10:42 -070061extern "C" gpr_atm gpr_now_call_count;
Craig Tiller5e321532017-03-03 08:50:25 -080062#endif
63
64class TrackCounters {
65 public:
Craig Tillerb8930462017-07-19 16:24:19 -070066 TrackCounters() { grpc_stats_collect(&stats_begin_); }
Craig Tillerf09ec592017-03-03 16:52:37 -080067 virtual void Finish(benchmark::State& state);
68 virtual void AddToLabel(std::ostream& out, benchmark::State& state);
Craig Tiller5e321532017-03-03 08:50:25 -080069
70 private:
Craig Tillerb8930462017-07-19 16:24:19 -070071 grpc_stats_data stats_begin_;
Craig Tiller5e321532017-03-03 08:50:25 -080072#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 Tillere1523e92017-03-14 21:10:42 -070078 const size_t now_calls_at_start_ =
79 gpr_atm_no_barrier_load(&gpr_now_call_count);
Craig Tiller5e321532017-03-03 08:50:25 -080080 grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot();
Craig Tiller71c28642017-03-09 08:03:27 -080081#endif
Craig Tiller5e321532017-03-03 08:50:25 -080082};
83
84#endif