cpupower-info.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. static struct option set_opts[] = {
  16. { .name = "perf-bias", .has_arg = optional_argument, .flag = NULL, .val = 'b'},
  17. { .name = "sched-mc", .has_arg = optional_argument, .flag = NULL, .val = 'm'},
  18. { .name = "sched-smt", .has_arg = optional_argument, .flag = NULL, .val = 's'},
  19. { },
  20. };
  21. static void print_wrong_arg_exit(void)
  22. {
  23. printf(_("invalid or unknown argument\n"));
  24. exit(EXIT_FAILURE);
  25. }
  26. int cmd_info(int argc, char **argv)
  27. {
  28. extern char *optarg;
  29. extern int optind, opterr, optopt;
  30. unsigned int cpu;
  31. union {
  32. struct {
  33. int sched_mc:1;
  34. int sched_smt:1;
  35. int perf_bias:1;
  36. };
  37. int params;
  38. } params = {};
  39. int ret = 0;
  40. setlocale(LC_ALL, "");
  41. textdomain(PACKAGE);
  42. /* parameter parsing */
  43. while ((ret = getopt_long(argc, argv, "msb", set_opts, NULL)) != -1) {
  44. switch (ret) {
  45. case 'b':
  46. if (params.perf_bias)
  47. print_wrong_arg_exit();
  48. params.perf_bias = 1;
  49. break;
  50. case 'm':
  51. if (params.sched_mc)
  52. print_wrong_arg_exit();
  53. params.sched_mc = 1;
  54. break;
  55. case 's':
  56. if (params.sched_smt)
  57. print_wrong_arg_exit();
  58. params.sched_smt = 1;
  59. break;
  60. default:
  61. print_wrong_arg_exit();
  62. }
  63. };
  64. if (!params.params)
  65. params.params = 0x7;
  66. /* Default is: show output of CPU 0 only */
  67. if (bitmask_isallclear(cpus_chosen))
  68. bitmask_setbit(cpus_chosen, 0);
  69. if (params.sched_mc) {
  70. ret = sysfs_get_sched("mc");
  71. printf(_("System's multi core scheduler setting: "));
  72. if (ret < 0)
  73. /* if sysfs file is missing it's: errno == ENOENT */
  74. printf(_("not supported\n"));
  75. else
  76. printf("%d\n", ret);
  77. }
  78. if (params.sched_smt) {
  79. ret = sysfs_get_sched("smt");
  80. printf(_("System's thread sibling scheduler setting: "));
  81. if (ret < 0)
  82. /* if sysfs file is missing it's: errno == ENOENT */
  83. printf(_("not supported\n"));
  84. else
  85. printf("%d\n", ret);
  86. }
  87. /* Add more per cpu options here */
  88. if (!params.perf_bias)
  89. return ret;
  90. if (params.perf_bias) {
  91. if (!run_as_root) {
  92. params.perf_bias = 0;
  93. printf(_("Intel's performance bias setting needs root privileges\n"));
  94. } else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
  95. printf(_("System does not support Intel's performance"
  96. " bias setting\n"));
  97. params.perf_bias = 0;
  98. }
  99. }
  100. /* loop over CPUs */
  101. for (cpu = bitmask_first(cpus_chosen);
  102. cpu <= bitmask_last(cpus_chosen); cpu++) {
  103. if (!bitmask_isbitset(cpus_chosen, cpu) ||
  104. cpufreq_cpu_exists(cpu))
  105. continue;
  106. printf(_("analyzing CPU %d:\n"), cpu);
  107. if (params.perf_bias) {
  108. ret = msr_intel_get_perf_bias(cpu);
  109. if (ret < 0) {
  110. printf(_("Could not read perf-bias value\n"));
  111. break;
  112. } else
  113. printf(_("perf-bias: %d\n"), ret);
  114. }
  115. }
  116. return ret;
  117. }