void requestServeStatic(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); // Rather than call read() to read the file into memory, // which would require that we allocate a buffer, we memory-map the file srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); // put together response sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: CS537 Web Server\r\n", buf); sprintf(buf, "%sContent-Length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // Writes out to the client socket the memory-mapped file Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
/* * serve_static - serve static content */ void serve_static(int connfd, char* filename, struct stat filestat) { int srcfd; char *srcp; char filetype[MAXLINE], body[MAXBUF]; /* get file type */ if (strstr(filename, ".html")) strcpy(filetype, "text/html"); else if (strstr(filename, ".gif")) strcpy(filetype, "text/gif"); else if (strstr(filename, ".jpg")) strcpy(filetype, "text/jpg"); else strcpy(filetype, "text/plain"); /* send response headers to client */ sprintf(body, "HTTP/1.0 200 OK\r\n"); sprintf(body, "%sServer: Tiny Web Server\r\n", body); sprintf(body, "%sContent-length: %d\r\n", body, (int)filestat.st_size); sprintf(body, "%sContent-type: %s\r\n\r\n", body, filetype); Rio_writen(connfd, body, strlen(body)); /* send response body to client */ srcfd = Open(filename, O_RDONLY, 0); /* map the file into a chunk of virtual memory */ srcp = Mmap(NULL, filestat.st_size, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(connfd, srcp, filestat.st_size); Munmap(srcp, filestat.st_size); }
MemMappedFile::~MemMappedFile() { if (_isLocked && 0!=cf_munlock(_pShm, _size)) ;//_THROW(SyscallExecuteError, "Failed to execute cf_munlock !") if(_autoUnmap) Munmap(); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); if(ishttps) { SSL_write(ssl, buf, strlen(buf)); SSL_write(ssl, srcp, filesize); } else { Rio_writen(fd, buf, strlen(buf)); Rio_writen(fd, srcp, filesize); } Munmap(srcp, filesize); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize, int is_head) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); // line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); // line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); // char c; // c = getchar(); // printf("%c\n", c);/*to ignore the EPIPE error!!! */ sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // line:netp:servestatic:endserve if (is_head) { return; // the request is head } /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); // line:netp:servestatic:open srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); // Close(srcfd); // Rio_writen(fd, srcp, filesize); // Munmap(srcp, filesize); // }
void requestServeStatic(request_t request, char *filename, int filesize, thread_t * thread) { int fd = request.connfd; int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; char tmp = 0; int i; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); double time_start_read = get_time(); // Rather than call read() to read the file into memory, // which would require that we allocate a buffer, we memory-map the file srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); // The following code is only needed to help you time the "read" given // that the file is memory-mapped. // This code ensures that the memory-mapped file is brought into memory // from disk. // When you time this, you will see that the first time a client //requests a file, the read is much slower than subsequent requests. for (i = 0; i < filesize; i++) { tmp += *(srcp + i); } double time_end_read = get_time(); request.Stat_req_read = time_end_read - time_start_read; double time_start_write = get_time(); request.Stat_req_complete = time_start_write - request.Stat_req_arrival; sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%s Server: CS537 Web Server\r\n", buf); // CS537: Your statistics go here -- fill in the 0's with something useful! sprintf(buf, "%s Stat-req-arrival: %d\r\n", buf, request.Stat_req_arrival); sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, request.Stat_req_dispatch); sprintf(buf, "%s Stat-req-read: %d\r\n", buf, request.Stat_req_read); sprintf(buf, "%s Stat-req-complete: %d\r\n", buf, request.Stat_req_complete); sprintf(buf, "%s Stat-req-age: %d\r\n", buf, request.Stat_req_age); sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, thread->Stat_thread_id); sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, thread->Stat_thread_count); sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, thread->Stat_thread_static); sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, thread->Stat_thread_dynamic); sprintf(buf, "%s Content-Length: %d\r\n", buf, filesize); sprintf(buf, "%s Content-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // Writes out to the client socket the memory-mapped file Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
void IoWrite::writeFile(const std::string&fileName,int filesSize) { int srcfd; char *srcp; srcfd = Open(const_cast<char*>(fileName.c_str()),O_RDONLY,0); srcp= reinterpret_cast<char *>(Mmap(0,filesSize,PROT_READ,MAP_PRIVATE,srcfd,0)); Close(srcfd); Rio_write(fileDescriptor,srcp,filesSize); Munmap(srcp,filesSize); }
// Release bytes from the back of the mmap int trim_mmap_window(mmap_window *mw,size_t shrink){ if(shrink == 0 || shrink >= mw->maplen){ bitch("Invalid argument (%zu)\n",shrink); return -1; } // FIXME we ought be using Mremap() for performance if(Munmap(mw->mapbase + (mw->maplen - shrink),shrink)){ return -1; } mw->maplen -= shrink; return 0; }
// Release the map in its entirety int release_mmap_window(mmap_window *mw){ int ret = 0; if(mw->maplen){ ret |= Munmap(mw->mapbase,mw->maplen); track_deallocation(); mw->mapbase = MAP_FAILED; mw->maplen = 0; mw->mapoff = 0; } return ret; }
void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; get_filetype(filename, filetype); sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
void HostSys::MmapResetPtr(void* base, size_t size) { // On linux the only way to reset the memory is to unmap and remap it as PROT_NONE. // That forces linux to unload all committed pages and start from scratch. // FIXME: Ideally this code would have some threading lock on it to prevent any other // malloc/free code in the current process from interfering with the operation, but I // can't think of any good way to do that. (generally it shouldn't be a problem in // PCSX2 anyway, since MmapReset is only called when the ps2vm is suspended; so that // pretty well stops all PCSX2 threads anyway). Munmap(base, size); void* result = MmapReservePtr(base, size); pxAssertRel ((uptr)result == (uptr)base, pxsFmt( "Virtual memory decommit failed: memory at 0x%08X -> 0x%08X could not be remapped. " "This is likely caused by multi-thread memory contention.", base, (uptr)base+size )); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); //line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); //line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); //line:netp:servestatic:endserve /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); //line:netp:servestatic:open srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);//line:netp:servestatic:mmap Close(srcfd); //line:netp:servestatic:close Rio_writen(fd, srcp, filesize); //line:netp:servestatic:write Munmap(srcp, filesize); //line:netp:servestatic:munmap }
// Move the map back to the beginning of the underlying object (slide it left) int reset_mmap_window(mmap_window *mw,int fd,int prot){ void *tmp; if(mw->maplen <= 0){ bitch("Invalid arguments (%zu)\n",mw->maplen); return -1; } if(fd >= 0){ tmp = Mmap(mw->mapbase,mw->maplen,prot,mmap_flags(fd) | MAP_FIXED,fd,0); if(tmp != mw->mapbase){ bitch("Invalid MAP_FIXED result (%p, %p)\n",tmp,mw->mapbase); if(tmp != MAP_FAILED){ // FIXME verify it doesn't overlap! Munmap(tmp,mw->maplen); } return -1; } } mw->mapoff = 0; return 0; }
// Slide the map forward over the mapped object int slide_mmap_window(mmap_window *mw,int fd,int prot,size_t delta){ void *tmp; if(delta == 0 || mw->maplen <= 0){ bitch("Invalid arguments (%zu, %zu)\n",delta,mw->maplen); return -1; } // FIXME ought be using remap_file_pages(2) on Linux for performance tmp = Mmap(mw->mapbase,mw->maplen,prot,mmap_flags(fd) | MAP_FIXED, fd,mw->mapoff + delta); if(tmp != mw->mapbase){ bitch("Invalid MAP_FIXED result (%p, %p)\n",tmp,mw->mapbase); if(tmp != MAP_FAILED){ Munmap(tmp,mw->maplen); // FIXME unsafe } track_failloc(); return -1; } mw->mapoff += delta; return 0; }
void requestServeStatic(int fd, char *filename, int filesize, request* req) { int srcfd; char *srcp; char filetype[MAXLINE]; char buf[MAXBUF]; char tmp = 0; int i; long t, read_start, read_end; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); /* Rather than call read() to read the file into memory, which would * require that we allocate a buffer, we memory-map the file */ srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); req->req_age = *(req->req_disp_count)-req->old_disp_count; (*(req->req_disp_count))++; /* The following code is only needed to help you time the "read" given * that the file is memory-mapped. * This code ensures that the memory-mapped file is brought into memory * from disk. */ read_start = getTime(); /* When you time this, you will see that the first time a client * requests a file, the read is much slower than subsequent requests. */ for (i = 0; i < filesize; i++) { tmp += *(srcp+i); } read_end = getTime(); req->req_read = read_end - read_start; /* the request is complete and we store that time */ t = getTime(); req->req_complete = t - req->req_arrival; sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%s Server: CS537 Web Server\r\n", buf); /* CS537: Your statistics go here -- fill in the 0's with something * useful! */ sprintf(buf, "%s Stat-req-arrival: %f\r\n", buf, toMS(req->req_arrival)); sprintf(buf, "%s Stat-req-dispatch: %f\r\n", buf, toMS(req->req_dispatch)); sprintf(buf, "%s Stat-req-read: %f\r\n", buf, toMS(req->req_read)); sprintf(buf, "%s Stat-req-complete: %f\r\n", buf, toMS(req->req_complete)); sprintf(buf, "%s Stat-req-age: %d\r\n", buf, req->req_age); sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, req->t_stats.thread_id); sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, req->t_stats.thread_count); sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, req->t_stats.thread_static); sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, req->t_stats.thread_dynamic); sprintf(buf, "%s Content-Length: %d\r\n", buf, filesize); sprintf(buf, "%s Content-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); /* Writes out to the client socket the memory-mapped file */ Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
/*forward response (lines/headers/body) to client*/ int forward_response(rio_t *rio_server, char *uri, char *resp_buf, int connfd) { char response[MAX_OBJECT_SIZE]; char resp_body[MAX_OBJECT_SIZE]; char resp_line[MAXLINE]; int resp_size = 0; int length = 0; int body_size = 0; int content_len = -1; int cont_size = 0; #if 1 if((web_load(uri, response)) == 0) { printf("Cache hit!\n"); if(rio_writen(connfd, response, sizeof(response)) < 0) { fprintf(stderr, "rio_writen send cache response error\n"); return -1; } memset(response, 0, sizeof(response)); return 0; } #endif /*send reponse line and headers to client*/ while((length = rio_readlineb(rio_server, resp_line, MAXLINE)) > 0) { strcat(response, resp_line); /* send headers to clinet*/ if (rio_writen(connfd, resp_line, length) < 0) { fprintf(stderr, "Error: rio_writen() in forward_response header: %s\n", strerror(errno)); DEBUG_PRINT("Error: rio_writen() in forward_response header\n"); return -1; } /*empty line between headers and body*/ if(strcmp(resp_line, "\r\n") == 0) break; /* get size of response body from response header: Content-Length */ if (strstr(resp_line, "Content-Length: ")) { content_len = parse_num(resp_line); if(content_len < 0) { fprintf(stderr, "Error get Content-Length: %s", resp_line); DEBUG_PRINT("Error get Content-Length\n"); Close(connfd); return -1; } } memset(resp_line, 0, sizeof(resp_line)); resp_size += length; } /*w/o content length in response headers*/ if(content_len == -1) content_len = MAX_OBJECT_SIZE; cont_size = MIN(content_len, MAX_OBJECT_SIZE); /* Send response body to client */ #if DEBUG /* send fake response body */ Rio_writen(connfd, test, strlen(test)); #else while((length = rio_readnb(rio_server, resp_body, cont_size)) > 0) { strcat(response, resp_body); if (rio_writen(connfd, resp_body, length) < 0) { fprintf(stderr, "rio_writen in forward_response body error: %s!", strerror(errno)); DEBUG_PRINT("rio_writen in forward_response body error!"); return -1; } body_size += length; } #endif #if 1 resp_size += body_size; if(resp_size <= MAX_OBJECT_SIZE) { if(web_store(uri, response) < 0) //store response in cache { printf("web_store, cache error!\n"); return -1; } } #endif #if 0 srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); #endif return 0; }