"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.13.05/stress-malloc.c" (11 Oct 2021, 10779 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-malloc.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 #define MAX_MALLOC_PTHREADS     (32)
   28 
   29 static size_t malloc_max;       /* Maximum number of allocations */
   30 static size_t malloc_bytes;     /* Maximum per-allocation size */
   31 #if defined(HAVE_LIB_PTHREAD)
   32 static volatile bool keep_thread_running_flag;  /* False to stop pthreads */
   33 #endif
   34 static size_t malloc_pthreads;      /* Number of pthreads */
   35 #if defined(__GNUC__) &&    \
   36     defined(HAVE_MALLOPT) &&    \
   37     defined(M_MMAP_THRESHOLD)
   38 static size_t malloc_threshold;     /* When to use mmap and not sbrk */
   39 #endif
   40 static bool malloc_touch;       /* True will touch allocate pages */
   41 
   42 #if defined(HAVE_LIB_PTHREAD)
   43 /* per pthread data */
   44 typedef struct {
   45         pthread_t pthread;      /* The pthread */
   46         int       ret;          /* pthread create return */
   47 } stress_pthread_info_t;
   48 #endif
   49 
   50 typedef struct {
   51     const stress_args_t *args;          /* args info */
   52     uint64_t *counters;             /* bogo op counters */
   53     size_t instance;                /* per thread instance number */
   54 } stress_malloc_args_t;
   55 
   56 static const stress_help_t help[] = {
   57     { NULL, "malloc N",     "start N workers exercising malloc/realloc/free" },
   58     { NULL, "malloc-bytes N",   "allocate up to N bytes per allocation" },
   59     { NULL, "malloc-max N",     "keep up to N allocations at a time" },
   60     { NULL, "malloc-ops N",     "stop after N malloc bogo operations" },
   61     { NULL, "malloc-thresh N",  "threshold where malloc uses mmap instead of sbrk" },
   62     { NULL, "malloc-pthreads N",    "number of pthreads to run concurrently" },
   63     { NULL, "malloc-touch",     "touch pages force pages to be populated" },
   64     { NULL, NULL,           NULL }
   65 };
   66 
   67 static int stress_set_malloc_bytes(const char *opt)
   68 {
   69     size_t bytes;
   70 
   71     bytes = (size_t)stress_get_uint64_byte_memory(opt, 1);
   72     stress_check_range_bytes("malloc-bytes", bytes,
   73         MIN_MALLOC_BYTES, MAX_MEM_LIMIT);
   74     return stress_set_setting("malloc-bytes", TYPE_ID_SIZE_T, &bytes);
   75 }
   76 
   77 static int stress_set_malloc_max(const char *opt)
   78 {
   79     size_t max;
   80 
   81     max = (size_t)stress_get_uint64_byte(opt);
   82     stress_check_range("malloc-max", max,
   83         MIN_MALLOC_MAX, MAX_MALLOC_MAX);
   84     return stress_set_setting("malloc-max", TYPE_ID_SIZE_T, &max);
   85 }
   86 
   87 static int stress_set_malloc_threshold(const char *opt)
   88 {
   89     size_t threshold;
   90 
   91     threshold = (size_t)stress_get_uint64_byte(opt);
   92     stress_check_range("malloc-threshold", threshold,
   93         MIN_MALLOC_THRESHOLD, MAX_MALLOC_THRESHOLD);
   94     return stress_set_setting("malloc-threshold", TYPE_ID_SIZE_T, &threshold);
   95 }
   96 
   97 static int stress_set_malloc_pthreads(const char *opt)
   98 {
   99     size_t npthreads;
  100 
  101     npthreads = (size_t)stress_get_uint64_byte(opt);
  102     stress_check_range("malloc-pthreads", npthreads,
  103         0, MAX_MALLOC_PTHREADS);
  104     return stress_set_setting("malloc-pthreads", TYPE_ID_SIZE_T, &npthreads);
  105 }
  106 
  107 static int stress_set_malloc_touch(const char *opt)
  108 {
  109     bool malloc_touch_tmp = true;
  110 
  111     (void)opt;
  112     return stress_set_setting("malloc-touch", TYPE_ID_BOOL, &malloc_touch_tmp);
  113 }
  114 
  115 /*
  116  *  stress_alloc_size()
  117  *  get a new allocation size, ensuring
  118  *  it is never zero bytes.
  119  */
  120 static inline size_t stress_alloc_size(const size_t size)
  121 {
  122     const size_t len = stress_mwc64() % size;
  123 
  124     return len ? len : 1;
  125 }
  126 
  127 static void stress_malloc_page_touch(
  128     uint8_t *buffer,
  129     const size_t size,
  130     const size_t page_size)
  131 {
  132 
  133     if (malloc_touch) {
  134         register uint8_t *ptr;
  135         const uint8_t *end = buffer + size;
  136 
  137         for (ptr = buffer; keep_stressing_flag() && (ptr < end); ptr += page_size)
  138             *ptr = 0xff;
  139     } else {
  140         (void)stress_mincore_touch_pages_interruptible(buffer, size);
  141     }
  142 }
  143 
  144 /*
  145  *  stress_malloc_racy_count()
  146  *  racy bogo op counter, we have a lot of contention
  147  *  if we lock the args->counter, so sum per-process
  148  *  counters in a racy way.
  149  */
  150 static uint64_t stress_malloc_racy_count(const uint64_t *counters)
  151 {
  152     register uint64_t count = 0;
  153     register size_t i;
  154 
  155     for (i = 0; i < malloc_pthreads + 1; i++)
  156         count += counters[i];
  157 
  158     return count;
  159 }
  160 
  161 /*
  162  *  stress_malloc_keep_stressing(args)
  163  *      check if SIGALRM has triggered to the bogo ops count
  164  *      has been reached, counter is racy, but that's OK
  165  */
  166 static bool HOT OPTIMIZE3 stress_malloc_keep_stressing(
  167         const stress_args_t *args,
  168         uint64_t *counters)
  169 {
  170         return (LIKELY(g_keep_stressing_flag) &&
  171                 LIKELY(!args->max_ops ||
  172                 (stress_malloc_racy_count(counters) < args->max_ops)));
  173 }
  174 
  175 static void *stress_malloc_loop(void *ptr)
  176 {
  177     const stress_malloc_args_t *malloc_args = (stress_malloc_args_t *)ptr;
  178     const stress_args_t *args = malloc_args->args;
  179     const size_t page_size = args->page_size;
  180     uint64_t *counters = malloc_args->counters;
  181     uint64_t *counter = &counters[malloc_args->instance];
  182     void **addr;
  183     static void *nowt = NULL;
  184     size_t j;
  185 
  186     addr = (void **)calloc(malloc_max, sizeof(*addr));
  187     if (!addr) {
  188         pr_dbg("%s: cannot allocate address buffer: %d (%s)\n",
  189             args->name, errno, strerror(errno));
  190         return &nowt;
  191     }
  192 
  193     for (;;) {
  194         const unsigned int rnd = stress_mwc32();
  195         const unsigned int i = rnd % malloc_max;
  196         const unsigned int action = (rnd >> 12) & 1;
  197         const unsigned int do_calloc = (rnd >> 14) & 0x1f;
  198 
  199         /*
  200          * With many instances running it is wise to
  201          * double check before the next allocation as
  202          * sometimes process start up is delayed for
  203          * some time and we should bail out before
  204          * exerting any more memory pressure
  205          */
  206 #if defined(HAVE_LIB_PTHREAD)
  207         if (!keep_thread_running_flag)
  208             break;
  209 #endif
  210         if (!stress_malloc_keep_stressing(args, counters))
  211             break;
  212 
  213         if (addr[i]) {
  214             /* 50% free, 50% realloc */
  215             if (action) {
  216                 free(addr[i]);
  217                 addr[i] = NULL;
  218                 (*counter)++;
  219             } else {
  220                 void *tmp;
  221                 const size_t len = stress_alloc_size(malloc_bytes);
  222 
  223                 tmp = realloc(addr[i], len);
  224                 if (tmp) {
  225                     addr[i] = tmp;
  226                     stress_malloc_page_touch(addr[i], len, page_size);
  227                     (*counter)++;
  228                 }
  229             }
  230         } else {
  231             /* 50% free, 50% alloc */
  232             if (action) {
  233                 size_t len = stress_alloc_size(malloc_bytes);
  234 
  235                 if (do_calloc == 0) {
  236                     size_t n = ((rnd >> 15) % 17) + 1;
  237                     addr[i] = calloc(n, len / n);
  238                     len = n * (len / n);
  239                 } else {
  240                     addr[i] = malloc(len);
  241                 }
  242                 if (addr[i]) {
  243                     stress_malloc_page_touch(addr[i], len, page_size);
  244                     (*counter)++;
  245                 }
  246             }
  247         }
  248     }
  249 
  250     for (j = 0; j < malloc_max; j++) {
  251         free(addr[j]);
  252     }
  253     free(addr);
  254 
  255     return &nowt;
  256 }
  257 
  258 static int stress_malloc_child(const stress_args_t *args, void *context)
  259 {
  260 #if defined(HAVE_LIB_PTHREAD)
  261     stress_pthread_info_t pthreads[MAX_MALLOC_PTHREADS];
  262     size_t j;
  263 #endif
  264     /*
  265      *  pthread instance 0 is actually the main child process,
  266      *  insances 1..N are pthreads 0..N-1
  267      */
  268     uint64_t counters[MAX_MALLOC_PTHREADS + 1];
  269     stress_malloc_args_t malloc_args[MAX_MALLOC_PTHREADS + 1];
  270 
  271     (void)memset(counters, 0, sizeof(counters));
  272     (void)memset(malloc_args, 0, sizeof(malloc_args));
  273 
  274     malloc_args[0].args = args;
  275     malloc_args[0].counters = counters;
  276     malloc_args[0].instance = 0;
  277 
  278     (void)context;
  279 
  280     stress_set_proc_state(args->name, STRESS_STATE_RUN);
  281 
  282 #if defined(HAVE_LIB_PTHREAD)
  283     keep_thread_running_flag = true;
  284     (void)memset(pthreads, 0, sizeof(pthreads));
  285     for (j = 0; j < malloc_pthreads; j++) {
  286         malloc_args[j + 1].args = args;
  287         malloc_args[j + 1].counters = counters;
  288         malloc_args[j + 1].instance = j + 1;
  289         pthreads[j].ret = pthread_create(&pthreads[j].pthread, NULL,
  290             stress_malloc_loop, (void *)&malloc_args);
  291     }
  292 #else
  293     if ((args->instance == 0) && (malloc_pthreads > 0))
  294         pr_inf("%s: pthreads not supported, ignoring the "
  295             "--malloc-pthreads option\n", args->name);
  296 #endif
  297     stress_malloc_loop(&malloc_args);
  298 
  299     stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
  300 #if defined(HAVE_LIB_PTHREAD)
  301     keep_thread_running_flag = false;
  302     for (j = 0; j < malloc_pthreads; j++) {
  303         int ret;
  304 
  305         if (pthreads[j].ret)
  306             continue;
  307 
  308         ret = pthread_join(pthreads[j].pthread, NULL);
  309         if ((ret) && (ret != ESRCH)) {
  310             pr_fail("%s: pthread_join failed (parent), errno=%d (%s)\n",
  311                 args->name, ret, strerror(ret));
  312         }
  313     }
  314 #endif
  315 
  316     set_counter(args, stress_malloc_racy_count(counters));
  317 
  318     return EXIT_SUCCESS;
  319 }
  320 
  321 /*
  322  *  stress_malloc()
  323  *  stress malloc by performing a mix of
  324  *  allocation and frees
  325  */
  326 static int stress_malloc(const stress_args_t *args)
  327 {
  328     malloc_bytes = DEFAULT_MALLOC_BYTES;
  329     if (!stress_get_setting("malloc-bytes", &malloc_bytes)) {
  330         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
  331             malloc_bytes = MAX_32;
  332         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
  333             malloc_bytes = MIN_MALLOC_BYTES;
  334     }
  335     malloc_bytes /= args->num_instances;
  336     if (malloc_bytes < MIN_MALLOC_BYTES)
  337         malloc_bytes = MIN_MALLOC_BYTES;
  338 
  339     malloc_max = DEFAULT_MALLOC_MAX;
  340     if (!stress_get_setting("malloc-max", &malloc_max)) {
  341         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
  342             malloc_max = MAX_MALLOC_MAX;
  343         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
  344             malloc_max = MIN_MALLOC_MAX;
  345     }
  346 
  347 #if defined(__GNUC__) &&    \
  348     defined(HAVE_MALLOPT) &&    \
  349     defined(M_MMAP_THRESHOLD)
  350     malloc_threshold = DEFAULT_MALLOC_THRESHOLD;
  351     if (stress_get_setting("malloc-threshold", &malloc_threshold))
  352         (void)mallopt(M_MMAP_THRESHOLD, (int)malloc_threshold);
  353 #endif
  354     malloc_pthreads = 0;
  355     (void)stress_get_setting("malloc-pthreads", &malloc_pthreads);
  356 
  357     malloc_touch = false;
  358     (void)stress_get_setting("malloc-touch", &malloc_touch);
  359 
  360     return stress_oomable_child(args, NULL, stress_malloc_child, STRESS_OOMABLE_NORMAL);
  361 }
  362 
  363 static const stress_opt_set_func_t opt_set_funcs[] = {
  364     { OPT_malloc_max,   stress_set_malloc_max },
  365     { OPT_malloc_bytes, stress_set_malloc_bytes },
  366     { OPT_malloc_pthreads,  stress_set_malloc_pthreads },
  367     { OPT_malloc_threshold, stress_set_malloc_threshold },
  368     { OPT_malloc_touch, stress_set_malloc_touch },
  369     { 0,        NULL }
  370 };
  371 
  372 stressor_info_t stress_malloc_info = {
  373     .stressor = stress_malloc,
  374     .class = CLASS_CPU_CACHE | CLASS_MEMORY | CLASS_VM | CLASS_OS,
  375     .opt_set_funcs = opt_set_funcs,
  376     .help = help
  377 };