turbostat.c 55 KB

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