diff --git a/Makefile b/Makefile index fe080d1..98d61c4 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,21 @@ -CFLAGS = -g -Wall +CFLAGS = -g -Wall -O3 + +.PHONY: all clean run all: pipe_lat pipe_thr \ unix_lat unix_thr \ tcp_lat tcp_thr \ tcp_local_lat tcp_remote_lat -# shm -shm: CFLAGS += -lrt + +%_lat: atomicio.o xutil.o %_lat.o + $(CC) $(CFLAGS) -o $@ $^ + +%_thr: atomicio.o xutil.o %_thr.o + $(CC) $(CFLAGS) -o $@ $^ + +#shm: CFLAGS += -lrt run: ./pipe_lat 100 10000 diff --git a/pipe_lat.c b/pipe_lat.c index aa10015..daa3f29 100644 --- a/pipe_lat.c +++ b/pipe_lat.c @@ -1,8 +1,8 @@ /* Measure latency of IPC using unix domain sockets - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -26,17 +26,16 @@ OTHER DEALINGS IN THE SOFTWARE. */ - #include #include #include -#include #include -#include +#include #include +#include "xutil.h" - -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int ofds[2]; int ifds[2]; @@ -54,51 +53,24 @@ int main(int argc, char *argv[]) size = atoi(argv[1]); count = atol(argv[2]); - buf = malloc(size); - if (buf == NULL) { - err(1, "malloc"); - return 1; - } - - printf("message size: %i octets\n", size); - printf("roundtrip count: %lli\n", count); + buf = xmalloc(size); - if (pipe(ofds) == -1) { - perror("pipe"); - return 1; - } + if (pipe(ofds) == -1) + err(1, "pipe"); - if (pipe(ifds) == -1) { - perror("pipe"); - return 1; - } + if (pipe(ifds) == -1) + err(1, "pipe"); if (!fork()) { /* child */ - int r; for (i = 0; i < count; i++) { - r = read(ifds[0], buf, size); - if (r != size) - err(1, "child read ret=%d", r); - - r = write(ofds[1], buf, size); - if (r != size) - err(1, "child write ret=%d", r); + xread(ifds[0], buf, size); + xwrite(ofds[1], buf, size); } } else { /* parent */ gettimeofday(&start, NULL); - for (i = 0; i < count; i++) { - - if (write(ifds[1], buf, size) != size) { - perror("child write"); - return 1; - } - - if (read(ofds[0], buf, size) != size) { - perror("child read"); - return 1; - } - + xwrite(ifds[1], buf, size); + xread(ofds[0], buf, size); } gettimeofday(&stop, NULL); @@ -106,9 +78,7 @@ int main(int argc, char *argv[]) delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1000000 + stop.tv_usec - start.tv_usec); - printf("average latency: %lli us\n", delta / (count * 2)); - + printf("pipe_lat %d %" PRId64 " %" PRId64 "\n", size, count, delta / (count * 2)); } - return 0; } diff --git a/pipe_thr.c b/pipe_thr.c index 49fd34f..3652801 100644 --- a/pipe_thr.c +++ b/pipe_thr.c @@ -1,8 +1,8 @@ /* Measure throughput of IPC using pipes - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -31,10 +31,12 @@ #include #include #include -#include - +#include +#include +#include "xutil.h" -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int fds[2]; @@ -51,48 +53,30 @@ int main(int argc, char *argv[]) size = atoi(argv[1]); count = atol(argv[2]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - exit(1); - } + buf = xmalloc(size); - printf("message size: %i octets\n", size); - printf("message count: %lli\n", count); - - if (pipe(fds) == -1) { - perror("pipe"); - exit(1); - } + if (pipe(fds) == -1) + err(1, "pipe"); if (!fork()) { /* child */ - for (i = 0; i < count; i++) { - if (read(fds[0], buf, size) != size) { - perror("read"); - exit(1); - } - } + for (i = 0; i < count; i++) + xread(fds[0], buf, size); } else { /* parent */ gettimeofday(&start, NULL); - for (i = 0; i < count; i++) { - if (write(fds[1], buf, size) != size) { - perror("write"); - exit(1); - } - } + for (i = 0; i < count; i++) + xwrite(fds[1], buf, size); gettimeofday(&stop, NULL); delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1e6 + stop.tv_usec - start.tv_usec); - printf("average throughput: %lli msg/s\n", (count * (int64_t) 1e6) / delta); - printf("average throughput: %lli Mb/s\n", (((count * (int64_t) 1e6) / delta) * size * 8) / (int64_t) 1e6); + printf("pipe_thr %d %" PRId64 " %" PRId64 "\n", size, count, (((count * (int64_t) 1e6) / delta) * size * 8) / (int64_t) 1e6); } return 0; diff --git a/run.sh b/run.sh index 086121c..f2f3487 100755 --- a/run.sh +++ b/run.sh @@ -1,14 +1,23 @@ #!/usr/bin/env bash +set -e -SIZES="1 2 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536" -NUMS="50000" -TEST="pipe_lat unix_lat" +#SIZES="1 2 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536" +#SIZES="1 8 32 64 256 1024 4096 8192 16384 32768 65536" +SIZES="8 64 1024 8192 16384" +NUMS="40000" +TEST="pipe_lat unix_lat tcp_lat tcp_thr pipe_thr unix_thr" + +IF=eth0 +EXTERNAL_IP=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}') + +OUT=results.csv +rm -f ${OUT} for test in ${TEST}; do for size in ${SIZES}; do for num in ${NUMS}; do echo ${test}: ${size} x ${num} - ./${test} ${size} ${num} + ./${test} ${size} ${num} >> ${OUT} done done done diff --git a/tcp_lat.c b/tcp_lat.c index 113ec98..c877a0c 100644 --- a/tcp_lat.c +++ b/tcp_lat.c @@ -1,8 +1,8 @@ /* Measure latency of IPC using tcp sockets - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -32,20 +32,20 @@ #include #include #include -#include +#include +#include #include +#include "xutil.h" -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int size; char *buf; int64_t count, i, delta; struct timeval start, stop; - ssize_t len; - size_t sofar; - int yes = 1; int ret; struct sockaddr_storage their_addr; @@ -62,111 +62,56 @@ int main(int argc, char *argv[]) size = atoi(argv[1]); count = atol(argv[2]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - return 1; - } + buf = xmalloc(size); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // fill in my IP for me - if ((ret = getaddrinfo("127.0.0.1", "3491", &hints, &res)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - return 1; - } - - printf("message size: %i octets\n", size); - printf("roundtrip count: %lli\n", count); + if ((ret = getaddrinfo("127.0.0.1", "3491", &hints, &res)) != 0) + errx(1, "getaddrinfo: %s\n", gai_strerror(ret)); if (!fork()) { /* child */ - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - exit(1); - } + if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + err(1, "socket"); - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { - perror("setsockopt"); - exit(1); - } + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) + err(1, "setsockopt"); - if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("bind"); - exit(1); - } + if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) + err(1, "bind"); - if (listen(sockfd, 1) == -1) { - perror("listen"); - exit(1); - } + if (listen(sockfd, 1) == -1) + err(1, "listen"); addr_size = sizeof their_addr; - if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == -1) { - perror("accept"); - exit(1); - } + if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == -1) + err(1, "accept"); for (i = 0; i < count; i++) { - - for (sofar = 0; sofar < size; ) { - len = read(new_fd, buf, size - sofar); - if (len == -1) { - perror("read"); - return 1; - } - sofar += len; - } - - if (write(new_fd, buf, size) != size) { - perror("write"); - return 1; - } + xread(new_fd, buf, size); + xwrite(new_fd, buf, size); } } else { /* parent */ - - sleep(1); + + sleep(1); + if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + err(1, "socket"); - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - exit(1); - } - - if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("connect"); - exit(1); - } - - gettimeofday(&start, NULL); + if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) + err(1, "connect"); + gettimeofday(&start, NULL); for (i = 0; i < count; i++) { - - if (write(sockfd, buf, size) != size) { - perror("write"); - return 1; - } - - for (sofar = 0; sofar < size; ) { - len = read(sockfd, buf, size - sofar); - if (len == -1) { - perror("read"); - return 1; - } - sofar += len; - } - + xwrite(sockfd, buf, size); + xread(sockfd, buf, size); } - gettimeofday(&stop, NULL); - delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1e6 + stop.tv_usec - start.tv_usec); - - printf("average latency: %lli us\n", delta / (count * 2)); - + printf("tcp_lat %d %" PRId64 " %" PRId64 "\n", size, count, delta / (count * 2)); } - return 0; } diff --git a/tcp_local_lat.c b/tcp_local_lat.c index b46cb15..6629604 100644 --- a/tcp_local_lat.c +++ b/tcp_local_lat.c @@ -1,8 +1,8 @@ /* Measure latency of IPC using tcp sockets - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -32,19 +32,18 @@ #include #include #include -#include +#include #include +#include +#include "xutil.h" - -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int size; char *buf; int64_t count, i; - ssize_t len; - size_t sofar; - int yes = 1; int ret; struct sockaddr_storage their_addr; @@ -61,66 +60,35 @@ int main(int argc, char *argv[]) size = atoi(argv[3]); count = atol(argv[4]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - return 1; - } - - printf("message size: %i octets\n", size); - printf("roundtrip count: %lli\n", count); + buf = xmalloc(size); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // fill in my IP for me - if ((ret = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - return 1; - } + if ((ret = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0) + errx(1, "getaddrinfo: %s", gai_strerror(ret)); - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - exit(1); - } + if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + err(1, "socket"); - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { - perror("setsockopt"); - exit(1); - } + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) + err(1, "setsockopt"); - if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("bind"); - exit(1); - } + if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) + err(1, "bind"); - if (listen(sockfd, 1) == -1) { - perror("listen"); - exit(1); - } + if (listen(sockfd, 1) == -1) + err(1, "listen"); addr_size = sizeof their_addr; - if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == -1) { - perror("accept"); - exit(1); - } + if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == -1) + err(1, "accept"); for (i = 0; i < count; i++) { - - for (sofar = 0; sofar < size; ) { - len = read(new_fd, buf, size - sofar); - if (len == -1) { - perror("read"); - return 1; - } - sofar += len; - } - - if (write(new_fd, buf, size) != size) { - perror("write"); - return 1; - } + xread(new_fd, buf, size); + xwrite(new_fd, buf, size); } return 0; diff --git a/tcp_remote_lat.c b/tcp_remote_lat.c index db41382..584c689 100644 --- a/tcp_remote_lat.c +++ b/tcp_remote_lat.c @@ -1,8 +1,8 @@ /* Measure latency of IPC using tcp sockets - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -32,20 +32,19 @@ #include #include #include -#include +#include #include +#include +#include "xutil.h" - -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int size; char *buf; int64_t count, i, delta; struct timeval start, stop; - ssize_t len; - size_t sofar; - int ret; struct addrinfo hints; struct addrinfo *res; @@ -59,62 +58,32 @@ int main(int argc, char *argv[]) size = atoi(argv[4]); count = atol(argv[5]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - return 1; - } - - printf("message size: %i octets\n", size); - printf("roundtrip count: %lli\n", count); + buf = xmalloc(size); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // fill in my IP for me - if ((ret = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - return 1; - } + if ((ret = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) + errx(1, "getaddrinfo: %s", gai_strerror(ret)); - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - exit(1); - } + if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + err(1, "socket"); - if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("bind"); - exit(1); - } + if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) + err(1, "bind"); - if ((ret = getaddrinfo(argv[2], argv[3], &hints, &res)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - return 1; - } + if ((ret = getaddrinfo(argv[2], argv[3], &hints, &res)) != 0) + errx(1, "getaddrinfo: %s", gai_strerror(ret)); - if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("connect"); - exit(1); - } + if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) + err(1, "connect"); gettimeofday(&start, NULL); for (i = 0; i < count; i++) { - - if (write(sockfd, buf, size) != size) { - perror("write"); - return 1; - } - - for (sofar = 0; sofar < size; ) { - len = read(sockfd, buf, size - sofar); - if (len == -1) { - perror("read"); - return 1; - } - sofar += len; - } - + xwrite(sockfd, buf, size); + xread(sockfd, buf, size); } gettimeofday(&stop, NULL); @@ -122,7 +91,6 @@ int main(int argc, char *argv[]) delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1e6 + stop.tv_usec - start.tv_usec); - printf("average latency: %lli us\n", delta / (count * 2)); - + printf("tcp_remote %d %" PRId64 " %" PRId64 "\n", size, count, delta / (count * 2)); return 0; } diff --git a/tcp_thr.c b/tcp_thr.c index 7eda0f6..e4d762c 100644 --- a/tcp_thr.c +++ b/tcp_thr.c @@ -1,8 +1,8 @@ /* Measure throughput of IPC using tcp sockets - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -31,22 +31,21 @@ #include #include #include -#include -#include +#include +#include #include #include +#include +#include "xutil.h" - -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int size; char *buf; int64_t count, i, delta; struct timeval start, stop; - ssize_t len; - size_t sofar; - int yes = 1; int ret; struct sockaddr_storage their_addr; @@ -63,98 +62,63 @@ int main(int argc, char *argv[]) size = atoi(argv[1]); count = atol(argv[2]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - exit(1); - } + buf = xmalloc(size); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // fill in my IP for me - if ((ret = getaddrinfo("127.0.0.1", "3491", &hints, &res)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - return 1; - } - - printf("message size: %i octets\n", size); - printf("message count: %lli\n", count); + if ((ret = getaddrinfo("127.0.0.1", "3491", &hints, &res)) != 0) + errx(1, "getaddrinfo: %s", gai_strerror(ret)); if (!fork()) { /* child */ - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - exit(1); - } + if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + err(1, "socket"); - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { - perror("setsockopt"); - exit(1); - } - - if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("bind"); - exit(1); - } + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) + err(1, "setsockopt"); + + while (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) + {}; - if (listen(sockfd, 1) == -1) { - perror("listen"); - exit(1); - } + if (listen(sockfd, 1) == -1) + err(1, "listen"); addr_size = sizeof their_addr; - if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == -1) { - perror("accept"); - exit(1); - } - - for (sofar = 0; sofar < (count * size);) { - len = read(new_fd, buf, size); - if (len == -1) { - perror("read"); - exit(1); - } - sofar += len; - } + if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == -1) + err(1, "accept"); + + for (i = 0; i < count; i++) + xread(new_fd, buf, size); + + close(sockfd); } else { /* parent */ sleep(1); - if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - perror("socket"); - exit(1); - } - - if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) { - perror("connect"); - exit(1); - } + if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) + err(1, "socket"); - if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int)) == -1) { - perror("setsockopt"); - exit(1); - } + if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) + err(1, "connect"); - gettimeofday(&start, NULL); + if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int)) == -1) + err(1, "setsockopt"); - for (i = 0; i < count; i++) { - if (write(sockfd, buf, size) != size) { - perror("write"); - exit(1); - } - } + gettimeofday(&start, NULL); + for (i = 0; i < count; i++) + xwrite(sockfd, buf, size); gettimeofday(&stop, NULL); - + close(sockfd); delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1e6 + stop.tv_usec - start.tv_usec); - printf("average throughput: %lli msg/s\n", (count * (int64_t) 1e6) / delta); - printf("average throughput: %lli Mb/s\n", (((count * (int64_t) 1e6) / delta) * size * 8) / (int64_t) 1e6); + printf("tcp_thr %d %" PRId64 " %" PRId64 "\n", size, count, (((count * (int64_t) 1e6) / delta) * size * 8) / (int64_t) 1e6); } return 0; diff --git a/unix_lat.c b/unix_lat.c index 9972a4f..dfed1e4 100644 --- a/unix_lat.c +++ b/unix_lat.c @@ -1,8 +1,8 @@ /* Measure latency of IPC using unix domain sockets - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -31,10 +31,13 @@ #include #include #include -#include +#include +#include +#include "xutil.h" -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int sv[2]; /* the pair of socket descriptors */ int size; @@ -50,59 +53,26 @@ int main(int argc, char *argv[]) size = atoi(argv[1]); count = atol(argv[2]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - return 1; - } - - printf("message size: %i octets\n", size); - printf("roundtrip count: %lli\n", count); + buf = xmalloc(size); - if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) { - perror("socketpair"); - exit(1); - } + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) + err(1, "socketpair"); if (!fork()) { /* child */ for (i = 0; i < count; i++) { - - if (read(sv[1], buf, size) != size) { - perror("read"); - return 1; - } - - if (write(sv[1], buf, size) != size) { - perror("write"); - return 1; - } + xread(sv[1], buf, size); + xwrite(sv[1], buf, size); } } else { /* parent */ - gettimeofday(&start, NULL); - for (i = 0; i < count; i++) { - - if (write(sv[0], buf, size) != size) { - perror("write"); - return 1; - } - - if (read(sv[0], buf, size) != size) { - perror("read"); - return 1; - } - + xwrite(sv[0], buf, size); + xread(sv[0], buf, size); } - gettimeofday(&stop, NULL); - delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1e6 + stop.tv_usec - start.tv_usec); - - printf("average latency: %lli us\n", delta / (count * 2)); - + printf("unix_lat %d %" PRId64 " %" PRId64 "\n", size, count, delta / (count * 2)); } - return 0; } diff --git a/unix_thr.c b/unix_thr.c index fdea948..405965a 100644 --- a/unix_thr.c +++ b/unix_thr.c @@ -1,8 +1,8 @@ /* Measure throughput of IPC using unix domain sockets. - Copyright (c) 2010 Erik Rigtorp + Copyright (c) 2011 Anil Madhavapeddy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -31,10 +31,12 @@ #include #include #include -#include - +#include +#include +#include "xutil.h" -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int fds[2]; /* the pair of socket descriptors */ int size; @@ -50,49 +52,30 @@ int main(int argc, char *argv[]) size = atoi(argv[1]); count = atol(argv[2]); - buf = malloc(size); - if (buf == NULL) { - perror("malloc"); - exit(1); - } - - printf("message size: %i octets\n", size); - printf("message count: %lli\n", count); + buf = xmalloc(size); - if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) { - perror("socketpair"); - exit(1); - } + if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) + err(1, "socketpair"); if (!fork()) { /* child */ - for (i = 0; i < count; i++) { - if (read(fds[1], buf, size) != size) { - perror("read"); - exit(1); - } - } + for (i = 0; i < count; i++) + xread(fds[1], buf, size); } else { /* parent */ gettimeofday(&start, NULL); - for (i = 0; i < count; i++) { - if (write(fds[0], buf, size) != size) { - perror("write"); - exit(1); - } - } + for (i = 0; i < count; i++) + xwrite(fds[0], buf, size); gettimeofday(&stop, NULL); delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1e6 + stop.tv_usec - start.tv_usec); - - printf("average throughput: %lli msg/s\n", (count * (int64_t) 1e6) / delta); - printf("average throughput: %lli Mb/s\n", (((count * (int64_t) 1e6) / delta) * size * 8) / (int64_t) 1e6); - } + printf("unix_thr %d %" PRId64 " %" PRId64 "\n", size, count, (((count * (int64_t) 1e6) / delta) * size * 8) / (int64_t) 1e6); + } return 0; }