turbostat.c 36 KB

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