"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-tsc.c" (15 Mar 2019, 3036 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-tsc.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(HAVE_CPUID_H) &&    \
   28     defined(STRESS_X86) &&  \
   29     defined(HAVE_CPUID) &&  \
   30     NEED_GNUC(4,6,0)
   31 
   32 static bool tsc_supported = false;
   33 
   34 /*
   35  *  stress_tsc_supported()
   36  *  check if tsc is supported
   37  */
   38 static int stress_tsc_supported(void)
   39 {
   40     uint32_t eax, ebx, ecx, edx;
   41 
   42     /* Intel CPU? */
   43     if (!cpu_is_x86()) {
   44         pr_inf("tsc stressor will be skipped, "
   45             "not a recognised Intel CPU\n");
   46         return -1;
   47     }
   48     /* ..and supports tsc? */
   49     __cpuid(1, eax, ebx, ecx, edx);
   50     if (!(edx & 0x10)) {
   51         pr_inf("tsc stressor will be skipped, CPU "
   52             "does not support the rdtsc instruction\n");
   53         return -1;
   54     }
   55     tsc_supported = true;
   56     return 0;
   57 }
   58 
   59 /*
   60  *  read tsc
   61  */
   62 static inline void rdtsc(void)
   63 {
   64 #if STRESS_TSC_SERIALIZED
   65     asm volatile("cpuid\nrdtsc\n" : : : "%edx", "%eax");
   66 #else
   67     asm volatile("rdtsc\n" : : : "%edx", "%eax");
   68 #endif
   69 }
   70 
   71 /*
   72  *  Unrolled 32 times
   73  */
   74 #define TSCx32()    \
   75 {           \
   76     rdtsc();    \
   77     rdtsc();    \
   78     rdtsc();    \
   79     rdtsc();    \
   80     rdtsc();    \
   81     rdtsc();    \
   82     rdtsc();    \
   83     rdtsc();    \
   84     rdtsc();    \
   85     rdtsc();    \
   86     rdtsc();    \
   87     rdtsc();    \
   88     rdtsc();    \
   89     rdtsc();    \
   90     rdtsc();    \
   91     rdtsc();    \
   92     rdtsc();    \
   93     rdtsc();    \
   94     rdtsc();    \
   95     rdtsc();    \
   96     rdtsc();    \
   97     rdtsc();    \
   98     rdtsc();    \
   99     rdtsc();    \
  100     rdtsc();    \
  101     rdtsc();    \
  102     rdtsc();    \
  103     rdtsc();    \
  104     rdtsc();    \
  105     rdtsc();    \
  106     rdtsc();    \
  107     rdtsc();    \
  108 }
  109 
  110 /*
  111  *  stress_tsc()
  112  *      stress Intel tsc instruction
  113  */
  114 static int stress_tsc(const args_t *args)
  115 {
  116     if (tsc_supported) {
  117         do {
  118             TSCx32();
  119             TSCx32();
  120             TSCx32();
  121             TSCx32();
  122             inc_counter(args);
  123         } while (keep_stressing());
  124     }
  125     return EXIT_SUCCESS;
  126 }
  127 
  128 stressor_info_t stress_tsc_info = {
  129     .stressor = stress_tsc,
  130     .supported = stress_tsc_supported,
  131     .class = CLASS_CPU
  132 };
  133 #else
  134 
  135 static int stress_tsc_supported(void)
  136 {
  137     pr_inf("tsc stressor will be skipped, CPU "
  138         "does not support the rdtsc instruction.\n");
  139     return -1;
  140 }
  141 
  142 stressor_info_t stress_tsc_info = {
  143     .stressor = stress_not_implemented,
  144     .supported = stress_tsc_supported,
  145     .class = CLASS_CPU
  146 };
  147 #endif