From a34a7fd467f3223b58fa24e9f67e175f36764f22 Mon Sep 17 00:00:00 2001 From: "Eric B. Munson" Date: Tue, 22 Mar 2022 16:12:38 -0400 Subject: [PATCH 1/4] Standardize formatting Hope this format works for all, the inconsistent one was difficult to read (tabs always folks). Signed-off-by: Eric B. Munson --- new_lebench.c | 931 +++++++++++++++++++++++++++----------------------- 1 file changed, 508 insertions(+), 423 deletions(-) diff --git a/new_lebench.c b/new_lebench.c index 81097f6..55cb04b 100644 --- a/new_lebench.c +++ b/new_lebench.c @@ -26,12 +26,12 @@ #include #define MAX_SIZE 8192 -#define PF_MAX_SIZE 100*4096 +#define PF_MAX_SIZE 100 * 4096 #define LOOP 1000 #define STEP 256 #define PF_STEP 4096 -#define CENT ((MAX_SIZE/STEP)/100) -#define PF_CENT ((PF_MAX_SIZE/PF_STEP)/100) +#define CENT ((MAX_SIZE / STEP) / 100) +#define PF_CENT ((PF_MAX_SIZE / PF_STEP) / 100) #define ADDR_HINT 0x300000000000 @@ -67,35 +67,37 @@ extern ssize_t bp_recvfrom(int socket, void *restrict buffer, size_t length, int void calc_diff(struct timespec *diff, struct timespec *bigger, struct timespec *smaller) { - if (smaller->tv_nsec > bigger->tv_nsec) - { - diff->tv_nsec = 1000000000 + bigger->tv_nsec - smaller->tv_nsec; - diff->tv_sec = bigger->tv_sec - 1 - smaller->tv_sec; - } - else - { - diff->tv_nsec = bigger->tv_nsec - smaller->tv_nsec; - diff->tv_sec = bigger->tv_sec - smaller->tv_sec; - } + if (smaller->tv_nsec > bigger->tv_nsec) + { + diff->tv_nsec = 1000000000 + bigger->tv_nsec - smaller->tv_nsec; + diff->tv_sec = bigger->tv_sec - 1 - smaller->tv_sec; + } + else + { + diff->tv_nsec = bigger->tv_nsec - smaller->tv_nsec; + diff->tv_sec = bigger->tv_sec - smaller->tv_sec; + } } void calc_sum(struct timespec *sum, struct timespec *bigger, struct timespec *smaller) { - sum->tv_sec = 0; - sum->tv_nsec = 0; - if (smaller->tv_nsec >= (1000000000 - bigger->tv_nsec)) - { - bigger->tv_sec = bigger->tv_sec +1; - sum->tv_nsec = smaller->tv_nsec - (1000000000 - bigger->tv_nsec); - sum->tv_sec = bigger->tv_sec + smaller->tv_sec; - } else { - - sum->tv_nsec = bigger->tv_nsec + smaller->tv_nsec; - sum->tv_sec = bigger->tv_sec + smaller->tv_sec; - } + sum->tv_sec = 0; + sum->tv_nsec = 0; + if (smaller->tv_nsec >= (1000000000 - bigger->tv_nsec)) + { + bigger->tv_sec = bigger->tv_sec + 1; + sum->tv_nsec = smaller->tv_nsec - (1000000000 - bigger->tv_nsec); + sum->tv_sec = bigger->tv_sec + smaller->tv_sec; + } + else + { + + sum->tv_nsec = bigger->tv_nsec + smaller->tv_nsec; + sum->tv_sec = bigger->tv_sec + smaller->tv_sec; + } } -void add_diff_to_sum(struct timespec *result, struct timespec* a, struct timespec *b) +void add_diff_to_sum(struct timespec *result, struct timespec *a, struct timespec *b) { struct timespec diff; struct timespec tmp; @@ -107,85 +109,94 @@ void add_diff_to_sum(struct timespec *result, struct timespec* a, struct timespe void calc_average(struct timespec *average, struct timespec *sum, int size) { - average->tv_sec = 0; - average->tv_nsec = 0; - if (size == 0) return; + average->tv_sec = 0; + average->tv_nsec = 0; + if (size == 0) + return; - average->tv_nsec = sum->tv_nsec / size + sum->tv_sec % size * 1000000000 / size; - average->tv_sec = sum->tv_sec / size; + average->tv_nsec = sum->tv_nsec / size + sum->tv_sec % size * 1000000000 / size; + average->tv_sec = sum->tv_sec / size; } //--------------------------------------------------------------------- -void getppid_bench(void){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void getppid_bench(void) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; int l; int loop = 100000; - for(l = 0; l < loop; l++){ + for (l = 0; l < loop; l++) + { clock_gettime(CLOCK_MONOTONIC, &startTime); #ifdef BYPASS bp_getppid(); #else - syscall(SYS_getppid); + syscall(SYS_getppid); #endif - clock_gettime(CLOCK_MONOTONIC,&endTime); - + clock_gettime(CLOCK_MONOTONIC, &endTime); + calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - + return; } -void clock_bench(void){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void clock_bench(void) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; int l; int loop = 100000; - for(l = 0; l < loop; l++){ - clock_gettime(CLOCK_MONOTONIC, &startTime); - clock_gettime(CLOCK_MONOTONIC,&endTime); - + for (l = 0; l < loop; l++) + { + clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &endTime); + calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - return; + return; } -void cpu_bench(void){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void cpu_bench(void) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; int i, l; double start; - double div; + double div; int loop = 1000; - for(l = 0; l < loop; l++){ + for (l = 0; l < loop; l++) + { start = 9903290.789798798; - div = 3232.32; - - clock_gettime(CLOCK_MONOTONIC, &startTime); - for (i = 0; i < 500000; i ++) { - start = start / div; - } - clock_gettime(CLOCK_MONOTONIC,&endTime); - + div = 3232.32; + + clock_gettime(CLOCK_MONOTONIC, &startTime); + for (i = 0; i < 500000; i++) + { + start = start / div; + } + clock_gettime(CLOCK_MONOTONIC, &endTime); + calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - return; + return; } -void write_bench(int file_size){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; - char *buf; +void write_bench(int file_size) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + char *buf; int fd, i, l; #if defined(USE_VMALLOC) @@ -193,32 +204,35 @@ void write_bench(int file_size){ #elif defined(USE_MALLOC) buf = (char *)malloc(sizeof(char) * file_size); #else - buf = (char *)syscall(SYS_mmap, (void *) ADDR_HINT, sizeof(char) * file_size, \ - PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * file_size, + PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif - for (i = 0; i < file_size; i++) { - buf[i] = i%93 + 33; - } - fd = open("test_file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); - if (fd < 0){ - printf("invalid fd in write: %d\n", fd); + for (i = 0; i < file_size; i++) + { + buf[i] = i % 93 + 33; + } + fd = open("test_file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); + if (fd < 0) + { + printf("invalid fd in write: %d\n", fd); exit(0); } - - for(l = 0; l < LOOP*10; l++){ - clock_gettime(CLOCK_MONOTONIC, &startTime); + + for (l = 0; l < LOOP * 10; l++) + { + clock_gettime(CLOCK_MONOTONIC, &startTime); #ifdef BYPASS bp_write(fd, buf, file_size); #else - syscall(SYS_write, fd, buf, file_size); + syscall(SYS_write, fd, buf, file_size); #endif - clock_gettime(CLOCK_MONOTONIC,&endTime); + clock_gettime(CLOCK_MONOTONIC, &endTime); calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - close(fd); + close(fd); #if defined(USE_VMALLOC) vfree(buf); @@ -227,47 +241,51 @@ void write_bench(int file_size){ #else syscall(SYS_munmap, buf, file_size); #endif - - return; + + return; } -void read_bench(int file_size){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; - char *buf; +void read_bench(int file_size) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + char *buf; int fd, l, i; - + #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * file_size); #elif defined(USE_MALLOC) buf = (char *)malloc(sizeof(char) * file_size); #else - buf = (char *)syscall(SYS_mmap, (void *) ADDR_HINT, sizeof(char) * file_size, \ - PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * file_size, + PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif - for (i = 0; i < file_size; i++) { - buf[i] = i%93; - } + for (i = 0; i < file_size; i++) + { + buf[i] = i % 93; + } fd = open("test_file.txt", O_RDONLY); - if (fd < 0) { + if (fd < 0) + { perror("invalid fd in read\n"); exit(0); } - for(l = 0; l < LOOP*10; l++){ - clock_gettime(CLOCK_MONOTONIC, &startTime); + for (l = 0; l < LOOP * 10; l++) + { + clock_gettime(CLOCK_MONOTONIC, &startTime); #ifdef BYPASS bp_read(fd, buf, file_size); #else syscall(SYS_read, fd, buf, file_size); #endif - clock_gettime(CLOCK_MONOTONIC,&endTime); + clock_gettime(CLOCK_MONOTONIC, &endTime); calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - - close(fd); + + close(fd); #if defined(USE_VMALLOC) vfree(buf); @@ -277,299 +295,335 @@ void read_bench(int file_size){ syscall(SYS_munmap, buf, file_size); #endif - return; + return; } #define sock "./my_sock" -void send_bench(int msg_size){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void send_bench(int msg_size) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; // need to set affinity of parent and child to different cpus - int retval, forkId, status, l; - int fds1[2], fds2[2]; - char w = 'b', r; + int retval, forkId, status, l; + int fds1[2], fds2[2]; + char w = 'b', r; char *buf; - struct sockaddr_un server_addr; - + struct sockaddr_un server_addr; + int recvbuff, retsock, sendbuff, newbuff; socklen_t optlen = sizeof(recvbuff); - + cpu_set_t cpuset; - int prio; + int prio; - memset(&server_addr, 0, sizeof(struct sockaddr_un)); - server_addr.sun_family = AF_UNIX; + memset(&server_addr, 0, sizeof(struct sockaddr_un)); + server_addr.sun_family = AF_UNIX; + + strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1); - strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1); - // create a buffer (this needs to be mmap) #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * msg_size); #elif defined(USE_MALLOC) - buf = (char *)malloc(sizeof(char) * msg_size); + buf = (char *)malloc(sizeof(char) * msg_size); #else - buf = (char *)syscall(SYS_mmap, (void *) ADDR_HINT, sizeof(char) * msg_size, \ - PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * msg_size, + PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif // write character 'a' in the buffer - for (int i = 0; i < msg_size; i++) { + for (int i = 0; i < msg_size; i++) + { buf[i] = 'a'; } - for(l = 0; l < LOOP; l++){ + for (l = 0; l < LOOP; l++) + { retval = pipe(fds1); - if (retval != 0) printf("[error] failed to open pipe1.\n"); - retval = pipe(fds2); - if (retval != 0) printf("[error] failed to open pipe1.\n"); - + if (retval != 0) + printf("[error] failed to open pipe1.\n"); + retval = pipe(fds2); + if (retval != 0) + printf("[error] failed to open pipe1.\n"); + forkId = fork(); - - if (forkId < 0) { + + if (forkId < 0) + { printf("[error] fork failed.\n"); return; } - if (forkId == 0) { - close(fds1[0]); // close the read end of pipe 1 - close(fds2[1]); // close the write end of pipe 2 - - int fd_server = socket(AF_UNIX, SOCK_STREAM, 0); // create a socket for server - if (fd_server < 0) printf("[error] failed to open server socket.\n"); - - // bind the socket to a filename so communication can occur - retval = bind(fd_server, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_un)); - if (retval == -1) printf("[error] failed to bind.\n"); - - // Listen on the socket - retval = listen(fd_server, 10); - if (retval == -1) printf("[error] failed to listen.\n"); - if (DEBUG) printf("Waiting for connection\n"); - - // Write to pipe 1 to let parent know we are listening - write(fds1[1], &w, 1); - - // wait for connection - int fd_connect = accept(fd_server, (struct sockaddr *)0, - (socklen_t *)0); - if (DEBUG) printf("Connection accepted.\n"); - - // Read update from parent on pipe 2 - read(fds2[0], &r, 1); - - // remove sockets and close file descriptors and pipes - remove(sock); - close(fd_server); - close(fd_connect); - close(fds1[1]); - close(fds2[0]); - - // job done, time to exit + if (forkId == 0) + { + close(fds1[0]); // close the read end of pipe 1 + close(fds2[1]); // close the write end of pipe 2 + + int fd_server = socket(AF_UNIX, SOCK_STREAM, 0); // create a socket for server + if (fd_server < 0) + printf("[error] failed to open server socket.\n"); + + // bind the socket to a filename so communication can occur + retval = bind(fd_server, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un)); + if (retval == -1) + printf("[error] failed to bind.\n"); + + // Listen on the socket + retval = listen(fd_server, 10); + if (retval == -1) + printf("[error] failed to listen.\n"); + if (DEBUG) + printf("Waiting for connection\n"); + + // Write to pipe 1 to let parent know we are listening + write(fds1[1], &w, 1); + + // wait for connection + int fd_connect = accept(fd_server, (struct sockaddr *)0, + (socklen_t *)0); + if (DEBUG) + printf("Connection accepted.\n"); + + // Read update from parent on pipe 2 + read(fds2[0], &r, 1); + + // remove sockets and close file descriptors and pipes + remove(sock); + close(fd_server); + close(fd_connect); + close(fds1[1]); + close(fds2[0]); + + // job done, time to exit exit(0); - } else { - close(fds1[1]); // close the write end of pipe 1 - close(fds2[0]); // close the read end of pipe 2 - - // Wait for update from child - read(fds1[0], &r, 1); - - // create socket - int fd_client = socket(AF_UNIX, SOCK_STREAM, 0); - if (fd_client < 0) printf("[error] failed to open client socket.\n"); - - // connect to child address - retval = connect(fd_client, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_un)); - if (retval == -1) printf("[error] failed to connect.\n"); - - // send the buffer over to child (for warm-up) - retval = send(fd_client, buf, msg_size, MSG_DONTWAIT); - - // send buffer over to child and measure latency - clock_gettime(CLOCK_MONOTONIC,&startTime); + } + else + { + close(fds1[1]); // close the write end of pipe 1 + close(fds2[0]); // close the read end of pipe 2 + + // Wait for update from child + read(fds1[0], &r, 1); + + // create socket + int fd_client = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd_client < 0) + printf("[error] failed to open client socket.\n"); + + // connect to child address + retval = connect(fd_client, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un)); + if (retval == -1) + printf("[error] failed to connect.\n"); + + // send the buffer over to child (for warm-up) + retval = send(fd_client, buf, msg_size, MSG_DONTWAIT); + + // send buffer over to child and measure latency + clock_gettime(CLOCK_MONOTONIC, &startTime); #ifdef BYPASS - retval = bp_sendto(fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); + retval = bp_sendto(fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); #else - retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); + retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); #endif - clock_gettime(CLOCK_MONOTONIC,&endTime); - - if (retval == -1) { + clock_gettime(CLOCK_MONOTONIC, &endTime); + + if (retval == -1) + { printf("[error %d] failed to send. %s\n", errno, strerror(errno)); } - + calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); // update child so clean-up can happen - write(fds2[1], &w, 1); - - // do cleanup your-self - close(fd_client); - close(fds1[0]); - close(fds2[1]); - - wait(&status); + write(fds2[1], &w, 1); + + // do cleanup your-self + close(fd_client); + close(fds1[0]); + close(fds2[1]); + + wait(&status); } } #if defined(USE_VMALLOC) vfree(buf); #elif defined(USE_MALLOC) - free(buf); + free(buf); #else syscall(SYS_munmap, buf, msg_size); #endif - return; + return; } -void recv_bench(int msg_size){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void recv_bench(int msg_size) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; // need to set affinity of parent and child to different cpus - int retval, forkId, status, l; - int fds1[2], fds2[2]; - char w = 'b', r; + int retval, forkId, status, l; + int fds1[2], fds2[2]; + char w = 'b', r; char *buf; - struct sockaddr_un server_addr; - + struct sockaddr_un server_addr; + int recvbuff, retsock, sendbuff, newbuff; socklen_t optlen = sizeof(recvbuff); cpu_set_t cpuset; - int prio; + int prio; - if (DEBUG) printf("recv_bench(%d)\n", msg_size); + if (DEBUG) + printf("recv_bench(%d)\n", msg_size); - memset(&server_addr, 0, sizeof(struct sockaddr_un)); - server_addr.sun_family = AF_UNIX; + memset(&server_addr, 0, sizeof(struct sockaddr_un)); + server_addr.sun_family = AF_UNIX; + + strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1); - strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1); - - // create a buffer (this needs to be mmap) #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * msg_size); #elif defined(USE_MALLOC) - buf = (char *)malloc(sizeof(char) * msg_size); + buf = (char *)malloc(sizeof(char) * msg_size); #else - buf = (char *)syscall(SYS_mmap, (void *) ADDR_HINT, sizeof(char) * msg_size, \ - PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * msg_size, + PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif - - for(l = 0; l < LOOP; l++){ + + for (l = 0; l < LOOP; l++) + { retval = pipe(fds1); - if (retval != 0) printf("[error] failed to open pipe1.\n"); - retval = pipe(fds2); - if (retval != 0) printf("[error] failed to open pipe1.\n"); + if (retval != 0) + printf("[error] failed to open pipe1.\n"); + retval = pipe(fds2); + if (retval != 0) + printf("[error] failed to open pipe1.\n"); forkId = fork(); - - if (forkId < 0) { + + if (forkId < 0) + { printf("[error] fork failed.\n"); return; } - if (forkId > 0) { // parent - close(fds1[0]); // close the read end of pipe 1 - close(fds2[1]); // close the write end of pipe 2 - + if (forkId > 0) + { // parent + close(fds1[0]); // close the read end of pipe 1 + close(fds2[1]); // close the write end of pipe 2 + // create a socket for server - int fd_server = socket(AF_UNIX, SOCK_STREAM, 0); - if (fd_server < 0) printf("[error] failed to open server socket.\n"); - - // bind the socket to a filename so communication can occur - retval = bind(fd_server, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_un)); - if (retval == -1) printf("[error] failed to bind.\n"); - - // Listen on the socket - retval = listen(fd_server, 10); - if (retval == -1) printf("[error] failed to listen.\n"); - if (DEBUG) printf("Waiting for connection\n"); - - // Write to pipe 1 to let parent know we are listening - write(fds1[1], &w, 1); - - // wait for connection - int fd_connect = accept(fd_server, (struct sockaddr *)0, - (socklen_t *)0); - if (DEBUG) printf("Connection accepted.\n"); - - // Read update from parent on pipe 2 - read(fds2[0], &r, 1); - + int fd_server = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd_server < 0) + printf("[error] failed to open server socket.\n"); + + // bind the socket to a filename so communication can occur + retval = bind(fd_server, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un)); + if (retval == -1) + printf("[error] failed to bind.\n"); + + // Listen on the socket + retval = listen(fd_server, 11); + if (retval == -1) + printf("[error] failed to listen.\n"); + if (DEBUG) + printf("Waiting for connection\n"); + + // Write to pipe 1 to let parent know we are listening + write(fds1[1], &w, 1); + + // wait for connection + int fd_connect = accept(fd_server, (struct sockaddr *)0, + (socklen_t *)0); + if (DEBUG) + printf("Connection accepted.\n"); + + // Read update from parent on pipe 2 + read(fds2[0], &r, 1); + // recv data from child (for warm-up) - retval = recv(fd_connect, buf, msg_size, MSG_DONTWAIT); - - // recv data from child and measure latency - clock_gettime(CLOCK_MONOTONIC,&startTime); + retval = recv(fd_connect, buf, msg_size, MSG_DONTWAIT); + + // recv data from child and measure latency + clock_gettime(CLOCK_MONOTONIC, &startTime); #ifdef BYPASS - retval = bp_recvfrom(fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL); + retval = bp_recvfrom(fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL); #else retval = syscall(SYS_recvfrom, fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL); #endif - clock_gettime(CLOCK_MONOTONIC,&endTime); + clock_gettime(CLOCK_MONOTONIC, &endTime); - if (retval == -1) { + if (retval == -1) + { printf("[error %d] failed to send. %s\n", errno, strerror(errno)); } calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); - + // update child so clean-up can happen - write(fds1[1], &w, 1); - - // remove sockets and close file descriptors and pipes - remove(sock); - close(fd_server); - close(fd_connect); - close(fds1[1]); - close(fds2[0]); - - wait(&status); - } else { - close(fds1[1]); // close the write end of pipe 1 - close(fds2[0]); // close the read end of pipe 2 - - // Wait for update from parent - read(fds1[0], &r, 1); - - // create socket - int fd_client = socket(AF_UNIX, SOCK_STREAM, 0); - if (fd_client < 0) printf("[error] failed to open client socket.\n"); - - // connect to parent address - retval = connect(fd_client, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_un)); - if (retval == -1) printf("[error] failed to connect.\n"); - - // write character 'a' in the buffer - for (int i = 0; i < msg_size; i++) { - buf[i] = i%93; - } - + write(fds1[1], &w, 1); + + // remove sockets and close file descriptors and pipes + remove(sock); + close(fd_server); + close(fd_connect); + close(fds1[1]); + close(fds2[0]); + + wait(&status); + } + else + { + close(fds1[1]); // close the write end of pipe 1 + close(fds2[0]); // close the read end of pipe 2 + + // Wait for update from parent + read(fds1[0], &r, 1); + + // create socket + int fd_client = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd_client < 0) + printf("[error] failed to open client socket.\n"); + + // connect to parent address + retval = connect(fd_client, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un)); + if (retval == -1) + printf("[error] failed to connect.\n"); + + // write character 'a' in the buffer + for (int i = 0; i < msg_size; i++) + { + buf[i] = i % 93; + } + // send data over to parent + one more copy for warm up - for(l = 0; l < 2; l++){ + for (l = 0; l < 2; l++) + { retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); - - if (retval == -1) { - printf("[error %d] failed to send. %s\n", errno, strerror(errno)); - } + + if (retval == -1) + { + printf("[error %d] failed to send. %s\n", errno, strerror(errno)); + } } - + // tell parent it should start reading - write(fds2[1], &w, 1); - - // wait for parent - read(fds1[0], &r, 1); - - // do cleanup your-self - close(fd_client); - close(fds1[0]); - close(fds2[1]); - - // job done, time to exit + write(fds2[1], &w, 1); + + // wait for parent + read(fds1[0], &r, 1); + + // do cleanup your-self + close(fd_client); + close(fds1[0]); + close(fds2[1]); + + // job done, time to exit exit(0); } } @@ -577,40 +631,48 @@ void recv_bench(int msg_size){ #if defined(USE_VMALLOC) vfree(buf); #elif defined(USE_MALLOC) - free(buf); + free(buf); #else syscall(SYS_munmap, buf, msg_size); #endif - return; + return; } struct timespec *forkTime; -void fork_bench(void){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void fork_bench(void) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; forkTime = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); int forkId, l, status; - - for(l = 0; l < LOOP; l++){ + + for (l = 0; l < LOOP; l++) + { clock_gettime(CLOCK_MONOTONIC, &startTime); forkId = fork(); - if (forkId == 0){ + if (forkId == 0) + { clock_gettime(CLOCK_MONOTONIC, forkTime); exit(0); - } else if (forkId > 0){ - clock_gettime(CLOCK_MONOTONIC,&endTime); + } + else if (forkId > 0) + { + clock_gettime(CLOCK_MONOTONIC, &endTime); wait(&status); - } else { - printf("[error] fork failed.\n"); fflush(stdout); + } + else + { + printf("[error] fork failed.\n"); + fflush(stdout); } calc_diff(&diffTime, &endTime, &startTime); calc_diff(&aveTime, forkTime, &startTime); - fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec, aveTime.tv_sec, aveTime.tv_nsec); + fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec, aveTime.tv_sec, aveTime.tv_nsec); fflush(fp); } - + munmap(forkTime, sizeof(struct timespec)); return; } @@ -619,109 +681,119 @@ struct timespec *threadTime; void *thrdfnc(void *args) { - clock_gettime(CLOCK_MONOTONIC,threadTime); + clock_gettime(CLOCK_MONOTONIC, threadTime); pthread_exit(0); } -void thread_bench(void){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; +void thread_bench(void) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; int l, retval; pthread_t newThrd; - + threadTime = (struct timespec *)malloc(sizeof(struct timespec)); - - for(l = 0; l < LOOP; l++){ - clock_gettime(CLOCK_MONOTONIC, &startTime); - retval = pthread_create (&newThrd, NULL, thrdfnc, NULL); - clock_gettime(CLOCK_MONOTONIC,&endTime); - - pthread_join(newThrd,NULL); - + + for (l = 0; l < LOOP; l++) + { + clock_gettime(CLOCK_MONOTONIC, &startTime); + retval = pthread_create(&newThrd, NULL, thrdfnc, NULL); + clock_gettime(CLOCK_MONOTONIC, &endTime); + + pthread_join(newThrd, NULL); + calc_diff(&diffTime, &endTime, &startTime); calc_diff(&aveTime, threadTime, &startTime); - fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec, aveTime.tv_sec, aveTime.tv_nsec); + fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec, aveTime.tv_sec, aveTime.tv_nsec); fflush(fp); } - + free(threadTime); return; } -void pagefault_bench(int file_size){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; - char *buf; +void pagefault_bench(int file_size) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + char *buf; int fd, l, i; FILE *fp2; char *addr; char a[file_size]; - for(l = 0; l < LOOP; l++){ - fd = open("tmp_file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); - if (fd < 0){ - perror("invalid fd in write\n"); + for (l = 0; l < LOOP; l++) + { + fd = open("tmp_file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); + if (fd < 0) + { + perror("invalid fd in write\n"); exit(0); } - addr = (char *)syscall(SYS_mmap, (void *) ADDR_HINT, file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + addr = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); clock_gettime(CLOCK_MONOTONIC, &startTime); i = 0; - while (i < file_size) { - addr[i] = i%93 + 33; + while (i < file_size) + { + addr[i] = i % 93 + 33; i = i + 4096; - //i++; - } - clock_gettime(CLOCK_MONOTONIC,&endTime); - + // i++; + } + clock_gettime(CLOCK_MONOTONIC, &endTime); + syscall(SYS_write, fd, addr, file_size); - + close(fd); syscall(SYS_munmap, addr, file_size); - + calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - return; + return; } -void stack_pagefault_bench(int file_size){ - struct timespec startTime = { 0, 0 }, endTime = { 0, 0 }; - struct timespec diffTime = { 0, 0 }, aveTime = { 0, 0 }; - char *buf; +void stack_pagefault_bench(int file_size) +{ + struct timespec startTime = {0, 0}, endTime = {0, 0}; + struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + char *buf; int fd, l, i; FILE *fp2; char *addr; char a[file_size]; - for(l = 0; l < LOOP; l++){ - fd = open("tmp_file.txt", O_CREAT | O_WRONLY); - if (fd < 0){ - printf("invalid fd in write: %d\n", fd); + for (l = 0; l < LOOP; l++) + { + fd = open("tmp_file.txt", O_CREAT | O_WRONLY); + if (fd < 0) + { + printf("invalid fd in write: %d\n", fd); exit(0); } - addr = (char*)alloca(file_size * sizeof(long)); - + addr = (char *)alloca(file_size * sizeof(long)); + clock_gettime(CLOCK_MONOTONIC, &startTime); i = 0; - while (i < file_size) { - addr[i] = i%93 + 33; + while (i < file_size) + { + addr[i] = i % 93 + 33; i = i + 4096; - } - clock_gettime(CLOCK_MONOTONIC,&endTime); - + } + clock_gettime(CLOCK_MONOTONIC, &endTime); + syscall(SYS_write, fd, addr, file_size); - + close(fd); calc_diff(&diffTime, &endTime, &startTime); fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); fflush(fp); } - return; + return; } //--------------------------------------------------------------------- @@ -731,29 +803,31 @@ extern void set_bypass_limit(int val); extern void set_bypass_syscall(int val); #endif -int main(void){ +int main(void) +{ int file_size, pf_size, fd_count, retval; int i = 0, percentage = 0; void *addr; cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(CPU1, &set); - retval = sched_setaffinity(getpid(), sizeof(set), &set); - if (retval == -1) printf("[error] failed to set processor affinity.\n"); - retval = setpriority(PRIO_PROCESS, 0, -20); - if (retval == -1) printf("[error] failed to set process priority.\n"); - + CPU_ZERO(&set); + CPU_SET(CPU1, &set); + retval = sched_setaffinity(getpid(), sizeof(set), &set); + if (retval == -1) + printf("[error] failed to set processor affinity.\n"); + retval = setpriority(PRIO_PROCESS, 0, -20); + if (retval == -1) + printf("[error] failed to set process priority.\n"); remove("test_file.txt"); remove("tmp_file.txt"); - + #ifdef BYPASS - //set_bypass_limit(50); - //set_bypass_syscall(1); + // set_bypass_limit(50); + // set_bypass_syscall(1); #endif - -//************************************* + + //************************************* #ifdef REF_TEST printf("Starting reference benchmarks\n"); @@ -769,7 +843,7 @@ int main(void){ fflush(fp); cpu_bench(); fclose(fp); - + fp = fopen("./new_lebench_getppid.csv", "w"); fprintf(fp, "Sr,latency\n"); fflush(fp); @@ -778,7 +852,7 @@ int main(void){ printf("Reference benchmarks done\n"); #endif -//************************************* + //************************************* #ifdef THREAD_TEST printf("Running Thread Create benchmarks\n"); @@ -787,15 +861,15 @@ int main(void){ fprintf(fp, "Sr,LatencyParent,LatencyChild\n"); fflush(fp); - + thread_bench(); - + printf("Running thread test 100 %% done\n"); fflush(stdout); fclose(fp); -#endif +#endif -//************************************* + //************************************* #ifdef FORK_TEST fp = fopen("./new_lebench_fork.csv", "w"); @@ -804,13 +878,13 @@ int main(void){ fflush(fp); fork_bench(); - + printf("Running fork test 100 %% done\n"); fflush(stdout); fclose(fp); -#endif +#endif -//************************************* + //************************************* #ifdef SEND_TEST fp = fopen("./new_lebench_send.csv", "w"); @@ -821,22 +895,24 @@ int main(void){ send_bench(file_size); file_size = 0; i = 0; - while(file_size < MAX_SIZE){ + while (file_size < MAX_SIZE) + { file_size = file_size + STEP; send_bench(file_size); i++; - if(i > CENT){ + if (i > CENT) + { i = 0; - percentage = (file_size*100)/MAX_SIZE; + percentage = (file_size * 100) / MAX_SIZE; printf("Running send test %d %% done\n", percentage); fflush(stdout); } } fclose(fp); -#endif +#endif -//************************************* + //************************************* #ifdef RECV_TEST fp = fopen("./new_lebench_recv.csv", "w"); @@ -847,22 +923,24 @@ int main(void){ recv_bench(file_size); file_size = 0; i = 0; - while(file_size < MAX_SIZE){ + while (file_size < MAX_SIZE) + { file_size = file_size + STEP; recv_bench(file_size); i++; - if(i > CENT){ + if (i > CENT) + { i = 0; - percentage = (file_size*100)/MAX_SIZE; + percentage = (file_size * 100) / MAX_SIZE; printf("Running recv test %d %% done\n", percentage); fflush(stdout); } } fclose(fp); -#endif +#endif -//************************************* + //************************************* #ifdef WRITE_TEST fp = fopen("./new_lebench_write.csv", "w"); @@ -873,93 +951,100 @@ int main(void){ write_bench(file_size); file_size = 0; i = 0; - while(file_size < MAX_SIZE){ + while (file_size < MAX_SIZE) + { file_size = file_size + STEP; write_bench(file_size); i++; - if(i > CENT){ + if (i > CENT) + { i = 0; - percentage = (file_size*100)/MAX_SIZE; + percentage = (file_size * 100) / MAX_SIZE; printf("Running write test %d %% done\n", percentage); fflush(stdout); } } fclose(fp); -#endif +#endif + + //************************************* -//************************************* - #ifdef READ_TEST fp = fopen("./new_lebench_read.csv", "w"); - + fprintf(fp, "Sr,Size,Latency\n"); fflush(fp); file_size = 1; read_bench(file_size); file_size = 0; i = 0; - while(file_size < MAX_SIZE){ + while (file_size < MAX_SIZE) + { file_size = file_size + STEP; read_bench(file_size); i++; - if(i > CENT){ + if (i > CENT) + { i = 0; - percentage = (file_size*100)/MAX_SIZE; + percentage = (file_size * 100) / MAX_SIZE; printf("Running read test %d %% done\n", percentage); fflush(stdout); } } fclose(fp); -#endif +#endif + + //************************************* -//************************************* - #ifdef PF_TEST fp = fopen("./new_lebench_pagefault.csv", "w"); - + fprintf(fp, "Sr,Size,Latency\n"); fflush(fp); pf_size = 0; i = 0; - while(pf_size < PF_MAX_SIZE){ + while (pf_size < PF_MAX_SIZE) + { pf_size = pf_size + PF_STEP; pagefault_bench(pf_size); i++; - if(i > PF_CENT){ + if (i > PF_CENT) + { i = 0; - percentage = (pf_size*100)/(PF_MAX_SIZE); + percentage = (pf_size * 100) / (PF_MAX_SIZE); printf("Running pagefault test %d %% done\n", percentage); fflush(stdout); } } fclose(fp); -#endif - -//************************************* +#endif + + //************************************* #ifdef ST_PF_TEST fp = fopen("./new_lebench_stackpagefault.csv", "w"); - + fprintf(fp, "Sr,Size,Latency\n"); fflush(fp); pf_size = 0; i = 0; - while(pf_size < PF_MAX_SIZE){ + while (pf_size < PF_MAX_SIZE) + { pf_size = pf_size + PF_STEP; pagefault_bench(pf_size); i++; - if(i > PF_CENT){ + if (i > PF_CENT) + { i = 0; - percentage = (pf_size*100)/(PF_MAX_SIZE); + percentage = (pf_size * 100) / (PF_MAX_SIZE); printf("Running stack pagefault test %d %% done\n", percentage); fflush(stdout); } } fclose(fp); -#endif - +#endif } From 46526341d37cd33fd140670adb68545b3733d59b Mon Sep 17 00:00:00 2001 From: "Eric B. Munson" Date: Tue, 22 Mar 2022 16:48:46 -0400 Subject: [PATCH 2/4] Add select test The original lebench includes this test and it will be useful to Symbiote, add it back. Note that this test departs from the method used by the others for recording run times. Instead of doing the file I/O in the test loop, it collects the results in a large array and then writes them out after the test iterations are finished. Signed-off-by: Eric B. Munson --- new_lebench.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/new_lebench.c b/new_lebench.c index 55cb04b..d9d2fd8 100644 --- a/new_lebench.c +++ b/new_lebench.c @@ -39,6 +39,13 @@ FILE *fp; +struct Record +{ + size_t size; + struct timespec start; + struct timespec end; +}; + //--------------------------------------------------------------------- #ifdef USE_VMALLOC extern void *vmalloc(unsigned long size); @@ -796,6 +803,76 @@ void stack_pagefault_bench(int file_size) return; } +static void select_bench(size_t fd_count, int iters) +{ + struct Record *runs; + int *fds; + fd_set rfds; + struct timeval timeout; + int maxFd = 0; + + FD_ZERO(&rfds); + timeout.tv_sec = 0; + timeout.tv_usec = 0; + +#if defined(USE_VMALLOC) + fds = (int *)vmalloc(sizeof(int) * fd_count); + runs = (struct Record *)vmalloc(sizeof(struct Record) * iters); +#elif defined(USE_MALLOC) + fds = (int *)malloc(sizeof(int) * fd_count); + runs = (struct Record *)malloc(sizeof(struct Record) * iters); +#else + fds = (int *)mmap(NULL, sizeof(int) * fd_count, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * iters, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +#endif + + for (size_t i = 0; i < fd_count; i++) + { + fds[i] = socket(AF_INET, SOCK_STREAM, 0); + if (fds[i] < 0) + { + i--; + continue; + } + if (fds[i] > maxFd) + maxFd = fds[i]; + FD_SET(fds[i], &rfds); + } + + memset(runs, 0, sizeof(struct Record) * iters); + for (int i = 0; i < iters; i++) + { + clock_gettime(CLOCK_MONOTONIC, &runs[i].start); + syscall(SYS_select, maxFd + 1, &rfds, NULL, NULL, &timeout); + clock_gettime(CLOCK_MONOTONIC, &runs[i].end); + } + + for (size_t i = 0; i < fd_count; i++) + { + close(fds[i]); + } + + for (int i = 0; i < iters; i++) + { + struct timespec diff; + calc_diff(&diff, &runs[i].end, &runs[i].start); + fprintf(fp, "%d,%ld,%ld.%09ld\n", i, fd_count, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); + +#if defined(USE_VMALLOC) + vfree(fds); + vfree(runs); +#elif defined(USE_MALLOC) + free(fds); + free(runs); +#else + munmap(fds, sizeof(int) * fd_count); + munmap(runs, sizeof(struct Record) * iters); +#endif +} //--------------------------------------------------------------------- #ifdef BYPASS @@ -1047,4 +1124,19 @@ int main(void) fclose(fp); #endif + +#ifdef SELECT_TEST + fp = fopen("./new_lebench_select.csv", "w"); + fprintf(fp, "Index,Size,Latency\n"); + + printf("Running select test small\n"); + fflush(stdout); + select_bench(10, LOOP); + + printf("Running select test large\n"); + fflush(stdout); + select_bench(1000, LOOP); + + fclose(fp); +#endif } From 3d22cac7aebf58bb271d883218ee7381bf65b7f3 Mon Sep 17 00:00:00 2001 From: "Eric B. Munson" Date: Wed, 23 Mar 2022 08:33:17 -0400 Subject: [PATCH 3/4] Remove run output from benchmark loop File IO is heavy weight and might introduce jitter into our measurements. Instead of doing the file IO to report individual results, store the start and end time for each run in a structure. Then caclulate the run time and write out each line after the test is completed. Signed-off-by: Eric B. Munson --- new_lebench.c | 399 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 264 insertions(+), 135 deletions(-) diff --git a/new_lebench.c b/new_lebench.c index d9d2fd8..1454972 100644 --- a/new_lebench.c +++ b/new_lebench.c @@ -129,91 +129,123 @@ void calc_average(struct timespec *average, struct timespec *sum, int size) void getppid_bench(void) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; int l; int loop = 100000; + struct Record *runs; + + runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + memset(runs, 0, sizeof(struct Record) * loop); for (l = 0; l < loop; l++) { - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); #ifdef BYPASS bp_getppid(); #else syscall(SYS_getppid); #endif - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); + } - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); + for (l = 0; l < loop; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec); } + fflush(fp); + munmap(runs, sizeof(struct Record) * loop); return; } void clock_bench(void) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; int l; int loop = 100000; + struct Record *runs; + + runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + memset(runs, 0, sizeof(struct Record) * loop); for (l = 0; l < loop; l++) { - clock_gettime(CLOCK_MONOTONIC, &startTime); - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); + } - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); + for (l = 0; l < loop; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec); } + fflush(fp); + + munmap(runs, sizeof(struct Record) * loop); + return; } void cpu_bench(void) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; int i, l; double start; double div; int loop = 1000; + struct Record *runs; + runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + memset(runs, 0, sizeof(struct Record) * loop); for (l = 0; l < loop; l++) { start = 9903290.789798798; div = 3232.32; - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); for (i = 0; i < 500000; i++) { start = start / div; } - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); + } - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); + for (l = 0; l < loop; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec); } + fflush(fp); + + munmap(runs, sizeof(struct Record) * loop); return; } void write_bench(int file_size) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; char *buf; int fd, i, l; + struct Record *runs; #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * file_size); + runs = (struct Record*)vmalloc(sizeof(struct Record) * LOOP * 10); #elif defined(USE_MALLOC) buf = (char *)malloc(sizeof(char) * file_size); + runs = (struct Record*)malloc(sizeof(struct Record) * LOOP * 10); #else buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + runs = (struct Record*)mmap(NULL, sizeof(struct Record) * LOOP * 10, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif + for (i = 0; i < file_size; i++) { buf[i] = i % 93 + 33; @@ -225,28 +257,36 @@ void write_bench(int file_size) exit(0); } + memset(runs, 0, sizeof(struct Record) * LOOP * 10); for (l = 0; l < LOOP * 10; l++) { - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); #ifdef BYPASS bp_write(fd, buf, file_size); #else syscall(SYS_write, fd, buf, file_size); #endif - clock_gettime(CLOCK_MONOTONIC, &endTime); - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); } close(fd); + for (l = 0; l < LOOP * 10; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); + #if defined(USE_VMALLOC) vfree(buf); + vfree(runs); #elif defined(USE_MALLOC) free(buf); + free(runs); #else syscall(SYS_munmap, buf, file_size); + munmap(runs, sizeof(struct Record) * LOOP * 10); #endif return; @@ -254,18 +294,22 @@ void write_bench(int file_size) void read_bench(int file_size) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; char *buf; int fd, l, i; + struct Record *runs; #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * file_size); + runs = (struct Record*)vmalloc(sizeof(struct Record) * LOOP * 10); #elif defined(USE_MALLOC) buf = (char *)malloc(sizeof(char) * file_size); + runs = (struct Record*)malloc(sizeof(struct Record) * LOOP * 10); #else buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + runs = (struct Record*)mmap(NULL, sizeof(struct Record) * LOOP * 10, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif for (i = 0; i < file_size; i++) { @@ -278,28 +322,36 @@ void read_bench(int file_size) exit(0); } + memset(runs, 0, sizeof(struct Record) * LOOP * 10); for (l = 0; l < LOOP * 10; l++) { - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); #ifdef BYPASS bp_read(fd, buf, file_size); #else syscall(SYS_read, fd, buf, file_size); #endif - clock_gettime(CLOCK_MONOTONIC, &endTime); - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); } close(fd); + for (l = 0; l < LOOP * 10; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); + #if defined(USE_VMALLOC) vfree(buf); + vfree(runs); #elif defined(USE_MALLOC) free(buf); + free(runs); #else syscall(SYS_munmap, buf, file_size); + munmap(runs, sizeof(struct Record) * LOOP * 10); #endif return; @@ -308,8 +360,7 @@ void read_bench(int file_size) #define sock "./my_sock" void send_bench(int msg_size) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; // need to set affinity of parent and child to different cpus int retval, forkId, status, l; @@ -317,12 +368,7 @@ void send_bench(int msg_size) char w = 'b', r; char *buf; struct sockaddr_un server_addr; - - int recvbuff, retsock, sendbuff, newbuff; - socklen_t optlen = sizeof(recvbuff); - - cpu_set_t cpuset; - int prio; + struct Record *runs; memset(&server_addr, 0, sizeof(struct sockaddr_un)); server_addr.sun_family = AF_UNIX; @@ -332,11 +378,15 @@ void send_bench(int msg_size) // create a buffer (this needs to be mmap) #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * msg_size); + runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP); #elif defined(USE_MALLOC) buf = (char *)malloc(sizeof(char) * msg_size); + runs = (struct Record *)malloc(sizeof(struct Record) * LOOP); #else buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * msg_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif // write character 'a' in the buffer @@ -345,6 +395,8 @@ void send_bench(int msg_size) buf[i] = 'a'; } + memset(runs, 0, sizeof(struct Record) * LOOP); + for (l = 0; l < LOOP; l++) { retval = pipe(fds1); @@ -426,23 +478,19 @@ void send_bench(int msg_size) retval = send(fd_client, buf, msg_size, MSG_DONTWAIT); // send buffer over to child and measure latency - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); #ifdef BYPASS retval = bp_sendto(fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); #else retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0); #endif - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); if (retval == -1) { printf("[error %d] failed to send. %s\n", errno, strerror(errno)); } - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); - // update child so clean-up can happen write(fds2[1], &w, 1); @@ -455,12 +503,22 @@ void send_bench(int msg_size) } } + for (l = 0; l < LOOP; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); + #if defined(USE_VMALLOC) vfree(buf); + vfree(runs); #elif defined(USE_MALLOC) free(buf); + free(runs); #else syscall(SYS_munmap, buf, msg_size); + munmap(runs, sizeof(struct Record) * LOOP); #endif return; @@ -468,8 +526,7 @@ void send_bench(int msg_size) void recv_bench(int msg_size) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; + struct timespec diff = {0, 0}; // need to set affinity of parent and child to different cpus int retval, forkId, status, l; @@ -477,12 +534,7 @@ void recv_bench(int msg_size) char w = 'b', r; char *buf; struct sockaddr_un server_addr; - - int recvbuff, retsock, sendbuff, newbuff; - socklen_t optlen = sizeof(recvbuff); - - cpu_set_t cpuset; - int prio; + struct Record *runs; if (DEBUG) printf("recv_bench(%d)\n", msg_size); @@ -495,13 +547,19 @@ void recv_bench(int msg_size) // create a buffer (this needs to be mmap) #if defined(USE_VMALLOC) buf = (char *)vmalloc(sizeof(char) * msg_size); + runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP); #elif defined(USE_MALLOC) buf = (char *)malloc(sizeof(char) * msg_size); + runs = (struct Record *)malloc(sizeof(struct Record) * LOOP); #else buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * msg_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif + memset(runs, 0, sizeof(struct Record) * LOOP); + for (l = 0; l < LOOP; l++) { retval = pipe(fds1); @@ -555,23 +613,19 @@ void recv_bench(int msg_size) retval = recv(fd_connect, buf, msg_size, MSG_DONTWAIT); // recv data from child and measure latency - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); #ifdef BYPASS retval = bp_recvfrom(fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL); #else retval = syscall(SYS_recvfrom, fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL); #endif - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); if (retval == -1) { printf("[error %d] failed to send. %s\n", errno, strerror(errno)); } - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); - // update child so clean-up can happen write(fds1[1], &w, 1); @@ -635,12 +689,22 @@ void recv_bench(int msg_size) } } + for (l = 0; l < LOOP; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); + #if defined(USE_VMALLOC) vfree(buf); + vfree(runs); #elif defined(USE_MALLOC) free(buf); + free(runs); #else syscall(SYS_munmap, buf, msg_size); + munmap(runs, sizeof(struct Record) * LOOP); #endif return; @@ -650,23 +714,35 @@ struct timespec *forkTime; void fork_bench(void) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; - forkTime = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + struct timespec diff = {0, 0}, ave= {0, 0}; + forkTime = mmap(NULL, sizeof(struct timespec) * LOOP, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); int forkId, l, status; + struct Record *runs; +#if defined(USE_VMALLOC) + runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP); +#elif defined(USE_MALLOC) + runs = (struct Record *)malloc(sizeof(struct Record) * LOOP); +#else + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +#endif + + memset(runs, 0, sizeof(struct Record) * LOOP); + memset(forkTime, 0, sizeof(struct timespec) * LOOP); for (l = 0; l < LOOP; l++) { - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); forkId = fork(); if (forkId == 0) { - clock_gettime(CLOCK_MONOTONIC, forkTime); + clock_gettime(CLOCK_MONOTONIC, &forkTime[l]); exit(0); } else if (forkId > 0) { - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); wait(&status); } else @@ -674,132 +750,186 @@ void fork_bench(void) printf("[error] fork failed.\n"); fflush(stdout); } - calc_diff(&diffTime, &endTime, &startTime); - calc_diff(&aveTime, forkTime, &startTime); - fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec, aveTime.tv_sec, aveTime.tv_nsec); - fflush(fp); } + for (l = 0; l < LOOP; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + calc_diff(&ave, forkTime, &runs[l].start); + fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec, ave.tv_sec, ave.tv_nsec); + } + fflush(fp); + munmap(forkTime, sizeof(struct timespec)); + +#if defined(USE_VMALLOC) + vfree(runs); +#elif defined(USE_MALLOC) + free(runs); +#else + munmap(runs, sizeof(struct Record) * LOOP); +#endif + return; } -struct timespec *threadTime; - void *thrdfnc(void *args) { - clock_gettime(CLOCK_MONOTONIC, threadTime); + clock_gettime(CLOCK_MONOTONIC, (struct timespec*)args); pthread_exit(0); } void thread_bench(void) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; - int l, retval; + struct timespec diff = {0, 0}, ave= {0, 0}; + int l; pthread_t newThrd; + struct timespec *threads; + struct Record *runs; - threadTime = (struct timespec *)malloc(sizeof(struct timespec)); +#if defined(USE_VMALLOC) + runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP); + threads = (struct timespec *)vmalloc(sizeof(struct timespec) * LOOP); +#elif defined(USE_MALLOC) + runs = (struct Record *)malloc(sizeof(struct Record) * LOOP); + threads = (struct timespec *)malloc(sizeof(struct timespec) * LOOP); +#else + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + threads = (struct timespec *)mmap(NULL, sizeof(struct timespec) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +#endif + memset(runs, 0, sizeof(struct Record) * LOOP); + memset(threads, 0, sizeof(struct timespec) * LOOP); for (l = 0; l < LOOP; l++) { - clock_gettime(CLOCK_MONOTONIC, &startTime); - retval = pthread_create(&newThrd, NULL, thrdfnc, NULL); - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); + pthread_create(&newThrd, NULL, thrdfnc, &threads[l]); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); pthread_join(newThrd, NULL); + } - calc_diff(&diffTime, &endTime, &startTime); - calc_diff(&aveTime, threadTime, &startTime); - fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diffTime.tv_sec, diffTime.tv_nsec, aveTime.tv_sec, aveTime.tv_nsec); - fflush(fp); + for (l = 0; l < LOOP; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + calc_diff(&ave, &threads[l], &runs[l].start); + fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec, ave.tv_sec, ave.tv_nsec); } + fflush(fp); + +#if defined(USE_VMALLOC) + vfree(runs); + vfree(threads); +#elif defined(USE_MALLOC) + free(runs); + free(threads); +#else + munmap(runs, sizeof(struct Record) * LOOP); + munmap(threads, sizeof(struct timespec) * LOOP); +#endif - free(threadTime); return; } void pagefault_bench(int file_size) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; - char *buf; - int fd, l, i; - FILE *fp2; + struct timespec diff = {0, 0}; + struct Record *runs; + int l, i; char *addr; - char a[file_size]; +#if defined(USE_VMALLOC) + runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP); +#elif defined(USE_MALLOC) + runs = (struct Record *)malloc(sizeof(struct Record) * LOOP); +#else + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +#endif + + memset(runs, 0, sizeof(struct Record) * LOOP); for (l = 0; l < LOOP; l++) { - fd = open("tmp_file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); - if (fd < 0) - { - perror("invalid fd in write\n"); - exit(0); - } - - addr = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - clock_gettime(CLOCK_MONOTONIC, &startTime); + addr = (char *)mmap((void *)ADDR_HINT, file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); i = 0; + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); while (i < file_size) { addr[i] = i % 93 + 33; i = i + 4096; - // i++; } - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); - syscall(SYS_write, fd, addr, file_size); + munmap(addr, file_size); - close(fd); + } - syscall(SYS_munmap, addr, file_size); + for (l = 0; l < LOOP; l++) + { + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec); - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); } + fflush(fp); + +#if defined(USE_VMALLOC) + vfree(runs); +#elif defined(USE_MALLOC) + free(runs); +#else + munmap(runs, sizeof(struct Record) * LOOP); +#endif + return; } void stack_pagefault_bench(int file_size) { - struct timespec startTime = {0, 0}, endTime = {0, 0}; - struct timespec diffTime = {0, 0}, aveTime = {0, 0}; - char *buf; - int fd, l, i; - FILE *fp2; + int l, i; char *addr; - char a[file_size]; + struct Record *runs; +#if defined(USE_VMALLOC) + runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP); +#elif defined(USE_MALLOC) + runs = (struct Record *)malloc(sizeof(struct Record) * LOOP); +#else + runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +#endif + + memset(runs, 0, sizeof(struct Record) * LOOP); for (l = 0; l < LOOP; l++) { - fd = open("tmp_file.txt", O_CREAT | O_WRONLY); - if (fd < 0) - { - printf("invalid fd in write: %d\n", fd); - exit(0); - } - addr = (char *)alloca(file_size * sizeof(long)); - - clock_gettime(CLOCK_MONOTONIC, &startTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].start); i = 0; while (i < file_size) { addr[i] = i % 93 + 33; i = i + 4096; } - clock_gettime(CLOCK_MONOTONIC, &endTime); + clock_gettime(CLOCK_MONOTONIC, &runs[l].end); + } - syscall(SYS_write, fd, addr, file_size); + for (l = 0; l < LOOP; l++) + { + struct timespec diff; + calc_diff(&diff, &runs[l].end, &runs[l].start); + fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); - close(fd); +#if defined(USE_VMALLOC) + vfree(runs); +#elif defined(USE_MALLOC) + free(runs); +#else + munmap(runs, sizeof(struct Record) * LOOP); +#endif - calc_diff(&diffTime, &endTime, &startTime); - fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diffTime.tv_sec, diffTime.tv_nsec); - fflush(fp); - } return; } @@ -882,9 +1012,8 @@ extern void set_bypass_syscall(int val); int main(void) { - int file_size, pf_size, fd_count, retval; + int file_size, pf_size, retval; int i = 0, percentage = 0; - void *addr; cpu_set_t set; CPU_ZERO(&set); From 57aeb09dc26503b3e1fa4cf954bc79abc9ea71a3 Mon Sep 17 00:00:00 2001 From: "Eric B. Munson" Date: Wed, 23 Mar 2022 09:32:36 -0400 Subject: [PATCH 4/4] Add context switch benchmark The original measured the cost of context switches, add this test into the new way of doing things. We keep our method of measuring each iteration and outputing all the raw values at the end of the test so that stats can be calculated after the fact. Signed-off-by: Eric B. Munson --- new_lebench.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/new_lebench.c b/new_lebench.c index 1454972..c8d1033 100644 --- a/new_lebench.c +++ b/new_lebench.c @@ -1003,6 +1003,117 @@ static void select_bench(size_t fd_count, int iters) munmap(runs, sizeof(struct Record) * iters); #endif } + +static void context_switch_bench(void) +{ + int iter = 1000; + int fds1[2], fds2[2], retval; + retval = pipe(fds1); + if (retval != 0) + printf("[error] failed to open pipe1.\n"); + retval = pipe(fds2); + if (retval != 0) + printf("[error] failed to open pipe2.\n"); + + char w = 'a', r; + cpu_set_t cpuset; + int prio; + struct Record *runs; + + retval = sched_getaffinity(getpid(), sizeof(cpuset), &cpuset); + if (retval == -1) + printf("[error] failed to get affinity.\n"); + prio = getpriority(PRIO_PROCESS, 0); + if (prio == -1) + printf("[error] failed to get priority.\n"); + + int forkId = fork(); + if (forkId > 0) { // is parent + retval = close(fds1[0]); + if (retval != 0) + printf("[error] failed to close fd1.\n"); + retval = close(fds2[1]); + if (retval != 0) + printf("[error] failed to close fd2.\n"); + + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(0, &set); + retval = sched_setaffinity(getpid(), sizeof(set), &set); + if (retval == -1) + printf("[error] failed to set processor affinity.\n"); + retval = setpriority(PRIO_PROCESS, 0, -20); + if (retval == -1) + printf("[error] failed to set process priority.\n"); + + runs = mmap(NULL, sizeof(struct Record) * iter, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + memset(runs, 0, sizeof(struct Record) * iter); + + read(fds2[0], &r, 1); + + for (int i = 0; i < iter; i++) { + clock_gettime(CLOCK_MONOTONIC, &runs[i].start); + write(fds1[1], &w, 1); + read(fds2[0], &r, 1); + clock_gettime(CLOCK_MONOTONIC, &runs[i].end); + } + int status; + wait(&status); + + close(fds1[1]); + close(fds2[0]); + + + } else if (forkId == 0){ + + retval = close(fds1[1]); + if (retval != 0) + printf("[error] failed to close fd1.\n"); + retval = close(fds2[0]); + if (retval != 0) + printf("[error] failed to close fd2.\n"); + + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(0, &set); + retval = sched_setaffinity(getpid(), sizeof(set), &set); + if (retval == -1) + printf("[error] failed to set processor affinity.\n"); + retval = setpriority(PRIO_PROCESS, 0, -20); + if (retval == -1) + printf("[error] failed to set process priority.\n"); + + write(fds2[1], &w, 1); + for (int i = 0; i < iter; i++) { + read(fds1[0], &r, 1); + write(fds2[1], &w, 1); + } + + kill(getpid(), SIGINT); + printf("[error] unable to kill child process\n"); + return; + } else { + printf("[error] failed to fork.\n"); + } + + retval = sched_setaffinity(getpid(), sizeof(cpuset), &cpuset); + if (retval == -1) + printf("[error] failed to restore affinity.\n"); + retval = setpriority(PRIO_PROCESS, 0, prio); + if (retval == -1) + printf("[error] failed to restore priority.\n"); + + struct timespec diff; + for (int i = 0; i < iter; i++) + { + calc_diff(&diff, &runs[i].end, &runs[i].start); + fprintf(fp, "%d,%ld.%09ld\n", i, diff.tv_sec, diff.tv_nsec); + } + fflush(fp); + + munmap(runs, sizeof(struct Record) * iter); +} //--------------------------------------------------------------------- #ifdef BYPASS @@ -1268,4 +1379,15 @@ int main(void) fclose(fp); #endif + +#ifdef CTX_SW_TEST + fp = fopen("./new_lebench_context_switch.csv", "w"); + fprintf(fp, "Index,Latency\n"); + + printf("Running context switch test\n"); + fflush(stdout); + context_switch_bench(); + + fclose(fp); +#endif }