turbostat.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /*
  2. * turbostat -- show CPU frequency and C-state residency
  3. * on modern Intel turbo-capable processors.
  4. *
  5. * Copyright (c) 2012 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 <asm/msr.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <sys/stat.h>
  28. #include <sys/resource.h>
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #include <sys/time.h>
  32. #include <stdlib.h>
  33. #include <dirent.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <sched.h>
  37. char *proc_stat = "/proc/stat";
  38. unsigned int interval_sec = 5; /* set with -i interval_sec */
  39. unsigned int verbose; /* set with -v */
  40. unsigned int rapl_verbose; /* set with -R */
  41. unsigned int thermal_verbose; /* set with -T */
  42. unsigned int summary_only; /* set with -s */
  43. unsigned int skip_c0;
  44. unsigned int skip_c1;
  45. unsigned int do_nhm_cstates;
  46. unsigned int do_snb_cstates;
  47. unsigned int do_c8_c9_c10;
  48. unsigned int has_aperf;
  49. unsigned int has_epb;
  50. unsigned int units = 1000000000; /* Ghz etc */
  51. unsigned int genuine_intel;
  52. unsigned int has_invariant_tsc;
  53. unsigned int do_nehalem_platform_info;
  54. unsigned int do_nehalem_turbo_ratio_limit;
  55. unsigned int do_ivt_turbo_ratio_limit;
  56. unsigned int extra_msr_offset32;
  57. unsigned int extra_msr_offset64;
  58. unsigned int extra_delta_offset32;
  59. unsigned int extra_delta_offset64;
  60. int do_smi;
  61. double bclk;
  62. unsigned int show_pkg;
  63. unsigned int show_core;
  64. unsigned int show_cpu;
  65. unsigned int show_pkg_only;
  66. unsigned int show_core_only;
  67. char *output_buffer, *outp;
  68. unsigned int do_rapl;
  69. unsigned int do_dts;
  70. unsigned int do_ptm;
  71. unsigned int tcc_activation_temp;
  72. unsigned int tcc_activation_temp_override;
  73. double rapl_power_units, rapl_energy_units, rapl_time_units;
  74. double rapl_joule_counter_range;
  75. #define RAPL_PKG (1 << 0)
  76. #define RAPL_CORES (1 << 1)
  77. #define RAPL_GFX (1 << 2)
  78. #define RAPL_DRAM (1 << 3)
  79. #define RAPL_PKG_PERF_STATUS (1 << 4)
  80. #define RAPL_DRAM_PERF_STATUS (1 << 5)
  81. #define TJMAX_DEFAULT 100
  82. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  83. int aperf_mperf_unstable;
  84. int backwards_count;
  85. char *progname;
  86. cpu_set_t *cpu_present_set, *cpu_affinity_set;
  87. size_t cpu_present_setsize, cpu_affinity_setsize;
  88. struct thread_data {
  89. unsigned long long tsc;
  90. unsigned long long aperf;
  91. unsigned long long mperf;
  92. unsigned long long c1; /* derived */
  93. unsigned long long extra_msr64;
  94. unsigned long long extra_delta64;
  95. unsigned long long extra_msr32;
  96. unsigned long long extra_delta32;
  97. unsigned int smi_count;
  98. unsigned int cpu_id;
  99. unsigned int flags;
  100. #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
  101. #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
  102. } *thread_even, *thread_odd;
  103. struct core_data {
  104. unsigned long long c3;
  105. unsigned long long c6;
  106. unsigned long long c7;
  107. unsigned int core_temp_c;
  108. unsigned int core_id;
  109. } *core_even, *core_odd;
  110. struct pkg_data {
  111. unsigned long long pc2;
  112. unsigned long long pc3;
  113. unsigned long long pc6;
  114. unsigned long long pc7;
  115. unsigned long long pc8;
  116. unsigned long long pc9;
  117. unsigned long long pc10;
  118. unsigned int package_id;
  119. unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
  120. unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
  121. unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
  122. unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
  123. unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
  124. unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
  125. unsigned int pkg_temp_c;
  126. } *package_even, *package_odd;
  127. #define ODD_COUNTERS thread_odd, core_odd, package_odd
  128. #define EVEN_COUNTERS thread_even, core_even, package_even
  129. #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
  130. (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
  131. topo.num_threads_per_core + \
  132. (core_no) * topo.num_threads_per_core + (thread_no))
  133. #define GET_CORE(core_base, core_no, pkg_no) \
  134. (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
  135. #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
  136. struct system_summary {
  137. struct thread_data threads;
  138. struct core_data cores;
  139. struct pkg_data packages;
  140. } sum, average;
  141. struct topo_params {
  142. int num_packages;
  143. int num_cpus;
  144. int num_cores;
  145. int max_cpu_num;
  146. int num_cores_per_pkg;
  147. int num_threads_per_core;
  148. } topo;
  149. struct timeval tv_even, tv_odd, tv_delta;
  150. void setup_all_buffers(void);
  151. int cpu_is_not_present(int cpu)
  152. {
  153. return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
  154. }
  155. /*
  156. * run func(thread, core, package) in topology order
  157. * skip non-present cpus
  158. */
  159. int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
  160. struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
  161. {
  162. int retval, pkg_no, core_no, thread_no;
  163. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  164. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  165. for (thread_no = 0; thread_no <
  166. topo.num_threads_per_core; ++thread_no) {
  167. struct thread_data *t;
  168. struct core_data *c;
  169. struct pkg_data *p;
  170. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  171. if (cpu_is_not_present(t->cpu_id))
  172. continue;
  173. c = GET_CORE(core_base, core_no, pkg_no);
  174. p = GET_PKG(pkg_base, pkg_no);
  175. retval = func(t, c, p);
  176. if (retval)
  177. return retval;
  178. }
  179. }
  180. }
  181. return 0;
  182. }
  183. int cpu_migrate(int cpu)
  184. {
  185. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  186. CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
  187. if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
  188. return -1;
  189. else
  190. return 0;
  191. }
  192. int get_msr(int cpu, off_t offset, unsigned long long *msr)
  193. {
  194. ssize_t retval;
  195. char pathname[32];
  196. int fd;
  197. sprintf(pathname, "/dev/cpu/%d/msr", cpu);
  198. fd = open(pathname, O_RDONLY);
  199. if (fd < 0)
  200. return -1;
  201. retval = pread(fd, msr, sizeof *msr, offset);
  202. close(fd);
  203. if (retval != sizeof *msr) {
  204. fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. void print_header(void)
  210. {
  211. if (show_pkg)
  212. outp += sprintf(outp, "pk");
  213. if (show_pkg)
  214. outp += sprintf(outp, " ");
  215. if (show_core)
  216. outp += sprintf(outp, "cor");
  217. if (show_cpu)
  218. outp += sprintf(outp, " CPU");
  219. if (show_pkg || show_core || show_cpu)
  220. outp += sprintf(outp, " ");
  221. if (do_nhm_cstates)
  222. outp += sprintf(outp, " %%c0");
  223. if (has_aperf)
  224. outp += sprintf(outp, " GHz");
  225. outp += sprintf(outp, " TSC");
  226. if (do_smi)
  227. outp += sprintf(outp, " SMI");
  228. if (extra_delta_offset32)
  229. outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
  230. if (extra_delta_offset64)
  231. outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
  232. if (extra_msr_offset32)
  233. outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
  234. if (extra_msr_offset64)
  235. outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
  236. if (do_nhm_cstates)
  237. outp += sprintf(outp, " %%c1");
  238. if (do_nhm_cstates)
  239. outp += sprintf(outp, " %%c3");
  240. if (do_nhm_cstates)
  241. outp += sprintf(outp, " %%c6");
  242. if (do_snb_cstates)
  243. outp += sprintf(outp, " %%c7");
  244. if (do_dts)
  245. outp += sprintf(outp, " CTMP");
  246. if (do_ptm)
  247. outp += sprintf(outp, " PTMP");
  248. if (do_snb_cstates)
  249. outp += sprintf(outp, " %%pc2");
  250. if (do_nhm_cstates)
  251. outp += sprintf(outp, " %%pc3");
  252. if (do_nhm_cstates)
  253. outp += sprintf(outp, " %%pc6");
  254. if (do_snb_cstates)
  255. outp += sprintf(outp, " %%pc7");
  256. if (do_c8_c9_c10) {
  257. outp += sprintf(outp, " %%pc8");
  258. outp += sprintf(outp, " %%pc9");
  259. outp += sprintf(outp, " %%pc10");
  260. }
  261. if (do_rapl & RAPL_PKG)
  262. outp += sprintf(outp, " Pkg_W");
  263. if (do_rapl & RAPL_CORES)
  264. outp += sprintf(outp, " Cor_W");
  265. if (do_rapl & RAPL_GFX)
  266. outp += sprintf(outp, " GFX_W");
  267. if (do_rapl & RAPL_DRAM)
  268. outp += sprintf(outp, " RAM_W");
  269. if (do_rapl & RAPL_PKG_PERF_STATUS)
  270. outp += sprintf(outp, " PKG_%%");
  271. if (do_rapl & RAPL_DRAM_PERF_STATUS)
  272. outp += sprintf(outp, " RAM_%%");
  273. outp += sprintf(outp, "\n");
  274. }
  275. int dump_counters(struct thread_data *t, struct core_data *c,
  276. struct pkg_data *p)
  277. {
  278. fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
  279. if (t) {
  280. fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
  281. fprintf(stderr, "TSC: %016llX\n", t->tsc);
  282. fprintf(stderr, "aperf: %016llX\n", t->aperf);
  283. fprintf(stderr, "mperf: %016llX\n", t->mperf);
  284. fprintf(stderr, "c1: %016llX\n", t->c1);
  285. fprintf(stderr, "msr0x%x: %08llX\n",
  286. extra_delta_offset32, t->extra_delta32);
  287. fprintf(stderr, "msr0x%x: %016llX\n",
  288. extra_delta_offset64, t->extra_delta64);
  289. fprintf(stderr, "msr0x%x: %08llX\n",
  290. extra_msr_offset32, t->extra_msr32);
  291. fprintf(stderr, "msr0x%x: %016llX\n",
  292. extra_msr_offset64, t->extra_msr64);
  293. if (do_smi)
  294. fprintf(stderr, "SMI: %08X\n", t->smi_count);
  295. }
  296. if (c) {
  297. fprintf(stderr, "core: %d\n", c->core_id);
  298. fprintf(stderr, "c3: %016llX\n", c->c3);
  299. fprintf(stderr, "c6: %016llX\n", c->c6);
  300. fprintf(stderr, "c7: %016llX\n", c->c7);
  301. fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
  302. }
  303. if (p) {
  304. fprintf(stderr, "package: %d\n", p->package_id);
  305. fprintf(stderr, "pc2: %016llX\n", p->pc2);
  306. fprintf(stderr, "pc3: %016llX\n", p->pc3);
  307. fprintf(stderr, "pc6: %016llX\n", p->pc6);
  308. fprintf(stderr, "pc7: %016llX\n", p->pc7);
  309. fprintf(stderr, "pc8: %016llX\n", p->pc8);
  310. fprintf(stderr, "pc9: %016llX\n", p->pc9);
  311. fprintf(stderr, "pc10: %016llX\n", p->pc10);
  312. fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
  313. fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
  314. fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
  315. fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
  316. fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
  317. fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
  318. fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
  319. }
  320. return 0;
  321. }
  322. /*
  323. * column formatting convention & formats
  324. * package: "pk" 2 columns %2d
  325. * core: "cor" 3 columns %3d
  326. * CPU: "CPU" 3 columns %3d
  327. * Pkg_W: %6.2
  328. * Cor_W: %6.2
  329. * GFX_W: %5.2
  330. * RAM_W: %5.2
  331. * GHz: "GHz" 3 columns %3.2
  332. * TSC: "TSC" 3 columns %3.2
  333. * SMI: "SMI" 4 columns %4d
  334. * percentage " %pc3" %6.2
  335. * Perf Status percentage: %5.2
  336. * "CTMP" 4 columns %4d
  337. */
  338. int format_counters(struct thread_data *t, struct core_data *c,
  339. struct pkg_data *p)
  340. {
  341. double interval_float;
  342. char *fmt5, *fmt6;
  343. /* if showing only 1st thread in core and this isn't one, bail out */
  344. if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  345. return 0;
  346. /* if showing only 1st thread in pkg and this isn't one, bail out */
  347. if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  348. return 0;
  349. interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
  350. /* topo columns, print blanks on 1st (average) line */
  351. if (t == &average.threads) {
  352. if (show_pkg)
  353. outp += sprintf(outp, " ");
  354. if (show_pkg && show_core)
  355. outp += sprintf(outp, " ");
  356. if (show_core)
  357. outp += sprintf(outp, " ");
  358. if (show_cpu)
  359. outp += sprintf(outp, " " " ");
  360. } else {
  361. if (show_pkg) {
  362. if (p)
  363. outp += sprintf(outp, "%2d", p->package_id);
  364. else
  365. outp += sprintf(outp, " ");
  366. }
  367. if (show_pkg && show_core)
  368. outp += sprintf(outp, " ");
  369. if (show_core) {
  370. if (c)
  371. outp += sprintf(outp, "%3d", c->core_id);
  372. else
  373. outp += sprintf(outp, " ");
  374. }
  375. if (show_cpu)
  376. outp += sprintf(outp, " %3d", t->cpu_id);
  377. }
  378. /* %c0 */
  379. if (do_nhm_cstates) {
  380. if (show_pkg || show_core || show_cpu)
  381. outp += sprintf(outp, " ");
  382. if (!skip_c0)
  383. outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
  384. else
  385. outp += sprintf(outp, " ****");
  386. }
  387. /* GHz */
  388. if (has_aperf) {
  389. if (!aperf_mperf_unstable) {
  390. outp += sprintf(outp, " %3.2f",
  391. 1.0 * t->tsc / units * t->aperf /
  392. t->mperf / interval_float);
  393. } else {
  394. if (t->aperf > t->tsc || t->mperf > t->tsc) {
  395. outp += sprintf(outp, " ***");
  396. } else {
  397. outp += sprintf(outp, "%3.1f*",
  398. 1.0 * t->tsc /
  399. units * t->aperf /
  400. t->mperf / interval_float);
  401. }
  402. }
  403. }
  404. /* TSC */
  405. outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
  406. /* SMI */
  407. if (do_smi)
  408. outp += sprintf(outp, "%4d", t->smi_count);
  409. /* delta */
  410. if (extra_delta_offset32)
  411. outp += sprintf(outp, " %11llu", t->extra_delta32);
  412. /* DELTA */
  413. if (extra_delta_offset64)
  414. outp += sprintf(outp, " %11llu", t->extra_delta64);
  415. /* msr */
  416. if (extra_msr_offset32)
  417. outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
  418. /* MSR */
  419. if (extra_msr_offset64)
  420. outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
  421. if (do_nhm_cstates) {
  422. if (!skip_c1)
  423. outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
  424. else
  425. outp += sprintf(outp, " ****");
  426. }
  427. /* print per-core data only for 1st thread in core */
  428. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  429. goto done;
  430. if (do_nhm_cstates)
  431. outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
  432. if (do_nhm_cstates)
  433. outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
  434. if (do_snb_cstates)
  435. outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
  436. if (do_dts)
  437. outp += sprintf(outp, " %4d", c->core_temp_c);
  438. /* print per-package data only for 1st core in package */
  439. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  440. goto done;
  441. if (do_ptm)
  442. outp += sprintf(outp, " %4d", p->pkg_temp_c);
  443. if (do_snb_cstates)
  444. outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
  445. if (do_nhm_cstates)
  446. outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
  447. if (do_nhm_cstates)
  448. outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
  449. if (do_snb_cstates)
  450. outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
  451. if (do_c8_c9_c10) {
  452. outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
  453. outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
  454. outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
  455. }
  456. /*
  457. * If measurement interval exceeds minimum RAPL Joule Counter range,
  458. * indicate that results are suspect by printing "**" in fraction place.
  459. */
  460. if (interval_float < rapl_joule_counter_range) {
  461. fmt5 = " %5.2f";
  462. fmt6 = " %6.2f";
  463. } else {
  464. fmt5 = " %3.0f**";
  465. fmt6 = " %4.0f**";
  466. }
  467. if (do_rapl & RAPL_PKG)
  468. outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
  469. if (do_rapl & RAPL_CORES)
  470. outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
  471. if (do_rapl & RAPL_GFX)
  472. outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
  473. if (do_rapl & RAPL_DRAM)
  474. outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
  475. if (do_rapl & RAPL_PKG_PERF_STATUS )
  476. outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
  477. if (do_rapl & RAPL_DRAM_PERF_STATUS )
  478. outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
  479. done:
  480. outp += sprintf(outp, "\n");
  481. return 0;
  482. }
  483. void flush_stdout()
  484. {
  485. fputs(output_buffer, stdout);
  486. fflush(stdout);
  487. outp = output_buffer;
  488. }
  489. void flush_stderr()
  490. {
  491. fputs(output_buffer, stderr);
  492. outp = output_buffer;
  493. }
  494. void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  495. {
  496. static int printed;
  497. if (!printed || !summary_only)
  498. print_header();
  499. if (topo.num_cpus > 1)
  500. format_counters(&average.threads, &average.cores,
  501. &average.packages);
  502. printed = 1;
  503. if (summary_only)
  504. return;
  505. for_all_cpus(format_counters, t, c, p);
  506. }
  507. #define DELTA_WRAP32(new, old) \
  508. if (new > old) { \
  509. old = new - old; \
  510. } else { \
  511. old = 0x100000000 + new - old; \
  512. }
  513. void
  514. delta_package(struct pkg_data *new, struct pkg_data *old)
  515. {
  516. old->pc2 = new->pc2 - old->pc2;
  517. old->pc3 = new->pc3 - old->pc3;
  518. old->pc6 = new->pc6 - old->pc6;
  519. old->pc7 = new->pc7 - old->pc7;
  520. old->pc8 = new->pc8 - old->pc8;
  521. old->pc9 = new->pc9 - old->pc9;
  522. old->pc10 = new->pc10 - old->pc10;
  523. old->pkg_temp_c = new->pkg_temp_c;
  524. DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
  525. DELTA_WRAP32(new->energy_cores, old->energy_cores);
  526. DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
  527. DELTA_WRAP32(new->energy_dram, old->energy_dram);
  528. DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
  529. DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
  530. }
  531. void
  532. delta_core(struct core_data *new, struct core_data *old)
  533. {
  534. old->c3 = new->c3 - old->c3;
  535. old->c6 = new->c6 - old->c6;
  536. old->c7 = new->c7 - old->c7;
  537. old->core_temp_c = new->core_temp_c;
  538. }
  539. /*
  540. * old = new - old
  541. */
  542. void
  543. delta_thread(struct thread_data *new, struct thread_data *old,
  544. struct core_data *core_delta)
  545. {
  546. old->tsc = new->tsc - old->tsc;
  547. /* check for TSC < 1 Mcycles over interval */
  548. if (old->tsc < (1000 * 1000)) {
  549. fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
  550. fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
  551. fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
  552. exit(-3);
  553. }
  554. old->c1 = new->c1 - old->c1;
  555. if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
  556. old->aperf = new->aperf - old->aperf;
  557. old->mperf = new->mperf - old->mperf;
  558. } else {
  559. if (!aperf_mperf_unstable) {
  560. fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
  561. fprintf(stderr, "* Frequency results do not cover entire interval *\n");
  562. fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
  563. aperf_mperf_unstable = 1;
  564. }
  565. /*
  566. * mperf delta is likely a huge "positive" number
  567. * can not use it for calculating c0 time
  568. */
  569. skip_c0 = 1;
  570. skip_c1 = 1;
  571. }
  572. /*
  573. * As counter collection is not atomic,
  574. * it is possible for mperf's non-halted cycles + idle states
  575. * to exceed TSC's all cycles: show c1 = 0% in that case.
  576. */
  577. if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
  578. old->c1 = 0;
  579. else {
  580. /* normal case, derive c1 */
  581. old->c1 = old->tsc - old->mperf - core_delta->c3
  582. - core_delta->c6 - core_delta->c7;
  583. }
  584. if (old->mperf == 0) {
  585. if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
  586. old->mperf = 1; /* divide by 0 protection */
  587. }
  588. old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
  589. old->extra_delta32 &= 0xFFFFFFFF;
  590. old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
  591. /*
  592. * Extra MSR is just a snapshot, simply copy latest w/o subtracting
  593. */
  594. old->extra_msr32 = new->extra_msr32;
  595. old->extra_msr64 = new->extra_msr64;
  596. if (do_smi)
  597. old->smi_count = new->smi_count - old->smi_count;
  598. }
  599. int delta_cpu(struct thread_data *t, struct core_data *c,
  600. struct pkg_data *p, struct thread_data *t2,
  601. struct core_data *c2, struct pkg_data *p2)
  602. {
  603. /* calculate core delta only for 1st thread in core */
  604. if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
  605. delta_core(c, c2);
  606. /* always calculate thread delta */
  607. delta_thread(t, t2, c2); /* c2 is core delta */
  608. /* calculate package delta only for 1st core in package */
  609. if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
  610. delta_package(p, p2);
  611. return 0;
  612. }
  613. void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  614. {
  615. t->tsc = 0;
  616. t->aperf = 0;
  617. t->mperf = 0;
  618. t->c1 = 0;
  619. t->smi_count = 0;
  620. t->extra_delta32 = 0;
  621. t->extra_delta64 = 0;
  622. /* tells format_counters to dump all fields from this set */
  623. t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
  624. c->c3 = 0;
  625. c->c6 = 0;
  626. c->c7 = 0;
  627. c->core_temp_c = 0;
  628. p->pc2 = 0;
  629. p->pc3 = 0;
  630. p->pc6 = 0;
  631. p->pc7 = 0;
  632. p->pc8 = 0;
  633. p->pc9 = 0;
  634. p->pc10 = 0;
  635. p->energy_pkg = 0;
  636. p->energy_dram = 0;
  637. p->energy_cores = 0;
  638. p->energy_gfx = 0;
  639. p->rapl_pkg_perf_status = 0;
  640. p->rapl_dram_perf_status = 0;
  641. p->pkg_temp_c = 0;
  642. }
  643. int sum_counters(struct thread_data *t, struct core_data *c,
  644. struct pkg_data *p)
  645. {
  646. average.threads.tsc += t->tsc;
  647. average.threads.aperf += t->aperf;
  648. average.threads.mperf += t->mperf;
  649. average.threads.c1 += t->c1;
  650. average.threads.extra_delta32 += t->extra_delta32;
  651. average.threads.extra_delta64 += t->extra_delta64;
  652. /* sum per-core values only for 1st thread in core */
  653. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  654. return 0;
  655. average.cores.c3 += c->c3;
  656. average.cores.c6 += c->c6;
  657. average.cores.c7 += c->c7;
  658. average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
  659. /* sum per-pkg values only for 1st core in pkg */
  660. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  661. return 0;
  662. average.packages.pc2 += p->pc2;
  663. average.packages.pc3 += p->pc3;
  664. average.packages.pc6 += p->pc6;
  665. average.packages.pc7 += p->pc7;
  666. average.packages.pc8 += p->pc8;
  667. average.packages.pc9 += p->pc9;
  668. average.packages.pc10 += p->pc10;
  669. average.packages.energy_pkg += p->energy_pkg;
  670. average.packages.energy_dram += p->energy_dram;
  671. average.packages.energy_cores += p->energy_cores;
  672. average.packages.energy_gfx += p->energy_gfx;
  673. average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
  674. average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
  675. average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
  676. return 0;
  677. }
  678. /*
  679. * sum the counters for all cpus in the system
  680. * compute the weighted average
  681. */
  682. void compute_average(struct thread_data *t, struct core_data *c,
  683. struct pkg_data *p)
  684. {
  685. clear_counters(&average.threads, &average.cores, &average.packages);
  686. for_all_cpus(sum_counters, t, c, p);
  687. average.threads.tsc /= topo.num_cpus;
  688. average.threads.aperf /= topo.num_cpus;
  689. average.threads.mperf /= topo.num_cpus;
  690. average.threads.c1 /= topo.num_cpus;
  691. average.threads.extra_delta32 /= topo.num_cpus;
  692. average.threads.extra_delta32 &= 0xFFFFFFFF;
  693. average.threads.extra_delta64 /= topo.num_cpus;
  694. average.cores.c3 /= topo.num_cores;
  695. average.cores.c6 /= topo.num_cores;
  696. average.cores.c7 /= topo.num_cores;
  697. average.packages.pc2 /= topo.num_packages;
  698. average.packages.pc3 /= topo.num_packages;
  699. average.packages.pc6 /= topo.num_packages;
  700. average.packages.pc7 /= topo.num_packages;
  701. average.packages.pc8 /= topo.num_packages;
  702. average.packages.pc9 /= topo.num_packages;
  703. average.packages.pc10 /= topo.num_packages;
  704. }
  705. static unsigned long long rdtsc(void)
  706. {
  707. unsigned int low, high;
  708. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  709. return low | ((unsigned long long)high) << 32;
  710. }
  711. /*
  712. * get_counters(...)
  713. * migrate to cpu
  714. * acquire and record local counters for that cpu
  715. */
  716. int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  717. {
  718. int cpu = t->cpu_id;
  719. unsigned long long msr;
  720. if (cpu_migrate(cpu)) {
  721. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  722. return -1;
  723. }
  724. t->tsc = rdtsc(); /* we are running on local CPU of interest */
  725. if (has_aperf) {
  726. if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
  727. return -3;
  728. if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
  729. return -4;
  730. }
  731. if (do_smi) {
  732. if (get_msr(cpu, MSR_SMI_COUNT, &msr))
  733. return -5;
  734. t->smi_count = msr & 0xFFFFFFFF;
  735. }
  736. if (extra_delta_offset32) {
  737. if (get_msr(cpu, extra_delta_offset32, &msr))
  738. return -5;
  739. t->extra_delta32 = msr & 0xFFFFFFFF;
  740. }
  741. if (extra_delta_offset64)
  742. if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
  743. return -5;
  744. if (extra_msr_offset32) {
  745. if (get_msr(cpu, extra_msr_offset32, &msr))
  746. return -5;
  747. t->extra_msr32 = msr & 0xFFFFFFFF;
  748. }
  749. if (extra_msr_offset64)
  750. if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
  751. return -5;
  752. /* collect core counters only for 1st thread in core */
  753. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  754. return 0;
  755. if (do_nhm_cstates) {
  756. if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
  757. return -6;
  758. if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
  759. return -7;
  760. }
  761. if (do_snb_cstates)
  762. if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
  763. return -8;
  764. if (do_dts) {
  765. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  766. return -9;
  767. c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  768. }
  769. /* collect package counters only for 1st core in package */
  770. if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  771. return 0;
  772. if (do_nhm_cstates) {
  773. if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
  774. return -9;
  775. if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
  776. return -10;
  777. }
  778. if (do_snb_cstates) {
  779. if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
  780. return -11;
  781. if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
  782. return -12;
  783. }
  784. if (do_c8_c9_c10) {
  785. if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
  786. return -13;
  787. if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
  788. return -13;
  789. if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
  790. return -13;
  791. }
  792. if (do_rapl & RAPL_PKG) {
  793. if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
  794. return -13;
  795. p->energy_pkg = msr & 0xFFFFFFFF;
  796. }
  797. if (do_rapl & RAPL_CORES) {
  798. if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
  799. return -14;
  800. p->energy_cores = msr & 0xFFFFFFFF;
  801. }
  802. if (do_rapl & RAPL_DRAM) {
  803. if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
  804. return -15;
  805. p->energy_dram = msr & 0xFFFFFFFF;
  806. }
  807. if (do_rapl & RAPL_GFX) {
  808. if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
  809. return -16;
  810. p->energy_gfx = msr & 0xFFFFFFFF;
  811. }
  812. if (do_rapl & RAPL_PKG_PERF_STATUS) {
  813. if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
  814. return -16;
  815. p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
  816. }
  817. if (do_rapl & RAPL_DRAM_PERF_STATUS) {
  818. if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
  819. return -16;
  820. p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
  821. }
  822. if (do_ptm) {
  823. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  824. return -17;
  825. p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
  826. }
  827. return 0;
  828. }
  829. void print_verbose_header(void)
  830. {
  831. unsigned long long msr;
  832. unsigned int ratio;
  833. if (!do_nehalem_platform_info)
  834. return;
  835. get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
  836. fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
  837. ratio = (msr >> 40) & 0xFF;
  838. fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
  839. ratio, bclk, ratio * bclk);
  840. ratio = (msr >> 8) & 0xFF;
  841. fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
  842. ratio, bclk, ratio * bclk);
  843. get_msr(0, MSR_IA32_POWER_CTL, &msr);
  844. fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n",
  845. msr, msr & 0x2 ? "EN" : "DIS");
  846. if (!do_ivt_turbo_ratio_limit)
  847. goto print_nhm_turbo_ratio_limits;
  848. get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
  849. fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
  850. ratio = (msr >> 56) & 0xFF;
  851. if (ratio)
  852. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
  853. ratio, bclk, ratio * bclk);
  854. ratio = (msr >> 48) & 0xFF;
  855. if (ratio)
  856. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
  857. ratio, bclk, ratio * bclk);
  858. ratio = (msr >> 40) & 0xFF;
  859. if (ratio)
  860. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
  861. ratio, bclk, ratio * bclk);
  862. ratio = (msr >> 32) & 0xFF;
  863. if (ratio)
  864. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
  865. ratio, bclk, ratio * bclk);
  866. ratio = (msr >> 24) & 0xFF;
  867. if (ratio)
  868. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
  869. ratio, bclk, ratio * bclk);
  870. ratio = (msr >> 16) & 0xFF;
  871. if (ratio)
  872. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
  873. ratio, bclk, ratio * bclk);
  874. ratio = (msr >> 8) & 0xFF;
  875. if (ratio)
  876. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
  877. ratio, bclk, ratio * bclk);
  878. ratio = (msr >> 0) & 0xFF;
  879. if (ratio)
  880. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
  881. ratio, bclk, ratio * bclk);
  882. print_nhm_turbo_ratio_limits:
  883. get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
  884. #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
  885. #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
  886. fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
  887. fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
  888. (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
  889. (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
  890. (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
  891. (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
  892. (msr & (1 << 15)) ? "" : "UN",
  893. (unsigned int)msr & 7);
  894. switch(msr & 0x7) {
  895. case 0:
  896. fprintf(stderr, "pc0");
  897. break;
  898. case 1:
  899. fprintf(stderr, do_snb_cstates ? "pc2" : "pc0");
  900. break;
  901. case 2:
  902. fprintf(stderr, do_snb_cstates ? "pc6-noret" : "pc3");
  903. break;
  904. case 3:
  905. fprintf(stderr, "pc6");
  906. break;
  907. case 4:
  908. fprintf(stderr, "pc7");
  909. break;
  910. case 5:
  911. fprintf(stderr, do_snb_cstates ? "pc7s" : "invalid");
  912. break;
  913. case 7:
  914. fprintf(stderr, "unlimited");
  915. break;
  916. default:
  917. fprintf(stderr, "invalid");
  918. }
  919. fprintf(stderr, ")\n");
  920. if (!do_nehalem_turbo_ratio_limit)
  921. return;
  922. get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
  923. fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
  924. ratio = (msr >> 56) & 0xFF;
  925. if (ratio)
  926. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
  927. ratio, bclk, ratio * bclk);
  928. ratio = (msr >> 48) & 0xFF;
  929. if (ratio)
  930. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
  931. ratio, bclk, ratio * bclk);
  932. ratio = (msr >> 40) & 0xFF;
  933. if (ratio)
  934. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
  935. ratio, bclk, ratio * bclk);
  936. ratio = (msr >> 32) & 0xFF;
  937. if (ratio)
  938. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
  939. ratio, bclk, ratio * bclk);
  940. ratio = (msr >> 24) & 0xFF;
  941. if (ratio)
  942. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
  943. ratio, bclk, ratio * bclk);
  944. ratio = (msr >> 16) & 0xFF;
  945. if (ratio)
  946. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
  947. ratio, bclk, ratio * bclk);
  948. ratio = (msr >> 8) & 0xFF;
  949. if (ratio)
  950. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
  951. ratio, bclk, ratio * bclk);
  952. ratio = (msr >> 0) & 0xFF;
  953. if (ratio)
  954. fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
  955. ratio, bclk, ratio * bclk);
  956. }
  957. void free_all_buffers(void)
  958. {
  959. CPU_FREE(cpu_present_set);
  960. cpu_present_set = NULL;
  961. cpu_present_set = 0;
  962. CPU_FREE(cpu_affinity_set);
  963. cpu_affinity_set = NULL;
  964. cpu_affinity_setsize = 0;
  965. free(thread_even);
  966. free(core_even);
  967. free(package_even);
  968. thread_even = NULL;
  969. core_even = NULL;
  970. package_even = NULL;
  971. free(thread_odd);
  972. free(core_odd);
  973. free(package_odd);
  974. thread_odd = NULL;
  975. core_odd = NULL;
  976. package_odd = NULL;
  977. free(output_buffer);
  978. output_buffer = NULL;
  979. outp = NULL;
  980. }
  981. /*
  982. * cpu_is_first_sibling_in_core(cpu)
  983. * return 1 if given CPU is 1st HT sibling in the core
  984. */
  985. int cpu_is_first_sibling_in_core(int cpu)
  986. {
  987. char path[64];
  988. FILE *filep;
  989. int first_cpu;
  990. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  991. filep = fopen(path, "r");
  992. if (filep == NULL) {
  993. perror(path);
  994. exit(1);
  995. }
  996. fscanf(filep, "%d", &first_cpu);
  997. fclose(filep);
  998. return (cpu == first_cpu);
  999. }
  1000. /*
  1001. * cpu_is_first_core_in_package(cpu)
  1002. * return 1 if given CPU is 1st core in package
  1003. */
  1004. int cpu_is_first_core_in_package(int cpu)
  1005. {
  1006. char path[64];
  1007. FILE *filep;
  1008. int first_cpu;
  1009. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
  1010. filep = fopen(path, "r");
  1011. if (filep == NULL) {
  1012. perror(path);
  1013. exit(1);
  1014. }
  1015. fscanf(filep, "%d", &first_cpu);
  1016. fclose(filep);
  1017. return (cpu == first_cpu);
  1018. }
  1019. int get_physical_package_id(int cpu)
  1020. {
  1021. char path[80];
  1022. FILE *filep;
  1023. int pkg;
  1024. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  1025. filep = fopen(path, "r");
  1026. if (filep == NULL) {
  1027. perror(path);
  1028. exit(1);
  1029. }
  1030. fscanf(filep, "%d", &pkg);
  1031. fclose(filep);
  1032. return pkg;
  1033. }
  1034. int get_core_id(int cpu)
  1035. {
  1036. char path[80];
  1037. FILE *filep;
  1038. int core;
  1039. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
  1040. filep = fopen(path, "r");
  1041. if (filep == NULL) {
  1042. perror(path);
  1043. exit(1);
  1044. }
  1045. fscanf(filep, "%d", &core);
  1046. fclose(filep);
  1047. return core;
  1048. }
  1049. int get_num_ht_siblings(int cpu)
  1050. {
  1051. char path[80];
  1052. FILE *filep;
  1053. int sib1, sib2;
  1054. int matches;
  1055. char character;
  1056. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  1057. filep = fopen(path, "r");
  1058. if (filep == NULL) {
  1059. perror(path);
  1060. exit(1);
  1061. }
  1062. /*
  1063. * file format:
  1064. * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
  1065. * otherwinse 1 sibling (self).
  1066. */
  1067. matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
  1068. fclose(filep);
  1069. if (matches == 3)
  1070. return 2;
  1071. else
  1072. return 1;
  1073. }
  1074. /*
  1075. * run func(thread, core, package) in topology order
  1076. * skip non-present cpus
  1077. */
  1078. int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
  1079. struct pkg_data *, struct thread_data *, struct core_data *,
  1080. struct pkg_data *), struct thread_data *thread_base,
  1081. struct core_data *core_base, struct pkg_data *pkg_base,
  1082. struct thread_data *thread_base2, struct core_data *core_base2,
  1083. struct pkg_data *pkg_base2)
  1084. {
  1085. int retval, pkg_no, core_no, thread_no;
  1086. for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
  1087. for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
  1088. for (thread_no = 0; thread_no <
  1089. topo.num_threads_per_core; ++thread_no) {
  1090. struct thread_data *t, *t2;
  1091. struct core_data *c, *c2;
  1092. struct pkg_data *p, *p2;
  1093. t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
  1094. if (cpu_is_not_present(t->cpu_id))
  1095. continue;
  1096. t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
  1097. c = GET_CORE(core_base, core_no, pkg_no);
  1098. c2 = GET_CORE(core_base2, core_no, pkg_no);
  1099. p = GET_PKG(pkg_base, pkg_no);
  1100. p2 = GET_PKG(pkg_base2, pkg_no);
  1101. retval = func(t, c, p, t2, c2, p2);
  1102. if (retval)
  1103. return retval;
  1104. }
  1105. }
  1106. }
  1107. return 0;
  1108. }
  1109. /*
  1110. * run func(cpu) on every cpu in /proc/stat
  1111. * return max_cpu number
  1112. */
  1113. int for_all_proc_cpus(int (func)(int))
  1114. {
  1115. FILE *fp;
  1116. int cpu_num;
  1117. int retval;
  1118. fp = fopen(proc_stat, "r");
  1119. if (fp == NULL) {
  1120. perror(proc_stat);
  1121. exit(1);
  1122. }
  1123. retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  1124. if (retval != 0) {
  1125. perror("/proc/stat format");
  1126. exit(1);
  1127. }
  1128. while (1) {
  1129. retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
  1130. if (retval != 1)
  1131. break;
  1132. retval = func(cpu_num);
  1133. if (retval) {
  1134. fclose(fp);
  1135. return(retval);
  1136. }
  1137. }
  1138. fclose(fp);
  1139. return 0;
  1140. }
  1141. void re_initialize(void)
  1142. {
  1143. free_all_buffers();
  1144. setup_all_buffers();
  1145. printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
  1146. }
  1147. /*
  1148. * count_cpus()
  1149. * remember the last one seen, it will be the max
  1150. */
  1151. int count_cpus(int cpu)
  1152. {
  1153. if (topo.max_cpu_num < cpu)
  1154. topo.max_cpu_num = cpu;
  1155. topo.num_cpus += 1;
  1156. return 0;
  1157. }
  1158. int mark_cpu_present(int cpu)
  1159. {
  1160. CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
  1161. return 0;
  1162. }
  1163. void turbostat_loop()
  1164. {
  1165. int retval;
  1166. int restarted = 0;
  1167. restart:
  1168. restarted++;
  1169. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  1170. if (retval < -1) {
  1171. exit(retval);
  1172. } else if (retval == -1) {
  1173. if (restarted > 1) {
  1174. exit(retval);
  1175. }
  1176. re_initialize();
  1177. goto restart;
  1178. }
  1179. restarted = 0;
  1180. gettimeofday(&tv_even, (struct timezone *)NULL);
  1181. while (1) {
  1182. if (for_all_proc_cpus(cpu_is_not_present)) {
  1183. re_initialize();
  1184. goto restart;
  1185. }
  1186. sleep(interval_sec);
  1187. retval = for_all_cpus(get_counters, ODD_COUNTERS);
  1188. if (retval < -1) {
  1189. exit(retval);
  1190. } else if (retval == -1) {
  1191. re_initialize();
  1192. goto restart;
  1193. }
  1194. gettimeofday(&tv_odd, (struct timezone *)NULL);
  1195. timersub(&tv_odd, &tv_even, &tv_delta);
  1196. for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
  1197. compute_average(EVEN_COUNTERS);
  1198. format_all_counters(EVEN_COUNTERS);
  1199. flush_stdout();
  1200. sleep(interval_sec);
  1201. retval = for_all_cpus(get_counters, EVEN_COUNTERS);
  1202. if (retval < -1) {
  1203. exit(retval);
  1204. } else if (retval == -1) {
  1205. re_initialize();
  1206. goto restart;
  1207. }
  1208. gettimeofday(&tv_even, (struct timezone *)NULL);
  1209. timersub(&tv_even, &tv_odd, &tv_delta);
  1210. for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
  1211. compute_average(ODD_COUNTERS);
  1212. format_all_counters(ODD_COUNTERS);
  1213. flush_stdout();
  1214. }
  1215. }
  1216. void check_dev_msr()
  1217. {
  1218. struct stat sb;
  1219. if (stat("/dev/cpu/0/msr", &sb)) {
  1220. fprintf(stderr, "no /dev/cpu/0/msr\n");
  1221. fprintf(stderr, "Try \"# modprobe msr\"\n");
  1222. exit(-5);
  1223. }
  1224. }
  1225. void check_super_user()
  1226. {
  1227. if (getuid() != 0) {
  1228. fprintf(stderr, "must be root\n");
  1229. exit(-6);
  1230. }
  1231. }
  1232. int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
  1233. {
  1234. if (!genuine_intel)
  1235. return 0;
  1236. if (family != 6)
  1237. return 0;
  1238. switch (model) {
  1239. case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
  1240. case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
  1241. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  1242. case 0x25: /* Westmere Client - Clarkdale, Arrandale */
  1243. case 0x2C: /* Westmere EP - Gulftown */
  1244. case 0x2A: /* SNB */
  1245. case 0x2D: /* SNB Xeon */
  1246. case 0x3A: /* IVB */
  1247. case 0x3E: /* IVB Xeon */
  1248. case 0x3C: /* HSW */
  1249. case 0x3F: /* HSW */
  1250. case 0x45: /* HSW */
  1251. case 0x46: /* HSW */
  1252. return 1;
  1253. case 0x2E: /* Nehalem-EX Xeon - Beckton */
  1254. case 0x2F: /* Westmere-EX Xeon - Eagleton */
  1255. default:
  1256. return 0;
  1257. }
  1258. }
  1259. int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
  1260. {
  1261. if (!genuine_intel)
  1262. return 0;
  1263. if (family != 6)
  1264. return 0;
  1265. switch (model) {
  1266. case 0x3E: /* IVB Xeon */
  1267. return 1;
  1268. default:
  1269. return 0;
  1270. }
  1271. }
  1272. /*
  1273. * print_epb()
  1274. * Decode the ENERGY_PERF_BIAS MSR
  1275. */
  1276. int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1277. {
  1278. unsigned long long msr;
  1279. char *epb_string;
  1280. int cpu;
  1281. if (!has_epb)
  1282. return 0;
  1283. cpu = t->cpu_id;
  1284. /* EPB is per-package */
  1285. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1286. return 0;
  1287. if (cpu_migrate(cpu)) {
  1288. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1289. return -1;
  1290. }
  1291. if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
  1292. return 0;
  1293. switch (msr & 0x7) {
  1294. case ENERGY_PERF_BIAS_PERFORMANCE:
  1295. epb_string = "performance";
  1296. break;
  1297. case ENERGY_PERF_BIAS_NORMAL:
  1298. epb_string = "balanced";
  1299. break;
  1300. case ENERGY_PERF_BIAS_POWERSAVE:
  1301. epb_string = "powersave";
  1302. break;
  1303. default:
  1304. epb_string = "custom";
  1305. break;
  1306. }
  1307. fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
  1308. return 0;
  1309. }
  1310. #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
  1311. #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
  1312. /*
  1313. * rapl_probe()
  1314. *
  1315. * sets do_rapl
  1316. */
  1317. void rapl_probe(unsigned int family, unsigned int model)
  1318. {
  1319. unsigned long long msr;
  1320. double tdp;
  1321. if (!genuine_intel)
  1322. return;
  1323. if (family != 6)
  1324. return;
  1325. switch (model) {
  1326. case 0x2A:
  1327. case 0x3A:
  1328. case 0x3C: /* HSW */
  1329. case 0x3F: /* HSW */
  1330. case 0x45: /* HSW */
  1331. case 0x46: /* HSW */
  1332. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
  1333. break;
  1334. case 0x2D:
  1335. case 0x3E:
  1336. do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS;
  1337. break;
  1338. default:
  1339. return;
  1340. }
  1341. /* units on package 0, verify later other packages match */
  1342. if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
  1343. return;
  1344. rapl_power_units = 1.0 / (1 << (msr & 0xF));
  1345. rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
  1346. rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
  1347. /* get TDP to determine energy counter range */
  1348. if (get_msr(0, MSR_PKG_POWER_INFO, &msr))
  1349. return;
  1350. tdp = ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
  1351. rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
  1352. if (verbose)
  1353. fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range);
  1354. return;
  1355. }
  1356. int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1357. {
  1358. unsigned long long msr;
  1359. unsigned int dts;
  1360. int cpu;
  1361. if (!(do_dts || do_ptm))
  1362. return 0;
  1363. cpu = t->cpu_id;
  1364. /* DTS is per-core, no need to print for each thread */
  1365. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
  1366. return 0;
  1367. if (cpu_migrate(cpu)) {
  1368. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1369. return -1;
  1370. }
  1371. if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
  1372. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
  1373. return 0;
  1374. dts = (msr >> 16) & 0x7F;
  1375. fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
  1376. cpu, msr, tcc_activation_temp - dts);
  1377. #ifdef THERM_DEBUG
  1378. if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
  1379. return 0;
  1380. dts = (msr >> 16) & 0x7F;
  1381. dts2 = (msr >> 8) & 0x7F;
  1382. fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  1383. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  1384. #endif
  1385. }
  1386. if (do_dts) {
  1387. unsigned int resolution;
  1388. if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
  1389. return 0;
  1390. dts = (msr >> 16) & 0x7F;
  1391. resolution = (msr >> 27) & 0xF;
  1392. fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
  1393. cpu, msr, tcc_activation_temp - dts, resolution);
  1394. #ifdef THERM_DEBUG
  1395. if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
  1396. return 0;
  1397. dts = (msr >> 16) & 0x7F;
  1398. dts2 = (msr >> 8) & 0x7F;
  1399. fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
  1400. cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
  1401. #endif
  1402. }
  1403. return 0;
  1404. }
  1405. void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
  1406. {
  1407. fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
  1408. cpu, label,
  1409. ((msr >> 15) & 1) ? "EN" : "DIS",
  1410. ((msr >> 0) & 0x7FFF) * rapl_power_units,
  1411. (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
  1412. (((msr >> 16) & 1) ? "EN" : "DIS"));
  1413. return;
  1414. }
  1415. int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1416. {
  1417. unsigned long long msr;
  1418. int cpu;
  1419. double local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units;
  1420. if (!do_rapl)
  1421. return 0;
  1422. /* RAPL counters are per package, so print only for 1st thread/package */
  1423. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1424. return 0;
  1425. cpu = t->cpu_id;
  1426. if (cpu_migrate(cpu)) {
  1427. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1428. return -1;
  1429. }
  1430. if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
  1431. return -1;
  1432. local_rapl_power_units = 1.0 / (1 << (msr & 0xF));
  1433. local_rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
  1434. local_rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
  1435. if (local_rapl_power_units != rapl_power_units)
  1436. fprintf(stderr, "cpu%d, ERROR: Power units mis-match\n", cpu);
  1437. if (local_rapl_energy_units != rapl_energy_units)
  1438. fprintf(stderr, "cpu%d, ERROR: Energy units mis-match\n", cpu);
  1439. if (local_rapl_time_units != rapl_time_units)
  1440. fprintf(stderr, "cpu%d, ERROR: Time units mis-match\n", cpu);
  1441. if (verbose) {
  1442. fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
  1443. "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
  1444. local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units);
  1445. }
  1446. if (do_rapl & RAPL_PKG) {
  1447. if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
  1448. return -5;
  1449. fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  1450. cpu, msr,
  1451. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1452. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1453. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1454. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  1455. if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
  1456. return -9;
  1457. fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1458. cpu, msr, (msr >> 63) & 1 ? "": "UN");
  1459. print_power_limit_msr(cpu, msr, "PKG Limit #1");
  1460. fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
  1461. cpu,
  1462. ((msr >> 47) & 1) ? "EN" : "DIS",
  1463. ((msr >> 32) & 0x7FFF) * rapl_power_units,
  1464. (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
  1465. ((msr >> 48) & 1) ? "EN" : "DIS");
  1466. }
  1467. if (do_rapl & RAPL_DRAM) {
  1468. if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
  1469. return -6;
  1470. fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
  1471. cpu, msr,
  1472. ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1473. ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1474. ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
  1475. ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
  1476. if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
  1477. return -9;
  1478. fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1479. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  1480. print_power_limit_msr(cpu, msr, "DRAM Limit");
  1481. }
  1482. if (do_rapl & RAPL_CORES) {
  1483. if (verbose) {
  1484. if (get_msr(cpu, MSR_PP0_POLICY, &msr))
  1485. return -7;
  1486. fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
  1487. if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
  1488. return -9;
  1489. fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1490. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  1491. print_power_limit_msr(cpu, msr, "Cores Limit");
  1492. }
  1493. }
  1494. if (do_rapl & RAPL_GFX) {
  1495. if (verbose) {
  1496. if (get_msr(cpu, MSR_PP1_POLICY, &msr))
  1497. return -8;
  1498. fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
  1499. if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
  1500. return -9;
  1501. fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
  1502. cpu, msr, (msr >> 31) & 1 ? "": "UN");
  1503. print_power_limit_msr(cpu, msr, "GFX Limit");
  1504. }
  1505. }
  1506. return 0;
  1507. }
  1508. int is_snb(unsigned int family, unsigned int model)
  1509. {
  1510. if (!genuine_intel)
  1511. return 0;
  1512. switch (model) {
  1513. case 0x2A:
  1514. case 0x2D:
  1515. case 0x3A: /* IVB */
  1516. case 0x3E: /* IVB Xeon */
  1517. case 0x3C: /* HSW */
  1518. case 0x3F: /* HSW */
  1519. case 0x45: /* HSW */
  1520. case 0x46: /* HSW */
  1521. return 1;
  1522. }
  1523. return 0;
  1524. }
  1525. int has_c8_c9_c10(unsigned int family, unsigned int model)
  1526. {
  1527. if (!genuine_intel)
  1528. return 0;
  1529. switch (model) {
  1530. case 0x45:
  1531. return 1;
  1532. }
  1533. return 0;
  1534. }
  1535. double discover_bclk(unsigned int family, unsigned int model)
  1536. {
  1537. if (is_snb(family, model))
  1538. return 100.00;
  1539. else
  1540. return 133.33;
  1541. }
  1542. /*
  1543. * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
  1544. * the Thermal Control Circuit (TCC) activates.
  1545. * This is usually equal to tjMax.
  1546. *
  1547. * Older processors do not have this MSR, so there we guess,
  1548. * but also allow cmdline over-ride with -T.
  1549. *
  1550. * Several MSR temperature values are in units of degrees-C
  1551. * below this value, including the Digital Thermal Sensor (DTS),
  1552. * Package Thermal Management Sensor (PTM), and thermal event thresholds.
  1553. */
  1554. int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
  1555. {
  1556. unsigned long long msr;
  1557. unsigned int target_c_local;
  1558. int cpu;
  1559. /* tcc_activation_temp is used only for dts or ptm */
  1560. if (!(do_dts || do_ptm))
  1561. return 0;
  1562. /* this is a per-package concept */
  1563. if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
  1564. return 0;
  1565. cpu = t->cpu_id;
  1566. if (cpu_migrate(cpu)) {
  1567. fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
  1568. return -1;
  1569. }
  1570. if (tcc_activation_temp_override != 0) {
  1571. tcc_activation_temp = tcc_activation_temp_override;
  1572. fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
  1573. cpu, tcc_activation_temp);
  1574. return 0;
  1575. }
  1576. /* Temperature Target MSR is Nehalem and newer only */
  1577. if (!do_nehalem_platform_info)
  1578. goto guess;
  1579. if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
  1580. goto guess;
  1581. target_c_local = (msr >> 16) & 0x7F;
  1582. if (verbose)
  1583. fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
  1584. cpu, msr, target_c_local);
  1585. if (target_c_local < 85 || target_c_local > 120)
  1586. goto guess;
  1587. tcc_activation_temp = target_c_local;
  1588. return 0;
  1589. guess:
  1590. tcc_activation_temp = TJMAX_DEFAULT;
  1591. fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
  1592. cpu, tcc_activation_temp);
  1593. return 0;
  1594. }
  1595. void check_cpuid()
  1596. {
  1597. unsigned int eax, ebx, ecx, edx, max_level;
  1598. unsigned int fms, family, model, stepping;
  1599. eax = ebx = ecx = edx = 0;
  1600. asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
  1601. if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
  1602. genuine_intel = 1;
  1603. if (verbose)
  1604. fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
  1605. (char *)&ebx, (char *)&edx, (char *)&ecx);
  1606. asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
  1607. family = (fms >> 8) & 0xf;
  1608. model = (fms >> 4) & 0xf;
  1609. stepping = fms & 0xf;
  1610. if (family == 6 || family == 0xf)
  1611. model += ((fms >> 16) & 0xf) << 4;
  1612. if (verbose)
  1613. fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
  1614. max_level, family, model, stepping, family, model, stepping);
  1615. if (!(edx & (1 << 5))) {
  1616. fprintf(stderr, "CPUID: no MSR\n");
  1617. exit(1);
  1618. }
  1619. /*
  1620. * check max extended function levels of CPUID.
  1621. * This is needed to check for invariant TSC.
  1622. * This check is valid for both Intel and AMD.
  1623. */
  1624. ebx = ecx = edx = 0;
  1625. asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
  1626. if (max_level < 0x80000007) {
  1627. fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
  1628. exit(1);
  1629. }
  1630. /*
  1631. * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
  1632. * this check is valid for both Intel and AMD
  1633. */
  1634. asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
  1635. has_invariant_tsc = edx & (1 << 8);
  1636. if (!has_invariant_tsc) {
  1637. fprintf(stderr, "No invariant TSC\n");
  1638. exit(1);
  1639. }
  1640. /*
  1641. * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
  1642. * this check is valid for both Intel and AMD
  1643. */
  1644. asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
  1645. has_aperf = ecx & (1 << 0);
  1646. do_dts = eax & (1 << 0);
  1647. do_ptm = eax & (1 << 6);
  1648. has_epb = ecx & (1 << 3);
  1649. if (verbose)
  1650. fprintf(stderr, "CPUID(6): %s%s%s%s\n",
  1651. has_aperf ? "APERF" : "No APERF!",
  1652. do_dts ? ", DTS" : "",
  1653. do_ptm ? ", PTM": "",
  1654. has_epb ? ", EPB": "");
  1655. if (!has_aperf)
  1656. exit(-1);
  1657. do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
  1658. do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
  1659. do_smi = do_nhm_cstates;
  1660. do_snb_cstates = is_snb(family, model);
  1661. do_c8_c9_c10 = has_c8_c9_c10(family, model);
  1662. bclk = discover_bclk(family, model);
  1663. do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
  1664. do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
  1665. rapl_probe(family, model);
  1666. return;
  1667. }
  1668. void usage()
  1669. {
  1670. fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
  1671. progname);
  1672. exit(1);
  1673. }
  1674. /*
  1675. * in /dev/cpu/ return success for names that are numbers
  1676. * ie. filter out ".", "..", "microcode".
  1677. */
  1678. int dir_filter(const struct dirent *dirp)
  1679. {
  1680. if (isdigit(dirp->d_name[0]))
  1681. return 1;
  1682. else
  1683. return 0;
  1684. }
  1685. int open_dev_cpu_msr(int dummy1)
  1686. {
  1687. return 0;
  1688. }
  1689. void topology_probe()
  1690. {
  1691. int i;
  1692. int max_core_id = 0;
  1693. int max_package_id = 0;
  1694. int max_siblings = 0;
  1695. struct cpu_topology {
  1696. int core_id;
  1697. int physical_package_id;
  1698. } *cpus;
  1699. /* Initialize num_cpus, max_cpu_num */
  1700. topo.num_cpus = 0;
  1701. topo.max_cpu_num = 0;
  1702. for_all_proc_cpus(count_cpus);
  1703. if (!summary_only && topo.num_cpus > 1)
  1704. show_cpu = 1;
  1705. if (verbose > 1)
  1706. fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
  1707. cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
  1708. if (cpus == NULL) {
  1709. perror("calloc cpus");
  1710. exit(1);
  1711. }
  1712. /*
  1713. * Allocate and initialize cpu_present_set
  1714. */
  1715. cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
  1716. if (cpu_present_set == NULL) {
  1717. perror("CPU_ALLOC");
  1718. exit(3);
  1719. }
  1720. cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  1721. CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
  1722. for_all_proc_cpus(mark_cpu_present);
  1723. /*
  1724. * Allocate and initialize cpu_affinity_set
  1725. */
  1726. cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
  1727. if (cpu_affinity_set == NULL) {
  1728. perror("CPU_ALLOC");
  1729. exit(3);
  1730. }
  1731. cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
  1732. CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
  1733. /*
  1734. * For online cpus
  1735. * find max_core_id, max_package_id
  1736. */
  1737. for (i = 0; i <= topo.max_cpu_num; ++i) {
  1738. int siblings;
  1739. if (cpu_is_not_present(i)) {
  1740. if (verbose > 1)
  1741. fprintf(stderr, "cpu%d NOT PRESENT\n", i);
  1742. continue;
  1743. }
  1744. cpus[i].core_id = get_core_id(i);
  1745. if (cpus[i].core_id > max_core_id)
  1746. max_core_id = cpus[i].core_id;
  1747. cpus[i].physical_package_id = get_physical_package_id(i);
  1748. if (cpus[i].physical_package_id > max_package_id)
  1749. max_package_id = cpus[i].physical_package_id;
  1750. siblings = get_num_ht_siblings(i);
  1751. if (siblings > max_siblings)
  1752. max_siblings = siblings;
  1753. if (verbose > 1)
  1754. fprintf(stderr, "cpu %d pkg %d core %d\n",
  1755. i, cpus[i].physical_package_id, cpus[i].core_id);
  1756. }
  1757. topo.num_cores_per_pkg = max_core_id + 1;
  1758. if (verbose > 1)
  1759. fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
  1760. max_core_id, topo.num_cores_per_pkg);
  1761. if (!summary_only && topo.num_cores_per_pkg > 1)
  1762. show_core = 1;
  1763. topo.num_packages = max_package_id + 1;
  1764. if (verbose > 1)
  1765. fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
  1766. max_package_id, topo.num_packages);
  1767. if (!summary_only && topo.num_packages > 1)
  1768. show_pkg = 1;
  1769. topo.num_threads_per_core = max_siblings;
  1770. if (verbose > 1)
  1771. fprintf(stderr, "max_siblings %d\n", max_siblings);
  1772. free(cpus);
  1773. }
  1774. void
  1775. allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
  1776. {
  1777. int i;
  1778. *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
  1779. topo.num_packages, sizeof(struct thread_data));
  1780. if (*t == NULL)
  1781. goto error;
  1782. for (i = 0; i < topo.num_threads_per_core *
  1783. topo.num_cores_per_pkg * topo.num_packages; i++)
  1784. (*t)[i].cpu_id = -1;
  1785. *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
  1786. sizeof(struct core_data));
  1787. if (*c == NULL)
  1788. goto error;
  1789. for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
  1790. (*c)[i].core_id = -1;
  1791. *p = calloc(topo.num_packages, sizeof(struct pkg_data));
  1792. if (*p == NULL)
  1793. goto error;
  1794. for (i = 0; i < topo.num_packages; i++)
  1795. (*p)[i].package_id = i;
  1796. return;
  1797. error:
  1798. perror("calloc counters");
  1799. exit(1);
  1800. }
  1801. /*
  1802. * init_counter()
  1803. *
  1804. * set cpu_id, core_num, pkg_num
  1805. * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
  1806. *
  1807. * increment topo.num_cores when 1st core in pkg seen
  1808. */
  1809. void init_counter(struct thread_data *thread_base, struct core_data *core_base,
  1810. struct pkg_data *pkg_base, int thread_num, int core_num,
  1811. int pkg_num, int cpu_id)
  1812. {
  1813. struct thread_data *t;
  1814. struct core_data *c;
  1815. struct pkg_data *p;
  1816. t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
  1817. c = GET_CORE(core_base, core_num, pkg_num);
  1818. p = GET_PKG(pkg_base, pkg_num);
  1819. t->cpu_id = cpu_id;
  1820. if (thread_num == 0) {
  1821. t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
  1822. if (cpu_is_first_core_in_package(cpu_id))
  1823. t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
  1824. }
  1825. c->core_id = core_num;
  1826. p->package_id = pkg_num;
  1827. }
  1828. int initialize_counters(int cpu_id)
  1829. {
  1830. int my_thread_id, my_core_id, my_package_id;
  1831. my_package_id = get_physical_package_id(cpu_id);
  1832. my_core_id = get_core_id(cpu_id);
  1833. if (cpu_is_first_sibling_in_core(cpu_id)) {
  1834. my_thread_id = 0;
  1835. topo.num_cores++;
  1836. } else {
  1837. my_thread_id = 1;
  1838. }
  1839. init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  1840. init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
  1841. return 0;
  1842. }
  1843. void allocate_output_buffer()
  1844. {
  1845. output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
  1846. outp = output_buffer;
  1847. if (outp == NULL) {
  1848. perror("calloc");
  1849. exit(-1);
  1850. }
  1851. }
  1852. void setup_all_buffers(void)
  1853. {
  1854. topology_probe();
  1855. allocate_counters(&thread_even, &core_even, &package_even);
  1856. allocate_counters(&thread_odd, &core_odd, &package_odd);
  1857. allocate_output_buffer();
  1858. for_all_proc_cpus(initialize_counters);
  1859. }
  1860. void turbostat_init()
  1861. {
  1862. check_cpuid();
  1863. check_dev_msr();
  1864. check_super_user();
  1865. setup_all_buffers();
  1866. if (verbose)
  1867. print_verbose_header();
  1868. if (verbose)
  1869. for_all_cpus(print_epb, ODD_COUNTERS);
  1870. if (verbose)
  1871. for_all_cpus(print_rapl, ODD_COUNTERS);
  1872. for_all_cpus(set_temperature_target, ODD_COUNTERS);
  1873. if (verbose)
  1874. for_all_cpus(print_thermal, ODD_COUNTERS);
  1875. }
  1876. int fork_it(char **argv)
  1877. {
  1878. pid_t child_pid;
  1879. int status;
  1880. status = for_all_cpus(get_counters, EVEN_COUNTERS);
  1881. if (status)
  1882. exit(status);
  1883. /* clear affinity side-effect of get_counters() */
  1884. sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
  1885. gettimeofday(&tv_even, (struct timezone *)NULL);
  1886. child_pid = fork();
  1887. if (!child_pid) {
  1888. /* child */
  1889. execvp(argv[0], argv);
  1890. } else {
  1891. /* parent */
  1892. if (child_pid == -1) {
  1893. perror("fork");
  1894. exit(1);
  1895. }
  1896. signal(SIGINT, SIG_IGN);
  1897. signal(SIGQUIT, SIG_IGN);
  1898. if (waitpid(child_pid, &status, 0) == -1) {
  1899. perror("wait");
  1900. exit(status);
  1901. }
  1902. }
  1903. /*
  1904. * n.b. fork_it() does not check for errors from for_all_cpus()
  1905. * because re-starting is problematic when forking
  1906. */
  1907. for_all_cpus(get_counters, ODD_COUNTERS);
  1908. gettimeofday(&tv_odd, (struct timezone *)NULL);
  1909. timersub(&tv_odd, &tv_even, &tv_delta);
  1910. for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
  1911. compute_average(EVEN_COUNTERS);
  1912. format_all_counters(EVEN_COUNTERS);
  1913. flush_stderr();
  1914. fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
  1915. return status;
  1916. }
  1917. void cmdline(int argc, char **argv)
  1918. {
  1919. int opt;
  1920. progname = argv[0];
  1921. while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
  1922. switch (opt) {
  1923. case 'p':
  1924. show_core_only++;
  1925. break;
  1926. case 'P':
  1927. show_pkg_only++;
  1928. break;
  1929. case 'S':
  1930. summary_only++;
  1931. break;
  1932. case 'v':
  1933. verbose++;
  1934. break;
  1935. case 'i':
  1936. interval_sec = atoi(optarg);
  1937. break;
  1938. case 'c':
  1939. sscanf(optarg, "%x", &extra_delta_offset32);
  1940. break;
  1941. case 'C':
  1942. sscanf(optarg, "%x", &extra_delta_offset64);
  1943. break;
  1944. case 'm':
  1945. sscanf(optarg, "%x", &extra_msr_offset32);
  1946. break;
  1947. case 'M':
  1948. sscanf(optarg, "%x", &extra_msr_offset64);
  1949. break;
  1950. case 'R':
  1951. rapl_verbose++;
  1952. break;
  1953. case 'T':
  1954. tcc_activation_temp_override = atoi(optarg);
  1955. break;
  1956. default:
  1957. usage();
  1958. }
  1959. }
  1960. }
  1961. int main(int argc, char **argv)
  1962. {
  1963. cmdline(argc, argv);
  1964. if (verbose)
  1965. fprintf(stderr, "turbostat v3.4 April 17, 2013"
  1966. " - Len Brown <lenb@kernel.org>\n");
  1967. turbostat_init();
  1968. /*
  1969. * if any params left, it must be a command to fork
  1970. */
  1971. if (argc - optind)
  1972. return fork_it(argv + optind);
  1973. else
  1974. turbostat_loop();
  1975. return 0;
  1976. }