turbostat.c 39 KB

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