"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-futex.c" (15 Mar 2019, 3375 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-futex.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_LINUX_FUTEX_H) &&  \
   28     defined(__NR_futex)
   29 
   30 #define THRESHOLD   (100000)
   31 
   32 /*
   33  *  stress_futex()
   34  *  stress system by futex calls. The intention is not to
   35  *  efficiently use futex, but to stress the futex system call
   36  *  by rapidly calling it on wait and wakes
   37  */
   38 static int stress_futex(const args_t *args)
   39 {
   40     uint64_t *timeout = &g_shared->futex.timeout[args->instance];
   41     uint32_t *futex = &g_shared->futex.futex[args->instance];
   42     pid_t pid;
   43 
   44 again:
   45     pid = fork();
   46     if (pid < 0) {
   47         if (g_keep_stressing_flag &&
   48             ((errno == EAGAIN) || (errno == ENOMEM)))
   49             goto again;
   50         pr_err("%s: fork failed: errno=%d: (%s)\n",
   51             args->name, errno, strerror(errno));
   52     }
   53     if (pid > 0) {
   54         int status;
   55 
   56         (void)setpgid(pid, g_pgrp);
   57 
   58         do {
   59             int ret;
   60 
   61             /*
   62              * Break early in case wake gets stuck
   63              * (which it shouldn't)
   64              */
   65             if (!g_keep_stressing_flag)
   66                 break;
   67             ret = shim_futex_wake(futex, 1);
   68             if (g_opt_flags & OPT_FLAGS_VERIFY) {
   69                 if (ret < 0)
   70                     pr_fail_err("futex wake");
   71             }
   72         } while (keep_stressing());
   73 
   74         /* Kill waiter process */
   75         (void)kill(pid, SIGKILL);
   76         (void)waitpid(pid, &status, 0);
   77 
   78         pr_dbg("%s: futex timeouts: %" PRIu64 "\n",
   79             args->name, *timeout);
   80     } else {
   81         uint64_t threshold = THRESHOLD;
   82 
   83         (void)setpgid(0, g_pgrp);
   84         stress_parent_died_alarm();
   85 
   86         do {
   87             /* Small timeout to force rapid timer wakeups */
   88             const struct timespec t = { .tv_sec = 0, .tv_nsec = 5000 };
   89             int ret;
   90 
   91             /* Break early before potential long wait */
   92             if (!g_keep_stressing_flag)
   93                 break;
   94 
   95             ret = shim_futex_wait(futex, 0, &t);
   96 
   97             /* timeout, re-do, stress on stupid fast polling */
   98             if ((ret < 0) && (errno == ETIMEDOUT)) {
   99                 (*timeout)++;
  100                 if (*timeout > threshold) {
  101                     /*
  102                      * Backoff for a short while
  103                      * and start again
  104                      */
  105                     (void)shim_usleep(250000);
  106                     threshold += THRESHOLD;
  107                 }
  108             } else {
  109                 if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
  110                     pr_fail_err("futex wait");
  111                 }
  112                 inc_counter(args);
  113             }
  114         } while (keep_stressing());
  115     }
  116 
  117     return EXIT_SUCCESS;
  118 }
  119 
  120 stressor_info_t stress_futex_info = {
  121     .stressor = stress_futex,
  122     .class = CLASS_SCHEDULER | CLASS_OS
  123 };
  124 #else
  125 stressor_info_t stress_futex_info = {
  126     .stressor = stress_not_implemented,
  127     .class = CLASS_SCHEDULER | CLASS_OS
  128 };
  129 #endif