turbostat.c 146 KB

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