sched_m2.c revision 1.6.2.2 1 1.6.2.2 joerg /* $NetBSD: sched_m2.c,v 1.6.2.2 2007/10/26 15:48:38 joerg Exp $ */
2 1.6.2.2 joerg
3 1.6.2.2 joerg /*
4 1.6.2.2 joerg * Copyright (c) 2007, Mindaugas Rasiukevicius
5 1.6.2.2 joerg *
6 1.6.2.2 joerg * Redistribution and use in source and binary forms, with or without
7 1.6.2.2 joerg * modification, are permitted provided that the following conditions
8 1.6.2.2 joerg * are met:
9 1.6.2.2 joerg * 1. Redistributions of source code must retain the above copyright
10 1.6.2.2 joerg * notice, this list of conditions and the following disclaimer.
11 1.6.2.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
12 1.6.2.2 joerg * notice, this list of conditions and the following disclaimer in the
13 1.6.2.2 joerg * documentation and/or other materials provided with the distribution.
14 1.6.2.2 joerg *
15 1.6.2.2 joerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 1.6.2.2 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.6.2.2 joerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.6.2.2 joerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.6.2.2 joerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.6.2.2 joerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.6.2.2 joerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.6.2.2 joerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.6.2.2 joerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.6.2.2 joerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.6.2.2 joerg * POSSIBILITY OF SUCH DAMAGE.
26 1.6.2.2 joerg */
27 1.6.2.2 joerg
28 1.6.2.2 joerg /*
29 1.6.2.2 joerg * TODO:
30 1.6.2.2 joerg * - Implementation of fair share queue;
31 1.6.2.2 joerg * - Support for NUMA;
32 1.6.2.2 joerg */
33 1.6.2.2 joerg
34 1.6.2.2 joerg #include <sys/cdefs.h>
35 1.6.2.2 joerg __KERNEL_RCSID(0, "$NetBSD: sched_m2.c,v 1.6.2.2 2007/10/26 15:48:38 joerg Exp $");
36 1.6.2.2 joerg
37 1.6.2.2 joerg #include <sys/param.h>
38 1.6.2.2 joerg
39 1.6.2.2 joerg #include <sys/cpu.h>
40 1.6.2.2 joerg #include <sys/callout.h>
41 1.6.2.2 joerg #include <sys/errno.h>
42 1.6.2.2 joerg #include <sys/kernel.h>
43 1.6.2.2 joerg #include <sys/kmem.h>
44 1.6.2.2 joerg #include <sys/lwp.h>
45 1.6.2.2 joerg #include <sys/mutex.h>
46 1.6.2.2 joerg #include <sys/pool.h>
47 1.6.2.2 joerg #include <sys/proc.h>
48 1.6.2.2 joerg #include <sys/resource.h>
49 1.6.2.2 joerg #include <sys/resourcevar.h>
50 1.6.2.2 joerg #include <sys/sched.h>
51 1.6.2.2 joerg #include <sys/syscallargs.h>
52 1.6.2.2 joerg #include <sys/sysctl.h>
53 1.6.2.2 joerg #include <sys/types.h>
54 1.6.2.2 joerg
55 1.6.2.2 joerg #include <sys/cpu.h>
56 1.6.2.2 joerg
57 1.6.2.2 joerg /*
58 1.6.2.2 joerg * XXX: Some definitions below will disappear
59 1.6.2.2 joerg * XXX: with the merge of vmlocking branch.
60 1.6.2.2 joerg */
61 1.6.2.2 joerg #define PRI_MAX MAXPRI
62 1.6.2.2 joerg #define PRI_COUNT (PRI_MAX + 1) /* 0 .. 127 -> 128 */
63 1.6.2.2 joerg #define PRI_RT_COUNT (50) /* 0 .. 49 -> 50 */
64 1.6.2.2 joerg #define PRI_TS_COUNT (PRI_COUNT - PRI_RT_COUNT) /* 50 .. 127 -> 78 */
65 1.6.2.2 joerg
66 1.6.2.2 joerg #define PRI_DEFAULT 70 /* 70 */
67 1.6.2.2 joerg #define PRI_REALTIME 50 /* 50 */
68 1.6.2.2 joerg #define PRI_HTS_RANGE 10 /* 50 .. 60 -> 10 */
69 1.6.2.2 joerg
70 1.6.2.2 joerg /*
71 1.6.2.2 joerg * Bits per map.
72 1.6.2.2 joerg */
73 1.6.2.2 joerg #define BITMAP_SHIFT 5 /* 32 bits */
74 1.6.2.2 joerg #define BITMAP_SIZE PRI_COUNT >> BITMAP_SHIFT
75 1.6.2.2 joerg
76 1.6.2.2 joerg /*
77 1.6.2.2 joerg * Time-slices and priorities.
78 1.6.2.2 joerg */
79 1.6.2.2 joerg static u_int min_ts; /* Minimal time-slice */
80 1.6.2.2 joerg static u_int max_ts; /* Maximal time-slice */
81 1.6.2.2 joerg static u_int rt_ts; /* Real-time time-slice */
82 1.6.2.2 joerg static u_int ts_map[PRI_COUNT]; /* Map of time-slices */
83 1.6.2.2 joerg static pri_t high_pri[PRI_COUNT]; /* Map for priority increase */
84 1.6.2.2 joerg
85 1.6.2.2 joerg /*
86 1.6.2.2 joerg * Migration and balancing.
87 1.6.2.2 joerg */
88 1.6.2.2 joerg #ifdef MULTIPROCESSOR
89 1.6.2.2 joerg static u_int cacheht_time; /* Cache hotness time */
90 1.6.2.2 joerg static u_int min_catch; /* Minimal LWP count for catching */
91 1.6.2.2 joerg
92 1.6.2.2 joerg static u_int balance_period; /* Balance period */
93 1.6.2.2 joerg static struct callout balance_ch; /* Callout of balancer */
94 1.6.2.2 joerg
95 1.6.2.2 joerg static struct cpu_info * volatile worker_ci;
96 1.6.2.2 joerg
97 1.6.2.2 joerg #define CACHE_HOT(sil) (sil->sl_lrtime && \
98 1.6.2.2 joerg (hardclock_ticks - sil->sl_lrtime < cacheht_time))
99 1.6.2.2 joerg
100 1.6.2.2 joerg #endif
101 1.6.2.2 joerg
102 1.6.2.2 joerg /*
103 1.6.2.2 joerg * Structures, runqueue.
104 1.6.2.2 joerg */
105 1.6.2.2 joerg
106 1.6.2.2 joerg typedef struct {
107 1.6.2.2 joerg TAILQ_HEAD(, lwp) q_head;
108 1.6.2.2 joerg } queue_t;
109 1.6.2.2 joerg
110 1.6.2.2 joerg typedef struct {
111 1.6.2.2 joerg /* Lock and bitmap */
112 1.6.2.2 joerg kmutex_t r_rq_mutex;
113 1.6.2.2 joerg uint32_t r_bitmap[BITMAP_SIZE];
114 1.6.2.2 joerg /* Counters */
115 1.6.2.2 joerg u_int r_count; /* Count of the threads */
116 1.6.2.2 joerg pri_t r_highest_pri; /* Highest priority */
117 1.6.2.2 joerg u_int r_avgcount; /* Average count of threads */
118 1.6.2.2 joerg u_int r_mcount; /* Count of migratable threads */
119 1.6.2.2 joerg /* Runqueues */
120 1.6.2.2 joerg queue_t r_rt_queue[PRI_RT_COUNT];
121 1.6.2.2 joerg queue_t r_ts_queue[PRI_TS_COUNT];
122 1.6.2.2 joerg } runqueue_t;
123 1.6.2.2 joerg
124 1.6.2.2 joerg typedef struct {
125 1.6.2.2 joerg u_int sl_flags;
126 1.6.2.2 joerg u_int sl_timeslice; /* Time-slice of thread */
127 1.6.2.2 joerg u_int sl_slept; /* Saved sleep time for sleep sum */
128 1.6.2.2 joerg u_int sl_slpsum; /* Sum of sleep time */
129 1.6.2.2 joerg u_int sl_rtime; /* Saved start time of run */
130 1.6.2.2 joerg u_int sl_rtsum; /* Sum of the run time */
131 1.6.2.2 joerg u_int sl_lrtime; /* Last run time */
132 1.6.2.2 joerg } sched_info_lwp_t;
133 1.6.2.2 joerg
134 1.6.2.2 joerg /* Flags */
135 1.6.2.2 joerg #define SL_BATCH 0x01
136 1.6.2.2 joerg
137 1.6.2.2 joerg /* Pool of the scheduler-specific structures for threads */
138 1.6.2.2 joerg static struct pool sil_pool;
139 1.6.2.2 joerg
140 1.6.2.2 joerg /*
141 1.6.2.2 joerg * Prototypes.
142 1.6.2.2 joerg */
143 1.6.2.2 joerg
144 1.6.2.2 joerg static inline void * sched_getrq(runqueue_t *, const pri_t);
145 1.6.2.2 joerg static inline void sched_newts(struct lwp *);
146 1.6.2.2 joerg static void sched_precalcts(void);
147 1.6.2.2 joerg
148 1.6.2.2 joerg #ifdef MULTIPROCESSOR
149 1.6.2.2 joerg static struct lwp * sched_catchlwp(void);
150 1.6.2.2 joerg static void sched_balance(void *);
151 1.6.2.2 joerg #endif
152 1.6.2.2 joerg
153 1.6.2.2 joerg /*
154 1.6.2.2 joerg * Initialization and setup.
155 1.6.2.2 joerg */
156 1.6.2.2 joerg
157 1.6.2.2 joerg void
158 1.6.2.2 joerg sched_rqinit(void)
159 1.6.2.2 joerg {
160 1.6.2.2 joerg struct cpu_info *ci = curcpu();
161 1.6.2.2 joerg
162 1.6.2.2 joerg if (hz < 100) {
163 1.6.2.2 joerg panic("sched_rqinit: value of HZ is too low\n");
164 1.6.2.2 joerg }
165 1.6.2.2 joerg
166 1.6.2.2 joerg /* Default timing ranges */
167 1.6.2.2 joerg min_ts = mstohz(50); /* ~50ms */
168 1.6.2.2 joerg max_ts = mstohz(150); /* ~150ms */
169 1.6.2.2 joerg rt_ts = mstohz(100); /* ~100ms */
170 1.6.2.2 joerg sched_precalcts();
171 1.6.2.2 joerg
172 1.6.2.2 joerg #ifdef MULTIPROCESSOR
173 1.6.2.2 joerg /* Balancing */
174 1.6.2.2 joerg worker_ci = ci;
175 1.6.2.2 joerg cacheht_time = mstohz(5); /* ~5 ms */
176 1.6.2.2 joerg balance_period = mstohz(300); /* ~300ms */
177 1.6.2.2 joerg min_catch = ~0;
178 1.6.2.2 joerg #endif
179 1.6.2.2 joerg
180 1.6.2.2 joerg /* Pool of the scheduler-specific structures */
181 1.6.2.2 joerg pool_init(&sil_pool, sizeof(sched_info_lwp_t), 0, 0, 0,
182 1.6.2.2 joerg "lwpsd", &pool_allocator_nointr, IPL_NONE);
183 1.6.2.2 joerg
184 1.6.2.2 joerg /* Attach the primary CPU here */
185 1.6.2.2 joerg sched_cpuattach(ci);
186 1.6.2.2 joerg
187 1.6.2.2 joerg /* Initialize the scheduler structure of the primary LWP */
188 1.6.2.2 joerg lwp0.l_mutex = &ci->ci_schedstate.spc_lwplock;
189 1.6.2.2 joerg sched_lwp_fork(&lwp0);
190 1.6.2.2 joerg sched_newts(&lwp0);
191 1.6.2.2 joerg }
192 1.6.2.2 joerg
193 1.6.2.2 joerg void
194 1.6.2.2 joerg sched_setup(void)
195 1.6.2.2 joerg {
196 1.6.2.2 joerg
197 1.6.2.2 joerg #ifdef MULTIPROCESSOR
198 1.6.2.2 joerg /* Minimal count of LWPs for catching: log2(count of CPUs) */
199 1.6.2.2 joerg min_catch = min(ffs(ncpu) - 1, 4);
200 1.6.2.2 joerg
201 1.6.2.2 joerg /* Initialize balancing callout and run it */
202 1.6.2.2 joerg callout_init(&balance_ch, CALLOUT_MPSAFE);
203 1.6.2.2 joerg callout_setfunc(&balance_ch, sched_balance, NULL);
204 1.6.2.2 joerg callout_schedule(&balance_ch, balance_period);
205 1.6.2.2 joerg #endif
206 1.6.2.2 joerg }
207 1.6.2.2 joerg
208 1.6.2.2 joerg void
209 1.6.2.2 joerg sched_cpuattach(struct cpu_info *ci)
210 1.6.2.2 joerg {
211 1.6.2.2 joerg runqueue_t *ci_rq;
212 1.6.2.2 joerg void *rq_ptr;
213 1.6.2.2 joerg u_int i, size;
214 1.6.2.2 joerg
215 1.6.2.2 joerg /*
216 1.6.2.2 joerg * Allocate the run queue.
217 1.6.2.2 joerg * XXX: Estimate cache behaviour more..
218 1.6.2.2 joerg */
219 1.6.2.2 joerg size = roundup(sizeof(runqueue_t), CACHE_LINE_SIZE) + CACHE_LINE_SIZE;
220 1.6.2.2 joerg rq_ptr = kmem_zalloc(size, KM_NOSLEEP);
221 1.6.2.2 joerg if (rq_ptr == NULL) {
222 1.6.2.2 joerg panic("scheduler: could not allocate the runqueue");
223 1.6.2.2 joerg }
224 1.6.2.2 joerg /* XXX: Save the original pointer for future.. */
225 1.6.2.2 joerg ci_rq = (void *)(roundup((intptr_t)(rq_ptr), CACHE_LINE_SIZE));
226 1.6.2.2 joerg
227 1.6.2.2 joerg /* Initialize run queues */
228 1.6.2.2 joerg mutex_init(&ci_rq->r_rq_mutex, MUTEX_SPIN, IPL_SCHED);
229 1.6.2.2 joerg for (i = 0; i < PRI_RT_COUNT; i++)
230 1.6.2.2 joerg TAILQ_INIT(&ci_rq->r_rt_queue[i].q_head);
231 1.6.2.2 joerg for (i = 0; i < PRI_TS_COUNT; i++)
232 1.6.2.2 joerg TAILQ_INIT(&ci_rq->r_ts_queue[i].q_head);
233 1.6.2.2 joerg ci_rq->r_highest_pri = PRI_MAX;
234 1.6.2.2 joerg
235 1.6.2.2 joerg ci->ci_schedstate.spc_sched_info = ci_rq;
236 1.6.2.2 joerg ci->ci_schedstate.spc_mutex = &ci_rq->r_rq_mutex;
237 1.6.2.2 joerg }
238 1.6.2.2 joerg
239 1.6.2.2 joerg /* Pre-calculate the time-slices for the priorities */
240 1.6.2.2 joerg static void
241 1.6.2.2 joerg sched_precalcts(void)
242 1.6.2.2 joerg {
243 1.6.2.2 joerg pri_t p;
244 1.6.2.2 joerg u_int i;
245 1.6.2.2 joerg
246 1.6.2.2 joerg for (p = 0; p < PRI_REALTIME; p++) {
247 1.6.2.2 joerg ts_map[p] = rt_ts;
248 1.6.2.2 joerg high_pri[p] = p;
249 1.6.2.2 joerg }
250 1.6.2.2 joerg
251 1.6.2.2 joerg for (p = PRI_REALTIME, i = 0; p < PRI_COUNT; p++, i++) {
252 1.6.2.2 joerg ts_map[p] = min_ts +
253 1.6.2.2 joerg (i * 100 / (PRI_TS_COUNT - 1) * (max_ts - min_ts) / 100);
254 1.6.2.2 joerg high_pri[p] = PRI_REALTIME + (i * PRI_HTS_RANGE /
255 1.6.2.2 joerg (PRI_MAX - PRI_REALTIME));
256 1.6.2.2 joerg }
257 1.6.2.2 joerg }
258 1.6.2.2 joerg
259 1.6.2.2 joerg /*
260 1.6.2.2 joerg * Hooks.
261 1.6.2.2 joerg */
262 1.6.2.2 joerg
263 1.6.2.2 joerg void
264 1.6.2.2 joerg sched_proc_fork(struct proc *parent, struct proc *child)
265 1.6.2.2 joerg {
266 1.6.2.2 joerg struct lwp *l;
267 1.6.2.2 joerg
268 1.6.2.2 joerg LIST_FOREACH(l, &child->p_lwps, l_sibling) {
269 1.6.2.2 joerg lwp_lock(l);
270 1.6.2.2 joerg sched_newts(l);
271 1.6.2.2 joerg lwp_unlock(l);
272 1.6.2.2 joerg }
273 1.6.2.2 joerg }
274 1.6.2.2 joerg
275 1.6.2.2 joerg void
276 1.6.2.2 joerg sched_proc_exit(struct proc *child, struct proc *parent)
277 1.6.2.2 joerg {
278 1.6.2.2 joerg
279 1.6.2.2 joerg /* Dummy */
280 1.6.2.2 joerg }
281 1.6.2.2 joerg
282 1.6.2.2 joerg void
283 1.6.2.2 joerg sched_lwp_fork(struct lwp *l)
284 1.6.2.2 joerg {
285 1.6.2.2 joerg
286 1.6.2.2 joerg KASSERT(l->l_sched_info == NULL);
287 1.6.2.2 joerg l->l_sched_info = pool_get(&sil_pool, PR_WAITOK);
288 1.6.2.2 joerg memset(l->l_sched_info, 0, sizeof(sched_info_lwp_t));
289 1.6.2.2 joerg if (l->l_usrpri >= PRI_REALTIME) /* XXX: For now only.. */
290 1.6.2.2 joerg l->l_usrpri = l->l_priority = PRI_DEFAULT;
291 1.6.2.2 joerg }
292 1.6.2.2 joerg
293 1.6.2.2 joerg void
294 1.6.2.2 joerg sched_lwp_exit(struct lwp *l)
295 1.6.2.2 joerg {
296 1.6.2.2 joerg
297 1.6.2.2 joerg KASSERT(l->l_sched_info != NULL);
298 1.6.2.2 joerg pool_put(&sil_pool, l->l_sched_info);
299 1.6.2.2 joerg l->l_sched_info = NULL;
300 1.6.2.2 joerg }
301 1.6.2.2 joerg
302 1.6.2.2 joerg void
303 1.6.2.2 joerg sched_setrunnable(struct lwp *l)
304 1.6.2.2 joerg {
305 1.6.2.2 joerg
306 1.6.2.2 joerg /* Dummy */
307 1.6.2.2 joerg }
308 1.6.2.2 joerg
309 1.6.2.2 joerg void
310 1.6.2.2 joerg sched_schedclock(struct lwp *l)
311 1.6.2.2 joerg {
312 1.6.2.2 joerg
313 1.6.2.2 joerg /* Dummy */
314 1.6.2.2 joerg }
315 1.6.2.2 joerg
316 1.6.2.2 joerg /*
317 1.6.2.2 joerg * Priorities and time-slice.
318 1.6.2.2 joerg */
319 1.6.2.2 joerg
320 1.6.2.2 joerg void
321 1.6.2.2 joerg sched_nice(struct proc *p, int prio)
322 1.6.2.2 joerg {
323 1.6.2.2 joerg int nprio;
324 1.6.2.2 joerg struct lwp *l;
325 1.6.2.2 joerg
326 1.6.2.2 joerg KASSERT(mutex_owned(&p->p_stmutex));
327 1.6.2.2 joerg
328 1.6.2.2 joerg p->p_nice = prio;
329 1.6.2.2 joerg nprio = max(PRI_DEFAULT + p->p_nice, PRI_REALTIME);
330 1.6.2.2 joerg
331 1.6.2.2 joerg LIST_FOREACH(l, &p->p_lwps, l_sibling) {
332 1.6.2.2 joerg lwp_lock(l);
333 1.6.2.2 joerg lwp_changepri(l, nprio);
334 1.6.2.2 joerg lwp_unlock(l);
335 1.6.2.2 joerg }
336 1.6.2.2 joerg }
337 1.6.2.2 joerg
338 1.6.2.2 joerg /* Recalculate the time-slice */
339 1.6.2.2 joerg static inline void
340 1.6.2.2 joerg sched_newts(struct lwp *l)
341 1.6.2.2 joerg {
342 1.6.2.2 joerg sched_info_lwp_t *sil = l->l_sched_info;
343 1.6.2.2 joerg
344 1.6.2.2 joerg sil->sl_timeslice = ts_map[lwp_eprio(l)];
345 1.6.2.2 joerg }
346 1.6.2.2 joerg
347 1.6.2.2 joerg /*
348 1.6.2.2 joerg * Control of the runqueue.
349 1.6.2.2 joerg */
350 1.6.2.2 joerg
351 1.6.2.2 joerg static inline void *
352 1.6.2.2 joerg sched_getrq(runqueue_t *ci_rq, const pri_t prio)
353 1.6.2.2 joerg {
354 1.6.2.2 joerg
355 1.6.2.2 joerg KASSERT(prio < PRI_COUNT);
356 1.6.2.2 joerg return (prio < PRI_REALTIME) ?
357 1.6.2.2 joerg &ci_rq->r_rt_queue[prio].q_head :
358 1.6.2.2 joerg &ci_rq->r_ts_queue[prio - PRI_REALTIME].q_head;
359 1.6.2.2 joerg }
360 1.6.2.2 joerg
361 1.6.2.2 joerg void
362 1.6.2.2 joerg sched_enqueue(struct lwp *l, bool swtch)
363 1.6.2.2 joerg {
364 1.6.2.2 joerg runqueue_t *ci_rq;
365 1.6.2.2 joerg sched_info_lwp_t *sil = l->l_sched_info;
366 1.6.2.2 joerg TAILQ_HEAD(, lwp) *q_head;
367 1.6.2.2 joerg const pri_t eprio = lwp_eprio(l);
368 1.6.2.2 joerg
369 1.6.2.2 joerg ci_rq = l->l_cpu->ci_schedstate.spc_sched_info;
370 1.6.2.2 joerg KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
371 1.6.2.2 joerg
372 1.6.2.2 joerg /* Update the last run time on switch */
373 1.6.2.2 joerg if (swtch == true) {
374 1.6.2.2 joerg sil->sl_lrtime = hardclock_ticks;
375 1.6.2.2 joerg sil->sl_rtsum += (hardclock_ticks - sil->sl_rtime);
376 1.6.2.2 joerg } else
377 1.6.2.2 joerg sil->sl_lrtime = 0;
378 1.6.2.2 joerg
379 1.6.2.2 joerg /* Enqueue the thread */
380 1.6.2.2 joerg q_head = sched_getrq(ci_rq, eprio);
381 1.6.2.2 joerg if (TAILQ_EMPTY(q_head)) {
382 1.6.2.2 joerg u_int i;
383 1.6.2.2 joerg uint32_t q;
384 1.6.2.2 joerg
385 1.6.2.2 joerg /* Mark bit */
386 1.6.2.2 joerg i = eprio >> BITMAP_SHIFT;
387 1.6.2.2 joerg q = eprio - (i << BITMAP_SHIFT);
388 1.6.2.2 joerg KASSERT((ci_rq->r_bitmap[i] & (1 << q)) == 0);
389 1.6.2.2 joerg ci_rq->r_bitmap[i] |= 1 << q;
390 1.6.2.2 joerg }
391 1.6.2.2 joerg TAILQ_INSERT_TAIL(q_head, l, l_runq);
392 1.6.2.2 joerg ci_rq->r_count++;
393 1.6.2.2 joerg if ((l->l_flag & LW_BOUND) == 0)
394 1.6.2.2 joerg ci_rq->r_mcount++;
395 1.6.2.2 joerg
396 1.6.2.2 joerg /*
397 1.6.2.2 joerg * Update the value of highest priority in the runqueue,
398 1.6.2.2 joerg * if priority of this thread is higher.
399 1.6.2.2 joerg */
400 1.6.2.2 joerg if (eprio < ci_rq->r_highest_pri)
401 1.6.2.2 joerg ci_rq->r_highest_pri = eprio;
402 1.6.2.2 joerg
403 1.6.2.2 joerg sched_newts(l);
404 1.6.2.2 joerg }
405 1.6.2.2 joerg
406 1.6.2.2 joerg void
407 1.6.2.2 joerg sched_dequeue(struct lwp *l)
408 1.6.2.2 joerg {
409 1.6.2.2 joerg runqueue_t *ci_rq;
410 1.6.2.2 joerg TAILQ_HEAD(, lwp) *q_head;
411 1.6.2.2 joerg const pri_t eprio = lwp_eprio(l);
412 1.6.2.2 joerg
413 1.6.2.2 joerg ci_rq = l->l_cpu->ci_schedstate.spc_sched_info;
414 1.6.2.2 joerg KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
415 1.6.2.2 joerg KASSERT(ci_rq->r_highest_pri <= eprio);
416 1.6.2.2 joerg KASSERT(ci_rq->r_bitmap[eprio >> BITMAP_SHIFT] != 0);
417 1.6.2.2 joerg KASSERT(ci_rq->r_count > 0);
418 1.6.2.2 joerg
419 1.6.2.2 joerg ci_rq->r_count--;
420 1.6.2.2 joerg if ((l->l_flag & LW_BOUND) == 0)
421 1.6.2.2 joerg ci_rq->r_mcount--;
422 1.6.2.2 joerg
423 1.6.2.2 joerg q_head = sched_getrq(ci_rq, eprio);
424 1.6.2.2 joerg TAILQ_REMOVE(q_head, l, l_runq);
425 1.6.2.2 joerg if (TAILQ_EMPTY(q_head)) {
426 1.6.2.2 joerg u_int i;
427 1.6.2.2 joerg uint32_t q;
428 1.6.2.2 joerg
429 1.6.2.2 joerg /* Unmark bit */
430 1.6.2.2 joerg i = eprio >> BITMAP_SHIFT;
431 1.6.2.2 joerg q = eprio - (i << BITMAP_SHIFT);
432 1.6.2.2 joerg KASSERT((ci_rq->r_bitmap[i] & (1 << q)) != 0);
433 1.6.2.2 joerg ci_rq->r_bitmap[i] &= ~(1 << q);
434 1.6.2.2 joerg
435 1.6.2.2 joerg /*
436 1.6.2.2 joerg * Update the value of highest priority in the runqueue, in a
437 1.6.2.2 joerg * case it was a last thread in the queue of highest priority.
438 1.6.2.2 joerg */
439 1.6.2.2 joerg if (eprio != ci_rq->r_highest_pri)
440 1.6.2.2 joerg return;
441 1.6.2.2 joerg
442 1.6.2.2 joerg do {
443 1.6.2.2 joerg q = ffs(ci_rq->r_bitmap[i]);
444 1.6.2.2 joerg if (q) {
445 1.6.2.2 joerg ci_rq->r_highest_pri =
446 1.6.2.2 joerg (i << BITMAP_SHIFT) + q - 1;
447 1.6.2.2 joerg return;
448 1.6.2.2 joerg }
449 1.6.2.2 joerg } while (++i < BITMAP_SIZE);
450 1.6.2.2 joerg
451 1.6.2.2 joerg /* If not found - set the maximal value */
452 1.6.2.2 joerg ci_rq->r_highest_pri = PRI_MAX;
453 1.6.2.2 joerg }
454 1.6.2.2 joerg }
455 1.6.2.2 joerg
456 1.6.2.2 joerg void
457 1.6.2.2 joerg sched_slept(struct lwp *l)
458 1.6.2.2 joerg {
459 1.6.2.2 joerg sched_info_lwp_t *sil = l->l_sched_info;
460 1.6.2.2 joerg
461 1.6.2.2 joerg /* Save the time when thread has slept */
462 1.6.2.2 joerg sil->sl_slept = hardclock_ticks;
463 1.6.2.2 joerg
464 1.6.2.2 joerg /*
465 1.6.2.2 joerg * If thread is not a real-time and batch flag is not marked,
466 1.6.2.2 joerg * increase the the priority, and run with lower time-quantum.
467 1.6.2.2 joerg */
468 1.6.2.2 joerg if (l->l_usrpri > PRI_REALTIME && (sil->sl_flags & SL_BATCH) == 0)
469 1.6.2.2 joerg l->l_usrpri--;
470 1.6.2.2 joerg }
471 1.6.2.2 joerg
472 1.6.2.2 joerg void
473 1.6.2.2 joerg sched_wakeup(struct lwp *l)
474 1.6.2.2 joerg {
475 1.6.2.2 joerg sched_info_lwp_t *sil = l->l_sched_info;
476 1.6.2.2 joerg
477 1.6.2.2 joerg /* Update sleep time delta */
478 1.6.2.2 joerg sil->sl_slpsum += (l->l_slptime == 0) ?
479 1.6.2.2 joerg (hardclock_ticks - sil->sl_slept) : hz;
480 1.6.2.2 joerg
481 1.6.2.2 joerg /* If thread was sleeping a second or more - set a high priority */
482 1.6.2.2 joerg if (l->l_slptime > 1 || (hardclock_ticks - sil->sl_slept) >= hz)
483 1.6.2.2 joerg l->l_usrpri = l->l_priority = high_pri[l->l_usrpri];
484 1.6.2.2 joerg
485 1.6.2.2 joerg /* Also, consider looking for a better CPU to wake up */
486 1.6.2.2 joerg if ((l->l_flag & (LW_BOUND | LW_SYSTEM)) == 0)
487 1.6.2.2 joerg l->l_cpu = sched_takecpu(l);
488 1.6.2.2 joerg }
489 1.6.2.2 joerg
490 1.6.2.2 joerg void
491 1.6.2.2 joerg sched_pstats_hook(struct lwp *l)
492 1.6.2.2 joerg {
493 1.6.2.2 joerg sched_info_lwp_t *sil = l->l_sched_info;
494 1.6.2.2 joerg
495 1.6.2.2 joerg /*
496 1.6.2.2 joerg * Set that thread is more CPU-bound, if sum of run time exceeds the
497 1.6.2.2 joerg * sum of sleep time. If it is CPU-bound not a first time - decrease
498 1.6.2.2 joerg * the priority.
499 1.6.2.2 joerg */
500 1.6.2.2 joerg if (sil->sl_rtsum > sil->sl_slpsum) {
501 1.6.2.2 joerg if ((sil->sl_flags & SL_BATCH) && (l->l_usrpri < PRI_MAX))
502 1.6.2.2 joerg l->l_usrpri++;
503 1.6.2.2 joerg sil->sl_flags |= SL_BATCH;
504 1.6.2.2 joerg } else {
505 1.6.2.2 joerg sil->sl_flags &= ~SL_BATCH;
506 1.6.2.2 joerg }
507 1.6.2.2 joerg sil->sl_slpsum = 0;
508 1.6.2.2 joerg sil->sl_rtsum = 0;
509 1.6.2.2 joerg
510 1.6.2.2 joerg /*
511 1.6.2.2 joerg * Estimate only threads on time-sharing run queue, also,
512 1.6.2.2 joerg * ignore the highest time-sharing priority.
513 1.6.2.2 joerg */
514 1.6.2.2 joerg if (l->l_stat != LSRUN || l->l_usrpri <= PRI_REALTIME)
515 1.6.2.2 joerg return;
516 1.6.2.2 joerg
517 1.6.2.2 joerg /* If thread was not ran a second or more - set a high priority */
518 1.6.2.2 joerg if (sil->sl_lrtime && (hardclock_ticks - sil->sl_lrtime >= hz))
519 1.6.2.2 joerg lwp_changepri(l, high_pri[l->l_usrpri]);
520 1.6.2.2 joerg }
521 1.6.2.2 joerg
522 1.6.2.2 joerg /*
523 1.6.2.2 joerg * Migration and balancing.
524 1.6.2.2 joerg */
525 1.6.2.2 joerg
526 1.6.2.2 joerg #ifdef MULTIPROCESSOR
527 1.6.2.2 joerg
528 1.6.2.2 joerg /* Check if LWP can migrate to the chosen CPU */
529 1.6.2.2 joerg static inline bool
530 1.6.2.2 joerg sched_migratable(const struct lwp *l, const struct cpu_info *ci)
531 1.6.2.2 joerg {
532 1.6.2.2 joerg
533 1.6.2.2 joerg if (ci->ci_schedstate.spc_flags & SPCF_OFFLINE)
534 1.6.2.2 joerg return false;
535 1.6.2.2 joerg
536 1.6.2.2 joerg if ((l->l_flag & LW_BOUND) == 0)
537 1.6.2.2 joerg return true;
538 1.6.2.2 joerg #if 0
539 1.6.2.2 joerg return cpu_in_pset(ci, l->l_psid);
540 1.6.2.2 joerg #else
541 1.6.2.2 joerg return false;
542 1.6.2.2 joerg #endif
543 1.6.2.2 joerg }
544 1.6.2.2 joerg
545 1.6.2.2 joerg /*
546 1.6.2.2 joerg * Estimate the migration of LWP to the other CPU.
547 1.6.2.2 joerg * Take and return the CPU, if migration is needed.
548 1.6.2.2 joerg */
549 1.6.2.2 joerg struct cpu_info *
550 1.6.2.2 joerg sched_takecpu(struct lwp *l)
551 1.6.2.2 joerg {
552 1.6.2.2 joerg struct cpu_info *ci, *tci = NULL;
553 1.6.2.2 joerg struct schedstate_percpu *spc;
554 1.6.2.2 joerg runqueue_t *ci_rq;
555 1.6.2.2 joerg sched_info_lwp_t *sil;
556 1.6.2.2 joerg CPU_INFO_ITERATOR cii;
557 1.6.2.2 joerg pri_t eprio, lpri;
558 1.6.2.2 joerg
559 1.6.2.2 joerg ci = l->l_cpu;
560 1.6.2.2 joerg spc = &ci->ci_schedstate;
561 1.6.2.2 joerg ci_rq = spc->spc_sched_info;
562 1.6.2.2 joerg
563 1.6.2.2 joerg /* CPU of this thread is idling - run there */
564 1.6.2.2 joerg if (ci_rq->r_count == 0)
565 1.6.2.2 joerg return ci;
566 1.6.2.2 joerg
567 1.6.2.2 joerg eprio = lwp_eprio(l);
568 1.6.2.2 joerg sil = l->l_sched_info;
569 1.6.2.2 joerg
570 1.6.2.2 joerg /* Stay if thread is cache-hot */
571 1.6.2.2 joerg if (l->l_stat == LSSLEEP && l->l_slptime <= 1 &&
572 1.6.2.2 joerg CACHE_HOT(sil) && eprio <= spc->spc_curpriority)
573 1.6.2.2 joerg return ci;
574 1.6.2.2 joerg
575 1.6.2.2 joerg /* Run on current CPU if priority of thread is higher */
576 1.6.2.2 joerg ci = curcpu();
577 1.6.2.2 joerg spc = &ci->ci_schedstate;
578 1.6.2.2 joerg if (eprio < spc->spc_curpriority && sched_migratable(l, ci))
579 1.6.2.2 joerg return ci;
580 1.6.2.2 joerg
581 1.6.2.2 joerg /*
582 1.6.2.2 joerg * Look for the CPU with the lowest priority thread. In case of
583 1.6.2.2 joerg * equal the priority - check the lower count of the threads.
584 1.6.2.2 joerg */
585 1.6.2.2 joerg lpri = 0;
586 1.6.2.2 joerg ci_rq = NULL;
587 1.6.2.2 joerg tci = l->l_cpu;
588 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci)) {
589 1.6.2.2 joerg runqueue_t *ici_rq;
590 1.6.2.2 joerg pri_t pri;
591 1.6.2.2 joerg
592 1.6.2.2 joerg spc = &ci->ci_schedstate;
593 1.6.2.2 joerg ici_rq = spc->spc_sched_info;
594 1.6.2.2 joerg pri = min(spc->spc_curpriority, ici_rq->r_highest_pri);
595 1.6.2.2 joerg if (pri < lpri)
596 1.6.2.2 joerg continue;
597 1.6.2.2 joerg
598 1.6.2.2 joerg if (pri == lpri && ci_rq && ci_rq->r_count < ici_rq->r_count)
599 1.6.2.2 joerg continue;
600 1.6.2.2 joerg
601 1.6.2.2 joerg if (sched_migratable(l, ci) == false)
602 1.6.2.2 joerg continue;
603 1.6.2.2 joerg
604 1.6.2.2 joerg lpri = pri;
605 1.6.2.2 joerg tci = ci;
606 1.6.2.2 joerg ci_rq = ici_rq;
607 1.6.2.2 joerg }
608 1.6.2.2 joerg
609 1.6.2.2 joerg return tci;
610 1.6.2.2 joerg }
611 1.6.2.2 joerg
612 1.6.2.2 joerg /*
613 1.6.2.2 joerg * Tries to catch an LWP from the runqueue of other CPU.
614 1.6.2.2 joerg */
615 1.6.2.2 joerg static struct lwp *
616 1.6.2.2 joerg sched_catchlwp(void)
617 1.6.2.2 joerg {
618 1.6.2.2 joerg struct cpu_info *curci = curcpu(), *ci = worker_ci;
619 1.6.2.2 joerg TAILQ_HEAD(, lwp) *q_head;
620 1.6.2.2 joerg runqueue_t *ci_rq;
621 1.6.2.2 joerg struct lwp *l;
622 1.6.2.2 joerg
623 1.6.2.2 joerg if (curci == ci)
624 1.6.2.2 joerg return NULL;
625 1.6.2.2 joerg
626 1.6.2.2 joerg /* Lockless check */
627 1.6.2.2 joerg ci_rq = ci->ci_schedstate.spc_sched_info;
628 1.6.2.2 joerg if (ci_rq->r_count < min_catch)
629 1.6.2.2 joerg return NULL;
630 1.6.2.2 joerg
631 1.6.2.2 joerg /*
632 1.6.2.2 joerg * Double-lock the runqueues.
633 1.6.2.2 joerg */
634 1.6.2.2 joerg if (curci < ci) {
635 1.6.2.2 joerg spc_lock(ci);
636 1.6.2.2 joerg } else if (!mutex_tryenter(ci->ci_schedstate.spc_mutex)) {
637 1.6.2.2 joerg const runqueue_t *cur_rq = curci->ci_schedstate.spc_sched_info;
638 1.6.2.2 joerg
639 1.6.2.2 joerg spc_unlock(curci);
640 1.6.2.2 joerg spc_lock(ci);
641 1.6.2.2 joerg spc_lock(curci);
642 1.6.2.2 joerg
643 1.6.2.2 joerg if (cur_rq->r_count) {
644 1.6.2.2 joerg spc_unlock(ci);
645 1.6.2.2 joerg return NULL;
646 1.6.2.2 joerg }
647 1.6.2.2 joerg }
648 1.6.2.2 joerg
649 1.6.2.2 joerg if (ci_rq->r_count < min_catch) {
650 1.6.2.2 joerg spc_unlock(ci);
651 1.6.2.2 joerg return NULL;
652 1.6.2.2 joerg }
653 1.6.2.2 joerg
654 1.6.2.2 joerg /* Take the highest priority thread */
655 1.6.2.2 joerg q_head = sched_getrq(ci_rq, ci_rq->r_highest_pri);
656 1.6.2.2 joerg l = TAILQ_FIRST(q_head);
657 1.6.2.2 joerg
658 1.6.2.2 joerg for (;;) {
659 1.6.2.2 joerg sched_info_lwp_t *sil;
660 1.6.2.2 joerg
661 1.6.2.2 joerg /* Check the first and next result from the queue */
662 1.6.2.2 joerg if (l == NULL)
663 1.6.2.2 joerg break;
664 1.6.2.2 joerg
665 1.6.2.2 joerg /* Look for threads, whose are allowed to migrate */
666 1.6.2.2 joerg sil = l->l_sched_info;
667 1.6.2.2 joerg if ((l->l_flag & LW_SYSTEM) || CACHE_HOT(sil) ||
668 1.6.2.2 joerg sched_migratable(l, curci) == false) {
669 1.6.2.2 joerg l = TAILQ_NEXT(l, l_runq);
670 1.6.2.2 joerg continue;
671 1.6.2.2 joerg }
672 1.6.2.2 joerg /* Recheck if chosen thread is still on the runqueue */
673 1.6.2.2 joerg if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM)) {
674 1.6.2.2 joerg sched_dequeue(l);
675 1.6.2.2 joerg l->l_cpu = curci;
676 1.6.2.2 joerg lwp_setlock(l, curci->ci_schedstate.spc_mutex);
677 1.6.2.2 joerg sched_enqueue(l, false);
678 1.6.2.2 joerg break;
679 1.6.2.2 joerg }
680 1.6.2.2 joerg l = TAILQ_NEXT(l, l_runq);
681 1.6.2.2 joerg }
682 1.6.2.2 joerg spc_unlock(ci);
683 1.6.2.2 joerg
684 1.6.2.2 joerg return l;
685 1.6.2.2 joerg }
686 1.6.2.2 joerg
687 1.6.2.2 joerg /*
688 1.6.2.2 joerg * Periodical calculations for balancing.
689 1.6.2.2 joerg */
690 1.6.2.2 joerg static void
691 1.6.2.2 joerg sched_balance(void *nocallout)
692 1.6.2.2 joerg {
693 1.6.2.2 joerg struct cpu_info *ci, *hci;
694 1.6.2.2 joerg runqueue_t *ci_rq;
695 1.6.2.2 joerg CPU_INFO_ITERATOR cii;
696 1.6.2.2 joerg u_int highest;
697 1.6.2.2 joerg
698 1.6.2.2 joerg hci = curcpu();
699 1.6.2.2 joerg highest = 0;
700 1.6.2.2 joerg
701 1.6.2.2 joerg /* Make lockless countings */
702 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci)) {
703 1.6.2.2 joerg ci_rq = ci->ci_schedstate.spc_sched_info;
704 1.6.2.2 joerg
705 1.6.2.2 joerg /* Average count of the threads */
706 1.6.2.2 joerg ci_rq->r_avgcount = (ci_rq->r_avgcount + ci_rq->r_mcount) >> 1;
707 1.6.2.2 joerg
708 1.6.2.2 joerg /* Look for CPU with the highest average */
709 1.6.2.2 joerg if (ci_rq->r_avgcount > highest) {
710 1.6.2.2 joerg hci = ci;
711 1.6.2.2 joerg highest = ci_rq->r_avgcount;
712 1.6.2.2 joerg }
713 1.6.2.2 joerg }
714 1.6.2.2 joerg
715 1.6.2.2 joerg /* Update the worker */
716 1.6.2.2 joerg worker_ci = hci;
717 1.6.2.2 joerg
718 1.6.2.2 joerg if (nocallout == NULL)
719 1.6.2.2 joerg callout_schedule(&balance_ch, balance_period);
720 1.6.2.2 joerg }
721 1.6.2.2 joerg
722 1.6.2.2 joerg #else
723 1.6.2.2 joerg
724 1.6.2.2 joerg struct cpu_info *
725 1.6.2.2 joerg sched_takecpu(struct lwp *l)
726 1.6.2.2 joerg {
727 1.6.2.2 joerg
728 1.6.2.2 joerg return l->l_cpu;
729 1.6.2.2 joerg }
730 1.6.2.2 joerg
731 1.6.2.2 joerg #endif /* MULTIPROCESSOR */
732 1.6.2.2 joerg
733 1.6.2.2 joerg /*
734 1.6.2.2 joerg * Scheduler mill.
735 1.6.2.2 joerg */
736 1.6.2.2 joerg struct lwp *
737 1.6.2.2 joerg sched_nextlwp(void)
738 1.6.2.2 joerg {
739 1.6.2.2 joerg struct cpu_info *ci = curcpu();
740 1.6.2.2 joerg struct schedstate_percpu *spc;
741 1.6.2.2 joerg TAILQ_HEAD(, lwp) *q_head;
742 1.6.2.2 joerg sched_info_lwp_t *sil;
743 1.6.2.2 joerg runqueue_t *ci_rq;
744 1.6.2.2 joerg struct lwp *l;
745 1.6.2.2 joerg
746 1.6.2.2 joerg spc = &ci->ci_schedstate;
747 1.6.2.2 joerg ci_rq = ci->ci_schedstate.spc_sched_info;
748 1.6.2.2 joerg
749 1.6.2.2 joerg #ifdef MULTIPROCESSOR
750 1.6.2.2 joerg /* If runqueue is empty, try to catch some thread from other CPU */
751 1.6.2.2 joerg if (spc->spc_flags & SPCF_OFFLINE) {
752 1.6.2.2 joerg if (ci_rq->r_mcount == 0)
753 1.6.2.2 joerg return NULL;
754 1.6.2.2 joerg } else if (ci_rq->r_count == 0) {
755 1.6.2.2 joerg /* Reset the counter, and call the balancer */
756 1.6.2.2 joerg ci_rq->r_avgcount = 0;
757 1.6.2.2 joerg sched_balance(ci);
758 1.6.2.2 joerg
759 1.6.2.2 joerg /* The re-locking will be done inside */
760 1.6.2.2 joerg return sched_catchlwp();
761 1.6.2.2 joerg }
762 1.6.2.2 joerg #else
763 1.6.2.2 joerg if (ci_rq->r_count == 0)
764 1.6.2.2 joerg return NULL;
765 1.6.2.2 joerg #endif
766 1.6.2.2 joerg
767 1.6.2.2 joerg /* Take the highest priority thread */
768 1.6.2.2 joerg KASSERT(ci_rq->r_bitmap[ci_rq->r_highest_pri >> BITMAP_SHIFT]);
769 1.6.2.2 joerg q_head = sched_getrq(ci_rq, ci_rq->r_highest_pri);
770 1.6.2.2 joerg l = TAILQ_FIRST(q_head);
771 1.6.2.2 joerg KASSERT(l != NULL);
772 1.6.2.2 joerg
773 1.6.2.2 joerg /* Update the counters */
774 1.6.2.2 joerg sil = l->l_sched_info;
775 1.6.2.2 joerg KASSERT(sil->sl_timeslice >= min_ts);
776 1.6.2.2 joerg KASSERT(sil->sl_timeslice <= max_ts);
777 1.6.2.2 joerg spc->spc_ticks = sil->sl_timeslice;
778 1.6.2.2 joerg sil->sl_rtime = hardclock_ticks;
779 1.6.2.2 joerg
780 1.6.2.2 joerg return l;
781 1.6.2.2 joerg }
782 1.6.2.2 joerg
783 1.6.2.2 joerg bool
784 1.6.2.2 joerg sched_curcpu_runnable_p(void)
785 1.6.2.2 joerg {
786 1.6.2.2 joerg const struct cpu_info *ci = curcpu();
787 1.6.2.2 joerg const runqueue_t *ci_rq = ci->ci_schedstate.spc_sched_info;
788 1.6.2.2 joerg
789 1.6.2.2 joerg if (ci->ci_schedstate.spc_flags & SPCF_OFFLINE)
790 1.6.2.2 joerg return ci_rq->r_mcount;
791 1.6.2.2 joerg
792 1.6.2.2 joerg return ci_rq->r_count;
793 1.6.2.2 joerg }
794 1.6.2.2 joerg
795 1.6.2.2 joerg /*
796 1.6.2.2 joerg * Time-driven events.
797 1.6.2.2 joerg */
798 1.6.2.2 joerg
799 1.6.2.2 joerg /*
800 1.6.2.2 joerg * Called once per time-quantum. This routine is CPU-local and runs at
801 1.6.2.2 joerg * IPL_SCHED, thus the locking is not needed.
802 1.6.2.2 joerg */
803 1.6.2.2 joerg void
804 1.6.2.2 joerg sched_tick(struct cpu_info *ci)
805 1.6.2.2 joerg {
806 1.6.2.2 joerg const runqueue_t *ci_rq = ci->ci_schedstate.spc_sched_info;
807 1.6.2.2 joerg struct schedstate_percpu *spc = &ci->ci_schedstate;
808 1.6.2.2 joerg struct lwp *l = curlwp;
809 1.6.2.2 joerg sched_info_lwp_t *sil = l->l_sched_info;
810 1.6.2.2 joerg
811 1.6.2.2 joerg if (CURCPU_IDLE_P())
812 1.6.2.2 joerg return;
813 1.6.2.2 joerg
814 1.6.2.2 joerg switch (l->l_policy) {
815 1.6.2.2 joerg case SCHED_FIFO:
816 1.6.2.2 joerg /*
817 1.6.2.2 joerg * Update the time-quantum, and continue running,
818 1.6.2.2 joerg * if thread runs on FIFO real-time policy.
819 1.6.2.2 joerg */
820 1.6.2.2 joerg spc->spc_ticks = sil->sl_timeslice;
821 1.6.2.2 joerg return;
822 1.6.2.2 joerg case SCHED_OTHER:
823 1.6.2.2 joerg /* Decrease the priority, and run with a higher time-quantum */
824 1.6.2.2 joerg if (l->l_usrpri < PRI_REALTIME)
825 1.6.2.2 joerg break;
826 1.6.2.2 joerg l->l_usrpri = min(l->l_usrpri + 1, PRI_MAX);
827 1.6.2.2 joerg l->l_priority = l->l_usrpri;
828 1.6.2.2 joerg break;
829 1.6.2.2 joerg }
830 1.6.2.2 joerg
831 1.6.2.2 joerg /*
832 1.6.2.2 joerg * If there are higher priority threads or threads in the same queue,
833 1.6.2.2 joerg * mark that thread should yield, otherwise, continue running.
834 1.6.2.2 joerg */
835 1.6.2.2 joerg if (lwp_eprio(l) >= ci_rq->r_highest_pri) {
836 1.6.2.2 joerg spc->spc_flags |= SPCF_SHOULDYIELD;
837 1.6.2.2 joerg cpu_need_resched(ci, 0);
838 1.6.2.2 joerg } else
839 1.6.2.2 joerg spc->spc_ticks = sil->sl_timeslice;
840 1.6.2.2 joerg }
841 1.6.2.2 joerg
842 1.6.2.2 joerg /*
843 1.6.2.2 joerg * Sysctl nodes and initialization.
844 1.6.2.2 joerg */
845 1.6.2.2 joerg
846 1.6.2.2 joerg static int
847 1.6.2.2 joerg sysctl_sched_mints(SYSCTLFN_ARGS)
848 1.6.2.2 joerg {
849 1.6.2.2 joerg struct sysctlnode node;
850 1.6.2.2 joerg struct cpu_info *ci;
851 1.6.2.2 joerg int error, newsize;
852 1.6.2.2 joerg CPU_INFO_ITERATOR cii;
853 1.6.2.2 joerg
854 1.6.2.2 joerg node = *rnode;
855 1.6.2.2 joerg node.sysctl_data = &newsize;
856 1.6.2.2 joerg
857 1.6.2.2 joerg newsize = hztoms(min_ts);
858 1.6.2.2 joerg error = sysctl_lookup(SYSCTLFN_CALL(&node));
859 1.6.2.2 joerg if (error || newp == NULL)
860 1.6.2.2 joerg return error;
861 1.6.2.2 joerg
862 1.6.2.2 joerg if (newsize < 1 || newsize > hz || newsize >= max_ts)
863 1.6.2.2 joerg return EINVAL;
864 1.6.2.2 joerg
865 1.6.2.2 joerg /* It is safe to do this in such order */
866 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci))
867 1.6.2.2 joerg spc_lock(ci);
868 1.6.2.2 joerg
869 1.6.2.2 joerg min_ts = mstohz(newsize);
870 1.6.2.2 joerg sched_precalcts();
871 1.6.2.2 joerg
872 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci))
873 1.6.2.2 joerg spc_unlock(ci);
874 1.6.2.2 joerg
875 1.6.2.2 joerg return 0;
876 1.6.2.2 joerg }
877 1.6.2.2 joerg
878 1.6.2.2 joerg static int
879 1.6.2.2 joerg sysctl_sched_maxts(SYSCTLFN_ARGS)
880 1.6.2.2 joerg {
881 1.6.2.2 joerg struct sysctlnode node;
882 1.6.2.2 joerg struct cpu_info *ci;
883 1.6.2.2 joerg int error, newsize;
884 1.6.2.2 joerg CPU_INFO_ITERATOR cii;
885 1.6.2.2 joerg
886 1.6.2.2 joerg node = *rnode;
887 1.6.2.2 joerg node.sysctl_data = &newsize;
888 1.6.2.2 joerg
889 1.6.2.2 joerg newsize = hztoms(max_ts);
890 1.6.2.2 joerg error = sysctl_lookup(SYSCTLFN_CALL(&node));
891 1.6.2.2 joerg if (error || newp == NULL)
892 1.6.2.2 joerg return error;
893 1.6.2.2 joerg
894 1.6.2.2 joerg if (newsize < 10 || newsize > hz || newsize <= min_ts)
895 1.6.2.2 joerg return EINVAL;
896 1.6.2.2 joerg
897 1.6.2.2 joerg /* It is safe to do this in such order */
898 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci))
899 1.6.2.2 joerg spc_lock(ci);
900 1.6.2.2 joerg
901 1.6.2.2 joerg max_ts = mstohz(newsize);
902 1.6.2.2 joerg sched_precalcts();
903 1.6.2.2 joerg
904 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci))
905 1.6.2.2 joerg spc_unlock(ci);
906 1.6.2.2 joerg
907 1.6.2.2 joerg return 0;
908 1.6.2.2 joerg }
909 1.6.2.2 joerg
910 1.6.2.2 joerg SYSCTL_SETUP(sysctl_sched_setup, "sysctl kern.sched subtree setup")
911 1.6.2.2 joerg {
912 1.6.2.2 joerg const struct sysctlnode *node = NULL;
913 1.6.2.2 joerg
914 1.6.2.2 joerg sysctl_createv(clog, 0, NULL, NULL,
915 1.6.2.2 joerg CTLFLAG_PERMANENT,
916 1.6.2.2 joerg CTLTYPE_NODE, "kern", NULL,
917 1.6.2.2 joerg NULL, 0, NULL, 0,
918 1.6.2.2 joerg CTL_KERN, CTL_EOL);
919 1.6.2.2 joerg sysctl_createv(clog, 0, NULL, &node,
920 1.6.2.2 joerg CTLFLAG_PERMANENT,
921 1.6.2.2 joerg CTLTYPE_NODE, "sched",
922 1.6.2.2 joerg SYSCTL_DESCR("Scheduler options"),
923 1.6.2.2 joerg NULL, 0, NULL, 0,
924 1.6.2.2 joerg CTL_KERN, CTL_CREATE, CTL_EOL);
925 1.6.2.2 joerg
926 1.6.2.2 joerg if (node == NULL)
927 1.6.2.2 joerg return;
928 1.6.2.2 joerg
929 1.6.2.2 joerg sysctl_createv(clog, 0, &node, NULL,
930 1.6.2.2 joerg CTLFLAG_PERMANENT,
931 1.6.2.2 joerg CTLTYPE_STRING, "name", NULL,
932 1.6.2.2 joerg NULL, 0, __UNCONST("M2"), 0,
933 1.6.2.2 joerg CTL_CREATE, CTL_EOL);
934 1.6.2.2 joerg sysctl_createv(clog, 0, &node, NULL,
935 1.6.2.2 joerg CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
936 1.6.2.2 joerg CTLTYPE_INT, "maxts",
937 1.6.2.2 joerg SYSCTL_DESCR("Maximal time quantum (in microseconds)"),
938 1.6.2.2 joerg sysctl_sched_maxts, 0, &max_ts, 0,
939 1.6.2.2 joerg CTL_CREATE, CTL_EOL);
940 1.6.2.2 joerg sysctl_createv(clog, 0, &node, NULL,
941 1.6.2.2 joerg CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
942 1.6.2.2 joerg CTLTYPE_INT, "mints",
943 1.6.2.2 joerg SYSCTL_DESCR("Minimal time quantum (in microseconds)"),
944 1.6.2.2 joerg sysctl_sched_mints, 0, &min_ts, 0,
945 1.6.2.2 joerg CTL_CREATE, CTL_EOL);
946 1.6.2.2 joerg
947 1.6.2.2 joerg #ifdef MULTIPROCESSOR
948 1.6.2.2 joerg sysctl_createv(clog, 0, &node, NULL,
949 1.6.2.2 joerg CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
950 1.6.2.2 joerg CTLTYPE_INT, "cacheht_time",
951 1.6.2.2 joerg SYSCTL_DESCR("Cache hotness time"),
952 1.6.2.2 joerg NULL, 0, &cacheht_time, 0,
953 1.6.2.2 joerg CTL_CREATE, CTL_EOL);
954 1.6.2.2 joerg sysctl_createv(clog, 0, &node, NULL,
955 1.6.2.2 joerg CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
956 1.6.2.2 joerg CTLTYPE_INT, "balance_period",
957 1.6.2.2 joerg SYSCTL_DESCR("Balance period"),
958 1.6.2.2 joerg NULL, 0, &balance_period, 0,
959 1.6.2.2 joerg CTL_CREATE, CTL_EOL);
960 1.6.2.2 joerg sysctl_createv(clog, 0, &node, NULL,
961 1.6.2.2 joerg CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
962 1.6.2.2 joerg CTLTYPE_INT, "min_catch",
963 1.6.2.2 joerg SYSCTL_DESCR("Minimal count of threads for catching"),
964 1.6.2.2 joerg NULL, 0, &min_catch, 0,
965 1.6.2.2 joerg CTL_CREATE, CTL_EOL);
966 1.6.2.2 joerg #endif
967 1.6.2.2 joerg }
968 1.6.2.2 joerg
969 1.6.2.2 joerg /*
970 1.6.2.2 joerg * Debugging.
971 1.6.2.2 joerg */
972 1.6.2.2 joerg
973 1.6.2.2 joerg #ifdef DDB
974 1.6.2.2 joerg
975 1.6.2.2 joerg void
976 1.6.2.2 joerg sched_print_runqueue(void (*pr)(const char *, ...))
977 1.6.2.2 joerg {
978 1.6.2.2 joerg runqueue_t *ci_rq;
979 1.6.2.2 joerg sched_info_lwp_t *sil;
980 1.6.2.2 joerg struct lwp *l;
981 1.6.2.2 joerg struct proc *p;
982 1.6.2.2 joerg int i;
983 1.6.2.2 joerg
984 1.6.2.2 joerg struct cpu_info *ci;
985 1.6.2.2 joerg CPU_INFO_ITERATOR cii;
986 1.6.2.2 joerg
987 1.6.2.2 joerg for (CPU_INFO_FOREACH(cii, ci)) {
988 1.6.2.2 joerg ci_rq = ci->ci_schedstate.spc_sched_info;
989 1.6.2.2 joerg
990 1.6.2.2 joerg (*pr)("Run-queue (CPU = %d):\n", ci->ci_cpuid);
991 1.6.2.2 joerg (*pr)(" pid.lid = %d.%d, threads count = %u, "
992 1.6.2.2 joerg "avgcount = %u, highest pri = %d\n",
993 1.6.2.2 joerg ci->ci_curlwp->l_proc->p_pid, ci->ci_curlwp->l_lid,
994 1.6.2.2 joerg ci_rq->r_count, ci_rq->r_avgcount, ci_rq->r_highest_pri);
995 1.6.2.2 joerg i = 0;
996 1.6.2.2 joerg do {
997 1.6.2.2 joerg int b;
998 1.6.2.2 joerg b = ci_rq->r_bitmap[i];
999 1.6.2.2 joerg (*pr)(" bitmap[%d] => [ %d (0x%x) ]\n", i, ffs(b), b);
1000 1.6.2.2 joerg } while (++i < BITMAP_SIZE);
1001 1.6.2.2 joerg }
1002 1.6.2.2 joerg
1003 1.6.2.2 joerg (*pr)(" %5s %4s %4s %10s %3s %4s %11s %3s %s\n",
1004 1.6.2.2 joerg "LID", "PRI", "UPRI", "FL", "ST", "TS", "LWP", "CPU", "LRTIME");
1005 1.6.2.2 joerg
1006 1.6.2.2 joerg PROCLIST_FOREACH(p, &allproc) {
1007 1.6.2.2 joerg (*pr)(" /- %d (%s)\n", (int)p->p_pid, p->p_comm);
1008 1.6.2.2 joerg LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1009 1.6.2.2 joerg sil = l->l_sched_info;
1010 1.6.2.2 joerg ci = l->l_cpu;
1011 1.6.2.2 joerg (*pr)(" | %5d %4u %4u 0x%8.8x %3s %4u %11p %3d "
1012 1.6.2.2 joerg "%u ST=%d RT=%d %d\n",
1013 1.6.2.2 joerg (int)l->l_lid, l->l_priority, l->l_usrpri,
1014 1.6.2.2 joerg l->l_flag, l->l_stat == LSRUN ? "RQ" :
1015 1.6.2.2 joerg (l->l_stat == LSSLEEP ? "SQ" : "-"),
1016 1.6.2.2 joerg sil->sl_timeslice, l, ci->ci_cpuid,
1017 1.6.2.2 joerg (u_int)(hardclock_ticks - sil->sl_lrtime),
1018 1.6.2.2 joerg sil->sl_slpsum, sil->sl_rtsum, sil->sl_flags);
1019 1.6.2.2 joerg }
1020 1.6.2.2 joerg }
1021 1.6.2.2 joerg }
1022 1.6.2.2 joerg
1023 1.6.2.2 joerg #endif /* defined(DDB) */
1024