turbostat.c 132 KB

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