123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819 |
- /*void sig_IDAG_FIND_IDAGS_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r=-1, sender_id, num_of_idags, i, one_idag;
- char *fifo_name;
- region cur_reg;
- inter_list *tmp_inter_list;
- //printf("I came through FIND_IDAGS!! idag_id = %d, node_id = %d\n",idag_id,node_id);
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL || core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS){ //I am the idag
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 1 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
-
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
- //printf("open3 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- read(fd_r, &cur_reg, sizeof(region));
- //printf("asdI am node with id %d idag_id %d and I am to investigate region C=%d r=%d\n",node_id,idag_id,cur_reg.C,cur_reg.r);
-
- if (core_inter_head[sender_id] == NULL){
- core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_head[sender_id];
- } else {
- core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
- }
- core_inter_tail[sender_id]->type = REP_IDAG_FIND_IDAGS;
- core_inter_tail[sender_id]->data.idags_in_reg = (int *) malloc((num_idags_x*num_idags_y+1)*sizeof(int));
- core_inter_tail[sender_id]->next = NULL;
-
- get_reg_idags(cur_reg, core_inter_tail[sender_id]->data.idags_in_reg);
- if (core_inter_head[sender_id]->next == NULL) {
- kill(info->si_pid, SIG_IDAG_FIND_IDAGS);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
-
- } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
- //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
- printf("I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS && state != IDLE_AGENT_WAITING_OFF) {
- printf("I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS){ //I am the requesting common node
- //printf("qweI am inside i am node with node id %d idag_id %d and sender_id %d %s\n",node_id,idag_id,sender_id,fifo_name);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 2 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open4 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- read(fd_r, &num_of_idags, sizeof(int));
- fprintf(log_file,"Number of agents in region %d\n",num_of_idags);
- fflush(log_file);
- //if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_FIND_IDAGS) far_req_max_man_count = num_of_idags;
- //else
- if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) {
- init_idags_areas_replies++;
- init_DDS_idags += num_of_idags;
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS) selfopt_DDS_idags += num_of_idags;
-
- for (i=0; i<num_of_idags; i++){
- read(fd_r, &one_idag, sizeof(int));
- fprintf(log_file,"In the region I have idag with id %d\n",one_idag);
- fflush(log_file);
- if (core_inter_head[one_idag] == NULL){
- core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_idag] = core_inter_head[one_idag];
- } else {
- for (tmp_inter_list = core_inter_head[one_idag]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
- if (tmp_inter_list->type == FAR_REQ_MAN) break;
- if (tmp_inter_list != NULL) {
- fprintf(log_file,"I dismissed node %d in rep_idag_find_idags\n",one_idag);
- fflush(log_file);
- init_DDS_idags--;
- continue;
- }
-
- core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
- }
- if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = IDAG_REQ_DDS_PENDING;
- else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
- else core_inter_tail[one_idag]->type = SELFOPT_IDAG_REQ_DDS_PENDING;
-
- core_inter_tail[one_idag]->data.reg.C = core_inter_head[sender_id]->data.reg.C;
- core_inter_tail[one_idag]->data.reg.r = core_inter_head[sender_id]->data.reg.r;
- core_inter_tail[one_idag]->next = NULL;
- if (core_inter_head[one_idag]->next == NULL) {
- kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_idag);
- } else {
- fprintf(log_file,"I did not sent req_dds to %d with interaction = %d inter 2=%d\n",one_idag,core_inter_head[one_idag]->type,core_inter_head[one_idag]->next->type);
- fflush(log_file);
- }
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING){
- printf("I am %d and deadlock was prevented in sig_find_idags\n",node_id);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else {
- printf("I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d node_id=%d interaction=%d\n",sender_id,node_id,core_inter_head[sender_id]->type);
- fprintf(log_file,"I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- if (fd_r != -1) close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }*/
- /*void sig_REQ_DDS_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r=-1, sender_id, num_of_agents, i, agent_id, num_of_cores,j;
- char *fifo_name;
- region cur_reg;
- core_list *tmp_cores_list;
- inter_list *tmp_inter_list;
- DDS_list *tmp_DDS;
- target_list *tmp_target_list;
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL){ //I am the idag
- //printf("I am %d and %d requested DDS\n",idag_id,sender_id);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 3 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open5 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
- read(fd_r, &cur_reg, sizeof(region));
- //sem_post(&node_sem[sender_id]);
- //while (cur_reg.C < 0) {
- // printf("I am %d and i got shit region (%d,%d) in req_dds from %d\n",node_id,cur_reg.C,cur_reg.r,sender_id);
- // read(fd_r, &cur_reg, sizeof(region));
- //}
- fprintf(log_file,"I am to investigate region C=%d r=%d for %d\n",cur_reg.C,cur_reg.r,sender_id);
- fflush(log_file);
- if (core_inter_head[sender_id] == NULL){
- core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_head[sender_id];
- } else {
- printf("Ton hpiame\n");
- core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
- }
- core_inter_tail[sender_id]->type = REP_IDAG_REQ_DDS;
- core_inter_tail[sender_id]->next = NULL;
- if (cur_reg.C > -1) {//gia na zhtaei o node 0 dds
- core_inter_tail[sender_id]->data.agents_in_reg = (int *) malloc((2*DDS_count+1)*sizeof(int));
- core_inter_tail[sender_id]->data.agents_in_reg[0] = 0;
- for (i=1; i<=2*DDS_count; i+=2){
- core_inter_tail[sender_id]->data.agents_in_reg[i] = -1;
- core_inter_tail[sender_id]->data.agents_in_reg[i+1] = 0;
- }
-
- tmp_cores_list = my_cores;
- while (tmp_cores_list != NULL){
- if (distance(tmp_cores_list->core_id, cur_reg.C) <= cur_reg.r){
- if (tmp_cores_list->offered_to == -1) agent_id=node_id;
- else {
- agent_id=tmp_cores_list->offered_to;
- for (tmp_DDS = DDS->next; tmp_DDS!=NULL; tmp_DDS=tmp_DDS->next) if (tmp_DDS->agent_id == agent_id) break;
- if (tmp_DDS == NULL) agent_id = node_id;
- }
- for (i=1; i<=2*DDS_count; i+=2)
- if (core_inter_tail[sender_id]->data.agents_in_reg[i] == agent_id) break;
- else if (core_inter_tail[sender_id]->data.agents_in_reg[i] == -1){
- core_inter_tail[sender_id]->data.agents_in_reg[0]++;
- core_inter_tail[sender_id]->data.agents_in_reg[i] = agent_id;
- break;
- }
- core_inter_tail[sender_id]->data.agents_in_reg[i+1]++;
- }
- tmp_cores_list = tmp_cores_list->next;
- }
- } else core_inter_tail[sender_id]->data.agents_in_reg = NULL;
-
- kill(info->si_pid, SIG_REQ_DDS);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
- //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
- printf("I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && state != IDLE_AGENT_WAITING_OFF) {
- printf("I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){ //I am the requesting common node
- //printf("vbnI am inside i am node with node id %d and sender_id %d\n",node_id,sender_id);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 4 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open5 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
- init_DDS_replies++;
- fprintf(log_file,"init_DDS_idags = %d, init_DDS_replies = %d init_idags_areas_replies=%d\n",init_DDS_idags,init_DDS_replies,init_idags_areas_replies);
- fflush(log_file);
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS) {
- selfopt_DDS_replies++;
- fprintf(log_file,"selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
- fflush(log_file);
- }
- read(fd_r, &num_of_agents, sizeof(int));
- while (num_of_agents > X_max*Y_max) {
- printf("I am %d and in req_dds i got shit num_of_agents %d from %d\n",node_id,num_of_agents,sender_id);
- read(fd_r, &num_of_agents, sizeof(int));
- }
- //if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) far_man_agent_count += num_of_agents;
- //printf("Number of agents in region = %d\n",num_of_agents);
- //fprintf(log_file, "Number of agents in region = %d of %d init_DDS_replies = %d init_DDS_idags = %d\n",num_of_agents,sender_id,init_DDS_replies,init_DDS_idags);
- fprintf(log_file, "Number of agents in region = %d of %d reg = (%d,%d)\n",num_of_agents,sender_id,core_inter_head[sender_id]->data.reg.C,core_inter_head[sender_id]->data.reg.r);
- fflush(log_file);
- for (i=0; i<num_of_agents; i++) {
- read(fd_r, &agent_id, sizeof(int));
- read(fd_r, &num_of_cores, sizeof(int));
- //printf("zxc I am %d and In the DDS of %d there is an agent with id %d and %d cores\n",node_id,sender_id,agent_id,num_of_cores);
- fprintf(log_file, "there is an agent with id %d and %d cores\n",agent_id,num_of_cores);
- fflush(log_file);
- if (agent_id == node_id) continue;//((node_id == -1 && agent_id == idag_id) || node_id == agent_id)
- if (core_inter_head[sender_id]->type == IDAG_REQ_DDS){
- tmp_target_list = init_targets_head;
- while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
- //if (tmp_target_list->target != agent_id)
- tmp_target_list = tmp_target_list->next;
- if (tmp_target_list == NULL) {
- if (init_targets_head == NULL) {
- init_targets_head = (target_list *) malloc(sizeof(target_list));
- init_targets_tail = init_targets_head;;
- } else {
- init_targets_tail->next = (target_list *) malloc(sizeof(target_list));
- init_targets_tail = init_targets_tail->next;
- }
- //fprintf(log_file, "in here it begins a\n");
- //fflush(log_file);
- init_targets_tail->next = NULL;
- init_targets_tail->target = agent_id;
- init_targets_tail->num_of_regions = 1;
- //init_targets_tail->region_arr = (region *) malloc(INIT_AREAS_NUM * sizeof(region));
- init_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
- //fprintf(log_file, "in here it begins b\n");
- //fflush(log_file);
- } else {
- fprintf(log_file, "in here num_of_regions = %d\n",tmp_target_list->num_of_regions);
- fflush(log_file);
- for (j=0; j<tmp_target_list->num_of_regions; j++)
- if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
- fprintf(log_file, "fucking area allready exists\n");
- fflush(log_file);
- break;
- }
-
- if (j == tmp_target_list->num_of_regions) {
- tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
- //fprintf(log_file, "new area added\n");
- //fflush(log_file);
- }
- }
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){
- tmp_target_list = selfopt_targets_head;
- while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
- tmp_target_list = tmp_target_list->next;
- if (tmp_target_list == NULL) {
- if (selfopt_targets_head == NULL) {
- selfopt_targets_head = (target_list *) malloc(sizeof(target_list));
- selfopt_targets_tail = selfopt_targets_head;;
- } else {
- selfopt_targets_tail->next = (target_list *) malloc(sizeof(target_list));
- selfopt_targets_tail = selfopt_targets_tail->next;
- }
- selfopt_targets_tail->next = NULL;
- selfopt_targets_tail->target = agent_id;
- selfopt_targets_tail->num_of_regions = 1;
- selfopt_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
- } else {
- for (j=0; j<tmp_target_list->num_of_regions; j++)
- if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
- break;
- }
-
- if (j == tmp_target_list->num_of_regions)
- tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
- } //tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
- } else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS){
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
- //if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
- //else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) core_inter_tail[agent_id]->type = FAR_REQ_CORES;
- //else core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
-
- core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
- core_inter_tail[agent_id]->data.reg = core_inter_head[sender_id]->data.reg;
- core_inter_tail[agent_id]->next = NULL;
-
- if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REQ_CORES); //newly created, not an idag
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- }
- //else printf("It is fucked here interaction= %d\n",core_inter_head[agent_id]->type);
- }
- }
-
- if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_areas_num) {
- for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
- agent_id = tmp_target_list->target;
- for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
- if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
- if (tmp_inter_list == NULL) {
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
-
- core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
- core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
- core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
-
- if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
- fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
- for (i=0; i<tmp_target_list->num_of_regions; i++) {
- core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
- fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
- }
- fprintf(log_file,"\n");
- fflush(log_file);
- core_inter_tail[agent_id]->next = NULL;
- //kill(pid_num[agent_id], SIG_REQ_CORES);
- if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
- } else
- printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
- }
- its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
- if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4\n");
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && selfopt_DDS_replies == selfopt_DDS_idags) {
- for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
- agent_id = tmp_target_list->target;
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
-
- core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
- core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
- core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
- if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
- fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
- for (i=0; i<tmp_target_list->num_of_regions; i++) {
- core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
- fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
- }
- fprintf(log_file,"\n");
- fflush(log_file);
- core_inter_tail[agent_id]->next = NULL;
- if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
- }
- its.it_value.tv_nsec = 500 * MS;// 750000000;
- selfopt_time_rem = its.it_value.tv_nsec;
- if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error7\n");
-
- if (state != IDLE_AGENT_WAITING_OFF) {
- printf("I am %d and about to set my alarm for selfopt check and my state before that was %d\n",node_id,state);
- state = IDLE_AGENT_WAITING_OFF;
- }
- }
- //printf("I come here\n");
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
- send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- //printf("I come here as well\n");
- } else if (core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){ //I am the requesting common node
- //printf("I am inside i am node with node id %d and sender_id %d %s\n",node_id,sender_id,fifo_name);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In DEBUG_IDAG_REQ_DDS Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- //kill(info->si_pid, SIG_ACK);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open6 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- read(fd_r, &num_of_agents, sizeof(int));
- printf("\n");
- printf("Number of agents in region = %d\n",num_of_agents);
- for (i=0; i<num_of_agents; i++){
- read(fd_r, &agent_id, sizeof(int));
- read(fd_r, &num_of_cores, sizeof(int));
- printf("Agent no %d is %d with %d cores\n",i,agent_id,num_of_cores);
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
- || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING){
- printf("I am %d and deadlock was prevented in sig_req_dds\n",node_id);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else {
- //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- printf("I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- fprintf(log_file,"I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- if (fd_r != -1) close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }*/
- void sig_REQ_CORES_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r=-1, sender_id, i, tmp_int, num_of_offers, j, non_zero_offers=0, cores_util;//num_of_idags, i, one_idag;
- char *fifo_name;
- float req_gain;
- region cur_reg;
- app req_app;
- offer one_offer;
- offer_list *tmp_offer_list, *tmp_offer_prev = NULL, *tmp_head, *chosen_node;
- inter_list *tmp_inter_list;
- offer_array off_arr;
- DDS_list *tmp_DDS;
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL && my_cores != NULL){ //I am the agent && (my_idag == -1 || (my_idag != -1 && my_app.num_of_cores != -1))
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 5 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
- //printf("open7 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- read(fd_r, &req_app, sizeof(app));
- //printf("I am node with id %d idag id %d with sender id %d and I am to investigate region C=%d r=%d and app %f\n",node_id,idag_id,sender_id,cur_reg.C,cur_reg.r,req_app.A);
-
- if (core_inter_head[sender_id] == NULL){
- core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_head[sender_id];
- } else {
- core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
- }
- core_inter_tail[sender_id]->type = REP_AGENT_REQ_CORES;
- read(fd_r, &off_arr.num_of_offers, sizeof(int));
- while (off_arr.num_of_offers > OLD_INIT_AREAS_NUM) {
- printf("I am %d kai fagame skoupidia apo ton %d kai einai %d\n",node_id,sender_id,off_arr.num_of_offers);
- read(fd_r, &off_arr.num_of_offers, sizeof(int));
- }
- off_arr.offer_arr = (offer *) malloc(off_arr.num_of_offers * sizeof(offer));
- core_inter_tail[sender_id]->next = NULL;
- for (i=0; i<off_arr.num_of_offers; i++) {
- read(fd_r, &cur_reg, sizeof(region));
-
- if (my_idag == -1) {
- off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
-
- tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
- off_arr.offer_arr[i].num_of_cores = tmp_int;
- my_stats.comp_effort++;
- if (tmp_int > 0) non_zero_offers++;
- cores_util = 0;
- for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
- cores_util += tmp_DDS->num_of_cores;
- if (cores_util == 0) off_arr.offer_arr[i].spd_loss = -2.0;
- else off_arr.offer_arr[i].spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);//0.0;
- } else if (my_cores != NULL && my_cores_count>2) {
- off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
-
- tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
- off_arr.offer_arr[i].num_of_cores = tmp_int;
- my_stats.comp_effort++;
- //printf("I am %d in i = %d and i offer %d cores\n",node_id,i,tmp_int);
- off_arr.offer_arr[i].spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-tmp_int);
- req_gain = Speedup(req_app,req_app.num_of_cores+tmp_int) - Speedup(req_app,req_app.num_of_cores);
- if (tmp_int > 0) {
- non_zero_offers++;
- fprintf(log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n",tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
- fflush(log_file);
- fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
- ,tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
- fflush(app_log_file);
- }
- } else {
- off_arr.offer_arr[i].num_of_cores = 0;
- off_arr.offer_arr[i].spd_loss = 0.0;
- }
- fprintf(log_file, "I offered %d %d cores: ",sender_id,off_arr.offer_arr[i].num_of_cores);
- for (j=0; j<off_arr.offer_arr[i].num_of_cores; j++)
- fprintf(log_file, "%d, ",off_arr.offer_arr[i].offered_cores[j]);
- fprintf(log_file, "\n");
- fflush(log_file);
- }
-
- fprintf(log_file,"non_zero_offers = %d\n",non_zero_offers);
- core_inter_tail[sender_id]->data.off_arr.num_of_offers = non_zero_offers;
- if (non_zero_offers > 0) core_inter_tail[sender_id]->data.off_arr.offer_arr = (offer *) malloc(non_zero_offers * sizeof(offer));
- else core_inter_tail[sender_id]->data.off_arr.offer_arr = NULL;
- j = 0;
- for (i=0; i<off_arr.num_of_offers; i++)
- if (off_arr.offer_arr[i].num_of_cores > 0) {
- core_inter_tail[sender_id]->data.off_arr.offer_arr[j].num_of_cores = off_arr.offer_arr[i].num_of_cores;
- core_inter_tail[sender_id]->data.off_arr.offer_arr[j].spd_loss = off_arr.offer_arr[i].spd_loss;
- core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores = (int *) malloc(off_arr.offer_arr[i].num_of_cores * sizeof(int));
- for (tmp_int=0; tmp_int<off_arr.offer_arr[i].num_of_cores; tmp_int++)
- core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores[tmp_int] = off_arr.offer_arr[i].offered_cores[tmp_int];
- j++;
- }
- if (core_inter_head[sender_id]->next == NULL) {
- kill(info->si_pid, SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- } else printf("Apparently not null interaction=%d\n",core_inter_head[sender_id]->type);
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- } else if (core_inter_head[sender_id] == NULL) {
- printf("I am %d and i have to reject req_cores from %d with null interaction\n",node_id,sender_id);
- fprintf(log_file,"i have to reject req_cores from %d. with null interaction\n",sender_id);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
- //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
- printf("I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && state != IDLE_AGENT_WAITING_OFF) {
- printf("I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == FAR_REQ_CORES && (state == IDLE_IDAG || (node_id == 0 && time_for_farman == -1))) {
- printf("I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == FAR_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES){
- //I am the requesting common node
-
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 6 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open8 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } else {
- fprintf(log_file, "I went through open type = %d\n",core_inter_head[sender_id]->type);
- fflush(log_file);
- }
-
- if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES) {//den erxontai ta far edw
- read(fd_r, &num_of_offers, sizeof(int));
- fprintf(log_file, "num_of_offers = %d\n",num_of_offers);
- fflush(log_file);
- if (num_of_offers > 0) {
- core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
- core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
- //core_inter_head[sender_id]->data.offer_accepted = -1;
- //one_offer.offered_cores = NULL;
- for (j=1; j<=num_of_offers; j++){
- read(fd_r, &one_offer.num_of_cores, sizeof(int));
- read(fd_r, &one_offer.spd_loss, sizeof(float));
-
- if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && init_man_offers == NULL) {
- init_man_offers = (offer_list *) malloc(sizeof(offer_list));
- chosen_node = init_man_offers;
- init_man_offers->next = NULL;
- } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && selfopt_man_offers == NULL){
- selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
- chosen_node = selfopt_man_offers;
- selfopt_man_offers->next = NULL;
- } else {
- tmp_offer_prev = NULL;
- if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
- tmp_offer_list = init_man_offers;
- if (one_offer.spd_loss < 0.0) {
- while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores && tmp_offer_list->off.spd_loss < 0.0){
- if (tmp_offer_list->off.num_of_cores > one_offer.num_of_cores) {
- tmp_offer_prev = tmp_offer_list;
- tmp_offer_list = tmp_offer_list->next;
- } else if (tmp_offer_list->off.spd_loss < one_offer.spd_loss) {
- tmp_offer_prev = tmp_offer_list;
- tmp_offer_list = tmp_offer_list->next;
- } else break;
- }
- /*while (tmp_offer_list != NULL && tmp_offer_list->off.spd_loss < one_offer.spd_loss && tmp_offer_list->off.spd_loss < 0.0){
- tmp_offer_prev = tmp_offer_list;
- tmp_offer_list = tmp_offer_list->next;
- }*/
- } else {
- while (tmp_offer_list != NULL && (tmp_offer_list->off.spd_loss < 0.0 || tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores)){
- tmp_offer_prev = tmp_offer_list;
- tmp_offer_list = tmp_offer_list->next;
- }
- }
- } else {
- tmp_offer_list = selfopt_man_offers;
- while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
- tmp_offer_prev = tmp_offer_list;
- tmp_offer_list = tmp_offer_list->next;
- }
- }
- if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
- tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
- tmp_offer_list = tmp_offer_prev->next;
- tmp_offer_list->next = NULL;
- chosen_node = tmp_offer_list;
- } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
- if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
- init_man_offers = (offer_list *) malloc(sizeof(offer_list));
- tmp_head = init_man_offers;
- } else {
- selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
- tmp_head = selfopt_man_offers;
- }
-
- chosen_node = tmp_head;
- tmp_head->next = tmp_offer_list;
- } else {
- tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
- tmp_offer_prev = tmp_offer_prev->next;
- chosen_node = tmp_offer_prev;
- tmp_offer_prev->next = tmp_offer_list;
- }
- }
-
- chosen_node->off.num_of_cores = one_offer.num_of_cores;
- chosen_node->off.spd_loss = one_offer.spd_loss;
- chosen_node->off.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
- for (i=0; i<one_offer.num_of_cores; i++)
- read(fd_r, &chosen_node->off.offered_cores[i], sizeof(int));
- /*printf("asdasNode %d is offering %d cores: ",sender_id,chosen_node->off.num_of_cores);
- for (i=0; i<chosen_node->off.num_of_cores; i++)
- printf(" %d,",chosen_node->off.offered_cores[i]);
- printf("\n");*/
- chosen_node->sender = sender_id;
- core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
- chosen_node->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: One node successfully added in list type=%d sender_id=%d\n",
- cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->type,sender_id);
- fflush(log_file);
- }
- core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
- } else { //no answer is required
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- }
- } else {
- read(fd_r, &num_of_offers, sizeof(int));
- fprintf(log_file, "num_of_offers is %d\n",num_of_offers);
- fflush(log_file);
- if (num_of_offers > 0) {
- core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
- core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
- core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
- read(fd_r, &one_offer.num_of_cores, sizeof(int));
- read(fd_r, &one_offer.spd_loss, sizeof(float));
- one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
- for (i=0; i<one_offer.num_of_cores; i++)
- read(fd_r, &one_offer.offered_cores[i], sizeof(int));
- fprintf(log_file, "%d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
- fflush(log_file);
- //core_inter_head[sender_id]->data.offer_accepted = -1;
- core_inter_head[sender_id]->data.offer_acc_array[1] = -1;
- fprintf(log_file, "a\n");
- fflush(log_file);
- if (far_man_offers == NULL){
- //fprintf(log_file, "b1\n");
- //fflush(log_file);
- far_man_offers = (offer_list *) malloc(sizeof(offer_list));
- far_man_offers_tail = far_man_offers;
- } else {
- //fprintf(log_file, "b2\n");
- //fflush(log_file);
- far_man_offers_tail->next = (offer_list *) malloc(sizeof(offer_list));
- far_man_offers_tail = far_man_offers_tail->next;
- }
- //fprintf(log_file, "c1\n");
- //fflush(log_file);
- far_man_offers_tail->off = one_offer;
- far_man_offers_tail->sender = sender_id;
- //fprintf(log_file, "c2\n");
- //fflush(log_file);
- far_man_offers_tail->answer = &core_inter_head[sender_id]->data.offer_acc_array[1];//offer_accepted;
- //fprintf(log_file, "c3\n");
- //fflush(log_file);
- far_man_offers_tail->next = NULL;
- far_list_count++;
- //fprintf(log_file, "c4\n");
- //fflush(log_file);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: One node successfully added in far_man_offers list %d\n",
- cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->data.offer_acc_array[0]);
- fflush(log_file);
- } else { //no answer is required
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- }
- }
-
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //sem_post(&node_sem[node_id]);
- ////sem_post(&node_sem[sender_id]);
- /*tmp_inter_list = core_inter_head[sender_id]->next;
- free(core_inter_head[sender_id]);
- core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_head[sender_id]->next = tmp_inter_list;
- core_inter_head[sender_id]->type = REP_AGENT_OFFER_SENT;
- core_inter_head[sender_id]->data.offer_accepted = -1;*/
- /*if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);*/
- } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING
- || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING){
- printf("I am %d and deadlock was prevented in req_cores by %d\n",node_id,sender_id);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) {
- if (core_inter_tail[sender_id] == NULL){
- printf("Malaka gamietai to core_inter_tail\n");
- core_inter_tail[sender_id] = NULL;
- }
- } else send_next_signal(core_inter_head[sender_id], sender_id);
- } else {
- //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- printf("I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- fprintf(log_file,"I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- //printf("We were fucked in sig_REQ_CORES_handler node_id = %d sender_id = %d interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- if (fd_r != -1) close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_REP_OFFERS_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, sender_id, offer_ans, i, one_core, j, old_cores_cnt;//num_of_idags, i, one_idag;
- core_list *tmp_cores, *tmp_cores_prev, *tmp_cores_list;
- inter_list *tmp_inter_list, *tmp_inter_prev;
- offer_list *tmp_offer_list;
- char *fifo_name;
-
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL)
- printf("fail!\n");
- else if (core_inter_head[sender_id]->type == AGENT_OFFER_SENT) {
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 7 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
- fprintf(log_file, "Num of offers is = %d\n",core_inter_head[sender_id]->data.off_arr.num_of_offers);
- fflush(log_file);
- //even if i am in a far req offer, my answer will be the first
- for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++) {
- read(fd_r, &offer_ans, sizeof(int));
- fprintf(log_file, "offer_ans = %d\n",offer_ans);
- fflush(log_file);
- //printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
- //printf("I am %d and i am getting bizarre answer = %d from %d with num of offers = %d\n",node_id,offer_ans,sender_id,core_inter_head[sender_id]->data.off_arr.num_of_offers);
- while (offer_ans != 0 && offer_ans != 1){
- printf("I am %d and i am getting bizarre answer = %d from %d\n",node_id,offer_ans,sender_id);
- read(fd_r, &offer_ans, sizeof(int));
- }
- if (offer_ans == 0 && my_cores != NULL) { // && my_cores != NULL
- for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
- for (tmp_cores = my_cores->next; tmp_cores != NULL; tmp_cores = tmp_cores->next)
- if (tmp_cores->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
- fprintf(log_file,"core id = %d offered_to = %d\n",tmp_cores->core_id,tmp_cores->offered_to);
- fflush(log_file);
- if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
- break;
- }
- if (app_state == APP_TERMINATED) {//app_terminated
- for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- if (tmp_cores_list->offered_to != -1) break;
- //if (tmp_cores_list != NULL && state ) state = AGENT_ZOMBIE;
- //else state = AGENT_ENDING;
- if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
- }
- /*tmp_cores = my_cores;
- while (tmp_cores != NULL){
- if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
- tmp_cores = tmp_cores->next;
- }*/
- } else if (offer_ans == 1) {//&& (!app_terminated || my_idag == -1) my_cores != NULL inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
- if (my_idag == -1){ //I am an idag
- //if (node_id == 0) printf("qwer asdf %d\n",DDS->num_of_cores);
- //if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
- DDS->num_of_cores -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
- //if (node_id == 0) printf("kariolares asdf %d\n",core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores);
- //printf("I did REM\n");
- } else {//I am common node
- //printf("I start REM here!!!!!\n");
- /*first i must get the remaining time from my cores, before i change my core list*/
-
- if (app_state != APP_TERMINATED) { //!app_terminated
- old_Speedup = my_Speedup;
- old_cores_cnt = my_cores_count;
- my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
- my_app.num_of_cores = my_cores_count;
- my_Speedup = Speedup(my_app, my_cores_count);
-
- fprintf(log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
- fflush(log_file);
- fprintf(app_log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
- fflush(app_log_file);
- if (app_state == RUNNING) {
- chk_rem_num = old_cores_cnt-1;
- chk_rem_count = 0;
- sum_rem_time = 0;
- app_state = RESIZING;
- for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
- one_core = tmp_cores_list->core_id;
- if (core_inter_head[one_core] == NULL){
- core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_core] = core_inter_head[one_core];
- } else {
- core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_core] = core_inter_tail[one_core]->next;
- }
- core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
- core_inter_tail[one_core]->next = NULL;
-
- if (core_inter_head[one_core]->next == NULL) {
- kill(pid_num[one_core], SIG_CHECK_REM_TIME);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_core);
- } else printf("I am %d and I am doing smth else with my working node %d in send SIG_CHECK_REM_TIME in rep offers type = %d\n",
- node_id,one_core,core_inter_head[one_core]->type);
- }
- } //else {
- } else {
- my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
- my_app.num_of_cores = my_cores_count;
- fprintf(log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
- fflush(log_file);
- fprintf(app_log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
- fflush(app_log_file);
- }
- if (core_inter_head[my_idag] == NULL){
- core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_idag] = core_inter_head[my_idag];
- } else {
- core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
- }
- core_inter_tail[my_idag]->type = IDAG_REM_CORES_DDS;
- core_inter_tail[my_idag]->data.app_cores = (int *)malloc((core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores+1)*sizeof(int));
- core_inter_tail[my_idag]->data.app_cores[0] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
- for(i=1; i<=core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++) {
- one_core = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
-
- if (app_state != APP_TERMINATED) {
- tmp_inter_prev = core_inter_head[one_core];
- tmp_inter_list = core_inter_head[one_core]->next;
- } else {
- tmp_inter_prev = NULL;
- tmp_inter_list = core_inter_head[one_core];
- }
- //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
- while (tmp_inter_list != NULL)
- if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
- || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
- //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
-
- fprintf(log_file, "Removing in rem offers one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
- fflush(log_file);
- if (tmp_inter_prev == NULL) {
- core_inter_head[one_core] = core_inter_head[one_core]->next;
- free(tmp_inter_list);
- tmp_inter_list = core_inter_head[one_core];
- } else {
- tmp_inter_prev->next = tmp_inter_list->next;
- if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
- free(tmp_inter_list);
- tmp_inter_list = tmp_inter_prev->next;
- }
- } else {
- tmp_inter_prev = tmp_inter_list;
- tmp_inter_list = tmp_inter_list->next;
- }
-
- if (app_state != APP_TERMINATED) {
- if (core_inter_head[one_core] == NULL) {
- fprintf(log_file,"No interaction with %d. Theoritically impossible\n",one_core);
- fflush(log_file);
- } else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {
- fprintf(log_file,"I offered my new core %d. I will clear the interaction\n",one_core);
- fflush(log_file);
- tmp_inter_list = core_inter_head[one_core];
- core_inter_head[one_core] = core_inter_head[one_core]->next;
- if (core_inter_head[one_core] == NULL) core_inter_tail[one_core] = NULL;
- else send_next_signal(core_inter_head[one_core], one_core);
- free(tmp_inter_list);
- } else if (core_inter_head[one_core]->type == INIT_WORK_NODE || core_inter_head[one_core]->type == APPOINT_WORK_NODE) {
- fprintf(log_file,"Invalidating %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
- fflush(log_file);
- core_inter_head[one_core]->data.work_time = -1;
- core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_core] = core_inter_tail[one_core]->next;
-
- core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
- core_inter_tail[one_core]->next = NULL;
- } else if (core_inter_head[one_core]->type == APPOINT_WORK_NODE_PENDING) {
- fprintf(log_file,"Everything ok %d.\n",one_core);
- fflush(log_file);
- core_inter_head[one_core]->type = REMOVED_NODE_REM_TIME;
- } else {
- fprintf(log_file,"Another interaction with %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
- fflush(log_file);
- core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_core] = core_inter_tail[one_core]->next;
-
- core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
- core_inter_tail[one_core]->next = NULL;
- }
- }
- tmp_cores = my_cores;
- tmp_cores_prev = NULL;
- while (tmp_cores != NULL && tmp_cores->core_id != core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1]){
- tmp_cores_prev = tmp_cores;
- tmp_cores = tmp_cores->next;
- }
- if (tmp_cores == NULL) printf("strangely offered core is not in my_cores list\n");
- else {
- if (tmp_cores_prev == NULL) {
- printf("i fucking offered my agent core!!!\n");
- my_cores = my_cores->next;
- } else if (tmp_cores == my_cores_tail){
- my_cores_tail = tmp_cores_prev;
- my_cores_tail->next = NULL;
- } else tmp_cores_prev->next = tmp_cores->next;
-
- free(tmp_cores);
- }
-
- core_inter_tail[my_idag]->data.app_cores[i] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
- }
- core_inter_tail[my_idag]->next = NULL;
- if (core_inter_head[my_idag]->next == NULL) {
- kill(pid_num[my_idag], SIG_REM_CORES_DDS);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,my_idag);
- } else printf("I did not send rem signal!\n");
- //printf("At least i am coming here!!!!!\n");
-
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(app_log_file, "[%d:%d:%d]: Removal ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
- fprintf(app_log_file, "my cores are:");
- 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);
- fprintf(app_log_file, " %d", tmp_cores_list->core_id);
- //printf(" %d",tmp_cores_list->core_id);
- }
- //printf("\n");
- //fprintf(log_file, "\n");
- //fflush(log_file);
- fprintf(app_log_file, "\n");
- fflush(app_log_file);
- if (app_state == APP_TERMINATED) {//app_terminated
- for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- if (tmp_cores_list->offered_to != -1) break;
-
- if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
- }
- }
- } else printf("I am %d and my Answer from %d different than 0 or 1 and is %d!!\n",node_id,sender_id,offer_ans);
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //sem_post(&node_sem[node_id]);
- //sem_post(&node_sem[sender_id]);
- } else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 8 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } else {
- fprintf(log_file, "I went through open\n");
- fflush(log_file);
- }
- if (far_man_offers->sender == node_id) {
- read(fd_r, &offer_ans, sizeof(int));
- printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
- if (offer_ans == 0) {
- tmp_cores = my_cores;
- while (tmp_cores != NULL){
- if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
- tmp_cores = tmp_cores->next;
- }
- } else if (offer_ans == 1) {//inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
- if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
- DDS->num_of_cores -= core_inter_head[sender_id]->data.my_offer.num_of_cores;
- } else printf("I am %d Answer different than 0 or 1 in far from %d!!\n",node_id,sender_id);
- //if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {//FAR_REQ_OFFER
- /*printf("poutanares\n");
- if (far_man_offers == NULL) printf("kai poytanes kai karioles\n");
- else printf("gamw ths papias\n");*/
- tmp_offer_list = far_man_offers;
- far_man_offers = far_man_offers->next;
- free(tmp_offer_list);
- }
- while (far_man_offers != NULL){
- //printf("kai edw ftamw\n");
- read(fd_r, &offer_ans, sizeof(int));
- printf("I am node %d and far offer answer for node %d is %d\n",node_id,far_man_offers->sender,offer_ans);
- if (offer_ans == 0 || offer_ans == 1){
- *far_man_offers->answer = offer_ans;
- if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
- kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
- core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,far_man_offers->sender);
- } else printf("Apparently lists are poutana ola\n");
- } else printf("Far Answer different than 0 or 1!!\n");
-
- tmp_offer_list = far_man_offers;
- far_man_offers = far_man_offers->next;
- free(tmp_offer_list);
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //sem_post(&node_sem[node_id]);
- //sem_post(&node_sem[sender_id]);
- far_req_app.A=-1.0;
- far_req_app.var=-1.0;
- far_req_app.num_of_cores=-1;
- far_req_or_sender = -1;
- far_reg.C = -1;
- far_reg.r = -1;
- } else printf("I am %d and fail 2\n",node_id);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_INIT_AGENT_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, i, tmp, sender_id;
- char *fifo_name;
- core_list *tmp_core;
- signals_disable();
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 9 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,get_id_from_pid(info->si_pid));
- //printf("open10 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } else {
- fprintf(log_file, "I went through open\n");
- fflush(log_file);
- }
-
- if (my_app.num_of_cores != -1) { //Very very very important!!!
- printf ("I am already managing an app!!! Fail!!!!\n");
- while (my_cores != NULL){
- tmp_core = my_cores;
- my_cores = my_cores->next;
- free(tmp_core);
- }
- my_cores_tail = NULL;
- my_cores_count = 0;
- }
- read(fd_r, &my_app, sizeof(app));
- my_cores_count = my_app.num_of_cores;//+1;
- if (my_cores == NULL) {
- my_cores = (core_list *) malloc(sizeof(core_list));
- my_cores_tail = my_cores;
- } else {
- printf("My cores still not fucking null!!\n");
- my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
- my_cores_tail = my_cores_tail->next;
- }
- my_cores_tail->core_id = node_id;
- my_cores_tail->offered_to = -1;
- my_cores_tail->next = NULL;
- //I want myself to be first in my_cores list
- for (i=0; i<my_app.num_of_cores; i++){
- read(fd_r, &tmp, sizeof(int));
- if (tmp != node_id){
- my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
- my_cores_tail = my_cores_tail->next;
- //read(fd_r, &my_cores_tail->core_id, sizeof(int));
- my_cores_tail->core_id = tmp;
- my_cores_tail->offered_to = -1;
- my_cores_tail->next = NULL;
- }
- }
- read(fd_r, &my_app_times[0], sizeof(my_time_stamp));
- read(fd_r, &my_app_times[1], sizeof(my_time_stamp));
- ////sem_post(&node_sem[sender_id]);
- printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
- ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
- my_Speedup = Speedup(my_app, my_app.num_of_cores);
- if (core_inter_head[my_idag] == NULL){
- core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_idag] = core_inter_head[my_idag];
- } else {
- core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
- }
- core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
- //core_inter_tail[my_idag]->data.my_offer = core_inter_head[sender_id]->data.my_offer;
- core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
- core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
- //for(i=1; i<=my_cores_count; i++)
- // core_inter_tail[my_idag]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
- tmp_core = my_cores;
- i=1;
- while (tmp_core != NULL){
- core_inter_tail[my_idag]->data.app_cores[i] = tmp_core->core_id;
- tmp_core = tmp_core->next;
- i++;
- }
- core_inter_tail[my_idag]->next = NULL;
- if (core_inter_head[my_idag]->next == NULL) {
- kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,my_idag);
- } else printf("I am %d and i didn't call add!! with interaction %d\n",node_id,core_inter_head[my_idag]->type);
- if (my_agent != -1) {
- printf("I am %d and i do this agent switch\n",node_id);
- //if (state != IDLE_CORE) {
- if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
- if (core_inter_head[my_agent] == NULL){
- core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_agent] = core_inter_head[my_agent];
- } else {
- core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
- }
- core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
- core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
- core_inter_tail[my_agent]->next = NULL;
- }
-
- upper_work_bound = 0;
- my_agent = -1;
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- printf("Init ok!! my_cores_count = %d app_cores=%d app_id=%d\n",my_cores_count,my_app.num_of_cores,my_app.id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- 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);
- fprintf(log_file, "my cores are:");
- for (tmp_core=my_cores; tmp_core!=NULL; tmp_core=tmp_core->next) fprintf(log_file, " %d", tmp_core->core_id);
- fprintf(log_file, "\n");
- printf("I am %d and about to do an selfopt agent with my state before change = %d\n",node_id,state);
- if (state == IDLE_INIT_MAN || state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
- fprintf(log_file,"I have pending init\n");
- //state = AGENT_INIT_STATE_INIT_INTERRUPTED;
- pending_state = state;
- state = AGENT_INIT_STATE;
- } else state = AGENT_INIT_STATE;
- close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- //an mou erthei to shma apo allon idag prepei apla na valw, an mou erthei apo common node tote prepei na upologisw
- void sig_ADD_CORES_DDS_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, i, is_sender_idag, sender_id, j, new_agent_id;
- int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
- int *tmp_list, tmp_cnt, tmp_idag;
- char *fifo_name;
- core_list *tmp_cores;//, *tmp_cores_list;
- DDS_list *tmp_DDS;
- signals_disable();
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 10 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open11 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } else {
- fprintf(log_file, "I went through open\n");
- fflush(log_file);
- }
- is_sender_idag = 0;
- for (i=0; i<num_idags; i++)
- if (idag_id_arr[i] == sender_id){
- is_sender_idag = 1;
- break;
- }
- //printf("I am in add with is_sender_idag = %d\n",is_sender_idag);
- if (is_sender_idag == 0){
- read(fd_r, &nodes_cnt, sizeof(int));
- while (nodes_cnt <=0){
- printf("i am %d and i read %d in nodes_cnt from %d\n",node_id,nodes_cnt,sender_id);
- read(fd_r, &nodes_cnt, sizeof(int));
- }
- nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
- for (i=0; i<nodes_cnt; i++)
- read(fd_r, &nodes_to_process[i], sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
- printf("I am %d in add cores with sender %d and nodes_cnt = %d\n",node_id,sender_id,nodes_cnt);
- while (processed_cnt < nodes_cnt){
-
- tmp_cnt = 0;
- tmp_idag = -1;
- for (i=0; i<nodes_cnt; i++){
- if (processed_cnt == nodes_cnt) break;
- if (nodes_to_process[i] == -1) continue;
- else {
- if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
- if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
- else {
- tmp_list[tmp_cnt++] = nodes_to_process[i];
- nodes_to_process[i] = -1;
- processed_cnt++;
- }
- }
- }
- if (tmp_idag != node_id){
- if (core_inter_head[tmp_idag] == NULL){
- core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
- } else {
- printf("Adding to DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
- core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
- }
- printf("In ADD tmp_idag = %d tmp_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,sender_id,node_id);
- core_inter_tail[tmp_idag]->type = IDAG_ADD_CORES_DDS;
- core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
- core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
- core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
- for (j=1; j<= tmp_cnt; j++)
- core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
-
- core_inter_tail[tmp_idag]->next = NULL;
- if (core_inter_head[tmp_idag]->next == NULL) {
- kill(pid_num[tmp_idag], SIG_ADD_CORES_DDS);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,tmp_idag);
- }
- } else {
- printf("In ADD same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
- new_agent_id = sender_id;
- for (i=0; i<tmp_cnt; i++){//nodes_cnt
- fprintf(log_file,"I am importing node %d\n",tmp_list[i]);
- fflush(log_file);
- tmp_cores = my_cores;
-
- while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
- tmp_cores = tmp_cores->next;
-
- if (tmp_cores->offered_to == -1) {
- fprintf(log_file,"Node %d was offered to nobody\n",tmp_list[i]);
- fflush(log_file);
- DDS->num_of_cores--;
- tmp_cores->offered_to = new_agent_id;
- } else if (tmp_cores->offered_to != new_agent_id) {
- fprintf(log_file,"Node %d is offered to %d\n",tmp_list[i],tmp_cores->offered_to);
- fflush(log_file);
- tmp_cores->offered_to = new_agent_id;
- }
- }
- tmp_DDS = DDS;
- while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
- tmp_DDS = tmp_DDS->next;
- if (tmp_DDS != NULL)
- tmp_DDS->num_of_cores += nodes_cnt;
- else {
- //printf("I am in here DDSing nodes_cnt=%d\n",nodes_cnt);
- DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
- DDS_tail = DDS_tail->next;
- DDS_tail->agent_id = new_agent_id;
- DDS_tail->num_of_cores = nodes_cnt;
- DDS_tail->next = NULL;
- DDS_count++;
- }
- }
- }
- } else {
- read(fd_r, &nodes_cnt, sizeof(int));
- nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
- for (i=0; i<nodes_cnt; i++)
- read(fd_r, &nodes_to_process[i], sizeof(int));
- read(fd_r, &new_agent_id, sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- //printf("new agent id = %d\n",new_agent_id);
- for (i=0; i<nodes_cnt; i++){
- //printf("Node %d is %d\n",i,nodes_to_process[i]);
- fprintf(log_file,"I am importing node %d\n",nodes_to_process[i]);
- fflush(log_file);
- tmp_cores = my_cores;
- while (tmp_cores->core_id != nodes_to_process[i])
- tmp_cores = tmp_cores->next;
-
- if (tmp_cores->offered_to == -1) {
- fprintf(log_file,"Node %d was offered to nobody\n",nodes_to_process[i]);
- fflush(log_file);
- DDS->num_of_cores--;
- tmp_cores->offered_to = new_agent_id;
- } else if (tmp_cores->offered_to != new_agent_id) {
- fprintf(log_file,"Node %d is offered to %d\n",nodes_to_process[i],tmp_cores->offered_to);
- fflush(log_file);
- tmp_cores->offered_to = new_agent_id;
- }
- //tmp_cores->offered_to = new_agent_id;
- }
- tmp_DDS = DDS;
- while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
- tmp_DDS = tmp_DDS->next;
- if (tmp_DDS != NULL)
- tmp_DDS->num_of_cores += nodes_cnt;
- else {
- if (DDS_count == 1 && DDS != DDS_tail) printf("I am %d kai ta hpiame asxhma!\n",node_id);
- DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
- DDS_tail = DDS_tail->next;
- DDS_tail->agent_id = new_agent_id;
- DDS_tail->num_of_cores = nodes_cnt;
- DDS_tail->next = NULL;
- DDS_count++;
- //printf("I did this with node_id = %d, new_agent_id = %d\n",node_id,new_agent_id);
- }
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- // if (tmp_cores_list->offered_to != -1) my_stats.cores_utilized++;
-
- my_stats.times_accessed++;
- printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
- printf("Number of agents in region = %d\n",DDS_count);
- printf("Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
- i=1;
- for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
- printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
- my_stats.cores_utilized += tmp_DDS->num_of_cores;
- i++;
- }
-
- close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_REM_CORES_DDS_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, i, is_sender_idag, sender_id, j, new_agent_id;
- int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
- int *tmp_list, tmp_cnt, tmp_idag;
- char *fifo_name;
- DDS_list *tmp_DDS,*tmp_DDS_prev;
- signals_disable();
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 11 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("open11 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } else {
- fprintf(log_file, "I went through open\n");
- fflush(log_file);
- }
- is_sender_idag = 0;
- for (i=0; i<num_idags; i++)
- if (idag_id_arr[i] == sender_id){
- is_sender_idag = 1;
- break;
- }
- //printf("I am in rem with is_sender_idag = %d\n",is_sender_idag);
- if (is_sender_idag == 0){
- read(fd_r, &nodes_cnt, sizeof(int));
- nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
- for (i=0; i<nodes_cnt; i++)
- read(fd_r, &nodes_to_process[i], sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
- while (processed_cnt < nodes_cnt){
-
- tmp_cnt = 0;
- tmp_idag = -1;
- for (i=0; i<nodes_cnt; i++){
- if (processed_cnt == nodes_cnt) break;
- if (nodes_to_process[i] == -1) continue;
- else {
- if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
- if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
- else {
- tmp_list[tmp_cnt++] = nodes_to_process[i];
- nodes_to_process[i] = -1;
- processed_cnt++;
- }
- }
- }
- if (tmp_idag != node_id){
- if (core_inter_head[tmp_idag] == NULL){
- core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
- } else {
- printf("Removing from DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
- core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
- }
- //printf("In REM tmp_idag = %d tmp_cnt = %d sender_id = %d idag_id = %d\n",tmp_idag,tmp_cnt,sender_id,idag_id);
- core_inter_tail[tmp_idag]->type = IDAG_REM_CORES_DDS;
- core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
- core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
- core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
- for (j=1; j<= tmp_cnt; j++)
- core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
-
- core_inter_tail[tmp_idag]->next = NULL;
- if (core_inter_head[tmp_idag]->next == NULL) {
- kill(pid_num[tmp_idag], SIG_REM_CORES_DDS);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,tmp_idag);
- }
- } else {
- printf("In REM node_id = %d same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d \n",node_id,tmp_idag,tmp_cnt,nodes_cnt,sender_id);
- new_agent_id = sender_id;
-
- tmp_DDS = DDS;
- tmp_DDS_prev = NULL;
- while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
- tmp_DDS_prev = tmp_DDS;
- tmp_DDS = tmp_DDS->next;
- }
- if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
- else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
- else {
- tmp_DDS->num_of_cores -= nodes_cnt;
- if (tmp_DDS->num_of_cores == 0){
- DDS_count--;
- if (tmp_DDS == DDS_tail){
- DDS_tail = tmp_DDS_prev;
- DDS_tail->next = NULL;
- } else tmp_DDS_prev->next = tmp_DDS->next;
- free(tmp_DDS);
- }
- }
- }
- }
- } else { //den afairw tous purhnes apo thn core list giati mporei na exoun hdh ginei add
- read(fd_r, &nodes_cnt, sizeof(int));
- nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
- for (i=0; i<nodes_cnt; i++)
- read(fd_r, &nodes_to_process[i], sizeof(int));
- read(fd_r, &new_agent_id, sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- //printf("I am in the second rem with new agent id = %d and nodes_cnt = %d\n",new_agent_id,nodes_cnt);
- tmp_DDS = DDS;
- tmp_DDS_prev = NULL;
- while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
- tmp_DDS_prev = tmp_DDS;
- tmp_DDS = tmp_DDS->next;
- }
- if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
- else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
- else {
- tmp_DDS->num_of_cores -= nodes_cnt;
- if (tmp_DDS->num_of_cores == 0){
- DDS_count--;
- if (tmp_DDS == DDS_tail){
- DDS_tail = tmp_DDS_prev;
- DDS_tail->next = NULL;
- } else tmp_DDS_prev->next = tmp_DDS->next;
- free(tmp_DDS);
- }
- }
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- printf("I am %d Removing ended well! with sender_id=%d!\n",node_id,sender_id);
- printf("Number of agents in region = %d\n",DDS_count);
- i=0;
- for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
- printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
- i++;
- }
- close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_FAR_REQ_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r=-1, sender_id, i, one_idag, *idags_in_reg, agent_id, num_of_offers, j, far_req_man, one_far_sender, cores_util;//num_of_idags,
- char *fifo_name;
- region cur_reg;
- offer one_offer;
- inter_list *tmp_inter_list, *tmp_inter_prev;
- DDS_list *tmp_DDS;
- offer_list *tmp_offer_list, *tmp_offer_prev;
- app one_app;
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_FAR_REQ_handler with sender=%d and signo=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,signo,state);
- fflush(log_file);
- //I am the node that is far.
- //I keep that as NULL because this must be the way, or else why is he sending me smth if we are allready communicating?
- if (core_inter_head[sender_id] == NULL && signo == SIG_FAR_REQ && my_idag != -1) {
- printf("I am %d and i think i cought a stray far_manager reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray far_manager reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- } else if (core_inter_head[sender_id] == NULL && (signo == SIG_INIT_FAR_REQ || (signo == SIG_FAR_REQ && my_idag == -1))) {
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 12 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
- //printf("open12 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- if (signo == SIG_INIT_FAR_REQ) {
- //far_req_or_sender = sender_id;
- one_far_sender = sender_id;
-
- //read(fd_r, &far_req_app, sizeof(app));
- read(fd_r, &one_app, sizeof(app));
- read(fd_r, &cur_reg, sizeof(region));
- if (core_inter_head[one_far_sender] == NULL){
- core_inter_head[one_far_sender] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_far_sender] = core_inter_head[one_far_sender];
- } else {
- core_inter_tail[one_far_sender]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_far_sender] = core_inter_tail[one_far_sender]->next;
- }
- core_inter_tail[one_far_sender]->type = REP_FAR_INIT_REQ;
- if (my_idag != -1) {
- far_req_man = my_idag;
- if (core_inter_head[far_req_man] == NULL){
- core_inter_head[far_req_man] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[far_req_man] = core_inter_head[far_req_man];
- } else {
- core_inter_tail[far_req_man]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[far_req_man] = core_inter_tail[far_req_man]->next;
- }
- //far_req_or_sender = one_far_sender;
- //far_req_app = one_app;
- core_inter_tail[far_req_man]->type = FAR_REQ_MAN_APPOINT_PENDING;
- core_inter_tail[far_req_man]->data.far_req.orig_sender = one_far_sender;
- core_inter_tail[far_req_man]->data.far_req.far_app = one_app;
- core_inter_tail[far_req_man]->data.far_req.reg = cur_reg;
- core_inter_tail[far_req_man]->next = NULL;
- //core_inter_tail[far_req_or_sender]->data.far_req_man = my_idag;
- } else {
- far_req_man = node_id;
- if (far_req_or_sender == -1) {
- far_req_or_sender = one_far_sender;
- far_req_app = one_app;
- }
- }
- //core_inter_tail[one_far_sender]->data.far_req_man = node_id;
- core_inter_tail[one_far_sender]->data.far_req_man = far_req_man;
- core_inter_tail[one_far_sender]->next = NULL;
- if (core_inter_head[one_far_sender]->next == NULL) {
- kill(pid_num[one_far_sender], SIG_INIT_FAR_REQ);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_far_sender);
- } else printf("I am %d and did not send %d one_far_sender init_far_req in here. interaction = %d\n",node_id,one_far_sender,core_inter_head[one_far_sender]->type);
- } else { //SIG_FAR_REQ
- //read(fd_r, &one_far_sender, sizeof(int));
- //read(fd_r, &far_req_app, sizeof(app));
- read(fd_r, &one_far_sender, sizeof(int));
- read(fd_r, &one_app, sizeof(app));
- read(fd_r, &cur_reg, sizeof(region));
- while (cur_reg.C > X_max * Y_max){
- printf("I am %d kai mas taizei malakies o %d\n",node_id,sender_id);
- read(fd_r, &cur_reg, sizeof(region));
- }
- if (far_req_or_sender == -1) {
- far_req_or_sender = one_far_sender;
- far_req_app = one_app;
- far_reg = cur_reg;
- //printf("I am %d and i come here far_req_or_sender = %d\n",node_id,far_req_or_sender);
- } //else printf("I am %d and karioles far_req_or_sender = %d one_far_sender = %d\n",node_id,far_req_or_sender,one_far_sender);
-
- fprintf(log_file,"one_far_sender=%d far_req_or_sender = %d\n",one_far_sender,far_req_or_sender);
- fflush(log_file);
- }
- if (signo == SIG_FAR_REQ || (signo == SIG_INIT_FAR_REQ && far_req_man == node_id)) {
- if (far_req_or_sender == one_far_sender && far_reg.C == cur_reg.C && far_reg.r == cur_reg.r) {// far_man_offers == NULL
- printf("I am node with id %d I am to far check-manage region C=%d r=%d for %d\n",node_id,cur_reg.C,cur_reg.r,far_req_or_sender);
- fprintf(log_file,"I am node with id %d I am to far check-manage region C=%d r=%d for %d\n",node_id,cur_reg.C,cur_reg.r,far_req_or_sender);
- fflush(log_file);
- //my offer
- if (far_man_offers == NULL){
- far_list_count = 1;
- far_man_offers = (offer_list *) malloc(sizeof(offer_list));
- far_man_offers_tail = far_man_offers;
- } else printf ("I am %d Far man offers list not NULL far_req_or_sender = %d one_far_sender = %d\n",node_id,far_req_or_sender,one_far_sender);
- far_man_offers_tail->sender = node_id;
- far_man_offers_tail->off.offered_cores = (int *) malloc(my_cores_count*sizeof(int));
- far_man_offers_tail->off.num_of_cores = offer_cores(my_cores, far_req_app, cur_reg, far_man_offers_tail->off.offered_cores, far_req_or_sender);
- my_stats.comp_effort++;
- //if (my_idag == -1)
- //far_man_offers_tail->off.spd_loss = 0.0;
- cores_util = 0;
- for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
- cores_util += tmp_DDS->num_of_cores;
- if (cores_util == 0) far_man_offers_tail->off.spd_loss = -2.0;
- else far_man_offers_tail->off.spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);
- //else far_man_offers_tail->off.spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-far_man_offers_tail->off.num_of_cores);
-
- far_man_offers_tail->sender = node_id;
- far_man_offers_tail->next = NULL;
- //far_list_count++;
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I added my offer in far_man_offers list num_of_cores=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,far_man_offers_tail->off.num_of_cores);
- fflush(log_file);
- if (far_man_offers_tail->off.num_of_cores == 0) {
- free(far_man_offers_tail->off.offered_cores);
- free(far_man_offers);
- far_man_offers = NULL;
- far_man_offers_tail = NULL;
- far_list_count = 0;
- }
- idags_in_reg = (int *) malloc((num_idags+1)*sizeof(int));
- get_reg_idags(cur_reg, idags_in_reg);
- for (i=0; i<num_idags; i++)
- if (idags_in_reg[i] && idag_id_arr[i] != node_id){
- one_idag = idag_id_arr[i];
-
- if (core_inter_head[one_idag] == NULL){
- core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_idag] = core_inter_head[one_idag];
- } else {
- core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
- }
- //if (signo == SIG_FAR_REQ)
- //else core_inter_tail[one_idag]->type = FAR_INIT_IDAG_REQ_DDS;
- core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
- core_inter_tail[one_idag]->data.reg = cur_reg;
- core_inter_tail[one_idag]->next = NULL;
- if (core_inter_head[one_idag]->next == NULL) {
- kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_idag);
- } else printf("I am %d and i want far dds from %d but interaction is %d\n",node_id,one_idag,core_inter_head[one_idag]->type);
- }
- //printf("uiou 0 qw %d num_of_idags = %d idag_id=%d\n",idags_in_reg[idag_id],idags_in_reg[num_idags],idag_id);
- //for (i=0; i<num_idags; i++)
- //printf("idag i=%d node_num = %d idags_in_reg = %d\n",i,idag_id_arr[i],idags_in_reg[i]);
-
- //asking offers from other agents in my DDS
- tmp_DDS = DDS->next;
- if (tmp_DDS != NULL) {
- while (tmp_DDS != NULL){
- agent_id = tmp_DDS->agent_id;
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
-
- core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
- core_inter_tail[agent_id]->data.reg = cur_reg;//core_inter_head[sender_id]->data.reg;
- core_inter_tail[agent_id]->next = NULL;
-
- //if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- //} else printf("!!!!! I am %d and in sending local-far offer to %d i have interaction %d\n",node_id,agent_id,core_inter_head[agent_id]->type);
- } else {
- printf("I did not send I am %d and in sending local-far offer to %d i have interaction %d\n",node_id,agent_id,core_inter_head[agent_id]->type);
- //core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- //core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
-
- tmp_DDS = tmp_DDS->next;
- }
-
- if (state != IDLE_CHK_APP_FILE) {
- its.it_value.tv_nsec = 250 * MS;
- if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error2\n");
- state = IDLE_FAR_MAN;
- } else time_for_farman = 25;
- } else if (idags_in_reg[num_idags] == 1) {//i am the only idag in region with no agents
- if (core_inter_head[far_req_or_sender] == NULL){
- core_inter_head[far_req_or_sender] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[far_req_or_sender] = core_inter_head[far_req_or_sender];
- } else {
- core_inter_tail[far_req_or_sender]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[far_req_or_sender] = core_inter_tail[far_req_or_sender]->next;
- }
- core_inter_tail[far_req_or_sender]->type = FAR_REQ_OFFER;
- if (far_man_offers != NULL) core_inter_tail[far_req_or_sender]->data.my_offer = far_man_offers->off;
- else core_inter_tail[far_req_or_sender]->data.my_offer.num_of_cores = 0;
- core_inter_tail[far_req_or_sender]->next = NULL;
- if (core_inter_head[far_req_or_sender]->next == NULL) {
- kill(pid_num[far_req_or_sender],SIG_FAR_REQ);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,far_req_or_sender);
- } else printf("I am %d and i am doing smth else with far_req_or_sender %d interaction %d\n",node_id,far_req_or_sender,core_inter_head[far_req_or_sender]->type);
- } else {
- if (state != IDLE_CHK_APP_FILE) {
- its.it_value.tv_nsec = 250 * MS;
- if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error3\n");
- state = IDLE_FAR_MAN;
- } else time_for_farman = 25;
- }
- } else if (far_req_or_sender != one_far_sender) {
- fprintf(log_file,"I am node with id %d and I am allready far managing %d so i am going to send %d only my offer\n",node_id,far_req_or_sender,one_far_sender);
- fflush(log_file);
- if (core_inter_head[one_far_sender] == NULL){
- core_inter_head[one_far_sender] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[one_far_sender] = core_inter_head[one_far_sender];
-
- core_inter_tail[one_far_sender]->type = REP_AGENT_REQ_CORES;
-
- core_inter_tail[one_far_sender]->data.off_arr.offer_arr = (offer *) malloc(sizeof(offer));
- core_inter_tail[one_far_sender]->next = NULL;
- core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
- j = offer_cores(my_cores, one_app, cur_reg, core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].offered_cores, one_far_sender);
- /*if (j > 0) {
- core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].num_of_cores = j;
- core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 1;
- } else {
- core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 0;
- free(core_inter_tail[one_far_sender]->data.off_arr.offer_arr);
- core_inter_tail[one_far_sender]->data.off_arr.offer_arr = NULL;
- }*/
-
- my_stats.comp_effort++;
- core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].num_of_cores = j;
- //gia na einai sumvato me to far_list_count=0 se mhdenikh prosfora
- if (j == 0) core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 0;
- else core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 1;
- //core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = 0.0;
- cores_util = 0;
- for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
- cores_util += tmp_DDS->num_of_cores;
- if (cores_util == 0) core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = -2.0;
- else core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);
- i=0;
- fprintf(log_file, "I offered in far only %d %d cores: ",one_far_sender,core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].num_of_cores);
- for (j=0; j<core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].num_of_cores; j++)
- fprintf(log_file, "%d, ",core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].offered_cores[j]);
- fprintf(log_file, "\n");
- fflush(log_file);
-
- //get_reg_idags(cur_reg, core_inter_tail[one_far_sender]->data.idags_in_reg);
- //if (core_inter_head[one_far_sender]->next == NULL) {
- kill(pid_num[one_far_sender], SIG_FAR_REQ);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_far_sender);
- //} else printf("i am %d and Apparently not null interaction in far only=%d\n",node_id,core_inter_head[one_far_sender]->type);
- } else {
- fprintf(log_file,"den to evala I am %d with one_far_sender %d in only my offer and interaction is %d\n",node_id,one_far_sender,core_inter_head[one_far_sender]->type);
- fflush(log_file);
- //core_inter_tail[one_far_sender]->next = (inter_list *) malloc(sizeof(inter_list));
- //core_inter_tail[one_far_sender] = core_inter_tail[one_far_sender]->next;
- }
- //far_man_agent_count = DDS_count;
- //printf("uioy 1 far_list_count = %d far_man_agent_count=%d\n",far_list_count,far_man_agent_count);
- } else printf("I am node with id %d and I am allready far managing %d so i am going to abort re managing him\n",node_id,far_req_or_sender);
- } else printf("I am node with id %d and I am to far check region C=%d r=%d with sender %d\n",node_id,cur_reg.C,cur_reg.r,sender_id);
-
-
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- } else if (core_inter_head[sender_id]->type == FAR_INIT_REQ && signo == SIG_INIT_FAR_REQ) {//in original manager change who the far manager is
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 13 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
- //printf("open13 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
- read(fd_r, &agent_id, sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
- if (tmp_inter_list->type == FAR_REQ_MAN) break;
-
- if (tmp_inter_list == NULL) {
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
- core_inter_tail[agent_id]->type = FAR_REQ_MAN;
- core_inter_tail[agent_id]->data.far_req_man = sender_id;
- core_inter_tail[agent_id]->next = NULL;
- printf("I am node with id %d and my far request manager is %d\n",node_id,agent_id);
- fprintf(log_file,"I am node with id %d and my far request manager is %d\n",node_id,agent_id);
- fflush(log_file);
- } else printf("I am node with id %d my far request manager is %d and i am aborting him being appointed again by sender=%d\n",node_id,agent_id,sender_id);
- /*if (agent_id == sender_id) {
- tmp_inter_prev = NULL;
- tmp_inter_list = core_inter_head[agent_id];//core_inter_head[agent_id]->next;
- } else {
- tmp_inter_prev = core_inter_head[agent_id];
- tmp_inter_list = core_inter_head[agent_id]->next;
- }*/
- tmp_inter_prev = core_inter_head[agent_id];
- tmp_inter_list = core_inter_head[agent_id]->next;
- j = 0;
- while (tmp_inter_list != NULL) {
- if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == IDAG_REQ_DDS_PENDING) {
- if (tmp_inter_list->type == IDAG_REQ_DDS_PENDING){
- init_DDS_replies++;
- if (init_DDS_idags == init_DDS_replies && init_idags_areas_replies == init_areas_num) j=1; //send_init_reqs(sender_id);
- }
- fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,agent_id);
- fflush(log_file);
- if (tmp_inter_prev == NULL) {
- core_inter_head[agent_id] = core_inter_head[agent_id]->next;
- free(tmp_inter_list);
- tmp_inter_list = core_inter_head[agent_id];
- } else {
- tmp_inter_prev->next = tmp_inter_list->next;
- if (tmp_inter_prev->next == NULL) core_inter_tail[agent_id] = tmp_inter_prev;
- free(tmp_inter_list);
- tmp_inter_list = tmp_inter_prev->next;
- }
- }
- else tmp_inter_list = tmp_inter_list->next;
- }
- if (j) send_init_reqs(sender_id);
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN && signo == SIG_FAR_REQ
- && state != IDLE_INIT_MAN && state != IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
- printf("I am %d and i think i cought a stray init far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fprintf(log_file,"I am %d and i think i cought a stray init far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
-
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN && signo == SIG_FAR_REQ) {//in original manager get far offers
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 14 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
- //printf("open14 idag_id=%d node_id=%d\n",idag_id,node_id);
- fd_r = open(fifo_name, O_RDONLY);
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
- read(fd_r, &num_of_offers, sizeof(int));
- fprintf(log_file,"num of offers = %d\n",num_of_offers);
- fflush(log_file);
- if (num_of_offers > 0) {
- core_inter_head[sender_id]->type = REP_FAR_REQ_OFFER_PENDING;
- core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
- core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
- //printf("qweNode %d num_of_offers %d \n",sender_id,num_of_offers);
- for (i=1; i<=num_of_offers; i++){
- read(fd_r, &one_offer.num_of_cores, sizeof(int));
- read(fd_r, &one_offer.spd_loss, sizeof(float));
- one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
- for (j=0; j<one_offer.num_of_cores; j++)
- read(fd_r, &one_offer.offered_cores[j], sizeof(int));
- //printf("qweNode %d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
-
- core_inter_head[sender_id]->data.offer_acc_array[i] = -1;
- if (init_man_offers == NULL) {
- init_man_offers = (offer_list *) malloc(sizeof(offer_list));
- init_man_offers->off = one_offer;
- init_man_offers->sender = sender_id;
- init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
- init_man_offers->next = NULL;
- } else {
- tmp_offer_prev = NULL;
- tmp_offer_list = init_man_offers;
- while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
- tmp_offer_prev = tmp_offer_list;
- tmp_offer_list = tmp_offer_list->next;
- }
- if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
- tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
- tmp_offer_list = tmp_offer_prev->next;
- tmp_offer_list->next = NULL;
- tmp_offer_list->off = one_offer;
- tmp_offer_list->sender = sender_id;
- tmp_offer_list->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
- } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
- init_man_offers = (offer_list *) malloc(sizeof(offer_list));
- init_man_offers->off = one_offer;
- init_man_offers->sender = sender_id;
- init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
- init_man_offers->next = tmp_offer_list;
- } else {
- tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
- tmp_offer_prev = tmp_offer_prev->next;
- tmp_offer_prev->off = one_offer;
- tmp_offer_prev->sender = sender_id;
- tmp_offer_prev->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
- tmp_offer_prev->next = tmp_offer_list;
- }
- }
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: One node added in init_man_offers in sig_FAR_REQ\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
- fflush(log_file);
- }
- } else {
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- free(tmp_inter_list);
-
- if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //sem_post(&node_sem[node_id]);
- ////sem_post(&node_sem[sender_id]);
- } else {
- //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- printf("I am %d and i have to reject sig_far_req from %d. Our interaction is %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
- fprintf(log_file,"I am %d and i have to reject sig_far_req from %d. Our interaction is %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- //printf("IIIII I am %d in sig_far_req with sender %d and interaction = %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
-
- //sem_getvalue(&node_sem[node_id],&i);
- //fprintf(log_file, "Sem value = %d\n",i);
- //fflush(log_file);
- if (fd_r != -1) close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_FAR_REQ_handler with sender=%d and signo=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,signo,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_APPOINT_WORK_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, sender_id, i, valid=0;
- char *fifo_name;
- // inter_list *tmp_inter_list;
-
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_APPOINT_WORK_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] != NULL) {
- //else {
- printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
-
- /*tmp_inter_list = core_inter_head[sender_id];
- while (tmp_inter_list != NULL) {
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
- fflush(log_file);
- free(tmp_inter_list);
- tmp_inter_list = core_inter_head[sender_id];
- }*/
- }
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 15 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- fd_r = open(fifo_name, O_RDONLY);
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } else {
- fprintf(log_file, "I went through open\n");
- fflush(log_file);
- }
-
- read(fd_r, &valid, sizeof(int));
- fprintf(log_file, "Validity of message = %d\n",valid);
- fflush(log_file);
- if (valid == 1) {
- if (my_agent == -1) read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
- else if (my_agent != sender_id) {
- printf("I am %d and i do in appoint work switch of agent with upper_work_bound = %ld\n",node_id,upper_work_bound);
- fprintf(log_file,"I do in appoint work switch of agent with upper_work_bound = %ld\n",upper_work_bound);
- fflush(log_file);
- //if (upper_work_bound > 0) {//case where my former agent has not collected my remaining work time
- if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
- if (core_inter_head[my_agent] == NULL){
- core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_agent] = core_inter_head[my_agent];
- } else {
- core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
- }
- core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
- core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
- core_inter_tail[my_agent]->next = NULL;
- }
- read(fd_r, &my_agent, sizeof(int));
- }
- //else if (state != WORKING_NODE) printf("I have been initialied but i am not working\n");
- read(fd_r, &upper_work_bound, sizeof(long int));
- time_worked=0;
- ////sem_post(&node_sem[sender_id]);
- printf("I am node %d with agent %d I am going to start working for %ld\n",node_id,my_agent,upper_work_bound);
- if (upper_work_bound > 0) {
- if (state == IDLE_INIT_MAN) state = WORKING_NODE_IDLE_INIT;
- else if (state == INIT_MANAGER || state == INIT_MAN_CHK_OFFERS || state == INIT_MANAGER_SEND_OFFERS) {
- //state = IDLE_INIT_MAN_WORK_PENDING;
- pending_state = WORKING_NODE;
- printf("I am node %d and sou milaw gia mallia\n",node_id);
- } else if (state != WORKING_NODE_IDLE_INIT) state = WORKING_NODE;
- } else {
- kill(info->si_pid, SIG_FINISH);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_APPOINT_WORK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_CHECK_REM_TIME_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r=-1, sender_id, tmp_rem_time, i;
- char *fifo_name=NULL;
- int time_per_node, time_left, time_to_work, one_core;
- core_list *tmp_cores_list;
- inter_list *tmp_inter_list;
- float rem_workld;
- //inter_list tmp_inter_list;
-
- signals_disable();
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL && sender_id == my_agent){ //edw tha mpei otan exw allaksei agent
- fprintf(log_file, "I sig_CHECK_REM_TIME_handler a\n");
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL){
- core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_head[sender_id];
- } else {
- core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
- }
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a1\n");
- //fflush(log_file);
- core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
- core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
- if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
- core_inter_tail[sender_id]->next = NULL;
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
- //fflush(log_file);
- upper_work_bound = 0;
- if (state == WORKING_NODE) state = IDLE_CORE;
- kill(info->si_pid, SIG_CHECK_REM_TIME);
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a3\n");
- //fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a4\n");
- //fflush(log_file);
- } else if (core_inter_head[sender_id] == NULL && sender_id != my_agent) {
- //I hope that this is where i end up when my former agent asks me for my remaining work but my original upper_work_bound was 0
- fprintf(log_file, "I sig_CHECK_REM_TIME_handler c\n");
- fflush(log_file);
-
- if (core_inter_head[sender_id] == NULL){
- core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_head[sender_id];
- } else {
- core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
- }
- core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
- core_inter_tail[sender_id]->data.work_time = 0;
- core_inter_tail[sender_id]->next = NULL;
- kill(info->si_pid, SIG_CHECK_REM_TIME);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
- fprintf(log_file, "I sig_CHECK_REM_TIME_handler b\n");
- fflush(log_file);
- kill(info->si_pid, SIG_CHECK_REM_TIME);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- } else if (core_inter_head[sender_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {//CHK_REM_TIME
- //printf("I an in here node_id=%d\n",node_id);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 16 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- fd_r = open(fifo_name, O_RDONLY);
- //printf("I come through here\n");
-
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
-
- if (app_state != RESIZING) {
- printf("I am %d in check rem and app_state = %d app_id = %d\n",node_id,app_state,my_app.id);
- fprintf(log_file,"I am in check rem and app_state = %d app_id = %d\n",app_state,my_app.id);
- fflush(log_file);
- }
- read(fd_r, &tmp_rem_time, sizeof(int));
- fprintf(app_log_file,"tmp_rem_time = %d sender_id = %d\n",tmp_rem_time,sender_id);
- fflush(app_log_file);
- ////sem_post(&node_sem[sender_id]);
- my_stats.app_turnaround -= tmp_rem_time;
- sum_rem_time += tmp_rem_time;
-
- if (++chk_rem_count == chk_rem_num){
- nodes_ended_cnt = 0;
- rem_workld = sum_rem_time * old_Speedup;
- time_to_work = (int) roundf(rem_workld / my_Speedup);
- if (my_cores_count > 1) {
- time_per_node = time_to_work / (my_cores_count-1);
- time_left = time_to_work % (my_cores_count-1);
- printf("i am %d in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
- node_id,sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
- fprintf(app_log_file,"i am in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
- sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
- fflush(app_log_file);
- if (rem_workld > 0.0) {
- if (time_to_work == 0) time_left++;
- tmp_cores_list = my_cores->next;
- while (tmp_cores_list != NULL) {
- one_core = tmp_cores_list->core_id;
-
- if (core_inter_head[one_core]->type != APPOINT_WORK_NODE_PENDING && core_inter_head[one_core]->type != INIT_WORK_NODE_PENDING) {
- printf("Wrong fucking interaction core = %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
- tmp_inter_list = core_inter_head[one_core];
- while (tmp_inter_list != NULL) {
- if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
- tmp_inter_list = tmp_inter_list->next;
- }
- if (tmp_inter_list == NULL) printf("I am %d and mas ksekwliase o %d\n",node_id,one_core);
- } else tmp_inter_list = core_inter_head[one_core];
-
- if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) tmp_inter_list->type = APPOINT_WORK_NODE;
- else tmp_inter_list->type = INIT_WORK_NODE;
- tmp_inter_list->data.work_time = time_per_node;
- if (time_left > 0) {
- tmp_inter_list->data.work_time++;
- time_left--;
- }
-
- if (tmp_inter_list->data.work_time > 0) {
- my_stats.app_turnaround += tmp_inter_list->data.work_time;
- //printf("sending core with id=%d\n",one_core);
- if (core_inter_head[one_core]->type == APPOINT_WORK_NODE || core_inter_head[one_core]->type == INIT_WORK_NODE) {//||next == NULL
- kill(pid_num[one_core], SIG_APPOINT_WORK);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_core);
- } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
- ,core_inter_head[one_core]->next->type);
- } else nodes_ended_cnt++;
- /*core_inter_head[one_core]->data.work_time = time_per_node;
- if (time_left > 0) {
- core_inter_head[one_core]->data.work_time++;
- time_left--;
- }
-
- if (core_inter_head[one_core]->data.work_time > 0) {
- my_stats.app_turnaround += core_inter_head[one_core]->data.work_time;
- //printf("sending core with id=%d\n",one_core);
- if (core_inter_head[one_core]->next == NULL) {
- kill(pid_num[one_core], SIG_APPOINT_WORK);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,one_core);
- } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
- ,core_inter_head[one_core]->next->type);
- } else nodes_ended_cnt++;*/
-
- tmp_cores_list = tmp_cores_list->next;
- }
- app_state = RUNNING;
- } else {
- //app_terminated = 1;
- app_state = APP_TERMINATED;
- //state = AGENT_ENDING;
- }
- } else {
- printf("I am %d and i am so stupid that i offered my only working core\n",node_id);
- fprintf(app_log_file,"I have only one core state = %d and pending_state = %d\n",state,pending_state);
- fflush(app_log_file);
- if (selfopt_time_rem == -1 && state == IDLE_AGENT) state = AGENT_REWIND_FILE;
- else if (pending_state == IDLE_AGENT) pending_state = AGENT_REWIND_FILE;
- }
- /*if (app_terminated) {
- for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- if (core_inter_head[tmp_cores_list->core_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[tmp_cores_list->core_id]->type == INIT_WORK_NODE_PENDING){
- tmp_inter_list = core_inter_head[tmp_cores_list->core_id];
- core_inter_head[tmp_cores_list->core_id] = core_inter_head[tmp_cores_list->core_id]->next;
- free(tmp_inter_list);
- printf("I am %d and i pre freed node %d\n",node_id,tmp_cores_list->core_id);
- } else printf("I am %d and i tried to pre free but my interaction with my working node %d is %d\n",
- node_id,tmp_cores_list->core_id,core_inter_head[tmp_cores_list->core_id]->type);
- } else {*/
- if (app_state != APP_TERMINATED) {//!app_terminated
- if (pending_state == INIT_MANAGER) {
- state = INIT_MANAGER;
- pending_state = IDLE_AGENT;
- } else {
- fprintf(app_log_file, "I enter here with app_state = %d.\n",app_state);
- fflush(app_log_file);
- if (selfopt_interval > 0) {
- if (my_cores_count == max_cores_count) {
- fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process.\n");
- fflush(app_log_file);
- selfopt_time_rem = -1;
- } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
- fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d.\n",time_per_node,selfopt_interval);
- fflush(app_log_file);
- selfopt_time_rem = -1;
- } else {
- its.it_value.tv_nsec = selfopt_interval * MS;
- selfopt_time_rem = selfopt_interval;
- if (timer_settime(timerid, 0, &its, NULL) == -1) {
- perror("timer_settime error8");
- printf("I am %d timer_settime error8 selfopt_interval = %d\n",node_id,selfopt_interval);
- }
- }
- } else selfopt_time_rem = -1;
- }
- }
- }
-
- if (core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {
- fprintf(log_file,"I am with sender_id = %d and a\n",sender_id);
- fflush(log_file);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- fprintf(log_file,"I am with sender_id = %d and b\n",sender_id);
- fflush(log_file);
- }
- //printf("I am node %d my agent is %d and I am going to start working\n",node_id,my_agent);
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- } else if (sender_id == my_agent){
- printf("I am %d and i am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- fprintf(log_file,"I am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
- fflush(log_file);
- core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
- core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
- core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
- if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
- core_inter_tail[sender_id]->next = NULL;
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
- //fflush(log_file);
- upper_work_bound = 0;
- if (state == WORKING_NODE) state = IDLE_CORE;
- } else {
- printf("I am %d and i am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- fprintf(log_file,"I am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
- fflush(log_file);
- kill(info->si_pid, SIG_REJECT);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- }
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a5\n");
- //fflush(log_file);
- if (fd_r != -1) close(fd_r);
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a6\n");
- //fflush(log_file);
- free(fifo_name);
- //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a7\n");
- //fflush(log_file);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_FINISH_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, i, is_sender_idag, sender_id, new_agent_id;
- int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
- int *tmp_list, tmp_cnt, tmp_idag;
- char *fifo_name;
- core_list *tmp_cores_list;//tmp_cores,
- DDS_list *tmp_DDS,*prev_DDS;
- inter_list *tmp_inter_list;
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- //if i am an idag, sig_finish should be proceed instantly, so i am dropping.
- //On the other hand if an common node doesn't set its agent to -1 its not a big deal. (Hopefully)
- if (core_inter_head[sender_id] != NULL && my_idag == -1) {
- //else {
- printf("I am %d in sig finish with sender %d and i don't know what to do with interaction %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
-
- if (my_idag == -1) {
- is_sender_idag = 0;
- for (i=0; i<num_idags; i++)
- if (idag_id_arr[i] == sender_id){
- is_sender_idag = 1;
- break;
- }
-
- if (!is_sender_idag) {
- tmp_inter_list = core_inter_head[sender_id];
- while (tmp_inter_list != NULL) {
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
- fflush(log_file);
- free(tmp_inter_list);
- tmp_inter_list = core_inter_head[sender_id];
- }
- }
- }
- }
-
- if (my_idag != -1){
- if (my_agent != -1 && my_agent == sender_id) my_agent = -1;//working node
- if (++nodes_ended_cnt == (my_cores_count - 1)) app_state = APP_TERMINATED;//app_terminated = 1;
- } else {
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In 17 Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- fd_r = open(fifo_name, O_RDONLY);
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- }
- is_sender_idag = 0;
- for (i=0; i<num_idags; i++)
- if (idag_id_arr[i] == sender_id){
- is_sender_idag = 1;
- break;
- }
- printf("I am %d in sig finish with is_sender_idag = %d\n",node_id,is_sender_idag);
- if (is_sender_idag == 0){
- read(fd_r, &nodes_cnt, sizeof(int));
- nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
- for (i=0; i<nodes_cnt; i++)
- read(fd_r, &nodes_to_process[i], sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
- printf("I am node_id=%d Cores to be processed: ",node_id);
- for (i=0; i<nodes_cnt; i++)
- printf(" %d",nodes_to_process[i]);
- printf("\n");
- printf("I am node_id=%d nodes_cnt = %d\n",node_id,nodes_cnt);
- while (processed_cnt < nodes_cnt){
- tmp_cnt = 0;
- tmp_idag = -1;
- for (i=0; i<nodes_cnt; i++){
- if (processed_cnt == nodes_cnt) break;
- if (nodes_to_process[i] == -1) continue;
- else {
- if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
- if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
- else {
- tmp_list[tmp_cnt++] = nodes_to_process[i];
- nodes_to_process[i] = -1;
- processed_cnt++;
- }
- }
- }
- if (tmp_idag != node_id){
- if (core_inter_head[tmp_idag] == NULL){
- core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
- } else {
- printf("finish agent DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
- core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
- }
- printf("node_id = %d In finish tmp_idag = %d tmp_cnt = %d sender_id = %d is_sender_idag=%d tmp_list[0]=%d\n",node_id,tmp_idag,tmp_cnt,sender_id,is_sender_idag,tmp_list[0]);
- core_inter_tail[tmp_idag]->type = REMOVE_APP;
- core_inter_tail[tmp_idag]->data.agent_ended = sender_id;
- /*core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
- core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
- core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
- for (j=1; j<= tmp_cnt; j++)
- core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
- */
- core_inter_tail[tmp_idag]->next = NULL;
-
- //if (tmp_idag != 0) why???
- if (core_inter_head[tmp_idag]->next == NULL) {
- kill(pid_num[tmp_idag], SIG_FINISH);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,tmp_idag);
- }
- } else {
- printf("In FINISH same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
- new_agent_id = sender_id;
- /*for (i=0; i<tmp_cnt; i++){
- tmp_cores = my_cores;
-
- while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
- tmp_cores = tmp_cores->next;
- if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
- }
- DDS->num_of_cores += tmp_cnt;*/
- for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- if (tmp_cores_list->offered_to == new_agent_id) {
- tmp_cores_list->offered_to = -1;
- DDS->num_of_cores++;
- }
- prev_DDS = DDS;
- tmp_DDS = DDS->next;
- while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
- prev_DDS = tmp_DDS;
- tmp_DDS = tmp_DDS->next;
- }
- if (tmp_DDS != NULL){
- DDS_count--;
- prev_DDS->next = tmp_DDS->next;
- if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
- free(tmp_DDS);
- } else printf("Fuck agent that finished is not in my DDS\n");
- printf("My removal of agent complete node_id = %d sender_id=%d\n",node_id,sender_id);
- printf("Number of agents in region = %d\n",DDS_count);
- i=0;
- for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
- printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
- i++;
- }
- }
- }
- free(nodes_to_process);
- free(tmp_list);
- } else {
- /*read(fd_r, &nodes_cnt, sizeof(int));
- printf("I am %d and secondary node cnt by %d is %d\n",node_id,sender_id,nodes_cnt);
- nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
- for (i=0; i<nodes_cnt; i++)
- read(fd_r, &nodes_to_process[i], sizeof(int));
- read(fd_r, &new_agent_id, sizeof(int));
- ////sem_post(&node_sem[sender_id]);
- printf("Secondary Cores to be processed of %d by %d: ",new_agent_id,node_id);
- for (i=0; i<nodes_cnt; i++)
- printf(" %d",nodes_to_process[i]);
- printf("\n");
- for (i=0; i<nodes_cnt; i++){
- tmp_cores = my_cores;
- while (tmp_cores->core_id != nodes_to_process[i])//nodes_to_process[i]
- tmp_cores = tmp_cores->next;
- if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
- }
- DDS->num_of_cores += nodes_cnt;
- */
- read(fd_r, &new_agent_id, sizeof(int));
- printf("I am %d in Secondary sig_finish for %d\n",node_id,new_agent_id);
- for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- if (tmp_cores_list->offered_to == new_agent_id) {
- tmp_cores_list->offered_to = -1;
- DDS->num_of_cores++;
- }
- prev_DDS = DDS;
- tmp_DDS = DDS->next;
- while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
- prev_DDS = tmp_DDS;
- tmp_DDS = tmp_DDS->next;
- }
- if (tmp_DDS != NULL){
- DDS_count--;
- prev_DDS->next = tmp_DDS->next;
- if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
- free(tmp_DDS);
- } else printf("Fuck agent that finished is not in my DDS\n");
- //free(nodes_to_process);
- /*my_stats.times_accessed++;
- printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
- printf("Number of agents in region = %d\n",DDS_count);
- printf("Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
- i=1;
- for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
- printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
- my_stats.cores_utilized += tmp_DDS->num_of_cores;
- i++;
- }*/
-
- printf("Secondary removal of agent complete node_id =%d sender_id=%d\n",node_id,sender_id);
- printf("Number of agents in region = %d\n",DDS_count);
- i=0;
- for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
- printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
- i++;
- }
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- close(fd_r);
- }
- //sem_post(&node_sem[node_id]);
- //} else printf("I am %d in sig finish with sender %d and i don't know what to do with interaction %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- //free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
- void sig_REJECT_handler(int signo, siginfo_t *info, void *context)
- {
- int sender_id, agent_id, i, j;//, crit_app_id;//fd_r,
- //char *fifo_name;
- inter_list *tmp_inter_list;
- core_list *tmp_cores_list;
- target_list *tmp_target_list;
- offer_list *tmp_offer_list;
- signals_disable();
- //inter_list tmp_inter_list;
-
- sender_id = get_id_from_pid(info->si_pid);
- //fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_REJECT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] != NULL){
- fprintf(log_file, "[%d:%d:%d]: Interaction with sender=%d is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,core_inter_head[sender_id]->type);
- fflush(log_file);
- }
- if (core_inter_head[sender_id] == NULL){ //edw tha mpei otan exw allaksei agent
- printf("I am %d in sig_reject and i have null interaction with sender %d\n",node_id,sender_id);
- } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS
- || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
- if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
- init_DDS_replies++;
- fprintf(log_file, "One init_req_dds has been rejected by %d!\n",sender_id);
- fflush(log_file);
- if (init_areas_num == 1) {
- fprintf(log_file, "My only init area was rejected. my state is %d my pending_state = %d\n",state,pending_state);
- fflush(log_file);
- state = INIT_MANAGER_SEND_OFFERS;
- }
- } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) {
- selfopt_DDS_replies++;
- fprintf(log_file, "One selfopt_req_dds has been rejected by %d!\n",sender_id);
- fflush(log_file);
- }
- //printf("selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
-
- if ((core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING)
- && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_areas_num)
- for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
- agent_id = tmp_target_list->target;
- for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
- if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
- if (tmp_inter_list == NULL) {
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
-
- core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
- core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
- core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
-
- if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
- fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
- for (i=0; i<tmp_target_list->num_of_regions; i++) {
- core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
- fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
- }
- fprintf(log_file,"\n");
- fflush(log_file);
- core_inter_tail[agent_id]->next = NULL;
- //kill(pid_num[agent_id], SIG_REQ_CORES);
- if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
- } else
- printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
- }
- else if ((core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) && selfopt_DDS_replies == selfopt_DDS_idags)
- for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
- agent_id = tmp_target_list->target;
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
- core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
- core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
- core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
- if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
- fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
- for (i=0; i<tmp_target_list->num_of_regions; i++) {
- core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
- fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
- }
- fprintf(log_file,"\n");
- fflush(log_file);
- core_inter_tail[agent_id]->next = NULL;
- if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
- kill(pid_num[agent_id], SIG_REQ_CORES);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
- }
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
- send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_CORES
- || core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
- || core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING
- || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING
- || core_inter_head[sender_id]->type == REP_FAR_INIT_REQ) {
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER) {//FAR_REQ_OFFER_SENT
- if (far_man_offers != NULL && far_man_offers->sender == node_id) {
- tmp_cores_list = my_cores;
- while (tmp_cores_list != NULL){
- if (tmp_cores_list->offered_to == sender_id) tmp_cores_list->offered_to = -1;
- tmp_cores_list = tmp_cores_list->next;
- }
-
- tmp_offer_list = far_man_offers;
- far_man_offers = far_man_offers->next;
- free(tmp_offer_list);
- }
- while (far_man_offers != NULL){
- printf("I am node %d in reject and far offer answer for node %d is 0\n",node_id,far_man_offers->sender);
- *far_man_offers->answer = 0;
- if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
- kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
- core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
- } else printf("I am %d and Apparently lists are poutana ola\n",node_id);
-
- tmp_offer_list = far_man_offers;
- far_man_offers = far_man_offers->next;
- free(tmp_offer_list);
- }
- far_req_app.A=-1.0;
- far_req_app.var=-1.0;
- far_req_app.num_of_cores=-1;
- far_req_or_sender = -1;
- far_reg.C = -1;
- far_reg.r = -1;
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS) {
- free(core_inter_head[sender_id]->data.idags_in_reg);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == REP_IDAG_REQ_DDS) {
- free(core_inter_head[sender_id]->data.agents_in_reg);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN_APPOINT) {
- agent_id = core_inter_head[sender_id]->data.far_req.orig_sender;
-
- if (core_inter_head[agent_id] == NULL){
- core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_head[agent_id];
- } else {
- core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
- core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
- }
- core_inter_tail[agent_id]->type = ABORT_FAR_MAN;
- core_inter_tail[agent_id]->data.far_req_man = sender_id;
- core_inter_tail[agent_id]->next = NULL;
- if (core_inter_head[agent_id]->next == NULL) {
- kill(pid_num[agent_id], SIG_REMOVE_FAR_MAN);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,agent_id);
- }
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == REP_AGENT_REQ_CORES) {
-
- for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++)
- if (my_cores != NULL)
- for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
- for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
- if (tmp_cores_list->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
- tmp_cores_list->offered_to = -1;
- break;
- }
- free(core_inter_head[sender_id]->data.off_arr.offer_arr);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == INIT_APP) {
- printf("I am 0 and %d rejected my init_app req\n",sender_id);
- fprintf(log_file,"I am 0 and %d rejected my init_app req\n",sender_id);
- fflush(log_file);
- if (init_pending_head == NULL){
- init_pending_head = (inter_list *) malloc(sizeof(inter_list));
- init_pending_tail = init_pending_head;
- } else {
- init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
- init_pending_tail = init_pending_tail->next;
- }
- init_pending_tail->type = INIT_APP;
- init_pending_tail->data.new_app = core_inter_head[sender_id]->data.new_app;
- init_pending_tail->data.new_app.num_of_cores = sender_id;
- init_pending_tail->next = NULL;
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
- printf("I am %d and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",node_id,sender_id,core_inter_head[sender_id]->data.work_time);
- fprintf(log_file,"I am and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",sender_id,core_inter_head[sender_id]->data.work_time);
- fflush(log_file);
- tmp_inter_list = core_inter_head[sender_id];
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- else send_next_signal(core_inter_head[sender_id], sender_id);
- free(tmp_inter_list);
- } else printf("I am %d in sig_reject and i have interaction with sender %d interaction = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended sig_REJECT_handler with sender = %d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- //fprintf(log_file, "[%ld:%ld:%ld:%ld]: my idle agent is %d and my pid is %d\n",tv.tv_sec/3600,tv.tv_sec%3600/60,tv.tv_sec%3600%60,(long int) tv.tv_usec,my_idag,getpid());
- //close(fd_r);
-
- //free(fifo_name);
- signals_enable();
- /*signals_disable();
- state = TERMINATED;
- signals_enable();*/
- }
- void sig_REMOVE_FAR_MAN_handler(int signo, siginfo_t *info, void *context)
- {
- int fd_r, sender_id, i, my_agent;
- char *fifo_name;
- inter_list *tmp_inter_list, *tmp_inter_prev;
-
- signals_disable();
-
- sender_id = get_id_from_pid(info->si_pid);
- fifo_name = get_pipe_name(node_id);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I entered sig_REMOVE_FAR_MAN_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
-
- if (core_inter_head[sender_id] != NULL) {
- printf("I am %d and i in am doing smth else with my agent %d in sig_REMOVE_FAR_MAN_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
-
- /*tmp_inter_list = core_inter_head[sender_id];
- while (tmp_inter_list != NULL) {
- core_inter_head[sender_id] = core_inter_head[sender_id]->next;
- if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
- fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
- fflush(log_file);
- free(tmp_inter_list);
- tmp_inter_list = core_inter_head[sender_id];
- }*/
- }
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "In REMOVE_FAR_MAN Trying to acquire semaphore. Sem value = %d\n",i);
- fflush(log_file);
- kill(info->si_pid, SIG_ACK);
- sem_wait(&node_sem[node_id]);
- sem_getvalue(&node_sem[node_id],&i);
- fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
- fflush(log_file);
- my_stats.msg_count++;
- my_stats.distance += distance(node_id,sender_id);
- fd_r = open(fifo_name, O_RDONLY);
- if (fd_r == -1) {
- perror("opening reading pipe");
- signals_enable();
- return;
- } /*else {
- fprintf(log_file, "I went through open\n");
- fflush(log_file);
- }*/
-
- read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
- tmp_inter_prev = NULL;
- tmp_inter_list = core_inter_head[my_agent];
- while (tmp_inter_list != NULL) {
- //the far manager being clear had to be the one tha has been appointed by sender
- if (tmp_inter_list->type == FAR_REQ_MAN && tmp_inter_list->data.far_req_man == sender_id) {
- fprintf(log_file, "I dismissed type = %d of node %d sender = %d\n",tmp_inter_list->type,my_agent,sender_id);
- fflush(log_file);
- if (tmp_inter_prev == NULL) {
- core_inter_head[my_agent] = core_inter_head[my_agent]->next;
- if (core_inter_head[my_agent] == NULL) core_inter_tail[my_agent] = NULL;
- else send_next_signal(core_inter_head[my_agent], my_agent);
- } else {
- tmp_inter_prev->next = tmp_inter_list->next;
- if (tmp_inter_prev->next == NULL) core_inter_tail[my_agent] = tmp_inter_prev;
- }
- free(tmp_inter_list);
- break;
- //tmp_inter_list = tmp_inter_prev->next;
- }
- else tmp_inter_list = tmp_inter_list->next;
- }
- //sem_post(&node_sem[node_id]);
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
- fflush(log_file);
- if (sem_post(&node_sem_out[sender_id]) == -1){
- printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
- perror("sem_post");
- }
- sem_getvalue(&node_sem_out[sender_id],&i);
- fprintf(log_file, "Sem value = %d\n",i);
- fflush(log_file);
- //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
- close(fd_r);
- free(fifo_name);
- cur_time = time(NULL);
- cur_t = localtime(&cur_time);
- fprintf(log_file, "[%d:%d:%d]: I ended REMOVE_FAR_MAN with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
- fflush(log_file);
- signals_enable();
- }
|