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