"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-bsearch.c" (15 Mar 2019, 3412 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-bsearch.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_bsearch_size()
   29  *  set bsearch size from given option string
   30  */
   31 int stress_set_bsearch_size(const char *opt)
   32 {
   33     uint64_t bsearch_size;
   34 
   35     bsearch_size = get_uint64(opt);
   36     check_range("bsearch-size", bsearch_size,
   37         MIN_BSEARCH_SIZE, MAX_BSEARCH_SIZE);
   38     return set_setting("bsearch-size", TYPE_ID_UINT64, &bsearch_size);
   39 }
   40 
   41 /*
   42  *  cmp()
   43  *  compare int32 values for bsearch
   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  *  Monotonically increasing values
   60  */
   61 #define SETDATA(d, i, v, prev)      \
   62     d[i] = 1 + prev + (v & 0x7);    \
   63     v >>= 2;            \
   64     prev = d[i];            \
   65     i++;                \
   66 
   67 /*
   68  *  stress_bsearch()
   69  *  stress bsearch
   70  */
   71 static int stress_bsearch(const args_t *args)
   72 {
   73     int32_t *data, *ptr, prev = 0;
   74     size_t n, n8, i;
   75     uint64_t bsearch_size = DEFAULT_BSEARCH_SIZE;
   76 
   77     if (!get_setting("bsearch-size", &bsearch_size)) {
   78         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   79             bsearch_size = MAX_BSEARCH_SIZE;
   80         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   81             bsearch_size = MIN_BSEARCH_SIZE;
   82     }
   83     n = (size_t)bsearch_size;
   84     n8 = (n + 7) & ~7;
   85 
   86     /* allocate in multiples of 8 */
   87     if ((data = calloc(n8, sizeof(*data))) == NULL) {
   88         pr_fail_dbg("malloc");
   89         return EXIT_NO_RESOURCE;
   90     }
   91 
   92     /* Populate with ascending data */
   93     prev = 0;
   94     for (i = 0; i < n;) {
   95         uint64_t v = mwc64();
   96 
   97         SETDATA(data, i, v, prev);
   98         SETDATA(data, i, v, prev);
   99         SETDATA(data, i, v, prev);
  100         SETDATA(data, i, v, prev);
  101         SETDATA(data, i, v, prev);
  102         SETDATA(data, i, v, prev);
  103         SETDATA(data, i, v, prev);
  104         SETDATA(data, i, v, prev);
  105     }
  106 
  107     do {
  108         for (ptr = data, i = 0; i < n; i++, ptr++) {
  109             int32_t *result;
  110             result = bsearch(ptr, data, n, sizeof(*ptr), cmp);
  111             if (g_opt_flags & OPT_FLAGS_VERIFY) {
  112                 if (result == NULL)
  113                     pr_fail("%s: element %zu could "
  114                         "not be found\n",
  115                         args->name, i);
  116                 else if (*result != *ptr)
  117                     pr_fail("%s: element %zu "
  118                         "found %" PRIu32
  119                         ", expecting %" PRIu32 "\n",
  120                         args->name, i, *result, *ptr);
  121             }
  122         }
  123         inc_counter(args);
  124     } while (keep_stressing());
  125 
  126     free(data);
  127     return EXIT_SUCCESS;
  128 }
  129 
  130 stressor_info_t stress_bsearch_info = {
  131     .stressor = stress_bsearch,
  132     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  133 };