turbostat.c 94 KB

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