turbostat.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124
  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 INTEL_FAMILY_HEADER
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <err.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <sys/stat.h>
  31. #include <sys/resource.h>
  32. #include <fcntl.h>
  33. #include <signal.h>
  34. #include <sys/time.h>
  35. #include <stdlib.h>
  36. #include <getopt.h>
  37. #include <dirent.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include <sched.h>
  41. #include <time.h>
  42. #include <cpuid.h>
  43. #include <linux/capability.h>
  44. #include <errno.h>
  45. char *proc_stat = "/proc/stat";
  46. FILE *outf;
  47. int *fd_percpu;
  48. struct timespec interval_ts = {5, 0};
  49. unsigned int debug;
  50. unsigned int quiet;
  51. unsigned int sums_need_wide_columns;
  52. unsigned int rapl_joules;
  53. unsigned int summary_only;
  54. unsigned int list_header_only;
  55. unsigned int dump_only;
  56. unsigned int do_snb_cstates;
  57. unsigned int do_knl_cstates;
  58. unsigned int do_slm_cstates;
  59. unsigned int use_c1_residency_msr;
  60. unsigned int has_aperf;
  61. unsigned int has_epb;
  62. unsigned int do_irtl_snb;
  63. unsigned int do_irtl_hsw;
  64. unsigned int units = 1000000; /* MHz etc */
  65. unsigned int genuine_intel;
  66. unsigned int has_invariant_tsc;
  67. unsigned int do_nhm_platform_info;
  68. unsigned int no_MSR_MISC_PWR_MGMT;
  69. unsigned int aperf_mperf_multiplier = 1;
  70. double bclk;
  71. double base_hz;
  72. unsigned int has_base_hz;
  73. double tsc_tweak = 1.0;
  74. unsigned int show_pkg_only;
  75. unsigned int show_core_only;
  76. char *output_buffer, *outp;
  77. unsigned int do_rapl;
  78. unsigned int do_dts;
  79. unsigned int do_ptm;
  80. unsigned long long gfx_cur_rc6_ms;
  81. unsigned int gfx_cur_mhz;
  82. unsigned int tcc_activation_temp;
  83. unsigned int tcc_activation_temp_override;
  84. double rapl_power_units, rapl_time_units;
  85. double rapl_dram_energy_units, rapl_energy_units;
  86. double rapl_joule_counter_range;
  87. unsigned int do_core_perf_limit_reasons;
  88. unsigned int do_gfx_perf_limit_reasons;
  89. unsigned int do_ring_perf_limit_reasons;
  90. unsigned int crystal_hz;
  91. unsigned long long tsc_hz;
  92. int base_cpu;
  93. int do_migrate;
  94. double discover_bclk(unsigned int family, unsigned int model);
  95. unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
  96. /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
  97. unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
  98. unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
  99. unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
  100. unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
  101. unsigned int has_misc_feature_control;
  102. #define RAPL_PKG (1 << 0)
  103. /* 0x610 MSR_PKG_POWER_LIMIT */
  104. /* 0x611 MSR_PKG_ENERGY_STATUS */
  105. #define RAPL_PKG_PERF_STATUS (1 << 1)
  106. /* 0x613 MSR_PKG_PERF_STATUS */
  107. #define RAPL_PKG_POWER_INFO (1 << 2)
  108. /* 0x614 MSR_PKG_POWER_INFO */
  109. #define RAPL_DRAM (1 << 3)
  110. /* 0x618 MSR_DRAM_POWER_LIMIT */
  111. /* 0x619 MSR_DRAM_ENERGY_STATUS */
  112. #define RAPL_DRAM_PERF_STATUS (1 << 4)
  113. /* 0x61b MSR_DRAM_PERF_STATUS */
  114. #define RAPL_DRAM_POWER_INFO (1 << 5)
  115. /* 0x61c MSR_DRAM_POWER_INFO */
  116. #define RAPL_CORES_POWER_LIMIT (1 << 6)
  117. /* 0x638 MSR_PP0_POWER_LIMIT */
  118. #define RAPL_CORE_POLICY (1 << 7)
  119. /* 0x63a MSR_PP0_POLICY */
  120. #define RAPL_GFX (1 << 8)
  121. /* 0x640 MSR_PP1_POWER_LIMIT */
  122. /* 0x641 MSR_PP1_ENERGY_STATUS */
  123. /* 0x642 MSR_PP1_POLICY */
  124. #define RAPL_CORES_ENERGY_STATUS (1 << 9)
  125. /* 0x639 MSR_PP0_ENERGY_STATUS */
  126. #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
  127. #define TJMAX_DEFAULT 100
  128. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  129. /*
  130. * buffer size used by sscanf() for added column names
  131. * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
  132. */
  133. #define NAME_BYTES 20
  134. #define PATH_BYTES 128
  135. int backwards_count;
  136. char *progname;
  137. #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
  138. cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
  139. size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
  140. #define MAX_ADDED_COUNTERS 16
  141. struct thread_data {
  142. struct timeval tv_begin;
  143. struct timeval tv_end;
  144. unsigned long long tsc;
  145. unsigned long long aperf;
  146. unsigned long long mperf;
  147. unsigned long long c1;
  148. unsigned long long irq_count;
  149. unsigned int smi_count;
  150. unsigned int cpu_id;
  151. unsigned int flags;
  152. #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
  153. #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
  154. unsigned long long counter[MAX_ADDED_COUNTERS];
  155. } *thread_even, *thread_odd;
  156. struct core_data {
  157. unsigned long long c3;
  158. unsigned long long c6;
  159. unsigned long long c7;
  160. unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
  161. unsigned int core_temp_c;
  162. unsigned int core_id;
  163. unsigned long long counter[MAX_ADDED_COUNTERS];
  164. } *core_even, *core_odd;
  165. struct pkg_data {
  166. unsigned long long pc2;
  167. unsigned long long pc3;
  168. unsigned long long pc6;
  169. unsigned long long pc7;
  170. unsigned long long pc8;
  171. unsigned long long pc9;
  172. unsigned long long pc10;
  173. unsigned long long pkg_wtd_core_c0;
  174. unsigned long long pkg_any_core_c0;
  175. unsigned long long pkg_any_gfxe_c0;
  176. unsigned long long pkg_both_core_gfxe_c0;
  177. long long gfx_rc6_ms;
  178. unsigned int gfx_mhz;
  179. unsigned int package_id;
  180. unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
  181. unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
  182. unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
  183. unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
  184. unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
  185. unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
  186. unsigned int pkg_temp_c;
  187. unsigned long long counter[MAX_ADDED_COUNTERS];
  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. enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
  199. enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
  200. enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
  201. struct msr_counter {
  202. unsigned int msr_num;
  203. char name[NAME_BYTES];
  204. char path[PATH_BYTES];
  205. unsigned int width;
  206. enum counter_type type;
  207. enum counter_format format;
  208. struct msr_counter *next;
  209. unsigned int flags;
  210. #define FLAGS_HIDE (1 << 0)
  211. #define FLAGS_SHOW (1 << 1)
  212. #define SYSFS_PERCPU (1 << 1)
  213. };
  214. struct sys_counters {
  215. unsigned int added_thread_counters;
  216. unsigned int added_core_counters;
  217. unsigned int added_package_counters;
  218. struct msr_counter *tp;
  219. struct msr_counter *cp;
  220. struct msr_counter *pp;
  221. } sys;
  222. struct system_summary {
  223. struct thread_data threads;
  224. struct core_data cores;
  225. struct pkg_data packages;
  226. } average;
  227. struct topo_params {
  228. int num_packages;
  229. int num_cpus;
  230. int num_cores;
  231. int max_cpu_num;
  232. int num_cores_per_pkg;
  233. int num_threads_per_core;
  234. } topo;
  235. struct timeval tv_even, tv_odd, tv_delta;
  236. int *irq_column_2_cpu; /* /proc/interrupts column numbers */
  237. int *irqs_per_cpu; /* indexed by cpu_num */
  238. void setup_all_buffers(void);
  239. int cpu_is_not_present(int cpu)
  240. {
  241. return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
  242. }
  243. /*
  244. * run func(thread, core, package) in topology order
  245. * skip non-present cpus
  246. */
  247. int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
  248. struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
  249. {
  250. int retval, pkg_no, core_no, thread_no;
  251. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  252. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  253. for (thread_no = 0; thread_no <
  254. topo.num_threads_per_core; ++thread_no) {
  255. struct thread_data *t;
  256. struct core_data *c;
  257. struct pkg_data *p;
  258. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  259. if (cpu_is_not_present(t->cpu_id))
  260. continue;
  261. c = GET_CORE(core_base, core_no, pkg_no);
  262. p = GET_PKG(pkg_base, pkg_no);
  263. retval = func(t, c, p);
  264. if (retval)
  265. return retval;
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. int cpu_migrate(int cpu)
  272. {
  273. if (!do_migrate)
  274. return 0;
  275. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  276. CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
  277. if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
  278. return -1;
  279. else
  280. return 0;
  281. }
  282. int get_msr_fd(int cpu)
  283. {
  284. char pathname[32];
  285. int fd;
  286. fd = fd_percpu[cpu];
  287. if (fd)
  288. return fd;
  289. sprintf(pathname, "/dev/cpu/%d/msr", cpu);
  290. fd = open(pathname, O_RDONLY);
  291. if (fd < 0)
  292. err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
  293. fd_percpu[cpu] = fd;
  294. return fd;
  295. }
  296. int get_msr(int cpu, off_t offset, unsigned long long *msr)
  297. {
  298. ssize_t retval;
  299. retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
  300. if (retval != sizeof *msr)
  301. err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
  302. return 0;
  303. }
  304. /*
  305. * Each string in this array is compared in --show and --hide cmdline.
  306. * Thus, strings that are proper sub-sets must follow their more specific peers.
  307. */
  308. struct msr_counter bic[] = {
  309. { 0x0, "Package" },
  310. { 0x0, "Avg_MHz" },
  311. { 0x0, "Bzy_MHz" },
  312. { 0x0, "TSC_MHz" },
  313. { 0x0, "IRQ" },
  314. { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
  315. { 0x0, "Busy%" },
  316. { 0x0, "CPU%c1" },
  317. { 0x0, "CPU%c3" },
  318. { 0x0, "CPU%c6" },
  319. { 0x0, "CPU%c7" },
  320. { 0x0, "ThreadC" },
  321. { 0x0, "CoreTmp" },
  322. { 0x0, "CoreCnt" },
  323. { 0x0, "PkgTmp" },
  324. { 0x0, "GFX%rc6" },
  325. { 0x0, "GFXMHz" },
  326. { 0x0, "Pkg%pc2" },
  327. { 0x0, "Pkg%pc3" },
  328. { 0x0, "Pkg%pc6" },
  329. { 0x0, "Pkg%pc7" },
  330. { 0x0, "Pkg%pc8" },
  331. { 0x0, "Pkg%pc9" },
  332. { 0x0, "Pkg%pc10" },
  333. { 0x0, "PkgWatt" },
  334. { 0x0, "CorWatt" },
  335. { 0x0, "GFXWatt" },
  336. { 0x0, "PkgCnt" },
  337. { 0x0, "RAMWatt" },
  338. { 0x0, "PKG_%" },
  339. { 0x0, "RAM_%" },
  340. { 0x0, "Pkg_J" },
  341. { 0x0, "Cor_J" },
  342. { 0x0, "GFX_J" },
  343. { 0x0, "RAM_J" },
  344. { 0x0, "Core" },
  345. { 0x0, "CPU" },
  346. { 0x0, "Mod%c6" },
  347. { 0x0, "sysfs" },
  348. { 0x0, "Totl%C0" },
  349. { 0x0, "Any%C0" },
  350. { 0x0, "GFX%C0" },
  351. { 0x0, "CPUGFX%" },
  352. };
  353. #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
  354. #define BIC_Package (1ULL << 0)
  355. #define BIC_Avg_MHz (1ULL << 1)
  356. #define BIC_Bzy_MHz (1ULL << 2)
  357. #define BIC_TSC_MHz (1ULL << 3)
  358. #define BIC_IRQ (1ULL << 4)
  359. #define BIC_SMI (1ULL << 5)
  360. #define BIC_Busy (1ULL << 6)
  361. #define BIC_CPU_c1 (1ULL << 7)
  362. #define BIC_CPU_c3 (1ULL << 8)
  363. #define BIC_CPU_c6 (1ULL << 9)
  364. #define BIC_CPU_c7 (1ULL << 10)
  365. #define BIC_ThreadC (1ULL << 11)
  366. #define BIC_CoreTmp (1ULL << 12)
  367. #define BIC_CoreCnt (1ULL << 13)
  368. #define BIC_PkgTmp (1ULL << 14)
  369. #define BIC_GFX_rc6 (1ULL << 15)
  370. #define BIC_GFXMHz (1ULL << 16)
  371. #define BIC_Pkgpc2 (1ULL << 17)
  372. #define BIC_Pkgpc3 (1ULL << 18)
  373. #define BIC_Pkgpc6 (1ULL << 19)
  374. #define BIC_Pkgpc7 (1ULL << 20)
  375. #define BIC_Pkgpc8 (1ULL << 21)
  376. #define BIC_Pkgpc9 (1ULL << 22)
  377. #define BIC_Pkgpc10 (1ULL << 23)
  378. #define BIC_PkgWatt (1ULL << 24)
  379. #define BIC_CorWatt (1ULL << 25)
  380. #define BIC_GFXWatt (1ULL << 26)
  381. #define BIC_PkgCnt (1ULL << 27)
  382. #define BIC_RAMWatt (1ULL << 28)
  383. #define BIC_PKG__ (1ULL << 29)
  384. #define BIC_RAM__ (1ULL << 30)
  385. #define BIC_Pkg_J (1ULL << 31)
  386. #define BIC_Cor_J (1ULL << 32)
  387. #define BIC_GFX_J (1ULL << 33)
  388. #define BIC_RAM_J (1ULL << 34)
  389. #define BIC_Core (1ULL << 35)
  390. #define BIC_CPU (1ULL << 36)
  391. #define BIC_Mod_c6 (1ULL << 37)
  392. #define BIC_sysfs (1ULL << 38)
  393. #define BIC_Totl_c0 (1ULL << 39)
  394. #define BIC_Any_c0 (1ULL << 40)
  395. #define BIC_GFX_c0 (1ULL << 41)
  396. #define BIC_CPUGFX (1ULL << 42)
  397. unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
  398. unsigned long long bic_present = BIC_sysfs;
  399. #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
  400. #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
  401. #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
  402. #define MAX_DEFERRED 16
  403. char *deferred_skip_names[MAX_DEFERRED];
  404. int deferred_skip_index;
  405. /*
  406. * HIDE_LIST - hide this list of counters, show the rest [default]
  407. * SHOW_LIST - show this list of counters, hide the rest
  408. */
  409. enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
  410. void help(void)
  411. {
  412. fprintf(outf,
  413. "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
  414. "\n"
  415. "Turbostat forks the specified COMMAND and prints statistics\n"
  416. "when COMMAND completes.\n"
  417. "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
  418. "to print statistics, until interrupted.\n"
  419. "--add add a counter\n"
  420. " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
  421. "--cpu cpu-set limit output to summary plus cpu-set:\n"
  422. " {core | package | j,k,l..m,n-p }\n"
  423. "--quiet skip decoding system configuration header\n"
  424. "--interval sec Override default 5-second measurement interval\n"
  425. "--help print this help message\n"
  426. "--list list column headers only\n"
  427. "--out file create or truncate \"file\" for all output\n"
  428. "--version print version information\n"
  429. "\n"
  430. "For more help, run \"man turbostat\"\n");
  431. }
  432. /*
  433. * bic_lookup
  434. * for all the strings in comma separate name_list,
  435. * set the approprate bit in return value.
  436. */
  437. unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
  438. {
  439. int i;
  440. unsigned long long retval = 0;
  441. while (name_list) {
  442. char *comma;
  443. comma = strchr(name_list, ',');
  444. if (comma)
  445. *comma = '\0';
  446. for (i = 0; i < MAX_BIC; ++i) {
  447. if (!strcmp(name_list, bic[i].name)) {
  448. retval |= (1ULL << i);
  449. break;
  450. }
  451. }
  452. if (i == MAX_BIC) {
  453. if (mode == SHOW_LIST) {
  454. fprintf(stderr, "Invalid counter name: %s\n", name_list);
  455. exit(-1);
  456. }
  457. deferred_skip_names[deferred_skip_index++] = name_list;
  458. if (debug)
  459. fprintf(stderr, "deferred \"%s\"\n", name_list);
  460. if (deferred_skip_index >= MAX_DEFERRED) {
  461. fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
  462. MAX_DEFERRED, name_list);
  463. help();
  464. exit(1);
  465. }
  466. }
  467. name_list = comma;
  468. if (name_list)
  469. name_list++;
  470. }
  471. return retval;
  472. }
  473. void print_header(char *delim)
  474. {
  475. struct msr_counter *mp;
  476. int printed = 0;
  477. if (debug)
  478. outp += sprintf(outp, "usec %s", delim);
  479. if (DO_BIC(BIC_Package))
  480. outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
  481. if (DO_BIC(BIC_Core))
  482. outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
  483. if (DO_BIC(BIC_CPU))
  484. outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
  485. if (DO_BIC(BIC_Avg_MHz))
  486. outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
  487. if (DO_BIC(BIC_Busy))
  488. outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
  489. if (DO_BIC(BIC_Bzy_MHz))
  490. outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
  491. if (DO_BIC(BIC_TSC_MHz))
  492. outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
  493. if (DO_BIC(BIC_IRQ)) {
  494. if (sums_need_wide_columns)
  495. outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
  496. else
  497. outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
  498. }
  499. if (DO_BIC(BIC_SMI))
  500. outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
  501. for (mp = sys.tp; mp; mp = mp->next) {
  502. if (mp->format == FORMAT_RAW) {
  503. if (mp->width == 64)
  504. outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
  505. else
  506. outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
  507. } else {
  508. if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
  509. outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
  510. else
  511. outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
  512. }
  513. }
  514. if (DO_BIC(BIC_CPU_c1))
  515. outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
  516. if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
  517. outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
  518. if (DO_BIC(BIC_CPU_c6))
  519. outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
  520. if (DO_BIC(BIC_CPU_c7))
  521. outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
  522. if (DO_BIC(BIC_Mod_c6))
  523. outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
  524. if (DO_BIC(BIC_CoreTmp))
  525. outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
  526. for (mp = sys.cp; mp; mp = mp->next) {
  527. if (mp->format == FORMAT_RAW) {
  528. if (mp->width == 64)
  529. outp += sprintf(outp, "%s%18.18s", delim, mp->name);
  530. else
  531. outp += sprintf(outp, "%s%10.10s", delim, mp->name);
  532. } else {
  533. if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
  534. outp += sprintf(outp, "%s%8s", delim, mp->name);
  535. else
  536. outp += sprintf(outp, "%s%s", delim, mp->name);
  537. }
  538. }
  539. if (DO_BIC(BIC_PkgTmp))
  540. outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
  541. if (DO_BIC(BIC_GFX_rc6))
  542. outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
  543. if (DO_BIC(BIC_GFXMHz))
  544. outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
  545. if (DO_BIC(BIC_Totl_c0))
  546. outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
  547. if (DO_BIC(BIC_Any_c0))
  548. outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
  549. if (DO_BIC(BIC_GFX_c0))
  550. outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
  551. if (DO_BIC(BIC_CPUGFX))
  552. outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
  553. if (DO_BIC(BIC_Pkgpc2))
  554. outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
  555. if (DO_BIC(BIC_Pkgpc3))
  556. outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
  557. if (DO_BIC(BIC_Pkgpc6))
  558. outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
  559. if (DO_BIC(BIC_Pkgpc7))
  560. outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
  561. if (DO_BIC(BIC_Pkgpc8))
  562. outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
  563. if (DO_BIC(BIC_Pkgpc9))
  564. outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
  565. if (DO_BIC(BIC_Pkgpc10))
  566. outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
  567. if (do_rapl && !rapl_joules) {
  568. if (DO_BIC(BIC_PkgWatt))
  569. outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
  570. if (DO_BIC(BIC_CorWatt))
  571. outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
  572. if (DO_BIC(BIC_GFXWatt))
  573. outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
  574. if (DO_BIC(BIC_RAMWatt))
  575. outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
  576. if (DO_BIC(BIC_PKG__))
  577. outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
  578. if (DO_BIC(BIC_RAM__))
  579. outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
  580. } else if (do_rapl && rapl_joules) {
  581. if (DO_BIC(BIC_Pkg_J))
  582. outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
  583. if (DO_BIC(BIC_Cor_J))
  584. outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
  585. if (DO_BIC(BIC_GFX_J))
  586. outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
  587. if (DO_BIC(BIC_RAM_J))
  588. outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
  589. if (DO_BIC(BIC_PKG__))
  590. outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
  591. if (DO_BIC(BIC_RAM__))
  592. outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
  593. }
  594. for (mp = sys.pp; mp; mp = mp->next) {
  595. if (mp->format == FORMAT_RAW) {
  596. if (mp->width == 64)
  597. outp += sprintf(outp, "%s%18.18s", delim, mp->name);
  598. else
  599. outp += sprintf(outp, "%s%10.10s", delim, mp->name);
  600. } else {
  601. if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
  602. outp += sprintf(outp, "%s%8s", delim, mp->name);
  603. else
  604. outp += sprintf(outp, "%s%s", delim, mp->name);
  605. }
  606. }
  607. outp += sprintf(outp, "\n");
  608. }
  609. int dump_counters(struct thread_data *t, struct core_data *c,
  610. struct pkg_data *p)
  611. {
  612. int i;
  613. struct msr_counter *mp;
  614. outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
  615. if (t) {
  616. outp += sprintf(outp, "CPU: %d flags 0x%x\n",
  617. t->cpu_id, t->flags);
  618. outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
  619. outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
  620. outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
  621. outp += sprintf(outp, "c1: %016llX\n", t->c1);
  622. if (DO_BIC(BIC_IRQ))
  623. outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
  624. if (DO_BIC(BIC_SMI))
  625. outp += sprintf(outp, "SMI: %d\n", t->smi_count);
  626. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
  627. outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
  628. i, mp->msr_num, t->counter[i]);
  629. }
  630. }
  631. if (c) {
  632. outp += sprintf(outp, "core: %d\n", c->core_id);
  633. outp += sprintf(outp, "c3: %016llX\n", c->c3);
  634. outp += sprintf(outp, "c6: %016llX\n", c->c6);
  635. outp += sprintf(outp, "c7: %016llX\n", c->c7);
  636. outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
  637. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
  638. outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
  639. i, mp->msr_num, c->counter[i]);
  640. }
  641. outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
  642. }
  643. if (p) {
  644. outp += sprintf(outp, "package: %d\n", p->package_id);
  645. outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
  646. outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
  647. outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
  648. outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
  649. outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
  650. if (DO_BIC(BIC_Pkgpc3))
  651. outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
  652. if (DO_BIC(BIC_Pkgpc6))
  653. outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
  654. if (DO_BIC(BIC_Pkgpc7))
  655. outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
  656. outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
  657. outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
  658. outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
  659. outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
  660. outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
  661. outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
  662. outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
  663. outp += sprintf(outp, "Throttle PKG: %0X\n",
  664. p->rapl_pkg_perf_status);
  665. outp += sprintf(outp, "Throttle RAM: %0X\n",
  666. p->rapl_dram_perf_status);
  667. outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
  668. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
  669. outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
  670. i, mp->msr_num, p->counter[i]);
  671. }
  672. }
  673. outp += sprintf(outp, "\n");
  674. return 0;
  675. }
  676. /*
  677. * column formatting convention & formats
  678. */
  679. int format_counters(struct thread_data *t, struct core_data *c,
  680. struct pkg_data *p)
  681. {
  682. double interval_float, tsc;
  683. char *fmt8;
  684. int i;
  685. struct msr_counter *mp;
  686. char *delim = "\t";
  687. int printed = 0;
  688. /* if showing only 1st thread in core and this isn't one, bail out */
  689. if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  690. return 0;
  691. /* if showing only 1st thread in pkg and this isn't one, bail out */
  692. if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  693. return 0;
  694. /*if not summary line and --cpu is used */
  695. if ((t != &average.threads) &&
  696. (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
  697. return 0;
  698. if (debug) {
  699. /* on each row, print how many usec each timestamp took to gather */
  700. struct timeval tv;
  701. timersub(&t->tv_end, &t->tv_begin, &tv);
  702. outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
  703. }
  704. interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
  705. tsc = t->tsc * tsc_tweak;
  706. /* topo columns, print blanks on 1st (average) line */
  707. if (t == &average.threads) {
  708. if (DO_BIC(BIC_Package))
  709. outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
  710. if (DO_BIC(BIC_Core))
  711. outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
  712. if (DO_BIC(BIC_CPU))
  713. outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
  714. } else {
  715. if (DO_BIC(BIC_Package)) {
  716. if (p)
  717. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
  718. else
  719. outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
  720. }
  721. if (DO_BIC(BIC_Core)) {
  722. if (c)
  723. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
  724. else
  725. outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
  726. }
  727. if (DO_BIC(BIC_CPU))
  728. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
  729. }
  730. if (DO_BIC(BIC_Avg_MHz))
  731. outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
  732. 1.0 / units * t->aperf / interval_float);
  733. if (DO_BIC(BIC_Busy))
  734. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
  735. if (DO_BIC(BIC_Bzy_MHz)) {
  736. if (has_base_hz)
  737. outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
  738. else
  739. outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
  740. tsc / units * t->aperf / t->mperf / interval_float);
  741. }
  742. if (DO_BIC(BIC_TSC_MHz))
  743. outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
  744. /* IRQ */
  745. if (DO_BIC(BIC_IRQ)) {
  746. if (sums_need_wide_columns)
  747. outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
  748. else
  749. outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
  750. }
  751. /* SMI */
  752. if (DO_BIC(BIC_SMI))
  753. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
  754. /* Added counters */
  755. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
  756. if (mp->format == FORMAT_RAW) {
  757. if (mp->width == 32)
  758. outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
  759. else
  760. outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
  761. } else if (mp->format == FORMAT_DELTA) {
  762. if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
  763. outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
  764. else
  765. outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
  766. } else if (mp->format == FORMAT_PERCENT) {
  767. if (mp->type == COUNTER_USEC)
  768. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
  769. else
  770. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
  771. }
  772. }
  773. /* C1 */
  774. if (DO_BIC(BIC_CPU_c1))
  775. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
  776. /* print per-core data only for 1st thread in core */
  777. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  778. goto done;
  779. if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
  780. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
  781. if (DO_BIC(BIC_CPU_c6))
  782. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
  783. if (DO_BIC(BIC_CPU_c7))
  784. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
  785. /* Mod%c6 */
  786. if (DO_BIC(BIC_Mod_c6))
  787. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
  788. if (DO_BIC(BIC_CoreTmp))
  789. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
  790. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
  791. if (mp->format == FORMAT_RAW) {
  792. if (mp->width == 32)
  793. outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
  794. else
  795. outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
  796. } else if (mp->format == FORMAT_DELTA) {
  797. if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
  798. outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
  799. else
  800. outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
  801. } else if (mp->format == FORMAT_PERCENT) {
  802. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
  803. }
  804. }
  805. /* print per-package data only for 1st core in package */
  806. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  807. goto done;
  808. /* PkgTmp */
  809. if (DO_BIC(BIC_PkgTmp))
  810. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
  811. /* GFXrc6 */
  812. if (DO_BIC(BIC_GFX_rc6)) {
  813. if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
  814. outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
  815. } else {
  816. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
  817. p->gfx_rc6_ms / 10.0 / interval_float);
  818. }
  819. }
  820. /* GFXMHz */
  821. if (DO_BIC(BIC_GFXMHz))
  822. outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
  823. /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
  824. if (DO_BIC(BIC_Totl_c0))
  825. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
  826. if (DO_BIC(BIC_Any_c0))
  827. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
  828. if (DO_BIC(BIC_GFX_c0))
  829. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
  830. if (DO_BIC(BIC_CPUGFX))
  831. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
  832. if (DO_BIC(BIC_Pkgpc2))
  833. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
  834. if (DO_BIC(BIC_Pkgpc3))
  835. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
  836. if (DO_BIC(BIC_Pkgpc6))
  837. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
  838. if (DO_BIC(BIC_Pkgpc7))
  839. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
  840. if (DO_BIC(BIC_Pkgpc8))
  841. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
  842. if (DO_BIC(BIC_Pkgpc9))
  843. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
  844. if (DO_BIC(BIC_Pkgpc10))
  845. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
  846. /*
  847. * If measurement interval exceeds minimum RAPL Joule Counter range,
  848. * indicate that results are suspect by printing "**" in fraction place.
  849. */
  850. if (interval_float < rapl_joule_counter_range)
  851. fmt8 = "%s%.2f";
  852. else
  853. fmt8 = "%6.0f**";
  854. if (DO_BIC(BIC_PkgWatt))
  855. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
  856. if (DO_BIC(BIC_CorWatt))
  857. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
  858. if (DO_BIC(BIC_GFXWatt))
  859. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
  860. if (DO_BIC(BIC_RAMWatt))
  861. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
  862. if (DO_BIC(BIC_Pkg_J))
  863. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
  864. if (DO_BIC(BIC_Cor_J))
  865. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
  866. if (DO_BIC(BIC_GFX_J))
  867. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
  868. if (DO_BIC(BIC_RAM_J))
  869. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
  870. if (DO_BIC(BIC_PKG__))
  871. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
  872. if (DO_BIC(BIC_RAM__))
  873. outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
  874. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
  875. if (mp->format == FORMAT_RAW) {
  876. if (mp->width == 32)
  877. outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
  878. else
  879. outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
  880. } else if (mp->format == FORMAT_DELTA) {
  881. if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
  882. outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
  883. else
  884. outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
  885. } else if (mp->format == FORMAT_PERCENT) {
  886. outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
  887. }
  888. }
  889. done:
  890. outp += sprintf(outp, "\n");
  891. return 0;
  892. }
  893. void flush_output_stdout(void)
  894. {
  895. FILE *filep;
  896. if (outf == stderr)
  897. filep = stdout;
  898. else
  899. filep = outf;
  900. fputs(output_buffer, filep);
  901. fflush(filep);
  902. outp = output_buffer;
  903. }
  904. void flush_output_stderr(void)
  905. {
  906. fputs(output_buffer, outf);
  907. fflush(outf);
  908. outp = output_buffer;
  909. }
  910. void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  911. {
  912. static int printed;
  913. if (!printed || !summary_only)
  914. print_header("\t");
  915. if (topo.num_cpus > 1)
  916. format_counters(&average.threads, &average.cores,
  917. &average.packages);
  918. printed = 1;
  919. if (summary_only)
  920. return;
  921. for_all_cpus(format_counters, t, c, p);
  922. }
  923. #define DELTA_WRAP32(new, old) \
  924. if (new > old) { \
  925. old = new - old; \
  926. } else { \
  927. old = 0x100000000 + new - old; \
  928. }
  929. int
  930. delta_package(struct pkg_data *new, struct pkg_data *old)
  931. {
  932. int i;
  933. struct msr_counter *mp;
  934. if (DO_BIC(BIC_Totl_c0))
  935. old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
  936. if (DO_BIC(BIC_Any_c0))
  937. old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
  938. if (DO_BIC(BIC_GFX_c0))
  939. old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
  940. if (DO_BIC(BIC_CPUGFX))
  941. old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
  942. old->pc2 = new->pc2 - old->pc2;
  943. if (DO_BIC(BIC_Pkgpc3))
  944. old->pc3 = new->pc3 - old->pc3;
  945. if (DO_BIC(BIC_Pkgpc6))
  946. old->pc6 = new->pc6 - old->pc6;
  947. if (DO_BIC(BIC_Pkgpc7))
  948. old->pc7 = new->pc7 - old->pc7;
  949. old->pc8 = new->pc8 - old->pc8;
  950. old->pc9 = new->pc9 - old->pc9;
  951. old->pc10 = new->pc10 - old->pc10;
  952. old->pkg_temp_c = new->pkg_temp_c;
  953. /* flag an error when rc6 counter resets/wraps */
  954. if (old->gfx_rc6_ms > new->gfx_rc6_ms)
  955. old->gfx_rc6_ms = -1;
  956. else
  957. old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
  958. old->gfx_mhz = new->gfx_mhz;
  959. DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
  960. DELTA_WRAP32(new->energy_cores, old->energy_cores);
  961. DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
  962. DELTA_WRAP32(new->energy_dram, old->energy_dram);
  963. DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
  964. DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
  965. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
  966. if (mp->format == FORMAT_RAW)
  967. old->counter[i] = new->counter[i];
  968. else
  969. old->counter[i] = new->counter[i] - old->counter[i];
  970. }
  971. return 0;
  972. }
  973. void
  974. delta_core(struct core_data *new, struct core_data *old)
  975. {
  976. int i;
  977. struct msr_counter *mp;
  978. old->c3 = new->c3 - old->c3;
  979. old->c6 = new->c6 - old->c6;
  980. old->c7 = new->c7 - old->c7;
  981. old->core_temp_c = new->core_temp_c;
  982. old->mc6_us = new->mc6_us - old->mc6_us;
  983. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
  984. if (mp->format == FORMAT_RAW)
  985. old->counter[i] = new->counter[i];
  986. else
  987. old->counter[i] = new->counter[i] - old->counter[i];
  988. }
  989. }
  990. /*
  991. * old = new - old
  992. */
  993. int
  994. delta_thread(struct thread_data *new, struct thread_data *old,
  995. struct core_data *core_delta)
  996. {
  997. int i;
  998. struct msr_counter *mp;
  999. old->tsc = new->tsc - old->tsc;
  1000. /* check for TSC < 1 Mcycles over interval */
  1001. if (old->tsc < (1000 * 1000))
  1002. errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
  1003. "You can disable all c-states by booting with \"idle=poll\"\n"
  1004. "or just the deep ones with \"processor.max_cstate=1\"");
  1005. old->c1 = new->c1 - old->c1;
  1006. if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
  1007. if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
  1008. old->aperf = new->aperf - old->aperf;
  1009. old->mperf = new->mperf - old->mperf;
  1010. } else {
  1011. return -1;
  1012. }
  1013. }
  1014. if (use_c1_residency_msr) {
  1015. /*
  1016. * Some models have a dedicated C1 residency MSR,
  1017. * which should be more accurate than the derivation below.
  1018. */
  1019. } else {
  1020. /*
  1021. * As counter collection is not atomic,
  1022. * it is possible for mperf's non-halted cycles + idle states
  1023. * to exceed TSC's all cycles: show c1 = 0% in that case.
  1024. */
  1025. if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
  1026. old->c1 = 0;
  1027. else {
  1028. /* normal case, derive c1 */
  1029. old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
  1030. - core_delta->c6 - core_delta->c7;
  1031. }
  1032. }
  1033. if (old->mperf == 0) {
  1034. if (debug > 1)
  1035. fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
  1036. old->mperf = 1; /* divide by 0 protection */
  1037. }
  1038. if (DO_BIC(BIC_IRQ))
  1039. old->irq_count = new->irq_count - old->irq_count;
  1040. if (DO_BIC(BIC_SMI))
  1041. old->smi_count = new->smi_count - old->smi_count;
  1042. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
  1043. if (mp->format == FORMAT_RAW)
  1044. old->counter[i] = new->counter[i];
  1045. else
  1046. old->counter[i] = new->counter[i] - old->counter[i];
  1047. }
  1048. return 0;
  1049. }
  1050. int delta_cpu(struct thread_data *t, struct core_data *c,
  1051. struct pkg_data *p, struct thread_data *t2,
  1052. struct core_data *c2, struct pkg_data *p2)
  1053. {
  1054. int retval = 0;
  1055. /* calculate core delta only for 1st thread in core */
  1056. if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
  1057. delta_core(c, c2);
  1058. /* always calculate thread delta */
  1059. retval = delta_thread(t, t2, c2); /* c2 is core delta */
  1060. if (retval)
  1061. return retval;
  1062. /* calculate package delta only for 1st core in package */
  1063. if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
  1064. retval = delta_package(p, p2);
  1065. return retval;
  1066. }
  1067. void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1068. {
  1069. int i;
  1070. struct msr_counter *mp;
  1071. t->tsc = 0;
  1072. t->aperf = 0;
  1073. t->mperf = 0;
  1074. t->c1 = 0;
  1075. t->irq_count = 0;
  1076. t->smi_count = 0;
  1077. /* tells format_counters to dump all fields from this set */
  1078. t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
  1079. c->c3 = 0;
  1080. c->c6 = 0;
  1081. c->c7 = 0;
  1082. c->mc6_us = 0;
  1083. c->core_temp_c = 0;
  1084. p->pkg_wtd_core_c0 = 0;
  1085. p->pkg_any_core_c0 = 0;
  1086. p->pkg_any_gfxe_c0 = 0;
  1087. p->pkg_both_core_gfxe_c0 = 0;
  1088. p->pc2 = 0;
  1089. if (DO_BIC(BIC_Pkgpc3))
  1090. p->pc3 = 0;
  1091. if (DO_BIC(BIC_Pkgpc6))
  1092. p->pc6 = 0;
  1093. if (DO_BIC(BIC_Pkgpc7))
  1094. p->pc7 = 0;
  1095. p->pc8 = 0;
  1096. p->pc9 = 0;
  1097. p->pc10 = 0;
  1098. p->energy_pkg = 0;
  1099. p->energy_dram = 0;
  1100. p->energy_cores = 0;
  1101. p->energy_gfx = 0;
  1102. p->rapl_pkg_perf_status = 0;
  1103. p->rapl_dram_perf_status = 0;
  1104. p->pkg_temp_c = 0;
  1105. p->gfx_rc6_ms = 0;
  1106. p->gfx_mhz = 0;
  1107. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
  1108. t->counter[i] = 0;
  1109. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
  1110. c->counter[i] = 0;
  1111. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
  1112. p->counter[i] = 0;
  1113. }
  1114. int sum_counters(struct thread_data *t, struct core_data *c,
  1115. struct pkg_data *p)
  1116. {
  1117. int i;
  1118. struct msr_counter *mp;
  1119. average.threads.tsc += t->tsc;
  1120. average.threads.aperf += t->aperf;
  1121. average.threads.mperf += t->mperf;
  1122. average.threads.c1 += t->c1;
  1123. average.threads.irq_count += t->irq_count;
  1124. average.threads.smi_count += t->smi_count;
  1125. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
  1126. if (mp->format == FORMAT_RAW)
  1127. continue;
  1128. average.threads.counter[i] += t->counter[i];
  1129. }
  1130. /* sum per-core values only for 1st thread in core */
  1131. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  1132. return 0;
  1133. average.cores.c3 += c->c3;
  1134. average.cores.c6 += c->c6;
  1135. average.cores.c7 += c->c7;
  1136. average.cores.mc6_us += c->mc6_us;
  1137. average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
  1138. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
  1139. if (mp->format == FORMAT_RAW)
  1140. continue;
  1141. average.cores.counter[i] += c->counter[i];
  1142. }
  1143. /* sum per-pkg values only for 1st core in pkg */
  1144. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1145. return 0;
  1146. if (DO_BIC(BIC_Totl_c0))
  1147. average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
  1148. if (DO_BIC(BIC_Any_c0))
  1149. average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
  1150. if (DO_BIC(BIC_GFX_c0))
  1151. average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
  1152. if (DO_BIC(BIC_CPUGFX))
  1153. average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
  1154. average.packages.pc2 += p->pc2;
  1155. if (DO_BIC(BIC_Pkgpc3))
  1156. average.packages.pc3 += p->pc3;
  1157. if (DO_BIC(BIC_Pkgpc6))
  1158. average.packages.pc6 += p->pc6;
  1159. if (DO_BIC(BIC_Pkgpc7))
  1160. average.packages.pc7 += p->pc7;
  1161. average.packages.pc8 += p->pc8;
  1162. average.packages.pc9 += p->pc9;
  1163. average.packages.pc10 += p->pc10;
  1164. average.packages.energy_pkg += p->energy_pkg;
  1165. average.packages.energy_dram += p->energy_dram;
  1166. average.packages.energy_cores += p->energy_cores;
  1167. average.packages.energy_gfx += p->energy_gfx;
  1168. average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
  1169. average.packages.gfx_mhz = p->gfx_mhz;
  1170. average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
  1171. average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
  1172. average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
  1173. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
  1174. if (mp->format == FORMAT_RAW)
  1175. continue;
  1176. average.packages.counter[i] += p->counter[i];
  1177. }
  1178. return 0;
  1179. }
  1180. /*
  1181. * sum the counters for all cpus in the system
  1182. * compute the weighted average
  1183. */
  1184. void compute_average(struct thread_data *t, struct core_data *c,
  1185. struct pkg_data *p)
  1186. {
  1187. int i;
  1188. struct msr_counter *mp;
  1189. clear_counters(&average.threads, &average.cores, &average.packages);
  1190. for_all_cpus(sum_counters, t, c, p);
  1191. average.threads.tsc /= topo.num_cpus;
  1192. average.threads.aperf /= topo.num_cpus;
  1193. average.threads.mperf /= topo.num_cpus;
  1194. average.threads.c1 /= topo.num_cpus;
  1195. if (average.threads.irq_count > 9999999)
  1196. sums_need_wide_columns = 1;
  1197. average.cores.c3 /= topo.num_cores;
  1198. average.cores.c6 /= topo.num_cores;
  1199. average.cores.c7 /= topo.num_cores;
  1200. average.cores.mc6_us /= topo.num_cores;
  1201. if (DO_BIC(BIC_Totl_c0))
  1202. average.packages.pkg_wtd_core_c0 /= topo.num_packages;
  1203. if (DO_BIC(BIC_Any_c0))
  1204. average.packages.pkg_any_core_c0 /= topo.num_packages;
  1205. if (DO_BIC(BIC_GFX_c0))
  1206. average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
  1207. if (DO_BIC(BIC_CPUGFX))
  1208. average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
  1209. average.packages.pc2 /= topo.num_packages;
  1210. if (DO_BIC(BIC_Pkgpc3))
  1211. average.packages.pc3 /= topo.num_packages;
  1212. if (DO_BIC(BIC_Pkgpc6))
  1213. average.packages.pc6 /= topo.num_packages;
  1214. if (DO_BIC(BIC_Pkgpc7))
  1215. average.packages.pc7 /= topo.num_packages;
  1216. average.packages.pc8 /= topo.num_packages;
  1217. average.packages.pc9 /= topo.num_packages;
  1218. average.packages.pc10 /= topo.num_packages;
  1219. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
  1220. if (mp->format == FORMAT_RAW)
  1221. continue;
  1222. if (mp->type == COUNTER_ITEMS) {
  1223. if (average.threads.counter[i] > 9999999)
  1224. sums_need_wide_columns = 1;
  1225. continue;
  1226. }
  1227. average.threads.counter[i] /= topo.num_cpus;
  1228. }
  1229. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
  1230. if (mp->format == FORMAT_RAW)
  1231. continue;
  1232. if (mp->type == COUNTER_ITEMS) {
  1233. if (average.cores.counter[i] > 9999999)
  1234. sums_need_wide_columns = 1;
  1235. }
  1236. average.cores.counter[i] /= topo.num_cores;
  1237. }
  1238. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
  1239. if (mp->format == FORMAT_RAW)
  1240. continue;
  1241. if (mp->type == COUNTER_ITEMS) {
  1242. if (average.packages.counter[i] > 9999999)
  1243. sums_need_wide_columns = 1;
  1244. }
  1245. average.packages.counter[i] /= topo.num_packages;
  1246. }
  1247. }
  1248. static unsigned long long rdtsc(void)
  1249. {
  1250. unsigned int low, high;
  1251. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  1252. return low | ((unsigned long long)high) << 32;
  1253. }
  1254. /*
  1255. * Open a file, and exit on failure
  1256. */
  1257. FILE *fopen_or_die(const char *path, const char *mode)
  1258. {
  1259. FILE *filep = fopen(path, mode);
  1260. if (!filep)
  1261. err(1, "%s: open failed", path);
  1262. return filep;
  1263. }
  1264. /*
  1265. * snapshot_sysfs_counter()
  1266. *
  1267. * return snapshot of given counter
  1268. */
  1269. unsigned long long snapshot_sysfs_counter(char *path)
  1270. {
  1271. FILE *fp;
  1272. int retval;
  1273. unsigned long long counter;
  1274. fp = fopen_or_die(path, "r");
  1275. retval = fscanf(fp, "%lld", &counter);
  1276. if (retval != 1)
  1277. err(1, "snapshot_sysfs_counter(%s)", path);
  1278. fclose(fp);
  1279. return counter;
  1280. }
  1281. int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
  1282. {
  1283. if (mp->msr_num != 0) {
  1284. if (get_msr(cpu, mp->msr_num, counterp))
  1285. return -1;
  1286. } else {
  1287. char path[128];
  1288. if (mp->flags & SYSFS_PERCPU) {
  1289. sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
  1290. cpu, mp->path);
  1291. *counterp = snapshot_sysfs_counter(path);
  1292. } else {
  1293. *counterp = snapshot_sysfs_counter(mp->path);
  1294. }
  1295. }
  1296. return 0;
  1297. }
  1298. /*
  1299. * get_counters(...)
  1300. * migrate to cpu
  1301. * acquire and record local counters for that cpu
  1302. */
  1303. int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1304. {
  1305. int cpu = t->cpu_id;
  1306. unsigned long long msr;
  1307. int aperf_mperf_retry_count = 0;
  1308. struct msr_counter *mp;
  1309. int i;
  1310. gettimeofday(&t->tv_begin, (struct timezone *)NULL);
  1311. if (cpu_migrate(cpu)) {
  1312. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  1313. return -1;
  1314. }
  1315. retry:
  1316. t->tsc = rdtsc(); /* we are running on local CPU of interest */
  1317. if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
  1318. unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
  1319. /*
  1320. * The TSC, APERF and MPERF must be read together for
  1321. * APERF/MPERF and MPERF/TSC to give accurate results.
  1322. *
  1323. * Unfortunately, APERF and MPERF are read by
  1324. * individual system call, so delays may occur
  1325. * between them. If the time to read them
  1326. * varies by a large amount, we re-read them.
  1327. */
  1328. /*
  1329. * This initial dummy APERF read has been seen to
  1330. * reduce jitter in the subsequent reads.
  1331. */
  1332. if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
  1333. return -3;
  1334. t->tsc = rdtsc(); /* re-read close to APERF */
  1335. tsc_before = t->tsc;
  1336. if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
  1337. return -3;
  1338. tsc_between = rdtsc();
  1339. if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
  1340. return -4;
  1341. tsc_after = rdtsc();
  1342. aperf_time = tsc_between - tsc_before;
  1343. mperf_time = tsc_after - tsc_between;
  1344. /*
  1345. * If the system call latency to read APERF and MPERF
  1346. * differ by more than 2x, then try again.
  1347. */
  1348. if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
  1349. aperf_mperf_retry_count++;
  1350. if (aperf_mperf_retry_count < 5)
  1351. goto retry;
  1352. else
  1353. warnx("cpu%d jitter %lld %lld",
  1354. cpu, aperf_time, mperf_time);
  1355. }
  1356. aperf_mperf_retry_count = 0;
  1357. t->aperf = t->aperf * aperf_mperf_multiplier;
  1358. t->mperf = t->mperf * aperf_mperf_multiplier;
  1359. }
  1360. if (DO_BIC(BIC_IRQ))
  1361. t->irq_count = irqs_per_cpu[cpu];
  1362. if (DO_BIC(BIC_SMI)) {
  1363. if (get_msr(cpu, MSR_SMI_COUNT, &msr))
  1364. return -5;
  1365. t->smi_count = msr & 0xFFFFFFFF;
  1366. }
  1367. if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
  1368. if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
  1369. return -6;
  1370. }
  1371. for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
  1372. if (get_mp(cpu, mp, &t->counter[i]))
  1373. return -10;
  1374. }
  1375. /* collect core counters only for 1st thread in core */
  1376. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  1377. goto done;
  1378. if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
  1379. if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
  1380. return -6;
  1381. }
  1382. if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
  1383. if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
  1384. return -7;
  1385. } else if (do_knl_cstates) {
  1386. if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
  1387. return -7;
  1388. }
  1389. if (DO_BIC(BIC_CPU_c7))
  1390. if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
  1391. return -8;
  1392. if (DO_BIC(BIC_Mod_c6))
  1393. if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
  1394. return -8;
  1395. if (DO_BIC(BIC_CoreTmp)) {
  1396. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  1397. return -9;
  1398. c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  1399. }
  1400. for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
  1401. if (get_mp(cpu, mp, &c->counter[i]))
  1402. return -10;
  1403. }
  1404. /* collect package counters only for 1st core in package */
  1405. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1406. goto done;
  1407. if (DO_BIC(BIC_Totl_c0)) {
  1408. if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
  1409. return -10;
  1410. }
  1411. if (DO_BIC(BIC_Any_c0)) {
  1412. if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
  1413. return -11;
  1414. }
  1415. if (DO_BIC(BIC_GFX_c0)) {
  1416. if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
  1417. return -12;
  1418. }
  1419. if (DO_BIC(BIC_CPUGFX)) {
  1420. if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
  1421. return -13;
  1422. }
  1423. if (DO_BIC(BIC_Pkgpc3))
  1424. if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
  1425. return -9;
  1426. if (DO_BIC(BIC_Pkgpc6)) {
  1427. if (do_slm_cstates) {
  1428. if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
  1429. return -10;
  1430. } else {
  1431. if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
  1432. return -10;
  1433. }
  1434. }
  1435. if (DO_BIC(BIC_Pkgpc2))
  1436. if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
  1437. return -11;
  1438. if (DO_BIC(BIC_Pkgpc7))
  1439. if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
  1440. return -12;
  1441. if (DO_BIC(BIC_Pkgpc8))
  1442. if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
  1443. return -13;
  1444. if (DO_BIC(BIC_Pkgpc9))
  1445. if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
  1446. return -13;
  1447. if (DO_BIC(BIC_Pkgpc10))
  1448. if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
  1449. return -13;
  1450. if (do_rapl & RAPL_PKG) {
  1451. if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
  1452. return -13;
  1453. p->energy_pkg = msr & 0xFFFFFFFF;
  1454. }
  1455. if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
  1456. if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
  1457. return -14;
  1458. p->energy_cores = msr & 0xFFFFFFFF;
  1459. }
  1460. if (do_rapl & RAPL_DRAM) {
  1461. if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
  1462. return -15;
  1463. p->energy_dram = msr & 0xFFFFFFFF;
  1464. }
  1465. if (do_rapl & RAPL_GFX) {
  1466. if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
  1467. return -16;
  1468. p->energy_gfx = msr & 0xFFFFFFFF;
  1469. }
  1470. if (do_rapl & RAPL_PKG_PERF_STATUS) {
  1471. if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
  1472. return -16;
  1473. p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
  1474. }
  1475. if (do_rapl & RAPL_DRAM_PERF_STATUS) {
  1476. if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
  1477. return -16;
  1478. p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
  1479. }
  1480. if (DO_BIC(BIC_PkgTmp)) {
  1481. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  1482. return -17;
  1483. p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  1484. }
  1485. if (DO_BIC(BIC_GFX_rc6))
  1486. p->gfx_rc6_ms = gfx_cur_rc6_ms;
  1487. if (DO_BIC(BIC_GFXMHz))
  1488. p->gfx_mhz = gfx_cur_mhz;
  1489. for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
  1490. if (get_mp(cpu, mp, &p->counter[i]))
  1491. return -10;
  1492. }
  1493. done:
  1494. gettimeofday(&t->tv_end, (struct timezone *)NULL);
  1495. return 0;
  1496. }
  1497. /*
  1498. * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
  1499. * If you change the values, note they are used both in comparisons
  1500. * (>= PCL__7) and to index pkg_cstate_limit_strings[].
  1501. */
  1502. #define PCLUKN 0 /* Unknown */
  1503. #define PCLRSV 1 /* Reserved */
  1504. #define PCL__0 2 /* PC0 */
  1505. #define PCL__1 3 /* PC1 */
  1506. #define PCL__2 4 /* PC2 */
  1507. #define PCL__3 5 /* PC3 */
  1508. #define PCL__4 6 /* PC4 */
  1509. #define PCL__6 7 /* PC6 */
  1510. #define PCL_6N 8 /* PC6 No Retention */
  1511. #define PCL_6R 9 /* PC6 Retention */
  1512. #define PCL__7 10 /* PC7 */
  1513. #define PCL_7S 11 /* PC7 Shrink */
  1514. #define PCL__8 12 /* PC8 */
  1515. #define PCL__9 13 /* PC9 */
  1516. #define PCLUNL 14 /* Unlimited */
  1517. int pkg_cstate_limit = PCLUKN;
  1518. char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
  1519. "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
  1520. 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};
  1521. 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};
  1522. 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};
  1523. 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, PCL__6, PCL__7};
  1524. int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1525. 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};
  1526. int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1527. int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
  1528. static void
  1529. calculate_tsc_tweak()
  1530. {
  1531. tsc_tweak = base_hz / tsc_hz;
  1532. }
  1533. static void
  1534. dump_nhm_platform_info(void)
  1535. {
  1536. unsigned long long msr;
  1537. unsigned int ratio;
  1538. get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
  1539. fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
  1540. ratio = (msr >> 40) & 0xFF;
  1541. fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
  1542. ratio, bclk, ratio * bclk);
  1543. ratio = (msr >> 8) & 0xFF;
  1544. fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
  1545. ratio, bclk, ratio * bclk);
  1546. get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
  1547. fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
  1548. base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
  1549. return;
  1550. }
  1551. static void
  1552. dump_hsw_turbo_ratio_limits(void)
  1553. {
  1554. unsigned long long msr;
  1555. unsigned int ratio;
  1556. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
  1557. fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
  1558. ratio = (msr >> 8) & 0xFF;
  1559. if (ratio)
  1560. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
  1561. ratio, bclk, ratio * bclk);
  1562. ratio = (msr >> 0) & 0xFF;
  1563. if (ratio)
  1564. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
  1565. ratio, bclk, ratio * bclk);
  1566. return;
  1567. }
  1568. static void
  1569. dump_ivt_turbo_ratio_limits(void)
  1570. {
  1571. unsigned long long msr;
  1572. unsigned int ratio;
  1573. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
  1574. fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
  1575. ratio = (msr >> 56) & 0xFF;
  1576. if (ratio)
  1577. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
  1578. ratio, bclk, ratio * bclk);
  1579. ratio = (msr >> 48) & 0xFF;
  1580. if (ratio)
  1581. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
  1582. ratio, bclk, ratio * bclk);
  1583. ratio = (msr >> 40) & 0xFF;
  1584. if (ratio)
  1585. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
  1586. ratio, bclk, ratio * bclk);
  1587. ratio = (msr >> 32) & 0xFF;
  1588. if (ratio)
  1589. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
  1590. ratio, bclk, ratio * bclk);
  1591. ratio = (msr >> 24) & 0xFF;
  1592. if (ratio)
  1593. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
  1594. ratio, bclk, ratio * bclk);
  1595. ratio = (msr >> 16) & 0xFF;
  1596. if (ratio)
  1597. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
  1598. ratio, bclk, ratio * bclk);
  1599. ratio = (msr >> 8) & 0xFF;
  1600. if (ratio)
  1601. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
  1602. ratio, bclk, ratio * bclk);
  1603. ratio = (msr >> 0) & 0xFF;
  1604. if (ratio)
  1605. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
  1606. ratio, bclk, ratio * bclk);
  1607. return;
  1608. }
  1609. int has_turbo_ratio_group_limits(int family, int model)
  1610. {
  1611. if (!genuine_intel)
  1612. return 0;
  1613. switch (model) {
  1614. case INTEL_FAM6_ATOM_GOLDMONT:
  1615. case INTEL_FAM6_SKYLAKE_X:
  1616. case INTEL_FAM6_ATOM_DENVERTON:
  1617. return 1;
  1618. }
  1619. return 0;
  1620. }
  1621. static void
  1622. dump_turbo_ratio_limits(int family, int model)
  1623. {
  1624. unsigned long long msr, core_counts;
  1625. unsigned int ratio, group_size;
  1626. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
  1627. fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
  1628. if (has_turbo_ratio_group_limits(family, model)) {
  1629. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
  1630. fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
  1631. } else {
  1632. core_counts = 0x0807060504030201;
  1633. }
  1634. ratio = (msr >> 56) & 0xFF;
  1635. group_size = (core_counts >> 56) & 0xFF;
  1636. if (ratio)
  1637. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1638. ratio, bclk, ratio * bclk, group_size);
  1639. ratio = (msr >> 48) & 0xFF;
  1640. group_size = (core_counts >> 48) & 0xFF;
  1641. if (ratio)
  1642. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1643. ratio, bclk, ratio * bclk, group_size);
  1644. ratio = (msr >> 40) & 0xFF;
  1645. group_size = (core_counts >> 40) & 0xFF;
  1646. if (ratio)
  1647. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1648. ratio, bclk, ratio * bclk, group_size);
  1649. ratio = (msr >> 32) & 0xFF;
  1650. group_size = (core_counts >> 32) & 0xFF;
  1651. if (ratio)
  1652. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1653. ratio, bclk, ratio * bclk, group_size);
  1654. ratio = (msr >> 24) & 0xFF;
  1655. group_size = (core_counts >> 24) & 0xFF;
  1656. if (ratio)
  1657. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1658. ratio, bclk, ratio * bclk, group_size);
  1659. ratio = (msr >> 16) & 0xFF;
  1660. group_size = (core_counts >> 16) & 0xFF;
  1661. if (ratio)
  1662. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1663. ratio, bclk, ratio * bclk, group_size);
  1664. ratio = (msr >> 8) & 0xFF;
  1665. group_size = (core_counts >> 8) & 0xFF;
  1666. if (ratio)
  1667. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1668. ratio, bclk, ratio * bclk, group_size);
  1669. ratio = (msr >> 0) & 0xFF;
  1670. group_size = (core_counts >> 0) & 0xFF;
  1671. if (ratio)
  1672. fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1673. ratio, bclk, ratio * bclk, group_size);
  1674. return;
  1675. }
  1676. static void
  1677. dump_atom_turbo_ratio_limits(void)
  1678. {
  1679. unsigned long long msr;
  1680. unsigned int ratio;
  1681. get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
  1682. fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
  1683. ratio = (msr >> 0) & 0x3F;
  1684. if (ratio)
  1685. fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
  1686. ratio, bclk, ratio * bclk);
  1687. ratio = (msr >> 8) & 0x3F;
  1688. if (ratio)
  1689. fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
  1690. ratio, bclk, ratio * bclk);
  1691. ratio = (msr >> 16) & 0x3F;
  1692. if (ratio)
  1693. fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
  1694. ratio, bclk, ratio * bclk);
  1695. get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
  1696. fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
  1697. ratio = (msr >> 24) & 0x3F;
  1698. if (ratio)
  1699. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
  1700. ratio, bclk, ratio * bclk);
  1701. ratio = (msr >> 16) & 0x3F;
  1702. if (ratio)
  1703. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
  1704. ratio, bclk, ratio * bclk);
  1705. ratio = (msr >> 8) & 0x3F;
  1706. if (ratio)
  1707. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
  1708. ratio, bclk, ratio * bclk);
  1709. ratio = (msr >> 0) & 0x3F;
  1710. if (ratio)
  1711. fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
  1712. ratio, bclk, ratio * bclk);
  1713. }
  1714. static void
  1715. dump_knl_turbo_ratio_limits(void)
  1716. {
  1717. const unsigned int buckets_no = 7;
  1718. unsigned long long msr;
  1719. int delta_cores, delta_ratio;
  1720. int i, b_nr;
  1721. unsigned int cores[buckets_no];
  1722. unsigned int ratio[buckets_no];
  1723. get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
  1724. fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
  1725. base_cpu, msr);
  1726. /**
  1727. * Turbo encoding in KNL is as follows:
  1728. * [0] -- Reserved
  1729. * [7:1] -- Base value of number of active cores of bucket 1.
  1730. * [15:8] -- Base value of freq ratio of bucket 1.
  1731. * [20:16] -- +ve delta of number of active cores of bucket 2.
  1732. * i.e. active cores of bucket 2 =
  1733. * active cores of bucket 1 + delta
  1734. * [23:21] -- Negative delta of freq ratio of bucket 2.
  1735. * i.e. freq ratio of bucket 2 =
  1736. * freq ratio of bucket 1 - delta
  1737. * [28:24]-- +ve delta of number of active cores of bucket 3.
  1738. * [31:29]-- -ve delta of freq ratio of bucket 3.
  1739. * [36:32]-- +ve delta of number of active cores of bucket 4.
  1740. * [39:37]-- -ve delta of freq ratio of bucket 4.
  1741. * [44:40]-- +ve delta of number of active cores of bucket 5.
  1742. * [47:45]-- -ve delta of freq ratio of bucket 5.
  1743. * [52:48]-- +ve delta of number of active cores of bucket 6.
  1744. * [55:53]-- -ve delta of freq ratio of bucket 6.
  1745. * [60:56]-- +ve delta of number of active cores of bucket 7.
  1746. * [63:61]-- -ve delta of freq ratio of bucket 7.
  1747. */
  1748. b_nr = 0;
  1749. cores[b_nr] = (msr & 0xFF) >> 1;
  1750. ratio[b_nr] = (msr >> 8) & 0xFF;
  1751. for (i = 16; i < 64; i += 8) {
  1752. delta_cores = (msr >> i) & 0x1F;
  1753. delta_ratio = (msr >> (i + 5)) & 0x7;
  1754. cores[b_nr + 1] = cores[b_nr] + delta_cores;
  1755. ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
  1756. b_nr++;
  1757. }
  1758. for (i = buckets_no - 1; i >= 0; i--)
  1759. if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
  1760. fprintf(outf,
  1761. "%d * %.1f = %.1f MHz max turbo %d active cores\n",
  1762. ratio[i], bclk, ratio[i] * bclk, cores[i]);
  1763. }
  1764. static void
  1765. dump_nhm_cst_cfg(void)
  1766. {
  1767. unsigned long long msr;
  1768. get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
  1769. #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
  1770. #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
  1771. fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
  1772. fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
  1773. (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
  1774. (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
  1775. (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
  1776. (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
  1777. (msr & (1 << 15)) ? "" : "UN",
  1778. (unsigned int)msr & 0xF,
  1779. pkg_cstate_limit_strings[pkg_cstate_limit]);
  1780. return;
  1781. }
  1782. static void
  1783. dump_config_tdp(void)
  1784. {
  1785. unsigned long long msr;
  1786. get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
  1787. fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
  1788. fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
  1789. get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
  1790. fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
  1791. if (msr) {
  1792. fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
  1793. fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
  1794. fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
  1795. fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
  1796. }
  1797. fprintf(outf, ")\n");
  1798. get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
  1799. fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
  1800. if (msr) {
  1801. fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
  1802. fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
  1803. fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
  1804. fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
  1805. }
  1806. fprintf(outf, ")\n");
  1807. get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
  1808. fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
  1809. if ((msr) & 0x3)
  1810. fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
  1811. fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
  1812. fprintf(outf, ")\n");
  1813. get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
  1814. fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
  1815. fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
  1816. fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
  1817. fprintf(outf, ")\n");
  1818. }
  1819. unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
  1820. void print_irtl(void)
  1821. {
  1822. unsigned long long msr;
  1823. get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
  1824. fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
  1825. fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
  1826. (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
  1827. get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
  1828. fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
  1829. fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
  1830. (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
  1831. get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
  1832. fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
  1833. fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
  1834. (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
  1835. if (!do_irtl_hsw)
  1836. return;
  1837. get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
  1838. fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
  1839. fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
  1840. (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
  1841. get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
  1842. fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
  1843. fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
  1844. (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
  1845. get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
  1846. fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
  1847. fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
  1848. (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
  1849. }
  1850. void free_fd_percpu(void)
  1851. {
  1852. int i;
  1853. for (i = 0; i < topo.max_cpu_num + 1; ++i) {
  1854. if (fd_percpu[i] != 0)
  1855. close(fd_percpu[i]);
  1856. }
  1857. free(fd_percpu);
  1858. }
  1859. void free_all_buffers(void)
  1860. {
  1861. CPU_FREE(cpu_present_set);
  1862. cpu_present_set = NULL;
  1863. cpu_present_setsize = 0;
  1864. CPU_FREE(cpu_affinity_set);
  1865. cpu_affinity_set = NULL;
  1866. cpu_affinity_setsize = 0;
  1867. free(thread_even);
  1868. free(core_even);
  1869. free(package_even);
  1870. thread_even = NULL;
  1871. core_even = NULL;
  1872. package_even = NULL;
  1873. free(thread_odd);
  1874. free(core_odd);
  1875. free(package_odd);
  1876. thread_odd = NULL;
  1877. core_odd = NULL;
  1878. package_odd = NULL;
  1879. free(output_buffer);
  1880. output_buffer = NULL;
  1881. outp = NULL;
  1882. free_fd_percpu();
  1883. free(irq_column_2_cpu);
  1884. free(irqs_per_cpu);
  1885. }
  1886. /*
  1887. * Parse a file containing a single int.
  1888. */
  1889. int parse_int_file(const char *fmt, ...)
  1890. {
  1891. va_list args;
  1892. char path[PATH_MAX];
  1893. FILE *filep;
  1894. int value;
  1895. va_start(args, fmt);
  1896. vsnprintf(path, sizeof(path), fmt, args);
  1897. va_end(args);
  1898. filep = fopen_or_die(path, "r");
  1899. if (fscanf(filep, "%d", &value) != 1)
  1900. err(1, "%s: failed to parse number from file", path);
  1901. fclose(filep);
  1902. return value;
  1903. }
  1904. /*
  1905. * get_cpu_position_in_core(cpu)
  1906. * return the position of the CPU among its HT siblings in the core
  1907. * return -1 if the sibling is not in list
  1908. */
  1909. int get_cpu_position_in_core(int cpu)
  1910. {
  1911. char path[64];
  1912. FILE *filep;
  1913. int this_cpu;
  1914. char character;
  1915. int i;
  1916. sprintf(path,
  1917. "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
  1918. cpu);
  1919. filep = fopen(path, "r");
  1920. if (filep == NULL) {
  1921. perror(path);
  1922. exit(1);
  1923. }
  1924. for (i = 0; i < topo.num_threads_per_core; i++) {
  1925. fscanf(filep, "%d", &this_cpu);
  1926. if (this_cpu == cpu) {
  1927. fclose(filep);
  1928. return i;
  1929. }
  1930. /* Account for no separator after last thread*/
  1931. if (i != (topo.num_threads_per_core - 1))
  1932. fscanf(filep, "%c", &character);
  1933. }
  1934. fclose(filep);
  1935. return -1;
  1936. }
  1937. /*
  1938. * cpu_is_first_core_in_package(cpu)
  1939. * return 1 if given CPU is 1st core in package
  1940. */
  1941. int cpu_is_first_core_in_package(int cpu)
  1942. {
  1943. return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
  1944. }
  1945. int get_physical_package_id(int cpu)
  1946. {
  1947. return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  1948. }
  1949. int get_core_id(int cpu)
  1950. {
  1951. return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
  1952. }
  1953. int get_num_ht_siblings(int cpu)
  1954. {
  1955. char path[80];
  1956. FILE *filep;
  1957. int sib1;
  1958. int matches = 0;
  1959. char character;
  1960. char str[100];
  1961. char *ch;
  1962. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  1963. filep = fopen_or_die(path, "r");
  1964. /*
  1965. * file format:
  1966. * A ',' separated or '-' separated set of numbers
  1967. * (eg 1-2 or 1,3,4,5)
  1968. */
  1969. fscanf(filep, "%d%c\n", &sib1, &character);
  1970. fseek(filep, 0, SEEK_SET);
  1971. fgets(str, 100, filep);
  1972. ch = strchr(str, character);
  1973. while (ch != NULL) {
  1974. matches++;
  1975. ch = strchr(ch+1, character);
  1976. }
  1977. fclose(filep);
  1978. return matches+1;
  1979. }
  1980. /*
  1981. * run func(thread, core, package) in topology order
  1982. * skip non-present cpus
  1983. */
  1984. int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
  1985. struct pkg_data *, struct thread_data *, struct core_data *,
  1986. struct pkg_data *), struct thread_data *thread_base,
  1987. struct core_data *core_base, struct pkg_data *pkg_base,
  1988. struct thread_data *thread_base2, struct core_data *core_base2,
  1989. struct pkg_data *pkg_base2)
  1990. {
  1991. int retval, pkg_no, core_no, thread_no;
  1992. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  1993. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  1994. for (thread_no = 0; thread_no <
  1995. topo.num_threads_per_core; ++thread_no) {
  1996. struct thread_data *t, *t2;
  1997. struct core_data *c, *c2;
  1998. struct pkg_data *p, *p2;
  1999. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  2000. if (cpu_is_not_present(t->cpu_id))
  2001. continue;
  2002. t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
  2003. c = GET_CORE(core_base, core_no, pkg_no);
  2004. c2 = GET_CORE(core_base2, core_no, pkg_no);
  2005. p = GET_PKG(pkg_base, pkg_no);
  2006. p2 = GET_PKG(pkg_base2, pkg_no);
  2007. retval = func(t, c, p, t2, c2, p2);
  2008. if (retval)
  2009. return retval;
  2010. }
  2011. }
  2012. }
  2013. return 0;
  2014. }
  2015. /*
  2016. * run func(cpu) on every cpu in /proc/stat
  2017. * return max_cpu number
  2018. */
  2019. int for_all_proc_cpus(int (func)(int))
  2020. {
  2021. FILE *fp;
  2022. int cpu_num;
  2023. int retval;
  2024. fp = fopen_or_die(proc_stat, "r");
  2025. retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  2026. if (retval != 0)
  2027. err(1, "%s: failed to parse format", proc_stat);
  2028. while (1) {
  2029. retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
  2030. if (retval != 1)
  2031. break;
  2032. retval = func(cpu_num);
  2033. if (retval) {
  2034. fclose(fp);
  2035. return(retval);
  2036. }
  2037. }
  2038. fclose(fp);
  2039. return 0;
  2040. }
  2041. void re_initialize(void)
  2042. {
  2043. free_all_buffers();
  2044. setup_all_buffers();
  2045. printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
  2046. }
  2047. /*
  2048. * count_cpus()
  2049. * remember the last one seen, it will be the max
  2050. */
  2051. int count_cpus(int cpu)
  2052. {
  2053. if (topo.max_cpu_num < cpu)
  2054. topo.max_cpu_num = cpu;
  2055. topo.num_cpus += 1;
  2056. return 0;
  2057. }
  2058. int mark_cpu_present(int cpu)
  2059. {
  2060. CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
  2061. return 0;
  2062. }
  2063. /*
  2064. * snapshot_proc_interrupts()
  2065. *
  2066. * read and record summary of /proc/interrupts
  2067. *
  2068. * return 1 if config change requires a restart, else return 0
  2069. */
  2070. int snapshot_proc_interrupts(void)
  2071. {
  2072. static FILE *fp;
  2073. int column, retval;
  2074. if (fp == NULL)
  2075. fp = fopen_or_die("/proc/interrupts", "r");
  2076. else
  2077. rewind(fp);
  2078. /* read 1st line of /proc/interrupts to get cpu* name for each column */
  2079. for (column = 0; column < topo.num_cpus; ++column) {
  2080. int cpu_number;
  2081. retval = fscanf(fp, " CPU%d", &cpu_number);
  2082. if (retval != 1)
  2083. break;
  2084. if (cpu_number > topo.max_cpu_num) {
  2085. warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
  2086. return 1;
  2087. }
  2088. irq_column_2_cpu[column] = cpu_number;
  2089. irqs_per_cpu[cpu_number] = 0;
  2090. }
  2091. /* read /proc/interrupt count lines and sum up irqs per cpu */
  2092. while (1) {
  2093. int column;
  2094. char buf[64];
  2095. retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
  2096. if (retval != 1)
  2097. break;
  2098. /* read the count per cpu */
  2099. for (column = 0; column < topo.num_cpus; ++column) {
  2100. int cpu_number, irq_count;
  2101. retval = fscanf(fp, " %d", &irq_count);
  2102. if (retval != 1)
  2103. break;
  2104. cpu_number = irq_column_2_cpu[column];
  2105. irqs_per_cpu[cpu_number] += irq_count;
  2106. }
  2107. while (getc(fp) != '\n')
  2108. ; /* flush interrupt description */
  2109. }
  2110. return 0;
  2111. }
  2112. /*
  2113. * snapshot_gfx_rc6_ms()
  2114. *
  2115. * record snapshot of
  2116. * /sys/class/drm/card0/power/rc6_residency_ms
  2117. *
  2118. * return 1 if config change requires a restart, else return 0
  2119. */
  2120. int snapshot_gfx_rc6_ms(void)
  2121. {
  2122. FILE *fp;
  2123. int retval;
  2124. fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
  2125. retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
  2126. if (retval != 1)
  2127. err(1, "GFX rc6");
  2128. fclose(fp);
  2129. return 0;
  2130. }
  2131. /*
  2132. * snapshot_gfx_mhz()
  2133. *
  2134. * record snapshot of
  2135. * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
  2136. *
  2137. * return 1 if config change requires a restart, else return 0
  2138. */
  2139. int snapshot_gfx_mhz(void)
  2140. {
  2141. static FILE *fp;
  2142. int retval;
  2143. if (fp == NULL)
  2144. fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
  2145. else {
  2146. rewind(fp);
  2147. fflush(fp);
  2148. }
  2149. retval = fscanf(fp, "%d", &gfx_cur_mhz);
  2150. if (retval != 1)
  2151. err(1, "GFX MHz");
  2152. return 0;
  2153. }
  2154. /*
  2155. * snapshot /proc and /sys files
  2156. *
  2157. * return 1 if configuration restart needed, else return 0
  2158. */
  2159. int snapshot_proc_sysfs_files(void)
  2160. {
  2161. if (DO_BIC(BIC_IRQ))
  2162. if (snapshot_proc_interrupts())
  2163. return 1;
  2164. if (DO_BIC(BIC_GFX_rc6))
  2165. snapshot_gfx_rc6_ms();
  2166. if (DO_BIC(BIC_GFXMHz))
  2167. snapshot_gfx_mhz();
  2168. return 0;
  2169. }
  2170. void turbostat_loop()
  2171. {
  2172. int retval;
  2173. int restarted = 0;
  2174. restart:
  2175. restarted++;
  2176. snapshot_proc_sysfs_files();
  2177. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  2178. if (retval < -1) {
  2179. exit(retval);
  2180. } else if (retval == -1) {
  2181. if (restarted > 1) {
  2182. exit(retval);
  2183. }
  2184. re_initialize();
  2185. goto restart;
  2186. }
  2187. restarted = 0;
  2188. gettimeofday(&tv_even, (struct timezone *)NULL);
  2189. while (1) {
  2190. if (for_all_proc_cpus(cpu_is_not_present)) {
  2191. re_initialize();
  2192. goto restart;
  2193. }
  2194. nanosleep(&interval_ts, NULL);
  2195. if (snapshot_proc_sysfs_files())
  2196. goto restart;
  2197. retval = for_all_cpus(get_counters, ODD_COUNTERS);
  2198. if (retval < -1) {
  2199. exit(retval);
  2200. } else if (retval == -1) {
  2201. re_initialize();
  2202. goto restart;
  2203. }
  2204. gettimeofday(&tv_odd, (struct timezone *)NULL);
  2205. timersub(&tv_odd, &tv_even, &tv_delta);
  2206. if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
  2207. re_initialize();
  2208. goto restart;
  2209. }
  2210. compute_average(EVEN_COUNTERS);
  2211. format_all_counters(EVEN_COUNTERS);
  2212. flush_output_stdout();
  2213. nanosleep(&interval_ts, NULL);
  2214. if (snapshot_proc_sysfs_files())
  2215. goto restart;
  2216. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  2217. if (retval < -1) {
  2218. exit(retval);
  2219. } else if (retval == -1) {
  2220. re_initialize();
  2221. goto restart;
  2222. }
  2223. gettimeofday(&tv_even, (struct timezone *)NULL);
  2224. timersub(&tv_even, &tv_odd, &tv_delta);
  2225. if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
  2226. re_initialize();
  2227. goto restart;
  2228. }
  2229. compute_average(ODD_COUNTERS);
  2230. format_all_counters(ODD_COUNTERS);
  2231. flush_output_stdout();
  2232. }
  2233. }
  2234. void check_dev_msr()
  2235. {
  2236. struct stat sb;
  2237. char pathname[32];
  2238. sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
  2239. if (stat(pathname, &sb))
  2240. if (system("/sbin/modprobe msr > /dev/null 2>&1"))
  2241. err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
  2242. }
  2243. void check_permissions()
  2244. {
  2245. struct __user_cap_header_struct cap_header_data;
  2246. cap_user_header_t cap_header = &cap_header_data;
  2247. struct __user_cap_data_struct cap_data_data;
  2248. cap_user_data_t cap_data = &cap_data_data;
  2249. extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
  2250. int do_exit = 0;
  2251. char pathname[32];
  2252. /* check for CAP_SYS_RAWIO */
  2253. cap_header->pid = getpid();
  2254. cap_header->version = _LINUX_CAPABILITY_VERSION;
  2255. if (capget(cap_header, cap_data) < 0)
  2256. err(-6, "capget(2) failed");
  2257. if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
  2258. do_exit++;
  2259. warnx("capget(CAP_SYS_RAWIO) failed,"
  2260. " try \"# setcap cap_sys_rawio=ep %s\"", progname);
  2261. }
  2262. /* test file permissions */
  2263. sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
  2264. if (euidaccess(pathname, R_OK)) {
  2265. do_exit++;
  2266. warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
  2267. }
  2268. /* if all else fails, thell them to be root */
  2269. if (do_exit)
  2270. if (getuid() != 0)
  2271. warnx("... or simply run as root");
  2272. if (do_exit)
  2273. exit(-6);
  2274. }
  2275. /*
  2276. * NHM adds support for additional MSRs:
  2277. *
  2278. * MSR_SMI_COUNT 0x00000034
  2279. *
  2280. * MSR_PLATFORM_INFO 0x000000ce
  2281. * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
  2282. *
  2283. * MSR_MISC_PWR_MGMT 0x000001aa
  2284. *
  2285. * MSR_PKG_C3_RESIDENCY 0x000003f8
  2286. * MSR_PKG_C6_RESIDENCY 0x000003f9
  2287. * MSR_CORE_C3_RESIDENCY 0x000003fc
  2288. * MSR_CORE_C6_RESIDENCY 0x000003fd
  2289. *
  2290. * Side effect:
  2291. * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
  2292. * sets has_misc_feature_control
  2293. */
  2294. int probe_nhm_msrs(unsigned int family, unsigned int model)
  2295. {
  2296. unsigned long long msr;
  2297. unsigned int base_ratio;
  2298. int *pkg_cstate_limits;
  2299. if (!genuine_intel)
  2300. return 0;
  2301. if (family != 6)
  2302. return 0;
  2303. bclk = discover_bclk(family, model);
  2304. switch (model) {
  2305. case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
  2306. case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
  2307. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  2308. case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
  2309. case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
  2310. case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
  2311. case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
  2312. pkg_cstate_limits = nhm_pkg_cstate_limits;
  2313. break;
  2314. case INTEL_FAM6_SANDYBRIDGE: /* SNB */
  2315. case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
  2316. case INTEL_FAM6_IVYBRIDGE: /* IVB */
  2317. case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
  2318. pkg_cstate_limits = snb_pkg_cstate_limits;
  2319. has_misc_feature_control = 1;
  2320. break;
  2321. case INTEL_FAM6_HASWELL_CORE: /* HSW */
  2322. case INTEL_FAM6_HASWELL_X: /* HSX */
  2323. case INTEL_FAM6_HASWELL_ULT: /* HSW */
  2324. case INTEL_FAM6_HASWELL_GT3E: /* HSW */
  2325. case INTEL_FAM6_BROADWELL_CORE: /* BDW */
  2326. case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
  2327. case INTEL_FAM6_BROADWELL_X: /* BDX */
  2328. case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
  2329. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  2330. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  2331. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  2332. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  2333. pkg_cstate_limits = hsw_pkg_cstate_limits;
  2334. has_misc_feature_control = 1;
  2335. break;
  2336. case INTEL_FAM6_SKYLAKE_X: /* SKX */
  2337. pkg_cstate_limits = skx_pkg_cstate_limits;
  2338. has_misc_feature_control = 1;
  2339. break;
  2340. case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
  2341. no_MSR_MISC_PWR_MGMT = 1;
  2342. case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
  2343. pkg_cstate_limits = slv_pkg_cstate_limits;
  2344. break;
  2345. case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
  2346. pkg_cstate_limits = amt_pkg_cstate_limits;
  2347. no_MSR_MISC_PWR_MGMT = 1;
  2348. break;
  2349. case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
  2350. case INTEL_FAM6_XEON_PHI_KNM:
  2351. pkg_cstate_limits = phi_pkg_cstate_limits;
  2352. break;
  2353. case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
  2354. case INTEL_FAM6_ATOM_GEMINI_LAKE:
  2355. case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
  2356. pkg_cstate_limits = bxt_pkg_cstate_limits;
  2357. break;
  2358. default:
  2359. return 0;
  2360. }
  2361. get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
  2362. pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
  2363. get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
  2364. base_ratio = (msr >> 8) & 0xFF;
  2365. base_hz = base_ratio * bclk * 1000000;
  2366. has_base_hz = 1;
  2367. return 1;
  2368. }
  2369. /*
  2370. * SLV client has support for unique MSRs:
  2371. *
  2372. * MSR_CC6_DEMOTION_POLICY_CONFIG
  2373. * MSR_MC6_DEMOTION_POLICY_CONFIG
  2374. */
  2375. int has_slv_msrs(unsigned int family, unsigned int model)
  2376. {
  2377. if (!genuine_intel)
  2378. return 0;
  2379. switch (model) {
  2380. case INTEL_FAM6_ATOM_SILVERMONT1:
  2381. case INTEL_FAM6_ATOM_MERRIFIELD:
  2382. case INTEL_FAM6_ATOM_MOOREFIELD:
  2383. return 1;
  2384. }
  2385. return 0;
  2386. }
  2387. int is_dnv(unsigned int family, unsigned int model)
  2388. {
  2389. if (!genuine_intel)
  2390. return 0;
  2391. switch (model) {
  2392. case INTEL_FAM6_ATOM_DENVERTON:
  2393. return 1;
  2394. }
  2395. return 0;
  2396. }
  2397. int is_bdx(unsigned int family, unsigned int model)
  2398. {
  2399. if (!genuine_intel)
  2400. return 0;
  2401. switch (model) {
  2402. case INTEL_FAM6_BROADWELL_X:
  2403. case INTEL_FAM6_BROADWELL_XEON_D:
  2404. return 1;
  2405. }
  2406. return 0;
  2407. }
  2408. int is_skx(unsigned int family, unsigned int model)
  2409. {
  2410. if (!genuine_intel)
  2411. return 0;
  2412. switch (model) {
  2413. case INTEL_FAM6_SKYLAKE_X:
  2414. return 1;
  2415. }
  2416. return 0;
  2417. }
  2418. int has_turbo_ratio_limit(unsigned int family, unsigned int model)
  2419. {
  2420. if (has_slv_msrs(family, model))
  2421. return 0;
  2422. switch (model) {
  2423. /* Nehalem compatible, but do not include turbo-ratio limit support */
  2424. case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
  2425. case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
  2426. case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
  2427. case INTEL_FAM6_XEON_PHI_KNM:
  2428. return 0;
  2429. default:
  2430. return 1;
  2431. }
  2432. }
  2433. int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
  2434. {
  2435. if (has_slv_msrs(family, model))
  2436. return 1;
  2437. return 0;
  2438. }
  2439. int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
  2440. {
  2441. if (!genuine_intel)
  2442. return 0;
  2443. if (family != 6)
  2444. return 0;
  2445. switch (model) {
  2446. case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
  2447. case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
  2448. return 1;
  2449. default:
  2450. return 0;
  2451. }
  2452. }
  2453. int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
  2454. {
  2455. if (!genuine_intel)
  2456. return 0;
  2457. if (family != 6)
  2458. return 0;
  2459. switch (model) {
  2460. case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
  2461. return 1;
  2462. default:
  2463. return 0;
  2464. }
  2465. }
  2466. int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
  2467. {
  2468. if (!genuine_intel)
  2469. return 0;
  2470. if (family != 6)
  2471. return 0;
  2472. switch (model) {
  2473. case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
  2474. case INTEL_FAM6_XEON_PHI_KNM:
  2475. return 1;
  2476. default:
  2477. return 0;
  2478. }
  2479. }
  2480. int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
  2481. {
  2482. if (!genuine_intel)
  2483. return 0;
  2484. if (family != 6)
  2485. return 0;
  2486. switch (model) {
  2487. case INTEL_FAM6_ATOM_GOLDMONT:
  2488. case INTEL_FAM6_SKYLAKE_X:
  2489. return 1;
  2490. default:
  2491. return 0;
  2492. }
  2493. }
  2494. int has_config_tdp(unsigned int family, unsigned int model)
  2495. {
  2496. if (!genuine_intel)
  2497. return 0;
  2498. if (family != 6)
  2499. return 0;
  2500. switch (model) {
  2501. case INTEL_FAM6_IVYBRIDGE: /* IVB */
  2502. case INTEL_FAM6_HASWELL_CORE: /* HSW */
  2503. case INTEL_FAM6_HASWELL_X: /* HSX */
  2504. case INTEL_FAM6_HASWELL_ULT: /* HSW */
  2505. case INTEL_FAM6_HASWELL_GT3E: /* HSW */
  2506. case INTEL_FAM6_BROADWELL_CORE: /* BDW */
  2507. case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
  2508. case INTEL_FAM6_BROADWELL_X: /* BDX */
  2509. case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
  2510. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  2511. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  2512. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  2513. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  2514. case INTEL_FAM6_SKYLAKE_X: /* SKX */
  2515. case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
  2516. case INTEL_FAM6_XEON_PHI_KNM:
  2517. return 1;
  2518. default:
  2519. return 0;
  2520. }
  2521. }
  2522. static void
  2523. dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
  2524. {
  2525. if (!do_nhm_platform_info)
  2526. return;
  2527. dump_nhm_platform_info();
  2528. if (has_hsw_turbo_ratio_limit(family, model))
  2529. dump_hsw_turbo_ratio_limits();
  2530. if (has_ivt_turbo_ratio_limit(family, model))
  2531. dump_ivt_turbo_ratio_limits();
  2532. if (has_turbo_ratio_limit(family, model))
  2533. dump_turbo_ratio_limits(family, model);
  2534. if (has_atom_turbo_ratio_limit(family, model))
  2535. dump_atom_turbo_ratio_limits();
  2536. if (has_knl_turbo_ratio_limit(family, model))
  2537. dump_knl_turbo_ratio_limits();
  2538. if (has_config_tdp(family, model))
  2539. dump_config_tdp();
  2540. dump_nhm_cst_cfg();
  2541. }
  2542. static void
  2543. dump_sysfs_cstate_config(void)
  2544. {
  2545. char path[64];
  2546. char name_buf[16];
  2547. char desc[64];
  2548. FILE *input;
  2549. int state;
  2550. char *sp;
  2551. if (!DO_BIC(BIC_sysfs))
  2552. return;
  2553. for (state = 0; state < 10; ++state) {
  2554. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
  2555. base_cpu, state);
  2556. input = fopen(path, "r");
  2557. if (input == NULL)
  2558. continue;
  2559. fgets(name_buf, sizeof(name_buf), input);
  2560. /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
  2561. sp = strchr(name_buf, '-');
  2562. if (!sp)
  2563. sp = strchrnul(name_buf, '\n');
  2564. *sp = '\0';
  2565. fclose(input);
  2566. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
  2567. base_cpu, state);
  2568. input = fopen(path, "r");
  2569. if (input == NULL)
  2570. continue;
  2571. fgets(desc, sizeof(desc), input);
  2572. fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
  2573. fclose(input);
  2574. }
  2575. }
  2576. static void
  2577. dump_sysfs_pstate_config(void)
  2578. {
  2579. char path[64];
  2580. char driver_buf[64];
  2581. char governor_buf[64];
  2582. FILE *input;
  2583. int turbo;
  2584. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
  2585. base_cpu);
  2586. input = fopen(path, "r");
  2587. if (input == NULL) {
  2588. fprintf(stderr, "NSFOD %s\n", path);
  2589. return;
  2590. }
  2591. fgets(driver_buf, sizeof(driver_buf), input);
  2592. fclose(input);
  2593. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
  2594. base_cpu);
  2595. input = fopen(path, "r");
  2596. if (input == NULL) {
  2597. fprintf(stderr, "NSFOD %s\n", path);
  2598. return;
  2599. }
  2600. fgets(governor_buf, sizeof(governor_buf), input);
  2601. fclose(input);
  2602. fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
  2603. fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
  2604. sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
  2605. input = fopen(path, "r");
  2606. if (input != NULL) {
  2607. fscanf(input, "%d", &turbo);
  2608. fprintf(outf, "cpufreq boost: %d\n", turbo);
  2609. fclose(input);
  2610. }
  2611. sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
  2612. input = fopen(path, "r");
  2613. if (input != NULL) {
  2614. fscanf(input, "%d", &turbo);
  2615. fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
  2616. fclose(input);
  2617. }
  2618. }
  2619. /*
  2620. * print_epb()
  2621. * Decode the ENERGY_PERF_BIAS MSR
  2622. */
  2623. int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  2624. {
  2625. unsigned long long msr;
  2626. char *epb_string;
  2627. int cpu;
  2628. if (!has_epb)
  2629. return 0;
  2630. cpu = t->cpu_id;
  2631. /* EPB is per-package */
  2632. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  2633. return 0;
  2634. if (cpu_migrate(cpu)) {
  2635. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  2636. return -1;
  2637. }
  2638. if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
  2639. return 0;
  2640. switch (msr & 0xF) {
  2641. case ENERGY_PERF_BIAS_PERFORMANCE:
  2642. epb_string = "performance";
  2643. break;
  2644. case ENERGY_PERF_BIAS_NORMAL:
  2645. epb_string = "balanced";
  2646. break;
  2647. case ENERGY_PERF_BIAS_POWERSAVE:
  2648. epb_string = "powersave";
  2649. break;
  2650. default:
  2651. epb_string = "custom";
  2652. break;
  2653. }
  2654. fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
  2655. return 0;
  2656. }
  2657. /*
  2658. * print_hwp()
  2659. * Decode the MSR_HWP_CAPABILITIES
  2660. */
  2661. int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  2662. {
  2663. unsigned long long msr;
  2664. int cpu;
  2665. if (!has_hwp)
  2666. return 0;
  2667. cpu = t->cpu_id;
  2668. /* MSR_HWP_CAPABILITIES is per-package */
  2669. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  2670. return 0;
  2671. if (cpu_migrate(cpu)) {
  2672. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  2673. return -1;
  2674. }
  2675. if (get_msr(cpu, MSR_PM_ENABLE, &msr))
  2676. return 0;
  2677. fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
  2678. cpu, msr, (msr & (1 << 0)) ? "" : "No-");
  2679. /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
  2680. if ((msr & (1 << 0)) == 0)
  2681. return 0;
  2682. if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
  2683. return 0;
  2684. fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
  2685. "(high %d guar %d eff %d low %d)\n",
  2686. cpu, msr,
  2687. (unsigned int)HWP_HIGHEST_PERF(msr),
  2688. (unsigned int)HWP_GUARANTEED_PERF(msr),
  2689. (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
  2690. (unsigned int)HWP_LOWEST_PERF(msr));
  2691. if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
  2692. return 0;
  2693. fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
  2694. "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
  2695. cpu, msr,
  2696. (unsigned int)(((msr) >> 0) & 0xff),
  2697. (unsigned int)(((msr) >> 8) & 0xff),
  2698. (unsigned int)(((msr) >> 16) & 0xff),
  2699. (unsigned int)(((msr) >> 24) & 0xff),
  2700. (unsigned int)(((msr) >> 32) & 0xff3),
  2701. (unsigned int)(((msr) >> 42) & 0x1));
  2702. if (has_hwp_pkg) {
  2703. if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
  2704. return 0;
  2705. fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
  2706. "(min %d max %d des %d epp 0x%x window 0x%x)\n",
  2707. cpu, msr,
  2708. (unsigned int)(((msr) >> 0) & 0xff),
  2709. (unsigned int)(((msr) >> 8) & 0xff),
  2710. (unsigned int)(((msr) >> 16) & 0xff),
  2711. (unsigned int)(((msr) >> 24) & 0xff),
  2712. (unsigned int)(((msr) >> 32) & 0xff3));
  2713. }
  2714. if (has_hwp_notify) {
  2715. if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
  2716. return 0;
  2717. fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
  2718. "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
  2719. cpu, msr,
  2720. ((msr) & 0x1) ? "EN" : "Dis",
  2721. ((msr) & 0x2) ? "EN" : "Dis");
  2722. }
  2723. if (get_msr(cpu, MSR_HWP_STATUS, &msr))
  2724. return 0;
  2725. fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
  2726. "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
  2727. cpu, msr,
  2728. ((msr) & 0x1) ? "" : "No-",
  2729. ((msr) & 0x2) ? "" : "No-");
  2730. return 0;
  2731. }
  2732. /*
  2733. * print_perf_limit()
  2734. */
  2735. int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  2736. {
  2737. unsigned long long msr;
  2738. int cpu;
  2739. cpu = t->cpu_id;
  2740. /* per-package */
  2741. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  2742. return 0;
  2743. if (cpu_migrate(cpu)) {
  2744. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  2745. return -1;
  2746. }
  2747. if (do_core_perf_limit_reasons) {
  2748. get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
  2749. fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
  2750. fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
  2751. (msr & 1 << 15) ? "bit15, " : "",
  2752. (msr & 1 << 14) ? "bit14, " : "",
  2753. (msr & 1 << 13) ? "Transitions, " : "",
  2754. (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
  2755. (msr & 1 << 11) ? "PkgPwrL2, " : "",
  2756. (msr & 1 << 10) ? "PkgPwrL1, " : "",
  2757. (msr & 1 << 9) ? "CorePwr, " : "",
  2758. (msr & 1 << 8) ? "Amps, " : "",
  2759. (msr & 1 << 6) ? "VR-Therm, " : "",
  2760. (msr & 1 << 5) ? "Auto-HWP, " : "",
  2761. (msr & 1 << 4) ? "Graphics, " : "",
  2762. (msr & 1 << 2) ? "bit2, " : "",
  2763. (msr & 1 << 1) ? "ThermStatus, " : "",
  2764. (msr & 1 << 0) ? "PROCHOT, " : "");
  2765. fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
  2766. (msr & 1 << 31) ? "bit31, " : "",
  2767. (msr & 1 << 30) ? "bit30, " : "",
  2768. (msr & 1 << 29) ? "Transitions, " : "",
  2769. (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
  2770. (msr & 1 << 27) ? "PkgPwrL2, " : "",
  2771. (msr & 1 << 26) ? "PkgPwrL1, " : "",
  2772. (msr & 1 << 25) ? "CorePwr, " : "",
  2773. (msr & 1 << 24) ? "Amps, " : "",
  2774. (msr & 1 << 22) ? "VR-Therm, " : "",
  2775. (msr & 1 << 21) ? "Auto-HWP, " : "",
  2776. (msr & 1 << 20) ? "Graphics, " : "",
  2777. (msr & 1 << 18) ? "bit18, " : "",
  2778. (msr & 1 << 17) ? "ThermStatus, " : "",
  2779. (msr & 1 << 16) ? "PROCHOT, " : "");
  2780. }
  2781. if (do_gfx_perf_limit_reasons) {
  2782. get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
  2783. fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
  2784. fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
  2785. (msr & 1 << 0) ? "PROCHOT, " : "",
  2786. (msr & 1 << 1) ? "ThermStatus, " : "",
  2787. (msr & 1 << 4) ? "Graphics, " : "",
  2788. (msr & 1 << 6) ? "VR-Therm, " : "",
  2789. (msr & 1 << 8) ? "Amps, " : "",
  2790. (msr & 1 << 9) ? "GFXPwr, " : "",
  2791. (msr & 1 << 10) ? "PkgPwrL1, " : "",
  2792. (msr & 1 << 11) ? "PkgPwrL2, " : "");
  2793. fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
  2794. (msr & 1 << 16) ? "PROCHOT, " : "",
  2795. (msr & 1 << 17) ? "ThermStatus, " : "",
  2796. (msr & 1 << 20) ? "Graphics, " : "",
  2797. (msr & 1 << 22) ? "VR-Therm, " : "",
  2798. (msr & 1 << 24) ? "Amps, " : "",
  2799. (msr & 1 << 25) ? "GFXPwr, " : "",
  2800. (msr & 1 << 26) ? "PkgPwrL1, " : "",
  2801. (msr & 1 << 27) ? "PkgPwrL2, " : "");
  2802. }
  2803. if (do_ring_perf_limit_reasons) {
  2804. get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
  2805. fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
  2806. fprintf(outf, " (Active: %s%s%s%s%s%s)",
  2807. (msr & 1 << 0) ? "PROCHOT, " : "",
  2808. (msr & 1 << 1) ? "ThermStatus, " : "",
  2809. (msr & 1 << 6) ? "VR-Therm, " : "",
  2810. (msr & 1 << 8) ? "Amps, " : "",
  2811. (msr & 1 << 10) ? "PkgPwrL1, " : "",
  2812. (msr & 1 << 11) ? "PkgPwrL2, " : "");
  2813. fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
  2814. (msr & 1 << 16) ? "PROCHOT, " : "",
  2815. (msr & 1 << 17) ? "ThermStatus, " : "",
  2816. (msr & 1 << 22) ? "VR-Therm, " : "",
  2817. (msr & 1 << 24) ? "Amps, " : "",
  2818. (msr & 1 << 26) ? "PkgPwrL1, " : "",
  2819. (msr & 1 << 27) ? "PkgPwrL2, " : "");
  2820. }
  2821. return 0;
  2822. }
  2823. #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
  2824. #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
  2825. double get_tdp(unsigned int model)
  2826. {
  2827. unsigned long long msr;
  2828. if (do_rapl & RAPL_PKG_POWER_INFO)
  2829. if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
  2830. return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
  2831. switch (model) {
  2832. case INTEL_FAM6_ATOM_SILVERMONT1:
  2833. case INTEL_FAM6_ATOM_SILVERMONT2:
  2834. return 30.0;
  2835. default:
  2836. return 135.0;
  2837. }
  2838. }
  2839. /*
  2840. * rapl_dram_energy_units_probe()
  2841. * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
  2842. */
  2843. static double
  2844. rapl_dram_energy_units_probe(int model, double rapl_energy_units)
  2845. {
  2846. /* only called for genuine_intel, family 6 */
  2847. switch (model) {
  2848. case INTEL_FAM6_HASWELL_X: /* HSX */
  2849. case INTEL_FAM6_BROADWELL_X: /* BDX */
  2850. case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
  2851. case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
  2852. case INTEL_FAM6_XEON_PHI_KNM:
  2853. return (rapl_dram_energy_units = 15.3 / 1000000);
  2854. default:
  2855. return (rapl_energy_units);
  2856. }
  2857. }
  2858. /*
  2859. * rapl_probe()
  2860. *
  2861. * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
  2862. */
  2863. void rapl_probe(unsigned int family, unsigned int model)
  2864. {
  2865. unsigned long long msr;
  2866. unsigned int time_unit;
  2867. double tdp;
  2868. if (!genuine_intel)
  2869. return;
  2870. if (family != 6)
  2871. return;
  2872. switch (model) {
  2873. case INTEL_FAM6_SANDYBRIDGE:
  2874. case INTEL_FAM6_IVYBRIDGE:
  2875. case INTEL_FAM6_HASWELL_CORE: /* HSW */
  2876. case INTEL_FAM6_HASWELL_ULT: /* HSW */
  2877. case INTEL_FAM6_HASWELL_GT3E: /* HSW */
  2878. case INTEL_FAM6_BROADWELL_CORE: /* BDW */
  2879. case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
  2880. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
  2881. if (rapl_joules) {
  2882. BIC_PRESENT(BIC_Pkg_J);
  2883. BIC_PRESENT(BIC_Cor_J);
  2884. BIC_PRESENT(BIC_GFX_J);
  2885. } else {
  2886. BIC_PRESENT(BIC_PkgWatt);
  2887. BIC_PRESENT(BIC_CorWatt);
  2888. BIC_PRESENT(BIC_GFXWatt);
  2889. }
  2890. break;
  2891. case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
  2892. case INTEL_FAM6_ATOM_GEMINI_LAKE:
  2893. do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
  2894. if (rapl_joules)
  2895. BIC_PRESENT(BIC_Pkg_J);
  2896. else
  2897. BIC_PRESENT(BIC_PkgWatt);
  2898. break;
  2899. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  2900. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  2901. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  2902. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  2903. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
  2904. BIC_PRESENT(BIC_PKG__);
  2905. BIC_PRESENT(BIC_RAM__);
  2906. if (rapl_joules) {
  2907. BIC_PRESENT(BIC_Pkg_J);
  2908. BIC_PRESENT(BIC_Cor_J);
  2909. BIC_PRESENT(BIC_RAM_J);
  2910. BIC_PRESENT(BIC_GFX_J);
  2911. } else {
  2912. BIC_PRESENT(BIC_PkgWatt);
  2913. BIC_PRESENT(BIC_CorWatt);
  2914. BIC_PRESENT(BIC_RAMWatt);
  2915. BIC_PRESENT(BIC_GFXWatt);
  2916. }
  2917. break;
  2918. case INTEL_FAM6_HASWELL_X: /* HSX */
  2919. case INTEL_FAM6_BROADWELL_X: /* BDX */
  2920. case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
  2921. case INTEL_FAM6_SKYLAKE_X: /* SKX */
  2922. case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
  2923. case INTEL_FAM6_XEON_PHI_KNM:
  2924. do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
  2925. BIC_PRESENT(BIC_PKG__);
  2926. BIC_PRESENT(BIC_RAM__);
  2927. if (rapl_joules) {
  2928. BIC_PRESENT(BIC_Pkg_J);
  2929. BIC_PRESENT(BIC_RAM_J);
  2930. } else {
  2931. BIC_PRESENT(BIC_PkgWatt);
  2932. BIC_PRESENT(BIC_RAMWatt);
  2933. }
  2934. break;
  2935. case INTEL_FAM6_SANDYBRIDGE_X:
  2936. case INTEL_FAM6_IVYBRIDGE_X:
  2937. 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;
  2938. BIC_PRESENT(BIC_PKG__);
  2939. BIC_PRESENT(BIC_RAM__);
  2940. if (rapl_joules) {
  2941. BIC_PRESENT(BIC_Pkg_J);
  2942. BIC_PRESENT(BIC_Cor_J);
  2943. BIC_PRESENT(BIC_RAM_J);
  2944. } else {
  2945. BIC_PRESENT(BIC_PkgWatt);
  2946. BIC_PRESENT(BIC_CorWatt);
  2947. BIC_PRESENT(BIC_RAMWatt);
  2948. }
  2949. break;
  2950. case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
  2951. case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
  2952. do_rapl = RAPL_PKG | RAPL_CORES;
  2953. if (rapl_joules) {
  2954. BIC_PRESENT(BIC_Pkg_J);
  2955. BIC_PRESENT(BIC_Cor_J);
  2956. } else {
  2957. BIC_PRESENT(BIC_PkgWatt);
  2958. BIC_PRESENT(BIC_CorWatt);
  2959. }
  2960. break;
  2961. case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
  2962. do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
  2963. BIC_PRESENT(BIC_PKG__);
  2964. BIC_PRESENT(BIC_RAM__);
  2965. if (rapl_joules) {
  2966. BIC_PRESENT(BIC_Pkg_J);
  2967. BIC_PRESENT(BIC_Cor_J);
  2968. BIC_PRESENT(BIC_RAM_J);
  2969. } else {
  2970. BIC_PRESENT(BIC_PkgWatt);
  2971. BIC_PRESENT(BIC_CorWatt);
  2972. BIC_PRESENT(BIC_RAMWatt);
  2973. }
  2974. break;
  2975. default:
  2976. return;
  2977. }
  2978. /* units on package 0, verify later other packages match */
  2979. if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
  2980. return;
  2981. rapl_power_units = 1.0 / (1 << (msr & 0xF));
  2982. if (model == INTEL_FAM6_ATOM_SILVERMONT1)
  2983. rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
  2984. else
  2985. rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
  2986. rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
  2987. time_unit = msr >> 16 & 0xF;
  2988. if (time_unit == 0)
  2989. time_unit = 0xA;
  2990. rapl_time_units = 1.0 / (1 << (time_unit));
  2991. tdp = get_tdp(model);
  2992. rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
  2993. if (!quiet)
  2994. fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
  2995. return;
  2996. }
  2997. void perf_limit_reasons_probe(unsigned int family, unsigned int model)
  2998. {
  2999. if (!genuine_intel)
  3000. return;
  3001. if (family != 6)
  3002. return;
  3003. switch (model) {
  3004. case INTEL_FAM6_HASWELL_CORE: /* HSW */
  3005. case INTEL_FAM6_HASWELL_ULT: /* HSW */
  3006. case INTEL_FAM6_HASWELL_GT3E: /* HSW */
  3007. do_gfx_perf_limit_reasons = 1;
  3008. case INTEL_FAM6_HASWELL_X: /* HSX */
  3009. do_core_perf_limit_reasons = 1;
  3010. do_ring_perf_limit_reasons = 1;
  3011. default:
  3012. return;
  3013. }
  3014. }
  3015. int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  3016. {
  3017. unsigned long long msr;
  3018. unsigned int dts, dts2;
  3019. int cpu;
  3020. if (!(do_dts || do_ptm))
  3021. return 0;
  3022. cpu = t->cpu_id;
  3023. /* DTS is per-core, no need to print for each thread */
  3024. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  3025. return 0;
  3026. if (cpu_migrate(cpu)) {
  3027. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  3028. return -1;
  3029. }
  3030. if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
  3031. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  3032. return 0;
  3033. dts = (msr >> 16) & 0x7F;
  3034. fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
  3035. cpu, msr, tcc_activation_temp - dts);
  3036. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
  3037. return 0;
  3038. dts = (msr >> 16) & 0x7F;
  3039. dts2 = (msr >> 8) & 0x7F;
  3040. fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  3041. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  3042. }
  3043. if (do_dts && debug) {
  3044. unsigned int resolution;
  3045. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  3046. return 0;
  3047. dts = (msr >> 16) & 0x7F;
  3048. resolution = (msr >> 27) & 0xF;
  3049. fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
  3050. cpu, msr, tcc_activation_temp - dts, resolution);
  3051. if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
  3052. return 0;
  3053. dts = (msr >> 16) & 0x7F;
  3054. dts2 = (msr >> 8) & 0x7F;
  3055. fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  3056. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  3057. }
  3058. return 0;
  3059. }
  3060. void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
  3061. {
  3062. fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
  3063. cpu, label,
  3064. ((msr >> 15) & 1) ? "EN" : "DIS",
  3065. ((msr >> 0) & 0x7FFF) * rapl_power_units,
  3066. (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
  3067. (((msr >> 16) & 1) ? "EN" : "DIS"));
  3068. return;
  3069. }
  3070. int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  3071. {
  3072. unsigned long long msr;
  3073. int cpu;
  3074. if (!do_rapl)
  3075. return 0;
  3076. /* RAPL counters are per package, so print only for 1st thread/package */
  3077. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  3078. return 0;
  3079. cpu = t->cpu_id;
  3080. if (cpu_migrate(cpu)) {
  3081. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  3082. return -1;
  3083. }
  3084. if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
  3085. return -1;
  3086. fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
  3087. rapl_power_units, rapl_energy_units, rapl_time_units);
  3088. if (do_rapl & RAPL_PKG_POWER_INFO) {
  3089. if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
  3090. return -5;
  3091. fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  3092. cpu, msr,
  3093. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  3094. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  3095. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  3096. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  3097. }
  3098. if (do_rapl & RAPL_PKG) {
  3099. if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
  3100. return -9;
  3101. fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
  3102. cpu, msr, (msr >> 63) & 1 ? "" : "UN");
  3103. print_power_limit_msr(cpu, msr, "PKG Limit #1");
  3104. fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
  3105. cpu,
  3106. ((msr >> 47) & 1) ? "EN" : "DIS",
  3107. ((msr >> 32) & 0x7FFF) * rapl_power_units,
  3108. (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
  3109. ((msr >> 48) & 1) ? "EN" : "DIS");
  3110. }
  3111. if (do_rapl & RAPL_DRAM_POWER_INFO) {
  3112. if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
  3113. return -6;
  3114. fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  3115. cpu, msr,
  3116. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  3117. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  3118. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  3119. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  3120. }
  3121. if (do_rapl & RAPL_DRAM) {
  3122. if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
  3123. return -9;
  3124. fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
  3125. cpu, msr, (msr >> 31) & 1 ? "" : "UN");
  3126. print_power_limit_msr(cpu, msr, "DRAM Limit");
  3127. }
  3128. if (do_rapl & RAPL_CORE_POLICY) {
  3129. if (get_msr(cpu, MSR_PP0_POLICY, &msr))
  3130. return -7;
  3131. fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
  3132. }
  3133. if (do_rapl & RAPL_CORES_POWER_LIMIT) {
  3134. if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
  3135. return -9;
  3136. fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
  3137. cpu, msr, (msr >> 31) & 1 ? "" : "UN");
  3138. print_power_limit_msr(cpu, msr, "Cores Limit");
  3139. }
  3140. if (do_rapl & RAPL_GFX) {
  3141. if (get_msr(cpu, MSR_PP1_POLICY, &msr))
  3142. return -8;
  3143. fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
  3144. if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
  3145. return -9;
  3146. fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
  3147. cpu, msr, (msr >> 31) & 1 ? "" : "UN");
  3148. print_power_limit_msr(cpu, msr, "GFX Limit");
  3149. }
  3150. return 0;
  3151. }
  3152. /*
  3153. * SNB adds support for additional MSRs:
  3154. *
  3155. * MSR_PKG_C7_RESIDENCY 0x000003fa
  3156. * MSR_CORE_C7_RESIDENCY 0x000003fe
  3157. * MSR_PKG_C2_RESIDENCY 0x0000060d
  3158. */
  3159. int has_snb_msrs(unsigned int family, unsigned int model)
  3160. {
  3161. if (!genuine_intel)
  3162. return 0;
  3163. switch (model) {
  3164. case INTEL_FAM6_SANDYBRIDGE:
  3165. case INTEL_FAM6_SANDYBRIDGE_X:
  3166. case INTEL_FAM6_IVYBRIDGE: /* IVB */
  3167. case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
  3168. case INTEL_FAM6_HASWELL_CORE: /* HSW */
  3169. case INTEL_FAM6_HASWELL_X: /* HSW */
  3170. case INTEL_FAM6_HASWELL_ULT: /* HSW */
  3171. case INTEL_FAM6_HASWELL_GT3E: /* HSW */
  3172. case INTEL_FAM6_BROADWELL_CORE: /* BDW */
  3173. case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
  3174. case INTEL_FAM6_BROADWELL_X: /* BDX */
  3175. case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
  3176. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  3177. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  3178. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  3179. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  3180. case INTEL_FAM6_SKYLAKE_X: /* SKX */
  3181. case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
  3182. case INTEL_FAM6_ATOM_GEMINI_LAKE:
  3183. case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
  3184. return 1;
  3185. }
  3186. return 0;
  3187. }
  3188. /*
  3189. * HSW adds support for additional MSRs:
  3190. *
  3191. * MSR_PKG_C8_RESIDENCY 0x00000630
  3192. * MSR_PKG_C9_RESIDENCY 0x00000631
  3193. * MSR_PKG_C10_RESIDENCY 0x00000632
  3194. *
  3195. * MSR_PKGC8_IRTL 0x00000633
  3196. * MSR_PKGC9_IRTL 0x00000634
  3197. * MSR_PKGC10_IRTL 0x00000635
  3198. *
  3199. */
  3200. int has_hsw_msrs(unsigned int family, unsigned int model)
  3201. {
  3202. if (!genuine_intel)
  3203. return 0;
  3204. switch (model) {
  3205. case INTEL_FAM6_HASWELL_ULT: /* HSW */
  3206. case INTEL_FAM6_BROADWELL_CORE: /* BDW */
  3207. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  3208. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  3209. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  3210. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  3211. case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
  3212. case INTEL_FAM6_ATOM_GEMINI_LAKE:
  3213. return 1;
  3214. }
  3215. return 0;
  3216. }
  3217. /*
  3218. * SKL adds support for additional MSRS:
  3219. *
  3220. * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
  3221. * MSR_PKG_ANY_CORE_C0_RES 0x00000659
  3222. * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
  3223. * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
  3224. */
  3225. int has_skl_msrs(unsigned int family, unsigned int model)
  3226. {
  3227. if (!genuine_intel)
  3228. return 0;
  3229. switch (model) {
  3230. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  3231. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  3232. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  3233. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  3234. return 1;
  3235. }
  3236. return 0;
  3237. }
  3238. int is_slm(unsigned int family, unsigned int model)
  3239. {
  3240. if (!genuine_intel)
  3241. return 0;
  3242. switch (model) {
  3243. case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
  3244. case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
  3245. return 1;
  3246. }
  3247. return 0;
  3248. }
  3249. int is_knl(unsigned int family, unsigned int model)
  3250. {
  3251. if (!genuine_intel)
  3252. return 0;
  3253. switch (model) {
  3254. case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
  3255. case INTEL_FAM6_XEON_PHI_KNM:
  3256. return 1;
  3257. }
  3258. return 0;
  3259. }
  3260. unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
  3261. {
  3262. if (is_knl(family, model))
  3263. return 1024;
  3264. return 1;
  3265. }
  3266. #define SLM_BCLK_FREQS 5
  3267. double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
  3268. double slm_bclk(void)
  3269. {
  3270. unsigned long long msr = 3;
  3271. unsigned int i;
  3272. double freq;
  3273. if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
  3274. fprintf(outf, "SLM BCLK: unknown\n");
  3275. i = msr & 0xf;
  3276. if (i >= SLM_BCLK_FREQS) {
  3277. fprintf(outf, "SLM BCLK[%d] invalid\n", i);
  3278. i = 3;
  3279. }
  3280. freq = slm_freq_table[i];
  3281. if (!quiet)
  3282. fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
  3283. return freq;
  3284. }
  3285. double discover_bclk(unsigned int family, unsigned int model)
  3286. {
  3287. if (has_snb_msrs(family, model) || is_knl(family, model))
  3288. return 100.00;
  3289. else if (is_slm(family, model))
  3290. return slm_bclk();
  3291. else
  3292. return 133.33;
  3293. }
  3294. /*
  3295. * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
  3296. * the Thermal Control Circuit (TCC) activates.
  3297. * This is usually equal to tjMax.
  3298. *
  3299. * Older processors do not have this MSR, so there we guess,
  3300. * but also allow cmdline over-ride with -T.
  3301. *
  3302. * Several MSR temperature values are in units of degrees-C
  3303. * below this value, including the Digital Thermal Sensor (DTS),
  3304. * Package Thermal Management Sensor (PTM), and thermal event thresholds.
  3305. */
  3306. int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  3307. {
  3308. unsigned long long msr;
  3309. unsigned int target_c_local;
  3310. int cpu;
  3311. /* tcc_activation_temp is used only for dts or ptm */
  3312. if (!(do_dts || do_ptm))
  3313. return 0;
  3314. /* this is a per-package concept */
  3315. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  3316. return 0;
  3317. cpu = t->cpu_id;
  3318. if (cpu_migrate(cpu)) {
  3319. fprintf(outf, "Could not migrate to CPU %d\n", cpu);
  3320. return -1;
  3321. }
  3322. if (tcc_activation_temp_override != 0) {
  3323. tcc_activation_temp = tcc_activation_temp_override;
  3324. fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
  3325. cpu, tcc_activation_temp);
  3326. return 0;
  3327. }
  3328. /* Temperature Target MSR is Nehalem and newer only */
  3329. if (!do_nhm_platform_info)
  3330. goto guess;
  3331. if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
  3332. goto guess;
  3333. target_c_local = (msr >> 16) & 0xFF;
  3334. if (!quiet)
  3335. fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
  3336. cpu, msr, target_c_local);
  3337. if (!target_c_local)
  3338. goto guess;
  3339. tcc_activation_temp = target_c_local;
  3340. return 0;
  3341. guess:
  3342. tcc_activation_temp = TJMAX_DEFAULT;
  3343. fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
  3344. cpu, tcc_activation_temp);
  3345. return 0;
  3346. }
  3347. void decode_feature_control_msr(void)
  3348. {
  3349. unsigned long long msr;
  3350. if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
  3351. fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
  3352. base_cpu, msr,
  3353. msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
  3354. msr & (1 << 18) ? "SGX" : "");
  3355. }
  3356. void decode_misc_enable_msr(void)
  3357. {
  3358. unsigned long long msr;
  3359. if (!genuine_intel)
  3360. return;
  3361. if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
  3362. fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
  3363. base_cpu, msr,
  3364. msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
  3365. msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
  3366. msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
  3367. msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
  3368. msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
  3369. }
  3370. void decode_misc_feature_control(void)
  3371. {
  3372. unsigned long long msr;
  3373. if (!has_misc_feature_control)
  3374. return;
  3375. if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
  3376. fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
  3377. base_cpu, msr,
  3378. msr & (0 << 0) ? "No-" : "",
  3379. msr & (1 << 0) ? "No-" : "",
  3380. msr & (2 << 0) ? "No-" : "",
  3381. msr & (3 << 0) ? "No-" : "");
  3382. }
  3383. /*
  3384. * Decode MSR_MISC_PWR_MGMT
  3385. *
  3386. * Decode the bits according to the Nehalem documentation
  3387. * bit[0] seems to continue to have same meaning going forward
  3388. * bit[1] less so...
  3389. */
  3390. void decode_misc_pwr_mgmt_msr(void)
  3391. {
  3392. unsigned long long msr;
  3393. if (!do_nhm_platform_info)
  3394. return;
  3395. if (no_MSR_MISC_PWR_MGMT)
  3396. return;
  3397. if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
  3398. fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
  3399. base_cpu, msr,
  3400. msr & (1 << 0) ? "DIS" : "EN",
  3401. msr & (1 << 1) ? "EN" : "DIS",
  3402. msr & (1 << 8) ? "EN" : "DIS");
  3403. }
  3404. /*
  3405. * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
  3406. *
  3407. * This MSRs are present on Silvermont processors,
  3408. * Intel Atom processor E3000 series (Baytrail), and friends.
  3409. */
  3410. void decode_c6_demotion_policy_msr(void)
  3411. {
  3412. unsigned long long msr;
  3413. if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
  3414. fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
  3415. base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
  3416. if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
  3417. fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
  3418. base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
  3419. }
  3420. void process_cpuid()
  3421. {
  3422. unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
  3423. unsigned int fms, family, model, stepping;
  3424. unsigned int has_turbo;
  3425. eax = ebx = ecx = edx = 0;
  3426. __cpuid(0, max_level, ebx, ecx, edx);
  3427. if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
  3428. genuine_intel = 1;
  3429. if (!quiet)
  3430. fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
  3431. (char *)&ebx, (char *)&edx, (char *)&ecx);
  3432. __cpuid(1, fms, ebx, ecx, edx);
  3433. family = (fms >> 8) & 0xf;
  3434. model = (fms >> 4) & 0xf;
  3435. stepping = fms & 0xf;
  3436. if (family == 6 || family == 0xf)
  3437. model += ((fms >> 16) & 0xf) << 4;
  3438. if (!quiet) {
  3439. fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
  3440. max_level, family, model, stepping, family, model, stepping);
  3441. fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
  3442. ecx & (1 << 0) ? "SSE3" : "-",
  3443. ecx & (1 << 3) ? "MONITOR" : "-",
  3444. ecx & (1 << 6) ? "SMX" : "-",
  3445. ecx & (1 << 7) ? "EIST" : "-",
  3446. ecx & (1 << 8) ? "TM2" : "-",
  3447. edx & (1 << 4) ? "TSC" : "-",
  3448. edx & (1 << 5) ? "MSR" : "-",
  3449. edx & (1 << 22) ? "ACPI-TM" : "-",
  3450. edx & (1 << 29) ? "TM" : "-");
  3451. }
  3452. if (!(edx & (1 << 5)))
  3453. errx(1, "CPUID: no MSR");
  3454. /*
  3455. * check max extended function levels of CPUID.
  3456. * This is needed to check for invariant TSC.
  3457. * This check is valid for both Intel and AMD.
  3458. */
  3459. ebx = ecx = edx = 0;
  3460. __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
  3461. if (max_extended_level >= 0x80000007) {
  3462. /*
  3463. * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
  3464. * this check is valid for both Intel and AMD
  3465. */
  3466. __cpuid(0x80000007, eax, ebx, ecx, edx);
  3467. has_invariant_tsc = edx & (1 << 8);
  3468. }
  3469. /*
  3470. * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
  3471. * this check is valid for both Intel and AMD
  3472. */
  3473. __cpuid(0x6, eax, ebx, ecx, edx);
  3474. has_aperf = ecx & (1 << 0);
  3475. if (has_aperf) {
  3476. BIC_PRESENT(BIC_Avg_MHz);
  3477. BIC_PRESENT(BIC_Busy);
  3478. BIC_PRESENT(BIC_Bzy_MHz);
  3479. }
  3480. do_dts = eax & (1 << 0);
  3481. if (do_dts)
  3482. BIC_PRESENT(BIC_CoreTmp);
  3483. has_turbo = eax & (1 << 1);
  3484. do_ptm = eax & (1 << 6);
  3485. if (do_ptm)
  3486. BIC_PRESENT(BIC_PkgTmp);
  3487. has_hwp = eax & (1 << 7);
  3488. has_hwp_notify = eax & (1 << 8);
  3489. has_hwp_activity_window = eax & (1 << 9);
  3490. has_hwp_epp = eax & (1 << 10);
  3491. has_hwp_pkg = eax & (1 << 11);
  3492. has_epb = ecx & (1 << 3);
  3493. if (!quiet)
  3494. fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
  3495. "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
  3496. has_aperf ? "" : "No-",
  3497. has_turbo ? "" : "No-",
  3498. do_dts ? "" : "No-",
  3499. do_ptm ? "" : "No-",
  3500. has_hwp ? "" : "No-",
  3501. has_hwp_notify ? "" : "No-",
  3502. has_hwp_activity_window ? "" : "No-",
  3503. has_hwp_epp ? "" : "No-",
  3504. has_hwp_pkg ? "" : "No-",
  3505. has_epb ? "" : "No-");
  3506. if (!quiet)
  3507. decode_misc_enable_msr();
  3508. if (max_level >= 0x7 && !quiet) {
  3509. int has_sgx;
  3510. ecx = 0;
  3511. __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
  3512. has_sgx = ebx & (1 << 2);
  3513. fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
  3514. if (has_sgx)
  3515. decode_feature_control_msr();
  3516. }
  3517. if (max_level >= 0x15) {
  3518. unsigned int eax_crystal;
  3519. unsigned int ebx_tsc;
  3520. /*
  3521. * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
  3522. */
  3523. eax_crystal = ebx_tsc = crystal_hz = edx = 0;
  3524. __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
  3525. if (ebx_tsc != 0) {
  3526. if (!quiet && (ebx != 0))
  3527. fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
  3528. eax_crystal, ebx_tsc, crystal_hz);
  3529. if (crystal_hz == 0)
  3530. switch(model) {
  3531. case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
  3532. case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
  3533. case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
  3534. case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
  3535. crystal_hz = 24000000; /* 24.0 MHz */
  3536. break;
  3537. case INTEL_FAM6_SKYLAKE_X: /* SKX */
  3538. case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
  3539. crystal_hz = 25000000; /* 25.0 MHz */
  3540. break;
  3541. case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
  3542. case INTEL_FAM6_ATOM_GEMINI_LAKE:
  3543. crystal_hz = 19200000; /* 19.2 MHz */
  3544. break;
  3545. default:
  3546. crystal_hz = 0;
  3547. }
  3548. if (crystal_hz) {
  3549. tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
  3550. if (!quiet)
  3551. fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
  3552. tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
  3553. }
  3554. }
  3555. }
  3556. if (max_level >= 0x16) {
  3557. unsigned int base_mhz, max_mhz, bus_mhz, edx;
  3558. /*
  3559. * CPUID 16H Base MHz, Max MHz, Bus MHz
  3560. */
  3561. base_mhz = max_mhz = bus_mhz = edx = 0;
  3562. __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
  3563. if (!quiet)
  3564. fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
  3565. base_mhz, max_mhz, bus_mhz);
  3566. }
  3567. if (has_aperf)
  3568. aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
  3569. BIC_PRESENT(BIC_IRQ);
  3570. BIC_PRESENT(BIC_TSC_MHz);
  3571. if (probe_nhm_msrs(family, model)) {
  3572. do_nhm_platform_info = 1;
  3573. BIC_PRESENT(BIC_CPU_c1);
  3574. BIC_PRESENT(BIC_CPU_c3);
  3575. BIC_PRESENT(BIC_CPU_c6);
  3576. BIC_PRESENT(BIC_SMI);
  3577. }
  3578. do_snb_cstates = has_snb_msrs(family, model);
  3579. if (do_snb_cstates)
  3580. BIC_PRESENT(BIC_CPU_c7);
  3581. do_irtl_snb = has_snb_msrs(family, model);
  3582. if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
  3583. BIC_PRESENT(BIC_Pkgpc2);
  3584. if (pkg_cstate_limit >= PCL__3)
  3585. BIC_PRESENT(BIC_Pkgpc3);
  3586. if (pkg_cstate_limit >= PCL__6)
  3587. BIC_PRESENT(BIC_Pkgpc6);
  3588. if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
  3589. BIC_PRESENT(BIC_Pkgpc7);
  3590. if (has_slv_msrs(family, model)) {
  3591. BIC_NOT_PRESENT(BIC_Pkgpc2);
  3592. BIC_NOT_PRESENT(BIC_Pkgpc3);
  3593. BIC_PRESENT(BIC_Pkgpc6);
  3594. BIC_NOT_PRESENT(BIC_Pkgpc7);
  3595. BIC_PRESENT(BIC_Mod_c6);
  3596. use_c1_residency_msr = 1;
  3597. }
  3598. if (is_dnv(family, model)) {
  3599. BIC_PRESENT(BIC_CPU_c1);
  3600. BIC_NOT_PRESENT(BIC_CPU_c3);
  3601. BIC_NOT_PRESENT(BIC_Pkgpc3);
  3602. BIC_NOT_PRESENT(BIC_CPU_c7);
  3603. BIC_NOT_PRESENT(BIC_Pkgpc7);
  3604. use_c1_residency_msr = 1;
  3605. }
  3606. if (is_skx(family, model)) {
  3607. BIC_NOT_PRESENT(BIC_CPU_c3);
  3608. BIC_NOT_PRESENT(BIC_Pkgpc3);
  3609. BIC_NOT_PRESENT(BIC_CPU_c7);
  3610. BIC_NOT_PRESENT(BIC_Pkgpc7);
  3611. }
  3612. if (is_bdx(family, model)) {
  3613. BIC_NOT_PRESENT(BIC_CPU_c7);
  3614. BIC_NOT_PRESENT(BIC_Pkgpc7);
  3615. }
  3616. if (has_hsw_msrs(family, model)) {
  3617. BIC_PRESENT(BIC_Pkgpc8);
  3618. BIC_PRESENT(BIC_Pkgpc9);
  3619. BIC_PRESENT(BIC_Pkgpc10);
  3620. }
  3621. do_irtl_hsw = has_hsw_msrs(family, model);
  3622. if (has_skl_msrs(family, model)) {
  3623. BIC_PRESENT(BIC_Totl_c0);
  3624. BIC_PRESENT(BIC_Any_c0);
  3625. BIC_PRESENT(BIC_GFX_c0);
  3626. BIC_PRESENT(BIC_CPUGFX);
  3627. }
  3628. do_slm_cstates = is_slm(family, model);
  3629. do_knl_cstates = is_knl(family, model);
  3630. if (!quiet)
  3631. decode_misc_pwr_mgmt_msr();
  3632. if (!quiet && has_slv_msrs(family, model))
  3633. decode_c6_demotion_policy_msr();
  3634. rapl_probe(family, model);
  3635. perf_limit_reasons_probe(family, model);
  3636. if (!quiet)
  3637. dump_cstate_pstate_config_info(family, model);
  3638. if (!quiet)
  3639. dump_sysfs_cstate_config();
  3640. if (!quiet)
  3641. dump_sysfs_pstate_config();
  3642. if (has_skl_msrs(family, model))
  3643. calculate_tsc_tweak();
  3644. if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
  3645. BIC_PRESENT(BIC_GFX_rc6);
  3646. if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
  3647. BIC_PRESENT(BIC_GFXMHz);
  3648. if (!quiet)
  3649. decode_misc_feature_control();
  3650. return;
  3651. }
  3652. /*
  3653. * in /dev/cpu/ return success for names that are numbers
  3654. * ie. filter out ".", "..", "microcode".
  3655. */
  3656. int dir_filter(const struct dirent *dirp)
  3657. {
  3658. if (isdigit(dirp->d_name[0]))
  3659. return 1;
  3660. else
  3661. return 0;
  3662. }
  3663. int open_dev_cpu_msr(int dummy1)
  3664. {
  3665. return 0;
  3666. }
  3667. void topology_probe()
  3668. {
  3669. int i;
  3670. int max_core_id = 0;
  3671. int max_package_id = 0;
  3672. int max_siblings = 0;
  3673. struct cpu_topology {
  3674. int core_id;
  3675. int physical_package_id;
  3676. } *cpus;
  3677. /* Initialize num_cpus, max_cpu_num */
  3678. topo.num_cpus = 0;
  3679. topo.max_cpu_num = 0;
  3680. for_all_proc_cpus(count_cpus);
  3681. if (!summary_only && topo.num_cpus > 1)
  3682. BIC_PRESENT(BIC_CPU);
  3683. if (debug > 1)
  3684. fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
  3685. cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
  3686. if (cpus == NULL)
  3687. err(1, "calloc cpus");
  3688. /*
  3689. * Allocate and initialize cpu_present_set
  3690. */
  3691. cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
  3692. if (cpu_present_set == NULL)
  3693. err(3, "CPU_ALLOC");
  3694. cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  3695. CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
  3696. for_all_proc_cpus(mark_cpu_present);
  3697. /*
  3698. * Validate that all cpus in cpu_subset are also in cpu_present_set
  3699. */
  3700. for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
  3701. if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
  3702. if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
  3703. err(1, "cpu%d not present", i);
  3704. }
  3705. /*
  3706. * Allocate and initialize cpu_affinity_set
  3707. */
  3708. cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
  3709. if (cpu_affinity_set == NULL)
  3710. err(3, "CPU_ALLOC");
  3711. cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  3712. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  3713. /*
  3714. * For online cpus
  3715. * find max_core_id, max_package_id
  3716. */
  3717. for (i = 0; i <= topo.max_cpu_num; ++i) {
  3718. int siblings;
  3719. if (cpu_is_not_present(i)) {
  3720. if (debug > 1)
  3721. fprintf(outf, "cpu%d NOT PRESENT\n", i);
  3722. continue;
  3723. }
  3724. cpus[i].core_id = get_core_id(i);
  3725. if (cpus[i].core_id > max_core_id)
  3726. max_core_id = cpus[i].core_id;
  3727. cpus[i].physical_package_id = get_physical_package_id(i);
  3728. if (cpus[i].physical_package_id > max_package_id)
  3729. max_package_id = cpus[i].physical_package_id;
  3730. siblings = get_num_ht_siblings(i);
  3731. if (siblings > max_siblings)
  3732. max_siblings = siblings;
  3733. if (debug > 1)
  3734. fprintf(outf, "cpu %d pkg %d core %d\n",
  3735. i, cpus[i].physical_package_id, cpus[i].core_id);
  3736. }
  3737. topo.num_cores_per_pkg = max_core_id + 1;
  3738. if (debug > 1)
  3739. fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
  3740. max_core_id, topo.num_cores_per_pkg);
  3741. if (!summary_only && topo.num_cores_per_pkg > 1)
  3742. BIC_PRESENT(BIC_Core);
  3743. topo.num_packages = max_package_id + 1;
  3744. if (debug > 1)
  3745. fprintf(outf, "max_package_id %d, sizing for %d packages\n",
  3746. max_package_id, topo.num_packages);
  3747. if (!summary_only && topo.num_packages > 1)
  3748. BIC_PRESENT(BIC_Package);
  3749. topo.num_threads_per_core = max_siblings;
  3750. if (debug > 1)
  3751. fprintf(outf, "max_siblings %d\n", max_siblings);
  3752. free(cpus);
  3753. }
  3754. void
  3755. allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
  3756. {
  3757. int i;
  3758. *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
  3759. topo.num_packages, sizeof(struct thread_data));
  3760. if (*t == NULL)
  3761. goto error;
  3762. for (i = 0; i < topo.num_threads_per_core *
  3763. topo.num_cores_per_pkg * topo.num_packages; i++)
  3764. (*t)[i].cpu_id = -1;
  3765. *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
  3766. sizeof(struct core_data));
  3767. if (*c == NULL)
  3768. goto error;
  3769. for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
  3770. (*c)[i].core_id = -1;
  3771. *p = calloc(topo.num_packages, sizeof(struct pkg_data));
  3772. if (*p == NULL)
  3773. goto error;
  3774. for (i = 0; i < topo.num_packages; i++)
  3775. (*p)[i].package_id = i;
  3776. return;
  3777. error:
  3778. err(1, "calloc counters");
  3779. }
  3780. /*
  3781. * init_counter()
  3782. *
  3783. * set cpu_id, core_num, pkg_num
  3784. * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
  3785. *
  3786. * increment topo.num_cores when 1st core in pkg seen
  3787. */
  3788. void init_counter(struct thread_data *thread_base, struct core_data *core_base,
  3789. struct pkg_data *pkg_base, int thread_num, int core_num,
  3790. int pkg_num, int cpu_id)
  3791. {
  3792. struct thread_data *t;
  3793. struct core_data *c;
  3794. struct pkg_data *p;
  3795. t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
  3796. c = GET_CORE(core_base, core_num, pkg_num);
  3797. p = GET_PKG(pkg_base, pkg_num);
  3798. t->cpu_id = cpu_id;
  3799. if (thread_num == 0) {
  3800. t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
  3801. if (cpu_is_first_core_in_package(cpu_id))
  3802. t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
  3803. }
  3804. c->core_id = core_num;
  3805. p->package_id = pkg_num;
  3806. }
  3807. int initialize_counters(int cpu_id)
  3808. {
  3809. int my_thread_id, my_core_id, my_package_id;
  3810. my_package_id = get_physical_package_id(cpu_id);
  3811. my_core_id = get_core_id(cpu_id);
  3812. my_thread_id = get_cpu_position_in_core(cpu_id);
  3813. if (!my_thread_id)
  3814. topo.num_cores++;
  3815. init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  3816. init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  3817. return 0;
  3818. }
  3819. void allocate_output_buffer()
  3820. {
  3821. output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
  3822. outp = output_buffer;
  3823. if (outp == NULL)
  3824. err(-1, "calloc output buffer");
  3825. }
  3826. void allocate_fd_percpu(void)
  3827. {
  3828. fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
  3829. if (fd_percpu == NULL)
  3830. err(-1, "calloc fd_percpu");
  3831. }
  3832. void allocate_irq_buffers(void)
  3833. {
  3834. irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
  3835. if (irq_column_2_cpu == NULL)
  3836. err(-1, "calloc %d", topo.num_cpus);
  3837. irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
  3838. if (irqs_per_cpu == NULL)
  3839. err(-1, "calloc %d", topo.max_cpu_num + 1);
  3840. }
  3841. void setup_all_buffers(void)
  3842. {
  3843. topology_probe();
  3844. allocate_irq_buffers();
  3845. allocate_fd_percpu();
  3846. allocate_counters(&thread_even, &core_even, &package_even);
  3847. allocate_counters(&thread_odd, &core_odd, &package_odd);
  3848. allocate_output_buffer();
  3849. for_all_proc_cpus(initialize_counters);
  3850. }
  3851. void set_base_cpu(void)
  3852. {
  3853. base_cpu = sched_getcpu();
  3854. if (base_cpu < 0)
  3855. err(-ENODEV, "No valid cpus found");
  3856. if (debug > 1)
  3857. fprintf(outf, "base_cpu = %d\n", base_cpu);
  3858. }
  3859. void turbostat_init()
  3860. {
  3861. setup_all_buffers();
  3862. set_base_cpu();
  3863. check_dev_msr();
  3864. check_permissions();
  3865. process_cpuid();
  3866. if (!quiet)
  3867. for_all_cpus(print_hwp, ODD_COUNTERS);
  3868. if (!quiet)
  3869. for_all_cpus(print_epb, ODD_COUNTERS);
  3870. if (!quiet)
  3871. for_all_cpus(print_perf_limit, ODD_COUNTERS);
  3872. if (!quiet)
  3873. for_all_cpus(print_rapl, ODD_COUNTERS);
  3874. for_all_cpus(set_temperature_target, ODD_COUNTERS);
  3875. if (!quiet)
  3876. for_all_cpus(print_thermal, ODD_COUNTERS);
  3877. if (!quiet && do_irtl_snb)
  3878. print_irtl();
  3879. }
  3880. int fork_it(char **argv)
  3881. {
  3882. pid_t child_pid;
  3883. int status;
  3884. snapshot_proc_sysfs_files();
  3885. status = for_all_cpus(get_counters, EVEN_COUNTERS);
  3886. if (status)
  3887. exit(status);
  3888. /* clear affinity side-effect of get_counters() */
  3889. sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
  3890. gettimeofday(&tv_even, (struct timezone *)NULL);
  3891. child_pid = fork();
  3892. if (!child_pid) {
  3893. /* child */
  3894. execvp(argv[0], argv);
  3895. err(errno, "exec %s", argv[0]);
  3896. } else {
  3897. /* parent */
  3898. if (child_pid == -1)
  3899. err(1, "fork");
  3900. signal(SIGINT, SIG_IGN);
  3901. signal(SIGQUIT, SIG_IGN);
  3902. if (waitpid(child_pid, &status, 0) == -1)
  3903. err(status, "waitpid");
  3904. }
  3905. /*
  3906. * n.b. fork_it() does not check for errors from for_all_cpus()
  3907. * because re-starting is problematic when forking
  3908. */
  3909. snapshot_proc_sysfs_files();
  3910. for_all_cpus(get_counters, ODD_COUNTERS);
  3911. gettimeofday(&tv_odd, (struct timezone *)NULL);
  3912. timersub(&tv_odd, &tv_even, &tv_delta);
  3913. if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
  3914. fprintf(outf, "%s: Counter reset detected\n", progname);
  3915. else {
  3916. compute_average(EVEN_COUNTERS);
  3917. format_all_counters(EVEN_COUNTERS);
  3918. }
  3919. fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
  3920. flush_output_stderr();
  3921. return status;
  3922. }
  3923. int get_and_dump_counters(void)
  3924. {
  3925. int status;
  3926. snapshot_proc_sysfs_files();
  3927. status = for_all_cpus(get_counters, ODD_COUNTERS);
  3928. if (status)
  3929. return status;
  3930. status = for_all_cpus(dump_counters, ODD_COUNTERS);
  3931. if (status)
  3932. return status;
  3933. flush_output_stdout();
  3934. return status;
  3935. }
  3936. void print_version() {
  3937. fprintf(outf, "turbostat version 17.06.23"
  3938. " - Len Brown <lenb@kernel.org>\n");
  3939. }
  3940. int add_counter(unsigned int msr_num, char *path, char *name,
  3941. unsigned int width, enum counter_scope scope,
  3942. enum counter_type type, enum counter_format format, int flags)
  3943. {
  3944. struct msr_counter *msrp;
  3945. msrp = calloc(1, sizeof(struct msr_counter));
  3946. if (msrp == NULL) {
  3947. perror("calloc");
  3948. exit(1);
  3949. }
  3950. msrp->msr_num = msr_num;
  3951. strncpy(msrp->name, name, NAME_BYTES);
  3952. if (path)
  3953. strncpy(msrp->path, path, PATH_BYTES);
  3954. msrp->width = width;
  3955. msrp->type = type;
  3956. msrp->format = format;
  3957. msrp->flags = flags;
  3958. switch (scope) {
  3959. case SCOPE_CPU:
  3960. msrp->next = sys.tp;
  3961. sys.tp = msrp;
  3962. sys.added_thread_counters++;
  3963. if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
  3964. fprintf(stderr, "exceeded max %d added thread counters\n",
  3965. MAX_ADDED_COUNTERS);
  3966. exit(-1);
  3967. }
  3968. break;
  3969. case SCOPE_CORE:
  3970. msrp->next = sys.cp;
  3971. sys.cp = msrp;
  3972. sys.added_core_counters++;
  3973. if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
  3974. fprintf(stderr, "exceeded max %d added core counters\n",
  3975. MAX_ADDED_COUNTERS);
  3976. exit(-1);
  3977. }
  3978. break;
  3979. case SCOPE_PACKAGE:
  3980. msrp->next = sys.pp;
  3981. sys.pp = msrp;
  3982. sys.added_package_counters++;
  3983. if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
  3984. fprintf(stderr, "exceeded max %d added package counters\n",
  3985. MAX_ADDED_COUNTERS);
  3986. exit(-1);
  3987. }
  3988. break;
  3989. }
  3990. return 0;
  3991. }
  3992. void parse_add_command(char *add_command)
  3993. {
  3994. int msr_num = 0;
  3995. char *path = NULL;
  3996. char name_buffer[NAME_BYTES] = "";
  3997. int width = 64;
  3998. int fail = 0;
  3999. enum counter_scope scope = SCOPE_CPU;
  4000. enum counter_type type = COUNTER_CYCLES;
  4001. enum counter_format format = FORMAT_DELTA;
  4002. while (add_command) {
  4003. if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
  4004. goto next;
  4005. if (sscanf(add_command, "msr%d", &msr_num) == 1)
  4006. goto next;
  4007. if (*add_command == '/') {
  4008. path = add_command;
  4009. goto next;
  4010. }
  4011. if (sscanf(add_command, "u%d", &width) == 1) {
  4012. if ((width == 32) || (width == 64))
  4013. goto next;
  4014. width = 64;
  4015. }
  4016. if (!strncmp(add_command, "cpu", strlen("cpu"))) {
  4017. scope = SCOPE_CPU;
  4018. goto next;
  4019. }
  4020. if (!strncmp(add_command, "core", strlen("core"))) {
  4021. scope = SCOPE_CORE;
  4022. goto next;
  4023. }
  4024. if (!strncmp(add_command, "package", strlen("package"))) {
  4025. scope = SCOPE_PACKAGE;
  4026. goto next;
  4027. }
  4028. if (!strncmp(add_command, "cycles", strlen("cycles"))) {
  4029. type = COUNTER_CYCLES;
  4030. goto next;
  4031. }
  4032. if (!strncmp(add_command, "seconds", strlen("seconds"))) {
  4033. type = COUNTER_SECONDS;
  4034. goto next;
  4035. }
  4036. if (!strncmp(add_command, "usec", strlen("usec"))) {
  4037. type = COUNTER_USEC;
  4038. goto next;
  4039. }
  4040. if (!strncmp(add_command, "raw", strlen("raw"))) {
  4041. format = FORMAT_RAW;
  4042. goto next;
  4043. }
  4044. if (!strncmp(add_command, "delta", strlen("delta"))) {
  4045. format = FORMAT_DELTA;
  4046. goto next;
  4047. }
  4048. if (!strncmp(add_command, "percent", strlen("percent"))) {
  4049. format = FORMAT_PERCENT;
  4050. goto next;
  4051. }
  4052. if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
  4053. char *eos;
  4054. eos = strchr(name_buffer, ',');
  4055. if (eos)
  4056. *eos = '\0';
  4057. goto next;
  4058. }
  4059. next:
  4060. add_command = strchr(add_command, ',');
  4061. if (add_command) {
  4062. *add_command = '\0';
  4063. add_command++;
  4064. }
  4065. }
  4066. if ((msr_num == 0) && (path == NULL)) {
  4067. fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
  4068. fail++;
  4069. }
  4070. /* generate default column header */
  4071. if (*name_buffer == '\0') {
  4072. if (width == 32)
  4073. sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
  4074. else
  4075. sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
  4076. }
  4077. if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
  4078. fail++;
  4079. if (fail) {
  4080. help();
  4081. exit(1);
  4082. }
  4083. }
  4084. int is_deferred_skip(char *name)
  4085. {
  4086. int i;
  4087. for (i = 0; i < deferred_skip_index; ++i)
  4088. if (!strcmp(name, deferred_skip_names[i]))
  4089. return 1;
  4090. return 0;
  4091. }
  4092. void probe_sysfs(void)
  4093. {
  4094. char path[64];
  4095. char name_buf[16];
  4096. FILE *input;
  4097. int state;
  4098. char *sp;
  4099. if (!DO_BIC(BIC_sysfs))
  4100. return;
  4101. for (state = 10; state > 0; --state) {
  4102. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
  4103. base_cpu, state);
  4104. input = fopen(path, "r");
  4105. if (input == NULL)
  4106. continue;
  4107. fgets(name_buf, sizeof(name_buf), input);
  4108. /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
  4109. sp = strchr(name_buf, '-');
  4110. if (!sp)
  4111. sp = strchrnul(name_buf, '\n');
  4112. *sp = '%';
  4113. *(sp + 1) = '\0';
  4114. fclose(input);
  4115. sprintf(path, "cpuidle/state%d/time", state);
  4116. if (is_deferred_skip(name_buf))
  4117. continue;
  4118. add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
  4119. FORMAT_PERCENT, SYSFS_PERCPU);
  4120. }
  4121. for (state = 10; state > 0; --state) {
  4122. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
  4123. base_cpu, state);
  4124. input = fopen(path, "r");
  4125. if (input == NULL)
  4126. continue;
  4127. fgets(name_buf, sizeof(name_buf), input);
  4128. /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
  4129. sp = strchr(name_buf, '-');
  4130. if (!sp)
  4131. sp = strchrnul(name_buf, '\n');
  4132. *sp = '\0';
  4133. fclose(input);
  4134. sprintf(path, "cpuidle/state%d/usage", state);
  4135. if (is_deferred_skip(name_buf))
  4136. continue;
  4137. add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
  4138. FORMAT_DELTA, SYSFS_PERCPU);
  4139. }
  4140. }
  4141. /*
  4142. * parse cpuset with following syntax
  4143. * 1,2,4..6,8-10 and set bits in cpu_subset
  4144. */
  4145. void parse_cpu_command(char *optarg)
  4146. {
  4147. unsigned int start, end;
  4148. char *next;
  4149. if (!strcmp(optarg, "core")) {
  4150. if (cpu_subset)
  4151. goto error;
  4152. show_core_only++;
  4153. return;
  4154. }
  4155. if (!strcmp(optarg, "package")) {
  4156. if (cpu_subset)
  4157. goto error;
  4158. show_pkg_only++;
  4159. return;
  4160. }
  4161. if (show_core_only || show_pkg_only)
  4162. goto error;
  4163. cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
  4164. if (cpu_subset == NULL)
  4165. err(3, "CPU_ALLOC");
  4166. cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
  4167. CPU_ZERO_S(cpu_subset_size, cpu_subset);
  4168. next = optarg;
  4169. while (next && *next) {
  4170. if (*next == '-') /* no negative cpu numbers */
  4171. goto error;
  4172. start = strtoul(next, &next, 10);
  4173. if (start >= CPU_SUBSET_MAXCPUS)
  4174. goto error;
  4175. CPU_SET_S(start, cpu_subset_size, cpu_subset);
  4176. if (*next == '\0')
  4177. break;
  4178. if (*next == ',') {
  4179. next += 1;
  4180. continue;
  4181. }
  4182. if (*next == '-') {
  4183. next += 1; /* start range */
  4184. } else if (*next == '.') {
  4185. next += 1;
  4186. if (*next == '.')
  4187. next += 1; /* start range */
  4188. else
  4189. goto error;
  4190. }
  4191. end = strtoul(next, &next, 10);
  4192. if (end <= start)
  4193. goto error;
  4194. while (++start <= end) {
  4195. if (start >= CPU_SUBSET_MAXCPUS)
  4196. goto error;
  4197. CPU_SET_S(start, cpu_subset_size, cpu_subset);
  4198. }
  4199. if (*next == ',')
  4200. next += 1;
  4201. else if (*next != '\0')
  4202. goto error;
  4203. }
  4204. return;
  4205. error:
  4206. fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
  4207. help();
  4208. exit(-1);
  4209. }
  4210. int shown;
  4211. /*
  4212. * parse_show_hide() - process cmdline to set default counter action
  4213. */
  4214. void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
  4215. {
  4216. /*
  4217. * --show: show only those specified
  4218. * The 1st invocation will clear and replace the enabled mask
  4219. * subsequent invocations can add to it.
  4220. */
  4221. if (new_mode == SHOW_LIST) {
  4222. if (shown == 0)
  4223. bic_enabled = bic_lookup(optarg, new_mode);
  4224. else
  4225. bic_enabled |= bic_lookup(optarg, new_mode);
  4226. shown = 1;
  4227. return;
  4228. }
  4229. /*
  4230. * --hide: do not show those specified
  4231. * multiple invocations simply clear more bits in enabled mask
  4232. */
  4233. bic_enabled &= ~bic_lookup(optarg, new_mode);
  4234. }
  4235. void cmdline(int argc, char **argv)
  4236. {
  4237. int opt;
  4238. int option_index = 0;
  4239. static struct option long_options[] = {
  4240. {"add", required_argument, 0, 'a'},
  4241. {"cpu", required_argument, 0, 'c'},
  4242. {"Dump", no_argument, 0, 'D'},
  4243. {"debug", no_argument, 0, 'd'}, /* internal, not documented */
  4244. {"interval", required_argument, 0, 'i'},
  4245. {"help", no_argument, 0, 'h'},
  4246. {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
  4247. {"Joules", no_argument, 0, 'J'},
  4248. {"list", no_argument, 0, 'l'},
  4249. {"migrate", no_argument, 0, 'm'},
  4250. {"out", required_argument, 0, 'o'},
  4251. {"quiet", no_argument, 0, 'q'},
  4252. {"show", required_argument, 0, 's'},
  4253. {"Summary", no_argument, 0, 'S'},
  4254. {"TCC", required_argument, 0, 'T'},
  4255. {"version", no_argument, 0, 'v' },
  4256. {0, 0, 0, 0 }
  4257. };
  4258. progname = argv[0];
  4259. while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:Jmo:qST:v",
  4260. long_options, &option_index)) != -1) {
  4261. switch (opt) {
  4262. case 'a':
  4263. parse_add_command(optarg);
  4264. break;
  4265. case 'c':
  4266. parse_cpu_command(optarg);
  4267. break;
  4268. case 'D':
  4269. dump_only++;
  4270. break;
  4271. case 'd':
  4272. debug++;
  4273. break;
  4274. case 'H':
  4275. parse_show_hide(optarg, HIDE_LIST);
  4276. break;
  4277. case 'h':
  4278. default:
  4279. help();
  4280. exit(1);
  4281. case 'i':
  4282. {
  4283. double interval = strtod(optarg, NULL);
  4284. if (interval < 0.001) {
  4285. fprintf(outf, "interval %f seconds is too small\n",
  4286. interval);
  4287. exit(2);
  4288. }
  4289. interval_ts.tv_sec = interval;
  4290. interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
  4291. }
  4292. break;
  4293. case 'J':
  4294. rapl_joules++;
  4295. break;
  4296. case 'l':
  4297. list_header_only++;
  4298. quiet++;
  4299. break;
  4300. case 'm':
  4301. do_migrate = 1;
  4302. break;
  4303. case 'o':
  4304. outf = fopen_or_die(optarg, "w");
  4305. break;
  4306. case 'q':
  4307. quiet = 1;
  4308. break;
  4309. case 's':
  4310. parse_show_hide(optarg, SHOW_LIST);
  4311. break;
  4312. case 'S':
  4313. summary_only++;
  4314. break;
  4315. case 'T':
  4316. tcc_activation_temp_override = atoi(optarg);
  4317. break;
  4318. case 'v':
  4319. print_version();
  4320. exit(0);
  4321. break;
  4322. }
  4323. }
  4324. }
  4325. int main(int argc, char **argv)
  4326. {
  4327. outf = stderr;
  4328. cmdline(argc, argv);
  4329. if (!quiet)
  4330. print_version();
  4331. probe_sysfs();
  4332. turbostat_init();
  4333. /* dump counters and exit */
  4334. if (dump_only)
  4335. return get_and_dump_counters();
  4336. /* list header and exit */
  4337. if (list_header_only) {
  4338. print_header(",");
  4339. flush_output_stdout();
  4340. return 0;
  4341. }
  4342. /*
  4343. * if any params left, it must be a command to fork
  4344. */
  4345. if (argc - optind)
  4346. return fork_it(argv + optind);
  4347. else
  4348. turbostat_loop();
  4349. return 0;
  4350. }