"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-physpage.c" (15 Mar 2019, 4712 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-physpage.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(__linux__)
   28 
   29 #define PAGE_PRESENT    (1ULL << 63)
   30 #define PFN_MASK    ((1ULL << 54) - 1)
   31 
   32 /*
   33  *  stress_physpage_supported()
   34  *      check if we can run this as root
   35  */
   36 static int stress_physpage_supported(void)
   37 {
   38         if (geteuid() != 0) {
   39         pr_inf("stress-physpage stressor needs to be run as root to access page information\n");
   40                 return -1;
   41         }
   42         return 0;
   43 }
   44 
   45 static int stress_virt_to_phys(
   46     const args_t *args,
   47     const size_t page_size,
   48     const int fd_pm,
   49     const int fd_pc,
   50     const uintptr_t virt_addr)
   51 {
   52     off_t offset;
   53     uint64_t pageinfo;
   54 
   55     offset = (virt_addr / page_size) * sizeof(uint64_t);
   56     if (lseek(fd_pm, offset, SEEK_SET) != offset) {
   57         pr_err("%s: cannot seek on address %p in /proc/self/pagemap, errno=%d (%s)\n",
   58             args->name, (void *)virt_addr, errno, strerror(errno));
   59         goto err;
   60     }
   61     if (read(fd_pm, &pageinfo, sizeof(pageinfo)) != sizeof(pageinfo)) {
   62         pr_err("%s: cannot read address %p in /proc/self/pagemap, errno=%d (%s)\n",
   63             args->name, (void *)virt_addr, errno, strerror(errno));
   64         goto err;
   65     }
   66 
   67     if (pageinfo & PAGE_PRESENT) {
   68         uint64_t page_count;
   69         const uint64_t pfn = pageinfo & PFN_MASK;
   70         uintptr_t phys_addr = pfn * page_size;
   71 
   72         phys_addr |= (virt_addr & (page_size - 1));
   73         offset = pfn * sizeof(uint64_t);
   74 
   75         if (phys_addr == 0) {
   76             pr_err("%s: got zero physical address from virtual address %p\n",
   77                 args->name, (void *)virt_addr);
   78             goto err;
   79         }
   80 
   81         if (fd_pc < 0)
   82             return 0;
   83 
   84         if (lseek(fd_pc, offset, SEEK_SET) != offset) {
   85             pr_err("%s: cannot seek on address %p in /proc/kpagecount, errno=%d (%s)\n",
   86                 args->name, (void *)virt_addr, errno, strerror(errno));
   87             goto err;
   88         }
   89         if (read(fd_pc, &page_count, sizeof(page_count)) != sizeof(page_count)) {
   90             pr_err("%s: cannot read page count for address %p in /proc/kpagecount, errno=%d (%s)\n",
   91                 args->name, (void *)virt_addr, errno, strerror(errno));
   92             goto err;
   93         }
   94         if (page_count < 1) {
   95             pr_err("%s: got zero page count for physical address %p\n",
   96                 args->name, (void *)phys_addr);
   97             goto err;
   98         }
   99         return 0;
  100     } else {
  101         /*
  102          * Page is not present, it may have been swapped
  103          * out, so this is not an error, just highly unlikely
  104          */
  105         return 0;
  106     }
  107 err:
  108     return -1;
  109 }
  110 
  111 /*
  112  *  stress_physpage()
  113  *  stress physical page lookups
  114  */
  115 static int stress_physpage(const args_t *args)
  116 {
  117     int fd_pm, fd_pc;
  118     const size_t page_size = args->page_size;
  119     uint8_t *ptr = NULL;
  120 
  121     fd_pm = open("/proc/self/pagemap", O_RDONLY);
  122     if (fd_pm < 0) {
  123         pr_err("%s: cannot open /proc/self/pagemap, errno=%d (%s)\n",
  124             args->name, errno, strerror(errno));
  125         return EXIT_FAILURE;
  126     }
  127 
  128     /*
  129      *  this interface may not exist, don't make it a failure
  130      */
  131     fd_pc = open("/proc/kpagecount", O_RDONLY);
  132     if (fd_pc < 0) {
  133         if (args->instance == 0)
  134             pr_dbg("%s: cannot open /proc/kpagecount, errno=%d (%s)\n",
  135                 args->name, errno, strerror(errno));
  136         fd_pc = -1;
  137     }
  138 
  139     do {
  140         ptr = mmap(ptr + page_size, page_size, PROT_READ | PROT_WRITE,
  141             MAP_POPULATE | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  142         if (ptr != MAP_FAILED) {
  143             (void)stress_virt_to_phys(args, page_size, fd_pm, fd_pc, (uintptr_t)ptr);
  144             (void)munmap(ptr, page_size);
  145             (void)stress_virt_to_phys(args, page_size, fd_pm, fd_pc, (uintptr_t)g_shared->stats);
  146 
  147         }
  148         inc_counter(args);
  149     } while (keep_stressing());
  150 
  151     if (fd_pc > 0)
  152         (void)close(fd_pc);
  153     (void)close(fd_pm);
  154 
  155     return EXIT_SUCCESS;
  156 }
  157 
  158 stressor_info_t stress_physpage_info = {
  159     .stressor = stress_physpage,
  160     .supported = stress_physpage_supported,
  161     .class = CLASS_VM
  162 };
  163 #else
  164 stressor_info_t stress_physpage_info = {
  165     .stressor = stress_not_implemented,
  166     .class = CLASS_VM
  167 };
  168 #endif