forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-prefetch.c
283 lines (246 loc) · 7.74 KB
/
stress-prefetch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (C) 2016-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "core-cache.h"
#include "core-put.h"
#define MIN_PREFETCH_L3_SIZE (4 * KB)
#define MAX_PREFETCH_L3_SIZE (MAX_MEM_LIMIT)
#define DEFAULT_PREFETCH_L3_SIZE (4 * MB)
#define STRESS_PREFETCH_OFFSETS (128)
#define STRESS_CACHE_LINE_SIZE (64)
static const stress_help_t help[] = {
{ NULL, "prefetch N" , "start N workers exercising memory prefetching " },
{ NULL, "prefetch-ops N", "stop after N bogo prefetching operations" },
{ NULL, "prefetch-l3-size N", "specify the L3 cache size of the CPU" },
{ NULL, NULL, NULL }
};
typedef struct {
size_t offset;
uint64_t count;
double duration;
double bytes;
double rate;
} stress_prefetch_info_t;
static int stress_set_prefetch_L3_size(const char *opt)
{
uint64_t stream_L3_size;
size_t sz;
stream_L3_size = stress_get_uint64_byte(opt);
stress_check_range_bytes("stream-L3-size", stream_L3_size,
MIN_PREFETCH_L3_SIZE, MAX_PREFETCH_L3_SIZE);
sz = (size_t)stream_L3_size;
return stress_set_setting("stream-L3-size", TYPE_ID_SIZE_T, &sz);
}
static inline uint64_t get_prefetch_L3_size(const stress_args_t *args)
{
uint64_t cache_size = DEFAULT_PREFETCH_L3_SIZE;
#if defined(__linux__)
stress_cpus_t *cpu_caches;
stress_cpu_cache_t *cache = NULL;
uint16_t max_cache_level;
cpu_caches = stress_get_all_cpu_cache_details();
if (!cpu_caches) {
if (!args->instance)
pr_inf("%s: using built-in defaults as unable to "
"determine cache details\n", args->name);
return cache_size;
}
max_cache_level = stress_get_max_cache_level(cpu_caches);
if ((max_cache_level > 0) && (max_cache_level < 3) && (!args->instance))
pr_inf("%s: no L3 cache, using L%" PRIu16 " size instead\n",
args->name, max_cache_level);
cache = stress_get_cpu_cache(cpu_caches, max_cache_level);
if (!cache) {
if (!args->instance)
pr_inf("%s: using built-in defaults as no suitable "
"cache found\n", args->name);
stress_free_cpu_caches(cpu_caches);
return cache_size;
}
if (!cache->size) {
if (!args->instance)
pr_inf("%s: using built-in defaults as unable to "
"determine cache size\n", args->name);
stress_free_cpu_caches(cpu_caches);
return cache_size;
}
cache_size = cache->size;
stress_free_cpu_caches(cpu_caches);
#else
if (!args->instance)
pr_inf("%s: using built-in defaults as unable to "
"determine cache details\n", args->name);
#endif
return cache_size;
}
static inline void OPTIMIZE3 stress_prefetch_benchmark(
stress_prefetch_info_t *prefetch_info,
const size_t i,
uint64_t *RESTRICT l3_data,
uint64_t *RESTRICT l3_data_end,
uint64_t *total_count)
{
double t1, t2, t3, t4;
const size_t l3_data_size = (uintptr_t)l3_data_end - (uintptr_t)l3_data;
volatile uint64_t *ptr;
uint64_t *pre_ptr;
shim_cacheflush((char *)l3_data, (int)l3_data_size, SHIM_DCACHE);
#if defined(HAVE_BUILTIN___CLEAR_CACHE)
__builtin___clear_cache((void *)l3_data, (void *)l3_data_end);
#endif
/* Benchmark loop */
ptr = l3_data;
pre_ptr = l3_data + prefetch_info[i].offset;
t1 = stress_time_now();
while (ptr < l3_data_end) {
ptr += 8;
pre_ptr += 8;
shim_mb();
}
t2 = stress_time_now();
stress_void_ptr_put((volatile void *)ptr);
stress_void_ptr_put((volatile void *)pre_ptr);
/* Benchmark reads */
if (prefetch_info[i].offset == 0) {
/* Benchmark no prefetch */
t3 = stress_time_now();
ptr = l3_data;
pre_ptr = l3_data + prefetch_info[i].offset;
while (ptr < l3_data_end) {
(void)(*(ptr + 0));
(void)(*(ptr + 1));
(void)(*(ptr + 2));
(void)(*(ptr + 3));
(void)(*(ptr + 4));
(void)(*(ptr + 5));
(void)(*(ptr + 6));
(void)(*(ptr + 7));
ptr += 8;
pre_ptr += 8;
shim_mb();
}
stress_void_ptr_put(pre_ptr);
} else {
/* Benchmark prefetch */
t3 = stress_time_now();
ptr = l3_data;
pre_ptr = l3_data + prefetch_info[i].offset;
while (ptr < l3_data_end) {
(void)(*(ptr + 0));
(void)(*(ptr + 1));
(void)(*(ptr + 2));
(void)(*(ptr + 3));
(void)(*(ptr + 4));
(void)(*(ptr + 5));
(void)(*(ptr + 6));
(void)(*(ptr + 7));
shim_builtin_prefetch(pre_ptr, 0, 3);
ptr += 8;
pre_ptr += 8;
shim_mb();
}
}
t4 = stress_time_now();
/* Update stats */
prefetch_info[i].bytes += (double)l3_data_size;
prefetch_info[i].duration += (t4 - t3) - (t2 - t1);
prefetch_info[i].count++;
(*total_count)++;
}
/*
* stress_prefetch()
* stress cache/memory/CPU with stream stressors
*/
static int stress_prefetch(const stress_args_t *args)
{
uint64_t *l3_data, *l3_data_end, total_count = 0;
size_t l3_data_size = 0, l3_data_mmap_size;
stress_prefetch_info_t prefetch_info[STRESS_PREFETCH_OFFSETS];
size_t i, best;
double best_rate, ns;
(void)stress_get_setting("stream-L3-size", &l3_data_size);
if (l3_data_size == 0)
l3_data_size = get_prefetch_L3_size(args);
l3_data_mmap_size = l3_data_size + (STRESS_PREFETCH_OFFSETS * STRESS_CACHE_LINE_SIZE);
l3_data = (uint64_t *)mmap(NULL, l3_data_mmap_size,
PROT_READ | PROT_WRITE,
#if defined(MAP_POPULATE)
MAP_POPULATE |
#endif
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (l3_data == MAP_FAILED) {
pr_err("%s: cannot allocate %zu bytes\n",
args->name, l3_data_mmap_size);
return EXIT_NO_RESOURCE;
}
l3_data_end = (uint64_t *)((uintptr_t)l3_data + l3_data_size);
(void)memset(l3_data, 0xa5, l3_data_mmap_size);
for (i = 0; i < SIZEOF_ARRAY(prefetch_info); i++) {
prefetch_info[i].offset = i * STRESS_CACHE_LINE_SIZE;
prefetch_info[i].count = 0;
prefetch_info[i].duration = 0.0;
prefetch_info[i].bytes = 0.0;
prefetch_info[i].rate = 0.0;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
for (i = 0; i < SIZEOF_ARRAY(prefetch_info); i++) {
stress_prefetch_benchmark(prefetch_info, i,
l3_data, l3_data_end, &total_count);
}
inc_counter(args);
} while (keep_stressing(args));
best = 0;
best_rate = 0.0;
for (i = 0; i < SIZEOF_ARRAY(prefetch_info); i++) {
if (prefetch_info[i].duration > 0.0)
prefetch_info[i].rate = prefetch_info[i].bytes / prefetch_info[i].duration;
else
prefetch_info[i].rate = 0.0;
if (prefetch_info[i].rate > best_rate) {
best_rate = prefetch_info[i].rate;
best = i;
}
}
pr_inf("%s: using a %zd KB L3 cache, %" PRIu64 " benchmark rounds\n",
args->name, l3_data_size >> 10, total_count);
pr_inf("%s: non-prefetch read rate @ %.2f GB/s\n",
args->name, prefetch_info[0].rate / (double)GB);
if (best_rate > 0.0)
ns = 1000000000.0 * (double)prefetch_info[best].offset / best_rate;
else
ns = 0.0;
pr_inf("%s: best prefetch read rate @ %.2f GB/s at offset %zd (~%.2f nanoseconds)\n",
args->name, best_rate / (double)GB,
prefetch_info[best].offset, ns);
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
(void)munmap((void *)l3_data, l3_data_mmap_size);
return EXIT_SUCCESS;
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_prefetch_l3_size, stress_set_prefetch_L3_size },
{ 0, NULL }
};
stressor_info_t stress_prefetch_info = {
.stressor = stress_prefetch,
.class = CLASS_CPU | CLASS_CPU_CACHE | CLASS_MEMORY,
.opt_set_funcs = opt_set_funcs,
.help = help
};