| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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 <stdint.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <string> |
| 25 |
#include <utility> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "absl/flags/flag.h" |
| 29 |
#include "absl/flags/parse.h" |
| 30 |
#include "absl/strings/string_view.h" |
| 31 |
|
| 32 |
#include <grpc/byte_buffer.h> |
| 33 |
#include <grpc/byte_buffer_reader.h> |
| 34 |
#include <grpc/grpc.h> |
| 35 |
#include <grpc/grpc_security.h> |
| 36 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 37 |
#include <grpc/slice.h> |
| 38 |
#include <grpc/status.h> |
| 39 |
#include <grpc/support/log.h> |
| 40 |
#include <grpc/support/time.h> |
| 41 |
|
| 42 |
#include "src/core/lib/channel/channel_args.h" |
| 43 |
#include "src/core/lib/gpr/useful.h" |
| 44 |
#include "test/core/memory_usage/memstats.h" |
| 45 |
#include "test/core/util/test_config.h" |
| 46 |
|
| 47 |
static grpc_channel* channel; |
| 48 |
static grpc_completion_queue* cq; |
| 49 |
static grpc_op metadata_ops[2]; |
| 50 |
static grpc_op status_ops[2]; |
| 51 |
static grpc_op snapshot_ops[6]; |
| 52 |
static grpc_op* op; |
| 53 |
|
| 54 |
typedef struct { |
| 55 |
grpc_call* call; |
| 56 |
grpc_metadata_array initial_metadata_recv; |
| 57 |
grpc_status_code status; |
| 58 |
grpc_slice details; |
| 59 |
grpc_metadata_array trailing_metadata_recv; |
| 60 |
} fling_call; |
| 61 |
|
| 62 |
// Statically allocate call data structs. Enough to accommodate 100000 ping-pong |
| 63 |
// calls and 1 extra for the snapshot calls. |
| 64 |
static fling_call calls[100001]; |
| 65 |
|
| 66 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 67 |
|
| 68 |
// A call is intentionally divided into two steps. First step is to initiate a |
| 69 |
// call (i.e send and recv metadata). A call is outstanding after we initated, |
| 70 |
// so we can measure the call memory usage. |
| 71 |
static void init_ping_pong_request(int call_idx) { |
| 72 |
grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv); |
| 73 |
|
| 74 |
memset(metadata_ops, 0, sizeof(metadata_ops)); |
| 75 |
op = metadata_ops; |
| 76 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 77 |
op->data.send_initial_metadata.count = 0; |
| 78 |
op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 79 |
op++; |
| 80 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 81 |
op->data.recv_initial_metadata.recv_initial_metadata = |
| 82 |
&calls[call_idx].initial_metadata_recv; |
| 83 |
op++; |
| 84 |
|
| 85 |
grpc_slice hostname = grpc_slice_from_static_string("localhost"); |
| 86 |
calls[call_idx].call = grpc_channel_create_call( |
| 87 |
channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, |
| 88 |
grpc_slice_from_static_string("/Reflector/reflectUnary"), &hostname, |
| 89 |
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 90 |
|
| 91 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call, |
| 92 |
metadata_ops, |
| 93 |
(size_t)(op - metadata_ops), |
| 94 |
tag(call_idx), nullptr)); |
| 95 |
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 96 |
} |
| 97 |
|
| 98 |
// Second step is to finish the call (i.e recv status) and destroy the call. |
| 99 |
static void finish_ping_pong_request(int call_idx) { |
| 100 |
grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv); |
| 101 |
|
| 102 |
memset(status_ops, 0, sizeof(status_ops)); |
| 103 |
op = status_ops; |
| 104 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 105 |
op->data.recv_status_on_client.trailing_metadata = |
| 106 |
&calls[call_idx].trailing_metadata_recv; |
| 107 |
op->data.recv_status_on_client.status = &calls[call_idx].status; |
| 108 |
op->data.recv_status_on_client.status_details = &calls[call_idx].details; |
| 109 |
op++; |
| 110 |
|
| 111 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call, |
| 112 |
status_ops, |
| 113 |
(size_t)(op - status_ops), |
| 114 |
tag(call_idx), nullptr)); |
| 115 |
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 116 |
grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv); |
| 117 |
grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv); |
| 118 |
grpc_slice_unref(calls[call_idx].details); |
| 119 |
grpc_call_unref(calls[call_idx].call); |
| 120 |
calls[call_idx].call = nullptr; |
| 121 |
} |
| 122 |
|
| 123 |
static MemStats send_snapshot_request(int call_idx, grpc_slice call_type) { |
| 124 |
grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv); |
| 125 |
grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv); |
| 126 |
|
| 127 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 128 |
memset(snapshot_ops, 0, sizeof(snapshot_ops)); |
| 129 |
op = snapshot_ops; |
| 130 |
|
| 131 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 132 |
op->data.send_initial_metadata.count = 0; |
| 133 |
op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 134 |
op++; |
| 135 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 136 |
op++; |
| 137 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 138 |
op->data.recv_initial_metadata.recv_initial_metadata = |
| 139 |
&calls[call_idx].initial_metadata_recv; |
| 140 |
op++; |
| 141 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 142 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 143 |
op++; |
| 144 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 145 |
op->data.recv_status_on_client.trailing_metadata = |
| 146 |
&calls[call_idx].trailing_metadata_recv; |
| 147 |
op->data.recv_status_on_client.status = &calls[call_idx].status; |
| 148 |
op->data.recv_status_on_client.status_details = &calls[call_idx].details; |
| 149 |
op++; |
| 150 |
|
| 151 |
grpc_slice hostname = grpc_slice_from_static_string("localhost"); |
| 152 |
calls[call_idx].call = grpc_channel_create_call( |
| 153 |
channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, call_type, &hostname, |
| 154 |
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 155 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call, |
| 156 |
snapshot_ops, |
| 157 |
(size_t)(op - snapshot_ops), |
| 158 |
(void*)nullptr, nullptr)); |
| 159 |
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 160 |
|
| 161 |
grpc_byte_buffer_reader reader; |
| 162 |
grpc_byte_buffer_reader_init(&reader, response_payload_recv); |
| 163 |
grpc_slice response = grpc_byte_buffer_reader_readall(&reader); |
| 164 |
MemStats snapshot = |
| 165 |
*reinterpret_cast<MemStats*>(GRPC_SLICE_START_PTR(response)); |
| 166 |
|
| 167 |
grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv); |
| 168 |
grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv); |
| 169 |
grpc_slice_unref(response); |
| 170 |
grpc_byte_buffer_reader_destroy(&reader); |
| 171 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 172 |
grpc_slice_unref(calls[call_idx].details); |
| 173 |
calls[call_idx].details = grpc_empty_slice(); |
| 174 |
grpc_call_unref(calls[call_idx].call); |
| 175 |
calls[call_idx].call = nullptr; |
| 176 |
|
| 177 |
return snapshot; |
| 178 |
} |
| 179 |
|
| 180 |
// Create iterations calls, return MemStats when all outstanding |
| 181 |
std::pair<MemStats, MemStats> run_test_loop(int iterations, int* call_idx) { |
| 182 |
grpc_event event; |
| 183 |
|
| 184 |
// benchmark period |
| 185 |
for (int i = 0; i < iterations; ++i) { |
| 186 |
init_ping_pong_request(*call_idx + i + 1); |
| 187 |
} |
| 188 |
|
| 189 |
auto peak = std::make_pair( |
| 190 |
// client |
| 191 |
MemStats::Snapshot(), |
| 192 |
// server |
| 193 |
send_snapshot_request( |
| 194 |
0, grpc_slice_from_static_string("Reflector/DestroyCalls"))); |
| 195 |
|
| 196 |
do { |
| 197 |
event = grpc_completion_queue_next( |
| 198 |
cq, |
| 199 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 200 |
gpr_time_from_micros(10000, GPR_TIMESPAN)), |
| 201 |
nullptr); |
| 202 |
} while (event.type != GRPC_QUEUE_TIMEOUT); |
| 203 |
|
| 204 |
// second step - recv status and destroy call |
| 205 |
for (int i = 0; i < iterations; ++i) { |
| 206 |
finish_ping_pong_request(*call_idx + i + 1); |
| 207 |
} |
| 208 |
|
| 209 |
do { |
| 210 |
event = grpc_completion_queue_next( |
| 211 |
cq, |
| 212 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 213 |
gpr_time_from_micros(10000, GPR_TIMESPAN)), |
| 214 |
nullptr); |
| 215 |
} while (event.type != GRPC_QUEUE_TIMEOUT); |
| 216 |
|
| 217 |
*call_idx += iterations; |
| 218 |
|
| 219 |
return peak; |
| 220 |
} |
| 221 |
|
| 222 |
ABSL_FLAG(std::string, target, "localhost:443", "Target host:port"); |
| 223 |
ABSL_FLAG(int, warmup, 100, "Warmup iterations"); |
| 224 |
ABSL_FLAG(int, benchmark, 1000, "Benchmark iterations"); |
| 225 |
ABSL_FLAG(bool, minstack, false, "Use minimal stack"); |
| 226 |
|
| 227 |
int main(int argc, char** argv) { |
| 228 |
absl::ParseCommandLine(argc, argv); |
| 229 |
|
| 230 |
grpc_slice slice = grpc_slice_from_copied_string("x"); |
| 231 |
char* fake_argv[1]; |
| 232 |
|
| 233 |
GPR_ASSERT(argc >= 1); |
| 234 |
fake_argv[0] = argv[0]; |
| 235 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 236 |
|
| 237 |
grpc_init(); |
| 238 |
|
| 239 |
for (size_t k = 0; k < GPR_ARRAY_SIZE(calls); k++) { |
| 240 |
calls[k].details = grpc_empty_slice(); |
| 241 |
} |
| 242 |
|
| 243 |
cq = grpc_completion_queue_create_for_next(nullptr); |
| 244 |
|
| 245 |
std::vector<grpc_arg> args_vec; |
| 246 |
if (absl::GetFlag(FLAGS_minstack)) { |
| 247 |
args_vec.push_back(grpc_channel_arg_integer_create( |
| 248 |
const_cast<char*>(GRPC_ARG_MINIMAL_STACK), 1)); |
| 249 |
} |
| 250 |
grpc_channel_args args = {args_vec.size(), args_vec.data()}; |
| 251 |
|
| 252 |
channel = grpc_channel_create(absl::GetFlag(FLAGS_target).c_str(), |
| 253 |
grpc_insecure_credentials_create(), &args); |
| 254 |
|
| 255 |
int call_idx = 0; |
| 256 |
const int warmup_iterations = absl::GetFlag(FLAGS_warmup); |
| 257 |
const int benchmark_iterations = absl::GetFlag(FLAGS_benchmark); |
| 258 |
|
| 259 |
// warmup period |
| 260 |
MemStats server_benchmark_calls_start = send_snapshot_request( |
| 261 |
0, grpc_slice_from_static_string("Reflector/SimpleSnapshot")); |
| 262 |
MemStats client_benchmark_calls_start = MemStats::Snapshot(); |
| 263 |
|
| 264 |
run_test_loop(warmup_iterations, &call_idx); |
| 265 |
|
| 266 |
std::pair<MemStats, MemStats> peak = |
| 267 |
run_test_loop(benchmark_iterations, &call_idx); |
| 268 |
|
| 269 |
MemStats client_calls_inflight = peak.first; |
| 270 |
MemStats server_calls_inflight = peak.second; |
| 271 |
|
| 272 |
grpc_channel_destroy(channel); |
| 273 |
grpc_completion_queue_shutdown(cq); |
| 274 |
|
| 275 |
grpc_event event; |
| 276 |
do { |
| 277 |
event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 278 |
nullptr); |
| 279 |
} while (event.type != GRPC_QUEUE_SHUTDOWN); |
| 280 |
grpc_slice_unref(slice); |
| 281 |
|
| 282 |
grpc_completion_queue_destroy(cq); |
| 283 |
grpc_shutdown_blocking(); |
| 284 |
|
| 285 |
printf("---------client stats--------\n"); |
| 286 |
printf("client call memory usage: %f bytes per call\n", |
| 287 |
static_cast<double>(client_calls_inflight.rss - |
| 288 |
client_benchmark_calls_start.rss) / |
| 289 |
benchmark_iterations * 1024); |
| 290 |
|
| 291 |
printf("---------server stats--------\n"); |
| 292 |
printf("server call memory usage: %f bytes per call\n", |
| 293 |
static_cast<double>(server_calls_inflight.rss - |
| 294 |
server_benchmark_calls_start.rss) / |
| 295 |
benchmark_iterations * 1024); |
| 296 |
|
| 297 |
return 0; |
| 298 |
} |