cpupower-info.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <getopt.h>
  11. #include "helpers/helpers.h"
  12. #include "helpers/sysfs.h"
  13. static struct option set_opts[] = {
  14. {"perf-bias", optional_argument, NULL, 'b'},
  15. { },
  16. };
  17. static void print_wrong_arg_exit(void)
  18. {
  19. printf(_("invalid or unknown argument\n"));
  20. exit(EXIT_FAILURE);
  21. }
  22. int cmd_info(int argc, char **argv)
  23. {
  24. extern char *optarg;
  25. extern int optind, opterr, optopt;
  26. unsigned int cpu;
  27. union {
  28. struct {
  29. int perf_bias:1;
  30. };
  31. int params;
  32. } params = {};
  33. int ret = 0;
  34. setlocale(LC_ALL, "");
  35. textdomain(PACKAGE);
  36. /* parameter parsing */
  37. while ((ret = getopt_long(argc, argv, "b", set_opts, NULL)) != -1) {
  38. switch (ret) {
  39. case 'b':
  40. if (params.perf_bias)
  41. print_wrong_arg_exit();
  42. params.perf_bias = 1;
  43. break;
  44. default:
  45. print_wrong_arg_exit();
  46. }
  47. };
  48. if (!params.params)
  49. params.params = 0x7;
  50. /* Default is: show output of CPU 0 only */
  51. if (bitmask_isallclear(cpus_chosen))
  52. bitmask_setbit(cpus_chosen, 0);
  53. /* Add more per cpu options here */
  54. if (!params.perf_bias)
  55. return ret;
  56. if (params.perf_bias) {
  57. if (!run_as_root) {
  58. params.perf_bias = 0;
  59. printf(_("Intel's performance bias setting needs root privileges\n"));
  60. } else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
  61. printf(_("System does not support Intel's performance"
  62. " bias setting\n"));
  63. params.perf_bias = 0;
  64. }
  65. }
  66. /* loop over CPUs */
  67. for (cpu = bitmask_first(cpus_chosen);
  68. cpu <= bitmask_last(cpus_chosen); cpu++) {
  69. if (!bitmask_isbitset(cpus_chosen, cpu))
  70. continue;
  71. printf(_("analyzing CPU %d:\n"), cpu);
  72. if (sysfs_is_cpu_online(cpu) != 1){
  73. printf(_(" *is offline\n"));
  74. continue;
  75. }
  76. if (params.perf_bias) {
  77. ret = msr_intel_get_perf_bias(cpu);
  78. if (ret < 0) {
  79. fprintf(stderr,
  80. _("Could not read perf-bias value[%d]\n"), ret);
  81. exit(EXIT_FAILURE);
  82. } else
  83. printf(_("perf-bias: %d\n"), ret);
  84. }
  85. }
  86. return 0;
  87. }