Skip to content

Commit

Permalink
Merge pull request grpc#9619 from ctiller/bm_counters
Browse files Browse the repository at this point in the history
Add counters for important metrics to bm_fullstack
  • Loading branch information
ctiller authored Feb 7, 2017
2 parents 3f48430 + 01d7d9b commit ea2c594
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ CPPFLAGS_mutrace = -O3 -fno-omit-frame-pointer
LDFLAGS_mutrace = -rdynamic
DEFINES_mutrace = NDEBUG

VALID_CONFIG_counters = 1
CC_counters = $(DEFAULT_CC)
CXX_counters = $(DEFAULT_CXX)
LD_counters = $(DEFAULT_CC)
LDXX_counters = $(DEFAULT_CXX)
CPPFLAGS_counters = -O2 -DGPR_MU_COUNTERS
DEFINES_counters = NDEBUG



# General settings.
Expand Down
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,9 @@ configs:
basicprof:
CPPFLAGS: -O2 -DGRPC_BASIC_PROFILER -DGRPC_TIMERS_RDTSC
DEFINES: NDEBUG
counters:
CPPFLAGS: -O2 -DGPR_MU_COUNTERS
DEFINES: NDEBUG
dbg:
CPPFLAGS: -O0
DEFINES: _DEBUG DEBUG
Expand Down
7 changes: 7 additions & 0 deletions src/core/lib/support/sync_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@
#include <time.h>
#include "src/core/lib/profiling/timers.h"

#ifdef GPR_MU_COUNTERS
gpr_atm grpc_mu_locks = 0;
#endif

void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); }

void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); }

void gpr_mu_lock(gpr_mu* mu) {
#ifdef GPR_MU_COUNTERS
gpr_atm_no_barrier_fetch_add(&grpc_mu_locks, 1);
#endif
GPR_TIMER_BEGIN("gpr_mu_lock", 0);
GPR_ASSERT(pthread_mutex_lock(mu) == 0);
GPR_TIMER_END("gpr_mu_lock", 0);
Expand Down
53 changes: 44 additions & 9 deletions test/cpp/microbenchmarks/bm_fullstack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern "C" {
#include "src/core/lib/surface/channel.h"
#include "src/core/lib/surface/completion_queue.h"
#include "src/core/lib/surface/server.h"
#include "test/core/util/memory_counters.h"
#include "test/core/util/passthru_endpoint.h"
#include "test/core/util/port.h"
}
Expand All @@ -67,6 +68,7 @@ namespace testing {
static class InitializeStuff {
public:
InitializeStuff() {
grpc_memory_counters_init();
init_lib_.init();
rq_ = grpc_resource_quota_create("bm");
}
Expand Down Expand Up @@ -94,7 +96,42 @@ static void ApplyCommonChannelArguments(ChannelArguments* c) {
c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
}

class FullstackFixture {
#ifdef GPR_MU_COUNTERS
extern "C" gpr_atm grpc_mu_locks;
#endif

class BaseFixture {
public:
void Finish(benchmark::State& s) {
std::ostringstream out;
this->AddToLabel(out, s);
#ifdef GPR_MU_COUNTERS
out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&grpc_mu_locks) -
mu_locks_at_start_) /
(double)s.iterations());
#endif
grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot();
out << " allocs/iter:"
<< ((double)(counters_at_end.total_allocs_absolute -
counters_at_start_.total_allocs_absolute) /
(double)s.iterations());
auto label = out.str();
if (label.length() && label[0] == ' ') {
label = label.substr(1);
}
s.SetLabel(label);
}

virtual void AddToLabel(std::ostream& out, benchmark::State& s) = 0;

private:
#ifdef GPR_MU_COUNTERS
const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&grpc_mu_locks);
#endif
grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot();
};

class FullstackFixture : public BaseFixture {
public:
FullstackFixture(Service* service, const grpc::string& address) {
ServerBuilder b;
Expand Down Expand Up @@ -130,7 +167,7 @@ class TCP : public FullstackFixture {
public:
TCP(Service* service) : FullstackFixture(service, MakeAddress()) {}

void Finish(benchmark::State& state) {}
void AddToLabel(std::ostream& out, benchmark::State& state) {}

private:
static grpc::string MakeAddress() {
Expand All @@ -145,7 +182,7 @@ class UDS : public FullstackFixture {
public:
UDS(Service* service) : FullstackFixture(service, MakeAddress()) {}

void Finish(benchmark::State& state) {}
void AddToLabel(std::ostream& out, benchmark::State& state) override {}

private:
static grpc::string MakeAddress() {
Expand All @@ -157,7 +194,7 @@ class UDS : public FullstackFixture {
}
};

class EndpointPairFixture {
class EndpointPairFixture : public BaseFixture {
public:
EndpointPairFixture(Service* service, grpc_endpoint_pair endpoints) {
ServerBuilder b;
Expand Down Expand Up @@ -233,19 +270,17 @@ class SockPair : public EndpointPairFixture {
"test", initialize_stuff.rq(), 8192)) {
}

void Finish(benchmark::State& state) {}
void AddToLabel(std::ostream& out, benchmark::State& state) {}
};

class InProcessCHTTP2 : public EndpointPairFixture {
public:
InProcessCHTTP2(Service* service)
: EndpointPairFixture(service, MakeEndpoints()) {}

void Finish(benchmark::State& state) {
std::ostringstream out;
out << "writes/iteration:"
void AddToLabel(std::ostream& out, benchmark::State& state) {
out << " writes/iter:"
<< ((double)stats_.num_writes / (double)state.iterations());
state.SetLabel(out.str());
}

private:
Expand Down
3 changes: 3 additions & 0 deletions tools/run_tests/generated/configs.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@
},
{
"config": "mutrace"
},
{
"config": "counters"
}
]
Loading

0 comments on commit ea2c594

Please sign in to comment.