Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 1 | /* |
| 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 Quintas | 6deadf5 | 2016-11-28 18:02:37 -0800 | [diff] [blame] | 34 | #include <signal.h> |
| 35 | #include <string.h> |
| 36 | |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 37 | #include <memory> |
David Garcia Quintas | 6deadf5 | 2016-11-28 18:02:37 -0800 | [diff] [blame] | 38 | #include <mutex> |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 39 | #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 | |
| 48 | using grpc::SubProcess; |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 49 | |
David Garcia Quintas | d6936b6 | 2016-11-29 17:22:48 -0800 | [diff] [blame] | 50 | constexpr auto kNumWorkers = 2; |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 51 | |
| 52 | static SubProcess* g_driver; |
| 53 | static SubProcess* g_workers[kNumWorkers]; |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 54 | |
| 55 | template <class T> |
| 56 | std::string as_string(const T& val) { |
| 57 | std::ostringstream out; |
| 58 | out << val; |
| 59 | return out.str(); |
| 60 | } |
| 61 | |
David Garcia Quintas | 6deadf5 | 2016-11-28 18:02:37 -0800 | [diff] [blame] | 62 | static void sighandler(int sig) { |
David Garcia Quintas | d6936b6 | 2016-11-29 17:22:48 -0800 | [diff] [blame] | 63 | const int errno_saved = errno; |
David Garcia Quintas | 3c5a868 | 2016-11-30 11:26:58 -0800 | [diff] [blame^] | 64 | if (g_driver != NULL) g_driver->Interrupt(); |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 65 | for (int i = 0; i < kNumWorkers; ++i) { |
| 66 | if (g_workers[i]) g_workers[i]->Interrupt(); |
| 67 | } |
David Garcia Quintas | d6936b6 | 2016-11-29 17:22:48 -0800 | [diff] [blame] | 68 | errno = errno_saved; |
David Garcia Quintas | 6deadf5 | 2016-11-28 18:02:37 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static 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. Roth | c56d3eb | 2016-11-29 07:32:39 -0800 | [diff] [blame] | 80 | static void LogStatus(int status, const char* label) { |
Mark D. Roth | 37d5f9b | 2016-11-28 10:45:00 -0800 | [diff] [blame] | 81 | if (WIFEXITED(status)) { |
Mark D. Roth | c56d3eb | 2016-11-29 07:32:39 -0800 | [diff] [blame] | 82 | gpr_log(GPR_INFO, "%s: subprocess exited with status %d", label, |
Mark D. Roth | 37d5f9b | 2016-11-28 10:45:00 -0800 | [diff] [blame] | 83 | WEXITSTATUS(status)); |
| 84 | } else if (WIFSIGNALED(status)) { |
Mark D. Roth | c56d3eb | 2016-11-29 07:32:39 -0800 | [diff] [blame] | 85 | gpr_log(GPR_INFO, "%s: subprocess terminated with signal %d", label, |
Mark D. Roth | 37d5f9b | 2016-11-28 10:45:00 -0800 | [diff] [blame] | 86 | WTERMSIG(status)); |
| 87 | } else { |
Mark D. Roth | c56d3eb | 2016-11-29 07:32:39 -0800 | [diff] [blame] | 88 | gpr_log(GPR_INFO, "%s: unknown subprocess status: %d", label, status); |
Mark D. Roth | 37d5f9b | 2016-11-28 10:45:00 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 92 | int main(int argc, char** argv) { |
David Garcia Quintas | 6deadf5 | 2016-11-28 18:02:37 -0800 | [diff] [blame] | 93 | register_sighandler(); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 94 | |
| 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 Quintas | d6936b6 | 2016-11-29 17:22:48 -0800 | [diff] [blame] | 101 | for (int i = 0; i < kNumWorkers; i++) { |
| 102 | const auto port = grpc_pick_unused_port_or_die(); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 103 | std::vector<std::string> args = {bin_dir + "/qps_worker", "-driver_port", |
| 104 | as_string(port)}; |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 105 | g_workers[i] = new SubProcess(args); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 106 | 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 Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 116 | |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 117 | g_driver = new SubProcess(args); |
David Garcia Quintas | 6deadf5 | 2016-11-28 18:02:37 -0800 | [diff] [blame] | 118 | const int driver_join_status = g_driver->Join(); |
David Garcia Quintas | a0a3c41 | 2016-11-29 17:31:07 -0800 | [diff] [blame] | 119 | if (driver_join_status != 0) { |
| 120 | LogStatus(driver_join_status, "driver"); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 121 | } |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 122 | for (int i = 0; i < kNumWorkers; ++i) { |
| 123 | if (g_workers[i]) g_workers[i]->Interrupt(); |
David Garcia Quintas | a0a3c41 | 2016-11-29 17:31:07 -0800 | [diff] [blame] | 124 | } |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 125 | |
| 126 | for (int i = 0; i < kNumWorkers; ++i) { |
| 127 | if (g_workers[i]) { |
| 128 | const int worker_status = g_workers[i]->Join(); |
David Garcia Quintas | a0a3c41 | 2016-11-29 17:31:07 -0800 | [diff] [blame] | 129 | if (worker_status != 0) { |
| 130 | LogStatus(worker_status, "worker"); |
| 131 | } |
Mark D. Roth | 37d5f9b | 2016-11-28 10:45:00 -0800 | [diff] [blame] | 132 | } |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 133 | } |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 134 | |
| 135 | delete g_driver; |
David Garcia Quintas | 3c5a868 | 2016-11-30 11:26:58 -0800 | [diff] [blame^] | 136 | g_driver = NULL; |
David Garcia Quintas | 523a4aa | 2016-11-30 10:50:39 -0800 | [diff] [blame] | 137 | for (int i = 0; i < kNumWorkers; ++i) delete g_workers[i]; |
| 138 | GPR_ASSERT(driver_join_status == 0); |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 139 | } |