cpupower-monitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * Output format inspired by Len Brown's <lenb@kernel.org> turbostat tool.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <signal.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <libgen.h>
  18. #include "idle_monitor/cpupower-monitor.h"
  19. #include "idle_monitor/idle_monitors.h"
  20. #include "helpers/helpers.h"
  21. /* Define pointers to all monitors. */
  22. #define DEF(x) & x ## _monitor ,
  23. struct cpuidle_monitor *all_monitors[] = {
  24. #include "idle_monitors.def"
  25. 0
  26. };
  27. static struct cpuidle_monitor *monitors[MONITORS_MAX];
  28. static unsigned int avail_monitors;
  29. static char *progname;
  30. enum operation_mode_e { list = 1, show, show_all };
  31. static int mode;
  32. static int interval = 1;
  33. static char *show_monitors_param;
  34. static struct cpupower_topology cpu_top;
  35. static unsigned int wake_cpus;
  36. /* ToDo: Document this in the manpage */
  37. static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
  38. static void print_wrong_arg_exit(void)
  39. {
  40. printf(_("invalid or unknown argument\n"));
  41. exit(EXIT_FAILURE);
  42. }
  43. long long timespec_diff_us(struct timespec start, struct timespec end)
  44. {
  45. struct timespec temp;
  46. if ((end.tv_nsec - start.tv_nsec) < 0) {
  47. temp.tv_sec = end.tv_sec - start.tv_sec - 1;
  48. temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
  49. } else {
  50. temp.tv_sec = end.tv_sec - start.tv_sec;
  51. temp.tv_nsec = end.tv_nsec - start.tv_nsec;
  52. }
  53. return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
  54. }
  55. void print_n_spaces(int n)
  56. {
  57. int x;
  58. for (x = 0; x < n; x++)
  59. printf(" ");
  60. }
  61. /* size of s must be at least n + 1 */
  62. int fill_string_with_spaces(char *s, int n)
  63. {
  64. int len = strlen(s);
  65. if (len > n)
  66. return -1;
  67. for (; len < n; len++)
  68. s[len] = ' ';
  69. s[len] = '\0';
  70. return 0;
  71. }
  72. void print_header(int topology_depth)
  73. {
  74. int unsigned mon;
  75. int state, need_len;
  76. cstate_t s;
  77. char buf[128] = "";
  78. int percent_width = 4;
  79. fill_string_with_spaces(buf, topology_depth * 5 - 1);
  80. printf("%s|", buf);
  81. for (mon = 0; mon < avail_monitors; mon++) {
  82. need_len = monitors[mon]->hw_states_num * (percent_width + 3)
  83. - 1;
  84. if (mon != 0) {
  85. printf("|| ");
  86. need_len--;
  87. }
  88. sprintf(buf, "%s", monitors[mon]->name);
  89. fill_string_with_spaces(buf, need_len);
  90. printf("%s", buf);
  91. }
  92. printf("\n");
  93. if (topology_depth > 2)
  94. printf("PKG |");
  95. if (topology_depth > 1)
  96. printf("CORE|");
  97. if (topology_depth > 0)
  98. printf("CPU |");
  99. for (mon = 0; mon < avail_monitors; mon++) {
  100. if (mon != 0)
  101. printf("|| ");
  102. else
  103. printf(" ");
  104. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  105. if (state != 0)
  106. printf(" | ");
  107. s = monitors[mon]->hw_states[state];
  108. sprintf(buf, "%s", s.name);
  109. fill_string_with_spaces(buf, percent_width);
  110. printf("%s", buf);
  111. }
  112. printf(" ");
  113. }
  114. printf("\n");
  115. }
  116. void print_results(int topology_depth, int cpu)
  117. {
  118. unsigned int mon;
  119. int state, ret;
  120. double percent;
  121. unsigned long long result;
  122. cstate_t s;
  123. /* Be careful CPUs may got resorted for pkg value do not just use cpu */
  124. if (!bitmask_isbitset(cpus_chosen, cpu_top.core_info[cpu].cpu))
  125. return;
  126. if (!cpu_top.core_info[cpu].is_online &&
  127. cpu_top.core_info[cpu].pkg == -1)
  128. return;
  129. if (topology_depth > 2)
  130. printf("%4d|", cpu_top.core_info[cpu].pkg);
  131. if (topology_depth > 1)
  132. printf("%4d|", cpu_top.core_info[cpu].core);
  133. if (topology_depth > 0)
  134. printf("%4d|", cpu_top.core_info[cpu].cpu);
  135. for (mon = 0; mon < avail_monitors; mon++) {
  136. if (mon != 0)
  137. printf("||");
  138. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  139. if (state != 0)
  140. printf("|");
  141. s = monitors[mon]->hw_states[state];
  142. if (s.get_count_percent) {
  143. ret = s.get_count_percent(s.id, &percent,
  144. cpu_top.core_info[cpu].cpu);
  145. if (ret)
  146. printf("******");
  147. else if (percent >= 100.0)
  148. printf("%6.1f", percent);
  149. else
  150. printf("%6.2f", percent);
  151. } else if (s.get_count) {
  152. ret = s.get_count(s.id, &result,
  153. cpu_top.core_info[cpu].cpu);
  154. if (ret)
  155. printf("******");
  156. else
  157. printf("%6llu", result);
  158. } else {
  159. printf(_("Monitor %s, Counter %s has no count "
  160. "function. Implementation error\n"),
  161. monitors[mon]->name, s.name);
  162. exit(EXIT_FAILURE);
  163. }
  164. }
  165. }
  166. /*
  167. * The monitor could still provide useful data, for example
  168. * AMD HW counters partly sit in PCI config space.
  169. * It's up to the monitor plug-in to check .is_online, this one
  170. * is just for additional info.
  171. */
  172. if (!cpu_top.core_info[cpu].is_online &&
  173. cpu_top.core_info[cpu].pkg != -1) {
  174. printf(_(" *is offline\n"));
  175. return;
  176. } else
  177. printf("\n");
  178. }
  179. /* param: string passed by -m param (The list of monitors to show)
  180. *
  181. * Monitors must have been registered already, matching monitors
  182. * are picked out and available monitors array is overridden
  183. * with matching ones
  184. *
  185. * Monitors get sorted in the same order the user passes them
  186. */
  187. static void parse_monitor_param(char *param)
  188. {
  189. unsigned int num;
  190. int mon, hits = 0;
  191. char *tmp = param, *token;
  192. struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
  193. for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
  194. token = strtok(tmp, ",");
  195. if (token == NULL)
  196. break;
  197. if (strlen(token) >= MONITOR_NAME_LEN) {
  198. printf(_("%s: max monitor name length"
  199. " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
  200. continue;
  201. }
  202. for (num = 0; num < avail_monitors; num++) {
  203. if (!strcmp(monitors[num]->name, token)) {
  204. dprint("Found requested monitor: %s\n", token);
  205. tmp_mons[hits] = monitors[num];
  206. hits++;
  207. }
  208. }
  209. }
  210. if (hits == 0) {
  211. printf(_("No matching monitor found in %s, "
  212. "try -l option\n"), param);
  213. exit(EXIT_FAILURE);
  214. }
  215. /* Override detected/registerd monitors array with requested one */
  216. memcpy(monitors, tmp_mons,
  217. sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
  218. avail_monitors = hits;
  219. }
  220. void list_monitors(void)
  221. {
  222. unsigned int mon;
  223. int state;
  224. cstate_t s;
  225. for (mon = 0; mon < avail_monitors; mon++) {
  226. printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
  227. "s\n"),
  228. monitors[mon]->name, monitors[mon]->hw_states_num,
  229. monitors[mon]->overflow_s);
  230. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  231. s = monitors[mon]->hw_states[state];
  232. /*
  233. * ToDo show more state capabilities:
  234. * percent, time (granlarity)
  235. */
  236. printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
  237. gettext(s.desc));
  238. }
  239. }
  240. }
  241. int fork_it(char **argv)
  242. {
  243. int status;
  244. unsigned int num;
  245. unsigned long long timediff;
  246. pid_t child_pid;
  247. struct timespec start, end;
  248. child_pid = fork();
  249. clock_gettime(CLOCK_REALTIME, &start);
  250. for (num = 0; num < avail_monitors; num++)
  251. monitors[num]->start();
  252. if (!child_pid) {
  253. /* child */
  254. execvp(argv[0], argv);
  255. } else {
  256. /* parent */
  257. if (child_pid == -1) {
  258. perror("fork");
  259. exit(1);
  260. }
  261. signal(SIGINT, SIG_IGN);
  262. signal(SIGQUIT, SIG_IGN);
  263. if (waitpid(child_pid, &status, 0) == -1) {
  264. perror("wait");
  265. exit(1);
  266. }
  267. }
  268. clock_gettime(CLOCK_REALTIME, &end);
  269. for (num = 0; num < avail_monitors; num++)
  270. monitors[num]->stop();
  271. timediff = timespec_diff_us(start, end);
  272. if (WIFEXITED(status))
  273. printf(_("%s took %.5f seconds and exited with status %d\n"),
  274. argv[0], timediff / (1000.0 * 1000),
  275. WEXITSTATUS(status));
  276. return 0;
  277. }
  278. int do_interval_measure(int i)
  279. {
  280. unsigned int num;
  281. int cpu;
  282. if (wake_cpus)
  283. for (cpu = 0; cpu < cpu_count; cpu++)
  284. bind_cpu(cpu);
  285. for (num = 0; num < avail_monitors; num++) {
  286. dprint("HW C-state residency monitor: %s - States: %d\n",
  287. monitors[num]->name, monitors[num]->hw_states_num);
  288. monitors[num]->start();
  289. }
  290. sleep(i);
  291. if (wake_cpus)
  292. for (cpu = 0; cpu < cpu_count; cpu++)
  293. bind_cpu(cpu);
  294. for (num = 0; num < avail_monitors; num++)
  295. monitors[num]->stop();
  296. return 0;
  297. }
  298. static void cmdline(int argc, char *argv[])
  299. {
  300. int opt;
  301. progname = basename(argv[0]);
  302. while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
  303. switch (opt) {
  304. case 'l':
  305. if (mode)
  306. print_wrong_arg_exit();
  307. mode = list;
  308. break;
  309. case 'i':
  310. /* only allow -i with -m or no option */
  311. if (mode && mode != show)
  312. print_wrong_arg_exit();
  313. interval = atoi(optarg);
  314. break;
  315. case 'm':
  316. if (mode)
  317. print_wrong_arg_exit();
  318. mode = show;
  319. show_monitors_param = optarg;
  320. break;
  321. case 'c':
  322. wake_cpus = 1;
  323. break;
  324. default:
  325. print_wrong_arg_exit();
  326. }
  327. }
  328. if (!mode)
  329. mode = show_all;
  330. }
  331. int cmd_monitor(int argc, char **argv)
  332. {
  333. unsigned int num;
  334. struct cpuidle_monitor *test_mon;
  335. int cpu;
  336. cmdline(argc, argv);
  337. cpu_count = get_cpu_topology(&cpu_top);
  338. if (cpu_count < 0) {
  339. printf(_("Cannot read number of available processors\n"));
  340. return EXIT_FAILURE;
  341. }
  342. if (!cpu_top.core_info[0].is_online)
  343. printf("WARNING: at least one cpu is offline\n");
  344. /* Default is: monitor all CPUs */
  345. if (bitmask_isallclear(cpus_chosen))
  346. bitmask_setall(cpus_chosen);
  347. dprint("System has up to %d CPU cores\n", cpu_count);
  348. for (num = 0; all_monitors[num]; num++) {
  349. dprint("Try to register: %s\n", all_monitors[num]->name);
  350. test_mon = all_monitors[num]->do_register();
  351. if (test_mon) {
  352. if (test_mon->needs_root && !run_as_root) {
  353. fprintf(stderr, _("Available monitor %s needs "
  354. "root access\n"), test_mon->name);
  355. continue;
  356. }
  357. monitors[avail_monitors] = test_mon;
  358. dprint("%s registered\n", all_monitors[num]->name);
  359. avail_monitors++;
  360. }
  361. }
  362. if (avail_monitors == 0) {
  363. printf(_("No HW Cstate monitors found\n"));
  364. return 1;
  365. }
  366. if (mode == list) {
  367. list_monitors();
  368. exit(EXIT_SUCCESS);
  369. }
  370. if (mode == show)
  371. parse_monitor_param(show_monitors_param);
  372. dprint("Packages: %d - Cores: %d - CPUs: %d\n",
  373. cpu_top.pkgs, cpu_top.cores, cpu_count);
  374. /*
  375. * if any params left, it must be a command to fork
  376. */
  377. if (argc - optind)
  378. fork_it(argv + optind);
  379. else
  380. do_interval_measure(interval);
  381. /* ToDo: Topology parsing needs fixing first to do
  382. this more generically */
  383. if (cpu_top.pkgs > 1)
  384. print_header(3);
  385. else
  386. print_header(1);
  387. for (cpu = 0; cpu < cpu_count; cpu++) {
  388. if (cpu_top.pkgs > 1)
  389. print_results(3, cpu);
  390. else
  391. print_results(1, cpu);
  392. }
  393. for (num = 0; num < avail_monitors; num++)
  394. monitors[num]->unregister();
  395. cpu_topology_release(cpu_top);
  396. return 0;
  397. }