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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/cpuset.h>
34 #include <machine/atomic.h>
35 #include <machine/cpu.h>
36 #include <machine/cpufunc.h>
46 #define barrier() __asm __volatile("" ::: "memory")
50 static volatile int gate;
51 static volatile uint64_t thread_tsc;
53 /* Bind the current thread to the specified CPU. */
61 if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(set),
63 err(1, "cpuset_setaffinity(%d)", cpu);
67 thread_main(void *arg)
73 for (i = 0; i < TESTS; i++) {
79 __asm __volatile("lfence");
91 main(int ac __unused, char **av __unused)
94 int64_t **skew, *aveskew, *minskew, *maxskew;
100 int error, i, j, ncpu;
103 * Find all the CPUs this program is eligible to run on and use
104 * this as our global set. This means you can use cpuset to
105 * restrict this program to only run on a subset of CPUs.
107 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
108 sizeof(all_cpus), &all_cpus) < 0)
109 err(1, "cpuset_getaffinity");
110 for (ncpu = 0, i = 0; i < CPU_SETSIZE; i++) {
111 if (CPU_ISSET(i, &all_cpus))
115 errx(1, "Only one available CPU");
116 cpus = calloc(ncpu, sizeof(*cpus));
117 skew = calloc(ncpu, sizeof(*skew));
118 for (i = 0; i < ncpu; i++)
119 skew[i] = calloc(TESTS, sizeof(*skew[i]));
120 for (i = 0, j = 0; i < CPU_SETSIZE; i++)
121 if (CPU_ISSET(i, &all_cpus)) {
128 * We bind this thread to the first CPU and then bind all the
129 * other threads to other CPUs in turn saving TESTS counts of
133 for (i = 1; i < ncpu; i++) {
134 error = pthread_create(&child, NULL, thread_main,
135 (void *)(intptr_t)cpus[i]);
137 errc(1, error, "pthread_create");
139 for (j = 0; j < TESTS; j++) {
152 skew[i][j] = thread_tsc - tsc;
155 error = pthread_join(child, NULL);
157 errc(1, error, "pthread_join");
161 * Compute average skew for each CPU and output a summary of
164 aveskew = calloc(ncpu, sizeof(*aveskew));
165 minskew = calloc(ncpu, sizeof(*minskew));
166 maxskew = calloc(ncpu, sizeof(*maxskew));
167 stddev = calloc(ncpu, sizeof(*stddev));
169 for (i = 1; i < ncpu; i++) {
171 minskew[i] = maxskew[i] = skew[i][0];
172 for (j = 0; j < TESTS; j++) {
173 aveskew[i] += skew[i][j];
174 if (skew[i][j] < minskew[i])
175 minskew[i] = skew[i][j];
176 if (skew[i][j] > maxskew[i])
177 maxskew[i] = skew[i][j];
178 sumsq += (skew[i][j] * skew[i][j]);
182 sumsq -= aveskew[i] * aveskew[i];
183 stddev[i] = sqrt(sumsq);
186 printf("CPU | TSC skew (min/avg/max/stddev)\n");
187 printf("----+------------------------------\n");
188 for (i = 0; i < ncpu; i++)
189 printf("%3d | %5jd %5jd %5jd %6.3f\n", cpus[i],
190 (intmax_t)minskew[i], (intmax_t)aveskew[i],
191 (intmax_t)maxskew[i], stddev[i]);