|
4 | 4 | */
|
5 | 5 | #define _GNU_SOURCE
|
6 | 6 | #include <assert.h>
|
| 7 | +#include <err.h> |
7 | 8 | #include <limits.h>
|
| 9 | +#include <sched.h> |
8 | 10 | #include <stdbool.h>
|
9 | 11 | #include <stddef.h>
|
10 | 12 | #include <stdio.h>
|
@@ -76,8 +78,12 @@ unsigned long long calibrate(void)
|
76 | 78 |
|
77 | 79 | bool approx(int i_one, int i_two)
|
78 | 80 | {
|
79 |
| - double one = i_one, one_bump = one * 0.01; |
80 |
| - double two = i_two, two_bump = two * 0.01; |
| 81 | + /* |
| 82 | + * This continues to be a noisy test. Instead of a 1% comparison |
| 83 | + * go with 10%. |
| 84 | + */ |
| 85 | + double one = i_one, one_bump = one * 0.1; |
| 86 | + double two = i_two, two_bump = two * 0.1; |
81 | 87 |
|
82 | 88 | one_bump = one + MAX(one_bump, 2.0);
|
83 | 89 | two_bump = two + MAX(two_bump, 2.0);
|
@@ -119,6 +125,32 @@ long compare(const char *name_one, const char *name_eval, const char *name_two,
|
119 | 125 | return good ? 0 : 1;
|
120 | 126 | }
|
121 | 127 |
|
| 128 | +/* Pin to a single CPU so the benchmark won't bounce around the system. */ |
| 129 | +void affinity(void) |
| 130 | +{ |
| 131 | + long cpu; |
| 132 | + ulong ncores = sysconf(_SC_NPROCESSORS_CONF); |
| 133 | + cpu_set_t *setp = CPU_ALLOC(ncores); |
| 134 | + ulong setsz = CPU_ALLOC_SIZE(ncores); |
| 135 | + |
| 136 | + /* |
| 137 | + * Totally unscientific way to avoid CPUs that might be busier: |
| 138 | + * choose the highest CPU instead of the lowest. |
| 139 | + */ |
| 140 | + for (cpu = ncores - 1; cpu >= 0; cpu--) { |
| 141 | + CPU_ZERO_S(setsz, setp); |
| 142 | + CPU_SET_S(cpu, setsz, setp); |
| 143 | + if (sched_setaffinity(getpid(), setsz, setp) == -1) |
| 144 | + continue; |
| 145 | + printf("Pinned to CPU %lu of %lu\n", cpu + 1, ncores); |
| 146 | + goto out; |
| 147 | + } |
| 148 | + fprintf(stderr, "Could not set CPU affinity -- calibration may not work well"); |
| 149 | + |
| 150 | +out: |
| 151 | + CPU_FREE(setp); |
| 152 | +} |
| 153 | + |
122 | 154 | int main(int argc, char *argv[])
|
123 | 155 | {
|
124 | 156 | struct sock_filter bitmap_filter[] = {
|
@@ -153,6 +185,8 @@ int main(int argc, char *argv[])
|
153 | 185 | system("grep -H . /proc/sys/net/core/bpf_jit_enable");
|
154 | 186 | system("grep -H . /proc/sys/net/core/bpf_jit_harden");
|
155 | 187 |
|
| 188 | + affinity(); |
| 189 | + |
156 | 190 | if (argc > 1)
|
157 | 191 | samples = strtoull(argv[1], NULL, 0);
|
158 | 192 | else
|
|
0 commit comments