~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/arch/x86/util/tsc.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <linux/types.h>
  3 #include <math.h>
  4 #include <string.h>
  5 #include <stdlib.h>
  6 
  7 #include "../../../util/debug.h"
  8 #include "../../../util/tsc.h"
  9 #include "cpuid.h"
 10 
 11 u64 rdtsc(void)
 12 {
 13         unsigned int low, high;
 14 
 15         asm volatile("rdtsc" : "=a" (low), "=d" (high));
 16 
 17         return low | ((u64)high) << 32;
 18 }
 19 
 20 /*
 21  * Derive the TSC frequency in Hz from the /proc/cpuinfo, for example:
 22  * ...
 23  * model name      : Intel(R) Xeon(R) Gold 6154 CPU @ 3.00GHz
 24  * ...
 25  * will return 3000000000.
 26  */
 27 static double cpuinfo_tsc_freq(void)
 28 {
 29         double result = 0;
 30         FILE *cpuinfo;
 31         char *line = NULL;
 32         size_t len = 0;
 33 
 34         cpuinfo = fopen("/proc/cpuinfo", "r");
 35         if (!cpuinfo) {
 36                 pr_err("Failed to read /proc/cpuinfo for TSC frequency\n");
 37                 return NAN;
 38         }
 39         while (getline(&line, &len, cpuinfo) > 0) {
 40                 if (!strncmp(line, "model name", 10)) {
 41                         char *pos = strstr(line + 11, " @ ");
 42 
 43                         if (pos && sscanf(pos, " @ %lfGHz", &result) == 1) {
 44                                 result *= 1000000000;
 45                                 goto out;
 46                         }
 47                 }
 48         }
 49 out:
 50         if (fpclassify(result) == FP_ZERO)
 51                 pr_err("Failed to find TSC frequency in /proc/cpuinfo\n");
 52 
 53         free(line);
 54         fclose(cpuinfo);
 55         return result;
 56 }
 57 
 58 double arch_get_tsc_freq(void)
 59 {
 60         unsigned int a, b, c, d, lvl;
 61         static bool cached;
 62         static double tsc;
 63         char vendor[16];
 64 
 65         if (cached)
 66                 return tsc;
 67 
 68         cached = true;
 69         get_cpuid_0(vendor, &lvl);
 70         if (!strstr(vendor, "Intel"))
 71                 return 0;
 72 
 73         /*
 74          * Don't support Time Stamp Counter and
 75          * Nominal Core Crystal Clock Information Leaf.
 76          */
 77         if (lvl < 0x15) {
 78                 tsc = cpuinfo_tsc_freq();
 79                 return tsc;
 80         }
 81 
 82         cpuid(0x15, 0, &a, &b, &c, &d);
 83         /* TSC frequency is not enumerated */
 84         if (!a || !b || !c) {
 85                 tsc = cpuinfo_tsc_freq();
 86                 return tsc;
 87         }
 88 
 89         tsc = (double)c * (double)b / (double)a;
 90         return tsc;
 91 }
 92 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php