turbostat.c 38 KB

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