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;
31 int main(int argc, char **argv) {
33 GC_init(); // Initialize the garbage collector
39 initializeexithandler();
40 /* Create table for failed tasks */
41 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
42 (int (*)(void *,void *)) &comparetpd);
43 /* Create queue of active tasks */
44 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
45 (int (*)(void *,void *)) &comparetpd);
47 /* Process task information */
50 /* Create startup object */
51 createstartupobject(argc, argv);
53 /* Start executing the tasks */
57 void createstartupobject(int argc, char ** argv) {
60 /* Allocate startup object */
62 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
63 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
65 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
66 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
68 /* Build array of strings */
69 startupobject->___parameters___=stringarray;
71 int length=strlen(argv[i]);
73 struct ___String___ *newstring=NewString(NULL, argv[i],length);
75 struct ___String___ *newstring=NewString(argv[i],length);
77 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
80 /* Set initialized flag for startup object */
81 flagorand(startupobject,1,0xFFFFFFFF);
84 int hashCodetpd(struct taskparamdescriptor *ftd) {
85 int hash=(int)ftd->task;
87 for(i=0;i<ftd->numParameters;i++){
88 hash^=(int)ftd->parameterArray[i];
93 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
95 if (ftd1->task!=ftd2->task)
97 for(i=0;i<ftd1->numParameters;i++)
98 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
101 for(i=0;i<ftd1->numParameters;i++) {
102 if(ftd1->failed[i]!=ftd2->failed[i])
109 /* This function sets a tag. */
111 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
113 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
115 struct ___Object___ * tagptr=obj->___tags___;
117 obj->___tags___=(struct ___Object___ *)tagd;
119 /* Have to check if it is already set */
120 if (tagptr->type==TAGTYPE) {
121 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
125 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
126 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
127 obj=(struct ___Object___ *)ptrarray[2];
128 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
129 td=(struct ___TagDescriptor___ *) obj->___tags___;
131 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
133 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
134 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
135 obj->___tags___=(struct ___Object___ *) ao;
136 ao->___cachedCode___=2;
140 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
141 for(i=0;i<ao->___cachedCode___;i++) {
142 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
146 if (ao->___cachedCode___<ao->___length___) {
147 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
148 ao->___cachedCode___++;
151 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
152 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
153 obj=(struct ___Object___ *)ptrarray[2];
154 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
155 ao=(struct ArrayObject *)obj->___tags___;
157 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
159 aonew->___cachedCode___=ao->___length___+1;
160 for(i=0;i<ao->___length___;i++) {
161 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
163 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
169 struct ___Object___ * tagset=tagd->flagptr;
172 } else if (tagset->type!=OBJECTARRAYTYPE) {
174 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
175 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
176 obj=(struct ___Object___ *)ptrarray[2];
177 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
179 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
181 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
182 ARRAYSET(ao, struct ___Object___ *, 1, obj);
183 ao->___cachedCode___=2;
184 tagd->flagptr=(struct ___Object___ *)ao;
186 struct ArrayObject *ao=(struct ArrayObject *) tagset;
187 if (ao->___cachedCode___<ao->___length___) {
188 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
192 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
193 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
194 obj=(struct ___Object___ *)ptrarray[2];
195 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
196 ao=(struct ArrayObject *)tagd->flagptr;
198 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
200 aonew->___cachedCode___=ao->___cachedCode___+1;
201 for(i=0;i<ao->___length___;i++) {
202 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
204 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
205 tagd->flagptr=(struct ___Object___ *) ao;
211 /* This function clears a tag. */
213 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
215 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
217 /* We'll assume that tag is alway there.
218 Need to statically check for this of course. */
219 struct ___Object___ * tagptr=obj->___tags___;
221 if (tagptr->type==TAGTYPE) {
222 if ((struct ___TagDescriptor___ *)tagptr==tagd)
223 obj->___tags___=NULL;
225 printf("ERROR 1 in tagclear\n");
227 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
229 for(i=0;i<ao->___cachedCode___;i++) {
230 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
232 ao->___cachedCode___--;
233 if (i<ao->___cachedCode___)
234 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
235 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
236 if (ao->___cachedCode___==0)
237 obj->___tags___=NULL;
241 printf("ERROR 2 in tagclear\n");
245 struct ___Object___ *tagset=tagd->flagptr;
246 if (tagset->type!=OBJECTARRAYTYPE) {
250 printf("ERROR 3 in tagclear\n");
252 struct ArrayObject *ao=(struct ArrayObject *) tagset;
254 for(i=0;i<ao->___cachedCode___;i++) {
255 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
257 ao->___cachedCode___--;
258 if (i<ao->___cachedCode___)
259 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
260 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
261 if (ao->___cachedCode___==0)
266 printf("ERROR 4 in tagclear\n");
274 /* This function allocates a new tag. */
276 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
277 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
279 struct ___TagDescriptor___ * allocate_tag(int index) {
280 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
289 /* This function updates the flag for object ptr. It or's the flag
290 with the or mask and and's it with the andmask. */
292 void flagbody(struct ___Object___ *ptr, int flag);
294 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
297 int flagcomp(const int *val1, const int *val2) {
298 return (*val1)-(*val2);
301 void flagorand(void * ptr, int ormask, int andmask) {
303 struct ___Object___ * obj = (struct ___Object___ *)ptr;
304 if(obj->numfses){/*store the information about fses*/
305 int flag, i, j,counter, offset=0;
306 for(i=0;i<obj->numfses;i++) {
308 counter=obj->fses[offset++];
310 for(j=0;j<counter;j++) {
311 flag=obj->fses[offset];
312 obj->fses[offset++]=(flag|ormask)&andmask;
314 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
316 enqueueoptional(obj, 0, NULL, NULL, 0);
321 int oldflag=((int *)ptr)[1];
322 int flag=ormask|oldflag;
328 void intflagorand(void * ptr, int ormask, int andmask) {
330 struct ___Object___ * obj = (struct ___Object___ *)ptr;
331 if(obj->numfses) {/*store the information about fses*/
332 int flag, i, j,counter, offset=0;
333 for(i=0;i<obj->numfses;i++) {
335 counter=obj->fses[offset++];
337 for(j=0;j<counter;j++) {
338 flag=obj->fses[offset];
339 obj->fses[offset++]=(flag|ormask)&andmask;
341 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
343 enqueueoptional(obj, 0, NULL, NULL, 0);
348 int oldflag=((int *)ptr)[1];
349 int flag=ormask|oldflag;
351 if (flag==oldflag) /* Don't do anything */
353 else flagbody(ptr, flag);
357 void flagorandinit(void * ptr, int ormask, int andmask) {
358 int oldflag=((int *)ptr)[1];
359 int flag=ormask|oldflag;
364 void flagbody(struct ___Object___ *ptr, int flag) {
365 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
368 /*Remove object from all queues */
369 while(flagptr!=NULL) {
370 struct parameterwrapper *next;
371 int UNUSED, UNUSED2, UNUSED3;
372 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, &UNUSED, &UNUSED2, &UNUSED3);
373 ObjectHashremove(flagptr->objectset, (int)ptr);
378 struct QueueItem *tmpptr;
379 struct parameterwrapper * parameter=objectqueues[ptr->type];
381 struct parameterwrapper * prevptr=NULL;
382 struct ___Object___ *tagptr=ptr->___tags___;
384 /* Outer loop iterates through all parameter queues an object of
385 this type could be in. */
387 while(parameter!=NULL) {
389 if (parameter->numbertags>0) {
391 goto nextloop;//that means the object has no tag but that param needs tag
392 else if(tagptr->type==TAGTYPE) {//one tag
393 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
394 for(i=0;i<parameter->numbertags;i++) {
395 //slotid is parameter->tagarray[2*i];
396 int tagid=parameter->tagarray[2*i+1];
397 if (tagid!=tagptr->flag)
398 goto nextloop; /*We don't have this tag */
400 } else {//multiple tags
401 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
402 for(i=0;i<parameter->numbertags;i++) {
403 //slotid is parameter->tagarray[2*i];
404 int tagid=parameter->tagarray[2*i+1];
406 for(j=0;j<ao->___cachedCode___;j++) {
407 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
418 for(i=0;i<parameter->numberofterms;i++) {
419 int andmask=parameter->intarray[i*2];
420 int checkmask=parameter->intarray[i*2+1];
421 if ((flag&andmask)==checkmask) {
422 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
428 parameter=parameter->next;
430 ptr->flagptr=prevptr;
436 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
438 struct ___Object___ * tagptr = currobj->___tags___;
439 if(fswrapper->numtags>0){
441 return 0; //that means the object has no tag but that param
443 else if(tagptr->type==TAGTYPE) {//one tag
444 if(fswrapper->numtags!=1)
445 return 0; //we don't have the right number of tags
446 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
447 if (fswrapper->tags[0]!=tagptr->flag)
449 } else { //multiple tags
450 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
454 if(ao->___length___!=fswrapper->numtags)
455 return 0;//we don't have the right number of tags
456 for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
457 int tagid=fswrapper->tags[tag_counter];
459 for(j=0;j<ao->___cachedCode___;j++) {
460 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
470 int getlength(int *flist, int len) {
474 int size=flist[count];
480 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
481 int size1=getlength(flist1, len1);
482 int size2=getlength(flist2, len2);
483 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
484 memcpy(merge, flist1, size1*sizeof(int));
485 memcpy(&merge[size1], flist2, size2*sizeof(int));
489 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
493 while(i<len1||j<len2) {
494 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
496 merge[count]=flist1[i];
500 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
502 merge[count]=flist2[j];
506 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
508 merge[count]=flist1[i];
518 /* Merge flags from ftlmerge into ftl. */
519 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
524 for(i=0;i<ftl->numflags;i++) {
525 int len=ftl->flags[offset++];
527 for(j=0;j<ftlmerge->numflags;j++) {
528 int lenmerge=ftlmerge->flags[offsetmerge++];
529 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
530 offsetmerge+=lenmerge;
534 mergedlist=RUNMALLOC(sizeof(int)*length);
538 for(i=0;i<ftl->numflags;i++) {
539 int len=ftl->flags[offset++];
541 for(j=0;j<ftlmerge->numflags;j++) {
542 int lenmerge=ftlmerge->flags[offsetmerge++];
543 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
544 mergedlist[length]=size;
549 ftl->flags=mergedlist;
550 ftl->numflags*=ftlmerge->numflags;
553 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
554 struct failedtasklist *tmpptr;
555 while((*andlist)!=NULL) {
556 struct failedtasklist *searchftl=list;
557 while(searchftl!=NULL) {
558 if ((*andlist)->task==searchftl->task&&
559 (*andlist)->index==searchftl->index) {
560 mergeitems(*andlist, searchftl);
563 searchftl=searchftl->next;
565 if (searchftl==NULL) {
566 //didn't find andlist
568 *andlist=(*andlist)->next;//splice item out of list
569 RUNFREE(tmpptr->flags); //free the item
572 andlist=&((*andlist)->next); //iterate to next item
575 //free the list we're searching
578 RUNFREE(list->flags);
584 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
585 struct failedtasklist *list=NULL;
587 struct fsanalysiswrapper *fswrapper=NULL;
588 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
589 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
590 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
591 //we only match exactly here
598 for(i=0;i<fswrapper->numtaskfailures;i++) {
600 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
601 if (taskfail->task==task&&taskfail->index==index) {
603 while(start<taskfail->numoptionaltaskdescriptors) {
604 struct taskdescriptor *currtask=NULL;
605 struct failedtasklist *tmpftl;
609 int numenterflags, offset;
610 struct parameterwrapper *pw;
611 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
612 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
615 currindex=otd->index;
616 } else if (currtask!=otd->task||currindex!=otd->index)
618 totallength+=otd->numenterflags;
620 pw=currtask->descriptorarray[currindex]->queue;
621 enterflags=RUNMALLOC(totallength*sizeof(int));
622 numenterflags=j-start;
624 for(start;start<j;start++) {
625 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
626 enterflags[offset++]=otd->numenterflags;
627 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
628 offset+=otd->numenterflags;
630 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
632 tmpftl->task=currtask;
633 tmpftl->numflags=numenterflags;
634 tmpftl->flags=enterflags;
642 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
643 struct failedtasklist *list=NULL;
646 struct fsanalysiswrapper *fswrapper=NULL;
647 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
648 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
649 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
650 //we only match exactly here
658 while(start<fswrapper->numoptionaltaskdescriptors) {
659 struct taskdescriptor *currtask=NULL;
660 struct failedtasklist *tmpftl;
665 int numenterflags, offset;
666 struct parameterwrapper *pw;
667 for(j=start;j<fswrapper->numoptionaltaskdescriptors;j++) {
668 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
671 currindex=otd->index;
672 } else if (currtask!=otd->task||currindex!=otd->index)
674 totallength+=otd->numenterflags;
676 pw=currtask->descriptorarray[currindex]->queue;
677 enterflags=RUNMALLOC(totallength*sizeof(int));
678 numenterflags=j-start;
680 for(start;start<j;start++) {
681 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
682 enterflags[offset++]=otd->numenterflags;
683 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
684 offset+=otd->numenterflags;
686 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
688 tmpftl->task=currtask;
689 tmpftl->numflags=numenterflags;
690 tmpftl->flags=enterflags;
698 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
699 while(andlist!=NULL) {
700 struct failedtasklist *tmp=andlist;
701 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
702 struct parmaeterwrapper *next;
707 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
710 andlist=andlist->next;
715 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
716 struct classanalysiswrapper * classwrapper=NULL;
718 /*test what optionaltaskdescriptors are available, find the class
720 if (classanalysiswrapperarray[currobj->type]!=NULL) {
721 classwrapper = classanalysiswrapperarray[currobj->type];
726 /* We have a failure */
727 if (failedfses==NULL) {
728 /* Failed in normal state */
729 /*first time the method is invoked*/
731 struct fsanalysiswrapper *fswrapper=NULL;
733 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
734 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
735 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
736 //we only match exactly here
741 if(fswrapper==NULL) //nothing to do in this state
743 for(i=0;i<fswrapper->numtaskfailures;i++) {
745 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
746 if (taskfail->task==task&&taskfail->index==index) {
748 while(start<taskfail->numoptionaltaskdescriptors) {
749 struct taskdescriptor *currtask=NULL;
753 int numenterflags, offset;
754 struct parameterwrapper *pw;
755 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
756 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
759 currindex=otd->index;
760 } else if (currtask!=otd->task||currindex!=otd->index)
762 totallength+=otd->numenterflags;
764 pw=currtask->descriptorarray[currindex]->queue;
765 enterflags=RUNMALLOC(totallength*sizeof(int));
766 numenterflags=j-start;
769 for(start;start<j;start++) {
770 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
771 enterflags[offset++]=otd->numenterflags;
772 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
773 offset+=otd->numenterflags;
776 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
782 /* Failed in failed state */
785 for(i=0;i<numfailedfses;i++) {
786 int numfses=failedfses[offset++];
788 struct failedtasklist *andlist=NULL;
789 for(j=0;j<numfses;j++) {
790 int flagstate=failedfses[offset++];
791 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
795 mergefailedlists(&andlist, currlist);
797 enqueuelist(currobj, andlist);
801 /* No failure, but we are in a failed state */
802 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
804 /*Remove object from all queues */
805 while(flagptr!=NULL) {
806 struct parameterwrapper *next;
807 int UNUSED, UNUSED2, UNUSED3;
808 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, &UNUSED, &UNUSED2, &UNUSED3);
809 ObjectHashremove(flagptr->objectset, (int)currobj);
813 /* Failed in failed state */
816 for(i=0;i<currobj->numfses;i++) {
817 int numfses=currobj->fses[offset++];
819 struct failedtasklist *andlist=NULL;
820 for(j=0;j<numfses;j++) {
821 int flagstate=currobj->fses[offset++];
822 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
826 mergefailedlists(&andlist, currlist);
828 enqueuelist(currobj, andlist);
836 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
837 void * taskpointerarray[MAXTASKPARAMS];
839 int failed[MAXTASKPARAMS];
842 int numparams=parameter->task->numParameters;
843 int numiterators=parameter->task->numTotal-1;
848 struct taskdescriptor * task=parameter->task;
850 if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
851 /* The object is already here...or it with the existing item */
857 ObjectHashget(parameter->objectset, (int) ptr, & oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
858 mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
859 ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
864 //only add if truly needed
872 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
875 /* Add enqueued object to parameter vector */
876 taskpointerarray[parameter->slot]=ptr;
878 failed[parameter->slot]=(enterflags!=NULL);
881 /* Reset iterators */
882 for(j=0;j<numiterators;j++) {
883 toiReset(¶meter->iterators[j]);
886 /* Find initial state */
887 for(j=0;j<numiterators;j++) {
889 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
890 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
892 /* Need to backtrack */
893 toiReset(¶meter->iterators[j]);
897 /* Nothing to enqueue */
904 /* Enqueue current state */
906 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
908 tpd->numParameters=numiterators+1;
909 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
911 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
913 for(j=0;j<=numiterators;j++){
914 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
916 tpd->failed[j]=failed[j];
920 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
921 genputtable(activetasks, tpd, tpd);
923 RUNFREE(tpd->parameterArray);
927 /* This loop iterates to the next parameter combination */
931 for(j=numiterators-1; j<numiterators;j++) {
933 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
934 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
936 /* Need to backtrack */
937 toiReset(¶meter->iterators[j]);
941 /* Nothing more to enqueue */
949 /* Handler for signals. The signals catch null pointer errors and
950 arithmatic errors. */
952 void myhandler(int sig, siginfo_t *info, void *uap) {
955 printf("sig=%d\n",sig);
958 sigemptyset(&toclear);
959 sigaddset(&toclear, sig);
960 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
961 longjmp(error_handler,1);
966 struct RuntimeHash *fdtoobject;
968 void addreadfd(int fd) {
971 FD_SET(fd, &readfds);
974 void removereadfd(int fd) {
975 FD_CLR(fd, &readfds);
976 if (maxreadfd==(fd+1)) {
978 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
990 int * fsescopy(int *src, int len) {
994 dst=RUNMALLOC(len*sizeof(int));
995 memcpy(dst, src, len*sizeof(int));
1000 void executetasks() {
1001 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1003 int * fsesarray[MAXTASKPARAMS];
1004 int * oldfsesarray[MAXTASKPARAMS];
1005 int numfsesarray[MAXTASKPARAMS];
1008 /* Set up signal handlers */
1009 struct sigaction sig;
1010 sig.sa_sigaction=&myhandler;
1011 sig.sa_flags=SA_SIGINFO;
1012 sigemptyset(&sig.sa_mask);
1014 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1015 sigaction(SIGBUS,&sig,0);
1016 sigaction(SIGSEGV,&sig,0);
1017 sigaction(SIGFPE,&sig,0);
1018 sigaction(SIGPIPE,&sig,0);
1023 fdtoobject=allocateRuntimeHash(100);
1025 /* Map first block of memory to protected, anonymous page */
1026 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1029 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1031 /* Check if any filedescriptors have IO pending */
1034 struct timeval timeout={0,0};
1038 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1040 /* Process ready fd's */
1042 for(fd=0;fd<maxreadfd;fd++) {
1043 if (FD_ISSET(fd, &tmpreadfds)) {
1044 /* Set ready flag on object */
1046 // printf("Setting fd %d\n",fd);
1047 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1048 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
1055 /* See if there are any active tasks */
1056 if (hashsize(activetasks)>0) {
1058 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1059 genfreekey(activetasks, currtpd);
1061 /* Check if this task has failed, allow a task that contains optional objects to fire */
1062 if (gencontains(failedtasks, currtpd)) {
1063 // Free up task parameter descriptor
1064 RUNFREE(currtpd->parameterArray);
1068 int numparams=currtpd->task->numParameters;
1069 int numtotal=currtpd->task->numTotal;
1071 /* Make sure that the parameters are still in the queues */
1072 for(i=0;i<numparams;i++) {
1073 void * parameter=currtpd->parameterArray[i];
1074 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1075 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1077 /* Check that object is still in queue */
1080 int UNUSED, UNUSED2;
1082 int numflags, isnonfailed;
1083 int failed=currtpd->failed[i];
1084 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1085 RUNFREE(currtpd->parameterArray);
1086 RUNFREE(currtpd->failed);
1090 if (failed&&(flags!=NULL)) {
1093 numfsesarray[i]=numflags;
1094 } else if (!failed && isnonfailed) {
1095 //Non-failed parameter
1099 RUNFREE(currtpd->parameterArray);
1100 RUNFREE(currtpd->failed);
1108 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1109 RUNFREE(currtpd->parameterArray);
1117 /* Check that object still has necessary tags */
1118 for(j=0;j<pd->numbertags;j++) {
1119 int slotid=pd->tagarray[2*j]+numparams;
1120 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1121 if (!containstag(parameter, tagd)) {
1122 RUNFREE(currtpd->parameterArray);
1128 taskpointerarray[i+OFFSET]=parameter;
1131 for(;i<numtotal;i++) {
1132 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1136 /* Checkpoint the state */
1137 forward=allocateRuntimeHash(100);
1138 reverse=allocateRuntimeHash(100);
1139 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1141 if (x=setjmp(error_handler)) {
1145 printf("Fatal Error=%d, Recovering!\n",x);
1147 genputtable(failedtasks,currtpd,currtpd);
1148 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1151 for(counter=0; counter<currtpd->task->numParameters; counter++){
1153 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1156 if (fsesarray[counter]!=NULL)
1157 RUNFREE(fsesarray[counter]);
1160 freeRuntimeHash(forward);
1161 freeRuntimeHash(reverse);
1166 if (injectfailures) {
1167 if ((((double)random())/RAND_MAX)<failurechance) {
1168 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1169 longjmp(error_handler,10);
1172 /* Actually call task */
1174 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
1175 taskpointerarray[1]=NULL;
1178 //get the task flags set
1179 for(i=0;i<numparams;i++) {
1180 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1181 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1182 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1186 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1187 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1188 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1190 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1192 for(i=0;i<numparams;i++) {
1194 if(oldfsesarray[i]!=NULL)
1195 RUNFREE(oldfsesarray[i]);
1198 freeRuntimeHash(forward);
1199 freeRuntimeHash(reverse);
1201 // Free up task parameter descriptor
1202 RUNFREE(currtpd->parameterArray);
1212 /* This function processes an objects tags */
1213 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1216 for(i=0;i<pd->numbertags;i++) {
1217 int slotid=pd->tagarray[2*i];
1218 int tagid=pd->tagarray[2*i+1];
1220 if (statusarray[slotid+numparams]==0) {
1221 parameter->iterators[*iteratorcount].istag=1;
1222 parameter->iterators[*iteratorcount].tagid=tagid;
1223 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1224 parameter->iterators[*iteratorcount].tagobjectslot=index;
1225 statusarray[slotid+numparams]=1;
1232 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1235 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1237 parameter->iterators[*iteratorcount].istag=0;
1238 parameter->iterators[*iteratorcount].slot=index;
1239 parameter->iterators[*iteratorcount].objectset=objectset;
1240 statusarray[index]=1;
1242 for(i=0;i<pd->numbertags;i++) {
1243 int slotid=pd->tagarray[2*i];
1244 int tagid=pd->tagarray[2*i+1];
1245 if (statusarray[slotid+numparams]!=0) {
1246 /* This tag has already been enqueued, use it to narrow search */
1247 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1251 parameter->iterators[*iteratorcount].numtags=tagcount;
1256 /* This function builds the iterators for a task & parameter */
1258 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1259 int statusarray[MAXTASKPARAMS];
1261 int numparams=task->numParameters;
1262 int iteratorcount=0;
1263 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1265 statusarray[index]=1; /* Initial parameter */
1266 /* Process tags for initial iterator */
1268 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1272 /* Check for objects with existing tags */
1273 for(i=0;i<numparams;i++) {
1274 if (statusarray[i]==0) {
1275 struct parameterdescriptor *pd=task->descriptorarray[i];
1277 for(j=0;j<pd->numbertags;j++) {
1278 int slotid=pd->tagarray[2*j];
1279 if(statusarray[slotid+numparams]!=0) {
1280 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1281 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1288 /* Next do objects w/ unbound tags*/
1290 for(i=0;i<numparams;i++) {
1291 if (statusarray[i]==0) {
1292 struct parameterdescriptor *pd=task->descriptorarray[i];
1293 if (pd->numbertags>0) {
1294 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1295 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1301 /* Nothing with a tag enqueued */
1303 for(i=0;i<numparams;i++) {
1304 if (statusarray[i]==0) {
1305 struct parameterdescriptor *pd=task->descriptorarray[i];
1306 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1307 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1320 /* This function processes the task information to create queues for
1321 each parameter type. */
1323 void processtasks() {
1325 for(i=0;i<numtasks;i++) {
1326 struct taskdescriptor * task=taskarray[i];
1329 for(j=0;j<task->numParameters;j++) {
1330 struct parameterdescriptor *param=task->descriptorarray[j];
1331 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1332 struct parameterwrapper ** ptr=&objectqueues[param->type];
1334 param->queue=parameter;
1335 parameter->objectset=allocateObjectHash(10);
1336 parameter->numberofterms=param->numberterms;
1337 parameter->intarray=param->intarray;
1338 parameter->numbertags=param->numbertags;
1339 parameter->tagarray=param->tagarray;
1340 parameter->task=task;
1341 /* Link new queue in */
1343 ptr=&((*ptr)->next);
1347 /* Build iterators for parameters */
1348 for(j=0;j<task->numParameters;j++) {
1349 struct parameterdescriptor *param=task->descriptorarray[j];
1350 struct parameterwrapper *parameter=param->queue;
1352 builditerators(task, j, parameter);
1357 void toiReset(struct tagobjectiterator * it) {
1360 } else if (it->numtags>0) {
1366 ObjectHashiterator(it->objectset, &it->it);
1373 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1376 /* Get object with tags */
1377 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1378 struct ___Object___ *tagptr=obj->___tags___;
1379 if (tagptr->type==TAGTYPE) {
1380 if ((it->tagobjindex==0)&& /* First object */
1381 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1386 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1387 int tagindex=it->tagobjindex;
1388 for(;tagindex<ao->___cachedCode___;tagindex++) {
1389 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1390 if (td->flag==it->tagid) {
1391 it->tagobjindex=tagindex; /* Found right type of tag */
1397 } else if (it->numtags>0) {
1398 /* Use tags to locate appropriate objects */
1399 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1400 struct ___Object___ *objptr=tag->flagptr;
1402 if (objptr->type!=OBJECTARRAYTYPE) {
1403 if (it->tagobjindex>0)
1405 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1407 for(i=1;i<it->numtags;i++) {
1408 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1409 if (!containstag(objptr,tag2))
1413 if (it->failedstate==1) {
1414 int UNUSED, UNUSED2;
1417 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1426 int UNUSED, UNUSED2;
1429 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1438 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1442 if (it->failedstate==1) {
1443 int UNUSED, UNUSED2;
1446 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1447 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1456 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1457 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1458 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1460 for(i=1;i<it->numtags;i++) {
1461 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1462 if (!containstag(objptr,tag2))
1467 int UNUSED, UNUSED2;
1468 int flags, isnonfailed;
1469 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1470 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1476 it->tagobjindex=tagindex;
1481 it->tagobjindex=tagindex;
1486 if (it->failedstate==1) {
1487 if (Objdata2(&it->it))
1494 if (ObjhasNext(&it->it)) {
1495 if (!Objdata4(&it->it)) {
1503 return ObjhasNext(&it->it);
1508 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1510 struct ___Object___ * objptr=tag->flagptr;
1511 if (objptr->type==OBJECTARRAYTYPE) {
1512 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1513 for(j=0;j<ao->___cachedCode___;j++) {
1514 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1522 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1523 /* hasNext has all of the intelligence */
1526 /* Get object with tags */
1527 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1528 struct ___Object___ *tagptr=obj->___tags___;
1529 if (tagptr->type==TAGTYPE) {
1531 objectarray[it->slot]=tagptr;
1533 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1534 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1536 } else if (it->numtags>0) {
1537 /* Use tags to locate appropriate objects */
1538 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1539 struct ___Object___ *objptr=tag->flagptr;
1540 if (objptr->type!=OBJECTARRAYTYPE) {
1542 failed[it->slot]=it->failedstate;
1543 objectarray[it->slot]=objptr;
1544 if (it->failedstate==0) {
1552 objectarray[it->slot]=objptr;
1555 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1557 failed[it->slot]=it->failedstate;
1558 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1559 if (it->failedstate==0) {
1566 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1570 /* Iterate object */
1571 objectarray[it->slot]=(void *)Objkey(&it->it);
1573 failed[it->slot]=it->failedstate;
1574 if (it->failedstate==0) {