Allow userland to bind a process to specific CPUs. The initial
[dragonfly.git] / sys / kern / usched_dummy.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/kern/usched_dummy.c,v 1.2 2006/05/29 22:57:22 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/queue.h>
42 #include <sys/proc.h>
43 #include <sys/rtprio.h>
44 #include <sys/uio.h>
45 #include <sys/sysctl.h>
46 #include <sys/resourcevar.h>
47 #include <sys/spinlock.h>
48 #include <machine/ipl.h>
49 #include <machine/cpu.h>
50 #include <machine/smp.h>
51
52 #include <sys/thread2.h>
53 #include <sys/spinlock2.h>
54
55 #define MAXPRI                  128
56 #define PRIBASE_REALTIME        0
57 #define PRIBASE_NORMAL          MAXPRI
58 #define PRIBASE_IDLE            (MAXPRI * 2)
59 #define PRIBASE_THREAD          (MAXPRI * 3)
60 #define PRIBASE_NULL            (MAXPRI * 4)
61
62 #define lwp_priority    lwp_usdata.bsd4.priority
63 #define lwp_estcpu      lwp_usdata.bsd4.estcpu
64
65 static void dummy_acquire_curproc(struct lwp *lp);
66 static void dummy_release_curproc(struct lwp *lp);
67 static void dummy_select_curproc(globaldata_t gd);
68 static void dummy_setrunqueue(struct lwp *lp);
69 static void dummy_schedulerclock(struct lwp *lp, sysclock_t period,
70                                 sysclock_t cpstamp);
71 static void dummy_recalculate_estcpu(struct lwp *lp);
72 static void dummy_resetpriority(struct lwp *lp);
73 static void dummy_forking(struct lwp *plp, struct lwp *lp);
74 static void dummy_exiting(struct lwp *plp, struct lwp *lp);
75
76 struct usched usched_dummy = {
77         { NULL },
78         "dummy", "Dummy DragonFly Scheduler",
79         NULL,                   /* default registration */
80         NULL,                   /* default deregistration */
81         dummy_acquire_curproc,
82         dummy_release_curproc,
83         dummy_select_curproc,
84         dummy_setrunqueue,
85         dummy_schedulerclock,
86         dummy_recalculate_estcpu,
87         dummy_resetpriority,
88         dummy_forking,
89         dummy_exiting,
90         NULL                    /* setcpumask not supported */
91 };
92
93 struct usched_dummy_pcpu {
94         int     rrcount;
95         struct thread helper_thread;
96         struct lwp *uschedcp;
97 };
98
99 typedef struct usched_dummy_pcpu *dummy_pcpu_t;
100
101 static struct usched_dummy_pcpu dummy_pcpu[MAXCPU];
102 static cpumask_t dummy_curprocmask = -1;
103 static cpumask_t dummy_rdyprocmask;
104 static struct spinlock dummy_spin;
105 static TAILQ_HEAD(rq, lwp) dummy_runq;
106 static int dummy_runqcount;
107
108 static int usched_dummy_rrinterval = (ESTCPUFREQ + 9) / 10;
109 SYSCTL_INT(_kern, OID_AUTO, usched_dummy_rrinterval, CTLFLAG_RW,
110         &usched_dummy_rrinterval, 0, "");
111
112 /*
113  * Initialize the run queues at boot time, clear cpu 0 in curprocmask
114  * to allow dummy scheduling on cpu 0.
115  */
116 static void
117 dummyinit(void *dummy)
118 {
119         TAILQ_INIT(&dummy_runq);
120         spin_init(&dummy_spin);
121         atomic_clear_int(&dummy_curprocmask, 1);
122 }
123 SYSINIT(runqueue, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, dummyinit, NULL)
124
125 /*
126  * DUMMY_ACQUIRE_CURPROC
127  *
128  * This function is called when the kernel intends to return to userland.
129  * It is responsible for making the thread the current designated userland
130  * thread for this cpu, blocking if necessary.
131  *
132  * We are expected to handle userland reschedule requests here too.
133  *
134  * WARNING! THIS FUNCTION IS ALLOWED TO CAUSE THE CURRENT THREAD TO MIGRATE
135  * TO ANOTHER CPU!  Because most of the kernel assumes that no migration will
136  * occur, this function is called only under very controlled circumstances.
137  *
138  * MPSAFE
139  */
140 static void
141 dummy_acquire_curproc(struct lwp *lp)
142 {
143         globaldata_t gd = mycpu;
144         dummy_pcpu_t dd = &dummy_pcpu[gd->gd_cpuid];
145         thread_t td = lp->lwp_thread;
146
147         /*
148          * Possibly select another thread
149          */
150         if (user_resched_wanted())
151                 dummy_select_curproc(gd);
152
153         /*
154          * If this cpu has no current thread, select ourself
155          */
156         if (dd->uschedcp == NULL && TAILQ_EMPTY(&dummy_runq)) {
157                 atomic_set_int(&dummy_curprocmask, gd->gd_cpumask);
158                 dd->uschedcp = lp;
159                 return;
160         }
161
162         /*
163          * If this cpu's current user process thread is not our thread,
164          * deschedule ourselves and place us on the run queue, then
165          * switch away.
166          *
167          * We loop until we become the current process.  Its a good idea
168          * to run any passive release(s) before we mess with the scheduler
169          * so our thread is in the expected state.
170          */
171         KKASSERT(dd->uschedcp != lp);
172         if (td->td_release)
173                 td->td_release(lp->lwp_thread);
174         do {
175                 crit_enter();
176                 lwkt_deschedule_self(td);
177                 dummy_setrunqueue(lp);
178                 if ((td->td_flags & TDF_RUNQ) == 0)
179                         ++lp->lwp_stats->p_ru.ru_nivcsw;
180                 lwkt_switch();          /* WE MAY MIGRATE TO ANOTHER CPU */
181                 crit_exit();
182                 gd = mycpu;
183                 dd = &dummy_pcpu[gd->gd_cpuid];
184                 KKASSERT((lp->lwp_proc->p_flag & P_ONRUNQ) == 0);
185         } while (dd->uschedcp != lp);
186 }
187
188 /*
189  * DUMMY_RELEASE_CURPROC
190  *
191  * This routine detaches the current thread from the userland scheduler,
192  * usually because the thread needs to run in the kernel (at kernel priority)
193  * for a while.
194  *
195  * This routine is also responsible for selecting a new thread to
196  * make the current thread.
197  *
198  * WARNING!  The MP lock may be in an unsynchronized state due to the
199  * way get_mplock() works and the fact that this function may be called
200  * from a passive release during a lwkt_switch().   try_mplock() will deal 
201  * with this for us but you should be aware that td_mpcount may not be
202  * useable.
203  *
204  * MPSAFE
205  */
206 static void
207 dummy_release_curproc(struct lwp *lp)
208 {
209         globaldata_t gd = mycpu;
210         dummy_pcpu_t dd = &dummy_pcpu[gd->gd_cpuid];
211
212         KKASSERT((lp->lwp_proc->p_flag & P_ONRUNQ) == 0);
213         if (dd->uschedcp == lp) {
214                 dummy_select_curproc(gd);
215         }
216 }
217
218 /*
219  * DUMMY_SELECT_CURPROC
220  *
221  * Select a new current process for this cpu.  This satisfies a user
222  * scheduler reschedule request so clear that too.
223  *
224  * This routine is also responsible for equal-priority round-robining,
225  * typically triggered from dummy_schedulerclock().  In our dummy example
226  * all the 'user' threads are LWKT scheduled all at once and we just
227  * call lwkt_switch().
228  *
229  * MPSAFE
230  */
231 static
232 void
233 dummy_select_curproc(globaldata_t gd)
234 {
235         dummy_pcpu_t dd = &dummy_pcpu[gd->gd_cpuid];
236         struct lwp *lp;
237
238         clear_user_resched();
239         spin_lock_wr(&dummy_spin);
240         if ((lp = TAILQ_FIRST(&dummy_runq)) == NULL) {
241                 dd->uschedcp = NULL;
242                 atomic_clear_int(&dummy_curprocmask, gd->gd_cpumask);
243                 spin_unlock_wr(&dummy_spin);
244         } else {
245                 --dummy_runqcount;
246                 TAILQ_REMOVE(&dummy_runq, lp, lwp_procq);
247                 lp->lwp_proc->p_flag &= ~P_ONRUNQ;
248                 dd->uschedcp = lp;
249                 atomic_set_int(&dummy_curprocmask, gd->gd_cpumask);
250                 spin_unlock_wr(&dummy_spin);
251 #ifdef SMP
252                 lwkt_acquire(lp->lwp_thread);
253 #endif
254                 lwkt_schedule(lp->lwp_thread);
255         }
256 }
257
258 /*
259  * DUMMY_SETRUNQUEUE
260  *
261  * This routine is called to schedule a new user process after a fork.
262  * The scheduler module itself might also call this routine to place
263  * the current process on the userland scheduler's run queue prior
264  * to calling dummy_select_curproc().
265  *
266  * The caller may set P_PASSIVE_ACQ in p_flag to indicate that we should
267  * attempt to leave the thread on the current cpu.
268  *
269  * MPSAFE
270  */
271 static void
272 dummy_setrunqueue(struct lwp *lp)
273 {
274         globaldata_t gd = mycpu;
275         dummy_pcpu_t dd = &dummy_pcpu[gd->gd_cpuid];
276         cpumask_t mask;
277         int cpuid;
278
279         if (dd->uschedcp == NULL) {
280                 dd->uschedcp = lp;
281                 atomic_set_int(&dummy_curprocmask, gd->gd_cpumask);
282                 lwkt_schedule(lp->lwp_thread);
283         } else {
284                 /*
285                  * Add to our global runq
286                  */
287                 KKASSERT((lp->lwp_proc->p_flag & P_ONRUNQ) == 0);
288                 spin_lock_wr(&dummy_spin);
289                 ++dummy_runqcount;
290                 TAILQ_INSERT_TAIL(&dummy_runq, lp, lwp_procq);
291                 lp->lwp_proc->p_flag |= P_ONRUNQ;
292 #ifdef SMP
293                 lwkt_giveaway(lp->lwp_thread);
294 #endif
295
296                 /* lp = TAILQ_FIRST(&dummy_runq); */
297
298                 /*
299                  * Notify the next available cpu.  P.S. some
300                  * cpu affinity could be done here.
301                  *
302                  * The rdyprocmask bit placeholds the knowledge that there
303                  * is a process on the runq that needs service.  If the
304                  * helper thread cannot find a home for it it will forward
305                  * the request to another available cpu.
306                  */
307                 mask = ~dummy_curprocmask & dummy_rdyprocmask & 
308                        gd->gd_other_cpus;
309                 if (mask) {
310                         cpuid = bsfl(mask);
311                         atomic_clear_int(&dummy_rdyprocmask, 1 << cpuid);
312                         spin_unlock_wr(&dummy_spin);
313                         lwkt_schedule(&dummy_pcpu[cpuid].helper_thread);
314                 } else {
315                         spin_unlock_wr(&dummy_spin);
316                 }
317         }
318 }
319
320 /*
321  * This routine is called from a systimer IPI.  Thus it is called with 
322  * a critical section held.  Any spinlocks we get here that are also
323  * obtained in other procedures must be proected by a critical section
324  * in those other procedures to avoid a deadlock.
325  *
326  * The MP lock may or may not be held on entry and cannot be obtained
327  * by this routine (because it is called from a systimer IPI).  Additionally,
328  * because this is equivalent to a FAST interrupt, spinlocks cannot be used
329  * (or at least, you have to check that gd_spin* counts are 0 before you
330  * can).
331  *
332  * This routine is called at ESTCPUFREQ on each cpu independantly.
333  *
334  * This routine typically queues a reschedule request, which will cause
335  * the scheduler's BLAH_select_curproc() to be called as soon as possible.
336  *
337  * MPSAFE
338  */
339 static
340 void
341 dummy_schedulerclock(struct lwp *lp, sysclock_t period, sysclock_t cpstamp)
342 {
343         globaldata_t gd = mycpu;
344         dummy_pcpu_t dd = &dummy_pcpu[gd->gd_cpuid];
345
346         if (++dd->rrcount >= usched_dummy_rrinterval) {
347                 dd->rrcount = 0;
348                 need_user_resched();
349         }
350 }
351
352 /*
353  * DUMMY_RECALCULATE_ESTCPU
354  *
355  * Called once a second for any process that is running or has slept
356  * for less then 2 seconds.
357  *
358  * MPSAFE
359  */
360 static
361 void 
362 dummy_recalculate_estcpu(struct lwp *lp)
363 {
364 }
365
366 /*
367  * DUMMY_RESETPRIORITY
368  *
369  * This routine is called after the kernel has potentially modified
370  * the lwp_rtprio structure.  The target process may be running or sleeping
371  * or scheduled but not yet running or owned by another cpu.  Basically,
372  * it can be in virtually any state.
373  *
374  * This routine is called by fork1() for initial setup with the process 
375  * of the run queue, and also may be called normally with the process on or
376  * off the run queue.
377  *
378  * MPSAFE
379  */
380 static void
381 dummy_resetpriority(struct lwp *lp)
382 {
383         /* XXX spinlock usually needed */
384         /*
385          * Set p_priority for general process comparisons
386          */
387         switch(lp->lwp_rtprio.type) {
388         case RTP_PRIO_REALTIME:
389                 lp->lwp_priority = PRIBASE_REALTIME + lp->lwp_rtprio.prio;
390                 return;
391         case RTP_PRIO_NORMAL:
392                 lp->lwp_priority = PRIBASE_NORMAL + lp->lwp_rtprio.prio;
393                 break;
394         case RTP_PRIO_IDLE:
395                 lp->lwp_priority = PRIBASE_IDLE + lp->lwp_rtprio.prio;
396                 return;
397         case RTP_PRIO_THREAD:
398                 lp->lwp_priority = PRIBASE_THREAD + lp->lwp_rtprio.prio;
399                 return;
400         }
401         /* XXX spinlock usually needed */
402 }
403
404
405 /*
406  * DUMMY_FORKING
407  *
408  * Called from fork1() when a new child process is being created.  Allows
409  * the scheduler to predispose the child process before it gets scheduled.
410  *
411  * MPSAFE
412  */
413 static void
414 dummy_forking(struct lwp *plp, struct lwp *lp)
415 {
416         lp->lwp_estcpu = plp->lwp_estcpu;
417 #if 0
418         ++plp->lwp_estcpu;
419 #endif
420 }
421
422 /*
423  * DUMMY_EXITING
424  *
425  * Called when the parent reaps a child.   Typically used to propogate cpu
426  * use by the child back to the parent as part of a batch detection
427  * heuristic.  
428  *
429  * NOTE: cpu use is not normally back-propogated to PID 1.
430  *
431  * MPSAFE
432  */
433 static void
434 dummy_exiting(struct lwp *plp, struct lwp *lp)
435 {
436 }
437
438 /*
439  * SMP systems may need a scheduler helper thread.  This is how one can be
440  * setup.
441  *
442  * We use a neat LWKT scheduling trick to interlock the helper thread.  It
443  * is possible to deschedule an LWKT thread and then do some work before
444  * switching away.  The thread can be rescheduled at any time, even before
445  * we switch away.
446  */
447 #ifdef SMP
448
449 static void
450 dummy_sched_thread(void *dummy)
451 {
452     globaldata_t gd;
453     dummy_pcpu_t dd;
454     struct lwp *lp;
455     cpumask_t cpumask;
456     cpumask_t tmpmask;
457     int cpuid;
458     int tmpid;
459
460     gd = mycpu;
461     cpuid = gd->gd_cpuid;
462     dd = &dummy_pcpu[cpuid];
463     cpumask = 1 << cpuid;
464
465     /*
466      * Our Scheduler helper thread does not need to hold the MP lock
467      */
468     rel_mplock();
469
470     for (;;) {
471         lwkt_deschedule_self(gd->gd_curthread);         /* interlock */
472         atomic_set_int(&dummy_rdyprocmask, cpumask);
473         spin_lock_wr(&dummy_spin);
474         if (dd->uschedcp) {
475                 /*
476                  * We raced another cpu trying to schedule a thread onto us.
477                  * If the runq isn't empty hit another free cpu.
478                  */
479                 tmpmask = ~dummy_curprocmask & dummy_rdyprocmask & 
480                           gd->gd_other_cpus;
481                 if (tmpmask && dummy_runqcount) {
482                         tmpid = bsfl(tmpmask);
483                         KKASSERT(tmpid != cpuid);
484                         atomic_clear_int(&dummy_rdyprocmask, 1 << tmpid);
485                         spin_unlock_wr(&dummy_spin);
486                         lwkt_schedule(&dummy_pcpu[tmpid].helper_thread);
487                 } else {
488                         spin_unlock_wr(&dummy_spin);
489                 }
490         } else if ((lp = TAILQ_FIRST(&dummy_runq)) != NULL) {
491                 --dummy_runqcount;
492                 TAILQ_REMOVE(&dummy_runq, lp, lwp_procq);
493                 lp->lwp_proc->p_flag &= ~P_ONRUNQ;
494                 dd->uschedcp = lp;
495                 atomic_set_int(&dummy_curprocmask, cpumask);
496                 spin_unlock_wr(&dummy_spin);
497 #ifdef SMP
498                 lwkt_acquire(lp->lwp_thread);
499 #endif
500                 lwkt_schedule(lp->lwp_thread);
501         } else {
502                 spin_unlock_wr(&dummy_spin);
503         }
504         lwkt_switch();
505     }
506 }
507
508 /*
509  * Setup our scheduler helpers.  Note that curprocmask bit 0 has already
510  * been cleared by rqinit() and we should not mess with it further.
511  */
512 static void
513 dummy_sched_thread_cpu_init(void)
514 {
515     int i;
516
517     if (bootverbose)
518         printf("start dummy scheduler helpers on cpus:");
519
520     for (i = 0; i < ncpus; ++i) {
521         dummy_pcpu_t dd = &dummy_pcpu[i];
522         cpumask_t mask = 1 << i;
523
524         if ((mask & smp_active_mask) == 0)
525             continue;
526
527         if (bootverbose)
528             printf(" %d", i);
529
530         lwkt_create(dummy_sched_thread, NULL, NULL, &dd->helper_thread, 
531                     TDF_STOPREQ, i, "dsched %d", i);
532
533         /*
534          * Allow user scheduling on the target cpu.  cpu #0 has already
535          * been enabled in rqinit().
536          */
537         if (i)
538             atomic_clear_int(&dummy_curprocmask, mask);
539         atomic_set_int(&dummy_rdyprocmask, mask);
540     }
541     if (bootverbose)
542         printf("\n");
543 }
544 SYSINIT(uschedtd, SI_SUB_FINISH_SMP, SI_ORDER_ANY,
545         dummy_sched_thread_cpu_init, NULL)
546
547 #endif
548