Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / sys / net / in_cksum / in_cksum.c
blobd638403c1163ade1bee6b83bc6db8c7b1e62efff
1 /* $NetBSD: in_cksum.c,v 1.2 2008/01/27 16:49:13 chris Exp $ */
2 /*-
3 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.2 2008/01/27 16:49:13 chris Exp $");
34 #include <sys/param.h>
35 #include <sys/mbuf.h>
36 #include <sys/resource.h>
37 #include <err.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
43 int portable_cpu_in_cksum(struct mbuf*, int, int, uint32_t);
44 int cpu_in_cksum(struct mbuf*, int, int, uint32_t);
46 static bool random_aligned;
48 static void
49 free_mbuf_chain(struct mbuf *m)
51 struct mbuf *next;
53 if (m == NULL)
54 return;
56 next = m->m_next;
57 free(m);
58 free_mbuf_chain(next);
61 static struct mbuf *
62 allocate_mbuf_chain(char **lens)
64 int len, off;
65 struct mbuf *m;
67 if (*lens == NULL)
68 return NULL;
70 len = atoi(*lens);
71 off = random_aligned ? rand() % 64 : 0;
73 m = malloc(sizeof(struct m_hdr) + len + off);
74 if (m == NULL)
75 err(EXIT_FAILURE, "malloc failed");
77 m->m_data = (char *)m + sizeof(struct m_hdr) + off;
78 m->m_len = len;
80 m->m_next = allocate_mbuf_chain(lens + 1);
82 return m;
85 static void
86 randomise_mbuf_chain(struct mbuf *m)
88 int i, data, len;
90 for (i = 0; i < m->m_len; i += sizeof(int)) {
91 data = rand();
92 if (i + sizeof(int) < m->m_len)
93 len = sizeof(int);
94 else
95 len = m->m_len - i;
96 memcpy(m->m_data + i, &data, len);
98 if (m->m_next)
99 randomise_mbuf_chain(m->m_next);
102 static int
103 mbuf_len(struct mbuf *m)
105 return m == NULL ? 0 : m->m_len + mbuf_len(m->m_next);
108 int in_cksum_portable(struct mbuf *, int);
109 int in_cksum(struct mbuf *, int);
112 main(int argc, char **argv)
114 struct rusage res;
115 struct timeval tv, old_tv;
116 int loops, old_sum, new_sum, off, len;
117 long i, iterations;
118 uint32_t init_sum;
119 struct mbuf *m;
121 if (argc < 4)
122 err(1, "%s loops unalign iterations ...", argv[0]);
124 loops = atoi(argv[1]);
125 random_aligned = atoi(argv[2]);
126 iterations = atol(argv[3]);
128 for (; loops; --loops) {
129 if ((m = allocate_mbuf_chain(argv + 4)) == NULL)
130 continue;
131 randomise_mbuf_chain(m);
132 init_sum = rand();
133 len = mbuf_len(m);
135 /* force one loop over all data */
136 if (loops == 1)
137 off = 0;
138 else
139 off = len ? rand() % len : 0;
141 len -= off;
142 old_sum = portable_cpu_in_cksum(m, len, off, init_sum);
143 new_sum = cpu_in_cksum(m, len, off, init_sum);
144 if (old_sum != new_sum)
145 errx(1, "comparision failed: %x %x", old_sum, new_sum);
147 if (iterations == 0)
148 continue;
150 getrusage(RUSAGE_SELF, &res);
151 tv = res.ru_utime;
152 for (i = iterations; i; --i)
153 (void)portable_cpu_in_cksum(m, len, off, init_sum);
154 getrusage(RUSAGE_SELF, &res);
155 timersub(&res.ru_utime, &tv, &old_tv);
156 printf("portable version: %ld.%06ld\n", (long)old_tv.tv_sec, (long)old_tv.tv_usec);
158 getrusage(RUSAGE_SELF, &res);
159 tv = res.ru_utime;
160 for (i = iterations; i; --i)
161 (void)cpu_in_cksum(m, len, off, init_sum);
162 getrusage(RUSAGE_SELF, &res);
163 timersub(&res.ru_utime, &tv, &tv);
164 printf("test version: %ld.%06ld\n", (long)tv.tv_sec, (long)tv.tv_usec);
165 printf("relative time: %3.f%%\n",
166 100 * ((float)tv.tv_sec * 1e6 + tv.tv_usec) /
167 ((float)old_tv.tv_sec * 1e6 + old_tv.tv_usec + 1));
169 free_mbuf_chain(m);
172 return 0;