cpupower-monitor.c 9.7 KB

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