"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-remap-file-pages.c" (15 Mar 2019, 3883 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-remap-file-pages.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 #if defined(HAVE_REMAP_FILE_PAGES)
   28 
   29 #define N_PAGES     (512)
   30 
   31 typedef uint16_t mapdata_t;
   32 
   33 /*
   34  *  check_order()
   35  *  check page order
   36  */
   37 static void check_order(
   38     const args_t *args,
   39     const size_t stride,
   40     const mapdata_t *data,
   41     const size_t *order,
   42     const char *ordering)
   43 {
   44     size_t i;
   45     bool failed;
   46 
   47     for (failed = false, i = 0; i < N_PAGES; i++) {
   48         if (data[i * stride] != order[i]) {
   49             failed = true;
   50             break;
   51         }
   52     }
   53     if (failed)
   54         pr_fail("%s: remap %s order pages failed\n",
   55             args->name, ordering);
   56 }
   57 
   58 /*
   59  *  remap_order()
   60  *  remap based on given order
   61  */
   62 static int remap_order(
   63     const args_t *args,
   64     const size_t stride,
   65     mapdata_t *data,
   66     const size_t *order,
   67     const size_t page_size)
   68 {
   69     size_t i;
   70 
   71     for (i = 0; i < N_PAGES; i++) {
   72         int ret;
   73 
   74         ret = remap_file_pages(data + (i * stride), page_size,
   75             0, order[i], 0);
   76         if (ret < 0) {
   77             pr_fail_err("remap_file_pages");
   78             return -1;
   79         }
   80     }
   81 
   82     return 0;
   83 }
   84 
   85 /*
   86  *  stress_remap
   87  *  stress page remapping
   88  */
   89 static int stress_remap(const args_t *args)
   90 {
   91     mapdata_t *data;
   92     const size_t page_size = args->page_size;
   93     const size_t data_size = N_PAGES * page_size;
   94     const size_t stride = page_size / sizeof(*data);
   95     size_t i;
   96 
   97     data = mmap(NULL, data_size, PROT_READ | PROT_WRITE,
   98             MAP_SHARED | MAP_ANONYMOUS, -1, 0);
   99     if (data == MAP_FAILED) {
  100         pr_err("%s: mmap failed: errno=%d (%s)\n",
  101             args->name, errno, strerror(errno));
  102         return EXIT_NO_RESOURCE;
  103     }
  104 
  105     for (i = 0; i < N_PAGES; i++)
  106         data[i * stride] = i;
  107 
  108     do {
  109         size_t order[N_PAGES];
  110 
  111         /* Reverse pages */
  112         for (i = 0; i < N_PAGES; i++)
  113             order[i] = N_PAGES - 1 - i;
  114 
  115         if (remap_order(args, stride, data, order, page_size) < 0)
  116             break;
  117         check_order(args, stride, data, order, "reverse");
  118 
  119         /* random order pages */
  120         for (i = 0; i < N_PAGES; i++)
  121             order[i] = i;
  122         for (i = 0; i < N_PAGES; i++) {
  123             size_t tmp, j = mwc32() % N_PAGES;
  124 
  125             tmp = order[i];
  126             order[i] = order[j];
  127             order[j] = tmp;
  128         }
  129 
  130         if (remap_order(args, stride, data, order, page_size) < 0)
  131             break;
  132         check_order(args, stride, data, order, "random");
  133 
  134         /* all mapped to 1 page */
  135         for (i = 0; i < N_PAGES; i++)
  136             order[i] = 0;
  137         if (remap_order(args, stride, data, order, page_size) < 0)
  138             break;
  139         check_order(args, stride, data, order, "all-to-1");
  140 
  141         /* reorder pages back again */
  142         for (i = 0; i < N_PAGES; i++)
  143             order[i] = i;
  144         if (remap_order(args, stride, data, order, page_size) < 0)
  145             break;
  146         check_order(args, stride, data, order, "forward");
  147 
  148         inc_counter(args);
  149     } while (keep_stressing());
  150 
  151     (void)munmap(data, data_size);
  152 
  153     return EXIT_SUCCESS;
  154 }
  155 
  156 stressor_info_t stress_remap_info = {
  157     .stressor = stress_remap,
  158     .class = CLASS_MEMORY | CLASS_OS
  159 };
  160 #else
  161 stressor_info_t stress_remap_info = {
  162     .stressor = stress_not_implemented,
  163     .class = CLASS_MEMORY | CLASS_OS
  164 };
  165 #endif