"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.13.05/stress-kill.c" (11 Oct 2021, 4558 Bytes) of package /linux/privat/stress-ng-0.13.05.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-kill.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.13.04_vs_0.13.05.

    1 /*
    2  * Copyright (C) 2013-2021 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 stress_help_t help[] = {
   28     { NULL, "kill N",   "start N workers killing with SIGUSR1" },
   29     { NULL, "kill-ops N",   "stop after N kill bogo operations" },
   30     { NULL, NULL,       NULL }
   31 };
   32 
   33 /*
   34  *  stress_kill_handle_sigusr1()
   35  *  handle SIGUSR1
   36  */
   37 static void stress_kill_handle_sigusr1(int sig)
   38 {
   39     (void)sig;
   40 }
   41 
   42 /*
   43  *  stress on sched_kill()
   44  *  stress system by rapid kills
   45  */
   46 static int stress_kill(const stress_args_t *args)
   47 {
   48     uint64_t udelay = 5000;
   49     pid_t pid;
   50     const pid_t ppid = getpid();
   51     int ret;
   52 
   53     if (stress_sighandler(args->name, SIGUSR1, SIG_IGN, NULL) < 0)
   54         return EXIT_FAILURE;
   55 
   56     pid = fork();
   57     if (pid == 0) {
   58         ret = stress_sighandler(args->name, SIGUSR1, stress_kill_handle_sigusr1, NULL);
   59         (void)ret;
   60 
   61         while (keep_stressing(args)) {
   62             if (kill(ppid, 0) < 0)
   63                 break;
   64             pause();
   65         }
   66         _exit(0);
   67     }
   68 
   69     stress_set_proc_state(args->name, STRESS_STATE_RUN);
   70 
   71     do {
   72         /*
   73          *  With many kill stressors we get into a state
   74          *  where they all hammer on kill system calls and
   75          *  this stops the parent from getting scheduling
   76          *  time to spawn off the rest of the kill stressors
   77          *  causing some lag in getting all the stressors
   78          *  running. Ease this pressure off to being with
   79          *  with some small sleeps that shrink to zero over
   80          *  time. The alternative was to re-nice all the
   81          *  processes, but even then one gets the child
   82          *  stressors all contending and causing a bottle
   83          *  neck.  Any simpler and/or better solutions would
   84          *  be appreciated!
   85          */
   86         if (udelay >= 1000) {
   87             (void)shim_usleep(udelay);
   88             udelay -= 500;
   89         }
   90 
   91         ret = kill(args->pid, SIGUSR1);
   92         if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY))
   93             pr_fail("%s: kill PID %d with SIGUSR1 failed, errno=%d (%s)\n",
   94                 args->name, (int)args->pid, errno, strerror(errno));
   95 
   96         /* Zero signal can be used to see if process exists */
   97         ret = kill(args->pid, 0);
   98         if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY))
   99             pr_fail("%s: kill PID %d with signal 0 failed, errno=%d (%s)\n",
  100                 args->name, (int)args->pid, errno, strerror(errno));
  101 
  102         /*
  103          * Zero signal can be used to see if process exists,
  104          * -1 pid means signal sent to every process caller has
  105          * permission to send to
  106          */
  107         ret = kill(-1, 0);
  108         if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY))
  109             pr_fail("%s: kill PID -1 with signal 0 failed, errno=%d (%s)\n",
  110                 args->name, errno, strerror(errno));
  111 
  112         /*
  113          * Exercise the kernel by sending illegal signal numbers,
  114          * should return -EINVAL
  115          */
  116         ret = kill(args->pid, -1);
  117         (void)ret;
  118         ret = kill(args->pid, INT_MIN);
  119         (void)ret;
  120         ret = kill(0, INT_MIN);
  121         (void)ret;
  122 
  123         /*
  124          * Exercise the kernel by sending illegal pid INT_MIN,
  125          * should return -ESRCH but not sure if that is portable
  126          */
  127         ret = kill(INT_MIN, 0);
  128         (void)ret;
  129 
  130         /*
  131          * Send child process some signals to keep it busy
  132          */
  133         if (pid > 1) {
  134             ret = kill(pid, 0);
  135             (void)pid;
  136 #if defined(SIGSTOP) &&     \
  137     defined(SIGCONT)
  138             ret = kill(pid, SIGSTOP);
  139             (void)ret;
  140             ret = kill(pid, SIGCONT);
  141             (void)ret;
  142 #endif
  143             ret = kill(pid, SIGUSR1);
  144             (void)ret;
  145         }
  146 
  147         inc_counter(args);
  148     } while (keep_stressing(args));
  149 
  150     stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
  151 
  152     if (pid != -1) {
  153         int status;
  154 
  155         ret = kill(pid, SIGKILL);
  156         (void)ret;
  157         ret = waitpid(pid, &status, 0);
  158         (void)ret;
  159     }
  160 
  161     return EXIT_SUCCESS;
  162 }
  163 
  164 stressor_info_t stress_kill_info = {
  165     .stressor = stress_kill,
  166     .class = CLASS_INTERRUPT | CLASS_SCHEDULER | CLASS_OS,
  167     .help = help
  168 };