json_run_localhost.cc 3.7 KB

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