From cc96858f0b950baa62644d449b6df966ee4f286d Mon Sep 17 00:00:00 2001 From: nancylucy01 Date: Fri, 5 Aug 2022 16:24:24 +0000 Subject: [PATCH] Add channel memory benchmark into memory_diff.py (#30507) * Added the channel benchmark to the python script * removing warning * Automated change: Fix sanity tests * added label function for channel and skip for minstack channel * Automated change: Fix sanity tests Co-authored-by: nancylucy01 --- test/core/memory_usage/callback_client.cc | 20 +++---- test/core/memory_usage/callback_server.cc | 1 - test/core/memory_usage/memstats.cc | 1 + tools/profiling/memory/memory_diff.py | 64 +++++++++++++++-------- 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/test/core/memory_usage/callback_client.cc b/test/core/memory_usage/callback_client.cc index 60d93665eb0..d4be27160d6 100644 --- a/test/core/memory_usage/callback_client.cc +++ b/test/core/memory_usage/callback_client.cc @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -150,16 +151,6 @@ int main(int argc, char** argv) { // Getting peak memory usage long peak_server_memory = GetMemUsage(absl::GetFlag(FLAGS_server_pid)); long peak_client_memory = GetMemUsage(); - gpr_log(GPR_INFO, "Before Server Mem: %ld", before_server_memory); - gpr_log(GPR_INFO, "Before Client Mem: %ld", before_client_memory); - gpr_log(GPR_INFO, "Peak Server Mem: %ld", peak_server_memory); - gpr_log(GPR_INFO, "Peak Client Mem: %ld", peak_client_memory); - gpr_log(GPR_INFO, "Server Per Channel Memory: %f", - static_cast(peak_server_memory - before_server_memory) / - static_cast(size)); - gpr_log(GPR_INFO, "Client Per Channel Memory: %f", - static_cast(peak_client_memory - before_client_memory) / - static_cast(size)); // Checking that all channels are still open for (int i = 0; i < size; ++i) { @@ -168,6 +159,15 @@ int main(int argc, char** argv) { std::chrono::system_clock::now() + std::chrono::milliseconds(1))); } + + printf("---------Client channel stats--------\n"); + printf("client channel memory usage: %f bytes per channel\n", + static_cast(peak_client_memory - before_client_memory) / size * + 1024); + printf("---------Server channel stats--------\n"); + printf("server channel memory usage: %f bytes per channel\n", + static_cast(peak_server_memory - before_server_memory) / size * + 1024); gpr_log(GPR_INFO, "Client Done"); return 0; } diff --git a/test/core/memory_usage/callback_server.cc b/test/core/memory_usage/callback_server.cc index 6d69cb63243..dd29c78b643 100644 --- a/test/core/memory_usage/callback_server.cc +++ b/test/core/memory_usage/callback_server.cc @@ -90,7 +90,6 @@ int main(int argc, char** argv) { // Get initial process memory usage before creating server long before_server_create = GetMemUsage(); - gpr_log(GPR_INFO, "Server Before Mem: %ld", before_server_create); ServerCallbackImpl callback_server(before_server_create); grpc::ServerBuilder builder; diff --git a/test/core/memory_usage/memstats.cc b/test/core/memory_usage/memstats.cc index f14cafc78d4..5bb34361f93 100644 --- a/test/core/memory_usage/memstats.cc +++ b/test/core/memory_usage/memstats.cc @@ -52,5 +52,6 @@ long GetMemUsage(absl::optional pid) { // Calculations in case x86-64 is configured to use 2MB pages long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; resident_set = rss * page_size_kb; + // Memory in KB return resident_set; } diff --git a/tools/profiling/memory/memory_diff.py b/tools/profiling/memory/memory_diff.py index 981223f3403..72031af2417 100755 --- a/tools/profiling/memory/memory_diff.py +++ b/tools/profiling/memory/memory_diff.py @@ -43,10 +43,14 @@ argp.add_argument('-j', '--jobs', type=int, default=multiprocessing.cpu_count()) args = argp.parse_args() _INTERESTING = { - 'client call': + 'call/client': (rb'client call memory usage: ([0-9\.]+) bytes per call', float), - 'server call': + 'call/server': (rb'server call memory usage: ([0-9\.]+) bytes per call', float), + 'channel/client': + (rb'client channel memory usage: ([0-9\.]+) bytes per channel', float), + 'channel/server': + (rb'server channel memory usage: ([0-9\.]+) bytes per channel', float), } _SCENARIOS = { @@ -54,6 +58,11 @@ _SCENARIOS = { 'minstack': ['--scenario_config=minstack'], } +_BENCHMARKS = { + 'call': ['--benchmark_names=call', '--size=50000'], + 'channel': ['--benchmark_names=channel', '--size=10000'], +} + def _run(): """Build with Bazel, then run, and extract interesting lines from the output.""" @@ -62,21 +71,23 @@ def _run(): 'test/core/memory_usage/memory_usage_test' ]) ret = {} - for scenario, extra_args in _SCENARIOS.items(): - try: - output = subprocess.check_output([ - 'bazel-bin/test/core/memory_usage/memory_usage_test', - '--benchmark_names=call', - '--size=50000', - ] + extra_args) - except subprocess.CalledProcessError as e: - print('Error running benchmark:', e) - continue - for line in output.splitlines(): - for key, (pattern, conversion) in _INTERESTING.items(): - m = re.match(pattern, line) - if m: - ret[scenario + ': ' + key] = conversion(m.group(1)) + for name, benchmark_args in _BENCHMARKS.items(): + for scenario, extra_args in _SCENARIOS.items(): + #TODO(chenancy) Remove when minstack is implemented for channel + if name == 'channel' and scenario == 'minstack': + continue + try: + output = subprocess.check_output([ + 'bazel-bin/test/core/memory_usage/memory_usage_test', + ] + benchmark_args + extra_args) + except subprocess.CalledProcessError as e: + print('Error running benchmark:', e) + continue + for line in output.splitlines(): + for key, (pattern, conversion) in _INTERESTING.items(): + m = re.match(pattern, line) + if m: + ret[scenario + ': ' + key] = conversion(m.group(1)) return ret @@ -101,7 +112,8 @@ if old is None: text += '{}: {}\n'.format(key, value) else: print(cur, old) - diff_size = 0 + call_diff_size = 0 + channel_diff_size = 0 for scenario in _SCENARIOS.keys(): for key, value in sorted(_INTERESTING.items()): key = scenario + ': ' + key @@ -109,11 +121,19 @@ else: if key not in old: text += '{}: {}\n'.format(key, cur[key]) else: - diff_size += cur[key] - old[key] text += '{}: {} -> {}\n'.format(key, old[key], cur[key]) - - print("DIFF_SIZE: %f" % diff_size) - check_on_pr.label_increase_decrease_on_pr('per-call-memory', diff_size, 64) + if key == 'call/client' or 'call/server': + call_diff_size += cur[key] - old[key] + else: + channel_diff_size += cur[key] - old[key] + + print("CALL_DIFF_SIZE: %f" % call_diff_size) + print("CHANNEL_DIFF_SIZE: %f" % channel_diff_size) + check_on_pr.label_increase_decrease_on_pr('per-call-memory', call_diff_size, + 64) + check_on_pr.label_increase_decrease_on_pr('per-channel-memory', + channel_diff_size, 1000) + #TODO(chennancy)Change significant value when minstack also runs for channel print(text) check_on_pr.check_on_pr('Memory Difference', '```\n%s\n```' % text)