"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.10.00/stress-tsc.c" (8 Jul 2019, 3250 Bytes) of package /linux/privat/stress-ng-0.10.00.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.57_vs_0.09.58.

    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 static const help_t help[] = {
   28     { NULL, "tsc N",    "start N workers reading the TSC (x86 only)" },
   29     { NULL, "tsc-ops N",    "stop after N TSC bogo operations" },
   30     { NULL, NULL,       NULL }
   31 };
   32 
   33 #if defined(HAVE_CPUID_H) &&    \
   34     defined(STRESS_X86) &&  \
   35     defined(HAVE_CPUID) &&  \
   36     NEED_GNUC(4,6,0)
   37 
   38 static bool tsc_supported = false;
   39 
   40 /*
   41  *  stress_tsc_supported()
   42  *  check if tsc is supported
   43  */
   44 static int stress_tsc_supported(void)
   45 {
   46     uint32_t eax, ebx, ecx, edx;
   47 
   48     /* Intel CPU? */
   49     if (!cpu_is_x86()) {
   50         pr_inf("tsc stressor will be skipped, "
   51             "not a recognised Intel CPU\n");
   52         return -1;
   53     }
   54     /* ..and supports tsc? */
   55     __cpuid(1, eax, ebx, ecx, edx);
   56     if (!(edx & 0x10)) {
   57         pr_inf("tsc stressor will be skipped, CPU "
   58             "does not support the rdtsc instruction\n");
   59         return -1;
   60     }
   61     tsc_supported = true;
   62     return 0;
   63 }
   64 
   65 /*
   66  *  read tsc
   67  */
   68 static inline void rdtsc(void)
   69 {
   70 #if STRESS_TSC_SERIALIZED
   71     asm volatile("cpuid\nrdtsc\n" : : : "%edx", "%eax");
   72 #else
   73     asm volatile("rdtsc\n" : : : "%edx", "%eax");
   74 #endif
   75 }
   76 
   77 /*
   78  *  Unrolled 32 times
   79  */
   80 #define TSCx32()    \
   81 {           \
   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     rdtsc();    \
  109     rdtsc();    \
  110     rdtsc();    \
  111     rdtsc();    \
  112     rdtsc();    \
  113     rdtsc();    \
  114 }
  115 
  116 /*
  117  *  stress_tsc()
  118  *      stress Intel tsc instruction
  119  */
  120 static int stress_tsc(const args_t *args)
  121 {
  122     if (tsc_supported) {
  123         do {
  124             TSCx32();
  125             TSCx32();
  126             TSCx32();
  127             TSCx32();
  128             inc_counter(args);
  129         } while (keep_stressing());
  130     }
  131     return EXIT_SUCCESS;
  132 }
  133 
  134 stressor_info_t stress_tsc_info = {
  135     .stressor = stress_tsc,
  136     .supported = stress_tsc_supported,
  137     .class = CLASS_CPU,
  138     .help = help
  139 };
  140 #else
  141 
  142 static int stress_tsc_supported(void)
  143 {
  144     pr_inf("tsc stressor will be skipped, CPU "
  145         "does not support the rdtsc instruction.\n");
  146     return -1;
  147 }
  148 
  149 stressor_info_t stress_tsc_info = {
  150     .stressor = stress_not_implemented,
  151     .supported = stress_tsc_supported,
  152     .class = CLASS_CPU,
  153     .help = help
  154 };
  155 #endif