/* * Benchmark a pipe by seeing how many 511-byte writes can be stuffed through * it in a certain amount of time. Compile with -lm. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include static unsigned long long *results; static volatile int stop; void sigalrm(int sig) { stop = 1; } static __attribute__((noreturn)) void producer(int fd, int i, char *buf) { unsigned long long l = 0; ssize_t n; signal(SIGALRM, sigalrm); alarm(1); while (!stop) { struct iovec iov[1]; iov[0].iov_base = buf; iov[0].iov_len = 511; n = vmsplice(fd, iov, 1, SPLICE_F_GIFT); if (n == -1) { perror("vmsplice"); exit(1); } l += n; } results[i] = l; exit(0); } static __attribute__((noreturn)) void splicer(int fdfrom, int fdto) { unsigned long long l = 0; ssize_t n; for (;;) { n = splice(fdfrom, NULL, fdto, NULL, 511, SPLICE_F_MOVE); if (n == 0) break; if (n == -1) { perror("splice"); exit(1); } l += n; } exit(0); } static __attribute__((noreturn)) void consumer(int fd, char *buf) { unsigned long long l = 0; ssize_t n; for (;;) { n = read(fd, buf, 4096); if (n == 0) break; if (n == -1) { perror("read"); exit(1); } l += n; } exit(0); } unsigned long long stddev(const unsigned long long *vals, int nvals) { double sum = 0.0, mean, sd = 0.0; int i; for (i = 0; i < nvals; i++) sum += vals[i]; mean = sum / nvals; for (i = 0; i < nvals; i++) sd += pow(vals[i] - mean, 2); return sqrt(sd / 10); } int main() { unsigned long long total = 0, best = 0, sd; unsigned long pagesize; char *buf1, *buf2; int ex = 0, i; pagesize = sysconf(_SC_PAGESIZE); buf1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (results == MAP_FAILED) { perror("mmap/1"); exit(1); } buf2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (results == MAP_FAILED) { perror("mmap/2"); exit(1); } results = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (results == MAP_FAILED) { perror("mmap/r"); exit(1); } for (i = 0; i < 120 && ex == 0; i++) { int p1fd[2], p2fd[2], wt; if (pipe2(p1fd, 0) < 0) { perror("pipe/1"); exit(1); } if (pipe2(p2fd, 0) < 0) { perror("pipe/2"); exit(1); } switch (fork()) { case -1: perror("fork/c"); exit(1); case 0: close(p1fd[0]); close(p1fd[1]); close(p2fd[1]); consumer(p2fd[0], buf1); default: break; } switch (fork()) { case -1: perror("fork/s"); exit(1); case 0: close(p1fd[1]); close(p2fd[0]); splicer(p1fd[0], p2fd[1]); default: break; } switch (fork()) { case -1: perror("fork/p"); exit(1); case 0: close(p1fd[0]); close(p2fd[0]); close(p2fd[1]); producer(p1fd[1], i, buf2); default: break; } close(p1fd[0]); close(p1fd[1]); close(p2fd[0]); close(p2fd[1]); for (;;) { errno = 0; wait(&wt); if (errno == ECHILD) break; if (!WIFEXITED(wt) || WEXITSTATUS(wt) != 0) ex = 1; } //printf("WR[%02u]: %12llu\n", i, results[i]); total += results[i]; if (results[i] > best) best = results[i]; } sd = stddev(results, i); printf("PATCHES BENCHMARK BEST TOTAL BYTES AVG BYTES STDDEV\n"); printf("======= =============== =============== =============== =============== ===============\n"); printf("-\tsplice\t\t%15llu\t%15llu\t%15llu\t%15llu\n", best, total, total / i, sd); exit(ex); }