experimentQueueMain.cpp 3.2 KB

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