"Fossies" - the Fresh Open Source Software Archive

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

    1 /*
    2  * Copyright (C) 2016-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(__linux__)
   28 
   29 /*
   30  *  stress_cpu_online_set()
   31  *  set a specified CPUs online or offline
   32  */
   33 static int stress_cpu_online_set(
   34     const args_t *args,
   35     const int32_t cpu,
   36     const int setting)
   37 {
   38     char filename[PATH_MAX];
   39     char data[3] = { '0' + setting, '\n', 0 };
   40     ssize_t ret;
   41 
   42     (void)snprintf(filename, sizeof(filename),
   43         "/sys/devices/system/cpu/cpu%" PRId32 "/online", cpu);
   44 
   45     ret = system_write(filename, data, sizeof data);
   46     if ((ret < 0) &&
   47         ((ret != -EAGAIN) && (ret != -EINTR) &&
   48              (ret != -EBUSY) && (ret != -EOPNOTSUPP))) {
   49         pr_fail_err("write");
   50         return EXIT_FAILURE;
   51     }
   52     return EXIT_SUCCESS;
   53 }
   54 
   55 /*
   56  *  stress_cpu_online_supported()
   57  *      check if we can run this as root
   58  */
   59 static int stress_cpu_online_supported(void)
   60 {
   61     int ret;
   62 
   63         if (geteuid() != 0) {
   64                 pr_inf("cpu-online stressor will be skipped, "
   65                         "need to be running as root for this stressor\n");
   66                 return -1;
   67         }
   68 
   69     ret = system_write("/sys/devices/system/cpu/cpu1/online", "1\n", 2);
   70     if (ret < 0) {
   71                 pr_inf("cpu-online stressor will be skipped, "
   72                         "cannot write to cpu1 online sysfs control file\n");
   73                 return -1;
   74     }
   75 
   76         return 0;
   77 }
   78 
   79 /*
   80  *  stress_cpu_online
   81  *  stress twiddling CPUs online/offline
   82  */
   83 static int stress_cpu_online(const args_t *args)
   84 {
   85     const int32_t cpus = stress_get_processors_configured();
   86     int32_t i, cpu_online_count = 0;
   87     bool *cpu_online;
   88     int rc = EXIT_SUCCESS;
   89 
   90     if (geteuid() != 0) {
   91         if (args->instance == 0)
   92             pr_inf("%s: need root privilege to run "
   93                 "this stressor\n", args->name);
   94         /* Not strictly a test failure */
   95         return EXIT_SUCCESS;
   96     }
   97 
   98     if ((cpus < 1) || (cpus > 65536)) {
   99         pr_inf("%s: too few or too many CPUs (found %" PRId32 ")\n",
  100             args->name, cpus);
  101         return EXIT_FAILURE;
  102     }
  103 
  104     cpu_online = calloc(cpus, sizeof(*cpu_online));
  105     if (!cpu_online) {
  106         pr_err("%s: out of memory\n", args->name);
  107         return EXIT_NO_RESOURCE;
  108     }
  109 
  110     /*
  111      *  Determine how many CPUs we can online/offline via
  112      *  the online sys interface
  113      */
  114     for (i = 0; i < cpus; i++) {
  115         char filename[PATH_MAX];
  116         int ret;
  117 
  118         (void)snprintf(filename, sizeof(filename),
  119             "/sys/devices/system/cpu/cpu%" PRId32 "/online", i);
  120         ret = access(filename, O_RDWR);
  121         if (ret == 0) {
  122             cpu_online[i] = true;
  123             cpu_online_count++;
  124         }
  125     }
  126     if (cpu_online_count == 0) {
  127         pr_inf("%s: no CPUs can be set online/offline\n", args->name);
  128         free(cpu_online);
  129         return EXIT_FAILURE;
  130     }
  131     if ((args->num_instances > 1) &&
  132         (g_opt_flags & OPT_FLAGS_CPU_ONLINE_ALL)) {
  133         if (args->instance == 0) {
  134             pr_inf("%s: disabling --cpu-online-all option because "
  135                    "more than 1 %s stressor is being invoked\n",
  136                 args->name, args->name);
  137         }
  138         g_opt_flags &= ~OPT_FLAGS_CPU_ONLINE_ALL;
  139     }
  140 
  141     if ((args->instance == 0) &&
  142         (g_opt_flags & OPT_FLAGS_CPU_ONLINE_ALL)) {
  143         pr_inf("%s: exercising all %" PRId32 " cpus\n",
  144             args->name, cpu_online_count + 1);
  145     }
  146 
  147 
  148     /*
  149      *  Now randomly offline/online them all
  150      */
  151     do {
  152         unsigned long cpu = mwc32() % cpus;
  153 
  154         /*
  155          * Only allow CPU 0 to be offlined if OPT_FLAGS_CPU_ONLINE_ALL
  156          * --cpu-online-all has been enabled
  157          */
  158         if ((cpu == 0) && !(g_opt_flags & OPT_FLAGS_CPU_ONLINE_ALL))
  159             continue;
  160         if (cpu_online[cpu]) {
  161             rc = stress_cpu_online_set(args, cpu, 0);
  162             if (rc != EXIT_SUCCESS)
  163                 break;
  164             rc = stress_cpu_online_set(args, cpu, 1);
  165             if (rc != EXIT_SUCCESS)
  166                 break;
  167             inc_counter(args);
  168         }
  169     } while (keep_stressing());
  170 
  171     /*
  172      *  Force CPUs all back online
  173      */
  174     for (i = 0; i < cpus; i++) {
  175         if (cpu_online[i])
  176             (void)stress_cpu_online_set(args, i, 1);
  177     }
  178     free(cpu_online);
  179 
  180     return rc;
  181 }
  182 
  183 stressor_info_t stress_cpu_online_info = {
  184     .stressor = stress_cpu_online,
  185     .supported = stress_cpu_online_supported,
  186     .class = CLASS_CPU | CLASS_OS | CLASS_PATHOLOGICAL
  187 };
  188 #else
  189 stressor_info_t stress_cpu_online_info = {
  190     .stressor = stress_not_implemented,
  191     .class = CLASS_CPU | CLASS_OS | CLASS_PATHOLOGICAL
  192 };
  193 #endif