/
sched_helpers.h
267 lines (225 loc) · 5.61 KB
/
sched_helpers.h
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
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef SCHED_HELPERS_H
#define SCHED_HELPERS_H
#include <linux/kconfig.h>
/* Required for some structs */
#include <linux/irq_work.h>
#include <linux/cgroup.h>
#include "introspection.h"
#if HAS_TYPE(struct, cfs_rq)
# if defined(CONFIG_FAIR_GROUP_SCHED) && HAS_MEMBER(struct, cfs_rq, rq)
static inline const struct rq *rq_of(const struct cfs_rq *cfs_rq)
{
return cfs_rq->rq;
}
# elif HAS_MEMBER(struct, rq, cfs)
static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
{
return container_of(cfs_rq, struct rq, cfs);
}
# else
# warning "Cannot get the parent struct rq of a struct cfs_rq"
# endif
#endif
static inline bool entity_is_task(const struct sched_entity *se)
{
return
#if HAS_MEMBER(struct, sched_entity, my_q)
!se->my_q
#else
true
#endif
;
}
#if HAS_TYPE(struct, rq)
static inline int cpu_of(const struct rq *rq)
{
# if defined(CONFIG_SMP) && HAS_MEMBER(struct, rq, cpu)
return rq->cpu;
# else
return 0;
# endif
}
#endif
#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
#if HAS_TYPE(struct, task_group)
static inline bool task_group_is_autogroup(const struct task_group *tg)
{
# if HAS_KERNEL_FEATURE(SCHED_AUTOGROUP)
return !!tg->autogroup;
# else
return false;
# endif
}
#endif
#if HAS_TYPE(struct, task_group)
static int autogroup_path(const struct task_group *tg, char *buf, int buflen)
{
# if HAS_KERNEL_FEATURE(SCHED_AUTOGROUP) && HAS_MEMBER(struct, autogroup, id)
if (!task_group_is_autogroup(tg))
return 0;
return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
# else
return 0;
# endif
}
#endif
#if HAS_TYPE(struct, rq)
/* A cut down version of the original. @p MUST be NULL */
static inline unsigned long uclamp_rq_util_with(const struct rq *rq, unsigned long util)
{
# if HAS_KERNEL_FEATURE(RQ_UCLAMP)
unsigned long min_util;
unsigned long max_util;
min_util = READ_ONCE(rq->uclamp[UCLAMP_MIN].value);
max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
if (unlikely(min_util >= max_util))
return min_util;
return clamp(util, min_util, max_util);
# else
return util;
# endif
}
#endif
#if HAS_TYPE(struct, cfs_rq)
static inline void cfs_rq_tg_path(const struct cfs_rq *cfs_rq, char *path, int len)
{
if (!path)
return;
# if defined(CONFIG_FAIR_GROUP_SCHED) && HAS_MEMBER(struct, cfs_rq, tg) && HAS_MEMBER(struct, task_group, css) && HAS_MEMBER(struct, cgroup_subsys_state, cgroup)
if (cfs_rq && task_group_is_autogroup(cfs_rq->tg))
autogroup_path(cfs_rq->tg, path, len);
else if (cfs_rq && cfs_rq->tg->css.cgroup)
cgroup_path((struct cgroup *)cfs_rq->tg->css.cgroup, path, len);
else
# endif
strlcpy(path, "(null)", len);
}
#endif
#if HAS_TYPE(struct, sched_entity)
static inline const struct cfs_rq *get_group_cfs_rq(const struct sched_entity *se)
{
# if defined(CONFIG_FAIR_GROUP_SCHED) && HAS_MEMBER(struct, sched_entity, my_q)
return se->my_q;
# else
return NULL;
# endif
}
static inline const struct cfs_rq *get_se_cfs_rq(const struct sched_entity *se)
{
# if defined(CONFIG_FAIR_GROUP_SCHED) && HAS_MEMBER(struct, sched_entity, cfs_rq)
return se->cfs_rq;
# else
return NULL;
# endif
}
#endif
#if HAS_TYPE(struct, cfs_rq)
static inline const struct sched_avg *cfs_rq_avg(const struct cfs_rq *cfs_rq)
{
# if HAS_KERNEL_FEATURE(CFS_PELT)
return cfs_rq ? (struct sched_avg *)&cfs_rq->avg : NULL;
# else
return NULL;
# endif
}
static inline char *cfs_rq_path(const struct cfs_rq *cfs_rq, char *str, int len)
{
if (!cfs_rq) {
if (str)
strlcpy(str, "(null)", len);
else
return NULL;
}
cfs_rq_tg_path(cfs_rq, str, len);
return str;
}
static inline int cfs_rq_cpu(const struct cfs_rq *cfs_rq)
{
return cpu_of(rq_of(cfs_rq));
}
#endif
#if HAS_TYPE(struct, rq)
static inline const struct sched_avg *rq_avg_rt(struct rq *rq)
{
# if HAS_KERNEL_FEATURE(RT_PELT)
return rq ? (struct sched_avg *)&rq->avg_rt : NULL;
# else
return NULL;
# endif
}
static inline const struct sched_avg *rq_avg_dl(const struct rq *rq)
{
# if HAS_KERNEL_FEATURE(DL_PELT)
return rq ? (struct sched_avg *)&rq->avg_dl : NULL;
# else
return NULL;
# endif
}
static inline const struct sched_avg *rq_avg_irq(const struct rq *rq)
{
# if HAS_KERNEL_FEATURE(IRQ_PELT)
return rq ? (struct sched_avg *)&rq->avg_irq : NULL;
# else
return NULL;
# endif
}
static inline int rq_cpu(const struct rq *rq)
{
return cpu_of(rq);
}
static inline int rq_cpu_capacity(const struct rq *rq)
{
return
# if HAS_KERNEL_FEATURE(RQ_CAPACITY)
rq->cpu_capacity
# else
SCHED_CAPACITY_SCALE
# endif
;
}
static inline int rq_cpu_orig_capacity(const struct rq *rq)
{
return
# if HAS_KERNEL_FEATURE(FREQ_INVARIANCE)
rq->cpu_capacity_orig;
# else
rq_cpu_capacity(rq)
# endif
;
}
# if HAS_KERNEL_FEATURE(FREQ_INVARIANCE)
DECLARE_PER_CPU(unsigned long, arch_freq_scale);
# endif
static inline int rq_cpu_current_capacity(const struct rq *rq)
{
return
# if HAS_KERNEL_FEATURE(FREQ_INVARIANCE)
({
unsigned long capacity_orig = rq_cpu_orig_capacity(rq);
unsigned long scale_freq = per_cpu(arch_freq_scale, rq->cpu);
cap_scale(capacity_orig, scale_freq);
})
# else
rq_cpu_orig_capacity(rq)
# endif
;
}
# if HAS_KERNEL_FEATURE(RQ_NR_RUNNING)
static inline int rq_nr_running(const struct rq *rq)
{
return rq->nr_running;
}
# endif
#endif
#if HAS_TYPE(struct, root_domain)
static inline const struct cpumask *rd_span(const struct root_domain *rd)
{
# if defined(CONFIG_SMP) && HAS_MEMBER(struct, root_domain, span)
return rd ? (struct cpumask *)rd->span : NULL;
# else
return NULL;
# endif
}
#endif
#endif /* SCHED_HELPERS_H */