forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-context.c
174 lines (148 loc) · 4.99 KB
/
stress-context.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
/*
* Copyright (C) 2013-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"
#if defined(HAVE_UCONTEXT_H)
#include <ucontext.h>
#endif
static stress_help_t help[] = {
{ NULL, "context N", "start N workers exercising user context" },
{ NULL, "context-ops N", "stop context workers after N bogo operations" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_SWAPCONTEXT) && \
defined(HAVE_UCONTEXT_H)
#define CONTEXT_STACK_SIZE (16384)
typedef struct {
uint32_t check0; /* memory clobbering check canary */
ucontext_t uctx; /* swapcontext context */
uint32_t check1; /* memory clobbering check canary */
} chk_ucontext_t;
typedef struct {
uint32_t check0; /* copy of original check1 canary */
uint32_t check1; /* copy of original check1 canary */
} chk_canary_t;
typedef struct {
chk_ucontext_t cu; /* check ucontext */
uint8_t stack[CONTEXT_STACK_SIZE + STACK_ALIGNMENT]; /* stack */
chk_canary_t canary; /* copy of canary */
} context_info_t;
static context_info_t context[3];
static ucontext_t uctx_main;
static uint64_t context_counter;
static uint64_t stress_max_ops;
static void thread1(void)
{
do {
context_counter++;
(void)swapcontext(&context[0].cu.uctx, &context[1].cu.uctx);
} while (keep_stressing_flag() && (!stress_max_ops || (context_counter < stress_max_ops)));
(void)swapcontext(&context[0].cu.uctx, &uctx_main);
}
static void thread2(void)
{
do {
context_counter++;
(void)swapcontext(&context[1].cu.uctx, &context[2].cu.uctx);
} while (keep_stressing_flag() && (!stress_max_ops || (context_counter < stress_max_ops)));
(void)swapcontext(&context[1].cu.uctx, &uctx_main);
}
static void thread3(void)
{
do {
context_counter++;
(void)swapcontext(&context[2].cu.uctx, &context[0].cu.uctx);
} while (keep_stressing_flag() && (!stress_max_ops || (context_counter < stress_max_ops)));
(void)swapcontext(&context[2].cu.uctx, &uctx_main);
}
static int stress_context_init(
const stress_args_t *args,
void (*func)(void),
ucontext_t *uctx_link,
context_info_t *context_info)
{
(void)memset(context_info, 0, sizeof(*context_info));
if (getcontext(&context_info->cu.uctx) < 0) {
pr_fail("%s: getcontext failed: %d (%s)\n",
args->name, errno, strerror(errno));
return -1;
}
context_info->canary.check0 = stress_mwc32();
context_info->canary.check1 = stress_mwc32();
context_info->cu.check0 = context_info->canary.check0;
context_info->cu.check1 = context_info->canary.check1;
context_info->cu.uctx.uc_stack.ss_sp =
(void *)stress_align_address(context_info->stack, STACK_ALIGNMENT);
context_info->cu.uctx.uc_stack.ss_size = CONTEXT_STACK_SIZE;
context_info->cu.uctx.uc_link = uctx_link;
makecontext(&context_info->cu.uctx, func, 0);
return 0;
}
/*
* stress_context()
* stress that exercises CPU context save/restore
*/
static int stress_context(const stress_args_t *args)
{
size_t i;
(void)memset(&uctx_main, 0, sizeof(uctx_main));
(void)memset(context, 0, sizeof(context));
context_counter = 0;
stress_max_ops = args->max_ops * 1000;
/* Create 3 micro threads */
if (stress_context_init(args, thread1, &uctx_main, &context[0]) < 0)
return EXIT_FAILURE;
if (stress_context_init(args, thread2, &uctx_main, &context[1]) < 0)
return EXIT_FAILURE;
if (stress_context_init(args, thread3, &uctx_main, &context[2]) < 0)
return EXIT_FAILURE;
/* And start.. */
if (swapcontext(&uctx_main, &context[0].cu.uctx) < 0) {
pr_fail("%s: swapcontext failed: %d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
set_counter(args, context_counter / 1000);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
for (i = 0; i < SIZEOF_ARRAY(context); i++) {
if (context[i].canary.check0 != context[i].cu.check0) {
pr_fail("%s: swapcontext clobbered data before context region\n",
args->name);
}
if (context[i].canary.check1 != context[i].cu.check1) {
pr_fail("%s: swapcontext clobbered data after context region\n",
args->name);
}
}
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return EXIT_SUCCESS;
}
stressor_info_t stress_context_info = {
.stressor = stress_context,
.class = CLASS_MEMORY | CLASS_CPU,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_context_info = {
.stressor = stress_not_implemented,
.class = CLASS_MEMORY | CLASS_CPU,
.help = help
};
#endif