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);
82 enqueueObject(startupobject);
85 int hashCodetpd(struct taskparamdescriptor *ftd) {
86 int hash=(int)ftd->task;
88 for(i=0;i<ftd->numParameters;i++){
89 hash^=(int)ftd->parameterArray[i];
94 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
96 if (ftd1->task!=ftd2->task)
98 for(i=0;i<ftd1->numParameters;i++)
99 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
102 for(i=0;i<ftd1->numParameters;i++) {
103 if(ftd1->failed[i]!=ftd2->failed[i])
110 /* This function sets a tag. */
112 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
114 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
116 struct ___Object___ * tagptr=obj->___tags___;
118 obj->___tags___=(struct ___Object___ *)tagd;
120 /* Have to check if it is already set */
121 if (tagptr->type==TAGTYPE) {
122 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
126 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
127 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
128 obj=(struct ___Object___ *)ptrarray[2];
129 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
130 td=(struct ___TagDescriptor___ *) obj->___tags___;
132 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
134 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
135 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
136 obj->___tags___=(struct ___Object___ *) ao;
137 ao->___cachedCode___=2;
141 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
142 for(i=0;i<ao->___cachedCode___;i++) {
143 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
147 if (ao->___cachedCode___<ao->___length___) {
148 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
149 ao->___cachedCode___++;
152 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
153 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
154 obj=(struct ___Object___ *)ptrarray[2];
155 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
156 ao=(struct ArrayObject *)obj->___tags___;
158 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
160 aonew->___cachedCode___=ao->___length___+1;
161 for(i=0;i<ao->___length___;i++) {
162 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
164 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
170 struct ___Object___ * tagset=tagd->flagptr;
173 } else if (tagset->type!=OBJECTARRAYTYPE) {
175 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
176 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
177 obj=(struct ___Object___ *)ptrarray[2];
178 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
180 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
182 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
183 ARRAYSET(ao, struct ___Object___ *, 1, obj);
184 ao->___cachedCode___=2;
185 tagd->flagptr=(struct ___Object___ *)ao;
187 struct ArrayObject *ao=(struct ArrayObject *) tagset;
188 if (ao->___cachedCode___<ao->___length___) {
189 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
193 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
194 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
195 obj=(struct ___Object___ *)ptrarray[2];
196 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
197 ao=(struct ArrayObject *)tagd->flagptr;
199 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
201 aonew->___cachedCode___=ao->___cachedCode___+1;
202 for(i=0;i<ao->___length___;i++) {
203 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
205 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
206 tagd->flagptr=(struct ___Object___ *) aonew;
212 /* This function clears a tag. */
214 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
216 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
218 /* We'll assume that tag is alway there.
219 Need to statically check for this of course. */
220 struct ___Object___ * tagptr=obj->___tags___;
222 if (tagptr->type==TAGTYPE) {
223 if ((struct ___TagDescriptor___ *)tagptr==tagd)
224 obj->___tags___=NULL;
226 printf("ERROR 1 in tagclear\n");
228 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
230 for(i=0;i<ao->___cachedCode___;i++) {
231 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
233 ao->___cachedCode___--;
234 if (i<ao->___cachedCode___)
235 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
236 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
237 if (ao->___cachedCode___==0)
238 obj->___tags___=NULL;
242 printf("ERROR 2 in tagclear\n");
246 struct ___Object___ *tagset=tagd->flagptr;
247 if (tagset->type!=OBJECTARRAYTYPE) {
251 printf("ERROR 3 in tagclear\n");
253 struct ArrayObject *ao=(struct ArrayObject *) tagset;
255 for(i=0;i<ao->___cachedCode___;i++) {
256 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
258 ao->___cachedCode___--;
259 if (i<ao->___cachedCode___)
260 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
261 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
262 if (ao->___cachedCode___==0)
267 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 bool 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 */
360 void flagorandinit(void * ptr, int ormask, int andmask) {
361 int oldflag=((int *)ptr)[1];
362 int flag=ormask|oldflag;
367 void flagbody(struct ___Object___ *ptr, int flag) {
368 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
371 /*Remove object from all queues */
372 while(flagptr!=NULL) {
373 struct parameterwrapper *next;
376 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
377 ObjectHashremove(flagptr->objectset, (int)ptr);
378 if (enterflags!=NULL)
384 void enqueueObject(void *vptr) {
385 struct ___Object___ *ptr = (struct ___Object___ *)vptr;
388 struct QueueItem *tmpptr;
389 struct parameterwrapper * parameter=objectqueues[ptr->type];
391 struct parameterwrapper * prevptr=NULL;
392 struct ___Object___ *tagptr=ptr->___tags___;
394 /* Outer loop iterates through all parameter queues an object of
395 this type could be in. */
397 while(parameter!=NULL) {
399 if (parameter->numbertags>0) {
401 goto nextloop;//that means the object has no tag but that param needs tag
402 else if(tagptr->type==TAGTYPE) {//one tag
403 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
404 for(i=0;i<parameter->numbertags;i++) {
405 //slotid is parameter->tagarray[2*i];
406 int tagid=parameter->tagarray[2*i+1];
407 if (tagid!=tagptr->flag)
408 goto nextloop; /*We don't have this tag */
410 } else {//multiple tags
411 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
412 for(i=0;i<parameter->numbertags;i++) {
413 //slotid is parameter->tagarray[2*i];
414 int tagid=parameter->tagarray[2*i+1];
416 for(j=0;j<ao->___cachedCode___;j++) {
417 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
428 for(i=0;i<parameter->numberofterms;i++) {
429 int andmask=parameter->intarray[i*2];
430 int checkmask=parameter->intarray[i*2+1];
431 if ((ptr->flag&andmask)==checkmask) {
432 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
438 parameter=parameter->next;
440 ptr->flagptr=prevptr;
446 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
448 struct ___Object___ * tagptr = currobj->___tags___;
449 if(fswrapper->numtags>0){
451 return 0; //that means the object has no tag but that param
453 else if(tagptr->type==TAGTYPE) {//one tag
454 if(fswrapper->numtags!=1)
455 return 0; //we don't have the right number of tags
456 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
457 if (fswrapper->tags[0]!=tagptr->flag)
459 } else { //multiple tags
460 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
464 if(ao->___length___!=fswrapper->numtags)
465 return 0;//we don't have the right number of tags
466 for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
467 int tagid=fswrapper->tags[tag_counter];
469 for(j=0;j<ao->___cachedCode___;j++) {
470 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
480 int getlength(int *flist, int len) {
484 int size=flist[count];
490 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
491 int size1=getlength(flist1, len1);
492 int size2=getlength(flist2, len2);
493 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
494 memcpy(merge, flist1, size1*sizeof(int));
495 memcpy(&merge[size1], flist2, size2*sizeof(int));
499 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
503 while(i<len1||j<len2) {
504 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
506 merge[count]=flist1[i];
510 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
512 merge[count]=flist2[j];
516 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
518 merge[count]=flist1[i];
528 /* Merge flags from ftlmerge into ftl. */
529 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
534 for(i=0;i<ftl->numflags;i++) {
535 int len=ftl->flags[offset++];
537 for(j=0;j<ftlmerge->numflags;j++) {
538 int lenmerge=ftlmerge->flags[offsetmerge++];
539 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
540 offsetmerge+=lenmerge;
544 mergedlist=RUNMALLOC(sizeof(int)*length);
548 for(i=0;i<ftl->numflags;i++) {
549 int len=ftl->flags[offset++];
551 for(j=0;j<ftlmerge->numflags;j++) {
552 int lenmerge=ftlmerge->flags[offsetmerge++];
553 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
554 mergedlist[length]=size;
559 ftl->flags=mergedlist;
560 ftl->numflags*=ftlmerge->numflags;
563 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
564 struct failedtasklist *tmpptr;
565 while((*andlist)!=NULL) {
566 struct failedtasklist *searchftl=list;
567 while(searchftl!=NULL) {
568 if ((*andlist)->task==searchftl->task&&
569 (*andlist)->index==searchftl->index) {
570 mergeitems(*andlist, searchftl);
573 searchftl=searchftl->next;
575 if (searchftl==NULL) {
576 //didn't find andlist
578 *andlist=(*andlist)->next;//splice item out of list
579 RUNFREE(tmpptr->flags); //free the item
582 andlist=&((*andlist)->next); //iterate to next item
585 //free the list we're searching
588 RUNFREE(list->flags);
594 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
595 struct failedtasklist *list=NULL;
597 struct fsanalysiswrapper *fswrapper=NULL;
598 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
599 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
600 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
601 //we only match exactly here
608 for(i=0;i<fswrapper->numtaskfailures;i++) {
610 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
611 if (taskfail->task==task&&taskfail->index==index) {
613 while(start<taskfail->numoptionaltaskdescriptors) {
614 struct taskdescriptor *currtask=NULL;
615 struct failedtasklist *tmpftl;
619 int numenterflags, offset;
620 struct parameterwrapper *pw;
621 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
622 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
625 currindex=otd->index;
626 } else if (currtask!=otd->task||currindex!=otd->index)
628 totallength+=otd->numenterflags;
630 pw=currtask->descriptorarray[currindex]->queue;
631 enterflags=RUNMALLOC(totallength*sizeof(int));
632 numenterflags=j-start;
634 for(start;start<j;start++) {
635 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
636 enterflags[offset++]=otd->numenterflags;
637 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
638 offset+=otd->numenterflags;
640 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
642 tmpftl->task=currtask;
643 tmpftl->numflags=numenterflags;
644 tmpftl->flags=enterflags;
652 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
653 struct failedtasklist *list=NULL;
656 struct fsanalysiswrapper *fswrapper=NULL;
657 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
658 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
659 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
660 //we only match exactly here
668 while(start<fswrapper->numoptionaltaskdescriptors) {
669 struct taskdescriptor *currtask=NULL;
670 struct failedtasklist *tmpftl;
675 int numenterflags, offset;
676 struct parameterwrapper *pw;
677 for(j=start;j<fswrapper->numoptionaltaskdescriptors;j++) {
678 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
681 currindex=otd->index;
682 } else if (currtask!=otd->task||currindex!=otd->index)
684 totallength+=otd->numenterflags;
686 pw=currtask->descriptorarray[currindex]->queue;
687 enterflags=RUNMALLOC(totallength*sizeof(int));
688 numenterflags=j-start;
690 for(start;start<j;start++) {
691 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
692 enterflags[offset++]=otd->numenterflags;
693 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
694 offset+=otd->numenterflags;
696 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
698 tmpftl->task=currtask;
699 tmpftl->numflags=numenterflags;
700 tmpftl->flags=enterflags;
708 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
709 while(andlist!=NULL) {
710 struct failedtasklist *tmp=andlist;
711 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
712 struct parmaeterwrapper *next;
717 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
720 andlist=andlist->next;
725 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
726 struct classanalysiswrapper * classwrapper=NULL;
728 /*test what optionaltaskdescriptors are available, find the class
730 if (classanalysiswrapperarray[currobj->type]!=NULL) {
731 classwrapper = classanalysiswrapperarray[currobj->type];
736 /* We have a failure */
737 if (failedfses==NULL) {
738 /* Failed in normal state */
739 /*first time the method is invoked*/
741 struct fsanalysiswrapper *fswrapper=NULL;
743 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
744 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
745 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
746 //we only match exactly here
751 if(fswrapper==NULL) //nothing to do in this state
753 for(i=0;i<fswrapper->numtaskfailures;i++) {
755 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
756 if (taskfail->task==task&&taskfail->index==index) {
758 while(start<taskfail->numoptionaltaskdescriptors) {
759 struct taskdescriptor *currtask=NULL;
763 int numenterflags, offset;
764 struct parameterwrapper *pw;
765 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
766 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
769 currindex=otd->index;
770 } else if (currtask!=otd->task||currindex!=otd->index)
772 totallength+=otd->numenterflags;//1 is to store the lengths
774 pw=currtask->descriptorarray[currindex]->queue;
775 enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
776 numenterflags=j-start;
779 for(start;start<j;start++) {
780 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
781 enterflags[offset++]=otd->numenterflags;
782 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
783 offset+=otd->numenterflags;
786 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
792 /* Failed in failed state */
795 for(i=0;i<numfailedfses;i++) {
796 int numfses=failedfses[offset++];
798 struct failedtasklist *andlist=NULL;
799 for(j=0;j<numfses;j++) {
800 int flagstate=failedfses[offset++];
801 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
805 mergefailedlists(&andlist, currlist);
807 enqueuelist(currobj, andlist);
811 /* No failure, but we are in a failed state */
812 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
814 /*Remove object from all queues */
815 while(flagptr!=NULL) {
816 struct parameterwrapper *next;
819 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
820 ObjectHashremove(flagptr->objectset, (int)currobj);
821 if (enterflags!=NULL)
826 /* Failed in failed state */
829 for(i=0;i<currobj->numfses;i++) {
830 int numfses=currobj->fses[offset++];
832 struct failedtasklist *andlist=NULL;
833 for(j=0;j<numfses;j++) {
834 int flagstate=currobj->fses[offset++];
835 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
839 mergefailedlists(&andlist, currlist);
841 enqueuelist(currobj, andlist);
849 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
850 void * taskpointerarray[MAXTASKPARAMS];
852 int failed[MAXTASKPARAMS];
855 int numparams=parameter->task->numParameters;
856 int numiterators=parameter->task->numTotal-1;
861 struct taskdescriptor * task=parameter->task;
864 if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
865 /* The object is already here...or it with the existing item */
871 ObjectHashget(parameter->objectset, (int) ptr, & oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
872 mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
873 ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
878 //only add if truly needed
887 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
892 /* Add enqueued object to parameter vector */
893 taskpointerarray[parameter->slot]=ptr;
895 failed[parameter->slot]=(enterflags!=NULL);
898 /* Reset iterators */
899 for(j=0;j<numiterators;j++) {
900 toiReset(¶meter->iterators[j]);
903 /* Find initial state */
904 for(j=0;j<numiterators;j++) {
906 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
907 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
909 /* Need to backtrack */
910 toiReset(¶meter->iterators[j]);
914 /* Nothing to enqueue */
921 /* Enqueue current state */
923 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
925 tpd->numParameters=numiterators+1;
926 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
928 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
930 for(j=0;j<=numiterators;j++){
931 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
933 tpd->failed[j]=failed[j];
934 if (failed[j]!=0&&failed[j]!=1) {
940 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
941 genputtable(activetasks, tpd, tpd);
943 RUNFREE(tpd->parameterArray);
945 RUNFREE(tpd->failed);
950 /* This loop iterates to the next parameter combination */
954 for(j=numiterators-1; j<numiterators;j++) {
956 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
957 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
959 /* Need to backtrack */
960 toiReset(¶meter->iterators[j]);
964 /* Nothing more to enqueue */
972 /* Handler for signals. The signals catch null pointer errors and
973 arithmatic errors. */
975 void myhandler(int sig, siginfo_t *info, void *uap) {
978 printf("sig=%d\n",sig);
981 sigemptyset(&toclear);
982 sigaddset(&toclear, sig);
983 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
984 longjmp(error_handler,1);
989 struct RuntimeHash *fdtoobject;
991 void addreadfd(int fd) {
994 FD_SET(fd, &readfds);
997 void removereadfd(int fd) {
998 FD_CLR(fd, &readfds);
999 if (maxreadfd==(fd+1)) {
1001 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1013 int * fsescopy(int *src, int len) {
1017 dst=RUNMALLOC(len*sizeof(int));
1018 memcpy(dst, src, len*sizeof(int));
1023 void executetasks() {
1024 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1026 int * fsesarray[MAXTASKPARAMS];
1027 int * oldfsesarray[MAXTASKPARAMS];
1028 int numfsesarray[MAXTASKPARAMS];
1031 /* Set up signal handlers */
1032 struct sigaction sig;
1033 sig.sa_sigaction=&myhandler;
1034 sig.sa_flags=SA_SIGINFO;
1035 sigemptyset(&sig.sa_mask);
1037 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1038 sigaction(SIGBUS,&sig,0);
1039 sigaction(SIGSEGV,&sig,0);
1040 sigaction(SIGFPE,&sig,0);
1041 sigaction(SIGPIPE,&sig,0);
1046 fdtoobject=allocateRuntimeHash(100);
1048 /* Map first block of memory to protected, anonymous page */
1049 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1052 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1054 /* Check if any filedescriptors have IO pending */
1057 struct timeval timeout={0,0};
1061 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1063 /* Process ready fd's */
1065 for(fd=0;fd<maxreadfd;fd++) {
1066 if (FD_ISSET(fd, &tmpreadfds)) {
1067 /* Set ready flag on object */
1069 // printf("Setting fd %d\n",fd);
1070 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1071 if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1072 enqueueObject(objptr);
1080 /* See if there are any active tasks */
1081 if (hashsize(activetasks)>0) {
1083 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1084 genfreekey(activetasks, currtpd);
1086 /* Check if this task has failed, allow a task that contains optional objects to fire */
1087 if (gencontains(failedtasks, currtpd)) {
1088 // Free up task parameter descriptor
1089 RUNFREE(currtpd->parameterArray);
1091 RUNFREE(currtpd->failed);
1096 int numparams=currtpd->task->numParameters;
1097 int numtotal=currtpd->task->numTotal;
1099 /* Make sure that the parameters are still in the queues */
1100 for(i=0;i<numparams;i++) {
1101 void * parameter=currtpd->parameterArray[i];
1102 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1103 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1105 /* Check that object is still in queue */
1108 int UNUSED, UNUSED2;
1110 int numflags, isnonfailed;
1111 int failed=currtpd->failed[i];
1112 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1113 RUNFREE(currtpd->parameterArray);
1114 RUNFREE(currtpd->failed);
1118 if (failed&&(flags!=NULL)) {
1121 numfsesarray[i]=numflags;
1122 } else if (!failed && isnonfailed) {
1123 //Non-failed parameter
1127 RUNFREE(currtpd->parameterArray);
1128 RUNFREE(currtpd->failed);
1136 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1137 RUNFREE(currtpd->parameterArray);
1145 /* Check that object still has necessary tags */
1146 for(j=0;j<pd->numbertags;j++) {
1147 int slotid=pd->tagarray[2*j]+numparams;
1148 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1149 if (!containstag(parameter, tagd)) {
1150 RUNFREE(currtpd->parameterArray);
1152 RUNFREE(currtpd->failed);
1159 taskpointerarray[i+OFFSET]=parameter;
1162 for(;i<numtotal;i++) {
1163 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1167 /* Checkpoint the state */
1168 forward=allocateRuntimeHash(100);
1169 reverse=allocateRuntimeHash(100);
1170 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1172 if (x=setjmp(error_handler)) {
1176 printf("Fatal Error=%d, Recovering!\n",x);
1178 genputtable(failedtasks,currtpd,currtpd);
1179 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1182 for(counter=0; counter<currtpd->task->numParameters; counter++){
1184 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1187 if (fsesarray[counter]!=NULL)
1188 RUNFREE(fsesarray[counter]);
1191 freeRuntimeHash(forward);
1192 freeRuntimeHash(reverse);
1197 if (injectfailures) {
1198 if ((((double)random())/RAND_MAX)<failurechance) {
1199 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1200 longjmp(error_handler,10);
1203 /* Actually call task */
1205 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1206 taskpointerarray[1]=NULL;
1209 //get the task flags set
1210 for(i=0;i<numparams;i++) {
1211 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1212 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1213 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1217 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1218 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1219 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1221 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1224 for(i=0;i<numparams;i++) {
1226 if(oldfsesarray[i]!=NULL)
1227 RUNFREE(oldfsesarray[i]);
1231 freeRuntimeHash(forward);
1232 freeRuntimeHash(reverse);
1234 // Free up task parameter descriptor
1235 RUNFREE(currtpd->parameterArray);
1237 RUNFREE(currtpd->failed);
1248 /* This function processes an objects tags */
1249 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1252 for(i=0;i<pd->numbertags;i++) {
1253 int slotid=pd->tagarray[2*i];
1254 int tagid=pd->tagarray[2*i+1];
1256 if (statusarray[slotid+numparams]==0) {
1257 parameter->iterators[*iteratorcount].istag=1;
1258 parameter->iterators[*iteratorcount].tagid=tagid;
1259 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1260 parameter->iterators[*iteratorcount].tagobjectslot=index;
1261 statusarray[slotid+numparams]=1;
1268 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1271 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1273 parameter->iterators[*iteratorcount].istag=0;
1274 parameter->iterators[*iteratorcount].slot=index;
1275 parameter->iterators[*iteratorcount].objectset=objectset;
1276 statusarray[index]=1;
1278 for(i=0;i<pd->numbertags;i++) {
1279 int slotid=pd->tagarray[2*i];
1280 int tagid=pd->tagarray[2*i+1];
1281 if (statusarray[slotid+numparams]!=0) {
1282 /* This tag has already been enqueued, use it to narrow search */
1283 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1287 parameter->iterators[*iteratorcount].numtags=tagcount;
1292 /* This function builds the iterators for a task & parameter */
1294 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1295 int statusarray[MAXTASKPARAMS];
1297 int numparams=task->numParameters;
1298 int iteratorcount=0;
1299 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1301 statusarray[index]=1; /* Initial parameter */
1302 /* Process tags for initial iterator */
1304 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1308 /* Check for objects with existing tags */
1309 for(i=0;i<numparams;i++) {
1310 if (statusarray[i]==0) {
1311 struct parameterdescriptor *pd=task->descriptorarray[i];
1313 for(j=0;j<pd->numbertags;j++) {
1314 int slotid=pd->tagarray[2*j];
1315 if(statusarray[slotid+numparams]!=0) {
1316 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1317 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1324 /* Next do objects w/ unbound tags*/
1326 for(i=0;i<numparams;i++) {
1327 if (statusarray[i]==0) {
1328 struct parameterdescriptor *pd=task->descriptorarray[i];
1329 if (pd->numbertags>0) {
1330 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1331 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1337 /* Nothing with a tag enqueued */
1339 for(i=0;i<numparams;i++) {
1340 if (statusarray[i]==0) {
1341 struct parameterdescriptor *pd=task->descriptorarray[i];
1342 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1343 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1356 for(i=0;i<numtasks;i++) {
1357 struct taskdescriptor * task=taskarray[i];
1358 printf("%s\n", task->name);
1359 for(j=0;j<task->numParameters;j++) {
1360 struct parameterdescriptor *param=task->descriptorarray[j];
1361 struct parameterwrapper *parameter=param->queue;
1362 struct ObjectHash * set=parameter->objectset;
1363 struct ObjectIterator objit;
1364 printf(" Parameter %d\n", j);
1365 ObjectHashiterator(set, &objit);
1366 while(ObjhasNext(&objit)) {
1367 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1368 struct ___Object___ * tagptr=obj->___tags___;
1369 int nonfailed=Objdata4(&objit);
1370 int numflags=Objdata3(&objit);
1371 int flags=Objdata2(&objit);
1373 printf(" Contains %lx\n", obj);
1374 printf(" flag=%d\n", obj->flag);
1376 printf(" flagsstored=%x\n",flags);
1377 printf(" numflags=%d\n", numflags);
1378 printf(" nonfailed=%d\n",nonfailed);
1381 } else if (tagptr->type==TAGTYPE) {
1382 printf(" tag=%lx\n",tagptr);
1385 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1386 for(;tagindex<ao->___cachedCode___;tagindex++) {
1387 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1396 /* This function processes the task information to create queues for
1397 each parameter type. */
1399 void processtasks() {
1401 for(i=0;i<numtasks;i++) {
1402 struct taskdescriptor * task=taskarray[i];
1405 for(j=0;j<task->numParameters;j++) {
1406 struct parameterdescriptor *param=task->descriptorarray[j];
1407 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1408 struct parameterwrapper ** ptr=&objectqueues[param->type];
1410 param->queue=parameter;
1411 parameter->objectset=allocateObjectHash(10);
1412 parameter->numberofterms=param->numberterms;
1413 parameter->intarray=param->intarray;
1414 parameter->numbertags=param->numbertags;
1415 parameter->tagarray=param->tagarray;
1416 parameter->task=task;
1418 /* Link new queue in */
1420 ptr=&((*ptr)->next);
1424 /* Build iterators for parameters */
1425 for(j=0;j<task->numParameters;j++) {
1426 struct parameterdescriptor *param=task->descriptorarray[j];
1427 struct parameterwrapper *parameter=param->queue;
1428 builditerators(task, j, parameter);
1433 void toiReset(struct tagobjectiterator * it) {
1436 } else if (it->numtags>0) {
1442 ObjectHashiterator(it->objectset, &it->it);
1449 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1452 /* Get object with tags */
1453 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1454 struct ___Object___ *tagptr=obj->___tags___;
1455 if (tagptr->type==TAGTYPE) {
1456 if ((it->tagobjindex==0)&& /* First object */
1457 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1462 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1463 int tagindex=it->tagobjindex;
1464 for(;tagindex<ao->___cachedCode___;tagindex++) {
1465 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1466 if (td->flag==it->tagid) {
1467 it->tagobjindex=tagindex; /* Found right type of tag */
1473 } else if (it->numtags>0) {
1474 /* Use tags to locate appropriate objects */
1475 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1476 struct ___Object___ *objptr=tag->flagptr;
1478 if (objptr->type!=OBJECTARRAYTYPE) {
1479 if (it->tagobjindex>0)
1481 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1483 for(i=1;i<it->numtags;i++) {
1484 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1485 if (!containstag(objptr,tag2))
1489 if (it->failedstate==1) {
1490 int UNUSED, UNUSED2;
1493 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1502 int UNUSED, UNUSED2;
1505 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1514 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1518 if (it->failedstate==1) {
1519 int UNUSED, UNUSED2;
1522 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1523 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1532 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1533 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1534 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1536 for(i=1;i<it->numtags;i++) {
1537 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1538 if (!containstag(objptr,tag2))
1543 int UNUSED, UNUSED2;
1544 int flags, isnonfailed;
1545 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1546 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1552 it->tagobjindex=tagindex;
1557 it->tagobjindex=tagindex;
1562 if (it->failedstate==1) {
1563 if (Objdata2(&it->it))
1570 if (ObjhasNext(&it->it)) {
1571 if (!Objdata4(&it->it)) {
1579 return ObjhasNext(&it->it);
1584 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1586 struct ___Object___ * objptr=tag->flagptr;
1587 if (objptr->type==OBJECTARRAYTYPE) {
1588 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1589 for(j=0;j<ao->___cachedCode___;j++) {
1590 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1598 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1599 /* hasNext has all of the intelligence */
1602 /* Get object with tags */
1603 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1604 struct ___Object___ *tagptr=obj->___tags___;
1606 failed[it->slot]=0; //have to set it to something
1608 if (tagptr->type==TAGTYPE) {
1610 objectarray[it->slot]=tagptr;
1612 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1613 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1615 } else if (it->numtags>0) {
1616 /* Use tags to locate appropriate objects */
1617 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1618 struct ___Object___ *objptr=tag->flagptr;
1619 if (objptr->type!=OBJECTARRAYTYPE) {
1621 failed[it->slot]=it->failedstate;
1622 objectarray[it->slot]=objptr;
1623 if (it->failedstate==0) {
1631 objectarray[it->slot]=objptr;
1634 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1636 failed[it->slot]=it->failedstate;
1637 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1638 if (it->failedstate==0) {
1645 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1649 /* Iterate object */
1650 objectarray[it->slot]=(void *)Objkey(&it->it);
1652 failed[it->slot]=it->failedstate;
1653 if (it->failedstate==0) {