cpupower.c 5.0 KB

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