common_core.c 59 KB

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