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 | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 5 | * Copyright (c) 2012 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 |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/resource.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <signal.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <dirent.h> |
| 34 | #include <string.h> |
| 35 | #include <ctype.h> |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 36 | #include <sched.h> |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 37 | |
| 38 | #define MSR_TSC 0x10 |
| 39 | #define MSR_NEHALEM_PLATFORM_INFO 0xCE |
| 40 | #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD |
| 41 | #define MSR_APERF 0xE8 |
| 42 | #define MSR_MPERF 0xE7 |
| 43 | #define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */ |
| 44 | #define MSR_PKG_C3_RESIDENCY 0x3F8 |
| 45 | #define MSR_PKG_C6_RESIDENCY 0x3F9 |
| 46 | #define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */ |
| 47 | #define MSR_CORE_C3_RESIDENCY 0x3FC |
| 48 | #define MSR_CORE_C6_RESIDENCY 0x3FD |
| 49 | #define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */ |
| 50 | |
| 51 | char *proc_stat = "/proc/stat"; |
| 52 | unsigned int interval_sec = 5; /* set with -i interval_sec */ |
| 53 | unsigned int verbose; /* set with -v */ |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 54 | unsigned int summary_only; /* set with -s */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 55 | unsigned int skip_c0; |
| 56 | unsigned int skip_c1; |
| 57 | unsigned int do_nhm_cstates; |
| 58 | unsigned int do_snb_cstates; |
| 59 | unsigned int has_aperf; |
| 60 | unsigned int units = 1000000000; /* Ghz etc */ |
| 61 | unsigned int genuine_intel; |
| 62 | unsigned int has_invariant_tsc; |
| 63 | unsigned int do_nehalem_platform_info; |
| 64 | unsigned int do_nehalem_turbo_ratio_limit; |
| 65 | unsigned int extra_msr_offset; |
| 66 | double bclk; |
| 67 | unsigned int show_pkg; |
| 68 | unsigned int show_core; |
| 69 | unsigned int show_cpu; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 70 | unsigned int show_pkg_only; |
| 71 | unsigned int show_core_only; |
| 72 | char *output_buffer, *outp; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 73 | |
| 74 | int aperf_mperf_unstable; |
| 75 | int backwards_count; |
| 76 | char *progname; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 77 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 78 | cpu_set_t *cpu_present_set, *cpu_affinity_set; |
| 79 | size_t cpu_present_setsize, cpu_affinity_setsize; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 80 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 81 | struct thread_data { |
| 82 | unsigned long long tsc; |
| 83 | unsigned long long aperf; |
| 84 | unsigned long long mperf; |
| 85 | unsigned long long c1; /* derived */ |
| 86 | unsigned long long extra_msr; |
| 87 | unsigned int cpu_id; |
| 88 | unsigned int flags; |
| 89 | #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 |
| 90 | #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 |
| 91 | } *thread_even, *thread_odd; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 92 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 93 | struct core_data { |
| 94 | unsigned long long c3; |
| 95 | unsigned long long c6; |
| 96 | unsigned long long c7; |
| 97 | unsigned int core_id; |
| 98 | } *core_even, *core_odd; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 99 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 100 | struct pkg_data { |
| 101 | unsigned long long pc2; |
| 102 | unsigned long long pc3; |
| 103 | unsigned long long pc6; |
| 104 | unsigned long long pc7; |
| 105 | unsigned int package_id; |
| 106 | } *package_even, *package_odd; |
| 107 | |
| 108 | #define ODD_COUNTERS thread_odd, core_odd, package_odd |
| 109 | #define EVEN_COUNTERS thread_even, core_even, package_even |
| 110 | |
| 111 | #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ |
| 112 | (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ |
| 113 | topo.num_threads_per_core + \ |
| 114 | (core_no) * topo.num_threads_per_core + (thread_no)) |
| 115 | #define GET_CORE(core_base, core_no, pkg_no) \ |
| 116 | (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) |
| 117 | #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) |
| 118 | |
| 119 | struct system_summary { |
| 120 | struct thread_data threads; |
| 121 | struct core_data cores; |
| 122 | struct pkg_data packages; |
| 123 | } sum, average; |
| 124 | |
| 125 | |
| 126 | struct topo_params { |
| 127 | int num_packages; |
| 128 | int num_cpus; |
| 129 | int num_cores; |
| 130 | int max_cpu_num; |
| 131 | int num_cores_per_pkg; |
| 132 | int num_threads_per_core; |
| 133 | } topo; |
| 134 | |
| 135 | struct timeval tv_even, tv_odd, tv_delta; |
| 136 | |
| 137 | void setup_all_buffers(void); |
| 138 | |
| 139 | int cpu_is_not_present(int cpu) |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 140 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 141 | return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 142 | } |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 143 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 144 | * run func(thread, core, package) in topology order |
| 145 | * skip non-present cpus |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 146 | */ |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 147 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 148 | int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), |
| 149 | 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] | 150 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 151 | int retval, pkg_no, core_no, thread_no; |
| 152 | |
| 153 | for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { |
| 154 | for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { |
| 155 | for (thread_no = 0; thread_no < |
| 156 | topo.num_threads_per_core; ++thread_no) { |
| 157 | struct thread_data *t; |
| 158 | struct core_data *c; |
| 159 | struct pkg_data *p; |
| 160 | |
| 161 | t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); |
| 162 | |
| 163 | if (cpu_is_not_present(t->cpu_id)) |
| 164 | continue; |
| 165 | |
| 166 | c = GET_CORE(core_base, core_no, pkg_no); |
| 167 | p = GET_PKG(pkg_base, pkg_no); |
| 168 | |
| 169 | retval = func(t, c, p); |
| 170 | if (retval) |
| 171 | return retval; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return 0; |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int cpu_migrate(int cpu) |
| 179 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 180 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
| 181 | CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); |
| 182 | if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) |
Len Brown | 88c3281 | 2012-03-29 21:44:40 -0400 | [diff] [blame] | 183 | return -1; |
| 184 | else |
| 185 | return 0; |
| 186 | } |
| 187 | |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 188 | int get_msr(int cpu, off_t offset, unsigned long long *msr) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 189 | { |
| 190 | ssize_t retval; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 191 | char pathname[32]; |
| 192 | int fd; |
| 193 | |
| 194 | sprintf(pathname, "/dev/cpu/%d/msr", cpu); |
| 195 | fd = open(pathname, O_RDONLY); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 196 | if (fd < 0) |
| 197 | return -1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 198 | |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 199 | retval = pread(fd, msr, sizeof *msr, offset); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 200 | close(fd); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 201 | |
| 202 | if (retval != sizeof *msr) |
| 203 | return -1; |
| 204 | |
| 205 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Len Brown | a829eb4 | 2011-02-10 23:36:34 -0500 | [diff] [blame] | 208 | void print_header(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 209 | { |
| 210 | if (show_pkg) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 211 | outp += sprintf(outp, "pk"); |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 212 | if (show_pkg) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 213 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 214 | if (show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 215 | outp += sprintf(outp, "cor"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 216 | if (show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 217 | outp += sprintf(outp, " CPU"); |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 218 | if (show_pkg || show_core || show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 219 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 220 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 221 | outp += sprintf(outp, " %%c0"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 222 | if (has_aperf) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 223 | outp += sprintf(outp, " GHz"); |
| 224 | outp += sprintf(outp, " TSC"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 225 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 226 | outp += sprintf(outp, " %%c1"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 227 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 228 | outp += sprintf(outp, " %%c3"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 229 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 230 | outp += sprintf(outp, " %%c6"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 231 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 232 | outp += sprintf(outp, " %%c7"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 233 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 234 | outp += sprintf(outp, " %%pc2"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 235 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 236 | outp += sprintf(outp, " %%pc3"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 237 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 238 | outp += sprintf(outp, " %%pc6"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 239 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 240 | outp += sprintf(outp, " %%pc7"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 241 | if (extra_msr_offset) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 242 | outp += sprintf(outp, " MSR 0x%x ", extra_msr_offset); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 243 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 244 | outp += sprintf(outp, "\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 247 | int dump_counters(struct thread_data *t, struct core_data *c, |
| 248 | struct pkg_data *p) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 249 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 250 | fprintf(stderr, "t %p, c %p, p %p\n", t, c, p); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 251 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 252 | if (t) { |
| 253 | fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); |
| 254 | fprintf(stderr, "TSC: %016llX\n", t->tsc); |
| 255 | fprintf(stderr, "aperf: %016llX\n", t->aperf); |
| 256 | fprintf(stderr, "mperf: %016llX\n", t->mperf); |
| 257 | fprintf(stderr, "c1: %016llX\n", t->c1); |
| 258 | fprintf(stderr, "msr0x%x: %016llX\n", |
| 259 | extra_msr_offset, t->extra_msr); |
| 260 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 261 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 262 | if (c) { |
| 263 | fprintf(stderr, "core: %d\n", c->core_id); |
| 264 | fprintf(stderr, "c3: %016llX\n", c->c3); |
| 265 | fprintf(stderr, "c6: %016llX\n", c->c6); |
| 266 | fprintf(stderr, "c7: %016llX\n", c->c7); |
| 267 | } |
| 268 | |
| 269 | if (p) { |
| 270 | fprintf(stderr, "package: %d\n", p->package_id); |
| 271 | fprintf(stderr, "pc2: %016llX\n", p->pc2); |
| 272 | fprintf(stderr, "pc3: %016llX\n", p->pc3); |
| 273 | fprintf(stderr, "pc6: %016llX\n", p->pc6); |
| 274 | fprintf(stderr, "pc7: %016llX\n", p->pc7); |
| 275 | } |
| 276 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 279 | /* |
| 280 | * column formatting convention & formats |
| 281 | * package: "pk" 2 columns %2d |
| 282 | * core: "cor" 3 columns %3d |
| 283 | * CPU: "CPU" 3 columns %3d |
| 284 | * GHz: "GHz" 3 columns %3.2 |
| 285 | * TSC: "TSC" 3 columns %3.2 |
| 286 | * percentage " %pc3" %6.2 |
| 287 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 288 | int format_counters(struct thread_data *t, struct core_data *c, |
| 289 | struct pkg_data *p) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 290 | { |
| 291 | double interval_float; |
| 292 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 293 | /* if showing only 1st thread in core and this isn't one, bail out */ |
| 294 | if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 295 | return 0; |
| 296 | |
| 297 | /* if showing only 1st thread in pkg and this isn't one, bail out */ |
| 298 | if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 299 | return 0; |
| 300 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 301 | interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; |
| 302 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 303 | /* topo columns, print blanks on 1st (average) line */ |
| 304 | if (t == &average.threads) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 305 | if (show_pkg) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 306 | outp += sprintf(outp, " "); |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 307 | if (show_pkg && show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 308 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 309 | if (show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 310 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 311 | if (show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 312 | outp += sprintf(outp, " " " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 313 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 314 | if (show_pkg) { |
| 315 | if (p) |
| 316 | outp += sprintf(outp, "%2d", p->package_id); |
| 317 | else |
| 318 | outp += sprintf(outp, " "); |
| 319 | } |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 320 | if (show_pkg && show_core) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 321 | outp += sprintf(outp, " "); |
| 322 | if (show_core) { |
| 323 | if (c) |
| 324 | outp += sprintf(outp, "%3d", c->core_id); |
| 325 | else |
| 326 | outp += sprintf(outp, " "); |
| 327 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 328 | if (show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 329 | outp += sprintf(outp, " %3d", t->cpu_id); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* %c0 */ |
| 333 | if (do_nhm_cstates) { |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 334 | if (show_pkg || show_core || show_cpu) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 335 | outp += sprintf(outp, " "); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 336 | if (!skip_c0) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 337 | outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 338 | else |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 339 | outp += sprintf(outp, " ****"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* GHz */ |
| 343 | if (has_aperf) { |
| 344 | if (!aperf_mperf_unstable) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 345 | outp += sprintf(outp, " %3.2f", |
| 346 | 1.0 * t->tsc / units * t->aperf / |
| 347 | t->mperf / interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 348 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 349 | if (t->aperf > t->tsc || t->mperf > t->tsc) { |
| 350 | outp += sprintf(outp, " ***"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 351 | } else { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 352 | outp += sprintf(outp, "%3.1f*", |
| 353 | 1.0 * t->tsc / |
| 354 | units * t->aperf / |
| 355 | t->mperf / interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* TSC */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 361 | outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 362 | |
| 363 | if (do_nhm_cstates) { |
| 364 | if (!skip_c1) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 365 | outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 366 | else |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 367 | outp += sprintf(outp, " ****"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 368 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 369 | |
| 370 | /* print per-core data only for 1st thread in core */ |
| 371 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 372 | goto done; |
| 373 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 374 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 375 | outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 376 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 377 | outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 378 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 379 | outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc); |
| 380 | |
| 381 | /* print per-package data only for 1st core in package */ |
| 382 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 383 | goto done; |
| 384 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 385 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 386 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 387 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 388 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 389 | if (do_nhm_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 390 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 391 | if (do_snb_cstates) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 392 | outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc); |
| 393 | done: |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 394 | if (extra_msr_offset) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 395 | outp += sprintf(outp, " 0x%016llx", t->extra_msr); |
| 396 | outp += sprintf(outp, "\n"); |
| 397 | |
| 398 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 401 | void flush_stdout() |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 402 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 403 | fputs(output_buffer, stdout); |
| 404 | outp = output_buffer; |
| 405 | } |
| 406 | void flush_stderr() |
| 407 | { |
| 408 | fputs(output_buffer, stderr); |
| 409 | outp = output_buffer; |
| 410 | } |
| 411 | void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 412 | { |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 413 | static int printed; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 414 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 415 | if (!printed || !summary_only) |
| 416 | print_header(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 417 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 418 | if (topo.num_cpus > 1) |
| 419 | format_counters(&average.threads, &average.cores, |
| 420 | &average.packages); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 421 | |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 422 | printed = 1; |
| 423 | |
| 424 | if (summary_only) |
| 425 | return; |
| 426 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 427 | for_all_cpus(format_counters, t, c, p); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 428 | } |
| 429 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 430 | void |
| 431 | delta_package(struct pkg_data *new, struct pkg_data *old) |
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 | old->pc2 = new->pc2 - old->pc2; |
| 434 | old->pc3 = new->pc3 - old->pc3; |
| 435 | old->pc6 = new->pc6 - old->pc6; |
| 436 | old->pc7 = new->pc7 - old->pc7; |
| 437 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 438 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 439 | void |
| 440 | delta_core(struct core_data *new, struct core_data *old) |
| 441 | { |
| 442 | old->c3 = new->c3 - old->c3; |
| 443 | old->c6 = new->c6 - old->c6; |
| 444 | old->c7 = new->c7 - old->c7; |
| 445 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 446 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 447 | void |
| 448 | delta_thread(struct thread_data *new, struct thread_data *old, |
| 449 | struct core_data *core_delta) |
| 450 | { |
| 451 | old->tsc = new->tsc - old->tsc; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 452 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 453 | /* check for TSC < 1 Mcycles over interval */ |
| 454 | if (old->tsc < (1000 * 1000)) { |
| 455 | fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n"); |
| 456 | fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n"); |
| 457 | fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n"); |
| 458 | exit(-3); |
| 459 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 460 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 461 | old->c1 = new->c1 - old->c1; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 462 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 463 | if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { |
| 464 | old->aperf = new->aperf - old->aperf; |
| 465 | old->mperf = new->mperf - old->mperf; |
| 466 | } else { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 467 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 468 | if (!aperf_mperf_unstable) { |
| 469 | fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname); |
| 470 | fprintf(stderr, "* Frequency results do not cover entire interval *\n"); |
| 471 | fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n"); |
| 472 | |
| 473 | aperf_mperf_unstable = 1; |
| 474 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 475 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 476 | * mperf delta is likely a huge "positive" number |
| 477 | * can not use it for calculating c0 time |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 478 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 479 | skip_c0 = 1; |
| 480 | skip_c1 = 1; |
| 481 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 482 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 483 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 484 | /* |
| 485 | * As mperf and tsc collection are not atomic, |
| 486 | * it is possible for mperf's non-halted cycles |
| 487 | * to exceed TSC's all cycles: show c1 = 0% in that case. |
| 488 | */ |
| 489 | if (old->mperf > old->tsc) |
| 490 | old->c1 = 0; |
| 491 | else { |
| 492 | /* normal case, derive c1 */ |
| 493 | old->c1 = old->tsc - old->mperf - core_delta->c3 |
| 494 | - core_delta->c6 - core_delta->c7; |
| 495 | } |
| 496 | if (old->mperf == 0) { |
| 497 | if (verbose) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id); |
| 498 | old->mperf = 1; /* divide by 0 protection */ |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * for "extra msr", just copy the latest w/o subtracting |
| 503 | */ |
| 504 | old->extra_msr = new->extra_msr; |
| 505 | } |
| 506 | |
| 507 | int delta_cpu(struct thread_data *t, struct core_data *c, |
| 508 | struct pkg_data *p, struct thread_data *t2, |
| 509 | struct core_data *c2, struct pkg_data *p2) |
| 510 | { |
| 511 | /* calculate core delta only for 1st thread in core */ |
| 512 | if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) |
| 513 | delta_core(c, c2); |
| 514 | |
| 515 | /* always calculate thread delta */ |
| 516 | delta_thread(t, t2, c2); /* c2 is core delta */ |
| 517 | |
| 518 | /* calculate package delta only for 1st core in package */ |
| 519 | if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) |
| 520 | delta_package(p, p2); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 526 | { |
| 527 | t->tsc = 0; |
| 528 | t->aperf = 0; |
| 529 | t->mperf = 0; |
| 530 | t->c1 = 0; |
| 531 | |
| 532 | /* tells format_counters to dump all fields from this set */ |
| 533 | t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; |
| 534 | |
| 535 | c->c3 = 0; |
| 536 | c->c6 = 0; |
| 537 | c->c7 = 0; |
| 538 | |
| 539 | p->pc2 = 0; |
| 540 | p->pc3 = 0; |
| 541 | p->pc6 = 0; |
| 542 | p->pc7 = 0; |
| 543 | } |
| 544 | int sum_counters(struct thread_data *t, struct core_data *c, |
| 545 | struct pkg_data *p) |
| 546 | { |
| 547 | average.threads.tsc += t->tsc; |
| 548 | average.threads.aperf += t->aperf; |
| 549 | average.threads.mperf += t->mperf; |
| 550 | average.threads.c1 += t->c1; |
| 551 | |
| 552 | /* sum per-core values only for 1st thread in core */ |
| 553 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 554 | return 0; |
| 555 | |
| 556 | average.cores.c3 += c->c3; |
| 557 | average.cores.c6 += c->c6; |
| 558 | average.cores.c7 += c->c7; |
| 559 | |
| 560 | /* sum per-pkg values only for 1st core in pkg */ |
| 561 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 562 | return 0; |
| 563 | |
| 564 | average.packages.pc2 += p->pc2; |
| 565 | average.packages.pc3 += p->pc3; |
| 566 | average.packages.pc6 += p->pc6; |
| 567 | average.packages.pc7 += p->pc7; |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | /* |
| 572 | * sum the counters for all cpus in the system |
| 573 | * compute the weighted average |
| 574 | */ |
| 575 | void compute_average(struct thread_data *t, struct core_data *c, |
| 576 | struct pkg_data *p) |
| 577 | { |
| 578 | clear_counters(&average.threads, &average.cores, &average.packages); |
| 579 | |
| 580 | for_all_cpus(sum_counters, t, c, p); |
| 581 | |
| 582 | average.threads.tsc /= topo.num_cpus; |
| 583 | average.threads.aperf /= topo.num_cpus; |
| 584 | average.threads.mperf /= topo.num_cpus; |
| 585 | average.threads.c1 /= topo.num_cpus; |
| 586 | |
| 587 | average.cores.c3 /= topo.num_cores; |
| 588 | average.cores.c6 /= topo.num_cores; |
| 589 | average.cores.c7 /= topo.num_cores; |
| 590 | |
| 591 | average.packages.pc2 /= topo.num_packages; |
| 592 | average.packages.pc3 /= topo.num_packages; |
| 593 | average.packages.pc6 /= topo.num_packages; |
| 594 | average.packages.pc7 /= topo.num_packages; |
| 595 | } |
| 596 | |
| 597 | static unsigned long long rdtsc(void) |
| 598 | { |
| 599 | unsigned int low, high; |
| 600 | |
| 601 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
| 602 | |
| 603 | return low | ((unsigned long long)high) << 32; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /* |
| 608 | * get_counters(...) |
| 609 | * migrate to cpu |
| 610 | * acquire and record local counters for that cpu |
| 611 | */ |
| 612 | int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) |
| 613 | { |
| 614 | int cpu = t->cpu_id; |
| 615 | |
| 616 | if (cpu_migrate(cpu)) |
| 617 | return -1; |
| 618 | |
| 619 | t->tsc = rdtsc(); /* we are running on local CPU of interest */ |
| 620 | |
| 621 | if (has_aperf) { |
| 622 | if (get_msr(cpu, MSR_APERF, &t->aperf)) |
| 623 | return -3; |
| 624 | if (get_msr(cpu, MSR_MPERF, &t->mperf)) |
| 625 | return -4; |
| 626 | } |
| 627 | |
| 628 | if (extra_msr_offset) |
| 629 | if (get_msr(cpu, extra_msr_offset, &t->extra_msr)) |
| 630 | return -5; |
| 631 | |
| 632 | /* collect core counters only for 1st thread in core */ |
| 633 | if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) |
| 634 | return 0; |
| 635 | |
| 636 | if (do_nhm_cstates) { |
| 637 | if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) |
| 638 | return -6; |
| 639 | if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) |
| 640 | return -7; |
| 641 | } |
| 642 | |
| 643 | if (do_snb_cstates) |
| 644 | if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) |
| 645 | return -8; |
| 646 | |
| 647 | /* collect package counters only for 1st core in package */ |
| 648 | if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) |
| 649 | return 0; |
| 650 | |
| 651 | if (do_nhm_cstates) { |
| 652 | if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) |
| 653 | return -9; |
| 654 | if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) |
| 655 | return -10; |
| 656 | } |
| 657 | if (do_snb_cstates) { |
| 658 | if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) |
| 659 | return -11; |
| 660 | if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) |
| 661 | return -12; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 662 | } |
| 663 | return 0; |
| 664 | } |
| 665 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 666 | void print_verbose_header(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 667 | { |
| 668 | unsigned long long msr; |
| 669 | unsigned int ratio; |
| 670 | |
| 671 | if (!do_nehalem_platform_info) |
| 672 | return; |
| 673 | |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 674 | get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 675 | |
| 676 | ratio = (msr >> 40) & 0xFF; |
| 677 | fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n", |
| 678 | ratio, bclk, ratio * bclk); |
| 679 | |
| 680 | ratio = (msr >> 8) & 0xFF; |
| 681 | fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n", |
| 682 | ratio, bclk, ratio * bclk); |
| 683 | |
| 684 | if (verbose > 1) |
| 685 | fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr); |
| 686 | |
| 687 | if (!do_nehalem_turbo_ratio_limit) |
| 688 | return; |
| 689 | |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 690 | get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 691 | |
| 692 | ratio = (msr >> 24) & 0xFF; |
| 693 | if (ratio) |
| 694 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n", |
| 695 | ratio, bclk, ratio * bclk); |
| 696 | |
| 697 | ratio = (msr >> 16) & 0xFF; |
| 698 | if (ratio) |
| 699 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n", |
| 700 | ratio, bclk, ratio * bclk); |
| 701 | |
| 702 | ratio = (msr >> 8) & 0xFF; |
| 703 | if (ratio) |
| 704 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n", |
| 705 | ratio, bclk, ratio * bclk); |
| 706 | |
| 707 | ratio = (msr >> 0) & 0xFF; |
| 708 | if (ratio) |
| 709 | fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n", |
| 710 | ratio, bclk, ratio * bclk); |
| 711 | |
| 712 | } |
| 713 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 714 | void free_all_buffers(void) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 715 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 716 | CPU_FREE(cpu_present_set); |
| 717 | cpu_present_set = NULL; |
| 718 | cpu_present_set = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 719 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 720 | CPU_FREE(cpu_affinity_set); |
| 721 | cpu_affinity_set = NULL; |
| 722 | cpu_affinity_setsize = 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 723 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 724 | free(thread_even); |
| 725 | free(core_even); |
| 726 | free(package_even); |
| 727 | |
| 728 | thread_even = NULL; |
| 729 | core_even = NULL; |
| 730 | package_even = NULL; |
| 731 | |
| 732 | free(thread_odd); |
| 733 | free(core_odd); |
| 734 | free(package_odd); |
| 735 | |
| 736 | thread_odd = NULL; |
| 737 | core_odd = NULL; |
| 738 | package_odd = NULL; |
| 739 | |
| 740 | free(output_buffer); |
| 741 | output_buffer = NULL; |
| 742 | outp = NULL; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 745 | /* |
| 746 | * cpu_is_first_sibling_in_core(cpu) |
| 747 | * return 1 if given CPU is 1st HT sibling in the core |
| 748 | */ |
| 749 | int cpu_is_first_sibling_in_core(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 750 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 751 | char path[64]; |
| 752 | FILE *filep; |
| 753 | int first_cpu; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 754 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 755 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); |
| 756 | filep = fopen(path, "r"); |
| 757 | if (filep == NULL) { |
| 758 | perror(path); |
| 759 | exit(1); |
| 760 | } |
| 761 | fscanf(filep, "%d", &first_cpu); |
| 762 | fclose(filep); |
| 763 | return (cpu == first_cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 764 | } |
| 765 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 766 | /* |
| 767 | * cpu_is_first_core_in_package(cpu) |
| 768 | * return 1 if given CPU is 1st core in package |
| 769 | */ |
| 770 | int cpu_is_first_core_in_package(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 771 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 772 | char path[64]; |
| 773 | FILE *filep; |
| 774 | int first_cpu; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 775 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 776 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); |
| 777 | filep = fopen(path, "r"); |
| 778 | if (filep == NULL) { |
| 779 | perror(path); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 780 | exit(1); |
| 781 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 782 | fscanf(filep, "%d", &first_cpu); |
| 783 | fclose(filep); |
| 784 | return (cpu == first_cpu); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | int get_physical_package_id(int cpu) |
| 788 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 789 | char path[80]; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 790 | FILE *filep; |
| 791 | int pkg; |
| 792 | |
| 793 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); |
| 794 | filep = fopen(path, "r"); |
| 795 | if (filep == NULL) { |
| 796 | perror(path); |
| 797 | exit(1); |
| 798 | } |
| 799 | fscanf(filep, "%d", &pkg); |
| 800 | fclose(filep); |
| 801 | return pkg; |
| 802 | } |
| 803 | |
| 804 | int get_core_id(int cpu) |
| 805 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 806 | char path[80]; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 807 | FILE *filep; |
| 808 | int core; |
| 809 | |
| 810 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); |
| 811 | filep = fopen(path, "r"); |
| 812 | if (filep == NULL) { |
| 813 | perror(path); |
| 814 | exit(1); |
| 815 | } |
| 816 | fscanf(filep, "%d", &core); |
| 817 | fclose(filep); |
| 818 | return core; |
| 819 | } |
| 820 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 821 | int get_num_ht_siblings(int cpu) |
| 822 | { |
| 823 | char path[80]; |
| 824 | FILE *filep; |
| 825 | int sib1, sib2; |
| 826 | int matches; |
| 827 | char character; |
| 828 | |
| 829 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); |
| 830 | filep = fopen(path, "r"); |
| 831 | if (filep == NULL) { |
| 832 | perror(path); |
| 833 | exit(1); |
| 834 | } |
| 835 | /* |
| 836 | * file format: |
| 837 | * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) |
| 838 | * otherwinse 1 sibling (self). |
| 839 | */ |
| 840 | matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2); |
| 841 | |
| 842 | fclose(filep); |
| 843 | |
| 844 | if (matches == 3) |
| 845 | return 2; |
| 846 | else |
| 847 | return 1; |
| 848 | } |
| 849 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 850 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 851 | * run func(thread, core, package) in topology order |
| 852 | * skip non-present cpus |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 853 | */ |
| 854 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 855 | int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, |
| 856 | struct pkg_data *, struct thread_data *, struct core_data *, |
| 857 | struct pkg_data *), struct thread_data *thread_base, |
| 858 | struct core_data *core_base, struct pkg_data *pkg_base, |
| 859 | struct thread_data *thread_base2, struct core_data *core_base2, |
| 860 | struct pkg_data *pkg_base2) |
| 861 | { |
| 862 | int retval, pkg_no, core_no, thread_no; |
| 863 | |
| 864 | for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { |
| 865 | for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { |
| 866 | for (thread_no = 0; thread_no < |
| 867 | topo.num_threads_per_core; ++thread_no) { |
| 868 | struct thread_data *t, *t2; |
| 869 | struct core_data *c, *c2; |
| 870 | struct pkg_data *p, *p2; |
| 871 | |
| 872 | t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); |
| 873 | |
| 874 | if (cpu_is_not_present(t->cpu_id)) |
| 875 | continue; |
| 876 | |
| 877 | t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); |
| 878 | |
| 879 | c = GET_CORE(core_base, core_no, pkg_no); |
| 880 | c2 = GET_CORE(core_base2, core_no, pkg_no); |
| 881 | |
| 882 | p = GET_PKG(pkg_base, pkg_no); |
| 883 | p2 = GET_PKG(pkg_base2, pkg_no); |
| 884 | |
| 885 | retval = func(t, c, p, t2, c2, p2); |
| 886 | if (retval) |
| 887 | return retval; |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * run func(cpu) on every cpu in /proc/stat |
| 896 | * return max_cpu number |
| 897 | */ |
| 898 | int for_all_proc_cpus(int (func)(int)) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 899 | { |
| 900 | FILE *fp; |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 901 | int cpu_num; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 902 | int retval; |
| 903 | |
| 904 | fp = fopen(proc_stat, "r"); |
| 905 | if (fp == NULL) { |
| 906 | perror(proc_stat); |
| 907 | exit(1); |
| 908 | } |
| 909 | |
| 910 | retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); |
| 911 | if (retval != 0) { |
| 912 | perror("/proc/stat format"); |
| 913 | exit(1); |
| 914 | } |
| 915 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 916 | while (1) { |
| 917 | 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] | 918 | if (retval != 1) |
| 919 | break; |
| 920 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 921 | retval = func(cpu_num); |
| 922 | if (retval) { |
| 923 | fclose(fp); |
| 924 | return(retval); |
| 925 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 926 | } |
| 927 | fclose(fp); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 928 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | void re_initialize(void) |
| 932 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 933 | free_all_buffers(); |
| 934 | setup_all_buffers(); |
| 935 | printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 936 | } |
| 937 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 938 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 939 | /* |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 940 | * count_cpus() |
| 941 | * remember the last one seen, it will be the max |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 942 | */ |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 943 | int count_cpus(int cpu) |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 944 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 945 | if (topo.max_cpu_num < cpu) |
| 946 | topo.max_cpu_num = cpu; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 947 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 948 | topo.num_cpus += 1; |
| 949 | return 0; |
| 950 | } |
| 951 | int mark_cpu_present(int cpu) |
| 952 | { |
| 953 | CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 954 | return 0; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void turbostat_loop() |
| 958 | { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 959 | int retval; |
| 960 | |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 961 | restart: |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 962 | retval = for_all_cpus(get_counters, EVEN_COUNTERS); |
| 963 | if (retval) { |
| 964 | re_initialize(); |
| 965 | goto restart; |
| 966 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 967 | gettimeofday(&tv_even, (struct timezone *)NULL); |
| 968 | |
| 969 | while (1) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 970 | if (for_all_proc_cpus(cpu_is_not_present)) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 971 | re_initialize(); |
| 972 | goto restart; |
| 973 | } |
| 974 | sleep(interval_sec); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 975 | retval = for_all_cpus(get_counters, ODD_COUNTERS); |
| 976 | if (retval) { |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 977 | re_initialize(); |
| 978 | goto restart; |
| 979 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 980 | gettimeofday(&tv_odd, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 981 | timersub(&tv_odd, &tv_even, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 982 | for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); |
| 983 | compute_average(EVEN_COUNTERS); |
| 984 | format_all_counters(EVEN_COUNTERS); |
| 985 | flush_stdout(); |
Len Brown | 15aaa34 | 2012-03-29 22:19:58 -0400 | [diff] [blame] | 986 | sleep(interval_sec); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 987 | retval = for_all_cpus(get_counters, EVEN_COUNTERS); |
| 988 | if (retval) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 989 | re_initialize(); |
| 990 | goto restart; |
| 991 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 992 | gettimeofday(&tv_even, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 993 | timersub(&tv_even, &tv_odd, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 994 | for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS); |
| 995 | compute_average(ODD_COUNTERS); |
| 996 | format_all_counters(ODD_COUNTERS); |
| 997 | flush_stdout(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | void check_dev_msr() |
| 1002 | { |
| 1003 | struct stat sb; |
| 1004 | |
| 1005 | if (stat("/dev/cpu/0/msr", &sb)) { |
| 1006 | fprintf(stderr, "no /dev/cpu/0/msr\n"); |
| 1007 | fprintf(stderr, "Try \"# modprobe msr\"\n"); |
| 1008 | exit(-5); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | void check_super_user() |
| 1013 | { |
| 1014 | if (getuid() != 0) { |
| 1015 | fprintf(stderr, "must be root\n"); |
| 1016 | exit(-6); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) |
| 1021 | { |
| 1022 | if (!genuine_intel) |
| 1023 | return 0; |
| 1024 | |
| 1025 | if (family != 6) |
| 1026 | return 0; |
| 1027 | |
| 1028 | switch (model) { |
| 1029 | case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ |
| 1030 | case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ |
| 1031 | case 0x1F: /* Core i7 and i5 Processor - Nehalem */ |
| 1032 | case 0x25: /* Westmere Client - Clarkdale, Arrandale */ |
| 1033 | case 0x2C: /* Westmere EP - Gulftown */ |
| 1034 | case 0x2A: /* SNB */ |
| 1035 | case 0x2D: /* SNB Xeon */ |
Len Brown | 553575f | 2011-11-18 03:32:01 -0500 | [diff] [blame] | 1036 | case 0x3A: /* IVB */ |
| 1037 | case 0x3D: /* IVB Xeon */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1038 | return 1; |
| 1039 | case 0x2E: /* Nehalem-EX Xeon - Beckton */ |
| 1040 | case 0x2F: /* Westmere-EX Xeon - Eagleton */ |
| 1041 | default: |
| 1042 | return 0; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | int is_snb(unsigned int family, unsigned int model) |
| 1047 | { |
| 1048 | if (!genuine_intel) |
| 1049 | return 0; |
| 1050 | |
| 1051 | switch (model) { |
| 1052 | case 0x2A: |
| 1053 | case 0x2D: |
Len Brown | 650a37f | 2012-06-03 23:34:44 -0400 | [diff] [blame] | 1054 | case 0x3A: /* IVB */ |
| 1055 | case 0x3D: /* IVB Xeon */ |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1056 | return 1; |
| 1057 | } |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | double discover_bclk(unsigned int family, unsigned int model) |
| 1062 | { |
| 1063 | if (is_snb(family, model)) |
| 1064 | return 100.00; |
| 1065 | else |
| 1066 | return 133.33; |
| 1067 | } |
| 1068 | |
| 1069 | void check_cpuid() |
| 1070 | { |
| 1071 | unsigned int eax, ebx, ecx, edx, max_level; |
| 1072 | unsigned int fms, family, model, stepping; |
| 1073 | |
| 1074 | eax = ebx = ecx = edx = 0; |
| 1075 | |
| 1076 | asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0)); |
| 1077 | |
| 1078 | if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) |
| 1079 | genuine_intel = 1; |
| 1080 | |
| 1081 | if (verbose) |
| 1082 | fprintf(stderr, "%.4s%.4s%.4s ", |
| 1083 | (char *)&ebx, (char *)&edx, (char *)&ecx); |
| 1084 | |
| 1085 | asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx"); |
| 1086 | family = (fms >> 8) & 0xf; |
| 1087 | model = (fms >> 4) & 0xf; |
| 1088 | stepping = fms & 0xf; |
| 1089 | if (family == 6 || family == 0xf) |
| 1090 | model += ((fms >> 16) & 0xf) << 4; |
| 1091 | |
| 1092 | if (verbose) |
| 1093 | fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", |
| 1094 | max_level, family, model, stepping, family, model, stepping); |
| 1095 | |
| 1096 | if (!(edx & (1 << 5))) { |
| 1097 | fprintf(stderr, "CPUID: no MSR\n"); |
| 1098 | exit(1); |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * check max extended function levels of CPUID. |
| 1103 | * This is needed to check for invariant TSC. |
| 1104 | * This check is valid for both Intel and AMD. |
| 1105 | */ |
| 1106 | ebx = ecx = edx = 0; |
| 1107 | asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000)); |
| 1108 | |
| 1109 | if (max_level < 0x80000007) { |
| 1110 | fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); |
| 1111 | exit(1); |
| 1112 | } |
| 1113 | |
| 1114 | /* |
| 1115 | * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 |
| 1116 | * this check is valid for both Intel and AMD |
| 1117 | */ |
| 1118 | asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); |
Thomas Renninger | 8209e05 | 2011-01-21 15:11:19 +0100 | [diff] [blame] | 1119 | has_invariant_tsc = edx & (1 << 8); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1120 | |
| 1121 | if (!has_invariant_tsc) { |
| 1122 | fprintf(stderr, "No invariant TSC\n"); |
| 1123 | exit(1); |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 |
| 1128 | * this check is valid for both Intel and AMD |
| 1129 | */ |
| 1130 | |
| 1131 | asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); |
Thomas Renninger | 8209e05 | 2011-01-21 15:11:19 +0100 | [diff] [blame] | 1132 | has_aperf = ecx & (1 << 0); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1133 | if (!has_aperf) { |
| 1134 | fprintf(stderr, "No APERF MSR\n"); |
| 1135 | exit(1); |
| 1136 | } |
| 1137 | |
| 1138 | do_nehalem_platform_info = genuine_intel && has_invariant_tsc; |
| 1139 | do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ |
| 1140 | do_snb_cstates = is_snb(family, model); |
| 1141 | bclk = discover_bclk(family, model); |
| 1142 | |
| 1143 | do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model); |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | void usage() |
| 1148 | { |
| 1149 | fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n", |
| 1150 | progname); |
| 1151 | exit(1); |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | /* |
| 1156 | * in /dev/cpu/ return success for names that are numbers |
| 1157 | * ie. filter out ".", "..", "microcode". |
| 1158 | */ |
| 1159 | int dir_filter(const struct dirent *dirp) |
| 1160 | { |
| 1161 | if (isdigit(dirp->d_name[0])) |
| 1162 | return 1; |
| 1163 | else |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | int open_dev_cpu_msr(int dummy1) |
| 1168 | { |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1172 | void topology_probe() |
| 1173 | { |
| 1174 | int i; |
| 1175 | int max_core_id = 0; |
| 1176 | int max_package_id = 0; |
| 1177 | int max_siblings = 0; |
| 1178 | struct cpu_topology { |
| 1179 | int core_id; |
| 1180 | int physical_package_id; |
| 1181 | } *cpus; |
| 1182 | |
| 1183 | /* Initialize num_cpus, max_cpu_num */ |
| 1184 | topo.num_cpus = 0; |
| 1185 | topo.max_cpu_num = 0; |
| 1186 | for_all_proc_cpus(count_cpus); |
| 1187 | if (!summary_only && topo.num_cpus > 1) |
| 1188 | show_cpu = 1; |
| 1189 | |
| 1190 | if (verbose > 1) |
| 1191 | fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); |
| 1192 | |
| 1193 | cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); |
| 1194 | if (cpus == NULL) { |
| 1195 | perror("calloc cpus"); |
| 1196 | exit(1); |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Allocate and initialize cpu_present_set |
| 1201 | */ |
| 1202 | cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
| 1203 | if (cpu_present_set == NULL) { |
| 1204 | perror("CPU_ALLOC"); |
| 1205 | exit(3); |
| 1206 | } |
| 1207 | cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
| 1208 | CPU_ZERO_S(cpu_present_setsize, cpu_present_set); |
| 1209 | for_all_proc_cpus(mark_cpu_present); |
| 1210 | |
| 1211 | /* |
| 1212 | * Allocate and initialize cpu_affinity_set |
| 1213 | */ |
| 1214 | cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
| 1215 | if (cpu_affinity_set == NULL) { |
| 1216 | perror("CPU_ALLOC"); |
| 1217 | exit(3); |
| 1218 | } |
| 1219 | cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
| 1220 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
| 1221 | |
| 1222 | |
| 1223 | /* |
| 1224 | * For online cpus |
| 1225 | * find max_core_id, max_package_id |
| 1226 | */ |
| 1227 | for (i = 0; i <= topo.max_cpu_num; ++i) { |
| 1228 | int siblings; |
| 1229 | |
| 1230 | if (cpu_is_not_present(i)) { |
| 1231 | if (verbose > 1) |
| 1232 | fprintf(stderr, "cpu%d NOT PRESENT\n", i); |
| 1233 | continue; |
| 1234 | } |
| 1235 | cpus[i].core_id = get_core_id(i); |
| 1236 | if (cpus[i].core_id > max_core_id) |
| 1237 | max_core_id = cpus[i].core_id; |
| 1238 | |
| 1239 | cpus[i].physical_package_id = get_physical_package_id(i); |
| 1240 | if (cpus[i].physical_package_id > max_package_id) |
| 1241 | max_package_id = cpus[i].physical_package_id; |
| 1242 | |
| 1243 | siblings = get_num_ht_siblings(i); |
| 1244 | if (siblings > max_siblings) |
| 1245 | max_siblings = siblings; |
| 1246 | if (verbose > 1) |
| 1247 | fprintf(stderr, "cpu %d pkg %d core %d\n", |
| 1248 | i, cpus[i].physical_package_id, cpus[i].core_id); |
| 1249 | } |
| 1250 | topo.num_cores_per_pkg = max_core_id + 1; |
| 1251 | if (verbose > 1) |
| 1252 | fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n", |
| 1253 | max_core_id, topo.num_cores_per_pkg); |
| 1254 | if (!summary_only && topo.num_cores_per_pkg > 1) |
| 1255 | show_core = 1; |
| 1256 | |
| 1257 | topo.num_packages = max_package_id + 1; |
| 1258 | if (verbose > 1) |
| 1259 | fprintf(stderr, "max_package_id %d, sizing for %d packages\n", |
| 1260 | max_package_id, topo.num_packages); |
| 1261 | if (!summary_only && topo.num_packages > 1) |
| 1262 | show_pkg = 1; |
| 1263 | |
| 1264 | topo.num_threads_per_core = max_siblings; |
| 1265 | if (verbose > 1) |
| 1266 | fprintf(stderr, "max_siblings %d\n", max_siblings); |
| 1267 | |
| 1268 | free(cpus); |
| 1269 | } |
| 1270 | |
| 1271 | void |
| 1272 | allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) |
| 1273 | { |
| 1274 | int i; |
| 1275 | |
| 1276 | *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * |
| 1277 | topo.num_packages, sizeof(struct thread_data)); |
| 1278 | if (*t == NULL) |
| 1279 | goto error; |
| 1280 | |
| 1281 | for (i = 0; i < topo.num_threads_per_core * |
| 1282 | topo.num_cores_per_pkg * topo.num_packages; i++) |
| 1283 | (*t)[i].cpu_id = -1; |
| 1284 | |
| 1285 | *c = calloc(topo.num_cores_per_pkg * topo.num_packages, |
| 1286 | sizeof(struct core_data)); |
| 1287 | if (*c == NULL) |
| 1288 | goto error; |
| 1289 | |
| 1290 | for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) |
| 1291 | (*c)[i].core_id = -1; |
| 1292 | |
| 1293 | *p = calloc(topo.num_packages, sizeof(struct pkg_data)); |
| 1294 | if (*p == NULL) |
| 1295 | goto error; |
| 1296 | |
| 1297 | for (i = 0; i < topo.num_packages; i++) |
| 1298 | (*p)[i].package_id = i; |
| 1299 | |
| 1300 | return; |
| 1301 | error: |
| 1302 | perror("calloc counters"); |
| 1303 | exit(1); |
| 1304 | } |
| 1305 | /* |
| 1306 | * init_counter() |
| 1307 | * |
| 1308 | * set cpu_id, core_num, pkg_num |
| 1309 | * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE |
| 1310 | * |
| 1311 | * increment topo.num_cores when 1st core in pkg seen |
| 1312 | */ |
| 1313 | void init_counter(struct thread_data *thread_base, struct core_data *core_base, |
| 1314 | struct pkg_data *pkg_base, int thread_num, int core_num, |
| 1315 | int pkg_num, int cpu_id) |
| 1316 | { |
| 1317 | struct thread_data *t; |
| 1318 | struct core_data *c; |
| 1319 | struct pkg_data *p; |
| 1320 | |
| 1321 | t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); |
| 1322 | c = GET_CORE(core_base, core_num, pkg_num); |
| 1323 | p = GET_PKG(pkg_base, pkg_num); |
| 1324 | |
| 1325 | t->cpu_id = cpu_id; |
| 1326 | if (thread_num == 0) { |
| 1327 | t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; |
| 1328 | if (cpu_is_first_core_in_package(cpu_id)) |
| 1329 | t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; |
| 1330 | } |
| 1331 | |
| 1332 | c->core_id = core_num; |
| 1333 | p->package_id = pkg_num; |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | int initialize_counters(int cpu_id) |
| 1338 | { |
| 1339 | int my_thread_id, my_core_id, my_package_id; |
| 1340 | |
| 1341 | my_package_id = get_physical_package_id(cpu_id); |
| 1342 | my_core_id = get_core_id(cpu_id); |
| 1343 | |
| 1344 | if (cpu_is_first_sibling_in_core(cpu_id)) { |
| 1345 | my_thread_id = 0; |
| 1346 | topo.num_cores++; |
| 1347 | } else { |
| 1348 | my_thread_id = 1; |
| 1349 | } |
| 1350 | |
| 1351 | init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); |
| 1352 | init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | void allocate_output_buffer() |
| 1357 | { |
| 1358 | output_buffer = calloc(1, (1 + topo.num_cpus) * 128); |
| 1359 | outp = output_buffer; |
| 1360 | if (outp == NULL) { |
| 1361 | perror("calloc"); |
| 1362 | exit(-1); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | void setup_all_buffers(void) |
| 1367 | { |
| 1368 | topology_probe(); |
| 1369 | allocate_counters(&thread_even, &core_even, &package_even); |
| 1370 | allocate_counters(&thread_odd, &core_odd, &package_odd); |
| 1371 | allocate_output_buffer(); |
| 1372 | for_all_proc_cpus(initialize_counters); |
| 1373 | } |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1374 | void turbostat_init() |
| 1375 | { |
| 1376 | check_cpuid(); |
| 1377 | |
| 1378 | check_dev_msr(); |
| 1379 | check_super_user(); |
| 1380 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1381 | setup_all_buffers(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1382 | |
| 1383 | if (verbose) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1384 | print_verbose_header(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | int fork_it(char **argv) |
| 1388 | { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1389 | pid_t child_pid; |
Len Brown | d15cf7c | 2012-06-03 23:24:00 -0400 | [diff] [blame] | 1390 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1391 | for_all_cpus(get_counters, EVEN_COUNTERS); |
| 1392 | /* clear affinity side-effect of get_counters() */ |
| 1393 | sched_setaffinity(0, cpu_present_setsize, cpu_present_set); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1394 | gettimeofday(&tv_even, (struct timezone *)NULL); |
| 1395 | |
| 1396 | child_pid = fork(); |
| 1397 | if (!child_pid) { |
| 1398 | /* child */ |
| 1399 | execvp(argv[0], argv); |
| 1400 | } else { |
| 1401 | int status; |
| 1402 | |
| 1403 | /* parent */ |
| 1404 | if (child_pid == -1) { |
| 1405 | perror("fork"); |
| 1406 | exit(1); |
| 1407 | } |
| 1408 | |
| 1409 | signal(SIGINT, SIG_IGN); |
| 1410 | signal(SIGQUIT, SIG_IGN); |
| 1411 | if (waitpid(child_pid, &status, 0) == -1) { |
| 1412 | perror("wait"); |
| 1413 | exit(1); |
| 1414 | } |
| 1415 | } |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1416 | /* |
| 1417 | * n.b. fork_it() does not check for errors from for_all_cpus() |
| 1418 | * because re-starting is problematic when forking |
| 1419 | */ |
| 1420 | for_all_cpus(get_counters, ODD_COUNTERS); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1421 | gettimeofday(&tv_odd, (struct timezone *)NULL); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1422 | timersub(&tv_odd, &tv_even, &tv_delta); |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1423 | for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS); |
| 1424 | compute_average(EVEN_COUNTERS); |
| 1425 | format_all_counters(EVEN_COUNTERS); |
| 1426 | flush_stderr(); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1427 | |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 1428 | 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] | 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | void cmdline(int argc, char **argv) |
| 1434 | { |
| 1435 | int opt; |
| 1436 | |
| 1437 | progname = argv[0]; |
| 1438 | |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1439 | while ((opt = getopt(argc, argv, "+cpsvi:M:")) != -1) { |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1440 | switch (opt) { |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1441 | case 'c': |
| 1442 | show_core_only++; |
| 1443 | break; |
| 1444 | case 'p': |
| 1445 | show_pkg_only++; |
| 1446 | break; |
Len Brown | e23da03 | 2012-02-06 18:37:16 -0500 | [diff] [blame] | 1447 | case 's': |
| 1448 | summary_only++; |
| 1449 | break; |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1450 | case 'v': |
| 1451 | verbose++; |
| 1452 | break; |
| 1453 | case 'i': |
| 1454 | interval_sec = atoi(optarg); |
| 1455 | break; |
| 1456 | case 'M': |
| 1457 | sscanf(optarg, "%x", &extra_msr_offset); |
| 1458 | if (verbose > 1) |
| 1459 | fprintf(stderr, "MSR 0x%X\n", extra_msr_offset); |
| 1460 | break; |
| 1461 | default: |
| 1462 | usage(); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | int main(int argc, char **argv) |
| 1468 | { |
| 1469 | cmdline(argc, argv); |
| 1470 | |
| 1471 | if (verbose > 1) |
Len Brown | c98d5d9 | 2012-06-04 00:56:40 -0400 | [diff] [blame^] | 1472 | fprintf(stderr, "turbostat v2.0 May 16, 2012" |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1473 | " - Len Brown <lenb@kernel.org>\n"); |
Len Brown | 103a8fe | 2010-10-22 23:53:03 -0400 | [diff] [blame] | 1474 | |
| 1475 | turbostat_init(); |
| 1476 | |
| 1477 | /* |
| 1478 | * if any params left, it must be a command to fork |
| 1479 | */ |
| 1480 | if (argc - optind) |
| 1481 | return fork_it(argv + optind); |
| 1482 | else |
| 1483 | turbostat_loop(); |
| 1484 | |
| 1485 | return 0; |
| 1486 | } |