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));
910 /* Need to backtrack */
911 toiReset(¶meter->iterators[j]);
915 /* Nothing to enqueue */
922 /* Enqueue current state */
924 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
926 tpd->numParameters=numiterators+1;
927 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
929 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
931 for(j=0;j<=numiterators;j++){
932 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
934 tpd->failed[j]=failed[j];
935 if (failed[j]!=0&&failed[j]!=1) {
941 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
942 genputtable(activetasks, tpd, tpd);
944 RUNFREE(tpd->parameterArray);
946 RUNFREE(tpd->failed);
951 /* This loop iterates to the next parameter combination */
955 for(j=numiterators-1; j<numiterators;j++) {
957 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))){
958 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
961 /* Need to backtrack */
962 toiReset(¶meter->iterators[j]);
966 /* Nothing more to enqueue */
974 /* Handler for signals. The signals catch null pointer errors and
975 arithmatic errors. */
977 void myhandler(int sig, siginfo_t *info, void *uap) {
980 printf("sig=%d\n",sig);
983 sigemptyset(&toclear);
984 sigaddset(&toclear, sig);
985 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
986 longjmp(error_handler,1);
991 struct RuntimeHash *fdtoobject;
993 void addreadfd(int fd) {
996 FD_SET(fd, &readfds);
999 void removereadfd(int fd) {
1000 FD_CLR(fd, &readfds);
1001 if (maxreadfd==(fd+1)) {
1003 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1015 int * fsescopy(int *src, int len) {
1019 dst=RUNMALLOC(len*sizeof(int));
1020 memcpy(dst, src, len*sizeof(int));
1025 void executetasks() {
1026 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1028 int * fsesarray[MAXTASKPARAMS];
1029 int * oldfsesarray[MAXTASKPARAMS];
1030 int numfsesarray[MAXTASKPARAMS];
1033 /* Set up signal handlers */
1034 struct sigaction sig;
1035 sig.sa_sigaction=&myhandler;
1036 sig.sa_flags=SA_SIGINFO;
1037 sigemptyset(&sig.sa_mask);
1039 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1040 sigaction(SIGBUS,&sig,0);
1041 sigaction(SIGSEGV,&sig,0);
1042 sigaction(SIGFPE,&sig,0);
1043 sigaction(SIGPIPE,&sig,0);
1048 fdtoobject=allocateRuntimeHash(100);
1050 /* Map first block of memory to protected, anonymous page */
1051 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1054 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1056 /* Check if any filedescriptors have IO pending */
1059 struct timeval timeout={0,0};
1063 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1065 /* Process ready fd's */
1067 for(fd=0;fd<maxreadfd;fd++) {
1068 if (FD_ISSET(fd, &tmpreadfds)) {
1069 /* Set ready flag on object */
1071 // printf("Setting fd %d\n",fd);
1072 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1073 if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1074 enqueueObject(objptr);
1082 /* See if there are any active tasks */
1083 if (hashsize(activetasks)>0) {
1085 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1086 genfreekey(activetasks, currtpd);
1088 /* Check if this task has failed, allow a task that contains optional objects to fire */
1089 if (gencontains(failedtasks, currtpd)) {
1090 // Free up task parameter descriptor
1091 RUNFREE(currtpd->parameterArray);
1093 RUNFREE(currtpd->failed);
1098 int numparams=currtpd->task->numParameters;
1099 int numtotal=currtpd->task->numTotal;
1101 /* Make sure that the parameters are still in the queues */
1102 for(i=0;i<numparams;i++) {
1103 void * parameter=currtpd->parameterArray[i];
1104 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1105 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1107 /* Check that object is still in queue */
1110 int UNUSED, UNUSED2;
1112 int numflags, isnonfailed;
1113 int failed=currtpd->failed[i];
1114 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1115 RUNFREE(currtpd->parameterArray);
1116 RUNFREE(currtpd->failed);
1120 if (failed&&(flags!=NULL)) {
1123 numfsesarray[i]=numflags;
1124 } else if (!failed && isnonfailed) {
1125 //Non-failed parameter
1129 RUNFREE(currtpd->parameterArray);
1130 RUNFREE(currtpd->failed);
1138 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1139 RUNFREE(currtpd->parameterArray);
1147 /* Check that object still has necessary tags */
1148 for(j=0;j<pd->numbertags;j++) {
1149 int slotid=pd->tagarray[2*j]+numparams;
1150 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1151 if (!containstag(parameter, tagd)) {
1152 RUNFREE(currtpd->parameterArray);
1154 RUNFREE(currtpd->failed);
1161 taskpointerarray[i+OFFSET]=parameter;
1164 for(;i<numtotal;i++) {
1165 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1169 /* Checkpoint the state */
1170 forward=allocateRuntimeHash(100);
1171 reverse=allocateRuntimeHash(100);
1172 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1174 if (x=setjmp(error_handler)) {
1178 printf("Fatal Error=%d, Recovering!\n",x);
1180 genputtable(failedtasks,currtpd,currtpd);
1181 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1184 for(counter=0; counter<currtpd->task->numParameters; counter++){
1186 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1189 if (fsesarray[counter]!=NULL)
1190 RUNFREE(fsesarray[counter]);
1193 freeRuntimeHash(forward);
1194 freeRuntimeHash(reverse);
1199 if (injectfailures) {
1200 if ((((double)random())/RAND_MAX)<failurechance) {
1201 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1202 longjmp(error_handler,10);
1205 /* Actually call task */
1207 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1208 taskpointerarray[1]=NULL;
1211 //get the task flags set
1212 for(i=0;i<numparams;i++) {
1213 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1214 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1215 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1219 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1220 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1221 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1223 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1226 for(i=0;i<numparams;i++) {
1228 if(oldfsesarray[i]!=NULL)
1229 RUNFREE(oldfsesarray[i]);
1233 freeRuntimeHash(forward);
1234 freeRuntimeHash(reverse);
1236 // Free up task parameter descriptor
1237 RUNFREE(currtpd->parameterArray);
1239 RUNFREE(currtpd->failed);
1250 /* This function processes an objects tags */
1251 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1254 for(i=0;i<pd->numbertags;i++) {
1255 int slotid=pd->tagarray[2*i];
1256 int tagid=pd->tagarray[2*i+1];
1258 if (statusarray[slotid+numparams]==0) {
1259 parameter->iterators[*iteratorcount].istag=1;
1260 parameter->iterators[*iteratorcount].tagid=tagid;
1261 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1262 parameter->iterators[*iteratorcount].tagobjectslot=index;
1263 statusarray[slotid+numparams]=1;
1270 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1273 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1275 parameter->iterators[*iteratorcount].istag=0;
1276 parameter->iterators[*iteratorcount].slot=index;
1277 parameter->iterators[*iteratorcount].objectset=objectset;
1278 statusarray[index]=1;
1280 for(i=0;i<pd->numbertags;i++) {
1281 int slotid=pd->tagarray[2*i];
1282 int tagid=pd->tagarray[2*i+1];
1283 if (statusarray[slotid+numparams]!=0) {
1284 /* This tag has already been enqueued, use it to narrow search */
1285 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1289 parameter->iterators[*iteratorcount].numtags=tagcount;
1294 /* This function builds the iterators for a task & parameter */
1296 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1297 int statusarray[MAXTASKPARAMS];
1299 int numparams=task->numParameters;
1300 int iteratorcount=0;
1301 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1303 statusarray[index]=1; /* Initial parameter */
1304 /* Process tags for initial iterator */
1306 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1310 /* Check for objects with existing tags */
1311 for(i=0;i<numparams;i++) {
1312 if (statusarray[i]==0) {
1313 struct parameterdescriptor *pd=task->descriptorarray[i];
1315 for(j=0;j<pd->numbertags;j++) {
1316 int slotid=pd->tagarray[2*j];
1317 if(statusarray[slotid+numparams]!=0) {
1318 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1319 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1326 /* Next do objects w/ unbound tags*/
1328 for(i=0;i<numparams;i++) {
1329 if (statusarray[i]==0) {
1330 struct parameterdescriptor *pd=task->descriptorarray[i];
1331 if (pd->numbertags>0) {
1332 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1333 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1339 /* Nothing with a tag enqueued */
1341 for(i=0;i<numparams;i++) {
1342 if (statusarray[i]==0) {
1343 struct parameterdescriptor *pd=task->descriptorarray[i];
1344 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1345 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1358 for(i=0;i<numtasks;i++) {
1359 struct taskdescriptor * task=taskarray[i];
1360 printf("%s\n", task->name);
1361 for(j=0;j<task->numParameters;j++) {
1362 struct parameterdescriptor *param=task->descriptorarray[j];
1363 struct parameterwrapper *parameter=param->queue;
1364 struct ObjectHash * set=parameter->objectset;
1365 struct ObjectIterator objit;
1366 printf(" Parameter %d\n", j);
1367 ObjectHashiterator(set, &objit);
1368 while(ObjhasNext(&objit)) {
1369 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1370 struct ___Object___ * tagptr=obj->___tags___;
1371 int nonfailed=Objdata4(&objit);
1372 int numflags=Objdata3(&objit);
1373 int flags=Objdata2(&objit);
1375 printf(" Contains %lx\n", obj);
1376 printf(" flag=%d\n", obj->flag);
1378 printf(" flagsstored=%x\n",flags);
1379 printf(" numflags=%d\n", numflags);
1380 printf(" nonfailed=%d\n",nonfailed);
1383 } else if (tagptr->type==TAGTYPE) {
1384 printf(" tag=%lx\n",tagptr);
1387 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1388 for(;tagindex<ao->___cachedCode___;tagindex++) {
1389 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1398 /* This function processes the task information to create queues for
1399 each parameter type. */
1401 void processtasks() {
1403 for(i=0;i<numtasks;i++) {
1404 struct taskdescriptor * task=taskarray[i];
1407 for(j=0;j<task->numParameters;j++) {
1408 struct parameterdescriptor *param=task->descriptorarray[j];
1409 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1410 struct parameterwrapper ** ptr=&objectqueues[param->type];
1412 param->queue=parameter;
1413 parameter->objectset=allocateObjectHash(10);
1414 parameter->numberofterms=param->numberterms;
1415 parameter->intarray=param->intarray;
1416 parameter->numbertags=param->numbertags;
1417 parameter->tagarray=param->tagarray;
1418 parameter->task=task;
1420 /* Link new queue in */
1422 ptr=&((*ptr)->next);
1426 /* Build iterators for parameters */
1427 for(j=0;j<task->numParameters;j++) {
1428 struct parameterdescriptor *param=task->descriptorarray[j];
1429 struct parameterwrapper *parameter=param->queue;
1430 builditerators(task, j, parameter);
1435 void toiReset(struct tagobjectiterator * it) {
1438 } else if (it->numtags>0) {
1444 ObjectHashiterator(it->objectset, &it->it);
1451 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1454 /* Get object with tags */
1455 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1456 struct ___Object___ *tagptr=obj->___tags___;
1457 if (tagptr->type==TAGTYPE) {
1458 if ((it->tagobjindex==0)&& /* First object */
1459 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1464 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1465 int tagindex=it->tagobjindex;
1466 for(;tagindex<ao->___cachedCode___;tagindex++) {
1467 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1468 if (td->flag==it->tagid) {
1469 it->tagobjindex=tagindex; /* Found right type of tag */
1475 } else if (it->numtags>0) {
1476 /* Use tags to locate appropriate objects */
1477 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1478 struct ___Object___ *objptr=tag->flagptr;
1480 if (objptr->type!=OBJECTARRAYTYPE) {
1481 if (it->tagobjindex>0)
1483 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1485 for(i=1;i<it->numtags;i++) {
1486 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1487 if (!containstag(objptr,tag2))
1491 if (it->failedstate==1) {
1492 int UNUSED, UNUSED2;
1495 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1504 int UNUSED, UNUSED2;
1507 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1516 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1520 if (it->failedstate==1) {
1521 int UNUSED, UNUSED2;
1524 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1525 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1534 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1535 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1536 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1538 for(i=1;i<it->numtags;i++) {
1539 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1540 if (!containstag(objptr,tag2))
1545 int UNUSED, UNUSED2;
1546 int flags, isnonfailed;
1547 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1548 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1554 it->tagobjindex=tagindex;
1559 it->tagobjindex=tagindex;
1564 if (it->failedstate==1) {
1565 if (Objdata2(&it->it))
1572 if (ObjhasNext(&it->it)) {
1573 if (!Objdata4(&it->it)) {
1581 return ObjhasNext(&it->it);
1586 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1588 struct ___Object___ * objptr=tag->flagptr;
1589 if (objptr->type==OBJECTARRAYTYPE) {
1590 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1591 for(j=0;j<ao->___cachedCode___;j++) {
1592 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1600 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1601 /* hasNext has all of the intelligence */
1604 /* Get object with tags */
1605 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1606 struct ___Object___ *tagptr=obj->___tags___;
1608 failed[it->slot]=0; //have to set it to something
1610 if (tagptr->type==TAGTYPE) {
1612 objectarray[it->slot]=tagptr;
1614 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1615 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1617 } else if (it->numtags>0) {
1618 /* Use tags to locate appropriate objects */
1619 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1620 struct ___Object___ *objptr=tag->flagptr;
1621 if (objptr->type!=OBJECTARRAYTYPE) {
1623 failed[it->slot]=it->failedstate;
1624 objectarray[it->slot]=objptr;
1625 if (it->failedstate==0) {
1633 objectarray[it->slot]=objptr;
1636 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1638 failed[it->slot]=it->failedstate;
1639 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1640 if (it->failedstate==0) {
1647 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1651 /* Iterate object */
1652 void * tmpp = (void *) Objkey(&it->it);
1653 objectarray[it->slot]=tmpp;
1655 failed[it->slot]=it->failedstate;
1656 if (it->failedstate==0) {