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 ctable * forward;
29 struct ctable * 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;
70 for(i=1; i<argc; i++) {
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);
320 int oldflag=((int *)ptr)[1];
321 int flag=ormask|oldflag;
327 bool 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);
346 int oldflag=((int *)ptr)[1];
347 int flag=ormask|oldflag;
349 if (flag==oldflag) /* Don't do anything */
358 void flagorandinit(void * ptr, int ormask, int andmask) {
359 int oldflag=((int *)ptr)[1];
360 int flag=ormask|oldflag;
365 void flagbody(struct ___Object___ *ptr, int flag) {
366 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
369 /*Remove object from all queues */
370 while(flagptr!=NULL) {
371 struct parameterwrapper *next;
374 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
375 ObjectHashremove(flagptr->objectset, (int)ptr);
376 if (enterflags!=NULL)
382 void enqueueObject(void *vptr) {
383 struct ___Object___ *ptr = (struct ___Object___ *)vptr;
386 struct QueueItem *tmpptr;
387 struct parameterwrapper * parameter=objectqueues[ptr->type];
389 struct parameterwrapper * prevptr=NULL;
390 struct ___Object___ *tagptr=ptr->___tags___;
392 /* Outer loop iterates through all parameter queues an object of
393 this type could be in. */
395 while(parameter!=NULL) {
397 if (parameter->numbertags>0) {
399 goto nextloop; //that means the object has no tag but that param needs tag
400 else if(tagptr->type==TAGTYPE) { //one tag
401 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
402 for(i=0; i<parameter->numbertags; i++) {
403 //slotid is parameter->tagarray[2*i];
404 int tagid=parameter->tagarray[2*i+1];
405 if (tagid!=tagptr->flag)
406 goto nextloop; /*We don't have this tag */
408 } else { //multiple tags
409 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
410 for(i=0; i<parameter->numbertags; i++) {
411 //slotid is parameter->tagarray[2*i];
412 int tagid=parameter->tagarray[2*i+1];
414 for(j=0; j<ao->___cachedCode___; j++) {
415 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
426 for(i=0; i<parameter->numberofterms; i++) {
427 int andmask=parameter->intarray[i*2];
428 int checkmask=parameter->intarray[i*2+1];
429 if ((ptr->flag&andmask)==checkmask) {
430 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
436 parameter=parameter->next;
438 ptr->flagptr=prevptr;
444 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
446 struct ___Object___ * tagptr = currobj->___tags___;
447 if(fswrapper->numtags>0) {
449 return 0; //that means the object has no tag but that param
451 else if(tagptr->type==TAGTYPE) { //one tag
452 if(fswrapper->numtags!=1)
453 return 0; //we don't have the right number of tags
454 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
455 if (fswrapper->tags[0]!=tagptr->flag)
457 } else { //multiple tags
458 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
462 if(ao->___length___!=fswrapper->numtags)
463 return 0; //we don't have the right number of tags
464 for(tag_counter=0; tag_counter<fswrapper->numtags; tag_counter++) {
465 int tagid=fswrapper->tags[tag_counter];
467 for(j=0; j<ao->___cachedCode___; j++) {
468 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
478 int getlength(int *flist, int len) {
481 for(i=0; i<len; i++) {
482 int size=flist[count];
488 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
489 int size1=getlength(flist1, len1);
490 int size2=getlength(flist2, len2);
491 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
492 memcpy(merge, flist1, size1*sizeof(int));
493 memcpy(&merge[size1], flist2, size2*sizeof(int));
497 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
501 while(i<len1||j<len2) {
502 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
504 merge[count]=flist1[i];
508 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
510 merge[count]=flist2[j];
514 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
516 merge[count]=flist1[i];
526 /* Merge flags from ftlmerge into ftl. */
527 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
532 for(i=0; i<ftl->numflags; i++) {
533 int len=ftl->flags[offset++];
535 for(j=0; j<ftlmerge->numflags; j++) {
536 int lenmerge=ftlmerge->flags[offsetmerge++];
537 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
538 offsetmerge+=lenmerge;
542 mergedlist=RUNMALLOC(sizeof(int)*length);
546 for(i=0; i<ftl->numflags; i++) {
547 int len=ftl->flags[offset++];
549 for(j=0; j<ftlmerge->numflags; j++) {
550 int lenmerge=ftlmerge->flags[offsetmerge++];
551 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
552 mergedlist[length]=size;
557 ftl->flags=mergedlist;
558 ftl->numflags*=ftlmerge->numflags;
561 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
562 struct failedtasklist *tmpptr;
563 while((*andlist)!=NULL) {
564 struct failedtasklist *searchftl=list;
565 while(searchftl!=NULL) {
566 if ((*andlist)->task==searchftl->task&&
567 (*andlist)->index==searchftl->index) {
568 mergeitems(*andlist, searchftl);
571 searchftl=searchftl->next;
573 if (searchftl==NULL) {
574 //didn't find andlist
576 *andlist=(*andlist)->next; //splice item out of list
577 RUNFREE(tmpptr->flags); //free the item
580 andlist=&((*andlist)->next); //iterate to next item
583 //free the list we're searching
586 RUNFREE(list->flags);
592 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
593 struct failedtasklist *list=NULL;
595 struct fsanalysiswrapper *fswrapper=NULL;
596 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
597 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
598 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
599 //we only match exactly here
606 for(i=0; i<fswrapper->numtaskfailures; i++) {
608 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
609 if (taskfail->task==task&&taskfail->index==index) {
611 while(start<taskfail->numoptionaltaskdescriptors) {
612 struct taskdescriptor *currtask=NULL;
613 struct failedtasklist *tmpftl;
617 int numenterflags, offset;
618 struct parameterwrapper *pw;
619 for(j=start; j<taskfail->numoptionaltaskdescriptors; j++) {
620 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
623 currindex=otd->index;
624 } else if (currtask!=otd->task||currindex!=otd->index)
626 totallength+=otd->numenterflags;
628 pw=currtask->descriptorarray[currindex]->queue;
629 enterflags=RUNMALLOC(totallength*sizeof(int));
630 numenterflags=j-start;
632 for(start; start<j; start++) {
633 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
634 enterflags[offset++]=otd->numenterflags;
635 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
636 offset+=otd->numenterflags;
638 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
640 tmpftl->task=currtask;
641 tmpftl->numflags=numenterflags;
642 tmpftl->flags=enterflags;
650 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
651 struct failedtasklist *list=NULL;
654 struct fsanalysiswrapper *fswrapper=NULL;
655 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
656 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
657 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
658 //we only match exactly here
666 while(start<fswrapper->numoptionaltaskdescriptors) {
667 struct taskdescriptor *currtask=NULL;
668 struct failedtasklist *tmpftl;
673 int numenterflags, offset;
674 struct parameterwrapper *pw;
675 for(j=start; j<fswrapper->numoptionaltaskdescriptors; j++) {
676 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
679 currindex=otd->index;
680 } else if (currtask!=otd->task||currindex!=otd->index)
682 totallength+=otd->numenterflags;
684 pw=currtask->descriptorarray[currindex]->queue;
685 enterflags=RUNMALLOC(totallength*sizeof(int));
686 numenterflags=j-start;
688 for(start; start<j; start++) {
689 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
690 enterflags[offset++]=otd->numenterflags;
691 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
692 offset+=otd->numenterflags;
694 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
696 tmpftl->task=currtask;
697 tmpftl->numflags=numenterflags;
698 tmpftl->flags=enterflags;
706 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
707 while(andlist!=NULL) {
708 struct failedtasklist *tmp=andlist;
709 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
710 struct parmaeterwrapper *next;
715 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
718 andlist=andlist->next;
723 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
724 struct classanalysiswrapper * classwrapper=NULL;
726 /*test what optionaltaskdescriptors are available, find the class
728 if (classanalysiswrapperarray[currobj->type]!=NULL) {
729 classwrapper = classanalysiswrapperarray[currobj->type];
734 /* We have a failure */
735 if (failedfses==NULL) {
736 /* Failed in normal state */
737 /*first time the method is invoked*/
739 struct fsanalysiswrapper *fswrapper=NULL;
741 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
742 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
743 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
744 //we only match exactly here
749 if(fswrapper==NULL) //nothing to do in this state
751 for(i=0; i<fswrapper->numtaskfailures; i++) {
753 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
754 if (taskfail->task==task&&taskfail->index==index) {
756 while(start<taskfail->numoptionaltaskdescriptors) {
757 struct taskdescriptor *currtask=NULL;
761 int numenterflags, offset;
762 struct parameterwrapper *pw;
763 for(j=start; j<taskfail->numoptionaltaskdescriptors; j++) {
764 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
767 currindex=otd->index;
768 } else if (currtask!=otd->task||currindex!=otd->index)
770 totallength+=otd->numenterflags; //1 is to store the lengths
772 pw=currtask->descriptorarray[currindex]->queue;
773 numenterflags=j-start;
774 enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
777 for(start; start<j; start++) {
778 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
779 enterflags[offset++]=otd->numenterflags;
780 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
781 offset+=otd->numenterflags;
784 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
790 /* Failed in failed state */
793 for(i=0; i<numfailedfses; i++) {
794 int numfses=failedfses[offset++];
796 struct failedtasklist *andlist=NULL;
797 for(j=0; j<numfses; j++) {
798 int flagstate=failedfses[offset++];
799 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
803 mergefailedlists(&andlist, currlist);
805 enqueuelist(currobj, andlist);
809 /* No failure, but we are in a failed state */
810 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
812 /*Remove object from all queues */
813 while(flagptr!=NULL) {
814 struct parameterwrapper *next;
817 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
818 ObjectHashremove(flagptr->objectset, (int)currobj);
819 if (enterflags!=NULL)
824 /* Failed in failed state */
827 for(i=0; i<currobj->numfses; i++) {
828 int numfses=currobj->fses[offset++];
830 struct failedtasklist *andlist=NULL;
831 for(j=0; j<numfses; j++) {
832 int flagstate=currobj->fses[offset++];
833 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
837 mergefailedlists(&andlist, currlist);
839 enqueuelist(currobj, andlist);
847 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
848 void * taskpointerarray[MAXTASKPARAMS];
850 int failed[MAXTASKPARAMS];
853 int numparams=parameter->task->numParameters;
854 int numiterators=parameter->task->numTotal-1;
859 struct taskdescriptor * task=parameter->task;
862 if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
863 /* The object is already here...or it with the existing item */
869 ObjectHashget(parameter->objectset, (int) ptr, &oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
870 mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
871 ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
876 //only add if truly needed
885 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL); //this add the object to parameterwrapper
890 /* Add enqueued object to parameter vector */
891 taskpointerarray[parameter->slot]=ptr;
893 failed[parameter->slot]=(enterflags!=NULL);
896 /* Reset iterators */
897 for(j=0; j<numiterators; j++) {
898 toiReset(¶meter->iterators[j]);
901 /* Find initial state */
902 for(j=0; j<numiterators; j++) {
904 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))) {
905 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
907 /* Need to backtrack */
908 toiReset(¶meter->iterators[j]);
912 /* Nothing to enqueue */
919 /* Enqueue current state */
921 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
923 tpd->numParameters=numiterators+1;
924 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
926 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
928 for(j=0; j<=numiterators; j++) {
929 tpd->parameterArray[j]=taskpointerarray[j]; //store the actual parameters
931 tpd->failed[j]=failed[j];
932 if (failed[j]!=0&&failed[j]!=1) {
938 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
939 genputtable(activetasks, tpd, tpd);
941 RUNFREE(tpd->parameterArray);
943 RUNFREE(tpd->failed);
948 /* This loop iterates to the next parameter combination */
952 for(j=numiterators-1; j<numiterators; j++) {
954 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))) {
955 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
957 /* Need to backtrack */
958 toiReset(¶meter->iterators[j]);
962 /* Nothing more to enqueue */
970 /* Handler for signals. The signals catch null pointer errors and
971 arithmatic errors. */
973 void myhandler(int sig, siginfo_t *info, void *uap) {
976 printf("sig=%d\n",sig);
979 sigemptyset(&toclear);
980 sigaddset(&toclear, sig);
981 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
982 longjmp(error_handler,1);
987 struct RuntimeHash *fdtoobject;
989 void addreadfd(int fd) {
992 FD_SET(fd, &readfds);
995 void removereadfd(int fd) {
996 FD_CLR(fd, &readfds);
997 if (maxreadfd==(fd+1)) {
999 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1011 int * fsescopy(int *src, int len) {
1015 dst=RUNMALLOC(len*sizeof(int));
1016 memcpy(dst, src, len*sizeof(int));
1021 void executetasks() {
1022 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1024 int * fsesarray[MAXTASKPARAMS];
1025 int * oldfsesarray[MAXTASKPARAMS];
1026 int numfsesarray[MAXTASKPARAMS];
1029 /* Set up signal handlers */
1030 struct sigaction sig;
1031 sig.sa_sigaction=&myhandler;
1032 sig.sa_flags=SA_SIGINFO;
1033 sigemptyset(&sig.sa_mask);
1035 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1036 sigaction(SIGBUS,&sig,0);
1037 sigaction(SIGSEGV,&sig,0);
1038 sigaction(SIGFPE,&sig,0);
1039 sigaction(SIGPIPE,&sig,0);
1044 fdtoobject=allocateRuntimeHash(100);
1046 /* Map first block of memory to protected, anonymous page */
1047 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1050 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1052 /* Check if any filedescriptors have IO pending */
1055 struct timeval timeout={0,0};
1059 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1061 /* Process ready fd's */
1063 for(fd=0; fd<maxreadfd; fd++) {
1064 if (FD_ISSET(fd, &tmpreadfds)) {
1065 /* Set ready flag on object */
1067 // printf("Setting fd %d\n",fd);
1068 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1069 if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1070 enqueueObject(objptr);
1078 /* See if there are any active tasks */
1079 if (hashsize(activetasks)>0) {
1081 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1082 genfreekey(activetasks, currtpd);
1084 /* Check if this task has failed, allow a task that contains optional objects to fire */
1085 if (gencontains(failedtasks, currtpd)) {
1086 // Free up task parameter descriptor
1087 RUNFREE(currtpd->parameterArray);
1089 RUNFREE(currtpd->failed);
1094 int numparams=currtpd->task->numParameters;
1095 int numtotal=currtpd->task->numTotal;
1097 /* Make sure that the parameters are still in the queues */
1098 for(i=0; i<numparams; i++) {
1099 void * parameter=currtpd->parameterArray[i];
1100 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1101 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1103 /* Check that object is still in queue */
1106 int UNUSED, UNUSED2;
1108 int numflags, isnonfailed;
1109 int failed=currtpd->failed[i];
1110 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1111 RUNFREE(currtpd->parameterArray);
1112 RUNFREE(currtpd->failed);
1116 if (failed&&(flags!=NULL)) {
1119 numfsesarray[i]=numflags;
1120 } else if (!failed && isnonfailed) {
1121 //Non-failed parameter
1125 RUNFREE(currtpd->parameterArray);
1126 RUNFREE(currtpd->failed);
1134 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1135 RUNFREE(currtpd->parameterArray);
1143 /* Check that object still has necessary tags */
1144 for(j=0; j<pd->numbertags; j++) {
1145 int slotid=pd->tagarray[2*j]+numparams;
1146 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1147 if (!containstag(parameter, tagd)) {
1148 RUNFREE(currtpd->parameterArray);
1150 RUNFREE(currtpd->failed);
1157 taskpointerarray[i+OFFSET]=parameter;
1160 for(; i<numtotal; i++) {
1161 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1165 /* Checkpoint the state */
1166 forward=cCreate(256, 0.4);
1167 reverse=cCreate(256, 0.4);
1168 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1170 if (x=setjmp(error_handler)) {
1174 printf("Fatal Error=%d, Recovering!\n",x);
1176 genputtable(failedtasks,currtpd,currtpd);
1177 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1180 for(counter=0; counter<currtpd->task->numParameters; counter++) {
1182 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1185 if (fsesarray[counter]!=NULL)
1186 RUNFREE(fsesarray[counter]);
1195 if (injectfailures) {
1196 if ((((double)random())/RAND_MAX)<failurechance) {
1197 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1198 longjmp(error_handler,10);
1201 /* Actually call task */
1203 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1204 taskpointerarray[1]=NULL;
1207 //get the task flags set
1208 for(i=0; i<numparams; i++) {
1209 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1210 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1211 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1215 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1216 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
1217 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1219 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
1222 for(i=0; i<numparams; i++) {
1224 if(oldfsesarray[i]!=NULL)
1225 RUNFREE(oldfsesarray[i]);
1232 // Free up task parameter descriptor
1233 RUNFREE(currtpd->parameterArray);
1235 RUNFREE(currtpd->failed);
1246 /* This function processes an objects tags */
1247 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1250 for(i=0; i<pd->numbertags; i++) {
1251 int slotid=pd->tagarray[2*i];
1252 int tagid=pd->tagarray[2*i+1];
1254 if (statusarray[slotid+numparams]==0) {
1255 parameter->iterators[*iteratorcount].istag=1;
1256 parameter->iterators[*iteratorcount].tagid=tagid;
1257 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1258 parameter->iterators[*iteratorcount].tagobjectslot=index;
1259 statusarray[slotid+numparams]=1;
1266 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1269 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1271 parameter->iterators[*iteratorcount].istag=0;
1272 parameter->iterators[*iteratorcount].slot=index;
1273 parameter->iterators[*iteratorcount].objectset=objectset;
1274 statusarray[index]=1;
1276 for(i=0; i<pd->numbertags; i++) {
1277 int slotid=pd->tagarray[2*i];
1278 int tagid=pd->tagarray[2*i+1];
1279 if (statusarray[slotid+numparams]!=0) {
1280 /* This tag has already been enqueued, use it to narrow search */
1281 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1285 parameter->iterators[*iteratorcount].numtags=tagcount;
1290 /* This function builds the iterators for a task & parameter */
1292 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1293 int statusarray[MAXTASKPARAMS];
1295 int numparams=task->numParameters;
1296 int iteratorcount=0;
1297 for(i=0; i<MAXTASKPARAMS; i++) statusarray[i]=0;
1299 statusarray[index]=1; /* Initial parameter */
1300 /* Process tags for initial iterator */
1302 processtags(task->descriptorarray[index], index, parameter, &iteratorcount, statusarray, numparams);
1306 /* Check for objects with existing tags */
1307 for(i=0; i<numparams; i++) {
1308 if (statusarray[i]==0) {
1309 struct parameterdescriptor *pd=task->descriptorarray[i];
1311 for(j=0; j<pd->numbertags; j++) {
1312 int slotid=pd->tagarray[2*j];
1313 if(statusarray[slotid+numparams]!=0) {
1314 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1315 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1322 /* Next do objects w/ unbound tags*/
1324 for(i=0; i<numparams; i++) {
1325 if (statusarray[i]==0) {
1326 struct parameterdescriptor *pd=task->descriptorarray[i];
1327 if (pd->numbertags>0) {
1328 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1329 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1335 /* Nothing with a tag enqueued */
1337 for(i=0; i<numparams; i++) {
1338 if (statusarray[i]==0) {
1339 struct parameterdescriptor *pd=task->descriptorarray[i];
1340 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1341 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1354 for(i=0; i<numtasks; i++) {
1355 struct taskdescriptor * task=taskarray[i];
1356 printf("%s\n", task->name);
1357 for(j=0; j<task->numParameters; j++) {
1358 struct parameterdescriptor *param=task->descriptorarray[j];
1359 struct parameterwrapper *parameter=param->queue;
1360 struct ObjectHash * set=parameter->objectset;
1361 struct ObjectIterator objit;
1362 printf(" Parameter %d\n", j);
1363 ObjectHashiterator(set, &objit);
1364 while(ObjhasNext(&objit)) {
1365 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1366 struct ___Object___ * tagptr=obj->___tags___;
1367 int nonfailed=Objdata4(&objit);
1368 int numflags=Objdata3(&objit);
1369 int flags=Objdata2(&objit);
1371 printf(" Contains %lx\n", obj);
1372 printf(" flag=%d\n", obj->flag);
1374 printf(" flagsstored=%x\n",flags);
1375 printf(" numflags=%d\n", numflags);
1376 printf(" nonfailed=%d\n",nonfailed);
1379 } else if (tagptr->type==TAGTYPE) {
1380 printf(" tag=%lx\n",tagptr);
1383 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1384 for(; tagindex<ao->___cachedCode___; tagindex++) {
1385 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1394 /* This function processes the task information to create queues for
1395 each parameter type. */
1397 void processtasks() {
1399 for(i=0; i<numtasks; i++) {
1400 struct taskdescriptor * task=taskarray[i];
1403 for(j=0; j<task->numParameters; j++) {
1404 struct parameterdescriptor *param=task->descriptorarray[j];
1405 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1406 struct parameterwrapper ** ptr=&objectqueues[param->type];
1408 param->queue=parameter;
1409 parameter->objectset=allocateObjectHash(10);
1410 parameter->numberofterms=param->numberterms;
1411 parameter->intarray=param->intarray;
1412 parameter->numbertags=param->numbertags;
1413 parameter->tagarray=param->tagarray;
1414 parameter->task=task;
1416 /* Link new queue in */
1418 ptr=&((*ptr)->next);
1422 /* Build iterators for parameters */
1423 for(j=0; j<task->numParameters; j++) {
1424 struct parameterdescriptor *param=task->descriptorarray[j];
1425 struct parameterwrapper *parameter=param->queue;
1426 builditerators(task, j, parameter);
1431 void toiReset(struct tagobjectiterator * it) {
1434 } else if (it->numtags>0) {
1440 ObjectHashiterator(it->objectset, &it->it);
1447 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1450 /* Get object with tags */
1451 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1452 struct ___Object___ *tagptr=obj->___tags___;
1453 if (tagptr->type==TAGTYPE) {
1454 if ((it->tagobjindex==0)&& /* First object */
1455 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1460 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1461 int tagindex=it->tagobjindex;
1462 for(; tagindex<ao->___cachedCode___; tagindex++) {
1463 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1464 if (td->flag==it->tagid) {
1465 it->tagobjindex=tagindex; /* Found right type of tag */
1471 } else if (it->numtags>0) {
1472 /* Use tags to locate appropriate objects */
1473 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1474 struct ___Object___ *objptr=tag->flagptr;
1476 if (objptr->type!=OBJECTARRAYTYPE) {
1477 if (it->tagobjindex>0)
1479 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1481 for(i=1; i<it->numtags; i++) {
1482 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1483 if (!containstag(objptr,tag2))
1487 if (it->failedstate==1) {
1488 int UNUSED, UNUSED2;
1491 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1500 int UNUSED, UNUSED2;
1503 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1512 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1516 if (it->failedstate==1) {
1517 int UNUSED, UNUSED2;
1520 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1521 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1530 for(tagindex=it->tagobjindex; tagindex<ao->___cachedCode___; tagindex++) {
1531 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1532 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1534 for(i=1; i<it->numtags; i++) {
1535 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1536 if (!containstag(objptr,tag2))
1541 int UNUSED, UNUSED2;
1542 int flags, isnonfailed;
1543 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1544 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1550 it->tagobjindex=tagindex;
1555 it->tagobjindex=tagindex;
1560 if (it->failedstate==1) {
1561 if (Objdata2(&it->it))
1568 if (ObjhasNext(&it->it)) {
1569 if (!Objdata4(&it->it)) {
1577 return ObjhasNext(&it->it);
1582 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1584 struct ___Object___ * objptr=tag->flagptr;
1585 if (objptr->type==OBJECTARRAYTYPE) {
1586 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1587 for(j=0; j<ao->___cachedCode___; j++) {
1588 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1596 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1597 /* hasNext has all of the intelligence */
1600 /* Get object with tags */
1601 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1602 struct ___Object___ *tagptr=obj->___tags___;
1604 failed[it->slot]=0; //have to set it to something
1606 if (tagptr->type==TAGTYPE) {
1608 objectarray[it->slot]=tagptr;
1610 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1611 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1613 } else if (it->numtags>0) {
1614 /* Use tags to locate appropriate objects */
1615 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1616 struct ___Object___ *objptr=tag->flagptr;
1617 if (objptr->type!=OBJECTARRAYTYPE) {
1619 failed[it->slot]=it->failedstate;
1620 objectarray[it->slot]=objptr;
1621 if (it->failedstate==0) {
1629 objectarray[it->slot]=objptr;
1632 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1634 failed[it->slot]=it->failedstate;
1635 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1636 if (it->failedstate==0) {
1643 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1647 /* Iterate object */
1648 void * tmpp = (void *) Objkey(&it->it);
1649 objectarray[it->slot]=tmpp;
1651 failed[it->slot]=it->failedstate;
1652 if (it->failedstate==0) {