1ab4bc20fSMatthew Dillon /*
2ab4bc20fSMatthew Dillon * memzero.c
3ab4bc20fSMatthew Dillon */
4ab4bc20fSMatthew Dillon
5ab4bc20fSMatthew Dillon #include "blib.h"
6ab4bc20fSMatthew Dillon
7ab4bc20fSMatthew Dillon int glob[16384];
8ab4bc20fSMatthew Dillon
9ab4bc20fSMatthew Dillon void test_using(const char *ctl, char *buf, int bytes, void (*zerof)(void *d, size_t bytes));
10ab4bc20fSMatthew Dillon
11ab4bc20fSMatthew Dillon extern void dozero1(void *d, size_t bytes);
12ab4bc20fSMatthew Dillon extern void dozero2(void *d, size_t bytes);
13ab4bc20fSMatthew Dillon extern void dozero3(void *d, size_t bytes);
14ab4bc20fSMatthew Dillon extern void dozero4(void *d, size_t bytes);
15ab4bc20fSMatthew Dillon extern void dozero5(void *d, size_t bytes);
16ab4bc20fSMatthew Dillon extern void dozero6(void *d, size_t bytes);
17ab4bc20fSMatthew Dillon extern void dozero7(void *d, size_t bytes);
18ab4bc20fSMatthew Dillon extern void fpcleanup(void);
19ab4bc20fSMatthew Dillon
20ab4bc20fSMatthew Dillon int
main(int ac,char ** av)21ab4bc20fSMatthew Dillon main(int ac, char **av)
22ab4bc20fSMatthew Dillon {
23ab4bc20fSMatthew Dillon int bytes;
24ab4bc20fSMatthew Dillon char *ptr;
25ab4bc20fSMatthew Dillon char *buf;
26ab4bc20fSMatthew Dillon
27ab4bc20fSMatthew Dillon if (ac == 1) {
28ab4bc20fSMatthew Dillon fprintf(stderr, "%s bytes\n", av[0]);
29ab4bc20fSMatthew Dillon exit(1);
30ab4bc20fSMatthew Dillon }
31ab4bc20fSMatthew Dillon
32ab4bc20fSMatthew Dillon bytes = strtol(av[1], &ptr, 0);
33ab4bc20fSMatthew Dillon switch(*ptr) {
34ab4bc20fSMatthew Dillon case 'k':
35ab4bc20fSMatthew Dillon case 'K':
36ab4bc20fSMatthew Dillon bytes *= 1024;
37ab4bc20fSMatthew Dillon break;
38ab4bc20fSMatthew Dillon case 'm':
39ab4bc20fSMatthew Dillon case 'M':
40ab4bc20fSMatthew Dillon bytes *= 1024 * 1024;
41ab4bc20fSMatthew Dillon break;
42ab4bc20fSMatthew Dillon case 'g':
43ab4bc20fSMatthew Dillon case 'G':
44ab4bc20fSMatthew Dillon bytes *= 1024 * 1024 * 1024;
45ab4bc20fSMatthew Dillon break;
46ab4bc20fSMatthew Dillon case 0:
47ab4bc20fSMatthew Dillon break;
48ab4bc20fSMatthew Dillon default:
49*517258aaSSascha Wildner fprintf(stderr, "suffix '%s' not understood\n", ptr);
50ab4bc20fSMatthew Dillon exit(1);
51ab4bc20fSMatthew Dillon }
52ab4bc20fSMatthew Dillon if (bytes <= 0 && (bytes & 127)) {
53ab4bc20fSMatthew Dillon fprintf(stderr, "# of bytes must be a multiple of 128\n");
54ab4bc20fSMatthew Dillon exit(1);
55ab4bc20fSMatthew Dillon }
56ab4bc20fSMatthew Dillon
57ab4bc20fSMatthew Dillon buf = mmap(NULL, bytes * 2, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
58ab4bc20fSMatthew Dillon if (buf == MAP_FAILED) {
59ab4bc20fSMatthew Dillon perror("mmap/buffer");
60ab4bc20fSMatthew Dillon exit(1);
61ab4bc20fSMatthew Dillon }
62ab4bc20fSMatthew Dillon bzero(buf, bytes * 2);
63ab4bc20fSMatthew Dillon
64ab4bc20fSMatthew Dillon test_using("bzero", buf, bytes, (void *)bzero);
657141674dSMatthew Dillon #if 0
66ab4bc20fSMatthew Dillon test_using("dozero1", buf, bytes, dozero1);
67ab4bc20fSMatthew Dillon test_using("dozero2", buf, bytes, dozero2);
68ab4bc20fSMatthew Dillon test_using("dozero3", buf, bytes, dozero3);
69ab4bc20fSMatthew Dillon test_using("dozero4", buf, bytes, dozero4);
70ab4bc20fSMatthew Dillon test_using("dozero5", buf, bytes, dozero5);
71ab4bc20fSMatthew Dillon test_using("dozero6", buf, bytes, dozero6);
72ab4bc20fSMatthew Dillon test_using("dozero7", buf, bytes, dozero7);
737141674dSMatthew Dillon #endif
74ab4bc20fSMatthew Dillon return(0);
75ab4bc20fSMatthew Dillon }
76ab4bc20fSMatthew Dillon
77ab4bc20fSMatthew Dillon void
test_using(const char * ctl,char * buf,int bytes,void (* zerof)(void * d,size_t bytes))78ab4bc20fSMatthew Dillon test_using(const char *ctl, char *buf, int bytes, void (*zerof)(void *d, size_t bytes))
79ab4bc20fSMatthew Dillon {
80ab4bc20fSMatthew Dillon int i;
81ab4bc20fSMatthew Dillon int loops;
82ab4bc20fSMatthew Dillon long long us;
83ab4bc20fSMatthew Dillon
84ab4bc20fSMatthew Dillon start_timing();
85ab4bc20fSMatthew Dillon for (i = 0; (i & 31) || stop_timing(0, NULL) == 0; ++i) {
86ab4bc20fSMatthew Dillon zerof(buf, bytes);
87ab4bc20fSMatthew Dillon }
88ab4bc20fSMatthew Dillon
89ab4bc20fSMatthew Dillon loops = i * 2;
90ab4bc20fSMatthew Dillon start_timing();
91ab4bc20fSMatthew Dillon for (i = loops - 1; i >= 0; --i) {
92ab4bc20fSMatthew Dillon zerof(buf, bytes);
93ab4bc20fSMatthew Dillon }
947141674dSMatthew Dillon #if 0
95ab4bc20fSMatthew Dillon fpcleanup();
967141674dSMatthew Dillon #endif
97ab4bc20fSMatthew Dillon stop_timing(loops, ctl);
98ab4bc20fSMatthew Dillon us = get_timing();
99ab4bc20fSMatthew Dillon printf("%s %d %5.2f MBytes/sec\n", ctl, bytes,
100ab4bc20fSMatthew Dillon (double)loops * (double)bytes / (double)us);
101ab4bc20fSMatthew Dillon }
102