"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-radixsort.c" (15 Mar 2019, 3808 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-radixsort.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.52_vs_0.09.54.

    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 #if HAVE_LIB_BSD
   28 
   29 #define STR_SIZE    (8)
   30 
   31 static volatile bool do_jmp = true;
   32 static sigjmp_buf jmp_env;
   33 
   34 /*
   35  *  stress_radixsort_handler()
   36  *  SIGALRM generic handler
   37  */
   38 static void MLOCKED_TEXT stress_radixsort_handler(int signum)
   39 {
   40     (void)signum;
   41 
   42     if (do_jmp) {
   43         do_jmp = false;
   44         siglongjmp(jmp_env, 1);     /* Ugly, bounce back */
   45     }
   46 }
   47 #endif
   48 
   49 /*
   50  *  stress_set_radixsort_size()
   51  *  set radixsort size
   52  */
   53 int stress_set_radixsort_size(const char *opt)
   54 {
   55     uint64_t radixsort_size;
   56 
   57     radixsort_size = get_uint64(opt);
   58     check_range("radixsort-size", radixsort_size,
   59         MIN_QSORT_SIZE, MAX_QSORT_SIZE);
   60     return set_setting("radixsort-size", TYPE_ID_UINT64, &radixsort_size);
   61 }
   62 
   63 #if HAVE_LIB_BSD
   64 /*
   65  *  stress_radixsort()
   66  *  stress radixsort
   67  */
   68 static int stress_radixsort(const args_t *args)
   69 {
   70     uint64_t radixsort_size = DEFAULT_RADIXSORT_SIZE;
   71     const unsigned char **data;
   72     unsigned char *text, *ptr;
   73     size_t n, i;
   74     struct sigaction old_action;
   75     int ret;
   76     unsigned char revtable[256];
   77 
   78     if (!get_setting("radixsort-size", &radixsort_size)) {
   79         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   80             radixsort_size = MAX_RADIXSORT_SIZE;
   81         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   82             radixsort_size = MIN_RADIXSORT_SIZE;
   83     }
   84     n = (size_t)radixsort_size;
   85 
   86     text = calloc(n, STR_SIZE);
   87     if (!text) {
   88         pr_fail_dbg("calloc");
   89         return EXIT_NO_RESOURCE;
   90     }
   91     data = calloc(n, sizeof(*data));
   92     if (!data) {
   93         pr_fail_dbg("calloc");
   94         free(text);
   95         return EXIT_NO_RESOURCE;
   96     }
   97 
   98     if (stress_sighandler(args->name, SIGALRM, stress_radixsort_handler, &old_action) < 0) {
   99         free(data);
  100         free(text);
  101         return EXIT_FAILURE;
  102     }
  103 
  104     ret = sigsetjmp(jmp_env, 1);
  105     if (ret) {
  106         /*
  107          * We return here if SIGALRM jmp'd back
  108          */
  109         (void)stress_sigrestore(args->name, SIGALRM, &old_action);
  110         goto tidy;
  111     }
  112 
  113     for (i = 0; i < 256; i++)
  114         revtable[i] = 255 - i;
  115 
  116     /* This is very expensive, do it once */
  117     for (ptr = text, i = 0; i < n; i++, ptr += STR_SIZE) {
  118         data[i] = ptr;
  119         stress_strnrnd((char *)ptr, STR_SIZE);
  120     }
  121 
  122     do {
  123         /* Sort "random" data */
  124         (void)radixsort(data, n, NULL, 0);
  125         if (!g_keep_stressing_flag)
  126             break;
  127 
  128         /* Reverse sort */
  129         (void)radixsort(data, n, revtable, 0);
  130 
  131         /* Randomize first char */
  132         for (ptr = text, i = 0; i < n; i++, ptr += STR_SIZE)
  133             *ptr = 'a' + (mwc8() % 26);
  134 
  135         inc_counter(args);
  136     } while (keep_stressing());
  137 
  138     do_jmp = false;
  139     (void)stress_sigrestore(args->name, SIGALRM, &old_action);
  140 tidy:
  141     free(data);
  142     free(text);
  143 
  144     return EXIT_SUCCESS;
  145 }
  146 
  147 stressor_info_t stress_radixsort_info = {
  148     .stressor = stress_radixsort,
  149     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  150 };
  151 #else
  152 stressor_info_t stress_radixsort_info = {
  153     .stressor = stress_not_implemented,
  154     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  155 };
  156 #endif