/********************************************************************** ** calQ.c ** ** These routines implement a Calander Queue data structure for ** maintaining a priority queue in a shared memory multiprocessor ** system. **********************************************************************/ #include #include #include #include #include #include #include "snoopy.h" #include "rnum.h" /* ** GLOBAL DEFINES */ #define MAXBUCKETS (512 * 1024) /* max 512K buckets in calendar */ #define SNOOPyOn 16 /* ** GLOBAL VARIABLES */ bucketEntry *bfreelist; /* a head pointer to a free list of buckets */ int calInitialized = FALSE; /* has calendar been initialized? */ calendarHdr bucketArray[MAXBUCKETS]; /* array of buckets */ calendarHdr * calendar; /* ptr to base bucket of calendar */ int calFirstSubscript=0; /* bucketArray index of calendar base */ int calNBuckets=0; /* current calendar size in buckets */ int calBucketWidth=0; /* width of each bucket in time units */ int calQSize=0; /* total number of entries in queue */ int calLastBucket=0; /* bucket from which last event dequeued */ double calBucketTop=0; /* priority at top of current bucket */ int calBotThreshold=0; /* max queue size before shrink calendar */ int calTopThreshold=0; /* max queue size before expanding calendar */ double lastPriority=0; /* priority of last event */ double lastBucketTop=0; /* value of BucketTop when last event dequeued*/ double calYearStart=0.0; //Start of Calendar year (not used now) double myT=2.0; //Threshold value int SmplIntv=0; //Sampling Interval(Slot Size) int FineTune=0; //switch to off(0) discretization when doing finetuning of bucketwidth //when long term cost ratio is breached int Qsearch = 0; int Bsearch = 0; int Blsearch=0,rescnt=0; int Bs1=0,N1=0; double BOP1=0; double SumQsearch = 0; double SumBsearch = 0; double TotQsch = 0; double TotSumQsch = 0; double TotBsch = 0; double TotSumBsch = 0; double QOP=0; //Exponential Moving Average Cost of Enqueuing double BOP=0; //Exponential Moving Average Cost of Dequeuing int MAAECR=0,MAADCR=0,MAAECcnt=0,MAADCcnt=0; //Cost Ratio housekeeping variables double MAAEC[10],MAADC[10]; //10 Average Enqueue/Dequeue Cost Slots /* ** LOCAL ROUTINES */ void calInit(int,int,int,double); void calResize(); double bucketNewWidth(); void localEnqueue(bucketEntry*); bucketEntry* localDequeue(); int GetRatio(); //Cost Ratio Check int OccBuck(); //Returns number of occupied buckets void GetStats(); //Printout Stats of CQ void PrintBucks(); void CheckBuck(int); void VerifyCal(); //use to verify correctness of CQ void ModThreshold(); //Changes Threshold value /* */ /* ** EXPORTED ROUTINES */ /********************************************************************** */ void enqueue(double timeStamp, int cid, struct event* eventStruct) /* ** enqueue the specified eventStruct, using the timeStamp as its ** priority. no restrictions are placed on the eventStruct, other ** than that it should be allocated from a shared memory segment ** if events are to be accessed by multiple processes. **********************************************************************/ { unsigned long int virtualBucket; /* bucket index if calendar were infinite */ int actualBucket; /* bucket index for actual calendar */ bucketEntry *newBucketEntry; /* bucketEntry for new record */ bucketEntry *curBucketEntry; /* used to traverse entries in bucket */ bucketEntry *prevBucketEntry; /* used to traverse entries in bucket */ int SCnt=0; /* ** throw away the event if it's in the past ** NOTE: should actually return some indication that event ** was in the past and thrown away */ if (timeStamp < lastPriority) { cout<<"error"<p.priority = timeStamp; newBucketEntry->p.cid = cid; newBucketEntry->event = eventStruct; /* ** insert the newBucketEntry in the bucket */ if (!calendar[actualBucket].bp) { calendar[actualBucket].bp = newBucketEntry; newBucketEntry->next = (bucketEntry *)NULL; } else { prevBucketEntry = (bucketEntry *)NULL; curBucketEntry = calendar[actualBucket].bp; while (curBucketEntry && (curBucketEntry->p.priority <= newBucketEntry->p.priority)) { if (curBucketEntry->p.priority == newBucketEntry->p.priority) if (curBucketEntry->p.cid > newBucketEntry->p.cid) break; prevBucketEntry = curBucketEntry; curBucketEntry = curBucketEntry->next; SCnt++; } if (!prevBucketEntry) calendar[actualBucket].bp = newBucketEntry; else prevBucketEntry->next = newBucketEntry; newBucketEntry->next = curBucketEntry; } /* ** increment queue size and expand calendar if necessary */ calQSize++; SumQsearch += SCnt; //counts no. of events searched upon enqueue Qsearch++; //counts enqueue operations after last resize if (Qsearch > SmplIntv){ QOP=0.1*QOP+0.9*(double)SumQsearch/Qsearch; //GetStats(); if ( QOP >myT ){//&& calQSize > calNBuckets ) { // if total no. of events exceed threshold MAAECcnt=MAADCcnt=MAAECR=MAADCR=0; calResize(); } else if (QOP >myT ||GetRatio() ){ // if average no. of node searches exceeds Threshold MAAECcnt=MAADCcnt=MAAECR=MAADCR=0; //reset stats calResize(); } else { myT=0.9*myT; //Threshold decays if(myT<2.0) myT=2.0; MAAEC[MAAECcnt]=(double)SumQsearch/Qsearch; //update 10 most recent cost matrices ++MAAECcnt; if(MAAECcnt==10){ MAAECcnt=0; MAAECR=1; } Qsearch = 0; //reset stats SumQsearch = 0; } } /* countng the Entry search no */ TotQsch += 1.0; TotSumQsch += SCnt; /* ** release the calendar lock */ return; } /* */ /********************************************************************** */ struct event* dequeue() /* ** dequeue the highest priority event (smallest timeStamp), and return ** a pointer to it's associated eventStruct. **********************************************************************/ { bucketEntry *returnBucketEntry; /* ptr to event to dequeue */ event *returnEvent; /* ptr to event to return */ int i; /* loop index */ int directSearchInitialized;/* has direct search been initialized */ double directSearchPriority=0; /* highest priority event found in direct search */ int directSearchBucket=0; /* bucket of highest priority found in direct search */ unsigned long int warpBuckets; /* number of buckets warped during direct search */ unsigned long int warpYears; /* number of years warped during direct search */ int SCnt=0; /* ** make sure there is at least one event queued */ if (calQSize == 0) return((struct event *)NULL); /* ** starting from the bucket where the last event was retrieved, ** search for the next event in this year */ for (i = calLastBucket; ; ) { /* ** check the head element of the current bucket (if it exists) */ if ((calendar[i].bp != (bucketEntry *)NULL) && ((calendar[i].bp)->p.priority < calBucketTop)) { /* ** next event has been found ** remove it from the queue */ returnBucketEntry = calendar[i].bp; returnEvent = returnBucketEntry->event; calendar[i].bp = returnBucketEntry->next; /* ** update position in calendar */ calLastBucket = i; lastPriority = returnBucketEntry->p.priority; lastBucketTop = calBucketTop; calQSize--; BFREE(returnBucketEntry); /* ** reduce calendar size if its shrunk below threshold */ SumBsearch += SCnt; //counts no. of buckets searched upon dequeue Bsearch++; //counts dequeue operations after last resize if (Bsearch > SmplIntv) { BOP=0.1*BOP+0.9*(double)SumBsearch/Bsearch; //GetStats(); if (BOP > myT ){//&& calQSize < calNBuckets/2 ) { MAAECcnt=MAADCcnt=MAAECR=MAADCR=0; calResize(); } else if (BOP > myT||GetRatio()) {// if average no. of bucket searches exceeds Threshold MAAECcnt=MAADCcnt=MAAECR=MAADCR=0; //reset stats calResize(); } else { myT=0.9*myT; //Threshold decays if(myT<2.0) myT=2.0; MAADC[MAADCcnt]=(double)SumBsearch/Bsearch; //update 10 most recent cost matrices ++MAADCcnt; if(MAADCcnt==10){ MAADCcnt=0; MAADCR=1; } Bsearch = 0; SumBsearch = 0; } } /* counting the bucket search no */ TotBsch += 1.0; TotSumBsch += SCnt; return(returnEvent); } else { /* ** advance (circularly) to the next bucket in the calendar */ i++; if (i == calNBuckets){ i = 0; calYearStart=calYearStart+(double)calBucketWidth*(double)calNBuckets; } calBucketTop += calBucketWidth; SCnt++; /* ** if we've cycled a complete year without finding ** an event, then break out and go to direct search */ if (i == calLastBucket) break; } } /* ** perform direct search through a whole year */ directSearchInitialized = FALSE; for (i = 0; i < calNBuckets; i++) { if (calendar[i].bp) { if (!directSearchInitialized) { directSearchPriority = (calendar[i].bp)->p.priority; directSearchBucket = i; directSearchInitialized = TRUE; } else if ((calendar[i].bp)->p.priority < directSearchPriority) { directSearchPriority = (calendar[i].bp)->p.priority; directSearchBucket = i; } } } /* ** dequeue the event found */ returnBucketEntry = calendar[directSearchBucket].bp; returnEvent = returnBucketEntry->event; calendar[directSearchBucket].bp = returnBucketEntry->next; /* ** update position in calendar */ warpBuckets = (unsigned long int)(directSearchPriority / calBucketWidth) - (unsigned long int)(lastPriority / calBucketWidth); warpYears= (unsigned long int)((directSearchPriority- calYearStart) / ((double)calBucketWidth*(double)calNBuckets)) ; calBucketTop = lastBucketTop + (double)warpBuckets * calBucketWidth; calYearStart=calYearStart+ (double)warpYears * (double)calBucketWidth*(double)calNBuckets; lastBucketTop = calBucketTop; calLastBucket = directSearchBucket; lastPriority = directSearchPriority; calQSize--; BFREE(returnBucketEntry); SumBsearch += SCnt; Bsearch++; //Too many direct search protection Blsearch++; if(Blsearch<2){ //keep calendar parameter of first direct search Bs1=Bsearch; BOP1=BOP; N1=calNBuckets; } else if(Blsearch>99){//compare calendar parameter with the first direct search Blsearch=0; if(BOP1==BOP){ if(N1==calNBuckets){ if(Bsearch-Bs1+1>=100){ if(calNBuckets>SNOOPyOn){//all parameters the same if(100.0/(Bsearch-Bs1+1)>0.3){ //>0.3 searches are direct searches BOP=0.1*BOP+(double)SumBsearch/Bsearch; MAAECcnt=MAADCcnt=MAAECR=MAADCR=0; calResize(); } } } } } } /* counting the bucket search no */ TotBsch += 1.0; TotSumBsch += SCnt; /* ** unlock the calendar and return the dequeued event */ return(returnEvent); } /* */ /********************************************************************** ** Local Routines **********************************************************************/ /********************************************************************** */ void calInit(int bucketArrayBase,int numBuckets,int bucketWidth,double startPriority) /* ** Initialize all of the global state variables related to changing ** the calendar size. **********************************************************************/ // int bucketArrayBase; /* index of calendar base in bucketArray */ // int numBuckets; /* number of buckets in calendar */ // int bucketWidth; /* width of each bucket in time units */ // double startPriority; /* timeStamp to start at */ { unsigned long int virtualBucket; /* bucket index if calendar were infinite */ int i; /* loop index */ unsigned long int warpYears; /* ** set position and size of new calendar */ calFirstSubscript = bucketArrayBase; calendar = &(bucketArray[bucketArrayBase]); calBucketWidth = bucketWidth; calNBuckets = numBuckets; //Finding a sampling interval to use if(calQSize>0) SmplIntv=pow(2.0,(ceil(log(calQSize)/log(2)))); else SmplIntv=numBuckets; if(SmplIntv>numBuckets) SmplIntv=numBuckets; //Slot size if(SmplIntv (MAXBUCKETS / 2)){ /* fatal("The number of buckets is not enough at CalInit"); */ } calQSize = 0; for (i = 0; i < calNBuckets; i++) { calendar[i].bp = (bucketEntry *)NULL; calendar[i].LBE = (bucketEntry *)NULL; //ponter for smart recopying } /* ** set initial position in queue */ lastPriority = startPriority; virtualBucket = (unsigned long int)(startPriority / bucketWidth); calLastBucket = (int)(virtualBucket%calNBuckets); calBucketTop = (virtualBucket + 1.0) * bucketWidth+ 0.5 * bucketWidth; warpYears = (unsigned long int)(startPriority / ((double)bucketWidth*(double)numBuckets)); calYearStart= (double)numBuckets*(double)bucketWidth*(double)warpYears; lastBucketTop = calBucketTop; /* ** set queue size change thresholds */ //calBotThreshold = numBuckets;//numBuckets/2 - 2; // calTopThreshold = numBuckets;//2 * numBuckets; } /* */ /********************************************************************** */ void calResize() /* ** Copy the current queue onto a calendar with newCalSize buckets. ** The new bucket array is on the opposite end of the array ** bucketArray from the original. **********************************************************************/ { int calOldBW;//old bucketwidth int calOldLB; // old lastbucket double resFac; //Resize Factor for upsizing of bucket width double dwnFac=0.0; //Resize factor for downsizing of bucketwidth int bucketWidth; /* width of each bucket in time units */ int calOldSize; /* current calendar size */ int calOldQSize; //No of Buckets for Old calendar int myIdx; int i,k; /* loop index */ calendarHdr * calOld; /* ptr to current calendar base bucket */ bucketEntry * curBucketEntry; //recopying pointers bucketEntry * nextBucketEntry; bucketEntry * prevBucketEntry; int calNewSize; if(!FineTune){ //not Finetuning if(calNBuckets>SNOOPyOn && BOP >0.001 && QOP>0.001){ //for stability QOP, BOP not too small if not (int) overflow bucketWidth = (int)(calBucketWidth*sqrt(BOP/QOP)); } else if(calNBuckets>SNOOPyOn && BOP >0.001 && QOP<0.001 ){ bucketWidth = (int)(calBucketWidth*sqrt(BOP/1));//keep BO low assume QOP=1 (no info on QO) } else if(calNBuckets>SNOOPyOn && QOP >0.001 && BOP<0.001 ){ bucketWidth = (int)(calBucketWidth*sqrt(1/(QOP))); //keep QO low assume BOP=1(no info on BO) } else{ bucketWidth = (int)bucketNewWidth(); //calNBucket1) resFac=bucketWidth/(double)calBucketWidth; if(resFac>=1){ if(resFac-floor(resFac)>0.5) resFac=ceil(resFac); else resFac=floor(resFac); } else{ dwnFac=1/resFac; if(dwnFac-floor(dwnFac)>0.5) dwnFac=ceil(dwnFac); else dwnFac=floor(dwnFac); } //Conditions for discretizing and maintaining bucketwidth if((resFac==1 || dwnFac==1)&& !FineTune ) bucketWidth=(int)calBucketWidth; //Min buketwidth if(bucketWidth<1) bucketWidth =1; //Check if after discretization need to resize? if ((calNBuckets == calNewSize) && (bucketWidth == calBucketWidth)){ Qsearch = SumQsearch = 0; Bsearch = SumBsearch = 0; FineTune=0; return; } if(!FineTune) ModThreshold(); //Modifies Threshold /* ** initialize the new calendar ** ** if (current calendar at end of bucketArray) then ** place the new calendar at start of bucketArray ** else ** place the new calendar at the end of bucketArray */ if (calFirstSubscript != 0) calInit(0, calNewSize, bucketWidth, lastPriority); else calInit((MAXBUCKETS-calNewSize), calNewSize, bucketWidth, lastPriority); //GetStats(); /* ** move all of the current queue entries from the current calendar ** to the appropriate bucket in the new calendar */ //Smart recopying for 2*calNBuckets==calOldSize if(calBucketWidth==calOldBW && 2*calNBuckets==calOldSize && !FineTune ){ for(i=0;icalNBuckets-1;--i){ myIdx=i-calNBuckets; if (calOld[i].bp) { if(calendar[myIdx].bp){ bucketEntry* queueEntry = calOld[i].bp; while (queueEntry) { nextBucketEntry = queueEntry->next; prevBucketEntry = (bucketEntry *)NULL; curBucketEntry = calendar[myIdx].LBE; while (curBucketEntry && (curBucketEntry->p.priority <= queueEntry->p.priority)) { prevBucketEntry = curBucketEntry; curBucketEntry = curBucketEntry->next; } if (!prevBucketEntry){ calendar[myIdx].bp = queueEntry; } else prevBucketEntry->next = queueEntry; calendar[myIdx].LBE = queueEntry; queueEntry->next = curBucketEntry; queueEntry= nextBucketEntry; } } else{ calendar[myIdx].bp=calOld[i].bp; calendar[myIdx].LBE= calOld[i].bp; } } } calQSize=calOldQSize; } //Smart recopying for calNBuckets==2*calOldSize else if( calBucketWidth==calOldBW && calNBuckets==2*calOldSize && !FineTune){ for(i=0;inext; if((unsigned long int)(curBucketEntry->p.priority/calOldBW)%calNBuckets==i) k=i; else k=myIdx; if(calendar[k].bp){ calendar[k].LBE->next=curBucketEntry; calendar[k].LBE=curBucketEntry; curBucketEntry->next=NULL; } else{ calendar[k].bp=curBucketEntry; calendar[k].LBE=curBucketEntry; curBucketEntry->next=NULL; } curBucketEntry = nextBucketEntry; } } } calQSize=calOldQSize; } //Standard recopying else{ for (i =0; i < calOldSize && calQSize !=calOldQSize; i++) { myIdx=(calOldLB+calOldSize-i)% calOldSize; if (calOld[myIdx].bp) { curBucketEntry = calOld[myIdx].bp; while (curBucketEntry) { nextBucketEntry = curBucketEntry->next; localEnqueue(curBucketEntry); curBucketEntry = nextBucketEntry; } } } //Resetting Stats due to new bucketwidth Qsearch =0; SumQsearch = 0; QOP=0; SumBsearch =0; Bsearch =0 ; BOP=0; } ++rescnt; FineTune=0; return; } /* */ /********************************************************************** */ double bucketNewWidth() /* ** calculate the width to use for buckets **********************************************************************/ { int numSamples; /* number of queue elements to sample */ int localCalLastBucket; /* local copy of calLastBucket */ double localCalBucketTop; /* local copy of calBucketTop */ double localLastPriority; /* local copy of lastPriority */ double localLastBucketTop; /* local copy of lastBucketTop */ #define MAXSAMPLES (1000) bucketEntry * sampleArray[MAXSAMPLES]; /* dequeued events to sample */ double priorities[MAXSAMPLES]; /* priorities from sampled events */ double cumSeparation; /* cumulative separations between events */ double avgSeparation; /* average separation between events */ double twiceAvgSeparation; /* twice the calculated avg separation */ int numSeparations; /* number of separations less than twice avg */ double curSeparation; /* separation between current event pair */ int i; /* loop index */ /* ** decide how many samples to use in calculation of bucketWidth */ if (calQSize < 2) return calBucketWidth; //retain bucket width if bucket width is still good to be used (no overcrowding in 1 or 2 buckets) if(calNBuckets<=SNOOPyOn && calBucketWidth!=1 && calBucketWidth>3 ) if(OccBuck()/(double)calNBuckets>0.2) return calBucketWidth; if (calQSize <= 5) numSamples = calQSize; else numSamples = 5 + calQSize / 10; if (numSamples > 25) numSamples = 25; //Save State Info localCalLastBucket = calLastBucket; localCalBucketTop = calBucketTop; localLastPriority = lastPriority; localLastBucketTop = lastBucketTop; for (i = 0; i < numSamples; i++) { sampleArray[i] = localDequeue(); priorities[i] = (sampleArray[i])->p.priority; } /* ** re-enqueue the events, and restore state info */ for (i = 0; i < numSamples; i++) localEnqueue(sampleArray[i]); //restore State Info calLastBucket = localCalLastBucket; calBucketTop = localCalBucketTop; lastPriority = localLastPriority; lastBucketTop = localLastBucketTop; /* ** calculate the average separation of sampled events */ cumSeparation = (double)0.0; for (i = 0; i < (numSamples - 1); i++) cumSeparation += (priorities[i+1] - priorities[i]); avgSeparation = cumSeparation / (double)(numSamples - 1); /* ** recalculate the average using only separation smaller ** than twice the original average */ twiceAvgSeparation = (double)2.0 * avgSeparation; cumSeparation = (double)0.0; numSeparations = 0; for (i = 0; i < (numSamples - 1); i++) { curSeparation = priorities[i+1] - priorities[i]; if (curSeparation < twiceAvgSeparation && curSeparation >0) { cumSeparation += curSeparation; numSeparations++; } } /* jahn Nov. 28 for example 0 0 0 1000 */ if (numSeparations == 0) avgSeparation = twiceAvgSeparation/2.0; else avgSeparation = cumSeparation / (double)numSeparations; /* Assume that all priorities are integer */ if (avgSeparation < 1) avgSeparation = 1; /* ** return newBucketWidth as three times last calculated average */ return((double)3.0 * avgSeparation); } /* */ /********************************************************************** */ void localEnqueue( bucketEntry* queueEntry) /* ** this Enqueue routine is used internally by the calQ package. ** it enqueues an already allocated bucketEntry into the current ** calendar. **********************************************************************/ // bucketEntry * queueEntry; { unsigned long int virtualBucket; /* bucket index if calendar were infinite */ int actualBucket; /* bucket index for actual calendar */ bucketEntry * curBucketEntry; /* used to traverse entries in bucket */ bucketEntry * prevBucketEntry; /* used to traverse entries in bucket */ /* ** calculate which bucket to insert the bucketEntry into */ virtualBucket = (unsigned long int)(queueEntry->p.priority / calBucketWidth); actualBucket = (int)(virtualBucket % calNBuckets); /* ** insert the bucketEntry into the appropriate calendar bucket */ if (!calendar[actualBucket].bp) { calendar[actualBucket].bp = queueEntry; queueEntry->next = (bucketEntry *)NULL; } else { prevBucketEntry = (bucketEntry *)NULL; curBucketEntry = calendar[actualBucket].bp; while (curBucketEntry && (curBucketEntry->p.priority <= queueEntry->p.priority)) { if (curBucketEntry->p.priority == queueEntry->p.priority && curBucketEntry->p.cid > queueEntry->p.cid){ break; } prevBucketEntry = curBucketEntry; curBucketEntry = curBucketEntry->next; } if (!prevBucketEntry) calendar[actualBucket].bp = queueEntry; else prevBucketEntry->next = queueEntry; queueEntry->next = curBucketEntry; } /* ** increment queue size, but don't worry about resizeing calendar */ calQSize++; } /* */ /********************************************************************** */ bucketEntry *localDequeue() /* ** this Dequeue routine is used internally by the calQ package. ** it dequeues and returns an entire bucketEntry. **********************************************************************/ { bucketEntry * returnBucketEntry; /* ptr to event to dequeue */ int directSearchInitialized; /* has direct search been initialized */ double directSearchPriority=0; /* highest priority event found in ** direct search */ int directSearchBucket=0; /* bucket number containing highest ** priority found in direct search */ long int warpBuckets; /* number of buckets warped during ** direct search */ int i; /* loop index */ /* ** starting from the bucket where the last event was retrieved, ** search for the next event in this year */ for (i = calLastBucket; ; ) { /* ** check the head element of the current bucket (if it exists) */ if ((calendar[i].bp != (bucketEntry *)NULL) && ((calendar[i].bp)->p.priority < calBucketTop)) { /* ** next event has been found ** remove it from the queue */ returnBucketEntry = calendar[i].bp; calendar[i].bp = returnBucketEntry->next; /* ** update position in calendar */ calLastBucket = i; lastPriority = returnBucketEntry->p.priority; lastBucketTop = calBucketTop; calQSize--; return(returnBucketEntry); } else { /* ** advance (circularly) to the next bucket in the calendar */ i++; if (i == calNBuckets) i = 0; calBucketTop += calBucketWidth; /* ** if we've cycled a complete year without finding ** an event, then break out and go to direct search */ if (i == calLastBucket) break; } } /* ** perform direct search through a whole year */ directSearchInitialized = FALSE; for (i = 0; i < calNBuckets; i++) { if (calendar[i].bp) { if (!directSearchInitialized) { directSearchPriority = (calendar[i].bp)->p.priority; directSearchBucket = i; directSearchInitialized = TRUE; } else if ((calendar[i].bp)->p.priority < directSearchPriority) { directSearchPriority = (calendar[i].bp)->p.priority; directSearchBucket = i; } } } /* ** dequeue the event found */ returnBucketEntry = calendar[directSearchBucket].bp; calendar[directSearchBucket].bp = returnBucketEntry->next; /* ** update position in calendar */ warpBuckets = (long int)(directSearchPriority / calBucketWidth) - (long int)(lastPriority / calBucketWidth); calBucketTop = lastBucketTop + (double)warpBuckets * calBucketWidth; lastBucketTop = calBucketTop; calLastBucket = directSearchBucket; lastPriority = directSearchPriority; calQSize--; return(returnBucketEntry); } //to find number of occupied bucket int OccBuck(){ int COUNT=0; for (int i=0;iQOP && QOP>0) myT=QOP+(0.75*(BOP-QOP)); else if(BOP0) myT=BOP+(0.75*(QOP-BOP)); if(myT<2.0) myT=2.0; } /* Long term cost ratio check If breached set SumQSearch, Qsearch, SumBSearch, Bsearch to these long term ratios and return Bool TRUE */ int GetRatio(){ double num=0,denom=0; //temp var for calculation if (!(MAADCR)|| !(MAAECR)){ //if switch not ON return 0; } for(int p=0;p<10;++p){ //sum up all of MA data to num and denom num=num+MAADC[p]; denom=denom+MAAEC[p]; } if(num<10 && denom<10 ||(num==0 || denom==0) ) // if 10 MAEC & MADC <10 don't trigger return 0; if ((num/denom >2.0 || num/denom<0.5)){ //if ratio num/denom >2 or <0.5 trigger SumBsearch=num; SumQsearch=denom; FineTune=1; return 1; } else //all other cases dont trigger MA Algo return 0; } //Print out performance stats void GetStats(){ cout<p.priority / calBucketWidth); actualBucket = (int)(virtualBucket % calNBuckets); cout<p.priority<<","<p.priorityp.priority; nextBucketEntry = curBucketEntry->next; curBucketEntry = nextBucketEntry; } } else cout<next; } curr=calendar[i].bp; if(numEvts>0){ t=curr->p.priority; virtualBucket = (unsigned long int)(t/ calBucketWidth); actualBucket = (int)(virtualBucket % calNBuckets); if(actualBucket!=i) k=1; for(j=0;jnext; if(t>curr->p.priority){ k=1; break; } t=curr->p.priority; virtualBucket = (unsigned long int)(t / calBucketWidth); actualBucket = (int)(virtualBucket % calNBuckets); if(actualBucket!=i) k=1; } } if(k==1){ curr=calendar[i].bp; cout<<"B["<p.priority; virtualBucket = (unsigned long int)(t / calBucketWidth); actualBucket = (int)(virtualBucket % calNBuckets); cout<next; t=curr->p.priority; virtualBucket = (unsigned long int)(t / calBucketWidth); actualBucket = (int)(virtualBucket % calNBuckets); cout<