The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/ksched.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1996, 1997
    3  *      HD Associates, Inc.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by HD Associates, Inc
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /* ksched: Soft real time scheduling based on "rtprio".
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD$");
   38 
   39 #include "opt_posix.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/lock.h>
   44 #include <sys/mutex.h>
   45 #include <sys/proc.h>
   46 #include <sys/posix4.h>
   47 #include <sys/resource.h>
   48 #include <sys/sched.h>
   49 
   50 /* ksched: Real-time extension to support POSIX priority scheduling.
   51  */
   52 
   53 struct ksched {
   54         struct timespec rr_interval;
   55 };
   56 
   57 int
   58 ksched_attach(struct ksched **p)
   59 {
   60         struct ksched *ksched= p31b_malloc(sizeof(*ksched));
   61 
   62         ksched->rr_interval.tv_sec = 0;
   63         ksched->rr_interval.tv_nsec = 1000000000L / sched_rr_interval();
   64 
   65         *p = ksched;
   66         return 0;
   67 }
   68 
   69 int
   70 ksched_detach(struct ksched *ks)
   71 {
   72         p31b_free(ks);
   73 
   74         return 0;
   75 }
   76 
   77 /*
   78  * XXX About priorities
   79  *
   80  *      POSIX 1003.1b requires that numerically higher priorities be of
   81  *      higher priority.  It also permits sched_setparam to be
   82  *      implementation defined for SCHED_OTHER.  I don't like
   83  *      the notion of inverted priorites for normal processes when
   84  *      you can use "setpriority" for that.
   85  *
   86  */
   87 
   88 /* Macros to convert between the unix (lower numerically is higher priority)
   89  * and POSIX 1003.1b (higher numerically is higher priority)
   90  */
   91 
   92 #define p4prio_to_rtpprio(P) (RTP_PRIO_MAX - (P))
   93 #define rtpprio_to_p4prio(P) (RTP_PRIO_MAX - (P))
   94 
   95 #define p4prio_to_tsprio(P) ((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) - (P))
   96 #define tsprio_to_p4prio(P) ((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) - (P))
   97 
   98 /* These improve readability a bit for me:
   99  */
  100 #define P1B_PRIO_MIN rtpprio_to_p4prio(RTP_PRIO_MAX)
  101 #define P1B_PRIO_MAX rtpprio_to_p4prio(RTP_PRIO_MIN)
  102 
  103 static __inline int
  104 getscheduler(struct ksched *ksched, struct thread *td, int *policy)
  105 {
  106         struct rtprio rtp;
  107         int e = 0;
  108 
  109         pri_to_rtp(td, &rtp);
  110         switch (rtp.type)
  111         {
  112                 case RTP_PRIO_FIFO:
  113                 *policy = SCHED_FIFO;
  114                 break;
  115 
  116                 case RTP_PRIO_REALTIME:
  117                 *policy = SCHED_RR;
  118                 break;
  119 
  120                 default:
  121                 *policy = SCHED_OTHER;
  122                 break;
  123         }
  124 
  125         return e;
  126 }
  127 
  128 int
  129 ksched_setparam(struct ksched *ksched,
  130     struct thread *td, const struct sched_param *param)
  131 {
  132         int policy;
  133         int e;
  134 
  135         e = getscheduler(ksched, td, &policy);
  136 
  137         if (e == 0)
  138         {
  139                         e = ksched_setscheduler(ksched, td, policy, param);
  140         }
  141 
  142         return e;
  143 }
  144 
  145 int
  146 ksched_getparam(struct ksched *ksched,
  147     struct thread *td, struct sched_param *param)
  148 {
  149         struct rtprio rtp;
  150 
  151         pri_to_rtp(td, &rtp);
  152         if (RTP_PRIO_IS_REALTIME(rtp.type))
  153                 param->sched_priority = rtpprio_to_p4prio(rtp.prio);
  154         else {
  155                 if (PRI_MIN_TIMESHARE < rtp.prio) 
  156                         /*
  157                          * The interactive score has it to min realtime
  158                          * so we must show max (64 most likely
  159                          */ 
  160                         param->sched_priority = (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE);
  161                 else
  162                         param->sched_priority = tsprio_to_p4prio(rtp.prio);
  163         }
  164         return 0;
  165 }
  166 
  167 /*
  168  * XXX The priority and scheduler modifications should
  169  *     be moved into published interfaces in kern/kern_sync.
  170  *
  171  * The permissions to modify process p were checked in "p31b_proc()".
  172  *
  173  */
  174 int
  175 ksched_setscheduler(struct ksched *ksched,
  176     struct thread *td, int policy, const struct sched_param *param)
  177 {
  178         int e = 0;
  179         struct rtprio rtp;
  180 
  181         switch(policy)
  182         {
  183                 case SCHED_RR:
  184                 case SCHED_FIFO:
  185 
  186                 if (param->sched_priority >= P1B_PRIO_MIN &&
  187                     param->sched_priority <= P1B_PRIO_MAX)
  188                 {
  189                         rtp.prio = p4prio_to_rtpprio(param->sched_priority);
  190                         rtp.type = (policy == SCHED_FIFO)
  191                                 ? RTP_PRIO_FIFO : RTP_PRIO_REALTIME;
  192 
  193                         rtp_to_pri(&rtp, td);
  194                 }
  195                 else
  196                         e = EPERM;
  197 
  198 
  199                 break;
  200 
  201                 case SCHED_OTHER:
  202                 if (param->sched_priority >= 0 &&
  203                         param->sched_priority <= (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE)) {
  204                         rtp.type = RTP_PRIO_NORMAL;
  205                         rtp.prio = p4prio_to_tsprio(param->sched_priority);
  206                         rtp_to_pri(&rtp, td);
  207                 } else
  208                         e = EINVAL;
  209 
  210                 break;
  211                 
  212                 default:
  213                         e = EINVAL;
  214                         break;
  215         }
  216 
  217         return e;
  218 }
  219 
  220 int
  221 ksched_getscheduler(struct ksched *ksched, struct thread *td, int *policy)
  222 {
  223         return getscheduler(ksched, td, policy);
  224 }
  225 
  226 /* ksched_yield: Yield the CPU.
  227  */
  228 int
  229 ksched_yield(struct ksched *ksched)
  230 {
  231         sched_relinquish(curthread);
  232         return 0;
  233 }
  234 
  235 int
  236 ksched_get_priority_max(struct ksched *ksched, int policy, int *prio)
  237 {
  238         int e = 0;
  239 
  240         switch (policy)
  241         {
  242                 case SCHED_FIFO:
  243                 case SCHED_RR:
  244                 *prio = RTP_PRIO_MAX;
  245                 break;
  246 
  247                 case SCHED_OTHER:
  248                 *prio = PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
  249                 break;
  250 
  251                 default:
  252                 e = EINVAL;
  253         }
  254 
  255         return e;
  256 }
  257 
  258 int
  259 ksched_get_priority_min(struct ksched *ksched, int policy, int *prio)
  260 {
  261         int e = 0;
  262 
  263         switch (policy)
  264         {
  265                 case SCHED_FIFO:
  266                 case SCHED_RR:
  267                 *prio = P1B_PRIO_MIN;
  268                 break;
  269 
  270                 case SCHED_OTHER:
  271                 *prio = 0;
  272                 break;
  273 
  274                 default:
  275                 e = EINVAL;
  276         }
  277 
  278         return e;
  279 }
  280 
  281 int
  282 ksched_rr_get_interval(struct ksched *ksched,
  283    struct thread *td, struct timespec *timespec)
  284 {
  285         *timespec = ksched->rr_interval;
  286 
  287         return 0;
  288 }

Cache object: d6c843f21caf8766ba354c6b3e93929f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.