"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-lsearch.c" (15 Mar 2019, 3154 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-lsearch.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_lsearch_size()
   29  *      set lsearch size from given option string
   30  */
   31 int stress_set_lsearch_size(const char *opt)
   32 {
   33     uint64_t lsearch_size;
   34 
   35     lsearch_size = get_uint64(opt);
   36     check_range("lsearch-size", lsearch_size,
   37         MIN_TSEARCH_SIZE, MAX_TSEARCH_SIZE);
   38     return set_setting("lsearch-size", TYPE_ID_UINT64, &lsearch_size);
   39 }
   40 
   41 /*
   42  *  cmp()
   43  *  lsearch uint32 comparison for sorting
   44  */
   45 static int cmp(const void *p1, const void *p2)
   46 {
   47     return (*(const uint32_t *)p1 - *(const uint32_t *)p2);
   48 }
   49 
   50 /*
   51  *  stress_lsearch()
   52  *  stress lsearch
   53  */
   54 static int stress_lsearch(const args_t *args)
   55 {
   56     int32_t *data, *root;
   57     size_t i, max;
   58     uint64_t lsearch_size = DEFAULT_LSEARCH_SIZE;
   59 
   60     if (!get_setting("lsearch-size", &lsearch_size)) {
   61         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   62             lsearch_size = MAX_LSEARCH_SIZE;
   63         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   64             lsearch_size = MIN_LSEARCH_SIZE;
   65     }
   66     max = (size_t)lsearch_size;
   67 
   68     if ((data = calloc(max, sizeof(*data))) == NULL) {
   69         pr_fail_dbg("malloc");
   70         return EXIT_NO_RESOURCE;
   71     }
   72     if ((root = calloc(max, sizeof(*data))) == NULL) {
   73         free(data);
   74         pr_fail_dbg("malloc");
   75         return EXIT_NO_RESOURCE;
   76     }
   77 
   78     do {
   79         size_t n = 0;
   80 
   81         /* Step #1, populate with data */
   82         for (i = 0; g_keep_stressing_flag && i < max; i++) {
   83             void *ptr;
   84 
   85             data[i] = ((mwc32() & 0xfff) << 20) ^ i;
   86             ptr = lsearch(&data[i], root, &n, sizeof(*data), cmp);
   87             (void)ptr;
   88         }
   89         /* Step #2, find */
   90         for (i = 0; g_keep_stressing_flag && i < n; i++) {
   91             int32_t *result;
   92 
   93             result = lfind(&data[i], root, &n, sizeof(*data), cmp);
   94             if (g_opt_flags & OPT_FLAGS_VERIFY) {
   95                 if (result == NULL)
   96                     pr_fail("%s: element %zu could not be found\n", args->name, i);
   97                 else if (*result != data[i])
   98                     pr_fail("%s: element %zu found %" PRIu32 ", expecting %" PRIu32 "\n",
   99                     args->name, i, *result, data[i]);
  100             }
  101         }
  102         inc_counter(args);
  103     } while (keep_stressing());
  104 
  105     free(root);
  106     free(data);
  107     return EXIT_SUCCESS;
  108 }
  109 
  110 stressor_info_t stress_lsearch_info = {
  111     .stressor = stress_lsearch,
  112     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  113 };