"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-sched.c" (15 Mar 2019, 6239 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "core-sched.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
   28     !defined(__OpenBSD__) && !defined(__minix__) && !defined(__APPLE__)
   29 /*
   30  *  get_sched_name()
   31  *  convert sched class to human readable string
   32  */
   33 const char *stress_get_sched_name(const int sched)
   34 {
   35     switch (sched) {
   36 #if defined(SCHED_IDLE)
   37     case SCHED_IDLE:
   38         return "idle";
   39 #endif
   40 #if defined(SCHED_FIFO)
   41     case SCHED_FIFO:
   42         return "fifo";
   43 #endif
   44 #if defined(SCHED_RR)
   45     case SCHED_RR:
   46         return "rr";
   47 #endif
   48 #if defined(SCHED_OTHER)
   49     case SCHED_OTHER:
   50         return "other";
   51 #endif
   52 #if defined(SCHED_BATCH)
   53     case SCHED_BATCH:
   54         return "batch";
   55 #endif
   56 #if defined(SCHED_DEADLINE)
   57     case SCHED_DEADLINE:
   58         return "deadline";
   59 #endif
   60     default:
   61         return "unknown";
   62     }
   63 }
   64 #endif
   65 
   66 #if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
   67      !defined(__OpenBSD__) && !defined(__minix__) && !defined(__APPLE__)
   68 /*
   69  *  set_sched()
   70  *  are sched settings valid, if so, set them
   71  */
   72 int stress_set_sched(
   73     const pid_t pid,
   74     const int sched,
   75     const int32_t sched_priority,
   76     const bool quiet)
   77 {
   78 #if defined(SCHED_FIFO) || defined(SCHED_RR)
   79     int min, max;
   80 #endif
   81 #if defined(SCHED_DEADLINE) && defined(__linux__)
   82     struct shim_sched_attr attr;
   83 #endif
   84     int rc;
   85     struct sched_param param;
   86     const char *name = stress_get_sched_name(sched);
   87 
   88     (void)memset(&param, 0, sizeof(param));
   89 
   90     switch (sched) {
   91     case UNDEFINED: /* No preference, don't set */
   92         return 0;
   93 #if defined(SCHED_FIFO) || defined(SCHED_RR)
   94 #if defined(SCHED_FIFO)
   95     case SCHED_FIFO:
   96 #endif
   97 #if defined(SCHED_RR)
   98     case SCHED_RR:
   99 #endif
  100         min = sched_get_priority_min(sched);
  101         max = sched_get_priority_max(sched);
  102         param.sched_priority = sched_priority;
  103 
  104         if (sched_priority == UNDEFINED) {
  105             if (g_opt_flags & OPT_FLAGS_AGGRESSIVE)
  106                 param.sched_priority = max;
  107             else
  108                 param.sched_priority = (max - min) / 2;
  109             if (!quiet)
  110                 pr_inf("priority not given, defaulting to %d\n",
  111                     param.sched_priority);
  112         }
  113         if ((param.sched_priority < min) ||
  114             (param.sched_priority > max)) {
  115             if (!quiet)
  116                 pr_inf("Scheduler priority level must be "
  117                     "set between %d and %d\n",
  118                     min, max);
  119             return -EINVAL;
  120         }
  121         if (!quiet)
  122             pr_dbg("sched: setting scheduler class '%s', priority %d\n",
  123                 name, param.sched_priority);
  124         break;
  125 #endif
  126 #if defined(SCHED_DEADLINE) && defined(__linux__)
  127     case SCHED_DEADLINE:
  128         min = sched_get_priority_min(sched);
  129         max = sched_get_priority_max(sched);
  130         attr.size = sizeof(attr);
  131         attr.sched_policy = SCHED_DEADLINE;
  132         attr.sched_flags = 0;
  133         attr.sched_nice = SCHED_OTHER;
  134         attr.sched_priority = sched_priority;
  135         if (sched_priority == UNDEFINED) {
  136             if (g_opt_flags & OPT_FLAGS_AGGRESSIVE)
  137                 attr.sched_priority = max;
  138             else
  139                 attr.sched_priority = (max - min) / 2;
  140             if (!quiet)
  141                 pr_inf("priority not given, defaulting to %d\n",
  142                     attr.sched_priority);
  143         }
  144         if ((attr.sched_priority < (uint32_t)min) ||
  145             (attr.sched_priority > (uint32_t)max)) {
  146             if (!quiet)
  147                 pr_inf("Scheduler priority level must be "
  148                 "set between %d and %d\n",
  149                 min, max);
  150             return -EINVAL;
  151         }
  152         if (!quiet)
  153             pr_dbg("sched: setting scheduler class '%s'\n",
  154                 name);
  155         attr.sched_runtime = 10000;
  156         attr.sched_deadline = 100000;
  157         attr.sched_period = 0;
  158 
  159         rc = shim_sched_setattr(pid, &attr, 0);
  160         if (rc < 0) {
  161             rc = -errno;
  162             if (!quiet)
  163                 pr_inf("Cannot set scheduler: errno=%d (%s)\n",
  164                     errno, strerror(errno));
  165             return rc;
  166         }
  167         return 0;
  168         break;      /* Keep static analysers happy */
  169 #endif
  170     default:
  171         param.sched_priority = 0;
  172         if (sched_priority != UNDEFINED)
  173             if (!quiet)
  174                 pr_inf("ignoring priority level for "
  175                     "scheduler class '%s'\n", name);
  176         if (!quiet)
  177             pr_dbg("sched: setting scheduler class '%s'\n", name);
  178         break;
  179     }
  180     rc = sched_setscheduler(pid, sched, &param);
  181     if (rc < 0) {
  182         rc = -errno;
  183         if (!quiet)
  184             pr_inf("Cannot set scheduler: errno=%d (%s)\n",
  185                 errno, strerror(errno));
  186         return rc;
  187     }
  188     return 0;
  189 }
  190 #else
  191 int stress_set_sched(
  192     const pid_t pid,
  193     const int sched,
  194     const int32_t sched_priority,
  195     const bool quiet)
  196 {
  197     (void)pid;
  198     (void)sched;
  199     (void)sched_priority;
  200     (void)quiet;
  201 
  202     return 0;
  203 }
  204 #endif
  205 
  206 /*
  207  *  get_opt_sched()
  208  *  get scheduler policy
  209  */
  210 int32_t get_opt_sched(const char *const str)
  211 {
  212 #if defined(SCHED_OTHER)
  213     if (!strcmp("other", str))
  214         return SCHED_OTHER;
  215 #endif
  216 #if defined(SCHED_BATCH)
  217     if (!strcmp("batch", str))
  218         return SCHED_BATCH;
  219 #endif
  220 #if defined(SCHED_IDLE)
  221     if (!strcmp("idle", str))
  222         return SCHED_IDLE;
  223 #endif
  224 #if defined(SCHED_FIFO)
  225     if (!strcmp("fifo", str))
  226         return SCHED_FIFO;
  227 #endif
  228 #if defined(SCHED_RR)
  229     if (!strcmp("rr", str))
  230         return SCHED_RR;
  231 #endif
  232 #if defined(SCHED_DEADLINE)
  233     if (!strcmp("deadline", str))
  234         return SCHED_DEADLINE;
  235 #endif
  236     if (strcmp("which", str))
  237         (void)fprintf(stderr, "Invalid sched option: %s\n", str);
  238     (void)fprintf(stderr, "Available scheduler options are:"
  239 #if defined(SCHED_OTHER)
  240         " other"
  241 #endif
  242 #if defined(SCHED_BATCH)
  243         " batch"
  244 #endif
  245 #if defined(SCHED_DEADLINE)
  246         " deadline"
  247 #endif
  248 #if defined(SCHED_IDLE)
  249         " idle"
  250 #endif
  251 #if defined(SCHED_FIFO)
  252         " fifo"
  253 #endif
  254 #if defined(SCHED_FIFO)
  255         " rr"
  256 #endif
  257         "\n");
  258     _exit(EXIT_FAILURE);
  259 }