| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License, version 2, as |
| 4 |
* published by the Free Software Foundation |
| 5 |
* |
| 6 |
* This program is distributed in the hope that it will be useful, |
| 7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 |
* GNU General Public License for more details. |
| 10 |
* |
| 11 |
* You should have received a copy of the GNU General Public License |
| 12 |
* along with this program. If not, see <https://gnu.org/licenses/>. |
| 13 |
* |
| 14 |
* Copyright (C) 2004 Robert Love |
| 15 |
* Copyright (C) 2020-2021 Qais Yousef |
| 16 |
* Copyright (C) 2020-2021 Arm Ltd |
| 17 |
*/ |
| 18 |
#ifndef UTIL_LINUX_SCHED_ATTR_H |
| 19 |
#define UTIL_LINUX_SCHED_ATTR_H |
| 20 |
|
| 21 |
/* the SCHED_BATCH is supported since Linux 2.6.16 |
| 22 |
* -- temporary workaround for people with old glibc headers |
| 23 |
*/ |
| 24 |
#if defined (__linux__) && !defined(SCHED_BATCH) |
| 25 |
# define SCHED_BATCH 3 |
| 26 |
#endif |
| 27 |
|
| 28 |
/* the SCHED_IDLE is supported since Linux 2.6.23 |
| 29 |
* commit id 0e6aca43e08a62a48d6770e9a159dbec167bf4c6 |
| 30 |
* -- temporary workaround for people with old glibc headers |
| 31 |
*/ |
| 32 |
#if defined (__linux__) && !defined(SCHED_IDLE) |
| 33 |
# define SCHED_IDLE 5 |
| 34 |
#endif |
| 35 |
|
| 36 |
/* flag by sched_getscheduler() */ |
| 37 |
#if defined(__linux__) && !defined(SCHED_RESET_ON_FORK) |
| 38 |
# define SCHED_RESET_ON_FORK 0x40000000 |
| 39 |
#endif |
| 40 |
|
| 41 |
/* flag by sched_getattr() */ |
| 42 |
#if defined(__linux__) && !defined(SCHED_FLAG_RESET_ON_FORK) |
| 43 |
# define SCHED_FLAG_RESET_ON_FORK 0x01 |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined(__linux__) && !defined(SCHED_FLAG_RECLAIM) |
| 47 |
# define SCHED_FLAG_RECLAIM 0x02 |
| 48 |
#endif |
| 49 |
|
| 50 |
#if defined(__linux__) && !defined(SCHED_FLAG_DL_OVERRUN) |
| 51 |
# define SCHED_FLAG_DL_OVERRUN 0x04 |
| 52 |
#endif |
| 53 |
|
| 54 |
#if defined(__linux__) && !defined(SCHED_FLAG_KEEP_POLICY) |
| 55 |
# define SCHED_FLAG_KEEP_POLICY 0x08 |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(__linux__) && !defined(SCHED_FLAG_KEEP_PARAMS) |
| 59 |
# define SCHED_FLAG_KEEP_PARAMS 0x10 |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(__linux__) && !defined(SCHED_FLAG_UTIL_CLAMP_MIN) |
| 63 |
# define SCHED_FLAG_UTIL_CLAMP_MIN 0x20 |
| 64 |
#endif |
| 65 |
|
| 66 |
#if defined(__linux__) && !defined(SCHED_FLAG_UTIL_CLAMP_MAX) |
| 67 |
# define SCHED_FLAG_UTIL_CLAMP_MAX 0x40 |
| 68 |
#endif |
| 69 |
|
| 70 |
#ifdef HAVE_SYS_SYSCALL_H |
| 71 |
# include <sys/syscall.h> |
| 72 |
#endif |
| 73 |
|
| 74 |
/* usable kernel-headers, but old glibc-headers */ |
| 75 |
#if defined (__linux__) && !defined(SYS_sched_setattr) && defined(__NR_sched_setattr) |
| 76 |
# define SYS_sched_setattr __NR_sched_setattr |
| 77 |
#endif |
| 78 |
|
| 79 |
#if defined (__linux__) && !defined(SYS_sched_getattr) && defined(__NR_sched_getattr) |
| 80 |
# define SYS_sched_getattr __NR_sched_getattr |
| 81 |
#endif |
| 82 |
|
| 83 |
#if defined (__linux__) && !defined(HAVE_SCHED_SETATTR) && defined(SYS_sched_setattr) |
| 84 |
# define HAVE_SCHED_SETATTR |
| 85 |
|
| 86 |
struct sched_attr { |
| 87 |
uint32_t size; |
| 88 |
uint32_t sched_policy; |
| 89 |
uint64_t sched_flags; |
| 90 |
|
| 91 |
/* SCHED_NORMAL, SCHED_BATCH */ |
| 92 |
int32_t sched_nice; |
| 93 |
|
| 94 |
/* SCHED_FIFO, SCHED_RR */ |
| 95 |
uint32_t sched_priority; |
| 96 |
|
| 97 |
/* SCHED_DEADLINE (nsec) */ |
| 98 |
uint64_t sched_runtime; |
| 99 |
uint64_t sched_deadline; |
| 100 |
uint64_t sched_period; |
| 101 |
|
| 102 |
/* UTILIZATION CLAMPING */ |
| 103 |
uint32_t sched_util_min; |
| 104 |
uint32_t sched_util_max; |
| 105 |
}; |
| 106 |
|
| 107 |
static int sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags) |
| 108 |
{ |
| 109 |
return syscall(SYS_sched_setattr, pid, attr, flags); |
| 110 |
} |
| 111 |
|
| 112 |
static int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int size, unsigned int flags) |
| 113 |
{ |
| 114 |
return syscall(SYS_sched_getattr, pid, attr, size, flags); |
| 115 |
} |
| 116 |
#endif |
| 117 |
|
| 118 |
/* the SCHED_DEADLINE is supported since Linux 3.14 |
| 119 |
* commit id aab03e05e8f7e26f51dee792beddcb5cca9215a5 |
| 120 |
* -- sched_setattr() is required for this policy! |
| 121 |
*/ |
| 122 |
#if defined (__linux__) && !defined(SCHED_DEADLINE) && defined(HAVE_SCHED_SETATTR) |
| 123 |
# define SCHED_DEADLINE 6 |
| 124 |
#endif |
| 125 |
|
| 126 |
/* the SCHED_EXT is supported since Linux 6.12 |
| 127 |
* commit id f0e1a0643a59bf1f922fa209cec86a170b784f3f |
| 128 |
* -- temporary workaround for people with old glibc headers |
| 129 |
*/ |
| 130 |
#if defined(__linux__) && !defined(SCHED_EXT) |
| 131 |
# define SCHED_EXT 7 |
| 132 |
#endif |
| 133 |
|
| 134 |
#endif /* UTIL_LINUX_SCHED_ATTR_H */ |