common_node.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. #include "common_node.h"
  2. //extern int *pid_num;
  3. extern int num_idags, node_id ,my_idag;//, fd_log;
  4. extern FILE *log_file;
  5. extern core_states state;
  6. core_states pending_state=NO_PENDING_STATE;
  7. extern inter_list **core_inter_head,**core_inter_tail;
  8. extern app my_app;
  9. extern app init_app;
  10. extern app far_req_app;
  11. extern metrics my_stats;
  12. extern int far_req_or_sender;
  13. extern int *idag_mask, *idag_id_arr;
  14. extern int *Cl_x_max_arr, *Cl_y_max_arr;
  15. extern int DDS_count, my_cores_count;
  16. extern DDS_list *DDS, *DDS_tail;
  17. extern core_list *my_cores, *my_cores_tail;
  18. extern offer_list *init_man_offers, *selfopt_man_offers;
  19. extern offer_list *far_man_offers, *far_man_offers_tail;
  20. extern int far_list_count, far_man_agent_count;
  21. extern int my_agent, time_worked;
  22. extern int debug_global;
  23. extern time_t cur_time;
  24. extern struct tm *cur_t;
  25. extern struct sigevent sev;
  26. extern struct itimerspec its, chk_timer;
  27. extern timer_t timerid;
  28. extern long int selfopt_time_rem;//-1 means it is not set
  29. extern long int upper_work_bound;
  30. extern int time_for_farman;
  31. extern char scen_num[4];
  32. extern int chk_rem_count, chk_rem_num, sum_rem_time;
  33. extern float old_Speedup, my_Speedup;
  34. extern int nodes_ended_cnt;//, app_terminated;
  35. int my_x, my_y, init_areas_num;
  36. int init_DDS_replies, selfopt_DDS_replies, init_DDS_idags, selfopt_DDS_idags, selfopt_interval=200, init_idags_areas_replies, max_cores_count;
  37. target_list *init_targets_head=NULL, *init_targets_tail;
  38. target_list *selfopt_targets_head=NULL, *selfopt_targets_tail;
  39. FILE *app_log_file;
  40. application_states app_state;
  41. my_time_stamp init_app_times[2], my_app_times[2];
  42. extern int *sig_array, *data_array, NUES;
  43. extern RCCE_FLAG flag_signals_enabled,flag_data_written;
  44. void common_node_actions(char scen_num[4]){
  45. offer_list *tmp_offer_list;
  46. int one_core, i, old_cores_cnt;
  47. core_list *tmp_cores_list;
  48. target_list *tmp_target_list;
  49. inter_list *tmp_inter_list, *tmp_inter_prev;
  50. FILE *init_areas_file;//, *selfopt_areas_file;
  51. char init_areas_file_name[64], app_log_file_name[64];//selfopt_areas_file_name[32],
  52. int C,r,init_areas_cnt, init_areas_sent;//area_cnt,selfopt_areas=0,
  53. int time_per_node, time_left, time_to_work, init_core_found=0;
  54. int new_agent, min_dist=-1, tmp_int, j;
  55. //int new_x, new_y;//, selfopt_areas_sent;
  56. int selfopt_r;
  57. DDS_count=0;
  58. my_cores_count=0;
  59. my_stats.msg_count=0;
  60. my_stats.message_size=0;
  61. my_stats.distance=0;
  62. my_stats.app_turnaround=0;
  63. my_stats.comp_effort=0;
  64. my_stats.cores_utilized=0;
  65. my_stats.times_accessed=0;
  66. init_app_times[0].tm_sec = 0;
  67. init_app_times[0].tm_min = 0;
  68. init_app_times[0].tm_hour = 0;
  69. init_app_times[1].tm_sec = 0;
  70. init_app_times[1].tm_min = 0;
  71. init_app_times[1].tm_hour = 0;
  72. my_app_times[0].tm_sec = 0;
  73. my_app_times[0].tm_min = 0;
  74. my_app_times[0].tm_hour = 0;
  75. my_app_times[1].tm_sec = 0;
  76. my_app_times[1].tm_min = 0;
  77. my_app_times[1].tm_hour = 0;
  78. //printf("I Am %d\n",node_id);
  79. DDS=NULL;
  80. my_cores=NULL;
  81. selfopt_r = (int) (1.5 * (X_max / num_idags_x));
  82. install_signal_handlers();
  83. //sig_SEGV_enable();
  84. sev.sigev_notify = SIGEV_SIGNAL;
  85. sev.sigev_signo = SIG_TIMER;
  86. sev.sigev_value.sival_ptr = &timerid;
  87. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  88. its.it_value.tv_sec = 0;
  89. its.it_interval.tv_sec = 0;//its.it_value.tv_sec;
  90. its.it_interval.tv_nsec = 0;
  91. log_file = create_log_file(node_id,scen_num);
  92. cur_time = time(NULL);
  93. cur_t = localtime(&cur_time);
  94. fprintf(log_file, "[%d:%d:%d]: Initialisation complete\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  95. fflush(log_file);
  96. RCCE_barrier(&RCCE_COMM_WORLD);
  97. my_x = node_id % X_max;
  98. my_y = node_id / X_max;
  99. state = IDLE_CORE;
  100. RCCE_flag_write(&flag_signals_enabled, RCCE_FLAG_SET, node_id);
  101. my_idag = -1;
  102. int dummy=0;
  103. while (my_idag==-1) {//pause();
  104. for (i=0; i<1000; i++)
  105. for(j=0; j<1000; j++)
  106. dummy++;
  107. scc_signals_check();
  108. }
  109. while (state != TERMINATED)
  110. if (state == IDLE_CORE) {
  111. pending_state = NO_PENDING_STATE;
  112. //pause();
  113. dummy=0;
  114. for (i=0; i<1000; i++)
  115. for(j=0; j<1000; j++)
  116. dummy++;
  117. scc_signals_check();
  118. } else if (state == IDLE_INIT_MAN) {// || state == IDLE_FAR_MAN || state == IDLE_INIT_MAN_SELFOPT_PENDING || state == IDLE_INIT_MAN_WORK_PENDING) {
  119. //pause();
  120. dummy=0;
  121. for (i=0; i<1000; i++)
  122. for(j=0; j<1000; j++)
  123. dummy++;
  124. scc_signals_check();
  125. } else if (state == IDLE_AGENT) {
  126. if (app_state != APP_TERMINATED) {
  127. //pause();//!app_terminated
  128. dummy=0;
  129. for (i=0; i<1000; i++)
  130. for(j=0; j<1000; j++)
  131. dummy++;
  132. scc_signals_check();
  133. } else {
  134. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error 2\n");
  135. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  136. if (selfopt_time_rem > 0) {
  137. its.it_value.tv_nsec = 0;
  138. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error42\n");
  139. }
  140. state = AGENT_ENDING;
  141. }
  142. } else if (state == AGENT_REWIND_FILE) {
  143. //printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  144. fprintf(log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",init_app.id);
  145. selfopt_interval = 200;
  146. its.it_value.tv_nsec = selfopt_interval * MS;
  147. selfopt_time_rem = selfopt_interval;
  148. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error823\n");
  149. state = IDLE_AGENT;
  150. } else if (state == IDLE_AGENT_WAITING_OFF) {
  151. //pause();
  152. if (app_state != APP_TERMINATED) {
  153. //pause();//!app_terminated
  154. dummy=0;
  155. for (i=0; i<1000; i++)
  156. for(j=0; j<1000; j++)
  157. dummy++;
  158. scc_signals_check();
  159. } else {
  160. signals_disable();
  161. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error 2\n");
  162. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  163. //den stamataw edw thn diadikasia tou selfopt gia na mhn meinoun oi mexri twra prosfores kai oxi mono anapanthtes
  164. if (selfopt_time_rem == 0) state = AGENT_SELF_CHK_OFFERS;
  165. signals_enable();
  166. }
  167. } else if (state == INIT_MANAGER) {
  168. signals_disable();
  169. //printf("Initialising app node_id = %d\n",node_id);
  170. cur_time = time(NULL);
  171. cur_t = localtime(&cur_time);
  172. fprintf(log_file, "[%d:%d:%d]: Initialising app\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  173. fflush(log_file);
  174. strcpy(init_areas_file_name,"/shared/herc/scenaria/");
  175. strcat(init_areas_file_name,scen_num);
  176. strcat(init_areas_file_name,"/");
  177. strcat(init_areas_file_name, itoa(init_app.id));
  178. strcat(init_areas_file_name, ".txt");
  179. //printf("area file path = %s\n",areas_file_name);
  180. if ((init_areas_file = fopen(init_areas_file_name, "r")) == NULL){
  181. printf("Cannot open input file with file path = %s ",init_areas_file_name);
  182. perror("open init_areas_file_name");
  183. }
  184. init_areas_sent = 0;
  185. fscanf(init_areas_file,"%d",&init_areas_cnt);
  186. state = INIT_MANAGER_SEND_OFFERS;
  187. signals_enable();
  188. scc_signals_check();
  189. } else if (state == INIT_MANAGER_SEND_OFFERS) {// || state == INIT_MANAGER_SEND_OFFERS_SELFOPT_PENDING || state == INIT_MANAGER_SEND_OFFERS_WORK_PENDING) {
  190. signals_disable();
  191. init_idags_areas_replies=0;
  192. init_DDS_idags = 0;
  193. init_DDS_replies = 0;
  194. init_areas_num = 1;
  195. if (init_targets_head != NULL)
  196. while (init_targets_head != NULL){
  197. tmp_target_list = init_targets_head;
  198. init_targets_head = init_targets_head->next;
  199. free(tmp_target_list);
  200. }
  201. if (init_man_offers != NULL)
  202. while (init_man_offers != NULL) {
  203. tmp_offer_list = init_man_offers;
  204. init_man_offers = init_man_offers->next;
  205. free(tmp_offer_list);
  206. }
  207. fprintf(log_file, "near init area (%d,%d)\n",node_id,selfopt_r);
  208. fflush(log_file);
  209. if (core_inter_head[my_idag] == NULL){
  210. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  211. core_inter_tail[my_idag] = core_inter_head[my_idag];
  212. } else {
  213. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  214. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  215. }
  216. core_inter_tail[my_idag]->type = IDAG_FIND_IDAGS_PENDING;
  217. core_inter_tail[my_idag]->data.reg.C = node_id;
  218. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  219. core_inter_tail[my_idag]->next = NULL;
  220. if (core_inter_head[my_idag]->next == NULL) {
  221. //kill(pid_num[my_idag],SIG_IDAG_FIND_IDAGS);
  222. scc_kill(my_idag,SIG_IDAG_FIND_IDAGS);
  223. my_stats.msg_count++;
  224. my_stats.distance += distance(node_id,my_idag);
  225. } else {
  226. fprintf(log_file, "I did not send idag_find_idags with interaction %d no2 %d\n",
  227. core_inter_head[my_idag]->type,core_inter_head[my_idag]->next->type);
  228. fflush(log_file);
  229. }
  230. for (i=0; i<init_areas_cnt && i<INIT_FAR_AREAS_NUM; i++) {
  231. fscanf(init_areas_file,"%d",&C);
  232. fscanf(init_areas_file,"%d",&r);
  233. fprintf(log_file, "init area (%d,%d)\n",C,r);
  234. fflush(log_file);
  235. printf("I am %d and i am sending a far offer in region (%d,%d)\n",node_id,C,r);
  236. one_core = C;
  237. if (core_inter_head[one_core] == NULL){
  238. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  239. core_inter_tail[one_core] = core_inter_head[one_core];
  240. } else {
  241. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  242. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  243. }
  244. core_inter_tail[one_core]->type = FAR_INIT_REQ;
  245. core_inter_tail[one_core]->data.reg.C = C;
  246. core_inter_tail[one_core]->data.reg.r = r;
  247. core_inter_tail[one_core]->next = NULL;
  248. if (core_inter_head[one_core]->next == NULL) {
  249. //kill(pid_num[one_core],SIG_INIT_FAR_REQ);
  250. scc_kill(one_core,SIG_INIT_FAR_REQ);
  251. my_stats.msg_count++;
  252. my_stats.distance += distance(node_id,one_core);
  253. }
  254. //init_areas_num++;
  255. }
  256. init_areas_sent += i;
  257. //fclose(areas);
  258. if (selfopt_time_rem != -1){
  259. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error\n");
  260. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  261. if (selfopt_time_rem > 0) {
  262. its.it_value.tv_nsec = 0;//750000000;// * MS;
  263. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4123\n");
  264. }
  265. }
  266. state = IDLE_INIT_MAN;
  267. signals_enable();
  268. scc_signals_check();
  269. } else if (state == INIT_MAN_CHK_OFFERS) {// || state == INIT_MAN_CHK_OFFERS_SELFOPT_PENDING || state == INIT_MAN_CHK_OFFERS_WORK_PENDING){
  270. signals_disable();
  271. //printf("init check alarm went off node_id=%d init_DDS_idags = %d init_DDS_replies = %d state=%d!\n",node_id,init_DDS_idags,init_DDS_replies,state);
  272. cur_time = time(NULL);
  273. cur_t = localtime(&cur_time);
  274. fprintf(log_file, "[%d:%d:%d]: init check alarm went off init_DDS_idags = %d init_DDS_replies = %d state=%d\n",
  275. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,init_DDS_idags,init_DDS_replies,state);
  276. fflush(log_file);
  277. init_core_found = 0;
  278. if (init_man_offers == NULL) {
  279. //printf("I don't have offers\n");
  280. fprintf(log_file, "I don't have init man offers\n");
  281. fflush(log_file);
  282. //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);
  283. //state = IDLE_CORE;
  284. if (init_areas_sent == init_areas_cnt){
  285. fprintf(log_file,"I have to rewind my init_areas_file for app with id %d\n",init_app.id);
  286. fflush(log_file);
  287. rewind(init_areas_file);
  288. init_areas_sent = 0;
  289. fscanf(init_areas_file,"%d",&init_areas_cnt);
  290. }
  291. state = INIT_MANAGER_SEND_OFFERS;
  292. } else {
  293. tmp_offer_list = init_man_offers;
  294. while (tmp_offer_list != NULL){
  295. 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);
  296. for (i=0; i<tmp_offer_list->off.num_of_cores; i++)
  297. fprintf(log_file," %d,",tmp_offer_list->off.offered_cores[i]);
  298. fprintf(log_file,"\n");
  299. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  300. tmp_offer_list = tmp_offer_list->next;
  301. }
  302. fflush(log_file);
  303. *init_man_offers->answer = 1;
  304. //decide which will be the agent. Will be the one tha minimizes distance between cores
  305. min_dist = -1;
  306. for (i=0; i<init_man_offers->off.num_of_cores; i++){
  307. tmp_int = 0;
  308. for (j=0; j<init_man_offers->off.num_of_cores; j++){
  309. if (i == j) continue;
  310. tmp_int += distance(init_man_offers->off.offered_cores[i],init_man_offers->off.offered_cores[j]);
  311. }
  312. if (min_dist == -1 || tmp_int < min_dist) {
  313. min_dist = tmp_int;
  314. new_agent = init_man_offers->off.offered_cores[i];
  315. }
  316. }
  317. cur_time = time(NULL);
  318. cur_t = localtime(&cur_time);
  319. init_app_times[1].tm_sec = cur_t->tm_sec;
  320. init_app_times[1].tm_min = cur_t->tm_min;
  321. init_app_times[1].tm_hour = cur_t->tm_hour;
  322. //printf("New agent is %d\n",new_agent);
  323. cur_time = time(NULL);
  324. cur_t = localtime(&cur_time);
  325. 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);
  326. fflush(log_file);
  327. if (new_agent != node_id) {
  328. if (core_inter_head[new_agent] == NULL){
  329. core_inter_head[new_agent] = (inter_list *) malloc(sizeof(inter_list));
  330. core_inter_tail[new_agent] = core_inter_head[new_agent];
  331. } else {
  332. printf("Starting this app is not high priority. Should we change that?\n");
  333. core_inter_tail[new_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  334. core_inter_tail[new_agent] = core_inter_tail[new_agent]->next;
  335. }
  336. core_inter_tail[new_agent]->type = INIT_AGENT;
  337. core_inter_tail[new_agent]->data.app_cores = (int *)malloc((init_man_offers->off.num_of_cores+1)*sizeof(int));
  338. core_inter_tail[new_agent]->data.app_cores[0] = init_man_offers->off.num_of_cores;
  339. for(i=1; i<=init_man_offers->off.num_of_cores; i++)
  340. core_inter_tail[new_agent]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
  341. core_inter_tail[new_agent]->next = NULL;
  342. init_app.num_of_cores = init_man_offers->off.num_of_cores;
  343. if (core_inter_head[new_agent]->next == NULL) {
  344. //kill(pid_num[new_agent], SIG_INIT_AGENT);
  345. scc_kill(new_agent, SIG_INIT_AGENT);
  346. my_stats.msg_count++;
  347. my_stats.distance += distance(node_id,new_agent);
  348. } else {
  349. 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);
  350. fflush(log_file);
  351. }
  352. } else {
  353. my_app = init_app;
  354. my_cores_count = init_man_offers->off.num_of_cores;
  355. my_app.num_of_cores = my_cores_count;//+1;
  356. if (my_cores == NULL) {
  357. my_cores = (core_list *) malloc(sizeof(core_list));
  358. my_cores_tail = my_cores;
  359. } else {
  360. printf("My cores still not fucking null!!\n");
  361. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  362. my_cores_tail = my_cores_tail->next;
  363. }
  364. my_cores_tail->core_id = node_id;
  365. my_cores_tail->offered_to = -1;
  366. my_cores_tail->next = NULL;
  367. if (core_inter_head[my_idag] == NULL){
  368. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  369. core_inter_tail[my_idag] = core_inter_head[my_idag];
  370. } else {
  371. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  372. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  373. }
  374. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  375. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  376. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  377. core_inter_tail[my_idag]->next = NULL;
  378. //I want myself to be first in my_cores list
  379. for(i=0; i<init_man_offers->off.num_of_cores; i++) {
  380. one_core = init_man_offers->off.offered_cores[i];
  381. core_inter_tail[my_idag]->data.app_cores[i+1] = one_core;
  382. if (one_core != node_id){
  383. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  384. my_cores_tail = my_cores_tail->next;
  385. my_cores_tail->core_id = one_core;
  386. my_cores_tail->offered_to = -1;
  387. my_cores_tail->next = NULL;
  388. }
  389. }
  390. my_app_times[0].tm_sec = init_app_times[0].tm_sec;
  391. my_app_times[0].tm_min = init_app_times[0].tm_min;
  392. my_app_times[0].tm_hour = init_app_times[0].tm_hour;
  393. my_app_times[1].tm_sec = init_app_times[1].tm_sec;
  394. my_app_times[1].tm_min = init_app_times[1].tm_min;
  395. my_app_times[1].tm_hour = init_app_times[1].tm_hour;
  396. init_app_times[0].tm_sec = 0;
  397. init_app_times[0].tm_min = 0;
  398. init_app_times[0].tm_hour = 0;
  399. init_app_times[1].tm_sec = 0;
  400. init_app_times[1].tm_min = 0;
  401. init_app_times[1].tm_hour = 0;
  402. //kill(pid_num[0], SIG_INIT_APP);
  403. scc_kill(0, SIG_INIT_APP);
  404. my_stats.msg_count++;
  405. my_stats.distance += distance(node_id,0);
  406. //printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  407. // ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  408. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  409. if (core_inter_head[my_idag]->next == NULL) {
  410. //kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  411. scc_kill(my_idag, SIG_ADD_CORES_DDS);
  412. my_stats.msg_count++;
  413. my_stats.distance += distance(node_id,my_idag);
  414. } else {
  415. fprintf(log_file,"I didn't call add!! with interaction %d\n",core_inter_head[my_idag]->type);
  416. fflush(log_file);
  417. }
  418. if (my_agent != -1) {
  419. //printf("I am %d and i do this agent switch in common node with pending_state=%d\n",node_id,pending_state);
  420. fprintf(log_file,"I do this agent switch in common node with pending_state=%d\n",pending_state);
  421. if (pending_state == WORKING_NODE) {
  422. if (core_inter_head[my_agent] == NULL){
  423. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  424. core_inter_tail[my_agent] = core_inter_head[my_agent];
  425. } else {
  426. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  427. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  428. }
  429. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  430. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  431. core_inter_tail[my_agent]->next = NULL;
  432. }
  433. upper_work_bound = 0;
  434. my_agent = -1;
  435. }
  436. state = AGENT_INIT_STATE;
  437. cur_time = time(NULL);
  438. cur_t = localtime(&cur_time);
  439. 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);
  440. fprintf(log_file, "my cores are:");
  441. 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);
  442. fprintf(log_file, "\n");
  443. fflush(log_file);
  444. }
  445. tmp_offer_list = init_man_offers->next;
  446. while (tmp_offer_list != NULL){
  447. fflush(log_file);
  448. *tmp_offer_list->answer = 0;
  449. tmp_offer_list = tmp_offer_list->next;
  450. }
  451. while (init_man_offers != NULL) {
  452. if (core_inter_head[init_man_offers->sender] != NULL) {
  453. if (core_inter_head[init_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  454. core_inter_head[init_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  455. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  456. scc_kill(init_man_offers->sender, SIG_REP_OFFERS);
  457. my_stats.msg_count++;
  458. my_stats.distance += distance(node_id,init_man_offers->sender);
  459. } else if (core_inter_head[init_man_offers->sender]->type == REP_FAR_REQ_OFFER_PENDING){
  460. core_inter_head[init_man_offers->sender]->type = REP_FAR_REQ_OFFER_SENT;
  461. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  462. scc_kill(init_man_offers->sender, SIG_REP_OFFERS);
  463. my_stats.msg_count++;
  464. my_stats.distance += distance(node_id,init_man_offers->sender);
  465. } else if (core_inter_head[init_man_offers->sender]->type != REP_FAR_REQ_OFFER_SENT && core_inter_head[init_man_offers->sender]->type != REP_AGENT_OFFER_SENT)
  466. printf("We have aasdfa problem!\n");
  467. } else {
  468. printf("gamietai b = %d",init_man_offers->sender);
  469. fprintf(log_file,"gamietai b = %d",init_man_offers->sender);
  470. fflush(log_file);
  471. }
  472. tmp_offer_list = init_man_offers;
  473. init_man_offers = init_man_offers->next;
  474. free(tmp_offer_list);
  475. }
  476. fclose(init_areas_file);
  477. fprintf(log_file,"selfopt_time_rem = %ld app_state = %d state = %d pending_state = %d\n",selfopt_time_rem,app_state,state,pending_state);
  478. fflush(log_file);
  479. if (selfopt_time_rem != -1 && app_state != APP_TERMINATED){ //a selfopt is pending !app_terminated
  480. selfopt_time_rem -= 500000000;
  481. if (selfopt_time_rem > 0){
  482. its.it_value.tv_nsec = selfopt_time_rem;
  483. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error5\n");
  484. state = IDLE_AGENT;
  485. } else {
  486. selfopt_time_rem = -1;
  487. state = AGENT_SELF_OPT;
  488. }
  489. } else if (state != AGENT_INIT_STATE) {
  490. if (pending_state == IDLE_AGENT) {
  491. printf("I am %d and i entered this realm of whores\n",node_id);
  492. fprintf(log_file,"I finished my init selfopt_interval=%d, selfopt_time_rem=%ld my_cores_count=%d, old_cores_cnt=%d\n"
  493. ,selfopt_interval,selfopt_time_rem,my_cores_count,old_cores_cnt);
  494. fflush(log_file);
  495. fprintf(app_log_file,"I finished my init selfopt_interval=%d, selfopt_time_rem=%ld my_cores_count=%d, old_cores_cnt=%d\n"
  496. ,selfopt_interval,selfopt_time_rem,my_cores_count,old_cores_cnt);
  497. fflush(app_log_file);
  498. if (selfopt_interval > 0) { // && !app_terminated
  499. its.it_value.tv_nsec = selfopt_interval * MS;
  500. selfopt_time_rem = selfopt_interval;
  501. if (old_cores_cnt == 1 || old_cores_cnt == my_cores_count) {
  502. if (my_cores_count == max_cores_count) {
  503. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process in here anyway.\n");
  504. fflush(app_log_file);
  505. selfopt_time_rem = -1;
  506. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  507. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d in here anyway.\n",time_per_node,selfopt_interval);
  508. fflush(app_log_file);
  509. selfopt_time_rem = -1;
  510. } else if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error8\n");
  511. }
  512. } else if (my_cores_count == 1) {
  513. //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);
  514. fprintf(log_file,"I have to rewind my selfopt_areas_file after init for app with id %d\n",init_app.id);
  515. selfopt_interval = 200;
  516. its.it_value.tv_nsec = selfopt_interval * MS;
  517. selfopt_time_rem = selfopt_interval;
  518. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error823\n");
  519. } else selfopt_time_rem = -1;
  520. state = IDLE_AGENT;
  521. pending_state = NO_PENDING_STATE;
  522. } else if (pending_state == AGENT_SELF_OPT || pending_state == WORKING_NODE || pending_state == AGENT_REWIND_FILE) {
  523. state = pending_state;//IDLE_AGENT;
  524. pending_state = NO_PENDING_STATE;
  525. } else state = IDLE_CORE;
  526. } else if (app_state != APP_TERMINATED && new_agent != node_id) state = IDLE_AGENT;//!app_terminated
  527. }
  528. signals_enable();
  529. scc_signals_check();
  530. } else if (state == AGENT_INIT_STATE) {//|| state == AGENT_INIT_STATE_INIT_INTERRUPTED
  531. signals_disable();
  532. selfopt_interval=200;
  533. nodes_ended_cnt = 0;
  534. app_state = RUNNING;
  535. max_cores_count = get_max_cores_count(my_app);
  536. //app_terminated = 0;
  537. //pending_state = NO_PENDING_STATE;
  538. if (my_cores_count > 1){
  539. time_to_work = (int) roundf(my_app.workld / my_Speedup);
  540. time_per_node = time_to_work / (my_cores_count-1);
  541. time_left = time_to_work % (my_cores_count-1);
  542. if (time_to_work == 0) time_left++;
  543. }
  544. //printf("i am init agent %d. speedup = %0.2f time_to_work = %d time_per_node = %d, workld=%0.2f\n",node_id,my_Speedup,time_to_work,time_per_node,my_app.workld);
  545. strcpy(app_log_file_name,"/shared/herc/scenaria/");
  546. strcat(app_log_file_name,scen_num);
  547. strcat(app_log_file_name,"/app_logs/");
  548. strcat(app_log_file_name, itoa(my_app.id));
  549. strcat(app_log_file_name, ".txt");
  550. //printf("area file path self opt = %s\n",areas_file_name);
  551. if ((app_log_file = fopen(app_log_file_name, "w")) == NULL){
  552. printf("Cannot open input file with file path = %s ",app_log_file_name);
  553. perror("open selfopt_areas_file_name");
  554. }
  555. fprintf(app_log_file, "Came into init_agent at [%d:%d:%d]\n",my_app_times[0].tm_hour,my_app_times[0].tm_min,my_app_times[0].tm_sec);
  556. fprintf(app_log_file, "New agent found at [%d:%d:%d]\n",my_app_times[1].tm_hour,my_app_times[1].tm_min,my_app_times[1].tm_sec);
  557. cur_time = time(NULL);
  558. cur_t = localtime(&cur_time);
  559. fprintf(app_log_file, "[%d:%d:%d]: I am agent %d . Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,node_id,my_cores_count,my_app.id);
  560. fprintf(app_log_file, "my cores are:");
  561. 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);
  562. fprintf(app_log_file, "\n");
  563. 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);
  564. fflush(app_log_file);
  565. tmp_cores_list = my_cores->next;
  566. while (tmp_cores_list != NULL){
  567. one_core = tmp_cores_list->core_id;
  568. if (core_inter_head[one_core] == NULL){
  569. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  570. core_inter_tail[one_core] = core_inter_head[one_core];
  571. } else {
  572. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  573. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  574. }
  575. core_inter_tail[one_core]->type = INIT_WORK_NODE;
  576. core_inter_tail[one_core]->data.work_time = time_per_node;
  577. if (time_left > 0) {
  578. core_inter_tail[one_core]->data.work_time++;
  579. time_left--;
  580. }
  581. core_inter_tail[one_core]->next = NULL;
  582. my_stats.app_turnaround += core_inter_tail[one_core]->data.work_time;
  583. if (core_inter_head[one_core]->next == NULL) {
  584. //kill(pid_num[one_core], SIG_APPOINT_WORK);
  585. scc_kill(one_core, SIG_APPOINT_WORK);
  586. my_stats.msg_count++;
  587. my_stats.distance += distance(node_id,one_core);
  588. } else printf("I am %d and i am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",
  589. node_id,one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  590. tmp_cores_list = tmp_cores_list->next;
  591. } //else printf("i have only one fucking core\n");
  592. if (my_cores_count == max_cores_count) {
  593. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process ini init_agent.\n");
  594. fflush(app_log_file);
  595. selfopt_time_rem = -1;
  596. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  597. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  598. state = pending_state;
  599. pending_state = IDLE_AGENT;
  600. } else state = IDLE_AGENT;
  601. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  602. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d in init_agent.\n",time_per_node,selfopt_interval);
  603. fflush(app_log_file);
  604. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  605. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  606. state = pending_state;
  607. pending_state = IDLE_AGENT;
  608. } else state = IDLE_AGENT;
  609. selfopt_time_rem = -1;
  610. } else {
  611. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  612. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  613. state = pending_state;//IDLE_INIT_MAN;//IDLE_INIT_MAN_SELFOPT_PENDING;
  614. pending_state = AGENT_SELF_OPT;
  615. //printf("I am %d o fountas einai poustara pou zwgrafizei k ton fuckaroun oi gkomenes tou!\n",node_id);
  616. } else state = AGENT_SELF_OPT;
  617. }
  618. signals_enable();
  619. scc_signals_check();
  620. } else if (state == AGENT_SELF_OPT) {
  621. signals_disable();
  622. //printf("Initialising self opt node_id = %d\n",node_id);
  623. cur_time = time(NULL);
  624. cur_t = localtime(&cur_time);
  625. fprintf(log_file, "[%d:%d:%d]: Initialising self opt!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  626. fflush(log_file);
  627. fprintf(app_log_file, "[%d:%d:%d]: Initialising self opt!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  628. fflush(app_log_file);
  629. pending_state = NO_PENDING_STATE;
  630. if (app_state != APP_TERMINATED) {//!app_terminated
  631. selfopt_DDS_idags = 0;
  632. selfopt_DDS_replies = 0;
  633. //selfopt_areas_sent = 0;
  634. //if (selfopt_man_offers != NULL) printf("selfopt offers list is not NULL\n");
  635. if (selfopt_targets_head != NULL) {
  636. //printf("selfopt targets list is not NULL\n");
  637. while (selfopt_targets_head != NULL){
  638. tmp_target_list = selfopt_targets_head;
  639. selfopt_targets_head = selfopt_targets_head->next;
  640. free(tmp_target_list);
  641. }
  642. }
  643. if (core_inter_head[my_idag] == NULL){
  644. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  645. core_inter_tail[my_idag] = core_inter_head[my_idag];
  646. } else {
  647. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  648. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  649. }
  650. core_inter_tail[my_idag]->type = SELFOPT_IDAG_FIND_IDAGS_PENDING;
  651. core_inter_tail[my_idag]->data.reg.C = node_id;
  652. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  653. core_inter_tail[my_idag]->next = NULL;
  654. if (core_inter_head[my_idag]->next == NULL) {
  655. //kill(pid_num[my_idag],SIG_IDAG_FIND_IDAGS);
  656. scc_kill(my_idag,SIG_IDAG_FIND_IDAGS);
  657. my_stats.msg_count++;
  658. my_stats.distance += distance(node_id,my_idag);
  659. }
  660. if (selfopt_interval != 800) selfopt_interval = 2 * selfopt_interval;
  661. else selfopt_interval = -1;
  662. state = IDLE_AGENT_WAITING_OFF;
  663. } else state = AGENT_ENDING;
  664. signals_enable();
  665. scc_signals_check();
  666. } else if (state == AGENT_SELF_CHK_OFFERS) {
  667. signals_disable();
  668. //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);
  669. cur_time = time(NULL);
  670. cur_t = localtime(&cur_time);
  671. 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\n",
  672. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state);
  673. fflush(log_file);
  674. 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\n",
  675. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state);
  676. fflush(app_log_file);
  677. old_cores_cnt = my_cores_count;
  678. if (selfopt_man_offers == NULL) {
  679. printf("I don't have self opt offers app_state=%d\n",app_state);
  680. fprintf(log_file,"I don't have self opt offers app_state=%d\n",app_state);
  681. fflush(log_file);
  682. fprintf(app_log_file,"I don't have self opt offers app_state=%d\n",app_state);
  683. fflush(app_log_file);
  684. //state = IDLE_AGENT;
  685. } else {
  686. tmp_offer_list = selfopt_man_offers;
  687. while (tmp_offer_list != NULL){
  688. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  689. fprintf(log_file,"Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  690. fflush(log_file);
  691. tmp_offer_list = tmp_offer_list->next;
  692. }
  693. if (app_state != APP_TERMINATED) {
  694. *selfopt_man_offers->answer = 1;
  695. if (core_inter_head[my_idag] == NULL){
  696. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  697. core_inter_tail[my_idag] = core_inter_head[my_idag];
  698. } else {
  699. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  700. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  701. }
  702. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  703. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((selfopt_man_offers->off.num_of_cores+1)*sizeof(int));
  704. core_inter_tail[my_idag]->data.app_cores[0] = selfopt_man_offers->off.num_of_cores;
  705. core_inter_tail[my_idag]->next = NULL;
  706. //
  707. if (app_state != RESIZING) {
  708. old_Speedup = my_Speedup;
  709. chk_rem_num = my_cores_count-1;
  710. fprintf(app_log_file,"Initialising resize with check_rem = %d\n",chk_rem_num);
  711. fflush(app_log_file);
  712. chk_rem_count = 0;
  713. sum_rem_time = 0;
  714. app_state = RESIZING;
  715. my_cores_count += selfopt_man_offers->off.num_of_cores;
  716. my_app.num_of_cores = my_cores_count;
  717. my_Speedup = Speedup(my_app, my_cores_count);
  718. if (old_cores_cnt > 1)
  719. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  720. one_core = tmp_cores_list->core_id;
  721. if (core_inter_head[one_core] == NULL){
  722. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  723. core_inter_tail[one_core] = core_inter_head[one_core];
  724. } else {
  725. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  726. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  727. }
  728. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  729. core_inter_tail[one_core]->next = NULL;
  730. if (core_inter_head[one_core]->next == NULL) {
  731. //kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  732. scc_kill(one_core, SIG_CHECK_REM_TIME);
  733. my_stats.msg_count++;
  734. my_stats.distance += distance(node_id,one_core);
  735. } else printf("I am %d and i am doing smth else with my working node %d in send SIG_CHECK_REM_TIME interaction = %d\n",node_id,one_core,core_inter_head[one_core]->type);
  736. }
  737. } else {
  738. fprintf(app_log_file,"Allready resizing with chk_rem_num = %d chk_rem_count=%d\n",chk_rem_num,chk_rem_count);
  739. fflush(app_log_file);
  740. my_cores_count += selfopt_man_offers->off.num_of_cores;
  741. my_app.num_of_cores = my_cores_count;
  742. my_Speedup = Speedup(my_app, my_cores_count);
  743. }
  744. for (i=0; i<selfopt_man_offers->off.num_of_cores; i++){
  745. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  746. my_cores_tail = my_cores_tail->next;
  747. my_cores_tail->core_id = selfopt_man_offers->off.offered_cores[i];
  748. my_cores_tail->offered_to = -1;
  749. my_cores_tail->next = NULL;
  750. core_inter_tail[my_idag]->data.app_cores[i+1] = selfopt_man_offers->off.offered_cores[i];
  751. one_core = my_cores_tail->core_id;
  752. //printf("I am %d and i put in appoint work %d\n",node_id,one_core);
  753. if (core_inter_head[one_core] == NULL){
  754. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  755. core_inter_tail[one_core] = core_inter_head[one_core];
  756. } else {
  757. fprintf(log_file,"I am doing sth with my new core %d interaction is %d\n",one_core,core_inter_head[one_core]->type);
  758. fflush(log_file);
  759. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  760. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  761. }
  762. core_inter_tail[one_core]->type = INIT_WORK_NODE_PENDING;
  763. core_inter_tail[one_core]->next = NULL;
  764. }
  765. } else *selfopt_man_offers->answer = 0;
  766. tmp_offer_list = selfopt_man_offers->next;
  767. while (tmp_offer_list != NULL){
  768. *tmp_offer_list->answer = 0;
  769. tmp_offer_list = tmp_offer_list->next;
  770. }
  771. if (app_state != APP_TERMINATED) {
  772. //my idag may have offered smth
  773. if (core_inter_head[my_idag]->next == NULL) {
  774. //kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  775. scc_kill(my_idag, SIG_ADD_CORES_DDS);
  776. my_stats.msg_count++;
  777. my_stats.distance += distance(node_id,my_idag);
  778. } else {
  779. fprintf(log_file,"I didn't call add inside selfopt with interaction %d\n",core_inter_head[my_idag]->type);
  780. fflush(log_file);
  781. }
  782. if (old_cores_cnt == 1) {
  783. nodes_ended_cnt = 0;
  784. time_to_work = (int) roundf(my_app.workld / my_Speedup);
  785. time_per_node = time_to_work / (my_cores_count-1);
  786. time_left = time_to_work % (my_cores_count-1);
  787. if (time_to_work == 0) time_left++;
  788. fprintf(app_log_file,"in one core speedup = %0.2f time_to_work = %d time_per_node = %d, workld=%0.2f\n",my_Speedup,time_to_work,time_per_node,my_app.workld);
  789. fflush(app_log_file);
  790. tmp_cores_list = my_cores->next;
  791. while (tmp_cores_list != NULL){
  792. //printf("I am inside core listing node=%d\n",node_id);
  793. one_core = tmp_cores_list->core_id;
  794. for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  795. if (tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  796. if (tmp_inter_list != NULL) {
  797. tmp_inter_list->type = INIT_WORK_NODE;
  798. tmp_inter_list->data.work_time = time_per_node;
  799. if (time_left > 0) {
  800. tmp_inter_list->data.work_time++;
  801. time_left--;
  802. }
  803. my_stats.app_turnaround += tmp_inter_list->data.work_time;
  804. } else printf("i am %d kai sto common_node ta hpiame one core = %d\n",node_id,one_core);
  805. if (core_inter_head[one_core]->next == NULL) {
  806. //kill(pid_num[one_core], SIG_APPOINT_WORK);
  807. scc_kill(one_core, SIG_APPOINT_WORK);
  808. my_stats.msg_count++;
  809. my_stats.distance += distance(node_id,one_core);
  810. } else {
  811. fprintf(log_file,"I am doing smth else with my working node? interaction=%d interaction2=%d\n",
  812. core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  813. fflush(log_file);
  814. }
  815. tmp_cores_list = tmp_cores_list->next;
  816. }
  817. app_state = RUNNING;
  818. }
  819. }
  820. while (selfopt_man_offers != NULL) {
  821. if (core_inter_head[selfopt_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  822. core_inter_head[selfopt_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  823. //kill(pid_num[selfopt_man_offers->sender], SIG_REP_OFFERS);
  824. scc_kill(selfopt_man_offers->sender, SIG_REP_OFFERS);
  825. my_stats.msg_count++;
  826. my_stats.distance += distance(node_id,selfopt_man_offers->sender);
  827. } else if (core_inter_head[selfopt_man_offers->sender]->type != REP_AGENT_OFFER_SENT) printf("We have zxcvzxc problem!\n");
  828. tmp_offer_list = selfopt_man_offers;
  829. selfopt_man_offers = selfopt_man_offers->next;
  830. free(tmp_offer_list);
  831. }
  832. //printf("Self opt seems to be ok cores count = %d\n",my_cores_count);
  833. cur_time = time(NULL);
  834. cur_t = localtime(&cur_time);
  835. 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);
  836. fprintf(log_file, "my cores are:");
  837. 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);
  838. fprintf(app_log_file, "my cores are:");
  839. //printf("I am %d and my cores are:",node_id);
  840. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  841. fprintf(log_file, " %d", tmp_cores_list->core_id);
  842. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  843. //printf(" %d",tmp_cores_list->core_id);
  844. }
  845. //printf("\n");
  846. fprintf(log_file, "\n");
  847. fflush(log_file);
  848. fprintf(app_log_file, "\n");
  849. fflush(app_log_file);
  850. }
  851. if (app_state == APP_TERMINATED) state = AGENT_ENDING;//app_terminated
  852. else {
  853. if (pending_state == INIT_MANAGER) {
  854. state = INIT_MANAGER;
  855. pending_state = IDLE_AGENT;
  856. } else {
  857. printf("I am %d in chk_offers and pending_state=%d\n",node_id,pending_state);
  858. if (selfopt_interval > 0) { // && !app_terminated
  859. its.it_value.tv_nsec = selfopt_interval * MS;
  860. selfopt_time_rem = selfopt_interval;
  861. if (old_cores_cnt == 1 || old_cores_cnt == my_cores_count) {
  862. if (my_cores_count == max_cores_count) {
  863. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process in selfopt.\n");
  864. fflush(app_log_file);
  865. selfopt_time_rem = -1;
  866. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  867. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d in selfopt.\n",time_per_node,selfopt_interval);
  868. fflush(app_log_file);
  869. selfopt_time_rem = -1;
  870. } else if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error8\n");
  871. }
  872. } else if (my_cores_count == 1) {
  873. //printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  874. fprintf(log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",my_app.id);
  875. fflush(log_file);
  876. fprintf(app_log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",my_app.id);
  877. fflush(app_log_file);
  878. selfopt_interval = 200;
  879. its.it_value.tv_nsec = selfopt_interval * MS;
  880. selfopt_time_rem = selfopt_interval;
  881. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error82\n");
  882. } else selfopt_time_rem = -1;
  883. state = IDLE_AGENT;
  884. }
  885. }
  886. signals_enable();
  887. scc_signals_check();
  888. } else if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT){
  889. int j,dummy=0;
  890. signals_disable();
  891. while (time_worked <= upper_work_bound){
  892. scc_signals_check();
  893. signals_enable();
  894. //the following code should be one ms long
  895. for (i=0; i<1000; i++)
  896. for(j=0; j<1000; j++)
  897. dummy++;
  898. signals_disable();
  899. time_worked++;
  900. }
  901. printf("I am node %d with agent = %d and I finished my work upper_work_bound = %ld time_passed=%d\n",node_id,my_agent,upper_work_bound,time_worked);
  902. if (upper_work_bound > 0) {
  903. //kill(pid_num[my_agent],SIG_FINISH);
  904. scc_kill(my_agent,SIG_FINISH);
  905. my_stats.msg_count++;
  906. my_stats.distance += distance(node_id,my_agent);
  907. }
  908. if (state == WORKING_NODE) state = IDLE_CORE;
  909. else if (state == WORKING_NODE_IDLE_INIT) state = IDLE_INIT_MAN;
  910. else if (pending_state == WORKING_NODE) pending_state = NO_PENDING_STATE; //just finished on return
  911. signals_enable();
  912. scc_signals_check();
  913. } else if (state == AGENT_ZOMBIE) {
  914. //pause();
  915. dummy=0;
  916. for (i=0; i<1000; i++)
  917. for(j=0; j<1000; j++)
  918. dummy++;
  919. scc_signals_check();
  920. } else if (state == AGENT_ENDING) {
  921. signals_disable();
  922. cur_time = time(NULL);
  923. cur_t = localtime(&cur_time);
  924. fprintf(log_file, "[%d:%d:%d]: I entered agent_ending\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  925. fflush(log_file);
  926. fprintf(app_log_file, "[%d:%d:%d]: I entered agent_ending\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  927. fflush(app_log_file);
  928. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  929. fprintf(app_log_file,"Core %d is offered to %d\n",tmp_cores_list->core_id,tmp_cores_list->offered_to);
  930. fflush(app_log_file);
  931. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  932. if (tmp_cores_list->offered_to != -1) break;
  933. if (tmp_cores_list != NULL) {
  934. state = AGENT_ZOMBIE;
  935. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next) {
  936. one_core = tmp_cores_list->core_id;
  937. tmp_inter_prev = NULL;
  938. tmp_inter_list = core_inter_head[one_core];
  939. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  940. while (tmp_inter_list != NULL)
  941. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  942. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == REMOVED_NODE_REM_TIME) {
  943. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  944. fprintf(log_file, "Removing in zombie one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  945. fflush(log_file);
  946. if (tmp_inter_prev == NULL) {
  947. core_inter_head[one_core] = core_inter_head[one_core]->next;
  948. free(tmp_inter_list);
  949. tmp_inter_list = core_inter_head[one_core];
  950. } else {
  951. tmp_inter_prev->next = tmp_inter_list->next;
  952. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  953. free(tmp_inter_list);
  954. tmp_inter_list = tmp_inter_prev->next;
  955. }
  956. } else {
  957. tmp_inter_prev = tmp_inter_list;
  958. tmp_inter_list = tmp_inter_list->next;
  959. }
  960. }
  961. } else {
  962. if (core_inter_head[my_idag] == NULL){
  963. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  964. core_inter_tail[my_idag] = core_inter_head[my_idag];
  965. } else {
  966. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  967. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  968. }
  969. core_inter_tail[my_idag]->type = REMOVE_APP;
  970. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  971. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  972. core_inter_tail[my_idag]->next = NULL;
  973. i=1;
  974. core_inter_tail[my_idag]->data.app_cores[i++] = my_cores->core_id;
  975. tmp_cores_list = my_cores;
  976. my_cores=my_cores->next;
  977. free(tmp_cores_list);
  978. //for(my_cores=my_cores->next; my_cores!=NULL; my_cores = my_cores->next) {
  979. //free(tmp_cores_list);
  980. while (my_cores != NULL) {
  981. tmp_cores_list = my_cores;
  982. one_core = tmp_cores_list->core_id;
  983. core_inter_tail[my_idag]->data.app_cores[i++] = one_core;
  984. tmp_inter_prev = NULL;
  985. tmp_inter_list = core_inter_head[one_core];
  986. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  987. while (tmp_inter_list != NULL)
  988. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  989. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == REMOVED_NODE_REM_TIME) {
  990. fprintf(log_file, "Removing one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  991. fflush(log_file);
  992. if (tmp_inter_prev == NULL) {
  993. core_inter_head[one_core] = core_inter_head[one_core]->next;
  994. free(tmp_inter_list);
  995. tmp_inter_list = core_inter_head[one_core];
  996. } else {
  997. tmp_inter_prev->next = tmp_inter_list->next;
  998. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  999. free(tmp_inter_list);
  1000. tmp_inter_list = tmp_inter_prev->next;
  1001. }
  1002. } else {
  1003. tmp_inter_prev = tmp_inter_list;
  1004. tmp_inter_list = tmp_inter_list->next;
  1005. }
  1006. if (core_inter_head[one_core] == NULL) {
  1007. //kill(pid_num[one_core],SIG_FINISH);
  1008. scc_kill(one_core,SIG_FINISH);
  1009. my_stats.msg_count++;
  1010. my_stats.distance += distance(node_id,one_core);
  1011. } else {
  1012. 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);
  1013. fflush(log_file);
  1014. }
  1015. my_cores=my_cores->next;
  1016. free(tmp_cores_list);
  1017. }
  1018. if (core_inter_head[my_idag]->next == NULL) {
  1019. //kill(pid_num[my_idag],SIG_FINISH);
  1020. scc_kill(my_idag,SIG_FINISH);
  1021. my_stats.msg_count++;
  1022. my_stats.distance += distance(node_id,my_idag);
  1023. } else {
  1024. fprintf(log_file,"I am doing smth else with my idag app_remove interaction=%d\n",core_inter_head[my_idag]->type);
  1025. fflush(log_file);
  1026. }
  1027. my_app.A = 0.0;
  1028. my_app.var = 0.0;
  1029. my_app.num_of_cores = -1;
  1030. selfopt_time_rem = -1;
  1031. //app_terminated = 0;
  1032. app_state = NO_APP;
  1033. //printf("I am %d My app ended app_id = %d pending_state=%d\n",node_id,my_app.id,pending_state);
  1034. cur_time = time(NULL);
  1035. cur_t = localtime(&cur_time);
  1036. fprintf(app_log_file, "[%d:%d:%d]: App ended app_id = %d pending_state=%d",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_app.id,pending_state);
  1037. //fprintf(app_log_file,"App ended app_id = %d pending_state=%d\n",my_app.id,pending_state);
  1038. fclose(app_log_file);
  1039. if (pending_state == INIT_MANAGER) {
  1040. state = INIT_MANAGER;
  1041. pending_state = IDLE_CORE;
  1042. } else if (pending_state == NO_PENDING_STATE) state = IDLE_CORE;
  1043. else {
  1044. //printf("I am %d in agent_ending and pending_state = %d\n",node_id,pending_state);
  1045. state = IDLE_CORE;
  1046. }
  1047. }
  1048. if (my_idag != 0) {
  1049. if (core_inter_head[0] == NULL) scc_kill(0, SIG_APP_TERMINATED);
  1050. else {
  1051. core_inter_tail[0]->next = (inter_list *) malloc(sizeof(inter_list));
  1052. core_inter_tail[0] = core_inter_tail[0]->next;
  1053. core_inter_tail[0]->type = NOTIFY_APP_TERMINATION;
  1054. core_inter_tail[0]->next = NULL;
  1055. }
  1056. }
  1057. signals_enable();
  1058. scc_signals_check();
  1059. } else {
  1060. printf("Unknown state node_id = %d state = %d\n",node_id,state);
  1061. state = IDLE_CORE;
  1062. }
  1063. RCCE_flag_free(&flag_signals_enabled);
  1064. RCCE_flag_free(&flag_data_written);
  1065. RCCE_free((t_vcharp) sig_array);
  1066. RCCE_free((t_vcharp) data_array);
  1067. cur_time = time(NULL);
  1068. cur_t = localtime(&cur_time);
  1069. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1070. fclose(log_file);
  1071. exit(0);
  1072. }