void smartPageIn(PPP* active, int numActive, PPP* comingUp, int numComingUp, PPP* available, int numAvailable, Pentry* q) { int i; PPP cur; int used = 0; //try to pagein the active ones for(i = 0; i < numActive; ++i) { cur = active[i]; if(!q[cur.proc].pages[cur.page]) { if(!pagein(cur.proc, cur.page)) { //if we can't pagein any more pages, and we don't have any more available to pageout, then break if(used >= numAvailable) { break; } pageout(available[used].proc, available[used].page); ++used; } } } for(i = 0; i < numComingUp; ++i) { cur = comingUp[i]; if(!q[cur.proc].pages[cur.page]) { if(!pagein(cur.proc, cur.page)) { if(used >= numAvailable) { //same deal as above - nothing else to do now //not as crutial, since these pages aren't needed yet, and may never be needed break; } pageout(available[used].proc, available[used].page); ++used; } } } }
void pageit(Pentry q[MAXPROCESSES]) { // these are globals that only the subroutine can see static int tick=0; // artificial time static int timestamps[MAXPROCESSES][MAXPROCPAGES]; // these are regular dynamic variables on stack int proc,pc,page,oldpage; // select first active process for (proc=0; proc<MAXPROCESSES; proc++) { // if active, then work on it ONLY if (q[proc].active) { pc=q[proc].pc; // program counter for process page = pc/PAGESIZE; // page the program counter needs timestamps[proc][page]=tick; // last access if (!q[proc].pages[page]) { // if page is not there: if (!pagein(proc,page)) { // try to swap in, if this fails: // look at all old pages, swap out any other pages for (oldpage=0; oldpage<q[proc].npages; oldpage++) { // if I find a page that's not equal to the one I want, // swap it out => 100 ticks later, pagein will succeed. if (oldpage!=page && pageout(proc,oldpage)) break; // ^^^^^^^^^^^^^^^^^^^^^ swapout starts // ^^^^^^^^^^^^^ it's not the page I want } } } break; // no more } } tick++; // advance time for next iteration }
void swap_page(Pentry q[MAXPROCESSES], int timestamps[MAXPROCESSES][MAXPROCPAGES], int proc, int page, int current, int hotpage[MAXPROCESSES][MAXPROCPAGES], int prediction[MAXPROCESSES][MAXPROCPAGES]){ int pagetmp, outproc, evictpage, least, hotness; //is page swapped in current++; if(!q[proc].pages[page]){ //page not swapped in if(!pagein(proc,page)){ //failure - select a page to evict //find a default page for(pagetmp = 0; pagetmp < MAXPROCPAGES; pagetmp++){ if(q[proc].pages[pagetmp] && timestamps[proc][pagetmp] > 0){ least = timestamps[proc][pagetmp]; evictpage = pagetmp; outproc = proc; hotness = hotpage[proc][page]; //printf("do have a default\n"); break; } } //look for better - LRU for(pagetmp = 1; pagetmp < MAXPROCPAGES; pagetmp++){ if(!q[proc].pages[pagetmp]) //cant evict page thats not in continue; if(timestamps[proc][pagetmp] > least && !prediction[proc][page]){ least = timestamps[proc][pagetmp]; evictpage = pagetmp; outproc = proc; // hotness = hotpage[proctmp][pagetmp]; // printf("current LRU timestamp = %d\n", least); } if(hotpage[proc][pagetmp] < hotness){ hotness = hotpage[proc][pagetmp]; // evictpage = pagetmp; // outproc = proc; } } //call pageout() if(q[outproc].pages[evictpage]){ //can only evict page thats in pageout(outproc, evictpage); //and if have prediction put it in so its there before needed pagein(proc, page); } } } }
static void zerorange(vlong offset, int len) { int i; vlong ooff; int olen; enum { MinBlock = 4*K, MaxBlock = 8*K }; if(0) if(bufoffset <= offset && offset+len <= bufoffset+buflen){ memset(buf+(offset-bufoffset), 0, len); return; } ooff = bufoffset; olen = buflen; i = offset%MinBlock; if(i+len < MaxBlock){ pagein(offset-i, (len+MinBlock-1)&~(MinBlock-1)); memset(buf+i, 0, len); }else{ pagein(offset-i, MaxBlock); memset(buf+i, 0, MaxBlock-i); offset += MaxBlock-i; len -= MaxBlock-i; while(len >= MaxBlock){ pagein(offset, MaxBlock); memset(buf, 0, MaxBlock); offset += MaxBlock; len -= MaxBlock; } pagein(offset, (len+MinBlock-1)&~(MinBlock-1)); memset(buf, 0, len); } pagein(ooff, olen); }
int main(int argc, char **argv) { int c, i; char *fname; char *infile = NULL; int (*pagein)(const char *, off_t) = pagein_fadvise; pgsz = getpagesize(); while ((c = getopt(argc, argv, "afi:mprst")) != EOF) { switch(c) { case 'a': o_noatime = 1; break; case 'f': pagein = pagein_fadvise; break; case 'i': infile = optarg; break; case 't': o_time = 1; /* XXX not done yet, fall through */ case 'm': case 'p': case 'r': case 's': die("unimplemented option '%c'\n", c); default: usage(argv[0]); } } if (infile) { } else { if (argc < optind + 2) usage(argv[0]); fname = argv[optind++]; for (i = optind; i < argc; i++) { off_t blkno, a, b; parse_intrange(argv[i], &a, &b); for(blkno = a; blkno <= b; blkno++) { pagein(fname, blkno); } } } return 0; }
void pageit(Pentry q[MAXPROCESSES]) { /* This file contains the stub for an LRU pager */ /* You may need to add/remove/modify any part of this file */ /* Static vars */ static int initialized = 0; static int tick = 1; // artificial time static int timestamps[MAXPROCESSES][MAXPROCPAGES]; /* Local vars */ int proctmp; int pagetmp; int pctemp; int proc; /* initialize static vars on first run */ if(!initialized){ for(proctmp=0; proctmp < MAXPROCESSES; proctmp++){ for(pagetmp=0; pagetmp < MAXPROCPAGES; pagetmp++){ timestamps[proctmp][pagetmp] = 0; } } initialized = 1; } for(proc=0; proc<MAXPROCESSES; proc++){ if(q[proc].active){ pctemp=q[proc].pc; pagetmp = pctemp/PAGESIZE; /*If the page is swapped out*/ if(!q[proc].pages[pagetmp]){ /*If we haven't allocated our max already*/ if(!pagein(proc,pagetmp)){ handleLRUSwap(proc,timestamps, tick,q); break; } } timestamps[proc][pagetmp]=tick; } } /* advance time for next pageit iteration */ tick++; }
static void lru_pageit(Pentry q[MAXPROCESSES],int tick) { int proc,pg,evicted; for(proc=0;proc<MAXPROCESSES;proc++) { pg=q[proc].pc/PAGESIZE; timestamps[proc][pg]=tick; if(pagein(proc,pg)) continue; if(pages_alloc(q,proc)<1) continue; lru_page(q,proc,tick,&evicted); pageout(proc,evicted); } }
static void lru_pageit(Pentry q[MAXPROCESSES], uint32_t tick) { int proc, page, state, evicted; for(proc = 0; proc < MAXPROCESSES; proc++) { if(!q[proc].active) /* done if its not active */ continue; page = q[proc].pc/PAGESIZE; /* note this time for future eviction decisions */ timestamps[proc][page] = tick; /* done if the page is already in memory */ if(q[proc].pages[page]) continue; /* the page is not in memory. * if pagein give 1 the page is either * on its way already or we just got it * started, so we are done with this process */ if(pagein(proc, page, &state) ) continue; /* either the page is swapping out or * there are no free physical pages */ if(state == SWAPOUT) continue; /* just have to wait... */ /* there are no free physical pages */ if(pages_alloc(q, proc) < 1) continue; /* must have at least one page to evict */ lru_page(q, proc, tick, &evicted); if(!pageout(proc, evicted) ) { endit(); } } }
void pageit(Pentry q[MAXPROCESSES]) { /* This file contains the stub for an LRU pager */ /* You may need to add/remove/modify any part of this file */ //printf("%ld",processes[0]->pid); /* Static vars */ static int initialized = 0; //static int tick = 1; // artificial time //printf("here\n"); /* Local vars */ int proctmp; int minPageTwo; int secondMinPageOne; int secondMinPageTwo; int pagetmp; int secondMaxPage; int secondsecondMaxPage; int pc; int page; static int procount=0; int min; int pageOne; int predOne; int predTwo; int predThree; int swapRow=0; int swapCol=0; int tmppage; int proc; int pageTwo; int minPage; int i; int minPageOne; /* initialize static vars on first run */ if(!initialized){ for(proctmp=0; proctmp < MAXPROCESSES; proctmp++){ for(pagetmp=0; pagetmp < MAXPROCPAGES; pagetmp++){ timestamps[proctmp][pagetmp] = 0; pageLastRemovedAt[proctmp][pagetmp]=0; numberofpagesout=0; for(i=0; i<2;i++){ betRevlentPage[proctmp][pagetmp][i]=0; } for(i=0;i<MAXPROCPAGES;i++){ revelentPageFreq[proctmp][pagetmp][i]=0; } } lastPageCalled[proctmp]=0; } tick=1; prevtick=0; initialized = 1; } //pager(); for(proc=0; proc <MAXPROCESSES; proc++){ //printf("1\n"); if(q[proc].active){ //printf("2\n"); pc = q[proc].pc; page=pc/PAGESIZE; /*if(procount>=4){ pager(q); }*/ timestamps[proc][page] = tick; tmppage=lastPageCalled[page]; betRevlentPage[proc][tmppage][1]=betRevlentPage[proc][tmppage][0]; betRevlentPage[proc][tmppage][0]=page; predOne=betRevlentPage[proc][page][0]; predTwo=betRevlentPage[proc][predOne][0]; predThree=betRevlentPage[proc][predTwo][0]; revelentPageFreq[proc][tmppage][page]++; lastPageCalled[proc]=page; pageOne=arrayMax(proc,page,&secondMaxPage); pageTwo=arrayMax(proc,pageOne,&secondsecondMaxPage); minPageOne=arrayMin(q,proc,page,&secondMinPageOne); minPageTwo=arrayMin(q,proc,pageOne,&secondMinPageTwo); minPage=lru(q,proc,page); pagein(proc,page); pagein(proc,pageTwo); pagein(proc,pageOne); /*if(secondMaxPage!=-100 && secondMaxPage!=secondMinPageOne){ pagein(proc,secondMaxPage); if(secondMinPageOne!=-100){ pageout(proc, secondMinPageOne); } } if(secondMaxPage!=-100 && secondMaxPage!=secondMinPageTwo){ pagein(proc,secondsecondMaxPage);* if(secondMinPageTwo!=-100){ pageout(proc,secondMinPageTwo); } }*/ //pagein(proc,predThree); //pagein(proc,predTwo); //pagein(proc,predOne); if(!q[proc].pages[page]){ effChecker(proc,page); if(!pagein(proc,page)){ pageout(proc,minPage); pageout(proc,minPageOne); pageout(proc,minPageTwo); if(secondMinPageTwo!=-100){ pageout(proc,secondMinPageTwo); } if(secondMinPageOne!=-100){ pageout(proc, secondMinPageOne); } }else if(!pagein(proc,pageTwo)){ pageout(proc,minPage); pageout(proc,minPageOne); if(secondMinPageTwo!=-100){ pageout(proc,secondMinPageTwo); } if(secondMinPageOne!=-100){ pageout(proc, secondMinPageOne); } }else if(!pagein(proc,pageOne)){ //pageout(proc,minPage); pageout(proc,minPage); pageout(proc,minPageOne); pageout(proc,minPageTwo); //pageout(proc,minPageOne); if(secondMinPageTwo!=-100){ pageout(proc,secondMinPageTwo); } if(secondMinPageOne!=-100){ pageout(proc, secondMinPageOne); } } } }else{//remove all things related to the process if not active for(i=0;i<MAXPROCPAGES;i++){ pageout(proc,i); procount++; } } } /* TODO: Implement LRU Paging */ //pager(q); /* advance time for next pageit iteration */ pager(q); tick++; }
void pageit(Pentry q[MAXPROCESSES]) { /* Static Vars */ static int tick = 0; static int outTestRun = 0; static int inTestRun = 0; static int iterations = 0; /* Local vars */ int testProc = 0; int testPage = 0; int pageinret = -1; int pageoutret = -1; /* All pages are swapped out on start */ if(q[testProc].pages[testPage]){ /* Page is swapped in */ if(!inTestRun){ fprintf(stdout, "%4d - %d:%d is swapped in\n", tick, testProc, testPage); fprintf(stdout, "%4d - q[%d].pages[%d] = %ld\n", tick, testProc, testPage, q[testProc].pages[testPage]); pageinret = pagein(testProc, testPage); fprintf(stdout, "%4d - pagein(%d, %d) returns %d\n", tick, testProc, testPage, pageinret); fprintf(stdout, "%4d - q[%d].pages[%d] = %ld\n", tick, testProc, testPage, q[testProc].pages[testPage]); pageoutret = pageout(testProc, testPage); fprintf(stdout, "%4d - pageout(%d, %d) returns %d\n", tick, testProc, testPage, pageoutret); if(pageoutret){ /* Wait for pageout to complete */ inTestRun = 1; outTestRun = 0; iterations++; } fprintf(stdout, "%4d - q[%d].pages[%d] = %ld\n", tick, testProc, testPage, q[testProc].pages[testPage]); } } else{ /* Page is swapped out */ if(!outTestRun){ fprintf(stdout, "%4d - %d:%d is swapped out\n", tick, testProc, testPage); fprintf(stdout, "%4d - q[%d].pages[%d] = %ld\n", tick, testProc, testPage, q[testProc].pages[testPage]); pageoutret = pageout(testProc, testPage); fprintf(stdout, "%4d - pageout(%d, %d) returns %d\n", tick, testProc, testPage, pageoutret); fprintf(stdout, "%4d - q[%d].pages[%d] = %ld\n", tick, testProc, testPage, q[testProc].pages[testPage]); pageinret = pagein(testProc, testPage); fprintf(stdout, "%4d - pagein(%d, %d) returns %d\n", tick, testProc, testPage, pageinret); if(pageinret){ /* Wait for pagein to complete */ outTestRun = 1; inTestRun = 0; iterations++; }else{ fprintf(stdout, "%4d - pageout in progress...\n", tick); } fprintf(stdout, "%4d - q[%d].pages[%d] = %ld\n", tick, testProc, testPage, q[testProc].pages[testPage]); } } /* Run test for I state change iterations */ if(iterations > MAXITERATIONS){ fprintf(stdout, "API Test Exiting\n"); exit(EXIT_SUCCESS); } tick++; }