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;
22 #include "instrument.h"
26 struct ___Object___ * ___fcrevert___;
27 #include "localobjects.h"
30 struct genhashtable * activetasks;
31 struct parameterwrapper * objectqueues[NUMCLASSES];
32 struct genhashtable * failedtasks;
33 struct taskparamdescriptor * currtpd;
34 struct ctable * forward;
35 struct ctable * reverse;
37 int main(int argc, char **argv) {
39 GC_init(); // Initialize the garbage collector
45 initializeexithandler();
46 /* Create table for failed tasks */
47 failedtasks=genallocatehashtable((unsigned int(*) (void *)) &hashCodetpd,
48 (int(*) (void *,void *)) &comparetpd);
49 /* Create queue of active tasks */
50 activetasks=genallocatehashtable((unsigned int(*) (void *)) &hashCodetpd,
51 (int(*) (void *,void *)) &comparetpd);
53 /* Process task information */
56 /* Create startup object */
57 createstartupobject(argc, argv);
59 /* Start executing the tasks */
63 void createstartupobject(int argc, char ** argv) {
66 /* Allocate startup object */
68 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
69 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
71 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
72 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
74 /* Build array of strings */
75 startupobject->___parameters___=stringarray;
76 for(i=1; i<argc; i++) {
77 int length=strlen(argv[i]);
79 struct ___String___ *newstring=NewString(NULL, argv[i],length);
81 struct ___String___ *newstring=NewString(argv[i],length);
83 ((void **)(((char *)&stringarray->___length___)+sizeof(int)))[i-1]=newstring;
86 /* Set initialized flag for startup object */
87 flagorand(startupobject,1,0xFFFFFFFF);
88 enqueueObject(startupobject);
91 int hashCodetpd(struct taskparamdescriptor *ftd) {
92 int hash=(int)ftd->task;
94 for(i=0; i<ftd->numParameters; i++) {
95 hash^=(int)ftd->parameterArray[i];
100 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
102 if (ftd1->task!=ftd2->task)
104 for(i=0; i<ftd1->numParameters; i++)
105 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
108 for(i=0; i<ftd1->numParameters; i++) {
109 if(ftd1->failed[i]!=ftd2->failed[i])
116 /* This function sets a tag. */
118 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
120 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
122 struct ___Object___ * tagptr=obj->___tags___;
124 obj->___tags___=(struct ___Object___ *)tagd;
126 /* Have to check if it is already set */
127 if (tagptr->type==TAGTYPE) {
128 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
132 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
133 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
134 obj=(struct ___Object___ *)ptrarray[2];
135 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
136 td=(struct ___TagDescriptor___ *) obj->___tags___;
138 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
140 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
141 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
142 obj->___tags___=(struct ___Object___ *) ao;
143 ao->___cachedCode___=2;
147 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
148 for(i=0; i<ao->___cachedCode___; i++) {
149 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
153 if (ao->___cachedCode___<ao->___length___) {
154 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
155 ao->___cachedCode___++;
158 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
159 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
160 obj=(struct ___Object___ *)ptrarray[2];
161 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
162 ao=(struct ArrayObject *)obj->___tags___;
164 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
166 aonew->___cachedCode___=ao->___length___+1;
167 for(i=0; i<ao->___length___; i++) {
168 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
170 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
176 struct ___Object___ * tagset=tagd->flagptr;
179 } else if (tagset->type!=OBJECTARRAYTYPE) {
181 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
182 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
183 obj=(struct ___Object___ *)ptrarray[2];
184 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
186 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
188 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
189 ARRAYSET(ao, struct ___Object___ *, 1, obj);
190 ao->___cachedCode___=2;
191 tagd->flagptr=(struct ___Object___ *)ao;
193 struct ArrayObject *ao=(struct ArrayObject *) tagset;
194 if (ao->___cachedCode___<ao->___length___) {
195 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
199 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
200 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
201 obj=(struct ___Object___ *)ptrarray[2];
202 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
203 ao=(struct ArrayObject *)tagd->flagptr;
205 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
207 aonew->___cachedCode___=ao->___cachedCode___+1;
208 for(i=0; i<ao->___length___; i++) {
209 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
211 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
212 tagd->flagptr=(struct ___Object___ *) aonew;
218 /* This function clears a tag. */
220 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
222 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
224 /* We'll assume that tag is alway there.
225 Need to statically check for this of course. */
226 struct ___Object___ * tagptr=obj->___tags___;
228 if (tagptr->type==TAGTYPE) {
229 if ((struct ___TagDescriptor___ *)tagptr==tagd)
230 obj->___tags___=NULL;
232 printf("ERROR 1 in tagclear\n");
234 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
236 for(i=0; i<ao->___cachedCode___; i++) {
237 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
239 ao->___cachedCode___--;
240 if (i<ao->___cachedCode___)
241 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
242 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
243 if (ao->___cachedCode___==0)
244 obj->___tags___=NULL;
248 printf("ERROR 2 in tagclear\n");
252 struct ___Object___ *tagset=tagd->flagptr;
253 if (tagset->type!=OBJECTARRAYTYPE) {
257 printf("ERROR 3 in tagclear\n");
259 struct ArrayObject *ao=(struct ArrayObject *) tagset;
261 for(i=0; i<ao->___cachedCode___; i++) {
262 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
264 ao->___cachedCode___--;
265 if (i<ao->___cachedCode___)
266 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
267 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
268 if (ao->___cachedCode___==0)
273 printf("ERROR 4 in tagclear\n");
280 /* This function allocates a new tag. */
282 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
283 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
285 struct ___TagDescriptor___ * allocate_tag(int index) {
286 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
295 /* This function updates the flag for object ptr. It or's the flag
296 with the or mask and and's it with the andmask. */
298 void flagbody(struct ___Object___ *ptr, int flag);
300 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
303 int flagcomp(const int *val1, const int *val2) {
304 return (*val1)-(*val2);
307 void flagorand(void * ptr, int ormask, int andmask) {
309 struct ___Object___ * obj = (struct ___Object___ *)ptr;
310 if(obj->numfses) { /*store the information about fses*/
311 int flag, i, j,counter, offset=0;
312 for(i=0; i<obj->numfses; i++) {
314 counter=obj->fses[offset++];
316 for(j=0; j<counter; j++) {
317 flag=obj->fses[offset];
318 obj->fses[offset++]=(flag|ormask)&andmask;
320 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int(*) (const void *, const void *)) &flagcomp);
322 enqueueoptional(obj, 0, NULL, NULL, 0);
326 int oldflag=((int *)ptr)[1];
327 int flag=ormask|oldflag;
333 bool intflagorand(void * ptr, int ormask, int andmask) {
335 struct ___Object___ * obj = (struct ___Object___ *)ptr;
336 if(obj->numfses) { /*store the information about fses*/
337 int flag, i, j,counter, offset=0;
338 for(i=0; i<obj->numfses; i++) {
340 counter=obj->fses[offset++];
342 for(j=0; j<counter; j++) {
343 flag=obj->fses[offset];
344 obj->fses[offset++]=(flag|ormask)&andmask;
346 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int(*) (const void *, const void *)) &flagcomp);
348 enqueueoptional(obj, 0, NULL, NULL, 0);
352 int oldflag=((int *)ptr)[1];
353 int flag=ormask|oldflag;
355 if (flag==oldflag) /* Don't do anything */
364 void flagorandinit(void * ptr, int ormask, int andmask) {
365 int oldflag=((int *)ptr)[1];
366 int flag=ormask|oldflag;
371 void flagbody(struct ___Object___ *ptr, int flag) {
372 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
375 /*Remove object from all queues */
376 while(flagptr!=NULL) {
377 struct parameterwrapper *next;
380 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
381 ObjectHashremove(flagptr->objectset, (int)ptr);
382 if (enterflags!=NULL)
388 void enqueueObject(void *vptr) {
389 struct ___Object___ *ptr = (struct ___Object___ *)vptr;
392 struct QueueItem *tmpptr;
393 struct parameterwrapper * parameter=objectqueues[ptr->type];
395 struct parameterwrapper * prevptr=NULL;
396 struct ___Object___ *tagptr=ptr->___tags___;
398 /* Outer loop iterates through all parameter queues an object of
399 this type could be in. */
401 while(parameter!=NULL) {
403 if (parameter->numbertags>0) {
405 goto nextloop; //that means the object has no tag but that param needs tag
406 else if(tagptr->type==TAGTYPE) { //one tag
407 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
408 for(i=0; i<parameter->numbertags; i++) {
409 //slotid is parameter->tagarray[2*i];
410 int tagid=parameter->tagarray[2*i+1];
411 if (tagid!=tagptr->flag)
412 goto nextloop; /*We don't have this tag */
414 } else { //multiple tags
415 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
416 for(i=0; i<parameter->numbertags; i++) {
417 //slotid is parameter->tagarray[2*i];
418 int tagid=parameter->tagarray[2*i+1];
420 for(j=0; j<ao->___cachedCode___; j++) {
421 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
432 for(i=0; i<parameter->numberofterms; i++) {
433 int andmask=parameter->intarray[i*2];
434 int checkmask=parameter->intarray[i*2+1];
435 if ((ptr->flag&andmask)==checkmask) {
436 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
442 parameter=parameter->next;
444 ptr->flagptr=prevptr;
450 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
452 struct ___Object___ * tagptr = currobj->___tags___;
453 if(fswrapper->numtags>0) {
455 return 0; //that means the object has no tag but that param
457 else if(tagptr->type==TAGTYPE) { //one tag
458 if(fswrapper->numtags!=1)
459 return 0; //we don't have the right number of tags
460 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
461 if (fswrapper->tags[0]!=tagptr->flag)
463 } else { //multiple tags
464 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
468 if(ao->___length___!=fswrapper->numtags)
469 return 0; //we don't have the right number of tags
470 for(tag_counter=0; tag_counter<fswrapper->numtags; tag_counter++) {
471 int tagid=fswrapper->tags[tag_counter];
473 for(j=0; j<ao->___cachedCode___; j++) {
474 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
484 int getlength(int *flist, int len) {
487 for(i=0; i<len; i++) {
488 int size=flist[count];
494 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
495 int size1=getlength(flist1, len1);
496 int size2=getlength(flist2, len2);
497 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
498 memcpy(merge, flist1, size1*sizeof(int));
499 memcpy(&merge[size1], flist2, size2*sizeof(int));
503 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
507 while(i<len1||j<len2) {
508 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
510 merge[count]=flist1[i];
514 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
516 merge[count]=flist2[j];
520 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
522 merge[count]=flist1[i];
532 /* Merge flags from ftlmerge into ftl. */
533 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
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 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
544 offsetmerge+=lenmerge;
548 mergedlist=RUNMALLOC(sizeof(int)*length);
552 for(i=0; i<ftl->numflags; i++) {
553 int len=ftl->flags[offset++];
555 for(j=0; j<ftlmerge->numflags; j++) {
556 int lenmerge=ftlmerge->flags[offsetmerge++];
557 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
558 mergedlist[length]=size;
563 ftl->flags=mergedlist;
564 ftl->numflags*=ftlmerge->numflags;
567 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
568 struct failedtasklist *tmpptr;
569 while((*andlist)!=NULL) {
570 struct failedtasklist *searchftl=list;
571 while(searchftl!=NULL) {
572 if ((*andlist)->task==searchftl->task&&
573 (*andlist)->index==searchftl->index) {
574 mergeitems(*andlist, searchftl);
577 searchftl=searchftl->next;
579 if (searchftl==NULL) {
580 //didn't find andlist
582 *andlist=(*andlist)->next; //splice item out of list
583 RUNFREE(tmpptr->flags); //free the item
586 andlist=&((*andlist)->next); //iterate to next item
589 //free the list we're searching
592 RUNFREE(list->flags);
598 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
599 struct failedtasklist *list=NULL;
601 struct fsanalysiswrapper *fswrapper=NULL;
602 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
603 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
604 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
605 //we only match exactly here
612 for(i=0; i<fswrapper->numtaskfailures; i++) {
614 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
615 if (taskfail->task==task&&taskfail->index==index) {
617 while(start<taskfail->numoptionaltaskdescriptors) {
618 struct taskdescriptor *currtask=NULL;
619 struct failedtasklist *tmpftl;
623 int numenterflags, offset;
624 struct parameterwrapper *pw;
625 for(j=start; j<taskfail->numoptionaltaskdescriptors; j++) {
626 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
629 currindex=otd->index;
630 } else if (currtask!=otd->task||currindex!=otd->index)
632 totallength+=otd->numenterflags;
634 pw=currtask->descriptorarray[currindex]->queue;
635 enterflags=RUNMALLOC(totallength*sizeof(int));
636 numenterflags=j-start;
638 for(start; start<j; start++) {
639 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
640 enterflags[offset++]=otd->numenterflags;
641 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
642 offset+=otd->numenterflags;
644 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
646 tmpftl->task=currtask;
647 tmpftl->numflags=numenterflags;
648 tmpftl->flags=enterflags;
656 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
657 struct failedtasklist *list=NULL;
660 struct fsanalysiswrapper *fswrapper=NULL;
661 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
662 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
663 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
664 //we only match exactly here
672 while(start<fswrapper->numoptionaltaskdescriptors) {
673 struct taskdescriptor *currtask=NULL;
674 struct failedtasklist *tmpftl;
679 int numenterflags, offset;
680 struct parameterwrapper *pw;
681 for(j=start; j<fswrapper->numoptionaltaskdescriptors; j++) {
682 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
685 currindex=otd->index;
686 } else if (currtask!=otd->task||currindex!=otd->index)
688 totallength+=otd->numenterflags;
690 pw=currtask->descriptorarray[currindex]->queue;
691 enterflags=RUNMALLOC(totallength*sizeof(int));
692 numenterflags=j-start;
694 for(start; start<j; start++) {
695 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
696 enterflags[offset++]=otd->numenterflags;
697 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
698 offset+=otd->numenterflags;
700 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
702 tmpftl->task=currtask;
703 tmpftl->numflags=numenterflags;
704 tmpftl->flags=enterflags;
712 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
713 while(andlist!=NULL) {
714 struct failedtasklist *tmp=andlist;
715 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
716 struct parmaeterwrapper *next;
721 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
724 andlist=andlist->next;
729 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
730 struct classanalysiswrapper * classwrapper=NULL;
732 /*test what optionaltaskdescriptors are available, find the class
734 if (classanalysiswrapperarray[currobj->type]!=NULL) {
735 classwrapper = classanalysiswrapperarray[currobj->type];
740 /* We have a failure */
741 if (failedfses==NULL) {
742 /* Failed in normal state */
743 /*first time the method is invoked*/
745 struct fsanalysiswrapper *fswrapper=NULL;
747 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
748 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
749 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
750 //we only match exactly here
755 if(fswrapper==NULL) //nothing to do in this state
757 for(i=0; i<fswrapper->numtaskfailures; i++) {
759 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
760 if (taskfail->task==task&&taskfail->index==index) {
762 while(start<taskfail->numoptionaltaskdescriptors) {
763 struct taskdescriptor *currtask=NULL;
767 int numenterflags, offset;
768 struct parameterwrapper *pw;
769 for(j=start; j<taskfail->numoptionaltaskdescriptors; j++) {
770 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
773 currindex=otd->index;
774 } else if (currtask!=otd->task||currindex!=otd->index)
776 totallength+=otd->numenterflags; //1 is to store the lengths
778 pw=currtask->descriptorarray[currindex]->queue;
779 numenterflags=j-start;
780 enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
783 for(start; start<j; start++) {
784 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
785 enterflags[offset++]=otd->numenterflags;
786 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
787 offset+=otd->numenterflags;
790 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
796 /* Failed in failed state */
799 for(i=0; i<numfailedfses; i++) {
800 int numfses=failedfses[offset++];
802 struct failedtasklist *andlist=NULL;
803 for(j=0; j<numfses; j++) {
804 int flagstate=failedfses[offset++];
805 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
809 mergefailedlists(&andlist, currlist);
811 enqueuelist(currobj, andlist);
815 /* No failure, but we are in a failed state */
816 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
818 /*Remove object from all queues */
819 while(flagptr!=NULL) {
820 struct parameterwrapper *next;
823 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
824 ObjectHashremove(flagptr->objectset, (int)currobj);
825 if (enterflags!=NULL)
830 /* Failed in failed state */
833 for(i=0; i<currobj->numfses; i++) {
834 int numfses=currobj->fses[offset++];
836 struct failedtasklist *andlist=NULL;
837 for(j=0; j<numfses; j++) {
838 int flagstate=currobj->fses[offset++];
839 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
843 mergefailedlists(&andlist, currlist);
845 enqueuelist(currobj, andlist);
853 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
854 void * taskpointerarray[MAXTASKPARAMS];
856 int failed[MAXTASKPARAMS];
859 int numparams=parameter->task->numParameters;
860 int numiterators=parameter->task->numTotal-1;
865 struct taskdescriptor * task=parameter->task;
868 if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
869 /* The object is already here...or it with the existing item */
875 ObjectHashget(parameter->objectset, (int) ptr, &oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
876 mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
877 ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
882 //only add if truly needed
891 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL); //this add the object to parameterwrapper
896 /* Add enqueued object to parameter vector */
897 taskpointerarray[parameter->slot]=ptr;
899 failed[parameter->slot]=(enterflags!=NULL);
902 /* Reset iterators */
903 for(j=0; j<numiterators; j++) {
904 toiReset(¶meter->iterators[j]);
907 /* Find initial state */
908 for(j=0; j<numiterators; j++) {
910 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))) {
911 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
913 /* Need to backtrack */
914 toiReset(¶meter->iterators[j]);
918 /* Nothing to enqueue */
925 /* Enqueue current state */
927 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
929 tpd->numParameters=numiterators+1;
930 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
932 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
934 for(j=0; j<=numiterators; j++) {
935 tpd->parameterArray[j]=taskpointerarray[j]; //store the actual parameters
937 tpd->failed[j]=failed[j];
938 if (failed[j]!=0&&failed[j]!=1) {
944 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
945 genputtable(activetasks, tpd, tpd);
947 RUNFREE(tpd->parameterArray);
949 RUNFREE(tpd->failed);
954 /* This loop iterates to the next parameter combination */
958 for(j=numiterators-1; j<numiterators; j++) {
960 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))) {
961 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
963 /* Need to backtrack */
964 toiReset(¶meter->iterators[j]);
968 /* Nothing more to enqueue */
976 /* Handler for signals. The signals catch null pointer errors and
977 arithmatic errors. */
979 void myhandler(int sig, siginfo_t *info, void *uap) {
982 printf("sig=%d\n",sig);
985 sigemptyset(&toclear);
986 sigaddset(&toclear, sig);
987 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
988 longjmp(error_handler,1);
993 struct RuntimeHash *fdtoobject;
995 void addreadfd(int fd) {
998 FD_SET(fd, &readfds);
1001 void removereadfd(int fd) {
1002 FD_CLR(fd, &readfds);
1003 if (maxreadfd==(fd+1)) {
1005 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1017 int * fsescopy(int *src, int len) {
1021 dst=RUNMALLOC(len*sizeof(int));
1022 memcpy(dst, src, len*sizeof(int));
1027 void executetasks() {
1028 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1030 int * fsesarray[MAXTASKPARAMS];
1031 int * oldfsesarray[MAXTASKPARAMS];
1032 int numfsesarray[MAXTASKPARAMS];
1035 /* Set up signal handlers */
1036 struct sigaction sig;
1037 sig.sa_sigaction=&myhandler;
1038 sig.sa_flags=SA_SIGINFO;
1039 sigemptyset(&sig.sa_mask);
1041 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1043 sigaction(SIGBUS,&sig,0);
1044 sigaction(SIGSEGV,&sig,0);
1045 sigaction(SIGFPE,&sig,0);
1046 sigaction(SIGPIPE,&sig,0);
1052 fdtoobject=allocateRuntimeHash(100);
1054 /* Map first block of memory to protected, anonymous page */
1055 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1058 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1060 /* Check if any filedescriptors have IO pending */
1063 struct timeval timeout={0,0};
1067 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1069 /* Process ready fd's */
1071 for(fd=0; fd<maxreadfd; fd++) {
1072 if (FD_ISSET(fd, &tmpreadfds)) {
1073 /* Set ready flag on object */
1075 // printf("Setting fd %d\n",fd);
1076 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1077 if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1078 enqueueObject(objptr);
1086 /* See if there are any active tasks */
1087 if (hashsize(activetasks)>0) {
1089 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1090 genfreekey(activetasks, currtpd);
1092 /* Check if this task has failed, allow a task that contains optional objects to fire */
1093 if (gencontains(failedtasks, currtpd)) {
1094 // Free up task parameter descriptor
1095 RUNFREE(currtpd->parameterArray);
1097 RUNFREE(currtpd->failed);
1102 int numparams=currtpd->task->numParameters;
1103 int numtotal=currtpd->task->numTotal;
1105 /* Make sure that the parameters are still in the queues */
1106 for(i=0; i<numparams; i++) {
1107 void * parameter=currtpd->parameterArray[i];
1108 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1109 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1111 /* Check that object is still in queue */
1114 int UNUSED, UNUSED2;
1116 int numflags, isnonfailed;
1117 int failed=currtpd->failed[i];
1118 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1119 RUNFREE(currtpd->parameterArray);
1120 RUNFREE(currtpd->failed);
1124 if (failed&&(flags!=NULL)) {
1127 numfsesarray[i]=numflags;
1128 } else if (!failed && isnonfailed) {
1129 //Non-failed parameter
1133 RUNFREE(currtpd->parameterArray);
1134 RUNFREE(currtpd->failed);
1142 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1143 RUNFREE(currtpd->parameterArray);
1151 /* Check that object still has necessary tags */
1152 for(j=0; j<pd->numbertags; j++) {
1153 int slotid=pd->tagarray[2*j]+numparams;
1154 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1155 if (!containstag(parameter, tagd)) {
1156 RUNFREE(currtpd->parameterArray);
1158 RUNFREE(currtpd->failed);
1165 taskpointerarray[i+OFFSET]=parameter;
1168 for(; i<numtotal; i++) {
1169 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1173 /* Checkpoint the state */
1175 ___fcrevert___=NULL;
1177 forward=cCreate(256, 0.4);
1178 reverse=cCreate(256, 0.4);
1179 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1182 if (x=setjmp(error_handler)) {
1186 printf("Fatal Error=%d, Recovering!\n",x);
1188 genputtable(failedtasks,currtpd,currtpd);
1190 REVERT_OBJ(___fcrevert___);
1192 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1196 for(counter=0; counter<currtpd->task->numParameters; counter++) {
1198 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1201 if (fsesarray[counter]!=NULL)
1202 RUNFREE(fsesarray[counter]);
1214 if (injectfailures) {
1215 if ((((double)random())/RAND_MAX)<failurechance) {
1216 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1217 longjmp(error_handler,10);
1220 /* Actually call task */
1222 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1223 taskpointerarray[1]=NULL;
1226 //get the task flags set
1227 for(i=0; i<numparams; i++) {
1228 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1229 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1230 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1234 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1235 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
1236 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1238 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
1241 for(i=0; i<numparams; i++) {
1243 if(oldfsesarray[i]!=NULL)
1244 RUNFREE(oldfsesarray[i]);
1249 while(___fcrevert___) {
1250 struct ___Object___ *tmpptr=___fcrevert___->___nextobject___;
1251 COMMIT_OBJ(___fcrevert___);
1252 ___fcrevert___=tmpptr;
1259 // Free up task parameter descriptor
1260 RUNFREE(currtpd->parameterArray);
1262 RUNFREE(currtpd->failed);
1273 /* This function processes an objects tags */
1274 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1277 for(i=0; i<pd->numbertags; i++) {
1278 int slotid=pd->tagarray[2*i];
1279 int tagid=pd->tagarray[2*i+1];
1281 if (statusarray[slotid+numparams]==0) {
1282 parameter->iterators[*iteratorcount].istag=1;
1283 parameter->iterators[*iteratorcount].tagid=tagid;
1284 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1285 parameter->iterators[*iteratorcount].tagobjectslot=index;
1286 statusarray[slotid+numparams]=1;
1293 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1296 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1298 parameter->iterators[*iteratorcount].istag=0;
1299 parameter->iterators[*iteratorcount].slot=index;
1300 parameter->iterators[*iteratorcount].objectset=objectset;
1301 statusarray[index]=1;
1303 for(i=0; i<pd->numbertags; i++) {
1304 int slotid=pd->tagarray[2*i];
1305 int tagid=pd->tagarray[2*i+1];
1306 if (statusarray[slotid+numparams]!=0) {
1307 /* This tag has already been enqueued, use it to narrow search */
1308 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1312 parameter->iterators[*iteratorcount].numtags=tagcount;
1317 /* This function builds the iterators for a task & parameter */
1319 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1320 int statusarray[MAXTASKPARAMS];
1322 int numparams=task->numParameters;
1323 int iteratorcount=0;
1324 for(i=0; i<MAXTASKPARAMS; i++) statusarray[i]=0;
1326 statusarray[index]=1; /* Initial parameter */
1327 /* Process tags for initial iterator */
1329 processtags(task->descriptorarray[index], index, parameter, &iteratorcount, statusarray, numparams);
1333 /* Check for objects with existing tags */
1334 for(i=0; i<numparams; i++) {
1335 if (statusarray[i]==0) {
1336 struct parameterdescriptor *pd=task->descriptorarray[i];
1338 for(j=0; j<pd->numbertags; j++) {
1339 int slotid=pd->tagarray[2*j];
1340 if(statusarray[slotid+numparams]!=0) {
1341 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1342 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1349 /* Next do objects w/ unbound tags*/
1351 for(i=0; i<numparams; i++) {
1352 if (statusarray[i]==0) {
1353 struct parameterdescriptor *pd=task->descriptorarray[i];
1354 if (pd->numbertags>0) {
1355 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1356 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1362 /* Nothing with a tag enqueued */
1364 for(i=0; i<numparams; i++) {
1365 if (statusarray[i]==0) {
1366 struct parameterdescriptor *pd=task->descriptorarray[i];
1367 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1368 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1381 for(i=0; i<numtasks; i++) {
1382 struct taskdescriptor * task=taskarray[i];
1383 printf("%s\n", task->name);
1384 for(j=0; j<task->numParameters; j++) {
1385 struct parameterdescriptor *param=task->descriptorarray[j];
1386 struct parameterwrapper *parameter=param->queue;
1387 struct ObjectHash * set=parameter->objectset;
1388 struct ObjectIterator objit;
1389 printf(" Parameter %d\n", j);
1390 ObjectHashiterator(set, &objit);
1391 while(ObjhasNext(&objit)) {
1392 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1393 struct ___Object___ * tagptr=obj->___tags___;
1394 int nonfailed=Objdata4(&objit);
1395 int numflags=Objdata3(&objit);
1396 int flags=Objdata2(&objit);
1398 printf(" Contains %lx\n", obj);
1399 printf(" flag=%d\n", obj->flag);
1401 printf(" flagsstored=%x\n",flags);
1402 printf(" numflags=%d\n", numflags);
1403 printf(" nonfailed=%d\n",nonfailed);
1406 } else if (tagptr->type==TAGTYPE) {
1407 printf(" tag=%lx\n",tagptr);
1410 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1411 for(; tagindex<ao->___cachedCode___; tagindex++) {
1412 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1421 /* This function processes the task information to create queues for
1422 each parameter type. */
1424 void processtasks() {
1426 for(i=0; i<numtasks; i++) {
1427 struct taskdescriptor * task=taskarray[i];
1430 for(j=0; j<task->numParameters; j++) {
1431 struct parameterdescriptor *param=task->descriptorarray[j];
1432 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1433 struct parameterwrapper ** ptr=&objectqueues[param->type];
1435 param->queue=parameter;
1436 parameter->objectset=allocateObjectHash(10);
1437 parameter->numberofterms=param->numberterms;
1438 parameter->intarray=param->intarray;
1439 parameter->numbertags=param->numbertags;
1440 parameter->tagarray=param->tagarray;
1441 parameter->task=task;
1443 /* Link new queue in */
1445 ptr=&((*ptr)->next);
1449 /* Build iterators for parameters */
1450 for(j=0; j<task->numParameters; j++) {
1451 struct parameterdescriptor *param=task->descriptorarray[j];
1452 struct parameterwrapper *parameter=param->queue;
1453 builditerators(task, j, parameter);
1458 void toiReset(struct tagobjectiterator * it) {
1461 } else if (it->numtags>0) {
1467 ObjectHashiterator(it->objectset, &it->it);
1474 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1477 /* Get object with tags */
1478 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1479 struct ___Object___ *tagptr=obj->___tags___;
1480 if (tagptr->type==TAGTYPE) {
1481 if ((it->tagobjindex==0)&& /* First object */
1482 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1487 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1488 int tagindex=it->tagobjindex;
1489 for(; tagindex<ao->___cachedCode___; tagindex++) {
1490 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1491 if (td->flag==it->tagid) {
1492 it->tagobjindex=tagindex; /* Found right type of tag */
1498 } else if (it->numtags>0) {
1499 /* Use tags to locate appropriate objects */
1500 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1501 struct ___Object___ *objptr=tag->flagptr;
1503 if (objptr->type!=OBJECTARRAYTYPE) {
1504 if (it->tagobjindex>0)
1506 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1508 for(i=1; i<it->numtags; i++) {
1509 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1510 if (!containstag(objptr,tag2))
1514 if (it->failedstate==1) {
1515 int UNUSED, UNUSED2;
1518 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1527 int UNUSED, UNUSED2;
1530 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1539 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1543 if (it->failedstate==1) {
1544 int UNUSED, UNUSED2;
1547 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1548 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1557 for(tagindex=it->tagobjindex; tagindex<ao->___cachedCode___; tagindex++) {
1558 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1559 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1561 for(i=1; i<it->numtags; i++) {
1562 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1563 if (!containstag(objptr,tag2))
1568 int UNUSED, UNUSED2;
1569 int flags, isnonfailed;
1570 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1571 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1577 it->tagobjindex=tagindex;
1582 it->tagobjindex=tagindex;
1587 if (it->failedstate==1) {
1588 if (Objdata2(&it->it))
1595 if (ObjhasNext(&it->it)) {
1596 if (!Objdata4(&it->it)) {
1604 return ObjhasNext(&it->it);
1609 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1611 struct ___Object___ * objptr=tag->flagptr;
1612 if (objptr->type==OBJECTARRAYTYPE) {
1613 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1614 for(j=0; j<ao->___cachedCode___; j++) {
1615 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1623 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1624 /* hasNext has all of the intelligence */
1627 /* Get object with tags */
1628 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1629 struct ___Object___ *tagptr=obj->___tags___;
1631 failed[it->slot]=0; //have to set it to something
1633 if (tagptr->type==TAGTYPE) {
1635 objectarray[it->slot]=tagptr;
1637 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1638 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1640 } else if (it->numtags>0) {
1641 /* Use tags to locate appropriate objects */
1642 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1643 struct ___Object___ *objptr=tag->flagptr;
1644 if (objptr->type!=OBJECTARRAYTYPE) {
1646 failed[it->slot]=it->failedstate;
1647 objectarray[it->slot]=objptr;
1648 if (it->failedstate==0) {
1656 objectarray[it->slot]=objptr;
1659 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1661 failed[it->slot]=it->failedstate;
1662 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1663 if (it->failedstate==0) {
1670 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1674 /* Iterate object */
1675 void * tmpp = (void *) Objkey(&it->it);
1676 objectarray[it->slot]=tmpp;
1678 failed[it->slot]=it->failedstate;
1679 if (it->failedstate==0) {