Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * turbostat -- show CPU frequency and C-state residency |
| 3 | * on modern Intel turbo-capable processors. |
| 4 | * |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 5 | * Copyright (c) 2013 Intel Corporation. |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 6 | * Len Brown <len.brown@intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms and conditions of the GNU General Public License, |
| 10 | * version 2, as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 22 | #define _GNU_SOURCE |
Josh Triplett | b731f31 | 2013-08-20 17:20:12 -0700 | [diff] [blame] | 23 | #include MSRHEADER |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 24 | #include <stdarg.h> |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 25 | #include <stdio.h> |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 26 | #include <err.h> |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/resource.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <stdlib.h> |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 36 | #include <getopt.h> |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 37 | #include <dirent.h> |
| 38 | #include <string.h> |
| 39 | #include <ctype.h> |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 40 | #include <sched.h> |
Len Brown | 2a0609c | 2016-02-12 22:44:48 -0500 | [diff] [blame] | 41 | #include <time.h> |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 42 | #include <cpuid.h> |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 43 | #include <linux/capability.h> |
| 44 | #include <errno.h> |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 45 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 46 | char *proc_stat = "/proc/stat"; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 47 | FILE *outf; |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 48 | int *fd_percpu; |
Len Brown | 2a0609c | 2016-02-12 22:44:48 -0500 | [diff] [blame] | 49 | struct timespec interval_ts = {5, 0}; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 50 | unsigned int debug; |
| 51 | unsigned int rapl_joules; |
| 52 | unsigned int summary_only; |
| 53 | unsigned int dump_only; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 54 | unsigned int skip_c0; |
| 55 | unsigned int skip_c1; |
| 56 | unsigned int do_nhm_cstates; |
| 57 | unsigned int do_snb_cstates; |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 58 | unsigned int do_knl_cstates; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 59 | unsigned int do_pc2; |
| 60 | unsigned int do_pc3; |
| 61 | unsigned int do_pc6; |
| 62 | unsigned int do_pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 63 | unsigned int do_c8_c9_c10; |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 64 | unsigned int do_skl_residency; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 65 | unsigned int do_slm_cstates; |
| 66 | unsigned int use_c1_residency_msr; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 67 | unsigned int has_aperf; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 68 | unsigned int has_epb; |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 69 | unsigned int units = 1000000; /* MHz etc */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 70 | unsigned int genuine_intel; |
| 71 | unsigned int has_invariant_tsc; |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 72 | unsigned int do_nhm_platform_info; |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 73 | unsigned int extra_msr_offset32; |
| 74 | unsigned int extra_msr_offset64; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 75 | unsigned int extra_delta_offset32; |
| 76 | unsigned int extra_delta_offset64; |
Hubert Chrzaniuk | b2b34df | 2015-09-14 13:31:00 +0200 | [diff] [blame] | 77 | unsigned int aperf_mperf_multiplier = 1; |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 78 | int do_irq = 1; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 79 | int do_smi; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 80 | double bclk; |
Len Brown | a2b7b74 | 2015-09-26 00:12:38 -0400 | [diff] [blame] | 81 | double base_hz; |
Len Brown | 21ed557 | 2015-10-19 22:37:40 -0400 | [diff] [blame] | 82 | unsigned int has_base_hz; |
Len Brown | a2b7b74 | 2015-09-26 00:12:38 -0400 | [diff] [blame] | 83 | double tsc_tweak = 1.0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 84 | unsigned int show_pkg; |
| 85 | unsigned int show_core; |
| 86 | unsigned int show_cpu; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 87 | unsigned int show_pkg_only; |
| 88 | unsigned int show_core_only; |
| 89 | char *output_buffer, *outp; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 90 | unsigned int do_rapl; |
| 91 | unsigned int do_dts; |
| 92 | unsigned int do_ptm; |
| 93 | unsigned int tcc_activation_temp; |
| 94 | unsigned int tcc_activation_temp_override; |
Andrey Semin | 40ee8e3 | 2014-12-05 00:07:00 -0500 | [diff] [blame] | 95 | double rapl_power_units, rapl_time_units; |
| 96 | double rapl_dram_energy_units, rapl_energy_units; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 97 | double rapl_joule_counter_range; |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 98 | unsigned int do_core_perf_limit_reasons; |
| 99 | unsigned int do_gfx_perf_limit_reasons; |
| 100 | unsigned int do_ring_perf_limit_reasons; |
Len Brown | 8a5bdf4 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 101 | unsigned int crystal_hz; |
| 102 | unsigned long long tsc_hz; |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 103 | int base_cpu; |
Len Brown | 21ed557 | 2015-10-19 22:37:40 -0400 | [diff] [blame] | 104 | double discover_bclk(unsigned int family, unsigned int model); |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 105 | unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ |
| 106 | /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ |
| 107 | unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ |
| 108 | unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ |
| 109 | unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ |
| 110 | unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 111 | |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 112 | #define RAPL_PKG (1 << 0) |
| 113 | /* 0x610 MSR_PKG_POWER_LIMIT */ |
| 114 | /* 0x611 MSR_PKG_ENERGY_STATUS */ |
| 115 | #define RAPL_PKG_PERF_STATUS (1 << 1) |
| 116 | /* 0x613 MSR_PKG_PERF_STATUS */ |
| 117 | #define RAPL_PKG_POWER_INFO (1 << 2) |
| 118 | /* 0x614 MSR_PKG_POWER_INFO */ |
| 119 | |
| 120 | #define RAPL_DRAM (1 << 3) |
| 121 | /* 0x618 MSR_DRAM_POWER_LIMIT */ |
| 122 | /* 0x619 MSR_DRAM_ENERGY_STATUS */ |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 123 | #define RAPL_DRAM_PERF_STATUS (1 << 4) |
| 124 | /* 0x61b MSR_DRAM_PERF_STATUS */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 125 | #define RAPL_DRAM_POWER_INFO (1 << 5) |
| 126 | /* 0x61c MSR_DRAM_POWER_INFO */ |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 127 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 128 | #define RAPL_CORES (1 << 6) |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 129 | /* 0x638 MSR_PP0_POWER_LIMIT */ |
| 130 | /* 0x639 MSR_PP0_ENERGY_STATUS */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 131 | #define RAPL_CORE_POLICY (1 << 7) |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 132 | /* 0x63a MSR_PP0_POLICY */ |
| 133 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 134 | #define RAPL_GFX (1 << 8) |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 135 | /* 0x640 MSR_PP1_POWER_LIMIT */ |
| 136 | /* 0x641 MSR_PP1_ENERGY_STATUS */ |
| 137 | /* 0x642 MSR_PP1_POLICY */ |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 138 | #define TJMAX_DEFAULT 100 |
| 139 | |
| 140 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 141 | |
| 142 | int aperf_mperf_unstable; |
| 143 | int backwards_count; |
| 144 | char *progname; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 145 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 146 | cpu_set_t *cpu_present_set, *cpu_affinity_set; |
| 147 | size_t cpu_present_setsize, cpu_affinity_setsize; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 148 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 149 | struct thread_data { |
| 150 | unsigned long long tsc; |
| 151 | unsigned long long aperf; |
| 152 | unsigned long long mperf; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 153 | unsigned long long c1; |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 154 | unsigned long long extra_msr64; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 155 | unsigned long long extra_delta64; |
| 156 | unsigned long long extra_msr32; |
| 157 | unsigned long long extra_delta32; |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 158 | unsigned int irq_count; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 159 | unsigned int smi_count; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 160 | unsigned int cpu_id; |
| 161 | unsigned int flags; |
| 162 | #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 |
| 163 | #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 |
| 164 | } *thread_even, *thread_odd; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 165 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 166 | struct core_data { |
| 167 | unsigned long long c3; |
| 168 | unsigned long long c6; |
| 169 | unsigned long long c7; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 170 | unsigned int core_temp_c; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 171 | unsigned int core_id; |
| 172 | } *core_even, *core_odd; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 173 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 174 | struct pkg_data { |
| 175 | unsigned long long pc2; |
| 176 | unsigned long long pc3; |
| 177 | unsigned long long pc6; |
| 178 | unsigned long long pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 179 | unsigned long long pc8; |
| 180 | unsigned long long pc9; |
| 181 | unsigned long long pc10; |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 182 | unsigned long long pkg_wtd_core_c0; |
| 183 | unsigned long long pkg_any_core_c0; |
| 184 | unsigned long long pkg_any_gfxe_c0; |
| 185 | unsigned long long pkg_both_core_gfxe_c0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 186 | unsigned int package_id; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 187 | unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ |
| 188 | unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ |
| 189 | unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ |
| 190 | unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ |
| 191 | unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ |
| 192 | unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ |
| 193 | unsigned int pkg_temp_c; |
| 194 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 195 | } *package_even, *package_odd; |
| 196 | |
| 197 | #define ODD_COUNTERS thread_odd, core_odd, package_odd |
| 198 | #define EVEN_COUNTERS thread_even, core_even, package_even |
| 199 | |
| 200 | #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ |
| 201 | (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ |
| 202 | topo.num_threads_per_core + \ |
| 203 | (core_no) * topo.num_threads_per_core + (thread_no)) |
| 204 | #define GET_CORE(core_base, core_no, pkg_no) \ |
| 205 | (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) |
| 206 | #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) |
| 207 | |
| 208 | struct system_summary { |
| 209 | struct thread_data threads; |
| 210 | struct core_data cores; |
| 211 | struct pkg_data packages; |
| 212 | } sum, average; |
| 213 | |
| 214 | |
| 215 | struct topo_params { |
| 216 | int num_packages; |
| 217 | int num_cpus; |
| 218 | int num_cores; |
| 219 | int max_cpu_num; |
| 220 | int num_cores_per_pkg; |
| 221 | int num_threads_per_core; |
| 222 | } topo; |
| 223 | |
| 224 | struct timeval tv_even, tv_odd, tv_delta; |
| 225 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 226 | int *irq_column_2_cpu; /* /proc/interrupts column numbers */ |
| 227 | int *irqs_per_cpu; /* indexed by cpu_num */ |
| 228 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 229 | void setup_all_buffers(void); |
| 230 | |
| 231 | int cpu_is_not_present(int cpu) |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 232 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 233 | return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 234 | } |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 235 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 236 | * run func(thread, core, package) in topology order |
| 237 | * skip non-present cpus |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 238 | */ |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 239 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 240 | int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), |
| 241 | struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 242 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 243 | int retval, pkg_no, core_no, thread_no; |
| 244 | |
| 245 | for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { |
| 246 | for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { |
| 247 | for (thread_no = 0; thread_no < |
| 248 | topo.num_threads_per_core; ++thread_no) { |
| 249 | struct thread_data *t; |
| 250 | struct core_data *c; |
| 251 | struct pkg_data *p; |
| 252 | |
| 253 | t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); |
| 254 | |
| 255 | if (cpu_is_not_present(t->cpu_id)) |
| 256 | continue; |
| 257 | |
| 258 | c = GET_CORE(core_base, core_no, pkg_no); |
| 259 | p = GET_PKG(pkg_base, pkg_no); |
| 260 | |
| 261 | retval = func(t, c, p); |
| 262 | if (retval) |
| 263 | return retval; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | return 0; |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int cpu_migrate(int cpu) |
| 271 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 272 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
| 273 | CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); |
| 274 | if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 275 | return -1; |
| 276 | else |
| 277 | return 0; |
| 278 | } |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 279 | int get_msr_fd(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 280 | { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 281 | char pathname[32]; |
| 282 | int fd; |
| 283 | |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 284 | fd = fd_percpu[cpu]; |
| 285 | |
| 286 | if (fd) |
| 287 | return fd; |
| 288 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 289 | sprintf(pathname, "/dev/cpu/%d/msr", cpu); |
| 290 | fd = open(pathname, O_RDONLY); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 291 | if (fd < 0) |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 292 | err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 293 | |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 294 | fd_percpu[cpu] = fd; |
| 295 | |
| 296 | return fd; |
| 297 | } |
| 298 | |
| 299 | int get_msr(int cpu, off_t offset, unsigned long long *msr) |
| 300 | { |
| 301 | ssize_t retval; |
| 302 | |
| 303 | retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 304 | |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 305 | if (retval != sizeof *msr) |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 306 | err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 307 | |
| 308 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 311 | /* |
| 312 | * Example Format w/ field column widths: |
| 313 | * |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 314 | * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz IRQ SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt |
| 315 | * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678 |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 316 | */ |
| 317 | |
Len Brown | a829eb4 | 2011-02-10 23:36:34 -0500 | [diff] [blame] | 318 | void print_header(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 319 | { |
| 320 | if (show_pkg) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 321 | outp += sprintf(outp, " Package"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 322 | if (show_core) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 323 | outp += sprintf(outp, " Core"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 324 | if (show_cpu) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 325 | outp += sprintf(outp, " CPU"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 326 | if (has_aperf) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 327 | outp += sprintf(outp, " Avg_MHz"); |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 328 | if (has_aperf) |
Len Brown | 75d2e44 | 2016-02-13 23:41:53 -0500 | [diff] [blame] | 329 | outp += sprintf(outp, " Busy%%"); |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 330 | if (has_aperf) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 331 | outp += sprintf(outp, " Bzy_MHz"); |
| 332 | outp += sprintf(outp, " TSC_MHz"); |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 333 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 334 | if (extra_delta_offset32) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 335 | outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 336 | if (extra_delta_offset64) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 337 | outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 338 | if (extra_msr_offset32) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 339 | outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 340 | if (extra_msr_offset64) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 341 | outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 342 | |
| 343 | if (!debug) |
| 344 | goto done; |
| 345 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 346 | if (do_irq) |
| 347 | outp += sprintf(outp, " IRQ"); |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 348 | if (do_smi) |
| 349 | outp += sprintf(outp, " SMI"); |
| 350 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 351 | if (do_nhm_cstates) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 352 | outp += sprintf(outp, " CPU%%c1"); |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 353 | if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 354 | outp += sprintf(outp, " CPU%%c3"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 355 | if (do_nhm_cstates) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 356 | outp += sprintf(outp, " CPU%%c6"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 357 | if (do_snb_cstates) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 358 | outp += sprintf(outp, " CPU%%c7"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 359 | |
| 360 | if (do_dts) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 361 | outp += sprintf(outp, " CoreTmp"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 362 | if (do_ptm) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 363 | outp += sprintf(outp, " PkgTmp"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 364 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 365 | if (do_skl_residency) { |
| 366 | outp += sprintf(outp, " Totl%%C0"); |
| 367 | outp += sprintf(outp, " Any%%C0"); |
| 368 | outp += sprintf(outp, " GFX%%C0"); |
| 369 | outp += sprintf(outp, " CPUGFX%%"); |
| 370 | } |
| 371 | |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 372 | if (do_pc2) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 373 | outp += sprintf(outp, " Pkg%%pc2"); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 374 | if (do_pc3) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 375 | outp += sprintf(outp, " Pkg%%pc3"); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 376 | if (do_pc6) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 377 | outp += sprintf(outp, " Pkg%%pc6"); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 378 | if (do_pc7) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 379 | outp += sprintf(outp, " Pkg%%pc7"); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 380 | if (do_c8_c9_c10) { |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 381 | outp += sprintf(outp, " Pkg%%pc8"); |
| 382 | outp += sprintf(outp, " Pkg%%pc9"); |
| 383 | outp += sprintf(outp, " Pk%%pc10"); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 384 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 385 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 386 | if (do_rapl && !rapl_joules) { |
| 387 | if (do_rapl & RAPL_PKG) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 388 | outp += sprintf(outp, " PkgWatt"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 389 | if (do_rapl & RAPL_CORES) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 390 | outp += sprintf(outp, " CorWatt"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 391 | if (do_rapl & RAPL_GFX) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 392 | outp += sprintf(outp, " GFXWatt"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 393 | if (do_rapl & RAPL_DRAM) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 394 | outp += sprintf(outp, " RAMWatt"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 395 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 396 | outp += sprintf(outp, " PKG_%%"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 397 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 398 | outp += sprintf(outp, " RAM_%%"); |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 399 | } else if (do_rapl && rapl_joules) { |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 400 | if (do_rapl & RAPL_PKG) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 401 | outp += sprintf(outp, " Pkg_J"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 402 | if (do_rapl & RAPL_CORES) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 403 | outp += sprintf(outp, " Cor_J"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 404 | if (do_rapl & RAPL_GFX) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 405 | outp += sprintf(outp, " GFX_J"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 406 | if (do_rapl & RAPL_DRAM) |
Len Brown | bd6906e | 2015-07-24 10:35:23 -0400 | [diff] [blame] | 407 | outp += sprintf(outp, " RAM_J"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 408 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 409 | outp += sprintf(outp, " PKG_%%"); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 410 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
Len Brown | e7c95ff | 2014-08-14 21:22:13 -0400 | [diff] [blame] | 411 | outp += sprintf(outp, " RAM_%%"); |
| 412 | outp += sprintf(outp, " time"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 413 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 414 | } |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 415 | done: |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 416 | outp += sprintf(outp, "\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 419 | int dump_counters(struct thread_data *t, struct core_data *c, |
| 420 | struct pkg_data *p) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 421 | { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 422 | outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 423 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 424 | if (t) { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 425 | outp += sprintf(outp, "CPU: %d flags 0x%x\n", |
| 426 | t->cpu_id, t->flags); |
| 427 | outp += sprintf(outp, "TSC: %016llX\n", t->tsc); |
| 428 | outp += sprintf(outp, "aperf: %016llX\n", t->aperf); |
| 429 | outp += sprintf(outp, "mperf: %016llX\n", t->mperf); |
| 430 | outp += sprintf(outp, "c1: %016llX\n", t->c1); |
| 431 | outp += sprintf(outp, "msr0x%x: %08llX\n", |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 432 | extra_delta_offset32, t->extra_delta32); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 433 | outp += sprintf(outp, "msr0x%x: %016llX\n", |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 434 | extra_delta_offset64, t->extra_delta64); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 435 | outp += sprintf(outp, "msr0x%x: %08llX\n", |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 436 | extra_msr_offset32, t->extra_msr32); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 437 | outp += sprintf(outp, "msr0x%x: %016llX\n", |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 438 | extra_msr_offset64, t->extra_msr64); |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 439 | if (do_irq) |
| 440 | outp += sprintf(outp, "IRQ: %08X\n", t->irq_count); |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 441 | if (do_smi) |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 442 | outp += sprintf(outp, "SMI: %08X\n", t->smi_count); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 443 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 444 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 445 | if (c) { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 446 | outp += sprintf(outp, "core: %d\n", c->core_id); |
| 447 | outp += sprintf(outp, "c3: %016llX\n", c->c3); |
| 448 | outp += sprintf(outp, "c6: %016llX\n", c->c6); |
| 449 | outp += sprintf(outp, "c7: %016llX\n", c->c7); |
| 450 | outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (p) { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 454 | outp += sprintf(outp, "package: %d\n", p->package_id); |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 455 | |
| 456 | outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); |
| 457 | outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); |
| 458 | outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); |
| 459 | outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); |
| 460 | |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 461 | outp += sprintf(outp, "pc2: %016llX\n", p->pc2); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 462 | if (do_pc3) |
| 463 | outp += sprintf(outp, "pc3: %016llX\n", p->pc3); |
| 464 | if (do_pc6) |
| 465 | outp += sprintf(outp, "pc6: %016llX\n", p->pc6); |
| 466 | if (do_pc7) |
| 467 | outp += sprintf(outp, "pc7: %016llX\n", p->pc7); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 468 | outp += sprintf(outp, "pc8: %016llX\n", p->pc8); |
| 469 | outp += sprintf(outp, "pc9: %016llX\n", p->pc9); |
| 470 | outp += sprintf(outp, "pc10: %016llX\n", p->pc10); |
| 471 | outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); |
| 472 | outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); |
| 473 | outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); |
| 474 | outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); |
| 475 | outp += sprintf(outp, "Throttle PKG: %0X\n", |
| 476 | p->rapl_pkg_perf_status); |
| 477 | outp += sprintf(outp, "Throttle RAM: %0X\n", |
| 478 | p->rapl_dram_perf_status); |
| 479 | outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 480 | } |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 481 | |
| 482 | outp += sprintf(outp, "\n"); |
| 483 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 484 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 487 | /* |
| 488 | * column formatting convention & formats |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 489 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 490 | int format_counters(struct thread_data *t, struct core_data *c, |
| 491 | struct pkg_data *p) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 492 | { |
| 493 | double interval_float; |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 494 | char *fmt8; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 495 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 496 | /* if showing only 1st thread in core and this isn't one, bail out */ |
| 497 | if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 498 | return 0; |
| 499 | |
| 500 | /* if showing only 1st thread in pkg and this isn't one, bail out */ |
| 501 | if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 502 | return 0; |
| 503 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 504 | interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; |
| 505 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 506 | /* topo columns, print blanks on 1st (average) line */ |
| 507 | if (t == &average.threads) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 508 | if (show_pkg) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 509 | outp += sprintf(outp, " -"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 510 | if (show_core) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 511 | outp += sprintf(outp, " -"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 512 | if (show_cpu) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 513 | outp += sprintf(outp, " -"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 514 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 515 | if (show_pkg) { |
| 516 | if (p) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 517 | outp += sprintf(outp, "%8d", p->package_id); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 518 | else |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 519 | outp += sprintf(outp, " -"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 520 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 521 | if (show_core) { |
| 522 | if (c) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 523 | outp += sprintf(outp, "%8d", c->core_id); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 524 | else |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 525 | outp += sprintf(outp, " -"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 526 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 527 | if (show_cpu) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 528 | outp += sprintf(outp, "%8d", t->cpu_id); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 529 | } |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 530 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 531 | /* Avg_MHz */ |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 532 | if (has_aperf) |
| 533 | outp += sprintf(outp, "%8.0f", |
| 534 | 1.0 / units * t->aperf / interval_float); |
| 535 | |
Len Brown | 75d2e44 | 2016-02-13 23:41:53 -0500 | [diff] [blame] | 536 | /* Busy% */ |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 537 | if (has_aperf) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 538 | if (!skip_c0) |
Len Brown | a2b7b74 | 2015-09-26 00:12:38 -0400 | [diff] [blame] | 539 | outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 540 | else |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 541 | outp += sprintf(outp, "********"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 542 | } |
| 543 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 544 | /* Bzy_MHz */ |
Len Brown | 21ed557 | 2015-10-19 22:37:40 -0400 | [diff] [blame] | 545 | if (has_aperf) { |
| 546 | if (has_base_hz) |
| 547 | outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf); |
| 548 | else |
| 549 | outp += sprintf(outp, "%8.0f", |
| 550 | 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float); |
| 551 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 552 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 553 | /* TSC_MHz */ |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 554 | outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 555 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 556 | /* delta */ |
| 557 | if (extra_delta_offset32) |
| 558 | outp += sprintf(outp, " %11llu", t->extra_delta32); |
| 559 | |
| 560 | /* DELTA */ |
| 561 | if (extra_delta_offset64) |
| 562 | outp += sprintf(outp, " %11llu", t->extra_delta64); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 563 | /* msr */ |
| 564 | if (extra_msr_offset32) |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 565 | outp += sprintf(outp, " 0x%08llx", t->extra_msr32); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 566 | |
Len Brown | 130ff30 | 2012-09-21 22:56:06 -0400 | [diff] [blame] | 567 | /* MSR */ |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 568 | if (extra_msr_offset64) |
| 569 | outp += sprintf(outp, " 0x%016llx", t->extra_msr64); |
Len Brown | 130ff30 | 2012-09-21 22:56:06 -0400 | [diff] [blame] | 570 | |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 571 | if (!debug) |
| 572 | goto done; |
| 573 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 574 | /* IRQ */ |
| 575 | if (do_irq) |
| 576 | outp += sprintf(outp, "%8d", t->irq_count); |
| 577 | |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 578 | /* SMI */ |
| 579 | if (do_smi) |
| 580 | outp += sprintf(outp, "%8d", t->smi_count); |
| 581 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 582 | if (do_nhm_cstates) { |
| 583 | if (!skip_c1) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 584 | outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 585 | else |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 586 | outp += sprintf(outp, "********"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 587 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 588 | |
| 589 | /* print per-core data only for 1st thread in core */ |
| 590 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 591 | goto done; |
| 592 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 593 | if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 594 | outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 595 | if (do_nhm_cstates) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 596 | outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 597 | if (do_snb_cstates) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 598 | outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 599 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 600 | if (do_dts) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 601 | outp += sprintf(outp, "%8d", c->core_temp_c); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 602 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 603 | /* print per-package data only for 1st core in package */ |
| 604 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 605 | goto done; |
| 606 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 607 | /* PkgTmp */ |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 608 | if (do_ptm) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 609 | outp += sprintf(outp, "%8d", p->pkg_temp_c); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 610 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 611 | /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ |
| 612 | if (do_skl_residency) { |
| 613 | outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc); |
| 614 | outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc); |
| 615 | outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc); |
| 616 | outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc); |
| 617 | } |
| 618 | |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 619 | if (do_pc2) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 620 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 621 | if (do_pc3) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 622 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 623 | if (do_pc6) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 624 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 625 | if (do_pc7) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 626 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 627 | if (do_c8_c9_c10) { |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 628 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc); |
| 629 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc); |
| 630 | outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 631 | } |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * If measurement interval exceeds minimum RAPL Joule Counter range, |
| 635 | * indicate that results are suspect by printing "**" in fraction place. |
| 636 | */ |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 637 | if (interval_float < rapl_joule_counter_range) |
| 638 | fmt8 = "%8.2f"; |
| 639 | else |
| 640 | fmt8 = " %6.0f**"; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 641 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 642 | if (do_rapl && !rapl_joules) { |
| 643 | if (do_rapl & RAPL_PKG) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 644 | outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 645 | if (do_rapl & RAPL_CORES) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 646 | outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 647 | if (do_rapl & RAPL_GFX) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 648 | outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 649 | if (do_rapl & RAPL_DRAM) |
Andrey Semin | 40ee8e3 | 2014-12-05 00:07:00 -0500 | [diff] [blame] | 650 | outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 651 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 652 | outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 653 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 654 | outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 655 | } else if (do_rapl && rapl_joules) { |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 656 | if (do_rapl & RAPL_PKG) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 657 | outp += sprintf(outp, fmt8, |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 658 | p->energy_pkg * rapl_energy_units); |
| 659 | if (do_rapl & RAPL_CORES) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 660 | outp += sprintf(outp, fmt8, |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 661 | p->energy_cores * rapl_energy_units); |
| 662 | if (do_rapl & RAPL_GFX) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 663 | outp += sprintf(outp, fmt8, |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 664 | p->energy_gfx * rapl_energy_units); |
| 665 | if (do_rapl & RAPL_DRAM) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 666 | outp += sprintf(outp, fmt8, |
Andrey Semin | 40ee8e3 | 2014-12-05 00:07:00 -0500 | [diff] [blame] | 667 | p->energy_dram * rapl_dram_energy_units); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 668 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 669 | outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 670 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
Len Brown | fc04cc6 | 2014-02-06 00:55:19 -0500 | [diff] [blame] | 671 | outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 672 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 673 | outp += sprintf(outp, fmt8, interval_float); |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 674 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 675 | done: |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 676 | outp += sprintf(outp, "\n"); |
| 677 | |
| 678 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 679 | } |
| 680 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 681 | void flush_output_stdout(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 682 | { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 683 | FILE *filep; |
| 684 | |
| 685 | if (outf == stderr) |
| 686 | filep = stdout; |
| 687 | else |
| 688 | filep = outf; |
| 689 | |
| 690 | fputs(output_buffer, filep); |
| 691 | fflush(filep); |
| 692 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 693 | outp = output_buffer; |
| 694 | } |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 695 | void flush_output_stderr(void) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 696 | { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 697 | fputs(output_buffer, outf); |
| 698 | fflush(outf); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 699 | outp = output_buffer; |
| 700 | } |
| 701 | void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 702 | { |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 703 | static int printed; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 704 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 705 | if (!printed || !summary_only) |
| 706 | print_header(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 707 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 708 | if (topo.num_cpus > 1) |
| 709 | format_counters(&average.threads, &average.cores, |
| 710 | &average.packages); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 711 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 712 | printed = 1; |
| 713 | |
| 714 | if (summary_only) |
| 715 | return; |
| 716 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 717 | for_all_cpus(format_counters, t, c, p); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 720 | #define DELTA_WRAP32(new, old) \ |
| 721 | if (new > old) { \ |
| 722 | old = new - old; \ |
| 723 | } else { \ |
| 724 | old = 0x100000000 + new - old; \ |
| 725 | } |
| 726 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 727 | void |
| 728 | delta_package(struct pkg_data *new, struct pkg_data *old) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 729 | { |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 730 | |
| 731 | if (do_skl_residency) { |
| 732 | old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; |
| 733 | old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; |
| 734 | old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; |
| 735 | old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; |
| 736 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 737 | old->pc2 = new->pc2 - old->pc2; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 738 | if (do_pc3) |
| 739 | old->pc3 = new->pc3 - old->pc3; |
| 740 | if (do_pc6) |
| 741 | old->pc6 = new->pc6 - old->pc6; |
| 742 | if (do_pc7) |
| 743 | old->pc7 = new->pc7 - old->pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 744 | old->pc8 = new->pc8 - old->pc8; |
| 745 | old->pc9 = new->pc9 - old->pc9; |
| 746 | old->pc10 = new->pc10 - old->pc10; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 747 | old->pkg_temp_c = new->pkg_temp_c; |
| 748 | |
| 749 | DELTA_WRAP32(new->energy_pkg, old->energy_pkg); |
| 750 | DELTA_WRAP32(new->energy_cores, old->energy_cores); |
| 751 | DELTA_WRAP32(new->energy_gfx, old->energy_gfx); |
| 752 | DELTA_WRAP32(new->energy_dram, old->energy_dram); |
| 753 | DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); |
| 754 | DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 755 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 756 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 757 | void |
| 758 | delta_core(struct core_data *new, struct core_data *old) |
| 759 | { |
| 760 | old->c3 = new->c3 - old->c3; |
| 761 | old->c6 = new->c6 - old->c6; |
| 762 | old->c7 = new->c7 - old->c7; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 763 | old->core_temp_c = new->core_temp_c; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 764 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 765 | |
Len Brown | c3ae331 | 2012-06-13 21:31:46 -0400 | [diff] [blame] | 766 | /* |
| 767 | * old = new - old |
| 768 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 769 | void |
| 770 | delta_thread(struct thread_data *new, struct thread_data *old, |
| 771 | struct core_data *core_delta) |
| 772 | { |
| 773 | old->tsc = new->tsc - old->tsc; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 774 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 775 | /* check for TSC < 1 Mcycles over interval */ |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 776 | if (old->tsc < (1000 * 1000)) |
| 777 | errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" |
| 778 | "You can disable all c-states by booting with \"idle=poll\"\n" |
| 779 | "or just the deep ones with \"processor.max_cstate=1\""); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 780 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 781 | old->c1 = new->c1 - old->c1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 782 | |
Len Brown | a729617 | 2015-01-23 01:33:58 -0500 | [diff] [blame] | 783 | if (has_aperf) { |
| 784 | if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { |
| 785 | old->aperf = new->aperf - old->aperf; |
| 786 | old->mperf = new->mperf - old->mperf; |
| 787 | } else { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 788 | |
Len Brown | a729617 | 2015-01-23 01:33:58 -0500 | [diff] [blame] | 789 | if (!aperf_mperf_unstable) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 790 | fprintf(outf, "%s: APERF or MPERF went backwards *\n", progname); |
| 791 | fprintf(outf, "* Frequency results do not cover entire interval *\n"); |
| 792 | fprintf(outf, "* fix this by running Linux-2.6.30 or later *\n"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 793 | |
Len Brown | a729617 | 2015-01-23 01:33:58 -0500 | [diff] [blame] | 794 | aperf_mperf_unstable = 1; |
| 795 | } |
| 796 | /* |
| 797 | * mperf delta is likely a huge "positive" number |
| 798 | * can not use it for calculating c0 time |
| 799 | */ |
| 800 | skip_c0 = 1; |
| 801 | skip_c1 = 1; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 802 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 803 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 804 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 805 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 806 | if (use_c1_residency_msr) { |
| 807 | /* |
| 808 | * Some models have a dedicated C1 residency MSR, |
| 809 | * which should be more accurate than the derivation below. |
| 810 | */ |
| 811 | } else { |
| 812 | /* |
| 813 | * As counter collection is not atomic, |
| 814 | * it is possible for mperf's non-halted cycles + idle states |
| 815 | * to exceed TSC's all cycles: show c1 = 0% in that case. |
| 816 | */ |
| 817 | if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) |
| 818 | old->c1 = 0; |
| 819 | else { |
| 820 | /* normal case, derive c1 */ |
| 821 | old->c1 = old->tsc - old->mperf - core_delta->c3 |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 822 | - core_delta->c6 - core_delta->c7; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 823 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 824 | } |
Len Brown | c3ae331 | 2012-06-13 21:31:46 -0400 | [diff] [blame] | 825 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 826 | if (old->mperf == 0) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 827 | if (debug > 1) |
| 828 | fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 829 | old->mperf = 1; /* divide by 0 protection */ |
| 830 | } |
| 831 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 832 | old->extra_delta32 = new->extra_delta32 - old->extra_delta32; |
| 833 | old->extra_delta32 &= 0xFFFFFFFF; |
| 834 | |
| 835 | old->extra_delta64 = new->extra_delta64 - old->extra_delta64; |
| 836 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 837 | /* |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 838 | * Extra MSR is just a snapshot, simply copy latest w/o subtracting |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 839 | */ |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 840 | old->extra_msr32 = new->extra_msr32; |
| 841 | old->extra_msr64 = new->extra_msr64; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 842 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 843 | if (do_irq) |
| 844 | old->irq_count = new->irq_count - old->irq_count; |
| 845 | |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 846 | if (do_smi) |
| 847 | old->smi_count = new->smi_count - old->smi_count; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | int delta_cpu(struct thread_data *t, struct core_data *c, |
| 851 | struct pkg_data *p, struct thread_data *t2, |
| 852 | struct core_data *c2, struct pkg_data *p2) |
| 853 | { |
| 854 | /* calculate core delta only for 1st thread in core */ |
| 855 | if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) |
| 856 | delta_core(c, c2); |
| 857 | |
| 858 | /* always calculate thread delta */ |
| 859 | delta_thread(t, t2, c2); /* c2 is core delta */ |
| 860 | |
| 861 | /* calculate package delta only for 1st core in package */ |
| 862 | if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) |
| 863 | delta_package(p, p2); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 869 | { |
| 870 | t->tsc = 0; |
| 871 | t->aperf = 0; |
| 872 | t->mperf = 0; |
| 873 | t->c1 = 0; |
| 874 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 875 | t->extra_delta32 = 0; |
| 876 | t->extra_delta64 = 0; |
| 877 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 878 | t->irq_count = 0; |
| 879 | t->smi_count = 0; |
| 880 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 881 | /* tells format_counters to dump all fields from this set */ |
| 882 | t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; |
| 883 | |
| 884 | c->c3 = 0; |
| 885 | c->c6 = 0; |
| 886 | c->c7 = 0; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 887 | c->core_temp_c = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 888 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 889 | p->pkg_wtd_core_c0 = 0; |
| 890 | p->pkg_any_core_c0 = 0; |
| 891 | p->pkg_any_gfxe_c0 = 0; |
| 892 | p->pkg_both_core_gfxe_c0 = 0; |
| 893 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 894 | p->pc2 = 0; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 895 | if (do_pc3) |
| 896 | p->pc3 = 0; |
| 897 | if (do_pc6) |
| 898 | p->pc6 = 0; |
| 899 | if (do_pc7) |
| 900 | p->pc7 = 0; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 901 | p->pc8 = 0; |
| 902 | p->pc9 = 0; |
| 903 | p->pc10 = 0; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 904 | |
| 905 | p->energy_pkg = 0; |
| 906 | p->energy_dram = 0; |
| 907 | p->energy_cores = 0; |
| 908 | p->energy_gfx = 0; |
| 909 | p->rapl_pkg_perf_status = 0; |
| 910 | p->rapl_dram_perf_status = 0; |
| 911 | p->pkg_temp_c = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 912 | } |
| 913 | int sum_counters(struct thread_data *t, struct core_data *c, |
| 914 | struct pkg_data *p) |
| 915 | { |
| 916 | average.threads.tsc += t->tsc; |
| 917 | average.threads.aperf += t->aperf; |
| 918 | average.threads.mperf += t->mperf; |
| 919 | average.threads.c1 += t->c1; |
| 920 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 921 | average.threads.extra_delta32 += t->extra_delta32; |
| 922 | average.threads.extra_delta64 += t->extra_delta64; |
| 923 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 924 | average.threads.irq_count += t->irq_count; |
| 925 | average.threads.smi_count += t->smi_count; |
| 926 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 927 | /* sum per-core values only for 1st thread in core */ |
| 928 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 929 | return 0; |
| 930 | |
| 931 | average.cores.c3 += c->c3; |
| 932 | average.cores.c6 += c->c6; |
| 933 | average.cores.c7 += c->c7; |
| 934 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 935 | average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); |
| 936 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 937 | /* sum per-pkg values only for 1st core in pkg */ |
| 938 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 939 | return 0; |
| 940 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 941 | if (do_skl_residency) { |
| 942 | average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; |
| 943 | average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; |
| 944 | average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; |
| 945 | average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; |
| 946 | } |
| 947 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 948 | average.packages.pc2 += p->pc2; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 949 | if (do_pc3) |
| 950 | average.packages.pc3 += p->pc3; |
| 951 | if (do_pc6) |
| 952 | average.packages.pc6 += p->pc6; |
| 953 | if (do_pc7) |
| 954 | average.packages.pc7 += p->pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 955 | average.packages.pc8 += p->pc8; |
| 956 | average.packages.pc9 += p->pc9; |
| 957 | average.packages.pc10 += p->pc10; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 958 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 959 | average.packages.energy_pkg += p->energy_pkg; |
| 960 | average.packages.energy_dram += p->energy_dram; |
| 961 | average.packages.energy_cores += p->energy_cores; |
| 962 | average.packages.energy_gfx += p->energy_gfx; |
| 963 | |
| 964 | average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); |
| 965 | |
| 966 | average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; |
| 967 | average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | /* |
| 971 | * sum the counters for all cpus in the system |
| 972 | * compute the weighted average |
| 973 | */ |
| 974 | void compute_average(struct thread_data *t, struct core_data *c, |
| 975 | struct pkg_data *p) |
| 976 | { |
| 977 | clear_counters(&average.threads, &average.cores, &average.packages); |
| 978 | |
| 979 | for_all_cpus(sum_counters, t, c, p); |
| 980 | |
| 981 | average.threads.tsc /= topo.num_cpus; |
| 982 | average.threads.aperf /= topo.num_cpus; |
| 983 | average.threads.mperf /= topo.num_cpus; |
| 984 | average.threads.c1 /= topo.num_cpus; |
| 985 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 986 | average.threads.extra_delta32 /= topo.num_cpus; |
| 987 | average.threads.extra_delta32 &= 0xFFFFFFFF; |
| 988 | |
| 989 | average.threads.extra_delta64 /= topo.num_cpus; |
| 990 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 991 | average.cores.c3 /= topo.num_cores; |
| 992 | average.cores.c6 /= topo.num_cores; |
| 993 | average.cores.c7 /= topo.num_cores; |
| 994 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 995 | if (do_skl_residency) { |
| 996 | average.packages.pkg_wtd_core_c0 /= topo.num_packages; |
| 997 | average.packages.pkg_any_core_c0 /= topo.num_packages; |
| 998 | average.packages.pkg_any_gfxe_c0 /= topo.num_packages; |
| 999 | average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; |
| 1000 | } |
| 1001 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1002 | average.packages.pc2 /= topo.num_packages; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1003 | if (do_pc3) |
| 1004 | average.packages.pc3 /= topo.num_packages; |
| 1005 | if (do_pc6) |
| 1006 | average.packages.pc6 /= topo.num_packages; |
| 1007 | if (do_pc7) |
| 1008 | average.packages.pc7 /= topo.num_packages; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 1009 | |
| 1010 | average.packages.pc8 /= topo.num_packages; |
| 1011 | average.packages.pc9 /= topo.num_packages; |
| 1012 | average.packages.pc10 /= topo.num_packages; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | static unsigned long long rdtsc(void) |
| 1016 | { |
| 1017 | unsigned int low, high; |
| 1018 | |
| 1019 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
| 1020 | |
| 1021 | return low | ((unsigned long long)high) << 32; |
| 1022 | } |
| 1023 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1024 | /* |
| 1025 | * get_counters(...) |
| 1026 | * migrate to cpu |
| 1027 | * acquire and record local counters for that cpu |
| 1028 | */ |
| 1029 | int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 1030 | { |
| 1031 | int cpu = t->cpu_id; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1032 | unsigned long long msr; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1033 | |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1034 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1035 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1036 | return -1; |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1037 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1038 | |
| 1039 | t->tsc = rdtsc(); /* we are running on local CPU of interest */ |
| 1040 | |
| 1041 | if (has_aperf) { |
Len Brown | 9c63a65 | 2012-10-31 01:29:52 -0400 | [diff] [blame] | 1042 | if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1043 | return -3; |
Len Brown | 9c63a65 | 2012-10-31 01:29:52 -0400 | [diff] [blame] | 1044 | if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1045 | return -4; |
Hubert Chrzaniuk | b2b34df | 2015-09-14 13:31:00 +0200 | [diff] [blame] | 1046 | t->aperf = t->aperf * aperf_mperf_multiplier; |
| 1047 | t->mperf = t->mperf * aperf_mperf_multiplier; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 1050 | if (do_irq) |
| 1051 | t->irq_count = irqs_per_cpu[cpu]; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 1052 | if (do_smi) { |
| 1053 | if (get_msr(cpu, MSR_SMI_COUNT, &msr)) |
| 1054 | return -5; |
| 1055 | t->smi_count = msr & 0xFFFFFFFF; |
| 1056 | } |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 1057 | if (extra_delta_offset32) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1058 | if (get_msr(cpu, extra_delta_offset32, &msr)) |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 1059 | return -5; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1060 | t->extra_delta32 = msr & 0xFFFFFFFF; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | if (extra_delta_offset64) |
| 1064 | if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) |
| 1065 | return -5; |
| 1066 | |
| 1067 | if (extra_msr_offset32) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1068 | if (get_msr(cpu, extra_msr_offset32, &msr)) |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 1069 | return -5; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1070 | t->extra_msr32 = msr & 0xFFFFFFFF; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 1071 | } |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 1072 | |
| 1073 | if (extra_msr_offset64) |
| 1074 | if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1075 | return -5; |
| 1076 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1077 | if (use_c1_residency_msr) { |
| 1078 | if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) |
| 1079 | return -6; |
| 1080 | } |
| 1081 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1082 | /* collect core counters only for 1st thread in core */ |
| 1083 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 1084 | return 0; |
| 1085 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1086 | if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1087 | if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) |
| 1088 | return -6; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1091 | if (do_nhm_cstates && !do_knl_cstates) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1092 | if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) |
| 1093 | return -7; |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1094 | } else if (do_knl_cstates) { |
| 1095 | if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) |
| 1096 | return -7; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | if (do_snb_cstates) |
| 1100 | if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) |
| 1101 | return -8; |
| 1102 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1103 | if (do_dts) { |
| 1104 | if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) |
| 1105 | return -9; |
| 1106 | c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); |
| 1107 | } |
| 1108 | |
| 1109 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1110 | /* collect package counters only for 1st core in package */ |
| 1111 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 1112 | return 0; |
| 1113 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 1114 | if (do_skl_residency) { |
| 1115 | if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) |
| 1116 | return -10; |
| 1117 | if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) |
| 1118 | return -11; |
| 1119 | if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) |
| 1120 | return -12; |
| 1121 | if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) |
| 1122 | return -13; |
| 1123 | } |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1124 | if (do_pc3) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1125 | if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) |
| 1126 | return -9; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1127 | if (do_pc6) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1128 | if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) |
| 1129 | return -10; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1130 | if (do_pc2) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1131 | if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) |
| 1132 | return -11; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1133 | if (do_pc7) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1134 | if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) |
| 1135 | return -12; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 1136 | if (do_c8_c9_c10) { |
| 1137 | if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) |
| 1138 | return -13; |
| 1139 | if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) |
| 1140 | return -13; |
| 1141 | if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) |
| 1142 | return -13; |
| 1143 | } |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1144 | if (do_rapl & RAPL_PKG) { |
| 1145 | if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) |
| 1146 | return -13; |
| 1147 | p->energy_pkg = msr & 0xFFFFFFFF; |
| 1148 | } |
| 1149 | if (do_rapl & RAPL_CORES) { |
| 1150 | if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) |
| 1151 | return -14; |
| 1152 | p->energy_cores = msr & 0xFFFFFFFF; |
| 1153 | } |
| 1154 | if (do_rapl & RAPL_DRAM) { |
| 1155 | if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) |
| 1156 | return -15; |
| 1157 | p->energy_dram = msr & 0xFFFFFFFF; |
| 1158 | } |
| 1159 | if (do_rapl & RAPL_GFX) { |
| 1160 | if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) |
| 1161 | return -16; |
| 1162 | p->energy_gfx = msr & 0xFFFFFFFF; |
| 1163 | } |
| 1164 | if (do_rapl & RAPL_PKG_PERF_STATUS) { |
| 1165 | if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) |
| 1166 | return -16; |
| 1167 | p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; |
| 1168 | } |
| 1169 | if (do_rapl & RAPL_DRAM_PERF_STATUS) { |
| 1170 | if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) |
| 1171 | return -16; |
| 1172 | p->rapl_dram_perf_status = msr & 0xFFFFFFFF; |
| 1173 | } |
| 1174 | if (do_ptm) { |
| 1175 | if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) |
| 1176 | return -17; |
| 1177 | p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); |
| 1178 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1179 | return 0; |
| 1180 | } |
| 1181 | |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1182 | /* |
| 1183 | * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: |
| 1184 | * If you change the values, note they are used both in comparisons |
| 1185 | * (>= PCL__7) and to index pkg_cstate_limit_strings[]. |
| 1186 | */ |
| 1187 | |
| 1188 | #define PCLUKN 0 /* Unknown */ |
| 1189 | #define PCLRSV 1 /* Reserved */ |
| 1190 | #define PCL__0 2 /* PC0 */ |
| 1191 | #define PCL__1 3 /* PC1 */ |
| 1192 | #define PCL__2 4 /* PC2 */ |
| 1193 | #define PCL__3 5 /* PC3 */ |
| 1194 | #define PCL__4 6 /* PC4 */ |
| 1195 | #define PCL__6 7 /* PC6 */ |
| 1196 | #define PCL_6N 8 /* PC6 No Retention */ |
| 1197 | #define PCL_6R 9 /* PC6 Retention */ |
| 1198 | #define PCL__7 10 /* PC7 */ |
| 1199 | #define PCL_7S 11 /* PC7 Shrink */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 1200 | #define PCL__8 12 /* PC8 */ |
| 1201 | #define PCL__9 13 /* PC9 */ |
| 1202 | #define PCLUNL 14 /* Unlimited */ |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1203 | |
| 1204 | int pkg_cstate_limit = PCLUKN; |
| 1205 | char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 1206 | "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1207 | |
Len Brown | e9257f5 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 1208 | int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; |
| 1209 | int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; |
| 1210 | int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; |
| 1211 | int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; |
| 1212 | int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; |
| 1213 | int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1214 | |
Len Brown | a2b7b74 | 2015-09-26 00:12:38 -0400 | [diff] [blame] | 1215 | |
| 1216 | static void |
| 1217 | calculate_tsc_tweak() |
| 1218 | { |
Len Brown | a2b7b74 | 2015-09-26 00:12:38 -0400 | [diff] [blame] | 1219 | tsc_tweak = base_hz / tsc_hz; |
| 1220 | } |
| 1221 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1222 | static void |
| 1223 | dump_nhm_platform_info(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1224 | { |
| 1225 | unsigned long long msr; |
| 1226 | unsigned int ratio; |
| 1227 | |
Len Brown | ec0adc5 | 2015-11-12 02:42:31 -0500 | [diff] [blame] | 1228 | get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1229 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1230 | fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1231 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1232 | ratio = (msr >> 40) & 0xFF; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1233 | fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1234 | ratio, bclk, ratio * bclk); |
| 1235 | |
| 1236 | ratio = (msr >> 8) & 0xFF; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1237 | fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1238 | ratio, bclk, ratio * bclk); |
| 1239 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1240 | get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1241 | fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", |
Len Brown | bfae205 | 2015-06-17 12:27:21 -0400 | [diff] [blame] | 1242 | base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); |
Len Brown | 6792041 | 2013-01-31 15:22:15 -0500 | [diff] [blame] | 1243 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | static void |
| 1248 | dump_hsw_turbo_ratio_limits(void) |
| 1249 | { |
| 1250 | unsigned long long msr; |
| 1251 | unsigned int ratio; |
| 1252 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1253 | get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1254 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1255 | fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1256 | |
| 1257 | ratio = (msr >> 8) & 0xFF; |
| 1258 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1259 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n", |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1260 | ratio, bclk, ratio * bclk); |
| 1261 | |
| 1262 | ratio = (msr >> 0) & 0xFF; |
| 1263 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1264 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n", |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1265 | ratio, bclk, ratio * bclk); |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | static void |
| 1270 | dump_ivt_turbo_ratio_limits(void) |
| 1271 | { |
| 1272 | unsigned long long msr; |
| 1273 | unsigned int ratio; |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1274 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1275 | get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1276 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1277 | fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1278 | |
| 1279 | ratio = (msr >> 56) & 0xFF; |
| 1280 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1281 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1282 | ratio, bclk, ratio * bclk); |
| 1283 | |
| 1284 | ratio = (msr >> 48) & 0xFF; |
| 1285 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1286 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1287 | ratio, bclk, ratio * bclk); |
| 1288 | |
| 1289 | ratio = (msr >> 40) & 0xFF; |
| 1290 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1291 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1292 | ratio, bclk, ratio * bclk); |
| 1293 | |
| 1294 | ratio = (msr >> 32) & 0xFF; |
| 1295 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1296 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1297 | ratio, bclk, ratio * bclk); |
| 1298 | |
| 1299 | ratio = (msr >> 24) & 0xFF; |
| 1300 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1301 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1302 | ratio, bclk, ratio * bclk); |
| 1303 | |
| 1304 | ratio = (msr >> 16) & 0xFF; |
| 1305 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1306 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1307 | ratio, bclk, ratio * bclk); |
| 1308 | |
| 1309 | ratio = (msr >> 8) & 0xFF; |
| 1310 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1311 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1312 | ratio, bclk, ratio * bclk); |
| 1313 | |
| 1314 | ratio = (msr >> 0) & 0xFF; |
| 1315 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1316 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1317 | ratio, bclk, ratio * bclk); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1318 | return; |
| 1319 | } |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1320 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1321 | static void |
| 1322 | dump_nhm_turbo_ratio_limits(void) |
| 1323 | { |
| 1324 | unsigned long long msr; |
| 1325 | unsigned int ratio; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1326 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1327 | get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1328 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1329 | fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1330 | |
| 1331 | ratio = (msr >> 56) & 0xFF; |
| 1332 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1333 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1334 | ratio, bclk, ratio * bclk); |
| 1335 | |
| 1336 | ratio = (msr >> 48) & 0xFF; |
| 1337 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1338 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1339 | ratio, bclk, ratio * bclk); |
| 1340 | |
| 1341 | ratio = (msr >> 40) & 0xFF; |
| 1342 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1343 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1344 | ratio, bclk, ratio * bclk); |
| 1345 | |
| 1346 | ratio = (msr >> 32) & 0xFF; |
| 1347 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1348 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1349 | ratio, bclk, ratio * bclk); |
| 1350 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1351 | ratio = (msr >> 24) & 0xFF; |
| 1352 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1353 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1354 | ratio, bclk, ratio * bclk); |
| 1355 | |
| 1356 | ratio = (msr >> 16) & 0xFF; |
| 1357 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1358 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1359 | ratio, bclk, ratio * bclk); |
| 1360 | |
| 1361 | ratio = (msr >> 8) & 0xFF; |
| 1362 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1363 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1364 | ratio, bclk, ratio * bclk); |
| 1365 | |
| 1366 | ratio = (msr >> 0) & 0xFF; |
| 1367 | if (ratio) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1368 | fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1369 | ratio, bclk, ratio * bclk); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1370 | return; |
| 1371 | } |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 1372 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1373 | static void |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1374 | dump_knl_turbo_ratio_limits(void) |
| 1375 | { |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1376 | const unsigned int buckets_no = 7; |
| 1377 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1378 | unsigned long long msr; |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1379 | int delta_cores, delta_ratio; |
| 1380 | int i, b_nr; |
| 1381 | unsigned int cores[buckets_no]; |
| 1382 | unsigned int ratio[buckets_no]; |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1383 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1384 | get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr); |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1385 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1386 | fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", |
Len Brown | bfae205 | 2015-06-17 12:27:21 -0400 | [diff] [blame] | 1387 | base_cpu, msr); |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1388 | |
| 1389 | /** |
| 1390 | * Turbo encoding in KNL is as follows: |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1391 | * [0] -- Reserved |
| 1392 | * [7:1] -- Base value of number of active cores of bucket 1. |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1393 | * [15:8] -- Base value of freq ratio of bucket 1. |
| 1394 | * [20:16] -- +ve delta of number of active cores of bucket 2. |
| 1395 | * i.e. active cores of bucket 2 = |
| 1396 | * active cores of bucket 1 + delta |
| 1397 | * [23:21] -- Negative delta of freq ratio of bucket 2. |
| 1398 | * i.e. freq ratio of bucket 2 = |
| 1399 | * freq ratio of bucket 1 - delta |
| 1400 | * [28:24]-- +ve delta of number of active cores of bucket 3. |
| 1401 | * [31:29]-- -ve delta of freq ratio of bucket 3. |
| 1402 | * [36:32]-- +ve delta of number of active cores of bucket 4. |
| 1403 | * [39:37]-- -ve delta of freq ratio of bucket 4. |
| 1404 | * [44:40]-- +ve delta of number of active cores of bucket 5. |
| 1405 | * [47:45]-- -ve delta of freq ratio of bucket 5. |
| 1406 | * [52:48]-- +ve delta of number of active cores of bucket 6. |
| 1407 | * [55:53]-- -ve delta of freq ratio of bucket 6. |
| 1408 | * [60:56]-- +ve delta of number of active cores of bucket 7. |
| 1409 | * [63:61]-- -ve delta of freq ratio of bucket 7. |
| 1410 | */ |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1411 | |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1412 | b_nr = 0; |
| 1413 | cores[b_nr] = (msr & 0xFF) >> 1; |
| 1414 | ratio[b_nr] = (msr >> 8) & 0xFF; |
| 1415 | |
| 1416 | for (i = 16; i < 64; i += 8) { |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1417 | delta_cores = (msr >> i) & 0x1F; |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1418 | delta_ratio = (msr >> (i + 5)) & 0x7; |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1419 | |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1420 | cores[b_nr + 1] = cores[b_nr] + delta_cores; |
| 1421 | ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; |
| 1422 | b_nr++; |
| 1423 | } |
| 1424 | |
| 1425 | for (i = buckets_no - 1; i >= 0; i--) |
| 1426 | if (i > 0 ? ratio[i] != ratio[i - 1] : 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1427 | fprintf(outf, |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1428 | "%d * %.0f = %.0f MHz max turbo %d active cores\n", |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 1429 | ratio[i], bclk, ratio[i] * bclk, cores[i]); |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | static void |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1433 | dump_nhm_cst_cfg(void) |
| 1434 | { |
| 1435 | unsigned long long msr; |
| 1436 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1437 | get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1438 | |
| 1439 | #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) |
| 1440 | #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) |
| 1441 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1442 | fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1443 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1444 | fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 1445 | (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", |
| 1446 | (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", |
| 1447 | (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", |
| 1448 | (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", |
| 1449 | (msr & (1 << 15)) ? "" : "UN", |
| 1450 | (unsigned int)msr & 7, |
| 1451 | pkg_cstate_limit_strings[pkg_cstate_limit]); |
| 1452 | return; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1453 | } |
| 1454 | |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1455 | static void |
| 1456 | dump_config_tdp(void) |
| 1457 | { |
| 1458 | unsigned long long msr; |
| 1459 | |
| 1460 | get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1461 | fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); |
| 1462 | fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1463 | |
| 1464 | get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1465 | fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1466 | if (msr) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1467 | fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF); |
| 1468 | fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF); |
| 1469 | fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF); |
| 1470 | fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1471 | } |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1472 | fprintf(outf, ")\n"); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1473 | |
| 1474 | get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1475 | fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1476 | if (msr) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1477 | fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF); |
| 1478 | fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF); |
| 1479 | fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF); |
| 1480 | fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1481 | } |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1482 | fprintf(outf, ")\n"); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1483 | |
| 1484 | get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1485 | fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1486 | if ((msr) & 0x3) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1487 | fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); |
| 1488 | fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); |
| 1489 | fprintf(outf, ")\n"); |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 1490 | |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1491 | get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1492 | fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); |
| 1493 | fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F); |
| 1494 | fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); |
| 1495 | fprintf(outf, ")\n"); |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1496 | } |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 1497 | void free_fd_percpu(void) |
| 1498 | { |
| 1499 | int i; |
| 1500 | |
| 1501 | for (i = 0; i < topo.max_cpu_num; ++i) { |
| 1502 | if (fd_percpu[i] != 0) |
| 1503 | close(fd_percpu[i]); |
| 1504 | } |
| 1505 | |
| 1506 | free(fd_percpu); |
| 1507 | } |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 1508 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1509 | void free_all_buffers(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1510 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1511 | CPU_FREE(cpu_present_set); |
| 1512 | cpu_present_set = NULL; |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 1513 | cpu_present_setsize = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1514 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1515 | CPU_FREE(cpu_affinity_set); |
| 1516 | cpu_affinity_set = NULL; |
| 1517 | cpu_affinity_setsize = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1518 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1519 | free(thread_even); |
| 1520 | free(core_even); |
| 1521 | free(package_even); |
| 1522 | |
| 1523 | thread_even = NULL; |
| 1524 | core_even = NULL; |
| 1525 | package_even = NULL; |
| 1526 | |
| 1527 | free(thread_odd); |
| 1528 | free(core_odd); |
| 1529 | free(package_odd); |
| 1530 | |
| 1531 | thread_odd = NULL; |
| 1532 | core_odd = NULL; |
| 1533 | package_odd = NULL; |
| 1534 | |
| 1535 | free(output_buffer); |
| 1536 | output_buffer = NULL; |
| 1537 | outp = NULL; |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 1538 | |
| 1539 | free_fd_percpu(); |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 1540 | |
| 1541 | free(irq_column_2_cpu); |
| 1542 | free(irqs_per_cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1543 | } |
| 1544 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1545 | /* |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1546 | * Open a file, and exit on failure |
| 1547 | */ |
| 1548 | FILE *fopen_or_die(const char *path, const char *mode) |
| 1549 | { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1550 | FILE *filep = fopen(path, mode); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1551 | if (!filep) |
| 1552 | err(1, "%s: open failed", path); |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1553 | return filep; |
| 1554 | } |
| 1555 | |
| 1556 | /* |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1557 | * Parse a file containing a single int. |
| 1558 | */ |
| 1559 | int parse_int_file(const char *fmt, ...) |
| 1560 | { |
| 1561 | va_list args; |
| 1562 | char path[PATH_MAX]; |
| 1563 | FILE *filep; |
| 1564 | int value; |
| 1565 | |
| 1566 | va_start(args, fmt); |
| 1567 | vsnprintf(path, sizeof(path), fmt, args); |
| 1568 | va_end(args); |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1569 | filep = fopen_or_die(path, "r"); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1570 | if (fscanf(filep, "%d", &value) != 1) |
| 1571 | err(1, "%s: failed to parse number from file", path); |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1572 | fclose(filep); |
| 1573 | return value; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1577 | * get_cpu_position_in_core(cpu) |
| 1578 | * return the position of the CPU among its HT siblings in the core |
| 1579 | * return -1 if the sibling is not in list |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1580 | */ |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1581 | int get_cpu_position_in_core(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1582 | { |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1583 | char path[64]; |
| 1584 | FILE *filep; |
| 1585 | int this_cpu; |
| 1586 | char character; |
| 1587 | int i; |
| 1588 | |
| 1589 | sprintf(path, |
| 1590 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", |
| 1591 | cpu); |
| 1592 | filep = fopen(path, "r"); |
| 1593 | if (filep == NULL) { |
| 1594 | perror(path); |
| 1595 | exit(1); |
| 1596 | } |
| 1597 | |
| 1598 | for (i = 0; i < topo.num_threads_per_core; i++) { |
| 1599 | fscanf(filep, "%d", &this_cpu); |
| 1600 | if (this_cpu == cpu) { |
| 1601 | fclose(filep); |
| 1602 | return i; |
| 1603 | } |
| 1604 | |
| 1605 | /* Account for no separator after last thread*/ |
| 1606 | if (i != (topo.num_threads_per_core - 1)) |
| 1607 | fscanf(filep, "%c", &character); |
| 1608 | } |
| 1609 | |
| 1610 | fclose(filep); |
| 1611 | return -1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1612 | } |
| 1613 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1614 | /* |
| 1615 | * cpu_is_first_core_in_package(cpu) |
| 1616 | * return 1 if given CPU is 1st core in package |
| 1617 | */ |
| 1618 | int cpu_is_first_core_in_package(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1619 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1620 | return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | int get_physical_package_id(int cpu) |
| 1624 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1625 | return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | int get_core_id(int cpu) |
| 1629 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1630 | return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1631 | } |
| 1632 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1633 | int get_num_ht_siblings(int cpu) |
| 1634 | { |
| 1635 | char path[80]; |
| 1636 | FILE *filep; |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1637 | int sib1; |
| 1638 | int matches = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1639 | char character; |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1640 | char str[100]; |
| 1641 | char *ch; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1642 | |
| 1643 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1644 | filep = fopen_or_die(path, "r"); |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1645 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1646 | /* |
| 1647 | * file format: |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1648 | * A ',' separated or '-' separated set of numbers |
| 1649 | * (eg 1-2 or 1,3,4,5) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1650 | */ |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1651 | fscanf(filep, "%d%c\n", &sib1, &character); |
| 1652 | fseek(filep, 0, SEEK_SET); |
| 1653 | fgets(str, 100, filep); |
| 1654 | ch = strchr(str, character); |
| 1655 | while (ch != NULL) { |
| 1656 | matches++; |
| 1657 | ch = strchr(ch+1, character); |
| 1658 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1659 | |
| 1660 | fclose(filep); |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 1661 | return matches+1; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1662 | } |
| 1663 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1664 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1665 | * run func(thread, core, package) in topology order |
| 1666 | * skip non-present cpus |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1667 | */ |
| 1668 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1669 | int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, |
| 1670 | struct pkg_data *, struct thread_data *, struct core_data *, |
| 1671 | struct pkg_data *), struct thread_data *thread_base, |
| 1672 | struct core_data *core_base, struct pkg_data *pkg_base, |
| 1673 | struct thread_data *thread_base2, struct core_data *core_base2, |
| 1674 | struct pkg_data *pkg_base2) |
| 1675 | { |
| 1676 | int retval, pkg_no, core_no, thread_no; |
| 1677 | |
| 1678 | for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { |
| 1679 | for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { |
| 1680 | for (thread_no = 0; thread_no < |
| 1681 | topo.num_threads_per_core; ++thread_no) { |
| 1682 | struct thread_data *t, *t2; |
| 1683 | struct core_data *c, *c2; |
| 1684 | struct pkg_data *p, *p2; |
| 1685 | |
| 1686 | t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); |
| 1687 | |
| 1688 | if (cpu_is_not_present(t->cpu_id)) |
| 1689 | continue; |
| 1690 | |
| 1691 | t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); |
| 1692 | |
| 1693 | c = GET_CORE(core_base, core_no, pkg_no); |
| 1694 | c2 = GET_CORE(core_base2, core_no, pkg_no); |
| 1695 | |
| 1696 | p = GET_PKG(pkg_base, pkg_no); |
| 1697 | p2 = GET_PKG(pkg_base2, pkg_no); |
| 1698 | |
| 1699 | retval = func(t, c, p, t2, c2, p2); |
| 1700 | if (retval) |
| 1701 | return retval; |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | * run func(cpu) on every cpu in /proc/stat |
| 1710 | * return max_cpu number |
| 1711 | */ |
| 1712 | int for_all_proc_cpus(int (func)(int)) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1713 | { |
| 1714 | FILE *fp; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1715 | int cpu_num; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1716 | int retval; |
| 1717 | |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1718 | fp = fopen_or_die(proc_stat, "r"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1719 | |
| 1720 | retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1721 | if (retval != 0) |
| 1722 | err(1, "%s: failed to parse format", proc_stat); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1723 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1724 | while (1) { |
| 1725 | retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1726 | if (retval != 1) |
| 1727 | break; |
| 1728 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1729 | retval = func(cpu_num); |
| 1730 | if (retval) { |
| 1731 | fclose(fp); |
| 1732 | return(retval); |
| 1733 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1734 | } |
| 1735 | fclose(fp); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1736 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | void re_initialize(void) |
| 1740 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1741 | free_all_buffers(); |
| 1742 | setup_all_buffers(); |
| 1743 | printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1746 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1747 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1748 | * count_cpus() |
| 1749 | * remember the last one seen, it will be the max |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1750 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1751 | int count_cpus(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1752 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1753 | if (topo.max_cpu_num < cpu) |
| 1754 | topo.max_cpu_num = cpu; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1755 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1756 | topo.num_cpus += 1; |
| 1757 | return 0; |
| 1758 | } |
| 1759 | int mark_cpu_present(int cpu) |
| 1760 | { |
| 1761 | CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 1762 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1763 | } |
| 1764 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 1765 | /* |
| 1766 | * snapshot_proc_interrupts() |
| 1767 | * |
| 1768 | * read and record summary of /proc/interrupts |
| 1769 | * |
| 1770 | * return 1 if config change requires a restart, else return 0 |
| 1771 | */ |
| 1772 | int snapshot_proc_interrupts(void) |
| 1773 | { |
| 1774 | static FILE *fp; |
| 1775 | int column, retval; |
| 1776 | |
| 1777 | if (fp == NULL) |
| 1778 | fp = fopen_or_die("/proc/interrupts", "r"); |
| 1779 | else |
| 1780 | rewind(fp); |
| 1781 | |
| 1782 | /* read 1st line of /proc/interrupts to get cpu* name for each column */ |
| 1783 | for (column = 0; column < topo.num_cpus; ++column) { |
| 1784 | int cpu_number; |
| 1785 | |
| 1786 | retval = fscanf(fp, " CPU%d", &cpu_number); |
| 1787 | if (retval != 1) |
| 1788 | break; |
| 1789 | |
| 1790 | if (cpu_number > topo.max_cpu_num) { |
| 1791 | warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); |
| 1792 | return 1; |
| 1793 | } |
| 1794 | |
| 1795 | irq_column_2_cpu[column] = cpu_number; |
| 1796 | irqs_per_cpu[cpu_number] = 0; |
| 1797 | } |
| 1798 | |
| 1799 | /* read /proc/interrupt count lines and sum up irqs per cpu */ |
| 1800 | while (1) { |
| 1801 | int column; |
| 1802 | char buf[64]; |
| 1803 | |
| 1804 | retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ |
| 1805 | if (retval != 1) |
| 1806 | break; |
| 1807 | |
| 1808 | /* read the count per cpu */ |
| 1809 | for (column = 0; column < topo.num_cpus; ++column) { |
| 1810 | |
| 1811 | int cpu_number, irq_count; |
| 1812 | |
| 1813 | retval = fscanf(fp, " %d", &irq_count); |
| 1814 | if (retval != 1) |
| 1815 | break; |
| 1816 | |
| 1817 | cpu_number = irq_column_2_cpu[column]; |
| 1818 | irqs_per_cpu[cpu_number] += irq_count; |
| 1819 | |
| 1820 | } |
| 1821 | |
| 1822 | while (getc(fp) != '\n') |
| 1823 | ; /* flush interrupt description */ |
| 1824 | |
| 1825 | } |
| 1826 | return 0; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * snapshot /proc and /sys files |
| 1831 | * |
| 1832 | * return 1 if configuration restart needed, else return 0 |
| 1833 | */ |
| 1834 | int snapshot_proc_sysfs_files(void) |
| 1835 | { |
| 1836 | if (snapshot_proc_interrupts()) |
| 1837 | return 1; |
| 1838 | |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1842 | void turbostat_loop() |
| 1843 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1844 | int retval; |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1845 | int restarted = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1846 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1847 | restart: |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1848 | restarted++; |
| 1849 | |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 1850 | snapshot_proc_sysfs_files(); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1851 | retval = for_all_cpus(get_counters, EVEN_COUNTERS); |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 1852 | if (retval < -1) { |
| 1853 | exit(retval); |
| 1854 | } else if (retval == -1) { |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1855 | if (restarted > 1) { |
| 1856 | exit(retval); |
| 1857 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1858 | re_initialize(); |
| 1859 | goto restart; |
| 1860 | } |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1861 | restarted = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1862 | gettimeofday(&tv_even, (struct timezone *)NULL); |
| 1863 | |
| 1864 | while (1) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1865 | if (for_all_proc_cpus(cpu_is_not_present)) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1866 | re_initialize(); |
| 1867 | goto restart; |
| 1868 | } |
Len Brown | 2a0609c | 2016-02-12 22:44:48 -0500 | [diff] [blame] | 1869 | nanosleep(&interval_ts, NULL); |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 1870 | if (snapshot_proc_sysfs_files()) |
| 1871 | goto restart; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1872 | retval = for_all_cpus(get_counters, ODD_COUNTERS); |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 1873 | if (retval < -1) { |
| 1874 | exit(retval); |
| 1875 | } else if (retval == -1) { |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 1876 | re_initialize(); |
| 1877 | goto restart; |
| 1878 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1879 | gettimeofday(&tv_odd, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1880 | timersub(&tv_odd, &tv_even, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1881 | for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); |
| 1882 | compute_average(EVEN_COUNTERS); |
| 1883 | format_all_counters(EVEN_COUNTERS); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1884 | flush_output_stdout(); |
Len Brown | 2a0609c | 2016-02-12 22:44:48 -0500 | [diff] [blame] | 1885 | nanosleep(&interval_ts, NULL); |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 1886 | if (snapshot_proc_sysfs_files()) |
| 1887 | goto restart; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1888 | retval = for_all_cpus(get_counters, EVEN_COUNTERS); |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 1889 | if (retval < -1) { |
| 1890 | exit(retval); |
| 1891 | } else if (retval == -1) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1892 | re_initialize(); |
| 1893 | goto restart; |
| 1894 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1895 | gettimeofday(&tv_even, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1896 | timersub(&tv_even, &tv_odd, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1897 | for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); |
| 1898 | compute_average(ODD_COUNTERS); |
| 1899 | format_all_counters(ODD_COUNTERS); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 1900 | flush_output_stdout(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | void check_dev_msr() |
| 1905 | { |
| 1906 | struct stat sb; |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1907 | char pathname[32]; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1908 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1909 | sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); |
| 1910 | if (stat(pathname, &sb)) |
Len Brown | a21d38c | 2015-03-24 16:37:35 -0400 | [diff] [blame] | 1911 | if (system("/sbin/modprobe msr > /dev/null 2>&1")) |
| 1912 | err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1913 | } |
| 1914 | |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 1915 | void check_permissions() |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1916 | { |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 1917 | struct __user_cap_header_struct cap_header_data; |
| 1918 | cap_user_header_t cap_header = &cap_header_data; |
| 1919 | struct __user_cap_data_struct cap_data_data; |
| 1920 | cap_user_data_t cap_data = &cap_data_data; |
| 1921 | extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); |
| 1922 | int do_exit = 0; |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1923 | char pathname[32]; |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 1924 | |
| 1925 | /* check for CAP_SYS_RAWIO */ |
| 1926 | cap_header->pid = getpid(); |
| 1927 | cap_header->version = _LINUX_CAPABILITY_VERSION; |
| 1928 | if (capget(cap_header, cap_data) < 0) |
| 1929 | err(-6, "capget(2) failed"); |
| 1930 | |
| 1931 | if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { |
| 1932 | do_exit++; |
| 1933 | warnx("capget(CAP_SYS_RAWIO) failed," |
| 1934 | " try \"# setcap cap_sys_rawio=ep %s\"", progname); |
| 1935 | } |
| 1936 | |
| 1937 | /* test file permissions */ |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 1938 | sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); |
| 1939 | if (euidaccess(pathname, R_OK)) { |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 1940 | do_exit++; |
| 1941 | warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); |
| 1942 | } |
| 1943 | |
| 1944 | /* if all else fails, thell them to be root */ |
| 1945 | if (do_exit) |
| 1946 | if (getuid() != 0) |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 1947 | warnx("... or simply run as root"); |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 1948 | |
| 1949 | if (do_exit) |
| 1950 | exit(-6); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1951 | } |
| 1952 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 1953 | /* |
| 1954 | * NHM adds support for additional MSRs: |
| 1955 | * |
| 1956 | * MSR_SMI_COUNT 0x00000034 |
| 1957 | * |
Len Brown | ec0adc5 | 2015-11-12 02:42:31 -0500 | [diff] [blame] | 1958 | * MSR_PLATFORM_INFO 0x000000ce |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 1959 | * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2 |
| 1960 | * |
| 1961 | * MSR_PKG_C3_RESIDENCY 0x000003f8 |
| 1962 | * MSR_PKG_C6_RESIDENCY 0x000003f9 |
| 1963 | * MSR_CORE_C3_RESIDENCY 0x000003fc |
| 1964 | * MSR_CORE_C6_RESIDENCY 0x000003fd |
| 1965 | * |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1966 | * Side effect: |
| 1967 | * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 1968 | */ |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1969 | int probe_nhm_msrs(unsigned int family, unsigned int model) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1970 | { |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1971 | unsigned long long msr; |
Len Brown | 21ed557 | 2015-10-19 22:37:40 -0400 | [diff] [blame] | 1972 | unsigned int base_ratio; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1973 | int *pkg_cstate_limits; |
| 1974 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1975 | if (!genuine_intel) |
| 1976 | return 0; |
| 1977 | |
| 1978 | if (family != 6) |
| 1979 | return 0; |
| 1980 | |
Len Brown | 21ed557 | 2015-10-19 22:37:40 -0400 | [diff] [blame] | 1981 | bclk = discover_bclk(family, model); |
| 1982 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1983 | switch (model) { |
| 1984 | case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ |
| 1985 | case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ |
| 1986 | case 0x1F: /* Core i7 and i5 Processor - Nehalem */ |
| 1987 | case 0x25: /* Westmere Client - Clarkdale, Arrandale */ |
| 1988 | case 0x2C: /* Westmere EP - Gulftown */ |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1989 | case 0x2E: /* Nehalem-EX Xeon - Beckton */ |
| 1990 | case 0x2F: /* Westmere-EX Xeon - Eagleton */ |
| 1991 | pkg_cstate_limits = nhm_pkg_cstate_limits; |
| 1992 | break; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1993 | case 0x2A: /* SNB */ |
| 1994 | case 0x2D: /* SNB Xeon */ |
Len Brown | 553575f | 2011-11-18 03:32:01 -0500 | [diff] [blame] | 1995 | case 0x3A: /* IVB */ |
Len Brown | 1300651 | 2012-09-26 18:11:31 -0400 | [diff] [blame] | 1996 | case 0x3E: /* IVB Xeon */ |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 1997 | pkg_cstate_limits = snb_pkg_cstate_limits; |
| 1998 | break; |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 1999 | case 0x3C: /* HSW */ |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 2000 | case 0x3F: /* HSX */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 2001 | case 0x45: /* HSW */ |
Len Brown | 149c231 | 2013-03-15 10:58:02 -0400 | [diff] [blame] | 2002 | case 0x46: /* HSW */ |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2003 | case 0x3D: /* BDW */ |
Len Brown | 48a0631 | 2015-02-10 15:38:04 -0500 | [diff] [blame] | 2004 | case 0x47: /* BDW */ |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2005 | case 0x4F: /* BDX */ |
| 2006 | case 0x56: /* BDX-DE */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2007 | case 0x4E: /* SKL */ |
| 2008 | case 0x5E: /* SKL */ |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 2009 | pkg_cstate_limits = hsw_pkg_cstate_limits; |
| 2010 | break; |
| 2011 | case 0x37: /* BYT */ |
| 2012 | case 0x4D: /* AVN */ |
| 2013 | pkg_cstate_limits = slv_pkg_cstate_limits; |
| 2014 | break; |
| 2015 | case 0x4C: /* AMT */ |
| 2016 | pkg_cstate_limits = amt_pkg_cstate_limits; |
| 2017 | break; |
| 2018 | case 0x57: /* PHI */ |
| 2019 | pkg_cstate_limits = phi_pkg_cstate_limits; |
| 2020 | break; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2021 | default: |
| 2022 | return 0; |
| 2023 | } |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 2024 | get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); |
Len Brown | e9257f5 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 2025 | pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 2026 | |
Len Brown | ec0adc5 | 2015-11-12 02:42:31 -0500 | [diff] [blame] | 2027 | get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); |
Len Brown | 21ed557 | 2015-10-19 22:37:40 -0400 | [diff] [blame] | 2028 | base_ratio = (msr >> 8) & 0xFF; |
| 2029 | |
| 2030 | base_hz = base_ratio * bclk * 1000000; |
| 2031 | has_base_hz = 1; |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 2032 | return 1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2033 | } |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2034 | int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 2035 | { |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2036 | switch (model) { |
| 2037 | /* Nehalem compatible, but do not include turbo-ratio limit support */ |
| 2038 | case 0x2E: /* Nehalem-EX Xeon - Beckton */ |
| 2039 | case 0x2F: /* Westmere-EX Xeon - Eagleton */ |
Hubert Chrzaniuk | cbf97ab | 2016-02-10 14:55:22 +0100 | [diff] [blame] | 2040 | case 0x57: /* PHI - Knights Landing (different MSR definition) */ |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2041 | return 0; |
| 2042 | default: |
| 2043 | return 1; |
| 2044 | } |
| 2045 | } |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 2046 | int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 2047 | { |
| 2048 | if (!genuine_intel) |
| 2049 | return 0; |
| 2050 | |
| 2051 | if (family != 6) |
| 2052 | return 0; |
| 2053 | |
| 2054 | switch (model) { |
| 2055 | case 0x3E: /* IVB Xeon */ |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 2056 | case 0x3F: /* HSW Xeon */ |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 2057 | return 1; |
| 2058 | default: |
| 2059 | return 0; |
| 2060 | } |
| 2061 | } |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 2062 | int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 2063 | { |
| 2064 | if (!genuine_intel) |
| 2065 | return 0; |
| 2066 | |
| 2067 | if (family != 6) |
| 2068 | return 0; |
| 2069 | |
| 2070 | switch (model) { |
| 2071 | case 0x3F: /* HSW Xeon */ |
| 2072 | return 1; |
| 2073 | default: |
| 2074 | return 0; |
| 2075 | } |
| 2076 | } |
| 2077 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 2078 | int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 2079 | { |
| 2080 | if (!genuine_intel) |
| 2081 | return 0; |
| 2082 | |
| 2083 | if (family != 6) |
| 2084 | return 0; |
| 2085 | |
| 2086 | switch (model) { |
| 2087 | case 0x57: /* Knights Landing */ |
| 2088 | return 1; |
| 2089 | default: |
| 2090 | return 0; |
| 2091 | } |
| 2092 | } |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 2093 | int has_config_tdp(unsigned int family, unsigned int model) |
| 2094 | { |
| 2095 | if (!genuine_intel) |
| 2096 | return 0; |
| 2097 | |
| 2098 | if (family != 6) |
| 2099 | return 0; |
| 2100 | |
| 2101 | switch (model) { |
| 2102 | case 0x3A: /* IVB */ |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 2103 | case 0x3C: /* HSW */ |
| 2104 | case 0x3F: /* HSX */ |
| 2105 | case 0x45: /* HSW */ |
| 2106 | case 0x46: /* HSW */ |
| 2107 | case 0x3D: /* BDW */ |
| 2108 | case 0x47: /* BDW */ |
| 2109 | case 0x4F: /* BDX */ |
| 2110 | case 0x56: /* BDX-DE */ |
| 2111 | case 0x4E: /* SKL */ |
| 2112 | case 0x5E: /* SKL */ |
| 2113 | |
| 2114 | case 0x57: /* Knights Landing */ |
| 2115 | return 1; |
| 2116 | default: |
| 2117 | return 0; |
| 2118 | } |
| 2119 | } |
| 2120 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 2121 | static void |
Len Brown | 58cc30a | 2016-02-24 18:16:40 -0500 | [diff] [blame] | 2122 | dump_cstate_pstate_config_info(int family, int model) |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 2123 | { |
| 2124 | if (!do_nhm_platform_info) |
| 2125 | return; |
| 2126 | |
| 2127 | dump_nhm_platform_info(); |
| 2128 | |
| 2129 | if (has_hsw_turbo_ratio_limit(family, model)) |
| 2130 | dump_hsw_turbo_ratio_limits(); |
| 2131 | |
| 2132 | if (has_ivt_turbo_ratio_limit(family, model)) |
| 2133 | dump_ivt_turbo_ratio_limits(); |
| 2134 | |
| 2135 | if (has_nhm_turbo_ratio_limit(family, model)) |
| 2136 | dump_nhm_turbo_ratio_limits(); |
| 2137 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 2138 | if (has_knl_turbo_ratio_limit(family, model)) |
| 2139 | dump_knl_turbo_ratio_limits(); |
| 2140 | |
Len Brown | 6fb3143 | 2015-06-17 16:23:45 -0400 | [diff] [blame] | 2141 | if (has_config_tdp(family, model)) |
| 2142 | dump_config_tdp(); |
| 2143 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 2144 | dump_nhm_cst_cfg(); |
| 2145 | } |
| 2146 | |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 2147 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2148 | /* |
| 2149 | * print_epb() |
| 2150 | * Decode the ENERGY_PERF_BIAS MSR |
| 2151 | */ |
| 2152 | int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 2153 | { |
| 2154 | unsigned long long msr; |
| 2155 | char *epb_string; |
| 2156 | int cpu; |
| 2157 | |
| 2158 | if (!has_epb) |
| 2159 | return 0; |
| 2160 | |
| 2161 | cpu = t->cpu_id; |
| 2162 | |
| 2163 | /* EPB is per-package */ |
| 2164 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 2165 | return 0; |
| 2166 | |
| 2167 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2168 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2169 | return -1; |
| 2170 | } |
| 2171 | |
| 2172 | if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) |
| 2173 | return 0; |
| 2174 | |
Len Brown | e9be7dd | 2015-05-26 12:19:37 -0400 | [diff] [blame] | 2175 | switch (msr & 0xF) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2176 | case ENERGY_PERF_BIAS_PERFORMANCE: |
| 2177 | epb_string = "performance"; |
| 2178 | break; |
| 2179 | case ENERGY_PERF_BIAS_NORMAL: |
| 2180 | epb_string = "balanced"; |
| 2181 | break; |
| 2182 | case ENERGY_PERF_BIAS_POWERSAVE: |
| 2183 | epb_string = "powersave"; |
| 2184 | break; |
| 2185 | default: |
| 2186 | epb_string = "custom"; |
| 2187 | break; |
| 2188 | } |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2189 | fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2190 | |
| 2191 | return 0; |
| 2192 | } |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2193 | /* |
| 2194 | * print_hwp() |
| 2195 | * Decode the MSR_HWP_CAPABILITIES |
| 2196 | */ |
| 2197 | int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 2198 | { |
| 2199 | unsigned long long msr; |
| 2200 | int cpu; |
| 2201 | |
| 2202 | if (!has_hwp) |
| 2203 | return 0; |
| 2204 | |
| 2205 | cpu = t->cpu_id; |
| 2206 | |
| 2207 | /* MSR_HWP_CAPABILITIES is per-package */ |
| 2208 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 2209 | return 0; |
| 2210 | |
| 2211 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2212 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2213 | return -1; |
| 2214 | } |
| 2215 | |
| 2216 | if (get_msr(cpu, MSR_PM_ENABLE, &msr)) |
| 2217 | return 0; |
| 2218 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2219 | fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2220 | cpu, msr, (msr & (1 << 0)) ? "" : "No-"); |
| 2221 | |
| 2222 | /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ |
| 2223 | if ((msr & (1 << 0)) == 0) |
| 2224 | return 0; |
| 2225 | |
| 2226 | if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) |
| 2227 | return 0; |
| 2228 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2229 | fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2230 | "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n", |
| 2231 | cpu, msr, |
| 2232 | (unsigned int)HWP_HIGHEST_PERF(msr), |
| 2233 | (unsigned int)HWP_GUARANTEED_PERF(msr), |
| 2234 | (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), |
| 2235 | (unsigned int)HWP_LOWEST_PERF(msr)); |
| 2236 | |
| 2237 | if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) |
| 2238 | return 0; |
| 2239 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2240 | fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2241 | "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n", |
| 2242 | cpu, msr, |
| 2243 | (unsigned int)(((msr) >> 0) & 0xff), |
| 2244 | (unsigned int)(((msr) >> 8) & 0xff), |
| 2245 | (unsigned int)(((msr) >> 16) & 0xff), |
| 2246 | (unsigned int)(((msr) >> 24) & 0xff), |
| 2247 | (unsigned int)(((msr) >> 32) & 0xff3), |
| 2248 | (unsigned int)(((msr) >> 42) & 0x1)); |
| 2249 | |
| 2250 | if (has_hwp_pkg) { |
| 2251 | if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) |
| 2252 | return 0; |
| 2253 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2254 | fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2255 | "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n", |
| 2256 | cpu, msr, |
| 2257 | (unsigned int)(((msr) >> 0) & 0xff), |
| 2258 | (unsigned int)(((msr) >> 8) & 0xff), |
| 2259 | (unsigned int)(((msr) >> 16) & 0xff), |
| 2260 | (unsigned int)(((msr) >> 24) & 0xff), |
| 2261 | (unsigned int)(((msr) >> 32) & 0xff3)); |
| 2262 | } |
| 2263 | if (has_hwp_notify) { |
| 2264 | if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) |
| 2265 | return 0; |
| 2266 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2267 | fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2268 | "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", |
| 2269 | cpu, msr, |
| 2270 | ((msr) & 0x1) ? "EN" : "Dis", |
| 2271 | ((msr) & 0x2) ? "EN" : "Dis"); |
| 2272 | } |
| 2273 | if (get_msr(cpu, MSR_HWP_STATUS, &msr)) |
| 2274 | return 0; |
| 2275 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2276 | fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2277 | "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", |
| 2278 | cpu, msr, |
| 2279 | ((msr) & 0x1) ? "" : "No-", |
| 2280 | ((msr) & 0x2) ? "" : "No-"); |
| 2281 | |
| 2282 | return 0; |
| 2283 | } |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2284 | |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2285 | /* |
| 2286 | * print_perf_limit() |
| 2287 | */ |
| 2288 | int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 2289 | { |
| 2290 | unsigned long long msr; |
| 2291 | int cpu; |
| 2292 | |
| 2293 | cpu = t->cpu_id; |
| 2294 | |
| 2295 | /* per-package */ |
| 2296 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 2297 | return 0; |
| 2298 | |
| 2299 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2300 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2301 | return -1; |
| 2302 | } |
| 2303 | |
| 2304 | if (do_core_perf_limit_reasons) { |
| 2305 | get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2306 | fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); |
| 2307 | fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", |
Len Brown | e33cbe8 | 2015-03-13 16:30:57 -0400 | [diff] [blame] | 2308 | (msr & 1 << 15) ? "bit15, " : "", |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2309 | (msr & 1 << 14) ? "bit14, " : "", |
Len Brown | e33cbe8 | 2015-03-13 16:30:57 -0400 | [diff] [blame] | 2310 | (msr & 1 << 13) ? "Transitions, " : "", |
| 2311 | (msr & 1 << 12) ? "MultiCoreTurbo, " : "", |
| 2312 | (msr & 1 << 11) ? "PkgPwrL2, " : "", |
| 2313 | (msr & 1 << 10) ? "PkgPwrL1, " : "", |
| 2314 | (msr & 1 << 9) ? "CorePwr, " : "", |
| 2315 | (msr & 1 << 8) ? "Amps, " : "", |
| 2316 | (msr & 1 << 6) ? "VR-Therm, " : "", |
| 2317 | (msr & 1 << 5) ? "Auto-HWP, " : "", |
| 2318 | (msr & 1 << 4) ? "Graphics, " : "", |
| 2319 | (msr & 1 << 2) ? "bit2, " : "", |
| 2320 | (msr & 1 << 1) ? "ThermStatus, " : "", |
| 2321 | (msr & 1 << 0) ? "PROCHOT, " : ""); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2322 | fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", |
Len Brown | e33cbe8 | 2015-03-13 16:30:57 -0400 | [diff] [blame] | 2323 | (msr & 1 << 31) ? "bit31, " : "", |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2324 | (msr & 1 << 30) ? "bit30, " : "", |
Len Brown | e33cbe8 | 2015-03-13 16:30:57 -0400 | [diff] [blame] | 2325 | (msr & 1 << 29) ? "Transitions, " : "", |
| 2326 | (msr & 1 << 28) ? "MultiCoreTurbo, " : "", |
| 2327 | (msr & 1 << 27) ? "PkgPwrL2, " : "", |
| 2328 | (msr & 1 << 26) ? "PkgPwrL1, " : "", |
| 2329 | (msr & 1 << 25) ? "CorePwr, " : "", |
| 2330 | (msr & 1 << 24) ? "Amps, " : "", |
| 2331 | (msr & 1 << 22) ? "VR-Therm, " : "", |
| 2332 | (msr & 1 << 21) ? "Auto-HWP, " : "", |
| 2333 | (msr & 1 << 20) ? "Graphics, " : "", |
| 2334 | (msr & 1 << 18) ? "bit18, " : "", |
| 2335 | (msr & 1 << 17) ? "ThermStatus, " : "", |
| 2336 | (msr & 1 << 16) ? "PROCHOT, " : ""); |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2337 | |
| 2338 | } |
| 2339 | if (do_gfx_perf_limit_reasons) { |
| 2340 | get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2341 | fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); |
| 2342 | fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2343 | (msr & 1 << 0) ? "PROCHOT, " : "", |
| 2344 | (msr & 1 << 1) ? "ThermStatus, " : "", |
| 2345 | (msr & 1 << 4) ? "Graphics, " : "", |
| 2346 | (msr & 1 << 6) ? "VR-Therm, " : "", |
| 2347 | (msr & 1 << 8) ? "Amps, " : "", |
| 2348 | (msr & 1 << 9) ? "GFXPwr, " : "", |
| 2349 | (msr & 1 << 10) ? "PkgPwrL1, " : "", |
| 2350 | (msr & 1 << 11) ? "PkgPwrL2, " : ""); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2351 | fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2352 | (msr & 1 << 16) ? "PROCHOT, " : "", |
| 2353 | (msr & 1 << 17) ? "ThermStatus, " : "", |
| 2354 | (msr & 1 << 20) ? "Graphics, " : "", |
| 2355 | (msr & 1 << 22) ? "VR-Therm, " : "", |
| 2356 | (msr & 1 << 24) ? "Amps, " : "", |
| 2357 | (msr & 1 << 25) ? "GFXPwr, " : "", |
| 2358 | (msr & 1 << 26) ? "PkgPwrL1, " : "", |
| 2359 | (msr & 1 << 27) ? "PkgPwrL2, " : ""); |
| 2360 | } |
| 2361 | if (do_ring_perf_limit_reasons) { |
| 2362 | get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2363 | fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); |
| 2364 | fprintf(outf, " (Active: %s%s%s%s%s%s)", |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2365 | (msr & 1 << 0) ? "PROCHOT, " : "", |
| 2366 | (msr & 1 << 1) ? "ThermStatus, " : "", |
| 2367 | (msr & 1 << 6) ? "VR-Therm, " : "", |
| 2368 | (msr & 1 << 8) ? "Amps, " : "", |
| 2369 | (msr & 1 << 10) ? "PkgPwrL1, " : "", |
| 2370 | (msr & 1 << 11) ? "PkgPwrL2, " : ""); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2371 | fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2372 | (msr & 1 << 16) ? "PROCHOT, " : "", |
| 2373 | (msr & 1 << 17) ? "ThermStatus, " : "", |
| 2374 | (msr & 1 << 22) ? "VR-Therm, " : "", |
| 2375 | (msr & 1 << 24) ? "Amps, " : "", |
| 2376 | (msr & 1 << 26) ? "PkgPwrL1, " : "", |
| 2377 | (msr & 1 << 27) ? "PkgPwrL2, " : ""); |
| 2378 | } |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2382 | #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ |
| 2383 | #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ |
| 2384 | |
Len Brown | 58cc30a | 2016-02-24 18:16:40 -0500 | [diff] [blame] | 2385 | double get_tdp(int model) |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2386 | { |
| 2387 | unsigned long long msr; |
| 2388 | |
| 2389 | if (do_rapl & RAPL_PKG_POWER_INFO) |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 2390 | if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2391 | return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; |
| 2392 | |
| 2393 | switch (model) { |
| 2394 | case 0x37: |
| 2395 | case 0x4D: |
| 2396 | return 30.0; |
| 2397 | default: |
| 2398 | return 135.0; |
| 2399 | } |
| 2400 | } |
| 2401 | |
Andrey Semin | 40ee8e3 | 2014-12-05 00:07:00 -0500 | [diff] [blame] | 2402 | /* |
| 2403 | * rapl_dram_energy_units_probe() |
| 2404 | * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. |
| 2405 | */ |
| 2406 | static double |
| 2407 | rapl_dram_energy_units_probe(int model, double rapl_energy_units) |
| 2408 | { |
| 2409 | /* only called for genuine_intel, family 6 */ |
| 2410 | |
| 2411 | switch (model) { |
| 2412 | case 0x3F: /* HSX */ |
| 2413 | case 0x4F: /* BDX */ |
| 2414 | case 0x56: /* BDX-DE */ |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 2415 | case 0x57: /* KNL */ |
Andrey Semin | 40ee8e3 | 2014-12-05 00:07:00 -0500 | [diff] [blame] | 2416 | return (rapl_dram_energy_units = 15.3 / 1000000); |
| 2417 | default: |
| 2418 | return (rapl_energy_units); |
| 2419 | } |
| 2420 | } |
| 2421 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2422 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2423 | /* |
| 2424 | * rapl_probe() |
| 2425 | * |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2426 | * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2427 | */ |
| 2428 | void rapl_probe(unsigned int family, unsigned int model) |
| 2429 | { |
| 2430 | unsigned long long msr; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2431 | unsigned int time_unit; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2432 | double tdp; |
| 2433 | |
| 2434 | if (!genuine_intel) |
| 2435 | return; |
| 2436 | |
| 2437 | if (family != 6) |
| 2438 | return; |
| 2439 | |
| 2440 | switch (model) { |
| 2441 | case 0x2A: |
| 2442 | case 0x3A: |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 2443 | case 0x3C: /* HSW */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 2444 | case 0x45: /* HSW */ |
Len Brown | 149c231 | 2013-03-15 10:58:02 -0400 | [diff] [blame] | 2445 | case 0x46: /* HSW */ |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2446 | case 0x3D: /* BDW */ |
Len Brown | 48a0631 | 2015-02-10 15:38:04 -0500 | [diff] [blame] | 2447 | case 0x47: /* BDW */ |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2448 | do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2449 | break; |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2450 | case 0x4E: /* SKL */ |
| 2451 | case 0x5E: /* SKL */ |
| 2452 | do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; |
| 2453 | break; |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 2454 | case 0x3F: /* HSX */ |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2455 | case 0x4F: /* BDX */ |
| 2456 | case 0x56: /* BDX-DE */ |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 2457 | case 0x57: /* KNL */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2458 | do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 2459 | break; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2460 | case 0x2D: |
| 2461 | case 0x3E: |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2462 | do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2463 | break; |
| 2464 | case 0x37: /* BYT */ |
| 2465 | case 0x4D: /* AVN */ |
| 2466 | do_rapl = RAPL_PKG | RAPL_CORES ; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2467 | break; |
| 2468 | default: |
| 2469 | return; |
| 2470 | } |
| 2471 | |
| 2472 | /* units on package 0, verify later other packages match */ |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 2473 | if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2474 | return; |
| 2475 | |
| 2476 | rapl_power_units = 1.0 / (1 << (msr & 0xF)); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2477 | if (model == 0x37) |
| 2478 | rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; |
| 2479 | else |
| 2480 | rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2481 | |
Andrey Semin | 40ee8e3 | 2014-12-05 00:07:00 -0500 | [diff] [blame] | 2482 | rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); |
| 2483 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2484 | time_unit = msr >> 16 & 0xF; |
| 2485 | if (time_unit == 0) |
| 2486 | time_unit = 0xA; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2487 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2488 | rapl_time_units = 1.0 / (1 << (time_unit)); |
| 2489 | |
| 2490 | tdp = get_tdp(model); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2491 | |
| 2492 | rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2493 | if (debug) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2494 | fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2495 | |
| 2496 | return; |
| 2497 | } |
| 2498 | |
Len Brown | 58cc30a | 2016-02-24 18:16:40 -0500 | [diff] [blame] | 2499 | void perf_limit_reasons_probe(int family, int model) |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 2500 | { |
| 2501 | if (!genuine_intel) |
| 2502 | return; |
| 2503 | |
| 2504 | if (family != 6) |
| 2505 | return; |
| 2506 | |
| 2507 | switch (model) { |
| 2508 | case 0x3C: /* HSW */ |
| 2509 | case 0x45: /* HSW */ |
| 2510 | case 0x46: /* HSW */ |
| 2511 | do_gfx_perf_limit_reasons = 1; |
| 2512 | case 0x3F: /* HSX */ |
| 2513 | do_core_perf_limit_reasons = 1; |
| 2514 | do_ring_perf_limit_reasons = 1; |
| 2515 | default: |
| 2516 | return; |
| 2517 | } |
| 2518 | } |
| 2519 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2520 | int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 2521 | { |
| 2522 | unsigned long long msr; |
| 2523 | unsigned int dts; |
| 2524 | int cpu; |
| 2525 | |
| 2526 | if (!(do_dts || do_ptm)) |
| 2527 | return 0; |
| 2528 | |
| 2529 | cpu = t->cpu_id; |
| 2530 | |
| 2531 | /* DTS is per-core, no need to print for each thread */ |
| 2532 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 2533 | return 0; |
| 2534 | |
| 2535 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2536 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2537 | return -1; |
| 2538 | } |
| 2539 | |
| 2540 | if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { |
| 2541 | if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) |
| 2542 | return 0; |
| 2543 | |
| 2544 | dts = (msr >> 16) & 0x7F; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2545 | fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2546 | cpu, msr, tcc_activation_temp - dts); |
| 2547 | |
| 2548 | #ifdef THERM_DEBUG |
| 2549 | if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) |
| 2550 | return 0; |
| 2551 | |
| 2552 | dts = (msr >> 16) & 0x7F; |
| 2553 | dts2 = (msr >> 8) & 0x7F; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2554 | fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2555 | cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); |
| 2556 | #endif |
| 2557 | } |
| 2558 | |
| 2559 | |
| 2560 | if (do_dts) { |
| 2561 | unsigned int resolution; |
| 2562 | |
| 2563 | if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) |
| 2564 | return 0; |
| 2565 | |
| 2566 | dts = (msr >> 16) & 0x7F; |
| 2567 | resolution = (msr >> 27) & 0xF; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2568 | fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2569 | cpu, msr, tcc_activation_temp - dts, resolution); |
| 2570 | |
| 2571 | #ifdef THERM_DEBUG |
| 2572 | if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) |
| 2573 | return 0; |
| 2574 | |
| 2575 | dts = (msr >> 16) & 0x7F; |
| 2576 | dts2 = (msr >> 8) & 0x7F; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2577 | fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2578 | cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); |
| 2579 | #endif |
| 2580 | } |
| 2581 | |
| 2582 | return 0; |
| 2583 | } |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 2584 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2585 | void print_power_limit_msr(int cpu, unsigned long long msr, char *label) |
| 2586 | { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2587 | fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2588 | cpu, label, |
| 2589 | ((msr >> 15) & 1) ? "EN" : "DIS", |
| 2590 | ((msr >> 0) & 0x7FFF) * rapl_power_units, |
| 2591 | (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, |
| 2592 | (((msr >> 16) & 1) ? "EN" : "DIS")); |
| 2593 | |
| 2594 | return; |
| 2595 | } |
| 2596 | |
| 2597 | int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 2598 | { |
| 2599 | unsigned long long msr; |
| 2600 | int cpu; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2601 | |
| 2602 | if (!do_rapl) |
| 2603 | return 0; |
| 2604 | |
| 2605 | /* RAPL counters are per package, so print only for 1st thread/package */ |
| 2606 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 2607 | return 0; |
| 2608 | |
| 2609 | cpu = t->cpu_id; |
| 2610 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2611 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2612 | return -1; |
| 2613 | } |
| 2614 | |
| 2615 | if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) |
| 2616 | return -1; |
| 2617 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2618 | if (debug) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2619 | fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2620 | "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2621 | rapl_power_units, rapl_energy_units, rapl_time_units); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2622 | } |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2623 | if (do_rapl & RAPL_PKG_POWER_INFO) { |
| 2624 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2625 | if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) |
| 2626 | return -5; |
| 2627 | |
| 2628 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2629 | fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2630 | cpu, msr, |
| 2631 | ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 2632 | ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 2633 | ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 2634 | ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); |
| 2635 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2636 | } |
| 2637 | if (do_rapl & RAPL_PKG) { |
| 2638 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2639 | if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) |
| 2640 | return -9; |
| 2641 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2642 | fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2643 | cpu, msr, (msr >> 63) & 1 ? "": "UN"); |
| 2644 | |
| 2645 | print_power_limit_msr(cpu, msr, "PKG Limit #1"); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2646 | fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2647 | cpu, |
| 2648 | ((msr >> 47) & 1) ? "EN" : "DIS", |
| 2649 | ((msr >> 32) & 0x7FFF) * rapl_power_units, |
| 2650 | (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, |
| 2651 | ((msr >> 48) & 1) ? "EN" : "DIS"); |
| 2652 | } |
| 2653 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2654 | if (do_rapl & RAPL_DRAM_POWER_INFO) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2655 | if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) |
| 2656 | return -6; |
| 2657 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2658 | fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2659 | cpu, msr, |
| 2660 | ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 2661 | ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 2662 | ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 2663 | ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2664 | } |
| 2665 | if (do_rapl & RAPL_DRAM) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2666 | if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) |
| 2667 | return -9; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2668 | fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2669 | cpu, msr, (msr >> 31) & 1 ? "": "UN"); |
| 2670 | |
| 2671 | print_power_limit_msr(cpu, msr, "DRAM Limit"); |
| 2672 | } |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2673 | if (do_rapl & RAPL_CORE_POLICY) { |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2674 | if (debug) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2675 | if (get_msr(cpu, MSR_PP0_POLICY, &msr)) |
| 2676 | return -7; |
| 2677 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2678 | fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2679 | } |
| 2680 | } |
| 2681 | if (do_rapl & RAPL_CORES) { |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2682 | if (debug) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2683 | |
| 2684 | if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) |
| 2685 | return -9; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2686 | fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2687 | cpu, msr, (msr >> 31) & 1 ? "": "UN"); |
| 2688 | print_power_limit_msr(cpu, msr, "Cores Limit"); |
| 2689 | } |
| 2690 | } |
| 2691 | if (do_rapl & RAPL_GFX) { |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2692 | if (debug) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2693 | if (get_msr(cpu, MSR_PP1_POLICY, &msr)) |
| 2694 | return -8; |
| 2695 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2696 | fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2697 | |
| 2698 | if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) |
| 2699 | return -9; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2700 | fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2701 | cpu, msr, (msr >> 31) & 1 ? "": "UN"); |
| 2702 | print_power_limit_msr(cpu, msr, "GFX Limit"); |
| 2703 | } |
| 2704 | } |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2708 | /* |
| 2709 | * SNB adds support for additional MSRs: |
| 2710 | * |
| 2711 | * MSR_PKG_C7_RESIDENCY 0x000003fa |
| 2712 | * MSR_CORE_C7_RESIDENCY 0x000003fe |
| 2713 | * MSR_PKG_C2_RESIDENCY 0x0000060d |
| 2714 | */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2715 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2716 | int has_snb_msrs(unsigned int family, unsigned int model) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2717 | { |
| 2718 | if (!genuine_intel) |
| 2719 | return 0; |
| 2720 | |
| 2721 | switch (model) { |
| 2722 | case 0x2A: |
| 2723 | case 0x2D: |
Len Brown | 650a37f | 2012-06-03 23:34:44 -0400 | [diff] [blame] | 2724 | case 0x3A: /* IVB */ |
Len Brown | 1300651 | 2012-09-26 18:11:31 -0400 | [diff] [blame] | 2725 | case 0x3E: /* IVB Xeon */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 2726 | case 0x3C: /* HSW */ |
| 2727 | case 0x3F: /* HSW */ |
| 2728 | case 0x45: /* HSW */ |
Len Brown | 149c231 | 2013-03-15 10:58:02 -0400 | [diff] [blame] | 2729 | case 0x46: /* HSW */ |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2730 | case 0x3D: /* BDW */ |
Len Brown | 48a0631 | 2015-02-10 15:38:04 -0500 | [diff] [blame] | 2731 | case 0x47: /* BDW */ |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2732 | case 0x4F: /* BDX */ |
| 2733 | case 0x56: /* BDX-DE */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2734 | case 0x4E: /* SKL */ |
| 2735 | case 0x5E: /* SKL */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2736 | return 1; |
| 2737 | } |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2741 | /* |
| 2742 | * HSW adds support for additional MSRs: |
| 2743 | * |
| 2744 | * MSR_PKG_C8_RESIDENCY 0x00000630 |
| 2745 | * MSR_PKG_C9_RESIDENCY 0x00000631 |
| 2746 | * MSR_PKG_C10_RESIDENCY 0x00000632 |
| 2747 | */ |
| 2748 | int has_hsw_msrs(unsigned int family, unsigned int model) |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 2749 | { |
| 2750 | if (!genuine_intel) |
| 2751 | return 0; |
| 2752 | |
| 2753 | switch (model) { |
Len Brown | 4e8e863f | 2014-02-27 23:28:53 -0500 | [diff] [blame] | 2754 | case 0x45: /* HSW */ |
| 2755 | case 0x3D: /* BDW */ |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2756 | case 0x4E: /* SKL */ |
| 2757 | case 0x5E: /* SKL */ |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 2758 | return 1; |
| 2759 | } |
| 2760 | return 0; |
| 2761 | } |
| 2762 | |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 2763 | /* |
| 2764 | * SKL adds support for additional MSRS: |
| 2765 | * |
| 2766 | * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 |
| 2767 | * MSR_PKG_ANY_CORE_C0_RES 0x00000659 |
| 2768 | * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A |
| 2769 | * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B |
| 2770 | */ |
| 2771 | int has_skl_msrs(unsigned int family, unsigned int model) |
| 2772 | { |
| 2773 | if (!genuine_intel) |
| 2774 | return 0; |
| 2775 | |
| 2776 | switch (model) { |
| 2777 | case 0x4E: /* SKL */ |
| 2778 | case 0x5E: /* SKL */ |
| 2779 | return 1; |
| 2780 | } |
| 2781 | return 0; |
| 2782 | } |
| 2783 | |
| 2784 | |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 2785 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2786 | int is_slm(unsigned int family, unsigned int model) |
| 2787 | { |
| 2788 | if (!genuine_intel) |
| 2789 | return 0; |
| 2790 | switch (model) { |
| 2791 | case 0x37: /* BYT */ |
| 2792 | case 0x4D: /* AVN */ |
| 2793 | return 1; |
| 2794 | } |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 2798 | int is_knl(unsigned int family, unsigned int model) |
| 2799 | { |
| 2800 | if (!genuine_intel) |
| 2801 | return 0; |
| 2802 | switch (model) { |
| 2803 | case 0x57: /* KNL */ |
| 2804 | return 1; |
| 2805 | } |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
Hubert Chrzaniuk | b2b34df | 2015-09-14 13:31:00 +0200 | [diff] [blame] | 2809 | unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) |
| 2810 | { |
| 2811 | if (is_knl(family, model)) |
| 2812 | return 1024; |
| 2813 | return 1; |
| 2814 | } |
| 2815 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2816 | #define SLM_BCLK_FREQS 5 |
| 2817 | double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; |
| 2818 | |
| 2819 | double slm_bclk(void) |
| 2820 | { |
| 2821 | unsigned long long msr = 3; |
| 2822 | unsigned int i; |
| 2823 | double freq; |
| 2824 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 2825 | if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2826 | fprintf(outf, "SLM BCLK: unknown\n"); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2827 | |
| 2828 | i = msr & 0xf; |
| 2829 | if (i >= SLM_BCLK_FREQS) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2830 | fprintf(outf, "SLM BCLK[%d] invalid\n", i); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2831 | msr = 3; |
| 2832 | } |
| 2833 | freq = slm_freq_table[i]; |
| 2834 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2835 | fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2836 | |
| 2837 | return freq; |
| 2838 | } |
| 2839 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2840 | double discover_bclk(unsigned int family, unsigned int model) |
| 2841 | { |
Chrzaniuk, Hubert | 121b48b | 2016-02-10 16:35:17 +0100 | [diff] [blame] | 2842 | if (has_snb_msrs(family, model) || is_knl(family, model)) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2843 | return 100.00; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2844 | else if (is_slm(family, model)) |
| 2845 | return slm_bclk(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2846 | else |
| 2847 | return 133.33; |
| 2848 | } |
| 2849 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2850 | /* |
| 2851 | * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where |
| 2852 | * the Thermal Control Circuit (TCC) activates. |
| 2853 | * This is usually equal to tjMax. |
| 2854 | * |
| 2855 | * Older processors do not have this MSR, so there we guess, |
| 2856 | * but also allow cmdline over-ride with -T. |
| 2857 | * |
| 2858 | * Several MSR temperature values are in units of degrees-C |
| 2859 | * below this value, including the Digital Thermal Sensor (DTS), |
| 2860 | * Package Thermal Management Sensor (PTM), and thermal event thresholds. |
| 2861 | */ |
| 2862 | int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 2863 | { |
| 2864 | unsigned long long msr; |
| 2865 | unsigned int target_c_local; |
| 2866 | int cpu; |
| 2867 | |
| 2868 | /* tcc_activation_temp is used only for dts or ptm */ |
| 2869 | if (!(do_dts || do_ptm)) |
| 2870 | return 0; |
| 2871 | |
| 2872 | /* this is a per-package concept */ |
| 2873 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 2874 | return 0; |
| 2875 | |
| 2876 | cpu = t->cpu_id; |
| 2877 | if (cpu_migrate(cpu)) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2878 | fprintf(outf, "Could not migrate to CPU %d\n", cpu); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2879 | return -1; |
| 2880 | } |
| 2881 | |
| 2882 | if (tcc_activation_temp_override != 0) { |
| 2883 | tcc_activation_temp = tcc_activation_temp_override; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2884 | fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2885 | cpu, tcc_activation_temp); |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | /* Temperature Target MSR is Nehalem and newer only */ |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 2890 | if (!do_nhm_platform_info) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2891 | goto guess; |
| 2892 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 2893 | if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2894 | goto guess; |
| 2895 | |
Jean Delvare | 3482124 | 2014-05-01 11:40:19 +0200 | [diff] [blame] | 2896 | target_c_local = (msr >> 16) & 0xFF; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2897 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2898 | if (debug) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2899 | fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2900 | cpu, msr, target_c_local); |
| 2901 | |
Jean Delvare | 3482124 | 2014-05-01 11:40:19 +0200 | [diff] [blame] | 2902 | if (!target_c_local) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2903 | goto guess; |
| 2904 | |
| 2905 | tcc_activation_temp = target_c_local; |
| 2906 | |
| 2907 | return 0; |
| 2908 | |
| 2909 | guess: |
| 2910 | tcc_activation_temp = TJMAX_DEFAULT; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2911 | fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2912 | cpu, tcc_activation_temp); |
| 2913 | |
| 2914 | return 0; |
| 2915 | } |
Len Brown | 69807a6 | 2015-11-21 12:22:47 -0500 | [diff] [blame] | 2916 | |
| 2917 | void decode_misc_enable_msr(void) |
| 2918 | { |
| 2919 | unsigned long long msr; |
| 2920 | |
| 2921 | if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2922 | fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n", |
Len Brown | 69807a6 | 2015-11-21 12:22:47 -0500 | [diff] [blame] | 2923 | base_cpu, msr, |
| 2924 | msr & (1 << 3) ? "TCC" : "", |
| 2925 | msr & (1 << 16) ? "EIST" : "", |
| 2926 | msr & (1 << 18) ? "MONITOR" : ""); |
| 2927 | } |
| 2928 | |
Len Brown | f005731 | 2015-12-03 01:35:36 -0500 | [diff] [blame] | 2929 | /* |
| 2930 | * Decode MSR_MISC_PWR_MGMT |
| 2931 | * |
| 2932 | * Decode the bits according to the Nehalem documentation |
| 2933 | * bit[0] seems to continue to have same meaning going forward |
| 2934 | * bit[1] less so... |
| 2935 | */ |
| 2936 | void decode_misc_pwr_mgmt_msr(void) |
| 2937 | { |
| 2938 | unsigned long long msr; |
| 2939 | |
| 2940 | if (!do_nhm_platform_info) |
| 2941 | return; |
| 2942 | |
| 2943 | if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2944 | fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n", |
Len Brown | f005731 | 2015-12-03 01:35:36 -0500 | [diff] [blame] | 2945 | base_cpu, msr, |
| 2946 | msr & (1 << 0) ? "DIS" : "EN", |
| 2947 | msr & (1 << 1) ? "EN" : "DIS"); |
| 2948 | } |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 2949 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 2950 | void process_cpuid() |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2951 | { |
Len Brown | 61a87ba | 2015-11-23 02:30:51 -0500 | [diff] [blame] | 2952 | unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2953 | unsigned int fms, family, model, stepping; |
| 2954 | |
| 2955 | eax = ebx = ecx = edx = 0; |
| 2956 | |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2957 | __get_cpuid(0, &max_level, &ebx, &ecx, &edx); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2958 | |
| 2959 | if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) |
| 2960 | genuine_intel = 1; |
| 2961 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 2962 | if (debug) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2963 | fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2964 | (char *)&ebx, (char *)&edx, (char *)&ecx); |
| 2965 | |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2966 | __get_cpuid(1, &fms, &ebx, &ecx, &edx); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2967 | family = (fms >> 8) & 0xf; |
| 2968 | model = (fms >> 4) & 0xf; |
| 2969 | stepping = fms & 0xf; |
| 2970 | if (family == 6 || family == 0xf) |
| 2971 | model += ((fms >> 16) & 0xf) << 4; |
| 2972 | |
Len Brown | 69807a6 | 2015-11-21 12:22:47 -0500 | [diff] [blame] | 2973 | if (debug) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2974 | fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2975 | max_level, family, model, stepping, family, model, stepping); |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 2976 | fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s\n", |
Len Brown | 69807a6 | 2015-11-21 12:22:47 -0500 | [diff] [blame] | 2977 | ecx & (1 << 0) ? "SSE3" : "-", |
| 2978 | ecx & (1 << 3) ? "MONITOR" : "-", |
| 2979 | ecx & (1 << 7) ? "EIST" : "-", |
| 2980 | ecx & (1 << 8) ? "TM2" : "-", |
| 2981 | edx & (1 << 4) ? "TSC" : "-", |
| 2982 | edx & (1 << 5) ? "MSR" : "-", |
| 2983 | edx & (1 << 22) ? "ACPI-TM" : "-", |
| 2984 | edx & (1 << 29) ? "TM" : "-"); |
| 2985 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2986 | |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2987 | if (!(edx & (1 << 5))) |
| 2988 | errx(1, "CPUID: no MSR"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2989 | |
| 2990 | /* |
| 2991 | * check max extended function levels of CPUID. |
| 2992 | * This is needed to check for invariant TSC. |
| 2993 | * This check is valid for both Intel and AMD. |
| 2994 | */ |
| 2995 | ebx = ecx = edx = 0; |
Len Brown | 61a87ba | 2015-11-23 02:30:51 -0500 | [diff] [blame] | 2996 | __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2997 | |
Len Brown | 61a87ba | 2015-11-23 02:30:51 -0500 | [diff] [blame] | 2998 | if (max_extended_level >= 0x80000007) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2999 | |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 3000 | /* |
| 3001 | * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 |
| 3002 | * this check is valid for both Intel and AMD |
| 3003 | */ |
| 3004 | __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); |
| 3005 | has_invariant_tsc = edx & (1 << 8); |
| 3006 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3007 | |
| 3008 | /* |
| 3009 | * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 |
| 3010 | * this check is valid for both Intel and AMD |
| 3011 | */ |
| 3012 | |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 3013 | __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); |
Thomas Renninger | 8209e05 | 2011-01-21 15:11:19 +0100 | [diff] [blame] | 3014 | has_aperf = ecx & (1 << 0); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3015 | do_dts = eax & (1 << 0); |
| 3016 | do_ptm = eax & (1 << 6); |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 3017 | has_hwp = eax & (1 << 7); |
| 3018 | has_hwp_notify = eax & (1 << 8); |
| 3019 | has_hwp_activity_window = eax & (1 << 9); |
| 3020 | has_hwp_epp = eax & (1 << 10); |
| 3021 | has_hwp_pkg = eax & (1 << 11); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3022 | has_epb = ecx & (1 << 3); |
| 3023 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3024 | if (debug) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3025 | fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, " |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 3026 | "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", |
| 3027 | has_aperf ? "" : "No-", |
| 3028 | do_dts ? "" : "No-", |
| 3029 | do_ptm ? "" : "No-", |
| 3030 | has_hwp ? "" : "No-", |
| 3031 | has_hwp_notify ? "" : "No-", |
| 3032 | has_hwp_activity_window ? "" : "No-", |
| 3033 | has_hwp_epp ? "" : "No-", |
| 3034 | has_hwp_pkg ? "" : "No-", |
| 3035 | has_epb ? "" : "No-"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3036 | |
Len Brown | 69807a6 | 2015-11-21 12:22:47 -0500 | [diff] [blame] | 3037 | if (debug) |
| 3038 | decode_misc_enable_msr(); |
| 3039 | |
Len Brown | 61a87ba | 2015-11-23 02:30:51 -0500 | [diff] [blame] | 3040 | if (max_level >= 0x15) { |
Len Brown | 8a5bdf4 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 3041 | unsigned int eax_crystal; |
| 3042 | unsigned int ebx_tsc; |
| 3043 | |
| 3044 | /* |
| 3045 | * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz |
| 3046 | */ |
| 3047 | eax_crystal = ebx_tsc = crystal_hz = edx = 0; |
| 3048 | __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx); |
| 3049 | |
| 3050 | if (ebx_tsc != 0) { |
| 3051 | |
| 3052 | if (debug && (ebx != 0)) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3053 | fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", |
Len Brown | 8a5bdf4 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 3054 | eax_crystal, ebx_tsc, crystal_hz); |
| 3055 | |
| 3056 | if (crystal_hz == 0) |
| 3057 | switch(model) { |
| 3058 | case 0x4E: /* SKL */ |
| 3059 | case 0x5E: /* SKL */ |
| 3060 | crystal_hz = 24000000; /* 24 MHz */ |
| 3061 | break; |
| 3062 | default: |
| 3063 | crystal_hz = 0; |
| 3064 | } |
| 3065 | |
| 3066 | if (crystal_hz) { |
| 3067 | tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; |
| 3068 | if (debug) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3069 | fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", |
Len Brown | 8a5bdf4 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 3070 | tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); |
| 3071 | } |
| 3072 | } |
| 3073 | } |
Len Brown | 61a87ba | 2015-11-23 02:30:51 -0500 | [diff] [blame] | 3074 | if (max_level >= 0x16) { |
| 3075 | unsigned int base_mhz, max_mhz, bus_mhz, edx; |
| 3076 | |
| 3077 | /* |
| 3078 | * CPUID 16H Base MHz, Max MHz, Bus MHz |
| 3079 | */ |
| 3080 | base_mhz = max_mhz = bus_mhz = edx = 0; |
| 3081 | |
| 3082 | __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx); |
| 3083 | if (debug) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3084 | fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", |
Len Brown | 61a87ba | 2015-11-23 02:30:51 -0500 | [diff] [blame] | 3085 | base_mhz, max_mhz, bus_mhz); |
| 3086 | } |
Len Brown | 8a5bdf4 | 2015-04-01 21:02:57 -0400 | [diff] [blame] | 3087 | |
Hubert Chrzaniuk | b2b34df | 2015-09-14 13:31:00 +0200 | [diff] [blame] | 3088 | if (has_aperf) |
| 3089 | aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); |
| 3090 | |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 3091 | do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model); |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 3092 | do_snb_cstates = has_snb_msrs(family, model); |
Len Brown | ee7e38e | 2015-02-09 23:39:45 -0500 | [diff] [blame] | 3093 | do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2); |
| 3094 | do_pc3 = (pkg_cstate_limit >= PCL__3); |
| 3095 | do_pc6 = (pkg_cstate_limit >= PCL__6); |
| 3096 | do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7); |
Len Brown | d789944 | 2015-01-23 00:12:33 -0500 | [diff] [blame] | 3097 | do_c8_c9_c10 = has_hsw_msrs(family, model); |
Len Brown | 0b2bb69 | 2015-03-26 00:50:30 -0400 | [diff] [blame] | 3098 | do_skl_residency = has_skl_msrs(family, model); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 3099 | do_slm_cstates = is_slm(family, model); |
Dasaratharaman Chandramouli | fb5d432 | 2015-05-20 09:49:34 -0700 | [diff] [blame] | 3100 | do_knl_cstates = is_knl(family, model); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3101 | |
Len Brown | f005731 | 2015-12-03 01:35:36 -0500 | [diff] [blame] | 3102 | if (debug) |
| 3103 | decode_misc_pwr_mgmt_msr(); |
| 3104 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3105 | rapl_probe(family, model); |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 3106 | perf_limit_reasons_probe(family, model); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3107 | |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 3108 | if (debug) |
Len Brown | 58cc30a | 2016-02-24 18:16:40 -0500 | [diff] [blame] | 3109 | dump_cstate_pstate_config_info(family, model); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 3110 | |
Len Brown | a2b7b74 | 2015-09-26 00:12:38 -0400 | [diff] [blame] | 3111 | if (has_skl_msrs(family, model)) |
| 3112 | calculate_tsc_tweak(); |
| 3113 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3114 | return; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3115 | } |
| 3116 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3117 | void help() |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3118 | { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3119 | fprintf(outf, |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3120 | "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" |
| 3121 | "\n" |
| 3122 | "Turbostat forks the specified COMMAND and prints statistics\n" |
| 3123 | "when COMMAND completes.\n" |
| 3124 | "If no COMMAND is specified, turbostat wakes every 5-seconds\n" |
| 3125 | "to print statistics, until interrupted.\n" |
| 3126 | "--debug run in \"debug\" mode\n" |
| 3127 | "--interval sec Override default 5-second measurement interval\n" |
| 3128 | "--help print this help message\n" |
| 3129 | "--counter msr print 32-bit counter at address \"msr\"\n" |
| 3130 | "--Counter msr print 64-bit Counter at address \"msr\"\n" |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3131 | "--out file create or truncate \"file\" for all output\n" |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3132 | "--msr msr print 32-bit value at address \"msr\"\n" |
| 3133 | "--MSR msr print 64-bit Value at address \"msr\"\n" |
| 3134 | "--version print version information\n" |
| 3135 | "\n" |
| 3136 | "For more help, run \"man turbostat\"\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | |
| 3140 | /* |
| 3141 | * in /dev/cpu/ return success for names that are numbers |
| 3142 | * ie. filter out ".", "..", "microcode". |
| 3143 | */ |
| 3144 | int dir_filter(const struct dirent *dirp) |
| 3145 | { |
| 3146 | if (isdigit(dirp->d_name[0])) |
| 3147 | return 1; |
| 3148 | else |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | int open_dev_cpu_msr(int dummy1) |
| 3153 | { |
| 3154 | return 0; |
| 3155 | } |
| 3156 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3157 | void topology_probe() |
| 3158 | { |
| 3159 | int i; |
| 3160 | int max_core_id = 0; |
| 3161 | int max_package_id = 0; |
| 3162 | int max_siblings = 0; |
| 3163 | struct cpu_topology { |
| 3164 | int core_id; |
| 3165 | int physical_package_id; |
| 3166 | } *cpus; |
| 3167 | |
| 3168 | /* Initialize num_cpus, max_cpu_num */ |
| 3169 | topo.num_cpus = 0; |
| 3170 | topo.max_cpu_num = 0; |
| 3171 | for_all_proc_cpus(count_cpus); |
| 3172 | if (!summary_only && topo.num_cpus > 1) |
| 3173 | show_cpu = 1; |
| 3174 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3175 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3176 | fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3177 | |
| 3178 | cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3179 | if (cpus == NULL) |
| 3180 | err(1, "calloc cpus"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3181 | |
| 3182 | /* |
| 3183 | * Allocate and initialize cpu_present_set |
| 3184 | */ |
| 3185 | cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3186 | if (cpu_present_set == NULL) |
| 3187 | err(3, "CPU_ALLOC"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3188 | cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
| 3189 | CPU_ZERO_S(cpu_present_setsize, cpu_present_set); |
| 3190 | for_all_proc_cpus(mark_cpu_present); |
| 3191 | |
| 3192 | /* |
| 3193 | * Allocate and initialize cpu_affinity_set |
| 3194 | */ |
| 3195 | cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3196 | if (cpu_affinity_set == NULL) |
| 3197 | err(3, "CPU_ALLOC"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3198 | cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
| 3199 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
| 3200 | |
| 3201 | |
| 3202 | /* |
| 3203 | * For online cpus |
| 3204 | * find max_core_id, max_package_id |
| 3205 | */ |
| 3206 | for (i = 0; i <= topo.max_cpu_num; ++i) { |
| 3207 | int siblings; |
| 3208 | |
| 3209 | if (cpu_is_not_present(i)) { |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3210 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3211 | fprintf(outf, "cpu%d NOT PRESENT\n", i); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3212 | continue; |
| 3213 | } |
| 3214 | cpus[i].core_id = get_core_id(i); |
| 3215 | if (cpus[i].core_id > max_core_id) |
| 3216 | max_core_id = cpus[i].core_id; |
| 3217 | |
| 3218 | cpus[i].physical_package_id = get_physical_package_id(i); |
| 3219 | if (cpus[i].physical_package_id > max_package_id) |
| 3220 | max_package_id = cpus[i].physical_package_id; |
| 3221 | |
| 3222 | siblings = get_num_ht_siblings(i); |
| 3223 | if (siblings > max_siblings) |
| 3224 | max_siblings = siblings; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3225 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3226 | fprintf(outf, "cpu %d pkg %d core %d\n", |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3227 | i, cpus[i].physical_package_id, cpus[i].core_id); |
| 3228 | } |
| 3229 | topo.num_cores_per_pkg = max_core_id + 1; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3230 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3231 | fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3232 | max_core_id, topo.num_cores_per_pkg); |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 3233 | if (debug && !summary_only && topo.num_cores_per_pkg > 1) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3234 | show_core = 1; |
| 3235 | |
| 3236 | topo.num_packages = max_package_id + 1; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3237 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3238 | fprintf(outf, "max_package_id %d, sizing for %d packages\n", |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3239 | max_package_id, topo.num_packages); |
Len Brown | 1cc21f7 | 2015-02-23 00:34:57 -0500 | [diff] [blame] | 3240 | if (debug && !summary_only && topo.num_packages > 1) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3241 | show_pkg = 1; |
| 3242 | |
| 3243 | topo.num_threads_per_core = max_siblings; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3244 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3245 | fprintf(outf, "max_siblings %d\n", max_siblings); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3246 | |
| 3247 | free(cpus); |
| 3248 | } |
| 3249 | |
| 3250 | void |
| 3251 | allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) |
| 3252 | { |
| 3253 | int i; |
| 3254 | |
| 3255 | *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * |
| 3256 | topo.num_packages, sizeof(struct thread_data)); |
| 3257 | if (*t == NULL) |
| 3258 | goto error; |
| 3259 | |
| 3260 | for (i = 0; i < topo.num_threads_per_core * |
| 3261 | topo.num_cores_per_pkg * topo.num_packages; i++) |
| 3262 | (*t)[i].cpu_id = -1; |
| 3263 | |
| 3264 | *c = calloc(topo.num_cores_per_pkg * topo.num_packages, |
| 3265 | sizeof(struct core_data)); |
| 3266 | if (*c == NULL) |
| 3267 | goto error; |
| 3268 | |
| 3269 | for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) |
| 3270 | (*c)[i].core_id = -1; |
| 3271 | |
| 3272 | *p = calloc(topo.num_packages, sizeof(struct pkg_data)); |
| 3273 | if (*p == NULL) |
| 3274 | goto error; |
| 3275 | |
| 3276 | for (i = 0; i < topo.num_packages; i++) |
| 3277 | (*p)[i].package_id = i; |
| 3278 | |
| 3279 | return; |
| 3280 | error: |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3281 | err(1, "calloc counters"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3282 | } |
| 3283 | /* |
| 3284 | * init_counter() |
| 3285 | * |
| 3286 | * set cpu_id, core_num, pkg_num |
| 3287 | * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE |
| 3288 | * |
| 3289 | * increment topo.num_cores when 1st core in pkg seen |
| 3290 | */ |
| 3291 | void init_counter(struct thread_data *thread_base, struct core_data *core_base, |
| 3292 | struct pkg_data *pkg_base, int thread_num, int core_num, |
| 3293 | int pkg_num, int cpu_id) |
| 3294 | { |
| 3295 | struct thread_data *t; |
| 3296 | struct core_data *c; |
| 3297 | struct pkg_data *p; |
| 3298 | |
| 3299 | t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); |
| 3300 | c = GET_CORE(core_base, core_num, pkg_num); |
| 3301 | p = GET_PKG(pkg_base, pkg_num); |
| 3302 | |
| 3303 | t->cpu_id = cpu_id; |
| 3304 | if (thread_num == 0) { |
| 3305 | t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; |
| 3306 | if (cpu_is_first_core_in_package(cpu_id)) |
| 3307 | t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; |
| 3308 | } |
| 3309 | |
| 3310 | c->core_id = core_num; |
| 3311 | p->package_id = pkg_num; |
| 3312 | } |
| 3313 | |
| 3314 | |
| 3315 | int initialize_counters(int cpu_id) |
| 3316 | { |
| 3317 | int my_thread_id, my_core_id, my_package_id; |
| 3318 | |
| 3319 | my_package_id = get_physical_package_id(cpu_id); |
| 3320 | my_core_id = get_core_id(cpu_id); |
Dasaratharaman Chandramouli | e275b38 | 2015-04-15 10:09:50 -0700 | [diff] [blame] | 3321 | my_thread_id = get_cpu_position_in_core(cpu_id); |
| 3322 | if (!my_thread_id) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3323 | topo.num_cores++; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3324 | |
| 3325 | init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); |
| 3326 | init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); |
| 3327 | return 0; |
| 3328 | } |
| 3329 | |
| 3330 | void allocate_output_buffer() |
| 3331 | { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 3332 | output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3333 | outp = output_buffer; |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3334 | if (outp == NULL) |
| 3335 | err(-1, "calloc output buffer"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3336 | } |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 3337 | void allocate_fd_percpu(void) |
| 3338 | { |
| 3339 | fd_percpu = calloc(topo.max_cpu_num, sizeof(int)); |
| 3340 | if (fd_percpu == NULL) |
| 3341 | err(-1, "calloc fd_percpu"); |
| 3342 | } |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 3343 | void allocate_irq_buffers(void) |
| 3344 | { |
| 3345 | irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); |
| 3346 | if (irq_column_2_cpu == NULL) |
| 3347 | err(-1, "calloc %d", topo.num_cpus); |
| 3348 | |
| 3349 | irqs_per_cpu = calloc(topo.max_cpu_num, sizeof(int)); |
| 3350 | if (irqs_per_cpu == NULL) |
| 3351 | err(-1, "calloc %d", topo.max_cpu_num); |
| 3352 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3353 | void setup_all_buffers(void) |
| 3354 | { |
| 3355 | topology_probe(); |
Len Brown | 562a2d3 | 2016-02-26 23:48:05 -0500 | [diff] [blame^] | 3356 | allocate_irq_buffers(); |
Len Brown | 3622989 | 2016-02-26 20:51:02 -0500 | [diff] [blame] | 3357 | allocate_fd_percpu(); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3358 | allocate_counters(&thread_even, &core_even, &package_even); |
| 3359 | allocate_counters(&thread_odd, &core_odd, &package_odd); |
| 3360 | allocate_output_buffer(); |
| 3361 | for_all_proc_cpus(initialize_counters); |
| 3362 | } |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 3363 | |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 3364 | void set_base_cpu(void) |
| 3365 | { |
| 3366 | base_cpu = sched_getcpu(); |
| 3367 | if (base_cpu < 0) |
| 3368 | err(-ENODEV, "No valid cpus found"); |
| 3369 | |
| 3370 | if (debug > 1) |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3371 | fprintf(outf, "base_cpu = %d\n", base_cpu); |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 3372 | } |
| 3373 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3374 | void turbostat_init() |
| 3375 | { |
Prarit Bhargava | 7ce7d5d | 2015-05-25 08:34:28 -0400 | [diff] [blame] | 3376 | setup_all_buffers(); |
| 3377 | set_base_cpu(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3378 | check_dev_msr(); |
Len Brown | 98481e7 | 2014-08-15 00:36:50 -0400 | [diff] [blame] | 3379 | check_permissions(); |
Len Brown | fcd1721 | 2015-03-23 20:29:09 -0400 | [diff] [blame] | 3380 | process_cpuid(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3381 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3382 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3383 | if (debug) |
Len Brown | 7f5c258 | 2015-12-01 01:36:39 -0500 | [diff] [blame] | 3384 | for_all_cpus(print_hwp, ODD_COUNTERS); |
| 3385 | |
| 3386 | if (debug) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3387 | for_all_cpus(print_epb, ODD_COUNTERS); |
| 3388 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3389 | if (debug) |
Len Brown | 3a9a941 | 2014-08-15 02:39:52 -0400 | [diff] [blame] | 3390 | for_all_cpus(print_perf_limit, ODD_COUNTERS); |
| 3391 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3392 | if (debug) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3393 | for_all_cpus(print_rapl, ODD_COUNTERS); |
| 3394 | |
| 3395 | for_all_cpus(set_temperature_target, ODD_COUNTERS); |
| 3396 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3397 | if (debug) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3398 | for_all_cpus(print_thermal, ODD_COUNTERS); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | int fork_it(char **argv) |
| 3402 | { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3403 | pid_t child_pid; |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 3404 | int status; |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 3405 | |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 3406 | status = for_all_cpus(get_counters, EVEN_COUNTERS); |
| 3407 | if (status) |
| 3408 | exit(status); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3409 | /* clear affinity side-effect of get_counters() */ |
| 3410 | sched_setaffinity(0, cpu_present_setsize, cpu_present_set); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3411 | gettimeofday(&tv_even, (struct timezone *)NULL); |
| 3412 | |
| 3413 | child_pid = fork(); |
| 3414 | if (!child_pid) { |
| 3415 | /* child */ |
| 3416 | execvp(argv[0], argv); |
| 3417 | } else { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3418 | |
| 3419 | /* parent */ |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3420 | if (child_pid == -1) |
| 3421 | err(1, "fork"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3422 | |
| 3423 | signal(SIGINT, SIG_IGN); |
| 3424 | signal(SIGQUIT, SIG_IGN); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 3425 | if (waitpid(child_pid, &status, 0) == -1) |
| 3426 | err(status, "waitpid"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3427 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3428 | /* |
| 3429 | * n.b. fork_it() does not check for errors from for_all_cpus() |
| 3430 | * because re-starting is problematic when forking |
| 3431 | */ |
| 3432 | for_all_cpus(get_counters, ODD_COUNTERS); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3433 | gettimeofday(&tv_odd, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3434 | timersub(&tv_odd, &tv_even, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 3435 | for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); |
| 3436 | compute_average(EVEN_COUNTERS); |
| 3437 | format_all_counters(EVEN_COUNTERS); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3438 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3439 | fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); |
| 3440 | |
| 3441 | flush_output_stderr(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3442 | |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 3443 | return status; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3444 | } |
| 3445 | |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 3446 | int get_and_dump_counters(void) |
| 3447 | { |
| 3448 | int status; |
| 3449 | |
| 3450 | status = for_all_cpus(get_counters, ODD_COUNTERS); |
| 3451 | if (status) |
| 3452 | return status; |
| 3453 | |
| 3454 | status = for_all_cpus(dump_counters, ODD_COUNTERS); |
| 3455 | if (status) |
| 3456 | return status; |
| 3457 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3458 | flush_output_stdout(); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 3459 | |
| 3460 | return status; |
| 3461 | } |
| 3462 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3463 | void print_version() { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3464 | fprintf(outf, "turbostat version 4.10 10 Dec, 2015" |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3465 | " - Len Brown <lenb@kernel.org>\n"); |
| 3466 | } |
| 3467 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3468 | void cmdline(int argc, char **argv) |
| 3469 | { |
| 3470 | int opt; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3471 | int option_index = 0; |
| 3472 | static struct option long_options[] = { |
| 3473 | {"Counter", required_argument, 0, 'C'}, |
| 3474 | {"counter", required_argument, 0, 'c'}, |
| 3475 | {"Dump", no_argument, 0, 'D'}, |
| 3476 | {"debug", no_argument, 0, 'd'}, |
| 3477 | {"interval", required_argument, 0, 'i'}, |
| 3478 | {"help", no_argument, 0, 'h'}, |
| 3479 | {"Joules", no_argument, 0, 'J'}, |
| 3480 | {"MSR", required_argument, 0, 'M'}, |
| 3481 | {"msr", required_argument, 0, 'm'}, |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3482 | {"out", required_argument, 0, 'o'}, |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3483 | {"Package", no_argument, 0, 'p'}, |
| 3484 | {"processor", no_argument, 0, 'p'}, |
| 3485 | {"Summary", no_argument, 0, 'S'}, |
| 3486 | {"TCC", required_argument, 0, 'T'}, |
| 3487 | {"version", no_argument, 0, 'v' }, |
| 3488 | {0, 0, 0, 0 } |
| 3489 | }; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3490 | |
| 3491 | progname = argv[0]; |
| 3492 | |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3493 | while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v", |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3494 | long_options, &option_index)) != -1) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3495 | switch (opt) { |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3496 | case 'C': |
| 3497 | sscanf(optarg, "%x", &extra_delta_offset64); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3498 | break; |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 3499 | case 'c': |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 3500 | sscanf(optarg, "%x", &extra_delta_offset32); |
| 3501 | break; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3502 | case 'D': |
| 3503 | dump_only++; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 3504 | break; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3505 | case 'd': |
| 3506 | debug++; |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 3507 | break; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3508 | case 'h': |
| 3509 | default: |
| 3510 | help(); |
| 3511 | exit(1); |
| 3512 | case 'i': |
Len Brown | 2a0609c | 2016-02-12 22:44:48 -0500 | [diff] [blame] | 3513 | { |
| 3514 | double interval = strtod(optarg, NULL); |
| 3515 | |
| 3516 | if (interval < 0.001) { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3517 | fprintf(outf, "interval %f seconds is too small\n", |
Len Brown | 2a0609c | 2016-02-12 22:44:48 -0500 | [diff] [blame] | 3518 | interval); |
| 3519 | exit(2); |
| 3520 | } |
| 3521 | |
| 3522 | interval_ts.tv_sec = interval; |
| 3523 | interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; |
| 3524 | } |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 3525 | break; |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 3526 | case 'J': |
| 3527 | rapl_joules++; |
| 3528 | break; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3529 | case 'M': |
| 3530 | sscanf(optarg, "%x", &extra_msr_offset64); |
| 3531 | break; |
| 3532 | case 'm': |
| 3533 | sscanf(optarg, "%x", &extra_msr_offset32); |
| 3534 | break; |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3535 | case 'o': |
| 3536 | outf = fopen_or_die(optarg, "w"); |
| 3537 | break; |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3538 | case 'P': |
| 3539 | show_pkg_only++; |
| 3540 | break; |
| 3541 | case 'p': |
| 3542 | show_core_only++; |
| 3543 | break; |
| 3544 | case 'S': |
| 3545 | summary_only++; |
| 3546 | break; |
| 3547 | case 'T': |
| 3548 | tcc_activation_temp_override = atoi(optarg); |
| 3549 | break; |
| 3550 | case 'v': |
| 3551 | print_version(); |
| 3552 | exit(0); |
| 3553 | break; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3554 | } |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | int main(int argc, char **argv) |
| 3559 | { |
Len Brown | b7d8c14 | 2016-02-13 23:36:17 -0500 | [diff] [blame] | 3560 | outf = stderr; |
| 3561 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3562 | cmdline(argc, argv); |
| 3563 | |
Len Brown | d8af6f5 | 2015-02-10 01:56:38 -0500 | [diff] [blame] | 3564 | if (debug) |
| 3565 | print_version(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3566 | |
| 3567 | turbostat_init(); |
| 3568 | |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame] | 3569 | /* dump counters and exit */ |
| 3570 | if (dump_only) |
| 3571 | return get_and_dump_counters(); |
| 3572 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 3573 | /* |
| 3574 | * if any params left, it must be a command to fork |
| 3575 | */ |
| 3576 | if (argc - optind) |
| 3577 | return fork_it(argv + optind); |
| 3578 | else |
| 3579 | turbostat_loop(); |
| 3580 | |
| 3581 | return 0; |
| 3582 | } |