"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-mincore.c" (15 Mar 2019, 2695 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 "core-mincore.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 /*
   28  * mincore_touch_pages_slow()
   29  *  touch pages, even when they are resident
   30  */
   31 static void mincore_touch_pages_slow(
   32     void *buf,
   33     const size_t n_pages,
   34     const size_t page_size)
   35 {
   36     size_t i;
   37     volatile char *buffer;
   38 
   39     for (buffer = buf, i = 0; i < n_pages; i++, buffer += page_size)
   40         (*buffer)++;
   41     for (buffer = buf, i = 0; i < n_pages; i++, buffer += page_size)
   42         (*buffer)--;
   43 }
   44 
   45 /*
   46  * mincore_touch_pages()
   47  *  touch a range of pages, ensure they are all in memory
   48  */
   49 int mincore_touch_pages(void *buf, const size_t buf_len)
   50 {
   51     const size_t page_size = stress_get_pagesize();
   52     const size_t n_pages = buf_len / page_size;
   53 
   54 #if !defined(HAVE_MINCORE)
   55     /* systems that don't have mincore */
   56     mincore_touch_pages_slow(buf, n_pages, page_size);
   57     return 0;
   58 #else
   59     /* systems that support mincore */
   60     unsigned char *vec;
   61     volatile char *buffer;
   62     size_t i;
   63     uintptr_t uintptr = (uintptr_t)buf & (page_size - 1);
   64 
   65     if (!(g_opt_flags & OPT_FLAGS_MMAP_MINCORE))
   66         return 0;
   67     if (n_pages < 1)
   68         return -1;
   69 
   70     vec = calloc(n_pages, 1);
   71     if (!vec) {
   72         mincore_touch_pages_slow(buf, n_pages, page_size);
   73         return 0;
   74     }
   75 
   76     /*
   77      *  Find range of pages that are not in memory
   78      */
   79     if (shim_mincore((void *)uintptr, buf_len, vec) < 0) {
   80         free(vec);
   81 
   82         mincore_touch_pages_slow(buf, n_pages, page_size);
   83         return 0;
   84     }
   85 
   86     /* If page is not resident in memory, touch it */
   87     for (buffer = buf, i = 0; i < n_pages; i++, buffer += page_size)
   88         if (!(vec[i] & 1))
   89             (*buffer)++;
   90 
   91     /* And restore contents */
   92     for (buffer = buf, i = 0; i < n_pages; i++, buffer += page_size)
   93         if (!(vec[i] & 1))
   94             (*buffer)--;
   95     free(vec);
   96     return 0;
   97 #endif
   98 }