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);
1146 taskpointerarray[i+OFFSET]=parameter;
1149 for(;i<numtotal;i++) {
1150 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1154 /* Checkpoint the state */
1155 forward=allocateRuntimeHash(100);
1156 reverse=allocateRuntimeHash(100);
1157 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1159 if (x=setjmp(error_handler)) {
1163 printf("Fatal Error=%d, Recovering!\n",x);
1165 genputtable(failedtasks,currtpd,currtpd);
1166 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1169 for(counter=0; counter<currtpd->task->numParameters; counter++){
1171 enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1174 if (fsesarray[counter]!=NULL)
1175 RUNFREE(fsesarray[counter]);
1178 freeRuntimeHash(forward);
1179 freeRuntimeHash(reverse);
1184 if (injectfailures) {
1185 if ((((double)random())/RAND_MAX)<failurechance) {
1186 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1187 longjmp(error_handler,10);
1190 /* Actually call task */
1192 ((int *)taskpointerarray)[0]=currtpd->numParameters;
1193 taskpointerarray[1]=NULL;
1196 //get the task flags set
1197 for(i=0;i<numparams;i++) {
1198 oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1199 fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1200 ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];
1204 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1205 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1206 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1208 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1211 for(i=0;i<numparams;i++) {
1213 if(oldfsesarray[i]!=NULL)
1214 RUNFREE(oldfsesarray[i]);
1218 freeRuntimeHash(forward);
1219 freeRuntimeHash(reverse);
1221 // Free up task parameter descriptor
1222 RUNFREE(currtpd->parameterArray);
1232 /* This function processes an objects tags */
1233 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1236 for(i=0;i<pd->numbertags;i++) {
1237 int slotid=pd->tagarray[2*i];
1238 int tagid=pd->tagarray[2*i+1];
1240 if (statusarray[slotid+numparams]==0) {
1241 parameter->iterators[*iteratorcount].istag=1;
1242 parameter->iterators[*iteratorcount].tagid=tagid;
1243 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1244 parameter->iterators[*iteratorcount].tagobjectslot=index;
1245 statusarray[slotid+numparams]=1;
1252 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1255 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1257 parameter->iterators[*iteratorcount].istag=0;
1258 parameter->iterators[*iteratorcount].slot=index;
1259 parameter->iterators[*iteratorcount].objectset=objectset;
1260 statusarray[index]=1;
1262 for(i=0;i<pd->numbertags;i++) {
1263 int slotid=pd->tagarray[2*i];
1264 int tagid=pd->tagarray[2*i+1];
1265 if (statusarray[slotid+numparams]!=0) {
1266 /* This tag has already been enqueued, use it to narrow search */
1267 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1271 parameter->iterators[*iteratorcount].numtags=tagcount;
1276 /* This function builds the iterators for a task & parameter */
1278 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1279 int statusarray[MAXTASKPARAMS];
1281 int numparams=task->numParameters;
1282 int iteratorcount=0;
1283 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1285 statusarray[index]=1; /* Initial parameter */
1286 /* Process tags for initial iterator */
1288 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1292 /* Check for objects with existing tags */
1293 for(i=0;i<numparams;i++) {
1294 if (statusarray[i]==0) {
1295 struct parameterdescriptor *pd=task->descriptorarray[i];
1297 for(j=0;j<pd->numbertags;j++) {
1298 int slotid=pd->tagarray[2*j];
1299 if(statusarray[slotid+numparams]!=0) {
1300 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1301 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1308 /* Next do objects w/ unbound tags*/
1310 for(i=0;i<numparams;i++) {
1311 if (statusarray[i]==0) {
1312 struct parameterdescriptor *pd=task->descriptorarray[i];
1313 if (pd->numbertags>0) {
1314 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1315 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1321 /* Nothing with a tag enqueued */
1323 for(i=0;i<numparams;i++) {
1324 if (statusarray[i]==0) {
1325 struct parameterdescriptor *pd=task->descriptorarray[i];
1326 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1327 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1340 for(i=0;i<numtasks;i++) {
1341 struct taskdescriptor * task=taskarray[i];
1342 printf("%s\n", task->name);
1343 for(j=0;j<task->numParameters;j++) {
1344 struct parameterdescriptor *param=task->descriptorarray[j];
1345 struct parameterwrapper *parameter=param->queue;
1346 struct ObjectHash * set=parameter->objectset;
1347 struct ObjectIterator objit;
1348 printf(" Parameter %d\n", j);
1349 ObjectHashiterator(set, &objit);
1350 while(ObjhasNext(&objit)) {
1351 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1352 struct ___Object___ * tagptr=obj->___tags___;
1353 int nonfailed=Objdata4(&objit);
1354 int numflags=Objdata3(&objit);
1355 int flags=Objdata2(&objit);
1357 printf(" Contains %lx\n", obj);
1358 printf(" flag=%d\n", obj->flag);
1360 printf(" flagsstored=%x\n",flags);
1361 printf(" numflags=%d\n", numflags);
1362 printf(" nonfailed=%d\n",nonfailed);
1365 } else if (tagptr->type==TAGTYPE) {
1366 printf(" tag=%lx\n",tagptr);
1369 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1370 for(;tagindex<ao->___cachedCode___;tagindex++) {
1371 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1380 /* This function processes the task information to create queues for
1381 each parameter type. */
1383 void processtasks() {
1385 for(i=0;i<numtasks;i++) {
1386 struct taskdescriptor * task=taskarray[i];
1389 for(j=0;j<task->numParameters;j++) {
1390 struct parameterdescriptor *param=task->descriptorarray[j];
1391 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1392 struct parameterwrapper ** ptr=&objectqueues[param->type];
1394 param->queue=parameter;
1395 parameter->objectset=allocateObjectHash(10);
1396 parameter->numberofterms=param->numberterms;
1397 parameter->intarray=param->intarray;
1398 parameter->numbertags=param->numbertags;
1399 parameter->tagarray=param->tagarray;
1400 parameter->task=task;
1401 /* Link new queue in */
1403 ptr=&((*ptr)->next);
1407 /* Build iterators for parameters */
1408 for(j=0;j<task->numParameters;j++) {
1409 struct parameterdescriptor *param=task->descriptorarray[j];
1410 struct parameterwrapper *parameter=param->queue;
1412 builditerators(task, j, parameter);
1417 void toiReset(struct tagobjectiterator * it) {
1420 } else if (it->numtags>0) {
1426 ObjectHashiterator(it->objectset, &it->it);
1433 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1436 /* Get object with tags */
1437 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1438 struct ___Object___ *tagptr=obj->___tags___;
1439 if (tagptr->type==TAGTYPE) {
1440 if ((it->tagobjindex==0)&& /* First object */
1441 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1446 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1447 int tagindex=it->tagobjindex;
1448 for(;tagindex<ao->___cachedCode___;tagindex++) {
1449 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1450 if (td->flag==it->tagid) {
1451 it->tagobjindex=tagindex; /* Found right type of tag */
1457 } else if (it->numtags>0) {
1458 /* Use tags to locate appropriate objects */
1459 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1460 struct ___Object___ *objptr=tag->flagptr;
1462 if (objptr->type!=OBJECTARRAYTYPE) {
1463 if (it->tagobjindex>0)
1465 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1467 for(i=1;i<it->numtags;i++) {
1468 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1469 if (!containstag(objptr,tag2))
1473 if (it->failedstate==1) {
1474 int UNUSED, UNUSED2;
1477 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1486 int UNUSED, UNUSED2;
1489 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1498 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1502 if (it->failedstate==1) {
1503 int UNUSED, UNUSED2;
1506 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1507 ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1516 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1517 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1518 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1520 for(i=1;i<it->numtags;i++) {
1521 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1522 if (!containstag(objptr,tag2))
1527 int UNUSED, UNUSED2;
1528 int flags, isnonfailed;
1529 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1530 ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1536 it->tagobjindex=tagindex;
1541 it->tagobjindex=tagindex;
1546 if (it->failedstate==1) {
1547 if (Objdata2(&it->it))
1554 if (ObjhasNext(&it->it)) {
1555 if (!Objdata4(&it->it)) {
1563 return ObjhasNext(&it->it);
1568 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1570 struct ___Object___ * objptr=tag->flagptr;
1571 if (objptr->type==OBJECTARRAYTYPE) {
1572 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1573 for(j=0;j<ao->___cachedCode___;j++) {
1574 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1582 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1583 /* hasNext has all of the intelligence */
1586 /* Get object with tags */
1587 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1588 struct ___Object___ *tagptr=obj->___tags___;
1590 failed[it->slot]=0; //have to set it to something
1592 if (tagptr->type==TAGTYPE) {
1594 objectarray[it->slot]=tagptr;
1596 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1597 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1599 } else if (it->numtags>0) {
1600 /* Use tags to locate appropriate objects */
1601 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1602 struct ___Object___ *objptr=tag->flagptr;
1603 if (objptr->type!=OBJECTARRAYTYPE) {
1605 failed[it->slot]=it->failedstate;
1606 objectarray[it->slot]=objptr;
1607 if (it->failedstate==0) {
1615 objectarray[it->slot]=objptr;
1618 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1620 failed[it->slot]=it->failedstate;
1621 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1622 if (it->failedstate==0) {
1629 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1633 /* Iterate object */
1634 objectarray[it->slot]=(void *)Objkey(&it->it);
1636 failed[it->slot]=it->failedstate;
1637 if (it->failedstate==0) {