controller_core.c 11 KB

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