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> |
| 36 | #include <dirent.h> |
| 37 | #include <string.h> |
| 38 | #include <ctype.h> |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 39 | #include <sched.h> |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 40 | #include <cpuid.h> |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 41 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 42 | char *proc_stat = "/proc/stat"; |
| 43 | unsigned int interval_sec = 5; /* set with -i interval_sec */ |
| 44 | unsigned int verbose; /* set with -v */ |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 45 | unsigned int rapl_verbose; /* set with -R */ |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 46 | unsigned int rapl_joules; /* set with -J */ |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 47 | unsigned int thermal_verbose; /* set with -T */ |
Andy Shevchenko | f591c38 | 2014-01-23 17:13:14 +0200 | [diff] [blame] | 48 | unsigned int summary_only; /* set with -S */ |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 49 | unsigned int dump_only; /* set with -s */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 50 | unsigned int skip_c0; |
| 51 | unsigned int skip_c1; |
| 52 | unsigned int do_nhm_cstates; |
| 53 | unsigned int do_snb_cstates; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 54 | unsigned int do_c8_c9_c10; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 55 | unsigned int do_slm_cstates; |
| 56 | unsigned int use_c1_residency_msr; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 57 | unsigned int has_aperf; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 58 | unsigned int has_epb; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 59 | unsigned int units = 1000000000; /* Ghz etc */ |
| 60 | unsigned int genuine_intel; |
| 61 | unsigned int has_invariant_tsc; |
| 62 | unsigned int do_nehalem_platform_info; |
| 63 | unsigned int do_nehalem_turbo_ratio_limit; |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 64 | unsigned int do_ivt_turbo_ratio_limit; |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 65 | unsigned int extra_msr_offset32; |
| 66 | unsigned int extra_msr_offset64; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 67 | unsigned int extra_delta_offset32; |
| 68 | unsigned int extra_delta_offset64; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 69 | int do_smi; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 70 | double bclk; |
| 71 | unsigned int show_pkg; |
| 72 | unsigned int show_core; |
| 73 | unsigned int show_cpu; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 74 | unsigned int show_pkg_only; |
| 75 | unsigned int show_core_only; |
| 76 | char *output_buffer, *outp; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 77 | unsigned int do_rapl; |
| 78 | unsigned int do_dts; |
| 79 | unsigned int do_ptm; |
| 80 | unsigned int tcc_activation_temp; |
| 81 | unsigned int tcc_activation_temp_override; |
| 82 | double rapl_power_units, rapl_energy_units, rapl_time_units; |
| 83 | double rapl_joule_counter_range; |
| 84 | |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 85 | #define RAPL_PKG (1 << 0) |
| 86 | /* 0x610 MSR_PKG_POWER_LIMIT */ |
| 87 | /* 0x611 MSR_PKG_ENERGY_STATUS */ |
| 88 | #define RAPL_PKG_PERF_STATUS (1 << 1) |
| 89 | /* 0x613 MSR_PKG_PERF_STATUS */ |
| 90 | #define RAPL_PKG_POWER_INFO (1 << 2) |
| 91 | /* 0x614 MSR_PKG_POWER_INFO */ |
| 92 | |
| 93 | #define RAPL_DRAM (1 << 3) |
| 94 | /* 0x618 MSR_DRAM_POWER_LIMIT */ |
| 95 | /* 0x619 MSR_DRAM_ENERGY_STATUS */ |
| 96 | /* 0x61c MSR_DRAM_POWER_INFO */ |
| 97 | #define RAPL_DRAM_PERF_STATUS (1 << 4) |
| 98 | /* 0x61b MSR_DRAM_PERF_STATUS */ |
| 99 | |
| 100 | #define RAPL_CORES (1 << 5) |
| 101 | /* 0x638 MSR_PP0_POWER_LIMIT */ |
| 102 | /* 0x639 MSR_PP0_ENERGY_STATUS */ |
| 103 | #define RAPL_CORE_POLICY (1 << 6) |
| 104 | /* 0x63a MSR_PP0_POLICY */ |
| 105 | |
| 106 | |
| 107 | #define RAPL_GFX (1 << 7) |
| 108 | /* 0x640 MSR_PP1_POWER_LIMIT */ |
| 109 | /* 0x641 MSR_PP1_ENERGY_STATUS */ |
| 110 | /* 0x642 MSR_PP1_POLICY */ |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 111 | #define TJMAX_DEFAULT 100 |
| 112 | |
| 113 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 114 | |
| 115 | int aperf_mperf_unstable; |
| 116 | int backwards_count; |
| 117 | char *progname; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 118 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 119 | cpu_set_t *cpu_present_set, *cpu_affinity_set; |
| 120 | size_t cpu_present_setsize, cpu_affinity_setsize; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 121 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 122 | struct thread_data { |
| 123 | unsigned long long tsc; |
| 124 | unsigned long long aperf; |
| 125 | unsigned long long mperf; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 126 | unsigned long long c1; |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 127 | unsigned long long extra_msr64; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 128 | unsigned long long extra_delta64; |
| 129 | unsigned long long extra_msr32; |
| 130 | unsigned long long extra_delta32; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 131 | unsigned int smi_count; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 132 | unsigned int cpu_id; |
| 133 | unsigned int flags; |
| 134 | #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 |
| 135 | #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 |
| 136 | } *thread_even, *thread_odd; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 137 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 138 | struct core_data { |
| 139 | unsigned long long c3; |
| 140 | unsigned long long c6; |
| 141 | unsigned long long c7; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 142 | unsigned int core_temp_c; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 143 | unsigned int core_id; |
| 144 | } *core_even, *core_odd; |
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 | struct pkg_data { |
| 147 | unsigned long long pc2; |
| 148 | unsigned long long pc3; |
| 149 | unsigned long long pc6; |
| 150 | unsigned long long pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 151 | unsigned long long pc8; |
| 152 | unsigned long long pc9; |
| 153 | unsigned long long pc10; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 154 | unsigned int package_id; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 155 | unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ |
| 156 | unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ |
| 157 | unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ |
| 158 | unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ |
| 159 | unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ |
| 160 | unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ |
| 161 | unsigned int pkg_temp_c; |
| 162 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 163 | } *package_even, *package_odd; |
| 164 | |
| 165 | #define ODD_COUNTERS thread_odd, core_odd, package_odd |
| 166 | #define EVEN_COUNTERS thread_even, core_even, package_even |
| 167 | |
| 168 | #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ |
| 169 | (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ |
| 170 | topo.num_threads_per_core + \ |
| 171 | (core_no) * topo.num_threads_per_core + (thread_no)) |
| 172 | #define GET_CORE(core_base, core_no, pkg_no) \ |
| 173 | (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) |
| 174 | #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) |
| 175 | |
| 176 | struct system_summary { |
| 177 | struct thread_data threads; |
| 178 | struct core_data cores; |
| 179 | struct pkg_data packages; |
| 180 | } sum, average; |
| 181 | |
| 182 | |
| 183 | struct topo_params { |
| 184 | int num_packages; |
| 185 | int num_cpus; |
| 186 | int num_cores; |
| 187 | int max_cpu_num; |
| 188 | int num_cores_per_pkg; |
| 189 | int num_threads_per_core; |
| 190 | } topo; |
| 191 | |
| 192 | struct timeval tv_even, tv_odd, tv_delta; |
| 193 | |
| 194 | void setup_all_buffers(void); |
| 195 | |
| 196 | int cpu_is_not_present(int cpu) |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 197 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 198 | return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 199 | } |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 200 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 201 | * run func(thread, core, package) in topology order |
| 202 | * skip non-present cpus |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 203 | */ |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 204 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 205 | int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), |
| 206 | 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] | 207 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 208 | int retval, pkg_no, core_no, thread_no; |
| 209 | |
| 210 | for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { |
| 211 | for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { |
| 212 | for (thread_no = 0; thread_no < |
| 213 | topo.num_threads_per_core; ++thread_no) { |
| 214 | struct thread_data *t; |
| 215 | struct core_data *c; |
| 216 | struct pkg_data *p; |
| 217 | |
| 218 | t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); |
| 219 | |
| 220 | if (cpu_is_not_present(t->cpu_id)) |
| 221 | continue; |
| 222 | |
| 223 | c = GET_CORE(core_base, core_no, pkg_no); |
| 224 | p = GET_PKG(pkg_base, pkg_no); |
| 225 | |
| 226 | retval = func(t, c, p); |
| 227 | if (retval) |
| 228 | return retval; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | return 0; |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | int cpu_migrate(int cpu) |
| 236 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 237 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
| 238 | CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); |
| 239 | if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 240 | return -1; |
| 241 | else |
| 242 | return 0; |
| 243 | } |
| 244 | |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 245 | int get_msr(int cpu, off_t offset, unsigned long long *msr) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 246 | { |
| 247 | ssize_t retval; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 248 | char pathname[32]; |
| 249 | int fd; |
| 250 | |
| 251 | sprintf(pathname, "/dev/cpu/%d/msr", cpu); |
| 252 | fd = open(pathname, O_RDONLY); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 253 | if (fd < 0) |
| 254 | return -1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 255 | |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 256 | retval = pread(fd, msr, sizeof *msr, offset); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 257 | close(fd); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 258 | |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 259 | if (retval != sizeof *msr) { |
Josh Triplett | 2e9c6bc | 2013-08-20 17:20:13 -0700 | [diff] [blame] | 260 | fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 261 | return -1; |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 262 | } |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 263 | |
| 264 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Len Brown | a829eb4 | 2011-02-10 23:36:34 -0500 | [diff] [blame] | 267 | void print_header(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 268 | { |
| 269 | if (show_pkg) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 270 | outp += sprintf(outp, "pk"); |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 271 | if (show_pkg) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 272 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 273 | if (show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 274 | outp += sprintf(outp, "cor"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 275 | if (show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 276 | outp += sprintf(outp, " CPU"); |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 277 | if (show_pkg || show_core || show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 278 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 279 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 280 | outp += sprintf(outp, " %%c0"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 281 | if (has_aperf) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 282 | outp += sprintf(outp, " GHz"); |
| 283 | outp += sprintf(outp, " TSC"); |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 284 | if (do_smi) |
| 285 | outp += sprintf(outp, " SMI"); |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 286 | if (extra_delta_offset32) |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 287 | outp += sprintf(outp, " count 0x%03X", extra_delta_offset32); |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 288 | if (extra_delta_offset64) |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 289 | outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 290 | if (extra_msr_offset32) |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 291 | outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 292 | if (extra_msr_offset64) |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 293 | outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 294 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 295 | outp += sprintf(outp, " %%c1"); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 296 | if (do_nhm_cstates && !do_slm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 297 | outp += sprintf(outp, " %%c3"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 298 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 299 | outp += sprintf(outp, " %%c6"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 300 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 301 | outp += sprintf(outp, " %%c7"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 302 | |
| 303 | if (do_dts) |
| 304 | outp += sprintf(outp, " CTMP"); |
| 305 | if (do_ptm) |
| 306 | outp += sprintf(outp, " PTMP"); |
| 307 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 308 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 309 | outp += sprintf(outp, " %%pc2"); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 310 | if (do_nhm_cstates && !do_slm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 311 | outp += sprintf(outp, " %%pc3"); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 312 | if (do_nhm_cstates && !do_slm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 313 | outp += sprintf(outp, " %%pc6"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 314 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 315 | outp += sprintf(outp, " %%pc7"); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 316 | if (do_c8_c9_c10) { |
| 317 | outp += sprintf(outp, " %%pc8"); |
| 318 | outp += sprintf(outp, " %%pc9"); |
| 319 | outp += sprintf(outp, " %%pc10"); |
| 320 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 321 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 322 | if (do_rapl && !rapl_joules) { |
| 323 | if (do_rapl & RAPL_PKG) |
| 324 | outp += sprintf(outp, " Pkg_W"); |
| 325 | if (do_rapl & RAPL_CORES) |
| 326 | outp += sprintf(outp, " Cor_W"); |
| 327 | if (do_rapl & RAPL_GFX) |
| 328 | outp += sprintf(outp, " GFX_W"); |
| 329 | if (do_rapl & RAPL_DRAM) |
| 330 | outp += sprintf(outp, " RAM_W"); |
| 331 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
| 332 | outp += sprintf(outp, " PKG_%%"); |
| 333 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
| 334 | outp += sprintf(outp, " RAM_%%"); |
| 335 | } else { |
| 336 | if (do_rapl & RAPL_PKG) |
| 337 | outp += sprintf(outp, " Pkg_J"); |
| 338 | if (do_rapl & RAPL_CORES) |
| 339 | outp += sprintf(outp, " Cor_J"); |
| 340 | if (do_rapl & RAPL_GFX) |
| 341 | outp += sprintf(outp, " GFX_J"); |
| 342 | if (do_rapl & RAPL_DRAM) |
| 343 | outp += sprintf(outp, " RAM_W"); |
| 344 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
| 345 | outp += sprintf(outp, " PKG_%%"); |
| 346 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
| 347 | outp += sprintf(outp, " RAM_%%"); |
| 348 | outp += sprintf(outp, " time"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 349 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 350 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 351 | outp += sprintf(outp, "\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 354 | int dump_counters(struct thread_data *t, struct core_data *c, |
| 355 | struct pkg_data *p) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 356 | { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 357 | 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] | 358 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 359 | if (t) { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 360 | outp += sprintf(outp, "CPU: %d flags 0x%x\n", |
| 361 | t->cpu_id, t->flags); |
| 362 | outp += sprintf(outp, "TSC: %016llX\n", t->tsc); |
| 363 | outp += sprintf(outp, "aperf: %016llX\n", t->aperf); |
| 364 | outp += sprintf(outp, "mperf: %016llX\n", t->mperf); |
| 365 | outp += sprintf(outp, "c1: %016llX\n", t->c1); |
| 366 | outp += sprintf(outp, "msr0x%x: %08llX\n", |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 367 | extra_delta_offset32, t->extra_delta32); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 368 | outp += sprintf(outp, "msr0x%x: %016llX\n", |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 369 | extra_delta_offset64, t->extra_delta64); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 370 | outp += sprintf(outp, "msr0x%x: %08llX\n", |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 371 | extra_msr_offset32, t->extra_msr32); |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 372 | outp += sprintf(outp, "msr0x%x: %016llX\n", |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 373 | extra_msr_offset64, t->extra_msr64); |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 374 | if (do_smi) |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 375 | outp += sprintf(outp, "SMI: %08X\n", t->smi_count); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 376 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 377 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 378 | if (c) { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 379 | outp += sprintf(outp, "core: %d\n", c->core_id); |
| 380 | outp += sprintf(outp, "c3: %016llX\n", c->c3); |
| 381 | outp += sprintf(outp, "c6: %016llX\n", c->c6); |
| 382 | outp += sprintf(outp, "c7: %016llX\n", c->c7); |
| 383 | outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (p) { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 387 | outp += sprintf(outp, "package: %d\n", p->package_id); |
| 388 | outp += sprintf(outp, "pc2: %016llX\n", p->pc2); |
| 389 | outp += sprintf(outp, "pc3: %016llX\n", p->pc3); |
| 390 | outp += sprintf(outp, "pc6: %016llX\n", p->pc6); |
| 391 | outp += sprintf(outp, "pc7: %016llX\n", p->pc7); |
| 392 | outp += sprintf(outp, "pc8: %016llX\n", p->pc8); |
| 393 | outp += sprintf(outp, "pc9: %016llX\n", p->pc9); |
| 394 | outp += sprintf(outp, "pc10: %016llX\n", p->pc10); |
| 395 | outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); |
| 396 | outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); |
| 397 | outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); |
| 398 | outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); |
| 399 | outp += sprintf(outp, "Throttle PKG: %0X\n", |
| 400 | p->rapl_pkg_perf_status); |
| 401 | outp += sprintf(outp, "Throttle RAM: %0X\n", |
| 402 | p->rapl_dram_perf_status); |
| 403 | outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 404 | } |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 405 | |
| 406 | outp += sprintf(outp, "\n"); |
| 407 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 408 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 411 | /* |
| 412 | * column formatting convention & formats |
| 413 | * package: "pk" 2 columns %2d |
| 414 | * core: "cor" 3 columns %3d |
| 415 | * CPU: "CPU" 3 columns %3d |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 416 | * Pkg_W: %6.2 |
| 417 | * Cor_W: %6.2 |
| 418 | * GFX_W: %5.2 |
| 419 | * RAM_W: %5.2 |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 420 | * GHz: "GHz" 3 columns %3.2 |
| 421 | * TSC: "TSC" 3 columns %3.2 |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 422 | * SMI: "SMI" 4 columns %4d |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 423 | * percentage " %pc3" %6.2 |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 424 | * Perf Status percentage: %5.2 |
| 425 | * "CTMP" 4 columns %4d |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 426 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 427 | int format_counters(struct thread_data *t, struct core_data *c, |
| 428 | struct pkg_data *p) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 429 | { |
| 430 | double interval_float; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 431 | char *fmt5, *fmt6; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 432 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 433 | /* if showing only 1st thread in core and this isn't one, bail out */ |
| 434 | if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 435 | return 0; |
| 436 | |
| 437 | /* if showing only 1st thread in pkg and this isn't one, bail out */ |
| 438 | if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 439 | return 0; |
| 440 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 441 | interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; |
| 442 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 443 | /* topo columns, print blanks on 1st (average) line */ |
| 444 | if (t == &average.threads) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 445 | if (show_pkg) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 446 | outp += sprintf(outp, " "); |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 447 | if (show_pkg && show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 448 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 449 | if (show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 450 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 451 | if (show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 452 | outp += sprintf(outp, " " " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 453 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 454 | if (show_pkg) { |
| 455 | if (p) |
| 456 | outp += sprintf(outp, "%2d", p->package_id); |
| 457 | else |
| 458 | outp += sprintf(outp, " "); |
| 459 | } |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 460 | if (show_pkg && show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 461 | outp += sprintf(outp, " "); |
| 462 | if (show_core) { |
| 463 | if (c) |
| 464 | outp += sprintf(outp, "%3d", c->core_id); |
| 465 | else |
| 466 | outp += sprintf(outp, " "); |
| 467 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 468 | if (show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 469 | outp += sprintf(outp, " %3d", t->cpu_id); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 470 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 471 | /* %c0 */ |
| 472 | if (do_nhm_cstates) { |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 473 | if (show_pkg || show_core || show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 474 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 475 | if (!skip_c0) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 476 | outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 477 | else |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 478 | outp += sprintf(outp, " ****"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* GHz */ |
| 482 | if (has_aperf) { |
| 483 | if (!aperf_mperf_unstable) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 484 | outp += sprintf(outp, " %3.2f", |
| 485 | 1.0 * t->tsc / units * t->aperf / |
| 486 | t->mperf / interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 487 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 488 | if (t->aperf > t->tsc || t->mperf > t->tsc) { |
| 489 | outp += sprintf(outp, " ***"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 490 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 491 | outp += sprintf(outp, "%3.1f*", |
| 492 | 1.0 * t->tsc / |
| 493 | units * t->aperf / |
| 494 | t->mperf / interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* TSC */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 500 | outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 501 | |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 502 | /* SMI */ |
| 503 | if (do_smi) |
| 504 | outp += sprintf(outp, "%4d", t->smi_count); |
| 505 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 506 | /* delta */ |
| 507 | if (extra_delta_offset32) |
| 508 | outp += sprintf(outp, " %11llu", t->extra_delta32); |
| 509 | |
| 510 | /* DELTA */ |
| 511 | if (extra_delta_offset64) |
| 512 | outp += sprintf(outp, " %11llu", t->extra_delta64); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 513 | /* msr */ |
| 514 | if (extra_msr_offset32) |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 515 | outp += sprintf(outp, " 0x%08llx", t->extra_msr32); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 516 | |
Len Brown | 130ff30 | 2012-09-21 22:56:06 -0400 | [diff] [blame] | 517 | /* MSR */ |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 518 | if (extra_msr_offset64) |
| 519 | outp += sprintf(outp, " 0x%016llx", t->extra_msr64); |
Len Brown | 130ff30 | 2012-09-21 22:56:06 -0400 | [diff] [blame] | 520 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 521 | if (do_nhm_cstates) { |
| 522 | if (!skip_c1) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 523 | outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 524 | else |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 525 | outp += sprintf(outp, " ****"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 526 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 527 | |
| 528 | /* print per-core data only for 1st thread in core */ |
| 529 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 530 | goto done; |
| 531 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 532 | if (do_nhm_cstates && !do_slm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 533 | outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 534 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 535 | outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 536 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 537 | outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); |
| 538 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 539 | if (do_dts) |
| 540 | outp += sprintf(outp, " %4d", c->core_temp_c); |
| 541 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 542 | /* print per-package data only for 1st core in package */ |
| 543 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 544 | goto done; |
| 545 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 546 | if (do_ptm) |
| 547 | outp += sprintf(outp, " %4d", p->pkg_temp_c); |
| 548 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 549 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 550 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 551 | if (do_nhm_cstates && !do_slm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 552 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 553 | if (do_nhm_cstates && !do_slm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 554 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 555 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 556 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 557 | if (do_c8_c9_c10) { |
| 558 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc); |
| 559 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc); |
| 560 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc); |
| 561 | } |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 562 | |
| 563 | /* |
| 564 | * If measurement interval exceeds minimum RAPL Joule Counter range, |
| 565 | * indicate that results are suspect by printing "**" in fraction place. |
| 566 | */ |
| 567 | if (interval_float < rapl_joule_counter_range) { |
| 568 | fmt5 = " %5.2f"; |
| 569 | fmt6 = " %6.2f"; |
| 570 | } else { |
| 571 | fmt5 = " %3.0f**"; |
| 572 | fmt6 = " %4.0f**"; |
| 573 | } |
| 574 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 575 | if (do_rapl && !rapl_joules) { |
| 576 | if (do_rapl & RAPL_PKG) |
| 577 | outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float); |
| 578 | if (do_rapl & RAPL_CORES) |
| 579 | outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float); |
| 580 | if (do_rapl & RAPL_GFX) |
| 581 | outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float); |
| 582 | if (do_rapl & RAPL_DRAM) |
| 583 | outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float); |
| 584 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
| 585 | outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); |
| 586 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
| 587 | outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); |
| 588 | } else { |
| 589 | if (do_rapl & RAPL_PKG) |
| 590 | outp += sprintf(outp, fmt6, |
| 591 | p->energy_pkg * rapl_energy_units); |
| 592 | if (do_rapl & RAPL_CORES) |
| 593 | outp += sprintf(outp, fmt6, |
| 594 | p->energy_cores * rapl_energy_units); |
| 595 | if (do_rapl & RAPL_GFX) |
| 596 | outp += sprintf(outp, fmt5, |
| 597 | p->energy_gfx * rapl_energy_units); |
| 598 | if (do_rapl & RAPL_DRAM) |
| 599 | outp += sprintf(outp, fmt5, |
| 600 | p->energy_dram * rapl_energy_units); |
| 601 | if (do_rapl & RAPL_PKG_PERF_STATUS) |
| 602 | outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); |
| 603 | if (do_rapl & RAPL_DRAM_PERF_STATUS) |
| 604 | outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); |
| 605 | outp += sprintf(outp, fmt5, interval_float); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 606 | |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 607 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 608 | done: |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 609 | outp += sprintf(outp, "\n"); |
| 610 | |
| 611 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 612 | } |
| 613 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 614 | void flush_stdout() |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 615 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 616 | fputs(output_buffer, stdout); |
Len Brown | ddac0d6 | 2012-11-30 01:01:40 -0500 | [diff] [blame] | 617 | fflush(stdout); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 618 | outp = output_buffer; |
| 619 | } |
| 620 | void flush_stderr() |
| 621 | { |
| 622 | fputs(output_buffer, stderr); |
| 623 | outp = output_buffer; |
| 624 | } |
| 625 | void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 626 | { |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 627 | static int printed; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 628 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 629 | if (!printed || !summary_only) |
| 630 | print_header(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 631 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 632 | if (topo.num_cpus > 1) |
| 633 | format_counters(&average.threads, &average.cores, |
| 634 | &average.packages); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 635 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 636 | printed = 1; |
| 637 | |
| 638 | if (summary_only) |
| 639 | return; |
| 640 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 641 | for_all_cpus(format_counters, t, c, p); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 644 | #define DELTA_WRAP32(new, old) \ |
| 645 | if (new > old) { \ |
| 646 | old = new - old; \ |
| 647 | } else { \ |
| 648 | old = 0x100000000 + new - old; \ |
| 649 | } |
| 650 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 651 | void |
| 652 | delta_package(struct pkg_data *new, struct pkg_data *old) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 653 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 654 | old->pc2 = new->pc2 - old->pc2; |
| 655 | old->pc3 = new->pc3 - old->pc3; |
| 656 | old->pc6 = new->pc6 - old->pc6; |
| 657 | old->pc7 = new->pc7 - old->pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 658 | old->pc8 = new->pc8 - old->pc8; |
| 659 | old->pc9 = new->pc9 - old->pc9; |
| 660 | old->pc10 = new->pc10 - old->pc10; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 661 | old->pkg_temp_c = new->pkg_temp_c; |
| 662 | |
| 663 | DELTA_WRAP32(new->energy_pkg, old->energy_pkg); |
| 664 | DELTA_WRAP32(new->energy_cores, old->energy_cores); |
| 665 | DELTA_WRAP32(new->energy_gfx, old->energy_gfx); |
| 666 | DELTA_WRAP32(new->energy_dram, old->energy_dram); |
| 667 | DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); |
| 668 | DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 669 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 670 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 671 | void |
| 672 | delta_core(struct core_data *new, struct core_data *old) |
| 673 | { |
| 674 | old->c3 = new->c3 - old->c3; |
| 675 | old->c6 = new->c6 - old->c6; |
| 676 | old->c7 = new->c7 - old->c7; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 677 | old->core_temp_c = new->core_temp_c; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 678 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 679 | |
Len Brown | c3ae331 | 2012-06-13 21:31:46 -0400 | [diff] [blame] | 680 | /* |
| 681 | * old = new - old |
| 682 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 683 | void |
| 684 | delta_thread(struct thread_data *new, struct thread_data *old, |
| 685 | struct core_data *core_delta) |
| 686 | { |
| 687 | old->tsc = new->tsc - old->tsc; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 688 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 689 | /* check for TSC < 1 Mcycles over interval */ |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 690 | if (old->tsc < (1000 * 1000)) |
| 691 | errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" |
| 692 | "You can disable all c-states by booting with \"idle=poll\"\n" |
| 693 | "or just the deep ones with \"processor.max_cstate=1\""); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 694 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 695 | old->c1 = new->c1 - old->c1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 696 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 697 | if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { |
| 698 | old->aperf = new->aperf - old->aperf; |
| 699 | old->mperf = new->mperf - old->mperf; |
| 700 | } else { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 701 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 702 | if (!aperf_mperf_unstable) { |
| 703 | fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); |
| 704 | fprintf(stderr, "* Frequency results do not cover entire interval *\n"); |
| 705 | fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); |
| 706 | |
| 707 | aperf_mperf_unstable = 1; |
| 708 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 709 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 710 | * mperf delta is likely a huge "positive" number |
| 711 | * can not use it for calculating c0 time |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 712 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 713 | skip_c0 = 1; |
| 714 | skip_c1 = 1; |
| 715 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 716 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 717 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 718 | if (use_c1_residency_msr) { |
| 719 | /* |
| 720 | * Some models have a dedicated C1 residency MSR, |
| 721 | * which should be more accurate than the derivation below. |
| 722 | */ |
| 723 | } else { |
| 724 | /* |
| 725 | * As counter collection is not atomic, |
| 726 | * it is possible for mperf's non-halted cycles + idle states |
| 727 | * to exceed TSC's all cycles: show c1 = 0% in that case. |
| 728 | */ |
| 729 | if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) |
| 730 | old->c1 = 0; |
| 731 | else { |
| 732 | /* normal case, derive c1 */ |
| 733 | old->c1 = old->tsc - old->mperf - core_delta->c3 |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 734 | - core_delta->c6 - core_delta->c7; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 735 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 736 | } |
Len Brown | c3ae331 | 2012-06-13 21:31:46 -0400 | [diff] [blame] | 737 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 738 | if (old->mperf == 0) { |
Len Brown | c3ae331 | 2012-06-13 21:31:46 -0400 | [diff] [blame] | 739 | if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 740 | old->mperf = 1; /* divide by 0 protection */ |
| 741 | } |
| 742 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 743 | old->extra_delta32 = new->extra_delta32 - old->extra_delta32; |
| 744 | old->extra_delta32 &= 0xFFFFFFFF; |
| 745 | |
| 746 | old->extra_delta64 = new->extra_delta64 - old->extra_delta64; |
| 747 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 748 | /* |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 749 | * Extra MSR is just a snapshot, simply copy latest w/o subtracting |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 750 | */ |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 751 | old->extra_msr32 = new->extra_msr32; |
| 752 | old->extra_msr64 = new->extra_msr64; |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 753 | |
| 754 | if (do_smi) |
| 755 | old->smi_count = new->smi_count - old->smi_count; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | int delta_cpu(struct thread_data *t, struct core_data *c, |
| 759 | struct pkg_data *p, struct thread_data *t2, |
| 760 | struct core_data *c2, struct pkg_data *p2) |
| 761 | { |
| 762 | /* calculate core delta only for 1st thread in core */ |
| 763 | if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) |
| 764 | delta_core(c, c2); |
| 765 | |
| 766 | /* always calculate thread delta */ |
| 767 | delta_thread(t, t2, c2); /* c2 is core delta */ |
| 768 | |
| 769 | /* calculate package delta only for 1st core in package */ |
| 770 | if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) |
| 771 | delta_package(p, p2); |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 777 | { |
| 778 | t->tsc = 0; |
| 779 | t->aperf = 0; |
| 780 | t->mperf = 0; |
| 781 | t->c1 = 0; |
| 782 | |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 783 | t->smi_count = 0; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 784 | t->extra_delta32 = 0; |
| 785 | t->extra_delta64 = 0; |
| 786 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 787 | /* tells format_counters to dump all fields from this set */ |
| 788 | t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; |
| 789 | |
| 790 | c->c3 = 0; |
| 791 | c->c6 = 0; |
| 792 | c->c7 = 0; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 793 | c->core_temp_c = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 794 | |
| 795 | p->pc2 = 0; |
| 796 | p->pc3 = 0; |
| 797 | p->pc6 = 0; |
| 798 | p->pc7 = 0; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 799 | p->pc8 = 0; |
| 800 | p->pc9 = 0; |
| 801 | p->pc10 = 0; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 802 | |
| 803 | p->energy_pkg = 0; |
| 804 | p->energy_dram = 0; |
| 805 | p->energy_cores = 0; |
| 806 | p->energy_gfx = 0; |
| 807 | p->rapl_pkg_perf_status = 0; |
| 808 | p->rapl_dram_perf_status = 0; |
| 809 | p->pkg_temp_c = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 810 | } |
| 811 | int sum_counters(struct thread_data *t, struct core_data *c, |
| 812 | struct pkg_data *p) |
| 813 | { |
| 814 | average.threads.tsc += t->tsc; |
| 815 | average.threads.aperf += t->aperf; |
| 816 | average.threads.mperf += t->mperf; |
| 817 | average.threads.c1 += t->c1; |
| 818 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 819 | average.threads.extra_delta32 += t->extra_delta32; |
| 820 | average.threads.extra_delta64 += t->extra_delta64; |
| 821 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 822 | /* sum per-core values only for 1st thread in core */ |
| 823 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 824 | return 0; |
| 825 | |
| 826 | average.cores.c3 += c->c3; |
| 827 | average.cores.c6 += c->c6; |
| 828 | average.cores.c7 += c->c7; |
| 829 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 830 | average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); |
| 831 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 832 | /* sum per-pkg values only for 1st core in pkg */ |
| 833 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 834 | return 0; |
| 835 | |
| 836 | average.packages.pc2 += p->pc2; |
| 837 | average.packages.pc3 += p->pc3; |
| 838 | average.packages.pc6 += p->pc6; |
| 839 | average.packages.pc7 += p->pc7; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 840 | average.packages.pc8 += p->pc8; |
| 841 | average.packages.pc9 += p->pc9; |
| 842 | average.packages.pc10 += p->pc10; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 843 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 844 | average.packages.energy_pkg += p->energy_pkg; |
| 845 | average.packages.energy_dram += p->energy_dram; |
| 846 | average.packages.energy_cores += p->energy_cores; |
| 847 | average.packages.energy_gfx += p->energy_gfx; |
| 848 | |
| 849 | average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); |
| 850 | |
| 851 | average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; |
| 852 | average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | /* |
| 856 | * sum the counters for all cpus in the system |
| 857 | * compute the weighted average |
| 858 | */ |
| 859 | void compute_average(struct thread_data *t, struct core_data *c, |
| 860 | struct pkg_data *p) |
| 861 | { |
| 862 | clear_counters(&average.threads, &average.cores, &average.packages); |
| 863 | |
| 864 | for_all_cpus(sum_counters, t, c, p); |
| 865 | |
| 866 | average.threads.tsc /= topo.num_cpus; |
| 867 | average.threads.aperf /= topo.num_cpus; |
| 868 | average.threads.mperf /= topo.num_cpus; |
| 869 | average.threads.c1 /= topo.num_cpus; |
| 870 | |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 871 | average.threads.extra_delta32 /= topo.num_cpus; |
| 872 | average.threads.extra_delta32 &= 0xFFFFFFFF; |
| 873 | |
| 874 | average.threads.extra_delta64 /= topo.num_cpus; |
| 875 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 876 | average.cores.c3 /= topo.num_cores; |
| 877 | average.cores.c6 /= topo.num_cores; |
| 878 | average.cores.c7 /= topo.num_cores; |
| 879 | |
| 880 | average.packages.pc2 /= topo.num_packages; |
| 881 | average.packages.pc3 /= topo.num_packages; |
| 882 | average.packages.pc6 /= topo.num_packages; |
| 883 | average.packages.pc7 /= topo.num_packages; |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 884 | |
| 885 | average.packages.pc8 /= topo.num_packages; |
| 886 | average.packages.pc9 /= topo.num_packages; |
| 887 | average.packages.pc10 /= topo.num_packages; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static unsigned long long rdtsc(void) |
| 891 | { |
| 892 | unsigned int low, high; |
| 893 | |
| 894 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
| 895 | |
| 896 | return low | ((unsigned long long)high) << 32; |
| 897 | } |
| 898 | |
| 899 | |
| 900 | /* |
| 901 | * get_counters(...) |
| 902 | * migrate to cpu |
| 903 | * acquire and record local counters for that cpu |
| 904 | */ |
| 905 | int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 906 | { |
| 907 | int cpu = t->cpu_id; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 908 | unsigned long long msr; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 909 | |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 910 | if (cpu_migrate(cpu)) { |
| 911 | fprintf(stderr, "Could not migrate to CPU %d\n", cpu); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 912 | return -1; |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 913 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 914 | |
| 915 | t->tsc = rdtsc(); /* we are running on local CPU of interest */ |
| 916 | |
| 917 | if (has_aperf) { |
Len Brown | 9c63a65 | 2012-10-31 01:29:52 -0400 | [diff] [blame] | 918 | if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 919 | return -3; |
Len Brown | 9c63a65 | 2012-10-31 01:29:52 -0400 | [diff] [blame] | 920 | if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 921 | return -4; |
| 922 | } |
| 923 | |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 924 | if (do_smi) { |
| 925 | if (get_msr(cpu, MSR_SMI_COUNT, &msr)) |
| 926 | return -5; |
| 927 | t->smi_count = msr & 0xFFFFFFFF; |
| 928 | } |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 929 | if (extra_delta_offset32) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 930 | if (get_msr(cpu, extra_delta_offset32, &msr)) |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 931 | return -5; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 932 | t->extra_delta32 = msr & 0xFFFFFFFF; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | if (extra_delta_offset64) |
| 936 | if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64)) |
| 937 | return -5; |
| 938 | |
| 939 | if (extra_msr_offset32) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 940 | if (get_msr(cpu, extra_msr_offset32, &msr)) |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 941 | return -5; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 942 | t->extra_msr32 = msr & 0xFFFFFFFF; |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 943 | } |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 944 | |
| 945 | if (extra_msr_offset64) |
| 946 | if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64)) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 947 | return -5; |
| 948 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 949 | if (use_c1_residency_msr) { |
| 950 | if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) |
| 951 | return -6; |
| 952 | } |
| 953 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 954 | /* collect core counters only for 1st thread in core */ |
| 955 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 956 | return 0; |
| 957 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 958 | if (do_nhm_cstates && !do_slm_cstates) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 959 | if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) |
| 960 | return -6; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | if (do_nhm_cstates) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 964 | if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) |
| 965 | return -7; |
| 966 | } |
| 967 | |
| 968 | if (do_snb_cstates) |
| 969 | if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) |
| 970 | return -8; |
| 971 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 972 | if (do_dts) { |
| 973 | if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) |
| 974 | return -9; |
| 975 | c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); |
| 976 | } |
| 977 | |
| 978 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 979 | /* collect package counters only for 1st core in package */ |
| 980 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 981 | return 0; |
| 982 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 983 | if (do_nhm_cstates && !do_slm_cstates) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 984 | if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) |
| 985 | return -9; |
| 986 | if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) |
| 987 | return -10; |
| 988 | } |
| 989 | if (do_snb_cstates) { |
| 990 | if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) |
| 991 | return -11; |
| 992 | if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) |
| 993 | return -12; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 994 | } |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 995 | if (do_c8_c9_c10) { |
| 996 | if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) |
| 997 | return -13; |
| 998 | if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) |
| 999 | return -13; |
| 1000 | if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) |
| 1001 | return -13; |
| 1002 | } |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1003 | if (do_rapl & RAPL_PKG) { |
| 1004 | if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) |
| 1005 | return -13; |
| 1006 | p->energy_pkg = msr & 0xFFFFFFFF; |
| 1007 | } |
| 1008 | if (do_rapl & RAPL_CORES) { |
| 1009 | if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) |
| 1010 | return -14; |
| 1011 | p->energy_cores = msr & 0xFFFFFFFF; |
| 1012 | } |
| 1013 | if (do_rapl & RAPL_DRAM) { |
| 1014 | if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) |
| 1015 | return -15; |
| 1016 | p->energy_dram = msr & 0xFFFFFFFF; |
| 1017 | } |
| 1018 | if (do_rapl & RAPL_GFX) { |
| 1019 | if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) |
| 1020 | return -16; |
| 1021 | p->energy_gfx = msr & 0xFFFFFFFF; |
| 1022 | } |
| 1023 | if (do_rapl & RAPL_PKG_PERF_STATUS) { |
| 1024 | if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) |
| 1025 | return -16; |
| 1026 | p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; |
| 1027 | } |
| 1028 | if (do_rapl & RAPL_DRAM_PERF_STATUS) { |
| 1029 | if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) |
| 1030 | return -16; |
| 1031 | p->rapl_dram_perf_status = msr & 0xFFFFFFFF; |
| 1032 | } |
| 1033 | if (do_ptm) { |
| 1034 | if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) |
| 1035 | return -17; |
| 1036 | p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); |
| 1037 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1038 | return 0; |
| 1039 | } |
| 1040 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1041 | void print_verbose_header(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1042 | { |
| 1043 | unsigned long long msr; |
| 1044 | unsigned int ratio; |
| 1045 | |
| 1046 | if (!do_nehalem_platform_info) |
| 1047 | return; |
| 1048 | |
Len Brown | 9c63a65 | 2012-10-31 01:29:52 -0400 | [diff] [blame] | 1049 | get_msr(0, MSR_NHM_PLATFORM_INFO, &msr); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1050 | |
Len Brown | 6792041 | 2013-01-31 15:22:15 -0500 | [diff] [blame] | 1051 | fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1052 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1053 | ratio = (msr >> 40) & 0xFF; |
| 1054 | fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", |
| 1055 | ratio, bclk, ratio * bclk); |
| 1056 | |
| 1057 | ratio = (msr >> 8) & 0xFF; |
| 1058 | fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", |
| 1059 | ratio, bclk, ratio * bclk); |
| 1060 | |
Len Brown | 6792041 | 2013-01-31 15:22:15 -0500 | [diff] [blame] | 1061 | get_msr(0, MSR_IA32_POWER_CTL, &msr); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1062 | fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", |
Len Brown | 6792041 | 2013-01-31 15:22:15 -0500 | [diff] [blame] | 1063 | msr, msr & 0x2 ? "EN" : "DIS"); |
| 1064 | |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1065 | if (!do_ivt_turbo_ratio_limit) |
| 1066 | goto print_nhm_turbo_ratio_limits; |
| 1067 | |
| 1068 | get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr); |
| 1069 | |
Len Brown | 6792041 | 2013-01-31 15:22:15 -0500 | [diff] [blame] | 1070 | fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1071 | |
| 1072 | ratio = (msr >> 56) & 0xFF; |
| 1073 | if (ratio) |
| 1074 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n", |
| 1075 | ratio, bclk, ratio * bclk); |
| 1076 | |
| 1077 | ratio = (msr >> 48) & 0xFF; |
| 1078 | if (ratio) |
| 1079 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n", |
| 1080 | ratio, bclk, ratio * bclk); |
| 1081 | |
| 1082 | ratio = (msr >> 40) & 0xFF; |
| 1083 | if (ratio) |
| 1084 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n", |
| 1085 | ratio, bclk, ratio * bclk); |
| 1086 | |
| 1087 | ratio = (msr >> 32) & 0xFF; |
| 1088 | if (ratio) |
| 1089 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n", |
| 1090 | ratio, bclk, ratio * bclk); |
| 1091 | |
| 1092 | ratio = (msr >> 24) & 0xFF; |
| 1093 | if (ratio) |
| 1094 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n", |
| 1095 | ratio, bclk, ratio * bclk); |
| 1096 | |
| 1097 | ratio = (msr >> 16) & 0xFF; |
| 1098 | if (ratio) |
| 1099 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n", |
| 1100 | ratio, bclk, ratio * bclk); |
| 1101 | |
| 1102 | ratio = (msr >> 8) & 0xFF; |
| 1103 | if (ratio) |
| 1104 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n", |
| 1105 | ratio, bclk, ratio * bclk); |
| 1106 | |
| 1107 | ratio = (msr >> 0) & 0xFF; |
| 1108 | if (ratio) |
| 1109 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n", |
| 1110 | ratio, bclk, ratio * bclk); |
| 1111 | |
| 1112 | print_nhm_turbo_ratio_limits: |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1113 | get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr); |
| 1114 | |
| 1115 | #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) |
| 1116 | #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) |
| 1117 | |
| 1118 | fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr); |
| 1119 | |
| 1120 | fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ", |
| 1121 | (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", |
| 1122 | (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", |
| 1123 | (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", |
| 1124 | (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", |
| 1125 | (msr & (1 << 15)) ? "" : "UN", |
| 1126 | (unsigned int)msr & 7); |
| 1127 | |
| 1128 | |
| 1129 | switch(msr & 0x7) { |
| 1130 | case 0: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1131 | fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1132 | break; |
| 1133 | case 1: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1134 | fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1135 | break; |
| 1136 | case 2: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1137 | fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1138 | break; |
| 1139 | case 3: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1140 | fprintf(stderr, do_slm_cstates ? "invalid" : "pc6"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1141 | break; |
| 1142 | case 4: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1143 | fprintf(stderr, do_slm_cstates ? "pc4" : "pc7"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1144 | break; |
| 1145 | case 5: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1146 | fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid"); |
| 1147 | break; |
| 1148 | case 6: |
| 1149 | fprintf(stderr, do_slm_cstates ? "pc6" : "invalid"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1150 | break; |
| 1151 | case 7: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1152 | fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited"); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1153 | break; |
| 1154 | default: |
| 1155 | fprintf(stderr, "invalid"); |
| 1156 | } |
| 1157 | fprintf(stderr, ")\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1158 | |
| 1159 | if (!do_nehalem_turbo_ratio_limit) |
| 1160 | return; |
| 1161 | |
Len Brown | 9c63a65 | 2012-10-31 01:29:52 -0400 | [diff] [blame] | 1162 | get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1163 | |
Len Brown | 6792041 | 2013-01-31 15:22:15 -0500 | [diff] [blame] | 1164 | fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1165 | |
| 1166 | ratio = (msr >> 56) & 0xFF; |
| 1167 | if (ratio) |
| 1168 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n", |
| 1169 | ratio, bclk, ratio * bclk); |
| 1170 | |
| 1171 | ratio = (msr >> 48) & 0xFF; |
| 1172 | if (ratio) |
| 1173 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n", |
| 1174 | ratio, bclk, ratio * bclk); |
| 1175 | |
| 1176 | ratio = (msr >> 40) & 0xFF; |
| 1177 | if (ratio) |
| 1178 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n", |
| 1179 | ratio, bclk, ratio * bclk); |
| 1180 | |
| 1181 | ratio = (msr >> 32) & 0xFF; |
| 1182 | if (ratio) |
| 1183 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n", |
| 1184 | ratio, bclk, ratio * bclk); |
| 1185 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1186 | ratio = (msr >> 24) & 0xFF; |
| 1187 | if (ratio) |
| 1188 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", |
| 1189 | ratio, bclk, ratio * bclk); |
| 1190 | |
| 1191 | ratio = (msr >> 16) & 0xFF; |
| 1192 | if (ratio) |
| 1193 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", |
| 1194 | ratio, bclk, ratio * bclk); |
| 1195 | |
| 1196 | ratio = (msr >> 8) & 0xFF; |
| 1197 | if (ratio) |
| 1198 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", |
| 1199 | ratio, bclk, ratio * bclk); |
| 1200 | |
| 1201 | ratio = (msr >> 0) & 0xFF; |
| 1202 | if (ratio) |
| 1203 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", |
| 1204 | ratio, bclk, ratio * bclk); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1207 | void free_all_buffers(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1208 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1209 | CPU_FREE(cpu_present_set); |
| 1210 | cpu_present_set = NULL; |
| 1211 | cpu_present_set = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1212 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1213 | CPU_FREE(cpu_affinity_set); |
| 1214 | cpu_affinity_set = NULL; |
| 1215 | cpu_affinity_setsize = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1216 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1217 | free(thread_even); |
| 1218 | free(core_even); |
| 1219 | free(package_even); |
| 1220 | |
| 1221 | thread_even = NULL; |
| 1222 | core_even = NULL; |
| 1223 | package_even = NULL; |
| 1224 | |
| 1225 | free(thread_odd); |
| 1226 | free(core_odd); |
| 1227 | free(package_odd); |
| 1228 | |
| 1229 | thread_odd = NULL; |
| 1230 | core_odd = NULL; |
| 1231 | package_odd = NULL; |
| 1232 | |
| 1233 | free(output_buffer); |
| 1234 | output_buffer = NULL; |
| 1235 | outp = NULL; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1236 | } |
| 1237 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1238 | /* |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1239 | * Open a file, and exit on failure |
| 1240 | */ |
| 1241 | FILE *fopen_or_die(const char *path, const char *mode) |
| 1242 | { |
| 1243 | FILE *filep = fopen(path, "r"); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1244 | if (!filep) |
| 1245 | err(1, "%s: open failed", path); |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1246 | return filep; |
| 1247 | } |
| 1248 | |
| 1249 | /* |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1250 | * Parse a file containing a single int. |
| 1251 | */ |
| 1252 | int parse_int_file(const char *fmt, ...) |
| 1253 | { |
| 1254 | va_list args; |
| 1255 | char path[PATH_MAX]; |
| 1256 | FILE *filep; |
| 1257 | int value; |
| 1258 | |
| 1259 | va_start(args, fmt); |
| 1260 | vsnprintf(path, sizeof(path), fmt, args); |
| 1261 | va_end(args); |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1262 | filep = fopen_or_die(path, "r"); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1263 | if (fscanf(filep, "%d", &value) != 1) |
| 1264 | err(1, "%s: failed to parse number from file", path); |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1265 | fclose(filep); |
| 1266 | return value; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1270 | * cpu_is_first_sibling_in_core(cpu) |
| 1271 | * return 1 if given CPU is 1st HT sibling in the core |
| 1272 | */ |
| 1273 | int cpu_is_first_sibling_in_core(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1274 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1275 | return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1278 | /* |
| 1279 | * cpu_is_first_core_in_package(cpu) |
| 1280 | * return 1 if given CPU is 1st core in package |
| 1281 | */ |
| 1282 | int cpu_is_first_core_in_package(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1283 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1284 | 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] | 1285 | } |
| 1286 | |
| 1287 | int get_physical_package_id(int cpu) |
| 1288 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1289 | 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] | 1290 | } |
| 1291 | |
| 1292 | int get_core_id(int cpu) |
| 1293 | { |
Josh Triplett | 95aebc4 | 2013-08-20 17:20:16 -0700 | [diff] [blame] | 1294 | 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] | 1295 | } |
| 1296 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1297 | int get_num_ht_siblings(int cpu) |
| 1298 | { |
| 1299 | char path[80]; |
| 1300 | FILE *filep; |
| 1301 | int sib1, sib2; |
| 1302 | int matches; |
| 1303 | char character; |
| 1304 | |
| 1305 | 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] | 1306 | filep = fopen_or_die(path, "r"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1307 | /* |
| 1308 | * file format: |
| 1309 | * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) |
| 1310 | * otherwinse 1 sibling (self). |
| 1311 | */ |
| 1312 | matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); |
| 1313 | |
| 1314 | fclose(filep); |
| 1315 | |
| 1316 | if (matches == 3) |
| 1317 | return 2; |
| 1318 | else |
| 1319 | return 1; |
| 1320 | } |
| 1321 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1322 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1323 | * run func(thread, core, package) in topology order |
| 1324 | * skip non-present cpus |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1325 | */ |
| 1326 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1327 | int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, |
| 1328 | struct pkg_data *, struct thread_data *, struct core_data *, |
| 1329 | struct pkg_data *), struct thread_data *thread_base, |
| 1330 | struct core_data *core_base, struct pkg_data *pkg_base, |
| 1331 | struct thread_data *thread_base2, struct core_data *core_base2, |
| 1332 | struct pkg_data *pkg_base2) |
| 1333 | { |
| 1334 | int retval, pkg_no, core_no, thread_no; |
| 1335 | |
| 1336 | for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { |
| 1337 | for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { |
| 1338 | for (thread_no = 0; thread_no < |
| 1339 | topo.num_threads_per_core; ++thread_no) { |
| 1340 | struct thread_data *t, *t2; |
| 1341 | struct core_data *c, *c2; |
| 1342 | struct pkg_data *p, *p2; |
| 1343 | |
| 1344 | t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); |
| 1345 | |
| 1346 | if (cpu_is_not_present(t->cpu_id)) |
| 1347 | continue; |
| 1348 | |
| 1349 | t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); |
| 1350 | |
| 1351 | c = GET_CORE(core_base, core_no, pkg_no); |
| 1352 | c2 = GET_CORE(core_base2, core_no, pkg_no); |
| 1353 | |
| 1354 | p = GET_PKG(pkg_base, pkg_no); |
| 1355 | p2 = GET_PKG(pkg_base2, pkg_no); |
| 1356 | |
| 1357 | retval = func(t, c, p, t2, c2, p2); |
| 1358 | if (retval) |
| 1359 | return retval; |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * run func(cpu) on every cpu in /proc/stat |
| 1368 | * return max_cpu number |
| 1369 | */ |
| 1370 | int for_all_proc_cpus(int (func)(int)) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1371 | { |
| 1372 | FILE *fp; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1373 | int cpu_num; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1374 | int retval; |
| 1375 | |
Josh Triplett | 57a42a3 | 2013-08-20 17:20:17 -0700 | [diff] [blame] | 1376 | fp = fopen_or_die(proc_stat, "r"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1377 | |
| 1378 | 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] | 1379 | if (retval != 0) |
| 1380 | err(1, "%s: failed to parse format", proc_stat); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1381 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1382 | while (1) { |
| 1383 | 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] | 1384 | if (retval != 1) |
| 1385 | break; |
| 1386 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1387 | retval = func(cpu_num); |
| 1388 | if (retval) { |
| 1389 | fclose(fp); |
| 1390 | return(retval); |
| 1391 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1392 | } |
| 1393 | fclose(fp); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1394 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | void re_initialize(void) |
| 1398 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1399 | free_all_buffers(); |
| 1400 | setup_all_buffers(); |
| 1401 | printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1402 | } |
| 1403 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1404 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1405 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1406 | * count_cpus() |
| 1407 | * remember the last one seen, it will be the max |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1408 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1409 | int count_cpus(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1410 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1411 | if (topo.max_cpu_num < cpu) |
| 1412 | topo.max_cpu_num = cpu; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1413 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1414 | topo.num_cpus += 1; |
| 1415 | return 0; |
| 1416 | } |
| 1417 | int mark_cpu_present(int cpu) |
| 1418 | { |
| 1419 | CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 1420 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | void turbostat_loop() |
| 1424 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1425 | int retval; |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1426 | int restarted = 0; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1427 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1428 | restart: |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1429 | restarted++; |
| 1430 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1431 | retval = for_all_cpus(get_counters, EVEN_COUNTERS); |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 1432 | if (retval < -1) { |
| 1433 | exit(retval); |
| 1434 | } else if (retval == -1) { |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1435 | if (restarted > 1) { |
| 1436 | exit(retval); |
| 1437 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1438 | re_initialize(); |
| 1439 | goto restart; |
| 1440 | } |
Len Brown | e52966c | 2012-11-08 22:38:05 -0500 | [diff] [blame] | 1441 | restarted = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1442 | gettimeofday(&tv_even, (struct timezone *)NULL); |
| 1443 | |
| 1444 | while (1) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1445 | if (for_all_proc_cpus(cpu_is_not_present)) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1446 | re_initialize(); |
| 1447 | goto restart; |
| 1448 | } |
| 1449 | sleep(interval_sec); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1450 | retval = for_all_cpus(get_counters, ODD_COUNTERS); |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 1451 | if (retval < -1) { |
| 1452 | exit(retval); |
| 1453 | } else if (retval == -1) { |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 1454 | re_initialize(); |
| 1455 | goto restart; |
| 1456 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1457 | gettimeofday(&tv_odd, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1458 | timersub(&tv_odd, &tv_even, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1459 | for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); |
| 1460 | compute_average(EVEN_COUNTERS); |
| 1461 | format_all_counters(EVEN_COUNTERS); |
| 1462 | flush_stdout(); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 1463 | sleep(interval_sec); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1464 | retval = for_all_cpus(get_counters, EVEN_COUNTERS); |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 1465 | if (retval < -1) { |
| 1466 | exit(retval); |
| 1467 | } else if (retval == -1) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1468 | re_initialize(); |
| 1469 | goto restart; |
| 1470 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1471 | gettimeofday(&tv_even, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1472 | timersub(&tv_even, &tv_odd, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 1473 | for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); |
| 1474 | compute_average(ODD_COUNTERS); |
| 1475 | format_all_counters(ODD_COUNTERS); |
| 1476 | flush_stdout(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | void check_dev_msr() |
| 1481 | { |
| 1482 | struct stat sb; |
| 1483 | |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1484 | if (stat("/dev/cpu/0/msr", &sb)) |
| 1485 | err(-5, "no /dev/cpu/0/msr\n" |
| 1486 | "Try \"# modprobe msr\""); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | void check_super_user() |
| 1490 | { |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 1491 | if (getuid() != 0) |
| 1492 | errx(-6, "must be root"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 1496 | { |
| 1497 | if (!genuine_intel) |
| 1498 | return 0; |
| 1499 | |
| 1500 | if (family != 6) |
| 1501 | return 0; |
| 1502 | |
| 1503 | switch (model) { |
| 1504 | case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ |
| 1505 | case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ |
| 1506 | case 0x1F: /* Core i7 and i5 Processor - Nehalem */ |
| 1507 | case 0x25: /* Westmere Client - Clarkdale, Arrandale */ |
| 1508 | case 0x2C: /* Westmere EP - Gulftown */ |
| 1509 | case 0x2A: /* SNB */ |
| 1510 | case 0x2D: /* SNB Xeon */ |
Len Brown | 553575f | 2011-11-18 03:32:01 -0500 | [diff] [blame] | 1511 | case 0x3A: /* IVB */ |
Len Brown | 1300651 | 2012-09-26 18:11:31 -0400 | [diff] [blame] | 1512 | case 0x3E: /* IVB Xeon */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 1513 | case 0x3C: /* HSW */ |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 1514 | case 0x3F: /* HSX */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 1515 | case 0x45: /* HSW */ |
Len Brown | 149c231 | 2013-03-15 10:58:02 -0400 | [diff] [blame] | 1516 | case 0x46: /* HSW */ |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1517 | case 0x37: /* BYT */ |
| 1518 | case 0x4D: /* AVN */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1519 | return 1; |
| 1520 | case 0x2E: /* Nehalem-EX Xeon - Beckton */ |
| 1521 | case 0x2F: /* Westmere-EX Xeon - Eagleton */ |
| 1522 | default: |
| 1523 | return 0; |
| 1524 | } |
| 1525 | } |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 1526 | int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 1527 | { |
| 1528 | if (!genuine_intel) |
| 1529 | return 0; |
| 1530 | |
| 1531 | if (family != 6) |
| 1532 | return 0; |
| 1533 | |
| 1534 | switch (model) { |
| 1535 | case 0x3E: /* IVB Xeon */ |
| 1536 | return 1; |
| 1537 | default: |
| 1538 | return 0; |
| 1539 | } |
| 1540 | } |
| 1541 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1542 | /* |
| 1543 | * print_epb() |
| 1544 | * Decode the ENERGY_PERF_BIAS MSR |
| 1545 | */ |
| 1546 | int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 1547 | { |
| 1548 | unsigned long long msr; |
| 1549 | char *epb_string; |
| 1550 | int cpu; |
| 1551 | |
| 1552 | if (!has_epb) |
| 1553 | return 0; |
| 1554 | |
| 1555 | cpu = t->cpu_id; |
| 1556 | |
| 1557 | /* EPB is per-package */ |
| 1558 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 1559 | return 0; |
| 1560 | |
| 1561 | if (cpu_migrate(cpu)) { |
| 1562 | fprintf(stderr, "Could not migrate to CPU %d\n", cpu); |
| 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) |
| 1567 | return 0; |
| 1568 | |
| 1569 | switch (msr & 0x7) { |
| 1570 | case ENERGY_PERF_BIAS_PERFORMANCE: |
| 1571 | epb_string = "performance"; |
| 1572 | break; |
| 1573 | case ENERGY_PERF_BIAS_NORMAL: |
| 1574 | epb_string = "balanced"; |
| 1575 | break; |
| 1576 | case ENERGY_PERF_BIAS_POWERSAVE: |
| 1577 | epb_string = "powersave"; |
| 1578 | break; |
| 1579 | default: |
| 1580 | epb_string = "custom"; |
| 1581 | break; |
| 1582 | } |
| 1583 | fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); |
| 1584 | |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
| 1588 | #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ |
| 1589 | #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ |
| 1590 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1591 | double get_tdp(model) |
| 1592 | { |
| 1593 | unsigned long long msr; |
| 1594 | |
| 1595 | if (do_rapl & RAPL_PKG_POWER_INFO) |
| 1596 | if (!get_msr(0, MSR_PKG_POWER_INFO, &msr)) |
| 1597 | return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; |
| 1598 | |
| 1599 | switch (model) { |
| 1600 | case 0x37: |
| 1601 | case 0x4D: |
| 1602 | return 30.0; |
| 1603 | default: |
| 1604 | return 135.0; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1609 | /* |
| 1610 | * rapl_probe() |
| 1611 | * |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1612 | * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1613 | */ |
| 1614 | void rapl_probe(unsigned int family, unsigned int model) |
| 1615 | { |
| 1616 | unsigned long long msr; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1617 | unsigned int time_unit; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1618 | double tdp; |
| 1619 | |
| 1620 | if (!genuine_intel) |
| 1621 | return; |
| 1622 | |
| 1623 | if (family != 6) |
| 1624 | return; |
| 1625 | |
| 1626 | switch (model) { |
| 1627 | case 0x2A: |
| 1628 | case 0x3A: |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 1629 | case 0x3C: /* HSW */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 1630 | case 0x45: /* HSW */ |
Len Brown | 149c231 | 2013-03-15 10:58:02 -0400 | [diff] [blame] | 1631 | case 0x46: /* HSW */ |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1632 | 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] | 1633 | break; |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 1634 | case 0x3F: /* HSX */ |
| 1635 | do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; |
| 1636 | break; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1637 | case 0x2D: |
| 1638 | case 0x3E: |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1639 | do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; |
| 1640 | break; |
| 1641 | case 0x37: /* BYT */ |
| 1642 | case 0x4D: /* AVN */ |
| 1643 | do_rapl = RAPL_PKG | RAPL_CORES ; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1644 | break; |
| 1645 | default: |
| 1646 | return; |
| 1647 | } |
| 1648 | |
| 1649 | /* units on package 0, verify later other packages match */ |
| 1650 | if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr)) |
| 1651 | return; |
| 1652 | |
| 1653 | rapl_power_units = 1.0 / (1 << (msr & 0xF)); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1654 | if (model == 0x37) |
| 1655 | rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; |
| 1656 | else |
| 1657 | rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1658 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1659 | time_unit = msr >> 16 & 0xF; |
| 1660 | if (time_unit == 0) |
| 1661 | time_unit = 0xA; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1662 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1663 | rapl_time_units = 1.0 / (1 << (time_unit)); |
| 1664 | |
| 1665 | tdp = get_tdp(model); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1666 | |
| 1667 | rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1668 | if (verbose) |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1669 | fprintf(stderr, "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] | 1670 | |
| 1671 | return; |
| 1672 | } |
| 1673 | |
| 1674 | int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 1675 | { |
| 1676 | unsigned long long msr; |
| 1677 | unsigned int dts; |
| 1678 | int cpu; |
| 1679 | |
| 1680 | if (!(do_dts || do_ptm)) |
| 1681 | return 0; |
| 1682 | |
| 1683 | cpu = t->cpu_id; |
| 1684 | |
| 1685 | /* DTS is per-core, no need to print for each thread */ |
| 1686 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 1687 | return 0; |
| 1688 | |
| 1689 | if (cpu_migrate(cpu)) { |
| 1690 | fprintf(stderr, "Could not migrate to CPU %d\n", cpu); |
| 1691 | return -1; |
| 1692 | } |
| 1693 | |
| 1694 | if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { |
| 1695 | if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) |
| 1696 | return 0; |
| 1697 | |
| 1698 | dts = (msr >> 16) & 0x7F; |
| 1699 | fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", |
| 1700 | cpu, msr, tcc_activation_temp - dts); |
| 1701 | |
| 1702 | #ifdef THERM_DEBUG |
| 1703 | if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) |
| 1704 | return 0; |
| 1705 | |
| 1706 | dts = (msr >> 16) & 0x7F; |
| 1707 | dts2 = (msr >> 8) & 0x7F; |
| 1708 | fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", |
| 1709 | cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); |
| 1710 | #endif |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | if (do_dts) { |
| 1715 | unsigned int resolution; |
| 1716 | |
| 1717 | if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) |
| 1718 | return 0; |
| 1719 | |
| 1720 | dts = (msr >> 16) & 0x7F; |
| 1721 | resolution = (msr >> 27) & 0xF; |
| 1722 | fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", |
| 1723 | cpu, msr, tcc_activation_temp - dts, resolution); |
| 1724 | |
| 1725 | #ifdef THERM_DEBUG |
| 1726 | if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) |
| 1727 | return 0; |
| 1728 | |
| 1729 | dts = (msr >> 16) & 0x7F; |
| 1730 | dts2 = (msr >> 8) & 0x7F; |
| 1731 | fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", |
| 1732 | cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); |
| 1733 | #endif |
| 1734 | } |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | void print_power_limit_msr(int cpu, unsigned long long msr, char *label) |
| 1740 | { |
| 1741 | fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", |
| 1742 | cpu, label, |
| 1743 | ((msr >> 15) & 1) ? "EN" : "DIS", |
| 1744 | ((msr >> 0) & 0x7FFF) * rapl_power_units, |
| 1745 | (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, |
| 1746 | (((msr >> 16) & 1) ? "EN" : "DIS")); |
| 1747 | |
| 1748 | return; |
| 1749 | } |
| 1750 | |
| 1751 | int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 1752 | { |
| 1753 | unsigned long long msr; |
| 1754 | int cpu; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1755 | |
| 1756 | if (!do_rapl) |
| 1757 | return 0; |
| 1758 | |
| 1759 | /* RAPL counters are per package, so print only for 1st thread/package */ |
| 1760 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 1761 | return 0; |
| 1762 | |
| 1763 | cpu = t->cpu_id; |
| 1764 | if (cpu_migrate(cpu)) { |
| 1765 | fprintf(stderr, "Could not migrate to CPU %d\n", cpu); |
| 1766 | return -1; |
| 1767 | } |
| 1768 | |
| 1769 | if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) |
| 1770 | return -1; |
| 1771 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1772 | if (verbose) { |
| 1773 | fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx " |
| 1774 | "(%f Watts, %f Joules, %f sec.)\n", cpu, msr, |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1775 | rapl_power_units, rapl_energy_units, rapl_time_units); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1776 | } |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1777 | if (do_rapl & RAPL_PKG_POWER_INFO) { |
| 1778 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1779 | if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) |
| 1780 | return -5; |
| 1781 | |
| 1782 | |
| 1783 | fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", |
| 1784 | cpu, msr, |
| 1785 | ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 1786 | ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 1787 | ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 1788 | ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); |
| 1789 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1790 | } |
| 1791 | if (do_rapl & RAPL_PKG) { |
| 1792 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1793 | if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) |
| 1794 | return -9; |
| 1795 | |
| 1796 | fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", |
| 1797 | cpu, msr, (msr >> 63) & 1 ? "": "UN"); |
| 1798 | |
| 1799 | print_power_limit_msr(cpu, msr, "PKG Limit #1"); |
| 1800 | fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", |
| 1801 | cpu, |
| 1802 | ((msr >> 47) & 1) ? "EN" : "DIS", |
| 1803 | ((msr >> 32) & 0x7FFF) * rapl_power_units, |
| 1804 | (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, |
| 1805 | ((msr >> 48) & 1) ? "EN" : "DIS"); |
| 1806 | } |
| 1807 | |
| 1808 | if (do_rapl & RAPL_DRAM) { |
| 1809 | if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) |
| 1810 | return -6; |
| 1811 | |
| 1812 | |
| 1813 | fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", |
| 1814 | cpu, msr, |
| 1815 | ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 1816 | ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 1817 | ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, |
| 1818 | ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); |
| 1819 | |
| 1820 | |
| 1821 | if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) |
| 1822 | return -9; |
| 1823 | fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", |
| 1824 | cpu, msr, (msr >> 31) & 1 ? "": "UN"); |
| 1825 | |
| 1826 | print_power_limit_msr(cpu, msr, "DRAM Limit"); |
| 1827 | } |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1828 | if (do_rapl & RAPL_CORE_POLICY) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1829 | if (verbose) { |
| 1830 | if (get_msr(cpu, MSR_PP0_POLICY, &msr)) |
| 1831 | return -7; |
| 1832 | |
| 1833 | fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | if (do_rapl & RAPL_CORES) { |
| 1837 | if (verbose) { |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1838 | |
| 1839 | if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) |
| 1840 | return -9; |
| 1841 | fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", |
| 1842 | cpu, msr, (msr >> 31) & 1 ? "": "UN"); |
| 1843 | print_power_limit_msr(cpu, msr, "Cores Limit"); |
| 1844 | } |
| 1845 | } |
| 1846 | if (do_rapl & RAPL_GFX) { |
| 1847 | if (verbose) { |
| 1848 | if (get_msr(cpu, MSR_PP1_POLICY, &msr)) |
| 1849 | return -8; |
| 1850 | |
| 1851 | fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); |
| 1852 | |
| 1853 | if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) |
| 1854 | return -9; |
| 1855 | fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", |
| 1856 | cpu, msr, (msr >> 31) & 1 ? "": "UN"); |
| 1857 | print_power_limit_msr(cpu, msr, "GFX Limit"); |
| 1858 | } |
| 1859 | } |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1863 | |
| 1864 | int is_snb(unsigned int family, unsigned int model) |
| 1865 | { |
| 1866 | if (!genuine_intel) |
| 1867 | return 0; |
| 1868 | |
| 1869 | switch (model) { |
| 1870 | case 0x2A: |
| 1871 | case 0x2D: |
Len Brown | 650a37f | 2012-06-03 23:34:44 -0400 | [diff] [blame] | 1872 | case 0x3A: /* IVB */ |
Len Brown | 1300651 | 2012-09-26 18:11:31 -0400 | [diff] [blame] | 1873 | case 0x3E: /* IVB Xeon */ |
Len Brown | 70b4340 | 2013-01-08 01:26:07 -0500 | [diff] [blame] | 1874 | case 0x3C: /* HSW */ |
| 1875 | case 0x3F: /* HSW */ |
| 1876 | case 0x45: /* HSW */ |
Len Brown | 149c231 | 2013-03-15 10:58:02 -0400 | [diff] [blame] | 1877 | case 0x46: /* HSW */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1878 | return 1; |
| 1879 | } |
| 1880 | return 0; |
| 1881 | } |
| 1882 | |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 1883 | int has_c8_c9_c10(unsigned int family, unsigned int model) |
| 1884 | { |
| 1885 | if (!genuine_intel) |
| 1886 | return 0; |
| 1887 | |
| 1888 | switch (model) { |
| 1889 | case 0x45: |
| 1890 | return 1; |
| 1891 | } |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1896 | int is_slm(unsigned int family, unsigned int model) |
| 1897 | { |
| 1898 | if (!genuine_intel) |
| 1899 | return 0; |
| 1900 | switch (model) { |
| 1901 | case 0x37: /* BYT */ |
| 1902 | case 0x4D: /* AVN */ |
| 1903 | return 1; |
| 1904 | } |
| 1905 | return 0; |
| 1906 | } |
| 1907 | |
| 1908 | #define SLM_BCLK_FREQS 5 |
| 1909 | double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; |
| 1910 | |
| 1911 | double slm_bclk(void) |
| 1912 | { |
| 1913 | unsigned long long msr = 3; |
| 1914 | unsigned int i; |
| 1915 | double freq; |
| 1916 | |
| 1917 | if (get_msr(0, MSR_FSB_FREQ, &msr)) |
| 1918 | fprintf(stderr, "SLM BCLK: unknown\n"); |
| 1919 | |
| 1920 | i = msr & 0xf; |
| 1921 | if (i >= SLM_BCLK_FREQS) { |
| 1922 | fprintf(stderr, "SLM BCLK[%d] invalid\n", i); |
| 1923 | msr = 3; |
| 1924 | } |
| 1925 | freq = slm_freq_table[i]; |
| 1926 | |
| 1927 | fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq); |
| 1928 | |
| 1929 | return freq; |
| 1930 | } |
| 1931 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1932 | double discover_bclk(unsigned int family, unsigned int model) |
| 1933 | { |
| 1934 | if (is_snb(family, model)) |
| 1935 | return 100.00; |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1936 | else if (is_slm(family, model)) |
| 1937 | return slm_bclk(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1938 | else |
| 1939 | return 133.33; |
| 1940 | } |
| 1941 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1942 | /* |
| 1943 | * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where |
| 1944 | * the Thermal Control Circuit (TCC) activates. |
| 1945 | * This is usually equal to tjMax. |
| 1946 | * |
| 1947 | * Older processors do not have this MSR, so there we guess, |
| 1948 | * but also allow cmdline over-ride with -T. |
| 1949 | * |
| 1950 | * Several MSR temperature values are in units of degrees-C |
| 1951 | * below this value, including the Digital Thermal Sensor (DTS), |
| 1952 | * Package Thermal Management Sensor (PTM), and thermal event thresholds. |
| 1953 | */ |
| 1954 | int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 1955 | { |
| 1956 | unsigned long long msr; |
| 1957 | unsigned int target_c_local; |
| 1958 | int cpu; |
| 1959 | |
| 1960 | /* tcc_activation_temp is used only for dts or ptm */ |
| 1961 | if (!(do_dts || do_ptm)) |
| 1962 | return 0; |
| 1963 | |
| 1964 | /* this is a per-package concept */ |
| 1965 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 1966 | return 0; |
| 1967 | |
| 1968 | cpu = t->cpu_id; |
| 1969 | if (cpu_migrate(cpu)) { |
| 1970 | fprintf(stderr, "Could not migrate to CPU %d\n", cpu); |
| 1971 | return -1; |
| 1972 | } |
| 1973 | |
| 1974 | if (tcc_activation_temp_override != 0) { |
| 1975 | tcc_activation_temp = tcc_activation_temp_override; |
| 1976 | fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n", |
| 1977 | cpu, tcc_activation_temp); |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | /* Temperature Target MSR is Nehalem and newer only */ |
| 1982 | if (!do_nehalem_platform_info) |
| 1983 | goto guess; |
| 1984 | |
| 1985 | if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr)) |
| 1986 | goto guess; |
| 1987 | |
| 1988 | target_c_local = (msr >> 16) & 0x7F; |
| 1989 | |
| 1990 | if (verbose) |
| 1991 | fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", |
| 1992 | cpu, msr, target_c_local); |
| 1993 | |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 1994 | if (target_c_local < 85 || target_c_local > 127) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 1995 | goto guess; |
| 1996 | |
| 1997 | tcc_activation_temp = target_c_local; |
| 1998 | |
| 1999 | return 0; |
| 2000 | |
| 2001 | guess: |
| 2002 | tcc_activation_temp = TJMAX_DEFAULT; |
| 2003 | fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", |
| 2004 | cpu, tcc_activation_temp); |
| 2005 | |
| 2006 | return 0; |
| 2007 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2008 | void check_cpuid() |
| 2009 | { |
| 2010 | unsigned int eax, ebx, ecx, edx, max_level; |
| 2011 | unsigned int fms, family, model, stepping; |
| 2012 | |
| 2013 | eax = ebx = ecx = edx = 0; |
| 2014 | |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2015 | __get_cpuid(0, &max_level, &ebx, &ecx, &edx); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2016 | |
| 2017 | if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) |
| 2018 | genuine_intel = 1; |
| 2019 | |
| 2020 | if (verbose) |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2021 | fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ", |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2022 | (char *)&ebx, (char *)&edx, (char *)&ecx); |
| 2023 | |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2024 | __get_cpuid(1, &fms, &ebx, &ecx, &edx); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2025 | family = (fms >> 8) & 0xf; |
| 2026 | model = (fms >> 4) & 0xf; |
| 2027 | stepping = fms & 0xf; |
| 2028 | if (family == 6 || family == 0xf) |
| 2029 | model += ((fms >> 16) & 0xf) << 4; |
| 2030 | |
| 2031 | if (verbose) |
| 2032 | fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", |
| 2033 | max_level, family, model, stepping, family, model, stepping); |
| 2034 | |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2035 | if (!(edx & (1 << 5))) |
| 2036 | errx(1, "CPUID: no MSR"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2037 | |
| 2038 | /* |
| 2039 | * check max extended function levels of CPUID. |
| 2040 | * This is needed to check for invariant TSC. |
| 2041 | * This check is valid for both Intel and AMD. |
| 2042 | */ |
| 2043 | ebx = ecx = edx = 0; |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2044 | __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2045 | |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2046 | if (max_level < 0x80000007) |
| 2047 | errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2048 | |
| 2049 | /* |
| 2050 | * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 |
| 2051 | * this check is valid for both Intel and AMD |
| 2052 | */ |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2053 | __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); |
Thomas Renninger | 8209e05 | 2011-01-21 15:11:19 +0100 | [diff] [blame] | 2054 | has_invariant_tsc = edx & (1 << 8); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2055 | |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2056 | if (!has_invariant_tsc) |
| 2057 | errx(1, "No invariant TSC"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2058 | |
| 2059 | /* |
| 2060 | * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 |
| 2061 | * this check is valid for both Intel and AMD |
| 2062 | */ |
| 2063 | |
Josh Triplett | 2b92865 | 2013-08-20 17:20:14 -0700 | [diff] [blame] | 2064 | __get_cpuid(0x6, &eax, &ebx, &ecx, &edx); |
Thomas Renninger | 8209e05 | 2011-01-21 15:11:19 +0100 | [diff] [blame] | 2065 | has_aperf = ecx & (1 << 0); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2066 | do_dts = eax & (1 << 0); |
| 2067 | do_ptm = eax & (1 << 6); |
| 2068 | has_epb = ecx & (1 << 3); |
| 2069 | |
| 2070 | if (verbose) |
| 2071 | fprintf(stderr, "CPUID(6): %s%s%s%s\n", |
| 2072 | has_aperf ? "APERF" : "No APERF!", |
| 2073 | do_dts ? ", DTS" : "", |
| 2074 | do_ptm ? ", PTM": "", |
| 2075 | has_epb ? ", EPB": ""); |
| 2076 | |
| 2077 | if (!has_aperf) |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2078 | errx(-1, "No APERF"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2079 | |
| 2080 | do_nehalem_platform_info = genuine_intel && has_invariant_tsc; |
| 2081 | do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ |
Len Brown | 1ed5101 | 2013-02-10 17:19:24 -0500 | [diff] [blame] | 2082 | do_smi = do_nhm_cstates; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2083 | do_snb_cstates = is_snb(family, model); |
Kristen Carlson Accardi | ca58710 | 2012-11-21 05:22:43 -0800 | [diff] [blame] | 2084 | do_c8_c9_c10 = has_c8_c9_c10(family, model); |
Len Brown | 144b44b | 2013-11-09 00:30:16 -0500 | [diff] [blame] | 2085 | do_slm_cstates = is_slm(family, model); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2086 | bclk = discover_bclk(family, model); |
| 2087 | |
| 2088 | do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); |
Len Brown | 6574a5d | 2012-09-21 00:01:31 -0400 | [diff] [blame] | 2089 | do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2090 | rapl_probe(family, model); |
| 2091 | |
| 2092 | return; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | |
| 2096 | void usage() |
| 2097 | { |
Andy Shevchenko | f591c38 | 2014-01-23 17:13:14 +0200 | [diff] [blame] | 2098 | errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2099 | progname); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | |
| 2103 | /* |
| 2104 | * in /dev/cpu/ return success for names that are numbers |
| 2105 | * ie. filter out ".", "..", "microcode". |
| 2106 | */ |
| 2107 | int dir_filter(const struct dirent *dirp) |
| 2108 | { |
| 2109 | if (isdigit(dirp->d_name[0])) |
| 2110 | return 1; |
| 2111 | else |
| 2112 | return 0; |
| 2113 | } |
| 2114 | |
| 2115 | int open_dev_cpu_msr(int dummy1) |
| 2116 | { |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2120 | void topology_probe() |
| 2121 | { |
| 2122 | int i; |
| 2123 | int max_core_id = 0; |
| 2124 | int max_package_id = 0; |
| 2125 | int max_siblings = 0; |
| 2126 | struct cpu_topology { |
| 2127 | int core_id; |
| 2128 | int physical_package_id; |
| 2129 | } *cpus; |
| 2130 | |
| 2131 | /* Initialize num_cpus, max_cpu_num */ |
| 2132 | topo.num_cpus = 0; |
| 2133 | topo.max_cpu_num = 0; |
| 2134 | for_all_proc_cpus(count_cpus); |
| 2135 | if (!summary_only && topo.num_cpus > 1) |
| 2136 | show_cpu = 1; |
| 2137 | |
| 2138 | if (verbose > 1) |
| 2139 | fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); |
| 2140 | |
| 2141 | cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2142 | if (cpus == NULL) |
| 2143 | err(1, "calloc cpus"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2144 | |
| 2145 | /* |
| 2146 | * Allocate and initialize cpu_present_set |
| 2147 | */ |
| 2148 | cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2149 | if (cpu_present_set == NULL) |
| 2150 | err(3, "CPU_ALLOC"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2151 | cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
| 2152 | CPU_ZERO_S(cpu_present_setsize, cpu_present_set); |
| 2153 | for_all_proc_cpus(mark_cpu_present); |
| 2154 | |
| 2155 | /* |
| 2156 | * Allocate and initialize cpu_affinity_set |
| 2157 | */ |
| 2158 | cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2159 | if (cpu_affinity_set == NULL) |
| 2160 | err(3, "CPU_ALLOC"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2161 | cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
| 2162 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
| 2163 | |
| 2164 | |
| 2165 | /* |
| 2166 | * For online cpus |
| 2167 | * find max_core_id, max_package_id |
| 2168 | */ |
| 2169 | for (i = 0; i <= topo.max_cpu_num; ++i) { |
| 2170 | int siblings; |
| 2171 | |
| 2172 | if (cpu_is_not_present(i)) { |
| 2173 | if (verbose > 1) |
| 2174 | fprintf(stderr, "cpu%d NOT PRESENT\n", i); |
| 2175 | continue; |
| 2176 | } |
| 2177 | cpus[i].core_id = get_core_id(i); |
| 2178 | if (cpus[i].core_id > max_core_id) |
| 2179 | max_core_id = cpus[i].core_id; |
| 2180 | |
| 2181 | cpus[i].physical_package_id = get_physical_package_id(i); |
| 2182 | if (cpus[i].physical_package_id > max_package_id) |
| 2183 | max_package_id = cpus[i].physical_package_id; |
| 2184 | |
| 2185 | siblings = get_num_ht_siblings(i); |
| 2186 | if (siblings > max_siblings) |
| 2187 | max_siblings = siblings; |
| 2188 | if (verbose > 1) |
| 2189 | fprintf(stderr, "cpu %d pkg %d core %d\n", |
| 2190 | i, cpus[i].physical_package_id, cpus[i].core_id); |
| 2191 | } |
| 2192 | topo.num_cores_per_pkg = max_core_id + 1; |
| 2193 | if (verbose > 1) |
| 2194 | fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", |
| 2195 | max_core_id, topo.num_cores_per_pkg); |
| 2196 | if (!summary_only && topo.num_cores_per_pkg > 1) |
| 2197 | show_core = 1; |
| 2198 | |
| 2199 | topo.num_packages = max_package_id + 1; |
| 2200 | if (verbose > 1) |
| 2201 | fprintf(stderr, "max_package_id %d, sizing for %d packages\n", |
| 2202 | max_package_id, topo.num_packages); |
| 2203 | if (!summary_only && topo.num_packages > 1) |
| 2204 | show_pkg = 1; |
| 2205 | |
| 2206 | topo.num_threads_per_core = max_siblings; |
| 2207 | if (verbose > 1) |
| 2208 | fprintf(stderr, "max_siblings %d\n", max_siblings); |
| 2209 | |
| 2210 | free(cpus); |
| 2211 | } |
| 2212 | |
| 2213 | void |
| 2214 | allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) |
| 2215 | { |
| 2216 | int i; |
| 2217 | |
| 2218 | *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * |
| 2219 | topo.num_packages, sizeof(struct thread_data)); |
| 2220 | if (*t == NULL) |
| 2221 | goto error; |
| 2222 | |
| 2223 | for (i = 0; i < topo.num_threads_per_core * |
| 2224 | topo.num_cores_per_pkg * topo.num_packages; i++) |
| 2225 | (*t)[i].cpu_id = -1; |
| 2226 | |
| 2227 | *c = calloc(topo.num_cores_per_pkg * topo.num_packages, |
| 2228 | sizeof(struct core_data)); |
| 2229 | if (*c == NULL) |
| 2230 | goto error; |
| 2231 | |
| 2232 | for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) |
| 2233 | (*c)[i].core_id = -1; |
| 2234 | |
| 2235 | *p = calloc(topo.num_packages, sizeof(struct pkg_data)); |
| 2236 | if (*p == NULL) |
| 2237 | goto error; |
| 2238 | |
| 2239 | for (i = 0; i < topo.num_packages; i++) |
| 2240 | (*p)[i].package_id = i; |
| 2241 | |
| 2242 | return; |
| 2243 | error: |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2244 | err(1, "calloc counters"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2245 | } |
| 2246 | /* |
| 2247 | * init_counter() |
| 2248 | * |
| 2249 | * set cpu_id, core_num, pkg_num |
| 2250 | * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE |
| 2251 | * |
| 2252 | * increment topo.num_cores when 1st core in pkg seen |
| 2253 | */ |
| 2254 | void init_counter(struct thread_data *thread_base, struct core_data *core_base, |
| 2255 | struct pkg_data *pkg_base, int thread_num, int core_num, |
| 2256 | int pkg_num, int cpu_id) |
| 2257 | { |
| 2258 | struct thread_data *t; |
| 2259 | struct core_data *c; |
| 2260 | struct pkg_data *p; |
| 2261 | |
| 2262 | t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); |
| 2263 | c = GET_CORE(core_base, core_num, pkg_num); |
| 2264 | p = GET_PKG(pkg_base, pkg_num); |
| 2265 | |
| 2266 | t->cpu_id = cpu_id; |
| 2267 | if (thread_num == 0) { |
| 2268 | t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; |
| 2269 | if (cpu_is_first_core_in_package(cpu_id)) |
| 2270 | t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; |
| 2271 | } |
| 2272 | |
| 2273 | c->core_id = core_num; |
| 2274 | p->package_id = pkg_num; |
| 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | int initialize_counters(int cpu_id) |
| 2279 | { |
| 2280 | int my_thread_id, my_core_id, my_package_id; |
| 2281 | |
| 2282 | my_package_id = get_physical_package_id(cpu_id); |
| 2283 | my_core_id = get_core_id(cpu_id); |
| 2284 | |
| 2285 | if (cpu_is_first_sibling_in_core(cpu_id)) { |
| 2286 | my_thread_id = 0; |
| 2287 | topo.num_cores++; |
| 2288 | } else { |
| 2289 | my_thread_id = 1; |
| 2290 | } |
| 2291 | |
| 2292 | init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); |
| 2293 | init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); |
| 2294 | return 0; |
| 2295 | } |
| 2296 | |
| 2297 | void allocate_output_buffer() |
| 2298 | { |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 2299 | output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2300 | outp = output_buffer; |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2301 | if (outp == NULL) |
| 2302 | err(-1, "calloc output buffer"); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | void setup_all_buffers(void) |
| 2306 | { |
| 2307 | topology_probe(); |
| 2308 | allocate_counters(&thread_even, &core_even, &package_even); |
| 2309 | allocate_counters(&thread_odd, &core_odd, &package_odd); |
| 2310 | allocate_output_buffer(); |
| 2311 | for_all_proc_cpus(initialize_counters); |
| 2312 | } |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 2313 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2314 | void turbostat_init() |
| 2315 | { |
| 2316 | check_cpuid(); |
| 2317 | |
| 2318 | check_dev_msr(); |
| 2319 | check_super_user(); |
| 2320 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2321 | setup_all_buffers(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2322 | |
| 2323 | if (verbose) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2324 | print_verbose_header(); |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2325 | |
| 2326 | if (verbose) |
| 2327 | for_all_cpus(print_epb, ODD_COUNTERS); |
| 2328 | |
| 2329 | if (verbose) |
| 2330 | for_all_cpus(print_rapl, ODD_COUNTERS); |
| 2331 | |
| 2332 | for_all_cpus(set_temperature_target, ODD_COUNTERS); |
| 2333 | |
| 2334 | if (verbose) |
| 2335 | for_all_cpus(print_thermal, ODD_COUNTERS); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | int fork_it(char **argv) |
| 2339 | { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2340 | pid_t child_pid; |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 2341 | int status; |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 2342 | |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 2343 | status = for_all_cpus(get_counters, EVEN_COUNTERS); |
| 2344 | if (status) |
| 2345 | exit(status); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2346 | /* clear affinity side-effect of get_counters() */ |
| 2347 | sched_setaffinity(0, cpu_present_setsize, cpu_present_set); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2348 | gettimeofday(&tv_even, (struct timezone *)NULL); |
| 2349 | |
| 2350 | child_pid = fork(); |
| 2351 | if (!child_pid) { |
| 2352 | /* child */ |
| 2353 | execvp(argv[0], argv); |
| 2354 | } else { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2355 | |
| 2356 | /* parent */ |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2357 | if (child_pid == -1) |
| 2358 | err(1, "fork"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2359 | |
| 2360 | signal(SIGINT, SIG_IGN); |
| 2361 | signal(SIGQUIT, SIG_IGN); |
Josh Triplett | b2c95d9 | 2013-08-20 17:20:18 -0700 | [diff] [blame] | 2362 | if (waitpid(child_pid, &status, 0) == -1) |
| 2363 | err(status, "waitpid"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2364 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2365 | /* |
| 2366 | * n.b. fork_it() does not check for errors from for_all_cpus() |
| 2367 | * because re-starting is problematic when forking |
| 2368 | */ |
| 2369 | for_all_cpus(get_counters, ODD_COUNTERS); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2370 | gettimeofday(&tv_odd, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2371 | timersub(&tv_odd, &tv_even, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2372 | for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); |
| 2373 | compute_average(EVEN_COUNTERS); |
| 2374 | format_all_counters(EVEN_COUNTERS); |
| 2375 | flush_stderr(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2376 | |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 2377 | fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2378 | |
Len Brown | d91bb17 | 2012-11-01 00:08:19 -0400 | [diff] [blame] | 2379 | return status; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2380 | } |
| 2381 | |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 2382 | int get_and_dump_counters(void) |
| 2383 | { |
| 2384 | int status; |
| 2385 | |
| 2386 | status = for_all_cpus(get_counters, ODD_COUNTERS); |
| 2387 | if (status) |
| 2388 | return status; |
| 2389 | |
| 2390 | status = for_all_cpus(dump_counters, ODD_COUNTERS); |
| 2391 | if (status) |
| 2392 | return status; |
| 2393 | |
| 2394 | flush_stdout(); |
| 2395 | |
| 2396 | return status; |
| 2397 | } |
| 2398 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2399 | void cmdline(int argc, char **argv) |
| 2400 | { |
| 2401 | int opt; |
| 2402 | |
| 2403 | progname = argv[0]; |
| 2404 | |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 2405 | while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2406 | switch (opt) { |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 2407 | case 'p': |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2408 | show_core_only++; |
| 2409 | break; |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 2410 | case 'P': |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame] | 2411 | show_pkg_only++; |
| 2412 | break; |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 2413 | case 's': |
| 2414 | dump_only++; |
| 2415 | break; |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 2416 | case 'S': |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 2417 | summary_only++; |
| 2418 | break; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2419 | case 'v': |
| 2420 | verbose++; |
| 2421 | break; |
| 2422 | case 'i': |
| 2423 | interval_sec = atoi(optarg); |
| 2424 | break; |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 2425 | case 'c': |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 2426 | sscanf(optarg, "%x", &extra_delta_offset32); |
| 2427 | break; |
Len Brown | f924081 | 2012-10-06 15:26:31 -0400 | [diff] [blame] | 2428 | case 'C': |
Len Brown | 8e180f3 | 2012-09-22 01:25:08 -0400 | [diff] [blame] | 2429 | sscanf(optarg, "%x", &extra_delta_offset64); |
| 2430 | break; |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 2431 | case 'm': |
| 2432 | sscanf(optarg, "%x", &extra_msr_offset32); |
Len Brown | 2f32edf | 2012-09-21 23:45:46 -0400 | [diff] [blame] | 2433 | break; |
| 2434 | case 'M': |
| 2435 | sscanf(optarg, "%x", &extra_msr_offset64); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2436 | break; |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2437 | case 'R': |
| 2438 | rapl_verbose++; |
| 2439 | break; |
| 2440 | case 'T': |
| 2441 | tcc_activation_temp_override = atoi(optarg); |
| 2442 | break; |
Dirk Brandewie | 5c56be9 | 2013-12-16 10:23:41 -0800 | [diff] [blame] | 2443 | case 'J': |
| 2444 | rapl_joules++; |
| 2445 | break; |
| 2446 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2447 | default: |
| 2448 | usage(); |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | int main(int argc, char **argv) |
| 2454 | { |
| 2455 | cmdline(argc, argv); |
| 2456 | |
Len Brown | 889facb | 2012-11-08 00:48:57 -0500 | [diff] [blame] | 2457 | if (verbose) |
Len Brown | e6f9bb3 | 2013-12-03 02:19:19 -0500 | [diff] [blame] | 2458 | fprintf(stderr, "turbostat v3.6 Dec 2, 2013" |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2459 | " - Len Brown <lenb@kernel.org>\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2460 | |
| 2461 | turbostat_init(); |
| 2462 | |
Andy Shevchenko | 3b4d5c7 | 2014-01-23 17:13:15 +0200 | [diff] [blame^] | 2463 | /* dump counters and exit */ |
| 2464 | if (dump_only) |
| 2465 | return get_and_dump_counters(); |
| 2466 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 2467 | /* |
| 2468 | * if any params left, it must be a command to fork |
| 2469 | */ |
| 2470 | if (argc - optind) |
| 2471 | return fork_it(argv + optind); |
| 2472 | else |
| 2473 | turbostat_loop(); |
| 2474 | |
| 2475 | return 0; |
| 2476 | } |