char* readPage(int absolutePageNumber) { retardo(); int page_size = getSwapPagesSize(); char *file_name = getSwapFileName(); FILE *fp = fopen(file_name, "r"); char* page = malloc(sizeof(char) * page_size); int position = absolutePageNumber * page_size; fseek(fp, position, SEEK_SET); fgets(page, page_size + 1, fp); return page; }
void swapOut(struct proc* p) { //write to file char filename[9]; struct file* f; uint i; pte_t* pte=0; getSwapFileName(p, filename); //cprintf("swapout %s %d\n", p->name, p->pid); //release(&inswapper_lk); //acquire(&inswapper_lk); release(&ptable.lock); f = openKernelFile(filename, O_CREATE | O_WRONLY); acquire(&ptable.lock); //cprintf("sfff\n"); //release(&inswapper_lk); if(f == 0) panic("swapout: file open error\n"); //cprintf("swapout: before write\n"); int freed = 0; for (i = 0; i < p->sz; i += PGSIZE) { if (!(pte = walkpgdir(p->pgdir, (void *) i, 0))) panic("swapout: pte should exist\n"); //cprintf("walkpgdir: ok\n"); if (!(*pte & PTE_P)) panic("swapout: page not present\n"); if((*pte & PTE_SHR) || !(*pte & PTE_U)) continue; char *addr=(char*)p2v(PTE_ADDR(*pte)); //acquire(&inswapper_lk); release(&ptable.lock); filewrite(f, addr, PGSIZE); acquire(&ptable.lock); // release(&inswapper_lk); //cprintf("(w=%s)", addr); kfree(addr); *pte = 0; freed++; //cprintf("swapout: wrote %d\n",i/PGSIZE); } //cprintf("swapout freed %d\n", freed); //kfree((char*) p->pgdir); //cprintf("swapout: after write\n"); //freevm(p->pgdir); // acquire(&inswapper_lk); release(&ptable.lock); fileclose(f); acquire(&ptable.lock); // release(&inswapper_lk); }
void swapIn(struct proc* p) { //read from file char filename[9]; struct file* f; uint i; char* buff; pte_t* pte; //cprintf("swapin %s %d\n", p->name, p->pid); getSwapFileName(p, filename); release(&ptable.lock); f = openKernelFile(filename, O_RDWR); //cprintf("1"); acquire(&ptable.lock); if(f == 0) panic("swapin: file open error\n"); f->off = 0; //p->pgdir = setupkvm(); if (!p->pgdir) panic("swapin: setupkvm failed\n"); int recovered = 0; for (i = 0; i < p->sz; i += PGSIZE) { if((pte = walkpgdir(p->pgdir, (char*) i, 0)) == 0){ //cprintf("skip"); //continue; } if(*pte != 0) continue; if (!(buff = kalloc())) panic("swapin: kalloc failed\n"); release(&ptable.lock); fileread(f, buff, PGSIZE); acquire(&ptable.lock); //cprintf("(%s)", buff); if (mappages(p->pgdir, (void*) i, PGSIZE, v2p(buff), PTE_W | PTE_U) < 0) panic("swapin: mappages failed\n"); recovered++; } //cprintf("swapin recovered %d\n", recovered); release(&ptable.lock); fileclose(f); //cprintf("swapin2"); //unlinkKernelFile(filename); acquire(&ptable.lock); //cprintf("swapin3"); }
void setupSwap() { //Traigo datos del archivo de configuracion de Swap y creo un archivo //con las especificaciones de configuracion con /0 log_swap = log_create("/tp-2015-2c-signiorcodigo/swap/log_swap", "SWAP", true, LOG_LEVEL_INFO); int pages_amount = getSwapPagesAmount(); int page_size = getSwapPagesSize(); char *file_name = getSwapFileName(); char command[100]; int total = pages_amount * page_size; remove(file_name); sprintf(command, "dd if=/dev/zero of=%s bs=%d count=1", file_name, total); system(command); //printf("Archivo de swap creado bajo el nombre %s\n",file_name ); }
void writePage(int absolutePageNumber, char *content) { retardo(); int page_size = getSwapPagesSize(); char *file_name = getSwapFileName(); FILE *fp = fopen(file_name, "rb+"); int position = absolutePageNumber * page_size; fseek(fp, position, SEEK_SET); fwrite(content, sizeof(char), strlen(content), fp); fclose(fp); }