turbostat.c 24 KB

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