forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-icache.c
211 lines (200 loc) · 6.18 KB
/
stress-icache.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
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* 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.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if (defined(STRESS_X86) || defined(STRESS_ARM) || \
defined(STRESS_S390) || defined(STRESS_PPC64)) && \
defined(__GNUC__) && NEED_GNUC(4,6,0)
#define SIZE_1K (1024)
#define SIZE_4K (4 * SIZE_1K)
#define SIZE_16K (16 * SIZE_1K)
#define SIZE_64K (64 * SIZE_1K)
#if defined(__GNUC__) && NEED_GNUC(4,6,0)
#define SECTION(s) __attribute__((__section__(# s)))
#define ALIGNED(a) __attribute__((aligned(a)))
#endif
/*
* STRESS_ICACHE_FUNC()
* generates a simple function that is page aligned in its own
* section so we can change the code mapping and make it
* modifyable to force I-cache refreshes by modifying the code
*/
#define STRESS_ICACHE_FUNC(func_name, page_sz) \
static void SECTION(stress_icache_callee) ALIGNED(page_sz) \
func_name(void) \
{ \
return; \
} \
/*
* STRESS_ICACHE()
* macro to generate functions that stress instruction cache
* load misses
*
* I-cache load misses can be observed using:
* perf stat -e L1-icache-load-misses stress-ng --icache 0 -t 1
*/
#define STRESS_ICACHE(func_name, page_sz, icache_func) \
static int SECTION(stress_icache_caller) ALIGNED(page_sz) \
func_name(const args_t *args) \
{ \
uint8_t *addr = (uint8_t *)icache_func; \
const size_t ps = args->page_size; \
void *page_addr = (void *)((uintptr_t)addr & ~(ps - 1)); \
\
if (icache_madvise(args, addr, page_sz) < 0) \
return EXIT_NO_RESOURCE; \
\
do { \
register uint8_t val; \
register int i = 1024; \
\
while (--i) { \
volatile uint8_t *vaddr = \
(volatile uint8_t *)addr; \
/* \
* Change protection to make page modifyable. \
* It may be that some architectures don't \
* allow this, so don't bail out on an \
* EXIT_FAILURE; this is a not necessarily a \
* fault in the the stressor, just an arch \
* resource protection issue. \
*/ \
if (mprotect((void *)page_addr, page_sz, \
PROT_READ | PROT_WRITE) < 0) { \
pr_inf("%s: PROT_WRITE mprotect failed "\
"on text page %p: errno=%d " \
"(%s)\n", args->name, vaddr, \
errno, strerror(errno)); \
return EXIT_NO_RESOURCE; \
} \
/* \
* Modifying executable code on x86 will \
* call a I-cache reload when we execute \
* the modfied ops. \
*/ \
val = *vaddr; \
*vaddr ^= ~0; \
/* \
* ARM CPUs need us to clear the I$ between \
* each modification of the object code. \
* \
* We may need to do the same for other CPUs \
* as the default code assumes smart x86 style \
* I$ behaviour. \
*/ \
shim_clear_cache((char *)addr, (char *)addr + 64);\
*vaddr = val; \
shim_clear_cache((char *)addr, (char *)addr + 64);\
/* \
* Set back to a text segment READ/EXEC page \
* attributes, this really should not fail. \
*/ \
if (mprotect((void *)page_addr, page_sz, \
PROT_READ | PROT_EXEC) < 0) { \
pr_err("%s: mprotect failed: errno=%d " \
"(%s)\n", args->name, errno, \
strerror(errno)); \
return EXIT_FAILURE; \
} \
icache_func(); \
} \
inc_counter(args); \
} while (keep_stressing()); \
\
return EXIT_SUCCESS; \
}
static inline int icache_madvise(const args_t *args, void *addr, size_t size)
{
#if defined(MADV_NOHUGEPAGE)
if (shim_madvise((void *)addr, size, MADV_NOHUGEPAGE) < 0) {
/*
* We may get EINVAL on kernels that don't support this
* so don't treat that as non-fatal as this is just advistory
*/
if (errno != EINVAL) {
pr_inf("%s: madvise MADV_NOHUGEPAGE failed on text "
"page %p: errno=%d (%s)\n",
args->name, addr, errno, strerror(errno));
return -1;
}
}
#else
(void)args;
(void)addr;
(void)size;
#endif
return 0;
}
#if defined(HAVE_ALIGNED_64K)
STRESS_ICACHE_FUNC(stress_icache_func_64K, SIZE_64K)
#endif
STRESS_ICACHE_FUNC(stress_icache_func_16K, SIZE_16K)
STRESS_ICACHE_FUNC(stress_icache_func_4K, SIZE_4K)
#if defined(HAVE_ALIGNED_64K)
STRESS_ICACHE(stress_icache_64K, SIZE_64K, stress_icache_func_64K)
#endif
STRESS_ICACHE(stress_icache_16K, SIZE_16K, stress_icache_func_16K)
STRESS_ICACHE(stress_icache_4K, SIZE_4K, stress_icache_func_4K)
/*
* stress_icache()
* entry point for stress instruction cache load misses
*
* I-cache load misses can be observed using:
* perf stat -e L1-icache-load-misses stress-ng --icache 0 -t 1
*/
int stress_icache(const args_t *args)
{
int ret;
switch (args->page_size) {
case SIZE_4K:
ret = stress_icache_4K(args);
break;
case SIZE_16K:
ret = stress_icache_16K(args);
break;
#if defined(HAVE_ALIGNED_64K)
case SIZE_64K:
ret = stress_icache_64K(args);
break;
#endif
default:
#if defined(HAVE_ALIGNED_64K)
pr_inf("%s: page size %zu is not %u or %u or %u, cannot test\n",
args->name, args->page_size,
SIZE_4K, SIZE_16K, SIZE_64K);
#else
pr_inf("%s: page size %zu is not %u or %u, cannot test\n",
args->name, args->page_size,
SIZE_4K, SIZE_16K);
#endif
ret = EXIT_NO_RESOURCE;
}
return ret;
}
#else
int stress_icache(const args_t *args)
{
return stress_not_implemented(args);
}
#endif