turbostat.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. /*
  2. * turbostat -- show CPU frequency and C-state residency
  3. * on modern Intel turbo-capable processors.
  4. *
  5. * Copyright (c) 2012 Intel Corporation.
  6. * Len Brown <len.brown@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #define _GNU_SOURCE
  22. #include <asm/msr.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <sys/stat.h>
  28. #include <sys/resource.h>
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #include <sys/time.h>
  32. #include <stdlib.h>
  33. #include <dirent.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <sched.h>
  37. char *proc_stat = "/proc/stat";
  38. unsigned int interval_sec = 5; /* set with -i interval_sec */
  39. unsigned int verbose; /* set with -v */
  40. unsigned int rapl_verbose; /* set with -R */
  41. unsigned int thermal_verbose; /* set with -T */
  42. unsigned int summary_only; /* set with -s */
  43. unsigned int skip_c0;
  44. unsigned int skip_c1;
  45. unsigned int do_nhm_cstates;
  46. unsigned int do_snb_cstates;
  47. unsigned int has_aperf;
  48. unsigned int has_epb;
  49. unsigned int units = 1000000000; /* Ghz etc */
  50. unsigned int genuine_intel;
  51. unsigned int has_invariant_tsc;
  52. unsigned int do_nehalem_platform_info;
  53. unsigned int do_nehalem_turbo_ratio_limit;
  54. unsigned int do_ivt_turbo_ratio_limit;
  55. unsigned int extra_msr_offset32;
  56. unsigned int extra_msr_offset64;
  57. unsigned int extra_delta_offset32;
  58. unsigned int extra_delta_offset64;
  59. int do_smi;
  60. double bclk;
  61. unsigned int show_pkg;
  62. unsigned int show_core;
  63. unsigned int show_cpu;
  64. unsigned int show_pkg_only;
  65. unsigned int show_core_only;
  66. char *output_buffer, *outp;
  67. unsigned int do_rapl;
  68. unsigned int do_dts;
  69. unsigned int do_ptm;
  70. unsigned int tcc_activation_temp;
  71. unsigned int tcc_activation_temp_override;
  72. double rapl_power_units, rapl_energy_units, rapl_time_units;
  73. double rapl_joule_counter_range;
  74. #define RAPL_PKG (1 << 0)
  75. #define RAPL_CORES (1 << 1)
  76. #define RAPL_GFX (1 << 2)
  77. #define RAPL_DRAM (1 << 3)
  78. #define RAPL_PKG_PERF_STATUS (1 << 4)
  79. #define RAPL_DRAM_PERF_STATUS (1 << 5)
  80. #define TJMAX_DEFAULT 100
  81. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  82. int aperf_mperf_unstable;
  83. int backwards_count;
  84. char *progname;
  85. cpu_set_t *cpu_present_set, *cpu_affinity_set;
  86. size_t cpu_present_setsize, cpu_affinity_setsize;
  87. struct thread_data {
  88. unsigned long long tsc;
  89. unsigned long long aperf;
  90. unsigned long long mperf;
  91. unsigned long long c1; /* derived */
  92. unsigned long long extra_msr64;
  93. unsigned long long extra_delta64;
  94. unsigned long long extra_msr32;
  95. unsigned long long extra_delta32;
  96. unsigned int smi_count;
  97. unsigned int cpu_id;
  98. unsigned int flags;
  99. #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
  100. #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
  101. } *thread_even, *thread_odd;
  102. struct core_data {
  103. unsigned long long c3;
  104. unsigned long long c6;
  105. unsigned long long c7;
  106. unsigned int core_temp_c;
  107. unsigned int core_id;
  108. } *core_even, *core_odd;
  109. struct pkg_data {
  110. unsigned long long pc2;
  111. unsigned long long pc3;
  112. unsigned long long pc6;
  113. unsigned long long pc7;
  114. unsigned int package_id;
  115. unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
  116. unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
  117. unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
  118. unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
  119. unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
  120. unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
  121. unsigned int pkg_temp_c;
  122. } *package_even, *package_odd;
  123. #define ODD_COUNTERS thread_odd, core_odd, package_odd
  124. #define EVEN_COUNTERS thread_even, core_even, package_even
  125. #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
  126. (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
  127. topo.num_threads_per_core + \
  128. (core_no) * topo.num_threads_per_core + (thread_no))
  129. #define GET_CORE(core_base, core_no, pkg_no) \
  130. (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
  131. #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
  132. struct system_summary {
  133. struct thread_data threads;
  134. struct core_data cores;
  135. struct pkg_data packages;
  136. } sum, average;
  137. struct topo_params {
  138. int num_packages;
  139. int num_cpus;
  140. int num_cores;
  141. int max_cpu_num;
  142. int num_cores_per_pkg;
  143. int num_threads_per_core;
  144. } topo;
  145. struct timeval tv_even, tv_odd, tv_delta;
  146. void setup_all_buffers(void);
  147. int cpu_is_not_present(int cpu)
  148. {
  149. return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
  150. }
  151. /*
  152. * run func(thread, core, package) in topology order
  153. * skip non-present cpus
  154. */
  155. int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
  156. struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
  157. {
  158. int retval, pkg_no, core_no, thread_no;
  159. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  160. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  161. for (thread_no = 0; thread_no <
  162. topo.num_threads_per_core; ++thread_no) {
  163. struct thread_data *t;
  164. struct core_data *c;
  165. struct pkg_data *p;
  166. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  167. if (cpu_is_not_present(t->cpu_id))
  168. continue;
  169. c = GET_CORE(core_base, core_no, pkg_no);
  170. p = GET_PKG(pkg_base, pkg_no);
  171. retval = func(t, c, p);
  172. if (retval)
  173. return retval;
  174. }
  175. }
  176. }
  177. return 0;
  178. }
  179. int cpu_migrate(int cpu)
  180. {
  181. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  182. CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
  183. if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
  184. return -1;
  185. else
  186. return 0;
  187. }
  188. int get_msr(int cpu, off_t offset, unsigned long long *msr)
  189. {
  190. ssize_t retval;
  191. char pathname[32];
  192. int fd;
  193. sprintf(pathname, "/dev/cpu/%d/msr", cpu);
  194. fd = open(pathname, O_RDONLY);
  195. if (fd < 0)
  196. return -1;
  197. retval = pread(fd, msr, sizeof *msr, offset);
  198. close(fd);
  199. if (retval != sizeof *msr) {
  200. fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. void print_header(void)
  206. {
  207. if (show_pkg)
  208. outp += sprintf(outp, "pk");
  209. if (show_pkg)
  210. outp += sprintf(outp, " ");
  211. if (show_core)
  212. outp += sprintf(outp, "cor");
  213. if (show_cpu)
  214. outp += sprintf(outp, " CPU");
  215. if (show_pkg || show_core || show_cpu)
  216. outp += sprintf(outp, " ");
  217. if (do_nhm_cstates)
  218. outp += sprintf(outp, " %%c0");
  219. if (has_aperf)
  220. outp += sprintf(outp, " GHz");
  221. outp += sprintf(outp, " TSC");
  222. if (do_smi)
  223. outp += sprintf(outp, " SMI");
  224. if (extra_delta_offset32)
  225. outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
  226. if (extra_delta_offset64)
  227. outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
  228. if (extra_msr_offset32)
  229. outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
  230. if (extra_msr_offset64)
  231. outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
  232. if (do_nhm_cstates)
  233. outp += sprintf(outp, " %%c1");
  234. if (do_nhm_cstates)
  235. outp += sprintf(outp, " %%c3");
  236. if (do_nhm_cstates)
  237. outp += sprintf(outp, " %%c6");
  238. if (do_snb_cstates)
  239. outp += sprintf(outp, " %%c7");
  240. if (do_dts)
  241. outp += sprintf(outp, " CTMP");
  242. if (do_ptm)
  243. outp += sprintf(outp, " PTMP");
  244. if (do_snb_cstates)
  245. outp += sprintf(outp, " %%pc2");
  246. if (do_nhm_cstates)
  247. outp += sprintf(outp, " %%pc3");
  248. if (do_nhm_cstates)
  249. outp += sprintf(outp, " %%pc6");
  250. if (do_snb_cstates)
  251. outp += sprintf(outp, " %%pc7");
  252. if (do_rapl & RAPL_PKG)
  253. outp += sprintf(outp, " Pkg_W");
  254. if (do_rapl & RAPL_CORES)
  255. outp += sprintf(outp, " Cor_W");
  256. if (do_rapl & RAPL_GFX)
  257. outp += sprintf(outp, " GFX_W");
  258. if (do_rapl & RAPL_DRAM)
  259. outp += sprintf(outp, " RAM_W");
  260. if (do_rapl & RAPL_PKG_PERF_STATUS)
  261. outp += sprintf(outp, " PKG_%%");
  262. if (do_rapl & RAPL_DRAM_PERF_STATUS)
  263. outp += sprintf(outp, " RAM_%%");
  264. outp += sprintf(outp, "\n");
  265. }
  266. int dump_counters(struct thread_data *t, struct core_data *c,
  267. struct pkg_data *p)
  268. {
  269. fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
  270. if (t) {
  271. fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
  272. fprintf(stderr, "TSC: %016llX\n", t->tsc);
  273. fprintf(stderr, "aperf: %016llX\n", t->aperf);
  274. fprintf(stderr, "mperf: %016llX\n", t->mperf);
  275. fprintf(stderr, "c1: %016llX\n", t->c1);
  276. fprintf(stderr, "msr0x%x: %08llX\n",
  277. extra_delta_offset32, t->extra_delta32);
  278. fprintf(stderr, "msr0x%x: %016llX\n",
  279. extra_delta_offset64, t->extra_delta64);
  280. fprintf(stderr, "msr0x%x: %08llX\n",
  281. extra_msr_offset32, t->extra_msr32);
  282. fprintf(stderr, "msr0x%x: %016llX\n",
  283. extra_msr_offset64, t->extra_msr64);
  284. if (do_smi)
  285. fprintf(stderr, "SMI: %08X\n", t->smi_count);
  286. }
  287. if (c) {
  288. fprintf(stderr, "core: %d\n", c->core_id);
  289. fprintf(stderr, "c3: %016llX\n", c->c3);
  290. fprintf(stderr, "c6: %016llX\n", c->c6);
  291. fprintf(stderr, "c7: %016llX\n", c->c7);
  292. fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
  293. }
  294. if (p) {
  295. fprintf(stderr, "package: %d\n", p->package_id);
  296. fprintf(stderr, "pc2: %016llX\n", p->pc2);
  297. fprintf(stderr, "pc3: %016llX\n", p->pc3);
  298. fprintf(stderr, "pc6: %016llX\n", p->pc6);
  299. fprintf(stderr, "pc7: %016llX\n", p->pc7);
  300. fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
  301. fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
  302. fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
  303. fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
  304. fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
  305. fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
  306. fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
  307. }
  308. return 0;
  309. }
  310. /*
  311. * column formatting convention & formats
  312. * package: "pk" 2 columns %2d
  313. * core: "cor" 3 columns %3d
  314. * CPU: "CPU" 3 columns %3d
  315. * Pkg_W: %6.2
  316. * Cor_W: %6.2
  317. * GFX_W: %5.2
  318. * RAM_W: %5.2
  319. * GHz: "GHz" 3 columns %3.2
  320. * TSC: "TSC" 3 columns %3.2
  321. * SMI: "SMI" 4 columns %4d
  322. * percentage " %pc3" %6.2
  323. * Perf Status percentage: %5.2
  324. * "CTMP" 4 columns %4d
  325. */
  326. int format_counters(struct thread_data *t, struct core_data *c,
  327. struct pkg_data *p)
  328. {
  329. double interval_float;
  330. char *fmt5, *fmt6;
  331. /* if showing only 1st thread in core and this isn't one, bail out */
  332. if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  333. return 0;
  334. /* if showing only 1st thread in pkg and this isn't one, bail out */
  335. if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  336. return 0;
  337. interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
  338. /* topo columns, print blanks on 1st (average) line */
  339. if (t == &average.threads) {
  340. if (show_pkg)
  341. outp += sprintf(outp, " ");
  342. if (show_pkg && show_core)
  343. outp += sprintf(outp, " ");
  344. if (show_core)
  345. outp += sprintf(outp, " ");
  346. if (show_cpu)
  347. outp += sprintf(outp, " " " ");
  348. } else {
  349. if (show_pkg) {
  350. if (p)
  351. outp += sprintf(outp, "%2d", p->package_id);
  352. else
  353. outp += sprintf(outp, " ");
  354. }
  355. if (show_pkg && show_core)
  356. outp += sprintf(outp, " ");
  357. if (show_core) {
  358. if (c)
  359. outp += sprintf(outp, "%3d", c->core_id);
  360. else
  361. outp += sprintf(outp, " ");
  362. }
  363. if (show_cpu)
  364. outp += sprintf(outp, " %3d", t->cpu_id);
  365. }
  366. /* %c0 */
  367. if (do_nhm_cstates) {
  368. if (show_pkg || show_core || show_cpu)
  369. outp += sprintf(outp, " ");
  370. if (!skip_c0)
  371. outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
  372. else
  373. outp += sprintf(outp, " ****");
  374. }
  375. /* GHz */
  376. if (has_aperf) {
  377. if (!aperf_mperf_unstable) {
  378. outp += sprintf(outp, " %3.2f",
  379. 1.0 * t->tsc / units * t->aperf /
  380. t->mperf / interval_float);
  381. } else {
  382. if (t->aperf > t->tsc || t->mperf > t->tsc) {
  383. outp += sprintf(outp, " ***");
  384. } else {
  385. outp += sprintf(outp, "%3.1f*",
  386. 1.0 * t->tsc /
  387. units * t->aperf /
  388. t->mperf / interval_float);
  389. }
  390. }
  391. }
  392. /* TSC */
  393. outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
  394. /* SMI */
  395. if (do_smi)
  396. outp += sprintf(outp, "%4d", t->smi_count);
  397. /* delta */
  398. if (extra_delta_offset32)
  399. outp += sprintf(outp, " %11llu", t->extra_delta32);
  400. /* DELTA */
  401. if (extra_delta_offset64)
  402. outp += sprintf(outp, " %11llu", t->extra_delta64);
  403. /* msr */
  404. if (extra_msr_offset32)
  405. outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
  406. /* MSR */
  407. if (extra_msr_offset64)
  408. outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
  409. if (do_nhm_cstates) {
  410. if (!skip_c1)
  411. outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
  412. else
  413. outp += sprintf(outp, " ****");
  414. }
  415. /* print per-core data only for 1st thread in core */
  416. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  417. goto done;
  418. if (do_nhm_cstates)
  419. outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
  420. if (do_nhm_cstates)
  421. outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
  422. if (do_snb_cstates)
  423. outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
  424. if (do_dts)
  425. outp += sprintf(outp, " %4d", c->core_temp_c);
  426. /* print per-package data only for 1st core in package */
  427. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  428. goto done;
  429. if (do_ptm)
  430. outp += sprintf(outp, " %4d", p->pkg_temp_c);
  431. if (do_snb_cstates)
  432. outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
  433. if (do_nhm_cstates)
  434. outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
  435. if (do_nhm_cstates)
  436. outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
  437. if (do_snb_cstates)
  438. outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
  439. /*
  440. * If measurement interval exceeds minimum RAPL Joule Counter range,
  441. * indicate that results are suspect by printing "**" in fraction place.
  442. */
  443. if (interval_float < rapl_joule_counter_range) {
  444. fmt5 = " %5.2f";
  445. fmt6 = " %6.2f";
  446. } else {
  447. fmt5 = " %3.0f**";
  448. fmt6 = " %4.0f**";
  449. }
  450. if (do_rapl & RAPL_PKG)
  451. outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
  452. if (do_rapl & RAPL_CORES)
  453. outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
  454. if (do_rapl & RAPL_GFX)
  455. outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
  456. if (do_rapl & RAPL_DRAM)
  457. outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
  458. if (do_rapl & RAPL_PKG_PERF_STATUS )
  459. outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
  460. if (do_rapl & RAPL_DRAM_PERF_STATUS )
  461. outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
  462. done:
  463. outp += sprintf(outp, "\n");
  464. return 0;
  465. }
  466. void flush_stdout()
  467. {
  468. fputs(output_buffer, stdout);
  469. fflush(stdout);
  470. outp = output_buffer;
  471. }
  472. void flush_stderr()
  473. {
  474. fputs(output_buffer, stderr);
  475. outp = output_buffer;
  476. }
  477. void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  478. {
  479. static int printed;
  480. if (!printed || !summary_only)
  481. print_header();
  482. if (topo.num_cpus > 1)
  483. format_counters(&average.threads, &average.cores,
  484. &average.packages);
  485. printed = 1;
  486. if (summary_only)
  487. return;
  488. for_all_cpus(format_counters, t, c, p);
  489. }
  490. #define DELTA_WRAP32(new, old) \
  491. if (new > old) { \
  492. old = new - old; \
  493. } else { \
  494. old = 0x100000000 + new - old; \
  495. }
  496. void
  497. delta_package(struct pkg_data *new, struct pkg_data *old)
  498. {
  499. old->pc2 = new->pc2 - old->pc2;
  500. old->pc3 = new->pc3 - old->pc3;
  501. old->pc6 = new->pc6 - old->pc6;
  502. old->pc7 = new->pc7 - old->pc7;
  503. old->pkg_temp_c = new->pkg_temp_c;
  504. DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
  505. DELTA_WRAP32(new->energy_cores, old->energy_cores);
  506. DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
  507. DELTA_WRAP32(new->energy_dram, old->energy_dram);
  508. DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
  509. DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
  510. }
  511. void
  512. delta_core(struct core_data *new, struct core_data *old)
  513. {
  514. old->c3 = new->c3 - old->c3;
  515. old->c6 = new->c6 - old->c6;
  516. old->c7 = new->c7 - old->c7;
  517. old->core_temp_c = new->core_temp_c;
  518. }
  519. /*
  520. * old = new - old
  521. */
  522. void
  523. delta_thread(struct thread_data *new, struct thread_data *old,
  524. struct core_data *core_delta)
  525. {
  526. old->tsc = new->tsc - old->tsc;
  527. /* check for TSC < 1 Mcycles over interval */
  528. if (old->tsc < (1000 * 1000)) {
  529. fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
  530. fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
  531. fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
  532. exit(-3);
  533. }
  534. old->c1 = new->c1 - old->c1;
  535. if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
  536. old->aperf = new->aperf - old->aperf;
  537. old->mperf = new->mperf - old->mperf;
  538. } else {
  539. if (!aperf_mperf_unstable) {
  540. fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
  541. fprintf(stderr, "* Frequency results do not cover entire interval *\n");
  542. fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
  543. aperf_mperf_unstable = 1;
  544. }
  545. /*
  546. * mperf delta is likely a huge "positive" number
  547. * can not use it for calculating c0 time
  548. */
  549. skip_c0 = 1;
  550. skip_c1 = 1;
  551. }
  552. /*
  553. * As counter collection is not atomic,
  554. * it is possible for mperf's non-halted cycles + idle states
  555. * to exceed TSC's all cycles: show c1 = 0% in that case.
  556. */
  557. if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
  558. old->c1 = 0;
  559. else {
  560. /* normal case, derive c1 */
  561. old->c1 = old->tsc - old->mperf - core_delta->c3
  562. - core_delta->c6 - core_delta->c7;
  563. }
  564. if (old->mperf == 0) {
  565. if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
  566. old->mperf = 1; /* divide by 0 protection */
  567. }
  568. old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
  569. old->extra_delta32 &= 0xFFFFFFFF;
  570. old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
  571. /*
  572. * Extra MSR is just a snapshot, simply copy latest w/o subtracting
  573. */
  574. old->extra_msr32 = new->extra_msr32;
  575. old->extra_msr64 = new->extra_msr64;
  576. if (do_smi)
  577. old->smi_count = new->smi_count - old->smi_count;
  578. }
  579. int delta_cpu(struct thread_data *t, struct core_data *c,
  580. struct pkg_data *p, struct thread_data *t2,
  581. struct core_data *c2, struct pkg_data *p2)
  582. {
  583. /* calculate core delta only for 1st thread in core */
  584. if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
  585. delta_core(c, c2);
  586. /* always calculate thread delta */
  587. delta_thread(t, t2, c2); /* c2 is core delta */
  588. /* calculate package delta only for 1st core in package */
  589. if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
  590. delta_package(p, p2);
  591. return 0;
  592. }
  593. void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  594. {
  595. t->tsc = 0;
  596. t->aperf = 0;
  597. t->mperf = 0;
  598. t->c1 = 0;
  599. t->smi_count = 0;
  600. t->extra_delta32 = 0;
  601. t->extra_delta64 = 0;
  602. /* tells format_counters to dump all fields from this set */
  603. t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
  604. c->c3 = 0;
  605. c->c6 = 0;
  606. c->c7 = 0;
  607. c->core_temp_c = 0;
  608. p->pc2 = 0;
  609. p->pc3 = 0;
  610. p->pc6 = 0;
  611. p->pc7 = 0;
  612. p->energy_pkg = 0;
  613. p->energy_dram = 0;
  614. p->energy_cores = 0;
  615. p->energy_gfx = 0;
  616. p->rapl_pkg_perf_status = 0;
  617. p->rapl_dram_perf_status = 0;
  618. p->pkg_temp_c = 0;
  619. }
  620. int sum_counters(struct thread_data *t, struct core_data *c,
  621. struct pkg_data *p)
  622. {
  623. average.threads.tsc += t->tsc;
  624. average.threads.aperf += t->aperf;
  625. average.threads.mperf += t->mperf;
  626. average.threads.c1 += t->c1;
  627. average.threads.extra_delta32 += t->extra_delta32;
  628. average.threads.extra_delta64 += t->extra_delta64;
  629. /* sum per-core values only for 1st thread in core */
  630. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  631. return 0;
  632. average.cores.c3 += c->c3;
  633. average.cores.c6 += c->c6;
  634. average.cores.c7 += c->c7;
  635. average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
  636. /* sum per-pkg values only for 1st core in pkg */
  637. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  638. return 0;
  639. average.packages.pc2 += p->pc2;
  640. average.packages.pc3 += p->pc3;
  641. average.packages.pc6 += p->pc6;
  642. average.packages.pc7 += p->pc7;
  643. average.packages.energy_pkg += p->energy_pkg;
  644. average.packages.energy_dram += p->energy_dram;
  645. average.packages.energy_cores += p->energy_cores;
  646. average.packages.energy_gfx += p->energy_gfx;
  647. average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
  648. average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
  649. average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
  650. return 0;
  651. }
  652. /*
  653. * sum the counters for all cpus in the system
  654. * compute the weighted average
  655. */
  656. void compute_average(struct thread_data *t, struct core_data *c,
  657. struct pkg_data *p)
  658. {
  659. clear_counters(&average.threads, &average.cores, &average.packages);
  660. for_all_cpus(sum_counters, t, c, p);
  661. average.threads.tsc /= topo.num_cpus;
  662. average.threads.aperf /= topo.num_cpus;
  663. average.threads.mperf /= topo.num_cpus;
  664. average.threads.c1 /= topo.num_cpus;
  665. average.threads.extra_delta32 /= topo.num_cpus;
  666. average.threads.extra_delta32 &= 0xFFFFFFFF;
  667. average.threads.extra_delta64 /= topo.num_cpus;
  668. average.cores.c3 /= topo.num_cores;
  669. average.cores.c6 /= topo.num_cores;
  670. average.cores.c7 /= topo.num_cores;
  671. average.packages.pc2 /= topo.num_packages;
  672. average.packages.pc3 /= topo.num_packages;
  673. average.packages.pc6 /= topo.num_packages;
  674. average.packages.pc7 /= topo.num_packages;
  675. }
  676. static unsigned long long rdtsc(void)
  677. {
  678. unsigned int low, high;
  679. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  680. return low | ((unsigned long long)high) << 32;
  681. }
  682. /*
  683. * get_counters(...)
  684. * migrate to cpu
  685. * acquire and record local counters for that cpu
  686. */
  687. int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  688. {
  689. int cpu = t->cpu_id;
  690. unsigned long long msr;
  691. if (cpu_migrate(cpu)) {
  692. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  693. return -1;
  694. }
  695. t->tsc = rdtsc(); /* we are running on local CPU of interest */
  696. if (has_aperf) {
  697. if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
  698. return -3;
  699. if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
  700. return -4;
  701. }
  702. if (do_smi) {
  703. if (get_msr(cpu, MSR_SMI_COUNT, &msr))
  704. return -5;
  705. t->smi_count = msr & 0xFFFFFFFF;
  706. }
  707. if (extra_delta_offset32) {
  708. if (get_msr(cpu, extra_delta_offset32, &msr))
  709. return -5;
  710. t->extra_delta32 = msr & 0xFFFFFFFF;
  711. }
  712. if (extra_delta_offset64)
  713. if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
  714. return -5;
  715. if (extra_msr_offset32) {
  716. if (get_msr(cpu, extra_msr_offset32, &msr))
  717. return -5;
  718. t->extra_msr32 = msr & 0xFFFFFFFF;
  719. }
  720. if (extra_msr_offset64)
  721. if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
  722. return -5;
  723. /* collect core counters only for 1st thread in core */
  724. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  725. return 0;
  726. if (do_nhm_cstates) {
  727. if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
  728. return -6;
  729. if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
  730. return -7;
  731. }
  732. if (do_snb_cstates)
  733. if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
  734. return -8;
  735. if (do_dts) {
  736. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  737. return -9;
  738. c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  739. }
  740. /* collect package counters only for 1st core in package */
  741. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  742. return 0;
  743. if (do_nhm_cstates) {
  744. if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
  745. return -9;
  746. if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
  747. return -10;
  748. }
  749. if (do_snb_cstates) {
  750. if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
  751. return -11;
  752. if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
  753. return -12;
  754. }
  755. if (do_rapl & RAPL_PKG) {
  756. if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
  757. return -13;
  758. p->energy_pkg = msr & 0xFFFFFFFF;
  759. }
  760. if (do_rapl & RAPL_CORES) {
  761. if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
  762. return -14;
  763. p->energy_cores = msr & 0xFFFFFFFF;
  764. }
  765. if (do_rapl & RAPL_DRAM) {
  766. if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
  767. return -15;
  768. p->energy_dram = msr & 0xFFFFFFFF;
  769. }
  770. if (do_rapl & RAPL_GFX) {
  771. if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
  772. return -16;
  773. p->energy_gfx = msr & 0xFFFFFFFF;
  774. }
  775. if (do_rapl & RAPL_PKG_PERF_STATUS) {
  776. if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
  777. return -16;
  778. p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
  779. }
  780. if (do_rapl & RAPL_DRAM_PERF_STATUS) {
  781. if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
  782. return -16;
  783. p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
  784. }
  785. if (do_ptm) {
  786. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  787. return -17;
  788. p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  789. }
  790. return 0;
  791. }
  792. void print_verbose_header(void)
  793. {
  794. unsigned long long msr;
  795. unsigned int ratio;
  796. if (!do_nehalem_platform_info)
  797. return;
  798. get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
  799. fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
  800. ratio = (msr >> 40) & 0xFF;
  801. fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
  802. ratio, bclk, ratio * bclk);
  803. ratio = (msr >> 8) & 0xFF;
  804. fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
  805. ratio, bclk, ratio * bclk);
  806. get_msr(0, MSR_IA32_POWER_CTL, &msr);
  807. fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n",
  808. msr, msr & 0x2 ? "EN" : "DIS");
  809. if (!do_ivt_turbo_ratio_limit)
  810. goto print_nhm_turbo_ratio_limits;
  811. get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
  812. fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
  813. ratio = (msr >> 56) & 0xFF;
  814. if (ratio)
  815. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
  816. ratio, bclk, ratio * bclk);
  817. ratio = (msr >> 48) & 0xFF;
  818. if (ratio)
  819. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
  820. ratio, bclk, ratio * bclk);
  821. ratio = (msr >> 40) & 0xFF;
  822. if (ratio)
  823. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
  824. ratio, bclk, ratio * bclk);
  825. ratio = (msr >> 32) & 0xFF;
  826. if (ratio)
  827. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
  828. ratio, bclk, ratio * bclk);
  829. ratio = (msr >> 24) & 0xFF;
  830. if (ratio)
  831. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
  832. ratio, bclk, ratio * bclk);
  833. ratio = (msr >> 16) & 0xFF;
  834. if (ratio)
  835. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
  836. ratio, bclk, ratio * bclk);
  837. ratio = (msr >> 8) & 0xFF;
  838. if (ratio)
  839. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
  840. ratio, bclk, ratio * bclk);
  841. ratio = (msr >> 0) & 0xFF;
  842. if (ratio)
  843. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
  844. ratio, bclk, ratio * bclk);
  845. print_nhm_turbo_ratio_limits:
  846. get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
  847. #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
  848. #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
  849. fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
  850. fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
  851. (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
  852. (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
  853. (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
  854. (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
  855. (msr & (1 << 15)) ? "" : "UN",
  856. (unsigned int)msr & 7);
  857. switch(msr & 0x7) {
  858. case 0:
  859. fprintf(stderr, "pc0");
  860. break;
  861. case 1:
  862. fprintf(stderr, do_snb_cstates ? "pc2" : "pc0");
  863. break;
  864. case 2:
  865. fprintf(stderr, do_snb_cstates ? "pc6-noret" : "pc3");
  866. break;
  867. case 3:
  868. fprintf(stderr, "pc6");
  869. break;
  870. case 4:
  871. fprintf(stderr, "pc7");
  872. break;
  873. case 5:
  874. fprintf(stderr, do_snb_cstates ? "pc7s" : "invalid");
  875. break;
  876. case 7:
  877. fprintf(stderr, "unlimited");
  878. break;
  879. default:
  880. fprintf(stderr, "invalid");
  881. }
  882. fprintf(stderr, ")\n");
  883. if (!do_nehalem_turbo_ratio_limit)
  884. return;
  885. get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
  886. fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
  887. ratio = (msr >> 56) & 0xFF;
  888. if (ratio)
  889. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
  890. ratio, bclk, ratio * bclk);
  891. ratio = (msr >> 48) & 0xFF;
  892. if (ratio)
  893. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
  894. ratio, bclk, ratio * bclk);
  895. ratio = (msr >> 40) & 0xFF;
  896. if (ratio)
  897. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
  898. ratio, bclk, ratio * bclk);
  899. ratio = (msr >> 32) & 0xFF;
  900. if (ratio)
  901. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
  902. ratio, bclk, ratio * bclk);
  903. ratio = (msr >> 24) & 0xFF;
  904. if (ratio)
  905. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
  906. ratio, bclk, ratio * bclk);
  907. ratio = (msr >> 16) & 0xFF;
  908. if (ratio)
  909. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
  910. ratio, bclk, ratio * bclk);
  911. ratio = (msr >> 8) & 0xFF;
  912. if (ratio)
  913. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
  914. ratio, bclk, ratio * bclk);
  915. ratio = (msr >> 0) & 0xFF;
  916. if (ratio)
  917. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
  918. ratio, bclk, ratio * bclk);
  919. }
  920. void free_all_buffers(void)
  921. {
  922. CPU_FREE(cpu_present_set);
  923. cpu_present_set = NULL;
  924. cpu_present_set = 0;
  925. CPU_FREE(cpu_affinity_set);
  926. cpu_affinity_set = NULL;
  927. cpu_affinity_setsize = 0;
  928. free(thread_even);
  929. free(core_even);
  930. free(package_even);
  931. thread_even = NULL;
  932. core_even = NULL;
  933. package_even = NULL;
  934. free(thread_odd);
  935. free(core_odd);
  936. free(package_odd);
  937. thread_odd = NULL;
  938. core_odd = NULL;
  939. package_odd = NULL;
  940. free(output_buffer);
  941. output_buffer = NULL;
  942. outp = NULL;
  943. }
  944. /*
  945. * cpu_is_first_sibling_in_core(cpu)
  946. * return 1 if given CPU is 1st HT sibling in the core
  947. */
  948. int cpu_is_first_sibling_in_core(int cpu)
  949. {
  950. char path[64];
  951. FILE *filep;
  952. int first_cpu;
  953. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  954. filep = fopen(path, "r");
  955. if (filep == NULL) {
  956. perror(path);
  957. exit(1);
  958. }
  959. fscanf(filep, "%d", &first_cpu);
  960. fclose(filep);
  961. return (cpu == first_cpu);
  962. }
  963. /*
  964. * cpu_is_first_core_in_package(cpu)
  965. * return 1 if given CPU is 1st core in package
  966. */
  967. int cpu_is_first_core_in_package(int cpu)
  968. {
  969. char path[64];
  970. FILE *filep;
  971. int first_cpu;
  972. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
  973. filep = fopen(path, "r");
  974. if (filep == NULL) {
  975. perror(path);
  976. exit(1);
  977. }
  978. fscanf(filep, "%d", &first_cpu);
  979. fclose(filep);
  980. return (cpu == first_cpu);
  981. }
  982. int get_physical_package_id(int cpu)
  983. {
  984. char path[80];
  985. FILE *filep;
  986. int pkg;
  987. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  988. filep = fopen(path, "r");
  989. if (filep == NULL) {
  990. perror(path);
  991. exit(1);
  992. }
  993. fscanf(filep, "%d", &pkg);
  994. fclose(filep);
  995. return pkg;
  996. }
  997. int get_core_id(int cpu)
  998. {
  999. char path[80];
  1000. FILE *filep;
  1001. int core;
  1002. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
  1003. filep = fopen(path, "r");
  1004. if (filep == NULL) {
  1005. perror(path);
  1006. exit(1);
  1007. }
  1008. fscanf(filep, "%d", &core);
  1009. fclose(filep);
  1010. return core;
  1011. }
  1012. int get_num_ht_siblings(int cpu)
  1013. {
  1014. char path[80];
  1015. FILE *filep;
  1016. int sib1, sib2;
  1017. int matches;
  1018. char character;
  1019. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  1020. filep = fopen(path, "r");
  1021. if (filep == NULL) {
  1022. perror(path);
  1023. exit(1);
  1024. }
  1025. /*
  1026. * file format:
  1027. * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
  1028. * otherwinse 1 sibling (self).
  1029. */
  1030. matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
  1031. fclose(filep);
  1032. if (matches == 3)
  1033. return 2;
  1034. else
  1035. return 1;
  1036. }
  1037. /*
  1038. * run func(thread, core, package) in topology order
  1039. * skip non-present cpus
  1040. */
  1041. int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
  1042. struct pkg_data *, struct thread_data *, struct core_data *,
  1043. struct pkg_data *), struct thread_data *thread_base,
  1044. struct core_data *core_base, struct pkg_data *pkg_base,
  1045. struct thread_data *thread_base2, struct core_data *core_base2,
  1046. struct pkg_data *pkg_base2)
  1047. {
  1048. int retval, pkg_no, core_no, thread_no;
  1049. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  1050. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  1051. for (thread_no = 0; thread_no <
  1052. topo.num_threads_per_core; ++thread_no) {
  1053. struct thread_data *t, *t2;
  1054. struct core_data *c, *c2;
  1055. struct pkg_data *p, *p2;
  1056. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  1057. if (cpu_is_not_present(t->cpu_id))
  1058. continue;
  1059. t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
  1060. c = GET_CORE(core_base, core_no, pkg_no);
  1061. c2 = GET_CORE(core_base2, core_no, pkg_no);
  1062. p = GET_PKG(pkg_base, pkg_no);
  1063. p2 = GET_PKG(pkg_base2, pkg_no);
  1064. retval = func(t, c, p, t2, c2, p2);
  1065. if (retval)
  1066. return retval;
  1067. }
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. /*
  1073. * run func(cpu) on every cpu in /proc/stat
  1074. * return max_cpu number
  1075. */
  1076. int for_all_proc_cpus(int (func)(int))
  1077. {
  1078. FILE *fp;
  1079. int cpu_num;
  1080. int retval;
  1081. fp = fopen(proc_stat, "r");
  1082. if (fp == NULL) {
  1083. perror(proc_stat);
  1084. exit(1);
  1085. }
  1086. retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  1087. if (retval != 0) {
  1088. perror("/proc/stat format");
  1089. exit(1);
  1090. }
  1091. while (1) {
  1092. retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
  1093. if (retval != 1)
  1094. break;
  1095. retval = func(cpu_num);
  1096. if (retval) {
  1097. fclose(fp);
  1098. return(retval);
  1099. }
  1100. }
  1101. fclose(fp);
  1102. return 0;
  1103. }
  1104. void re_initialize(void)
  1105. {
  1106. free_all_buffers();
  1107. setup_all_buffers();
  1108. printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
  1109. }
  1110. /*
  1111. * count_cpus()
  1112. * remember the last one seen, it will be the max
  1113. */
  1114. int count_cpus(int cpu)
  1115. {
  1116. if (topo.max_cpu_num < cpu)
  1117. topo.max_cpu_num = cpu;
  1118. topo.num_cpus += 1;
  1119. return 0;
  1120. }
  1121. int mark_cpu_present(int cpu)
  1122. {
  1123. CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
  1124. return 0;
  1125. }
  1126. void turbostat_loop()
  1127. {
  1128. int retval;
  1129. int restarted = 0;
  1130. restart:
  1131. restarted++;
  1132. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  1133. if (retval < -1) {
  1134. exit(retval);
  1135. } else if (retval == -1) {
  1136. if (restarted > 1) {
  1137. exit(retval);
  1138. }
  1139. re_initialize();
  1140. goto restart;
  1141. }
  1142. restarted = 0;
  1143. gettimeofday(&tv_even, (struct timezone *)NULL);
  1144. while (1) {
  1145. if (for_all_proc_cpus(cpu_is_not_present)) {
  1146. re_initialize();
  1147. goto restart;
  1148. }
  1149. sleep(interval_sec);
  1150. retval = for_all_cpus(get_counters, ODD_COUNTERS);
  1151. if (retval < -1) {
  1152. exit(retval);
  1153. } else if (retval == -1) {
  1154. re_initialize();
  1155. goto restart;
  1156. }
  1157. gettimeofday(&tv_odd, (struct timezone *)NULL);
  1158. timersub(&tv_odd, &tv_even, &tv_delta);
  1159. for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
  1160. compute_average(EVEN_COUNTERS);
  1161. format_all_counters(EVEN_COUNTERS);
  1162. flush_stdout();
  1163. sleep(interval_sec);
  1164. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  1165. if (retval < -1) {
  1166. exit(retval);
  1167. } else if (retval == -1) {
  1168. re_initialize();
  1169. goto restart;
  1170. }
  1171. gettimeofday(&tv_even, (struct timezone *)NULL);
  1172. timersub(&tv_even, &tv_odd, &tv_delta);
  1173. for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
  1174. compute_average(ODD_COUNTERS);
  1175. format_all_counters(ODD_COUNTERS);
  1176. flush_stdout();
  1177. }
  1178. }
  1179. void check_dev_msr()
  1180. {
  1181. struct stat sb;
  1182. if (stat("/dev/cpu/0/msr", &sb)) {
  1183. fprintf(stderr, "no /dev/cpu/0/msr\n");
  1184. fprintf(stderr, "Try \"# modprobe msr\"\n");
  1185. exit(-5);
  1186. }
  1187. }
  1188. void check_super_user()
  1189. {
  1190. if (getuid() != 0) {
  1191. fprintf(stderr, "must be root\n");
  1192. exit(-6);
  1193. }
  1194. }
  1195. int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
  1196. {
  1197. if (!genuine_intel)
  1198. return 0;
  1199. if (family != 6)
  1200. return 0;
  1201. switch (model) {
  1202. case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
  1203. case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
  1204. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  1205. case 0x25: /* Westmere Client - Clarkdale, Arrandale */
  1206. case 0x2C: /* Westmere EP - Gulftown */
  1207. case 0x2A: /* SNB */
  1208. case 0x2D: /* SNB Xeon */
  1209. case 0x3A: /* IVB */
  1210. case 0x3E: /* IVB Xeon */
  1211. case 0x3C: /* HSW */
  1212. case 0x3F: /* HSW */
  1213. case 0x45: /* HSW */
  1214. return 1;
  1215. case 0x2E: /* Nehalem-EX Xeon - Beckton */
  1216. case 0x2F: /* Westmere-EX Xeon - Eagleton */
  1217. default:
  1218. return 0;
  1219. }
  1220. }
  1221. int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
  1222. {
  1223. if (!genuine_intel)
  1224. return 0;
  1225. if (family != 6)
  1226. return 0;
  1227. switch (model) {
  1228. case 0x3E: /* IVB Xeon */
  1229. return 1;
  1230. default:
  1231. return 0;
  1232. }
  1233. }
  1234. /*
  1235. * print_epb()
  1236. * Decode the ENERGY_PERF_BIAS MSR
  1237. */
  1238. int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1239. {
  1240. unsigned long long msr;
  1241. char *epb_string;
  1242. int cpu;
  1243. if (!has_epb)
  1244. return 0;
  1245. cpu = t->cpu_id;
  1246. /* EPB is per-package */
  1247. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1248. return 0;
  1249. if (cpu_migrate(cpu)) {
  1250. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1251. return -1;
  1252. }
  1253. if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
  1254. return 0;
  1255. switch (msr & 0x7) {
  1256. case ENERGY_PERF_BIAS_PERFORMANCE:
  1257. epb_string = "performance";
  1258. break;
  1259. case ENERGY_PERF_BIAS_NORMAL:
  1260. epb_string = "balanced";
  1261. break;
  1262. case ENERGY_PERF_BIAS_POWERSAVE:
  1263. epb_string = "powersave";
  1264. break;
  1265. default:
  1266. epb_string = "custom";
  1267. break;
  1268. }
  1269. fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
  1270. return 0;
  1271. }
  1272. #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
  1273. #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
  1274. /*
  1275. * rapl_probe()
  1276. *
  1277. * sets do_rapl
  1278. */
  1279. void rapl_probe(unsigned int family, unsigned int model)
  1280. {
  1281. unsigned long long msr;
  1282. double tdp;
  1283. if (!genuine_intel)
  1284. return;
  1285. if (family != 6)
  1286. return;
  1287. switch (model) {
  1288. case 0x2A:
  1289. case 0x3A:
  1290. case 0x3C: /* HSW */
  1291. case 0x3F: /* HSW */
  1292. case 0x45: /* HSW */
  1293. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
  1294. break;
  1295. case 0x2D:
  1296. case 0x3E:
  1297. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS;
  1298. break;
  1299. default:
  1300. return;
  1301. }
  1302. /* units on package 0, verify later other packages match */
  1303. if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
  1304. return;
  1305. rapl_power_units = 1.0 / (1 << (msr & 0xF));
  1306. rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
  1307. rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
  1308. /* get TDP to determine energy counter range */
  1309. if (get_msr(0, MSR_PKG_POWER_INFO, &msr))
  1310. return;
  1311. tdp = ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
  1312. rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
  1313. if (verbose)
  1314. fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range);
  1315. return;
  1316. }
  1317. int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1318. {
  1319. unsigned long long msr;
  1320. unsigned int dts;
  1321. int cpu;
  1322. if (!(do_dts || do_ptm))
  1323. return 0;
  1324. cpu = t->cpu_id;
  1325. /* DTS is per-core, no need to print for each thread */
  1326. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  1327. return 0;
  1328. if (cpu_migrate(cpu)) {
  1329. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1330. return -1;
  1331. }
  1332. if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
  1333. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  1334. return 0;
  1335. dts = (msr >> 16) & 0x7F;
  1336. fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
  1337. cpu, msr, tcc_activation_temp - dts);
  1338. #ifdef THERM_DEBUG
  1339. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
  1340. return 0;
  1341. dts = (msr >> 16) & 0x7F;
  1342. dts2 = (msr >> 8) & 0x7F;
  1343. fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  1344. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  1345. #endif
  1346. }
  1347. if (do_dts) {
  1348. unsigned int resolution;
  1349. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  1350. return 0;
  1351. dts = (msr >> 16) & 0x7F;
  1352. resolution = (msr >> 27) & 0xF;
  1353. fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
  1354. cpu, msr, tcc_activation_temp - dts, resolution);
  1355. #ifdef THERM_DEBUG
  1356. if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
  1357. return 0;
  1358. dts = (msr >> 16) & 0x7F;
  1359. dts2 = (msr >> 8) & 0x7F;
  1360. fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  1361. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  1362. #endif
  1363. }
  1364. return 0;
  1365. }
  1366. void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
  1367. {
  1368. fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
  1369. cpu, label,
  1370. ((msr >> 15) & 1) ? "EN" : "DIS",
  1371. ((msr >> 0) & 0x7FFF) * rapl_power_units,
  1372. (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
  1373. (((msr >> 16) & 1) ? "EN" : "DIS"));
  1374. return;
  1375. }
  1376. int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1377. {
  1378. unsigned long long msr;
  1379. int cpu;
  1380. double local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units;
  1381. if (!do_rapl)
  1382. return 0;
  1383. /* RAPL counters are per package, so print only for 1st thread/package */
  1384. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1385. return 0;
  1386. cpu = t->cpu_id;
  1387. if (cpu_migrate(cpu)) {
  1388. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1389. return -1;
  1390. }
  1391. if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
  1392. return -1;
  1393. local_rapl_power_units = 1.0 / (1 << (msr & 0xF));
  1394. local_rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
  1395. local_rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
  1396. if (local_rapl_power_units != rapl_power_units)
  1397. fprintf(stderr, "cpu%d, ERROR: Power units mis-match\n", cpu);
  1398. if (local_rapl_energy_units != rapl_energy_units)
  1399. fprintf(stderr, "cpu%d, ERROR: Energy units mis-match\n", cpu);
  1400. if (local_rapl_time_units != rapl_time_units)
  1401. fprintf(stderr, "cpu%d, ERROR: Time units mis-match\n", cpu);
  1402. if (verbose) {
  1403. fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
  1404. "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
  1405. local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units);
  1406. }
  1407. if (do_rapl & RAPL_PKG) {
  1408. if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
  1409. return -5;
  1410. fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  1411. cpu, msr,
  1412. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1413. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1414. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1415. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  1416. if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
  1417. return -9;
  1418. fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1419. cpu, msr, (msr >> 63) & 1 ? "": "UN");
  1420. print_power_limit_msr(cpu, msr, "PKG Limit #1");
  1421. fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
  1422. cpu,
  1423. ((msr >> 47) & 1) ? "EN" : "DIS",
  1424. ((msr >> 32) & 0x7FFF) * rapl_power_units,
  1425. (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
  1426. ((msr >> 48) & 1) ? "EN" : "DIS");
  1427. }
  1428. if (do_rapl & RAPL_DRAM) {
  1429. if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
  1430. return -6;
  1431. fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  1432. cpu, msr,
  1433. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1434. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1435. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1436. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  1437. if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
  1438. return -9;
  1439. fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1440. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  1441. print_power_limit_msr(cpu, msr, "DRAM Limit");
  1442. }
  1443. if (do_rapl & RAPL_CORES) {
  1444. if (verbose) {
  1445. if (get_msr(cpu, MSR_PP0_POLICY, &msr))
  1446. return -7;
  1447. fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
  1448. if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
  1449. return -9;
  1450. fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1451. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  1452. print_power_limit_msr(cpu, msr, "Cores Limit");
  1453. }
  1454. }
  1455. if (do_rapl & RAPL_GFX) {
  1456. if (verbose) {
  1457. if (get_msr(cpu, MSR_PP1_POLICY, &msr))
  1458. return -8;
  1459. fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
  1460. if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
  1461. return -9;
  1462. fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1463. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  1464. print_power_limit_msr(cpu, msr, "GFX Limit");
  1465. }
  1466. }
  1467. return 0;
  1468. }
  1469. int is_snb(unsigned int family, unsigned int model)
  1470. {
  1471. if (!genuine_intel)
  1472. return 0;
  1473. switch (model) {
  1474. case 0x2A:
  1475. case 0x2D:
  1476. case 0x3A: /* IVB */
  1477. case 0x3E: /* IVB Xeon */
  1478. case 0x3C: /* HSW */
  1479. case 0x3F: /* HSW */
  1480. case 0x45: /* HSW */
  1481. return 1;
  1482. }
  1483. return 0;
  1484. }
  1485. double discover_bclk(unsigned int family, unsigned int model)
  1486. {
  1487. if (is_snb(family, model))
  1488. return 100.00;
  1489. else
  1490. return 133.33;
  1491. }
  1492. /*
  1493. * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
  1494. * the Thermal Control Circuit (TCC) activates.
  1495. * This is usually equal to tjMax.
  1496. *
  1497. * Older processors do not have this MSR, so there we guess,
  1498. * but also allow cmdline over-ride with -T.
  1499. *
  1500. * Several MSR temperature values are in units of degrees-C
  1501. * below this value, including the Digital Thermal Sensor (DTS),
  1502. * Package Thermal Management Sensor (PTM), and thermal event thresholds.
  1503. */
  1504. int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1505. {
  1506. unsigned long long msr;
  1507. unsigned int target_c_local;
  1508. int cpu;
  1509. /* tcc_activation_temp is used only for dts or ptm */
  1510. if (!(do_dts || do_ptm))
  1511. return 0;
  1512. /* this is a per-package concept */
  1513. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1514. return 0;
  1515. cpu = t->cpu_id;
  1516. if (cpu_migrate(cpu)) {
  1517. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1518. return -1;
  1519. }
  1520. if (tcc_activation_temp_override != 0) {
  1521. tcc_activation_temp = tcc_activation_temp_override;
  1522. fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
  1523. cpu, tcc_activation_temp);
  1524. return 0;
  1525. }
  1526. /* Temperature Target MSR is Nehalem and newer only */
  1527. if (!do_nehalem_platform_info)
  1528. goto guess;
  1529. if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
  1530. goto guess;
  1531. target_c_local = (msr >> 16) & 0x7F;
  1532. if (verbose)
  1533. fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
  1534. cpu, msr, target_c_local);
  1535. if (target_c_local < 85 || target_c_local > 120)
  1536. goto guess;
  1537. tcc_activation_temp = target_c_local;
  1538. return 0;
  1539. guess:
  1540. tcc_activation_temp = TJMAX_DEFAULT;
  1541. fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
  1542. cpu, tcc_activation_temp);
  1543. return 0;
  1544. }
  1545. void check_cpuid()
  1546. {
  1547. unsigned int eax, ebx, ecx, edx, max_level;
  1548. unsigned int fms, family, model, stepping;
  1549. eax = ebx = ecx = edx = 0;
  1550. asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
  1551. if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
  1552. genuine_intel = 1;
  1553. if (verbose)
  1554. fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
  1555. (char *)&ebx, (char *)&edx, (char *)&ecx);
  1556. asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
  1557. family = (fms >> 8) & 0xf;
  1558. model = (fms >> 4) & 0xf;
  1559. stepping = fms & 0xf;
  1560. if (family == 6 || family == 0xf)
  1561. model += ((fms >> 16) & 0xf) << 4;
  1562. if (verbose)
  1563. fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
  1564. max_level, family, model, stepping, family, model, stepping);
  1565. if (!(edx & (1 << 5))) {
  1566. fprintf(stderr, "CPUID: no MSR\n");
  1567. exit(1);
  1568. }
  1569. /*
  1570. * check max extended function levels of CPUID.
  1571. * This is needed to check for invariant TSC.
  1572. * This check is valid for both Intel and AMD.
  1573. */
  1574. ebx = ecx = edx = 0;
  1575. asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
  1576. if (max_level < 0x80000007) {
  1577. fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
  1578. exit(1);
  1579. }
  1580. /*
  1581. * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
  1582. * this check is valid for both Intel and AMD
  1583. */
  1584. asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
  1585. has_invariant_tsc = edx & (1 << 8);
  1586. if (!has_invariant_tsc) {
  1587. fprintf(stderr, "No invariant TSC\n");
  1588. exit(1);
  1589. }
  1590. /*
  1591. * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
  1592. * this check is valid for both Intel and AMD
  1593. */
  1594. asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
  1595. has_aperf = ecx & (1 << 0);
  1596. do_dts = eax & (1 << 0);
  1597. do_ptm = eax & (1 << 6);
  1598. has_epb = ecx & (1 << 3);
  1599. if (verbose)
  1600. fprintf(stderr, "CPUID(6): %s%s%s%s\n",
  1601. has_aperf ? "APERF" : "No APERF!",
  1602. do_dts ? ", DTS" : "",
  1603. do_ptm ? ", PTM": "",
  1604. has_epb ? ", EPB": "");
  1605. if (!has_aperf)
  1606. exit(-1);
  1607. do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
  1608. do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
  1609. do_smi = do_nhm_cstates;
  1610. do_snb_cstates = is_snb(family, model);
  1611. bclk = discover_bclk(family, model);
  1612. do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
  1613. do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
  1614. rapl_probe(family, model);
  1615. return;
  1616. }
  1617. void usage()
  1618. {
  1619. fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
  1620. progname);
  1621. exit(1);
  1622. }
  1623. /*
  1624. * in /dev/cpu/ return success for names that are numbers
  1625. * ie. filter out ".", "..", "microcode".
  1626. */
  1627. int dir_filter(const struct dirent *dirp)
  1628. {
  1629. if (isdigit(dirp->d_name[0]))
  1630. return 1;
  1631. else
  1632. return 0;
  1633. }
  1634. int open_dev_cpu_msr(int dummy1)
  1635. {
  1636. return 0;
  1637. }
  1638. void topology_probe()
  1639. {
  1640. int i;
  1641. int max_core_id = 0;
  1642. int max_package_id = 0;
  1643. int max_siblings = 0;
  1644. struct cpu_topology {
  1645. int core_id;
  1646. int physical_package_id;
  1647. } *cpus;
  1648. /* Initialize num_cpus, max_cpu_num */
  1649. topo.num_cpus = 0;
  1650. topo.max_cpu_num = 0;
  1651. for_all_proc_cpus(count_cpus);
  1652. if (!summary_only && topo.num_cpus > 1)
  1653. show_cpu = 1;
  1654. if (verbose > 1)
  1655. fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
  1656. cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
  1657. if (cpus == NULL) {
  1658. perror("calloc cpus");
  1659. exit(1);
  1660. }
  1661. /*
  1662. * Allocate and initialize cpu_present_set
  1663. */
  1664. cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
  1665. if (cpu_present_set == NULL) {
  1666. perror("CPU_ALLOC");
  1667. exit(3);
  1668. }
  1669. cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  1670. CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
  1671. for_all_proc_cpus(mark_cpu_present);
  1672. /*
  1673. * Allocate and initialize cpu_affinity_set
  1674. */
  1675. cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
  1676. if (cpu_affinity_set == NULL) {
  1677. perror("CPU_ALLOC");
  1678. exit(3);
  1679. }
  1680. cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  1681. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  1682. /*
  1683. * For online cpus
  1684. * find max_core_id, max_package_id
  1685. */
  1686. for (i = 0; i <= topo.max_cpu_num; ++i) {
  1687. int siblings;
  1688. if (cpu_is_not_present(i)) {
  1689. if (verbose > 1)
  1690. fprintf(stderr, "cpu%d NOT PRESENT\n", i);
  1691. continue;
  1692. }
  1693. cpus[i].core_id = get_core_id(i);
  1694. if (cpus[i].core_id > max_core_id)
  1695. max_core_id = cpus[i].core_id;
  1696. cpus[i].physical_package_id = get_physical_package_id(i);
  1697. if (cpus[i].physical_package_id > max_package_id)
  1698. max_package_id = cpus[i].physical_package_id;
  1699. siblings = get_num_ht_siblings(i);
  1700. if (siblings > max_siblings)
  1701. max_siblings = siblings;
  1702. if (verbose > 1)
  1703. fprintf(stderr, "cpu %d pkg %d core %d\n",
  1704. i, cpus[i].physical_package_id, cpus[i].core_id);
  1705. }
  1706. topo.num_cores_per_pkg = max_core_id + 1;
  1707. if (verbose > 1)
  1708. fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
  1709. max_core_id, topo.num_cores_per_pkg);
  1710. if (!summary_only && topo.num_cores_per_pkg > 1)
  1711. show_core = 1;
  1712. topo.num_packages = max_package_id + 1;
  1713. if (verbose > 1)
  1714. fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
  1715. max_package_id, topo.num_packages);
  1716. if (!summary_only && topo.num_packages > 1)
  1717. show_pkg = 1;
  1718. topo.num_threads_per_core = max_siblings;
  1719. if (verbose > 1)
  1720. fprintf(stderr, "max_siblings %d\n", max_siblings);
  1721. free(cpus);
  1722. }
  1723. void
  1724. allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
  1725. {
  1726. int i;
  1727. *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
  1728. topo.num_packages, sizeof(struct thread_data));
  1729. if (*t == NULL)
  1730. goto error;
  1731. for (i = 0; i < topo.num_threads_per_core *
  1732. topo.num_cores_per_pkg * topo.num_packages; i++)
  1733. (*t)[i].cpu_id = -1;
  1734. *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
  1735. sizeof(struct core_data));
  1736. if (*c == NULL)
  1737. goto error;
  1738. for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
  1739. (*c)[i].core_id = -1;
  1740. *p = calloc(topo.num_packages, sizeof(struct pkg_data));
  1741. if (*p == NULL)
  1742. goto error;
  1743. for (i = 0; i < topo.num_packages; i++)
  1744. (*p)[i].package_id = i;
  1745. return;
  1746. error:
  1747. perror("calloc counters");
  1748. exit(1);
  1749. }
  1750. /*
  1751. * init_counter()
  1752. *
  1753. * set cpu_id, core_num, pkg_num
  1754. * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
  1755. *
  1756. * increment topo.num_cores when 1st core in pkg seen
  1757. */
  1758. void init_counter(struct thread_data *thread_base, struct core_data *core_base,
  1759. struct pkg_data *pkg_base, int thread_num, int core_num,
  1760. int pkg_num, int cpu_id)
  1761. {
  1762. struct thread_data *t;
  1763. struct core_data *c;
  1764. struct pkg_data *p;
  1765. t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
  1766. c = GET_CORE(core_base, core_num, pkg_num);
  1767. p = GET_PKG(pkg_base, pkg_num);
  1768. t->cpu_id = cpu_id;
  1769. if (thread_num == 0) {
  1770. t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
  1771. if (cpu_is_first_core_in_package(cpu_id))
  1772. t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
  1773. }
  1774. c->core_id = core_num;
  1775. p->package_id = pkg_num;
  1776. }
  1777. int initialize_counters(int cpu_id)
  1778. {
  1779. int my_thread_id, my_core_id, my_package_id;
  1780. my_package_id = get_physical_package_id(cpu_id);
  1781. my_core_id = get_core_id(cpu_id);
  1782. if (cpu_is_first_sibling_in_core(cpu_id)) {
  1783. my_thread_id = 0;
  1784. topo.num_cores++;
  1785. } else {
  1786. my_thread_id = 1;
  1787. }
  1788. init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  1789. init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  1790. return 0;
  1791. }
  1792. void allocate_output_buffer()
  1793. {
  1794. output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
  1795. outp = output_buffer;
  1796. if (outp == NULL) {
  1797. perror("calloc");
  1798. exit(-1);
  1799. }
  1800. }
  1801. void setup_all_buffers(void)
  1802. {
  1803. topology_probe();
  1804. allocate_counters(&thread_even, &core_even, &package_even);
  1805. allocate_counters(&thread_odd, &core_odd, &package_odd);
  1806. allocate_output_buffer();
  1807. for_all_proc_cpus(initialize_counters);
  1808. }
  1809. void turbostat_init()
  1810. {
  1811. check_cpuid();
  1812. check_dev_msr();
  1813. check_super_user();
  1814. setup_all_buffers();
  1815. if (verbose)
  1816. print_verbose_header();
  1817. if (verbose)
  1818. for_all_cpus(print_epb, ODD_COUNTERS);
  1819. if (verbose)
  1820. for_all_cpus(print_rapl, ODD_COUNTERS);
  1821. for_all_cpus(set_temperature_target, ODD_COUNTERS);
  1822. if (verbose)
  1823. for_all_cpus(print_thermal, ODD_COUNTERS);
  1824. }
  1825. int fork_it(char **argv)
  1826. {
  1827. pid_t child_pid;
  1828. int status;
  1829. status = for_all_cpus(get_counters, EVEN_COUNTERS);
  1830. if (status)
  1831. exit(status);
  1832. /* clear affinity side-effect of get_counters() */
  1833. sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
  1834. gettimeofday(&tv_even, (struct timezone *)NULL);
  1835. child_pid = fork();
  1836. if (!child_pid) {
  1837. /* child */
  1838. execvp(argv[0], argv);
  1839. } else {
  1840. /* parent */
  1841. if (child_pid == -1) {
  1842. perror("fork");
  1843. exit(1);
  1844. }
  1845. signal(SIGINT, SIG_IGN);
  1846. signal(SIGQUIT, SIG_IGN);
  1847. if (waitpid(child_pid, &status, 0) == -1) {
  1848. perror("wait");
  1849. exit(status);
  1850. }
  1851. }
  1852. /*
  1853. * n.b. fork_it() does not check for errors from for_all_cpus()
  1854. * because re-starting is problematic when forking
  1855. */
  1856. for_all_cpus(get_counters, ODD_COUNTERS);
  1857. gettimeofday(&tv_odd, (struct timezone *)NULL);
  1858. timersub(&tv_odd, &tv_even, &tv_delta);
  1859. for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
  1860. compute_average(EVEN_COUNTERS);
  1861. format_all_counters(EVEN_COUNTERS);
  1862. flush_stderr();
  1863. fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
  1864. return status;
  1865. }
  1866. void cmdline(int argc, char **argv)
  1867. {
  1868. int opt;
  1869. progname = argv[0];
  1870. while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
  1871. switch (opt) {
  1872. case 'p':
  1873. show_core_only++;
  1874. break;
  1875. case 'P':
  1876. show_pkg_only++;
  1877. break;
  1878. case 'S':
  1879. summary_only++;
  1880. break;
  1881. case 'v':
  1882. verbose++;
  1883. break;
  1884. case 'i':
  1885. interval_sec = atoi(optarg);
  1886. break;
  1887. case 'c':
  1888. sscanf(optarg, "%x", &extra_delta_offset32);
  1889. break;
  1890. case 'C':
  1891. sscanf(optarg, "%x", &extra_delta_offset64);
  1892. break;
  1893. case 'm':
  1894. sscanf(optarg, "%x", &extra_msr_offset32);
  1895. break;
  1896. case 'M':
  1897. sscanf(optarg, "%x", &extra_msr_offset64);
  1898. break;
  1899. case 'R':
  1900. rapl_verbose++;
  1901. break;
  1902. case 'T':
  1903. tcc_activation_temp_override = atoi(optarg);
  1904. break;
  1905. default:
  1906. usage();
  1907. }
  1908. }
  1909. }
  1910. int main(int argc, char **argv)
  1911. {
  1912. cmdline(argc, argv);
  1913. if (verbose)
  1914. fprintf(stderr, "turbostat v3.2 February 11, 2013"
  1915. " - Len Brown <lenb@kernel.org>\n");
  1916. turbostat_init();
  1917. /*
  1918. * if any params left, it must be a command to fork
  1919. */
  1920. if (argc - optind)
  1921. return fork_it(argv + optind);
  1922. else
  1923. turbostat_loop();
  1924. return 0;
  1925. }