"Fossies" - the Fresh Open Source Software Archive

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

    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(__minix__)
   28 
   29 /*
   30  *  stress on sched_yield()
   31  *  stress system by sched_yield
   32  */
   33 static int stress_yield(const args_t *args)
   34 {
   35     uint64_t *counters;
   36     uint64_t max_ops_per_yielder;
   37     size_t counters_sz;
   38     int32_t cpus = stress_get_processors_configured();
   39     const size_t instances = args->num_instances;
   40     size_t yielders = 2;
   41 #if defined(HAVE_SCHED_GETAFFINITY)
   42     cpu_set_t mask;
   43 #endif
   44     pid_t *pids;
   45     size_t i;
   46 
   47 #if defined(HAVE_SCHED_GETAFFINITY)
   48     /*
   49      *  If the process is limited to a subset of cores
   50      *  then make sure we do not create too many yielders
   51      */
   52     if (sched_getaffinity(0, sizeof(mask), &mask) < 0) {
   53         pr_inf("%s: can't get sched affinity, defaulting to %"
   54             PRId32 " yielder%s (instance %" PRIu32 ")\n",
   55             args->name, cpus, (cpus == 1) ? "" : "s", args->instance);
   56     } else {
   57         if (CPU_COUNT(&mask) < cpus)
   58             cpus = CPU_COUNT(&mask);
   59         pr_inf("%s: limiting to %" PRId32 " yielder%s (instance %"
   60             PRIu32 ")\n", args->name, cpus, (cpus == 1) ? "" : "s", args->instance);
   61     }
   62 #endif
   63 
   64     /*
   65      *  Ensure we always have at least 2 yielders per
   66      *  CPU available to force context switching on yields
   67      */
   68     if (cpus > 0) {
   69         cpus *= 2;
   70         yielders = cpus / instances;
   71         if (yielders < 1)
   72             yielders = 1;
   73         if (!args->instance) {
   74             int32_t residual = cpus - (yielders * instances);
   75             if (residual > 0)
   76                 yielders += residual;
   77         }
   78     }
   79 
   80     max_ops_per_yielder = args->max_ops / yielders;
   81     pids = calloc(yielders, sizeof(*pids));
   82     if (!pids) {
   83         pr_err("%s: calloc failed\n", args->name);
   84         return EXIT_NO_RESOURCE;
   85     }
   86 
   87     counters_sz = yielders * sizeof(*counters);
   88     counters = (uint64_t *)mmap(NULL, counters_sz, PROT_READ | PROT_WRITE,
   89         MAP_SHARED | MAP_ANONYMOUS, -1, 0);
   90     if (counters == MAP_FAILED) {
   91         int rc = exit_status(errno);
   92 
   93         pr_err("%s: mmap failed: errno=%d (%s)\n",
   94             args->name, errno, strerror(errno));
   95         free(pids);
   96         return rc;
   97     }
   98     (void)memset(counters, 0, counters_sz);
   99 
  100     for (i = 0; g_keep_stressing_flag && (i < yielders); i++) {
  101         pids[i] = fork();
  102         if (pids[i] < 0) {
  103             pr_dbg("%s: fork failed (instance %" PRIu32
  104                 ", yielder %zd): errno=%d (%s)\n",
  105                 args->name, args->instance, i, errno, strerror(errno));
  106         } else if (pids[i] == 0) {
  107             (void)setpgid(0, g_pgrp);
  108             stress_parent_died_alarm();
  109 
  110             do {
  111                 int ret;
  112 
  113                 ret = shim_sched_yield();
  114                 if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY))
  115                     pr_fail_err("sched_yield");
  116                 counters[i]++;
  117             } while (g_keep_stressing_flag && (!max_ops_per_yielder || counters[i] < max_ops_per_yielder));
  118             _exit(EXIT_SUCCESS);
  119         }
  120     }
  121 
  122     do {
  123         set_counter(args, 0);
  124         (void)shim_usleep(100000);
  125         for (i = 0; i < yielders; i++)
  126             add_counter(args, counters[i]);
  127     } while (keep_stressing());
  128 
  129     /* Parent, wait for children */
  130     set_counter(args, 0);
  131     for (i = 0; i < yielders; i++) {
  132         if (pids[i] > 0) {
  133             int status;
  134 
  135             (void)kill(pids[i], SIGKILL);
  136             (void)waitpid(pids[i], &status, 0);
  137             add_counter(args, counters[i]);
  138         }
  139     }
  140     (void)munmap((void *)counters, counters_sz);
  141     free(pids);
  142 
  143     return EXIT_SUCCESS;
  144 }
  145 
  146 stressor_info_t stress_yield_info = {
  147     .stressor = stress_yield,
  148     .class = CLASS_SCHEDULER | CLASS_OS
  149 };
  150 #else
  151 stressor_info_t stress_yield_info = {
  152     .stressor = stress_not_implemented,
  153     .class = CLASS_SCHEDULER | CLASS_OS
  154 };
  155 #endif