mirror of https://github.com/grpc/grpc.git
Merge pull request #20462 from veblush/remove-gpr-allocation
Remove gpr_get/set_allocation_functionspull/20934/head
commit
0228f76515
30 changed files with 181 additions and 1214 deletions
@ -1,59 +0,0 @@ |
||||
# Copyright 2017 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
load("//bazel:grpc_build_system.bzl", "grpc_cc_library", "grpc_cc_test", "grpc_package") |
||||
|
||||
grpc_package(name = "test/core/memory_usage") |
||||
|
||||
licenses(["notice"]) # Apache v2 |
||||
|
||||
grpc_cc_library( |
||||
name = "memory_usage_client", |
||||
testonly = 1, |
||||
srcs = ["client.cc"], |
||||
deps = [ |
||||
"//:gpr", |
||||
"//:grpc", |
||||
"//test/core/util:grpc_test_util", |
||||
], |
||||
) |
||||
|
||||
grpc_cc_library( |
||||
name = "memory_usage_server", |
||||
testonly = 1, |
||||
srcs = ["server.cc"], |
||||
deps = [ |
||||
"//:gpr", |
||||
"//:grpc", |
||||
"//test/core/end2end:ssl_test_data", |
||||
"//test/core/util:grpc_test_util", |
||||
], |
||||
) |
||||
|
||||
grpc_cc_test( |
||||
name = "memory_usage_test", |
||||
srcs = ["memory_usage_test.cc"], |
||||
data = [ |
||||
":memory_usage_client", |
||||
":memory_usage_server", |
||||
], |
||||
language = "C++", |
||||
tags = ["no_windows"], # TODO(jtattermusch): breaks windows RBE build if enabled |
||||
deps = [ |
||||
"//:gpr", |
||||
"//:grpc", |
||||
"//test/core/util:grpc_test_util", |
||||
], |
||||
uses_polling = False, |
||||
) |
@ -1,340 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/byte_buffer_reader.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/time.h> |
||||
#include "src/core/lib/gpr/env.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
|
||||
#include "test/core/util/cmdline.h" |
||||
#include "test/core/util/memory_counters.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
static grpc_channel* channel; |
||||
static grpc_completion_queue* cq; |
||||
static grpc_op metadata_ops[2]; |
||||
static grpc_op status_ops[2]; |
||||
static grpc_op snapshot_ops[6]; |
||||
static grpc_op* op; |
||||
|
||||
typedef struct { |
||||
grpc_call* call; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_status_code status; |
||||
grpc_slice details; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
} fling_call; |
||||
|
||||
// Statically allocate call data structs. Enough to accommodate 10000 ping-pong
|
||||
// calls and 1 extra for the snapshot calls.
|
||||
static fling_call calls[10001]; |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
// A call is intentionally divided into two steps. First step is to initiate a
|
||||
// call (i.e send and recv metadata). A call is outstanding after we initated,
|
||||
// so we can measure the call memory usage.
|
||||
static void init_ping_pong_request(int call_idx) { |
||||
grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv); |
||||
|
||||
memset(metadata_ops, 0, sizeof(metadata_ops)); |
||||
op = metadata_ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = |
||||
&calls[call_idx].initial_metadata_recv; |
||||
op++; |
||||
|
||||
grpc_slice hostname = grpc_slice_from_static_string("localhost"); |
||||
calls[call_idx].call = grpc_channel_create_call( |
||||
channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, |
||||
grpc_slice_from_static_string("/Reflector/reflectUnary"), &hostname, |
||||
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call, |
||||
metadata_ops, |
||||
(size_t)(op - metadata_ops), |
||||
tag(call_idx), nullptr)); |
||||
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
||||
} |
||||
|
||||
// Second step is to finish the call (i.e recv status) and destroy the call.
|
||||
static void finish_ping_pong_request(int call_idx) { |
||||
grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv); |
||||
|
||||
memset(status_ops, 0, sizeof(status_ops)); |
||||
op = status_ops; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = |
||||
&calls[call_idx].trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &calls[call_idx].status; |
||||
op->data.recv_status_on_client.status_details = &calls[call_idx].details; |
||||
op++; |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call, |
||||
status_ops, |
||||
(size_t)(op - status_ops), |
||||
tag(call_idx), nullptr)); |
||||
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
||||
grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv); |
||||
grpc_slice_unref(calls[call_idx].details); |
||||
grpc_call_unref(calls[call_idx].call); |
||||
calls[call_idx].call = nullptr; |
||||
} |
||||
|
||||
static struct grpc_memory_counters send_snapshot_request(int call_idx, |
||||
grpc_slice call_type) { |
||||
grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv); |
||||
grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv); |
||||
|
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
memset(snapshot_ops, 0, sizeof(snapshot_ops)); |
||||
op = snapshot_ops; |
||||
|
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = |
||||
&calls[call_idx].initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = |
||||
&calls[call_idx].trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &calls[call_idx].status; |
||||
op->data.recv_status_on_client.status_details = &calls[call_idx].details; |
||||
op++; |
||||
|
||||
grpc_slice hostname = grpc_slice_from_static_string("localhost"); |
||||
calls[call_idx].call = grpc_channel_create_call( |
||||
channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, call_type, &hostname, |
||||
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call, |
||||
snapshot_ops, |
||||
(size_t)(op - snapshot_ops), |
||||
(void*)nullptr, nullptr)); |
||||
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
||||
|
||||
grpc_byte_buffer_reader reader; |
||||
grpc_byte_buffer_reader_init(&reader, response_payload_recv); |
||||
grpc_slice response = grpc_byte_buffer_reader_readall(&reader); |
||||
|
||||
struct grpc_memory_counters snapshot; |
||||
snapshot.total_size_absolute = |
||||
(reinterpret_cast<struct grpc_memory_counters*> GRPC_SLICE_START_PTR( |
||||
response)) |
||||
->total_size_absolute; |
||||
snapshot.total_allocs_absolute = |
||||
(reinterpret_cast<struct grpc_memory_counters*> GRPC_SLICE_START_PTR( |
||||
response)) |
||||
->total_allocs_absolute; |
||||
snapshot.total_size_relative = |
||||
(reinterpret_cast<struct grpc_memory_counters*> GRPC_SLICE_START_PTR( |
||||
response)) |
||||
->total_size_relative; |
||||
snapshot.total_allocs_relative = |
||||
(reinterpret_cast<struct grpc_memory_counters*> GRPC_SLICE_START_PTR( |
||||
response)) |
||||
->total_allocs_relative; |
||||
|
||||
grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv); |
||||
grpc_slice_unref(response); |
||||
grpc_byte_buffer_reader_destroy(&reader); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
grpc_slice_unref(calls[call_idx].details); |
||||
calls[call_idx].details = grpc_empty_slice(); |
||||
grpc_call_unref(calls[call_idx].call); |
||||
calls[call_idx].call = nullptr; |
||||
|
||||
return snapshot; |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_memory_counters_init(); |
||||
grpc_slice slice = grpc_slice_from_copied_string("x"); |
||||
char* fake_argv[1]; |
||||
|
||||
const char* target = "localhost:443"; |
||||
gpr_cmdline* cl; |
||||
grpc_event event; |
||||
|
||||
GPR_ASSERT(argc >= 1); |
||||
fake_argv[0] = argv[0]; |
||||
grpc::testing::TestEnvironment env(1, fake_argv); |
||||
|
||||
grpc_init(); |
||||
|
||||
int warmup_iterations = 100; |
||||
int benchmark_iterations = 1000; |
||||
|
||||
cl = gpr_cmdline_create("memory profiling client"); |
||||
gpr_cmdline_add_string(cl, "target", "Target host:port", &target); |
||||
gpr_cmdline_add_int(cl, "warmup", "Warmup iterations", &warmup_iterations); |
||||
gpr_cmdline_add_int(cl, "benchmark", "Benchmark iterations", |
||||
&benchmark_iterations); |
||||
gpr_cmdline_parse(cl, argc, argv); |
||||
gpr_cmdline_destroy(cl); |
||||
|
||||
for (size_t k = 0; k < GPR_ARRAY_SIZE(calls); k++) { |
||||
calls[k].details = grpc_empty_slice(); |
||||
} |
||||
|
||||
cq = grpc_completion_queue_create_for_next(nullptr); |
||||
|
||||
struct grpc_memory_counters client_channel_start = |
||||
grpc_memory_counters_snapshot(); |
||||
channel = grpc_insecure_channel_create(target, nullptr, nullptr); |
||||
|
||||
int call_idx = 0; |
||||
|
||||
struct grpc_memory_counters before_server_create = send_snapshot_request( |
||||
0, grpc_slice_from_static_string("Reflector/GetBeforeSvrCreation")); |
||||
struct grpc_memory_counters after_server_create = send_snapshot_request( |
||||
0, grpc_slice_from_static_string("Reflector/GetAfterSvrCreation")); |
||||
|
||||
// warmup period
|
||||
for (int i = 0; i < warmup_iterations; i++) { |
||||
send_snapshot_request( |
||||
0, grpc_slice_from_static_string("Reflector/SimpleSnapshot")); |
||||
} |
||||
|
||||
for (call_idx = 0; call_idx < warmup_iterations; ++call_idx) { |
||||
init_ping_pong_request(call_idx + 1); |
||||
} |
||||
|
||||
struct grpc_memory_counters server_benchmark_calls_start = |
||||
send_snapshot_request( |
||||
0, grpc_slice_from_static_string("Reflector/SimpleSnapshot")); |
||||
|
||||
struct grpc_memory_counters client_benchmark_calls_start = |
||||
grpc_memory_counters_snapshot(); |
||||
|
||||
// benchmark period
|
||||
for (; call_idx < warmup_iterations + benchmark_iterations; ++call_idx) { |
||||
init_ping_pong_request(call_idx + 1); |
||||
} |
||||
|
||||
struct grpc_memory_counters client_calls_inflight = |
||||
grpc_memory_counters_snapshot(); |
||||
|
||||
struct grpc_memory_counters server_calls_inflight = send_snapshot_request( |
||||
0, grpc_slice_from_static_string("Reflector/DestroyCalls")); |
||||
|
||||
do { |
||||
event = grpc_completion_queue_next( |
||||
cq, |
||||
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
||||
gpr_time_from_micros(10000, GPR_TIMESPAN)), |
||||
nullptr); |
||||
} while (event.type != GRPC_QUEUE_TIMEOUT); |
||||
|
||||
// second step - recv status and destroy call
|
||||
for (call_idx = 0; call_idx < warmup_iterations + benchmark_iterations; |
||||
++call_idx) { |
||||
finish_ping_pong_request(call_idx + 1); |
||||
} |
||||
|
||||
struct grpc_memory_counters server_calls_end = send_snapshot_request( |
||||
0, grpc_slice_from_static_string("Reflector/SimpleSnapshot")); |
||||
|
||||
struct grpc_memory_counters client_channel_end = |
||||
grpc_memory_counters_snapshot(); |
||||
|
||||
grpc_channel_destroy(channel); |
||||
grpc_completion_queue_shutdown(cq); |
||||
|
||||
do { |
||||
event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
||||
nullptr); |
||||
} while (event.type != GRPC_QUEUE_SHUTDOWN); |
||||
grpc_slice_unref(slice); |
||||
|
||||
grpc_completion_queue_destroy(cq); |
||||
grpc_shutdown_blocking(); |
||||
|
||||
gpr_log(GPR_INFO, "---------client stats--------"); |
||||
gpr_log( |
||||
GPR_INFO, "client call memory usage: %f bytes per call", |
||||
static_cast<double>(client_calls_inflight.total_size_relative - |
||||
client_benchmark_calls_start.total_size_relative) / |
||||
benchmark_iterations); |
||||
gpr_log(GPR_INFO, "client channel memory usage %zi bytes", |
||||
client_channel_end.total_size_relative - |
||||
client_channel_start.total_size_relative); |
||||
|
||||
gpr_log(GPR_INFO, "---------server stats--------"); |
||||
gpr_log(GPR_INFO, "server create: %zi bytes", |
||||
after_server_create.total_size_relative - |
||||
before_server_create.total_size_relative); |
||||
gpr_log( |
||||
GPR_INFO, "server call memory usage: %f bytes per call", |
||||
static_cast<double>(server_calls_inflight.total_size_relative - |
||||
server_benchmark_calls_start.total_size_relative) / |
||||
benchmark_iterations); |
||||
gpr_log(GPR_INFO, "server channel memory usage %zi bytes", |
||||
server_calls_end.total_size_relative - |
||||
after_server_create.total_size_relative); |
||||
|
||||
const char* csv_file = "memory_usage.csv"; |
||||
FILE* csv = fopen(csv_file, "w"); |
||||
if (csv) { |
||||
char* env_build = gpr_getenv("BUILD_NUMBER"); |
||||
char* env_job = gpr_getenv("JOB_NAME"); |
||||
fprintf( |
||||
csv, "%f,%zi,%zi,%f,%zi,%s,%s\n", |
||||
static_cast<double>(client_calls_inflight.total_size_relative - |
||||
client_benchmark_calls_start.total_size_relative) / |
||||
benchmark_iterations, |
||||
client_channel_end.total_size_relative - |
||||
client_channel_start.total_size_relative, |
||||
after_server_create.total_size_relative - |
||||
before_server_create.total_size_relative, |
||||
static_cast<double>(server_calls_inflight.total_size_relative - |
||||
server_benchmark_calls_start.total_size_relative) / |
||||
benchmark_iterations, |
||||
server_calls_end.total_size_relative - |
||||
after_server_create.total_size_relative, |
||||
env_build == nullptr ? "" : env_build, |
||||
env_job == nullptr ? "" : env_job); |
||||
fclose(csv); |
||||
gpr_log(GPR_INFO, "Summary written to %s", csv_file); |
||||
} |
||||
|
||||
grpc_memory_counters_destroy(); |
||||
return 0; |
||||
} |
@ -1,80 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gprpp/host_port.h" |
||||
#include "test/core/util/port.h" |
||||
#include "test/core/util/subprocess.h" |
||||
|
||||
int main(int /*argc*/, char** argv) { |
||||
char* me = argv[0]; |
||||
char* lslash = strrchr(me, '/'); |
||||
char root[1024]; |
||||
int port = grpc_pick_unused_port_or_die(); |
||||
char* args[10]; |
||||
int status; |
||||
gpr_subprocess *svr, *cli; |
||||
/* figure out where we are */ |
||||
if (lslash) { |
||||
memcpy(root, me, static_cast<size_t>(lslash - me)); |
||||
root[lslash - me] = 0; |
||||
} else { |
||||
strcpy(root, "."); |
||||
} |
||||
/* start the server */ |
||||
gpr_asprintf(&args[0], "%s/memory_usage_server%s", root, |
||||
gpr_subprocess_binary_extension()); |
||||
args[1] = const_cast<char*>("--bind"); |
||||
grpc_core::UniquePtr<char> joined; |
||||
grpc_core::JoinHostPort(&joined, "::", port); |
||||
args[2] = joined.get(); |
||||
args[3] = const_cast<char*>("--no-secure"); |
||||
svr = gpr_subprocess_create(4, (const char**)args); |
||||
gpr_free(args[0]); |
||||
|
||||
/* start the client */ |
||||
gpr_asprintf(&args[0], "%s/memory_usage_client%s", root, |
||||
gpr_subprocess_binary_extension()); |
||||
args[1] = const_cast<char*>("--target"); |
||||
grpc_core::JoinHostPort(&joined, "127.0.0.1", port); |
||||
args[2] = joined.get(); |
||||
args[3] = const_cast<char*>("--warmup=1000"); |
||||
args[4] = const_cast<char*>("--benchmark=9000"); |
||||
cli = gpr_subprocess_create(5, (const char**)args); |
||||
gpr_free(args[0]); |
||||
|
||||
/* wait for completion */ |
||||
printf("waiting for client\n"); |
||||
if ((status = gpr_subprocess_join(cli))) { |
||||
gpr_subprocess_destroy(cli); |
||||
gpr_subprocess_destroy(svr); |
||||
return status; |
||||
} |
||||
gpr_subprocess_destroy(cli); |
||||
|
||||
gpr_subprocess_interrupt(svr); |
||||
status = gpr_subprocess_join(svr); |
||||
gpr_subprocess_destroy(svr); |
||||
return status; |
||||
} |
@ -1,324 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/grpc_security.h> |
||||
|
||||
#include <signal.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <time.h> |
||||
#ifndef _WIN32 |
||||
/* This is for _exit() below, which is temporary. */ |
||||
#include <unistd.h> |
||||
#endif |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/gprpp/host_port.h" |
||||
#include "test/core/end2end/data/ssl_test_data.h" |
||||
#include "test/core/util/cmdline.h" |
||||
#include "test/core/util/memory_counters.h" |
||||
#include "test/core/util/port.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
static grpc_completion_queue* cq; |
||||
static grpc_server* server; |
||||
static grpc_op metadata_ops[2]; |
||||
static grpc_op snapshot_ops[5]; |
||||
static grpc_op status_op; |
||||
static int got_sigint = 0; |
||||
static grpc_byte_buffer* payload_buffer = nullptr; |
||||
static grpc_byte_buffer* terminal_buffer = nullptr; |
||||
static int was_cancelled = 2; |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
typedef enum { |
||||
FLING_SERVER_NEW_REQUEST = 1, |
||||
FLING_SERVER_SEND_INIT_METADATA, |
||||
FLING_SERVER_WAIT_FOR_DESTROY, |
||||
FLING_SERVER_SEND_STATUS_FLING_CALL, |
||||
FLING_SERVER_SEND_STATUS_SNAPSHOT, |
||||
FLING_SERVER_BATCH_SEND_STATUS_FLING_CALL |
||||
} fling_server_tags; |
||||
|
||||
typedef struct { |
||||
fling_server_tags state; |
||||
grpc_call* call; |
||||
grpc_call_details call_details; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_metadata_array initial_metadata_send; |
||||
} fling_call; |
||||
|
||||
// hold up to 10000 calls and 6 snaphost calls
|
||||
static fling_call calls[100006]; |
||||
|
||||
static void request_call_unary(int call_idx) { |
||||
if (call_idx == static_cast<int>(sizeof(calls) / sizeof(fling_call))) { |
||||
gpr_log(GPR_INFO, "Used all call slots (10000) on server. Server exit."); |
||||
_exit(0); |
||||
} |
||||
grpc_metadata_array_init(&calls[call_idx].request_metadata_recv); |
||||
grpc_server_request_call( |
||||
server, &calls[call_idx].call, &calls[call_idx].call_details, |
||||
&calls[call_idx].request_metadata_recv, cq, cq, &calls[call_idx]); |
||||
} |
||||
|
||||
static void send_initial_metadata_unary(void* tag) { |
||||
grpc_metadata_array_init( |
||||
&(*static_cast<fling_call*>(tag)).initial_metadata_send); |
||||
metadata_ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
metadata_ops[0].data.send_initial_metadata.count = 0; |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch((*(fling_call*)tag).call, |
||||
metadata_ops, 1, tag, |
||||
nullptr)); |
||||
} |
||||
|
||||
static void send_status(void* tag) { |
||||
status_op.op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
status_op.data.send_status_from_server.status = GRPC_STATUS_OK; |
||||
status_op.data.send_status_from_server.trailing_metadata_count = 0; |
||||
grpc_slice details = grpc_slice_from_static_string(""); |
||||
status_op.data.send_status_from_server.status_details = &details; |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch((*(fling_call*)tag).call, |
||||
&status_op, 1, tag, |
||||
nullptr)); |
||||
} |
||||
|
||||
static void send_snapshot(void* tag, struct grpc_memory_counters* snapshot) { |
||||
grpc_op* op; |
||||
|
||||
grpc_slice snapshot_slice = |
||||
grpc_slice_new(snapshot, sizeof(*snapshot), gpr_free); |
||||
payload_buffer = grpc_raw_byte_buffer_create(&snapshot_slice, 1); |
||||
grpc_metadata_array_init( |
||||
&(*static_cast<fling_call*>(tag)).initial_metadata_send); |
||||
|
||||
op = snapshot_ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &terminal_buffer; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
if (payload_buffer == nullptr) { |
||||
gpr_log(GPR_INFO, "NULL payload buffer !!!"); |
||||
} |
||||
op->data.send_message.send_message = payload_buffer; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_OK; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
grpc_slice details = grpc_slice_from_static_string(""); |
||||
op->data.send_status_from_server.status_details = &details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == |
||||
grpc_call_start_batch((*(fling_call*)tag).call, snapshot_ops, |
||||
(size_t)(op - snapshot_ops), tag, nullptr)); |
||||
} |
||||
/* We have some sort of deadlock, so let's not exit gracefully for now.
|
||||
When that is resolved, please remove the #include <unistd.h> above. */ |
||||
static void sigint_handler(int /*x*/) { _exit(0); } |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_memory_counters_init(); |
||||
grpc_event ev; |
||||
grpc_core::UniquePtr<char> addr_buf; |
||||
gpr_cmdline* cl; |
||||
grpc_completion_queue* shutdown_cq; |
||||
int shutdown_started = 0; |
||||
int shutdown_finished = 0; |
||||
|
||||
int secure = 0; |
||||
const char* addr = nullptr; |
||||
|
||||
char* fake_argv[1]; |
||||
|
||||
GPR_ASSERT(argc >= 1); |
||||
fake_argv[0] = argv[0]; |
||||
grpc::testing::TestEnvironment env(1, fake_argv); |
||||
|
||||
grpc_init(); |
||||
srand(static_cast<unsigned>(clock())); |
||||
|
||||
cl = gpr_cmdline_create("fling server"); |
||||
gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr); |
||||
gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure); |
||||
gpr_cmdline_parse(cl, argc, argv); |
||||
gpr_cmdline_destroy(cl); |
||||
|
||||
if (addr == nullptr) { |
||||
grpc_core::JoinHostPort(&addr_buf, "::", grpc_pick_unused_port_or_die()); |
||||
addr = addr_buf.get(); |
||||
} |
||||
gpr_log(GPR_INFO, "creating server on: %s", addr); |
||||
|
||||
cq = grpc_completion_queue_create_for_next(nullptr); |
||||
|
||||
struct grpc_memory_counters before_server_create = |
||||
grpc_memory_counters_snapshot(); |
||||
if (secure) { |
||||
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key, |
||||
test_server1_cert}; |
||||
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create( |
||||
nullptr, &pem_key_cert_pair, 1, 0, nullptr); |
||||
server = grpc_server_create(nullptr, nullptr); |
||||
GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds)); |
||||
grpc_server_credentials_release(ssl_creds); |
||||
} else { |
||||
server = grpc_server_create(nullptr, nullptr); |
||||
GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr)); |
||||
} |
||||
|
||||
grpc_server_register_completion_queue(server, cq, nullptr); |
||||
grpc_server_start(server); |
||||
|
||||
struct grpc_memory_counters after_server_create = |
||||
grpc_memory_counters_snapshot(); |
||||
|
||||
addr = nullptr; |
||||
addr_buf.reset(); |
||||
|
||||
// initialize call instances
|
||||
for (int i = 0; i < static_cast<int>(sizeof(calls) / sizeof(fling_call)); |
||||
i++) { |
||||
grpc_call_details_init(&calls[i].call_details); |
||||
calls[i].state = FLING_SERVER_NEW_REQUEST; |
||||
} |
||||
|
||||
int next_call_idx = 0; |
||||
struct grpc_memory_counters current_snapshot; |
||||
|
||||
request_call_unary(next_call_idx); |
||||
|
||||
signal(SIGINT, sigint_handler); |
||||
|
||||
while (!shutdown_finished) { |
||||
if (got_sigint && !shutdown_started) { |
||||
gpr_log(GPR_INFO, "Shutting down due to SIGINT"); |
||||
|
||||
shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr); |
||||
grpc_server_shutdown_and_notify(server, shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck( |
||||
shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_completion_queue_destroy(shutdown_cq); |
||||
grpc_completion_queue_shutdown(cq); |
||||
shutdown_started = 1; |
||||
} |
||||
ev = grpc_completion_queue_next( |
||||
cq, |
||||
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
||||
gpr_time_from_micros(1000000, GPR_TIMESPAN)), |
||||
nullptr); |
||||
fling_call* s = static_cast<fling_call*>(ev.tag); |
||||
switch (ev.type) { |
||||
case GRPC_OP_COMPLETE: |
||||
switch (s->state) { |
||||
case FLING_SERVER_NEW_REQUEST: |
||||
request_call_unary(++next_call_idx); |
||||
if (0 == grpc_slice_str_cmp(s->call_details.method, |
||||
"/Reflector/reflectUnary")) { |
||||
s->state = FLING_SERVER_SEND_INIT_METADATA; |
||||
send_initial_metadata_unary(s); |
||||
} else if (0 == |
||||
grpc_slice_str_cmp(s->call_details.method, |
||||
"Reflector/GetBeforeSvrCreation")) { |
||||
s->state = FLING_SERVER_SEND_STATUS_SNAPSHOT; |
||||
send_snapshot(s, &before_server_create); |
||||
} else if (0 == |
||||
grpc_slice_str_cmp(s->call_details.method, |
||||
"Reflector/GetAfterSvrCreation")) { |
||||
s->state = FLING_SERVER_SEND_STATUS_SNAPSHOT; |
||||
send_snapshot(s, &after_server_create); |
||||
} else if (0 == grpc_slice_str_cmp(s->call_details.method, |
||||
"Reflector/SimpleSnapshot")) { |
||||
s->state = FLING_SERVER_SEND_STATUS_SNAPSHOT; |
||||
current_snapshot = grpc_memory_counters_snapshot(); |
||||
send_snapshot(s, ¤t_snapshot); |
||||
} else if (0 == grpc_slice_str_cmp(s->call_details.method, |
||||
"Reflector/DestroyCalls")) { |
||||
s->state = FLING_SERVER_BATCH_SEND_STATUS_FLING_CALL; |
||||
current_snapshot = grpc_memory_counters_snapshot(); |
||||
send_snapshot(s, ¤t_snapshot); |
||||
} else { |
||||
gpr_log(GPR_ERROR, "Wrong call method"); |
||||
} |
||||
break; |
||||
case FLING_SERVER_SEND_INIT_METADATA: |
||||
s->state = FLING_SERVER_WAIT_FOR_DESTROY; |
||||
break; |
||||
case FLING_SERVER_WAIT_FOR_DESTROY: |
||||
break; |
||||
case FLING_SERVER_SEND_STATUS_FLING_CALL: |
||||
grpc_call_unref(s->call); |
||||
grpc_call_details_destroy(&s->call_details); |
||||
grpc_metadata_array_destroy(&s->initial_metadata_send); |
||||
grpc_metadata_array_destroy(&s->request_metadata_recv); |
||||
break; |
||||
case FLING_SERVER_BATCH_SEND_STATUS_FLING_CALL: |
||||
for (int k = 0; |
||||
k < static_cast<int>(sizeof(calls) / sizeof(fling_call)); |
||||
++k) { |
||||
if (calls[k].state == FLING_SERVER_WAIT_FOR_DESTROY) { |
||||
calls[k].state = FLING_SERVER_SEND_STATUS_FLING_CALL; |
||||
send_status(&calls[k]); |
||||
} |
||||
} |
||||
/* fallthrough */ |
||||
// no break here since we want to continue to case
|
||||
// FLING_SERVER_SEND_STATUS_SNAPSHOT to destroy the snapshot call
|
||||
case FLING_SERVER_SEND_STATUS_SNAPSHOT: |
||||
grpc_byte_buffer_destroy(payload_buffer); |
||||
grpc_byte_buffer_destroy(terminal_buffer); |
||||
grpc_call_unref(s->call); |
||||
grpc_call_details_destroy(&s->call_details); |
||||
grpc_metadata_array_destroy(&s->initial_metadata_send); |
||||
grpc_metadata_array_destroy(&s->request_metadata_recv); |
||||
terminal_buffer = nullptr; |
||||
payload_buffer = nullptr; |
||||
break; |
||||
} |
||||
break; |
||||
case GRPC_QUEUE_SHUTDOWN: |
||||
GPR_ASSERT(shutdown_started); |
||||
shutdown_finished = 1; |
||||
break; |
||||
case GRPC_QUEUE_TIMEOUT: |
||||
break; |
||||
} |
||||
} |
||||
|
||||
grpc_server_destroy(server); |
||||
grpc_completion_queue_destroy(cq); |
||||
grpc_shutdown_blocking(); |
||||
grpc_memory_counters_destroy(); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue