Repository navigation
Expand file tree
/
Copy pathniceness.c
More file actions
356 lines (314 loc) · 8.3 KB
/
Copy pathniceness.c
File metadata and controls
356 lines (314 loc) · 8.3 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "rsync.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_LINUX_IOPRIO_H
#include <linux/ioprio.h>
#endif
#ifdef HAVE_SYS_SYSCALL_H
#include <sys/syscall.h>
#endif
extern int am_server;
extern int am_daemon;
#define PRIORITY_UNSET (-100)
#define DEFAULT_NICE 19
#if defined HAVE_SETPRIORITY && defined HAVE_SYS_RESOURCE_H
#define SUPPORT_NICE_PRIORITY 1
#endif
#if defined HAVE_LINUX_IOPRIO_H && defined HAVE_SYS_SYSCALL_H \
&& defined SYS_ioprio_set && defined IOPRIO_PRIO_VALUE
#define SUPPORT_IO_PRIORITY 1
#endif
enum io_priority_class {
IO_PRIORITY_NONE,
IO_PRIORITY_REALTIME,
IO_PRIORITY_BEST_EFFORT,
IO_PRIORITY_IDLE
};
struct priority_setting {
int cpu;
int io_class;
int io_data;
};
struct priority_options {
struct priority_setting local;
struct priority_setting remote;
};
static struct priority_options priorities = {
{ PRIORITY_UNSET, PRIORITY_UNSET, 0 },
{ PRIORITY_UNSET, PRIORITY_UNSET, 0 }
};
static void clear_setting(struct priority_setting *setting)
{
setting->cpu = PRIORITY_UNSET;
setting->io_class = PRIORITY_UNSET;
setting->io_data = 0;
}
void niceness_reset(void)
{
clear_setting(&priorities.local);
clear_setting(&priorities.remote);
}
void niceness_use_remote(void)
{
priorities.local = priorities.remote;
clear_setting(&priorities.remote);
}
static void default_setting(struct priority_setting *setting)
{
setting->cpu = DEFAULT_NICE;
setting->io_class = IO_PRIORITY_IDLE;
setting->io_data = 0;
}
static uchar ascii_lower(uchar ch)
{
return ch >= 'A' && ch <= 'Z' ? ch + ('a' - 'A') : ch;
}
static int token_equal(const char *str, size_t len, const char *word)
{
size_t i;
if (strlen(word) != len)
return 0;
for (i = 0; i < len; i++) {
if (ascii_lower((uchar)str[i]) != ascii_lower((uchar)word[i]))
return 0;
}
return 1;
}
static int parse_io_priority(const char *str, size_t len, int *class_p, int *data_p)
{
if (token_equal(str, len, "none"))
*class_p = IO_PRIORITY_NONE;
else if (token_equal(str, len, "idle"))
*class_p = IO_PRIORITY_IDLE;
else if (len == 4 && str[2] == '_' && str[3] >= '0' && str[3] <= '7'
&& (token_equal(str, 2, "rt") || token_equal(str, 2, "be"))) {
*class_p = ascii_lower((uchar)str[0]) == 'r'
? IO_PRIORITY_REALTIME : IO_PRIORITY_BEST_EFFORT;
*data_p = str[3] - '0';
return 1;
} else
return 0;
*data_p = 0;
return 1;
}
static int parse_priority(const char *str, const char *end, struct priority_setting *setting)
{
char *num_end;
long value;
clear_setting(setting);
if (parse_io_priority(str, end - str, &setting->io_class, &setting->io_data))
return 1;
if (!isdigit((uchar)*str)
&& !((*str == '+' || *str == '-') && str + 1 < end
&& isdigit((uchar)str[1])))
return 0;
errno = 0;
value = strtol(str, &num_end, 10);
if (errno == ERANGE || num_end == str || num_end > end || value < -20 || value > 19)
return 0;
setting->cpu = (int)value;
if (num_end == end)
return 1;
if (*num_end++ != '/' || num_end == end)
return 0;
return parse_io_priority(num_end, end - num_end,
&setting->io_class, &setting->io_data);
}
static int parse_target(const char *str, size_t len, int *local_p, int *remote_p)
{
if (token_equal(str, len, "local")) {
*local_p = 1;
*remote_p = 0;
} else if (token_equal(str, len, "remote")) {
*local_p = 0;
*remote_p = 1;
} else if (token_equal(str, len, "all"))
*local_p = *remote_p = 1;
else
return 0;
return 1;
}
static void merge_setting(struct priority_setting *dest, const struct priority_setting *src)
{
if (src->cpu != PRIORITY_UNSET)
dest->cpu = src->cpu;
if (src->io_class != PRIORITY_UNSET) {
dest->io_class = src->io_class;
dest->io_data = src->io_data;
}
}
int niceness_parse(const char *arg)
{
struct priority_options parsed = priorities;
const char *str = arg;
if (!str || !*str)
return 0;
while (*str) {
struct priority_setting setting;
const char *end = strchr(str, ',');
const char *colon;
int local = 1, remote = 1;
if (!end)
end = str + strlen(str);
if (end == str)
return 0;
colon = memchr(str, ':', end - str);
if (colon) {
if (!parse_target(str, colon - str, &local, &remote)
|| !parse_priority(colon + 1, end, &setting))
return 0;
} else if (parse_target(str, end - str, &local, &remote))
default_setting(&setting);
else if (!parse_priority(str, end, &setting))
return 0;
if (local)
merge_setting(&parsed.local, &setting);
if (remote)
merge_setting(&parsed.remote, &setting);
if (*end) {
str = end + 1;
if (!*str)
return 0;
} else
str = end;
}
priorities = parsed;
return 1;
}
static const char *io_priority_name(int class, int data, char *buf, size_t len)
{
switch (class) {
case IO_PRIORITY_NONE:
return "none";
case IO_PRIORITY_REALTIME:
snprintf(buf, len, "rt_%d", data);
return buf;
case IO_PRIORITY_BEST_EFFORT:
snprintf(buf, len, "be_%d", data);
return buf;
case IO_PRIORITY_IDLE:
return "idle";
}
return "unknown";
}
char *niceness_remote_option(void)
{
struct priority_setting *setting = &priorities.remote;
char *arg;
char io_buf[5];
const char *io_name;
int ret;
if (setting->cpu == PRIORITY_UNSET && setting->io_class == PRIORITY_UNSET)
return NULL;
if (setting->io_class != PRIORITY_UNSET)
io_name = io_priority_name(setting->io_class, setting->io_data,
io_buf, sizeof io_buf);
else
io_name = NULL;
if (setting->cpu != PRIORITY_UNSET && setting->io_class != PRIORITY_UNSET)
ret = asprintf(&arg, "--nice=local:%d/%s", setting->cpu, io_name);
else if (setting->cpu != PRIORITY_UNSET)
ret = asprintf(&arg, "--nice=local:%d", setting->cpu);
else
ret = asprintf(&arg, "--nice=local:%s", io_name);
if (ret < 0)
out_of_memory("niceness_remote_option");
return arg;
}
int niceness_check_daemon_request(char *err_buf, size_t err_len)
{
struct priority_setting *setting = &priorities.local;
if (setting->cpu != PRIORITY_UNSET && setting->cpu != DEFAULT_NICE) {
snprintf(err_buf, err_len,
"daemon clients may only request CPU nice 19\n");
return 0;
}
if (setting->io_class != PRIORITY_UNSET
&& setting->io_class != IO_PRIORITY_IDLE) {
snprintf(err_buf, err_len,
"daemon clients may only request idle I/O priority\n");
return 0;
}
return 1;
}
static const char *process_role(void)
{
return am_daemon ? "daemon" : am_server ? "server" : "client";
}
static int apply_cpu_priority(int priority)
{
#ifdef SUPPORT_NICE_PRIORITY
if (setpriority(PRIO_PROCESS, 0, priority) < 0) {
rsyserr(FWARNING, errno, "failed to set %s CPU priority to %d",
process_role(), priority);
return 0;
}
if (DEBUG_GTE(CMD, 1))
rprintf(FINFO, "set %s CPU priority to %d\n", process_role(), priority);
return 1;
#else
rprintf(FWARNING,
"cannot set %s CPU priority to %d: "
"operation is not supported on this system\n",
process_role(), priority);
return 0;
#endif
}
static void apply_io_priority(int class, int data)
{
char name_buf[5];
const char *name = io_priority_name(class, data, name_buf, sizeof name_buf);
#ifdef SUPPORT_IO_PRIORITY
int kernel_class;
switch (class) {
case IO_PRIORITY_NONE:
kernel_class = IOPRIO_CLASS_NONE;
break;
case IO_PRIORITY_REALTIME:
kernel_class = IOPRIO_CLASS_RT;
break;
case IO_PRIORITY_BEST_EFFORT:
kernel_class = IOPRIO_CLASS_BE;
break;
case IO_PRIORITY_IDLE:
kernel_class = IOPRIO_CLASS_IDLE;
break;
default:
return;
}
if (syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0,
IOPRIO_PRIO_VALUE(kernel_class, data)) < 0)
rsyserr(FWARNING, errno, "failed to set %s I/O priority to %s",
process_role(), name);
else if (DEBUG_GTE(CMD, 1))
rprintf(FINFO, "set %s I/O priority to %s\n", process_role(), name);
#else
rprintf(FWARNING,
"cannot set %s I/O priority to %s: "
"operation is not supported on this system\n",
process_role(), name);
#endif
}
void niceness_apply(void)
{
struct priority_setting setting = priorities.local;
int cpu_applied = 1;
clear_setting(&priorities.local);
if (setting.cpu != PRIORITY_UNSET)
cpu_applied = apply_cpu_priority(setting.cpu);
#ifndef SUPPORT_NICE_PRIORITY
(void)cpu_applied;
#endif
if (setting.io_class != PRIORITY_UNSET) {
#ifdef SUPPORT_NICE_PRIORITY
if (am_daemon && setting.cpu != PRIORITY_UNSET
&& setting.io_class == IO_PRIORITY_NONE && !cpu_applied) {
rprintf(FWARNING,
"not resetting daemon I/O priority because the CPU priority change failed\n");
return;
}
#endif
apply_io_priority(setting.io_class, setting.io_data);
}
}