3 #include "structdefs.h"
5 #include "checkpoint.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
15 extern int injectfailures;
16 extern float failurechance;
22 #include "instrument.h"
25 struct genhashtable * activetasks;
26 struct parameterwrapper * objectqueues[NUMCLASSES];
27 struct genhashtable * failedtasks;
28 struct taskparamdescriptor * currtpd;
29 struct ctable * forward;
30 struct ctable * reverse;
32 int main(int argc, char **argv) {
34 GC_init(); // Initialize the garbage collector
40 initializeexithandler();
41 /* Create table for failed tasks */
42 failedtasks=genallocatehashtable((unsigned int(*) (void *)) &hashCodetpd,
43 (int(*) (void *,void *)) &comparetpd);
44 /* Create queue of active tasks */
45 activetasks=genallocatehashtable((unsigned int(*) (void *)) &hashCodetpd,
46 (int(*) (void *,void *)) &comparetpd);
48 /* Process task information */
51 /* Create startup object */
52 createstartupobject(argc, argv);
54 /* Start executing the tasks */
58 void createstartupobject(int argc, char ** argv) {
61 /* Allocate startup object */
63 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
64 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
66 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
67 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
69 /* Build array of strings */
70 startupobject->___parameters___=stringarray;
71 for(i=1; i<argc; i++) {
72 int length=strlen(argv[i]);
74 struct ___String___ *newstring=NewString(NULL, argv[i],length);
76 struct ___String___ *newstring=NewString(argv[i],length);
78 ((void **)(((char *)&stringarray->___length___)+sizeof(int)))[i-1]=newstring;
81 /* Set initialized flag for startup object */
82 flagorand(startupobject,1,0xFFFFFFFF);
83 enqueueObject(startupobject);
86 int hashCodetpd(struct taskparamdescriptor *ftd) {
87 int hash=(int)ftd->task;
89 for(i=0; i<ftd->numParameters; i++) {
90 hash^=(int)ftd->parameterArray[i];
95 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
97 if (ftd1->task!=ftd2->task)
99 for(i=0; i<ftd1->numParameters; i++)
100 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
103 for(i=0; i<ftd1->numParameters; i++) {
104 if(ftd1->failed[i]!=ftd2->failed[i])
111 /* This function sets a tag. */
113 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
115 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
117 struct ___Object___ * tagptr=obj->___tags___;
119 obj->___tags___=(struct ___Object___ *)tagd;
121 /* Have to check if it is already set */
122 if (tagptr->type==TAGTYPE) {
123 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
127 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
128 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
129 obj=(struct ___Object___ *)ptrarray[2];
130 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
131 td=(struct ___TagDescriptor___ *) obj->___tags___;
133 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
135 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
136 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
137 obj->___tags___=(struct ___Object___ *) ao;
138 ao->___cachedCode___=2;
142 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
143 for(i=0; i<ao->___cachedCode___; i++) {
144 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
148 if (ao->___cachedCode___<ao->___length___) {
149 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
150 ao->___cachedCode___++;
153 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
154 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
155 obj=(struct ___Object___ *)ptrarray[2];
156 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
157 ao=(struct ArrayObject *)obj->___tags___;
159 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
161 aonew->___cachedCode___=ao->___length___+1;
162 for(i=0; i<ao->___length___; i++) {
163 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
165 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
171 struct ___Object___ * tagset=tagd->flagptr;
174 } else if (tagset->type!=OBJECTARRAYTYPE) {
176 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
177 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
178 obj=(struct ___Object___ *)ptrarray[2];
179 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
181 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
183 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
184 ARRAYSET(ao, struct ___Object___ *, 1, obj);
185 ao->___cachedCode___=2;
186 tagd->flagptr=(struct ___Object___ *)ao;
188 struct ArrayObject *ao=(struct ArrayObject *) tagset;
189 if (ao->___cachedCode___<ao->___length___) {
190 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
194 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
195 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
196 obj=(struct ___Object___ *)ptrarray[2];
197 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
198 ao=(struct ArrayObject *)tagd->flagptr;
200 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
202 aonew->___cachedCode___=ao->___cachedCode___+1;
203 for(i=0; i<ao->___length___; i++) {
204 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
206 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
207 tagd->flagptr=(struct ___Object___ *) aonew;
213 /* This function clears a tag. */
215 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
217 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
219 /* We'll assume that tag is alway there.
220 Need to statically check for this of course. */
221 struct ___Object___ * tagptr=obj->___tags___;
223 if (tagptr->type==TAGTYPE) {
224 if ((struct ___TagDescriptor___ *)tagptr==tagd)
225 obj->___tags___=NULL;
227 printf("ERROR 1 in tagclear\n");
229 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
231 for(i=0; i<ao->___cachedCode___; i++) {
232 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
234 ao->___cachedCode___--;
235 if (i<ao->___cachedCode___)
236 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
237 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
238 if (ao->___cachedCode___==0)
239 obj->___tags___=NULL;
243 printf("ERROR 2 in tagclear\n");
247 struct ___Object___ *tagset=tagd->flagptr;
248 if (tagset->type!=OBJECTARRAYTYPE) {
252 printf("ERROR 3 in tagclear\n");
254 struct ArrayObject *ao=(struct ArrayObject *) tagset;
256 for(i=0; i<ao->___cachedCode___; i++) {
257 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
259 ao->___cachedCode___--;
260 if (i<ao->___cachedCode___)
261 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
262 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
263 if (ao->___cachedCode___==0)
268 printf("ERROR 4 in tagclear\n");
275 /* This function allocates a new tag. */
277 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
278 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
280 struct ___TagDescriptor___ * allocate_tag(int index) {
281 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
290 /* This function updates the flag for object ptr. It or's the flag
291 with the or mask and and's it with the andmask. */
293 void flagbody(struct ___Object___ *ptr, int flag);
295 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
298 int flagcomp(const int *val1, const int *val2) {
299 return (*val1)-(*val2);
302 void flagorand(void * ptr, int ormask, int andmask) {
304 struct ___Object___ * obj = (struct ___Object___ *)ptr;
305 if(obj->numfses) { /*store the information about fses*/
306 int flag, i, j,counter, offset=0;
307 for(i=0; i<obj->numfses; i++) {
309 counter=obj->fses[offset++];
311 for(j=0; j<counter; j++) {
312 flag=obj->fses[offset];
313 obj->fses[offset++]=(flag|ormask)&andmask;
315 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int(*) (const void *, const void *)) &flagcomp);
317 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);
347 int oldflag=((int *)ptr)[1];
348 int flag=ormask|oldflag;
350 if (flag==oldflag) /* Don't do anything */
359 void flagorandinit(void * ptr, int ormask, int andmask) {
360 int oldflag=((int *)ptr)[1];
361 int flag=ormask|oldflag;
366 void flagbody(struct ___Object___ *ptr, int flag) {
367 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
370 /*Remove object from all queues */
371 while(flagptr!=NULL) {
372 struct parameterwrapper *next;
375 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
376 ObjectHashremove(flagptr->objectset, (int)ptr);
377 if (enterflags!=NULL)
383 void enqueueObject(void *vptr) {
384 struct ___Object___ *ptr = (struct ___Object___ *)vptr;
387 struct QueueItem *tmpptr;
388 struct parameterwrapper * parameter=objectqueues[ptr->type];
390 struct parameterwrapper * prevptr=NULL;
391 struct ___Object___ *tagptr=ptr->___tags___;
393 /* Outer loop iterates through all parameter queues an object of
394 this type could be in. */
396 while(parameter!=NULL) {
398 if (parameter->numbertags>0) {
400 goto nextloop; //that means the object has no tag but that param needs tag
401 else if(tagptr->type==TAGTYPE) { //one tag
402 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
403 for(i=0; i<parameter->numbertags; i++) {
404 //slotid is parameter->tagarray[2*i];
405 int tagid=parameter->tagarray[2*i+1];
406 if (tagid!=tagptr->flag)
407 goto nextloop; /*We don't have this tag */
409 } else { //multiple tags
410 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
411 for(i=0; i<parameter->numbertags; i++) {
412 //slotid is parameter->tagarray[2*i];
413 int tagid=parameter->tagarray[2*i+1];
415 for(j=0; j<ao->___cachedCode___; j++) {
416 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
427 for(i=0; i<parameter->numberofterms; i++) {
428 int andmask=parameter->intarray[i*2];
429 int checkmask=parameter->intarray[i*2+1];
430 if ((ptr->flag&andmask)==checkmask) {
431 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
437 parameter=parameter->next;
439 ptr->flagptr=prevptr;
445 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
447 struct ___Object___ * tagptr = currobj->___tags___;
448 if(fswrapper->numtags>0) {
450 return 0; //that means the object has no tag but that param
452 else if(tagptr->type==TAGTYPE) { //one tag
453 if(fswrapper->numtags!=1)
454 return 0; //we don't have the right number of tags
455 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
456 if (fswrapper->tags[0]!=tagptr->flag)
458 } else { //multiple tags
459 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
463 if(ao->___length___!=fswrapper->numtags)
464 return 0; //we don't have the right number of tags
465 for(tag_counter=0; tag_counter<fswrapper->numtags; tag_counter++) {
466 int tagid=fswrapper->tags[tag_counter];
468 for(j=0; j<ao->___cachedCode___; j++) {
469 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
479 int getlength(int *flist, int len) {
482 for(i=0; i<len; i++) {
483 int size=flist[count];
489 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
490 int size1=getlength(flist1, len1);
491 int size2=getlength(flist2, len2);
492 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
493 memcpy(merge, flist1, size1*sizeof(int));
494 memcpy(&merge[size1], flist2, size2*sizeof(int));
498 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
502 while(i<len1||j<len2) {
503 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
505 merge[count]=flist1[i];
509 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
511 merge[count]=flist2[j];
515 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
517 merge[count]=flist1[i];
527 /* Merge flags from ftlmerge into ftl. */
528 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
533 for(i=0; i<ftl->numflags; i++) {
534 int len=ftl->flags[offset++];
536 for(j=0; j<ftlmerge->numflags; j++) {
537 int lenmerge=ftlmerge->flags[offsetmerge++];
538 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
539 offsetmerge+=lenmerge;
543 mergedlist=RUNMALLOC(sizeof(int)*length);
547 for(i=0; i<ftl->numflags; i++) {
548 int len=ftl->flags[offset++];
550 for(j=0; j<ftlmerge->numflags; j++) {
551 int lenmerge=ftlmerge->flags[offsetmerge++];
552 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
553 mergedlist[length]=size;
558 ftl->flags=mergedlist;
559 ftl->numflags*=ftlmerge->numflags;
562 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
563 struct failedtasklist *tmpptr;
564 while((*andlist)!=NULL) {
565 struct failedtasklist *searchftl=list;
566 while(searchftl!=NULL) {
567 if ((*andlist)->task==searchftl->task&&
568 (*andlist)->index==searchftl->index) {
569 mergeitems(*andlist, searchftl);
572 searchftl=searchftl->next;
574 if (searchftl==NULL) {
575 //didn't find andlist
577 *andlist=(*andlist)->next; //splice item out of list
578 RUNFREE(tmpptr->flags); //free the item
581 andlist=&((*andlist)->next); //iterate to next item
584 //free the list we're searching
587 RUNFREE(list->flags);
593 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
594 struct failedtasklist *list=NULL;
596 struct fsanalysiswrapper *fswrapper=NULL;
597 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
598 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
599 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
600 //we only match exactly here
607 for(i=0; i<fswrapper->numtaskfailures; i++) {
609 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
610 if (taskfail->task==task&&taskfail->index==index) {
612 while(start<taskfail->numoptionaltaskdescriptors) {
613 struct taskdescriptor *currtask=NULL;
614 struct failedtasklist *tmpftl;
618 int numenterflags, offset;
619 struct parameterwrapper *pw;
620 for(j=start; j<taskfail->numoptionaltaskdescriptors; j++) {
621 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
624 currindex=otd->index;
625 } else if (currtask!=otd->task||currindex!=otd->index)
627 totallength+=otd->numenterflags;
629 pw=currtask->descriptorarray[currindex]->queue;
630 enterflags=RUNMALLOC(totallength*sizeof(int));
631 numenterflags=j-start;
633 for(start; start<j; start++) {
634 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
635 enterflags[offset++]=otd->numenterflags;
636 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
637 offset+=otd->numenterflags;
639 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
641 tmpftl->task=currtask;
642 tmpftl->numflags=numenterflags;
643 tmpftl->flags=enterflags;
651 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
652 struct failedtasklist *list=NULL;
655 struct fsanalysiswrapper *fswrapper=NULL;
656 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
657 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
658 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
659 //we only match exactly here
667 while(start<fswrapper->numoptionaltaskdescriptors) {
668 struct taskdescriptor *currtask=NULL;
669 struct failedtasklist *tmpftl;
674 int numenterflags, offset;
675 struct parameterwrapper *pw;
676 for(j=start; j<fswrapper->numoptionaltaskdescriptors; j++) {
677 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
680 currindex=otd->index;
681 } else if (currtask!=otd->task||currindex!=otd->index)
683 totallength+=otd->numenterflags;
685 pw=currtask->descriptorarray[currindex]->queue;
686 enterflags=RUNMALLOC(totallength*sizeof(int));
687 numenterflags=j-start;
689 for(start; start<j; start++) {
690 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
691 enterflags[offset++]=otd->numenterflags;
692 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
693 offset+=otd->numenterflags;
695 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
697 tmpftl->task=currtask;
698 tmpftl->numflags=numenterflags;
699 tmpftl->flags=enterflags;
707 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
708 while(andlist!=NULL) {
709 struct failedtasklist *tmp=andlist;
710 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
711 struct parmaeterwrapper *next;
716 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
719 andlist=andlist->next;
724 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
725 struct classanalysiswrapper * classwrapper=NULL;
727 /*test what optionaltaskdescriptors are available, find the class
729 if (classanalysiswrapperarray[currobj->type]!=NULL) {
730 classwrapper = classanalysiswrapperarray[currobj->type];
735 /* We have a failure */
736 if (failedfses==NULL) {
737 /* Failed in normal state */
738 /*first time the method is invoked*/
740 struct fsanalysiswrapper *fswrapper=NULL;
742 for(h=0; h<classwrapper->numfsanalysiswrappers; h++) {
743 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
744 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
745 //we only match exactly here
750 if(fswrapper==NULL) //nothing to do in this state
752 for(i=0; i<fswrapper->numtaskfailures; i++) {
754 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
755 if (taskfail->task==task&&taskfail->index==index) {
757 while(start<taskfail->numoptionaltaskdescriptors) {
758 struct taskdescriptor *currtask=NULL;
762 int numenterflags, offset;
763 struct parameterwrapper *pw;
764 for(j=start; j<taskfail->numoptionaltaskdescriptors; j++) {
765 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
768 currindex=otd->index;
769 } else if (currtask!=otd->task||currindex!=otd->index)
771 totallength+=otd->numenterflags; //1 is to store the lengths
773 pw=currtask->descriptorarray[currindex]->queue;
774 numenterflags=j-start;
775 enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
778 for(start; start<j; start++) {
779 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
780 enterflags[offset++]=otd->numenterflags;
781 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
782 offset+=otd->numenterflags;
785 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
791 /* Failed in failed state */
794 for(i=0; i<numfailedfses; i++) {
795 int numfses=failedfses[offset++];
797 struct failedtasklist *andlist=NULL;
798 for(j=0; j<numfses; j++) {
799 int flagstate=failedfses[offset++];
800 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
804 mergefailedlists(&andlist, currlist);
806 enqueuelist(currobj, andlist);
810 /* No failure, but we are in a failed state */
811 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
813 /*Remove object from all queues */
814 while(flagptr!=NULL) {
815 struct parameterwrapper *next;
818 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
819 ObjectHashremove(flagptr->objectset, (int)currobj);
820 if (enterflags!=NULL)
825 /* Failed in failed state */
828 for(i=0; i<currobj->numfses; i++) {
829 int numfses=currobj->fses[offset++];
831 struct failedtasklist *andlist=NULL;
832 for(j=0; j<numfses; j++) {
833 int flagstate=currobj->fses[offset++];
834 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
838 mergefailedlists(&andlist, currlist);
840 enqueuelist(currobj, andlist);
848 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
849 void * taskpointerarray[MAXTASKPARAMS];
851 int failed[MAXTASKPARAMS];
854 int numparams=parameter->task->numParameters;
855 int numiterators=parameter->task->numTotal-1;
860 struct taskdescriptor * task=parameter->task;
863 if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
864 /* The object is already here...or it with the existing item */
870 ObjectHashget(parameter->objectset, (int) ptr, &oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
871 mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
872 ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
877 //only add if truly needed
886 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL); //this add the object to parameterwrapper
891 /* Add enqueued object to parameter vector */
892 taskpointerarray[parameter->slot]=ptr;
894 failed[parameter->slot]=(enterflags!=NULL);
897 /* Reset iterators */
898 for(j=0; j<numiterators; j++) {
899 toiReset(¶meter->iterators[j]);
902 /* Find initial state */
903 for(j=0; j<numiterators; j++) {
905 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))) {
906 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
908 /* Need to backtrack */
909 toiReset(¶meter->iterators[j]);
913 /* Nothing to enqueue */
920 /* Enqueue current state */
922 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
924 tpd->numParameters=numiterators+1;
925 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
927 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
929 for(j=0; j<=numiterators; j++) {
930 tpd->parameterArray[j]=taskpointerarray[j]; //store the actual parameters
932 tpd->failed[j]=failed[j];
933 if (failed[j]!=0&&failed[j]!=1) {
939 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
940 genputtable(activetasks, tpd, tpd);
942 RUNFREE(tpd->parameterArray);
944 RUNFREE(tpd->failed);
949 /* This loop iterates to the next parameter combination */
953 for(j=numiterators-1; j<numiterators; j++) {
955 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed))) {
956 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
958 /* Need to backtrack */
959 toiReset(¶meter->iterators[j]);
963 /* Nothing more to enqueue */
971 /* Handler for signals. The signals catch null pointer errors and
972 arithmatic errors. */
974 void myhandler(int sig, siginfo_t *info, void *uap) {
977 printf("sig=%d\n",sig);
980 sigemptyset(&toclear);
981 sigaddset(&toclear, sig);
982 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
983 longjmp(error_handler,1);
988 struct RuntimeHash *fdtoobject;
990 void addreadfd(int fd) {
993 FD_SET(fd, &readfds);
996 void removereadfd(int fd) {
997 FD_CLR(fd, &readfds);
998 if (maxreadfd==(fd+1)) {
1000 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1012 int * fsescopy(int *src, int len) {
1016 dst=RUNMALLOC(len*sizeof(int));
1017 memcpy(dst, src, len*sizeof(int));
1022 void executetasks() {
1023 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1025 int * fsesarray[MAXTASKPARAMS];
1026 int * oldfsesarray[MAXTASKPARAMS];
1027 int numfsesarray[MAXTASKPARAMS];
1030 /* Set up signal handlers */
1031 struct sigaction sig;
1032 sig.sa_sigaction=&myhandler;
1033 sig.sa_flags=SA_SIGINFO;
1034 sigemptyset(&sig.sa_mask);
1036 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1038 sigaction(SIGBUS,&sig,0);
1039 sigaction(SIGSEGV,&sig,0);
1040 sigaction(SIGFPE,&sig,0);
1041 sigaction(SIGPIPE,&sig,0);
1047 fdtoobject=allocateRuntimeHash(100);
1049 /* Map first block of memory to protected, anonymous page */
1050 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1053 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1055 /* Check if any filedescriptors have IO pending */
1058 struct timeval timeout={0,0};
1062 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1064 /* Process ready fd's */
1066 for(fd=0; fd<maxreadfd; fd++) {
1067 if (FD_ISSET(fd, &tmpreadfds)) {
1068 /* Set ready flag on object */
1070 // printf("Setting fd %d\n",fd);
1071 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1072 if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1073 enqueueObject(objptr);
1081 /* See if there are any active tasks */
1082 if (hashsize(activetasks)>0) {
1084 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1085 genfreekey(activetasks, currtpd);
1087 /* Check if this task has failed, allow a task that contains optional objects to fire */
1088 if (gencontains(failedtasks, currtpd)) {
1089 // Free up task parameter descriptor
1090 RUNFREE(currtpd->parameterArray);
1092 RUNFREE(currtpd->failed);
1097 int numparams=currtpd->task->numParameters;
1098 int numtotal=currtpd->task->numTotal;
1100 /* Make sure that the parameters are still in the queues */
1101 for(i=0; i<numparams; i++) {
1102 void * parameter=currtpd->parameterArray[i];
1103 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1104 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1106 /* Check that object is still in queue */
1109 int UNUSED, UNUSED2;
1111 int numflags, isnonfailed;
1112 int failed=currtpd->failed[i];
1113 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1114 RUNFREE(currtpd->parameterArray);
1115 RUNFREE(currtpd->failed);
1119 if (failed&&(flags!=NULL)) {
1122 numfsesarray[i]=numflags;
1123 } else if (!failed && isnonfailed) {
1124 //Non-failed parameter
1128 RUNFREE(currtpd->parameterArray);
1129 RUNFREE(currtpd->failed);
1137 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1138 RUNFREE(currtpd->parameterArray);
1146 /* Check that object still has necessary tags */
1147 for(j=0; j<pd->numbertags; j++) {
1148 int slotid=pd->tagarray[2*j]+numparams;
1149 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1150 if (!containstag(parameter, tagd)) {
1151 RUNFREE(currtpd->parameterArray);
1153 RUNFREE(currtpd->failed);
1160 taskpointerarray[i+OFFSET]=parameter;
1163 for(; i<numtotal; i++) {
1164 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1168 /* Checkpoint the state */
1169 forward=cCreate(256, 0.4);
1170 reverse=cCreate(256, 0.4);
1171 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1173 if (x=setjmp(error_handler)) {
1177 printf("Fatal Error=%d, Recovering!\n",x);
1179 genputtable(failedtasks,currtpd,currtpd);
1180 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1183 for(counter=0; counter<currtpd->task->numParameters; counter++) {
1185 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1188 if (fsesarray[counter]!=NULL)
1189 RUNFREE(fsesarray[counter]);
1198 if (injectfailures) {
1199 if ((((double)random())/RAND_MAX)<failurechance) {
1200 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1201 longjmp(error_handler,10);
1204 /* Actually call task */
1206 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1207 taskpointerarray[1]=NULL;
1210 //get the task flags set
1211 for(i=0; i<numparams; i++) {
1212 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1213 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1214 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1218 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1219 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
1220 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1222 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
1225 for(i=0; i<numparams; i++) {
1227 if(oldfsesarray[i]!=NULL)
1228 RUNFREE(oldfsesarray[i]);
1235 // Free up task parameter descriptor
1236 RUNFREE(currtpd->parameterArray);
1238 RUNFREE(currtpd->failed);
1249 /* This function processes an objects tags */
1250 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1253 for(i=0; i<pd->numbertags; i++) {
1254 int slotid=pd->tagarray[2*i];
1255 int tagid=pd->tagarray[2*i+1];
1257 if (statusarray[slotid+numparams]==0) {
1258 parameter->iterators[*iteratorcount].istag=1;
1259 parameter->iterators[*iteratorcount].tagid=tagid;
1260 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1261 parameter->iterators[*iteratorcount].tagobjectslot=index;
1262 statusarray[slotid+numparams]=1;
1269 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1272 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1274 parameter->iterators[*iteratorcount].istag=0;
1275 parameter->iterators[*iteratorcount].slot=index;
1276 parameter->iterators[*iteratorcount].objectset=objectset;
1277 statusarray[index]=1;
1279 for(i=0; i<pd->numbertags; i++) {
1280 int slotid=pd->tagarray[2*i];
1281 int tagid=pd->tagarray[2*i+1];
1282 if (statusarray[slotid+numparams]!=0) {
1283 /* This tag has already been enqueued, use it to narrow search */
1284 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1288 parameter->iterators[*iteratorcount].numtags=tagcount;
1293 /* This function builds the iterators for a task & parameter */
1295 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1296 int statusarray[MAXTASKPARAMS];
1298 int numparams=task->numParameters;
1299 int iteratorcount=0;
1300 for(i=0; i<MAXTASKPARAMS; i++) statusarray[i]=0;
1302 statusarray[index]=1; /* Initial parameter */
1303 /* Process tags for initial iterator */
1305 processtags(task->descriptorarray[index], index, parameter, &iteratorcount, statusarray, numparams);
1309 /* Check for objects with existing tags */
1310 for(i=0; i<numparams; i++) {
1311 if (statusarray[i]==0) {
1312 struct parameterdescriptor *pd=task->descriptorarray[i];
1314 for(j=0; j<pd->numbertags; j++) {
1315 int slotid=pd->tagarray[2*j];
1316 if(statusarray[slotid+numparams]!=0) {
1317 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1318 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1325 /* Next do objects w/ unbound tags*/
1327 for(i=0; i<numparams; i++) {
1328 if (statusarray[i]==0) {
1329 struct parameterdescriptor *pd=task->descriptorarray[i];
1330 if (pd->numbertags>0) {
1331 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1332 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1338 /* Nothing with a tag enqueued */
1340 for(i=0; i<numparams; i++) {
1341 if (statusarray[i]==0) {
1342 struct parameterdescriptor *pd=task->descriptorarray[i];
1343 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1344 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1357 for(i=0; i<numtasks; i++) {
1358 struct taskdescriptor * task=taskarray[i];
1359 printf("%s\n", task->name);
1360 for(j=0; j<task->numParameters; j++) {
1361 struct parameterdescriptor *param=task->descriptorarray[j];
1362 struct parameterwrapper *parameter=param->queue;
1363 struct ObjectHash * set=parameter->objectset;
1364 struct ObjectIterator objit;
1365 printf(" Parameter %d\n", j);
1366 ObjectHashiterator(set, &objit);
1367 while(ObjhasNext(&objit)) {
1368 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1369 struct ___Object___ * tagptr=obj->___tags___;
1370 int nonfailed=Objdata4(&objit);
1371 int numflags=Objdata3(&objit);
1372 int flags=Objdata2(&objit);
1374 printf(" Contains %lx\n", obj);
1375 printf(" flag=%d\n", obj->flag);
1377 printf(" flagsstored=%x\n",flags);
1378 printf(" numflags=%d\n", numflags);
1379 printf(" nonfailed=%d\n",nonfailed);
1382 } else if (tagptr->type==TAGTYPE) {
1383 printf(" tag=%lx\n",tagptr);
1386 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1387 for(; tagindex<ao->___cachedCode___; tagindex++) {
1388 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1397 /* This function processes the task information to create queues for
1398 each parameter type. */
1400 void processtasks() {
1402 for(i=0; i<numtasks; i++) {
1403 struct taskdescriptor * task=taskarray[i];
1406 for(j=0; j<task->numParameters; j++) {
1407 struct parameterdescriptor *param=task->descriptorarray[j];
1408 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1409 struct parameterwrapper ** ptr=&objectqueues[param->type];
1411 param->queue=parameter;
1412 parameter->objectset=allocateObjectHash(10);
1413 parameter->numberofterms=param->numberterms;
1414 parameter->intarray=param->intarray;
1415 parameter->numbertags=param->numbertags;
1416 parameter->tagarray=param->tagarray;
1417 parameter->task=task;
1419 /* Link new queue in */
1421 ptr=&((*ptr)->next);
1425 /* Build iterators for parameters */
1426 for(j=0; j<task->numParameters; j++) {
1427 struct parameterdescriptor *param=task->descriptorarray[j];
1428 struct parameterwrapper *parameter=param->queue;
1429 builditerators(task, j, parameter);
1434 void toiReset(struct tagobjectiterator * it) {
1437 } else if (it->numtags>0) {
1443 ObjectHashiterator(it->objectset, &it->it);
1450 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1453 /* Get object with tags */
1454 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1455 struct ___Object___ *tagptr=obj->___tags___;
1456 if (tagptr->type==TAGTYPE) {
1457 if ((it->tagobjindex==0)&& /* First object */
1458 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1463 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1464 int tagindex=it->tagobjindex;
1465 for(; tagindex<ao->___cachedCode___; tagindex++) {
1466 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1467 if (td->flag==it->tagid) {
1468 it->tagobjindex=tagindex; /* Found right type of tag */
1474 } else if (it->numtags>0) {
1475 /* Use tags to locate appropriate objects */
1476 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1477 struct ___Object___ *objptr=tag->flagptr;
1479 if (objptr->type!=OBJECTARRAYTYPE) {
1480 if (it->tagobjindex>0)
1482 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1484 for(i=1; i<it->numtags; i++) {
1485 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1486 if (!containstag(objptr,tag2))
1490 if (it->failedstate==1) {
1491 int UNUSED, UNUSED2;
1494 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1503 int UNUSED, UNUSED2;
1506 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1515 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1519 if (it->failedstate==1) {
1520 int UNUSED, UNUSED2;
1523 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1524 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1533 for(tagindex=it->tagobjindex; tagindex<ao->___cachedCode___; tagindex++) {
1534 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1535 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1537 for(i=1; i<it->numtags; i++) {
1538 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1539 if (!containstag(objptr,tag2))
1544 int UNUSED, UNUSED2;
1545 int flags, isnonfailed;
1546 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1547 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1553 it->tagobjindex=tagindex;
1558 it->tagobjindex=tagindex;
1563 if (it->failedstate==1) {
1564 if (Objdata2(&it->it))
1571 if (ObjhasNext(&it->it)) {
1572 if (!Objdata4(&it->it)) {
1580 return ObjhasNext(&it->it);
1585 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1587 struct ___Object___ * objptr=tag->flagptr;
1588 if (objptr->type==OBJECTARRAYTYPE) {
1589 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1590 for(j=0; j<ao->___cachedCode___; j++) {
1591 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1599 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1600 /* hasNext has all of the intelligence */
1603 /* Get object with tags */
1604 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1605 struct ___Object___ *tagptr=obj->___tags___;
1607 failed[it->slot]=0; //have to set it to something
1609 if (tagptr->type==TAGTYPE) {
1611 objectarray[it->slot]=tagptr;
1613 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1614 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1616 } else if (it->numtags>0) {
1617 /* Use tags to locate appropriate objects */
1618 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1619 struct ___Object___ *objptr=tag->flagptr;
1620 if (objptr->type!=OBJECTARRAYTYPE) {
1622 failed[it->slot]=it->failedstate;
1623 objectarray[it->slot]=objptr;
1624 if (it->failedstate==0) {
1632 objectarray[it->slot]=objptr;
1635 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1637 failed[it->slot]=it->failedstate;
1638 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1639 if (it->failedstate==0) {
1646 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1650 /* Iterate object */
1651 void * tmpp = (void *) Objkey(&it->it);
1652 objectarray[it->slot]=tmpp;
1654 failed[it->slot]=it->failedstate;
1655 if (it->failedstate==0) {