Fix various compile warnings
[splice.git] / splice-test4c.c
index 1cbc078053a338754abbefa03552a2cda386ed30..fc5617a7d9d9bed63df2b9ae1daeebff0d3db384 100644 (file)
@@ -25,7 +25,7 @@
 #define TARGET_HOSTNAME "localhost"
 
 #define BYTES (128*1024*1024UL)
-#define BUFSIZE (64*1024)
+#define BUFSIZE (64*1024U)
 
 #define NR (BYTES/BUFSIZE)
 
 #define SPLICE_PIPE_LOOPS 10
 
 static int sendfile_loops = SENDFILE_LOOPS;
-static int splice_loops = SPLICE_LOOPS;
 static int splice_pipe_loops = SPLICE_PIPE_LOOPS;
+#if 0
+static int splice_loops = SPLICE_LOOPS;
+#endif
 
 static volatile long long *cycles, cycles_per_sec;
 
@@ -110,7 +112,7 @@ static void calibrate_loops(void)
        printf("%Ld cycles/sec\n", cycles_per_sec);
 }
 
-static int child(struct sockaddr *addr, int len)
+static int child(void)
 {
        static char buffer[BUFSIZE];
        int sk;
@@ -119,7 +121,8 @@ static int child(struct sockaddr *addr, int len)
        struct sockaddr_in s_to;
        struct hostent *hp;
        double r1, r2, r3, r4, r5;
-       int i, pipefd[2];
+       unsigned int i;
+       int pipefd[2];
        loff_t off = 0;
 
        r1 = r2 = r3 = r4 = r5 = 0;
@@ -139,125 +142,125 @@ static int child(struct sockaddr *addr, int len)
        fprintf(stdout, "BUFSIZE = %d\n", BUFSIZE);
        fflush(stdout);
 
-       if (getenv("EMPTY")) {
-               if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
-                       return error("connect");
+       if (connect(sk, (struct sockaddr *)&s_to, sizeof(s_to)) < 0)
+               return error("connect");
 
-               start_timing("Empty buffer");
-               for (i = 0; i < NR; i++)
-                       write(sk, buffer, BUFSIZE);
-               end_timing(NR*BUFSIZE, &r1);
-
-               fd = open("largefile", O_RDONLY);
-               if (fd < 0)
-                       return error("largefile");
-
-               start_timing("Read/write loop");
-               for (i = 0; i < NR; i++) {
-                       if (read(fd, buffer, BUFSIZE) != BUFSIZE)
-                               return error("largefile read");
-                       write(sk, buffer, BUFSIZE);
-               }
-               end_timing(NR*BUFSIZE, &r2);
-               close(fd);
-               close(sk);
+       start_timing("Empty buffer");
+       for (i = 0; i < NR; i++) {
+               if (write(sk, buffer, BUFSIZE) != BUFSIZE)
+                       return error("empty buffer write");
        }
+       end_timing(NR*BUFSIZE, &r1);
+
+       fd = open("largefile", O_RDONLY);
+       if (fd < 0)
+               return error("largefile");
+
+       start_timing("Read/write loop");
+       for (i = 0; i < NR; i++) {
+               if (read(fd, buffer, BUFSIZE) != BUFSIZE)
+                       return error("largefile read");
+               if (write(sk, buffer, BUFSIZE) != BUFSIZE)
+                       return error("largefile write");
+       }
+       end_timing(NR*BUFSIZE, &r2);
+       close(fd);
+       close(sk);
 
-       if (getenv("SF")) {
-               start_timing("sendfile");
+       start_timing("sendfile");
 sendfile_again:
-               sk = socket(PF_INET, SOCK_STREAM, 0);
-               if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
-                       return error("connect");
-
-               fd = open("largefile", O_RDONLY);
-               if (fd < 0)
-                       return error("largefile");
-
-               i = NR*BUFSIZE;
-               do {
-                       int ret = sendfile(sk, fd, NULL, i);
-                       i -= ret;
-               } while (i);
-
-               close(fd);
-               close(sk);
-               if (--sendfile_loops)
-                       goto sendfile_again;
-               c1 = end_timing(NR*BUFSIZE*SENDFILE_LOOPS, &r3);
-       } else
-               c1 = 0;
-
-       if (getenv("SPLICE_PIPE")) {
-               start_timing("splice-pipe");
+       sk = socket(PF_INET, SOCK_STREAM, 0);
+       if (connect(sk, (struct sockaddr *)&s_to, sizeof(s_to)) < 0)
+               return error("connect");
+
+       fd = open("largefile", O_RDONLY);
+       if (fd < 0)
+               return error("largefile");
+
+       i = NR*BUFSIZE;
+       do {
+               int ret = sendfile(sk, fd, NULL, i);
+               i -= ret;
+       } while (i);
+
+       close(fd);
+       close(sk);
+       if (--sendfile_loops)
+               goto sendfile_again;
+       c1 = end_timing(NR*BUFSIZE*SENDFILE_LOOPS, &r3);
+
+       start_timing("splice-pipe");
 splice_pipe_again:
-               sk = socket(PF_INET, SOCK_STREAM, 0);
-               if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
-                       return error("connect");
-
-               fd = open("largefile", O_RDONLY);
-               if (fd < 0)
-                       return error("largefile");
-               if (pipe(pipefd) < 0)
-                       return error("pipe");
-
-               i = NR*BUFSIZE;
-               off = 0;
-               do {
-                       int ret = splice(fd, &off, pipefd[1], NULL, min(i, BUFSIZE), SPLICE_F_NONBLOCK);
-                       if (ret <= 0)
-                               return error("splice-pipe-in");
-                       i -= ret;
-                       while (ret > 0) {
-                               int flags = i ? SPLICE_F_MORE : 0;
-                               int written = splice(pipefd[0], NULL, sk, NULL, ret, flags);
-                               if (written <= 0)
-                                       return error("splice-pipe-out");
-                               ret -= written;
-                       }
-               } while (i);
-
-               close(fd);
-               close(sk);
-               close(pipefd[0]);
-               close(pipefd[1]);
-               if (--splice_pipe_loops)
-                       goto splice_pipe_again;
-               c2 = end_timing(NR*BUFSIZE*SPLICE_LOOPS, &r4);
-       } else
-               c2 = 0;
-
-       if (getenv("SPLICE")) {
-               start_timing("splice");
+       sk = socket(PF_INET, SOCK_STREAM, 0);
+       if (connect(sk, (struct sockaddr *)&s_to, sizeof(s_to)) < 0)
+               return error("connect");
+
+       fd = open("largefile", O_RDONLY);
+       if (fd < 0)
+               return error("largefile");
+       if (pipe(pipefd) < 0)
+               return error("pipe");
+
+       i = NR*BUFSIZE;
+       off = 0;
+       do {
+               int ret = ssplice(fd, &off, pipefd[1], NULL, min(i, BUFSIZE), SPLICE_F_NONBLOCK);
+               if (ret <= 0)
+                       return error("splice-pipe-in");
+               i -= ret;
+               while (ret > 0) {
+                       int flags = i ? SPLICE_F_MORE : 0;
+                       int written = ssplice(pipefd[0], NULL, sk, NULL, ret, flags);
+                       if (written <= 0)
+                               return error("splice-pipe-out");
+                       ret -= written;
+               }
+       } while (i);
+
+       close(fd);
+       close(sk);
+       close(pipefd[0]);
+       close(pipefd[1]);
+       if (--splice_pipe_loops)
+               goto splice_pipe_again;
+       c2 = end_timing(NR*BUFSIZE*SPLICE_LOOPS, &r4);
+
+       /*
+        * Direct splicing was disabled as being immediately available,
+        * it's reserved for sendfile emulation now.
+        */
+#if 0
+       start_timing("splice");
 splice_again:
-               sk = socket(PF_INET, SOCK_STREAM, 0);
-               if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
-                       return error("connect");
-
-               fd = open("largefile", O_RDONLY);
-               if (fd < 0)
-                       return error("largefile");
-
-               i = NR*BUFSIZE;
-               off = 0;
-               do {
-                       int flags = BUFSIZE < i ? SPLICE_F_MORE : 0;
-                       int ret;
-
-                       ret = splice(fd, &off, sk, NULL, min(i, BUFSIZE), flags);
-
-                       if (ret <= 0)
-                               return error("splice");
-                       i -= ret;
-               } while (i);
-
-               close(fd);
-               close(sk);
-               if (--splice_loops)
-                       goto splice_again;
-               c3 = end_timing(NR*BUFSIZE*SPLICE_LOOPS, &r5);
-       } else
-               c3 = 0;
+       sk = socket(PF_INET, SOCK_STREAM, 0);
+       if (connect(sk, (struct sockaddr *)&s_to, sizeof(s_to)) < 0)
+               return error("connect");
+
+       fd = open("largefile", O_RDONLY);
+       if (fd < 0)
+               return error("largefile");
+
+       i = NR*BUFSIZE;
+       off = 0;
+       do {
+               int flags = BUFSIZE < i ? SPLICE_F_MORE : 0;
+               int ret;
+
+               ret = ssplice(fd, &off, sk, NULL, min(i, BUFSIZE), flags);
+
+               if (ret <= 0)
+                       return error("splice");
+               i -= ret;
+       } while (i);
+
+       close(fd);
+       close(sk);
+       if (--splice_loops)
+               goto splice_again;
+       c3 = end_timing(NR*BUFSIZE*SPLICE_LOOPS, &r5);
+#else
+       c3 = 0;
+#endif
 
        /*
         * c1/r3 - sendfile
@@ -297,7 +300,7 @@ static void setup_shared_var(void)
        BUG_ON(fd == -1);
        close(fd);
 
-       fd = open(".tmp_mmap", O_RDWR|O_CREAT|O_TRUNC);
+       fd = open(".tmp_mmap", O_RDWR|O_CREAT|O_TRUNC, 0644);
        BUG_ON(fd == -1);
        ret = write(fd, zerobuff, 4096);
        BUG_ON(ret != 4096);
@@ -310,18 +313,27 @@ static void setup_shared_var(void)
 
 #define SCHED_BATCH 3
 
-#ifdef __ia64__
-# define rdtscll(val)                                  \
+#if defined(__i386__)
+#define rdtscll(val)                                   \
 do {                                                   \
-       val = *__mm_clock_dev;                          \
+       __asm__ __volatile__("rdtsc" : "=A" (val));     \
+} while (0)
+#elif defined(__x86_64__)
+#define rdtscll(val)                                           \
+do {                                                           \
+       uint64_t lo, hi;                                        \
+       __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));   \
+       (val) = (hi << 32) | lo;                                \
 } while (0)
-#elif defined(__i386__)
-# define rdtscll(val)                                  \
+#if 0
+#elif defined(__ia64__)
+#define rdtscll(val)                                   \
 do {                                                   \
-       __asm__ __volatile__("rdtsc" : "=A" (val));     \
+       val = *__mm_clock_dev;                          \
 } while (0)
+#endif
 #else
-# define rdtscll(val) \
+#define rdtscll(val) \
        do { (val) = 0LL; } while (0)
 #endif
 
@@ -340,7 +352,8 @@ static void lowprio_cycle_soak_loop(void)
         * We are a nice +19 SCHED_BATCH task:
         */
        BUG_ON(sched_setscheduler(0, SCHED_BATCH, &p) != 0);
-       nice(40);
+       if (nice(40) < 0)
+               perror("nice");
 
        rdtscll(t0);
        while (cycles >= 0) {
@@ -352,36 +365,24 @@ static void lowprio_cycle_soak_loop(void)
        }
 }
 
-int main(int argc, char **argv)
+int main(__attribute__((__unused__)) int argc, __attribute__((__unused__)) char **argv)
 {
-       unsigned int sk, len;
-       struct sockaddr addr;
        pid_t pid;
 
        setup_shared_var();
 
        signal(SIGCHLD, SIG_IGN);
-       sk = socket(PF_INET, SOCK_STREAM, 0);
-       if (sk < 0) {
-               perror("socket");
-               exit(1);
-       }
-       if (listen(sk, 1) < 0) {
-               perror("listen");
-               exit(1);
-       }
-       len = sizeof(addr);
-       if (getsockname(sk, &addr, &len) < 0) {
-               perror("getsockname");
-               exit(1);
-       }
+
        pid = fork();
        if (!pid) {
                lowprio_cycle_soak_loop();
                exit(0);
        }
-       nice(-20);
-       child(&addr, len);
+
+       if (nice(-20) < 0)
+               perror("nice");
+
+       child();
        kill(pid, SIGHUP);
        exit(0);
 }