common_core.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. #include "common_core.h"
  2. #include "my_rtrm.h"
  3. #include "idag_defs.h" //PAXOS
  4. #include "libfunctions.h"
  5. #include "noc_functions.h"
  6. #include "sig_aux.h"
  7. #include "signal_handlers.h"
  8. #include "scc_signals.h"
  9. #include "structs.h"
  10. #include "apps.h"
  11. int worker_app_id = -1;
  12. int init_areas_num;
  13. int old_cores_cnt = 0;
  14. int active_working_cores = 0;
  15. int base_offset = -1;
  16. int init_DDS_replies;
  17. int init_DDS_idags;
  18. int selfopt_DDS_replies;
  19. int selfopt_DDS_idags;
  20. int selfopt_interval=LEAST_SELF_OPT_INTERVAL_MS;
  21. int max_cores_count;
  22. int *alive;
  23. int *suspected;
  24. long int selfopt_time_rem = -1;//-1 means it is not set
  25. float old_Speedup;
  26. float my_Speedup;
  27. app my_app;
  28. app init_app;
  29. application_states app_state;
  30. core_states pending_state = NO_PENDING_STATE;
  31. core_states paxos_state;
  32. offer_list *init_man_offers = NULL;
  33. offer_list *selfopt_man_offers = NULL;
  34. target_list *init_targets_head = NULL;
  35. target_list *init_targets_tail;
  36. target_list *selfopt_targets_head = NULL;
  37. target_list *selfopt_targets_tail;
  38. coworkers_list *coworkers;
  39. my_time_stamp init_app_times[2];
  40. my_time_stamp my_app_times[2];
  41. agent_info cur_agent;
  42. agent_info pending_agent;
  43. FILE *app_log_file;
  44. app_exec executed_app;
  45. void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]) {
  46. int i;
  47. int j;
  48. int tmp_int;
  49. int one_core;
  50. int new_agent;
  51. int min_dist;
  52. int selfopt_r;
  53. int row_reached;
  54. int per_core_rows;
  55. int rows_left;
  56. char app_log_file_name[128];
  57. offer_list *tmp_offer_list;
  58. core_list *tmp_cores_list;
  59. target_list *tmp_target_list;
  60. inter_list *tmp_inter_list, *tmp_inter_prev;
  61. if (paxos_state != NEW_AGENT) {
  62. init_app_times[0].tm_sec = 0;
  63. init_app_times[0].tm_min = 0;
  64. init_app_times[0].tm_hour = 0;
  65. init_app_times[0].tm_usec = 0;
  66. init_app_times[1].tm_sec = 0;
  67. init_app_times[1].tm_min = 0;
  68. init_app_times[1].tm_hour = 0;
  69. init_app_times[1].tm_usec = 0;
  70. my_app_times[0].tm_sec = 0;
  71. my_app_times[0].tm_min = 0;
  72. my_app_times[0].tm_hour = 0;
  73. my_app_times[0].tm_usec = 0;
  74. my_app_times[1].tm_sec = 0;
  75. my_app_times[1].tm_min = 0;
  76. my_app_times[1].tm_hour = 0;
  77. my_app_times[1].tm_usec = 0;
  78. my_cores_count=0;
  79. my_stats.msg_count=0;
  80. my_stats.fd_msg_count=0;
  81. my_stats.message_size=0;
  82. my_stats.distance=0;
  83. my_stats.app_turnaround=0;
  84. my_stats.comp_effort=0;
  85. my_stats.cores_utilized=0;
  86. my_stats.times_accessed=0;
  87. paxos_node_stats.msg_count=0;
  88. paxos_node_stats.fd_msg_count=0;
  89. paxos_node_stats.message_size=0;
  90. paxos_node_stats.distance=0;
  91. paxos_node_stats.app_turnaround=0;
  92. paxos_node_stats.comp_effort=0;
  93. paxos_node_stats.cores_utilized=0;
  94. paxos_node_stats.times_accessed=0;
  95. my_app.array_size=-1;
  96. my_app.workld=-1;
  97. my_app.num_of_cores=-1;
  98. //my_app.segment_id=-1;
  99. init_app.array_size=-1;
  100. init_app.workld=-1;
  101. init_app.num_of_cores=-1;
  102. //init_app.segment_id=-1;
  103. index_bottom[node_id] = 0;
  104. alive = (int *)malloc(X_max*Y_max*sizeof(int));
  105. suspected = (int *)malloc(X_max*Y_max*sizeof(int));
  106. for (i = 0; i < X_max*Y_max; i++){
  107. alive[i] = 1;
  108. suspected[i] = 0;
  109. }
  110. #ifdef PLAT_LINUX
  111. for (i=(node_id * MAX_SIGNAL_LIST_LEN * LINE_SIZE); i<((node_id + 1) * MAX_SIGNAL_LIST_LEN * LINE_SIZE); i++) {
  112. sig_array[i] = NO_SIG;
  113. }
  114. //semaphore inits
  115. if (sem_init(&scc_lock[node_id], 1, 1) == -1){
  116. printf("I am %d error\n",node_id);
  117. perror("sem_init");
  118. }
  119. if (sem_init(&flag_data_written[node_id], 1, 0) == -1){
  120. printf("I am %d error\n",node_id);
  121. perror("sem_init");
  122. }
  123. #endif
  124. cur_agent.my_agent = -1;
  125. cur_agent.array_size = -1;
  126. cur_agent.work_bounds[0]=0;
  127. cur_agent.work_bounds[1]=0;
  128. pending_agent.my_agent = -1;
  129. pending_agent.array_size = -1;
  130. pending_agent.work_bounds[0]=0;
  131. pending_agent.work_bounds[1]=0;
  132. my_cores=NULL;
  133. selfopt_r = 3; /* (int) (1.5 * (X_max / num_idags_x)); */
  134. install_signal_handlers();
  135. sev.sigev_notify = SIGEV_SIGNAL;
  136. sev.sigev_signo = SIG_TIMER;
  137. sev.sigev_value.sival_ptr = &timerid;
  138. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  139. its.it_value.tv_sec = 0;
  140. its.it_interval.tv_sec = 0;//its.it_value.tv_sec;
  141. its.it_interval.tv_nsec = 0;
  142. if (log_file == NULL){
  143. log_file = create_log_file(node_id, 0, scen_directory, scen_num);
  144. setbuf(log_file, NULL);
  145. }
  146. cur_time = time(NULL);
  147. cur_t = localtime(&cur_time);
  148. fprintf(log_file, "[%d:%d:%d]: Initialisation complete Selfopt R = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_r);
  149. #ifdef PLAT_SCC
  150. RCCE_barrier(&RCCE_COMM_WORLD);
  151. #else
  152. sleep(1);
  153. #endif
  154. state = IDLE_CORE;
  155. app_init(scen_directory, scen_num);
  156. /* Something must return here */
  157. fprintf(log_file, "Cache loading complete\n");
  158. my_idag = -1;
  159. while (my_idag == -1) {
  160. scc_pause();
  161. scc_signals_check();
  162. }
  163. #ifdef PLAT_SCC
  164. RCCE_barrier(&RCCE_COMM_WORLD);
  165. #else
  166. sleep(1);
  167. #endif
  168. }
  169. while (state != TERMINATED)
  170. if (state == IDLE_CORE) {
  171. pending_state = NO_PENDING_STATE;
  172. scc_pause();
  173. scc_signals_check();
  174. } else if (state == IDLE_INIT_MAN) {
  175. if (app_state == APP_TERMINATED) {
  176. state = AGENT_ENDING;
  177. pending_state = IDLE_INIT_MAN;
  178. fprintf(log_file,"I enter hell\n");
  179. }
  180. scc_pause();
  181. scc_signals_check();
  182. } else if (state == IDLE_AGENT) {
  183. if (app_state == APP_TERMINATED) state = AGENT_ENDING;
  184. scc_pause();
  185. scc_signals_check();
  186. if (app_state != APP_TERMINATED) {
  187. scc_pause();
  188. scc_signals_check();
  189. } else {
  190. selfopt_time_rem = my_gettimer();
  191. if (selfopt_time_rem > 0)
  192. my_settimer(0);
  193. selfopt_time_rem = -1;
  194. state = AGENT_ENDING;
  195. }
  196. } else if (state == IDLE_AGENT_WAITING_OFF) {
  197. //pause();
  198. scc_pause();
  199. scc_signals_check();
  200. /*if (app_state == APP_TERMINATED) {
  201. signals_disable();
  202. selfopt_time_rem = my_gettimer();
  203. //den stamataw edw thn diadikasia tou selfopt gia na mhn meinoun oi mexri twra prosfores kai oxi mono anapanthtes
  204. if (selfopt_time_rem == 0) {//state = AGENT_SELF_CHK_OFFERS;
  205. fprintf(log_file,"Timer is zero\n");
  206. fflush(log_file);
  207. if (selfopt_man_offers == NULL)
  208. state = AGENT_ENDING;
  209. else state = AGENT_SELF_CHK_OFFERS;
  210. }
  211. signals_enable();
  212. }*/
  213. } else if (state == INIT_MANAGER) {
  214. signals_disable();
  215. //printf("Initialising app node_id = %d\n",node_id);
  216. cur_time = time(NULL);
  217. cur_t = localtime(&cur_time);
  218. fprintf(log_file, "[%d:%d:%d]: Initialising app\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  219. state = INIT_MANAGER_SEND_OFFERS;
  220. signals_enable();
  221. scc_signals_check();
  222. } else if (state == INIT_MANAGER_SEND_OFFERS) {
  223. signals_disable();
  224. init_DDS_idags = 0;
  225. init_DDS_replies = 0;
  226. if (init_targets_head != NULL)
  227. while (init_targets_head != NULL){
  228. tmp_target_list = init_targets_head;
  229. init_targets_head = init_targets_head->next;
  230. free(tmp_target_list);
  231. }
  232. if (init_man_offers != NULL)
  233. while (init_man_offers != NULL) {
  234. tmp_offer_list = init_man_offers;
  235. init_man_offers = init_man_offers->next;
  236. free(tmp_offer_list);
  237. }
  238. fprintf(log_file, "near init area (%d,%d)\n",node_id,selfopt_r);
  239. if (core_inter_head[my_idag] == NULL){
  240. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  241. core_inter_tail[my_idag] = core_inter_head[my_idag];
  242. } else {
  243. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  244. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  245. }
  246. core_inter_tail[my_idag]->type = IDAG_FIND_IDAGS_PENDING;
  247. core_inter_tail[my_idag]->data.reg.C = node_id-1;
  248. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  249. core_inter_tail[my_idag]->next = NULL;
  250. if (core_inter_head[my_idag]->next == NULL) {
  251. //kill(pid_num[my_idag],SIG_IDAG_FIND_IDAGS);
  252. scc_kill(my_idag, SIG_IDAG_FIND_IDAGS, core_inter_head[my_idag]);
  253. my_stats.msg_count++;
  254. my_stats.distance += distance(node_id,my_idag);
  255. } else {
  256. fprintf(log_file, "common_core.c: Did not send idag_find_idags with interaction %s no2 %s\n",inter2string(core_inter_head[my_idag]->type),inter2string(core_inter_head[my_idag]->next->type));
  257. }
  258. if (selfopt_time_rem != -1) {
  259. selfopt_time_rem = my_gettimer();
  260. if (selfopt_time_rem > 0)
  261. my_settimer(0);
  262. }
  263. if (pending_state == WORKING_NODE) {
  264. fprintf(log_file, "I change to working idle init\n");
  265. state = WORKING_NODE;
  266. pending_state = IDLE_INIT_MAN;
  267. } else
  268. state = IDLE_INIT_MAN;
  269. signals_enable();
  270. scc_signals_check();
  271. } else if (state == INIT_MAN_CHK_OFFERS) {
  272. signals_disable();
  273. cur_time = time(NULL);
  274. cur_t = localtime(&cur_time);
  275. fprintf(log_file, "[%d:%d:%d]: init check alarm went off init_DDS_idags = %d init_DDS_replies = %d state=%s pending=%s\n",
  276. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,init_DDS_idags,init_DDS_replies,id2string(state),id2string(pending_state));
  277. //init_core_found = 0;
  278. if (init_man_offers == NULL) {
  279. //printf("I don't have offers\n");
  280. fprintf(log_file, "I don't have init man offers\n");
  281. //printf("I am %d and didn't manage to find my core in this round of search for app with id %d\n",node_id,init_app.id);
  282. /*if (pending_state == AGENT_ZOMBIE) {//|| app_state == APP_TERMINATED) {
  283. pending_state = INIT_MANAGER_SEND_OFFERS;
  284. state = AGENT_ZOMBIE;
  285. } else */
  286. if (app_state == APP_TERMINATED) {
  287. pending_state = INIT_MANAGER_SEND_OFFERS;
  288. state = AGENT_ENDING;
  289. } else state = INIT_MANAGER_SEND_OFFERS;
  290. //state = INIT_MANAGER_SEND_OFFERS;
  291. } else {
  292. tmp_offer_list = init_man_offers;
  293. while (tmp_offer_list != NULL){
  294. fprintf(log_file,"Offer by %d for %d cores with spd_loss %0.4f :",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores,tmp_offer_list->off.spd_loss);
  295. for (i=0; i<tmp_offer_list->off.num_of_cores; i++)
  296. fprintf(log_file," %d,",tmp_offer_list->off.offered_cores[i]);
  297. fprintf(log_file,"\n");
  298. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  299. tmp_offer_list = tmp_offer_list->next;
  300. }
  301. *init_man_offers->answer = 1;
  302. if (low_voltage_core[init_man_offers->off.offered_cores[0]]) {
  303. new_agent = init_man_offers->off.offered_cores[0];
  304. printf("I am %d and explicitly assigned %d as the new agent!\n",node_id,new_agent);
  305. fprintf(log_file,"I explicitly assigned %d as the new agent!\n",new_agent);
  306. } else {
  307. //decide which will be the agent. Will be the one tha minimizes distance between cores
  308. min_dist = -1;
  309. for (i=0; i<init_man_offers->off.num_of_cores; i++){
  310. tmp_int = 0;
  311. for (j=0; j<init_man_offers->off.num_of_cores; j++){
  312. if (i == j) continue;
  313. tmp_int += distance(init_man_offers->off.offered_cores[i],init_man_offers->off.offered_cores[j]);
  314. }
  315. if (min_dist == -1 || tmp_int < min_dist) {
  316. min_dist = tmp_int;
  317. new_agent = init_man_offers->off.offered_cores[i];
  318. }
  319. }
  320. }
  321. gettimeofday(&time_val, NULL);
  322. cur_t = localtime(&time_val.tv_sec);
  323. init_app_times[1].tm_sec = cur_t->tm_sec;
  324. init_app_times[1].tm_min = cur_t->tm_min;
  325. init_app_times[1].tm_hour = cur_t->tm_hour;
  326. init_app_times[1].tm_usec = time_val.tv_usec;
  327. //printf("New agent is %d\n",new_agent);
  328. cur_time = time(NULL);
  329. cur_t = localtime(&cur_time);
  330. fprintf(log_file, "[%d:%d:%d]: New agent is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,new_agent);
  331. printf("[%d:%d:%d]: New agent is %d for app %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,new_agent,init_app.id);
  332. #ifdef MANAGER
  333. /*if (init_app.id == 5){
  334. scc_kill(new_agent, SIG_FAIL, NULL);
  335. }*/
  336. #endif
  337. if (new_agent != node_id) {
  338. if (core_inter_head[new_agent] == NULL){
  339. core_inter_head[new_agent] = (inter_list *) malloc(sizeof(inter_list));
  340. core_inter_tail[new_agent] = core_inter_head[new_agent];
  341. } else {
  342. printf("Starting this app is not high priority. Should we change that?\n");
  343. core_inter_tail[new_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  344. core_inter_tail[new_agent] = core_inter_tail[new_agent]->next;
  345. }
  346. /* in old rtrm
  347. core_inter_tail[new_agent]->type = INIT_AGENT;
  348. core_inter_tail[new_agent]->data.app_cores = (int *)malloc((init_man_offers->off.num_of_cores+1)*sizeof(int));
  349. core_inter_tail[new_agent]->data.app_cores[0] = init_man_offers->off.num_of_cores;
  350. for(i=1; i<=init_man_offers->off.num_of_cores; i++)
  351. core_inter_tail[new_agent]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
  352. core_inter_tail[new_agent]->next = NULL;
  353. init_app.num_of_cores = init_man_offers->off.num_of_cores;*/
  354. core_inter_tail[new_agent]->type = INIT_AGENT;
  355. core_inter_tail[new_agent]->data.one_app.new_app = init_app;
  356. core_inter_tail[new_agent]->data.one_app.new_app.num_of_cores = init_man_offers->off.num_of_cores;
  357. core_inter_tail[new_agent]->data.one_app.new_app_times[0].tm_sec = init_app_times[0].tm_sec;
  358. core_inter_tail[new_agent]->data.one_app.new_app_times[0].tm_min = init_app_times[0].tm_min;
  359. core_inter_tail[new_agent]->data.one_app.new_app_times[0].tm_hour = init_app_times[0].tm_hour;
  360. core_inter_tail[new_agent]->data.one_app.new_app_times[0].tm_usec = init_app_times[0].tm_usec;
  361. core_inter_tail[new_agent]->data.one_app.new_app_times[1].tm_sec = init_app_times[1].tm_sec;
  362. core_inter_tail[new_agent]->data.one_app.new_app_times[1].tm_min = init_app_times[1].tm_min;
  363. core_inter_tail[new_agent]->data.one_app.new_app_times[1].tm_hour = init_app_times[1].tm_hour;
  364. core_inter_tail[new_agent]->data.one_app.new_app_times[1].tm_usec = init_app_times[1].tm_usec;
  365. core_inter_tail[new_agent]->data.one_app.new_app_cores = (int *)malloc((init_man_offers->off.num_of_cores)*sizeof(int));
  366. for(i=0; i<init_man_offers->off.num_of_cores; i++)
  367. core_inter_tail[new_agent]->data.one_app.new_app_cores[i] = init_man_offers->off.offered_cores[i];
  368. core_inter_tail[new_agent]->next = NULL;
  369. init_app.num_of_cores = init_man_offers->off.num_of_cores;
  370. if (core_inter_head[new_agent]->next == NULL) {
  371. //kill(pid_num[new_agent], SIG_INIT_AGENT);
  372. scc_kill(new_agent, SIG_INIT_AGENT, core_inter_head[new_agent]);
  373. my_stats.msg_count++;
  374. my_stats.distance += distance(node_id,new_agent);
  375. } else {
  376. fprintf(log_file,"I am init manager and i am doing smth else with init agent interaction=%d\n",core_inter_head[new_agent]->type);
  377. }
  378. } else {
  379. my_app = init_app;
  380. my_cores_count = init_man_offers->off.num_of_cores;
  381. my_app.num_of_cores = my_cores_count;//+1;
  382. if (my_cores == NULL) {
  383. my_cores = (core_list *) malloc(sizeof(core_list));
  384. my_cores_tail = my_cores;
  385. } else {
  386. printf("My cores still not fucking null!!\n");
  387. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  388. my_cores_tail = my_cores_tail->next;
  389. }
  390. my_cores_tail->core_id = node_id;
  391. my_cores_tail->offered_to = -1;
  392. my_cores_tail->workload[0] = -1;
  393. my_cores_tail->workload[1] = -1;
  394. my_cores_tail->next = NULL;
  395. if (core_inter_head[my_idag] == NULL){
  396. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  397. core_inter_tail[my_idag] = core_inter_head[my_idag];
  398. } else {
  399. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  400. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  401. }
  402. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  403. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  404. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  405. core_inter_tail[my_idag]->next = NULL;
  406. //I want myself to be first in my_cores list
  407. for(i=0; i<init_man_offers->off.num_of_cores; i++) {
  408. one_core = init_man_offers->off.offered_cores[i];
  409. core_inter_tail[my_idag]->data.app_cores[i+1] = one_core;
  410. if (one_core != node_id){
  411. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  412. my_cores_tail = my_cores_tail->next;
  413. my_cores_tail->core_id = one_core;
  414. my_cores_tail->offered_to = -1;
  415. my_cores_tail->next = NULL;
  416. }
  417. }
  418. my_app_times[0].tm_sec = init_app_times[0].tm_sec;
  419. my_app_times[0].tm_min = init_app_times[0].tm_min;
  420. my_app_times[0].tm_hour = init_app_times[0].tm_hour;
  421. my_app_times[0].tm_usec = init_app_times[0].tm_usec;
  422. my_app_times[1].tm_sec = init_app_times[1].tm_sec;
  423. my_app_times[1].tm_min = init_app_times[1].tm_min;
  424. my_app_times[1].tm_hour = init_app_times[1].tm_hour;
  425. my_app_times[1].tm_usec = init_app_times[1].tm_usec;
  426. init_app_times[0].tm_sec = 0;
  427. init_app_times[0].tm_min = 0;
  428. init_app_times[0].tm_hour = 0;
  429. init_app_times[0].tm_usec = 0;
  430. init_app_times[1].tm_sec = 0;
  431. init_app_times[1].tm_min = 0;
  432. init_app_times[1].tm_hour = 0;
  433. init_app_times[1].tm_usec = 0;
  434. //printf("ASDASD %d\n",idag_id_arr[0]);
  435. //kill(pid_num[0], SIG_INIT_APP);
  436. scc_kill(idag_id_arr[0], SIG_INIT_APP, NULL);
  437. my_stats.msg_count++;
  438. my_stats.distance += distance(node_id,idag_id_arr[0]);
  439. //printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  440. // ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  441. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  442. if (core_inter_head[my_idag]->next == NULL) {
  443. //kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  444. scc_kill(my_idag, SIG_ADD_CORES_DDS, core_inter_head[my_idag]);
  445. my_stats.msg_count++;
  446. my_stats.distance += distance(node_id,my_idag);
  447. } else {
  448. fprintf(log_file,"I didn't call add!! with interaction %d\n",core_inter_head[my_idag]->type);
  449. }
  450. /*if (cur_agent.my_agent != -1) {
  451. //printf("I am %d and i do this agent switch in common node with pending_state=%d\n",node_id,pending_state);
  452. fprintf(log_file,"I do this agent switch in common node with pending_state=%d\n",pending_state);
  453. }*/
  454. //state = AGENT_INIT_STATE;
  455. cur_time = time(NULL);
  456. cur_t = localtime(&cur_time);
  457. fprintf(log_file, "[%d:%d:%d]: Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count,my_app.id);
  458. fprintf(log_file, "my cores are:");
  459. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) fprintf(log_file, " %d", tmp_cores_list->core_id);
  460. fprintf(log_file, "\n");
  461. }
  462. tmp_offer_list = init_man_offers->next;
  463. while (tmp_offer_list != NULL){
  464. *tmp_offer_list->answer = 0;
  465. tmp_offer_list = tmp_offer_list->next;
  466. }
  467. while (init_man_offers != NULL) {
  468. if (core_inter_head[init_man_offers->sender] != NULL) {
  469. if (core_inter_head[init_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  470. core_inter_head[init_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  471. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  472. scc_kill(init_man_offers->sender, SIG_REP_OFFERS, core_inter_head[init_man_offers->sender]);
  473. my_stats.msg_count++;
  474. my_stats.distance += distance(node_id,init_man_offers->sender);
  475. } else if (core_inter_head[init_man_offers->sender]->type != REP_AGENT_OFFER_SENT)
  476. printf("We have aasdfa problem!\n");
  477. } else {
  478. printf("gamietai b = %d",init_man_offers->sender);
  479. fprintf(log_file,"gamietai b = %d",init_man_offers->sender);
  480. }
  481. tmp_offer_list = init_man_offers;
  482. init_man_offers = init_man_offers->next;
  483. free(tmp_offer_list);
  484. }
  485. fprintf(log_file,"selfopt_time_rem = %ld app_state = %d state = %d pending_state = %d\n",selfopt_time_rem,app_state,state,pending_state);
  486. if (new_agent == node_id)
  487. state = AGENT_INIT_STATE;
  488. else if (app_state != APP_TERMINATED) {
  489. if (selfopt_time_rem != -1) { //a selfopt is pending !app_terminated
  490. selfopt_time_rem -= INIT_NODE_INTERVAL;
  491. if (selfopt_time_rem > 0){
  492. my_settimer(selfopt_time_rem);
  493. state = IDLE_AGENT;
  494. } else {
  495. selfopt_time_rem = -1;
  496. state = AGENT_SELF_OPT;
  497. }
  498. } else if (pending_state == IDLE_AGENT) {
  499. if (my_cores_count == 1) {
  500. //printf("I am %d and i have to rewind my selfopt_areas_file after init for app with id %d\n",node_id,init_app.id);
  501. fprintf(log_file,"I have to restart optimization process after init for app with id %d\n",init_app.id);
  502. fprintf(app_log_file,"I have to restart optimization process after init\n");
  503. selfopt_interval = LEAST_SELF_OPT_INTERVAL_MS;
  504. state = AGENT_SELF_OPT;
  505. } else state = IDLE_AGENT;
  506. pending_state = NO_PENDING_STATE;
  507. } else if (pending_state == AGENT_SELF_OPT || pending_state == WORKING_NODE || pending_state == AGENT_ZOMBIE) {// || pending_state == AGENT_ENDING) {
  508. state = pending_state;//IDLE_AGENT;
  509. pending_state = NO_PENDING_STATE;
  510. } else state = IDLE_CORE;
  511. } else {
  512. selfopt_time_rem = -1;
  513. state = AGENT_ENDING;
  514. }
  515. fprintf(log_file,"state = %d pending = %d\n",state,pending_state);
  516. }
  517. signals_enable();
  518. scc_signals_check();
  519. } else if (state == AGENT_INIT_STATE) {//|| state == AGENT_INIT_STATE_INIT_INTERRUPTED
  520. signals_disable();
  521. /* Scenario pou peftei o manager */
  522. printf(KMAG "APP TO FAIL = %d\n" KNRM, manager_to_fail);
  523. if (paxos_state != NEW_AGENT && my_app.id == manager_to_fail){
  524. #if defined(BASIC_PAXOS) && defined(MANAGER)
  525. sev.sigev_notify = SIGEV_SIGNAL;
  526. sev.sigev_signo = SIG_CTIMER;
  527. sev.sigev_value.sival_ptr = &controller_timer;
  528. if (timer_create(CLOCK_REALTIME, &sev, &controller_timer) == -1)
  529. printf("timer_create error\n");
  530. else
  531. printf("Manager Timer created succesfully!\n");
  532. its.it_interval.tv_sec = 0;
  533. its.it_interval.tv_nsec = 0;
  534. its.it_value.tv_sec = 1;
  535. its.it_value.tv_nsec = 0;
  536. if (timer_settime(controller_timer, 0, &its, NULL) == -1)
  537. perror("controller_core.c : timer_settime error9");
  538. else
  539. printf(KMAG "%d : My timer will explode in %d seconds.\n" KNRM, node_id, 1);
  540. #endif
  541. }
  542. selfopt_interval = LEAST_SELF_OPT_INTERVAL_MS;
  543. nodes_ended_cnt = 0;
  544. app_state = RUNNING;
  545. max_cores_count = get_max_cores_count(my_app);
  546. active_working_cores = 0;
  547. /* Open Application Log File */
  548. #ifdef PLAT_SCC
  549. strcpy(app_log_file_name, "/shared/herc/");
  550. #else
  551. strcpy(app_log_file_name, "../");
  552. #endif
  553. strcat(app_log_file_name,scen_directory);
  554. strcat(app_log_file_name, "/");
  555. strcat(app_log_file_name,scen_num);
  556. strcat(app_log_file_name,"/app_logs/");
  557. strcat(app_log_file_name, itoa(my_app.id));
  558. strcat(app_log_file_name, ".txt");
  559. printf("app_log_file_name %s\n",app_log_file_name);
  560. if (paxos_state == NEW_AGENT){
  561. if ((app_log_file = fopen(app_log_file_name, "a")) == NULL){
  562. printf("Cannot open input file with file path = %s ",app_log_file_name);
  563. perror("open app_log_file");
  564. }
  565. }else{
  566. if ((app_log_file = fopen(app_log_file_name, "w")) == NULL){
  567. printf("Cannot open input file with file path = %s ",app_log_file_name);
  568. perror("open app_log_file");
  569. }else{
  570. setbuf(app_log_file, NULL);
  571. }
  572. }
  573. fprintf(app_log_file, "Came into init_agent at [%d:%d:%d:%ld]\n",my_app_times[0].tm_hour,my_app_times[0].tm_min,my_app_times[0].tm_sec,my_app_times[0].tm_usec);
  574. fprintf(app_log_file, "New agent found at [%d:%d:%d:%ld]\n",my_app_times[1].tm_hour,my_app_times[1].tm_min,my_app_times[1].tm_sec,my_app_times[1].tm_usec);
  575. gettimeofday(&time_val, NULL);
  576. cur_t = localtime(&time_val.tv_sec);
  577. fprintf(app_log_file, "[%d:%d:%d:%ld]: I am agent %d . Init ok!! my_cores_count = %d array_size = %d workld=%d\n",
  578. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec,node_id,my_cores_count,my_app.array_size,my_app.workld);
  579. active_working_cores = my_cores_count - 1;
  580. if (my_cores_count > 1) {
  581. row_reached = 0;
  582. per_core_rows = my_app.array_size / (my_cores_count-1);
  583. rows_left = my_app.array_size % (my_cores_count-1);
  584. }
  585. fprintf(app_log_file, "my cores are:");
  586. /*for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  587. fprintf(app_log_file, "\n");
  588. fprintf(app_log_file,"speedup = %0.2f time_to_work = %d time_per_node = %d, time_left = %d, workld=%0.2f\n",my_Speedup,time_to_work,time_per_node,time_left,my_app.workld);*/
  589. tmp_cores_list = my_cores->next;
  590. while (tmp_cores_list != NULL) {
  591. one_core = tmp_cores_list->core_id;
  592. if (core_inter_head[one_core] == NULL){
  593. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  594. core_inter_tail[one_core] = core_inter_head[one_core];
  595. } else {
  596. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  597. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  598. }
  599. core_inter_tail[one_core]->type = INIT_WORK_NODE;
  600. core_inter_tail[one_core]->data.work_bounds[0] = row_reached; /* 27.6.2016 Added by dimos. If worker fails i have to know the workload given in order to reappoint */
  601. tmp_cores_list->workload[0] = row_reached;
  602. row_reached += per_core_rows;
  603. if (rows_left > 0) {
  604. row_reached++;
  605. rows_left--;
  606. }
  607. core_inter_tail[one_core]->data.work_bounds[1] = row_reached-1;
  608. tmp_cores_list->workload[1] = row_reached-1; /* 27.6.2016 Added by dimos. If worker fails i have to know the workload given in order to reappoint */
  609. /* 28.6.2016 Scenario where worker fails */
  610. #ifdef WORKER
  611. if (worker_flag == 0){
  612. scc_kill(one_core, SIG_FAIL, NULL);
  613. worker_flag = 1;
  614. }
  615. #endif
  616. fprintf(app_log_file,"%d (%d, %d), ",one_core,core_inter_tail[one_core]->data.work_bounds[0],core_inter_tail[one_core]->data.work_bounds[1]);
  617. core_inter_tail[one_core]->next = NULL;
  618. if (core_inter_head[one_core]->next == NULL) {
  619. scc_kill(one_core, SIG_APPOINT_WORK, core_inter_head[one_core]);
  620. my_stats.msg_count++;
  621. my_stats.distance += distance(node_id,one_core);
  622. } else {
  623. fprintf(log_file,"I am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",
  624. one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  625. }
  626. tmp_cores_list = tmp_cores_list->next;
  627. } //else printf("i have only one fucking core\n");
  628. fprintf(app_log_file, "\n");
  629. if (my_cores_count == max_cores_count) {
  630. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process ini init_agent.\n");
  631. selfopt_time_rem = -1;
  632. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  633. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE) {
  634. state = pending_state;
  635. pending_state = IDLE_AGENT;
  636. } else state = IDLE_AGENT;
  637. } else if (my_cores_count > 1 && get_times(my_app, my_cores_count) <= INIT_NODE_INTERVAL) {
  638. fprintf(app_log_file, "I have little working time left %d selfopt_interval=%d in init_agent.\n",
  639. get_times(my_app, my_cores_count),selfopt_interval);
  640. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  641. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE) {
  642. state = pending_state;
  643. pending_state = IDLE_AGENT;
  644. } else state = IDLE_AGENT;
  645. selfopt_time_rem = -1;
  646. } else if (pending_state == INIT_MANAGER) {
  647. if (my_cores_count == 1)
  648. state = AGENT_SELF_OPT;
  649. else {
  650. state = INIT_MANAGER;
  651. pending_state = AGENT_SELF_OPT;
  652. }
  653. } else {
  654. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER_SEND_OFFERS || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE) {
  655. state = pending_state;
  656. pending_state = AGENT_SELF_OPT;
  657. //printf("I am %d o fountas einai poustara pou zwgrafizei k ton fuckaroun oi gkomenes tou!\n",node_id);
  658. } else state = AGENT_SELF_OPT;
  659. }
  660. /*
  661. if (state != WORKING_NODE && cur_agent.my_agent != -1) {
  662. fprintf(log_file,"I abandon my old agent %d in agent_init_state\n",cur_agent.my_agent);
  663. base_offset = -1;
  664. cur_agent.my_agent = -1;
  665. cur_agent.array_size = -1;
  666. cur_agent.work_bounds[0] = 0;
  667. cur_agent.work_bounds[1] = 0;
  668. //cur_agent.segment_id = -1;
  669. }*/
  670. gettimeofday(&time_val, NULL);
  671. cur_t = localtime(&time_val.tv_sec);
  672. fprintf(app_log_file, "[%d:%d:%d:%ld] Agent init ok\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec);
  673. signals_enable();
  674. scc_signals_check();
  675. } else if (state == AGENT_SELF_OPT) {
  676. signals_disable();
  677. //printf("Initialising self opt node_id = %d\n",node_id);
  678. cur_time = time(NULL);
  679. cur_t = localtime(&cur_time);
  680. fprintf(log_file, "[%d:%d:%d]: Initialising self opt %s!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec, id2string(state));
  681. fprintf(app_log_file, "[%d:%d:%d]: Initialising self opt!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  682. //pending_state = NO_PENDING_STATE;
  683. if (app_state != APP_TERMINATED) {
  684. selfopt_DDS_idags = 0;
  685. selfopt_DDS_replies = 0;
  686. //selfopt_areas_sent = 0;
  687. //if (selfopt_man_offers != NULL) printf("selfopt offers list is not NULL\n");
  688. fprintf(log_file,"asd1\n");
  689. if (selfopt_targets_head != NULL) {
  690. //printf("selfopt targets list is not NULL\n");
  691. while (selfopt_targets_head != NULL){
  692. tmp_target_list = selfopt_targets_head;
  693. selfopt_targets_head = selfopt_targets_head->next;
  694. free(tmp_target_list);
  695. }
  696. }
  697. fprintf(log_file,"asd2\n");
  698. if (core_inter_head[my_idag] == NULL){
  699. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  700. core_inter_tail[my_idag] = core_inter_head[my_idag];
  701. } else {
  702. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  703. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  704. }
  705. core_inter_tail[my_idag]->type = SELFOPT_IDAG_FIND_IDAGS_PENDING;
  706. core_inter_tail[my_idag]->data.reg.C = node_id;
  707. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  708. core_inter_tail[my_idag]->next = NULL;
  709. if (core_inter_head[my_idag]->next == NULL) {
  710. //kill(pid_num[my_idag],SIG_IDAG_FIND_IDAGS);
  711. scc_kill(my_idag, SIG_IDAG_FIND_IDAGS, core_inter_head[my_idag]);
  712. my_stats.msg_count++;
  713. my_stats.distance += distance(node_id,my_idag);
  714. } else {
  715. fprintf(log_file,"common_core.c: Did not send sig_find_idags with inter1 = %s, inter2 = %s\n",inter2string(core_inter_head[my_idag]->type),inter2string(core_inter_head[my_idag]->next->type));
  716. }
  717. if (selfopt_interval != MAX_SELF_OPT_INTERVAL_MS) selfopt_interval = 2 * selfopt_interval;
  718. else {
  719. selfopt_interval = -1;
  720. /*fprintf(log_file,"I invalidate selfopt process here\n");
  721. fprintf(app_log_file,"I invalidate selfopt process here\n");*/
  722. }
  723. /*FIXME*/
  724. state = IDLE_AGENT_WAITING_OFF;
  725. } else state = AGENT_ENDING;
  726. signals_enable();
  727. scc_signals_check();
  728. } else if (state == AGENT_SELF_CHK_OFFERS) {
  729. signals_disable();
  730. //printf("alarm went off for self opt node_id=%d selfopt_DDS_idags = %d selfopt_DDS_replies = %d! app_state=%d\n",node_id,selfopt_DDS_idags,selfopt_DDS_replies,app_state);
  731. cur_time = time(NULL);
  732. cur_t = localtime(&cur_time);
  733. fprintf(log_file, "[%d:%d:%d]: alarm went off for self opt selfopt_DDS_idags = %d selfopt_DDS_replies = %d app_state=%d pending_state = %d old_cores_cnt=%d\n",
  734. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state,old_cores_cnt);
  735. fprintf(app_log_file, "[%d:%d:%d]: alarm went off for self opt selfopt_DDS_idags = %d selfopt_DDS_replies = %d app_state=%d pending_state = %d old_cores_cnt=%d\n",
  736. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state,old_cores_cnt);
  737. //old_cores_cnt = my_cores_count;
  738. if (selfopt_man_offers == NULL) {
  739. //printf("I don't have self opt offers app_state=%d\n",app_state);
  740. fprintf(log_file,"I don't have self opt offers app_state=%d\n",app_state);
  741. fprintf(app_log_file,"I don't have self opt offers app_state=%d\n",app_state);
  742. //state = IDLE_AGENT;
  743. } else {
  744. tmp_offer_list = selfopt_man_offers;
  745. while (tmp_offer_list != NULL){
  746. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  747. fprintf(log_file,"Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  748. tmp_offer_list = tmp_offer_list->next;
  749. }
  750. if (app_state != APP_TERMINATED) {
  751. *selfopt_man_offers->answer = 1;
  752. if (core_inter_head[my_idag] == NULL) {
  753. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  754. core_inter_tail[my_idag] = core_inter_head[my_idag];
  755. } else {
  756. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  757. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  758. }
  759. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  760. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((selfopt_man_offers->off.num_of_cores+1)*sizeof(int));
  761. core_inter_tail[my_idag]->data.app_cores[0] = selfopt_man_offers->off.num_of_cores;
  762. core_inter_tail[my_idag]->next = NULL;
  763. if (app_state != RESIZING) {
  764. old_Speedup = my_Speedup;
  765. old_cores_cnt = my_cores_count;
  766. app_state = RESIZING;
  767. my_cores_count += selfopt_man_offers->off.num_of_cores;
  768. my_app.num_of_cores = my_cores_count;
  769. my_Speedup = Speedup(my_app, my_cores_count);
  770. } else {
  771. fprintf(app_log_file,"Allready resizing\n");
  772. my_cores_count += selfopt_man_offers->off.num_of_cores;
  773. my_app.num_of_cores = my_cores_count;
  774. my_Speedup = Speedup(my_app, my_cores_count);
  775. }
  776. for (i=0; i<selfopt_man_offers->off.num_of_cores; i++){
  777. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  778. my_cores_tail = my_cores_tail->next;
  779. my_cores_tail->core_id = selfopt_man_offers->off.offered_cores[i];
  780. my_cores_tail->offered_to = -1;
  781. my_cores_tail->next = NULL;
  782. core_inter_tail[my_idag]->data.app_cores[i+1] = selfopt_man_offers->off.offered_cores[i];
  783. one_core = my_cores_tail->core_id;
  784. //printf("I am %d and i put in appoint work %d\n",node_id,one_core);
  785. if (cur_agent.my_agent == one_core) {
  786. fprintf(log_file,"I cancel my agent here\n");
  787. base_offset = -1;
  788. cur_agent.my_agent = -1;
  789. cur_agent.array_size = -1;
  790. cur_agent.work_bounds[0] = 0;
  791. cur_agent.work_bounds[1] = 0;
  792. //cur_agent.segment_id = -1;
  793. }
  794. if (core_inter_head[one_core] == NULL){
  795. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  796. core_inter_tail[one_core] = core_inter_head[one_core];
  797. } else {
  798. fprintf(log_file,"I am doing sth with my new core %d interaction is %d\n",one_core,core_inter_head[one_core]->type);
  799. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  800. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  801. }
  802. core_inter_tail[one_core]->type = INIT_WORK_NODE_PENDING;
  803. core_inter_tail[one_core]->next = NULL;
  804. }
  805. } else *selfopt_man_offers->answer = 0;
  806. tmp_offer_list = selfopt_man_offers->next;
  807. while (tmp_offer_list != NULL){
  808. *tmp_offer_list->answer = 0;
  809. tmp_offer_list = tmp_offer_list->next;
  810. }
  811. if (app_state != APP_TERMINATED) {
  812. //my idag may have offered smth
  813. if (core_inter_head[my_idag]->next == NULL) {
  814. //kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  815. scc_kill(my_idag, SIG_ADD_CORES_DDS, core_inter_head[my_idag]);
  816. my_stats.msg_count++;
  817. my_stats.distance += distance(node_id,my_idag);
  818. } else {
  819. fprintf(log_file,"I didn't call add inside selfopt with interaction %d\n",core_inter_head[my_idag]->type);
  820. }
  821. if (old_cores_cnt == 1) {
  822. //nodes_ended_cnt = 0;
  823. row_reached = 0;
  824. per_core_rows = my_app.array_size / (my_cores_count - 1);
  825. rows_left = my_app.array_size % (my_cores_count - 1);
  826. active_working_cores = my_cores_count - 1;
  827. fprintf(app_log_file, "In one old core my cores are:");
  828. tmp_cores_list = my_cores->next;
  829. while (tmp_cores_list != NULL){
  830. //printf("I am inside core listing node=%d\n",node_id);
  831. one_core = tmp_cores_list->core_id;
  832. for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  833. if (tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  834. if (tmp_inter_list != NULL) {
  835. tmp_inter_list->type = INIT_WORK_NODE;
  836. tmp_inter_list->data.work_bounds[0] = row_reached;
  837. tmp_cores_list->workload[0] = row_reached; /* 27.6.2016 Added by dimos. If worker fails i have to know the workload given in order to reappoint */
  838. row_reached += per_core_rows;
  839. if (rows_left > 0) {
  840. row_reached++;
  841. rows_left--;
  842. }
  843. tmp_inter_list->data.work_bounds[1] = row_reached-1;
  844. tmp_cores_list->workload[1] = row_reached-1; /* 27.6.2016 Added by dimos. If worker fails i have to know the workload given in order to reappoint */
  845. fprintf(app_log_file,"%d (%d, %d), ",one_core,tmp_inter_list->data.work_bounds[0],
  846. tmp_inter_list->data.work_bounds[1]);
  847. } else {
  848. printf("i am %d kai sto common_node ta hpiame one core = %d\n",node_id,one_core);
  849. fprintf(log_file,"i am %d kai sto common_node ta hpiame one core = %d\n",node_id,one_core);
  850. }
  851. if (core_inter_head[one_core]->next == NULL) {
  852. //kill(pid_num[one_core], SIG_APPOINT_WORK);
  853. scc_kill(one_core, SIG_APPOINT_WORK, core_inter_head[one_core]);
  854. my_stats.msg_count++;
  855. my_stats.distance += distance(node_id,one_core);
  856. } else {
  857. fprintf(log_file,"I am doing smth else with my working node? interaction=%d interaction2=%d\n",
  858. core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  859. }
  860. tmp_cores_list = tmp_cores_list->next;
  861. }
  862. fprintf(app_log_file, "\n");
  863. app_state = RUNNING;
  864. }
  865. }
  866. while (selfopt_man_offers != NULL) {
  867. if (core_inter_head[selfopt_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  868. core_inter_head[selfopt_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  869. //kill(pid_num[selfopt_man_offers->sender], SIG_REP_OFFERS);
  870. scc_kill(selfopt_man_offers->sender, SIG_REP_OFFERS, core_inter_head[selfopt_man_offers->sender]);
  871. my_stats.msg_count++;
  872. my_stats.distance += distance(node_id,selfopt_man_offers->sender);
  873. } else if (core_inter_head[selfopt_man_offers->sender]->type != REP_AGENT_OFFER_SENT) printf("We have zxcvzxc problem!\n");
  874. tmp_offer_list = selfopt_man_offers;
  875. selfopt_man_offers = selfopt_man_offers->next;
  876. free(tmp_offer_list);
  877. }
  878. //printf("Self opt seems to be ok cores count = %d\n",my_cores_count);
  879. cur_time = time(NULL);
  880. cur_t = localtime(&cur_time);
  881. fprintf(log_file, "[%d:%d:%d]: Self opt ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  882. fprintf(log_file, "my cores are:");
  883. fprintf(app_log_file, "[%d:%d:%d]: Self opt ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  884. fprintf(app_log_file, "my cores are:");
  885. //printf("I am %d and my cores are:",node_id);
  886. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  887. fprintf(log_file, " %d", tmp_cores_list->core_id);
  888. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  889. //printf(" %d",tmp_cores_list->core_id);
  890. }
  891. //printf("\n");
  892. fprintf(log_file, "\n");
  893. fprintf(app_log_file, "\n");
  894. }
  895. if (app_state == APP_TERMINATED) {
  896. state = AGENT_ENDING;
  897. if (pending_state == IDLE_AGENT)
  898. pending_state = NO_PENDING_STATE;
  899. else {
  900. fprintf(log_file,"In init app terminated and pending state == %d\n",pending_state);
  901. }
  902. } else {
  903. fprintf(log_file,"in chk_offers and pending_state=%d selfopt_interval = %d\n",pending_state,selfopt_interval);
  904. if (selfopt_interval > 0) { // && !app_terminated
  905. selfopt_time_rem = selfopt_interval;
  906. if (my_cores_count == max_cores_count) {
  907. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process in selfopt.\n");
  908. selfopt_time_rem = -1;
  909. } else if (my_cores_count > 1 && get_times(my_app, my_cores_count) <= selfopt_interval) {
  910. fprintf(app_log_file, "I have little working time left %d selfopt_interval=%d in selfopt.\n",
  911. get_times(my_app, my_cores_count),selfopt_interval);
  912. selfopt_time_rem = -1;
  913. } else if (my_cores_count == 1) {
  914. fprintf(app_log_file,"I have one core and i restart optimization process with id %d\n",my_app.id);
  915. selfopt_interval = LEAST_SELF_OPT_INTERVAL_MS;//200;
  916. selfopt_time_rem = selfopt_interval;
  917. //my_settimer(LEAST_SELF_OPT_INTERVAL_MS);
  918. } else my_settimer(selfopt_time_rem);
  919. } else if (my_cores_count == 1) {
  920. //printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  921. fprintf(log_file,"I restart optimization process for app with id %d\n",my_app.id);
  922. fprintf(app_log_file,"I restart optimization process for app with id %d\n",my_app.id);
  923. selfopt_interval = LEAST_SELF_OPT_INTERVAL_MS;//200;
  924. selfopt_time_rem = selfopt_interval;
  925. //my_settimer(LEAST_SELF_OPT_INTERVAL_MS);
  926. } else selfopt_time_rem = -1;
  927. if (pending_state == INIT_MANAGER && my_cores_count > 1) {
  928. state = INIT_MANAGER;
  929. pending_state = IDLE_AGENT;
  930. } else {
  931. if (selfopt_time_rem != -1)
  932. my_settimer(selfopt_time_rem);
  933. state = IDLE_AGENT;
  934. }
  935. }
  936. signals_enable();
  937. scc_signals_check();
  938. } else if (state == WORKING_NODE) {
  939. if (executed_app == MATRIX_MUL) {
  940. /* WORKLOAD EXECUTION */
  941. //printf("I enter working state -- executing MATRIX MUL\n");
  942. fprintf(log_file, "I enter working state -- executing MATRIX MUL\n");
  943. signals_disable();
  944. execute_workload(cur_agent.work_bounds[0], cur_agent.work_bounds[1]);
  945. cur_time = time(NULL);
  946. cur_t = localtime(&cur_time);
  947. fprintf(log_file, "[%d:%d:%d]: finished work agent=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,cur_agent.my_agent);
  948. scc_kill(cur_agent.my_agent, SIG_FINISH, NULL);
  949. my_stats.msg_count++;
  950. my_stats.distance += distance(node_id,cur_agent.my_agent);
  951. fprintf(log_file, "finished work pending_agent=%d pending_state=%d\n",pending_agent.my_agent,pending_state);
  952. }else if (executed_app == SVM) {
  953. //printf("I enter working state -- executing SVM\n");
  954. fprintf(log_file,"I enter working state -- executing SVM\n");
  955. execute_workload(cur_agent.work_bounds[0], cur_agent.work_bounds[1]);
  956. /*TODO I HAVE TO SEND BACK MY svm_local_sum TO BE TOTALLY CORRECT */
  957. scc_kill(cur_agent.my_agent, SIG_FINISH, NULL);
  958. my_stats.msg_count++;
  959. my_stats.distance += distance(node_id,cur_agent.my_agent);
  960. fprintf(log_file, "finished work pending_agent=%d pending_state=%d\n",pending_agent.my_agent,pending_state);
  961. }else if (executed_app == FFT) {
  962. //printf("I enter working state -- executing FFT\n");
  963. fprintf(log_file,"I enter working state -- executing FFT\n");
  964. execute_workload(cur_agent.work_bounds[0], cur_agent.work_bounds[1]);
  965. /*TODO I HAVE TO SEND BACK result TO BE TOTALLY CORRECT */
  966. scc_kill(cur_agent.my_agent, SIG_FINISH, NULL);
  967. my_stats.msg_count++;
  968. my_stats.distance += distance(node_id,cur_agent.my_agent);
  969. fprintf(log_file, "finished work pending_agent=%d pending_state=%d\n",pending_agent.my_agent,pending_state);
  970. }
  971. if (pending_agent.my_agent == -1) { //den exei ginei allagh tou agent
  972. /*if (state == WORKING_NODE) state = IDLE_CORE;
  973. else if (state == WORKING_NODE_IDLE_INIT) state = IDLE_INIT_MAN;
  974. else*/
  975. if (pending_state == WORKING_NODE) pending_state = NO_PENDING_STATE; //just finished on return
  976. else if (pending_state == IDLE_AGENT) {
  977. pending_state = NO_PENDING_STATE;
  978. state = IDLE_AGENT;
  979. fprintf(log_file, "Finished working. Pending state is IDLE_AGENT\n");
  980. } else if (pending_state == AGENT_SELF_OPT) {
  981. pending_state = NO_PENDING_STATE;
  982. state = AGENT_SELF_OPT;
  983. fprintf(log_file, "Finished working. Pending state is AGENT_SELF_OPT\n");
  984. } else if (pending_state == AGENT_INIT_STATE || pending_state == AGENT_INIT_APP_INIT || pending_state == AGENT_INIT_CHK_OFFERS || pending_state == AGENT_INIT_IDLE_INIT) {
  985. fprintf(log_file, "Finished working. Pending state is AGENT_INIT_smth = %d\n",pending_state);
  986. //state = AGENT_INIT_STATE;
  987. if (pending_state == AGENT_INIT_APP_INIT)
  988. pending_state = INIT_MANAGER;
  989. else if (pending_state == AGENT_INIT_CHK_OFFERS)
  990. pending_state = INIT_MAN_CHK_OFFERS;
  991. else if (pending_state == AGENT_INIT_IDLE_INIT)
  992. pending_state = IDLE_INIT_MAN;
  993. else
  994. pending_state = NO_PENDING_STATE;
  995. state = AGENT_INIT_STATE;
  996. /*for (i=0; i<cur_agent.array_size; i++)
  997. free(matrix[i]);
  998. free(matrix);
  999. matrix = NULL;
  1000. free(vector);
  1001. vector = NULL;*/
  1002. /* Commented out on 8.9.2014. If the sig_finish from agent is received after
  1003. * this then nodes_ended_cnt is increased for no reason, leading to bug
  1004. base_offset = -1;
  1005. cur_agent.my_agent = -1;
  1006. cur_agent.array_size = -1;
  1007. cur_agent.work_bounds[0] = 0;
  1008. cur_agent.work_bounds[1] = 0;
  1009. */
  1010. //cur_agent.segment_id = -1;
  1011. //} else if (pending_state == WORKING_NODE_IDLE_INIT) pending_state = IDLE_INIT_MAN;
  1012. } else if (pending_state == IDLE_INIT_AGENT_SELFOPT) {
  1013. state = IDLE_INIT_MAN;
  1014. pending_state = AGENT_SELF_OPT;
  1015. } else if (pending_state == IDLE_INIT_IDLE_AGENT) {
  1016. state = IDLE_INIT_MAN;
  1017. pending_state = IDLE_AGENT;
  1018. } else if (pending_state == INIT_CHK_OFFERS_SELFOPT) {
  1019. state = INIT_MAN_CHK_OFFERS;
  1020. pending_state = AGENT_SELF_OPT;
  1021. } else if (pending_state == INIT_CHK_OFFERS_IDLE_AGENT) {
  1022. state = INIT_MAN_CHK_OFFERS;
  1023. pending_state = IDLE_AGENT;
  1024. } else if (pending_state == INIT_MAN_CHK_OFFERS || pending_state == IDLE_INIT_MAN) {
  1025. state = pending_state;
  1026. pending_state = NO_PENDING_STATE;
  1027. } else if (state == WORKING_NODE) state = IDLE_CORE;
  1028. } else {
  1029. fprintf(log_file, "In pre_change pending_agent = %d\n",pending_agent.my_agent);
  1030. base_offset = -1;
  1031. cur_agent = pending_agent;
  1032. pending_agent.my_agent = -1;
  1033. pending_agent.array_size = -1;
  1034. pending_agent.work_bounds[0] = 0;
  1035. pending_agent.work_bounds[1] = 0;
  1036. //pending_agent.segment_id = -1;
  1037. }
  1038. signals_enable();
  1039. scc_signals_check();
  1040. } else if (state == AGENT_ZOMBIE) {
  1041. scc_pause();
  1042. scc_signals_check();
  1043. } else if (state == AGENT_ENDING) {
  1044. signals_disable();
  1045. cur_time = time(NULL);
  1046. cur_t = localtime(&cur_time);
  1047. fprintf(log_file, "[%d:%d:%d]: I entered agent_ending pending_state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,id2string(pending_state));
  1048. fprintf(app_log_file, "[%d:%d:%d]: I entered agent_ending pending_state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec, id2string(pending_state));
  1049. fflush(app_log_file);
  1050. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1051. fprintf(app_log_file,"Core %d is offered to %d\n",tmp_cores_list->core_id,tmp_cores_list->offered_to);
  1052. fflush(app_log_file);
  1053. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1054. if (tmp_cores_list->offered_to != -1) break;
  1055. if (tmp_cores_list != NULL) {
  1056. //state = AGENT_ZOMBIE;
  1057. if (pending_state == INIT_MANAGER || pending_state == INIT_MAN_CHK_OFFERS || pending_state == INIT_MANAGER_SEND_OFFERS) {
  1058. state = pending_state;
  1059. pending_state = NO_PENDING_STATE;
  1060. //} else if (pending_state == IDLE_INIT_MAN) {
  1061. // state = AGENT_ZOMBIE;
  1062. } else if (pending_state == NO_PENDING_STATE || pending_state == IDLE_INIT_MAN) state = AGENT_ZOMBIE;
  1063. else if (pending_state == IDLE_AGENT || pending_state == IDLE_CORE) {
  1064. pending_state = NO_PENDING_STATE;
  1065. state = AGENT_ZOMBIE;
  1066. } else {
  1067. fprintf(log_file,"I am in zombie agent_ending and pending_state = %s\n",id2string(pending_state));
  1068. state = AGENT_ZOMBIE;
  1069. }
  1070. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next) {
  1071. one_core = tmp_cores_list->core_id;
  1072. tmp_inter_prev = NULL;
  1073. tmp_inter_list = core_inter_head[one_core];
  1074. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  1075. while (tmp_inter_list != NULL)
  1076. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  1077. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
  1078. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  1079. fprintf(log_file, "Removing in zombie one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  1080. if (tmp_inter_prev == NULL) {
  1081. core_inter_head[one_core] = core_inter_head[one_core]->next;
  1082. free(tmp_inter_list);
  1083. tmp_inter_list = core_inter_head[one_core];
  1084. } else {
  1085. tmp_inter_prev->next = tmp_inter_list->next;
  1086. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  1087. free(tmp_inter_list);
  1088. tmp_inter_list = tmp_inter_prev->next;
  1089. }
  1090. } else {
  1091. tmp_inter_prev = tmp_inter_list;
  1092. tmp_inter_list = tmp_inter_list->next;
  1093. }
  1094. }
  1095. } else {
  1096. if (core_inter_head[my_idag] == NULL){
  1097. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  1098. core_inter_tail[my_idag] = core_inter_head[my_idag];
  1099. } else {
  1100. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1101. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  1102. }
  1103. core_inter_tail[my_idag]->type = REMOVE_APP;
  1104. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  1105. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  1106. core_inter_tail[my_idag]->next = NULL;
  1107. i=1;
  1108. core_inter_tail[my_idag]->data.app_cores[i++] = my_cores->core_id;
  1109. tmp_cores_list = my_cores;
  1110. my_cores=my_cores->next;
  1111. free(tmp_cores_list);
  1112. //for(my_cores=my_cores->next; my_cores!=NULL; my_cores = my_cores->next) {
  1113. //free(tmp_cores_list);
  1114. while (my_cores != NULL) {
  1115. tmp_cores_list = my_cores;
  1116. one_core = tmp_cores_list->core_id;
  1117. core_inter_tail[my_idag]->data.app_cores[i++] = one_core;
  1118. tmp_inter_prev = NULL;
  1119. tmp_inter_list = core_inter_head[one_core];
  1120. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  1121. while (tmp_inter_list != NULL)
  1122. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  1123. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
  1124. fprintf(log_file, "Removing one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  1125. if (tmp_inter_prev == NULL) {
  1126. core_inter_head[one_core] = core_inter_head[one_core]->next;
  1127. free(tmp_inter_list);
  1128. tmp_inter_list = core_inter_head[one_core];
  1129. } else {
  1130. tmp_inter_prev->next = tmp_inter_list->next;
  1131. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  1132. free(tmp_inter_list);
  1133. tmp_inter_list = tmp_inter_prev->next;
  1134. }
  1135. } else {
  1136. tmp_inter_prev = tmp_inter_list;
  1137. tmp_inter_list = tmp_inter_list->next;
  1138. }
  1139. if (core_inter_head[one_core] == NULL) {
  1140. //kill(pid_num[one_core],SIG_FINISH);
  1141. scc_kill(one_core, SIG_FINISH, core_inter_head[one_core]);
  1142. my_stats.msg_count++;
  1143. my_stats.distance += distance(node_id,one_core);
  1144. } else {
  1145. fprintf(log_file,"I still still have smth to do with my work node %d before finish which is %d\n",one_core,core_inter_head[one_core]->type);
  1146. }
  1147. my_cores=my_cores->next;
  1148. free(tmp_cores_list);
  1149. }
  1150. if (core_inter_head[my_idag]->next == NULL) {
  1151. //kill(pid_num[my_idag],SIG_FINISH);
  1152. scc_kill(my_idag, SIG_FINISH, core_inter_head[my_idag]);
  1153. my_stats.msg_count++;
  1154. my_stats.distance += distance(node_id,my_idag);
  1155. } else {
  1156. fprintf(log_file,"I am doing smth else with my idag app_remove inter1=%d inter2=%d\n",core_inter_head[my_idag]->type,core_inter_head[my_idag]->next->type);
  1157. }
  1158. //my_app.A = 0.0;
  1159. //my_app.var = 0.0;
  1160. my_app.num_of_cores = -1;
  1161. selfopt_time_rem = -1;
  1162. app_state = NO_APP;
  1163. if (cur_agent.my_agent != -1) {
  1164. fprintf(log_file,"I abandon my old agent %d in agent_finish\n",cur_agent.my_agent);
  1165. base_offset = -1;
  1166. cur_agent.my_agent = -1;
  1167. cur_agent.array_size = -1;
  1168. cur_agent.work_bounds[0] = 0;
  1169. cur_agent.work_bounds[1] = 0;
  1170. //cur_agent.segment_id = -1;
  1171. }
  1172. //printf("I am %d My app ended app_id = %d pending_state=%d\n",node_id,my_app.id,pending_state);
  1173. gettimeofday(&time_val, NULL);
  1174. cur_t = localtime(&time_val.tv_sec);
  1175. fprintf(app_log_file, "[%d:%d:%d:%ld]: App ended pending_state=%s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec,id2string(pending_state));
  1176. fclose(app_log_file);
  1177. if (pending_state == INIT_MANAGER || pending_state == IDLE_INIT_MAN || pending_state == INIT_MAN_CHK_OFFERS || pending_state == INIT_MANAGER_SEND_OFFERS) {
  1178. state = pending_state;
  1179. pending_state = NO_PENDING_STATE;
  1180. } else if (pending_state == NO_PENDING_STATE) state = IDLE_CORE;
  1181. else if (pending_state == IDLE_AGENT || pending_state == IDLE_CORE) {
  1182. pending_state = NO_PENDING_STATE;
  1183. state = IDLE_CORE;
  1184. } else {
  1185. fprintf(log_file,"I am in agent_ending and pending_state = %d\n",pending_state);
  1186. state = IDLE_CORE;
  1187. }
  1188. }
  1189. fprintf(log_file,"I arrive here my_idag = %d state = %d idag_id_arr[0]=%d\n",my_idag,state,idag_id_arr[0]);
  1190. /* masouros: changed idag_id_arr[0] with 0 because it was -1 and we got fucked up*/
  1191. if (my_idag != 0 && state != AGENT_ZOMBIE) {
  1192. /*
  1193. if (core_inter_head[0] == NULL)
  1194. scc_kill(0, SIG_APP_TERMINATED, core_inter_head[0]);
  1195. */
  1196. if (core_inter_head[0] != NULL) {
  1197. fprintf(log_file,"I sent SIG_APP_TERMINATED to 0 with interaction = %d\n",core_inter_head[0]->type);
  1198. }
  1199. scc_kill(0, SIG_APP_TERMINATED, NULL);
  1200. } else {
  1201. fprintf(log_file,"I come here in agent_ending but state = %d\n",state);
  1202. }
  1203. signals_enable();
  1204. scc_signals_check();
  1205. } else {
  1206. printf("common_core.c : Unknown state node_id = %d state = %d\n",node_id,state);
  1207. state = IDLE_CORE;
  1208. }
  1209. #ifdef PLAT_SCC
  1210. RCCE_flag_free(&flag_data_written);
  1211. RCCE_free((t_vcharp) sig_array);
  1212. RCCE_free((t_vcharp) data_array);
  1213. #endif
  1214. /*FIXME frees for LINUX */
  1215. cur_time = time(NULL);
  1216. cur_t = localtime(&cur_time);
  1217. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1218. fclose(log_file);
  1219. exit(0);
  1220. }
  1221. void my_settimer(int msec) {
  1222. int sec;
  1223. sec = msec / 1000;
  1224. msec = msec % 1000;
  1225. its.it_value.tv_sec = sec;
  1226. its.it_value.tv_nsec = msec * MS;
  1227. if (timer_settime(timerid, 0, &its, NULL) == -1)
  1228. perror("timer_settime error\n");
  1229. }
  1230. int my_gettimer(void) { //return in ms
  1231. struct itimerspec chk_timer;
  1232. int msec=0;
  1233. if (timer_gettime(timerid, &chk_timer) == -1)
  1234. perror("timer_gettime error\n");
  1235. msec = (chk_timer.it_value.tv_sec * 1000) + (chk_timer.it_value.tv_nsec / MS);
  1236. if ((chk_timer.it_value.tv_nsec % MS) >= 500000) //rounding
  1237. msec++;
  1238. return msec;
  1239. }