turbostat.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * turbostat -- show CPU frequency and C-state residency
  3. * on modern Intel turbo-capable processors.
  4. *
  5. * Copyright (c) 2010, 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. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <sys/stat.h>
  26. #include <sys/resource.h>
  27. #include <fcntl.h>
  28. #include <signal.h>
  29. #include <sys/time.h>
  30. #include <stdlib.h>
  31. #include <dirent.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #define MSR_TSC 0x10
  35. #define MSR_NEHALEM_PLATFORM_INFO 0xCE
  36. #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
  37. #define MSR_APERF 0xE8
  38. #define MSR_MPERF 0xE7
  39. #define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */
  40. #define MSR_PKG_C3_RESIDENCY 0x3F8
  41. #define MSR_PKG_C6_RESIDENCY 0x3F9
  42. #define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */
  43. #define MSR_CORE_C3_RESIDENCY 0x3FC
  44. #define MSR_CORE_C6_RESIDENCY 0x3FD
  45. #define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */
  46. char *proc_stat = "/proc/stat";
  47. unsigned int interval_sec = 5; /* set with -i interval_sec */
  48. unsigned int verbose; /* set with -v */
  49. unsigned int skip_c0;
  50. unsigned int skip_c1;
  51. unsigned int do_nhm_cstates;
  52. unsigned int do_snb_cstates;
  53. unsigned int has_aperf;
  54. unsigned int units = 1000000000; /* Ghz etc */
  55. unsigned int genuine_intel;
  56. unsigned int has_invariant_tsc;
  57. unsigned int do_nehalem_platform_info;
  58. unsigned int do_nehalem_turbo_ratio_limit;
  59. unsigned int extra_msr_offset;
  60. double bclk;
  61. unsigned int show_pkg;
  62. unsigned int show_core;
  63. unsigned int show_cpu;
  64. int aperf_mperf_unstable;
  65. int backwards_count;
  66. char *progname;
  67. int need_reinitialize;
  68. int num_cpus;
  69. typedef struct per_cpu_counters {
  70. unsigned long long tsc; /* per thread */
  71. unsigned long long aperf; /* per thread */
  72. unsigned long long mperf; /* per thread */
  73. unsigned long long c1; /* per thread (calculated) */
  74. unsigned long long c3; /* per core */
  75. unsigned long long c6; /* per core */
  76. unsigned long long c7; /* per core */
  77. unsigned long long pc2; /* per package */
  78. unsigned long long pc3; /* per package */
  79. unsigned long long pc6; /* per package */
  80. unsigned long long pc7; /* per package */
  81. unsigned long long extra_msr; /* per thread */
  82. int pkg;
  83. int core;
  84. int cpu;
  85. struct per_cpu_counters *next;
  86. } PCC;
  87. PCC *pcc_even;
  88. PCC *pcc_odd;
  89. PCC *pcc_delta;
  90. PCC *pcc_average;
  91. struct timeval tv_even;
  92. struct timeval tv_odd;
  93. struct timeval tv_delta;
  94. unsigned long long get_msr(int cpu, off_t offset)
  95. {
  96. ssize_t retval;
  97. unsigned long long msr;
  98. char pathname[32];
  99. int fd;
  100. sprintf(pathname, "/dev/cpu/%d/msr", cpu);
  101. fd = open(pathname, O_RDONLY);
  102. if (fd < 0) {
  103. perror(pathname);
  104. need_reinitialize = 1;
  105. return 0;
  106. }
  107. retval = pread(fd, &msr, sizeof msr, offset);
  108. if (retval != sizeof msr) {
  109. fprintf(stderr, "cpu%d pread(..., 0x%zx) = %jd\n",
  110. cpu, offset, retval);
  111. exit(-2);
  112. }
  113. close(fd);
  114. return msr;
  115. }
  116. void print_header()
  117. {
  118. if (show_pkg)
  119. fprintf(stderr, "pkg ");
  120. if (show_core)
  121. fprintf(stderr, "core");
  122. if (show_cpu)
  123. fprintf(stderr, " CPU");
  124. if (do_nhm_cstates)
  125. fprintf(stderr, " %%c0 ");
  126. if (has_aperf)
  127. fprintf(stderr, " GHz");
  128. fprintf(stderr, " TSC");
  129. if (do_nhm_cstates)
  130. fprintf(stderr, " %%c1 ");
  131. if (do_nhm_cstates)
  132. fprintf(stderr, " %%c3 ");
  133. if (do_nhm_cstates)
  134. fprintf(stderr, " %%c6 ");
  135. if (do_snb_cstates)
  136. fprintf(stderr, " %%c7 ");
  137. if (do_snb_cstates)
  138. fprintf(stderr, " %%pc2 ");
  139. if (do_nhm_cstates)
  140. fprintf(stderr, " %%pc3 ");
  141. if (do_nhm_cstates)
  142. fprintf(stderr, " %%pc6 ");
  143. if (do_snb_cstates)
  144. fprintf(stderr, " %%pc7 ");
  145. if (extra_msr_offset)
  146. fprintf(stderr, " MSR 0x%x ", extra_msr_offset);
  147. putc('\n', stderr);
  148. }
  149. void dump_pcc(PCC *pcc)
  150. {
  151. fprintf(stderr, "package: %d ", pcc->pkg);
  152. fprintf(stderr, "core:: %d ", pcc->core);
  153. fprintf(stderr, "CPU: %d ", pcc->cpu);
  154. fprintf(stderr, "TSC: %016llX\n", pcc->tsc);
  155. fprintf(stderr, "c3: %016llX\n", pcc->c3);
  156. fprintf(stderr, "c6: %016llX\n", pcc->c6);
  157. fprintf(stderr, "c7: %016llX\n", pcc->c7);
  158. fprintf(stderr, "aperf: %016llX\n", pcc->aperf);
  159. fprintf(stderr, "pc2: %016llX\n", pcc->pc2);
  160. fprintf(stderr, "pc3: %016llX\n", pcc->pc3);
  161. fprintf(stderr, "pc6: %016llX\n", pcc->pc6);
  162. fprintf(stderr, "pc7: %016llX\n", pcc->pc7);
  163. fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, pcc->extra_msr);
  164. }
  165. void dump_list(PCC *pcc)
  166. {
  167. printf("dump_list 0x%p\n", pcc);
  168. for (; pcc; pcc = pcc->next)
  169. dump_pcc(pcc);
  170. }
  171. void print_pcc(PCC *p)
  172. {
  173. double interval_float;
  174. interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
  175. /* topology columns, print blanks on 1st (average) line */
  176. if (p == pcc_average) {
  177. if (show_pkg)
  178. fprintf(stderr, " ");
  179. if (show_core)
  180. fprintf(stderr, " ");
  181. if (show_cpu)
  182. fprintf(stderr, " ");
  183. } else {
  184. if (show_pkg)
  185. fprintf(stderr, "%4d", p->pkg);
  186. if (show_core)
  187. fprintf(stderr, "%4d", p->core);
  188. if (show_cpu)
  189. fprintf(stderr, "%4d", p->cpu);
  190. }
  191. /* %c0 */
  192. if (do_nhm_cstates) {
  193. if (!skip_c0)
  194. fprintf(stderr, "%7.2f", 100.0 * p->mperf/p->tsc);
  195. else
  196. fprintf(stderr, " ****");
  197. }
  198. /* GHz */
  199. if (has_aperf) {
  200. if (!aperf_mperf_unstable) {
  201. fprintf(stderr, "%5.2f",
  202. 1.0 * p->tsc / units * p->aperf /
  203. p->mperf / interval_float);
  204. } else {
  205. if (p->aperf > p->tsc || p->mperf > p->tsc) {
  206. fprintf(stderr, " ****");
  207. } else {
  208. fprintf(stderr, "%4.1f*",
  209. 1.0 * p->tsc /
  210. units * p->aperf /
  211. p->mperf / interval_float);
  212. }
  213. }
  214. }
  215. /* TSC */
  216. fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
  217. if (do_nhm_cstates) {
  218. if (!skip_c1)
  219. fprintf(stderr, "%7.2f", 100.0 * p->c1/p->tsc);
  220. else
  221. fprintf(stderr, " ****");
  222. }
  223. if (do_nhm_cstates)
  224. fprintf(stderr, "%7.2f", 100.0 * p->c3/p->tsc);
  225. if (do_nhm_cstates)
  226. fprintf(stderr, "%7.2f", 100.0 * p->c6/p->tsc);
  227. if (do_snb_cstates)
  228. fprintf(stderr, "%7.2f", 100.0 * p->c7/p->tsc);
  229. if (do_snb_cstates)
  230. fprintf(stderr, "%7.2f", 100.0 * p->pc2/p->tsc);
  231. if (do_nhm_cstates)
  232. fprintf(stderr, "%7.2f", 100.0 * p->pc3/p->tsc);
  233. if (do_nhm_cstates)
  234. fprintf(stderr, "%7.2f", 100.0 * p->pc6/p->tsc);
  235. if (do_snb_cstates)
  236. fprintf(stderr, "%7.2f", 100.0 * p->pc7/p->tsc);
  237. if (extra_msr_offset)
  238. fprintf(stderr, " 0x%016llx", p->extra_msr);
  239. putc('\n', stderr);
  240. }
  241. void print_counters(PCC *cnt)
  242. {
  243. PCC *pcc;
  244. print_header();
  245. if (num_cpus > 1)
  246. print_pcc(pcc_average);
  247. for (pcc = cnt; pcc != NULL; pcc = pcc->next)
  248. print_pcc(pcc);
  249. }
  250. #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
  251. int compute_delta(PCC *after, PCC *before, PCC *delta)
  252. {
  253. int errors = 0;
  254. int perf_err = 0;
  255. skip_c0 = skip_c1 = 0;
  256. for ( ; after && before && delta;
  257. after = after->next, before = before->next, delta = delta->next) {
  258. if (before->cpu != after->cpu) {
  259. printf("cpu configuration changed: %d != %d\n",
  260. before->cpu, after->cpu);
  261. return -1;
  262. }
  263. if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
  264. fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
  265. before->cpu, before->tsc, after->tsc);
  266. errors++;
  267. }
  268. /* check for TSC < 1 Mcycles over interval */
  269. if (delta->tsc < (1000 * 1000)) {
  270. fprintf(stderr, "Insanely slow TSC rate,"
  271. " TSC stops in idle?\n");
  272. fprintf(stderr, "You can disable all c-states"
  273. " by booting with \"idle=poll\"\n");
  274. fprintf(stderr, "or just the deep ones with"
  275. " \"processor.max_cstate=1\"\n");
  276. exit(-3);
  277. }
  278. if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
  279. fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
  280. before->cpu, before->c3, after->c3);
  281. errors++;
  282. }
  283. if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
  284. fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
  285. before->cpu, before->c6, after->c6);
  286. errors++;
  287. }
  288. if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
  289. fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
  290. before->cpu, before->c7, after->c7);
  291. errors++;
  292. }
  293. if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
  294. fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
  295. before->cpu, before->pc2, after->pc2);
  296. errors++;
  297. }
  298. if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
  299. fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
  300. before->cpu, before->pc3, after->pc3);
  301. errors++;
  302. }
  303. if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
  304. fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
  305. before->cpu, before->pc6, after->pc6);
  306. errors++;
  307. }
  308. if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
  309. fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
  310. before->cpu, before->pc7, after->pc7);
  311. errors++;
  312. }
  313. perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
  314. if (perf_err) {
  315. fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
  316. before->cpu, before->aperf, after->aperf);
  317. }
  318. perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
  319. if (perf_err) {
  320. fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
  321. before->cpu, before->mperf, after->mperf);
  322. }
  323. if (perf_err) {
  324. if (!aperf_mperf_unstable) {
  325. fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
  326. fprintf(stderr, "* Frequency results do not cover entire interval *\n");
  327. fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
  328. aperf_mperf_unstable = 1;
  329. }
  330. /*
  331. * mperf delta is likely a huge "positive" number
  332. * can not use it for calculating c0 time
  333. */
  334. skip_c0 = 1;
  335. skip_c1 = 1;
  336. }
  337. /*
  338. * As mperf and tsc collection are not atomic,
  339. * it is possible for mperf's non-halted cycles
  340. * to exceed TSC's all cycles: show c1 = 0% in that case.
  341. */
  342. if (delta->mperf > delta->tsc)
  343. delta->c1 = 0;
  344. else /* normal case, derive c1 */
  345. delta->c1 = delta->tsc - delta->mperf
  346. - delta->c3 - delta->c6 - delta->c7;
  347. if (delta->mperf == 0)
  348. delta->mperf = 1; /* divide by 0 protection */
  349. /*
  350. * for "extra msr", just copy the latest w/o subtracting
  351. */
  352. delta->extra_msr = after->extra_msr;
  353. if (errors) {
  354. fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
  355. dump_pcc(before);
  356. fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
  357. dump_pcc(after);
  358. errors = 0;
  359. }
  360. }
  361. return 0;
  362. }
  363. void compute_average(PCC *delta, PCC *avg)
  364. {
  365. PCC *sum;
  366. sum = calloc(1, sizeof(PCC));
  367. if (sum == NULL) {
  368. perror("calloc sum");
  369. exit(1);
  370. }
  371. for (; delta; delta = delta->next) {
  372. sum->tsc += delta->tsc;
  373. sum->c1 += delta->c1;
  374. sum->c3 += delta->c3;
  375. sum->c6 += delta->c6;
  376. sum->c7 += delta->c7;
  377. sum->aperf += delta->aperf;
  378. sum->mperf += delta->mperf;
  379. sum->pc2 += delta->pc2;
  380. sum->pc3 += delta->pc3;
  381. sum->pc6 += delta->pc6;
  382. sum->pc7 += delta->pc7;
  383. }
  384. avg->tsc = sum->tsc/num_cpus;
  385. avg->c1 = sum->c1/num_cpus;
  386. avg->c3 = sum->c3/num_cpus;
  387. avg->c6 = sum->c6/num_cpus;
  388. avg->c7 = sum->c7/num_cpus;
  389. avg->aperf = sum->aperf/num_cpus;
  390. avg->mperf = sum->mperf/num_cpus;
  391. avg->pc2 = sum->pc2/num_cpus;
  392. avg->pc3 = sum->pc3/num_cpus;
  393. avg->pc6 = sum->pc6/num_cpus;
  394. avg->pc7 = sum->pc7/num_cpus;
  395. free(sum);
  396. }
  397. void get_counters(PCC *pcc)
  398. {
  399. for ( ; pcc; pcc = pcc->next) {
  400. pcc->tsc = get_msr(pcc->cpu, MSR_TSC);
  401. if (do_nhm_cstates)
  402. pcc->c3 = get_msr(pcc->cpu, MSR_CORE_C3_RESIDENCY);
  403. if (do_nhm_cstates)
  404. pcc->c6 = get_msr(pcc->cpu, MSR_CORE_C6_RESIDENCY);
  405. if (do_snb_cstates)
  406. pcc->c7 = get_msr(pcc->cpu, MSR_CORE_C7_RESIDENCY);
  407. if (has_aperf)
  408. pcc->aperf = get_msr(pcc->cpu, MSR_APERF);
  409. if (has_aperf)
  410. pcc->mperf = get_msr(pcc->cpu, MSR_MPERF);
  411. if (do_snb_cstates)
  412. pcc->pc2 = get_msr(pcc->cpu, MSR_PKG_C2_RESIDENCY);
  413. if (do_nhm_cstates)
  414. pcc->pc3 = get_msr(pcc->cpu, MSR_PKG_C3_RESIDENCY);
  415. if (do_nhm_cstates)
  416. pcc->pc6 = get_msr(pcc->cpu, MSR_PKG_C6_RESIDENCY);
  417. if (do_snb_cstates)
  418. pcc->pc7 = get_msr(pcc->cpu, MSR_PKG_C7_RESIDENCY);
  419. if (extra_msr_offset)
  420. pcc->extra_msr = get_msr(pcc->cpu, extra_msr_offset);
  421. }
  422. }
  423. void print_nehalem_info()
  424. {
  425. unsigned long long msr;
  426. unsigned int ratio;
  427. if (!do_nehalem_platform_info)
  428. return;
  429. msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO);
  430. ratio = (msr >> 40) & 0xFF;
  431. fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
  432. ratio, bclk, ratio * bclk);
  433. ratio = (msr >> 8) & 0xFF;
  434. fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
  435. ratio, bclk, ratio * bclk);
  436. if (verbose > 1)
  437. fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
  438. if (!do_nehalem_turbo_ratio_limit)
  439. return;
  440. msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT);
  441. ratio = (msr >> 24) & 0xFF;
  442. if (ratio)
  443. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
  444. ratio, bclk, ratio * bclk);
  445. ratio = (msr >> 16) & 0xFF;
  446. if (ratio)
  447. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
  448. ratio, bclk, ratio * bclk);
  449. ratio = (msr >> 8) & 0xFF;
  450. if (ratio)
  451. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
  452. ratio, bclk, ratio * bclk);
  453. ratio = (msr >> 0) & 0xFF;
  454. if (ratio)
  455. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
  456. ratio, bclk, ratio * bclk);
  457. }
  458. void free_counter_list(PCC *list)
  459. {
  460. PCC *p;
  461. for (p = list; p; ) {
  462. PCC *free_me;
  463. free_me = p;
  464. p = p->next;
  465. free(free_me);
  466. }
  467. return;
  468. }
  469. void free_all_counters(void)
  470. {
  471. free_counter_list(pcc_even);
  472. pcc_even = NULL;
  473. free_counter_list(pcc_odd);
  474. pcc_odd = NULL;
  475. free_counter_list(pcc_delta);
  476. pcc_delta = NULL;
  477. free_counter_list(pcc_average);
  478. pcc_average = NULL;
  479. }
  480. void insert_cpu_counters(PCC **list, PCC *new)
  481. {
  482. PCC *prev;
  483. /*
  484. * list was empty
  485. */
  486. if (*list == NULL) {
  487. new->next = *list;
  488. *list = new;
  489. return;
  490. }
  491. show_cpu = 1; /* there is more than one CPU */
  492. /*
  493. * insert on front of list.
  494. * It is sorted by ascending package#, core#, cpu#
  495. */
  496. if (((*list)->pkg > new->pkg) ||
  497. (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
  498. (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
  499. new->next = *list;
  500. *list = new;
  501. return;
  502. }
  503. prev = *list;
  504. while (prev->next && (prev->next->pkg < new->pkg)) {
  505. prev = prev->next;
  506. show_pkg = 1; /* there is more than 1 package */
  507. }
  508. while (prev->next && (prev->next->pkg == new->pkg)
  509. && (prev->next->core < new->core)) {
  510. prev = prev->next;
  511. show_core = 1; /* there is more than 1 core */
  512. }
  513. while (prev->next && (prev->next->pkg == new->pkg)
  514. && (prev->next->core == new->core)
  515. && (prev->next->cpu < new->cpu)) {
  516. prev = prev->next;
  517. }
  518. /*
  519. * insert after "prev"
  520. */
  521. new->next = prev->next;
  522. prev->next = new;
  523. return;
  524. }
  525. void alloc_new_cpu_counters(int pkg, int core, int cpu)
  526. {
  527. PCC *new;
  528. if (verbose > 1)
  529. printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
  530. new = (PCC *)calloc(1, sizeof(PCC));
  531. if (new == NULL) {
  532. perror("calloc");
  533. exit(1);
  534. }
  535. new->pkg = pkg;
  536. new->core = core;
  537. new->cpu = cpu;
  538. insert_cpu_counters(&pcc_odd, new);
  539. new = (PCC *)calloc(1, sizeof(PCC));
  540. if (new == NULL) {
  541. perror("calloc");
  542. exit(1);
  543. }
  544. new->pkg = pkg;
  545. new->core = core;
  546. new->cpu = cpu;
  547. insert_cpu_counters(&pcc_even, new);
  548. new = (PCC *)calloc(1, sizeof(PCC));
  549. if (new == NULL) {
  550. perror("calloc");
  551. exit(1);
  552. }
  553. new->pkg = pkg;
  554. new->core = core;
  555. new->cpu = cpu;
  556. insert_cpu_counters(&pcc_delta, new);
  557. new = (PCC *)calloc(1, sizeof(PCC));
  558. if (new == NULL) {
  559. perror("calloc");
  560. exit(1);
  561. }
  562. new->pkg = pkg;
  563. new->core = core;
  564. new->cpu = cpu;
  565. pcc_average = new;
  566. }
  567. int get_physical_package_id(int cpu)
  568. {
  569. char path[64];
  570. FILE *filep;
  571. int pkg;
  572. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  573. filep = fopen(path, "r");
  574. if (filep == NULL) {
  575. perror(path);
  576. exit(1);
  577. }
  578. fscanf(filep, "%d", &pkg);
  579. fclose(filep);
  580. return pkg;
  581. }
  582. int get_core_id(int cpu)
  583. {
  584. char path[64];
  585. FILE *filep;
  586. int core;
  587. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
  588. filep = fopen(path, "r");
  589. if (filep == NULL) {
  590. perror(path);
  591. exit(1);
  592. }
  593. fscanf(filep, "%d", &core);
  594. fclose(filep);
  595. return core;
  596. }
  597. /*
  598. * run func(index, cpu) on every cpu in /proc/stat
  599. */
  600. int for_all_cpus(void (func)(int, int, int))
  601. {
  602. FILE *fp;
  603. int cpu_count;
  604. int retval;
  605. fp = fopen(proc_stat, "r");
  606. if (fp == NULL) {
  607. perror(proc_stat);
  608. exit(1);
  609. }
  610. retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  611. if (retval != 0) {
  612. perror("/proc/stat format");
  613. exit(1);
  614. }
  615. for (cpu_count = 0; ; cpu_count++) {
  616. int cpu;
  617. retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
  618. if (retval != 1)
  619. break;
  620. func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
  621. }
  622. fclose(fp);
  623. return cpu_count;
  624. }
  625. void re_initialize(void)
  626. {
  627. printf("turbostat: topology changed, re-initializing.\n");
  628. free_all_counters();
  629. num_cpus = for_all_cpus(alloc_new_cpu_counters);
  630. need_reinitialize = 0;
  631. printf("num_cpus is now %d\n", num_cpus);
  632. }
  633. void dummy(int pkg, int core, int cpu) { return; }
  634. /*
  635. * check to see if a cpu came on-line
  636. */
  637. void verify_num_cpus()
  638. {
  639. int new_num_cpus;
  640. new_num_cpus = for_all_cpus(dummy);
  641. if (new_num_cpus != num_cpus) {
  642. if (verbose)
  643. printf("num_cpus was %d, is now %d\n",
  644. num_cpus, new_num_cpus);
  645. need_reinitialize = 1;
  646. }
  647. return;
  648. }
  649. void turbostat_loop()
  650. {
  651. restart:
  652. get_counters(pcc_even);
  653. gettimeofday(&tv_even, (struct timezone *)NULL);
  654. while (1) {
  655. verify_num_cpus();
  656. if (need_reinitialize) {
  657. re_initialize();
  658. goto restart;
  659. }
  660. sleep(interval_sec);
  661. get_counters(pcc_odd);
  662. gettimeofday(&tv_odd, (struct timezone *)NULL);
  663. compute_delta(pcc_odd, pcc_even, pcc_delta);
  664. timersub(&tv_odd, &tv_even, &tv_delta);
  665. compute_average(pcc_delta, pcc_average);
  666. print_counters(pcc_delta);
  667. if (need_reinitialize) {
  668. re_initialize();
  669. goto restart;
  670. }
  671. sleep(interval_sec);
  672. get_counters(pcc_even);
  673. gettimeofday(&tv_even, (struct timezone *)NULL);
  674. compute_delta(pcc_even, pcc_odd, pcc_delta);
  675. timersub(&tv_even, &tv_odd, &tv_delta);
  676. compute_average(pcc_delta, pcc_average);
  677. print_counters(pcc_delta);
  678. }
  679. }
  680. void check_dev_msr()
  681. {
  682. struct stat sb;
  683. if (stat("/dev/cpu/0/msr", &sb)) {
  684. fprintf(stderr, "no /dev/cpu/0/msr\n");
  685. fprintf(stderr, "Try \"# modprobe msr\"\n");
  686. exit(-5);
  687. }
  688. }
  689. void check_super_user()
  690. {
  691. if (getuid() != 0) {
  692. fprintf(stderr, "must be root\n");
  693. exit(-6);
  694. }
  695. }
  696. int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
  697. {
  698. if (!genuine_intel)
  699. return 0;
  700. if (family != 6)
  701. return 0;
  702. switch (model) {
  703. case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
  704. case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
  705. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  706. case 0x25: /* Westmere Client - Clarkdale, Arrandale */
  707. case 0x2C: /* Westmere EP - Gulftown */
  708. case 0x2A: /* SNB */
  709. case 0x2D: /* SNB Xeon */
  710. return 1;
  711. case 0x2E: /* Nehalem-EX Xeon - Beckton */
  712. case 0x2F: /* Westmere-EX Xeon - Eagleton */
  713. default:
  714. return 0;
  715. }
  716. }
  717. int is_snb(unsigned int family, unsigned int model)
  718. {
  719. if (!genuine_intel)
  720. return 0;
  721. switch (model) {
  722. case 0x2A:
  723. case 0x2D:
  724. return 1;
  725. }
  726. return 0;
  727. }
  728. double discover_bclk(unsigned int family, unsigned int model)
  729. {
  730. if (is_snb(family, model))
  731. return 100.00;
  732. else
  733. return 133.33;
  734. }
  735. void check_cpuid()
  736. {
  737. unsigned int eax, ebx, ecx, edx, max_level;
  738. unsigned int fms, family, model, stepping;
  739. eax = ebx = ecx = edx = 0;
  740. asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
  741. if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
  742. genuine_intel = 1;
  743. if (verbose)
  744. fprintf(stderr, "%.4s%.4s%.4s ",
  745. (char *)&ebx, (char *)&edx, (char *)&ecx);
  746. asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
  747. family = (fms >> 8) & 0xf;
  748. model = (fms >> 4) & 0xf;
  749. stepping = fms & 0xf;
  750. if (family == 6 || family == 0xf)
  751. model += ((fms >> 16) & 0xf) << 4;
  752. if (verbose)
  753. fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
  754. max_level, family, model, stepping, family, model, stepping);
  755. if (!(edx & (1 << 5))) {
  756. fprintf(stderr, "CPUID: no MSR\n");
  757. exit(1);
  758. }
  759. /*
  760. * check max extended function levels of CPUID.
  761. * This is needed to check for invariant TSC.
  762. * This check is valid for both Intel and AMD.
  763. */
  764. ebx = ecx = edx = 0;
  765. asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
  766. if (max_level < 0x80000007) {
  767. fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
  768. exit(1);
  769. }
  770. /*
  771. * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
  772. * this check is valid for both Intel and AMD
  773. */
  774. asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
  775. has_invariant_tsc = edx && (1 << 8);
  776. if (!has_invariant_tsc) {
  777. fprintf(stderr, "No invariant TSC\n");
  778. exit(1);
  779. }
  780. /*
  781. * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
  782. * this check is valid for both Intel and AMD
  783. */
  784. asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
  785. has_aperf = ecx && (1 << 0);
  786. if (!has_aperf) {
  787. fprintf(stderr, "No APERF MSR\n");
  788. exit(1);
  789. }
  790. do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
  791. do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
  792. do_snb_cstates = is_snb(family, model);
  793. bclk = discover_bclk(family, model);
  794. do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
  795. }
  796. void usage()
  797. {
  798. fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
  799. progname);
  800. exit(1);
  801. }
  802. /*
  803. * in /dev/cpu/ return success for names that are numbers
  804. * ie. filter out ".", "..", "microcode".
  805. */
  806. int dir_filter(const struct dirent *dirp)
  807. {
  808. if (isdigit(dirp->d_name[0]))
  809. return 1;
  810. else
  811. return 0;
  812. }
  813. int open_dev_cpu_msr(int dummy1)
  814. {
  815. return 0;
  816. }
  817. void turbostat_init()
  818. {
  819. check_cpuid();
  820. check_dev_msr();
  821. check_super_user();
  822. num_cpus = for_all_cpus(alloc_new_cpu_counters);
  823. if (verbose)
  824. print_nehalem_info();
  825. }
  826. int fork_it(char **argv)
  827. {
  828. int retval;
  829. pid_t child_pid;
  830. get_counters(pcc_even);
  831. gettimeofday(&tv_even, (struct timezone *)NULL);
  832. child_pid = fork();
  833. if (!child_pid) {
  834. /* child */
  835. execvp(argv[0], argv);
  836. } else {
  837. int status;
  838. /* parent */
  839. if (child_pid == -1) {
  840. perror("fork");
  841. exit(1);
  842. }
  843. signal(SIGINT, SIG_IGN);
  844. signal(SIGQUIT, SIG_IGN);
  845. if (waitpid(child_pid, &status, 0) == -1) {
  846. perror("wait");
  847. exit(1);
  848. }
  849. }
  850. get_counters(pcc_odd);
  851. gettimeofday(&tv_odd, (struct timezone *)NULL);
  852. retval = compute_delta(pcc_odd, pcc_even, pcc_delta);
  853. timersub(&tv_odd, &tv_even, &tv_delta);
  854. compute_average(pcc_delta, pcc_average);
  855. if (!retval)
  856. print_counters(pcc_delta);
  857. fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);;
  858. return 0;
  859. }
  860. void cmdline(int argc, char **argv)
  861. {
  862. int opt;
  863. progname = argv[0];
  864. while ((opt = getopt(argc, argv, "+vi:M:")) != -1) {
  865. switch (opt) {
  866. case 'v':
  867. verbose++;
  868. break;
  869. case 'i':
  870. interval_sec = atoi(optarg);
  871. break;
  872. case 'M':
  873. sscanf(optarg, "%x", &extra_msr_offset);
  874. if (verbose > 1)
  875. fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
  876. break;
  877. default:
  878. usage();
  879. }
  880. }
  881. }
  882. int main(int argc, char **argv)
  883. {
  884. cmdline(argc, argv);
  885. if (verbose > 1)
  886. fprintf(stderr, "turbostat Dec 6, 2010"
  887. " - Len Brown <lenb@kernel.org>\n");
  888. if (verbose > 1)
  889. fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
  890. turbostat_init();
  891. /*
  892. * if any params left, it must be a command to fork
  893. */
  894. if (argc - optind)
  895. return fork_it(argv + optind);
  896. else
  897. turbostat_loop();
  898. return 0;
  899. }