2 * Copyright (c) 2014 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/cpuset.h>
30 #include <machine/atomic.h>
31 #include <machine/cpu.h>
32 #include <machine/cpufunc.h>
42 #define barrier() __asm __volatile("" ::: "memory")
46 static volatile int gate;
47 static volatile uint64_t thread_tsc;
49 /* Bind the current thread to the specified CPU. */
57 if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(set),
59 err(1, "cpuset_setaffinity(%d)", cpu);
63 thread_main(void *arg)
69 for (i = 0; i < TESTS; i++) {
75 __asm __volatile("lfence");
87 main(int ac __unused, char **av __unused)
90 int64_t **skew, *aveskew, *minskew, *maxskew;
96 int error, i, j, ncpu;
99 * Find all the CPUs this program is eligible to run on and use
100 * this as our global set. This means you can use cpuset to
101 * restrict this program to only run on a subset of CPUs.
103 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
104 sizeof(all_cpus), &all_cpus) < 0)
105 err(1, "cpuset_getaffinity");
106 for (ncpu = 0, i = 0; i < CPU_SETSIZE; i++) {
107 if (CPU_ISSET(i, &all_cpus))
111 errx(1, "Only one available CPU");
112 cpus = calloc(ncpu, sizeof(*cpus));
113 skew = calloc(ncpu, sizeof(*skew));
114 for (i = 0; i < ncpu; i++)
115 skew[i] = calloc(TESTS, sizeof(*skew[i]));
116 for (i = 0, j = 0; i < CPU_SETSIZE; i++)
117 if (CPU_ISSET(i, &all_cpus)) {
124 * We bind this thread to the first CPU and then bind all the
125 * other threads to other CPUs in turn saving TESTS counts of
129 for (i = 1; i < ncpu; i++) {
130 error = pthread_create(&child, NULL, thread_main,
131 (void *)(intptr_t)cpus[i]);
133 errc(1, error, "pthread_create");
135 for (j = 0; j < TESTS; j++) {
148 skew[i][j] = thread_tsc - tsc;
151 error = pthread_join(child, NULL);
153 errc(1, error, "pthread_join");
157 * Compute average skew for each CPU and output a summary of
160 aveskew = calloc(ncpu, sizeof(*aveskew));
161 minskew = calloc(ncpu, sizeof(*minskew));
162 maxskew = calloc(ncpu, sizeof(*maxskew));
163 stddev = calloc(ncpu, sizeof(*stddev));
165 for (i = 1; i < ncpu; i++) {
167 minskew[i] = maxskew[i] = skew[i][0];
168 for (j = 0; j < TESTS; j++) {
169 aveskew[i] += skew[i][j];
170 if (skew[i][j] < minskew[i])
171 minskew[i] = skew[i][j];
172 if (skew[i][j] > maxskew[i])
173 maxskew[i] = skew[i][j];
174 sumsq += (skew[i][j] * skew[i][j]);
178 sumsq -= aveskew[i] * aveskew[i];
179 stddev[i] = sqrt(sumsq);
182 printf("CPU | TSC skew (min/avg/max/stddev)\n");
183 printf("----+------------------------------\n");
184 for (i = 0; i < ncpu; i++)
185 printf("%3d | %5jd %5jd %5jd %6.3f\n", cpus[i],
186 (intmax_t)minskew[i], (intmax_t)aveskew[i],
187 (intmax_t)maxskew[i], stddev[i]);