X-Git-Url: http://rtime.felk.cvut.cz/gitweb/can-benchmark.git/blobdiff_plain/87ae560edb2ef5f1b6b2029a3fe7c0259c45ffb1..e3e90bc84127a14b7de6a827c087a1c51da70bdc:/recvmmsg/can_recvmmsg.c diff --git a/recvmmsg/can_recvmmsg.c b/recvmmsg/can_recvmmsg.c index 4dee5f1..7e21c65 100644 --- a/recvmmsg/can_recvmmsg.c +++ b/recvmmsg/can_recvmmsg.c @@ -16,11 +16,13 @@ #include #include -#define CHECK(cmd) do { if ((cmd) == -1) { perror(#cmd); exit(1); } } while (0) +#define STRINGIFY(val) #val +#define TOSTRING(val) STRINGIFY(val) +#define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; }) char *dev = "vcan0"; int count = 10000; -enum { READ, RECVMMSG } recv_method = READ; +enum { READWRITE, MMSG } method = READWRITE; bool quiet = false; /* Subtract the `struct timespec' values X and Y, @@ -29,25 +31,18 @@ bool quiet = false; int timespec_subtract (struct timespec *result, - struct timespec *x, - struct timespec *y) + const struct timespec *x, + const struct timespec *y) { - /* Perform the carry for the later subtraction by updating Y. */ - if (x->tv_nsec < y->tv_nsec) { - int num_sec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1; - y->tv_nsec -= 1000000000 * num_sec; - y->tv_sec += num_sec; - } - if (x->tv_nsec - y->tv_nsec > 1000000000) { - int num_sec = (x->tv_nsec - y->tv_nsec) / 1000000000; - y->tv_nsec += 1000000000 * num_sec; - y->tv_sec -= num_sec; - } + int carry = (x->tv_nsec < y->tv_nsec); + + result->tv_sec = x->tv_sec - y->tv_sec - carry; + result->tv_nsec = x->tv_nsec + carry*1000000000 - y->tv_nsec; - /* Compute the time remaining to wait. - `tv_nsec' is certainly positive. */ - result->tv_sec = x->tv_sec - y->tv_sec; - result->tv_nsec = x->tv_nsec - y->tv_nsec; +/* printf("%ld.%09ld - %ld.%09ld = %ld.%09ld\n", */ +/* x->tv_sec, x->tv_nsec, */ +/* y->tv_sec, y->tv_nsec, */ +/* result->tv_sec, result->tv_nsec); */ /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; @@ -61,7 +56,7 @@ void benchmark() struct ifreq ifr; struct can_frame cf; int ret, i; - struct timespec t, t1, t2; + struct timespec ttx, trx, t1, t2; ss = socket(PF_CAN, SOCK_RAW, CAN_RAW); sr = socket(PF_CAN, SOCK_RAW, CAN_RAW); @@ -81,17 +76,43 @@ void benchmark() memset(&cf, 0, sizeof(cf)); cf.can_dlc = 8; - if (!quiet) fprintf(stderr, "Sending %d frames\n", count); - for (i = 0; i < count; i++) { - ret = write(ss, &cf, sizeof(cf)); - if (ret != sizeof(cf)) { - perror("write"); - exit(1); + switch (method) { + case READWRITE: { + if (!quiet) fprintf(stderr, "Sending %d frames via write()\n", count); + clock_gettime(CLOCK_MONOTONIC, &t1); + for (i = 0; i < count; i++) { + ret = write(ss, &cf, sizeof(cf)); + if (ret != sizeof(cf)) { + perror("write"); + exit(1); + } } + clock_gettime(CLOCK_MONOTONIC, &t2); + break; + } + case MMSG: { + if (!quiet) fprintf(stderr, "Sending %d frames via sendmmsg()\n", count); + struct mmsghdr msgs[count]; + struct iovec iovecs[count]; + memset(msgs, 0, sizeof(msgs)); + memset(iovecs, 0, sizeof(iovecs)); + for (i = 0; i < count; i++) { + iovecs[i].iov_base = &cf; + iovecs[i].iov_len = sizeof(cf); + msgs[i].msg_hdr.msg_iov = &iovecs[i]; + msgs[i].msg_hdr.msg_iovlen = 1; + } + clock_gettime(CLOCK_MONOTONIC, &t1); + for (i = 0; i < count; i += ret) + ret = CHECK(sendmmsg(ss, &msgs[i], count - i, 0)); + clock_gettime(CLOCK_MONOTONIC, &t2); + break; + } } + timespec_subtract(&ttx, &t2, &t1); - switch (recv_method) { - case READ: { + switch (method) { + case READWRITE: { if (!quiet) fprintf(stderr, "Receiving %d frames with read()\n", count); clock_gettime(CLOCK_MONOTONIC, &t1); for (i = 0; i < count; i++) { @@ -106,7 +127,7 @@ void benchmark() //fprintf(stderr, "\n"); break; } - case RECVMMSG: { + case MMSG: { if (!quiet) fprintf(stderr, "Receiving %d frames with recvmmsg()\n", count); struct mmsghdr msgs[count]; struct iovec iovecs[count]; @@ -133,8 +154,9 @@ void benchmark() break; } } - timespec_subtract(&t, &t2, &t1); - printf("%d us\n", t.tv_nsec/1000); + timespec_subtract(&trx, &t2, &t1); + printf("tx %ld rx %ld [us]\n", ttx.tv_sec*1000000 + ttx.tv_nsec/1000, + trx.tv_sec*1000000 + trx.tv_nsec/1000); } int main(int argc, char *argv[]) @@ -147,10 +169,10 @@ int main(int argc, char *argv[]) count = atoi(optarg); break; case 'm': - recv_method = RECVMMSG; + method = MMSG; break; case 'r': - recv_method = READ; + method = READWRITE; break; case 'q': quiet = true;