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___ *) aonew;
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");
273 /* This function allocates a new tag. */
275 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
276 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
278 struct ___TagDescriptor___ * allocate_tag(int index) {
279 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
288 /* This function updates the flag for object ptr. It or's the flag
289 with the or mask and and's it with the andmask. */
291 void flagbody(struct ___Object___ *ptr, int flag);
293 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
296 int flagcomp(const int *val1, const int *val2) {
297 return (*val1)-(*val2);
300 void flagorand(void * ptr, int ormask, int andmask) {
302 struct ___Object___ * obj = (struct ___Object___ *)ptr;
303 if(obj->numfses){/*store the information about fses*/
304 int flag, i, j,counter, offset=0;
305 for(i=0;i<obj->numfses;i++) {
307 counter=obj->fses[offset++];
309 for(j=0;j<counter;j++) {
310 flag=obj->fses[offset];
311 obj->fses[offset++]=(flag|ormask)&andmask;
313 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
315 enqueueoptional(obj, 0, NULL, NULL, 0);
320 int oldflag=((int *)ptr)[1];
321 int flag=ormask|oldflag;
327 void intflagorand(void * ptr, int ormask, int andmask) {
329 struct ___Object___ * obj = (struct ___Object___ *)ptr;
330 if(obj->numfses) {/*store the information about fses*/
331 int flag, i, j,counter, offset=0;
332 for(i=0;i<obj->numfses;i++) {
334 counter=obj->fses[offset++];
336 for(j=0;j<counter;j++) {
337 flag=obj->fses[offset];
338 obj->fses[offset++]=(flag|ormask)&andmask;
340 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
342 enqueueoptional(obj, 0, NULL, NULL, 0);
347 int oldflag=((int *)ptr)[1];
348 int flag=ormask|oldflag;
350 if (flag==oldflag) /* Don't do anything */
352 else flagbody(ptr, flag);
356 void flagorandinit(void * ptr, int ormask, int andmask) {
357 int oldflag=((int *)ptr)[1];
358 int flag=ormask|oldflag;
363 void flagbody(struct ___Object___ *ptr, int flag) {
364 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
367 /*Remove object from all queues */
368 while(flagptr!=NULL) {
369 struct parameterwrapper *next;
370 int UNUSED, UNUSED2, UNUSED3;
371 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, &UNUSED, &UNUSED2, &UNUSED3);
372 ObjectHashremove(flagptr->objectset, (int)ptr);
377 struct QueueItem *tmpptr;
378 struct parameterwrapper * parameter=objectqueues[ptr->type];
380 struct parameterwrapper * prevptr=NULL;
381 struct ___Object___ *tagptr=ptr->___tags___;
383 /* Outer loop iterates through all parameter queues an object of
384 this type could be in. */
386 while(parameter!=NULL) {
388 if (parameter->numbertags>0) {
390 goto nextloop;//that means the object has no tag but that param needs tag
391 else if(tagptr->type==TAGTYPE) {//one tag
392 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
393 for(i=0;i<parameter->numbertags;i++) {
394 //slotid is parameter->tagarray[2*i];
395 int tagid=parameter->tagarray[2*i+1];
396 if (tagid!=tagptr->flag)
397 goto nextloop; /*We don't have this tag */
399 } else {//multiple tags
400 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
401 for(i=0;i<parameter->numbertags;i++) {
402 //slotid is parameter->tagarray[2*i];
403 int tagid=parameter->tagarray[2*i+1];
405 for(j=0;j<ao->___cachedCode___;j++) {
406 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
417 for(i=0;i<parameter->numberofterms;i++) {
418 int andmask=parameter->intarray[i*2];
419 int checkmask=parameter->intarray[i*2+1];
420 if ((flag&andmask)==checkmask) {
421 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
427 parameter=parameter->next;
429 ptr->flagptr=prevptr;
435 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
437 struct ___Object___ * tagptr = currobj->___tags___;
438 if(fswrapper->numtags>0){
440 return 0; //that means the object has no tag but that param
442 else if(tagptr->type==TAGTYPE) {//one tag
443 if(fswrapper->numtags!=1)
444 return 0; //we don't have the right number of tags
445 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
446 if (fswrapper->tags[0]!=tagptr->flag)
448 } else { //multiple tags
449 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
453 if(ao->___length___!=fswrapper->numtags)
454 return 0;//we don't have the right number of tags
455 for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
456 int tagid=fswrapper->tags[tag_counter];
458 for(j=0;j<ao->___cachedCode___;j++) {
459 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
469 int getlength(int *flist, int len) {
473 int size=flist[count];
479 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
480 int size1=getlength(flist1, len1);
481 int size2=getlength(flist2, len2);
482 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
483 memcpy(merge, flist1, size1*sizeof(int));
484 memcpy(&merge[size1], flist2, size2*sizeof(int));
488 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
492 while(i<len1||j<len2) {
493 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
495 merge[count]=flist1[i];
499 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
501 merge[count]=flist2[j];
505 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
507 merge[count]=flist1[i];
517 /* Merge flags from ftlmerge into ftl. */
518 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
523 for(i=0;i<ftl->numflags;i++) {
524 int len=ftl->flags[offset++];
526 for(j=0;j<ftlmerge->numflags;j++) {
527 int lenmerge=ftlmerge->flags[offsetmerge++];
528 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
529 offsetmerge+=lenmerge;
533 mergedlist=RUNMALLOC(sizeof(int)*length);
537 for(i=0;i<ftl->numflags;i++) {
538 int len=ftl->flags[offset++];
540 for(j=0;j<ftlmerge->numflags;j++) {
541 int lenmerge=ftlmerge->flags[offsetmerge++];
542 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
543 mergedlist[length]=size;
548 ftl->flags=mergedlist;
549 ftl->numflags*=ftlmerge->numflags;
552 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
553 struct failedtasklist *tmpptr;
554 while((*andlist)!=NULL) {
555 struct failedtasklist *searchftl=list;
556 while(searchftl!=NULL) {
557 if ((*andlist)->task==searchftl->task&&
558 (*andlist)->index==searchftl->index) {
559 mergeitems(*andlist, searchftl);
562 searchftl=searchftl->next;
564 if (searchftl==NULL) {
565 //didn't find andlist
567 *andlist=(*andlist)->next;//splice item out of list
568 RUNFREE(tmpptr->flags); //free the item
571 andlist=&((*andlist)->next); //iterate to next item
574 //free the list we're searching
577 RUNFREE(list->flags);
583 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
584 struct failedtasklist *list=NULL;
586 struct fsanalysiswrapper *fswrapper=NULL;
587 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
588 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
589 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
590 //we only match exactly here
597 for(i=0;i<fswrapper->numtaskfailures;i++) {
599 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
600 if (taskfail->task==task&&taskfail->index==index) {
602 while(start<taskfail->numoptionaltaskdescriptors) {
603 struct taskdescriptor *currtask=NULL;
604 struct failedtasklist *tmpftl;
608 int numenterflags, offset;
609 struct parameterwrapper *pw;
610 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
611 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
614 currindex=otd->index;
615 } else if (currtask!=otd->task||currindex!=otd->index)
617 totallength+=otd->numenterflags;
619 pw=currtask->descriptorarray[currindex]->queue;
620 enterflags=RUNMALLOC(totallength*sizeof(int));
621 numenterflags=j-start;
623 for(start;start<j;start++) {
624 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
625 enterflags[offset++]=otd->numenterflags;
626 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
627 offset+=otd->numenterflags;
629 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
631 tmpftl->task=currtask;
632 tmpftl->numflags=numenterflags;
633 tmpftl->flags=enterflags;
641 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
642 struct failedtasklist *list=NULL;
645 struct fsanalysiswrapper *fswrapper=NULL;
646 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
647 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
648 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
649 //we only match exactly here
657 while(start<fswrapper->numoptionaltaskdescriptors) {
658 struct taskdescriptor *currtask=NULL;
659 struct failedtasklist *tmpftl;
664 int numenterflags, offset;
665 struct parameterwrapper *pw;
666 for(j=start;j<fswrapper->numoptionaltaskdescriptors;j++) {
667 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
670 currindex=otd->index;
671 } else if (currtask!=otd->task||currindex!=otd->index)
673 totallength+=otd->numenterflags;
675 pw=currtask->descriptorarray[currindex]->queue;
676 enterflags=RUNMALLOC(totallength*sizeof(int));
677 numenterflags=j-start;
679 for(start;start<j;start++) {
680 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
681 enterflags[offset++]=otd->numenterflags;
682 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
683 offset+=otd->numenterflags;
685 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
687 tmpftl->task=currtask;
688 tmpftl->numflags=numenterflags;
689 tmpftl->flags=enterflags;
697 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
698 while(andlist!=NULL) {
699 struct failedtasklist *tmp=andlist;
700 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
701 struct parmaeterwrapper *next;
706 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
709 andlist=andlist->next;
714 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
715 struct classanalysiswrapper * classwrapper=NULL;
717 /*test what optionaltaskdescriptors are available, find the class
719 if (classanalysiswrapperarray[currobj->type]!=NULL) {
720 classwrapper = classanalysiswrapperarray[currobj->type];
725 /* We have a failure */
726 if (failedfses==NULL) {
727 /* Failed in normal state */
728 /*first time the method is invoked*/
730 struct fsanalysiswrapper *fswrapper=NULL;
732 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
733 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
734 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
735 //we only match exactly here
740 if(fswrapper==NULL) //nothing to do in this state
742 for(i=0;i<fswrapper->numtaskfailures;i++) {
744 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
745 if (taskfail->task==task&&taskfail->index==index) {
747 while(start<taskfail->numoptionaltaskdescriptors) {
748 struct taskdescriptor *currtask=NULL;
752 int numenterflags, offset;
753 struct parameterwrapper *pw;
754 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
755 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
758 currindex=otd->index;
759 } else if (currtask!=otd->task||currindex!=otd->index)
761 totallength+=otd->numenterflags;
763 pw=currtask->descriptorarray[currindex]->queue;
764 enterflags=RUNMALLOC(totallength*sizeof(int));
765 numenterflags=j-start;
768 for(start;start<j;start++) {
769 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
770 enterflags[offset++]=otd->numenterflags;
771 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
772 offset+=otd->numenterflags;
775 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
781 /* Failed in failed state */
784 for(i=0;i<numfailedfses;i++) {
785 int numfses=failedfses[offset++];
787 struct failedtasklist *andlist=NULL;
788 for(j=0;j<numfses;j++) {
789 int flagstate=failedfses[offset++];
790 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
794 mergefailedlists(&andlist, currlist);
796 enqueuelist(currobj, andlist);
800 /* No failure, but we are in a failed state */
801 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
803 /*Remove object from all queues */
804 while(flagptr!=NULL) {
805 struct parameterwrapper *next;
806 int UNUSED, UNUSED2, UNUSED3;
807 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, &UNUSED, &UNUSED2, &UNUSED3);
808 ObjectHashremove(flagptr->objectset, (int)currobj);
812 /* Failed in failed state */
815 for(i=0;i<currobj->numfses;i++) {
816 int numfses=currobj->fses[offset++];
818 struct failedtasklist *andlist=NULL;
819 for(j=0;j<numfses;j++) {
820 int flagstate=currobj->fses[offset++];
821 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
825 mergefailedlists(&andlist, currlist);
827 enqueuelist(currobj, andlist);
835 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
836 void * taskpointerarray[MAXTASKPARAMS];
838 int failed[MAXTASKPARAMS];
841 int numparams=parameter->task->numParameters;
842 int numiterators=parameter->task->numTotal-1;
847 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
873 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
878 /* Add enqueued object to parameter vector */
879 taskpointerarray[parameter->slot]=ptr;
881 failed[parameter->slot]=(enterflags!=NULL);
884 /* Reset iterators */
885 for(j=0;j<numiterators;j++) {
886 toiReset(¶meter->iterators[j]);
889 /* Find initial state */
890 for(j=0;j<numiterators;j++) {
892 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
893 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
895 /* Need to backtrack */
896 toiReset(¶meter->iterators[j]);
900 /* Nothing to enqueue */
907 /* Enqueue current state */
909 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
911 tpd->numParameters=numiterators+1;
912 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
914 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
916 for(j=0;j<=numiterators;j++){
917 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
919 tpd->failed[j]=failed[j];
920 if (failed[j]!=0&&failed[j]!=1) {
926 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
927 genputtable(activetasks, tpd, tpd);
929 RUNFREE(tpd->parameterArray);
931 RUNFREE(tpd->failed);
936 /* This loop iterates to the next parameter combination */
940 for(j=numiterators-1; j<numiterators;j++) {
942 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
943 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
945 /* Need to backtrack */
946 toiReset(¶meter->iterators[j]);
950 /* Nothing more to enqueue */
958 /* Handler for signals. The signals catch null pointer errors and
959 arithmatic errors. */
961 void myhandler(int sig, siginfo_t *info, void *uap) {
964 printf("sig=%d\n",sig);
967 sigemptyset(&toclear);
968 sigaddset(&toclear, sig);
969 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
970 longjmp(error_handler,1);
975 struct RuntimeHash *fdtoobject;
977 void addreadfd(int fd) {
980 FD_SET(fd, &readfds);
983 void removereadfd(int fd) {
984 FD_CLR(fd, &readfds);
985 if (maxreadfd==(fd+1)) {
987 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
999 int * fsescopy(int *src, int len) {
1003 dst=RUNMALLOC(len*sizeof(int));
1004 memcpy(dst, src, len*sizeof(int));
1009 void executetasks() {
1010 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1012 int * fsesarray[MAXTASKPARAMS];
1013 int * oldfsesarray[MAXTASKPARAMS];
1014 int numfsesarray[MAXTASKPARAMS];
1017 /* Set up signal handlers */
1018 struct sigaction sig;
1019 sig.sa_sigaction=&myhandler;
1020 sig.sa_flags=SA_SIGINFO;
1021 sigemptyset(&sig.sa_mask);
1023 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1024 sigaction(SIGBUS,&sig,0);
1025 sigaction(SIGSEGV,&sig,0);
1026 sigaction(SIGFPE,&sig,0);
1027 sigaction(SIGPIPE,&sig,0);
1032 fdtoobject=allocateRuntimeHash(100);
1034 /* Map first block of memory to protected, anonymous page */
1035 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1038 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1040 /* Check if any filedescriptors have IO pending */
1043 struct timeval timeout={0,0};
1047 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1049 /* Process ready fd's */
1051 for(fd=0;fd<maxreadfd;fd++) {
1052 if (FD_ISSET(fd, &tmpreadfds)) {
1053 /* Set ready flag on object */
1055 // printf("Setting fd %d\n",fd);
1056 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1057 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
1064 /* See if there are any active tasks */
1065 if (hashsize(activetasks)>0) {
1067 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1068 genfreekey(activetasks, currtpd);
1070 /* Check if this task has failed, allow a task that contains optional objects to fire */
1071 if (gencontains(failedtasks, currtpd)) {
1072 // Free up task parameter descriptor
1073 RUNFREE(currtpd->parameterArray);
1077 int numparams=currtpd->task->numParameters;
1078 int numtotal=currtpd->task->numTotal;
1080 /* Make sure that the parameters are still in the queues */
1081 for(i=0;i<numparams;i++) {
1082 void * parameter=currtpd->parameterArray[i];
1083 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1084 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1086 /* Check that object is still in queue */
1089 int UNUSED, UNUSED2;
1091 int numflags, isnonfailed;
1092 int failed=currtpd->failed[i];
1093 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1094 RUNFREE(currtpd->parameterArray);
1095 RUNFREE(currtpd->failed);
1099 if (failed&&(flags!=NULL)) {
1102 numfsesarray[i]=numflags;
1103 } else if (!failed && isnonfailed) {
1104 //Non-failed parameter
1108 RUNFREE(currtpd->parameterArray);
1109 RUNFREE(currtpd->failed);
1117 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1118 RUNFREE(currtpd->parameterArray);
1126 /* Check that object still has necessary tags */
1127 for(j=0;j<pd->numbertags;j++) {
1128 int slotid=pd->tagarray[2*j]+numparams;
1129 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1130 if (!containstag(parameter, tagd)) {
1131 RUNFREE(currtpd->parameterArray);
1137 taskpointerarray[i+OFFSET]=parameter;
1140 for(;i<numtotal;i++) {
1141 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1145 /* Checkpoint the state */
1146 forward=allocateRuntimeHash(100);
1147 reverse=allocateRuntimeHash(100);
1148 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1150 if (x=setjmp(error_handler)) {
1154 printf("Fatal Error=%d, Recovering!\n",x);
1156 genputtable(failedtasks,currtpd,currtpd);
1157 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1160 for(counter=0; counter<currtpd->task->numParameters; counter++){
1162 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1165 if (fsesarray[counter]!=NULL)
1166 RUNFREE(fsesarray[counter]);
1169 freeRuntimeHash(forward);
1170 freeRuntimeHash(reverse);
1175 if (injectfailures) {
1176 if ((((double)random())/RAND_MAX)<failurechance) {
1177 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1178 longjmp(error_handler,10);
1181 /* Actually call task */
1183 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1184 taskpointerarray[1]=NULL;
1187 //get the task flags set
1188 for(i=0;i<numparams;i++) {
1189 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1190 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1191 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1195 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1198 printf("[%x]\n",currtpd);
1199 for(i=0;i<currtpd->numParameters;i++) {
1200 printf("%x ", currtpd->parameterArray[i]);
1204 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1205 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1207 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1210 for(i=0;i<numparams;i++) {
1212 if(oldfsesarray[i]!=NULL)
1213 RUNFREE(oldfsesarray[i]);
1217 freeRuntimeHash(forward);
1218 freeRuntimeHash(reverse);
1220 // Free up task parameter descriptor
1221 RUNFREE(currtpd->parameterArray);
1231 /* This function processes an objects tags */
1232 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1235 for(i=0;i<pd->numbertags;i++) {
1236 int slotid=pd->tagarray[2*i];
1237 int tagid=pd->tagarray[2*i+1];
1239 if (statusarray[slotid+numparams]==0) {
1240 parameter->iterators[*iteratorcount].istag=1;
1241 parameter->iterators[*iteratorcount].tagid=tagid;
1242 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1243 parameter->iterators[*iteratorcount].tagobjectslot=index;
1244 statusarray[slotid+numparams]=1;
1251 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1254 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1256 parameter->iterators[*iteratorcount].istag=0;
1257 parameter->iterators[*iteratorcount].slot=index;
1258 parameter->iterators[*iteratorcount].objectset=objectset;
1259 statusarray[index]=1;
1261 for(i=0;i<pd->numbertags;i++) {
1262 int slotid=pd->tagarray[2*i];
1263 int tagid=pd->tagarray[2*i+1];
1264 if (statusarray[slotid+numparams]!=0) {
1265 /* This tag has already been enqueued, use it to narrow search */
1266 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1270 parameter->iterators[*iteratorcount].numtags=tagcount;
1275 /* This function builds the iterators for a task & parameter */
1277 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1278 int statusarray[MAXTASKPARAMS];
1280 int numparams=task->numParameters;
1281 int iteratorcount=0;
1282 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1284 statusarray[index]=1; /* Initial parameter */
1285 /* Process tags for initial iterator */
1287 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1291 /* Check for objects with existing tags */
1292 for(i=0;i<numparams;i++) {
1293 if (statusarray[i]==0) {
1294 struct parameterdescriptor *pd=task->descriptorarray[i];
1296 for(j=0;j<pd->numbertags;j++) {
1297 int slotid=pd->tagarray[2*j];
1298 if(statusarray[slotid+numparams]!=0) {
1299 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1300 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1307 /* Next do objects w/ unbound tags*/
1309 for(i=0;i<numparams;i++) {
1310 if (statusarray[i]==0) {
1311 struct parameterdescriptor *pd=task->descriptorarray[i];
1312 if (pd->numbertags>0) {
1313 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1314 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1320 /* Nothing with a tag enqueued */
1322 for(i=0;i<numparams;i++) {
1323 if (statusarray[i]==0) {
1324 struct parameterdescriptor *pd=task->descriptorarray[i];
1325 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1326 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1339 for(i=0;i<numtasks;i++) {
1340 struct taskdescriptor * task=taskarray[i];
1341 printf("%s\n", task->name);
1342 for(j=0;j<task->numParameters;j++) {
1343 struct parameterdescriptor *param=task->descriptorarray[j];
1344 struct parameterwrapper *parameter=param->queue;
1345 struct ObjectHash * set=parameter->objectset;
1346 struct ObjectIterator objit;
1347 printf(" Parameter %d\n", j);
1348 ObjectHashiterator(set, &objit);
1349 while(ObjhasNext(&objit)) {
1350 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1351 struct ___Object___ * tagptr=obj->___tags___;
1353 printf(" Contains %lx\n", obj);
1354 printf(" flag=%d\n", obj->flag);
1356 } else if (tagptr->type==TAGTYPE) {
1357 printf(" tag=%lx\n",tagptr);
1360 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1361 for(;tagindex<ao->___cachedCode___;tagindex++) {
1362 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1371 /* This function processes the task information to create queues for
1372 each parameter type. */
1374 void processtasks() {
1376 for(i=0;i<numtasks;i++) {
1377 struct taskdescriptor * task=taskarray[i];
1380 for(j=0;j<task->numParameters;j++) {
1381 struct parameterdescriptor *param=task->descriptorarray[j];
1382 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1383 struct parameterwrapper ** ptr=&objectqueues[param->type];
1385 param->queue=parameter;
1386 parameter->objectset=allocateObjectHash(10);
1387 parameter->numberofterms=param->numberterms;
1388 parameter->intarray=param->intarray;
1389 parameter->numbertags=param->numbertags;
1390 parameter->tagarray=param->tagarray;
1391 parameter->task=task;
1392 /* Link new queue in */
1394 ptr=&((*ptr)->next);
1398 /* Build iterators for parameters */
1399 for(j=0;j<task->numParameters;j++) {
1400 struct parameterdescriptor *param=task->descriptorarray[j];
1401 struct parameterwrapper *parameter=param->queue;
1403 builditerators(task, j, parameter);
1408 void toiReset(struct tagobjectiterator * it) {
1411 } else if (it->numtags>0) {
1417 ObjectHashiterator(it->objectset, &it->it);
1424 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1427 /* Get object with tags */
1428 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1429 struct ___Object___ *tagptr=obj->___tags___;
1430 if (tagptr->type==TAGTYPE) {
1431 if ((it->tagobjindex==0)&& /* First object */
1432 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1437 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1438 int tagindex=it->tagobjindex;
1439 for(;tagindex<ao->___cachedCode___;tagindex++) {
1440 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1441 if (td->flag==it->tagid) {
1442 it->tagobjindex=tagindex; /* Found right type of tag */
1448 } else if (it->numtags>0) {
1449 /* Use tags to locate appropriate objects */
1450 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1451 struct ___Object___ *objptr=tag->flagptr;
1453 if (objptr->type!=OBJECTARRAYTYPE) {
1454 if (it->tagobjindex>0)
1456 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1458 for(i=1;i<it->numtags;i++) {
1459 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1460 if (!containstag(objptr,tag2))
1464 if (it->failedstate==1) {
1465 int UNUSED, UNUSED2;
1468 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1477 int UNUSED, UNUSED2;
1480 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1489 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1493 if (it->failedstate==1) {
1494 int UNUSED, UNUSED2;
1497 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1498 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1507 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1508 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1509 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1511 for(i=1;i<it->numtags;i++) {
1512 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1513 if (!containstag(objptr,tag2))
1518 int UNUSED, UNUSED2;
1519 int flags, isnonfailed;
1520 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1521 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1527 it->tagobjindex=tagindex;
1532 it->tagobjindex=tagindex;
1537 if (it->failedstate==1) {
1538 if (Objdata2(&it->it))
1545 if (ObjhasNext(&it->it)) {
1546 if (!Objdata4(&it->it)) {
1554 return ObjhasNext(&it->it);
1559 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1561 struct ___Object___ * objptr=tag->flagptr;
1562 if (objptr->type==OBJECTARRAYTYPE) {
1563 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1564 for(j=0;j<ao->___cachedCode___;j++) {
1565 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1573 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1574 /* hasNext has all of the intelligence */
1577 /* Get object with tags */
1578 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1579 struct ___Object___ *tagptr=obj->___tags___;
1581 failed[it->slot]=0; //have to set it to something
1583 if (tagptr->type==TAGTYPE) {
1585 objectarray[it->slot]=tagptr;
1587 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1588 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1590 } else if (it->numtags>0) {
1591 /* Use tags to locate appropriate objects */
1592 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1593 struct ___Object___ *objptr=tag->flagptr;
1594 if (objptr->type!=OBJECTARRAYTYPE) {
1596 failed[it->slot]=it->failedstate;
1597 objectarray[it->slot]=objptr;
1598 if (it->failedstate==0) {
1606 objectarray[it->slot]=objptr;
1609 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1611 failed[it->slot]=it->failedstate;
1612 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1613 if (it->failedstate==0) {
1620 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1624 /* Iterate object */
1625 objectarray[it->slot]=(void *)Objkey(&it->it);
1627 failed[it->slot]=it->failedstate;
1628 if (it->failedstate==0) {