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 #include <string.h>
20
21 #include "test/cpp/microbenchmarks/helpers.h"
22
23 static grpc::internal::GrpcLibraryInitializer g_gli_initializer;
24 static LibraryInitializer* g_libraryInitializer;
25
LibraryInitializer()26 LibraryInitializer::LibraryInitializer() {
27 GPR_ASSERT(g_libraryInitializer == nullptr);
28 g_libraryInitializer = this;
29
30 g_gli_initializer.summon();
31 #ifdef GPR_LOW_LEVEL_COUNTERS
32 grpc_memory_counters_init();
33 #endif
34 init_lib_.init();
35 rq_ = grpc_resource_quota_create("bm");
36 }
37
~LibraryInitializer()38 LibraryInitializer::~LibraryInitializer() {
39 g_libraryInitializer = nullptr;
40 init_lib_.shutdown();
41 grpc_resource_quota_unref(rq_);
42 }
43
get()44 LibraryInitializer& LibraryInitializer::get() {
45 GPR_ASSERT(g_libraryInitializer != nullptr);
46 return *g_libraryInitializer;
47 }
48
Finish(benchmark::State & state)49 void TrackCounters::Finish(benchmark::State& state) {
50 std::ostringstream out;
51 for (const auto& l : labels_) {
52 out << l << ' ';
53 }
54 AddToLabel(out, state);
55 std::string label = out.str();
56 if (label.length() && label[0] == ' ') {
57 label = label.substr(1);
58 }
59 state.SetLabel(label.c_str());
60 }
61
AddLabel(const std::string & label)62 void TrackCounters::AddLabel(const std::string& label) {
63 labels_.push_back(label);
64 }
65
AddToLabel(std::ostream & out,benchmark::State & state)66 void TrackCounters::AddToLabel(std::ostream& out, benchmark::State& state) {
67 // Use the parameters to avoid unused-parameter warnings depending on the
68 // #define's present
69 (void)out;
70 (void)state;
71 #ifdef GRPC_COLLECT_STATS
72 grpc_stats_data stats_end;
73 grpc_stats_collect(&stats_end);
74 grpc_stats_data stats;
75 grpc_stats_diff(&stats_end, &stats_begin_, &stats);
76 for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
77 out << " " << grpc_stats_counter_name[i] << "/iter:"
78 << (static_cast<double>(stats.counters[i]) /
79 static_cast<double>(state.iterations()));
80 }
81 for (int i = 0; i < GRPC_STATS_HISTOGRAM_COUNT; i++) {
82 out << " " << grpc_stats_histogram_name[i] << "-median:"
83 << grpc_stats_histo_percentile(&stats, (grpc_stats_histograms)i, 50.0)
84 << " " << grpc_stats_histogram_name[i] << "-99p:"
85 << grpc_stats_histo_percentile(&stats, (grpc_stats_histograms)i, 99.0);
86 }
87 #endif
88 #ifdef GPR_LOW_LEVEL_COUNTERS
89 grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot();
90 out << " locks/iter:"
91 << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) -
92 mu_locks_at_start_) /
93 (double)state.iterations())
94 << " atm_cas/iter:"
95 << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) -
96 atm_cas_at_start_) /
97 (double)state.iterations())
98 << " atm_add/iter:"
99 << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) -
100 atm_add_at_start_) /
101 (double)state.iterations())
102 << " nows/iter:"
103 << ((double)(gpr_atm_no_barrier_load(&gpr_now_call_count) -
104 now_calls_at_start_) /
105 (double)state.iterations())
106 << " allocs/iter:"
107 << ((double)(counters_at_end.total_allocs_absolute -
108 counters_at_start_.total_allocs_absolute) /
109 (double)state.iterations());
110 #endif
111 }
112