turbostat.c 39 KB

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