turbostat.c 97 KB

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