turbostat.c 81 KB

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