cmogstored 1.8.1 - use default system stack size
[cmogstored.git] / ioprio.h
blob74b458143c43ccbec048014570f271ad952bccdc
1 /*
2 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
3 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 */
6 #include <sys/syscall.h>
8 #if defined(SYS_ioprio_get) && defined(SYS_ioprio_set)
10 #ifndef IOPRIO_PRIO_CLASS
11 #include "ioprio_linux.h"
12 #endif /* Linux headers */
15 * specifying which == IOPRIO_WHO_PROCESS and who == 0
16 * means ioprio works on the current task in Linux.
17 * This means we can avoid a syscall for gettid()
20 static inline int mog_ioprio_get(int which, int who)
22 return syscall(SYS_ioprio_get, which, who);
25 static inline int mog_ioprio_set(int which, int who, int ioprio)
27 return syscall(SYS_ioprio_set, which, who, ioprio);
30 static int mog_ioprio_drop(void)
32 int rc;
33 int oldprio = mog_ioprio_get(IOPRIO_WHO_PROCESS, 0);
34 int newprio;
35 static int warned;
37 if (oldprio == -1)
38 return -1;
40 /* don't bother unless it's already best-effort */
41 switch (IOPRIO_PRIO_CLASS(oldprio)) {
42 case IOPRIO_CLASS_NONE:
44 * we have to filter out data for ioprio_set to succeed,
45 * NONE means the task gets ioprio based on niceness
47 oldprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
48 case IOPRIO_CLASS_BE:
49 break; /* OK */
50 default:
51 return -1;
54 newprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 7);
55 rc = mog_ioprio_set(IOPRIO_WHO_PROCESS, 0, newprio);
56 if (rc == 0)
57 return oldprio;
59 if (!warned) {
60 warned = 1;
61 syslog(LOG_WARNING, "failed to drop IO priority: %m");
63 return -1;
66 static void mog_ioprio_restore(int ioprio)
68 static int warned;
69 int rc = mog_ioprio_set(IOPRIO_WHO_PROCESS, 0, ioprio);
71 if (rc < 0 && !warned) {
72 warned = 1;
73 syslog(LOG_WARNING, "failed to restore IO priority: %m");
77 #else /* workalikes */
78 static inline int mog_ioprio_drop(void)
80 return 0;
83 static inline void mog_ioprio_restore(int ioprio)
86 #endif