"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-tsearch.c" (15 Mar 2019, 3526 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-tsearch.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_tsearch_size()
   29  *      set tsearch size from given option string
   30  */
   31 int stress_set_tsearch_size(const char *opt)
   32 {
   33     uint64_t tsearch_size;
   34 
   35     tsearch_size = get_uint64(opt);
   36     check_range("tsearch-size", tsearch_size,
   37         MIN_TSEARCH_SIZE, MAX_TSEARCH_SIZE);
   38     return set_setting("tsearch-size", TYPE_ID_UINT64, &tsearch_size);
   39 }
   40 
   41 /*
   42  *  cmp()
   43  *  sort on int32 values
   44  */
   45 static int cmp(const void *p1, const void *p2)
   46 {
   47     const int32_t *i1 = (const int32_t *)p1;
   48     const int32_t *i2 = (const int32_t *)p2;
   49 
   50     if (*i1 > *i2)
   51         return 1;
   52     else if (*i1 < *i2)
   53         return -1;
   54     else
   55         return 0;
   56 }
   57 
   58 /*
   59  *  stress_tsearch()
   60  *  stress tsearch
   61  */
   62 static int stress_tsearch(const args_t *args)
   63 {
   64     uint64_t tsearch_size = DEFAULT_TSEARCH_SIZE;
   65     int32_t *data;
   66     size_t i, n;
   67 
   68     if (!get_setting("tsearch-size", &tsearch_size)) {
   69         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   70             tsearch_size = MAX_TSEARCH_SIZE;
   71         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   72             tsearch_size = MIN_TSEARCH_SIZE;
   73     }
   74     n = (size_t)tsearch_size;
   75 
   76     if ((data = calloc(n, sizeof(*data))) == NULL) {
   77         pr_fail_dbg("calloc");
   78         return EXIT_NO_RESOURCE;
   79     }
   80 
   81     do {
   82         void *root = NULL;
   83 
   84         /* Step #1, populate tree */
   85         for (i = 0; i < n; i++) {
   86             data[i] = ((mwc32() & 0xfff) << 20) ^ i;
   87             if (tsearch(&data[i], &root, cmp) == NULL) {
   88                 size_t j;
   89 
   90                 pr_err("%s: cannot allocate new "
   91                     "tree node\n", args->name);
   92                 for (j = 0; j < i; j++)
   93                     tdelete(&data[j], &root, cmp);
   94                 goto abort;
   95             }
   96         }
   97         /* Step #2, find */
   98         for (i = 0; g_keep_stressing_flag && i < n; i++) {
   99             void **result;
  100 
  101             result = tfind(&data[i], &root, cmp);
  102             if (g_opt_flags & OPT_FLAGS_VERIFY) {
  103                 if (result == NULL)
  104                     pr_fail("%s: element %zu "
  105                         "could not be found\n",
  106                         args->name, i);
  107                 else {
  108                     int32_t *val;
  109                     val = *result;
  110                     if (*val != data[i])
  111                         pr_fail("%s: element "
  112                             "%zu found %" PRIu32
  113                             ", expecting %" PRIu32 "\n",
  114                             args->name, i, *val, data[i]);
  115                 }
  116             }
  117         }
  118         /* Step #3, delete */
  119         for (i = 0; i < n; i++) {
  120             void **result;
  121 
  122             result = tdelete(&data[i], &root, cmp);
  123             if ((g_opt_flags & OPT_FLAGS_VERIFY) && (result == NULL))
  124                 pr_fail("%s: element %zu could not "
  125                     "be found\n", args->name, i);
  126         }
  127         inc_counter(args);
  128     } while (keep_stressing());
  129 
  130 abort:
  131     free(data);
  132     return EXIT_SUCCESS;
  133 }
  134 
  135 stressor_info_t stress_tsearch_info = {
  136     .stressor = stress_tsearch,
  137     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  138 };