1 /* Copyright (c) 2013 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Basic perf test for small functions. Will run them in a loop and give you
6 * the average cost per iteration. It'll run them both as an SCP and an MCP.
8 * To use this, define a function of the form:
10 * void my_test(unsigned long nr_loops)
12 * Which does some computation you wish to measure inside a loop that run
13 * nr_loops times. Then in microb_test(), add your function in a line such as:
15 * test_time_ns(my_test, 100000);
17 * This macro will run your test and print the results. Pick a loop amount that
18 * is reasonable for your operation. You can also use test_time_us() for longer
22 * - I went with this style so you could do some prep work before and after the
23 * loop (instead of having a macro build the loop). It's what I needed.
24 * - Be sure to double check the ASM inside the loop to make sure the compiler
25 * isn't optimizing out your main work.
26 * - Make sure your function isn't static. If it is static (and even if it is
27 * __attribute__((noinline))), if the function is called only once, the
28 * compiler will compile it differently (specifically, it will hardcode the
29 * number of loops into the function, instead of taking a parameter).
30 * Suddenly, the existence of a second test of the same function could change
31 * the performance of *both* test runs. Incidentally, when this happened to
32 * me, the tests were *better* when this optimization didn't happen. The way
33 * to avoid the optimization completely is to have extern functions, since the
34 * compiler can't assume it is only called once. Though technically they
35 * still could do some optimizations, and the only really safe way is to put
36 * the tests in another .c file. */
44 /* OS dependent #incs */
45 #include <parlib/parlib.h>
46 #include <parlib/vcore.h>
47 #include <parlib/timing.h>
49 static uint32_t __get_pcoreid(void)
51 return __procinfo.vcoremap[vcore_id()].pcoreid;
54 /* Testing functions here */
56 void set_tlsdesc_test(unsigned long nr_loops)
59 uint32_t vcoreid = vcore_id();
60 void *mytls = get_tls_desc();
61 void *vctls = get_vcpd_tls_desc(vcoreid);
62 segdesc_t tmp = SEG(STA_W, (uint32_t)vctls, 0xffffffff, 3);
63 uint32_t gs = (vcoreid << 3) | 0x07;
64 for (int i = 0; i < nr_loops; i++) {
65 __procdata.ldt[vcoreid] = tmp;
67 asm volatile("movl %0,%%gs" : : "r" (gs) : "memory");
73 /* Internal test infrastructure */
75 void loop_overhead(unsigned long nr_loops)
77 for (int i = 0; i < nr_loops; i++) {
82 /* Runs func(loops) and returns the usec elapsed */
83 #define __test_time_us(func, loops) \
85 struct timeval start_tv = {0}; \
86 struct timeval end_tv = {0}; \
87 if (gettimeofday(&start_tv, 0)) \
88 perror("Start time error..."); \
90 if (gettimeofday(&end_tv, 0)) \
91 perror("End time error..."); \
92 ((end_tv.tv_sec - start_tv.tv_sec) * 1000000 + \
93 (end_tv.tv_usec - start_tv.tv_usec)); \
96 /* Runs func(loops) and returns the nsec elapsed */
97 #define __test_time_ns(func, loops) \
99 (__test_time_us((func), (loops)) * 1000); \
102 /* Runs func(loops), subtracts the loop overhead, and prints the result */
103 #define test_time_us(func, loops) \
105 unsigned long long usec_diff; \
106 usec_diff = __test_time_us((func), (loops)) - nsec_per_loop * loops / 1000;\
107 printf("\"%s\" total: %lluus, per iteration: %lluus\n", #func, usec_diff, \
108 usec_diff / (loops)); \
111 /* Runs func(loops), subtracts the loop overhead, and prints the result */
112 #define test_time_ns(func, loops) \
114 unsigned long long nsec_diff; \
115 nsec_diff = __test_time_ns((func), (loops)) - nsec_per_loop * (loops); \
116 printf("\"%s\" total: %lluns, per iteration: %lluns\n", #func, nsec_diff, \
117 nsec_diff / (loops)); \
120 static void microb_test(void)
122 unsigned long long nsec_per_loop;
123 printf("We are %sin MCP mode, running on vcore %d, pcore %d\n",
124 (in_multi_mode() ? "" : "not "), vcore_id(),
126 /* We divide the overhead by loops, and later we multiply again, which drops
127 * off some accuracy at the expense of usability (can do different
128 * iterations for different tests without worrying about recomputing the
130 nsec_per_loop = __test_time_ns(loop_overhead, 100000) / 100000;
131 printd("Loop overhead per loop: %lluns\n", nsec_per_loop);
133 /* Add your tests here. Func name, number of loops */
134 test_time_ns(set_tlsdesc_test , 100000);
137 void *worker_thread(void* arg)
143 int main(int argc, char** argv)
148 printf("Spawning worker thread, etc...\n");
149 pthread_create(&child, NULL, &worker_thread, NULL);
150 pthread_join(child, &child_ret);