"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-tlb-shootdown.c" (15 Mar 2019, 4356 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-tlb-shootdown.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.50_vs_0.09.51.

    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_SCHED_GETAFFINITY) &&  \
   28     defined(HAVE_MPROTECT)
   29 
   30 #define MAX_TLB_PROCS   (8)
   31 #define MIN_TLB_PROCS   (2)
   32 #define MMAP_PAGES  (512)
   33 
   34 /*
   35  *  stress_tlb_shootdown()
   36  *  stress out TLB shootdowns
   37  */
   38 static int stress_tlb_shootdown(const args_t *args)
   39 {
   40     const size_t page_size = args->page_size;
   41     const size_t mmap_size = page_size * MMAP_PAGES;
   42     pid_t pids[MAX_TLB_PROCS];
   43     cpu_set_t proc_mask_initial;
   44 
   45     if (sched_getaffinity(0, sizeof(proc_mask_initial), &proc_mask_initial) < 0) {
   46         pr_fail_err("could not get CPU affinity");
   47         return EXIT_FAILURE;
   48     }
   49 
   50     do {
   51         uint8_t *mem, *ptr;
   52         int retry = 128;
   53         cpu_set_t proc_mask;
   54         int32_t tlb_procs, i;
   55         const int32_t max_cpus = stress_get_processors_configured();
   56 
   57         CPU_ZERO(&proc_mask);
   58         CPU_OR(&proc_mask, &proc_mask_initial, &proc_mask);
   59 
   60         tlb_procs = max_cpus;
   61         if (tlb_procs > MAX_TLB_PROCS)
   62             tlb_procs = MAX_TLB_PROCS;
   63         if (tlb_procs < MIN_TLB_PROCS)
   64             tlb_procs = MIN_TLB_PROCS;
   65 
   66         for (;;) {
   67             mem = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ,
   68                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
   69             if ((void *)mem == MAP_FAILED) {
   70                 if ((errno == EAGAIN) ||
   71                     (errno == ENOMEM) ||
   72                     (errno == ENFILE)) {
   73                     if (--retry < 0)
   74                         return EXIT_NO_RESOURCE;
   75                 } else {
   76                     pr_fail_err("mmap");
   77                 }
   78             } else {
   79                 break;
   80             }
   81         }
   82         (void)memset(mem, 0, mmap_size);
   83 
   84         for (i = 0; i < tlb_procs; i++)
   85             pids[i] = -1;
   86 
   87         for (i = 0; i < tlb_procs; i++) {
   88             int32_t j, cpu = -1;
   89 
   90             for (j = 0; j < max_cpus; j++) {
   91                 if (CPU_ISSET(j, &proc_mask)) {
   92                     cpu = j;
   93                     CPU_CLR(j, &proc_mask);
   94                     break;
   95                 }
   96             }
   97             if (cpu == -1)
   98                 break;
   99 
  100             pids[i] = fork();
  101             if (pids[i] < 0)
  102                 break;
  103             if (pids[i] == 0) {
  104                 cpu_set_t mask;
  105                 char buffer[page_size];
  106 
  107                 (void)setpgid(0, g_pgrp);
  108                 stress_parent_died_alarm();
  109 
  110                 /* Make sure this is killable by OOM killer */
  111                 set_oom_adjustment(args->name, true);
  112 
  113                 CPU_ZERO(&mask);
  114                 CPU_SET(cpu % max_cpus, &mask);
  115                 (void)sched_setaffinity(args->pid, sizeof(mask), &mask);
  116 
  117                 for (ptr = mem; ptr < mem + mmap_size; ptr += page_size) {
  118                     /* Force tlb shoot down on page */
  119                     (void)mprotect(ptr, page_size, PROT_READ);
  120                     (void)memcpy(buffer, ptr, page_size);
  121                     (void)munmap(ptr, page_size);
  122                 }
  123                 _exit(0);
  124             }
  125         }
  126 
  127         for (i = 0; i < tlb_procs; i++) {
  128             if (pids[i] != -1) {
  129                 int status, ret;
  130 
  131                 ret = waitpid(pids[i], &status, 0);
  132                 if ((ret < 0) && (errno == EINTR)) {
  133                     int j;
  134 
  135                     /*
  136                      * We got interrupted, so assume
  137                      * it was the alarm (timedout) or
  138                      * SIGINT so force terminate
  139                      */
  140                     for (j = i; j < tlb_procs; j++) {
  141                         if (pids[j] != -1)
  142                             (void)kill(pids[j], SIGKILL);
  143                     }
  144 
  145                     /* re-wait on the failed wait */
  146                     (void)waitpid(pids[i], &status, 0);
  147 
  148                     /* and continue waitpid on the pids */
  149                 }
  150             }
  151         }
  152         (void)munmap(mem, mmap_size);
  153         (void)sched_setaffinity(0, sizeof(proc_mask_initial), &proc_mask_initial);
  154         inc_counter(args);
  155     } while (keep_stressing());
  156 
  157     return EXIT_SUCCESS;
  158 }
  159 
  160 stressor_info_t stress_tlb_shootdown_info = {
  161     .stressor = stress_tlb_shootdown,
  162     .class = CLASS_OS | CLASS_MEMORY
  163 };
  164 #else
  165 stressor_info_t stress_tlb_shootdown_info = {
  166     .stressor = stress_not_implemented,
  167     .class = CLASS_OS | CLASS_MEMORY
  168 };
  169 #endif