cpupower-info.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. */
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <getopt.h>
  12. #include <cpufreq.h>
  13. #include "helpers/helpers.h"
  14. #include "helpers/sysfs.h"
  15. void info_help(void)
  16. {
  17. printf(_("Usage: cpupower info [ -b ] [ -m ] [ -s ]\n"));
  18. printf(_("Options:\n"));
  19. printf(_(" -b, --perf-bias Gets CPU's power vs performance policy on some\n"
  20. " Intel models [0-15], see manpage for details\n"));
  21. printf(_(" -m, --sched-mc Gets the kernel's multi core scheduler policy.\n"));
  22. printf(_(" -s, --sched-smt Gets the kernel's thread sibling scheduler policy.\n"));
  23. printf(_(" -h, --help Prints out this screen\n"));
  24. printf(_("\nPassing no option will show all info, by default only on core 0\n"));
  25. printf("\n");
  26. }
  27. static struct option set_opts[] = {
  28. { .name = "perf-bias", .has_arg = optional_argument, .flag = NULL, .val = 'b'},
  29. { .name = "sched-mc", .has_arg = optional_argument, .flag = NULL, .val = 'm'},
  30. { .name = "sched-smt", .has_arg = optional_argument, .flag = NULL, .val = 's'},
  31. { .name = "help", .has_arg = no_argument, .flag = NULL, .val = 'h'},
  32. { },
  33. };
  34. static void print_wrong_arg_exit(void)
  35. {
  36. printf(_("invalid or unknown argument\n"));
  37. info_help();
  38. exit(EXIT_FAILURE);
  39. }
  40. int cmd_info(int argc, char **argv)
  41. {
  42. extern char *optarg;
  43. extern int optind, opterr, optopt;
  44. unsigned int cpu;
  45. union {
  46. struct {
  47. int sched_mc:1;
  48. int sched_smt:1;
  49. int perf_bias:1;
  50. };
  51. int params;
  52. } params = {};
  53. int ret = 0;
  54. setlocale(LC_ALL, "");
  55. textdomain(PACKAGE);
  56. /* parameter parsing */
  57. while ((ret = getopt_long(argc, argv, "msbh", set_opts, NULL)) != -1) {
  58. switch (ret) {
  59. case 'h':
  60. info_help();
  61. return 0;
  62. case 'b':
  63. if (params.perf_bias)
  64. print_wrong_arg_exit();
  65. params.perf_bias = 1;
  66. break;
  67. case 'm':
  68. if (params.sched_mc)
  69. print_wrong_arg_exit();
  70. params.sched_mc = 1;
  71. break;
  72. case 's':
  73. if (params.sched_smt)
  74. print_wrong_arg_exit();
  75. params.sched_smt = 1;
  76. break;
  77. default:
  78. print_wrong_arg_exit();
  79. }
  80. };
  81. if (!params.params)
  82. params.params = 0x7;
  83. /* Default is: show output of CPU 0 only */
  84. if (bitmask_isallclear(cpus_chosen))
  85. bitmask_setbit(cpus_chosen, 0);
  86. if (params.sched_mc) {
  87. ret = sysfs_get_sched("mc");
  88. printf(_("System's multi core scheduler setting: "));
  89. if (ret < 0)
  90. /* if sysfs file is missing it's: errno == ENOENT */
  91. printf(_("not supported\n"));
  92. else
  93. printf("%d\n", ret);
  94. }
  95. if (params.sched_smt) {
  96. ret = sysfs_get_sched("smt");
  97. printf(_("System's thread sibling scheduler setting: "));
  98. if (ret < 0)
  99. /* if sysfs file is missing it's: errno == ENOENT */
  100. printf(_("not supported\n"));
  101. else
  102. printf("%d\n", ret);
  103. }
  104. /* Add more per cpu options here */
  105. if (!params.perf_bias)
  106. return ret;
  107. if (params.perf_bias) {
  108. if (!run_as_root) {
  109. params.perf_bias = 0;
  110. printf(_("Intel's performance bias setting needs root privileges\n"));
  111. } else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
  112. printf(_("System does not support Intel's performance"
  113. " bias setting\n"));
  114. params.perf_bias = 0;
  115. }
  116. }
  117. /* loop over CPUs */
  118. for (cpu = bitmask_first(cpus_chosen);
  119. cpu <= bitmask_last(cpus_chosen); cpu++) {
  120. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  121. cpufreq_cpu_exists(cpu))
  122. continue;
  123. printf(_("analyzing CPU %d:\n"), cpu);
  124. if (params.perf_bias) {
  125. ret = msr_intel_get_perf_bias(cpu);
  126. if (ret < 0) {
  127. printf(_("Could not read perf-bias value\n"));
  128. break;
  129. } else
  130. printf(_("perf-bias: %d\n"), ret);
  131. }
  132. }
  133. return ret;
  134. }