turbostat.c 25 KB

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