turbostat.c 34 KB

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