cpupower-monitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. } else if (s.get_count) {
  155. ret = s.get_count(s.id, &result,
  156. cpu_top.core_info[cpu].cpu);
  157. if (ret)
  158. printf("******");
  159. else
  160. printf("%6llu", result);
  161. } else {
  162. printf(_("Monitor %s, Counter %s has no count "
  163. "function. Implementation error\n"),
  164. monitors[mon]->name, s.name);
  165. exit(EXIT_FAILURE);
  166. }
  167. }
  168. }
  169. /* cpu offline */
  170. if (cpu_top.core_info[cpu].pkg == -1 ||
  171. cpu_top.core_info[cpu].core == -1) {
  172. printf(_(" *is offline\n"));
  173. return;
  174. } else
  175. printf("\n");
  176. }
  177. /* param: string passed by -m param (The list of monitors to show)
  178. *
  179. * Monitors must have been registered already, matching monitors
  180. * are picked out and available monitors array is overridden
  181. * with matching ones
  182. *
  183. * Monitors get sorted in the same order the user passes them
  184. */
  185. static void parse_monitor_param(char *param)
  186. {
  187. unsigned int num;
  188. int mon, hits = 0;
  189. char *tmp = param, *token;
  190. struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
  191. for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
  192. token = strtok(tmp, ",");
  193. if (token == NULL)
  194. break;
  195. if (strlen(token) >= MONITOR_NAME_LEN) {
  196. printf(_("%s: max monitor name length"
  197. " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
  198. continue;
  199. }
  200. for (num = 0; num < avail_monitors; num++) {
  201. if (!strcmp(monitors[num]->name, token)) {
  202. dprint("Found requested monitor: %s\n", token);
  203. tmp_mons[hits] = monitors[num];
  204. hits++;
  205. }
  206. }
  207. }
  208. if (hits == 0) {
  209. printf(_("No matching monitor found in %s, "
  210. "try -l option\n"), param);
  211. monitor_help();
  212. exit(EXIT_FAILURE);
  213. }
  214. /* Override detected/registerd monitors array with requested one */
  215. memcpy(monitors, tmp_mons,
  216. sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
  217. avail_monitors = hits;
  218. }
  219. void list_monitors(void)
  220. {
  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"),
  227. monitors[mon]->name, monitors[mon]->hw_states_num,
  228. monitors[mon]->overflow_s);
  229. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  230. s = monitors[mon]->hw_states[state];
  231. /*
  232. * ToDo show more state capabilities:
  233. * percent, time (granlarity)
  234. */
  235. printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
  236. gettext(s.desc));
  237. }
  238. }
  239. }
  240. int fork_it(char **argv)
  241. {
  242. int status;
  243. unsigned int num;
  244. unsigned long long timediff;
  245. pid_t child_pid;
  246. struct timespec start, end;
  247. child_pid = fork();
  248. clock_gettime(CLOCK_REALTIME, &start);
  249. for (num = 0; num < avail_monitors; num++)
  250. monitors[num]->start();
  251. if (!child_pid) {
  252. /* child */
  253. execvp(argv[0], argv);
  254. } else {
  255. /* parent */
  256. if (child_pid == -1) {
  257. perror("fork");
  258. exit(1);
  259. }
  260. signal(SIGINT, SIG_IGN);
  261. signal(SIGQUIT, SIG_IGN);
  262. if (waitpid(child_pid, &status, 0) == -1) {
  263. perror("wait");
  264. exit(1);
  265. }
  266. }
  267. clock_gettime(CLOCK_REALTIME, &end);
  268. for (num = 0; num < avail_monitors; num++)
  269. monitors[num]->stop();
  270. timediff = timespec_diff_us(start, end);
  271. if (WIFEXITED(status))
  272. printf(_("%s took %.5f seconds and exited with status %d\n"),
  273. argv[0], timediff / (1000.0 * 1000),
  274. WEXITSTATUS(status));
  275. return 0;
  276. }
  277. int do_interval_measure(int i)
  278. {
  279. unsigned int num;
  280. for (num = 0; num < avail_monitors; num++) {
  281. dprint("HW C-state residency monitor: %s - States: %d\n",
  282. monitors[num]->name, monitors[num]->hw_states_num);
  283. monitors[num]->start();
  284. }
  285. sleep(i);
  286. for (num = 0; num < avail_monitors; num++)
  287. monitors[num]->stop();
  288. return 0;
  289. }
  290. static void cmdline(int argc, char *argv[])
  291. {
  292. int opt;
  293. progname = basename(argv[0]);
  294. while ((opt = getopt(argc, argv, "+hli:m:")) != -1) {
  295. switch (opt) {
  296. case 'h':
  297. monitor_help();
  298. exit(EXIT_SUCCESS);
  299. case 'l':
  300. if (mode) {
  301. monitor_help();
  302. exit(EXIT_FAILURE);
  303. }
  304. mode = list;
  305. break;
  306. case 'i':
  307. /* only allow -i with -m or no option */
  308. if (mode && mode != show) {
  309. monitor_help();
  310. exit(EXIT_FAILURE);
  311. }
  312. interval = atoi(optarg);
  313. break;
  314. case 'm':
  315. if (mode) {
  316. monitor_help();
  317. exit(EXIT_FAILURE);
  318. }
  319. mode = show;
  320. show_monitors_param = optarg;
  321. break;
  322. default:
  323. monitor_help();
  324. exit(EXIT_FAILURE);
  325. }
  326. }
  327. if (!mode)
  328. mode = show_all;
  329. }
  330. int cmd_monitor(int argc, char **argv)
  331. {
  332. unsigned int num;
  333. struct cpuidle_monitor *test_mon;
  334. int cpu;
  335. cmdline(argc, argv);
  336. cpu_count = get_cpu_topology(&cpu_top);
  337. if (cpu_count < 0) {
  338. printf(_("Cannot read number of available processors\n"));
  339. return EXIT_FAILURE;
  340. }
  341. dprint("System has up to %d CPU cores\n", cpu_count);
  342. for (num = 0; all_monitors[num]; num++) {
  343. dprint("Try to register: %s\n", all_monitors[num]->name);
  344. test_mon = all_monitors[num]->do_register();
  345. if (test_mon) {
  346. if (test_mon->needs_root && !run_as_root) {
  347. fprintf(stderr, _("Available monitor %s needs "
  348. "root access\n"), test_mon->name);
  349. continue;
  350. }
  351. monitors[avail_monitors] = test_mon;
  352. dprint("%s registered\n", all_monitors[num]->name);
  353. avail_monitors++;
  354. }
  355. }
  356. if (avail_monitors == 0) {
  357. printf(_("No HW Cstate monitors found\n"));
  358. return 1;
  359. }
  360. if (mode == list) {
  361. list_monitors();
  362. exit(EXIT_SUCCESS);
  363. }
  364. if (mode == show)
  365. parse_monitor_param(show_monitors_param);
  366. dprint("Packages: %d - Cores: %d - CPUs: %d\n",
  367. cpu_top.pkgs, cpu_top.cores, cpu_count);
  368. /*
  369. * if any params left, it must be a command to fork
  370. */
  371. if (argc - optind)
  372. fork_it(argv + optind);
  373. else
  374. do_interval_measure(interval);
  375. /* ToDo: Topology parsing needs fixing first to do
  376. this more generically */
  377. if (cpu_top.pkgs > 1)
  378. print_header(3);
  379. else
  380. print_header(1);
  381. for (cpu = 0; cpu < cpu_count; cpu++) {
  382. if (cpu_top.pkgs > 1)
  383. print_results(3, cpu);
  384. else
  385. print_results(1, cpu);
  386. }
  387. for (num = 0; num < avail_monitors; num++)
  388. monitors[num]->unregister();
  389. cpu_topology_release(cpu_top);
  390. return 0;
  391. }