void setinp(int argc,char **argv) { sargc = argc; sargv = argv; sargc--; sargv++; if (sargc>0) swapin(); }
void setinp(int argc, char **argv) { sargc = argc; sargv = argv; sargc--; sargv++; if (sargc>0) swapin(); if (pr1403 || utf8 || tlp) nflm = 1; }
void setinp(int argc, char **argv) { sargc = argc; sargv = argv; sargc--; sargv++; if (sargc == 0 || swapin() == 0) { tabin = (Biobuf*)getcore(sizeof(Biobuf), 1); Binit(tabin, 0, OREAD); } }
/* * Called from switchin when we discover that we want to run * a swapped process. As all our processes are the same size * for now this remains fairly simple. */ void swapper(ptptr p) { pagemap_alloc(p); /* May cause a swapout */ #ifdef DEBUG kprintf("Swapping in %x (page %d), utab.ptab %x\n", p, p->p_page, udata.u_ptab); #endif swapin(p); #ifdef DEBUG kprintf("Swapped in %x (page %d), udata.ptab %x\n", p, p->p_page, udata.u_ptab); #endif }
/* * Called from switchin when we discover that we want to run * a swapped process. We let pagemap_alloc cause any needed swap * out of idle processes. */ void swapper(ptptr p) { uint16_t map = p->p_page2; pagemap_alloc(p); /* May cause a swapout. May also destroy the old value of p->page2 */ #ifdef DEBUG kprintf("Swapping in %x (page %d), utab.ptab %x\n", p, p->p_page, udata.u_ptab); #endif swapin(p, map); swapmap_add(map); #ifdef DEBUG kprintf("Swapped in %x (page %d), udata.ptab %x\n", p, p->p_page, udata.u_ptab); #endif }
int get1char(void) { int c; if (backp>backup) c = *--backp; else c=getc(tabin); if (c== EOF) /* EOF */ { if (swapin() ==0) error(gettext("unexpected EOF")); c = getc(tabin); } if (c== '\n') iline++; return(c); }
vaddr_t coremap_allocuserpages(unsigned npages, struct addrspace * as) { //kprintf("before inside cm alloc\n"); spinlock_acquire(&coremap_lock); //kprintf("inside cm alloc\n"); unsigned i = 0, j = 0; if (swap_state == SWAP_STATE_READY && coremap_pages_free == 0) { swapin(npages, as); } // search for n continuous free pages for (i = 0; i < page_count; i++) { if (!cm_isEntryUsed(COREMAP(i))) { for (j = i + 1; j < page_count && j - i < npages; j++) { if (cm_isEntryUsed(COREMAP(j))) { break; } } if (j - i == npages) { unsigned k = i; for (; k < j; k++) { // update the state cm_setEntryUseState(COREMAP(k), true); //cm_setEntryDirtyState(COREMAP(k), true); // let the address space identifier be NULL for now cm_setEntryAddrspaceIdent(COREMAP(k), as); // chunk start would be the first page in the chunk cm_setEntryChunkStart(COREMAP(k), i); } paddr_t output_paddr = cm_getEntryPaddr(i); //kprintf("exiting cm alloc\n"); spinlock_release(&coremap_lock); bzero(PADDR_TO_KVADDR((void* )output_paddr), npages * PAGE_SIZE); coremap_pages_free -= npages; return output_paddr; } } } //kprintf("could not allocate %u\n", npages); spinlock_release(&coremap_lock); return 0; }
char * gets1(char *s, int len) { char *p; int nbl; while(len > 0) { iline++; while ((p = fgets(s,len,tabin))==0) { if (swapin()==0) return((char *)0); } while (*s) s++; s--; if (*s == '\n') { *s-- = '\0'; } else { if (!feof(tabin)) { if (ferror(tabin)) error(strerror(errno)); else error(gettext("Line too long")); } } for(nbl=0; *s == '\\' && s>p; s--) nbl++; if (linstart && nbl % 2) /* fold escaped nl if in table */ { s++; len -= s - p; continue; } break; } return(p); }
/* * Bring back the page from disk into memory by swapping out a victim page if * necessary */ u_int32_t load_page_into_memory(u_int32_t vaddr, pid_t pid) { //panic("VM: hm......right...\n); //Get the chunk containing demanded page (not in memory) from disk u_int32_t chunk = get_spage(vaddr, pid); /* * snatch a entry in page table for this page by swapping out a victim page * if necessary */ u_int32_t paddr = snatch_a_page(); assert(paddr!=0x0); /* * Now, we have a free entry in the page table for this page. So bring * back the page from disk by swapping the chunk into paddr. */ swapin(paddr, chunk); /* * set the attributes */ paddr = SET_VALID(paddr); paddr = SET_SWAPPED(paddr); /* * So, we have swapped in the page into memory. Add the pagetable entry for * this page. */ int spl=splhigh(); assert((chunk & PAGE_FRAME)/PAGE_SIZE < swaparea_size); //add_ppage(vaddr, paddr, swaparea[(chunk & PAGE_FRAME)/PAGE_SIZE].pid, PAGE_CLEAN); add_ppage(vaddr, paddr, pid, PAGE_CLEAN); //Changed by tocurtis splx(spl); return paddr; }
/* * The main loop of the scheduling (swapping) process. * The basic idea is: * see if anyone wants to be swapped in * swap out processes until there is room * swap him in * repeat * The runout flag is set whenever someone is swapped out. Sched sleeps on * it awaiting work. Sched sleeps on runin whenever it cannot find enough * core (by swapping out or otherwise) to fit the selected swapped process. * It is awakened when the core situation changes and in any case once per * second. */ void sched() { register struct proc *rp; struct proc *swapped_out = 0, *in_core = 0; register int out_time, rptime; for (;;) { /* Perform swap-out/swap-in action. */ spl0(); if (in_core) swapout (in_core, X_FREECORE, X_OLDSIZE, X_OLDSIZE); if (swapped_out) swapin (swapped_out); splhigh(); in_core = 0; swapped_out = 0; /* Find user to swap in; of users ready, * select one out longest. */ out_time = -20000; for (rp = allproc; rp; rp = rp->p_nxt) { if (rp->p_stat != SRUN || (rp->p_flag & SLOAD)) continue; rptime = rp->p_time - rp->p_nice * 8; /* * Always bring in parents ending a vfork, * to avoid deadlock */ if (rptime > out_time || (rp->p_flag & SVFPRNT)) { swapped_out = rp; out_time = rptime; if (rp->p_flag & SVFPRNT) break; } } /* If there is no one there, wait. */ if (! swapped_out) { ++runout; //SETVAL(0); sleep ((caddr_t) &runout, PSWP); continue; } //SETVAL(swapped_out->p_pid); /* * Look around for somebody to swap out. * There may be only one non-system loaded process. */ for (rp = allproc; rp != NULL; rp = rp->p_nxt) { if (rp->p_stat != SZOMB && (rp->p_flag & (SSYS | SLOAD)) == SLOAD) { in_core = rp; break; } } if (! in_core) { /* In-core memory is empty. */ continue; } /* * Swap found user out if sleeping interruptibly, or if he has spent at * least 1 second in core and the swapped-out process has spent at * least 2 seconds out. Otherwise wait a bit and try again. */ if (! (in_core->p_flag & SLOCK) && (in_core->p_stat == SSTOP || (in_core->p_stat == SSLEEP && (in_core->p_flag & P_SINTR)) || ((in_core->p_stat == SRUN || in_core->p_stat == SSLEEP) && out_time >= 2 && in_core->p_time + in_core->p_nice >= 1))) { /* Swap out in-core process. */ in_core->p_flag &= ~SLOAD; if (in_core->p_stat == SRUN) remrq (in_core); } else { /* Nothing to swap in/out. */ in_core = 0; swapped_out = 0; ++runin; sleep ((caddr_t) &runin, PSWP); } } }
/* Switches from a thread to main or from main to a thread */ void my_pthread_yield(int priority_thread) { if(num_high_priority_threads==0 && priority_thread==0){ alarm_count=4; return; } if(num_low_priority_threads==0 && priority_thread==1) return; /* If we are in a thread, switch to the main process */ if ( inThread ) { //printf("inThread\n"); /* Switch to the main context */ if(threadList[currentThread].priority ==1) { //printf( "Thread %d yielding the processor...\n", currentThread ); swapcontext( &threadList[currentThread].context, &mainContext ); } if(threadList[currentThread].priority ==0) { threadList[currentThread].priority = 1; num_low_priority_threads++; num_high_priority_threads--; //printf( "Thread %d yielding the processor...\n", currentThread ); swapcontext( &threadList[currentThread].context, &mainContext ); } } /* Else, we are in the main process and we need to dispatch a new thread */ else { if ( numThreads == 0 ) return; /* Saved the state so call the next thread */ if(priority_thread==2) currentThread = (currentThread + 1) % numThreads; else { if(alarm_count==5) currentThread=-1; if (priority_thread==0 && num_high_priority_threads>0) { do { currentThread = (currentThread + 1) % numThreads; }while(threadList[currentThread].priority!=priority_thread); } else { if (priority_thread==1 && num_low_priority_threads>0) { do { currentThread = (currentThread + 1) % numThreads; }while(threadList[currentThread].priority!=priority_thread); } else return; } } inThread = 1; swapcontext( &mainContext, &threadList[ currentThread ].context ); inThread = 0; if(threadList[currentThread].swapped == 1) swapin(); if ( threadList[currentThread].active == 0 ) { printf( "Thread %d is finished. Cleaning up.\n", currentThread ); /* Free the "current" thread's stack */ free( threadList[currentThread].stack ); temp = global_base; while(temp != NULL) { if(temp->owner_thread == currentThread) temp->free = 1; temp = temp->next; } //printf("Freed\n"); /* Swap the last thread with the current, now empty, entry */ -- numThreads; if(threadList[currentThread].priority ==0) num_high_priority_threads--; if(threadList[currentThread].priority ==1) num_low_priority_threads--; if ( currentThread != numThreads ) { threadList[ currentThread ] = threadList[ numThreads ]; } threadList[ numThreads ].active = 0; } } return; }