Skip to content

Commit bf72db4

Browse files
authored
Collect memory measurements of the minimal stack (grpc#29164)
* minstack measuring * fix * sort * x * x * x * x * Automated change: Fix sanity tests * review feedback Co-authored-by: ctiller <[email protected]>
1 parent 6206700 commit bf72db4

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

test/core/memory_usage/client.cc

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <grpc/support/log.h>
3131
#include <grpc/support/time.h>
3232

33+
#include "src/core/lib/channel/channel_args.h"
3334
#include "src/core/lib/gpr/env.h"
3435
#include "src/core/lib/gpr/string.h"
3536
#include "src/core/lib/gpr/useful.h"
@@ -214,6 +215,7 @@ std::pair<MemStats, MemStats> run_test_loop(int iterations, int* call_idx) {
214215
ABSL_FLAG(std::string, target, "localhost:443", "Target host:port");
215216
ABSL_FLAG(int, warmup, 100, "Warmup iterations");
216217
ABSL_FLAG(int, benchmark, 1000, "Benchmark iterations");
218+
ABSL_FLAG(bool, minstack, false, "Use minimal stack");
217219

218220
int main(int argc, char** argv) {
219221
absl::ParseCommandLine(argc, argv);
@@ -233,8 +235,15 @@ int main(int argc, char** argv) {
233235

234236
cq = grpc_completion_queue_create_for_next(nullptr);
235237

238+
std::vector<grpc_arg> args_vec;
239+
if (absl::GetFlag(FLAGS_minstack)) {
240+
args_vec.push_back(grpc_channel_arg_integer_create(
241+
const_cast<char*>(GRPC_ARG_MINIMAL_STACK), 1));
242+
}
243+
grpc_channel_args args = {args_vec.size(), args_vec.data()};
244+
236245
channel = grpc_channel_create(absl::GetFlag(FLAGS_target).c_str(),
237-
grpc_insecure_credentials_create(), nullptr);
246+
grpc_insecure_credentials_create(), &args);
238247

239248
int call_idx = 0;
240249
const int warmup_iterations = absl::GetFlag(FLAGS_warmup);

test/core/memory_usage/memory_usage_test.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
ABSL_FLAG(int, warmup, 100, "Warmup iterations");
3636
ABSL_FLAG(int, benchmark, 1000, "Benchmark iterations");
37+
ABSL_FLAG(bool, minstack, false, "Use minimal stack");
3738

3839
class Subprocess {
3940
public:
@@ -74,15 +75,16 @@ int main(int argc, char** argv) {
7475
/* start the server */
7576
Subprocess svr({absl::StrCat(root, "/memory_usage_server",
7677
gpr_subprocess_binary_extension()),
77-
"--bind", grpc_core::JoinHostPort("::", port), "--nosecure"});
78+
"--bind", grpc_core::JoinHostPort("::", port), "--nosecure",
79+
absl::StrCat("--minstack=", absl::GetFlag(FLAGS_minstack))});
7880

7981
/* start the client */
80-
Subprocess cli(
81-
{absl::StrCat(root, "/memory_usage_client",
82-
gpr_subprocess_binary_extension()),
83-
"--target", grpc_core::JoinHostPort("127.0.0.1", port),
84-
absl::StrCat("--warmup=", absl::GetFlag(FLAGS_warmup)),
85-
absl::StrCat("--benchmark=", absl::GetFlag(FLAGS_benchmark))});
82+
Subprocess cli({absl::StrCat(root, "/memory_usage_client",
83+
gpr_subprocess_binary_extension()),
84+
"--target", grpc_core::JoinHostPort("127.0.0.1", port),
85+
absl::StrCat("--warmup=", absl::GetFlag(FLAGS_warmup)),
86+
absl::StrCat("--benchmark=", absl::GetFlag(FLAGS_benchmark)),
87+
absl::StrCat("--minstack=", absl::GetFlag(FLAGS_minstack))});
8688

8789
/* wait for completion */
8890
if ((status = cli.Join()) != 0) {

test/core/memory_usage/server.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <grpc/support/log.h>
4141
#include <grpc/support/time.h>
4242

43+
#include "src/core/lib/channel/channel_args.h"
4344
#include "src/core/lib/gprpp/host_port.h"
4445
#include "test/core/end2end/data/ssl_test_data.h"
4546
#include "test/core/memory_usage/memstats.h"
@@ -154,6 +155,7 @@ static void sigint_handler(int /*x*/) { _exit(0); }
154155

155156
ABSL_FLAG(std::string, bind, "", "Bind host:port");
156157
ABSL_FLAG(bool, secure, false, "Use security");
158+
ABSL_FLAG(bool, minstack, false, "Use minimal stack");
157159

158160
int main(int argc, char** argv) {
159161
absl::ParseCommandLine(argc, argv);
@@ -180,17 +182,24 @@ int main(int argc, char** argv) {
180182

181183
cq = grpc_completion_queue_create_for_next(nullptr);
182184

185+
std::vector<grpc_arg> args_vec;
186+
if (absl::GetFlag(FLAGS_minstack)) {
187+
args_vec.push_back(grpc_channel_arg_integer_create(
188+
const_cast<char*>(GRPC_ARG_MINIMAL_STACK), 1));
189+
}
190+
grpc_channel_args args = {args_vec.size(), args_vec.data()};
191+
183192
MemStats before_server_create = MemStats::Snapshot();
184193
if (absl::GetFlag(FLAGS_secure)) {
185194
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
186195
test_server1_cert};
187196
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
188197
nullptr, &pem_key_cert_pair, 1, 0, nullptr);
189-
server = grpc_server_create(nullptr, nullptr);
198+
server = grpc_server_create(&args, nullptr);
190199
GPR_ASSERT(grpc_server_add_http2_port(server, addr.c_str(), ssl_creds));
191200
grpc_server_credentials_release(ssl_creds);
192201
} else {
193-
server = grpc_server_create(nullptr, nullptr);
202+
server = grpc_server_create(&args, nullptr);
194203
GPR_ASSERT(grpc_server_add_http2_port(
195204
server, addr.c_str(), grpc_insecure_server_credentials_create()));
196205
}

tools/profiling/memory/memory_diff.py

+36-24
Original file line numberDiff line numberDiff line change
@@ -49,56 +49,68 @@
4949
(rb'server call memory usage: ([0-9\.]+) bytes per call', float),
5050
}
5151

52+
_SCENARIOS = {
53+
'default': [],
54+
'minstack': ['--minstack'],
55+
}
56+
5257

5358
def _run():
5459
"""Build with Bazel, then run, and extract interesting lines from the output."""
5560
subprocess.check_call([
5661
'tools/bazel', 'build', '-c', 'opt',
5762
'test/core/memory_usage/memory_usage_test'
5863
])
59-
output = subprocess.check_output([
60-
'bazel-bin/test/core/memory_usage/memory_usage_test',
61-
'--warmup=10000',
62-
'--benchmark=50000',
63-
])
6464
ret = {}
65-
for line in output.splitlines():
66-
for key, (pattern, conversion) in _INTERESTING.items():
67-
m = re.match(pattern, line)
68-
if m:
69-
ret[key] = conversion(m.group(1))
65+
for scenario, extra_args in _SCENARIOS.items():
66+
try:
67+
output = subprocess.check_output([
68+
'bazel-bin/test/core/memory_usage/memory_usage_test',
69+
'--warmup=10000',
70+
'--benchmark=50000',
71+
] + extra_args)
72+
except subprocess.CalledProcessError as e:
73+
print('Error running benchmark:', e)
74+
continue
75+
for line in output.splitlines():
76+
for key, (pattern, conversion) in _INTERESTING.items():
77+
m = re.match(pattern, line)
78+
if m:
79+
ret[scenario + ': ' + key] = conversion(m.group(1))
7080
return ret
7181

7282

7383
cur = _run()
74-
new = None
75-
76-
print(cur)
84+
old = None
7785

7886
if args.diff_base:
7987
where_am_i = subprocess.check_output(
8088
['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
8189
# checkout the diff base (="old")
8290
subprocess.check_call(['git', 'checkout', args.diff_base])
8391
try:
84-
new = _run()
92+
old = _run()
8593
finally:
86-
# restore the original revision (="new")
94+
# restore the original revision (="cur")
8795
subprocess.check_call(['git', 'checkout', where_am_i])
8896

8997
text = ''
90-
if new is None:
91-
for key, value in cur.items():
98+
if old is None:
99+
print(cur)
100+
for key, value in sorted(cur.items()):
92101
text += '{}: {}\n'.format(key, value)
93102
else:
103+
print(cur, old)
94104
diff_size = 0
95-
for key, value in _INTERESTING.items():
96-
if key in cur:
97-
if key not in new:
98-
text += '{}: {}\n'.format(key, value)
99-
else:
100-
diff_size += cur[key] - new[key]
101-
text += '{}: {} -> {}\n'.format(key, cur[key], new[key])
105+
for scenario in _SCENARIOS.keys():
106+
for key, value in sorted(_INTERESTING.items()):
107+
key = scenario + ': ' + key
108+
if key in cur:
109+
if key not in old:
110+
text += '{}: {}\n'.format(key, cur[key])
111+
else:
112+
diff_size += cur[key] - old[key]
113+
text += '{}: {} -> {}\n'.format(key, old[key], cur[key])
102114

103115
print("DIFF_SIZE: %f" % diff_size)
104116
check_on_pr.label_increase_decrease_on_pr('per-call-memory', diff_size, 64)

0 commit comments

Comments
 (0)