common_core.c 59 KB

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