#include #include #include #include #include #include //struct timeval { // time_t tv_sec; /* seconds */ // suseconds_t tv_usec; /* microseconds */ //}; // stolen from lmbench #define uint64 unsigned long long void tvsub(struct timeval * tdiff, struct timeval * t1, struct timeval * t0) { tdiff->tv_sec = t1->tv_sec - t0->tv_sec; tdiff->tv_usec = t1->tv_usec - t0->tv_usec; if (tdiff->tv_usec < 0 && tdiff->tv_sec > 0) { tdiff->tv_sec--; tdiff->tv_usec += 1000000; //assert(tdiff->tv_usec >= 0); } /* time shouldn't go backwards!!! */ if (tdiff->tv_usec < 0 || t1->tv_sec < t0->tv_sec) { tdiff->tv_sec = 0; tdiff->tv_usec = 0; } } tvdelta(struct timeval *start, struct timeval *stop) { struct timeval td; uint64 usecs; tvsub(&td, stop, start); usecs = td.tv_sec; usecs *= 1000000; usecs += td.tv_usec; return (usecs); } void write_file(char *file) { int fd = open(&file[0], O_WRONLY|O_CREAT); if (fd < 0) { perror("open error"); exit(fd); } write(fd, "foo"); close(fd); } const uint64 USECS_PER_SEC = 1000000; int run_openbench(int run_seconds) { pid_t pid = getpid(); char bufa[100]; char bufb[100]; int ret; int i; struct timeval start; struct timeval end; uint64 useconds = run_seconds * USECS_PER_SEC; uint64 running_for; uint64 last_running_for; printf("openbench running (pid %d)\n", pid); sprintf(&bufa[0], "/ram/openbench.%d", pid); sprintf(&bufb[0], "/ram1/openbench.%d", pid); uint64 opens = 0; int warmup = 1; gettimeofday(&start, NULL); do { int loops = 100000; for (i=0; i< loops; i++) { write_file(&bufa[0]); write_file(&bufb[0]); opens++; } ret = unlink(&bufa[0]); if (ret) perror("unlink error"); ret = unlink(&bufb[0]); if (ret) perror("unlink error"); gettimeofday(&end, NULL); running_for = tvdelta(&start, &end); if (running_for - last_running_for > USECS_PER_SEC) { if (warmup) printf("warmup "); printf("[%5d] opens/sec: %d\n", pid, opens*USECS_PER_SEC/running_for); fflush( stdout ); last_running_for = running_for; } if (warmup && running_for > run_seconds/5*USECS_PER_SEC) { gettimeofday(&start, NULL); opens = 0; warmup = 0; } } while (running_for < useconds); printf("[%5d] total opens/sec: %d\n", pid, opens*USECS_PER_SEC/running_for); return 0; } int main(int argc, char **argv) { int run_seconds = atoi(argv[1]); int nr_children = 4; int i; int status; for (i=0; i < nr_children; i++) { int pid = fork(); if (pid) continue; run_openbench(run_seconds); //printf("pid: %d done\n", getpid()); exit(0); } while ((status = wait(-1)) > 0 ) { printf("status: %d\n", status); } }