"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.59.1/stress-remap-file-pages.c" (8 Jun 2019, 4107 Bytes) of package /linux/privat/stress-ng-0.09.59.1.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.57_vs_0.09.58.

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