cpupower-monitor.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. static void print_wrong_arg_exit(void)
  38. {
  39. printf(_("invalid or unknown argument\n"));
  40. exit(EXIT_FAILURE);
  41. }
  42. long long timespec_diff_us(struct timespec start, struct timespec end)
  43. {
  44. struct timespec temp;
  45. if ((end.tv_nsec - start.tv_nsec) < 0) {
  46. temp.tv_sec = end.tv_sec - start.tv_sec - 1;
  47. temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
  48. } else {
  49. temp.tv_sec = end.tv_sec - start.tv_sec;
  50. temp.tv_nsec = end.tv_nsec - start.tv_nsec;
  51. }
  52. return (temp.tv_sec * 1000000) + (temp.tv_nsec / 1000);
  53. }
  54. void print_n_spaces(int n)
  55. {
  56. int x;
  57. for (x = 0; x < n; x++)
  58. printf(" ");
  59. }
  60. /* size of s must be at least n + 1 */
  61. int fill_string_with_spaces(char *s, int n)
  62. {
  63. int len = strlen(s);
  64. if (len > n)
  65. return -1;
  66. for (; len < n; len++)
  67. s[len] = ' ';
  68. s[len] = '\0';
  69. return 0;
  70. }
  71. void print_header(int topology_depth)
  72. {
  73. int unsigned mon;
  74. int state, need_len, pr_mon_len;
  75. cstate_t s;
  76. char buf[128] = "";
  77. int percent_width = 4;
  78. fill_string_with_spaces(buf, topology_depth * 5 - 1);
  79. printf("%s|", buf);
  80. for (mon = 0; mon < avail_monitors; mon++) {
  81. pr_mon_len = 0;
  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 (topology_depth > 2)
  127. printf("%4d|", cpu_top.core_info[cpu].pkg);
  128. if (topology_depth > 1)
  129. printf("%4d|", cpu_top.core_info[cpu].core);
  130. if (topology_depth > 0)
  131. printf("%4d|", cpu_top.core_info[cpu].cpu);
  132. for (mon = 0; mon < avail_monitors; mon++) {
  133. if (mon != 0)
  134. printf("||");
  135. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  136. if (state != 0)
  137. printf("|");
  138. s = monitors[mon]->hw_states[state];
  139. if (s.get_count_percent) {
  140. ret = s.get_count_percent(s.id, &percent,
  141. cpu_top.core_info[cpu].cpu);
  142. if (ret)
  143. printf("******");
  144. else if (percent >= 100.0)
  145. printf("%6.1f", percent);
  146. else
  147. printf("%6.2f", percent);
  148. } else if (s.get_count) {
  149. ret = s.get_count(s.id, &result,
  150. cpu_top.core_info[cpu].cpu);
  151. if (ret)
  152. printf("******");
  153. else
  154. printf("%6llu", result);
  155. } else {
  156. printf(_("Monitor %s, Counter %s has no count "
  157. "function. Implementation error\n"),
  158. monitors[mon]->name, s.name);
  159. exit(EXIT_FAILURE);
  160. }
  161. }
  162. }
  163. /*
  164. * The monitor could still provide useful data, for example
  165. * AMD HW counters partly sit in PCI config space.
  166. * It's up to the monitor plug-in to check .is_online, this one
  167. * is just for additional info.
  168. */
  169. if (!cpu_top.core_info[cpu].is_online) {
  170. printf(_(" *is offline\n"));
  171. return;
  172. } else
  173. printf("\n");
  174. }
  175. /* param: string passed by -m param (The list of monitors to show)
  176. *
  177. * Monitors must have been registered already, matching monitors
  178. * are picked out and available monitors array is overridden
  179. * with matching ones
  180. *
  181. * Monitors get sorted in the same order the user passes them
  182. */
  183. static void parse_monitor_param(char *param)
  184. {
  185. unsigned int num;
  186. int mon, hits = 0;
  187. char *tmp = param, *token;
  188. struct cpuidle_monitor *tmp_mons[MONITORS_MAX];
  189. for (mon = 0; mon < MONITORS_MAX; mon++, tmp = NULL) {
  190. token = strtok(tmp, ",");
  191. if (token == NULL)
  192. break;
  193. if (strlen(token) >= MONITOR_NAME_LEN) {
  194. printf(_("%s: max monitor name length"
  195. " (%d) exceeded\n"), token, MONITOR_NAME_LEN);
  196. continue;
  197. }
  198. for (num = 0; num < avail_monitors; num++) {
  199. if (!strcmp(monitors[num]->name, token)) {
  200. dprint("Found requested monitor: %s\n", token);
  201. tmp_mons[hits] = monitors[num];
  202. hits++;
  203. }
  204. }
  205. }
  206. if (hits == 0) {
  207. printf(_("No matching monitor found in %s, "
  208. "try -l option\n"), param);
  209. exit(EXIT_FAILURE);
  210. }
  211. /* Override detected/registerd monitors array with requested one */
  212. memcpy(monitors, tmp_mons,
  213. sizeof(struct cpuidle_monitor *) * MONITORS_MAX);
  214. avail_monitors = hits;
  215. }
  216. void list_monitors(void)
  217. {
  218. unsigned int mon;
  219. int state;
  220. cstate_t s;
  221. for (mon = 0; mon < avail_monitors; mon++) {
  222. printf(_("Monitor \"%s\" (%d states) - Might overflow after %u "
  223. "s\n"),
  224. monitors[mon]->name, monitors[mon]->hw_states_num,
  225. monitors[mon]->overflow_s);
  226. for (state = 0; state < monitors[mon]->hw_states_num; state++) {
  227. s = monitors[mon]->hw_states[state];
  228. /*
  229. * ToDo show more state capabilities:
  230. * percent, time (granlarity)
  231. */
  232. printf("%s\t[%c] -> %s\n", s.name, range_abbr[s.range],
  233. gettext(s.desc));
  234. }
  235. }
  236. }
  237. int fork_it(char **argv)
  238. {
  239. int status;
  240. unsigned int num;
  241. unsigned long long timediff;
  242. pid_t child_pid;
  243. struct timespec start, end;
  244. child_pid = fork();
  245. clock_gettime(CLOCK_REALTIME, &start);
  246. for (num = 0; num < avail_monitors; num++)
  247. monitors[num]->start();
  248. if (!child_pid) {
  249. /* child */
  250. execvp(argv[0], argv);
  251. } else {
  252. /* parent */
  253. if (child_pid == -1) {
  254. perror("fork");
  255. exit(1);
  256. }
  257. signal(SIGINT, SIG_IGN);
  258. signal(SIGQUIT, SIG_IGN);
  259. if (waitpid(child_pid, &status, 0) == -1) {
  260. perror("wait");
  261. exit(1);
  262. }
  263. }
  264. clock_gettime(CLOCK_REALTIME, &end);
  265. for (num = 0; num < avail_monitors; num++)
  266. monitors[num]->stop();
  267. timediff = timespec_diff_us(start, end);
  268. if (WIFEXITED(status))
  269. printf(_("%s took %.5f seconds and exited with status %d\n"),
  270. argv[0], timediff / (1000.0 * 1000),
  271. WEXITSTATUS(status));
  272. return 0;
  273. }
  274. int do_interval_measure(int i)
  275. {
  276. unsigned int num;
  277. for (num = 0; num < avail_monitors; num++) {
  278. dprint("HW C-state residency monitor: %s - States: %d\n",
  279. monitors[num]->name, monitors[num]->hw_states_num);
  280. monitors[num]->start();
  281. }
  282. sleep(i);
  283. for (num = 0; num < avail_monitors; num++)
  284. monitors[num]->stop();
  285. return 0;
  286. }
  287. static void cmdline(int argc, char *argv[])
  288. {
  289. int opt;
  290. progname = basename(argv[0]);
  291. while ((opt = getopt(argc, argv, "+li:m:")) != -1) {
  292. switch (opt) {
  293. case 'l':
  294. if (mode)
  295. print_wrong_arg_exit();
  296. mode = list;
  297. break;
  298. case 'i':
  299. /* only allow -i with -m or no option */
  300. if (mode && mode != show)
  301. print_wrong_arg_exit();
  302. interval = atoi(optarg);
  303. break;
  304. case 'm':
  305. if (mode)
  306. print_wrong_arg_exit();
  307. mode = show;
  308. show_monitors_param = optarg;
  309. break;
  310. default:
  311. print_wrong_arg_exit();
  312. }
  313. }
  314. if (!mode)
  315. mode = show_all;
  316. }
  317. int cmd_monitor(int argc, char **argv)
  318. {
  319. unsigned int num;
  320. struct cpuidle_monitor *test_mon;
  321. int cpu;
  322. cmdline(argc, argv);
  323. cpu_count = get_cpu_topology(&cpu_top);
  324. if (cpu_count < 0) {
  325. printf(_("Cannot read number of available processors\n"));
  326. return EXIT_FAILURE;
  327. }
  328. /* Default is: monitor all CPUs */
  329. if (bitmask_isallclear(cpus_chosen))
  330. bitmask_setall(cpus_chosen);
  331. dprint("System has up to %d CPU cores\n", cpu_count);
  332. for (num = 0; all_monitors[num]; num++) {
  333. dprint("Try to register: %s\n", all_monitors[num]->name);
  334. test_mon = all_monitors[num]->do_register();
  335. if (test_mon) {
  336. if (test_mon->needs_root && !run_as_root) {
  337. fprintf(stderr, _("Available monitor %s needs "
  338. "root access\n"), test_mon->name);
  339. continue;
  340. }
  341. monitors[avail_monitors] = test_mon;
  342. dprint("%s registered\n", all_monitors[num]->name);
  343. avail_monitors++;
  344. }
  345. }
  346. if (avail_monitors == 0) {
  347. printf(_("No HW Cstate monitors found\n"));
  348. return 1;
  349. }
  350. if (mode == list) {
  351. list_monitors();
  352. exit(EXIT_SUCCESS);
  353. }
  354. if (mode == show)
  355. parse_monitor_param(show_monitors_param);
  356. dprint("Packages: %d - Cores: %d - CPUs: %d\n",
  357. cpu_top.pkgs, cpu_top.cores, cpu_count);
  358. /*
  359. * if any params left, it must be a command to fork
  360. */
  361. if (argc - optind)
  362. fork_it(argv + optind);
  363. else
  364. do_interval_measure(interval);
  365. /* ToDo: Topology parsing needs fixing first to do
  366. this more generically */
  367. if (cpu_top.pkgs > 1)
  368. print_header(3);
  369. else
  370. print_header(1);
  371. for (cpu = 0; cpu < cpu_count; cpu++) {
  372. if (cpu_top.pkgs > 1)
  373. print_results(3, cpu);
  374. else
  375. print_results(1, cpu);
  376. }
  377. for (num = 0; num < avail_monitors; num++)
  378. monitors[num]->unregister();
  379. cpu_topology_release(cpu_top);
  380. return 0;
  381. }