kernel - add usched_dfly algorith, set as default for now
[dragonfly.git] / sys / kern / kern_usched.c
1 /*
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sergey Glushchenko <deen@smz.com.ua>
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/kern_usched.c,v 1.9 2007/07/02 17:06:55 dillon Exp $
35  */
36
37 #include <sys/errno.h>
38 #include <sys/globaldata.h>             /* curthread */
39 #include <sys/proc.h>
40 #include <sys/priv.h>
41 #include <sys/sysproto.h>               /* struct usched_set_args */
42 #include <sys/systm.h>                  /* strcmp() */
43 #include <sys/usched.h> 
44
45 #include <sys/mplock2.h>
46
47 #include <machine/smp.h>
48
49 static TAILQ_HEAD(, usched) usched_list = TAILQ_HEAD_INITIALIZER(usched_list);
50
51 cpumask_t usched_mastermask = -1;
52
53 /*
54  * Called from very low level boot code, i386/i386/machdep.c/init386().
55  * We cannot do anything fancy.  no malloc's, no nothing other then 
56  * static initialization.
57  */
58 struct usched *
59 usched_init(void)
60 {
61         const char *defsched;
62
63         defsched = kgetenv("kern.user_scheduler");
64
65         /*
66          * Add various userland schedulers to the system.
67          */
68         usched_ctl(&usched_bsd4, USCH_ADD);
69         usched_ctl(&usched_dfly, USCH_ADD);
70         usched_ctl(&usched_dummy, USCH_ADD);
71         if (defsched == NULL )
72                 return(&usched_dfly);
73         if (strcmp(defsched, "bsd4") == 0)
74                 return(&usched_bsd4);
75         if (strcmp(defsched, "dfly") == 0)
76                 return(&usched_dfly);
77         kprintf("WARNING: Running dummy userland scheduler\n");
78         return(&usched_dummy);
79 }
80
81 /*
82  * USCHED_CTL
83  *
84  * SYNOPSIS:
85  *      Add/remove usched to/from list.
86  *      
87  * ARGUMENTS:
88  *      usched - pointer to target scheduler
89  *      action - addition or removal ?
90  *
91  * RETURN VALUES:
92  *      0 - success
93  *      EINVAL - error
94  */
95 int
96 usched_ctl(struct usched *usched, int action)
97 {
98         struct usched *item;    /* temporaly for TAILQ processing */
99         int error = 0;
100
101         switch(action) {
102         case USCH_ADD:
103                 /*
104                  * Make sure it isn't already on the list
105                  */
106 #ifdef INVARIANTS
107                 TAILQ_FOREACH(item, &usched_list, entry) {
108                         KKASSERT(item != usched);
109                 }
110 #endif
111                 /*
112                  * Optional callback to the scheduler before we officially
113                  * add it to the list.
114                  */
115                 if (usched->usched_register)
116                         usched->usched_register();
117                 TAILQ_INSERT_TAIL(&usched_list, usched, entry);
118                 break;
119         case USCH_REM:
120                 /*
121                  * Do not allow the default scheduler to be removed
122                  */
123                 if (strcmp(usched->name, "bsd4") == 0) {
124                         error = EINVAL;
125                         break;
126                 }
127                 TAILQ_FOREACH(item, &usched_list, entry) {
128                         if (item == usched)
129                                 break;
130                 }
131                 if (item) {
132                         if (item->usched_unregister)
133                                 item->usched_unregister();
134                         TAILQ_REMOVE(&usched_list, item, entry);
135                 } else {
136                         error = EINVAL;
137                 }
138                 break;
139         default:
140                 error = EINVAL;
141                 break;
142         }
143         return (error);
144 }
145
146 /*
147  * USCHED_SET(syscall)
148  *
149  * SYNOPSIS:
150  *      Setting up a proc's usched.
151  *
152  * ARGUMENTS:
153  *      pid     -
154  *      cmd     -
155  *      data    - 
156  *      bytes   -
157  * RETURN VALUES:
158  *      0 - success
159  *      EINVAL - error
160  *
161  * MPALMOSTSAFE
162  */
163 int
164 sys_usched_set(struct usched_set_args *uap)
165 {
166         struct proc *p = curthread->td_proc;
167         struct usched *item;    /* temporaly for TAILQ processing */
168         int error;
169         char buffer[NAME_LENGTH];
170         cpumask_t mask;
171         struct lwp *lp;
172         int cpuid;
173
174         if (uap->pid != 0 && uap->pid != curthread->td_proc->p_pid)
175                 return (EINVAL);
176
177         lp = curthread->td_lwp;
178         get_mplock();
179
180         switch (uap->cmd) {
181         case USCHED_SET_SCHEDULER:
182                 if ((error = priv_check(curthread, PRIV_SCHED_SET)) != 0)
183                         break;
184                 error = copyinstr(uap->data, buffer, sizeof(buffer), NULL);
185                 if (error)
186                         break;
187                 TAILQ_FOREACH(item, &usched_list, entry) {
188                         if ((strcmp(item->name, buffer) == 0))
189                                 break;
190                 }
191
192                 /*
193                  * If the scheduler for a process is being changed, disassociate
194                  * the old scheduler before switching to the new one.  
195                  *
196                  * XXX we might have to add an additional ABI call to do a 'full
197                  * disassociation' and another ABI call to do a 'full
198                  * reassociation'
199                  */
200                 /* XXX lwp have to deal with multiple lwps here */
201                 if (p->p_nthreads != 1) {
202                         error = EINVAL;
203                         break;
204                 }
205                 if (item && item != p->p_usched) {
206                         /* XXX lwp */
207                         p->p_usched->release_curproc(ONLY_LWP_IN_PROC(p));
208                         p->p_usched->heuristic_exiting(ONLY_LWP_IN_PROC(p), p);
209                         p->p_usched = item;
210                 } else if (item == NULL) {
211                         error = EINVAL;
212                 }
213                 break;
214         case USCHED_SET_CPU:
215                 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
216                         break;
217                 if (uap->bytes != sizeof(int)) {
218                         error = EINVAL;
219                         break;
220                 }
221                 error = copyin(uap->data, &cpuid, sizeof(int));
222                 if (error)
223                         break;
224                 if (cpuid < 0 || cpuid >= ncpus) {
225                         error = EFBIG;
226                         break;
227                 }
228                 if ((smp_active_mask & CPUMASK(cpuid)) == 0) {
229                         error = EINVAL;
230                         break;
231                 }
232                 lp->lwp_cpumask = CPUMASK(cpuid);
233                 if (cpuid != mycpu->gd_cpuid)
234                         lwkt_migratecpu(cpuid);
235                 break;
236         case USCHED_GET_CPU:
237                 /* USCHED_GET_CPU doesn't require special privileges. */
238                 if (uap->bytes != sizeof(int)) {
239                         error = EINVAL;
240                         break;
241                 }
242                 error = copyout(&(mycpu->gd_cpuid), uap->data, sizeof(int));
243                 break;
244         case USCHED_ADD_CPU:
245                 if ((error = priv_check(curthread, PRIV_SCHED_CPUSET)) != 0)
246                         break;
247                 if (uap->bytes != sizeof(int)) {
248                         error = EINVAL;
249                         break;
250                 }
251                 error = copyin(uap->data, &cpuid, sizeof(int));
252                 if (error)
253                         break;
254                 if (cpuid < 0 || cpuid >= ncpus) {
255                         error = EFBIG;
256                         break;
257                 }
258                 if (!(smp_active_mask & CPUMASK(cpuid))) {
259                         error = EINVAL;
260                         break;
261                 }
262                 lp->lwp_cpumask |= CPUMASK(cpuid);
263                 break;
264         case USCHED_DEL_CPU:
265                 /* USCHED_DEL_CPU doesn't require special privileges. */
266                 if (uap->bytes != sizeof(int)) {
267                         error = EINVAL;
268                         break;
269                 }
270                 error = copyin(uap->data, &cpuid, sizeof(int));
271                 if (error)
272                         break;
273                 if (cpuid < 0 || cpuid >= ncpus) {
274                         error = EFBIG;
275                         break;
276                 }
277                 lp = curthread->td_lwp;
278                 mask = lp->lwp_cpumask & smp_active_mask & ~CPUMASK(cpuid);
279                 if (mask == 0)
280                         error = EPERM;
281                 else {
282                         lp->lwp_cpumask &= ~CPUMASK(cpuid);
283                         if ((lp->lwp_cpumask & mycpu->gd_cpumask) == 0) {
284                                 cpuid = BSFCPUMASK(lp->lwp_cpumask &
285                                                    smp_active_mask);
286                                 lwkt_migratecpu(cpuid);
287                         }
288                 }
289                 break;
290         default:
291                 error = EINVAL;
292                 break;
293         }
294         rel_mplock();
295         return (error);
296 }
297