turbostat.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. struct 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 counters *next;
  86. };
  87. struct counters *cnt_even;
  88. struct counters *cnt_odd;
  89. struct counters *cnt_delta;
  90. struct counters *cnt_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(void)
  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_cnt(struct counters *cnt)
  150. {
  151. fprintf(stderr, "package: %d ", cnt->pkg);
  152. fprintf(stderr, "core:: %d ", cnt->core);
  153. fprintf(stderr, "CPU: %d ", cnt->cpu);
  154. fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
  155. fprintf(stderr, "c3: %016llX\n", cnt->c3);
  156. fprintf(stderr, "c6: %016llX\n", cnt->c6);
  157. fprintf(stderr, "c7: %016llX\n", cnt->c7);
  158. fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
  159. fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
  160. fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
  161. fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
  162. fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
  163. fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
  164. }
  165. void dump_list(struct counters *cnt)
  166. {
  167. printf("dump_list 0x%p\n", cnt);
  168. for (; cnt; cnt = cnt->next)
  169. dump_cnt(cnt);
  170. }
  171. void print_cnt(struct counters *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 == cnt_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(struct counters *counters)
  242. {
  243. struct counters *cnt;
  244. print_header();
  245. if (num_cpus > 1)
  246. print_cnt(cnt_average);
  247. for (cnt = counters; cnt != NULL; cnt = cnt->next)
  248. print_cnt(cnt);
  249. }
  250. #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
  251. int compute_delta(struct counters *after,
  252. struct counters *before, struct counters *delta)
  253. {
  254. int errors = 0;
  255. int perf_err = 0;
  256. skip_c0 = skip_c1 = 0;
  257. for ( ; after && before && delta;
  258. after = after->next, before = before->next, delta = delta->next) {
  259. if (before->cpu != after->cpu) {
  260. printf("cpu configuration changed: %d != %d\n",
  261. before->cpu, after->cpu);
  262. return -1;
  263. }
  264. if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
  265. fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
  266. before->cpu, before->tsc, after->tsc);
  267. errors++;
  268. }
  269. /* check for TSC < 1 Mcycles over interval */
  270. if (delta->tsc < (1000 * 1000)) {
  271. fprintf(stderr, "Insanely slow TSC rate,"
  272. " TSC stops in idle?\n");
  273. fprintf(stderr, "You can disable all c-states"
  274. " by booting with \"idle=poll\"\n");
  275. fprintf(stderr, "or just the deep ones with"
  276. " \"processor.max_cstate=1\"\n");
  277. exit(-3);
  278. }
  279. if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
  280. fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
  281. before->cpu, before->c3, after->c3);
  282. errors++;
  283. }
  284. if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
  285. fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
  286. before->cpu, before->c6, after->c6);
  287. errors++;
  288. }
  289. if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
  290. fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
  291. before->cpu, before->c7, after->c7);
  292. errors++;
  293. }
  294. if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
  295. fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
  296. before->cpu, before->pc2, after->pc2);
  297. errors++;
  298. }
  299. if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
  300. fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
  301. before->cpu, before->pc3, after->pc3);
  302. errors++;
  303. }
  304. if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
  305. fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
  306. before->cpu, before->pc6, after->pc6);
  307. errors++;
  308. }
  309. if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
  310. fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
  311. before->cpu, before->pc7, after->pc7);
  312. errors++;
  313. }
  314. perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
  315. if (perf_err) {
  316. fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
  317. before->cpu, before->aperf, after->aperf);
  318. }
  319. perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
  320. if (perf_err) {
  321. fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
  322. before->cpu, before->mperf, after->mperf);
  323. }
  324. if (perf_err) {
  325. if (!aperf_mperf_unstable) {
  326. fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
  327. fprintf(stderr, "* Frequency results do not cover entire interval *\n");
  328. fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
  329. aperf_mperf_unstable = 1;
  330. }
  331. /*
  332. * mperf delta is likely a huge "positive" number
  333. * can not use it for calculating c0 time
  334. */
  335. skip_c0 = 1;
  336. skip_c1 = 1;
  337. }
  338. /*
  339. * As mperf and tsc collection are not atomic,
  340. * it is possible for mperf's non-halted cycles
  341. * to exceed TSC's all cycles: show c1 = 0% in that case.
  342. */
  343. if (delta->mperf > delta->tsc)
  344. delta->c1 = 0;
  345. else /* normal case, derive c1 */
  346. delta->c1 = delta->tsc - delta->mperf
  347. - delta->c3 - delta->c6 - delta->c7;
  348. if (delta->mperf == 0)
  349. delta->mperf = 1; /* divide by 0 protection */
  350. /*
  351. * for "extra msr", just copy the latest w/o subtracting
  352. */
  353. delta->extra_msr = after->extra_msr;
  354. if (errors) {
  355. fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
  356. dump_cnt(before);
  357. fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
  358. dump_cnt(after);
  359. errors = 0;
  360. }
  361. }
  362. return 0;
  363. }
  364. void compute_average(struct counters *delta, struct counters *avg)
  365. {
  366. struct counters *sum;
  367. sum = calloc(1, sizeof(struct counters));
  368. if (sum == NULL) {
  369. perror("calloc sum");
  370. exit(1);
  371. }
  372. for (; delta; delta = delta->next) {
  373. sum->tsc += delta->tsc;
  374. sum->c1 += delta->c1;
  375. sum->c3 += delta->c3;
  376. sum->c6 += delta->c6;
  377. sum->c7 += delta->c7;
  378. sum->aperf += delta->aperf;
  379. sum->mperf += delta->mperf;
  380. sum->pc2 += delta->pc2;
  381. sum->pc3 += delta->pc3;
  382. sum->pc6 += delta->pc6;
  383. sum->pc7 += delta->pc7;
  384. }
  385. avg->tsc = sum->tsc/num_cpus;
  386. avg->c1 = sum->c1/num_cpus;
  387. avg->c3 = sum->c3/num_cpus;
  388. avg->c6 = sum->c6/num_cpus;
  389. avg->c7 = sum->c7/num_cpus;
  390. avg->aperf = sum->aperf/num_cpus;
  391. avg->mperf = sum->mperf/num_cpus;
  392. avg->pc2 = sum->pc2/num_cpus;
  393. avg->pc3 = sum->pc3/num_cpus;
  394. avg->pc6 = sum->pc6/num_cpus;
  395. avg->pc7 = sum->pc7/num_cpus;
  396. free(sum);
  397. }
  398. void get_counters(struct counters *cnt)
  399. {
  400. for ( ; cnt; cnt = cnt->next) {
  401. cnt->tsc = get_msr(cnt->cpu, MSR_TSC);
  402. if (do_nhm_cstates)
  403. cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY);
  404. if (do_nhm_cstates)
  405. cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY);
  406. if (do_snb_cstates)
  407. cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY);
  408. if (has_aperf)
  409. cnt->aperf = get_msr(cnt->cpu, MSR_APERF);
  410. if (has_aperf)
  411. cnt->mperf = get_msr(cnt->cpu, MSR_MPERF);
  412. if (do_snb_cstates)
  413. cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY);
  414. if (do_nhm_cstates)
  415. cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY);
  416. if (do_nhm_cstates)
  417. cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY);
  418. if (do_snb_cstates)
  419. cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY);
  420. if (extra_msr_offset)
  421. cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset);
  422. }
  423. }
  424. void print_nehalem_info(void)
  425. {
  426. unsigned long long msr;
  427. unsigned int ratio;
  428. if (!do_nehalem_platform_info)
  429. return;
  430. msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO);
  431. ratio = (msr >> 40) & 0xFF;
  432. fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
  433. ratio, bclk, ratio * bclk);
  434. ratio = (msr >> 8) & 0xFF;
  435. fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
  436. ratio, bclk, ratio * bclk);
  437. if (verbose > 1)
  438. fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
  439. if (!do_nehalem_turbo_ratio_limit)
  440. return;
  441. msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT);
  442. ratio = (msr >> 24) & 0xFF;
  443. if (ratio)
  444. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
  445. ratio, bclk, ratio * bclk);
  446. ratio = (msr >> 16) & 0xFF;
  447. if (ratio)
  448. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
  449. ratio, bclk, ratio * bclk);
  450. ratio = (msr >> 8) & 0xFF;
  451. if (ratio)
  452. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
  453. ratio, bclk, ratio * bclk);
  454. ratio = (msr >> 0) & 0xFF;
  455. if (ratio)
  456. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
  457. ratio, bclk, ratio * bclk);
  458. }
  459. void free_counter_list(struct counters *list)
  460. {
  461. struct counters *p;
  462. for (p = list; p; ) {
  463. struct counters *free_me;
  464. free_me = p;
  465. p = p->next;
  466. free(free_me);
  467. }
  468. }
  469. void free_all_counters(void)
  470. {
  471. free_counter_list(cnt_even);
  472. cnt_even = NULL;
  473. free_counter_list(cnt_odd);
  474. cnt_odd = NULL;
  475. free_counter_list(cnt_delta);
  476. cnt_delta = NULL;
  477. free_counter_list(cnt_average);
  478. cnt_average = NULL;
  479. }
  480. void insert_counters(struct counters **list,
  481. struct counters *new)
  482. {
  483. struct counters *prev;
  484. /*
  485. * list was empty
  486. */
  487. if (*list == NULL) {
  488. new->next = *list;
  489. *list = new;
  490. return;
  491. }
  492. show_cpu = 1; /* there is more than one CPU */
  493. /*
  494. * insert on front of list.
  495. * It is sorted by ascending package#, core#, cpu#
  496. */
  497. if (((*list)->pkg > new->pkg) ||
  498. (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
  499. (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
  500. new->next = *list;
  501. *list = new;
  502. return;
  503. }
  504. prev = *list;
  505. while (prev->next && (prev->next->pkg < new->pkg)) {
  506. prev = prev->next;
  507. show_pkg = 1; /* there is more than 1 package */
  508. }
  509. while (prev->next && (prev->next->pkg == new->pkg)
  510. && (prev->next->core < new->core)) {
  511. prev = prev->next;
  512. show_core = 1; /* there is more than 1 core */
  513. }
  514. while (prev->next && (prev->next->pkg == new->pkg)
  515. && (prev->next->core == new->core)
  516. && (prev->next->cpu < new->cpu)) {
  517. prev = prev->next;
  518. }
  519. /*
  520. * insert after "prev"
  521. */
  522. new->next = prev->next;
  523. prev->next = new;
  524. }
  525. void alloc_new_counters(int pkg, int core, int cpu)
  526. {
  527. struct counters *new;
  528. if (verbose > 1)
  529. printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
  530. new = (struct counters *)calloc(1, sizeof(struct counters));
  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_counters(&cnt_odd, new);
  539. new = (struct counters *)calloc(1,
  540. sizeof(struct counters));
  541. if (new == NULL) {
  542. perror("calloc");
  543. exit(1);
  544. }
  545. new->pkg = pkg;
  546. new->core = core;
  547. new->cpu = cpu;
  548. insert_counters(&cnt_even, new);
  549. new = (struct counters *)calloc(1, sizeof(struct counters));
  550. if (new == NULL) {
  551. perror("calloc");
  552. exit(1);
  553. }
  554. new->pkg = pkg;
  555. new->core = core;
  556. new->cpu = cpu;
  557. insert_counters(&cnt_delta, new);
  558. new = (struct counters *)calloc(1, sizeof(struct counters));
  559. if (new == NULL) {
  560. perror("calloc");
  561. exit(1);
  562. }
  563. new->pkg = pkg;
  564. new->core = core;
  565. new->cpu = cpu;
  566. cnt_average = new;
  567. }
  568. int get_physical_package_id(int cpu)
  569. {
  570. char path[64];
  571. FILE *filep;
  572. int pkg;
  573. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  574. filep = fopen(path, "r");
  575. if (filep == NULL) {
  576. perror(path);
  577. exit(1);
  578. }
  579. fscanf(filep, "%d", &pkg);
  580. fclose(filep);
  581. return pkg;
  582. }
  583. int get_core_id(int cpu)
  584. {
  585. char path[64];
  586. FILE *filep;
  587. int core;
  588. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
  589. filep = fopen(path, "r");
  590. if (filep == NULL) {
  591. perror(path);
  592. exit(1);
  593. }
  594. fscanf(filep, "%d", &core);
  595. fclose(filep);
  596. return core;
  597. }
  598. /*
  599. * run func(index, cpu) on every cpu in /proc/stat
  600. */
  601. int for_all_cpus(void (func)(int, int, int))
  602. {
  603. FILE *fp;
  604. int cpu_count;
  605. int retval;
  606. fp = fopen(proc_stat, "r");
  607. if (fp == NULL) {
  608. perror(proc_stat);
  609. exit(1);
  610. }
  611. retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  612. if (retval != 0) {
  613. perror("/proc/stat format");
  614. exit(1);
  615. }
  616. for (cpu_count = 0; ; cpu_count++) {
  617. int cpu;
  618. retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
  619. if (retval != 1)
  620. break;
  621. func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
  622. }
  623. fclose(fp);
  624. return cpu_count;
  625. }
  626. void re_initialize(void)
  627. {
  628. printf("turbostat: topology changed, re-initializing.\n");
  629. free_all_counters();
  630. num_cpus = for_all_cpus(alloc_new_counters);
  631. need_reinitialize = 0;
  632. printf("num_cpus is now %d\n", num_cpus);
  633. }
  634. void dummy(int pkg, int core, int cpu) { return; }
  635. /*
  636. * check to see if a cpu came on-line
  637. */
  638. void verify_num_cpus(void)
  639. {
  640. int new_num_cpus;
  641. new_num_cpus = for_all_cpus(dummy);
  642. if (new_num_cpus != num_cpus) {
  643. if (verbose)
  644. printf("num_cpus was %d, is now %d\n",
  645. num_cpus, new_num_cpus);
  646. need_reinitialize = 1;
  647. }
  648. }
  649. void turbostat_loop()
  650. {
  651. restart:
  652. get_counters(cnt_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(cnt_odd);
  662. gettimeofday(&tv_odd, (struct timezone *)NULL);
  663. compute_delta(cnt_odd, cnt_even, cnt_delta);
  664. timersub(&tv_odd, &tv_even, &tv_delta);
  665. compute_average(cnt_delta, cnt_average);
  666. print_counters(cnt_delta);
  667. if (need_reinitialize) {
  668. re_initialize();
  669. goto restart;
  670. }
  671. sleep(interval_sec);
  672. get_counters(cnt_even);
  673. gettimeofday(&tv_even, (struct timezone *)NULL);
  674. compute_delta(cnt_even, cnt_odd, cnt_delta);
  675. timersub(&tv_even, &tv_odd, &tv_delta);
  676. compute_average(cnt_delta, cnt_average);
  677. print_counters(cnt_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_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(cnt_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(cnt_odd);
  851. gettimeofday(&tv_odd, (struct timezone *)NULL);
  852. retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
  853. timersub(&tv_odd, &tv_even, &tv_delta);
  854. compute_average(cnt_delta, cnt_average);
  855. if (!retval)
  856. print_counters(cnt_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. }