"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-softlockup.c" (15 Mar 2019, 6340 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-softlockup.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.52_vs_0.09.54.

    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_SCHED_GET_PRIORITY_MIN) && \
   28     defined(HAVE_SCHED_SETSCHEDULER)
   29 
   30 /*
   31  *  stress_softlockup_supported()
   32  *      check if we can run this as root
   33  */
   34 static int stress_softlockup_supported(void)
   35 {
   36         if (geteuid() != 0) {
   37         pr_inf("softlockup stressor needs to be run as root to "
   38             "set SCHED_RR or SCHED_FIFO priorities, "
   39             "skipping this stressor\n");
   40                 return -1;
   41         }
   42         return 0;
   43 }
   44 
   45 typedef struct {
   46     const int   policy;
   47     int     max_prio;
   48     const char  *name;
   49 } policy_t;
   50 
   51 static policy_t policies[] = {
   52 #if defined(SCHED_FIFO)
   53     { SCHED_FIFO, 0, "SCHED_FIFO"},
   54 #endif
   55 #if defined(SCHED_RR)
   56     { SCHED_RR, 0, "SCHED_RR" },
   57 #endif
   58 };
   59 
   60 static sigjmp_buf jmp_env;
   61 
   62 /*
   63  *  stress_rlimit_handler()
   64  *      rlimit generic handler
   65  */
   66 static void MLOCKED_TEXT stress_rlimit_handler(int signum)
   67 {
   68     (void)signum;
   69 
   70     g_keep_stressing_flag = 1;
   71     siglongjmp(jmp_env, 1);
   72 }
   73 
   74 static int stress_softlockup(const args_t *args)
   75 {
   76     size_t policy = 0;
   77     int max_prio = 0;
   78     bool good_policy = false;
   79     const uint32_t cpus_online = (uint32_t)stress_get_processors_online();
   80     const uint32_t num_instances = args->num_instances;
   81     struct sigaction old_action_xcpu;
   82     struct sched_param param;
   83     struct rlimit rlim;
   84     pid_t pid;
   85     NOCLOBBER uint64_t timeout;
   86     const double start = time_now();
   87 
   88     timeout = g_opt_timeout;
   89     (void)memset(&param, 0, sizeof(param));
   90 
   91     if (!args->instance) {
   92         if (SIZEOF_ARRAY(policies) == 0) {
   93             pr_inf("%s: no scheduling policies "
   94                 "available, skipping test\n",
   95                 args->name);
   96             return EXIT_NOT_IMPLEMENTED;
   97         }
   98     }
   99 
  100     /* Get the max priorities for each sched policy */
  101     for (policy = 0; policy < SIZEOF_ARRAY(policies); policy++) {
  102         const int prio = sched_get_priority_max(policies[policy].policy);
  103 
  104         policies[policy].max_prio = prio;
  105         good_policy |= (prio >= 0);
  106 
  107         if (max_prio < prio)
  108             max_prio = prio;
  109     }
  110 
  111     /*
  112      *  We may have a kernel that does not support these sched
  113      *  policies, so check for this
  114      */
  115     if (!good_policy) {
  116         pr_inf("%s: cannot get valid maximum priorities for the "
  117             "scheduling policies, skipping test\n",
  118                 args->name);
  119         return EXIT_NOT_IMPLEMENTED;
  120     }
  121 
  122     if (!args->instance) {
  123         if (max_prio < 1)
  124             pr_inf("%s: running with a low maximum priority of %d\n",
  125                 args->name, max_prio);
  126     }
  127 
  128     if (num_instances < cpus_online) {
  129         pr_inf("%s: for best reslults, run with at least %d instances "
  130             "of this stressor\n", args->name, cpus_online);
  131     }
  132 
  133     if (g_opt_timeout == TIMEOUT_NOT_SET) {
  134         timeout = 60;
  135         pr_inf("%s: timeout has not been set, forcing timeout to "
  136             "be %" PRIu64 " seconds\n", args->name, timeout);
  137     }
  138 
  139     pid = fork();
  140     if (pid < 0) {
  141         pr_inf("%s: cannot fork, errno=%d (%s)\n",
  142             args->name, errno, strerror(errno));
  143         return EXIT_NO_RESOURCE;
  144     } else if (pid == 0) {
  145         const pid_t mypid = getpid();
  146 #if defined(HAVE_ATOMIC)
  147         uint32_t count;
  148 #endif
  149         int ret;
  150         int rc = EXIT_FAILURE;
  151 
  152 #if defined(HAVE_ATOMIC)
  153         __sync_fetch_and_add(&g_shared->softlockup_count, 1);
  154 
  155         /*
  156          * Wait until all instances have reached this point
  157          */
  158         do {
  159             if ((time_now() - start) > (double)timeout)
  160                 goto tidy_ok;
  161             (void)usleep(50000);
  162             __atomic_load(&g_shared->softlockup_count, &count, __ATOMIC_RELAXED);
  163         } while (keep_stressing() && count < num_instances);
  164 #endif
  165 
  166         /*
  167          * We run the stressor as a child so that
  168          * if we the hard time timits the child is
  169          * terminated with a SIGKILL and we can
  170          * catch that with the parent
  171          */
  172         rlim.rlim_cur = timeout;
  173         rlim.rlim_max = timeout;
  174         (void)setrlimit(RLIMIT_CPU, &rlim);
  175 
  176 #if defined(RLIMIT_RTTIME)
  177         rlim.rlim_cur = 1000000 * timeout;
  178         rlim.rlim_max = 1000000 * timeout;
  179         (void)setrlimit(RLIMIT_RTTIME, &rlim);
  180 #endif
  181 
  182         if (stress_sighandler(args->name, SIGXCPU, stress_rlimit_handler, &old_action_xcpu) < 0)
  183             goto tidy;
  184 
  185         ret = sigsetjmp(jmp_env, 1);
  186         if (ret)
  187             goto tidy_ok;
  188 
  189         policy = 0;
  190         do {
  191             /*
  192              *  Note: Re-setting the scheduler policy on Linux
  193              *  puts the runnable process always onto the front
  194              *  of the scheduling list.
  195              */
  196             param.sched_priority = policies[policy].max_prio;
  197             ret = sched_setscheduler(mypid, policies[policy].policy, &param);
  198             if (ret < 0) {
  199                 if (errno != EPERM) {
  200                     pr_fail("%s: sched_setscheduler "
  201                         "failed: errno=%d (%s) "
  202                         "for scheduler policy %s\n",
  203                         args->name, errno, strerror(errno),
  204                         policies[policy].name);
  205                 }
  206             }
  207             policy++;
  208             policy %= SIZEOF_ARRAY(policies);
  209             inc_counter(args);
  210 
  211             /* Ensure we NEVER spin forever */
  212             if ((time_now() - start) > (double)timeout)
  213                 break;
  214         } while (keep_stressing());
  215 
  216 tidy_ok:
  217         rc = EXIT_SUCCESS;
  218 tidy:
  219         (void)fflush(stdout);
  220         _exit(rc);
  221     } else {
  222         int status;
  223 
  224         param.sched_priority = policies[0].max_prio;
  225         (void)sched_setscheduler(args->pid, policies[0].policy, &param);
  226 
  227         (void)pause();
  228         (void)kill(pid, SIGKILL);
  229 #if defined(HAVE_ATOMIC)
  230         __sync_fetch_and_sub(&g_shared->softlockup_count, 1);
  231 #endif
  232 
  233         (void)waitpid(pid, &status, 0);
  234     }
  235 
  236     return EXIT_SUCCESS;
  237 }
  238 
  239 stressor_info_t stress_softlockup_info = {
  240     .stressor = stress_softlockup,
  241     .supported = stress_softlockup_supported,
  242     .class = CLASS_SCHEDULER
  243 };
  244 #else
  245 stressor_info_t stress_softlockup_info = {
  246     .stressor = stress_not_implemented,
  247     .class = CLASS_SCHEDULER
  248 };
  249 #endif