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