"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-mmapfork.c" (15 Mar 2019, 4736 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-mmapfork.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_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
   28 
   29 #define MAX_PIDS        (32)
   30 
   31 #define _EXIT_FAILURE           (0x01)
   32 #define _EXIT_SEGV_MMAP         (0x02)
   33 #define _EXIT_SEGV_MADV_WILLNEED    (0x04)
   34 #define _EXIT_SEGV_MADV_DONTNEED    (0x08)
   35 #define _EXIT_SEGV_MEMSET       (0x10)
   36 #define _EXIT_SEGV_MUNMAP       (0x20)
   37 #define _EXIT_MASK  (_EXIT_SEGV_MMAP | \
   38              _EXIT_SEGV_MADV_WILLNEED | \
   39              _EXIT_SEGV_MADV_DONTNEED | \
   40              _EXIT_SEGV_MEMSET | \
   41              _EXIT_SEGV_MUNMAP)
   42 
   43 static volatile int segv_ret;
   44 
   45 /*
   46  *  stress_segvhandler()
   47  *      SEGV handler
   48  */
   49 static void MLOCKED_TEXT stress_segvhandler(int signum)
   50 {
   51     (void)signum;
   52 
   53     _exit(segv_ret);
   54 }
   55 
   56 static void __strlcat(char *dst, char *src, size_t *n)
   57 {
   58     const size_t ln = strlen(src);
   59 
   60     if (*n <= ln)
   61         return;
   62 
   63     (void)shim_strlcat(dst, src, *n);
   64     *n -= ln;
   65 }
   66 
   67 /*
   68  *  stress_mmapfork()
   69  *  stress mappings + fork VM subystem
   70  */
   71 static int stress_mmapfork(const args_t *args)
   72 {
   73     pid_t pids[MAX_PIDS];
   74     struct sysinfo info;
   75     void *ptr;
   76     uint64_t segv_count = 0;
   77     int8_t segv_reasons = 0;
   78 
   79     do {
   80         size_t i, n, len;
   81 
   82         (void)memset(pids, 0, sizeof(pids));
   83 
   84         for (n = 0; n < MAX_PIDS; n++) {
   85 retry:          if (!g_keep_stressing_flag)
   86                 goto reap;
   87 
   88             pids[n] = fork();
   89             if (pids[n] < 0) {
   90                 /* Out of resources for fork, re-do, ugh */
   91                 if ((errno == EAGAIN) || (errno == ENOMEM)) {
   92                     (void)shim_usleep(10000);
   93                     goto retry;
   94                 }
   95                 break;
   96             }
   97             if (pids[n] == 0) {
   98                 /* Child */
   99                 (void)setpgid(0, g_pgrp);
  100                 stress_parent_died_alarm();
  101 
  102                 if (stress_sighandler(args->name, SIGSEGV, stress_segvhandler, NULL) < 0)
  103                     _exit(_EXIT_FAILURE);
  104 
  105                 if (sysinfo(&info) < 0) {
  106                     pr_fail_err("sysinfo");
  107                     _exit(_EXIT_FAILURE);
  108                 }
  109 
  110                 len = ((size_t)info.freeram / (args->num_instances * MAX_PIDS)) / 2;
  111                 segv_ret = _EXIT_SEGV_MMAP;
  112                 ptr = mmap(NULL, len, PROT_READ | PROT_WRITE,
  113                     MAP_POPULATE | MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  114                 if (ptr != MAP_FAILED) {
  115                     segv_ret = _EXIT_SEGV_MADV_WILLNEED;
  116                     (void)shim_madvise(ptr, len, MADV_WILLNEED);
  117 
  118                     segv_ret = _EXIT_SEGV_MEMSET;
  119                     (void)memset(ptr, 0, len);
  120 
  121                     segv_ret = _EXIT_SEGV_MADV_DONTNEED;
  122                     (void)shim_madvise(ptr, len, MADV_DONTNEED);
  123 
  124                     segv_ret = _EXIT_SEGV_MUNMAP;
  125                     (void)munmap(ptr, len);
  126                 }
  127                 _exit(EXIT_SUCCESS);
  128             }
  129             (void)setpgid(pids[n], g_pgrp);
  130         }
  131 reap:
  132         for (i = 0; i < n; i++) {
  133             int status;
  134 
  135             if (waitpid(pids[i], &status, 0) < 0) {
  136                 if (errno != EINTR) {
  137                     pr_err("%s: waitpid errno=%d (%s)\n",
  138                         args->name, errno, strerror(errno));
  139                 }
  140             } else {
  141                 if (WIFEXITED(status)) {
  142                     int masked = WEXITSTATUS(status) & _EXIT_MASK;
  143 
  144                     if (masked) {
  145                         segv_count++;
  146                         segv_reasons |= masked;
  147                     }
  148                 }
  149             }
  150         }
  151         inc_counter(args);
  152     } while (keep_stressing());
  153 
  154     if (segv_count) {
  155         char buffer[1024];
  156         size_t n = sizeof(buffer) - 1;
  157 
  158         *buffer = '\0';
  159 
  160         if (segv_reasons & _EXIT_SEGV_MMAP)
  161             __strlcat(buffer, " mmap", &n);
  162         if (segv_reasons & _EXIT_SEGV_MADV_WILLNEED)
  163             __strlcat(buffer, " madvise-WILLNEED", &n);
  164         if (segv_reasons & _EXIT_SEGV_MADV_DONTNEED)
  165             __strlcat(buffer, " madvise-DONTNEED", &n);
  166         if (segv_reasons & _EXIT_SEGV_MEMSET)
  167             __strlcat(buffer, " memset", &n);
  168         if (segv_reasons & _EXIT_SEGV_MUNMAP)
  169             __strlcat(buffer, " munmap", &n);
  170 
  171         pr_dbg("%s: SIGSEGV errors: %" PRIu64 " (where:%s)\n",
  172             args->name, segv_count, buffer);
  173     }
  174 
  175     return EXIT_SUCCESS;
  176 }
  177 
  178 stressor_info_t stress_mmapfork_info = {
  179     .stressor = stress_mmapfork,
  180     .class = CLASS_SCHEDULER | CLASS_VM | CLASS_OS
  181 };
  182 #else
  183 stressor_info_t stress_mmapfork_info = {
  184     .stressor = stress_not_implemented,
  185     .class = CLASS_SCHEDULER | CLASS_VM | CLASS_OS
  186 };
  187 #endif