xref: /dflybsd-src/test/sysperf/memcpy.c (revision 517258aaf3627e15903b4ff32cb5f8c564c2d5ad)
1ab4bc20fSMatthew Dillon /*
2ab4bc20fSMatthew Dillon  * memcpy.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 (*copyf)(const void *s1, void *d, size_t bytes));
10ab4bc20fSMatthew Dillon 
117141674dSMatthew Dillon #if 0
12ab4bc20fSMatthew Dillon extern void docopy1(const void *s, void *d, size_t bytes);
13ab4bc20fSMatthew Dillon extern void docopy2(const void *s, void *d, size_t bytes);
14ab4bc20fSMatthew Dillon extern void docopy3(const void *s, void *d, size_t bytes);
15ab4bc20fSMatthew Dillon extern void docopy4(const void *s, void *d, size_t bytes);
16ab4bc20fSMatthew Dillon extern void docopy5(const void *s, void *d, size_t bytes);
17ab4bc20fSMatthew Dillon extern void docopy6(const void *s, void *d, size_t bytes);
18ab4bc20fSMatthew Dillon extern void docopy7(const void *s, void *d, size_t bytes);
19ab4bc20fSMatthew Dillon extern void fpcleanup(void);
207141674dSMatthew Dillon #endif
21ab4bc20fSMatthew Dillon 
22ab4bc20fSMatthew Dillon int
main(int ac,char ** av)23ab4bc20fSMatthew Dillon main(int ac, char **av)
24ab4bc20fSMatthew Dillon {
25ab4bc20fSMatthew Dillon     int bytes;
26ab4bc20fSMatthew Dillon     char *ptr;
27ab4bc20fSMatthew Dillon     char *buf;
28ab4bc20fSMatthew Dillon 
29ab4bc20fSMatthew Dillon     if (ac == 1) {
30ab4bc20fSMatthew Dillon 	fprintf(stderr, "%s bytes\n", av[0]);
31ab4bc20fSMatthew Dillon 	exit(1);
32ab4bc20fSMatthew Dillon     }
33ab4bc20fSMatthew Dillon 
34ab4bc20fSMatthew Dillon     bytes = strtol(av[1], &ptr, 0);
35ab4bc20fSMatthew Dillon     switch(*ptr) {
36ab4bc20fSMatthew Dillon     case 'k':
37ab4bc20fSMatthew Dillon     case 'K':
38ab4bc20fSMatthew Dillon 	bytes *= 1024;
39ab4bc20fSMatthew Dillon 	break;
40ab4bc20fSMatthew Dillon     case 'm':
41ab4bc20fSMatthew Dillon     case 'M':
42ab4bc20fSMatthew Dillon 	bytes *= 1024 * 1024;
43ab4bc20fSMatthew Dillon 	break;
44ab4bc20fSMatthew Dillon     case 'g':
45ab4bc20fSMatthew Dillon     case 'G':
46ab4bc20fSMatthew Dillon 	bytes *= 1024 * 1024 * 1024;
47ab4bc20fSMatthew Dillon 	break;
48ab4bc20fSMatthew Dillon     case 0:
49ab4bc20fSMatthew Dillon 	break;
50ab4bc20fSMatthew Dillon     default:
51*517258aaSSascha Wildner 	fprintf(stderr, "suffix '%s' not understood\n", ptr);
52ab4bc20fSMatthew Dillon 	exit(1);
53ab4bc20fSMatthew Dillon     }
54ab4bc20fSMatthew Dillon     if (bytes <= 0 && (bytes & 127)) {
55ab4bc20fSMatthew Dillon 	fprintf(stderr, "# of bytes must be a multiple of 128\n");
56ab4bc20fSMatthew Dillon 	exit(1);
57ab4bc20fSMatthew Dillon     }
58ab4bc20fSMatthew Dillon     buf = mmap(NULL, bytes * 2, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
59ab4bc20fSMatthew Dillon     if (buf == MAP_FAILED) {
60ab4bc20fSMatthew Dillon 	perror("mmap/buffer");
61ab4bc20fSMatthew Dillon 	exit(1);
62ab4bc20fSMatthew Dillon     }
63ab4bc20fSMatthew Dillon     bzero(buf, bytes * 2);
64ab4bc20fSMatthew Dillon 
65ab4bc20fSMatthew Dillon     test_using("bcopy", buf, bytes, bcopy);
667141674dSMatthew Dillon #if 0
67ab4bc20fSMatthew Dillon     test_using("docopy1", buf, bytes, docopy1);
68ab4bc20fSMatthew Dillon     test_using("docopy2", buf, bytes, docopy2);
69ab4bc20fSMatthew Dillon     test_using("docopy3", buf, bytes, docopy3);
70ab4bc20fSMatthew Dillon     test_using("docopy4", buf, bytes, docopy4);
71ab4bc20fSMatthew Dillon     test_using("docopy5", buf, bytes, docopy5);
72ab4bc20fSMatthew Dillon     test_using("docopy6", buf, bytes, docopy6);
73ab4bc20fSMatthew Dillon     test_using("docopy7", buf, bytes, docopy7);
747141674dSMatthew Dillon #endif
75ab4bc20fSMatthew Dillon     return(0);
76ab4bc20fSMatthew Dillon }
77ab4bc20fSMatthew Dillon 
78ab4bc20fSMatthew Dillon void
test_using(const char * ctl,char * buf,int bytes,void (* copyf)(const void * s1,void * d,size_t bytes))79ab4bc20fSMatthew Dillon test_using(const char *ctl, char *buf, int bytes, void (*copyf)(const void *s1, void *d, size_t bytes))
80ab4bc20fSMatthew Dillon {
81ab4bc20fSMatthew Dillon     int i;
82ab4bc20fSMatthew Dillon     int loops;
83ab4bc20fSMatthew Dillon     long long us;
84ab4bc20fSMatthew Dillon 
85ab4bc20fSMatthew Dillon     start_timing();
86ab4bc20fSMatthew Dillon     for (i = 0; (i & 31) || stop_timing(0, NULL) == 0; ++i) {
87ab4bc20fSMatthew Dillon 	copyf(buf, buf + bytes, bytes);
88ab4bc20fSMatthew Dillon     }
89ab4bc20fSMatthew Dillon 
90ab4bc20fSMatthew Dillon     loops = i * 2;
91ab4bc20fSMatthew Dillon     start_timing();
92ab4bc20fSMatthew Dillon     for (i = loops - 1; i >= 0; --i) {
93ab4bc20fSMatthew Dillon 	copyf(buf, buf + bytes, bytes);
94ab4bc20fSMatthew Dillon     }
957141674dSMatthew Dillon #if 0
96ab4bc20fSMatthew Dillon     fpcleanup();
977141674dSMatthew Dillon #endif
98ab4bc20fSMatthew Dillon     stop_timing(loops, ctl);
99ab4bc20fSMatthew Dillon     us = get_timing();
100ab4bc20fSMatthew Dillon     printf("%s %d %5.2f MBytes/sec\n", ctl, bytes,
101ab4bc20fSMatthew Dillon 	(double)loops * (double)bytes / (double)us);
102ab4bc20fSMatthew Dillon }
103ab4bc20fSMatthew Dillon 
104