run_tests.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <pthread.h>
  6. #include <sys/wait.h>
  7. FILE *fin;
  8. FILE *fout;
  9. pthread_mutex_t read_file_lock;
  10. pthread_mutex_t write_file_lock;
  11. const int SIZE_OF_OPTIONS_STRING = 64;
  12. char *python_file = "./test_harness.py";
  13. char *delim = ",";
  14. void *runner(void *input) {
  15. int flag = 1;
  16. char options[SIZE_OF_OPTIONS_STRING];
  17. char options_copy[SIZE_OF_OPTIONS_STRING];
  18. char *saveptr = NULL;
  19. char *exec_vector[9];
  20. pid_t child;
  21. exec_vector[0] = python_file;
  22. exec_vector[8] = NULL;
  23. while (flag) {
  24. pthread_mutex_lock(&read_file_lock);
  25. if(fgets(options, SIZE_OF_OPTIONS_STRING, fin) == NULL) {
  26. pthread_mutex_unlock(&read_file_lock);
  27. flag = 0;
  28. } else {
  29. pthread_mutex_unlock(&read_file_lock);
  30. strncpy(options_copy, options, SIZE_OF_OPTIONS_STRING);
  31. exec_vector[1] = strtok_r(options_copy, delim, &saveptr);
  32. exec_vector[2] = strtok_r(NULL, delim, &saveptr);
  33. exec_vector[3] = strtok_r(NULL, delim, &saveptr);
  34. exec_vector[4] = strtok_r(NULL, delim, &saveptr);
  35. exec_vector[5] = strtok_r(NULL, delim, &saveptr);
  36. exec_vector[6] = strtok_r(NULL, delim, &saveptr);
  37. exec_vector[7] = strtok_r(NULL, delim, &saveptr);
  38. exec_vector[7][2] = 0;
  39. if ((child = fork())) {
  40. waitpid(child, NULL, 0);
  41. pthread_mutex_lock(&write_file_lock);
  42. fputs(options, fout);
  43. fflush(fout);
  44. pthread_mutex_unlock(&write_file_lock);
  45. printf("Ending simulation: %s, %s, %s, %s, %s, %s\n", exec_vector[1], exec_vector[2], exec_vector[3], exec_vector[4], exec_vector[7], exec_vector[6]);
  46. } else {
  47. printf("Running simulation: %s, %s, %s, %s, %s, %s\n", exec_vector[1], exec_vector[2], exec_vector[3], exec_vector[4], exec_vector[7], exec_vector[6]);
  48. execve(python_file, exec_vector, NULL);
  49. }
  50. }
  51. }
  52. return NULL;
  53. }
  54. int main(int argc, char *argv[]) {
  55. int num_processes = 4;
  56. pthread_t *other_threads;
  57. if (argc > 1) {
  58. num_processes = atoi(argv[1]);
  59. }
  60. printf("Removing redundant runs.\n");
  61. system("echo 'comm -23 <(sort -u test_options) <(sort -u completed_options) > sorted_options' | bash");
  62. system("sort -R sorted_options > curr_options");
  63. fin = fopen("curr_options", "r");
  64. fout = fopen("completed_options", "a");
  65. pthread_mutex_init(&read_file_lock, NULL);
  66. pthread_mutex_init(&write_file_lock, NULL);
  67. other_threads = malloc(sizeof(*other_threads) * num_processes);
  68. printf("Creating threads.\n");
  69. for (int i = 0; i < num_processes; i++) {
  70. pthread_create(other_threads + i, NULL, runner, NULL);
  71. }
  72. for (int i = 0; i < num_processes; i++) {
  73. pthread_join(other_threads[i], NULL);
  74. }
  75. printf("All threads complete.\n");
  76. free(other_threads);
  77. pthread_mutex_destroy(&write_file_lock);
  78. pthread_mutex_destroy(&read_file_lock);
  79. fclose(fout);
  80. fclose(fin);
  81. return 0;
  82. }