cpupower.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * Ideas taken over from the perf userspace tool (included in the Linus
  7. * kernel git repo): subcommand builtins and param parsing.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include "builtin.h"
  15. #include "helpers/helpers.h"
  16. #include "helpers/bitmask.h"
  17. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  18. static int cmd_help(int argc, const char **argv);
  19. /* Global cpu_info object available for all binaries
  20. * Info only retrieved from CPU 0
  21. *
  22. * Values will be zero/unknown on non X86 archs
  23. */
  24. struct cpupower_cpu_info cpupower_cpu_info;
  25. int run_as_root;
  26. /* Affected cpus chosen by -c/--cpu param */
  27. struct bitmask *cpus_chosen;
  28. #ifdef DEBUG
  29. int be_verbose;
  30. #endif
  31. static void print_help(void);
  32. struct cmd_struct {
  33. const char *cmd;
  34. int (*main)(int, const char **);
  35. int needs_root;
  36. };
  37. static struct cmd_struct commands[] = {
  38. { "frequency-info", cmd_freq_info, 0 },
  39. { "frequency-set", cmd_freq_set, 1 },
  40. { "idle-info", cmd_idle_info, 0 },
  41. { "idle-set", cmd_idle_set, 1 },
  42. { "set", cmd_set, 1 },
  43. { "info", cmd_info, 0 },
  44. { "monitor", cmd_monitor, 0 },
  45. { "help", cmd_help, 0 },
  46. /* { "bench", cmd_bench, 1 }, */
  47. };
  48. static void print_help(void)
  49. {
  50. unsigned int i;
  51. #ifdef DEBUG
  52. printf(_("Usage:\tcpupower [-d|--debug] [-c|--cpu cpulist ] <command> [<args>]\n"));
  53. #else
  54. printf(_("Usage:\tcpupower [-c|--cpu cpulist ] <command> [<args>]\n"));
  55. #endif
  56. printf(_("Supported commands are:\n"));
  57. for (i = 0; i < ARRAY_SIZE(commands); i++)
  58. printf("\t%s\n", commands[i].cmd);
  59. printf(_("\nNot all commands can make use of the -c cpulist option.\n"));
  60. printf(_("\nUse 'cpupower help <command>' for getting help for above commands.\n"));
  61. }
  62. static int print_man_page(const char *subpage)
  63. {
  64. int len;
  65. char *page;
  66. len = 10; /* enough for "cpupower-" */
  67. if (subpage != NULL)
  68. len += strlen(subpage);
  69. page = malloc(len);
  70. if (!page)
  71. return -ENOMEM;
  72. sprintf(page, "cpupower");
  73. if ((subpage != NULL) && strcmp(subpage, "help")) {
  74. strcat(page, "-");
  75. strcat(page, subpage);
  76. }
  77. execlp("man", "man", page, NULL);
  78. /* should not be reached */
  79. return -EINVAL;
  80. }
  81. static int cmd_help(int argc, const char **argv)
  82. {
  83. if (argc > 1) {
  84. print_man_page(argv[1]); /* exits within execlp() */
  85. return EXIT_FAILURE;
  86. }
  87. print_help();
  88. return EXIT_SUCCESS;
  89. }
  90. static void print_version(void)
  91. {
  92. printf(PACKAGE " " VERSION "\n");
  93. printf(_("Report errors and bugs to %s, please.\n"), PACKAGE_BUGREPORT);
  94. }
  95. static void handle_options(int *argc, const char ***argv)
  96. {
  97. int ret, x, new_argc = 0;
  98. if (*argc < 1)
  99. return;
  100. for (x = 0; x < *argc && ((*argv)[x])[0] == '-'; x++) {
  101. const char *param = (*argv)[x];
  102. if (!strcmp(param, "-h") || !strcmp(param, "--help")) {
  103. print_help();
  104. exit(EXIT_SUCCESS);
  105. } else if (!strcmp(param, "-c") || !strcmp(param, "--cpu")) {
  106. if (*argc < 2) {
  107. print_help();
  108. exit(EXIT_FAILURE);
  109. }
  110. if (!strcmp((*argv)[x+1], "all"))
  111. bitmask_setall(cpus_chosen);
  112. else {
  113. ret = bitmask_parselist(
  114. (*argv)[x+1], cpus_chosen);
  115. if (ret < 0) {
  116. fprintf(stderr, _("Error parsing cpu "
  117. "list\n"));
  118. exit(EXIT_FAILURE);
  119. }
  120. }
  121. x += 1;
  122. /* Cut out param: cpupower -c 1 info -> cpupower info */
  123. new_argc += 2;
  124. continue;
  125. } else if (!strcmp(param, "-v") ||
  126. !strcmp(param, "--version")) {
  127. print_version();
  128. exit(EXIT_SUCCESS);
  129. #ifdef DEBUG
  130. } else if (!strcmp(param, "-d") || !strcmp(param, "--debug")) {
  131. be_verbose = 1;
  132. new_argc++;
  133. continue;
  134. #endif
  135. } else {
  136. fprintf(stderr, "Unknown option: %s\n", param);
  137. print_help();
  138. exit(EXIT_FAILURE);
  139. }
  140. }
  141. *argc -= new_argc;
  142. *argv += new_argc;
  143. }
  144. int main(int argc, const char *argv[])
  145. {
  146. const char *cmd;
  147. unsigned int i, ret;
  148. cpus_chosen = bitmask_alloc(sysconf(_SC_NPROCESSORS_CONF));
  149. argc--;
  150. argv += 1;
  151. handle_options(&argc, &argv);
  152. cmd = argv[0];
  153. if (argc < 1) {
  154. print_help();
  155. return EXIT_FAILURE;
  156. }
  157. setlocale(LC_ALL, "");
  158. textdomain(PACKAGE);
  159. /* Turn "perf cmd --help" into "perf help cmd" */
  160. if (argc > 1 && !strcmp(argv[1], "--help")) {
  161. argv[1] = argv[0];
  162. argv[0] = cmd = "help";
  163. }
  164. get_cpu_info(0, &cpupower_cpu_info);
  165. run_as_root = !getuid();
  166. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  167. struct cmd_struct *p = commands + i;
  168. if (strcmp(p->cmd, cmd))
  169. continue;
  170. if (!run_as_root && p->needs_root) {
  171. fprintf(stderr, _("Subcommand %s needs root "
  172. "privileges\n"), cmd);
  173. return EXIT_FAILURE;
  174. }
  175. ret = p->main(argc, argv);
  176. if (cpus_chosen)
  177. bitmask_free(cpus_chosen);
  178. return ret;
  179. }
  180. print_help();
  181. return EXIT_FAILURE;
  182. }