1 /* $NetBSD: in_cksum.c,v 1.2 2008/01/27 16:49:13 chris Exp $ */
3 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
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>
36 #include <sys/resource.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
;
49 free_mbuf_chain(struct mbuf
*m
)
58 free_mbuf_chain(next
);
62 allocate_mbuf_chain(char **lens
)
71 off
= random_aligned
? rand() % 64 : 0;
73 m
= malloc(sizeof(struct m_hdr
) + len
+ off
);
75 err(EXIT_FAILURE
, "malloc failed");
77 m
->m_data
= (char *)m
+ sizeof(struct m_hdr
) + off
;
80 m
->m_next
= allocate_mbuf_chain(lens
+ 1);
86 randomise_mbuf_chain(struct mbuf
*m
)
90 for (i
= 0; i
< m
->m_len
; i
+= sizeof(int)) {
92 if (i
+ sizeof(int) < m
->m_len
)
96 memcpy(m
->m_data
+ i
, &data
, len
);
99 randomise_mbuf_chain(m
->m_next
);
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
)
115 struct timeval tv
, old_tv
;
116 int loops
, old_sum
, new_sum
, off
, len
;
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
)
131 randomise_mbuf_chain(m
);
135 /* force one loop over all data */
139 off
= len
? rand() % len
: 0;
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
);
150 getrusage(RUSAGE_SELF
, &res
);
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
);
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));