File: core-sync.c

package info (click to toggle)
stress-ng 0.19.02-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 17,068 kB
  • sloc: ansic: 192,232; makefile: 818; sh: 808; cpp: 254
file content (187 lines) | stat: -rw-r--r-- 4,741 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2025      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-sync.h"

/*
 *  stress_sync_start_timeout()
 *	set the timeout for SIGALRM for a stressor
 */
static void stress_sync_start_timeout(void)
{
	if (g_opt_timeout)
		(void)alarm((unsigned int)g_opt_timeout);
}

/*
 *  stress_sync_s_pids_mmap()
 *	mmap an array of stress_pids_t of num elements; these need
 *	to map as shared so stressor and parent can load/store the
 *	state setting.
 */
stress_pid_t *stress_sync_s_pids_mmap(const size_t num)
{
	stress_pid_t *s_pids;
	const size_t size = num * sizeof(stress_pid_t);

	s_pids = (stress_pid_t *)mmap(NULL, size, PROT_READ | PROT_WRITE,
			MAP_ANONYMOUS | MAP_SHARED, -1, 0);
	if (s_pids != MAP_FAILED)
		stress_set_vma_anon_name(s_pids, size, "s_pids");
	return s_pids;
}

/*
 *  stress_sync_s_pids_munmap()
 *	unmap num sized stress_pids_t array
 */
int stress_sync_s_pids_munmap(stress_pid_t *s_pids, const size_t num)
{
	return munmap((void *)s_pids, num * sizeof(stress_pid_t));
}

/*
 *  stress_sync_start_init()
 *	initialize the stress_pid_t state
 */
void stress_sync_start_init(stress_pid_t *s_pid)
{
	s_pid->pid = -1;
	stress_sync_state_store(s_pid, STRESS_SYNC_START_FLAG_STARTED);
}

/*
 *  stress_sync_start_wait_s_pid()
 *	put process into a stop (waiting) state, will be
 *	woken up by a parent call to stress_sync_start_cont_s_pid()
 */
void stress_sync_start_wait_s_pid(stress_pid_t *s_pid)
{
	pid_t pid;

	if (!(g_opt_flags & OPT_FLAGS_SYNC_START))
		return;

	pid = s_pid->oomable_child ? s_pid->oomable_child : s_pid->pid;
	if (pid <= 1)
		return;

	stress_sync_state_store(s_pid, STRESS_SYNC_START_FLAG_WAITING);
	if (kill(pid, SIGSTOP) < 0) {
		pr_inf("cannot stop stressor on for --sync-start, errno=%d (%s)",
			errno, strerror(errno));
	}
	stress_sync_state_store(s_pid, STRESS_SYNC_START_FLAG_RUNNING);
	stress_sync_start_timeout();
}

/*
 *  stress_sync_start_wait()
 *	put stressor into a stop (waiting) state, will be
 *	woken up by a parent call to stress_sync_start_cont_s_pid()
 */
void stress_sync_start_wait(stress_args_t *args)
{
	pid_t pid;
	stress_pid_t *s_pid;

	if (!(g_opt_flags & OPT_FLAGS_SYNC_START))
		return;

	s_pid = &args->stats->s_pid;
	pid = s_pid->oomable_child ? s_pid->oomable_child : s_pid->pid;
	if (pid <= 1)
		return;

	stress_sync_state_store(s_pid, STRESS_SYNC_START_FLAG_WAITING);
	if (kill(pid, SIGSTOP) < 0) {
		pr_inf("%s: cannot stop stressor on for --sync-start, errno=%d (%s)",
			args->name, errno, strerror(errno));
	}
	stress_sync_state_store(s_pid, STRESS_SYNC_START_FLAG_RUNNING);
	stress_sync_start_timeout();
}

/*
 *  stress_sync_start_cont_s_pid()
 *	wake up (continue) a stopped process
 */
void stress_sync_start_cont_s_pid(stress_pid_t *s_pid)
{
	pid_t pid;

	if (!(g_opt_flags & OPT_FLAGS_SYNC_START))
		return;

	pid = s_pid->oomable_child ? s_pid->oomable_child : s_pid->pid;
	if (pid <= 1)
		return;

	(void)kill(pid, SIGCONT);
}

void stress_sync_start_cont_list(stress_pid_t *s_pids_head)
{
	int unready, n_pids;

	if (!(g_opt_flags & OPT_FLAGS_SYNC_START))
		return;

	do {
		stress_pid_t *s_pid;

		unready = 0;
		n_pids = 0;
		for (s_pid = s_pids_head; s_pid; s_pid = s_pid->next) {
			uint8_t state;

			n_pids++;
			stress_sync_state_load(s_pid, &state);
			if (state == STRESS_SYNC_START_FLAG_FINISHED)
				continue;
			if (state != STRESS_SYNC_START_FLAG_WAITING)
				unready++;
		}
		if (!unready)
			break;
		(void)shim_usleep(10000);
		unready = 0;
	} while (stress_continue_flag());

	if (!unready) {
		do {
			stress_pid_t *s_pid;
			int running = 0, finished = 0;
			uint8_t state;

			for (s_pid = s_pids_head; s_pid; s_pid = s_pid->next) {
				stress_sync_start_cont_s_pid(s_pid);
				stress_sync_state_load(s_pid, &state);
				if (state == STRESS_SYNC_START_FLAG_FINISHED)
					finished++;
				if (state == STRESS_SYNC_START_FLAG_RUNNING)
					running++;
			}
			if ((running + finished) == n_pids)
				break;
			(void)shim_usleep(10000);
		} while (stress_continue_flag());
	}
}