]> rtime.felk.cvut.cz Git - can-benchmark.git/blobdiff - recvmmsg/can_recvmmsg.c
Add reference from /usr/src/linux
[can-benchmark.git] / recvmmsg / can_recvmmsg.c
index 4041f62fbd72da9d325d712a963a908b9802c863..7e21c65edfe75f88dec9b46aa9bbcd510b6aa1fc 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
+#include <stdbool.h>
 
-#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,
    storing the result in RESULT (result = x - y).
@@ -27,25 +31,18 @@ enum { READ, RECVMMSG } recv_method = READ;
 
 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;
@@ -59,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);
@@ -79,18 +76,44 @@ void benchmark()
        memset(&cf, 0, sizeof(cf));
        cf.can_dlc = 8;
 
-       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: {
-               fprintf(stderr, "Receiving %d frames with read()\n", count);
+       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++) {
                        //fprintf(stderr, "Receiving frame %d\r", i);
@@ -104,8 +127,8 @@ void benchmark()
                //fprintf(stderr, "\n");
                break;
        }
-       case RECVMMSG: {
-               fprintf(stderr, "Receiving %d frames with recvmmsg()\n", count);
+       case MMSG: {
+               if (!quiet) fprintf(stderr, "Receiving %d frames with recvmmsg()\n", count);
                struct mmsghdr msgs[count];
                struct iovec iovecs[count];
                char bufs[count][sizeof(struct can_frame)];
@@ -131,24 +154,28 @@ 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[])
 {
        int opt;
 
-       while ((opt = getopt(argc, argv, "c:mr")) != -1) {
+       while ((opt = getopt(argc, argv, "c:mrq")) != -1) {
                switch (opt) {
                case 'c':
                        count = atoi(optarg);
                        break;
                case 'm':
-                       recv_method = RECVMMSG;
+                       method = MMSG;
                        break;
                case 'r':
-                       recv_method = READ;
+                       method = READWRITE;
+                       break;
+               case 'q':
+                       quiet = true;
                        break;
                default: /* '?' */
                        fprintf(stderr, "Usage: %s [-c <count>] [-r] [-m]  [interface]\n",