experimentQueueMain.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * experimentQueueMain.cpp
  3. * - compiles to bin/queue
  4. * - runs through a set of controls to run the orchestrator with multiple settings over time
  5. *
  6. * Stan Gurtler
  7. */
  8. #include <iostream>
  9. #include <fstream>
  10. #include <thread>
  11. #include <chrono>
  12. #include <cstring>
  13. #include <cstdlib>
  14. #include <csignal>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. const std::chrono::seconds FIVE_SECONDS(5);
  19. const char *ORCHESTRATOR = "bin/orchestrator";
  20. const char *SHUT_DOWN = "scripts/bringDownTestServers.sh";
  21. const int INPUT_BUFFER_LEN = 133;
  22. char whichTest[INPUT_BUFFER_LEN+2];
  23. int currPid = 0;
  24. bool stopSignaled = false;
  25. void exitInterruptHandler(int signum)
  26. {
  27. std::cout << "Interrupt signal received, quitting." << std::endl;
  28. kill(-currPid, SIGINT);
  29. char *argv[3];
  30. char shutdownBuffer[INPUT_BUFFER_LEN+2];
  31. strncpy(shutdownBuffer, SHUT_DOWN, INPUT_BUFFER_LEN+1);
  32. argv[0] = shutdownBuffer;
  33. argv[1] = whichTest;
  34. argv[2] = NULL;
  35. int lastPid = fork();
  36. if (currPid < 0)
  37. exit(-signum);
  38. if (currPid == 0)
  39. execv(SHUT_DOWN, argv);
  40. else
  41. waitpid(lastPid, NULL, 0);
  42. exit(signum);
  43. }
  44. void gentleInterruptHandler(int signum)
  45. {
  46. std::cout << "Upcoming stop signal received." << std::endl;
  47. std::cout << "After the current run is done, the program will quit." << std::endl;
  48. stopSignaled = true;
  49. }
  50. int main(int argc, char* argv[])
  51. {
  52. signal(SIGINT, exitInterruptHandler);
  53. signal(SIGTSTP, gentleInterruptHandler);
  54. char inputBuffer[INPUT_BUFFER_LEN+2];
  55. std::ifstream configFile("cfg/queue.cfg");
  56. while (!stopSignaled && !configFile.eof())
  57. {
  58. configFile.getline(inputBuffer, INPUT_BUFFER_LEN);
  59. if (strlen(inputBuffer) > 0)
  60. {
  61. char *helper = strtok(inputBuffer, " ");
  62. char *argv[6];
  63. char orchestratorBuffer[INPUT_BUFFER_LEN+2];
  64. strncpy(orchestratorBuffer, ORCHESTRATOR, INPUT_BUFFER_LEN+1);
  65. argv[0] = orchestratorBuffer;
  66. argv[1] = helper;
  67. strncpy(whichTest, helper, INPUT_BUFFER_LEN+1);
  68. size_t i = 2;
  69. while (helper != NULL && i < 6)
  70. {
  71. helper = strtok(NULL, " ");
  72. argv[i] = helper;
  73. i++;
  74. }
  75. argv[5] = NULL;
  76. currPid = fork();
  77. if (currPid < 0)
  78. {
  79. std::cerr << "Problem making new exec, aborting." << std::endl;
  80. return 1;
  81. }
  82. else if (currPid == 0)
  83. {
  84. execv(ORCHESTRATOR, argv);
  85. }
  86. else
  87. {
  88. int status;
  89. waitpid(currPid, &status, 0);
  90. if (WIFEXITED(status))
  91. {
  92. if (WEXITSTATUS(status) != 0)
  93. {
  94. std::cerr << "Something went wrong in previous run, aborting." << std::endl;
  95. return 1;
  96. }
  97. }
  98. // else if (WIFSIGNALED(status))
  99. // {
  100. // std::cerr << "Signal terminated previous run, aborting." << std::endl;
  101. // return 1;
  102. // }
  103. }
  104. currPid = 0;
  105. }
  106. std::this_thread::sleep_for(FIVE_SECONDS);
  107. }
  108. return 0;
  109. }