File: sched.c

package info (click to toggle)
stress-ng 0.07.16-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,240 kB
  • ctags: 3,428
  • sloc: ansic: 36,083; makefile: 551; sh: 129
file content (193 lines) | stat: -rw-r--r-- 4,663 bytes parent folder | download
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
/*
 * 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 <colin.king@canonical.com> and attempts to be
 * backwardly compatible with the stress tool by Amos Waterland
 * <apw@rossby.metr.ou.edu> but has more stress tests and more
 * functionality.
 *
 */
#include "stress-ng.h"

#if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
    !defined(__OpenBSD__) && !defined(__minix__)
/*
 *  get_sched_name()
 *	convert sched class to human readable string
 */
const char *get_sched_name(const int sched)
{
	switch (sched) {
#if defined(SCHED_IDLE)
	case SCHED_IDLE:
		return "idle";
#endif
#if defined(SCHED_FIFO)
	case SCHED_FIFO:
		return "fifo";
#endif
#if defined(SCHED_RR)
	case SCHED_RR:
		return "rr";
#endif
#if defined(SCHED_OTHER)
	case SCHED_OTHER:
		return "other";
#endif
#if defined(SCHED_BATCH)
	case SCHED_BATCH:
		return "batch";
#endif
#if defined(SCHED_DEADLINE)
	case SCHED_DEADLINE:
		return "deadline";
#endif
	default:
		return "unknown";
	}
}
#endif

#if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
     !defined(__OpenBSD__) && !defined(__minix__)
/*
 *  set_sched()
 * 	are sched settings valid, if so, set them
 */
void set_sched(const int sched, const int32_t sched_priority)
{
#if defined(SCHED_FIFO) || defined(SCHED_RR)
	int min, max;
#endif
	int rc;
	struct sched_param param;
	const char *name = get_sched_name(sched);

	memset(&param, 0, sizeof(param));

	switch (sched) {
	case UNDEFINED:	/* No preference, don't set */
		return;
#if defined(SCHED_FIFO) || defined(SCHED_RR)
#if defined(SCHED_FIFO)
	case SCHED_FIFO:
#endif
#if defined(SCHED_RR)
	case SCHED_RR:
#endif
		min = sched_get_priority_min(sched);
		max = sched_get_priority_max(sched);

		param.sched_priority = sched_priority;

		if (param.sched_priority == UNDEFINED) {
			if (opt_flags & OPT_FLAGS_AGGRESSIVE)
				param.sched_priority = max;
			else
				param.sched_priority = (max - min) / 2;
			pr_inf(stdout, "priority not given, defaulting to %d\n",
				param.sched_priority);
		}
		if ((param.sched_priority < min) ||
		    (param.sched_priority > max)) {
			fprintf(stderr, "Scheduler priority level must be "
				"set between %d and %d\n",
				min, max);
			exit(EXIT_FAILURE);
		}
		pr_dbg(stderr, "setting scheduler class '%s', priority %d\n",
			name, param.sched_priority);
		break;
#endif
	default:
		param.sched_priority = 0;
		if (sched_priority != UNDEFINED)
			pr_inf(stdout, "ignoring priority level for "
			"scheduler class '%s'\n", name);
		pr_dbg(stderr, "setting scheduler class '%s'\n", name);
		break;
	}
	rc = sched_setscheduler(getpid(), sched, &param);
	if (rc < 0) {
		fprintf(stderr, "Cannot set scheduler: errno=%d (%s)\n",
			errno, strerror(errno));
		exit(EXIT_FAILURE);
	}
}
#else
void set_sched(const int32_t sched, const int32_t sched_priority)
{
	(void)sched;
	(void)sched_priority;
}
#endif

/*
 *  get_opt_sched()
 *	get scheduler policy
 */
int32_t get_opt_sched(const char *const str)
{
#if defined(SCHED_OTHER)
	if (!strcmp("other", str))
		return SCHED_OTHER;
#endif
#if defined(SCHED_BATCH)
	if (!strcmp("batch", str))
		return SCHED_BATCH;
#endif
#if defined(SCHED_IDLE)
	if (!strcmp("idle", str))
		return SCHED_IDLE;
#endif
#if defined(SCHED_FIFO)
	if (!strcmp("fifo", str))
		return SCHED_FIFO;
#endif
#if defined(SCHED_RR)
	if (!strcmp("rr", str))
		return SCHED_RR;
#endif
#if defined(SCHED_DEADLINE)
	if (!strcmp("deadline", str))
		return SCHED_DEADLINE;
#endif
	if (strcmp("which", str))
		fprintf(stderr, "Invalid sched option: %s\n", str);
	fprintf(stderr, "Available scheduler options are:"
#if defined(SCHED_OTHER)
		" other"
#endif
#if defined(SCHED_BATCH)
		" batch"
#endif
#if defined(SCHED_DEADLINE)
		" deadline"
#endif
#if defined(SCHED_IDLE)
		" idle"
#endif
#if defined(SCHED_FIFO)
		" fifo"
#endif
#if defined(SCHED_FIFO)
		" rr"
#endif
		"\n");
	exit(EXIT_FAILURE);
}