turbostat.c 69 KB

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