cpuidle_sysfs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <limits.h>
  12. #include "helpers/sysfs.h"
  13. #include "helpers/helpers.h"
  14. #include "idle_monitor/cpupower-monitor.h"
  15. #define CPUIDLE_STATES_MAX 10
  16. static cstate_t cpuidle_cstates[CPUIDLE_STATES_MAX];
  17. struct cpuidle_monitor cpuidle_sysfs_monitor;
  18. static unsigned long long **previous_count;
  19. static unsigned long long **current_count;
  20. struct timespec start_time;
  21. static unsigned long long timediff;
  22. static int cpuidle_get_count_percent(unsigned int id, double *percent,
  23. unsigned int cpu)
  24. {
  25. unsigned long long statediff = current_count[cpu][id]
  26. - previous_count[cpu][id];
  27. dprint("%s: - diff: %llu - percent: %f (%u)\n",
  28. cpuidle_cstates[id].name, timediff, *percent, cpu);
  29. if (timediff == 0)
  30. *percent = 0.0;
  31. else
  32. *percent = ((100.0 * statediff) / timediff);
  33. dprint("%s: - timediff: %llu - statediff: %llu - percent: %f (%u)\n",
  34. cpuidle_cstates[id].name, timediff, statediff, *percent, cpu);
  35. return 0;
  36. }
  37. static int cpuidle_start(void)
  38. {
  39. int cpu, state;
  40. clock_gettime(CLOCK_REALTIME, &start_time);
  41. for (cpu = 0; cpu < cpu_count; cpu++) {
  42. for (state = 0; state < cpuidle_sysfs_monitor.hw_states_num;
  43. state++) {
  44. previous_count[cpu][state] =
  45. sysfs_get_idlestate_time(cpu, state);
  46. dprint("CPU %d - State: %d - Val: %llu\n",
  47. cpu, state, previous_count[cpu][state]);
  48. }
  49. };
  50. return 0;
  51. }
  52. static int cpuidle_stop(void)
  53. {
  54. int cpu, state;
  55. struct timespec end_time;
  56. clock_gettime(CLOCK_REALTIME, &end_time);
  57. timediff = timespec_diff_us(start_time, end_time);
  58. for (cpu = 0; cpu < cpu_count; cpu++) {
  59. for (state = 0; state < cpuidle_sysfs_monitor.hw_states_num;
  60. state++) {
  61. current_count[cpu][state] =
  62. sysfs_get_idlestate_time(cpu, state);
  63. dprint("CPU %d - State: %d - Val: %llu\n",
  64. cpu, state, previous_count[cpu][state]);
  65. }
  66. };
  67. return 0;
  68. }
  69. void fix_up_intel_idle_driver_name(char *tmp, int num)
  70. {
  71. /* fix up cpuidle name for intel idle driver */
  72. if (!strncmp(tmp, "NHM-", 4)) {
  73. switch(num) {
  74. case 1: strcpy(tmp, "C1");
  75. break;
  76. case 2: strcpy(tmp, "C3");
  77. break;
  78. case 3: strcpy(tmp, "C6");
  79. break;
  80. }
  81. } else if (!strncmp(tmp, "SNB-", 4)) {
  82. switch(num) {
  83. case 1: strcpy(tmp, "C1");
  84. break;
  85. case 2: strcpy(tmp, "C3");
  86. break;
  87. case 3: strcpy(tmp, "C6");
  88. break;
  89. case 4: strcpy(tmp, "C7");
  90. break;
  91. }
  92. } else if (!strncmp(tmp, "ATM-", 4)) {
  93. switch(num) {
  94. case 1: strcpy(tmp, "C1");
  95. break;
  96. case 2: strcpy(tmp, "C2");
  97. break;
  98. case 3: strcpy(tmp, "C4");
  99. break;
  100. case 4: strcpy(tmp, "C6");
  101. break;
  102. }
  103. }
  104. }
  105. static struct cpuidle_monitor* cpuidle_register(void)
  106. {
  107. int num;
  108. char *tmp;
  109. /* Assume idle state count is the same for all CPUs */
  110. cpuidle_sysfs_monitor.hw_states_num = sysfs_get_idlestate_count(0);
  111. if (cpuidle_sysfs_monitor.hw_states_num == 0)
  112. return NULL;
  113. for (num = 0; num < cpuidle_sysfs_monitor.hw_states_num; num ++) {
  114. tmp = sysfs_get_idlestate_name(0, num);
  115. if (tmp == NULL)
  116. continue;
  117. fix_up_intel_idle_driver_name(tmp, num);
  118. strncpy(cpuidle_cstates[num].name, tmp, CSTATE_NAME_LEN - 1);
  119. free(tmp);
  120. tmp = sysfs_get_idlestate_desc(0, num);
  121. if (tmp == NULL)
  122. continue;
  123. strncpy(cpuidle_cstates[num].desc, tmp, CSTATE_DESC_LEN - 1);
  124. free(tmp);
  125. cpuidle_cstates[num].range = RANGE_THREAD;
  126. cpuidle_cstates[num].id = num;
  127. cpuidle_cstates[num].get_count_percent = cpuidle_get_count_percent;
  128. };
  129. /* Free this at program termination */
  130. previous_count = malloc(sizeof (long long*) * cpu_count);
  131. current_count = malloc(sizeof (long long*) * cpu_count);
  132. for (num = 0; num < cpu_count; num++) {
  133. previous_count[num] = malloc (sizeof(long long) *
  134. cpuidle_sysfs_monitor.hw_states_num);
  135. current_count[num] = malloc (sizeof(long long) *
  136. cpuidle_sysfs_monitor.hw_states_num);
  137. }
  138. cpuidle_sysfs_monitor.name_len = strlen(cpuidle_sysfs_monitor.name);
  139. return &cpuidle_sysfs_monitor;
  140. }
  141. void cpuidle_unregister(void)
  142. {
  143. int num;
  144. for (num = 0; num < cpu_count; num++) {
  145. free(previous_count[num]);
  146. free(current_count[num]);
  147. }
  148. free(previous_count);
  149. free(current_count);
  150. }
  151. struct cpuidle_monitor cpuidle_sysfs_monitor = {
  152. .name = "Idle_Stats",
  153. .hw_states = cpuidle_cstates,
  154. .start = cpuidle_start,
  155. .stop = cpuidle_stop,
  156. .do_register = cpuidle_register,
  157. .unregister = cpuidle_unregister,
  158. .needs_root = 0,
  159. .overflow_s = UINT_MAX,
  160. };