"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-nice.c" (15 Mar 2019, 2955 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 "stress-nice.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.09.55_vs_0.09.56.

    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(HAVE_NICE) || defined(HAVE_SETPRIORITY)
   28 
   29 static void stress_nice_delay(void)
   30 {
   31     double start = time_now();
   32     double delay = 0.01 + (double)mwc16() / 3276800.0;
   33 
   34     while (time_now() - start < delay)
   35         (void)shim_sched_yield();
   36 }
   37 
   38 /*
   39  *  stress on sched_nice()
   40  *  stress system by sched_nice
   41  */
   42 static int stress_nice(const args_t *args)
   43 {
   44 #if defined(HAVE_SETPRIORITY)
   45     int max_prio, min_prio;
   46 
   47 #if defined(RLIMIT_NICE)
   48     {
   49         struct rlimit rlim;
   50 
   51         if (getrlimit(RLIMIT_NICE, &rlim) < 0) {
   52             /* Make an assumption, bah */
   53             max_prio = 20;
   54             min_prio = -20;
   55         } else {
   56             max_prio = 20 - (int)rlim.rlim_cur;
   57             min_prio = -max_prio;
   58         }
   59     }
   60 #else
   61     /* Make an assumption, bah */
   62     max_prio = 20;
   63     min_prio = -20;
   64 #endif
   65 #endif
   66 
   67     do {
   68         pid_t pid;
   69         int which = mwc1();
   70 
   71         pid = fork();
   72         if (pid == 0) {
   73             int i;
   74 
   75             /* Child */
   76             (void)setpgid(0, g_pgrp);
   77             stress_parent_died_alarm();
   78 
   79             switch (which) {
   80 #if defined(HAVE_SETPRIORITY)
   81             case 1:
   82                 pid = getpid();
   83                 for (i = min_prio; (i <= max_prio) && keep_stressing(); i++) {
   84                     errno = 0;
   85                     (void)setpriority(PRIO_PROCESS, pid, i);
   86                     if (!errno)
   87                         stress_nice_delay();
   88                     inc_counter(args);
   89                 }
   90                 break;
   91 #endif
   92             default:
   93                 for (i = -19; i < 20; i++) {
   94                     int ret;
   95 
   96                     ret = nice(1);
   97                     if (ret == 0)
   98                         stress_nice_delay();
   99                     inc_counter(args);
  100                 }
  101                 break;
  102             }
  103             _exit(0);
  104         }
  105         if (pid > 0) {
  106             int status;
  107 
  108             (void)setpgid(pid, g_pgrp);
  109 
  110             /* Parent, wait for child */
  111             if (waitpid(pid, &status, 0) < 0) {
  112                 (void)kill(pid, SIGTERM);
  113                 (void)kill(pid, SIGKILL);
  114             }
  115         }
  116     } while (keep_stressing());
  117 
  118     return EXIT_SUCCESS;
  119 }
  120 
  121 stressor_info_t stress_nice_info = {
  122     .stressor = stress_nice,
  123     .class = CLASS_SCHEDULER | CLASS_OS
  124 };
  125 
  126 #else
  127 stressor_info_t stress_nice_info = {
  128     .stressor = stress_not_implemented,
  129     .class = CLASS_SCHEDULER | CLASS_OS
  130 };
  131 #endif