"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-affinity.c" (15 Mar 2019, 3358 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-affinity.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 static const char *option = "taskset";
   28 
   29 #if defined(HAVE_AFFINITY)
   30 
   31 /*
   32  * check_cpu_affinity_range()
   33  * @max_cpus: maximum cpus allowed, 0..N-1
   34  * @cpu: cpu number to check
   35  */
   36 static void check_cpu_affinity_range(
   37     const int32_t max_cpus,
   38     const int32_t cpu)
   39 {
   40     if ((cpu < 0) || ((max_cpus != -1) && (cpu >= max_cpus))) {
   41         (void)fprintf(stderr, "%s: invalid range, %" PRId32 " is not allowed, "
   42             "allowed range: 0 to %" PRId32 "\n", option,
   43             cpu, max_cpus - 1);
   44         _exit(EXIT_FAILURE);
   45     }
   46 }
   47 
   48 /*
   49  * get_cpu()
   50  * @str: parse string containing decimal CPU number
   51  *
   52  * Returns: cpu number, or exits the program on invalid number in str
   53  */
   54 static int get_cpu(char *const str)
   55 {
   56     int val;
   57 
   58     if (sscanf(str, "%d", &val) != 1) {
   59         (void)fprintf(stderr, "%s: invalid number '%s'\n", option, str);
   60         _exit(EXIT_FAILURE);
   61     }
   62     return val;
   63 }
   64 
   65 /*
   66  * stress_set_cpu_affinity()
   67  * @arg: list of CPUs to set affinity to, comma separated
   68  *
   69  * Returns: 0 - OK
   70  */
   71 int stress_set_cpu_affinity(const char *arg)
   72 {
   73     cpu_set_t set;
   74     char *str, *ptr, *token;
   75     const int32_t max_cpus = stress_get_processors_configured();
   76 
   77     CPU_ZERO(&set);
   78 
   79     str = stress_const_optdup(arg);
   80     if (!str)
   81         return -1;
   82 
   83     for (ptr = str; (token = strtok(ptr, ",")) != NULL; ptr = NULL) {
   84         int i, lo, hi;
   85         char *tmpptr = strstr(token, "-");
   86 
   87         hi = lo = get_cpu(token);
   88         if (tmpptr) {
   89             tmpptr++;
   90             if (*tmpptr)
   91                 hi = get_cpu(tmpptr);
   92             else {
   93                 (void)fprintf(stderr, "%s: expecting number following "
   94                     "'-' in '%s'\n", option, token);
   95                 free(str);
   96                 _exit(EXIT_FAILURE);
   97             }
   98             if (hi <= lo) {
   99                 (void)fprintf(stderr, "%s: invalid range in '%s' "
  100                     "(end value must be larger than "
  101                     "start value\n", option, token);
  102                 free(str);
  103                 _exit(EXIT_FAILURE);
  104             }
  105         }
  106         check_cpu_affinity_range(max_cpus, lo);
  107         check_cpu_affinity_range(max_cpus, hi);
  108 
  109         for (i = lo; i <= hi; i++)
  110             CPU_SET(i, &set);
  111     }
  112     if (sched_setaffinity(getpid(), sizeof(set), &set) < 0) {
  113         pr_err("%s: cannot set CPU affinity, errno=%d (%s)\n",
  114             option, errno, strerror(errno));
  115         free(str);
  116         _exit(EXIT_FAILURE);
  117     }
  118 
  119     free(str);
  120     return 0;
  121 }
  122 
  123 #else
  124 int stress_set_cpu_affinity(const char *arg)
  125 {
  126     (void)arg;
  127 
  128     (void)fprintf(stderr, "%s: setting CPU affinity not supported\n", option);
  129     _exit(EXIT_FAILURE);
  130 }
  131 #endif