turbostat.c 145 KB

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