cpufreq_stats.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * drivers/cpufreq/cpufreq_stats.c
  3. *
  4. * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  5. * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/cpu.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/cputime.h>
  16. static DEFINE_SPINLOCK(cpufreq_stats_lock);
  17. struct cpufreq_stats {
  18. unsigned int total_trans;
  19. unsigned long long last_time;
  20. unsigned int max_state;
  21. unsigned int state_num;
  22. unsigned int last_index;
  23. u64 *time_in_state;
  24. unsigned int *freq_table;
  25. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  26. unsigned int *trans_table;
  27. #endif
  28. };
  29. static int cpufreq_stats_update(struct cpufreq_stats *stats)
  30. {
  31. unsigned long long cur_time = get_jiffies_64();
  32. spin_lock(&cpufreq_stats_lock);
  33. stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
  34. stats->last_time = cur_time;
  35. spin_unlock(&cpufreq_stats_lock);
  36. return 0;
  37. }
  38. static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
  39. {
  40. unsigned int count = stats->max_state;
  41. memset(stats->time_in_state, 0, count * sizeof(u64));
  42. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  43. memset(stats->trans_table, 0, count * count * sizeof(int));
  44. #endif
  45. stats->last_time = get_jiffies_64();
  46. stats->total_trans = 0;
  47. }
  48. static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  49. {
  50. return sprintf(buf, "%d\n", policy->stats->total_trans);
  51. }
  52. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  53. {
  54. struct cpufreq_stats *stats = policy->stats;
  55. ssize_t len = 0;
  56. int i;
  57. if (policy->fast_switch_enabled)
  58. return 0;
  59. cpufreq_stats_update(stats);
  60. for (i = 0; i < stats->state_num; i++) {
  61. len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
  62. (unsigned long long)
  63. jiffies_64_to_clock_t(stats->time_in_state[i]));
  64. }
  65. return len;
  66. }
  67. static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
  68. size_t count)
  69. {
  70. /* We don't care what is written to the attribute. */
  71. cpufreq_stats_clear_table(policy->stats);
  72. return count;
  73. }
  74. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  75. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  76. {
  77. struct cpufreq_stats *stats = policy->stats;
  78. ssize_t len = 0;
  79. int i, j;
  80. if (policy->fast_switch_enabled)
  81. return 0;
  82. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  83. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  84. for (i = 0; i < stats->state_num; i++) {
  85. if (len >= PAGE_SIZE)
  86. break;
  87. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  88. stats->freq_table[i]);
  89. }
  90. if (len >= PAGE_SIZE)
  91. return PAGE_SIZE;
  92. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  93. for (i = 0; i < stats->state_num; i++) {
  94. if (len >= PAGE_SIZE)
  95. break;
  96. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  97. stats->freq_table[i]);
  98. for (j = 0; j < stats->state_num; j++) {
  99. if (len >= PAGE_SIZE)
  100. break;
  101. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  102. stats->trans_table[i*stats->max_state+j]);
  103. }
  104. if (len >= PAGE_SIZE)
  105. break;
  106. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  107. }
  108. if (len >= PAGE_SIZE)
  109. return PAGE_SIZE;
  110. return len;
  111. }
  112. cpufreq_freq_attr_ro(trans_table);
  113. #endif
  114. cpufreq_freq_attr_ro(total_trans);
  115. cpufreq_freq_attr_ro(time_in_state);
  116. cpufreq_freq_attr_wo(reset);
  117. static struct attribute *default_attrs[] = {
  118. &total_trans.attr,
  119. &time_in_state.attr,
  120. &reset.attr,
  121. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  122. &trans_table.attr,
  123. #endif
  124. NULL
  125. };
  126. static struct attribute_group stats_attr_group = {
  127. .attrs = default_attrs,
  128. .name = "stats"
  129. };
  130. static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
  131. {
  132. int index;
  133. for (index = 0; index < stats->max_state; index++)
  134. if (stats->freq_table[index] == freq)
  135. return index;
  136. return -1;
  137. }
  138. void cpufreq_stats_free_table(struct cpufreq_policy *policy)
  139. {
  140. struct cpufreq_stats *stats = policy->stats;
  141. /* Already freed */
  142. if (!stats)
  143. return;
  144. pr_debug("%s: Free stats table\n", __func__);
  145. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  146. kfree(stats->time_in_state);
  147. kfree(stats);
  148. policy->stats = NULL;
  149. }
  150. void cpufreq_stats_create_table(struct cpufreq_policy *policy)
  151. {
  152. unsigned int i = 0, count = 0, ret = -ENOMEM;
  153. struct cpufreq_stats *stats;
  154. unsigned int alloc_size;
  155. struct cpufreq_frequency_table *pos, *table;
  156. /* We need cpufreq table for creating stats table */
  157. table = policy->freq_table;
  158. if (unlikely(!table))
  159. return;
  160. /* stats already initialized */
  161. if (policy->stats)
  162. return;
  163. stats = kzalloc(sizeof(*stats), GFP_KERNEL);
  164. if (!stats)
  165. return;
  166. /* Find total allocation size */
  167. cpufreq_for_each_valid_entry(pos, table)
  168. count++;
  169. alloc_size = count * sizeof(int) + count * sizeof(u64);
  170. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  171. alloc_size += count * count * sizeof(int);
  172. #endif
  173. /* Allocate memory for time_in_state/freq_table/trans_table in one go */
  174. stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  175. if (!stats->time_in_state)
  176. goto free_stat;
  177. stats->freq_table = (unsigned int *)(stats->time_in_state + count);
  178. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  179. stats->trans_table = stats->freq_table + count;
  180. #endif
  181. stats->max_state = count;
  182. /* Find valid-unique entries */
  183. cpufreq_for_each_valid_entry(pos, table)
  184. if (freq_table_get_index(stats, pos->frequency) == -1)
  185. stats->freq_table[i++] = pos->frequency;
  186. stats->state_num = i;
  187. stats->last_time = get_jiffies_64();
  188. stats->last_index = freq_table_get_index(stats, policy->cur);
  189. policy->stats = stats;
  190. ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
  191. if (!ret)
  192. return;
  193. /* We failed, release resources */
  194. policy->stats = NULL;
  195. kfree(stats->time_in_state);
  196. free_stat:
  197. kfree(stats);
  198. }
  199. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  200. unsigned int new_freq)
  201. {
  202. struct cpufreq_stats *stats = policy->stats;
  203. int old_index, new_index;
  204. if (!stats) {
  205. pr_debug("%s: No stats found\n", __func__);
  206. return;
  207. }
  208. old_index = stats->last_index;
  209. new_index = freq_table_get_index(stats, new_freq);
  210. /* We can't do stats->time_in_state[-1]= .. */
  211. if (old_index == -1 || new_index == -1 || old_index == new_index)
  212. return;
  213. cpufreq_stats_update(stats);
  214. stats->last_index = new_index;
  215. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  216. stats->trans_table[old_index * stats->max_state + new_index]++;
  217. #endif
  218. stats->total_trans++;
  219. }