blob: b7b2553f12d8d43607954f9479a7d5d4ea273df0 [file] [log] [blame]
Craig Tiller0bda0b32016-03-03 12:51:53 -08001/*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
David Garcia Quintas6deadf52016-11-28 18:02:37 -080034#include <signal.h>
35#include <string.h>
36
Craig Tiller0bda0b32016-03-03 12:51:53 -080037#include <memory>
David Garcia Quintas6deadf52016-11-28 18:02:37 -080038#include <mutex>
Craig Tiller0bda0b32016-03-03 12:51:53 -080039#include <sstream>
40#include <string>
41
42#include <grpc/support/log.h>
43
44#include "src/core/lib/support/env.h"
45#include "test/core/util/port.h"
46#include "test/cpp/util/subprocess.h"
47
48using grpc::SubProcess;
David Garcia Quintas523a4aa2016-11-30 10:50:39 -080049
David Garcia Quintasd6936b62016-11-29 17:22:48 -080050constexpr auto kNumWorkers = 2;
David Garcia Quintas523a4aa2016-11-30 10:50:39 -080051
52static SubProcess* g_driver;
53static SubProcess* g_workers[kNumWorkers];
Craig Tiller0bda0b32016-03-03 12:51:53 -080054
55template <class T>
56std::string as_string(const T& val) {
57 std::ostringstream out;
58 out << val;
59 return out.str();
60}
61
David Garcia Quintas6deadf52016-11-28 18:02:37 -080062static void sighandler(int sig) {
David Garcia Quintasd6936b62016-11-29 17:22:48 -080063 const int errno_saved = errno;
David Garcia Quintas3c5a8682016-11-30 11:26:58 -080064 if (g_driver != NULL) g_driver->Interrupt();
David Garcia Quintas523a4aa2016-11-30 10:50:39 -080065 for (int i = 0; i < kNumWorkers; ++i) {
66 if (g_workers[i]) g_workers[i]->Interrupt();
67 }
David Garcia Quintasd6936b62016-11-29 17:22:48 -080068 errno = errno_saved;
David Garcia Quintas6deadf52016-11-28 18:02:37 -080069}
70
71static void register_sighandler() {
72 struct sigaction act;
73 memset(&act, 0, sizeof(act));
74 act.sa_handler = sighandler;
75
76 sigaction(SIGINT, &act, NULL);
77 sigaction(SIGTERM, &act, NULL);
78}
79
Mark D. Rothc56d3eb2016-11-29 07:32:39 -080080static void LogStatus(int status, const char* label) {
Mark D. Roth37d5f9b2016-11-28 10:45:00 -080081 if (WIFEXITED(status)) {
Mark D. Rothc56d3eb2016-11-29 07:32:39 -080082 gpr_log(GPR_INFO, "%s: subprocess exited with status %d", label,
Mark D. Roth37d5f9b2016-11-28 10:45:00 -080083 WEXITSTATUS(status));
84 } else if (WIFSIGNALED(status)) {
Mark D. Rothc56d3eb2016-11-29 07:32:39 -080085 gpr_log(GPR_INFO, "%s: subprocess terminated with signal %d", label,
Mark D. Roth37d5f9b2016-11-28 10:45:00 -080086 WTERMSIG(status));
87 } else {
Mark D. Rothc56d3eb2016-11-29 07:32:39 -080088 gpr_log(GPR_INFO, "%s: unknown subprocess status: %d", label, status);
Mark D. Roth37d5f9b2016-11-28 10:45:00 -080089 }
90}
91
Craig Tiller0bda0b32016-03-03 12:51:53 -080092int main(int argc, char** argv) {
David Garcia Quintas6deadf52016-11-28 18:02:37 -080093 register_sighandler();
Craig Tiller0bda0b32016-03-03 12:51:53 -080094
95 std::string my_bin = argv[0];
96 std::string bin_dir = my_bin.substr(0, my_bin.rfind('/'));
97
98 std::ostringstream env;
99 bool first = true;
100
David Garcia Quintasd6936b62016-11-29 17:22:48 -0800101 for (int i = 0; i < kNumWorkers; i++) {
102 const auto port = grpc_pick_unused_port_or_die();
Craig Tiller0bda0b32016-03-03 12:51:53 -0800103 std::vector<std::string> args = {bin_dir + "/qps_worker", "-driver_port",
104 as_string(port)};
David Garcia Quintas523a4aa2016-11-30 10:50:39 -0800105 g_workers[i] = new SubProcess(args);
Craig Tiller0bda0b32016-03-03 12:51:53 -0800106 if (!first) env << ",";
107 env << "localhost:" << port;
108 first = false;
109 }
110
111 gpr_setenv("QPS_WORKERS", env.str().c_str());
112 std::vector<std::string> args = {bin_dir + "/qps_json_driver"};
113 for (int i = 1; i < argc; i++) {
114 args.push_back(argv[i]);
115 }
Craig Tiller0bda0b32016-03-03 12:51:53 -0800116
David Garcia Quintas523a4aa2016-11-30 10:50:39 -0800117 g_driver = new SubProcess(args);
David Garcia Quintas6deadf52016-11-28 18:02:37 -0800118 const int driver_join_status = g_driver->Join();
David Garcia Quintasa0a3c412016-11-29 17:31:07 -0800119 if (driver_join_status != 0) {
120 LogStatus(driver_join_status, "driver");
Craig Tiller0bda0b32016-03-03 12:51:53 -0800121 }
David Garcia Quintas523a4aa2016-11-30 10:50:39 -0800122 for (int i = 0; i < kNumWorkers; ++i) {
123 if (g_workers[i]) g_workers[i]->Interrupt();
David Garcia Quintasa0a3c412016-11-29 17:31:07 -0800124 }
David Garcia Quintas523a4aa2016-11-30 10:50:39 -0800125
126 for (int i = 0; i < kNumWorkers; ++i) {
127 if (g_workers[i]) {
128 const int worker_status = g_workers[i]->Join();
David Garcia Quintasa0a3c412016-11-29 17:31:07 -0800129 if (worker_status != 0) {
130 LogStatus(worker_status, "worker");
131 }
Mark D. Roth37d5f9b2016-11-28 10:45:00 -0800132 }
Craig Tiller0bda0b32016-03-03 12:51:53 -0800133 }
David Garcia Quintas523a4aa2016-11-30 10:50:39 -0800134
135 delete g_driver;
David Garcia Quintas3c5a8682016-11-30 11:26:58 -0800136 g_driver = NULL;
David Garcia Quintas523a4aa2016-11-30 10:50:39 -0800137 for (int i = 0; i < kNumWorkers; ++i) delete g_workers[i];
138 GPR_ASSERT(driver_join_status == 0);
Craig Tiller0bda0b32016-03-03 12:51:53 -0800139}