turbostat.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232
  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 <getopt.h>
  36. #include <dirent.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39. #include <sched.h>
  40. #include <cpuid.h>
  41. #include <linux/capability.h>
  42. #include <errno.h>
  43. char *proc_stat = "/proc/stat";
  44. unsigned int interval_sec = 5;
  45. unsigned int debug;
  46. unsigned int rapl_joules;
  47. unsigned int summary_only;
  48. unsigned int dump_only;
  49. unsigned int skip_c0;
  50. unsigned int skip_c1;
  51. unsigned int do_nhm_cstates;
  52. unsigned int do_snb_cstates;
  53. unsigned int do_knl_cstates;
  54. unsigned int do_pc2;
  55. unsigned int do_pc3;
  56. unsigned int do_pc6;
  57. unsigned int do_pc7;
  58. unsigned int do_c8_c9_c10;
  59. unsigned int do_skl_residency;
  60. unsigned int do_slm_cstates;
  61. unsigned int use_c1_residency_msr;
  62. unsigned int has_aperf;
  63. unsigned int has_epb;
  64. unsigned int units = 1000000; /* MHz etc */
  65. unsigned int genuine_intel;
  66. unsigned int has_invariant_tsc;
  67. unsigned int do_nhm_platform_info;
  68. unsigned int extra_msr_offset32;
  69. unsigned int extra_msr_offset64;
  70. unsigned int extra_delta_offset32;
  71. unsigned int extra_delta_offset64;
  72. unsigned int aperf_mperf_multiplier = 1;
  73. int do_smi;
  74. double bclk;
  75. double base_hz;
  76. unsigned int has_base_hz;
  77. double tsc_tweak = 1.0;
  78. unsigned int show_pkg;
  79. unsigned int show_core;
  80. unsigned int show_cpu;
  81. unsigned int show_pkg_only;
  82. unsigned int show_core_only;
  83. char *output_buffer, *outp;
  84. unsigned int do_rapl;
  85. unsigned int do_dts;
  86. unsigned int do_ptm;
  87. unsigned int tcc_activation_temp;
  88. unsigned int tcc_activation_temp_override;
  89. double rapl_power_units, rapl_time_units;
  90. double rapl_dram_energy_units, rapl_energy_units;
  91. double rapl_joule_counter_range;
  92. unsigned int do_core_perf_limit_reasons;
  93. unsigned int do_gfx_perf_limit_reasons;
  94. unsigned int do_ring_perf_limit_reasons;
  95. unsigned int crystal_hz;
  96. unsigned long long tsc_hz;
  97. int base_cpu;
  98. double discover_bclk(unsigned int family, unsigned int model);
  99. #define RAPL_PKG (1 << 0)
  100. /* 0x610 MSR_PKG_POWER_LIMIT */
  101. /* 0x611 MSR_PKG_ENERGY_STATUS */
  102. #define RAPL_PKG_PERF_STATUS (1 << 1)
  103. /* 0x613 MSR_PKG_PERF_STATUS */
  104. #define RAPL_PKG_POWER_INFO (1 << 2)
  105. /* 0x614 MSR_PKG_POWER_INFO */
  106. #define RAPL_DRAM (1 << 3)
  107. /* 0x618 MSR_DRAM_POWER_LIMIT */
  108. /* 0x619 MSR_DRAM_ENERGY_STATUS */
  109. #define RAPL_DRAM_PERF_STATUS (1 << 4)
  110. /* 0x61b MSR_DRAM_PERF_STATUS */
  111. #define RAPL_DRAM_POWER_INFO (1 << 5)
  112. /* 0x61c MSR_DRAM_POWER_INFO */
  113. #define RAPL_CORES (1 << 6)
  114. /* 0x638 MSR_PP0_POWER_LIMIT */
  115. /* 0x639 MSR_PP0_ENERGY_STATUS */
  116. #define RAPL_CORE_POLICY (1 << 7)
  117. /* 0x63a MSR_PP0_POLICY */
  118. #define RAPL_GFX (1 << 8)
  119. /* 0x640 MSR_PP1_POWER_LIMIT */
  120. /* 0x641 MSR_PP1_ENERGY_STATUS */
  121. /* 0x642 MSR_PP1_POLICY */
  122. #define TJMAX_DEFAULT 100
  123. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  124. int aperf_mperf_unstable;
  125. int backwards_count;
  126. char *progname;
  127. cpu_set_t *cpu_present_set, *cpu_affinity_set;
  128. size_t cpu_present_setsize, cpu_affinity_setsize;
  129. struct thread_data {
  130. unsigned long long tsc;
  131. unsigned long long aperf;
  132. unsigned long long mperf;
  133. unsigned long long c1;
  134. unsigned long long extra_msr64;
  135. unsigned long long extra_delta64;
  136. unsigned long long extra_msr32;
  137. unsigned long long extra_delta32;
  138. unsigned int smi_count;
  139. unsigned int cpu_id;
  140. unsigned int flags;
  141. #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
  142. #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
  143. } *thread_even, *thread_odd;
  144. struct core_data {
  145. unsigned long long c3;
  146. unsigned long long c6;
  147. unsigned long long c7;
  148. unsigned int core_temp_c;
  149. unsigned int core_id;
  150. } *core_even, *core_odd;
  151. struct pkg_data {
  152. unsigned long long pc2;
  153. unsigned long long pc3;
  154. unsigned long long pc6;
  155. unsigned long long pc7;
  156. unsigned long long pc8;
  157. unsigned long long pc9;
  158. unsigned long long pc10;
  159. unsigned long long pkg_wtd_core_c0;
  160. unsigned long long pkg_any_core_c0;
  161. unsigned long long pkg_any_gfxe_c0;
  162. unsigned long long pkg_both_core_gfxe_c0;
  163. unsigned int package_id;
  164. unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
  165. unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
  166. unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
  167. unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
  168. unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
  169. unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
  170. unsigned int pkg_temp_c;
  171. } *package_even, *package_odd;
  172. #define ODD_COUNTERS thread_odd, core_odd, package_odd
  173. #define EVEN_COUNTERS thread_even, core_even, package_even
  174. #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
  175. (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
  176. topo.num_threads_per_core + \
  177. (core_no) * topo.num_threads_per_core + (thread_no))
  178. #define GET_CORE(core_base, core_no, pkg_no) \
  179. (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
  180. #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
  181. struct system_summary {
  182. struct thread_data threads;
  183. struct core_data cores;
  184. struct pkg_data packages;
  185. } sum, average;
  186. struct topo_params {
  187. int num_packages;
  188. int num_cpus;
  189. int num_cores;
  190. int max_cpu_num;
  191. int num_cores_per_pkg;
  192. int num_threads_per_core;
  193. } topo;
  194. struct timeval tv_even, tv_odd, tv_delta;
  195. void setup_all_buffers(void);
  196. int cpu_is_not_present(int cpu)
  197. {
  198. return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
  199. }
  200. /*
  201. * run func(thread, core, package) in topology order
  202. * skip non-present cpus
  203. */
  204. int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
  205. struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
  206. {
  207. int retval, pkg_no, core_no, thread_no;
  208. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  209. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  210. for (thread_no = 0; thread_no <
  211. topo.num_threads_per_core; ++thread_no) {
  212. struct thread_data *t;
  213. struct core_data *c;
  214. struct pkg_data *p;
  215. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  216. if (cpu_is_not_present(t->cpu_id))
  217. continue;
  218. c = GET_CORE(core_base, core_no, pkg_no);
  219. p = GET_PKG(pkg_base, pkg_no);
  220. retval = func(t, c, p);
  221. if (retval)
  222. return retval;
  223. }
  224. }
  225. }
  226. return 0;
  227. }
  228. int cpu_migrate(int cpu)
  229. {
  230. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  231. CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
  232. if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
  233. return -1;
  234. else
  235. return 0;
  236. }
  237. int get_msr(int cpu, off_t offset, unsigned long long *msr)
  238. {
  239. ssize_t retval;
  240. char pathname[32];
  241. int fd;
  242. sprintf(pathname, "/dev/cpu/%d/msr", cpu);
  243. fd = open(pathname, O_RDONLY);
  244. if (fd < 0)
  245. err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
  246. retval = pread(fd, msr, sizeof *msr, offset);
  247. close(fd);
  248. if (retval != sizeof *msr)
  249. err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
  250. return 0;
  251. }
  252. /*
  253. * Example Format w/ field column widths:
  254. *
  255. * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
  256. * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
  257. */
  258. void print_header(void)
  259. {
  260. if (show_pkg)
  261. outp += sprintf(outp, " Package");
  262. if (show_core)
  263. outp += sprintf(outp, " Core");
  264. if (show_cpu)
  265. outp += sprintf(outp, " CPU");
  266. if (has_aperf)
  267. outp += sprintf(outp, " Avg_MHz");
  268. if (has_aperf)
  269. outp += sprintf(outp, " %%Busy");
  270. if (has_aperf)
  271. outp += sprintf(outp, " Bzy_MHz");
  272. outp += sprintf(outp, " TSC_MHz");
  273. if (extra_delta_offset32)
  274. outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
  275. if (extra_delta_offset64)
  276. outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
  277. if (extra_msr_offset32)
  278. outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
  279. if (extra_msr_offset64)
  280. outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
  281. if (!debug)
  282. goto done;
  283. if (do_smi)
  284. outp += sprintf(outp, " SMI");
  285. if (do_nhm_cstates)
  286. outp += sprintf(outp, " CPU%%c1");
  287. if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
  288. outp += sprintf(outp, " CPU%%c3");
  289. if (do_nhm_cstates)
  290. outp += sprintf(outp, " CPU%%c6");
  291. if (do_snb_cstates)
  292. outp += sprintf(outp, " CPU%%c7");
  293. if (do_dts)
  294. outp += sprintf(outp, " CoreTmp");
  295. if (do_ptm)
  296. outp += sprintf(outp, " PkgTmp");
  297. if (do_skl_residency) {
  298. outp += sprintf(outp, " Totl%%C0");
  299. outp += sprintf(outp, " Any%%C0");
  300. outp += sprintf(outp, " GFX%%C0");
  301. outp += sprintf(outp, " CPUGFX%%");
  302. }
  303. if (do_pc2)
  304. outp += sprintf(outp, " Pkg%%pc2");
  305. if (do_pc3)
  306. outp += sprintf(outp, " Pkg%%pc3");
  307. if (do_pc6)
  308. outp += sprintf(outp, " Pkg%%pc6");
  309. if (do_pc7)
  310. outp += sprintf(outp, " Pkg%%pc7");
  311. if (do_c8_c9_c10) {
  312. outp += sprintf(outp, " Pkg%%pc8");
  313. outp += sprintf(outp, " Pkg%%pc9");
  314. outp += sprintf(outp, " Pk%%pc10");
  315. }
  316. if (do_rapl && !rapl_joules) {
  317. if (do_rapl & RAPL_PKG)
  318. outp += sprintf(outp, " PkgWatt");
  319. if (do_rapl & RAPL_CORES)
  320. outp += sprintf(outp, " CorWatt");
  321. if (do_rapl & RAPL_GFX)
  322. outp += sprintf(outp, " GFXWatt");
  323. if (do_rapl & RAPL_DRAM)
  324. outp += sprintf(outp, " RAMWatt");
  325. if (do_rapl & RAPL_PKG_PERF_STATUS)
  326. outp += sprintf(outp, " PKG_%%");
  327. if (do_rapl & RAPL_DRAM_PERF_STATUS)
  328. outp += sprintf(outp, " RAM_%%");
  329. } else if (do_rapl && rapl_joules) {
  330. if (do_rapl & RAPL_PKG)
  331. outp += sprintf(outp, " Pkg_J");
  332. if (do_rapl & RAPL_CORES)
  333. outp += sprintf(outp, " Cor_J");
  334. if (do_rapl & RAPL_GFX)
  335. outp += sprintf(outp, " GFX_J");
  336. if (do_rapl & RAPL_DRAM)
  337. outp += sprintf(outp, " RAM_J");
  338. if (do_rapl & RAPL_PKG_PERF_STATUS)
  339. outp += sprintf(outp, " PKG_%%");
  340. if (do_rapl & RAPL_DRAM_PERF_STATUS)
  341. outp += sprintf(outp, " RAM_%%");
  342. outp += sprintf(outp, " time");
  343. }
  344. done:
  345. outp += sprintf(outp, "\n");
  346. }
  347. int dump_counters(struct thread_data *t, struct core_data *c,
  348. struct pkg_data *p)
  349. {
  350. outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
  351. if (t) {
  352. outp += sprintf(outp, "CPU: %d flags 0x%x\n",
  353. t->cpu_id, t->flags);
  354. outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
  355. outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
  356. outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
  357. outp += sprintf(outp, "c1: %016llX\n", t->c1);
  358. outp += sprintf(outp, "msr0x%x: %08llX\n",
  359. extra_delta_offset32, t->extra_delta32);
  360. outp += sprintf(outp, "msr0x%x: %016llX\n",
  361. extra_delta_offset64, t->extra_delta64);
  362. outp += sprintf(outp, "msr0x%x: %08llX\n",
  363. extra_msr_offset32, t->extra_msr32);
  364. outp += sprintf(outp, "msr0x%x: %016llX\n",
  365. extra_msr_offset64, t->extra_msr64);
  366. if (do_smi)
  367. outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
  368. }
  369. if (c) {
  370. outp += sprintf(outp, "core: %d\n", c->core_id);
  371. outp += sprintf(outp, "c3: %016llX\n", c->c3);
  372. outp += sprintf(outp, "c6: %016llX\n", c->c6);
  373. outp += sprintf(outp, "c7: %016llX\n", c->c7);
  374. outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
  375. }
  376. if (p) {
  377. outp += sprintf(outp, "package: %d\n", p->package_id);
  378. outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
  379. outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
  380. outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
  381. outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
  382. outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
  383. if (do_pc3)
  384. outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
  385. if (do_pc6)
  386. outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
  387. if (do_pc7)
  388. outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
  389. outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
  390. outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
  391. outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
  392. outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
  393. outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
  394. outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
  395. outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
  396. outp += sprintf(outp, "Throttle PKG: %0X\n",
  397. p->rapl_pkg_perf_status);
  398. outp += sprintf(outp, "Throttle RAM: %0X\n",
  399. p->rapl_dram_perf_status);
  400. outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
  401. }
  402. outp += sprintf(outp, "\n");
  403. return 0;
  404. }
  405. /*
  406. * column formatting convention & formats
  407. */
  408. int format_counters(struct thread_data *t, struct core_data *c,
  409. struct pkg_data *p)
  410. {
  411. double interval_float;
  412. char *fmt8;
  413. /* if showing only 1st thread in core and this isn't one, bail out */
  414. if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  415. return 0;
  416. /* if showing only 1st thread in pkg and this isn't one, bail out */
  417. if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  418. return 0;
  419. interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
  420. /* topo columns, print blanks on 1st (average) line */
  421. if (t == &average.threads) {
  422. if (show_pkg)
  423. outp += sprintf(outp, " -");
  424. if (show_core)
  425. outp += sprintf(outp, " -");
  426. if (show_cpu)
  427. outp += sprintf(outp, " -");
  428. } else {
  429. if (show_pkg) {
  430. if (p)
  431. outp += sprintf(outp, "%8d", p->package_id);
  432. else
  433. outp += sprintf(outp, " -");
  434. }
  435. if (show_core) {
  436. if (c)
  437. outp += sprintf(outp, "%8d", c->core_id);
  438. else
  439. outp += sprintf(outp, " -");
  440. }
  441. if (show_cpu)
  442. outp += sprintf(outp, "%8d", t->cpu_id);
  443. }
  444. /* Avg_MHz */
  445. if (has_aperf)
  446. outp += sprintf(outp, "%8.0f",
  447. 1.0 / units * t->aperf / interval_float);
  448. /* %Busy */
  449. if (has_aperf) {
  450. if (!skip_c0)
  451. outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
  452. else
  453. outp += sprintf(outp, "********");
  454. }
  455. /* Bzy_MHz */
  456. if (has_aperf) {
  457. if (has_base_hz)
  458. outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf);
  459. else
  460. outp += sprintf(outp, "%8.0f",
  461. 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
  462. }
  463. /* TSC_MHz */
  464. outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
  465. /* delta */
  466. if (extra_delta_offset32)
  467. outp += sprintf(outp, " %11llu", t->extra_delta32);
  468. /* DELTA */
  469. if (extra_delta_offset64)
  470. outp += sprintf(outp, " %11llu", t->extra_delta64);
  471. /* msr */
  472. if (extra_msr_offset32)
  473. outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
  474. /* MSR */
  475. if (extra_msr_offset64)
  476. outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
  477. if (!debug)
  478. goto done;
  479. /* SMI */
  480. if (do_smi)
  481. outp += sprintf(outp, "%8d", t->smi_count);
  482. if (do_nhm_cstates) {
  483. if (!skip_c1)
  484. outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
  485. else
  486. outp += sprintf(outp, "********");
  487. }
  488. /* print per-core data only for 1st thread in core */
  489. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  490. goto done;
  491. if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
  492. outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
  493. if (do_nhm_cstates)
  494. outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
  495. if (do_snb_cstates)
  496. outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
  497. if (do_dts)
  498. outp += sprintf(outp, "%8d", c->core_temp_c);
  499. /* print per-package data only for 1st core in package */
  500. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  501. goto done;
  502. /* PkgTmp */
  503. if (do_ptm)
  504. outp += sprintf(outp, "%8d", p->pkg_temp_c);
  505. /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
  506. if (do_skl_residency) {
  507. outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
  508. outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
  509. outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
  510. outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
  511. }
  512. if (do_pc2)
  513. outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
  514. if (do_pc3)
  515. outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
  516. if (do_pc6)
  517. outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
  518. if (do_pc7)
  519. outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
  520. if (do_c8_c9_c10) {
  521. outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
  522. outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
  523. outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
  524. }
  525. /*
  526. * If measurement interval exceeds minimum RAPL Joule Counter range,
  527. * indicate that results are suspect by printing "**" in fraction place.
  528. */
  529. if (interval_float < rapl_joule_counter_range)
  530. fmt8 = "%8.2f";
  531. else
  532. fmt8 = " %6.0f**";
  533. if (do_rapl && !rapl_joules) {
  534. if (do_rapl & RAPL_PKG)
  535. outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
  536. if (do_rapl & RAPL_CORES)
  537. outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
  538. if (do_rapl & RAPL_GFX)
  539. outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
  540. if (do_rapl & RAPL_DRAM)
  541. outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
  542. if (do_rapl & RAPL_PKG_PERF_STATUS)
  543. outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
  544. if (do_rapl & RAPL_DRAM_PERF_STATUS)
  545. outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
  546. } else if (do_rapl && rapl_joules) {
  547. if (do_rapl & RAPL_PKG)
  548. outp += sprintf(outp, fmt8,
  549. p->energy_pkg * rapl_energy_units);
  550. if (do_rapl & RAPL_CORES)
  551. outp += sprintf(outp, fmt8,
  552. p->energy_cores * rapl_energy_units);
  553. if (do_rapl & RAPL_GFX)
  554. outp += sprintf(outp, fmt8,
  555. p->energy_gfx * rapl_energy_units);
  556. if (do_rapl & RAPL_DRAM)
  557. outp += sprintf(outp, fmt8,
  558. p->energy_dram * rapl_dram_energy_units);
  559. if (do_rapl & RAPL_PKG_PERF_STATUS)
  560. outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
  561. if (do_rapl & RAPL_DRAM_PERF_STATUS)
  562. outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
  563. outp += sprintf(outp, fmt8, interval_float);
  564. }
  565. done:
  566. outp += sprintf(outp, "\n");
  567. return 0;
  568. }
  569. void flush_stdout()
  570. {
  571. fputs(output_buffer, stdout);
  572. fflush(stdout);
  573. outp = output_buffer;
  574. }
  575. void flush_stderr()
  576. {
  577. fputs(output_buffer, stderr);
  578. outp = output_buffer;
  579. }
  580. void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  581. {
  582. static int printed;
  583. if (!printed || !summary_only)
  584. print_header();
  585. format_counters(&average.threads, &average.cores, &average.packages);
  586. printed = 1;
  587. if (summary_only)
  588. return;
  589. for_all_cpus(format_counters, t, c, p);
  590. }
  591. #define DELTA_WRAP32(new, old) \
  592. if (new > old) { \
  593. old = new - old; \
  594. } else { \
  595. old = 0x100000000 + new - old; \
  596. }
  597. void
  598. delta_package(struct pkg_data *new, struct pkg_data *old)
  599. {
  600. if (do_skl_residency) {
  601. old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
  602. old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
  603. old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
  604. old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
  605. }
  606. old->pc2 = new->pc2 - old->pc2;
  607. if (do_pc3)
  608. old->pc3 = new->pc3 - old->pc3;
  609. if (do_pc6)
  610. old->pc6 = new->pc6 - old->pc6;
  611. if (do_pc7)
  612. old->pc7 = new->pc7 - old->pc7;
  613. old->pc8 = new->pc8 - old->pc8;
  614. old->pc9 = new->pc9 - old->pc9;
  615. old->pc10 = new->pc10 - old->pc10;
  616. old->pkg_temp_c = new->pkg_temp_c;
  617. DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
  618. DELTA_WRAP32(new->energy_cores, old->energy_cores);
  619. DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
  620. DELTA_WRAP32(new->energy_dram, old->energy_dram);
  621. DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
  622. DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
  623. }
  624. void
  625. delta_core(struct core_data *new, struct core_data *old)
  626. {
  627. old->c3 = new->c3 - old->c3;
  628. old->c6 = new->c6 - old->c6;
  629. old->c7 = new->c7 - old->c7;
  630. old->core_temp_c = new->core_temp_c;
  631. }
  632. /*
  633. * old = new - old
  634. */
  635. void
  636. delta_thread(struct thread_data *new, struct thread_data *old,
  637. struct core_data *core_delta)
  638. {
  639. old->tsc = new->tsc - old->tsc;
  640. /* check for TSC < 1 Mcycles over interval */
  641. if (old->tsc < (1000 * 1000))
  642. errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
  643. "You can disable all c-states by booting with \"idle=poll\"\n"
  644. "or just the deep ones with \"processor.max_cstate=1\"");
  645. old->c1 = new->c1 - old->c1;
  646. if (has_aperf) {
  647. if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
  648. old->aperf = new->aperf - old->aperf;
  649. old->mperf = new->mperf - old->mperf;
  650. } else {
  651. if (!aperf_mperf_unstable) {
  652. fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
  653. fprintf(stderr, "* Frequency results do not cover entire interval *\n");
  654. fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
  655. aperf_mperf_unstable = 1;
  656. }
  657. /*
  658. * mperf delta is likely a huge "positive" number
  659. * can not use it for calculating c0 time
  660. */
  661. skip_c0 = 1;
  662. skip_c1 = 1;
  663. }
  664. }
  665. if (use_c1_residency_msr) {
  666. /*
  667. * Some models have a dedicated C1 residency MSR,
  668. * which should be more accurate than the derivation below.
  669. */
  670. } else {
  671. /*
  672. * As counter collection is not atomic,
  673. * it is possible for mperf's non-halted cycles + idle states
  674. * to exceed TSC's all cycles: show c1 = 0% in that case.
  675. */
  676. if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
  677. old->c1 = 0;
  678. else {
  679. /* normal case, derive c1 */
  680. old->c1 = old->tsc - old->mperf - core_delta->c3
  681. - core_delta->c6 - core_delta->c7;
  682. }
  683. }
  684. if (old->mperf == 0) {
  685. if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
  686. old->mperf = 1; /* divide by 0 protection */
  687. }
  688. old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
  689. old->extra_delta32 &= 0xFFFFFFFF;
  690. old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
  691. /*
  692. * Extra MSR is just a snapshot, simply copy latest w/o subtracting
  693. */
  694. old->extra_msr32 = new->extra_msr32;
  695. old->extra_msr64 = new->extra_msr64;
  696. if (do_smi)
  697. old->smi_count = new->smi_count - old->smi_count;
  698. }
  699. int delta_cpu(struct thread_data *t, struct core_data *c,
  700. struct pkg_data *p, struct thread_data *t2,
  701. struct core_data *c2, struct pkg_data *p2)
  702. {
  703. /* calculate core delta only for 1st thread in core */
  704. if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
  705. delta_core(c, c2);
  706. /* always calculate thread delta */
  707. delta_thread(t, t2, c2); /* c2 is core delta */
  708. /* calculate package delta only for 1st core in package */
  709. if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
  710. delta_package(p, p2);
  711. return 0;
  712. }
  713. void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  714. {
  715. t->tsc = 0;
  716. t->aperf = 0;
  717. t->mperf = 0;
  718. t->c1 = 0;
  719. t->smi_count = 0;
  720. t->extra_delta32 = 0;
  721. t->extra_delta64 = 0;
  722. /* tells format_counters to dump all fields from this set */
  723. t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
  724. c->c3 = 0;
  725. c->c6 = 0;
  726. c->c7 = 0;
  727. c->core_temp_c = 0;
  728. p->pkg_wtd_core_c0 = 0;
  729. p->pkg_any_core_c0 = 0;
  730. p->pkg_any_gfxe_c0 = 0;
  731. p->pkg_both_core_gfxe_c0 = 0;
  732. p->pc2 = 0;
  733. if (do_pc3)
  734. p->pc3 = 0;
  735. if (do_pc6)
  736. p->pc6 = 0;
  737. if (do_pc7)
  738. p->pc7 = 0;
  739. p->pc8 = 0;
  740. p->pc9 = 0;
  741. p->pc10 = 0;
  742. p->energy_pkg = 0;
  743. p->energy_dram = 0;
  744. p->energy_cores = 0;
  745. p->energy_gfx = 0;
  746. p->rapl_pkg_perf_status = 0;
  747. p->rapl_dram_perf_status = 0;
  748. p->pkg_temp_c = 0;
  749. }
  750. int sum_counters(struct thread_data *t, struct core_data *c,
  751. struct pkg_data *p)
  752. {
  753. average.threads.tsc += t->tsc;
  754. average.threads.aperf += t->aperf;
  755. average.threads.mperf += t->mperf;
  756. average.threads.c1 += t->c1;
  757. average.threads.extra_delta32 += t->extra_delta32;
  758. average.threads.extra_delta64 += t->extra_delta64;
  759. /* sum per-core values only for 1st thread in core */
  760. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  761. return 0;
  762. average.cores.c3 += c->c3;
  763. average.cores.c6 += c->c6;
  764. average.cores.c7 += c->c7;
  765. average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
  766. /* sum per-pkg values only for 1st core in pkg */
  767. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  768. return 0;
  769. if (do_skl_residency) {
  770. average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
  771. average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
  772. average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
  773. average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
  774. }
  775. average.packages.pc2 += p->pc2;
  776. if (do_pc3)
  777. average.packages.pc3 += p->pc3;
  778. if (do_pc6)
  779. average.packages.pc6 += p->pc6;
  780. if (do_pc7)
  781. average.packages.pc7 += p->pc7;
  782. average.packages.pc8 += p->pc8;
  783. average.packages.pc9 += p->pc9;
  784. average.packages.pc10 += p->pc10;
  785. average.packages.energy_pkg += p->energy_pkg;
  786. average.packages.energy_dram += p->energy_dram;
  787. average.packages.energy_cores += p->energy_cores;
  788. average.packages.energy_gfx += p->energy_gfx;
  789. average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
  790. average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
  791. average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
  792. return 0;
  793. }
  794. /*
  795. * sum the counters for all cpus in the system
  796. * compute the weighted average
  797. */
  798. void compute_average(struct thread_data *t, struct core_data *c,
  799. struct pkg_data *p)
  800. {
  801. clear_counters(&average.threads, &average.cores, &average.packages);
  802. for_all_cpus(sum_counters, t, c, p);
  803. average.threads.tsc /= topo.num_cpus;
  804. average.threads.aperf /= topo.num_cpus;
  805. average.threads.mperf /= topo.num_cpus;
  806. average.threads.c1 /= topo.num_cpus;
  807. average.threads.extra_delta32 /= topo.num_cpus;
  808. average.threads.extra_delta32 &= 0xFFFFFFFF;
  809. average.threads.extra_delta64 /= topo.num_cpus;
  810. average.cores.c3 /= topo.num_cores;
  811. average.cores.c6 /= topo.num_cores;
  812. average.cores.c7 /= topo.num_cores;
  813. if (do_skl_residency) {
  814. average.packages.pkg_wtd_core_c0 /= topo.num_packages;
  815. average.packages.pkg_any_core_c0 /= topo.num_packages;
  816. average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
  817. average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
  818. }
  819. average.packages.pc2 /= topo.num_packages;
  820. if (do_pc3)
  821. average.packages.pc3 /= topo.num_packages;
  822. if (do_pc6)
  823. average.packages.pc6 /= topo.num_packages;
  824. if (do_pc7)
  825. average.packages.pc7 /= topo.num_packages;
  826. average.packages.pc8 /= topo.num_packages;
  827. average.packages.pc9 /= topo.num_packages;
  828. average.packages.pc10 /= topo.num_packages;
  829. }
  830. static unsigned long long rdtsc(void)
  831. {
  832. unsigned int low, high;
  833. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  834. return low | ((unsigned long long)high) << 32;
  835. }
  836. /*
  837. * get_counters(...)
  838. * migrate to cpu
  839. * acquire and record local counters for that cpu
  840. */
  841. int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  842. {
  843. int cpu = t->cpu_id;
  844. unsigned long long msr;
  845. if (cpu_migrate(cpu)) {
  846. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  847. return -1;
  848. }
  849. t->tsc = rdtsc(); /* we are running on local CPU of interest */
  850. if (has_aperf) {
  851. if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
  852. return -3;
  853. if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
  854. return -4;
  855. t->aperf = t->aperf * aperf_mperf_multiplier;
  856. t->mperf = t->mperf * aperf_mperf_multiplier;
  857. }
  858. if (do_smi) {
  859. if (get_msr(cpu, MSR_SMI_COUNT, &msr))
  860. return -5;
  861. t->smi_count = msr & 0xFFFFFFFF;
  862. }
  863. if (extra_delta_offset32) {
  864. if (get_msr(cpu, extra_delta_offset32, &msr))
  865. return -5;
  866. t->extra_delta32 = msr & 0xFFFFFFFF;
  867. }
  868. if (extra_delta_offset64)
  869. if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
  870. return -5;
  871. if (extra_msr_offset32) {
  872. if (get_msr(cpu, extra_msr_offset32, &msr))
  873. return -5;
  874. t->extra_msr32 = msr & 0xFFFFFFFF;
  875. }
  876. if (extra_msr_offset64)
  877. if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
  878. return -5;
  879. if (use_c1_residency_msr) {
  880. if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
  881. return -6;
  882. }
  883. /* collect core counters only for 1st thread in core */
  884. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  885. return 0;
  886. if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
  887. if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
  888. return -6;
  889. }
  890. if (do_nhm_cstates && !do_knl_cstates) {
  891. if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
  892. return -7;
  893. } else if (do_knl_cstates) {
  894. if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
  895. return -7;
  896. }
  897. if (do_snb_cstates)
  898. if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
  899. return -8;
  900. if (do_dts) {
  901. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  902. return -9;
  903. c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  904. }
  905. /* collect package counters only for 1st core in package */
  906. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  907. return 0;
  908. if (do_skl_residency) {
  909. if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
  910. return -10;
  911. if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
  912. return -11;
  913. if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
  914. return -12;
  915. if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
  916. return -13;
  917. }
  918. if (do_pc3)
  919. if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
  920. return -9;
  921. if (do_pc6)
  922. if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
  923. return -10;
  924. if (do_pc2)
  925. if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
  926. return -11;
  927. if (do_pc7)
  928. if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
  929. return -12;
  930. if (do_c8_c9_c10) {
  931. if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
  932. return -13;
  933. if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
  934. return -13;
  935. if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
  936. return -13;
  937. }
  938. if (do_rapl & RAPL_PKG) {
  939. if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
  940. return -13;
  941. p->energy_pkg = msr & 0xFFFFFFFF;
  942. }
  943. if (do_rapl & RAPL_CORES) {
  944. if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
  945. return -14;
  946. p->energy_cores = msr & 0xFFFFFFFF;
  947. }
  948. if (do_rapl & RAPL_DRAM) {
  949. if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
  950. return -15;
  951. p->energy_dram = msr & 0xFFFFFFFF;
  952. }
  953. if (do_rapl & RAPL_GFX) {
  954. if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
  955. return -16;
  956. p->energy_gfx = msr & 0xFFFFFFFF;
  957. }
  958. if (do_rapl & RAPL_PKG_PERF_STATUS) {
  959. if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
  960. return -16;
  961. p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
  962. }
  963. if (do_rapl & RAPL_DRAM_PERF_STATUS) {
  964. if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
  965. return -16;
  966. p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
  967. }
  968. if (do_ptm) {
  969. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  970. return -17;
  971. p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  972. }
  973. return 0;
  974. }
  975. /*
  976. * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
  977. * If you change the values, note they are used both in comparisons
  978. * (>= PCL__7) and to index pkg_cstate_limit_strings[].
  979. */
  980. #define PCLUKN 0 /* Unknown */
  981. #define PCLRSV 1 /* Reserved */
  982. #define PCL__0 2 /* PC0 */
  983. #define PCL__1 3 /* PC1 */
  984. #define PCL__2 4 /* PC2 */
  985. #define PCL__3 5 /* PC3 */
  986. #define PCL__4 6 /* PC4 */
  987. #define PCL__6 7 /* PC6 */
  988. #define PCL_6N 8 /* PC6 No Retention */
  989. #define PCL_6R 9 /* PC6 Retention */
  990. #define PCL__7 10 /* PC7 */
  991. #define PCL_7S 11 /* PC7 Shrink */
  992. #define PCL__8 12 /* PC8 */
  993. #define PCL__9 13 /* PC9 */
  994. #define PCLUNL 14 /* Unlimited */
  995. int pkg_cstate_limit = PCLUKN;
  996. char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
  997. "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
  998. int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  999. int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1000. int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1001. int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1002. int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1003. int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1004. static void
  1005. calculate_tsc_tweak()
  1006. {
  1007. tsc_tweak = base_hz / tsc_hz;
  1008. }
  1009. static void
  1010. dump_nhm_platform_info(void)
  1011. {
  1012. unsigned long long msr;
  1013. unsigned int ratio;
  1014. get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
  1015. fprintf(stderr, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
  1016. ratio = (msr >> 40) & 0xFF;
  1017. fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
  1018. ratio, bclk, ratio * bclk);
  1019. ratio = (msr >> 8) & 0xFF;
  1020. fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
  1021. ratio, bclk, ratio * bclk);
  1022. get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
  1023. fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
  1024. base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
  1025. return;
  1026. }
  1027. static void
  1028. dump_hsw_turbo_ratio_limits(void)
  1029. {
  1030. unsigned long long msr;
  1031. unsigned int ratio;
  1032. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
  1033. fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
  1034. ratio = (msr >> 8) & 0xFF;
  1035. if (ratio)
  1036. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
  1037. ratio, bclk, ratio * bclk);
  1038. ratio = (msr >> 0) & 0xFF;
  1039. if (ratio)
  1040. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
  1041. ratio, bclk, ratio * bclk);
  1042. return;
  1043. }
  1044. static void
  1045. dump_ivt_turbo_ratio_limits(void)
  1046. {
  1047. unsigned long long msr;
  1048. unsigned int ratio;
  1049. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
  1050. fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
  1051. ratio = (msr >> 56) & 0xFF;
  1052. if (ratio)
  1053. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
  1054. ratio, bclk, ratio * bclk);
  1055. ratio = (msr >> 48) & 0xFF;
  1056. if (ratio)
  1057. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
  1058. ratio, bclk, ratio * bclk);
  1059. ratio = (msr >> 40) & 0xFF;
  1060. if (ratio)
  1061. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
  1062. ratio, bclk, ratio * bclk);
  1063. ratio = (msr >> 32) & 0xFF;
  1064. if (ratio)
  1065. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
  1066. ratio, bclk, ratio * bclk);
  1067. ratio = (msr >> 24) & 0xFF;
  1068. if (ratio)
  1069. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
  1070. ratio, bclk, ratio * bclk);
  1071. ratio = (msr >> 16) & 0xFF;
  1072. if (ratio)
  1073. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
  1074. ratio, bclk, ratio * bclk);
  1075. ratio = (msr >> 8) & 0xFF;
  1076. if (ratio)
  1077. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
  1078. ratio, bclk, ratio * bclk);
  1079. ratio = (msr >> 0) & 0xFF;
  1080. if (ratio)
  1081. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
  1082. ratio, bclk, ratio * bclk);
  1083. return;
  1084. }
  1085. static void
  1086. dump_nhm_turbo_ratio_limits(void)
  1087. {
  1088. unsigned long long msr;
  1089. unsigned int ratio;
  1090. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
  1091. fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
  1092. ratio = (msr >> 56) & 0xFF;
  1093. if (ratio)
  1094. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
  1095. ratio, bclk, ratio * bclk);
  1096. ratio = (msr >> 48) & 0xFF;
  1097. if (ratio)
  1098. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
  1099. ratio, bclk, ratio * bclk);
  1100. ratio = (msr >> 40) & 0xFF;
  1101. if (ratio)
  1102. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
  1103. ratio, bclk, ratio * bclk);
  1104. ratio = (msr >> 32) & 0xFF;
  1105. if (ratio)
  1106. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
  1107. ratio, bclk, ratio * bclk);
  1108. ratio = (msr >> 24) & 0xFF;
  1109. if (ratio)
  1110. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
  1111. ratio, bclk, ratio * bclk);
  1112. ratio = (msr >> 16) & 0xFF;
  1113. if (ratio)
  1114. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
  1115. ratio, bclk, ratio * bclk);
  1116. ratio = (msr >> 8) & 0xFF;
  1117. if (ratio)
  1118. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
  1119. ratio, bclk, ratio * bclk);
  1120. ratio = (msr >> 0) & 0xFF;
  1121. if (ratio)
  1122. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
  1123. ratio, bclk, ratio * bclk);
  1124. return;
  1125. }
  1126. static void
  1127. dump_knl_turbo_ratio_limits(void)
  1128. {
  1129. int cores;
  1130. unsigned int ratio;
  1131. unsigned long long msr;
  1132. int delta_cores;
  1133. int delta_ratio;
  1134. int i;
  1135. get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
  1136. fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
  1137. base_cpu, msr);
  1138. /**
  1139. * Turbo encoding in KNL is as follows:
  1140. * [7:0] -- Base value of number of active cores of bucket 1.
  1141. * [15:8] -- Base value of freq ratio of bucket 1.
  1142. * [20:16] -- +ve delta of number of active cores of bucket 2.
  1143. * i.e. active cores of bucket 2 =
  1144. * active cores of bucket 1 + delta
  1145. * [23:21] -- Negative delta of freq ratio of bucket 2.
  1146. * i.e. freq ratio of bucket 2 =
  1147. * freq ratio of bucket 1 - delta
  1148. * [28:24]-- +ve delta of number of active cores of bucket 3.
  1149. * [31:29]-- -ve delta of freq ratio of bucket 3.
  1150. * [36:32]-- +ve delta of number of active cores of bucket 4.
  1151. * [39:37]-- -ve delta of freq ratio of bucket 4.
  1152. * [44:40]-- +ve delta of number of active cores of bucket 5.
  1153. * [47:45]-- -ve delta of freq ratio of bucket 5.
  1154. * [52:48]-- +ve delta of number of active cores of bucket 6.
  1155. * [55:53]-- -ve delta of freq ratio of bucket 6.
  1156. * [60:56]-- +ve delta of number of active cores of bucket 7.
  1157. * [63:61]-- -ve delta of freq ratio of bucket 7.
  1158. */
  1159. cores = msr & 0xFF;
  1160. ratio = (msr >> 8) && 0xFF;
  1161. if (ratio > 0)
  1162. fprintf(stderr,
  1163. "%d * %.0f = %.0f MHz max turbo %d active cores\n",
  1164. ratio, bclk, ratio * bclk, cores);
  1165. for (i = 16; i < 64; i = i + 8) {
  1166. delta_cores = (msr >> i) & 0x1F;
  1167. delta_ratio = (msr >> (i + 5)) && 0x7;
  1168. if (!delta_cores || !delta_ratio)
  1169. return;
  1170. cores = cores + delta_cores;
  1171. ratio = ratio - delta_ratio;
  1172. /** -ve ratios will make successive ratio calculations
  1173. * negative. Hence return instead of carrying on.
  1174. */
  1175. if (ratio > 0)
  1176. fprintf(stderr,
  1177. "%d * %.0f = %.0f MHz max turbo %d active cores\n",
  1178. ratio, bclk, ratio * bclk, cores);
  1179. }
  1180. }
  1181. static void
  1182. dump_nhm_cst_cfg(void)
  1183. {
  1184. unsigned long long msr;
  1185. get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
  1186. #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
  1187. #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
  1188. fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
  1189. fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
  1190. (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
  1191. (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
  1192. (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
  1193. (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
  1194. (msr & (1 << 15)) ? "" : "UN",
  1195. (unsigned int)msr & 7,
  1196. pkg_cstate_limit_strings[pkg_cstate_limit]);
  1197. return;
  1198. }
  1199. static void
  1200. dump_config_tdp(void)
  1201. {
  1202. unsigned long long msr;
  1203. get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
  1204. fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
  1205. fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
  1206. get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
  1207. fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
  1208. if (msr) {
  1209. fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
  1210. fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
  1211. fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
  1212. fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
  1213. }
  1214. fprintf(stderr, ")\n");
  1215. get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
  1216. fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
  1217. if (msr) {
  1218. fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
  1219. fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
  1220. fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
  1221. fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
  1222. }
  1223. fprintf(stderr, ")\n");
  1224. get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
  1225. fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
  1226. if ((msr) & 0x3)
  1227. fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
  1228. fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
  1229. fprintf(stderr, ")\n");
  1230. get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
  1231. fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
  1232. fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F);
  1233. fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
  1234. fprintf(stderr, ")\n");
  1235. }
  1236. void free_all_buffers(void)
  1237. {
  1238. CPU_FREE(cpu_present_set);
  1239. cpu_present_set = NULL;
  1240. cpu_present_set = 0;
  1241. CPU_FREE(cpu_affinity_set);
  1242. cpu_affinity_set = NULL;
  1243. cpu_affinity_setsize = 0;
  1244. free(thread_even);
  1245. free(core_even);
  1246. free(package_even);
  1247. thread_even = NULL;
  1248. core_even = NULL;
  1249. package_even = NULL;
  1250. free(thread_odd);
  1251. free(core_odd);
  1252. free(package_odd);
  1253. thread_odd = NULL;
  1254. core_odd = NULL;
  1255. package_odd = NULL;
  1256. free(output_buffer);
  1257. output_buffer = NULL;
  1258. outp = NULL;
  1259. }
  1260. /*
  1261. * Open a file, and exit on failure
  1262. */
  1263. FILE *fopen_or_die(const char *path, const char *mode)
  1264. {
  1265. FILE *filep = fopen(path, "r");
  1266. if (!filep)
  1267. err(1, "%s: open failed", path);
  1268. return filep;
  1269. }
  1270. /*
  1271. * Parse a file containing a single int.
  1272. */
  1273. int parse_int_file(const char *fmt, ...)
  1274. {
  1275. va_list args;
  1276. char path[PATH_MAX];
  1277. FILE *filep;
  1278. int value;
  1279. va_start(args, fmt);
  1280. vsnprintf(path, sizeof(path), fmt, args);
  1281. va_end(args);
  1282. filep = fopen_or_die(path, "r");
  1283. if (fscanf(filep, "%d", &value) != 1)
  1284. err(1, "%s: failed to parse number from file", path);
  1285. fclose(filep);
  1286. return value;
  1287. }
  1288. /*
  1289. * get_cpu_position_in_core(cpu)
  1290. * return the position of the CPU among its HT siblings in the core
  1291. * return -1 if the sibling is not in list
  1292. */
  1293. int get_cpu_position_in_core(int cpu)
  1294. {
  1295. char path[64];
  1296. FILE *filep;
  1297. int this_cpu;
  1298. char character;
  1299. int i;
  1300. sprintf(path,
  1301. "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
  1302. cpu);
  1303. filep = fopen(path, "r");
  1304. if (filep == NULL) {
  1305. perror(path);
  1306. exit(1);
  1307. }
  1308. for (i = 0; i < topo.num_threads_per_core; i++) {
  1309. fscanf(filep, "%d", &this_cpu);
  1310. if (this_cpu == cpu) {
  1311. fclose(filep);
  1312. return i;
  1313. }
  1314. /* Account for no separator after last thread*/
  1315. if (i != (topo.num_threads_per_core - 1))
  1316. fscanf(filep, "%c", &character);
  1317. }
  1318. fclose(filep);
  1319. return -1;
  1320. }
  1321. /*
  1322. * cpu_is_first_core_in_package(cpu)
  1323. * return 1 if given CPU is 1st core in package
  1324. */
  1325. int cpu_is_first_core_in_package(int cpu)
  1326. {
  1327. return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
  1328. }
  1329. int get_physical_package_id(int cpu)
  1330. {
  1331. return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  1332. }
  1333. int get_core_id(int cpu)
  1334. {
  1335. return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
  1336. }
  1337. int get_num_ht_siblings(int cpu)
  1338. {
  1339. char path[80];
  1340. FILE *filep;
  1341. int sib1;
  1342. int matches = 0;
  1343. char character;
  1344. char str[100];
  1345. char *ch;
  1346. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  1347. filep = fopen_or_die(path, "r");
  1348. /*
  1349. * file format:
  1350. * A ',' separated or '-' separated set of numbers
  1351. * (eg 1-2 or 1,3,4,5)
  1352. */
  1353. fscanf(filep, "%d%c\n", &sib1, &character);
  1354. fseek(filep, 0, SEEK_SET);
  1355. fgets(str, 100, filep);
  1356. ch = strchr(str, character);
  1357. while (ch != NULL) {
  1358. matches++;
  1359. ch = strchr(ch+1, character);
  1360. }
  1361. fclose(filep);
  1362. return matches+1;
  1363. }
  1364. /*
  1365. * run func(thread, core, package) in topology order
  1366. * skip non-present cpus
  1367. */
  1368. int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
  1369. struct pkg_data *, struct thread_data *, struct core_data *,
  1370. struct pkg_data *), struct thread_data *thread_base,
  1371. struct core_data *core_base, struct pkg_data *pkg_base,
  1372. struct thread_data *thread_base2, struct core_data *core_base2,
  1373. struct pkg_data *pkg_base2)
  1374. {
  1375. int retval, pkg_no, core_no, thread_no;
  1376. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  1377. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  1378. for (thread_no = 0; thread_no <
  1379. topo.num_threads_per_core; ++thread_no) {
  1380. struct thread_data *t, *t2;
  1381. struct core_data *c, *c2;
  1382. struct pkg_data *p, *p2;
  1383. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  1384. if (cpu_is_not_present(t->cpu_id))
  1385. continue;
  1386. t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
  1387. c = GET_CORE(core_base, core_no, pkg_no);
  1388. c2 = GET_CORE(core_base2, core_no, pkg_no);
  1389. p = GET_PKG(pkg_base, pkg_no);
  1390. p2 = GET_PKG(pkg_base2, pkg_no);
  1391. retval = func(t, c, p, t2, c2, p2);
  1392. if (retval)
  1393. return retval;
  1394. }
  1395. }
  1396. }
  1397. return 0;
  1398. }
  1399. /*
  1400. * run func(cpu) on every cpu in /proc/stat
  1401. * return max_cpu number
  1402. */
  1403. int for_all_proc_cpus(int (func)(int))
  1404. {
  1405. FILE *fp;
  1406. int cpu_num;
  1407. int retval;
  1408. fp = fopen_or_die(proc_stat, "r");
  1409. retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  1410. if (retval != 0)
  1411. err(1, "%s: failed to parse format", proc_stat);
  1412. while (1) {
  1413. retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
  1414. if (retval != 1)
  1415. break;
  1416. retval = func(cpu_num);
  1417. if (retval) {
  1418. fclose(fp);
  1419. return(retval);
  1420. }
  1421. }
  1422. fclose(fp);
  1423. return 0;
  1424. }
  1425. void re_initialize(void)
  1426. {
  1427. free_all_buffers();
  1428. setup_all_buffers();
  1429. printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
  1430. }
  1431. /*
  1432. * count_cpus()
  1433. * remember the last one seen, it will be the max
  1434. */
  1435. int count_cpus(int cpu)
  1436. {
  1437. if (topo.max_cpu_num < cpu)
  1438. topo.max_cpu_num = cpu;
  1439. topo.num_cpus += 1;
  1440. return 0;
  1441. }
  1442. int mark_cpu_present(int cpu)
  1443. {
  1444. CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
  1445. return 0;
  1446. }
  1447. void turbostat_loop()
  1448. {
  1449. int retval;
  1450. int restarted = 0;
  1451. restart:
  1452. restarted++;
  1453. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  1454. if (retval < -1) {
  1455. exit(retval);
  1456. } else if (retval == -1) {
  1457. if (restarted > 1) {
  1458. exit(retval);
  1459. }
  1460. re_initialize();
  1461. goto restart;
  1462. }
  1463. restarted = 0;
  1464. gettimeofday(&tv_even, (struct timezone *)NULL);
  1465. while (1) {
  1466. if (for_all_proc_cpus(cpu_is_not_present)) {
  1467. re_initialize();
  1468. goto restart;
  1469. }
  1470. sleep(interval_sec);
  1471. retval = for_all_cpus(get_counters, ODD_COUNTERS);
  1472. if (retval < -1) {
  1473. exit(retval);
  1474. } else if (retval == -1) {
  1475. re_initialize();
  1476. goto restart;
  1477. }
  1478. gettimeofday(&tv_odd, (struct timezone *)NULL);
  1479. timersub(&tv_odd, &tv_even, &tv_delta);
  1480. for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
  1481. compute_average(EVEN_COUNTERS);
  1482. format_all_counters(EVEN_COUNTERS);
  1483. flush_stdout();
  1484. sleep(interval_sec);
  1485. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  1486. if (retval < -1) {
  1487. exit(retval);
  1488. } else if (retval == -1) {
  1489. re_initialize();
  1490. goto restart;
  1491. }
  1492. gettimeofday(&tv_even, (struct timezone *)NULL);
  1493. timersub(&tv_even, &tv_odd, &tv_delta);
  1494. for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
  1495. compute_average(ODD_COUNTERS);
  1496. format_all_counters(ODD_COUNTERS);
  1497. flush_stdout();
  1498. }
  1499. }
  1500. void check_dev_msr()
  1501. {
  1502. struct stat sb;
  1503. char pathname[32];
  1504. sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
  1505. if (stat(pathname, &sb))
  1506. if (system("/sbin/modprobe msr > /dev/null 2>&1"))
  1507. err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
  1508. }
  1509. void check_permissions()
  1510. {
  1511. struct __user_cap_header_struct cap_header_data;
  1512. cap_user_header_t cap_header = &cap_header_data;
  1513. struct __user_cap_data_struct cap_data_data;
  1514. cap_user_data_t cap_data = &cap_data_data;
  1515. extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
  1516. int do_exit = 0;
  1517. char pathname[32];
  1518. /* check for CAP_SYS_RAWIO */
  1519. cap_header->pid = getpid();
  1520. cap_header->version = _LINUX_CAPABILITY_VERSION;
  1521. if (capget(cap_header, cap_data) < 0)
  1522. err(-6, "capget(2) failed");
  1523. if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
  1524. do_exit++;
  1525. warnx("capget(CAP_SYS_RAWIO) failed,"
  1526. " try \"# setcap cap_sys_rawio=ep %s\"", progname);
  1527. }
  1528. /* test file permissions */
  1529. sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
  1530. if (euidaccess(pathname, R_OK)) {
  1531. do_exit++;
  1532. warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
  1533. }
  1534. /* if all else fails, thell them to be root */
  1535. if (do_exit)
  1536. if (getuid() != 0)
  1537. warnx("... or simply run as root");
  1538. if (do_exit)
  1539. exit(-6);
  1540. }
  1541. /*
  1542. * NHM adds support for additional MSRs:
  1543. *
  1544. * MSR_SMI_COUNT 0x00000034
  1545. *
  1546. * MSR_PLATFORM_INFO 0x000000ce
  1547. * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
  1548. *
  1549. * MSR_PKG_C3_RESIDENCY 0x000003f8
  1550. * MSR_PKG_C6_RESIDENCY 0x000003f9
  1551. * MSR_CORE_C3_RESIDENCY 0x000003fc
  1552. * MSR_CORE_C6_RESIDENCY 0x000003fd
  1553. *
  1554. * Side effect:
  1555. * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
  1556. */
  1557. int probe_nhm_msrs(unsigned int family, unsigned int model)
  1558. {
  1559. unsigned long long msr;
  1560. unsigned int base_ratio;
  1561. int *pkg_cstate_limits;
  1562. if (!genuine_intel)
  1563. return 0;
  1564. if (family != 6)
  1565. return 0;
  1566. bclk = discover_bclk(family, model);
  1567. switch (model) {
  1568. case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
  1569. case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
  1570. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  1571. case 0x25: /* Westmere Client - Clarkdale, Arrandale */
  1572. case 0x2C: /* Westmere EP - Gulftown */
  1573. case 0x2E: /* Nehalem-EX Xeon - Beckton */
  1574. case 0x2F: /* Westmere-EX Xeon - Eagleton */
  1575. pkg_cstate_limits = nhm_pkg_cstate_limits;
  1576. break;
  1577. case 0x2A: /* SNB */
  1578. case 0x2D: /* SNB Xeon */
  1579. case 0x3A: /* IVB */
  1580. case 0x3E: /* IVB Xeon */
  1581. pkg_cstate_limits = snb_pkg_cstate_limits;
  1582. break;
  1583. case 0x3C: /* HSW */
  1584. case 0x3F: /* HSX */
  1585. case 0x45: /* HSW */
  1586. case 0x46: /* HSW */
  1587. case 0x3D: /* BDW */
  1588. case 0x47: /* BDW */
  1589. case 0x4F: /* BDX */
  1590. case 0x56: /* BDX-DE */
  1591. case 0x4E: /* SKL */
  1592. case 0x5E: /* SKL */
  1593. pkg_cstate_limits = hsw_pkg_cstate_limits;
  1594. break;
  1595. case 0x37: /* BYT */
  1596. case 0x4D: /* AVN */
  1597. pkg_cstate_limits = slv_pkg_cstate_limits;
  1598. break;
  1599. case 0x4C: /* AMT */
  1600. pkg_cstate_limits = amt_pkg_cstate_limits;
  1601. break;
  1602. case 0x57: /* PHI */
  1603. pkg_cstate_limits = phi_pkg_cstate_limits;
  1604. break;
  1605. default:
  1606. return 0;
  1607. }
  1608. get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
  1609. pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
  1610. get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
  1611. base_ratio = (msr >> 8) & 0xFF;
  1612. base_hz = base_ratio * bclk * 1000000;
  1613. has_base_hz = 1;
  1614. return 1;
  1615. }
  1616. int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
  1617. {
  1618. switch (model) {
  1619. /* Nehalem compatible, but do not include turbo-ratio limit support */
  1620. case 0x2E: /* Nehalem-EX Xeon - Beckton */
  1621. case 0x2F: /* Westmere-EX Xeon - Eagleton */
  1622. return 0;
  1623. default:
  1624. return 1;
  1625. }
  1626. }
  1627. int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
  1628. {
  1629. if (!genuine_intel)
  1630. return 0;
  1631. if (family != 6)
  1632. return 0;
  1633. switch (model) {
  1634. case 0x3E: /* IVB Xeon */
  1635. case 0x3F: /* HSW Xeon */
  1636. return 1;
  1637. default:
  1638. return 0;
  1639. }
  1640. }
  1641. int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
  1642. {
  1643. if (!genuine_intel)
  1644. return 0;
  1645. if (family != 6)
  1646. return 0;
  1647. switch (model) {
  1648. case 0x3F: /* HSW Xeon */
  1649. return 1;
  1650. default:
  1651. return 0;
  1652. }
  1653. }
  1654. int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
  1655. {
  1656. if (!genuine_intel)
  1657. return 0;
  1658. if (family != 6)
  1659. return 0;
  1660. switch (model) {
  1661. case 0x57: /* Knights Landing */
  1662. return 1;
  1663. default:
  1664. return 0;
  1665. }
  1666. }
  1667. int has_config_tdp(unsigned int family, unsigned int model)
  1668. {
  1669. if (!genuine_intel)
  1670. return 0;
  1671. if (family != 6)
  1672. return 0;
  1673. switch (model) {
  1674. case 0x3A: /* IVB */
  1675. case 0x3C: /* HSW */
  1676. case 0x3F: /* HSX */
  1677. case 0x45: /* HSW */
  1678. case 0x46: /* HSW */
  1679. case 0x3D: /* BDW */
  1680. case 0x47: /* BDW */
  1681. case 0x4F: /* BDX */
  1682. case 0x56: /* BDX-DE */
  1683. case 0x4E: /* SKL */
  1684. case 0x5E: /* SKL */
  1685. case 0x57: /* Knights Landing */
  1686. return 1;
  1687. default:
  1688. return 0;
  1689. }
  1690. }
  1691. static void
  1692. dump_cstate_pstate_config_info(family, model)
  1693. {
  1694. if (!do_nhm_platform_info)
  1695. return;
  1696. dump_nhm_platform_info();
  1697. if (has_hsw_turbo_ratio_limit(family, model))
  1698. dump_hsw_turbo_ratio_limits();
  1699. if (has_ivt_turbo_ratio_limit(family, model))
  1700. dump_ivt_turbo_ratio_limits();
  1701. if (has_nhm_turbo_ratio_limit(family, model))
  1702. dump_nhm_turbo_ratio_limits();
  1703. if (has_knl_turbo_ratio_limit(family, model))
  1704. dump_knl_turbo_ratio_limits();
  1705. if (has_config_tdp(family, model))
  1706. dump_config_tdp();
  1707. dump_nhm_cst_cfg();
  1708. }
  1709. /*
  1710. * print_epb()
  1711. * Decode the ENERGY_PERF_BIAS MSR
  1712. */
  1713. int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1714. {
  1715. unsigned long long msr;
  1716. char *epb_string;
  1717. int cpu;
  1718. if (!has_epb)
  1719. return 0;
  1720. cpu = t->cpu_id;
  1721. /* EPB is per-package */
  1722. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1723. return 0;
  1724. if (cpu_migrate(cpu)) {
  1725. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1726. return -1;
  1727. }
  1728. if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
  1729. return 0;
  1730. switch (msr & 0xF) {
  1731. case ENERGY_PERF_BIAS_PERFORMANCE:
  1732. epb_string = "performance";
  1733. break;
  1734. case ENERGY_PERF_BIAS_NORMAL:
  1735. epb_string = "balanced";
  1736. break;
  1737. case ENERGY_PERF_BIAS_POWERSAVE:
  1738. epb_string = "powersave";
  1739. break;
  1740. default:
  1741. epb_string = "custom";
  1742. break;
  1743. }
  1744. fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
  1745. return 0;
  1746. }
  1747. /*
  1748. * print_perf_limit()
  1749. */
  1750. int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1751. {
  1752. unsigned long long msr;
  1753. int cpu;
  1754. cpu = t->cpu_id;
  1755. /* per-package */
  1756. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1757. return 0;
  1758. if (cpu_migrate(cpu)) {
  1759. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1760. return -1;
  1761. }
  1762. if (do_core_perf_limit_reasons) {
  1763. get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
  1764. fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
  1765. fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
  1766. (msr & 1 << 15) ? "bit15, " : "",
  1767. (msr & 1 << 14) ? "bit14, " : "",
  1768. (msr & 1 << 13) ? "Transitions, " : "",
  1769. (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
  1770. (msr & 1 << 11) ? "PkgPwrL2, " : "",
  1771. (msr & 1 << 10) ? "PkgPwrL1, " : "",
  1772. (msr & 1 << 9) ? "CorePwr, " : "",
  1773. (msr & 1 << 8) ? "Amps, " : "",
  1774. (msr & 1 << 6) ? "VR-Therm, " : "",
  1775. (msr & 1 << 5) ? "Auto-HWP, " : "",
  1776. (msr & 1 << 4) ? "Graphics, " : "",
  1777. (msr & 1 << 2) ? "bit2, " : "",
  1778. (msr & 1 << 1) ? "ThermStatus, " : "",
  1779. (msr & 1 << 0) ? "PROCHOT, " : "");
  1780. fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
  1781. (msr & 1 << 31) ? "bit31, " : "",
  1782. (msr & 1 << 30) ? "bit30, " : "",
  1783. (msr & 1 << 29) ? "Transitions, " : "",
  1784. (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
  1785. (msr & 1 << 27) ? "PkgPwrL2, " : "",
  1786. (msr & 1 << 26) ? "PkgPwrL1, " : "",
  1787. (msr & 1 << 25) ? "CorePwr, " : "",
  1788. (msr & 1 << 24) ? "Amps, " : "",
  1789. (msr & 1 << 22) ? "VR-Therm, " : "",
  1790. (msr & 1 << 21) ? "Auto-HWP, " : "",
  1791. (msr & 1 << 20) ? "Graphics, " : "",
  1792. (msr & 1 << 18) ? "bit18, " : "",
  1793. (msr & 1 << 17) ? "ThermStatus, " : "",
  1794. (msr & 1 << 16) ? "PROCHOT, " : "");
  1795. }
  1796. if (do_gfx_perf_limit_reasons) {
  1797. get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
  1798. fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
  1799. fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
  1800. (msr & 1 << 0) ? "PROCHOT, " : "",
  1801. (msr & 1 << 1) ? "ThermStatus, " : "",
  1802. (msr & 1 << 4) ? "Graphics, " : "",
  1803. (msr & 1 << 6) ? "VR-Therm, " : "",
  1804. (msr & 1 << 8) ? "Amps, " : "",
  1805. (msr & 1 << 9) ? "GFXPwr, " : "",
  1806. (msr & 1 << 10) ? "PkgPwrL1, " : "",
  1807. (msr & 1 << 11) ? "PkgPwrL2, " : "");
  1808. fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
  1809. (msr & 1 << 16) ? "PROCHOT, " : "",
  1810. (msr & 1 << 17) ? "ThermStatus, " : "",
  1811. (msr & 1 << 20) ? "Graphics, " : "",
  1812. (msr & 1 << 22) ? "VR-Therm, " : "",
  1813. (msr & 1 << 24) ? "Amps, " : "",
  1814. (msr & 1 << 25) ? "GFXPwr, " : "",
  1815. (msr & 1 << 26) ? "PkgPwrL1, " : "",
  1816. (msr & 1 << 27) ? "PkgPwrL2, " : "");
  1817. }
  1818. if (do_ring_perf_limit_reasons) {
  1819. get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
  1820. fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
  1821. fprintf(stderr, " (Active: %s%s%s%s%s%s)",
  1822. (msr & 1 << 0) ? "PROCHOT, " : "",
  1823. (msr & 1 << 1) ? "ThermStatus, " : "",
  1824. (msr & 1 << 6) ? "VR-Therm, " : "",
  1825. (msr & 1 << 8) ? "Amps, " : "",
  1826. (msr & 1 << 10) ? "PkgPwrL1, " : "",
  1827. (msr & 1 << 11) ? "PkgPwrL2, " : "");
  1828. fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
  1829. (msr & 1 << 16) ? "PROCHOT, " : "",
  1830. (msr & 1 << 17) ? "ThermStatus, " : "",
  1831. (msr & 1 << 22) ? "VR-Therm, " : "",
  1832. (msr & 1 << 24) ? "Amps, " : "",
  1833. (msr & 1 << 26) ? "PkgPwrL1, " : "",
  1834. (msr & 1 << 27) ? "PkgPwrL2, " : "");
  1835. }
  1836. return 0;
  1837. }
  1838. #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
  1839. #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
  1840. double get_tdp(model)
  1841. {
  1842. unsigned long long msr;
  1843. if (do_rapl & RAPL_PKG_POWER_INFO)
  1844. if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
  1845. return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
  1846. switch (model) {
  1847. case 0x37:
  1848. case 0x4D:
  1849. return 30.0;
  1850. default:
  1851. return 135.0;
  1852. }
  1853. }
  1854. /*
  1855. * rapl_dram_energy_units_probe()
  1856. * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
  1857. */
  1858. static double
  1859. rapl_dram_energy_units_probe(int model, double rapl_energy_units)
  1860. {
  1861. /* only called for genuine_intel, family 6 */
  1862. switch (model) {
  1863. case 0x3F: /* HSX */
  1864. case 0x4F: /* BDX */
  1865. case 0x56: /* BDX-DE */
  1866. case 0x57: /* KNL */
  1867. return (rapl_dram_energy_units = 15.3 / 1000000);
  1868. default:
  1869. return (rapl_energy_units);
  1870. }
  1871. }
  1872. /*
  1873. * rapl_probe()
  1874. *
  1875. * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
  1876. */
  1877. void rapl_probe(unsigned int family, unsigned int model)
  1878. {
  1879. unsigned long long msr;
  1880. unsigned int time_unit;
  1881. double tdp;
  1882. if (!genuine_intel)
  1883. return;
  1884. if (family != 6)
  1885. return;
  1886. switch (model) {
  1887. case 0x2A:
  1888. case 0x3A:
  1889. case 0x3C: /* HSW */
  1890. case 0x45: /* HSW */
  1891. case 0x46: /* HSW */
  1892. case 0x3D: /* BDW */
  1893. case 0x47: /* BDW */
  1894. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
  1895. break;
  1896. case 0x4E: /* SKL */
  1897. case 0x5E: /* SKL */
  1898. do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
  1899. break;
  1900. case 0x3F: /* HSX */
  1901. case 0x4F: /* BDX */
  1902. case 0x56: /* BDX-DE */
  1903. case 0x57: /* KNL */
  1904. do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
  1905. break;
  1906. case 0x2D:
  1907. case 0x3E:
  1908. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
  1909. break;
  1910. case 0x37: /* BYT */
  1911. case 0x4D: /* AVN */
  1912. do_rapl = RAPL_PKG | RAPL_CORES ;
  1913. break;
  1914. default:
  1915. return;
  1916. }
  1917. /* units on package 0, verify later other packages match */
  1918. if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
  1919. return;
  1920. rapl_power_units = 1.0 / (1 << (msr & 0xF));
  1921. if (model == 0x37)
  1922. rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
  1923. else
  1924. rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
  1925. rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
  1926. time_unit = msr >> 16 & 0xF;
  1927. if (time_unit == 0)
  1928. time_unit = 0xA;
  1929. rapl_time_units = 1.0 / (1 << (time_unit));
  1930. tdp = get_tdp(model);
  1931. rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
  1932. if (debug)
  1933. fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
  1934. return;
  1935. }
  1936. void perf_limit_reasons_probe(family, model)
  1937. {
  1938. if (!genuine_intel)
  1939. return;
  1940. if (family != 6)
  1941. return;
  1942. switch (model) {
  1943. case 0x3C: /* HSW */
  1944. case 0x45: /* HSW */
  1945. case 0x46: /* HSW */
  1946. do_gfx_perf_limit_reasons = 1;
  1947. case 0x3F: /* HSX */
  1948. do_core_perf_limit_reasons = 1;
  1949. do_ring_perf_limit_reasons = 1;
  1950. default:
  1951. return;
  1952. }
  1953. }
  1954. int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1955. {
  1956. unsigned long long msr;
  1957. unsigned int dts;
  1958. int cpu;
  1959. if (!(do_dts || do_ptm))
  1960. return 0;
  1961. cpu = t->cpu_id;
  1962. /* DTS is per-core, no need to print for each thread */
  1963. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  1964. return 0;
  1965. if (cpu_migrate(cpu)) {
  1966. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1967. return -1;
  1968. }
  1969. if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
  1970. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  1971. return 0;
  1972. dts = (msr >> 16) & 0x7F;
  1973. fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
  1974. cpu, msr, tcc_activation_temp - dts);
  1975. #ifdef THERM_DEBUG
  1976. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
  1977. return 0;
  1978. dts = (msr >> 16) & 0x7F;
  1979. dts2 = (msr >> 8) & 0x7F;
  1980. fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  1981. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  1982. #endif
  1983. }
  1984. if (do_dts) {
  1985. unsigned int resolution;
  1986. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  1987. return 0;
  1988. dts = (msr >> 16) & 0x7F;
  1989. resolution = (msr >> 27) & 0xF;
  1990. fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
  1991. cpu, msr, tcc_activation_temp - dts, resolution);
  1992. #ifdef THERM_DEBUG
  1993. if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
  1994. return 0;
  1995. dts = (msr >> 16) & 0x7F;
  1996. dts2 = (msr >> 8) & 0x7F;
  1997. fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  1998. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  1999. #endif
  2000. }
  2001. return 0;
  2002. }
  2003. void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
  2004. {
  2005. fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
  2006. cpu, label,
  2007. ((msr >> 15) & 1) ? "EN" : "DIS",
  2008. ((msr >> 0) & 0x7FFF) * rapl_power_units,
  2009. (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
  2010. (((msr >> 16) & 1) ? "EN" : "DIS"));
  2011. return;
  2012. }
  2013. int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  2014. {
  2015. unsigned long long msr;
  2016. int cpu;
  2017. if (!do_rapl)
  2018. return 0;
  2019. /* RAPL counters are per package, so print only for 1st thread/package */
  2020. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  2021. return 0;
  2022. cpu = t->cpu_id;
  2023. if (cpu_migrate(cpu)) {
  2024. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  2025. return -1;
  2026. }
  2027. if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
  2028. return -1;
  2029. if (debug) {
  2030. fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
  2031. "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
  2032. rapl_power_units, rapl_energy_units, rapl_time_units);
  2033. }
  2034. if (do_rapl & RAPL_PKG_POWER_INFO) {
  2035. if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
  2036. return -5;
  2037. fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  2038. cpu, msr,
  2039. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  2040. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  2041. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  2042. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  2043. }
  2044. if (do_rapl & RAPL_PKG) {
  2045. if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
  2046. return -9;
  2047. fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
  2048. cpu, msr, (msr >> 63) & 1 ? "": "UN");
  2049. print_power_limit_msr(cpu, msr, "PKG Limit #1");
  2050. fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
  2051. cpu,
  2052. ((msr >> 47) & 1) ? "EN" : "DIS",
  2053. ((msr >> 32) & 0x7FFF) * rapl_power_units,
  2054. (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
  2055. ((msr >> 48) & 1) ? "EN" : "DIS");
  2056. }
  2057. if (do_rapl & RAPL_DRAM_POWER_INFO) {
  2058. if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
  2059. return -6;
  2060. fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  2061. cpu, msr,
  2062. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  2063. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  2064. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  2065. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  2066. }
  2067. if (do_rapl & RAPL_DRAM) {
  2068. if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
  2069. return -9;
  2070. fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
  2071. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  2072. print_power_limit_msr(cpu, msr, "DRAM Limit");
  2073. }
  2074. if (do_rapl & RAPL_CORE_POLICY) {
  2075. if (debug) {
  2076. if (get_msr(cpu, MSR_PP0_POLICY, &msr))
  2077. return -7;
  2078. fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
  2079. }
  2080. }
  2081. if (do_rapl & RAPL_CORES) {
  2082. if (debug) {
  2083. if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
  2084. return -9;
  2085. fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
  2086. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  2087. print_power_limit_msr(cpu, msr, "Cores Limit");
  2088. }
  2089. }
  2090. if (do_rapl & RAPL_GFX) {
  2091. if (debug) {
  2092. if (get_msr(cpu, MSR_PP1_POLICY, &msr))
  2093. return -8;
  2094. fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
  2095. if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
  2096. return -9;
  2097. fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
  2098. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  2099. print_power_limit_msr(cpu, msr, "GFX Limit");
  2100. }
  2101. }
  2102. return 0;
  2103. }
  2104. /*
  2105. * SNB adds support for additional MSRs:
  2106. *
  2107. * MSR_PKG_C7_RESIDENCY 0x000003fa
  2108. * MSR_CORE_C7_RESIDENCY 0x000003fe
  2109. * MSR_PKG_C2_RESIDENCY 0x0000060d
  2110. */
  2111. int has_snb_msrs(unsigned int family, unsigned int model)
  2112. {
  2113. if (!genuine_intel)
  2114. return 0;
  2115. switch (model) {
  2116. case 0x2A:
  2117. case 0x2D:
  2118. case 0x3A: /* IVB */
  2119. case 0x3E: /* IVB Xeon */
  2120. case 0x3C: /* HSW */
  2121. case 0x3F: /* HSW */
  2122. case 0x45: /* HSW */
  2123. case 0x46: /* HSW */
  2124. case 0x3D: /* BDW */
  2125. case 0x47: /* BDW */
  2126. case 0x4F: /* BDX */
  2127. case 0x56: /* BDX-DE */
  2128. case 0x4E: /* SKL */
  2129. case 0x5E: /* SKL */
  2130. return 1;
  2131. }
  2132. return 0;
  2133. }
  2134. /*
  2135. * HSW adds support for additional MSRs:
  2136. *
  2137. * MSR_PKG_C8_RESIDENCY 0x00000630
  2138. * MSR_PKG_C9_RESIDENCY 0x00000631
  2139. * MSR_PKG_C10_RESIDENCY 0x00000632
  2140. */
  2141. int has_hsw_msrs(unsigned int family, unsigned int model)
  2142. {
  2143. if (!genuine_intel)
  2144. return 0;
  2145. switch (model) {
  2146. case 0x45: /* HSW */
  2147. case 0x3D: /* BDW */
  2148. case 0x4E: /* SKL */
  2149. case 0x5E: /* SKL */
  2150. return 1;
  2151. }
  2152. return 0;
  2153. }
  2154. /*
  2155. * SKL adds support for additional MSRS:
  2156. *
  2157. * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
  2158. * MSR_PKG_ANY_CORE_C0_RES 0x00000659
  2159. * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
  2160. * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
  2161. */
  2162. int has_skl_msrs(unsigned int family, unsigned int model)
  2163. {
  2164. if (!genuine_intel)
  2165. return 0;
  2166. switch (model) {
  2167. case 0x4E: /* SKL */
  2168. case 0x5E: /* SKL */
  2169. return 1;
  2170. }
  2171. return 0;
  2172. }
  2173. int is_slm(unsigned int family, unsigned int model)
  2174. {
  2175. if (!genuine_intel)
  2176. return 0;
  2177. switch (model) {
  2178. case 0x37: /* BYT */
  2179. case 0x4D: /* AVN */
  2180. return 1;
  2181. }
  2182. return 0;
  2183. }
  2184. int is_knl(unsigned int family, unsigned int model)
  2185. {
  2186. if (!genuine_intel)
  2187. return 0;
  2188. switch (model) {
  2189. case 0x57: /* KNL */
  2190. return 1;
  2191. }
  2192. return 0;
  2193. }
  2194. unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
  2195. {
  2196. if (is_knl(family, model))
  2197. return 1024;
  2198. return 1;
  2199. }
  2200. #define SLM_BCLK_FREQS 5
  2201. double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
  2202. double slm_bclk(void)
  2203. {
  2204. unsigned long long msr = 3;
  2205. unsigned int i;
  2206. double freq;
  2207. if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
  2208. fprintf(stderr, "SLM BCLK: unknown\n");
  2209. i = msr & 0xf;
  2210. if (i >= SLM_BCLK_FREQS) {
  2211. fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
  2212. msr = 3;
  2213. }
  2214. freq = slm_freq_table[i];
  2215. fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
  2216. return freq;
  2217. }
  2218. double discover_bclk(unsigned int family, unsigned int model)
  2219. {
  2220. if (has_snb_msrs(family, model))
  2221. return 100.00;
  2222. else if (is_slm(family, model))
  2223. return slm_bclk();
  2224. else
  2225. return 133.33;
  2226. }
  2227. /*
  2228. * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
  2229. * the Thermal Control Circuit (TCC) activates.
  2230. * This is usually equal to tjMax.
  2231. *
  2232. * Older processors do not have this MSR, so there we guess,
  2233. * but also allow cmdline over-ride with -T.
  2234. *
  2235. * Several MSR temperature values are in units of degrees-C
  2236. * below this value, including the Digital Thermal Sensor (DTS),
  2237. * Package Thermal Management Sensor (PTM), and thermal event thresholds.
  2238. */
  2239. int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  2240. {
  2241. unsigned long long msr;
  2242. unsigned int target_c_local;
  2243. int cpu;
  2244. /* tcc_activation_temp is used only for dts or ptm */
  2245. if (!(do_dts || do_ptm))
  2246. return 0;
  2247. /* this is a per-package concept */
  2248. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  2249. return 0;
  2250. cpu = t->cpu_id;
  2251. if (cpu_migrate(cpu)) {
  2252. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  2253. return -1;
  2254. }
  2255. if (tcc_activation_temp_override != 0) {
  2256. tcc_activation_temp = tcc_activation_temp_override;
  2257. fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
  2258. cpu, tcc_activation_temp);
  2259. return 0;
  2260. }
  2261. /* Temperature Target MSR is Nehalem and newer only */
  2262. if (!do_nhm_platform_info)
  2263. goto guess;
  2264. if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
  2265. goto guess;
  2266. target_c_local = (msr >> 16) & 0xFF;
  2267. if (debug)
  2268. fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
  2269. cpu, msr, target_c_local);
  2270. if (!target_c_local)
  2271. goto guess;
  2272. tcc_activation_temp = target_c_local;
  2273. return 0;
  2274. guess:
  2275. tcc_activation_temp = TJMAX_DEFAULT;
  2276. fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
  2277. cpu, tcc_activation_temp);
  2278. return 0;
  2279. }
  2280. void process_cpuid()
  2281. {
  2282. unsigned int eax, ebx, ecx, edx, max_level;
  2283. unsigned int fms, family, model, stepping;
  2284. eax = ebx = ecx = edx = 0;
  2285. __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
  2286. if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
  2287. genuine_intel = 1;
  2288. if (debug)
  2289. fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
  2290. (char *)&ebx, (char *)&edx, (char *)&ecx);
  2291. __get_cpuid(1, &fms, &ebx, &ecx, &edx);
  2292. family = (fms >> 8) & 0xf;
  2293. model = (fms >> 4) & 0xf;
  2294. stepping = fms & 0xf;
  2295. if (family == 0xf)
  2296. family += (fms >> 20) & 0xff;
  2297. if (family >= 6)
  2298. model += ((fms >> 16) & 0xf) << 4;
  2299. if (debug)
  2300. fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
  2301. max_level, family, model, stepping, family, model, stepping);
  2302. if (!(edx & (1 << 5)))
  2303. errx(1, "CPUID: no MSR");
  2304. /*
  2305. * check max extended function levels of CPUID.
  2306. * This is needed to check for invariant TSC.
  2307. * This check is valid for both Intel and AMD.
  2308. */
  2309. ebx = ecx = edx = 0;
  2310. __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
  2311. if (max_level >= 0x80000007) {
  2312. /*
  2313. * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
  2314. * this check is valid for both Intel and AMD
  2315. */
  2316. __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  2317. has_invariant_tsc = edx & (1 << 8);
  2318. }
  2319. /*
  2320. * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
  2321. * this check is valid for both Intel and AMD
  2322. */
  2323. __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
  2324. has_aperf = ecx & (1 << 0);
  2325. do_dts = eax & (1 << 0);
  2326. do_ptm = eax & (1 << 6);
  2327. has_epb = ecx & (1 << 3);
  2328. if (debug)
  2329. fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
  2330. has_aperf ? "" : "No ",
  2331. do_dts ? "" : "No ",
  2332. do_ptm ? "" : "No ",
  2333. has_epb ? "" : "No ");
  2334. if (max_level > 0x15) {
  2335. unsigned int eax_crystal;
  2336. unsigned int ebx_tsc;
  2337. /*
  2338. * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
  2339. */
  2340. eax_crystal = ebx_tsc = crystal_hz = edx = 0;
  2341. __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
  2342. if (ebx_tsc != 0) {
  2343. if (debug && (ebx != 0))
  2344. fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
  2345. eax_crystal, ebx_tsc, crystal_hz);
  2346. if (crystal_hz == 0)
  2347. switch(model) {
  2348. case 0x4E: /* SKL */
  2349. case 0x5E: /* SKL */
  2350. crystal_hz = 24000000; /* 24 MHz */
  2351. break;
  2352. default:
  2353. crystal_hz = 0;
  2354. }
  2355. if (crystal_hz) {
  2356. tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
  2357. if (debug)
  2358. fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
  2359. tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
  2360. }
  2361. }
  2362. }
  2363. if (has_aperf)
  2364. aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
  2365. do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
  2366. do_snb_cstates = has_snb_msrs(family, model);
  2367. do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
  2368. do_pc3 = (pkg_cstate_limit >= PCL__3);
  2369. do_pc6 = (pkg_cstate_limit >= PCL__6);
  2370. do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
  2371. do_c8_c9_c10 = has_hsw_msrs(family, model);
  2372. do_skl_residency = has_skl_msrs(family, model);
  2373. do_slm_cstates = is_slm(family, model);
  2374. do_knl_cstates = is_knl(family, model);
  2375. rapl_probe(family, model);
  2376. perf_limit_reasons_probe(family, model);
  2377. if (debug)
  2378. dump_cstate_pstate_config_info();
  2379. if (has_skl_msrs(family, model))
  2380. calculate_tsc_tweak();
  2381. return;
  2382. }
  2383. void help()
  2384. {
  2385. fprintf(stderr,
  2386. "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
  2387. "\n"
  2388. "Turbostat forks the specified COMMAND and prints statistics\n"
  2389. "when COMMAND completes.\n"
  2390. "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
  2391. "to print statistics, until interrupted.\n"
  2392. "--debug run in \"debug\" mode\n"
  2393. "--interval sec Override default 5-second measurement interval\n"
  2394. "--help print this help message\n"
  2395. "--counter msr print 32-bit counter at address \"msr\"\n"
  2396. "--Counter msr print 64-bit Counter at address \"msr\"\n"
  2397. "--msr msr print 32-bit value at address \"msr\"\n"
  2398. "--MSR msr print 64-bit Value at address \"msr\"\n"
  2399. "--version print version information\n"
  2400. "\n"
  2401. "For more help, run \"man turbostat\"\n");
  2402. }
  2403. /*
  2404. * in /dev/cpu/ return success for names that are numbers
  2405. * ie. filter out ".", "..", "microcode".
  2406. */
  2407. int dir_filter(const struct dirent *dirp)
  2408. {
  2409. if (isdigit(dirp->d_name[0]))
  2410. return 1;
  2411. else
  2412. return 0;
  2413. }
  2414. int open_dev_cpu_msr(int dummy1)
  2415. {
  2416. return 0;
  2417. }
  2418. void topology_probe()
  2419. {
  2420. int i;
  2421. int max_core_id = 0;
  2422. int max_package_id = 0;
  2423. int max_siblings = 0;
  2424. struct cpu_topology {
  2425. int core_id;
  2426. int physical_package_id;
  2427. } *cpus;
  2428. /* Initialize num_cpus, max_cpu_num */
  2429. topo.num_cpus = 0;
  2430. topo.max_cpu_num = 0;
  2431. for_all_proc_cpus(count_cpus);
  2432. if (!summary_only && topo.num_cpus > 1)
  2433. show_cpu = 1;
  2434. if (debug > 1)
  2435. fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
  2436. cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
  2437. if (cpus == NULL)
  2438. err(1, "calloc cpus");
  2439. /*
  2440. * Allocate and initialize cpu_present_set
  2441. */
  2442. cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
  2443. if (cpu_present_set == NULL)
  2444. err(3, "CPU_ALLOC");
  2445. cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  2446. CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
  2447. for_all_proc_cpus(mark_cpu_present);
  2448. /*
  2449. * Allocate and initialize cpu_affinity_set
  2450. */
  2451. cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
  2452. if (cpu_affinity_set == NULL)
  2453. err(3, "CPU_ALLOC");
  2454. cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  2455. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  2456. /*
  2457. * For online cpus
  2458. * find max_core_id, max_package_id
  2459. */
  2460. for (i = 0; i <= topo.max_cpu_num; ++i) {
  2461. int siblings;
  2462. if (cpu_is_not_present(i)) {
  2463. if (debug > 1)
  2464. fprintf(stderr, "cpu%d NOT PRESENT\n", i);
  2465. continue;
  2466. }
  2467. cpus[i].core_id = get_core_id(i);
  2468. if (cpus[i].core_id > max_core_id)
  2469. max_core_id = cpus[i].core_id;
  2470. cpus[i].physical_package_id = get_physical_package_id(i);
  2471. if (cpus[i].physical_package_id > max_package_id)
  2472. max_package_id = cpus[i].physical_package_id;
  2473. siblings = get_num_ht_siblings(i);
  2474. if (siblings > max_siblings)
  2475. max_siblings = siblings;
  2476. if (debug > 1)
  2477. fprintf(stderr, "cpu %d pkg %d core %d\n",
  2478. i, cpus[i].physical_package_id, cpus[i].core_id);
  2479. }
  2480. topo.num_cores_per_pkg = max_core_id + 1;
  2481. if (debug > 1)
  2482. fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
  2483. max_core_id, topo.num_cores_per_pkg);
  2484. if (debug && !summary_only && topo.num_cores_per_pkg > 1)
  2485. show_core = 1;
  2486. topo.num_packages = max_package_id + 1;
  2487. if (debug > 1)
  2488. fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
  2489. max_package_id, topo.num_packages);
  2490. if (debug && !summary_only && topo.num_packages > 1)
  2491. show_pkg = 1;
  2492. topo.num_threads_per_core = max_siblings;
  2493. if (debug > 1)
  2494. fprintf(stderr, "max_siblings %d\n", max_siblings);
  2495. free(cpus);
  2496. }
  2497. void
  2498. allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
  2499. {
  2500. int i;
  2501. *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
  2502. topo.num_packages, sizeof(struct thread_data));
  2503. if (*t == NULL)
  2504. goto error;
  2505. for (i = 0; i < topo.num_threads_per_core *
  2506. topo.num_cores_per_pkg * topo.num_packages; i++)
  2507. (*t)[i].cpu_id = -1;
  2508. *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
  2509. sizeof(struct core_data));
  2510. if (*c == NULL)
  2511. goto error;
  2512. for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
  2513. (*c)[i].core_id = -1;
  2514. *p = calloc(topo.num_packages, sizeof(struct pkg_data));
  2515. if (*p == NULL)
  2516. goto error;
  2517. for (i = 0; i < topo.num_packages; i++)
  2518. (*p)[i].package_id = i;
  2519. return;
  2520. error:
  2521. err(1, "calloc counters");
  2522. }
  2523. /*
  2524. * init_counter()
  2525. *
  2526. * set cpu_id, core_num, pkg_num
  2527. * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
  2528. *
  2529. * increment topo.num_cores when 1st core in pkg seen
  2530. */
  2531. void init_counter(struct thread_data *thread_base, struct core_data *core_base,
  2532. struct pkg_data *pkg_base, int thread_num, int core_num,
  2533. int pkg_num, int cpu_id)
  2534. {
  2535. struct thread_data *t;
  2536. struct core_data *c;
  2537. struct pkg_data *p;
  2538. t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
  2539. c = GET_CORE(core_base, core_num, pkg_num);
  2540. p = GET_PKG(pkg_base, pkg_num);
  2541. t->cpu_id = cpu_id;
  2542. if (thread_num == 0) {
  2543. t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
  2544. if (cpu_is_first_core_in_package(cpu_id))
  2545. t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
  2546. }
  2547. c->core_id = core_num;
  2548. p->package_id = pkg_num;
  2549. }
  2550. int initialize_counters(int cpu_id)
  2551. {
  2552. int my_thread_id, my_core_id, my_package_id;
  2553. my_package_id = get_physical_package_id(cpu_id);
  2554. my_core_id = get_core_id(cpu_id);
  2555. my_thread_id = get_cpu_position_in_core(cpu_id);
  2556. if (!my_thread_id)
  2557. topo.num_cores++;
  2558. init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  2559. init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  2560. return 0;
  2561. }
  2562. void allocate_output_buffer()
  2563. {
  2564. output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
  2565. outp = output_buffer;
  2566. if (outp == NULL)
  2567. err(-1, "calloc output buffer");
  2568. }
  2569. void setup_all_buffers(void)
  2570. {
  2571. topology_probe();
  2572. allocate_counters(&thread_even, &core_even, &package_even);
  2573. allocate_counters(&thread_odd, &core_odd, &package_odd);
  2574. allocate_output_buffer();
  2575. for_all_proc_cpus(initialize_counters);
  2576. }
  2577. void set_base_cpu(void)
  2578. {
  2579. base_cpu = sched_getcpu();
  2580. if (base_cpu < 0)
  2581. err(-ENODEV, "No valid cpus found");
  2582. if (debug > 1)
  2583. fprintf(stderr, "base_cpu = %d\n", base_cpu);
  2584. }
  2585. void turbostat_init()
  2586. {
  2587. setup_all_buffers();
  2588. set_base_cpu();
  2589. check_dev_msr();
  2590. check_permissions();
  2591. process_cpuid();
  2592. if (debug)
  2593. for_all_cpus(print_epb, ODD_COUNTERS);
  2594. if (debug)
  2595. for_all_cpus(print_perf_limit, ODD_COUNTERS);
  2596. if (debug)
  2597. for_all_cpus(print_rapl, ODD_COUNTERS);
  2598. for_all_cpus(set_temperature_target, ODD_COUNTERS);
  2599. if (debug)
  2600. for_all_cpus(print_thermal, ODD_COUNTERS);
  2601. }
  2602. int fork_it(char **argv)
  2603. {
  2604. pid_t child_pid;
  2605. int status;
  2606. status = for_all_cpus(get_counters, EVEN_COUNTERS);
  2607. if (status)
  2608. exit(status);
  2609. /* clear affinity side-effect of get_counters() */
  2610. sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
  2611. gettimeofday(&tv_even, (struct timezone *)NULL);
  2612. child_pid = fork();
  2613. if (!child_pid) {
  2614. /* child */
  2615. execvp(argv[0], argv);
  2616. } else {
  2617. /* parent */
  2618. if (child_pid == -1)
  2619. err(1, "fork");
  2620. signal(SIGINT, SIG_IGN);
  2621. signal(SIGQUIT, SIG_IGN);
  2622. if (waitpid(child_pid, &status, 0) == -1)
  2623. err(status, "waitpid");
  2624. if (WIFEXITED(status))
  2625. status = WEXITSTATUS(status);
  2626. }
  2627. /*
  2628. * n.b. fork_it() does not check for errors from for_all_cpus()
  2629. * because re-starting is problematic when forking
  2630. */
  2631. for_all_cpus(get_counters, ODD_COUNTERS);
  2632. gettimeofday(&tv_odd, (struct timezone *)NULL);
  2633. timersub(&tv_odd, &tv_even, &tv_delta);
  2634. for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
  2635. compute_average(EVEN_COUNTERS);
  2636. format_all_counters(EVEN_COUNTERS);
  2637. flush_stderr();
  2638. fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
  2639. return status;
  2640. }
  2641. int get_and_dump_counters(void)
  2642. {
  2643. int status;
  2644. status = for_all_cpus(get_counters, ODD_COUNTERS);
  2645. if (status)
  2646. return status;
  2647. status = for_all_cpus(dump_counters, ODD_COUNTERS);
  2648. if (status)
  2649. return status;
  2650. flush_stdout();
  2651. return status;
  2652. }
  2653. void print_version() {
  2654. fprintf(stderr, "turbostat version 4.8 26-Sep, 2015"
  2655. " - Len Brown <lenb@kernel.org>\n");
  2656. }
  2657. void cmdline(int argc, char **argv)
  2658. {
  2659. int opt;
  2660. int option_index = 0;
  2661. static struct option long_options[] = {
  2662. {"Counter", required_argument, 0, 'C'},
  2663. {"counter", required_argument, 0, 'c'},
  2664. {"Dump", no_argument, 0, 'D'},
  2665. {"debug", no_argument, 0, 'd'},
  2666. {"interval", required_argument, 0, 'i'},
  2667. {"help", no_argument, 0, 'h'},
  2668. {"Joules", no_argument, 0, 'J'},
  2669. {"MSR", required_argument, 0, 'M'},
  2670. {"msr", required_argument, 0, 'm'},
  2671. {"Package", no_argument, 0, 'p'},
  2672. {"processor", no_argument, 0, 'p'},
  2673. {"Summary", no_argument, 0, 'S'},
  2674. {"TCC", required_argument, 0, 'T'},
  2675. {"version", no_argument, 0, 'v' },
  2676. {0, 0, 0, 0 }
  2677. };
  2678. progname = argv[0];
  2679. while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v",
  2680. long_options, &option_index)) != -1) {
  2681. switch (opt) {
  2682. case 'C':
  2683. sscanf(optarg, "%x", &extra_delta_offset64);
  2684. break;
  2685. case 'c':
  2686. sscanf(optarg, "%x", &extra_delta_offset32);
  2687. break;
  2688. case 'D':
  2689. dump_only++;
  2690. break;
  2691. case 'd':
  2692. debug++;
  2693. break;
  2694. case 'h':
  2695. default:
  2696. help();
  2697. exit(1);
  2698. case 'i':
  2699. interval_sec = atoi(optarg);
  2700. break;
  2701. case 'J':
  2702. rapl_joules++;
  2703. break;
  2704. case 'M':
  2705. sscanf(optarg, "%x", &extra_msr_offset64);
  2706. break;
  2707. case 'm':
  2708. sscanf(optarg, "%x", &extra_msr_offset32);
  2709. break;
  2710. case 'P':
  2711. show_pkg_only++;
  2712. break;
  2713. case 'p':
  2714. show_core_only++;
  2715. break;
  2716. case 'S':
  2717. summary_only++;
  2718. break;
  2719. case 'T':
  2720. tcc_activation_temp_override = atoi(optarg);
  2721. break;
  2722. case 'v':
  2723. print_version();
  2724. exit(0);
  2725. break;
  2726. }
  2727. }
  2728. }
  2729. int main(int argc, char **argv)
  2730. {
  2731. cmdline(argc, argv);
  2732. if (debug)
  2733. print_version();
  2734. turbostat_init();
  2735. /* dump counters and exit */
  2736. if (dump_only)
  2737. return get_and_dump_counters();
  2738. /*
  2739. * if any params left, it must be a command to fork
  2740. */
  2741. if (argc - optind)
  2742. return fork_it(argv + optind);
  2743. else
  2744. turbostat_loop();
  2745. return 0;
  2746. }