forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io-priority.c
145 lines (136 loc) · 3.53 KB
/
io-priority.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
/*
* 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"
/*
* get_opt_ionice_class()
* string io scheduler to IOPRIO_CLASS
*/
int32_t get_opt_ionice_class(const char *const str)
{
#if defined(IOPRIO_CLASS_IDLE)
if (!strcmp("idle", str))
return IOPRIO_CLASS_IDLE;
#endif
#if defined(IOPRIO_CLASS_BE)
if (!strcmp("besteffort", str) ||
!strcmp("be", str))
return IOPRIO_CLASS_BE;
#endif
#if defined(IOPRIO_CLASS_RT)
if (!strcmp("realtime", str) ||
!strcmp("rt", str))
return IOPRIO_CLASS_RT;
#endif
if (strcmp("which", str))
(void)fprintf(stderr, "Invalid ionice-class option: %s\n", str);
(void)fprintf(stderr, "Available options are:");
#if defined(IOPRIO_CLASS_IDLE)
(void)fprintf(stderr, " idle");
#endif
#if defined(IOPRIO_CLASS_BE)
(void)fprintf(stderr, " besteffort be");
#endif
#if defined(IOPRIO_CLASS_RT)
(void)fprintf(stderr, " realtime rt");
#endif
(void)fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
/*
* shim_ioprio_set()
* ioprio_set system call
*/
int shim_ioprio_set(int which, int who, int ioprio)
{
#if defined(__linux__) && defined(__NR_ioprio_set)
return syscall(__NR_ioprio_set, which, who, ioprio);
#else
(void)which;
(void)who;
(void)ioprio;
errno = ENOSYS;
return -1;
#endif
}
/*
* shim_ioprio_get()
* ioprio_get system call
*/
int shim_ioprio_get(int which, int who)
{
#if defined(__linux__) && defined(__NR_ioprio_get)
return syscall(__NR_ioprio_get, which, who);
#else
(void)which;
(void)who;
errno = ENOSYS;
return -1;
#endif
}
#if defined(__linux__) && defined(__NR_ioprio_set)
/*
* set_iopriority()
* check ioprio settings and set
*/
void set_iopriority(const int32_t class, const int32_t level)
{
int data = level, rc;
switch (class) {
case UNDEFINED: /* No preference, don't set */
return;
case IOPRIO_CLASS_RT:
case IOPRIO_CLASS_BE:
if (level < 0 || level > 7) {
(void)fprintf(stderr, "Priority levels range from 0 "
"(max) to 7 (min)\n");
exit(EXIT_FAILURE);
}
break;
case IOPRIO_CLASS_IDLE:
if ((level != UNDEFINED) &&
(level != 0))
(void)fprintf(stderr, "Cannot set priority level "
"with idle, defaulting to 0\n");
data = 0;
break;
default:
(void)fprintf(stderr, "Unknown priority class: %d\n", class);
exit(EXIT_FAILURE);
}
rc = shim_ioprio_set(IOPRIO_WHO_PROCESS, 0,
IOPRIO_PRIO_VALUE(class, data));
if (rc < 0) {
(void)fprintf(stderr, "Cannot set I/O priority: errno=%d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
#else
void set_iopriority(const int32_t class, const int32_t level)
{
(void)class;
(void)level;
}
#endif