"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-atomic.c" (15 Mar 2019, 5163 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-atomic.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 #define DO_ATOMIC_OPS(type, var)            \
   28 {                           \
   29     type tmp = mwc64();             \
   30                             \
   31     __atomic_store(var, &tmp, __ATOMIC_RELAXED);    \
   32     __atomic_load(var, &tmp, __ATOMIC_RELAXED); \
   33     __atomic_load(var, &tmp, __ATOMIC_ACQUIRE); \
   34     __atomic_add_fetch(var, 1, __ATOMIC_RELAXED);   \
   35     __atomic_add_fetch(var, 2, __ATOMIC_ACQUIRE);   \
   36     __atomic_sub_fetch(var, 3, __ATOMIC_RELAXED);   \
   37     __atomic_sub_fetch(var, 4, __ATOMIC_ACQUIRE);   \
   38     __atomic_and_fetch(var, ~1, __ATOMIC_RELAXED);  \
   39     __atomic_and_fetch(var, ~2, __ATOMIC_ACQUIRE);  \
   40     __atomic_xor_fetch(var, ~4, __ATOMIC_RELAXED);  \
   41     __atomic_xor_fetch(var, ~8, __ATOMIC_ACQUIRE);  \
   42     __atomic_or_fetch(var, 16, __ATOMIC_RELAXED);   \
   43     __atomic_or_fetch(var, 32, __ATOMIC_ACQUIRE);   \
   44     __atomic_nand_fetch(var, 64, __ATOMIC_RELAXED); \
   45     __atomic_nand_fetch(var, 128, __ATOMIC_ACQUIRE);\
   46     __atomic_clear(var, __ATOMIC_RELAXED);      \
   47                             \
   48     __atomic_store(var, &tmp, __ATOMIC_RELAXED);    \
   49     __atomic_fetch_add(var, 1, __ATOMIC_RELAXED);   \
   50     __atomic_fetch_add(var, 2, __ATOMIC_ACQUIRE);   \
   51     __atomic_fetch_sub(var, 3, __ATOMIC_RELAXED);   \
   52     __atomic_fetch_sub(var, 4, __ATOMIC_ACQUIRE);   \
   53     __atomic_fetch_and(var, ~1, __ATOMIC_RELAXED);  \
   54     __atomic_fetch_and(var, ~2, __ATOMIC_ACQUIRE);  \
   55     __atomic_fetch_xor(var, ~4, __ATOMIC_RELAXED);  \
   56     __atomic_fetch_xor(var, ~8, __ATOMIC_ACQUIRE);  \
   57     __atomic_fetch_or(var, 16, __ATOMIC_RELAXED);   \
   58     __atomic_fetch_or(var, 32, __ATOMIC_ACQUIRE);   \
   59     __atomic_fetch_nand(var, 64, __ATOMIC_RELAXED); \
   60     __atomic_fetch_nand(var, 128, __ATOMIC_ACQUIRE);\
   61     __atomic_clear(var, __ATOMIC_RELAXED);      \
   62                             \
   63     __atomic_store(var, &tmp, __ATOMIC_RELAXED);    \
   64     __atomic_load(var, &tmp, __ATOMIC_RELAXED); \
   65     __atomic_add_fetch(var, 1, __ATOMIC_RELAXED);   \
   66     __atomic_sub_fetch(var, 3, __ATOMIC_RELAXED);   \
   67     __atomic_and_fetch(var, ~1, __ATOMIC_RELAXED);  \
   68     __atomic_xor_fetch(var, ~4, __ATOMIC_RELAXED);  \
   69     __atomic_or_fetch(var, 16, __ATOMIC_RELAXED);   \
   70     __atomic_nand_fetch(var, 64, __ATOMIC_RELAXED); \
   71     __atomic_load(var, &tmp, __ATOMIC_ACQUIRE); \
   72     __atomic_add_fetch(var, 2, __ATOMIC_ACQUIRE);   \
   73     __atomic_sub_fetch(var, 4, __ATOMIC_ACQUIRE);   \
   74     __atomic_and_fetch(var, ~2, __ATOMIC_ACQUIRE);  \
   75     __atomic_xor_fetch(var, ~8, __ATOMIC_ACQUIRE);  \
   76     __atomic_or_fetch(var, 32, __ATOMIC_ACQUIRE);   \
   77     __atomic_nand_fetch(var, 128, __ATOMIC_ACQUIRE);\
   78     __atomic_clear(var, __ATOMIC_RELAXED);      \
   79                             \
   80     __atomic_store(var, &tmp, __ATOMIC_RELAXED);    \
   81     __atomic_fetch_add(var, 1, __ATOMIC_RELAXED);   \
   82     __atomic_fetch_sub(var, 3, __ATOMIC_RELAXED);   \
   83     __atomic_fetch_and(var, ~1, __ATOMIC_RELAXED);  \
   84     __atomic_fetch_xor(var, ~4, __ATOMIC_RELAXED);  \
   85     __atomic_fetch_or(var, 16, __ATOMIC_RELAXED);   \
   86     __atomic_fetch_nand(var, 64, __ATOMIC_RELAXED); \
   87     __atomic_fetch_add(var, 2, __ATOMIC_ACQUIRE);   \
   88     __atomic_fetch_sub(var, 4, __ATOMIC_ACQUIRE);   \
   89     __atomic_fetch_and(var, ~2, __ATOMIC_ACQUIRE);  \
   90     __atomic_fetch_xor(var, ~8, __ATOMIC_ACQUIRE);  \
   91     __atomic_fetch_or(var, 32, __ATOMIC_ACQUIRE);   \
   92     __atomic_fetch_nand(var, 128, __ATOMIC_ACQUIRE);\
   93     __atomic_clear(var, __ATOMIC_RELAXED);      \
   94 }
   95 
   96 
   97 #if defined(TEST_ATOMIC_BUILD)
   98 
   99 uint64_t mwc64(void)
  100 {
  101     static uint64_t v = 0xdeadbeef;
  102 
  103     v = (v >> 1) ^ (v << 63);
  104 
  105     return v;
  106 }
  107 
  108 int main(void)
  109 {
  110     uint64_t val64;
  111     uint32_t val32;
  112     uint16_t val16;
  113     uint8_t  val8;
  114 
  115     DO_ATOMIC_OPS(uint64_t, &val64);
  116     DO_ATOMIC_OPS(uint32_t, &val32);
  117     DO_ATOMIC_OPS(uint16_t, &val16);
  118     DO_ATOMIC_OPS(uint8_t, &val8);
  119 
  120     return 0;
  121 }
  122 
  123 #else
  124 #if defined(HAVE_ATOMIC)
  125 
  126 /*
  127  *  stress_atomic()
  128  *      stress gcc atomic memory ops
  129  */
  130 static int stress_atomic(const args_t *args)
  131 {
  132     do {
  133         DO_ATOMIC_OPS(uint64_t, &g_shared->atomic.val64);
  134         DO_ATOMIC_OPS(uint32_t, &g_shared->atomic.val32);
  135         DO_ATOMIC_OPS(uint16_t, &g_shared->atomic.val16);
  136         DO_ATOMIC_OPS(uint8_t, &g_shared->atomic.val8);
  137         inc_counter(args);
  138     } while (keep_stressing());
  139 
  140     return EXIT_SUCCESS;
  141 }
  142 
  143 stressor_info_t stress_atomic_info = {
  144     .stressor = stress_atomic,
  145     .class = CLASS_CPU | CLASS_MEMORY
  146 };
  147 
  148 #else
  149 stressor_info_t stress_atomic_info = {
  150     .stressor = stress_not_implemented,
  151     .class = CLASS_CPU | CLASS_MEMORY
  152 };
  153 #endif
  154 #endif