"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-lockbus.c" (15 Mar 2019, 2968 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-lockbus.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(__GNUC__) || defined(__clang__)) && defined(STRESS_X86)) || \
   28     (defined(__GNUC__) && NEED_GNUC(4,7,0) && defined(STRESS_ARM)))
   29 
   30 #define BUFFER_SIZE (1024 * 1024 * 16)
   31 #define CHUNK_SIZE  (64 * 4)
   32 
   33 #if defined(__GNUC__) && NEED_GNUC(4,7,0)
   34 #define LOCK_AND_INC(ptr, inc)                         \
   35     __atomic_add_fetch(ptr, inc, __ATOMIC_SEQ_CST);            \
   36     ptr++;
   37 
   38 #else
   39 #define LOCK_AND_INC(ptr, inc)                         \
   40     asm volatile("lock addl %1,%0" : "+m" (*ptr) : "ir" (inc));    \
   41     ptr++;
   42 
   43 #endif
   44 
   45 #define LOCK_AND_INCx8(ptr, inc)    \
   46     LOCK_AND_INC(ptr, inc)      \
   47     LOCK_AND_INC(ptr, inc)      \
   48     LOCK_AND_INC(ptr, inc)      \
   49     LOCK_AND_INC(ptr, inc)      \
   50     LOCK_AND_INC(ptr, inc)      \
   51     LOCK_AND_INC(ptr, inc)      \
   52     LOCK_AND_INC(ptr, inc)      \
   53     LOCK_AND_INC(ptr, inc)
   54 
   55 /*
   56  *  stress_lockbus()
   57  *      stress memory with lock and increment
   58  */
   59 static int stress_lockbus(const args_t *args)
   60 {
   61     uint32_t *buffer;
   62     int flags = MAP_ANONYMOUS | MAP_SHARED;
   63 
   64 #if defined(MAP_POPULATE)
   65     flags |= MAP_POPULATE;
   66 #endif
   67     buffer = (uint32_t*)mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, flags, -1, 0);
   68     if (buffer == MAP_FAILED) {
   69         int rc = exit_status(errno);
   70         pr_err("%s: mmap failed\n", args->name);
   71         return rc;
   72     }
   73 
   74     do {
   75         uint32_t *ptr = buffer + ((mwc32() % (BUFFER_SIZE - CHUNK_SIZE)) >> 2);
   76         const uint32_t inc = 1;
   77 
   78         LOCK_AND_INCx8(ptr, inc);
   79         LOCK_AND_INCx8(ptr, inc);
   80         LOCK_AND_INCx8(ptr, inc);
   81         LOCK_AND_INCx8(ptr, inc);
   82         LOCK_AND_INCx8(ptr, inc);
   83         LOCK_AND_INCx8(ptr, inc);
   84         LOCK_AND_INCx8(ptr, inc);
   85         LOCK_AND_INCx8(ptr, inc);
   86 
   87         inc_counter(args);
   88     } while (keep_stressing());
   89 
   90     (void)munmap((void *)buffer, BUFFER_SIZE);
   91 
   92     return EXIT_SUCCESS;
   93 }
   94 
   95 stressor_info_t stress_lockbus_info = {
   96     .stressor = stress_lockbus,
   97     .class = CLASS_CPU_CACHE | CLASS_MEMORY
   98 };
   99 #else
  100 stressor_info_t stress_lockbus_info = {
  101     .stressor = stress_not_implemented,
  102     .class = CLASS_CPU_CACHE | CLASS_MEMORY
  103 };
  104 #endif