xref: /dflybsd-src/test/sysperf/blib.c (revision d0128b6a1789e266a43d07d9baf8200378bd4596)
16b055cd4SMatthew Dillon /*
26b055cd4SMatthew Dillon  * BLIB.C
36b055cd4SMatthew Dillon  *
46b055cd4SMatthew Dillon  * Simple benchmarking library
56b055cd4SMatthew Dillon  *
6fa72a36bSSimon Schubert  * $DragonFly: src/test/sysperf/blib.c,v 1.6 2007/08/21 19:23:46 corecode Exp $
76b055cd4SMatthew Dillon  */
86b055cd4SMatthew Dillon 
96b055cd4SMatthew Dillon #include <sys/types.h>
106b055cd4SMatthew Dillon #include <sys/time.h>
116b055cd4SMatthew Dillon #include <stdio.h>
126b055cd4SMatthew Dillon #include <stdlib.h>
136b055cd4SMatthew Dillon #include <stdarg.h>
146b055cd4SMatthew Dillon 
15a33ac161SMatthew Dillon static __thread struct timeval tv1;
16a33ac161SMatthew Dillon static __thread struct timeval tv2;
17a33ac161SMatthew Dillon static __thread long long last_us;
186b055cd4SMatthew Dillon 
196b055cd4SMatthew Dillon void
start_timing(void)206b055cd4SMatthew Dillon start_timing(void)
216b055cd4SMatthew Dillon {
226b055cd4SMatthew Dillon     gettimeofday(&tv1, NULL);
236b055cd4SMatthew Dillon }
246b055cd4SMatthew Dillon 
256b055cd4SMatthew Dillon int
stop_timing(long long count,const char * ctl,...)266b055cd4SMatthew Dillon stop_timing(long long count, const char *ctl, ...)
276b055cd4SMatthew Dillon {
286b055cd4SMatthew Dillon     long long us;
296b055cd4SMatthew Dillon     va_list va;
306b055cd4SMatthew Dillon 
316b055cd4SMatthew Dillon     gettimeofday(&tv2, NULL);
326b055cd4SMatthew Dillon     us = (tv2.tv_usec - tv1.tv_usec) + (tv2.tv_sec - tv1.tv_sec) * 1000000LL;
337141674dSMatthew Dillon     last_us = us;
346b055cd4SMatthew Dillon     if (ctl == NULL) 	/* dummy call to pre-cache */
356b055cd4SMatthew Dillon 	return(us > 1000000);
366b055cd4SMatthew Dillon 
376b055cd4SMatthew Dillon     va_start(va, ctl);
38fa72a36bSSimon Schubert     vfprintf(stderr, ctl, va);
396b055cd4SMatthew Dillon     va_end(va);
406b055cd4SMatthew Dillon 
41*d0128b6aSMatthew Dillon     fprintf(stderr, " %6.3fs %lld loops, %8.0f loops/sec, %6.3fuS/loop\n",
426b055cd4SMatthew Dillon 	(double)us / 1000000.0,
436b055cd4SMatthew Dillon 	count,
44*d0128b6aSMatthew Dillon 	(double)count * 1e6 / (double)us,
456b055cd4SMatthew Dillon 	(double)us / (double)count
466b055cd4SMatthew Dillon     );
4775a0ef5dSMatthew Dillon 
4875a0ef5dSMatthew Dillon     tv1 = tv2;
4975a0ef5dSMatthew Dillon 
506b055cd4SMatthew Dillon     return(0);
516b055cd4SMatthew Dillon }
526b055cd4SMatthew Dillon 
53b40d9440SMatthew Dillon int
stop_timing2(long long count,long long us,const char * ctl,...)54b40d9440SMatthew Dillon stop_timing2(long long count, long long us, const char *ctl, ...)
55b40d9440SMatthew Dillon {
56b40d9440SMatthew Dillon     va_list va;
57b40d9440SMatthew Dillon 
58b40d9440SMatthew Dillon     va_start(va, ctl);
59fa72a36bSSimon Schubert     vfprintf(stderr, ctl, va);
60b40d9440SMatthew Dillon     va_end(va);
61b40d9440SMatthew Dillon 
62fa72a36bSSimon Schubert     fprintf(stderr, " %6.3fs %lld loops = %6.3fnS/loop\n",
63b40d9440SMatthew Dillon 	(double)us / 1000000.0,
64b40d9440SMatthew Dillon 	count,
65b40d9440SMatthew Dillon 	(double)us * 1000.0 / (double)count
66b40d9440SMatthew Dillon     );
67b40d9440SMatthew Dillon     return(0);
68b40d9440SMatthew Dillon }
69b40d9440SMatthew Dillon 
706b055cd4SMatthew Dillon long long
get_timing(void)716b055cd4SMatthew Dillon get_timing(void)
726b055cd4SMatthew Dillon {
737141674dSMatthew Dillon     return (last_us);
746b055cd4SMatthew Dillon }
756b055cd4SMatthew Dillon 
768faadb75SMatthew Dillon void
nop(void)778faadb75SMatthew Dillon nop(void)
788faadb75SMatthew Dillon {
798faadb75SMatthew Dillon }
80