cpupower-monitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /* ToDo: Document this in the manpage */
  36. static char range_abbr[RANGE_MAX] = { 'T', 'C', 'P', 'M', };
  37. long long timespec_diff_us(struct timespec start, struct timespec end)
  38. {
  39. struct timespec temp;
  40. if ((end.tv_nsec - start.tv_nsec) < 0) {
  41. temp.tv_sec = end.tv_sec - start.tv_sec - 1;
  42. temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
  43. } else {
  44. temp.tv_sec = end.tv_sec - start.tv_sec;
  45. temp.tv_nsec = end.tv_nsec - start.tv_nsec;
  46. }
  47. return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
  48. }
  49. void monitor_help(void)
  50. {
  51. printf(_("cpupower monitor: [-m <mon1>,[<mon2>],.. ] command\n"));
  52. printf(_("cpupower monitor: [-m <mon1>,[<mon2>],.. ] [ -i interval_sec ]\n"));
  53. printf(_("cpupower monitor: -l\n"));
  54. printf(_("\t command: pass an arbitrary command to measure specific workload\n"));
  55. printf(_("\t -i: time intervall to measure for in seconds (default 1)\n"));
  56. printf(_("\t -l: list available CPU sleep monitors (for use with -m)\n"));
  57. printf(_("\t -m: show specific CPU sleep monitors only (in same order)\n"));
  58. printf(_("\t -h: print this help\n"));
  59. printf("\n");
  60. printf(_("only one of: -l, -m are allowed\nIf none of them is passed,"));
  61. printf(_(" all supported monitors are shown\n"));
  62. }
  63. void print_n_spaces(int n)
  64. {
  65. int x;
  66. for (x = 0; x < n; x++)
  67. printf(" ");
  68. }
  69. /* size of s must be at least n + 1 */
  70. int fill_string_with_spaces(char *s, int n)
  71. {
  72. int len = strlen(s);
  73. if (len > n)
  74. return -1;
  75. for (; len < n; len++)
  76. s[len] = ' ';
  77. s[len] = '\0';
  78. return 0;
  79. }
  80. void print_header(int topology_depth)
  81. {
  82. int unsigned mon;
  83. int state, need_len, pr_mon_len;
  84. cstate_t s;
  85. char buf[128] = "";
  86. int percent_width = 4;
  87. fill_string_with_spaces(buf, topology_depth * 5 - 1);
  88. printf("%s|", buf);
  89. for (mon = 0; mon < avail_monitors; mon++) {
  90. pr_mon_len = 0;
  91. need_len = monitors[mon]->hw_states_num * (percent_width + 3)
  92. - 1;
  93. if (mon != 0) {
  94. printf("|| ");
  95. need_len --;
  96. }
  97. sprintf(buf, "%s", monitors[mon]->name);
  98. fill_string_with_spaces(buf, need_len);
  99. printf("%s", buf);
  100. }
  101. printf("\n");
  102. if (topology_depth > 2)
  103. printf("PKG |");
  104. if (topology_depth > 1)
  105. printf("CORE|");
  106. if (topology_depth > 0)
  107. printf("CPU |");
  108. for (mon = 0; mon < avail_monitors; mon++) {
  109. if (mon != 0)
  110. printf("|| ");
  111. else
  112. printf(" ");
  113. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  114. if (state != 0)
  115. printf(" | ");
  116. s = monitors[mon]->hw_states[state];
  117. sprintf(buf, "%s", s.name);
  118. fill_string_with_spaces(buf, percent_width);
  119. printf("%s", buf);
  120. }
  121. printf(" ");
  122. }
  123. printf("\n");
  124. }
  125. void print_results(int topology_depth, int cpu)
  126. {
  127. unsigned int mon;
  128. int state, ret;
  129. double percent;
  130. unsigned long long result;
  131. cstate_t s;
  132. if (topology_depth > 2)
  133. printf("%4d|", cpu_top.core_info[cpu].pkg);
  134. if (topology_depth > 1)
  135. printf("%4d|", cpu_top.core_info[cpu].core);
  136. if (topology_depth > 0)
  137. printf("%4d|", cpu_top.core_info[cpu].cpu);
  138. for (mon = 0; mon < avail_monitors; mon++) {
  139. if (mon != 0)
  140. printf("||");
  141. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  142. if (state != 0)
  143. printf("|");
  144. s = monitors[mon]->hw_states[state];
  145. if (s.get_count_percent) {
  146. ret = s.get_count_percent(s.id, &percent,
  147. cpu_top.core_info[cpu].cpu);
  148. if (ret) {
  149. printf("******");
  150. } else if (percent >= 100.0)
  151. printf("%6.1f", percent);
  152. else
  153. printf("%6.2f", percent);
  154. }
  155. else if (s.get_count) {
  156. ret = s.get_count(s.id, &result,
  157. cpu_top.core_info[cpu].cpu);
  158. if (ret) {
  159. printf("******");
  160. } else
  161. printf("%6llu", result);
  162. }
  163. else {
  164. printf(_("Monitor %s, Counter %s has no count "
  165. "function. Implementation error\n"),
  166. monitors[mon]->name, s.name);
  167. exit (EXIT_FAILURE);
  168. }
  169. }
  170. }
  171. /* cpu offline */
  172. if (cpu_top.core_info[cpu].pkg == -1 ||
  173. cpu_top.core_info[cpu].core == -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. monitor_help();
  214. exit(EXIT_FAILURE);
  215. }
  216. /* Override detected/registerd monitors array with requested one */
  217. memcpy(monitors, tmp_mons, sizeof(struct cpuidle_monitor*) * MONITORS_MAX);
  218. avail_monitors = hits;
  219. }
  220. void list_monitors(void) {
  221. unsigned int mon;
  222. int state;
  223. cstate_t s;
  224. for (mon = 0; mon < avail_monitors; mon++) {
  225. printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
  226. "s\n"), monitors[mon]->name, monitors[mon]->hw_states_num,
  227. monitors[mon]->overflow_s);
  228. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  229. s = monitors[mon]->hw_states[state];
  230. /*
  231. * ToDo show more state capabilities:
  232. * percent, time (granlarity)
  233. */
  234. printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
  235. gettext(s.desc));
  236. }
  237. }
  238. }
  239. int fork_it(char **argv)
  240. {
  241. int status;
  242. unsigned int num;
  243. unsigned long long timediff;
  244. pid_t child_pid;
  245. struct timespec start, end;
  246. child_pid = fork();
  247. clock_gettime(CLOCK_REALTIME, &start);
  248. for (num = 0; num < avail_monitors; num++)
  249. monitors[num]->start();
  250. if (!child_pid) {
  251. /* child */
  252. execvp(argv[0], argv);
  253. } else {
  254. /* parent */
  255. if (child_pid == -1) {
  256. perror("fork");
  257. exit(1);
  258. }
  259. signal(SIGINT, SIG_IGN);
  260. signal(SIGQUIT, SIG_IGN);
  261. if (waitpid(child_pid, &status, 0) == -1) {
  262. perror("wait");
  263. exit(1);
  264. }
  265. }
  266. clock_gettime(CLOCK_REALTIME, &end);
  267. for (num = 0; num < avail_monitors; num++)
  268. monitors[num]->stop();
  269. timediff = timespec_diff_us(start, end);
  270. if (WIFEXITED(status))
  271. printf(_("%s took %.5f seconds and exited with status %d\n"),
  272. argv[0], timediff / (1000.0 * 1000), WEXITSTATUS(status));
  273. return 0;
  274. }
  275. int do_interval_measure(int i)
  276. {
  277. unsigned int num;
  278. for (num = 0; num < avail_monitors; num++) {
  279. dprint("HW C-state residency monitor: %s - States: %d\n",
  280. monitors[num]->name, monitors[num]->hw_states_num);
  281. monitors[num]->start();
  282. }
  283. sleep(i);
  284. for (num = 0; num < avail_monitors; num++) {
  285. monitors[num]->stop();
  286. }
  287. return 0;
  288. }
  289. static void cmdline(int argc, char *argv[])
  290. {
  291. int opt;
  292. progname = basename(argv[0]);
  293. while ((opt = getopt(argc, argv, "+hli:m:")) != -1) {
  294. switch (opt) {
  295. case 'h':
  296. monitor_help();
  297. exit(EXIT_SUCCESS);
  298. case 'l':
  299. if (mode) {
  300. monitor_help();
  301. exit(EXIT_FAILURE);
  302. }
  303. mode = list;
  304. break;
  305. case 'i':
  306. /* only allow -i with -m or no option */
  307. if (mode && mode != show) {
  308. monitor_help();
  309. exit(EXIT_FAILURE);
  310. }
  311. interval = atoi(optarg);
  312. break;
  313. case 'm':
  314. if (mode) {
  315. monitor_help();
  316. exit(EXIT_FAILURE);
  317. }
  318. mode = show;
  319. show_monitors_param = optarg;
  320. break;
  321. default:
  322. monitor_help();
  323. exit(EXIT_FAILURE);
  324. }
  325. }
  326. if (!mode)
  327. mode = show_all;
  328. }
  329. int cmd_monitor(int argc, char **argv)
  330. {
  331. unsigned int num;
  332. struct cpuidle_monitor *test_mon;
  333. int cpu;
  334. cmdline(argc, argv);
  335. cpu_count = get_cpu_topology(&cpu_top);
  336. if (cpu_count < 0) {
  337. printf(_("Cannot read number of available processors\n"));
  338. return EXIT_FAILURE;
  339. }
  340. dprint("System has up to %d CPU cores\n", cpu_count);
  341. for (num = 0; all_monitors[num]; num++) {
  342. dprint("Try to register: %s\n", all_monitors[num]->name);
  343. test_mon = all_monitors[num]->do_register();
  344. if (test_mon) {
  345. if (test_mon->needs_root && !run_as_root) {
  346. fprintf(stderr, _("Available monitor %s needs "
  347. "root access\n"), test_mon->name);
  348. continue;
  349. }
  350. monitors[avail_monitors] = test_mon;
  351. dprint("%s registered\n", all_monitors[num]->name);
  352. avail_monitors++;
  353. }
  354. }
  355. if (avail_monitors == 0) {
  356. printf(_("No HW Cstate monitors found\n"));
  357. return 1;
  358. }
  359. if (mode == list) {
  360. list_monitors();
  361. exit(EXIT_SUCCESS);
  362. }
  363. if (mode == show)
  364. parse_monitor_param(show_monitors_param);
  365. dprint("Packages: %d - Cores: %d - CPUs: %d\n",
  366. cpu_top.pkgs, cpu_top.cores, cpu_count);
  367. /*
  368. * if any params left, it must be a command to fork
  369. */
  370. if (argc - optind)
  371. fork_it(argv + optind);
  372. else
  373. do_interval_measure(interval);
  374. /* ToDo: Topology parsing needs fixing first to do
  375. this more generically */
  376. if (cpu_top.pkgs > 1)
  377. print_header(3);
  378. else
  379. print_header(1);
  380. for (cpu = 0; cpu < cpu_count; cpu++) {
  381. if (cpu_top.pkgs > 1)
  382. print_results(3, cpu);
  383. else
  384. print_results(1, cpu);
  385. }
  386. for (num = 0; num < avail_monitors; num++) {
  387. monitors[num]->unregister();
  388. }
  389. cpu_topology_release(cpu_top);
  390. return 0;
  391. }