cpupower.c 5.2 KB

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