"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-idle-page.c" (15 Mar 2019, 3204 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-idle-page.c" see the Fossies "Dox" file reference documentation.

    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 char *bitmap_file = "/sys/kernel/mm/page_idle/bitmap";
   28 
   29 /*
   30  *  stress_idle_page_supported()
   31  *      check if we can run this as root
   32  */
   33 static int stress_idle_page_supported(void)
   34 {
   35         if (geteuid() != 0) {
   36                 pr_inf("idle_page stressor will be skipped, "
   37                         "need to be running as root for this stressor\n");
   38                 return -1;
   39         }
   40 
   41     if (access(bitmap_file, R_OK) != 0) {
   42         pr_inf("idle_page stressor will be skipped, "
   43             "cannot access file %s\n", bitmap_file);
   44         return -1;
   45     }
   46         return 0;
   47 }
   48 
   49 #if defined(__linux__)
   50 
   51 #define BITMAP_BYTES    (8)
   52 #define PAGES_TO_SCAN   (64)
   53 
   54 /*
   55  *  stress_idle_page
   56  *  stress kernel logging interface
   57  */
   58 static int stress_idle_page(const args_t *args)
   59 {
   60     int fd;
   61     off_t posn = 0, last_posn = 0xfffffffffffffff8ULL;
   62     uint64_t bitmap_set[PAGES_TO_SCAN] ALIGNED(8);
   63 
   64     fd = open(bitmap_file, O_RDWR);
   65     if (fd < 0) {
   66         pr_inf("idle_page stressor will be skipped, "
   67             "cannot access file %s\n", bitmap_file);
   68         return EXIT_NO_RESOURCE;
   69     }
   70 
   71     (void)memset(bitmap_set, 0xff, sizeof(bitmap_set));
   72 
   73     do {
   74         off_t oret;
   75         int ret;
   76         uint64_t bitmap_get[PAGES_TO_SCAN] ALIGNED(8);
   77 
   78         oret = lseek(fd, posn, SEEK_SET);
   79         if (oret == (off_t)-1)
   80             goto next;
   81 
   82         ret = write(fd, bitmap_set, sizeof(bitmap_set));
   83         if ((ret < 0) && (errno == ENXIO)) {
   84             posn = 0;
   85             goto next;
   86         }
   87 
   88         oret = lseek(fd, posn, SEEK_SET);
   89         if (oret == (off_t)-1)
   90             goto next;
   91 
   92         ret = read(fd, &bitmap_get, BITMAP_BYTES);
   93         if ((ret < 0) && (errno == ENXIO)) {
   94             posn = 0;
   95             goto next;
   96         }
   97         last_posn = posn;
   98         posn += sizeof(bitmap_set);
   99 
  100         inc_counter(args);
  101 next:
  102         if (posn == last_posn) {
  103             pr_inf("%s: aborting early, seek position not advancing\n",
  104                 args->name);
  105             break;
  106         }
  107     } while (keep_stressing());
  108 
  109     (void)close(fd);
  110 
  111     return EXIT_SUCCESS;
  112 }
  113 
  114 stressor_info_t stress_idle_page_info = {
  115     .stressor = stress_idle_page,
  116     .supported = stress_idle_page_supported,
  117     .class = CLASS_OS
  118 };
  119 #else
  120 stressor_info_t stress_idle_page_info = {
  121     .stressor = stress_not_implemented,
  122     .supported = stress_idle_page_supported,
  123     .class = CLASS_OS
  124 };
  125 #endif