3 #include "structdefs.h"
5 #include "checkpoint.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
15 extern int injectfailures;
16 extern float failurechance;
21 #include "instrument.h"
24 struct genhashtable * activetasks;
25 struct parameterwrapper * objectqueues[NUMCLASSES];
26 struct genhashtable * failedtasks;
27 struct taskparamdescriptor * currtpd;
28 struct RuntimeHash * forward;
29 struct RuntimeHash * reverse;
31 int main(int argc, char **argv) {
33 GC_init(); // Initialize the garbage collector
39 initializeexithandler();
40 /* Create table for failed tasks */
41 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
42 (int (*)(void *,void *)) &comparetpd);
43 /* Create queue of active tasks */
44 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
45 (int (*)(void *,void *)) &comparetpd);
47 /* Process task information */
50 /* Create startup object */
51 createstartupobject(argc, argv);
53 /* Start executing the tasks */
57 void createstartupobject(int argc, char ** argv) {
60 /* Allocate startup object */
62 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
63 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
65 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
66 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
68 /* Build array of strings */
69 startupobject->___parameters___=stringarray;
71 int length=strlen(argv[i]);
73 struct ___String___ *newstring=NewString(NULL, argv[i],length);
75 struct ___String___ *newstring=NewString(argv[i],length);
77 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
80 /* Set initialized flag for startup object */
81 flagorand(startupobject,1,0xFFFFFFFF);
84 int hashCodetpd(struct taskparamdescriptor *ftd) {
85 int hash=(int)ftd->task;
87 for(i=0;i<ftd->numParameters;i++){
88 hash^=(int)ftd->parameterArray[i];
93 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
95 if (ftd1->task!=ftd2->task)
97 for(i=0;i<ftd1->numParameters;i++)
98 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
101 for(i=0;i<ftd1->numParameters;i++) {
102 if(ftd1->failed[i]!=ftd2->failed[i])
109 /* This function sets a tag. */
111 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
113 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
115 struct ___Object___ * tagptr=obj->___tags___;
117 obj->___tags___=(struct ___Object___ *)tagd;
119 /* Have to check if it is already set */
120 if (tagptr->type==TAGTYPE) {
121 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
125 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
126 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
127 obj=(struct ___Object___ *)ptrarray[2];
128 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
129 td=(struct ___TagDescriptor___ *) obj->___tags___;
131 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
133 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
134 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
135 obj->___tags___=(struct ___Object___ *) ao;
136 ao->___cachedCode___=2;
140 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
141 for(i=0;i<ao->___cachedCode___;i++) {
142 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
146 if (ao->___cachedCode___<ao->___length___) {
147 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
148 ao->___cachedCode___++;
151 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
152 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
153 obj=(struct ___Object___ *)ptrarray[2];
154 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
155 ao=(struct ArrayObject *)obj->___tags___;
157 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
159 aonew->___cachedCode___=ao->___length___+1;
160 for(i=0;i<ao->___length___;i++) {
161 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
163 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
169 struct ___Object___ * tagset=tagd->flagptr;
172 } else if (tagset->type!=OBJECTARRAYTYPE) {
174 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
175 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
176 obj=(struct ___Object___ *)ptrarray[2];
177 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
179 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
181 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
182 ARRAYSET(ao, struct ___Object___ *, 1, obj);
183 ao->___cachedCode___=2;
184 tagd->flagptr=(struct ___Object___ *)ao;
186 struct ArrayObject *ao=(struct ArrayObject *) tagset;
187 if (ao->___cachedCode___<ao->___length___) {
188 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
192 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
193 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
194 obj=(struct ___Object___ *)ptrarray[2];
195 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
196 ao=(struct ArrayObject *)tagd->flagptr;
198 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
200 aonew->___cachedCode___=ao->___cachedCode___+1;
201 for(i=0;i<ao->___length___;i++) {
202 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
204 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
205 tagd->flagptr=(struct ___Object___ *) aonew;
211 /* This function clears a tag. */
213 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
215 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
217 /* We'll assume that tag is alway there.
218 Need to statically check for this of course. */
219 struct ___Object___ * tagptr=obj->___tags___;
221 if (tagptr->type==TAGTYPE) {
222 if ((struct ___TagDescriptor___ *)tagptr==tagd)
223 obj->___tags___=NULL;
225 printf("ERROR 1 in tagclear\n");
227 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
229 for(i=0;i<ao->___cachedCode___;i++) {
230 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
232 ao->___cachedCode___--;
233 if (i<ao->___cachedCode___)
234 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
235 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
236 if (ao->___cachedCode___==0)
237 obj->___tags___=NULL;
241 printf("ERROR 2 in tagclear\n");
245 struct ___Object___ *tagset=tagd->flagptr;
246 if (tagset->type!=OBJECTARRAYTYPE) {
250 printf("ERROR 3 in tagclear\n");
252 struct ArrayObject *ao=(struct ArrayObject *) tagset;
254 for(i=0;i<ao->___cachedCode___;i++) {
255 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
257 ao->___cachedCode___--;
258 if (i<ao->___cachedCode___)
259 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
260 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
261 if (ao->___cachedCode___==0)
266 printf("ERROR 4 in tagclear\n");
273 /* This function allocates a new tag. */
275 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
276 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
278 struct ___TagDescriptor___ * allocate_tag(int index) {
279 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
288 /* This function updates the flag for object ptr. It or's the flag
289 with the or mask and and's it with the andmask. */
291 void flagbody(struct ___Object___ *ptr, int flag);
293 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
296 int flagcomp(const int *val1, const int *val2) {
297 return (*val1)-(*val2);
300 void flagorand(void * ptr, int ormask, int andmask) {
302 struct ___Object___ * obj = (struct ___Object___ *)ptr;
303 if(obj->numfses){/*store the information about fses*/
304 int flag, i, j,counter, offset=0;
305 for(i=0;i<obj->numfses;i++) {
307 counter=obj->fses[offset++];
309 for(j=0;j<counter;j++) {
310 flag=obj->fses[offset];
311 obj->fses[offset++]=(flag|ormask)&andmask;
313 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
315 enqueueoptional(obj, 0, NULL, NULL, 0);
320 int oldflag=((int *)ptr)[1];
321 int flag=ormask|oldflag;
327 void intflagorand(void * ptr, int ormask, int andmask) {
329 struct ___Object___ * obj = (struct ___Object___ *)ptr;
330 if(obj->numfses) {/*store the information about fses*/
331 int flag, i, j,counter, offset=0;
332 for(i=0;i<obj->numfses;i++) {
334 counter=obj->fses[offset++];
336 for(j=0;j<counter;j++) {
337 flag=obj->fses[offset];
338 obj->fses[offset++]=(flag|ormask)&andmask;
340 qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
342 enqueueoptional(obj, 0, NULL, NULL, 0);
347 int oldflag=((int *)ptr)[1];
348 int flag=ormask|oldflag;
350 if (flag==oldflag) /* Don't do anything */
352 else flagbody(ptr, flag);
356 void flagorandinit(void * ptr, int ormask, int andmask) {
357 int oldflag=((int *)ptr)[1];
358 int flag=ormask|oldflag;
363 void flagbody(struct ___Object___ *ptr, int flag) {
364 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
367 /*Remove object from all queues */
368 while(flagptr!=NULL) {
369 struct parameterwrapper *next;
372 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
373 ObjectHashremove(flagptr->objectset, (int)ptr);
374 if (enterflags!=NULL)
380 struct QueueItem *tmpptr;
381 struct parameterwrapper * parameter=objectqueues[ptr->type];
383 struct parameterwrapper * prevptr=NULL;
384 struct ___Object___ *tagptr=ptr->___tags___;
386 /* Outer loop iterates through all parameter queues an object of
387 this type could be in. */
389 while(parameter!=NULL) {
391 if (parameter->numbertags>0) {
393 goto nextloop;//that means the object has no tag but that param needs tag
394 else if(tagptr->type==TAGTYPE) {//one tag
395 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
396 for(i=0;i<parameter->numbertags;i++) {
397 //slotid is parameter->tagarray[2*i];
398 int tagid=parameter->tagarray[2*i+1];
399 if (tagid!=tagptr->flag)
400 goto nextloop; /*We don't have this tag */
402 } else {//multiple tags
403 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
404 for(i=0;i<parameter->numbertags;i++) {
405 //slotid is parameter->tagarray[2*i];
406 int tagid=parameter->tagarray[2*i+1];
408 for(j=0;j<ao->___cachedCode___;j++) {
409 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
420 for(i=0;i<parameter->numberofterms;i++) {
421 int andmask=parameter->intarray[i*2];
422 int checkmask=parameter->intarray[i*2+1];
423 if ((flag&andmask)==checkmask) {
424 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
430 parameter=parameter->next;
432 ptr->flagptr=prevptr;
438 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
440 struct ___Object___ * tagptr = currobj->___tags___;
441 if(fswrapper->numtags>0){
443 return 0; //that means the object has no tag but that param
445 else if(tagptr->type==TAGTYPE) {//one tag
446 if(fswrapper->numtags!=1)
447 return 0; //we don't have the right number of tags
448 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
449 if (fswrapper->tags[0]!=tagptr->flag)
451 } else { //multiple tags
452 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
456 if(ao->___length___!=fswrapper->numtags)
457 return 0;//we don't have the right number of tags
458 for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
459 int tagid=fswrapper->tags[tag_counter];
461 for(j=0;j<ao->___cachedCode___;j++) {
462 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
472 int getlength(int *flist, int len) {
476 int size=flist[count];
482 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
483 int size1=getlength(flist1, len1);
484 int size2=getlength(flist2, len2);
485 int *merge=RUNMALLOC((size1+size2)*sizeof(int));
486 memcpy(merge, flist1, size1*sizeof(int));
487 memcpy(&merge[size1], flist2, size2*sizeof(int));
491 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
495 while(i<len1||j<len2) {
496 if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
498 merge[count]=flist1[i];
502 } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
504 merge[count]=flist2[j];
508 } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
510 merge[count]=flist1[i];
520 /* Merge flags from ftlmerge into ftl. */
521 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
526 for(i=0;i<ftl->numflags;i++) {
527 int len=ftl->flags[offset++];
529 for(j=0;j<ftlmerge->numflags;j++) {
530 int lenmerge=ftlmerge->flags[offsetmerge++];
531 length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
532 offsetmerge+=lenmerge;
536 mergedlist=RUNMALLOC(sizeof(int)*length);
540 for(i=0;i<ftl->numflags;i++) {
541 int len=ftl->flags[offset++];
543 for(j=0;j<ftlmerge->numflags;j++) {
544 int lenmerge=ftlmerge->flags[offsetmerge++];
545 int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
546 mergedlist[length]=size;
551 ftl->flags=mergedlist;
552 ftl->numflags*=ftlmerge->numflags;
555 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
556 struct failedtasklist *tmpptr;
557 while((*andlist)!=NULL) {
558 struct failedtasklist *searchftl=list;
559 while(searchftl!=NULL) {
560 if ((*andlist)->task==searchftl->task&&
561 (*andlist)->index==searchftl->index) {
562 mergeitems(*andlist, searchftl);
565 searchftl=searchftl->next;
567 if (searchftl==NULL) {
568 //didn't find andlist
570 *andlist=(*andlist)->next;//splice item out of list
571 RUNFREE(tmpptr->flags); //free the item
574 andlist=&((*andlist)->next); //iterate to next item
577 //free the list we're searching
580 RUNFREE(list->flags);
586 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
587 struct failedtasklist *list=NULL;
589 struct fsanalysiswrapper *fswrapper=NULL;
590 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
591 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
592 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
593 //we only match exactly here
600 for(i=0;i<fswrapper->numtaskfailures;i++) {
602 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
603 if (taskfail->task==task&&taskfail->index==index) {
605 while(start<taskfail->numoptionaltaskdescriptors) {
606 struct taskdescriptor *currtask=NULL;
607 struct failedtasklist *tmpftl;
611 int numenterflags, offset;
612 struct parameterwrapper *pw;
613 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
614 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
617 currindex=otd->index;
618 } else if (currtask!=otd->task||currindex!=otd->index)
620 totallength+=otd->numenterflags;
622 pw=currtask->descriptorarray[currindex]->queue;
623 enterflags=RUNMALLOC(totallength*sizeof(int));
624 numenterflags=j-start;
626 for(start;start<j;start++) {
627 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
628 enterflags[offset++]=otd->numenterflags;
629 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
630 offset+=otd->numenterflags;
632 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
634 tmpftl->task=currtask;
635 tmpftl->numflags=numenterflags;
636 tmpftl->flags=enterflags;
644 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
645 struct failedtasklist *list=NULL;
648 struct fsanalysiswrapper *fswrapper=NULL;
649 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
650 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
651 if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
652 //we only match exactly here
660 while(start<fswrapper->numoptionaltaskdescriptors) {
661 struct taskdescriptor *currtask=NULL;
662 struct failedtasklist *tmpftl;
667 int numenterflags, offset;
668 struct parameterwrapper *pw;
669 for(j=start;j<fswrapper->numoptionaltaskdescriptors;j++) {
670 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
673 currindex=otd->index;
674 } else if (currtask!=otd->task||currindex!=otd->index)
676 totallength+=otd->numenterflags;
678 pw=currtask->descriptorarray[currindex]->queue;
679 enterflags=RUNMALLOC(totallength*sizeof(int));
680 numenterflags=j-start;
682 for(start;start<j;start++) {
683 struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
684 enterflags[offset++]=otd->numenterflags;
685 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
686 offset+=otd->numenterflags;
688 tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
690 tmpftl->task=currtask;
691 tmpftl->numflags=numenterflags;
692 tmpftl->flags=enterflags;
700 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
701 while(andlist!=NULL) {
702 struct failedtasklist *tmp=andlist;
703 struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
704 struct parmaeterwrapper *next;
709 if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
712 andlist=andlist->next;
717 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
718 struct classanalysiswrapper * classwrapper=NULL;
720 /*test what optionaltaskdescriptors are available, find the class
722 if (classanalysiswrapperarray[currobj->type]!=NULL) {
723 classwrapper = classanalysiswrapperarray[currobj->type];
728 /* We have a failure */
729 if (failedfses==NULL) {
730 /* Failed in normal state */
731 /*first time the method is invoked*/
733 struct fsanalysiswrapper *fswrapper=NULL;
735 for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
736 struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
737 if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
738 //we only match exactly here
743 if(fswrapper==NULL) //nothing to do in this state
745 for(i=0;i<fswrapper->numtaskfailures;i++) {
747 struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
748 if (taskfail->task==task&&taskfail->index==index) {
750 while(start<taskfail->numoptionaltaskdescriptors) {
751 struct taskdescriptor *currtask=NULL;
755 int numenterflags, offset;
756 struct parameterwrapper *pw;
757 for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
758 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
761 currindex=otd->index;
762 } else if (currtask!=otd->task||currindex!=otd->index)
764 totallength+=otd->numenterflags;//1 is to store the lengths
766 pw=currtask->descriptorarray[currindex]->queue;
767 enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
768 numenterflags=j-start;
771 for(start;start<j;start++) {
772 struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
773 enterflags[offset++]=otd->numenterflags;
774 memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
775 offset+=otd->numenterflags;
778 if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
784 /* Failed in failed state */
787 for(i=0;i<numfailedfses;i++) {
788 int numfses=failedfses[offset++];
790 struct failedtasklist *andlist=NULL;
791 for(j=0;j<numfses;j++) {
792 int flagstate=failedfses[offset++];
793 struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
797 mergefailedlists(&andlist, currlist);
799 enqueuelist(currobj, andlist);
803 /* No failure, but we are in a failed state */
804 struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
806 /*Remove object from all queues */
807 while(flagptr!=NULL) {
808 struct parameterwrapper *next;
811 ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
812 ObjectHashremove(flagptr->objectset, (int)currobj);
813 if (enterflags!=NULL)
818 /* Failed in failed state */
821 for(i=0;i<currobj->numfses;i++) {
822 int numfses=currobj->fses[offset++];
824 struct failedtasklist *andlist=NULL;
825 for(j=0;j<numfses;j++) {
826 int flagstate=currobj->fses[offset++];
827 struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
831 mergefailedlists(&andlist, currlist);
833 enqueuelist(currobj, andlist);
841 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
842 void * taskpointerarray[MAXTASKPARAMS];
844 int failed[MAXTASKPARAMS];
847 int numparams=parameter->task->numParameters;
848 int numiterators=parameter->task->numTotal-1;
853 struct taskdescriptor * task=parameter->task;
856 if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
857 /* The object is already here...or it with the existing item */
863 ObjectHashget(parameter->objectset, (int) ptr, & oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
864 mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
865 ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
870 //only add if truly needed
879 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
884 /* Add enqueued object to parameter vector */
885 taskpointerarray[parameter->slot]=ptr;
887 failed[parameter->slot]=(enterflags!=NULL);
890 /* Reset iterators */
891 for(j=0;j<numiterators;j++) {
892 toiReset(¶meter->iterators[j]);
895 /* Find initial state */
896 for(j=0;j<numiterators;j++) {
898 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
899 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
901 /* Need to backtrack */
902 toiReset(¶meter->iterators[j]);
906 /* Nothing to enqueue */
913 /* Enqueue current state */
915 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
917 tpd->numParameters=numiterators+1;
918 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
920 tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
922 for(j=0;j<=numiterators;j++){
923 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
925 tpd->failed[j]=failed[j];
926 if (failed[j]!=0&&failed[j]!=1) {
932 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
933 genputtable(activetasks, tpd, tpd);
935 RUNFREE(tpd->parameterArray);
937 RUNFREE(tpd->failed);
942 /* This loop iterates to the next parameter combination */
946 for(j=numiterators-1; j<numiterators;j++) {
948 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
949 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
951 /* Need to backtrack */
952 toiReset(¶meter->iterators[j]);
956 /* Nothing more to enqueue */
964 /* Handler for signals. The signals catch null pointer errors and
965 arithmatic errors. */
967 void myhandler(int sig, siginfo_t *info, void *uap) {
970 printf("sig=%d\n",sig);
973 sigemptyset(&toclear);
974 sigaddset(&toclear, sig);
975 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
976 longjmp(error_handler,1);
981 struct RuntimeHash *fdtoobject;
983 void addreadfd(int fd) {
986 FD_SET(fd, &readfds);
989 void removereadfd(int fd) {
990 FD_CLR(fd, &readfds);
991 if (maxreadfd==(fd+1)) {
993 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1005 int * fsescopy(int *src, int len) {
1009 dst=RUNMALLOC(len*sizeof(int));
1010 memcpy(dst, src, len*sizeof(int));
1015 void executetasks() {
1016 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1018 int * fsesarray[MAXTASKPARAMS];
1019 int * oldfsesarray[MAXTASKPARAMS];
1020 int numfsesarray[MAXTASKPARAMS];
1023 /* Set up signal handlers */
1024 struct sigaction sig;
1025 sig.sa_sigaction=&myhandler;
1026 sig.sa_flags=SA_SIGINFO;
1027 sigemptyset(&sig.sa_mask);
1029 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1030 sigaction(SIGBUS,&sig,0);
1031 sigaction(SIGSEGV,&sig,0);
1032 sigaction(SIGFPE,&sig,0);
1033 sigaction(SIGPIPE,&sig,0);
1038 fdtoobject=allocateRuntimeHash(100);
1040 /* Map first block of memory to protected, anonymous page */
1041 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1044 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1046 /* Check if any filedescriptors have IO pending */
1049 struct timeval timeout={0,0};
1053 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1055 /* Process ready fd's */
1057 for(fd=0;fd<maxreadfd;fd++) {
1058 if (FD_ISSET(fd, &tmpreadfds)) {
1059 /* Set ready flag on object */
1061 // printf("Setting fd %d\n",fd);
1062 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1063 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
1070 /* See if there are any active tasks */
1071 if (hashsize(activetasks)>0) {
1073 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1074 genfreekey(activetasks, currtpd);
1076 /* Check if this task has failed, allow a task that contains optional objects to fire */
1077 if (gencontains(failedtasks, currtpd)) {
1078 // Free up task parameter descriptor
1079 RUNFREE(currtpd->parameterArray);
1081 RUNFREE(currtpd->failed);
1086 int numparams=currtpd->task->numParameters;
1087 int numtotal=currtpd->task->numTotal;
1089 /* Make sure that the parameters are still in the queues */
1090 for(i=0;i<numparams;i++) {
1091 void * parameter=currtpd->parameterArray[i];
1092 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1093 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1095 /* Check that object is still in queue */
1098 int UNUSED, UNUSED2;
1100 int numflags, isnonfailed;
1101 int failed=currtpd->failed[i];
1102 if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1103 RUNFREE(currtpd->parameterArray);
1104 RUNFREE(currtpd->failed);
1108 if (failed&&(flags!=NULL)) {
1111 numfsesarray[i]=numflags;
1112 } else if (!failed && isnonfailed) {
1113 //Non-failed parameter
1117 RUNFREE(currtpd->parameterArray);
1118 RUNFREE(currtpd->failed);
1126 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1127 RUNFREE(currtpd->parameterArray);
1135 /* Check that object still has necessary tags */
1136 for(j=0;j<pd->numbertags;j++) {
1137 int slotid=pd->tagarray[2*j]+numparams;
1138 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1139 if (!containstag(parameter, tagd)) {
1140 RUNFREE(currtpd->parameterArray);
1142 RUNFREE(currtpd->failed);
1149 taskpointerarray[i+OFFSET]=parameter;
1152 for(;i<numtotal;i++) {
1153 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1157 /* Checkpoint the state */
1158 forward=allocateRuntimeHash(100);
1159 reverse=allocateRuntimeHash(100);
1160 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1162 if (x=setjmp(error_handler)) {
1166 printf("Fatal Error=%d, Recovering!\n",x);
1168 genputtable(failedtasks,currtpd,currtpd);
1169 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1172 for(counter=0; counter<currtpd->task->numParameters; counter++){
1174 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1177 if (fsesarray[counter]!=NULL)
1178 RUNFREE(fsesarray[counter]);
1181 freeRuntimeHash(forward);
1182 freeRuntimeHash(reverse);
1187 if (injectfailures) {
1188 if ((((double)random())/RAND_MAX)<failurechance) {
1189 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1190 longjmp(error_handler,10);
1193 /* Actually call task */
1195 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1196 taskpointerarray[1]=NULL;
1199 //get the task flags set
1200 for(i=0;i<numparams;i++) {
1201 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1202 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1203 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1207 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1208 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1209 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1211 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1214 for(i=0;i<numparams;i++) {
1216 if(oldfsesarray[i]!=NULL)
1217 RUNFREE(oldfsesarray[i]);
1221 freeRuntimeHash(forward);
1222 freeRuntimeHash(reverse);
1224 // Free up task parameter descriptor
1225 RUNFREE(currtpd->parameterArray);
1227 RUNFREE(currtpd->failed);
1238 /* This function processes an objects tags */
1239 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1242 for(i=0;i<pd->numbertags;i++) {
1243 int slotid=pd->tagarray[2*i];
1244 int tagid=pd->tagarray[2*i+1];
1246 if (statusarray[slotid+numparams]==0) {
1247 parameter->iterators[*iteratorcount].istag=1;
1248 parameter->iterators[*iteratorcount].tagid=tagid;
1249 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1250 parameter->iterators[*iteratorcount].tagobjectslot=index;
1251 statusarray[slotid+numparams]=1;
1258 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1261 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1263 parameter->iterators[*iteratorcount].istag=0;
1264 parameter->iterators[*iteratorcount].slot=index;
1265 parameter->iterators[*iteratorcount].objectset=objectset;
1266 statusarray[index]=1;
1268 for(i=0;i<pd->numbertags;i++) {
1269 int slotid=pd->tagarray[2*i];
1270 int tagid=pd->tagarray[2*i+1];
1271 if (statusarray[slotid+numparams]!=0) {
1272 /* This tag has already been enqueued, use it to narrow search */
1273 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1277 parameter->iterators[*iteratorcount].numtags=tagcount;
1282 /* This function builds the iterators for a task & parameter */
1284 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1285 int statusarray[MAXTASKPARAMS];
1287 int numparams=task->numParameters;
1288 int iteratorcount=0;
1289 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1291 statusarray[index]=1; /* Initial parameter */
1292 /* Process tags for initial iterator */
1294 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1298 /* Check for objects with existing tags */
1299 for(i=0;i<numparams;i++) {
1300 if (statusarray[i]==0) {
1301 struct parameterdescriptor *pd=task->descriptorarray[i];
1303 for(j=0;j<pd->numbertags;j++) {
1304 int slotid=pd->tagarray[2*j];
1305 if(statusarray[slotid+numparams]!=0) {
1306 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1307 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1314 /* Next do objects w/ unbound tags*/
1316 for(i=0;i<numparams;i++) {
1317 if (statusarray[i]==0) {
1318 struct parameterdescriptor *pd=task->descriptorarray[i];
1319 if (pd->numbertags>0) {
1320 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1321 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1327 /* Nothing with a tag enqueued */
1329 for(i=0;i<numparams;i++) {
1330 if (statusarray[i]==0) {
1331 struct parameterdescriptor *pd=task->descriptorarray[i];
1332 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1333 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1346 for(i=0;i<numtasks;i++) {
1347 struct taskdescriptor * task=taskarray[i];
1348 printf("%s\n", task->name);
1349 for(j=0;j<task->numParameters;j++) {
1350 struct parameterdescriptor *param=task->descriptorarray[j];
1351 struct parameterwrapper *parameter=param->queue;
1352 struct ObjectHash * set=parameter->objectset;
1353 struct ObjectIterator objit;
1354 printf(" Parameter %d\n", j);
1355 ObjectHashiterator(set, &objit);
1356 while(ObjhasNext(&objit)) {
1357 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1358 struct ___Object___ * tagptr=obj->___tags___;
1359 int nonfailed=Objdata4(&objit);
1360 int numflags=Objdata3(&objit);
1361 int flags=Objdata2(&objit);
1363 printf(" Contains %lx\n", obj);
1364 printf(" flag=%d\n", obj->flag);
1366 printf(" flagsstored=%x\n",flags);
1367 printf(" numflags=%d\n", numflags);
1368 printf(" nonfailed=%d\n",nonfailed);
1371 } else if (tagptr->type==TAGTYPE) {
1372 printf(" tag=%lx\n",tagptr);
1375 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1376 for(;tagindex<ao->___cachedCode___;tagindex++) {
1377 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1386 /* This function processes the task information to create queues for
1387 each parameter type. */
1389 void processtasks() {
1391 for(i=0;i<numtasks;i++) {
1392 struct taskdescriptor * task=taskarray[i];
1395 for(j=0;j<task->numParameters;j++) {
1396 struct parameterdescriptor *param=task->descriptorarray[j];
1397 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1398 struct parameterwrapper ** ptr=&objectqueues[param->type];
1400 param->queue=parameter;
1401 parameter->objectset=allocateObjectHash(10);
1402 parameter->numberofterms=param->numberterms;
1403 parameter->intarray=param->intarray;
1404 parameter->numbertags=param->numbertags;
1405 parameter->tagarray=param->tagarray;
1406 parameter->task=task;
1407 /* Link new queue in */
1409 ptr=&((*ptr)->next);
1413 /* Build iterators for parameters */
1414 for(j=0;j<task->numParameters;j++) {
1415 struct parameterdescriptor *param=task->descriptorarray[j];
1416 struct parameterwrapper *parameter=param->queue;
1418 builditerators(task, j, parameter);
1423 void toiReset(struct tagobjectiterator * it) {
1426 } else if (it->numtags>0) {
1432 ObjectHashiterator(it->objectset, &it->it);
1439 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1442 /* Get object with tags */
1443 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1444 struct ___Object___ *tagptr=obj->___tags___;
1445 if (tagptr->type==TAGTYPE) {
1446 if ((it->tagobjindex==0)&& /* First object */
1447 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1452 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1453 int tagindex=it->tagobjindex;
1454 for(;tagindex<ao->___cachedCode___;tagindex++) {
1455 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1456 if (td->flag==it->tagid) {
1457 it->tagobjindex=tagindex; /* Found right type of tag */
1463 } else if (it->numtags>0) {
1464 /* Use tags to locate appropriate objects */
1465 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1466 struct ___Object___ *objptr=tag->flagptr;
1468 if (objptr->type!=OBJECTARRAYTYPE) {
1469 if (it->tagobjindex>0)
1471 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1473 for(i=1;i<it->numtags;i++) {
1474 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1475 if (!containstag(objptr,tag2))
1479 if (it->failedstate==1) {
1480 int UNUSED, UNUSED2;
1483 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1492 int UNUSED, UNUSED2;
1495 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1504 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1508 if (it->failedstate==1) {
1509 int UNUSED, UNUSED2;
1512 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1513 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1522 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1523 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1524 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1526 for(i=1;i<it->numtags;i++) {
1527 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1528 if (!containstag(objptr,tag2))
1533 int UNUSED, UNUSED2;
1534 int flags, isnonfailed;
1535 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1536 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1542 it->tagobjindex=tagindex;
1547 it->tagobjindex=tagindex;
1552 if (it->failedstate==1) {
1553 if (Objdata2(&it->it))
1560 if (ObjhasNext(&it->it)) {
1561 if (!Objdata4(&it->it)) {
1569 return ObjhasNext(&it->it);
1574 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1576 struct ___Object___ * objptr=tag->flagptr;
1577 if (objptr->type==OBJECTARRAYTYPE) {
1578 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1579 for(j=0;j<ao->___cachedCode___;j++) {
1580 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1588 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1589 /* hasNext has all of the intelligence */
1592 /* Get object with tags */
1593 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1594 struct ___Object___ *tagptr=obj->___tags___;
1596 failed[it->slot]=0; //have to set it to something
1598 if (tagptr->type==TAGTYPE) {
1600 objectarray[it->slot]=tagptr;
1602 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1603 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1605 } else if (it->numtags>0) {
1606 /* Use tags to locate appropriate objects */
1607 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1608 struct ___Object___ *objptr=tag->flagptr;
1609 if (objptr->type!=OBJECTARRAYTYPE) {
1611 failed[it->slot]=it->failedstate;
1612 objectarray[it->slot]=objptr;
1613 if (it->failedstate==0) {
1621 objectarray[it->slot]=objptr;
1624 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1626 failed[it->slot]=it->failedstate;
1627 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1628 if (it->failedstate==0) {
1635 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1639 /* Iterate object */
1640 objectarray[it->slot]=(void *)Objkey(&it->it);
1642 failed[it->slot]=it->failedstate;
1643 if (it->failedstate==0) {