xref: /aosp_15_r20/external/grpc-grpc/test/cpp/qps/json_run_localhost.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015-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 <signal.h>
20 #include <string.h>
21 
22 #include <memory>
23 #include <mutex>
24 #include <sstream>
25 #include <string>
26 
27 #ifdef __FreeBSD__
28 #include <sys/wait.h>
29 #endif
30 
31 #include <grpc/support/log.h>
32 
33 #include "src/core/lib/gprpp/crash.h"
34 #include "src/core/lib/gprpp/env.h"
35 #include "test/core/util/port.h"
36 #include "test/cpp/util/subprocess.h"
37 
38 using grpc::SubProcess;
39 
40 constexpr auto kNumWorkers = 2;
41 
42 static SubProcess* g_driver;
43 static SubProcess* g_workers[kNumWorkers];
44 
45 template <class T>
as_string(const T & val)46 std::string as_string(const T& val) {
47   std::ostringstream out;
48   out << val;
49   return out.str();
50 }
51 
sighandler(int)52 static void sighandler(int /*sig*/) {
53   const int errno_saved = errno;
54   if (g_driver != nullptr) g_driver->Interrupt();
55   for (int i = 0; i < kNumWorkers; ++i) {
56     if (g_workers[i]) g_workers[i]->Interrupt();
57   }
58   errno = errno_saved;
59 }
60 
register_sighandler()61 static void register_sighandler() {
62   struct sigaction act;
63   memset(&act, 0, sizeof(act));
64   act.sa_handler = sighandler;
65 
66   sigaction(SIGINT, &act, nullptr);
67   sigaction(SIGTERM, &act, nullptr);
68 }
69 
LogStatus(int status,const char * label)70 static void LogStatus(int status, const char* label) {
71   if (WIFEXITED(status)) {
72     gpr_log(GPR_INFO, "%s: subprocess exited with status %d", label,
73             WEXITSTATUS(status));
74   } else if (WIFSIGNALED(status)) {
75     gpr_log(GPR_INFO, "%s: subprocess terminated with signal %d", label,
76             WTERMSIG(status));
77   } else {
78     gpr_log(GPR_INFO, "%s: unknown subprocess status: %d", label, status);
79   }
80 }
81 
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83   register_sighandler();
84 
85   std::string my_bin = argv[0];
86   std::string bin_dir = my_bin.substr(0, my_bin.rfind('/'));
87 
88   std::ostringstream env;
89   bool first = true;
90 
91   for (int i = 0; i < kNumWorkers; i++) {
92     const auto driver_port = grpc_pick_unused_port_or_die();
93     // ServerPort can be used or not later depending on the type of worker
94     // but we like to issue all ports required here to avoid port conflict.
95     const auto server_port = grpc_pick_unused_port_or_die();
96     std::vector<std::string> args = {bin_dir + "/qps_worker", "-driver_port",
97                                      as_string(driver_port), "-server_port",
98                                      as_string(server_port)};
99     g_workers[i] = new SubProcess(args);
100     if (!first) env << ",";
101     env << "localhost:" << driver_port;
102     first = false;
103   }
104 
105   grpc_core::SetEnv("QPS_WORKERS", env.str().c_str());
106   std::vector<std::string> args = {bin_dir + "/qps_json_driver"};
107   for (int i = 1; i < argc; i++) {
108     args.push_back(argv[i]);
109   }
110 
111   g_driver = new SubProcess(args);
112   const int driver_join_status = g_driver->Join();
113   if (driver_join_status != 0) {
114     LogStatus(driver_join_status, "driver");
115   }
116   for (int i = 0; i < kNumWorkers; ++i) {
117     if (g_workers[i]) g_workers[i]->Interrupt();
118   }
119 
120   for (int i = 0; i < kNumWorkers; ++i) {
121     if (g_workers[i]) {
122       const int worker_status = g_workers[i]->Join();
123       if (worker_status != 0) {
124         LogStatus(worker_status, "worker");
125       }
126     }
127   }
128 
129   delete g_driver;
130 
131   g_driver = nullptr;
132   for (int i = 0; i < kNumWorkers; ++i) {
133     if (g_workers[i] != nullptr) {
134       delete g_workers[i];
135     }
136   }
137   GPR_ASSERT(driver_join_status == 0);
138 }
139