3 #include "structdefs.h"
5 #include "checkpoint.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
15 extern int injectfailures;
16 extern float failurechance;
21 #include "instrument.h"
24 struct genhashtable * activetasks;
25 struct parameterwrapper * objectqueues[NUMCLASSES];
26 struct genhashtable * failedtasks;
27 struct taskparamdescriptor * currtpd;
28 struct RuntimeHash * forward;
29 struct RuntimeHash * reverse;
30 struct RuntimeHash * exitfsesdata;
32 int main(int argc, char **argv) {
34 GC_init(); // Initialize the garbage collector
40 initializeexithandler();
41 /* Create table for failed tasks */
42 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
43 (int (*)(void *,void *)) &comparetpd);
44 /* Create queue of active tasks */
45 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
46 (int (*)(void *,void *)) &comparetpd);
48 exitfsesdata=allocateRuntimeHash(10);
49 /* Process task information */
52 /* Create startup object */
53 createstartupobject(argc, argv);
55 /* Start executing the tasks */
59 void createstartupobject(int argc, char ** argv) {
62 /* Allocate startup object */
64 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
65 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
67 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
68 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
70 /* Build array of strings */
71 startupobject->___parameters___=stringarray;
73 int length=strlen(argv[i]);
75 struct ___String___ *newstring=NewString(NULL, argv[i],length);
77 struct ___String___ *newstring=NewString(argv[i],length);
79 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
82 /* Set initialized flag for startup object */
83 flagorand(startupobject,1,0xFFFFFFFF);
86 int hashCodetpd(struct taskparamdescriptor *ftd) {
87 int hash=(int)ftd->task;
89 for(i=0;i<ftd->numParameters;i++){
90 hash^=(int)ftd->parameterArray[i];
95 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
97 if (ftd1->task!=ftd2->task)
99 for(i=0;i<ftd1->numParameters;i++)
100 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
105 /* This function sets a tag. */
107 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
109 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
111 struct ___Object___ * tagptr=obj->___tags___;
113 obj->___tags___=(struct ___Object___ *)tagd;
115 /* Have to check if it is already set */
116 if (tagptr->type==TAGTYPE) {
117 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
121 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
122 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
123 obj=(struct ___Object___ *)ptrarray[2];
124 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
125 td=(struct ___TagDescriptor___ *) obj->___tags___;
127 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
129 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
130 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
131 obj->___tags___=(struct ___Object___ *) ao;
132 ao->___cachedCode___=2;
136 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
137 for(i=0;i<ao->___cachedCode___;i++) {
138 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
142 if (ao->___cachedCode___<ao->___length___) {
143 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
144 ao->___cachedCode___++;
147 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
148 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
149 obj=(struct ___Object___ *)ptrarray[2];
150 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
151 ao=(struct ArrayObject *)obj->___tags___;
153 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
155 aonew->___cachedCode___=ao->___length___+1;
156 for(i=0;i<ao->___length___;i++) {
157 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
159 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
165 struct ___Object___ * tagset=tagd->flagptr;
169 } else if (tagset->type!=OBJECTARRAYTYPE) {
171 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
172 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
173 obj=(struct ___Object___ *)ptrarray[2];
174 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
176 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
178 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
179 ARRAYSET(ao, struct ___Object___ *, 1, obj);
180 ao->___cachedCode___=2;
181 tagd->flagptr=(struct ___Object___ *)ao;
183 struct ArrayObject *ao=(struct ArrayObject *) tagset;
184 if (ao->___cachedCode___<ao->___length___) {
185 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
189 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
190 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
191 obj=(struct ___Object___ *)ptrarray[2];
192 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
193 ao=(struct ArrayObject *)tagd->flagptr;
195 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
197 aonew->___cachedCode___=ao->___cachedCode___+1;
198 for(i=0;i<ao->___length___;i++) {
199 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
201 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
202 tagd->flagptr=(struct ___Object___ *) ao;
208 /* This function clears a tag. */
210 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
212 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
214 /* We'll assume that tag is alway there.
215 Need to statically check for this of course. */
216 struct ___Object___ * tagptr=obj->___tags___;
218 if (tagptr->type==TAGTYPE) {
219 if ((struct ___TagDescriptor___ *)tagptr==tagd)
220 obj->___tags___=NULL;
222 printf("ERROR 1 in tagclear\n");
224 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
226 for(i=0;i<ao->___cachedCode___;i++) {
227 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
229 ao->___cachedCode___--;
230 if (i<ao->___cachedCode___)
231 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
232 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
233 if (ao->___cachedCode___==0)
234 obj->___tags___=NULL;
238 printf("ERROR 2 in tagclear\n");
242 struct ___Object___ *tagset=tagd->flagptr;
243 if (tagset->type!=OBJECTARRAYTYPE) {
247 printf("ERROR 3 in tagclear\n");
249 struct ArrayObject *ao=(struct ArrayObject *) tagset;
251 for(i=0;i<ao->___cachedCode___;i++) {
252 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
254 ao->___cachedCode___--;
255 if (i<ao->___cachedCode___)
256 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
257 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
258 if (ao->___cachedCode___==0)
263 printf("ERROR 4 in tagclear\n");
271 /* This function allocates a new tag. */
273 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
274 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
276 struct ___TagDescriptor___ * allocate_tag(int index) {
277 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
286 /* This function updates the flag for object ptr. It or's the flag
287 with the or mask and and's it with the andmask. */
289 void flagbody(struct ___Object___ *ptr, int flag);
291 void enqueueoptional(struct ___Object___ * currobj);
293 struct optionaltaskdescriptor *** makeintersectionotd(int num, struct fsanalysiswrapper ** wrapperarray, int *result){
296 struct optionaltaskdescriptor *** bigtmparray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **)*maxotd);
297 struct fsanalysiswrapper * tmpwrapper;
298 struct fsanalysiswrapper * firstwrapper = wrapperarray[0];/*we are sure that num>0*/
299 /*we check if the otd of the first wrapper is contained in all others*/
300 for(i=0; i<firstwrapper->numoptionaltaskdescriptors; i++){
301 struct optionaltaskdescriptor ** tmparray = RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * num);
302 struct optionaltaskdescriptor * otd = firstwrapper->optionaltaskdescriptorarray[i];
304 for(j=1; j<num; j++){
305 tmpwrapper = wrapperarray[j];
306 for(k=0; k<tmpwrapper->numoptionaltaskdescriptors; k++){
307 struct optionaltaskdescriptor * tmpotd=tmpwrapper->optionaltaskdescriptorarray[k];
308 if(otd->task->name == tmpotd->task->name){
318 bigtmparray[(*result)]=tmparray;
324 {/*now allocate the good size for otdarray and put the otds*/
325 struct optionaltaskdescriptor *** otdarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * (*result));
326 for(i=0; i<(*result); i++)
327 otdarray[i]=bigtmparray[i];
329 RUNFREE(bigtmparray);
336 void flagorand(void * ptr, int ormask, int andmask) {
337 int oldflag=((int *)ptr)[1];
338 int flag=ormask|oldflag;
340 struct ___Object___ * obj = (struct ___Object___ *)ptr;
341 if(obj->failedstatus==1){/*store the information about exitfses*/
342 int i,j,counter=0, offset=0;
343 for(i=0; i<obj->numotds; i++){
344 counter+=obj->otds[i]->numenterflags;
346 obj->numexitfses=counter;
347 if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
348 obj->exitfses= RUNMALLOC(sizeof(int) * counter);
349 for(i=0; i<obj->numotds; i++){
350 for(j=0; j<obj->otds[i]->numenterflags; j++){
351 oldflag=obj->otds[i]->enterflags[j];
354 obj->exitfses[j+offset]=flag;
356 offset+=obj->otds[i]->numenterflags;
358 enqueueoptional(ptr);
364 // Not sure why this was necessary
365 // if (flag==oldflag) /* Don't do anything */
372 void intflagorand(void * ptr, int ormask, int andmask) {
373 int oldflag=((int *)ptr)[1];
374 int flag=ormask|oldflag;
376 struct ___Object___ * obj = (struct ___Object___ *)ptr;
377 if(obj->failedstatus==1){/*store the information about exitfses*/
378 int i,j,counter=0, offset=0;
379 for(i=0; i<obj->numotds; i++){
380 counter+=obj->otds[i]->numenterflags;
382 obj->numexitfses=counter;
383 if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
384 obj->exitfses= RUNMALLOC(sizeof(int) * counter);
385 for(i=0; i<obj->numotds; i++){
386 for(j=0; j<obj->otds[i]->numenterflags; j++){
387 oldflag=obj->otds[i]->enterflags[j];
390 obj->exitfses[j+offset]=flag;
392 offset+=obj->otds[i]->numenterflags;
394 enqueueoptional(ptr);
400 if (flag==oldflag) /* Don't do anything */
402 else flagbody(ptr, flag);
406 void flagorandinit(void * ptr, int ormask, int andmask) {
407 int oldflag=((int *)ptr)[1];
408 int flag=ormask|oldflag;
410 struct ___Object___ * obj = (struct ___Object___ *)ptr;
411 if(obj->failedstatus==1){/*store the information about exitfses*/
412 int i,j,counter=0, offset=0;
413 for(i=0; i<obj->numotds; i++){
414 counter+=obj->otds[i]->numenterflags;
416 obj->numexitfses=counter;
417 if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
418 obj->exitfses= RUNMALLOC(sizeof(int) * counter);
419 for(i=0; i<obj->numotds; i++){
420 for(j=0; j<obj->otds[i]->numenterflags; j++){
421 oldflag=obj->otds[i]->enterflags[j];
424 obj->exitfses[j+offset]=flag;
426 offset+=obj->otds[i]->numenterflags;
428 enqueueoptional(ptr);
439 removeoptionalfromqueues(int hashcode, struct ___Object___ * currobj, struct parameterwrapper * flagptr){/*find a better way to free the useless instances of the object*/
440 while(flagptr!=NULL) {
441 struct ___Object___ *temp=NULL;
442 struct parameterwrapper *ptr;
443 struct RuntimeNode * node = flagptr->objectset->listhead;
445 temp=(struct ___Object___ *)node->key;
446 if(temp->failedstatus==1 && temp->hashcode==currobj->hashcode){
448 RuntimeHashget(flagptr->objectset, (int)temp, (int *) &ptr);
449 RuntimeHashremove(flagptr->objectset, (int)temp, (int) ptr);//remove from wrapper
450 //delete the fields that wont be removed by the GC.
451 if(temp->exitfses!=NULL) RUNFREE(temp->exitfses);
452 if(temp->otds!=NULL) RUNFREE(temp->otds);
456 //remove from wrapper
457 RuntimeHashget(flagptr->objectset, (int)temp, (int *) &ptr);
458 RuntimeHashremove(flagptr->objectset, (int)temp, (int) ptr);
466 flagptr=flagptr->next;
471 void flagbody(struct ___Object___ *ptr, int flag) {
472 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
475 /*Remove object from all queues */
476 while(flagptr!=NULL) {
477 struct parameterwrapper *next;
478 struct ___Object___ * tag=ptr->___tags___;
479 RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
480 RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
485 struct QueueItem *tmpptr;
486 struct parameterwrapper * parameter=objectqueues[ptr->type];
488 struct parameterwrapper * prevptr=NULL;
489 struct ___Object___ *tagptr=ptr->___tags___;
491 /* Outer loop iterates through all parameter queues an object of
492 this type could be in. */
494 while(parameter!=NULL) {
496 if (parameter->numbertags>0) {
498 goto nextloop;//that means the object has no tag but that param needs tag
499 else if(tagptr->type==TAGTYPE) {//one tag
500 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
501 for(i=0;i<parameter->numbertags;i++) {
502 //slotid is parameter->tagarray[2*i];
503 int tagid=parameter->tagarray[2*i+1];
504 if (tagid!=tagptr->flag)
505 goto nextloop; /*We don't have this tag */
507 } else {//multiple tags
508 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
509 for(i=0;i<parameter->numbertags;i++) {
510 //slotid is parameter->tagarray[2*i];
511 int tagid=parameter->tagarray[2*i+1];
513 for(j=0;j<ao->___cachedCode___;j++) {
514 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
525 for(i=0;i<parameter->numberofterms;i++) {
526 int andmask=parameter->intarray[i*2];
527 int checkmask=parameter->intarray[i*2+1];
528 if ((flag&andmask)==checkmask) {
529 enqueuetasks(parameter, prevptr, ptr);
535 parameter=parameter->next;
537 ptr->flagptr=prevptr;
543 void enqueueoptional(struct ___Object___ * currobj){
545 struct classanalysiswrapper * classwrapper=NULL;
546 struct fsanalysiswrapper * fswrapper=NULL;
548 int numoptionaltaskdescriptors=0;
549 struct optionaltaskdescriptor *** optionaltaskdescriptorarray=NULL;
550 struct fsanalysiswrapper ** goodfswrappersarray=NULL;
551 int numgoodfswrappers=0;
553 if(currobj->numexitfses==0)
554 printf("Handling failed object\nType : %i\nFlag : 0x%x\n", currobj->type, currobj->flag);
556 printf("Handling failed object\nType : %i\n", currobj->type);
558 for(fscount=0; fscount<currobj->numexitfses; fscount++)
559 printf("Flag : 0x%x\n", currobj->exitfses[fscount]);
561 struct ___Object___ * tagptr = currobj->___tags___;
563 if(tagptr->type==TAGTYPE) {
564 printf("Tag : %i\n", tagptr->flag);}
566 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
567 int numbertags = ao->___length___;
568 for(counter=0; counter<numbertags; counter++){
569 printf("Tag : %i\n", ao[counter].flag);
574 /*set the object as failed*/
575 currobj->failedstatus = 1;
577 /*test what optionaltaskdescriptors are available,
578 find the class corresponding*/
580 for(counter = 0; counter<numclasses; counter++){
581 classwrapper = classanalysiswrapperarray[counter];
582 if(classwrapper == NULL){
583 fprintf(stderr, "ERROR : \"struct classanalysiswrapper * classwraper\" is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
586 /*check object type*/
587 if( currobj->type == classwrapper->type)
591 printf("No task will use this parameter as optional\n");
593 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
598 printf("Found the class, search through fses\n");
600 /*search through fses*/
601 goodfswrappersarray = RUNMALLOC(sizeof(struct fsanalysiswrapper *) * classwrapper->numfsanalysiswrappers); /*max number of good fswrappers*/
602 if(goodfswrappersarray == NULL){
603 fprintf(stderr, "ERROR : \"struct fsanalysiswrapper ** goodfswrappersarray\" is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
604 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
607 for(counter = 0; counter<classwrapper->numfsanalysiswrappers; counter++){
608 /*test the FS of the object (include flags and tags)*/
609 fswrapper = classwrapper->fsanalysiswrapperarray[counter];
610 if(fswrapper == NULL){
611 fprintf(stderr, "ERROR : struct fsanalysiswrapper * is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
612 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
616 struct ___Object___ * tagptr = currobj->___tags___;
617 if(fswrapper->numtags>0){
619 goto nextloop;//that means the object has no tag but that param needs tag
620 else if(tagptr->type==TAGTYPE) {//one tag
621 if(fswrapper->numtags!=1) goto nextloop;//we don't have the right number of tags
622 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
623 if (fswrapper->tags[0]!=tagptr->flag)
626 else {//multiple tags
627 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
629 if(ao->___length___!=fswrapper->numtags) goto nextloop;//we don't have the right number of tags
630 for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
631 int tagid=fswrapper->tags[tag_counter];
633 for(j=0;j<ao->___cachedCode___;j++) {
634 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
645 if(currobj->numexitfses==0){/*first time the method is invoqued*/
646 if( currobj->flag == fswrapper->flags){
648 optionaltaskdescriptorarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **) * fswrapper->numoptionaltaskdescriptors);
649 numoptionaltaskdescriptors = fswrapper->numoptionaltaskdescriptors;
650 for(otdc = 0; otdc<fswrapper->numoptionaltaskdescriptors; otdc++){
651 struct optionaltaskdescriptor ** tmpptr = RUNMALLOC(sizeof(struct optionaltaskdescriptor *));
652 tmpptr[0] = fswrapper->optionaltaskdescriptorarray[otdc];
653 optionaltaskdescriptorarray[otdc] = tmpptr;
659 else if(currobj->numexitfses==1){/*one fs exit*/
660 if(currobj->exitfses[0] == fswrapper->flags){
662 optionaltaskdescriptorarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **) * fswrapper->numoptionaltaskdescriptors);
663 numoptionaltaskdescriptors = fswrapper->numoptionaltaskdescriptors;
664 for(otdc = 0; otdc<fswrapper->numoptionaltaskdescriptors; otdc++){
665 struct optionaltaskdescriptor ** tmpptr = RUNMALLOC(sizeof(struct optionaltaskdescriptor *));
666 tmpptr[0] = fswrapper->optionaltaskdescriptorarray[otdc];
667 optionaltaskdescriptorarray[otdc] = tmpptr;
675 for(fscount=0; fscount<currobj->numexitfses; fscount++){
677 if( currobj->exitfses[fscount] == fswrapper->flags ){/*see if the fswraper correspond to one of the fses*/
678 goodfswrappersarray[numgoodfswrappers]=fswrapper;
682 if(counter==classwrapper->numfsanalysiswrappers-1) goto makeintersection; /*last fswrapper*/
690 printf("FS not found, Nothing more to do\n");
692 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
696 if(numgoodfswrappers==0 || numgoodfswrappers==1) goto nofs; /*nothing has been found, we expect more than one wrapper for multiple flags*/
697 optionaltaskdescriptorarray = makeintersectionotd(numgoodfswrappers, goodfswrappersarray, &numoptionaltaskdescriptors);
698 if(optionaltaskdescriptorarray==NULL){
699 fprintf(stderr, "ERROR : struct optionaltaskdescriptor ** is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
706 printf("FS(es) found, intersection created, %i potential tasks :\n", numoptionaltaskdescriptors);
709 /*find the parameterwrapper corresponding to the potential task*/
710 for(counter = 0; counter<numoptionaltaskdescriptors; counter++){
711 struct optionaltaskdescriptor ** tmpptr = optionaltaskdescriptorarray[counter];
712 printf("Task %s\n", tmpptr[0]->task->name);
717 struct parameterwrapper * prevptr = NULL;
718 struct parameterwrapper * flagptr = objectqueues[currobj->type];
719 removeoptionalfromqueues(currobj->hashcode,currobj, flagptr);
720 /*process for each otd*/
721 for(counter = 0; counter<numoptionaltaskdescriptors; counter++){
722 struct parameterwrapper * parameter = objectqueues[currobj->type];
723 struct optionaltaskdescriptor ** tmpptr = optionaltaskdescriptorarray[counter];
724 struct optionaltaskdescriptor * currotd = tmpptr[0];
726 while(parameter->task != currotd->task)
727 parameter=parameter->next;
729 printf("found parameterwrapper for task : %s\n", parameter->task->name);
732 struct ___Object___ * newobj = RUNMALLOC(sizeof(struct ___Object___));
733 (*newobj)=(*currobj);
734 newobj->numotds=numgoodfswrappers;
735 newobj->otds=RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * numgoodfswrappers);
736 for(otd_counter=0; otd_counter<numgoodfswrappers; otd_counter++){
737 newobj->otds[otd_counter]=tmpptr[otd_counter];
739 enqueuetasks(parameter, prevptr, newobj);
749 /*if(currobj->exitfses!=NULL) RUNFREE(currobj->exitfses);
750 if(currobj->otds!=NULL) RUNFREE(currobj->otds);*///there has been a problem just before the program exit, maybe due to the GC ?
751 RUNFREE(optionaltaskdescriptorarray);
755 /*we need to check if the object is optional, in this case, test the predicate*/
756 /*here is the code for predicate checking*/
757 /*The code has not been tested. I don't even know if it is working or efficient but it is a lead...
758 if(currotd->numpredicatemembers == 0){
759 printf("this task can be fired\n");
764 int predicatetrue = 0;
765 for(pred_counter = 0; pred_counter<currotd->numpredicatemembers; pred_counter++){
766 struct predicatemember * currpred = currotd->predicatememberarray[pred_counter];
767 printf("predicate type : %i\n", currpred->type);
769 //test if the predicate member is true
770 struct parameterwrapper * paramwrapper = objectqueues[currpred->type];
771 while(paramwrapper!=NULL){
772 struct RuntimeIterator * it = allocateRuntimeIterator(paramwrapper->objectset->listhead);
774 struct ___Object___ * obj = (struct ___Object___ *)Runkey(it);
775 printf("obj type : %i\n", obj->type);
776 if(obj->type == currpred->type){
778 printf("predicate to test\n");
783 }while(RunhasNext(it));
784 paramwrapper=paramwrapper->next;
786 printf("not the good predicate");
789 //the predicate members have to be all true
794 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
795 void * taskpointerarray[MAXTASKPARAMS];
797 int numparams=parameter->task->numParameters;
798 int numiterators=parameter->task->numTotal-1;
800 struct taskdescriptor * task=parameter->task;
802 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);//this add the object to parameterwrapper
804 /* Add enqueued object to parameter vector */
805 taskpointerarray[parameter->slot]=ptr;
807 /* Reset iterators */
808 for(j=0;j<numiterators;j++) {
809 toiReset(¶meter->iterators[j]);
812 /* Find initial state */
813 for(j=0;j<numiterators;j++) {
815 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
816 toiNext(¶meter->iterators[j], taskpointerarray);
818 /* Need to backtrack */
819 toiReset(¶meter->iterators[j]);
823 /* Nothing to enqueue */
830 /* Enqueue current state */
832 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
834 tpd->numParameters=numiterators+1;
835 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
836 for(j=0;j<=numiterators;j++){
839 struct ___Object___ * obj = (struct ___Object___ *)taskpointerarray[j];
840 if(obj->failedstatus==1)
841 printf("parameter %i used as optional for task %s\n", obj->type, task->name);
844 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
847 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
848 genputtable(activetasks, tpd, tpd);
850 RUNFREE(tpd->parameterArray);
854 /* This loop iterates to the next parameter combination */
858 for(j=numiterators-1; j<numiterators;j++) {
860 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
861 toiNext(¶meter->iterators[j], taskpointerarray);
863 /* Need to backtrack */
864 toiReset(¶meter->iterators[j]);
868 /* Nothing more to enqueue */
875 /* Handler for signals. The signals catch null pointer errors and
876 arithmatic errors. */
878 void myhandler(int sig, siginfo_t *info, void *uap) {
881 printf("sig=%d\n",sig);
884 sigemptyset(&toclear);
885 sigaddset(&toclear, sig);
886 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
887 longjmp(error_handler,1);
892 struct RuntimeHash *fdtoobject;
894 void addreadfd(int fd) {
897 FD_SET(fd, &readfds);
900 void removereadfd(int fd) {
901 FD_CLR(fd, &readfds);
902 if (maxreadfd==(fd+1)) {
904 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
915 void executetasks() {
916 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
918 /* Set up signal handlers */
919 struct sigaction sig;
920 sig.sa_sigaction=&myhandler;
921 sig.sa_flags=SA_SIGINFO;
922 sigemptyset(&sig.sa_mask);
924 /* Catch bus errors, segmentation faults, and floating point exceptions*/
925 sigaction(SIGBUS,&sig,0);
926 sigaction(SIGSEGV,&sig,0);
927 sigaction(SIGFPE,&sig,0);
928 sigaction(SIGPIPE,&sig,0);
933 fdtoobject=allocateRuntimeHash(100);
935 /* Map first block of memory to protected, anonymous page */
936 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
939 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
941 /* Check if any filedescriptors have IO pending */
944 struct timeval timeout={0,0};
948 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
950 /* Process ready fd's */
952 for(fd=0;fd<maxreadfd;fd++) {
953 if (FD_ISSET(fd, &tmpreadfds)) {
954 /* Set ready flag on object */
956 // printf("Setting fd %d\n",fd);
957 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
958 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
965 /* See if there are any active tasks */
966 if (hashsize(activetasks)>0) {
968 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
969 genfreekey(activetasks, currtpd);
971 /* Check if this task has failed, allow a task that contains optional objects to fire */
972 if (gencontains(failedtasks, currtpd)) {
973 // Free up task parameter descriptor
974 RUNFREE(currtpd->parameterArray);
978 int numparams=currtpd->task->numParameters;
979 int numtotal=currtpd->task->numTotal;
981 /* Make sure that the parameters are still in the queues */
982 for(i=0;i<numparams;i++) {
983 void * parameter=currtpd->parameterArray[i];
984 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
985 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
987 /* Check that object is still in queue */
989 struct ___Object___ * obj = (struct ___Object___ *)parameter;
990 if(obj->failedstatus==1){
991 struct ___Object___ *temp=NULL;
992 struct parameterwrapper * ptr;
993 struct RuntimeNode * node = pw->objectset->listhead;
995 temp=(struct ___Object___ *)node->key;
996 if(temp->failedstatus==1 && temp->hashcode==obj->hashcode){
998 goto parameterpresent;
1002 RUNFREE(currtpd->parameterArray);
1009 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
1010 RUNFREE(currtpd->parameterArray);
1017 /* Check that object still has necessary tags */
1018 for(j=0;j<pd->numbertags;j++) {
1019 int slotid=pd->tagarray[2*j]+numparams;
1020 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1021 if (!containstag(parameter, tagd)) {
1022 RUNFREE(currtpd->parameterArray);
1028 taskpointerarray[i+OFFSET]=parameter;
1031 for(;i<numtotal;i++) {
1032 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1036 /* Checkpoint the state */
1037 forward=allocateRuntimeHash(100);
1038 reverse=allocateRuntimeHash(100);
1039 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1041 if (x=setjmp(error_handler)) {
1045 printf("Fatal Error=%d, Recovering!\n",x);
1047 genputtable(failedtasks,currtpd,currtpd);
1048 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1052 printf("%i object(s) restored\n", currtpd->task->numParameters);
1055 for(counter=0; counter<currtpd->task->numParameters; counter++){
1056 //remove the object from the previous parameterwrapper (maybe not necessary)
1057 //do a new instance of the object. It allows the restored object to be used by other tasks as a non optional arg.
1058 struct ___Object___ * currobj = RUNMALLOC(sizeof(struct ___Object___));
1059 (*currobj)=(*(struct ___Object___ *)currtpd->parameterArray[counter]);
1060 currobj->numexitfses = 0;
1061 currobj->exitfses = NULL;
1063 currobj->hashcode=(int)currobj;
1064 enqueueoptional( currobj );
1067 freeRuntimeHash(forward);
1068 freeRuntimeHash(reverse);
1073 if (injectfailures) {
1074 if ((((double)random())/RAND_MAX)<failurechance) {
1075 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1076 longjmp(error_handler,10);
1079 /* Actually call task */
1081 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
1082 taskpointerarray[1]=NULL;
1085 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1086 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1087 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1089 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1091 freeRuntimeHash(forward);
1092 freeRuntimeHash(reverse);
1094 // Free up task parameter descriptor
1095 RUNFREE(currtpd->parameterArray);
1105 /* This function processes an objects tags */
1106 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1109 for(i=0;i<pd->numbertags;i++) {
1110 int slotid=pd->tagarray[2*i];
1111 int tagid=pd->tagarray[2*i+1];
1113 if (statusarray[slotid+numparams]==0) {
1114 parameter->iterators[*iteratorcount].istag=1;
1115 parameter->iterators[*iteratorcount].tagid=tagid;
1116 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1117 parameter->iterators[*iteratorcount].tagobjectslot=index;
1118 statusarray[slotid+numparams]=1;
1125 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1128 struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1130 parameter->iterators[*iteratorcount].istag=0;
1131 parameter->iterators[*iteratorcount].slot=index;
1132 parameter->iterators[*iteratorcount].objectset=objectset;
1133 statusarray[index]=1;
1135 for(i=0;i<pd->numbertags;i++) {
1136 int slotid=pd->tagarray[2*i];
1137 int tagid=pd->tagarray[2*i+1];
1138 if (statusarray[slotid+numparams]!=0) {
1139 /* This tag has already been enqueued, use it to narrow search */
1140 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1144 parameter->iterators[*iteratorcount].numtags=tagcount;
1149 /* This function builds the iterators for a task & parameter */
1151 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1152 int statusarray[MAXTASKPARAMS];
1154 int numparams=task->numParameters;
1155 int iteratorcount=0;
1156 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1158 statusarray[index]=1; /* Initial parameter */
1159 /* Process tags for initial iterator */
1161 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1165 /* Check for objects with existing tags */
1166 for(i=0;i<numparams;i++) {
1167 if (statusarray[i]==0) {
1168 struct parameterdescriptor *pd=task->descriptorarray[i];
1170 for(j=0;j<pd->numbertags;j++) {
1171 int slotid=pd->tagarray[2*j];
1172 if(statusarray[slotid+numparams]!=0) {
1173 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1174 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1181 /* Next do objects w/ unbound tags*/
1183 for(i=0;i<numparams;i++) {
1184 if (statusarray[i]==0) {
1185 struct parameterdescriptor *pd=task->descriptorarray[i];
1186 if (pd->numbertags>0) {
1187 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1188 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1194 /* Nothing with a tag enqueued */
1196 for(i=0;i<numparams;i++) {
1197 if (statusarray[i]==0) {
1198 struct parameterdescriptor *pd=task->descriptorarray[i];
1199 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1200 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1213 /* This function processes the task information to create queues for
1214 each parameter type. */
1216 void processtasks() {
1218 for(i=0;i<numtasks;i++) {
1219 struct taskdescriptor * task=taskarray[i];
1222 for(j=0;j<task->numParameters;j++) {
1223 struct parameterdescriptor *param=task->descriptorarray[j];
1224 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1225 struct parameterwrapper ** ptr=&objectqueues[param->type];
1227 param->queue=parameter;
1228 parameter->objectset=allocateRuntimeHash(10);
1229 parameter->numberofterms=param->numberterms;
1230 parameter->intarray=param->intarray;
1231 parameter->numbertags=param->numbertags;
1232 parameter->tagarray=param->tagarray;
1233 parameter->task=task;
1234 /* Link new queue in */
1236 ptr=&((*ptr)->next);
1240 /* Build iterators for parameters */
1241 for(j=0;j<task->numParameters;j++) {
1242 struct parameterdescriptor *param=task->descriptorarray[j];
1243 struct parameterwrapper *parameter=param->queue;
1245 builditerators(task, j, parameter);
1250 void toiReset(struct tagobjectiterator * it) {
1253 } else if (it->numtags>0) {
1256 RuntimeHashiterator(it->objectset, &it->it);
1260 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
1263 /* Get object with tags */
1264 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1265 struct ___Object___ *tagptr=obj->___tags___;
1266 if (tagptr->type==TAGTYPE) {
1267 if ((it->tagobjindex==0)&& /* First object */
1268 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1273 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1274 int tagindex=it->tagobjindex;
1275 for(;tagindex<ao->___cachedCode___;tagindex++) {
1276 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1277 if (td->flag==it->tagid) {
1278 it->tagobjindex=tagindex; /* Found right type of tag */
1284 } else if (it->numtags>0) {
1285 /* Use tags to locate appropriate objects */
1286 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1287 struct ___Object___ *objptr=tag->flagptr;
1289 if (objptr->type!=OBJECTARRAYTYPE) {
1290 if (it->tagobjindex>0)
1292 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
1294 for(i=1;i<it->numtags;i++) {
1295 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1296 if (!containstag(objptr,tag2))
1301 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1304 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1305 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1306 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
1308 for(i=1;i<it->numtags;i++) {
1309 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1310 if (!containstag(objptr,tag2))
1317 it->tagobjindex=tagindex;
1321 return RunhasNext(&it->it);
1325 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1327 struct ___Object___ * objptr=tag->flagptr;
1328 if (objptr->type==OBJECTARRAYTYPE) {
1329 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1330 for(j=0;j<ao->___cachedCode___;j++) {
1331 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1339 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
1340 /* hasNext has all of the intelligence */
1343 /* Get object with tags */
1344 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1345 struct ___Object___ *tagptr=obj->___tags___;
1346 if (tagptr->type==TAGTYPE) {
1348 objectarray[it->slot]=tagptr;
1350 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1351 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1353 } else if (it->numtags>0) {
1354 /* Use tags to locate appropriate objects */
1355 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1356 struct ___Object___ *objptr=tag->flagptr;
1357 if (objptr->type!=OBJECTARRAYTYPE) {
1359 objectarray[it->slot]=objptr;
1361 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1362 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1365 /* Iterate object */
1366 objectarray[it->slot]=(void *)Runkey(&it->it);