turbostat.c 61 KB

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