Debian Codegraph @

test/core/memory_usage/callback_server.cc

raw
1 /*
2 *
3 * Copyright 2022 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 <signal.h>
20 #include <unistd.h>
21
22 #include <memory>
23 #include <string>
24
25 #include "absl/flags/flag.h"
26 #include "absl/flags/parse.h"
27 #include "absl/strings/string_view.h"
28
29 #include <grpc/support/log.h>
30 #include <grpcpp/grpcpp.h>
31 #include <grpcpp/security/server_credentials.h>
32 #include <grpcpp/support/server_callback.h>
33 #include <grpcpp/support/status.h>
34
35 #include "src/proto/grpc/testing/benchmark_service.grpc.pb.h"
36 #include "src/proto/grpc/testing/messages.pb.h"
37 #include "test/core/memory_usage/memstats.h"
38 #include "test/core/util/test_config.h"
39
40 class ServerCallbackImpl final
41 : public grpc::testing::BenchmarkService::CallbackService {
42 public:
43 explicit ServerCallbackImpl(long before_server_memory)
44 : before_server_create(before_server_memory) {}
45
46 grpc::ServerUnaryReactor* UnaryCall(
47 grpc::CallbackServerContext* context,
48 const grpc::testing::SimpleRequest* /* request */,
49 grpc::testing::SimpleResponse* /* response */) override {
50 auto* reactor = context->DefaultReactor();
51 reactor->Finish(grpc::Status::OK);
52 return reactor;
53 }
54 grpc::ServerUnaryReactor* GetBeforeSnapshot(
55 grpc::CallbackServerContext* context,
56 const grpc::testing::SimpleRequest* /* request */,
57 grpc::testing::MemorySize* response) override {
58 gpr_log(GPR_INFO, "BeforeSnapshot RPC CALL RECEIVED");
59 response->set_rss(before_server_create);
60 auto* reactor = context->DefaultReactor();
61 reactor->Finish(grpc::Status::OK);
62 return reactor;
63 }
64
65 private:
66 long before_server_create;
67 };
68
69 /* We have some sort of deadlock, so let's not exit gracefully for now.
70 TODO(chennancy) Add graceful shutdown */
71 static void sigint_handler(int /*x*/) { _exit(0); }
72
73 ABSL_FLAG(std::string, bind, "", "Bind host:port");
74 ABSL_FLAG(bool, secure, false, "Use SSL Credentials");
75
76 int main(int argc, char** argv) {
77 absl::ParseCommandLine(argc, argv);
78 char* fake_argv[1];
79 GPR_ASSERT(argc >= 1);
80 fake_argv[0] = argv[0];
81 grpc::testing::TestEnvironment env(&argc, argv);
82 grpc_init();
83 signal(SIGINT, sigint_handler);
84 std::string server_address = absl::GetFlag(FLAGS_bind);
85 if (server_address.empty()) {
86 gpr_log(GPR_ERROR, "Server: No port entered");
87 return 1;
88 }
89 gpr_log(GPR_INFO, "Server port: %s", server_address.c_str());
90
91 // Get initial process memory usage before creating server
92 long before_server_create = GetMemUsage();
93 ServerCallbackImpl callback_server(before_server_create);
94 grpc::ServerBuilder builder;
95
96 // Set the authentication mechanism.
97 std::shared_ptr<grpc::ServerCredentials> creds =
98 grpc::InsecureServerCredentials();
99 if (absl::GetFlag(FLAGS_secure)) {
100 gpr_log(GPR_INFO, "Supposed to be secure, is not yet");
101 // TODO (chennancy) Add in secure credentials
102 }
103 builder.AddListeningPort(server_address, creds);
104 builder.RegisterService(&callback_server);
105
106 // Set up the server to start accepting requests.
107 std::shared_ptr<grpc::Server> server(builder.BuildAndStart());
108 gpr_log(GPR_INFO, "Server listening on %s", server_address.c_str());
109
110 // Keep the program running until the server shuts down.
111 server->Wait();
112 return 0;
113 }