7 #include <netinet/in.h>
8 #include <netinet/tcp.h>
15 #include <sys/sendfile.h>
16 #include <sys/socket.h>
18 #include <sys/types.h>
25 #define TARGET_HOSTNAME "localhost"
27 #define BYTES (128*1024*1024UL)
28 #define BUFSIZE (64*1024)
30 #define NR (BYTES/BUFSIZE)
32 #define SENDFILE_LOOPS 10
33 #define SPLICE_LOOPS 10
34 #define SPLICE_PIPE_LOOPS 10
36 static int sendfile_loops = SENDFILE_LOOPS;
37 static int splice_loops = SPLICE_LOOPS;
38 static int splice_pipe_loops = SPLICE_PIPE_LOOPS;
40 static volatile long long *cycles, cycles_per_sec;
42 static struct timeval start_time;
43 static double start_cycles;
44 static double cpu_pct;
46 static void start_timing(const char *desc)
48 printf("%-20s: ", desc);
50 gettimeofday(&start_time, NULL);
52 * Give the lowprio cycles thread a chance to run and thus
53 * we get an accurate timestamp:
56 start_cycles = (double)*cycles;
59 static double end_timing(unsigned long long bytes, double *rate)
61 static long long total;
62 struct timeval end_time;
64 double end_cycles, cpu_cycles;
66 gettimeofday(&end_time, NULL);
67 end_cycles = (double)*cycles;
69 usecs = (double) (end_time.tv_sec - start_time.tv_sec);
71 usecs += (double) (end_time.tv_usec - start_time.tv_usec);
74 cpu_cycles = end_cycles - start_cycles;
76 cpu_cycles / cycles_per_sec / ( usecs / 1000000.0 ) * 100.0;
78 *rate = (double) bytes / usecs / (1024*1024) * 1000000;
80 printf("%.2fMB/s (%.1fMB total, %.2f%% CPU)\n", *rate,
81 (double) total / (1024*1024),
88 static void calibrate_loops(void)
94 printf("calibrating cycles: "); fflush(stdout);
97 * Make sure we start on a precise timer IRQ boundary:
101 for (i = 0; i < 10; i++) {
106 cycles_per_sec = max(cycles_per_sec, l1-l0);
110 printf("%Ld cycles/sec\n", cycles_per_sec);
113 static int child(struct sockaddr *addr, int len)
115 static char buffer[BUFSIZE];
119 struct sockaddr_in s_to;
121 double r1, r2, r3, r4, r5;
125 r1 = r2 = r3 = r4 = r5 = 0;
127 sk = socket(PF_INET, SOCK_STREAM, 0);
129 return error("socket");
130 hp = gethostbyname (TARGET_HOSTNAME);
132 bzero ((char *) &s_to, sizeof (s_to));
133 bcopy ((char *) hp->h_addr, (char *) &(s_to.sin_addr), hp->h_length);
134 s_to.sin_family = hp->h_addrtype;
135 s_to.sin_port = htons(1111);
139 fprintf(stdout, "BUFSIZE = %d\n", BUFSIZE);
142 if (getenv("EMPTY")) {
143 if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
144 return error("connect");
146 start_timing("Empty buffer");
147 for (i = 0; i < NR; i++)
148 write(sk, buffer, BUFSIZE);
149 end_timing(NR*BUFSIZE, &r1);
151 fd = open("largefile", O_RDONLY);
153 return error("largefile");
155 start_timing("Read/write loop");
156 for (i = 0; i < NR; i++) {
157 if (read(fd, buffer, BUFSIZE) != BUFSIZE)
158 return error("largefile read");
159 write(sk, buffer, BUFSIZE);
161 end_timing(NR*BUFSIZE, &r2);
167 start_timing("sendfile");
169 sk = socket(PF_INET, SOCK_STREAM, 0);
170 if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
171 return error("connect");
173 fd = open("largefile", O_RDONLY);
175 return error("largefile");
179 int ret = sendfile(sk, fd, NULL, i);
185 if (--sendfile_loops)
187 c1 = end_timing(NR*BUFSIZE*SENDFILE_LOOPS, &r3);
191 if (getenv("SPLICE_PIPE")) {
192 start_timing("splice-pipe");
194 sk = socket(PF_INET, SOCK_STREAM, 0);
195 if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
196 return error("connect");
198 fd = open("largefile", O_RDONLY);
200 return error("largefile");
201 if (pipe(pipefd) < 0)
202 return error("pipe");
207 int ret = splice(fd, &off, pipefd[1], NULL, min(i, BUFSIZE), SPLICE_F_NONBLOCK);
209 return error("splice-pipe-in");
212 int flags = i ? SPLICE_F_MORE : 0;
213 int written = splice(pipefd[0], NULL, sk, NULL, ret, flags);
215 return error("splice-pipe-out");
224 if (--splice_pipe_loops)
225 goto splice_pipe_again;
226 c2 = end_timing(NR*BUFSIZE*SPLICE_LOOPS, &r4);
230 if (getenv("SPLICE")) {
231 start_timing("splice");
233 sk = socket(PF_INET, SOCK_STREAM, 0);
234 if (connect(sk, (struct sockaddr *)&s_to, len) < 0)
235 return error("connect");
237 fd = open("largefile", O_RDONLY);
239 return error("largefile");
244 int flags = BUFSIZE < i ? SPLICE_F_MORE : 0;
247 ret = splice(fd, &off, sk, NULL, min(i, BUFSIZE), flags);
250 return error("splice");
258 c3 = end_timing(NR*BUFSIZE*SPLICE_LOOPS, &r5);
264 * c2/r4 - splice-pipe
269 printf("sendfile is %.2f%% more efficient than splice-pipe.\n",
270 (c2 - c1) / c1 * 100.0 );
272 printf("sendfile is %.2f%% more efficient than splice.\n",
273 (c3 - c1) / c1 * 100.0 );
275 printf("splice is %.2f%% more efficient splice-pipe.\n",
276 (c2 - c3) / c3 * 100.0 );
278 printf("sendfile is %.2f%% faster than splice-pipe.\n",
279 (r3 - r4) / r4 * 100.0 );
281 printf("sendfile is %.2f%% faster than splice.\n",
282 (r3 - r5) / r5 * 100.0 );
284 printf("splice is %.2f%% faster than splice-pipe.\n",
285 (r5 - r4) / r4 * 100.0 );
291 static void setup_shared_var(void)
293 char zerobuff [4096] = { 0, };
296 fd = creat(".tmp_mmap", 0700);
300 fd = open(".tmp_mmap", O_RDWR|O_CREAT|O_TRUNC);
302 ret = write(fd, zerobuff, 4096);
305 cycles = (void *)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
306 BUG_ON(cycles == (void *)-1);
311 #define SCHED_BATCH 3
314 # define rdtscll(val) \
316 val = *__mm_clock_dev; \
318 #elif defined(__i386__)
319 # define rdtscll(val) \
321 __asm__ __volatile__("rdtsc" : "=A" (val)); \
324 # define rdtscll(val) \
325 do { (val) = 0LL; } while (0)
329 * Keep lowprio looping - to meausure the number of idle cycles
330 * available. It's tricky: we do a series of RDTSC calls, and
331 * if the delay to the last measurement was less than 500 cycles,
332 * we conclude that only this loop ran.
334 static void lowprio_cycle_soak_loop(void)
336 struct sched_param p = { sched_priority: 0 };
337 unsigned long long t0, t1, delta;
340 * We are a nice +19 SCHED_BATCH task:
342 BUG_ON(sched_setscheduler(0, SCHED_BATCH, &p) != 0);
346 while (cycles >= 0) {
355 int main(int argc, char **argv)
357 unsigned int sk, len;
358 struct sockaddr addr;
363 signal(SIGCHLD, SIG_IGN);
364 sk = socket(PF_INET, SOCK_STREAM, 0);
369 if (listen(sk, 1) < 0) {
374 if (getsockname(sk, &addr, &len) < 0) {
375 perror("getsockname");
380 lowprio_cycle_soak_loop();