"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-affinity.c" (15 Mar 2019, 3124 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-affinity.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(HAVE_AFFINITY) && \
   28     defined(HAVE_SCHED_GETAFFINITY)
   29 
   30 /*
   31  *  stress_affinity_supported()
   32  *      check that we can set affinity
   33  */
   34 static int stress_affinity_supported(void)
   35 {
   36     cpu_set_t mask;
   37 
   38     CPU_ZERO(&mask);
   39 
   40     if (sched_getaffinity(0, sizeof(mask), &mask) < 0) {
   41         pr_inf("affinity stressor cannot get CPU affinity, skipping the stressor\n");
   42         return -1;
   43     }
   44     if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
   45         if (errno == EPERM) {
   46             pr_inf("affinity stressor cannot set CPU affinity, "
   47                    "process lacks privilege, skipping the stressor\n");
   48             return -1;
   49         }
   50     }
   51         return 0;
   52 }
   53 
   54 /*
   55  *  stress on sched_affinity()
   56  *  stress system by changing CPU affinity periodically
   57  */
   58 static int stress_affinity(const args_t *args)
   59 {
   60     uint32_t cpu = args->instance;
   61     const uint32_t cpus = (uint32_t)stress_get_processors_configured();
   62     cpu_set_t mask;
   63 
   64     do {
   65         cpu = (g_opt_flags & OPT_FLAGS_AFFINITY_RAND) ?
   66             (mwc32() >> 4) : cpu + 1;
   67         cpu %= cpus;
   68         CPU_ZERO(&mask);
   69         CPU_SET(cpu, &mask);
   70         if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
   71             if (errno == EINVAL) {
   72                 /*
   73                  * We get this if CPU is offline'd,
   74                  * and since that can be dynamically
   75                  * set, we should just retry
   76                  */
   77                 continue;
   78             }
   79             pr_fail("%s: failed to move to CPU %" PRIu32
   80                 ", errno=%d (%s)\n",
   81                 args->name, cpu, errno, strerror(errno));
   82             (void)shim_sched_yield();
   83         } else {
   84             /* Now get and check */
   85             CPU_ZERO(&mask);
   86             CPU_SET(cpu, &mask);
   87             if (sched_getaffinity(0, sizeof(mask), &mask) == 0) {
   88                 if ((g_opt_flags & OPT_FLAGS_VERIFY) &&
   89                     (!CPU_ISSET(cpu, &mask)))
   90                     pr_fail("%s: failed to move "
   91                         "to CPU %" PRIu32 "\n",
   92                         args->name, cpu);
   93             }
   94         }
   95         inc_counter(args);
   96     } while (keep_stressing());
   97 
   98     return EXIT_SUCCESS;
   99 }
  100 
  101 stressor_info_t stress_affinity_info = {
  102     .stressor = stress_affinity,
  103     .class = CLASS_SCHEDULER,
  104     .supported = stress_affinity_supported
  105 };
  106 #else
  107 stressor_info_t stress_affinity_info = {
  108     .stressor = stress_not_implemented,
  109     .class = CLASS_SCHEDULER
  110 };
  111 #endif