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 #include <grpc/support/log.h> 28 29 #include "src/core/lib/gpr/env.h" 30 #include "test/core/util/port.h" 31 #include "test/cpp/util/subprocess.h" 32 33 using grpc::SubProcess; 34 35 constexpr auto kNumWorkers = 2; 36 37 static SubProcess* g_driver; 38 static SubProcess* g_workers[kNumWorkers]; 39 40 template <class T> 41 std::string as_string(const T& val) { 42 std::ostringstream out; 43 out << val; 44 return out.str(); 45 } 46 47 static void sighandler(int sig) { 48 const int errno_saved = errno; 49 if (g_driver != nullptr) g_driver->Interrupt(); 50 for (int i = 0; i < kNumWorkers; ++i) { 51 if (g_workers[i]) g_workers[i]->Interrupt(); 52 } 53 errno = errno_saved; 54 } 55 56 static void register_sighandler() { 57 struct sigaction act; 58 memset(&act, 0, sizeof(act)); 59 act.sa_handler = sighandler; 60 61 sigaction(SIGINT, &act, nullptr); 62 sigaction(SIGTERM, &act, nullptr); 63 } 64 65 static void LogStatus(int status, const char* label) { 66 if (WIFEXITED(status)) { 67 gpr_log(GPR_INFO, "%s: subprocess exited with status %d", label, 68 WEXITSTATUS(status)); 69 } else if (WIFSIGNALED(status)) { 70 gpr_log(GPR_INFO, "%s: subprocess terminated with signal %d", label, 71 WTERMSIG(status)); 72 } else { 73 gpr_log(GPR_INFO, "%s: unknown subprocess status: %d", label, status); 74 } 75 } 76 77 int main(int argc, char** argv) { 78 register_sighandler(); 79 80 std::string my_bin = argv[0]; 81 std::string bin_dir = my_bin.substr(0, my_bin.rfind('/')); 82 83 std::ostringstream env; 84 bool first = true; 85 86 for (int i = 0; i < kNumWorkers; i++) { 87 const auto port = grpc_pick_unused_port_or_die(); 88 std::vector<std::string> args = {bin_dir + "/qps_worker", "-driver_port", 89 as_string(port)}; 90 g_workers[i] = new SubProcess(args); 91 if (!first) env << ","; 92 env << "localhost:" << port; 93 first = false; 94 } 95 96 gpr_setenv("QPS_WORKERS", env.str().c_str()); 97 std::vector<std::string> args = {bin_dir + "/qps_json_driver"}; 98 for (int i = 1; i < argc; i++) { 99 args.push_back(argv[i]); 100 } 101 102 g_driver = new SubProcess(args); 103 const int driver_join_status = g_driver->Join(); 104 if (driver_join_status != 0) { 105 LogStatus(driver_join_status, "driver"); 106 } 107 for (int i = 0; i < kNumWorkers; ++i) { 108 if (g_workers[i]) g_workers[i]->Interrupt(); 109 } 110 111 for (int i = 0; i < kNumWorkers; ++i) { 112 if (g_workers[i]) { 113 const int worker_status = g_workers[i]->Join(); 114 if (worker_status != 0) { 115 LogStatus(worker_status, "worker"); 116 } 117 } 118 } 119 120 if (g_driver != nullptr) { 121 delete g_driver; 122 } 123 g_driver = nullptr; 124 for (int i = 0; i < kNumWorkers; ++i) { 125 if (g_workers[i] != nullptr) { 126 delete g_workers[i]; 127 } 128 } 129 GPR_ASSERT(driver_join_status == 0); 130 } 131