"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-qsort.c" (15 Mar 2019, 4882 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-qsort.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 static volatile bool do_jmp = true;
   28 static sigjmp_buf jmp_env;
   29 
   30 /*
   31  *  stress_qsort_handler()
   32  *  SIGALRM generic handler
   33  */
   34 static void MLOCKED_TEXT stress_qsort_handler(int signum)
   35 {
   36     (void)signum;
   37 
   38     if (do_jmp) {
   39         do_jmp = false;
   40         siglongjmp(jmp_env, 1);     /* Ugly, bounce back */
   41     }
   42 }
   43 
   44 /*
   45  *  stress_set_qsort_size()
   46  *  set qsort size
   47  */
   48 int stress_set_qsort_size(const char *opt)
   49 {
   50     uint64_t qsort_size;
   51 
   52     qsort_size = get_uint64(opt);
   53     check_range("qsort-size", qsort_size,
   54         MIN_QSORT_SIZE, MAX_QSORT_SIZE);
   55     return set_setting("qsort-size", TYPE_ID_UINT64, &qsort_size);
   56 }
   57 
   58 /*
   59  *  stress_qsort_cmp_1()
   60  *  qsort comparison - sort on int32 values
   61  */
   62 static int stress_qsort_cmp_1(const void *p1, const void *p2)
   63 {
   64     const int32_t *i1 = (const int32_t *)p1;
   65     const int32_t *i2 = (const int32_t *)p2;
   66 
   67     if (*i1 > *i2)
   68         return 1;
   69     else if (*i1 < *i2)
   70         return -1;
   71     else
   72         return 0;
   73 }
   74 
   75 /*
   76  *  stress_qsort_cmp_1()
   77  *  qsort comparison - reverse sort on int32 values
   78  */
   79 static int stress_qsort_cmp_2(const void *p1, const void *p2)
   80 {
   81     return stress_qsort_cmp_1(p2, p1);
   82 }
   83 
   84 /*
   85  *  stress_qsort_cmp_1()
   86  *  qsort comparison - sort on int8 values
   87  */
   88 static int stress_qsort_cmp_3(const void *p1, const void *p2)
   89 {
   90     const int8_t *i1 = (const int8_t *)p1;
   91     const int8_t *i2 = (const int8_t *)p2;
   92 
   93     /* Force re-ordering on 8 bit value */
   94     return *i1 - *i2;
   95 }
   96 
   97 /*
   98  *  stress_qsort()
   99  *  stress qsort
  100  */
  101 static int stress_qsort(const args_t *args)
  102 {
  103     uint64_t qsort_size = DEFAULT_QSORT_SIZE;
  104     int32_t *data, *ptr;
  105     size_t n, i;
  106     struct sigaction old_action;
  107     int ret;
  108 
  109     if (!get_setting("qsort-size", &qsort_size)) {
  110         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
  111             qsort_size = MAX_QSORT_SIZE;
  112         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
  113             qsort_size = MIN_QSORT_SIZE;
  114     }
  115     n = (size_t)qsort_size;
  116 
  117     if ((data = calloc(n, sizeof(*data))) == NULL) {
  118         pr_fail_dbg("calloc");
  119         return EXIT_NO_RESOURCE;
  120     }
  121 
  122     if (stress_sighandler(args->name, SIGALRM, stress_qsort_handler, &old_action) < 0) {
  123         free(data);
  124         return EXIT_FAILURE;
  125     }
  126 
  127     ret = sigsetjmp(jmp_env, 1);
  128     if (ret) {
  129         /*
  130          * We return here if SIGALRM jmp'd back
  131          */
  132         (void)stress_sigrestore(args->name, SIGALRM, &old_action);
  133         goto tidy;
  134     }
  135 
  136     /* This is expensive, do it once */
  137     for (ptr = data, i = 0; i < n; i++)
  138         *ptr++ = mwc32();
  139 
  140     do {
  141         /* Sort "random" data */
  142         qsort(data, n, sizeof(*data), stress_qsort_cmp_1);
  143         if (g_opt_flags & OPT_FLAGS_VERIFY) {
  144             for (ptr = data, i = 0; i < n - 1; i++, ptr++) {
  145                 if (*ptr > *(ptr+1)) {
  146                     pr_fail("%s: sort error "
  147                         "detected, incorrect ordering "
  148                         "found\n", args->name);
  149                     break;
  150                 }
  151             }
  152         }
  153         if (!g_keep_stressing_flag)
  154             break;
  155 
  156         /* Reverse sort */
  157         qsort(data, n, sizeof(*data), stress_qsort_cmp_2);
  158         if (g_opt_flags & OPT_FLAGS_VERIFY) {
  159             for (ptr = data, i = 0; i < n - 1; i++, ptr++) {
  160                 if (*ptr < *(ptr+1)) {
  161                     pr_fail("%s: reverse sort "
  162                         "error detected, incorrect "
  163                         "ordering found\n", args->name);
  164                     break;
  165                 }
  166             }
  167         }
  168         if (!g_keep_stressing_flag)
  169             break;
  170         /* And re-order by byte compare */
  171         qsort((uint8_t *)data, n * 4, sizeof(uint8_t), stress_qsort_cmp_3);
  172 
  173         /* Reverse sort this again */
  174         qsort(data, n, sizeof(*data), stress_qsort_cmp_2);
  175         if (g_opt_flags & OPT_FLAGS_VERIFY) {
  176             for (ptr = data, i = 0; i < n - 1; i++, ptr++) {
  177                 if (*ptr < *(ptr+1)) {
  178                     pr_fail("%s: reverse sort "
  179                         "error detected, incorrect "
  180                         "ordering found\n", args->name);
  181                     break;
  182                 }
  183             }
  184         }
  185         if (!g_keep_stressing_flag)
  186             break;
  187 
  188         inc_counter(args);
  189     } while (keep_stressing());
  190 
  191     do_jmp = false;
  192     (void)stress_sigrestore(args->name, SIGALRM, &old_action);
  193 tidy:
  194     free(data);
  195 
  196     return EXIT_SUCCESS;
  197 }
  198 
  199 stressor_info_t stress_qsort_info = {
  200     .stressor = stress_qsort,
  201     .class = CLASS_CPU_CACHE | CLASS_CPU | CLASS_MEMORY
  202 };