cpufreq_stats.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. static DEFINE_SPINLOCK(cpufreq_stats_lock);
  16. struct cpufreq_stats {
  17. unsigned int total_trans;
  18. unsigned long long last_time;
  19. unsigned int max_state;
  20. unsigned int state_num;
  21. unsigned int last_index;
  22. u64 *time_in_state;
  23. unsigned int *freq_table;
  24. unsigned int *trans_table;
  25. };
  26. static void cpufreq_stats_update(struct cpufreq_stats *stats)
  27. {
  28. unsigned long long cur_time = get_jiffies_64();
  29. spin_lock(&cpufreq_stats_lock);
  30. stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
  31. stats->last_time = cur_time;
  32. spin_unlock(&cpufreq_stats_lock);
  33. }
  34. static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
  35. {
  36. unsigned int count = stats->max_state;
  37. memset(stats->time_in_state, 0, count * sizeof(u64));
  38. memset(stats->trans_table, 0, count * count * sizeof(int));
  39. stats->last_time = get_jiffies_64();
  40. stats->total_trans = 0;
  41. }
  42. static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  43. {
  44. return sprintf(buf, "%d\n", policy->stats->total_trans);
  45. }
  46. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  47. {
  48. struct cpufreq_stats *stats = policy->stats;
  49. ssize_t len = 0;
  50. int i;
  51. if (policy->fast_switch_enabled)
  52. return 0;
  53. cpufreq_stats_update(stats);
  54. for (i = 0; i < stats->state_num; i++) {
  55. len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
  56. (unsigned long long)
  57. jiffies_64_to_clock_t(stats->time_in_state[i]));
  58. }
  59. return len;
  60. }
  61. static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
  62. size_t count)
  63. {
  64. /* We don't care what is written to the attribute. */
  65. cpufreq_stats_clear_table(policy->stats);
  66. return count;
  67. }
  68. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  69. {
  70. struct cpufreq_stats *stats = policy->stats;
  71. ssize_t len = 0;
  72. int i, j;
  73. if (policy->fast_switch_enabled)
  74. return 0;
  75. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  76. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  77. for (i = 0; i < stats->state_num; i++) {
  78. if (len >= PAGE_SIZE)
  79. break;
  80. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  81. stats->freq_table[i]);
  82. }
  83. if (len >= PAGE_SIZE)
  84. return PAGE_SIZE;
  85. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  86. for (i = 0; i < stats->state_num; i++) {
  87. if (len >= PAGE_SIZE)
  88. break;
  89. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  90. stats->freq_table[i]);
  91. for (j = 0; j < stats->state_num; j++) {
  92. if (len >= PAGE_SIZE)
  93. break;
  94. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  95. stats->trans_table[i*stats->max_state+j]);
  96. }
  97. if (len >= PAGE_SIZE)
  98. break;
  99. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  100. }
  101. if (len >= PAGE_SIZE) {
  102. pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
  103. return -EFBIG;
  104. }
  105. return len;
  106. }
  107. cpufreq_freq_attr_ro(trans_table);
  108. cpufreq_freq_attr_ro(total_trans);
  109. cpufreq_freq_attr_ro(time_in_state);
  110. cpufreq_freq_attr_wo(reset);
  111. static struct attribute *default_attrs[] = {
  112. &total_trans.attr,
  113. &time_in_state.attr,
  114. &reset.attr,
  115. &trans_table.attr,
  116. NULL
  117. };
  118. static const struct attribute_group stats_attr_group = {
  119. .attrs = default_attrs,
  120. .name = "stats"
  121. };
  122. static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
  123. {
  124. int index;
  125. for (index = 0; index < stats->max_state; index++)
  126. if (stats->freq_table[index] == freq)
  127. return index;
  128. return -1;
  129. }
  130. void cpufreq_stats_free_table(struct cpufreq_policy *policy)
  131. {
  132. struct cpufreq_stats *stats = policy->stats;
  133. /* Already freed */
  134. if (!stats)
  135. return;
  136. pr_debug("%s: Free stats table\n", __func__);
  137. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  138. kfree(stats->time_in_state);
  139. kfree(stats);
  140. policy->stats = NULL;
  141. }
  142. void cpufreq_stats_create_table(struct cpufreq_policy *policy)
  143. {
  144. unsigned int i = 0, count = 0, ret = -ENOMEM;
  145. struct cpufreq_stats *stats;
  146. unsigned int alloc_size;
  147. struct cpufreq_frequency_table *pos;
  148. count = cpufreq_table_count_valid_entries(policy);
  149. if (!count)
  150. return;
  151. /* stats already initialized */
  152. if (policy->stats)
  153. return;
  154. stats = kzalloc(sizeof(*stats), GFP_KERNEL);
  155. if (!stats)
  156. return;
  157. alloc_size = count * sizeof(int) + count * sizeof(u64);
  158. alloc_size += count * count * sizeof(int);
  159. /* Allocate memory for time_in_state/freq_table/trans_table in one go */
  160. stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  161. if (!stats->time_in_state)
  162. goto free_stat;
  163. stats->freq_table = (unsigned int *)(stats->time_in_state + count);
  164. stats->trans_table = stats->freq_table + count;
  165. stats->max_state = count;
  166. /* Find valid-unique entries */
  167. cpufreq_for_each_valid_entry(pos, policy->freq_table)
  168. if (freq_table_get_index(stats, pos->frequency) == -1)
  169. stats->freq_table[i++] = pos->frequency;
  170. stats->state_num = i;
  171. stats->last_time = get_jiffies_64();
  172. stats->last_index = freq_table_get_index(stats, policy->cur);
  173. policy->stats = stats;
  174. ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
  175. if (!ret)
  176. return;
  177. /* We failed, release resources */
  178. policy->stats = NULL;
  179. kfree(stats->time_in_state);
  180. free_stat:
  181. kfree(stats);
  182. }
  183. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  184. unsigned int new_freq)
  185. {
  186. struct cpufreq_stats *stats = policy->stats;
  187. int old_index, new_index;
  188. if (!stats) {
  189. pr_debug("%s: No stats found\n", __func__);
  190. return;
  191. }
  192. old_index = stats->last_index;
  193. new_index = freq_table_get_index(stats, new_freq);
  194. /* We can't do stats->time_in_state[-1]= .. */
  195. if (old_index == -1 || new_index == -1 || old_index == new_index)
  196. return;
  197. cpufreq_stats_update(stats);
  198. stats->last_index = new_index;
  199. stats->trans_table[old_index * stats->max_state + new_index]++;
  200. stats->total_trans++;
  201. }