"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-mmapmany.c" (15 Mar 2019, 3973 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-mmapmany.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) 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 MMAP_MAX    (256*1024)
   28 
   29 /*
   30  *  stress_mmapmany()
   31  *  stress mmap with many pages being mapped
   32  */
   33 static int stress_mmapmany(const args_t *args)
   34 {
   35     const size_t page_size = args->page_size;
   36     pid_t pid;
   37     ssize_t max = sysconf(_SC_MAPPED_FILES);
   38     uint8_t **mappings;
   39     max = STRESS_MAXIMUM(max, MMAP_MAX);
   40 
   41     if (max < 1) {
   42         pr_fail_dbg("sysconf(_SC_MAPPED_FILES)");
   43         return EXIT_NO_RESOURCE;
   44     }
   45     if ((mappings = calloc(max, sizeof(uint8_t *))) == NULL) {
   46         pr_fail_dbg("malloc");
   47         return EXIT_NO_RESOURCE;
   48     }
   49 again:
   50     pid = fork();
   51     if (pid < 0) {
   52         if (g_keep_stressing_flag && (errno == EAGAIN))
   53             goto again;
   54         pr_err("%s: fork failed: errno=%d: (%s)\n",
   55             args->name, errno, strerror(errno));
   56     } else if (pid > 0) {
   57         int status, ret;
   58 
   59         (void)setpgid(pid, g_pgrp);
   60 
   61         /* Parent, wait for child */
   62         ret = waitpid(pid, &status, 0);
   63         if (ret < 0) {
   64             if (errno != EINTR)
   65                 pr_dbg("%s: waitpid(): errno=%d (%s)\n",
   66                     args->name, errno, strerror(errno));
   67             (void)kill(pid, SIGTERM);
   68             (void)kill(pid, SIGKILL);
   69             (void)waitpid(pid, &status, 0);
   70         } else if (WIFSIGNALED(status)) {
   71             pr_dbg("%s: child died: %s (instance %d)\n",
   72                 args->name, stress_strsignal(WTERMSIG(status)),
   73                 args->instance);
   74             /* If we got killed by OOM killer, re-start */
   75             if (WTERMSIG(status) == SIGKILL) {
   76                 if (g_opt_flags & OPT_FLAGS_OOMABLE) {
   77                     log_system_mem_info();
   78                     pr_dbg("%s: assuming killed by OOM "
   79                         "killer, bailing out "
   80                         "(instance %d)\n",
   81                         args->name, args->instance);
   82                     _exit(0);
   83                 } else {
   84                     pr_dbg("%s: assuming killed by OOM "
   85                         "killer, restarting again  "
   86                         "(instance %d)\n", args->name,
   87                         args->instance);
   88                     goto again;
   89                 }
   90             }
   91             /* If we got killed by sigsegv, re-start */
   92             if (WTERMSIG(status) == SIGSEGV) {
   93                 pr_dbg("%s: killed by SIGSEGV, "
   94                     "restarting again "
   95                     "(instance %d)\n",
   96                     args->name, args->instance);
   97                 goto again;
   98             }
   99         }
  100     } else if (pid == 0) {
  101         ssize_t i, n;
  102 
  103         (void)setpgid(0, g_pgrp);
  104         stress_parent_died_alarm();
  105 
  106         /* Make sure this is killable by OOM killer */
  107         set_oom_adjustment(args->name, true);
  108 
  109         do {
  110             for (n = 0; g_keep_stressing_flag && (n < max); n++) {
  111                 if (!keep_stressing())
  112                     break;
  113 
  114                 mappings[n] = (uint8_t *)mmap(NULL,
  115                     page_size * 3,
  116                     PROT_READ | PROT_WRITE,
  117                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  118                 if (mappings[n] == MAP_FAILED)
  119                     break;
  120                 if (munmap((void *)(mappings[n] + page_size), page_size) < 0)
  121                     break;
  122                 inc_counter(args);
  123             }
  124 
  125             for (i = 0; i < n;  i++) {
  126                 munmap((void *)mappings[i], page_size);
  127                 munmap((void *)(mappings[i] + page_size), page_size);
  128                 munmap((void *)(mappings[i] + page_size + page_size), page_size);
  129             }
  130         } while (keep_stressing());
  131     }
  132 
  133     free(mappings);
  134 
  135     return EXIT_SUCCESS;
  136 }
  137 
  138 stressor_info_t stress_mmapmany_info = {
  139     .stressor = stress_mmapmany,
  140     .class = CLASS_VM | CLASS_OS
  141 };