"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-hsearch.c" (15 Mar 2019, 3730 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-hsearch.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 /*
   28  *  stress_set_hsearch_size()
   29  *      set hsearch size from given option string
   30  */
   31 int stress_set_hsearch_size(const char *opt)
   32 {
   33     uint64_t hsearch_size;
   34 
   35     hsearch_size = get_uint64(opt);
   36     check_range("hsearch-size", hsearch_size,
   37         MIN_TSEARCH_SIZE, MAX_TSEARCH_SIZE);
   38     return set_setting("hsearch-size", TYPE_ID_UINT64, &hsearch_size);
   39 }
   40 
   41 /*
   42  *  stress_hsearch()
   43  *  stress hsearch
   44  */
   45 static int stress_hsearch(const args_t *args)
   46 {
   47     uint64_t hsearch_size = DEFAULT_HSEARCH_SIZE;
   48     size_t i, max;
   49     int ret = EXIT_FAILURE;
   50     char **keys;
   51 
   52     if (!get_setting("hsearch-size", &hsearch_size)) {
   53         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   54             hsearch_size = MAX_HSEARCH_SIZE;
   55         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   56             hsearch_size = MIN_HSEARCH_SIZE;
   57     }
   58 
   59     max = (size_t)hsearch_size;
   60 
   61     /* Make hash table with 25% slack */
   62     if (!hcreate(max + (max / 4))) {
   63         pr_fail_err("hcreate");
   64         return EXIT_FAILURE;
   65     }
   66 
   67     if ((keys = calloc(max, sizeof(*keys))) == NULL) {
   68         pr_err("%s: cannot allocate keys\n", args->name);
   69         goto free_hash;
   70     }
   71 
   72     /* Populate hash, make it 100% full for worst performance */
   73     for (i = 0; i < max; i++) {
   74         char buffer[32];
   75         ENTRY e;
   76 
   77         (void)snprintf(buffer, sizeof(buffer), "%zu", i);
   78         keys[i] = strdup(buffer);
   79         if (!keys[i]) {
   80             pr_err("%s: cannot allocate key\n", args->name);
   81             goto free_all;
   82         }
   83 
   84         e.key = keys[i];
   85         e.data = (void *)i;
   86 
   87         if (hsearch(e, ENTER) == NULL) {
   88             pr_err("%s: cannot allocate new hash item\n", args->name);
   89             goto free_all;
   90         }
   91     }
   92 
   93     do {
   94         for (i = 0; g_keep_stressing_flag && i < max; i++) {
   95             ENTRY e, *ep;
   96 
   97             e.key = keys[i];
   98             e.data = NULL;  /* Keep Coverity quiet */
   99             ep = hsearch(e, FIND);
  100             if (g_opt_flags & OPT_FLAGS_VERIFY) {
  101                 if (ep == NULL) {
  102                     pr_fail("%s: cannot find key %s\n", args->name, keys[i]);
  103                 } else {
  104                     if (i != (size_t)ep->data) {
  105                         pr_fail("%s: hash returned incorrect data %zd\n", args->name, i);
  106                     }
  107                 }
  108             }
  109         }
  110         inc_counter(args);
  111     } while (keep_stressing());
  112 
  113     ret = EXIT_SUCCESS;
  114 
  115 free_all:
  116     /*
  117      * The sematics to hdestroy are rather varied from
  118      * system to system.  OpenBSD will free the keys,
  119      * where as NetBSD provides traditional functionality
  120      * that does not free them, plus hdestroy1 where
  121      * one can provide a free'ing callback.  Linux
  122      * currently does not destroy them.  It's a mess,
  123      * so for now, don't free them and just let it
  124      * leak, the exit() will clean up the heap for us
  125      */
  126     /*
  127     for (i = 0; i < max; i++)
  128         free(keys[i]);
  129     */
  130     free(keys);
  131 free_hash:
  132     hdestroy();
  133 
  134     return ret;
  135 }
  136 
  137 stressor_info_t stress_hsearch_info = {
  138     .stressor = stress_hsearch,
  139     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  140 };