"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-sigsuspend.c" (15 Mar 2019, 3001 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-sigsuspend.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 #define CACHE_STRIDE_SHIFT  (6)
   28 
   29 /*
   30  *  stress_usr1_handler()
   31  *      SIGUSR1 handler
   32  */
   33 static void MLOCKED_TEXT stress_usr1_handler(int signum)
   34 {
   35         (void)signum;
   36 }
   37 
   38 /*
   39  *  stress_sigsuspend
   40  *  stress sigsuspend
   41  */
   42 static int stress_sigsuspend(const args_t *args)
   43 {
   44     pid_t pid[MAX_SIGSUSPEND_PIDS];
   45     size_t n, i;
   46     sigset_t mask, oldmask;
   47     int status;
   48     uint64_t *counters;
   49     volatile uint64_t *v_counters;
   50     const size_t counters_size =
   51         (sizeof(*counters) * MAX_SIGSUSPEND_PIDS) << CACHE_STRIDE_SHIFT;
   52 
   53     if (stress_sighandler(args->name, SIGUSR1, stress_usr1_handler, NULL) < 0)
   54         return EXIT_FAILURE;
   55 
   56     v_counters = counters = (uint64_t *)mmap(NULL, counters_size,
   57             PROT_READ | PROT_WRITE,
   58             MAP_SHARED | MAP_ANONYMOUS, -1, 0);
   59     if (counters == MAP_FAILED) {
   60         pr_fail_dbg("mmap");
   61         return EXIT_FAILURE;
   62     }
   63     (void)memset(counters, 0, counters_size);
   64 
   65     (void)sigemptyset(&mask);
   66     (void)sigprocmask(SIG_BLOCK, &mask, &oldmask);
   67 
   68     for (n = 0; n < MAX_SIGSUSPEND_PIDS; n++) {
   69 again:
   70         pid[n] = fork();
   71         if (pid[n] < 0) {
   72             if (g_keep_stressing_flag && (errno == EAGAIN))
   73                 goto again;
   74             pr_fail_dbg("fork");
   75             goto reap;
   76         } else if (pid[n] == 0) {
   77             (void)setpgid(0, g_pgrp);
   78             stress_parent_died_alarm();
   79 
   80             while (g_keep_stressing_flag) {
   81                 (void)sigsuspend(&mask);
   82                 v_counters[n << CACHE_STRIDE_SHIFT]++;
   83             }
   84             _exit(0);
   85         }
   86         (void)setpgid(pid[n], g_pgrp);
   87     }
   88 
   89     /* Parent */
   90     do {
   91         set_counter(args, 0);
   92         for (i = 0; (i < n) && keep_stressing(); i++) {
   93             add_counter(args, v_counters[i << CACHE_STRIDE_SHIFT]);
   94             (void)kill(pid[i], SIGUSR1);
   95         }
   96     } while (keep_stressing());
   97 
   98 
   99 reap:
  100     for (i = 0; i < n; i++) {
  101         /* terminate child */
  102         (void)kill(pid[i], SIGKILL);
  103         (void)waitpid(pid[i], &status, 0);
  104     }
  105     (void)munmap((void *)counters, counters_size);
  106 
  107     return EXIT_SUCCESS;
  108 }
  109 
  110 stressor_info_t stress_sigsuspend_info = {
  111     .stressor = stress_sigsuspend,
  112     .class = CLASS_INTERRUPT | CLASS_OS
  113 };