controller_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include <sys/wait.h>
  2. #include "controller_core.h"
  3. #include "my_rtrm.h"
  4. #include "libfunctions.h"
  5. #include "noc_functions.h"
  6. #include "sig_aux.h"
  7. #include "common_core.h"
  8. #include "signal_handlers.h"
  9. #include "scc_signals.h"
  10. #include "idag_defs.h"
  11. #include "structs.h"
  12. #include "variables.h"
  13. void idle_agent_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]){
  14. int i, one_core;
  15. pid_t p;
  16. core_list *tmp_cores_list;
  17. inter_list tmp_inter_list;
  18. float avg_cluster_util;
  19. int init_delay_sec;
  20. printf("I an idag with node_id = %d, pid = %d paxos_state = %s\n",node_id,getpid(),id2string(paxos_state));
  21. if (paxos_state == NEW_IDAG){
  22. fprintf(log_file,"I an idag with node_id = %d, pid = %d\n",node_id,getpid());
  23. }
  24. if (paxos_state != NEW_IDAG)
  25. {
  26. its.it_value.tv_sec = 0;
  27. its.it_interval.tv_sec = 0;//its.it_value.tv_sec;
  28. its.it_interval.tv_nsec = 0;
  29. nodes_initialised=0;
  30. index_bottom[node_id] = 0;
  31. #ifdef PLAT_LINUX
  32. for (i=(node_id * MAX_SIGNAL_LIST_LEN * LINE_SIZE); i<((node_id + 1) * MAX_SIGNAL_LIST_LEN * LINE_SIZE); i++) {
  33. sig_array[i] = NO_SIG;
  34. }
  35. //semaphore inits
  36. if (sem_init(&scc_lock[node_id], 1, 1) == -1){
  37. printf("I am %d error\n",node_id);
  38. perror("sem_init");
  39. }
  40. if (sem_init(&flag_data_written[node_id], 1, 0) == -1){
  41. printf("I am %d error\n",node_id);
  42. perror("sem_init");
  43. }
  44. #endif
  45. install_signal_handlers();
  46. #ifdef PLAT_LINUX
  47. sig_SEGV_enable();
  48. #endif
  49. sev.sigev_notify = SIGEV_SIGNAL;
  50. sev.sigev_signo = SIG_TIMER;
  51. sev.sigev_value.sival_ptr = &timerid;
  52. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  53. DDS_count=0;
  54. my_cores_count=0;
  55. DDS=NULL;
  56. my_cores=NULL;
  57. if (my_cores == NULL) {
  58. my_cores = (core_list *) malloc(sizeof(core_list));
  59. my_cores_tail = my_cores;
  60. }
  61. my_cores_count++;
  62. my_cores_tail->core_id = node_id;
  63. my_cores_tail->offered_to = -1;
  64. my_cores_tail->next = NULL;
  65. DDS = (DDS_list *) malloc(sizeof(DDS_list));
  66. DDS->agent_id = node_id;
  67. DDS->next = NULL;
  68. DDS_tail = DDS;
  69. DDS_count++;
  70. for (i = 0; i < NUES; i++)
  71. if (i != node_id && idag_mask[i] == node_id) {
  72. if (my_cores == NULL) {
  73. my_cores = (core_list *) malloc(sizeof(core_list));
  74. my_cores_tail = my_cores;
  75. } else {
  76. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  77. my_cores_tail = my_cores_tail->next;
  78. }
  79. my_cores_count++;
  80. my_cores_tail->core_id = i;
  81. my_cores_tail->offered_to = -1;
  82. my_cores_tail->next = NULL;
  83. #ifdef PLAT_LINUX
  84. /* 7.12.2015 I have to create new children only if i am initial IDAG */
  85. p = fork();
  86. if (p==0){
  87. node_id = i;
  88. common_node_actions(scen_directory, scen_num);
  89. }
  90. #endif
  91. }
  92. DDS->num_of_cores = my_cores_count;
  93. if (log_file == NULL){
  94. log_file = create_log_file(node_id, 0, scen_directory, scen_num);
  95. setbuf(log_file, NULL);
  96. }
  97. fprintf(log_file,"my pid is %d\n",getpid());
  98. cur_time = time(NULL);
  99. cur_t = localtime(&cur_time);
  100. fprintf(log_file, "[%d:%d:%d]: I initialized node_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,node_id);
  101. alive = (int *)malloc(X_max*Y_max*sizeof(int));
  102. suspected = (int *)malloc(X_max*Y_max*sizeof(int));
  103. for (i = 0; i < X_max*Y_max; i++){
  104. alive[i] = 1;
  105. suspected[i] = 0;
  106. }
  107. #ifdef PLAT_SCC
  108. RCCE_barrier(&RCCE_COMM_WORLD);
  109. #else
  110. sleep(1);
  111. #endif
  112. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next) {
  113. one_core = tmp_cores_list->core_id;
  114. if (core_inter_head[one_core] == NULL){
  115. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  116. core_inter_tail[one_core] = core_inter_head[one_core];
  117. } else {
  118. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  119. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  120. }
  121. core_inter_tail[one_core]->type = INIT_CORE;
  122. core_inter_tail[one_core]->next = NULL;
  123. //if (paxos_state != NEW_IDAG)
  124. signals_disable();
  125. scc_kill(one_core, SIG_INIT, core_inter_head[one_core]);
  126. signals_enable();
  127. }
  128. state = IDLE_IDAG;
  129. while (nodes_initialised != my_cores_count-1) {
  130. scc_pause();
  131. scc_signals_check();
  132. }
  133. #ifdef PLAT_SCC
  134. RCCE_barrier(&RCCE_COMM_WORLD);
  135. #else
  136. sleep(1);
  137. #endif
  138. #if defined(EPFD) || defined(tEPFD)
  139. sev.sigev_notify = SIGEV_SIGNAL;
  140. sev.sigev_signo = SIG_EPFD_TIMER;
  141. sev.sigev_value.sival_ptr = &epfd_timer;
  142. if (timer_create(CLOCK_REALTIME, &sev, &epfd_timer) == -1)
  143. printf("timer_create error\n");
  144. else
  145. fprintf(log_file,"I succesfully created epfd_timer\n");
  146. #endif
  147. #if defined(PFD) || defined(tPFD)
  148. sev.sigev_notify = SIGEV_SIGNAL;
  149. sev.sigev_signo = SIG_PFD_TIMER;
  150. sev.sigev_value.sival_ptr = &pfd_timer;
  151. if (timer_create(CLOCK_REALTIME, &sev, &pfd_timer) == -1)
  152. printf("timer_create error\n");
  153. else
  154. fprintf(log_file,"I succesfully created pfd_timer\n");
  155. #endif
  156. if (node_id < 10){
  157. init_delay_sec = node_id;
  158. }else if (node_id < 100){
  159. init_delay_sec = node_id%10;
  160. }else{
  161. init_delay_sec = node_id%100;
  162. }
  163. #if defined(EPFD) || defined(tEPFD)
  164. its.it_interval.tv_sec = 0;
  165. its.it_interval.tv_nsec = 0;
  166. its.it_value.tv_sec = init_delay_sec;
  167. its.it_value.tv_nsec = 0;
  168. if (timer_settime(epfd_timer, 0, &its, NULL) == -1) perror("timer_settime error9");
  169. #endif
  170. #if defined (PFD) || defined (tPFD)
  171. its.it_interval.tv_sec = 0;
  172. its.it_interval.tv_nsec = 0;
  173. its.it_value.tv_sec = init_delay_sec;
  174. its.it_value.tv_nsec = 0;
  175. if (timer_settime(pfd_timer, 0, &its, NULL) == -1) perror("timer_settime error9");
  176. #endif
  177. }
  178. if (paxos_state == NEW_IDAG){
  179. tmp_inter_list.type = RECOVERED;
  180. scc_kill(10,SIG_RECOVER,&tmp_inter_list);
  181. state = IDLE_IDAG;
  182. pending_state = NO_PENDING_STATE;
  183. paxos_state = NO_PENDING_STATE;
  184. fprintf(log_file,"state : %s\n",id2string(state));
  185. }
  186. time_t t;
  187. srand((unsigned) time(&t));
  188. #ifdef CONTROLLER
  189. /* Scenario pou peftei o controller */
  190. if (timer_schedule[node_id] != 0){
  191. timer_schedule[node_id] = 8 + rand() % 10;
  192. sev.sigev_notify = SIGEV_SIGNAL;
  193. sev.sigev_signo = SIG_CTIMER;
  194. sev.sigev_value.sival_ptr = &controller_timer;
  195. if (timer_create(CLOCK_REALTIME, &sev, &controller_timer) == -1)
  196. printf("timer_create error\n");
  197. else
  198. printf("Controller Timer created succesfully!\n");
  199. its.it_interval.tv_sec = 0;
  200. its.it_interval.tv_nsec = 0;
  201. its.it_value.tv_sec = timer_schedule[node_id];
  202. its.it_value.tv_nsec = 0;
  203. if (timer_settime(controller_timer, 0, &its, NULL) == -1)
  204. perror("controller_core.c : timer_settime error9");
  205. else
  206. printf("%d : My timer will explode in %d seconds.\n", node_id, timer_schedule[node_id]);
  207. }
  208. #endif
  209. while (state != IDAG_ENDING)
  210. if (state == IDLE_IDAG) {
  211. scc_pause();
  212. if (paxos_state == NEW_IDAG){
  213. fprintf(log_file, "I will check for signals now!\n");
  214. fflush(log_file);
  215. }
  216. scc_signals_check();
  217. } else {
  218. printf("idle_agent.c : Uknown state node_id = %d state = %s\n",node_id,id2string(state));
  219. state = IDLE_IDAG;
  220. }
  221. //inter_list tmp_inter_list;
  222. tmp_cores_list = my_cores;
  223. my_cores = my_cores->next;
  224. free(tmp_cores_list);
  225. for (; my_cores != NULL; my_cores = my_cores->next){
  226. tmp_cores_list = my_cores;
  227. one_core = my_cores->core_id;
  228. if (core_inter_head[one_core] == NULL) {
  229. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  230. core_inter_tail[one_core] = core_inter_head[one_core];
  231. } else {
  232. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  233. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  234. fprintf(log_file,"I am still doing smth with my node %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  235. }
  236. core_inter_tail[one_core]->type = TERMINATION_STATS;
  237. core_inter_tail[one_core]->next = NULL;
  238. //kill(pid_num[one_core], SIG_TERMINATE);
  239. //signals_disable();
  240. scc_kill(one_core, SIG_TERMINATE, core_inter_head[one_core]);
  241. /* 8.7.2016 Paxos Stats */
  242. tmp_inter_list.next = NULL;
  243. tmp_inter_list.type = PAXOS_STATS_REQ;
  244. scc_kill(one_core, SIG_PAXOS_STATS_REQ, &tmp_inter_list);
  245. paxos_node_stats.msg_count++;
  246. paxos_node_stats.distance += distance(node_id,one_core);
  247. //signals_enable();
  248. my_stats.msg_count++;
  249. my_stats.distance += distance(node_id,one_core);
  250. free(tmp_cores_list);
  251. }
  252. while (state == IDAG_ENDING) {
  253. scc_pause();
  254. scc_signals_check();
  255. /* 8.7.2016 Paxos stats */
  256. if (paxos_stats_replied == my_cores_count -1) {
  257. tmp_inter_list.next = NULL;
  258. tmp_inter_list.type = PAXOS_STATS_REP;
  259. paxos_total_stats.msg_count += paxos_node_stats.msg_count;
  260. paxos_total_stats.distance += paxos_node_stats.distance;
  261. tmp_inter_list.data.paxos_stats[0] = paxos_total_stats.msg_count;
  262. tmp_inter_list.data.paxos_stats[1] = paxos_total_stats.fd_msg_count;
  263. scc_kill(idag_id_arr[0], SIG_PAXOS_STATS_REP, &tmp_inter_list);
  264. paxos_stats_replied++;
  265. }
  266. if (stats_replied == my_cores_count-1 && paxos_stats_replied == my_cores_count) {
  267. //printf("I am %d and all my cores replied their stats\n",node_id);
  268. core_inter_head[0] = (inter_list *) malloc(sizeof(inter_list));
  269. core_inter_tail[0] = core_inter_head[0];
  270. core_inter_tail[0]->type = REP_STATISTICS;
  271. core_inter_tail[0]->next = NULL;
  272. total_stats.msg_count += my_stats.msg_count;
  273. total_stats.message_size += my_stats.message_size;
  274. total_stats.distance += my_stats.distance;
  275. total_stats.app_turnaround += my_stats.app_turnaround;
  276. total_stats.comp_effort += my_stats.comp_effort;
  277. total_stats.cores_utilized += my_stats.cores_utilized;
  278. total_stats.times_accessed += my_stats.times_accessed;
  279. avg_cluster_util = (float) my_stats.cores_utilized / (my_stats.times_accessed * (my_cores_count-1));
  280. printf("I am %d with cores_utilized = %d times_accessed = %d my_cores_count = %d and avg_cluster_util = %0.2f\n",
  281. node_id,my_stats.cores_utilized,my_stats.times_accessed,my_cores_count,avg_cluster_util);
  282. fprintf(log_file,"cores_utilized = %d times_accessed = %d my_cores_count = %d and avg_cluster_util = %0.2f\n",
  283. my_stats.cores_utilized,my_stats.times_accessed,my_cores_count,avg_cluster_util);
  284. core_inter_tail[0]->data.stats = total_stats;
  285. //kill(pid_num[0], SIG_TERMINATE);
  286. signals_disable();
  287. scc_kill(idag_id_arr[0], SIG_TERMINATE, core_inter_head[0]);
  288. signals_enable();
  289. stats_replied++;
  290. //my_cores_count = 0;
  291. }
  292. }
  293. #ifdef PLAT_SCC
  294. RCCE_flag_free(&flag_data_written);
  295. RCCE_free((t_vcharp) sig_array);
  296. RCCE_free((t_vcharp) data_array);
  297. #else
  298. for (i=0; i<my_cores_count-1; i++)
  299. wait(NULL);
  300. #endif
  301. cur_time = time(NULL);
  302. cur_t = localtime(&cur_time);
  303. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  304. fclose(log_file);
  305. exit(0);
  306. }