turbostat.c 61 KB

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