cpufreq_stats.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/cpufreq/cpufreq_stats.c
  4. *
  5. * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  6. * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
  7. */
  8. #include <linux/cpu.h>
  9. #include <linux/cpufreq.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. struct cpufreq_stats {
  13. unsigned int total_trans;
  14. unsigned long long last_time;
  15. unsigned int max_state;
  16. unsigned int state_num;
  17. unsigned int last_index;
  18. u64 *time_in_state;
  19. unsigned int *freq_table;
  20. unsigned int *trans_table;
  21. /* Deferred reset */
  22. unsigned int reset_pending;
  23. unsigned long long reset_time;
  24. };
  25. static void cpufreq_stats_update(struct cpufreq_stats *stats,
  26. unsigned long long time)
  27. {
  28. unsigned long long cur_time = get_jiffies_64();
  29. stats->time_in_state[stats->last_index] += cur_time - time;
  30. stats->last_time = cur_time;
  31. }
  32. static void cpufreq_stats_reset_table(struct cpufreq_stats *stats)
  33. {
  34. unsigned int count = stats->max_state;
  35. memset(stats->time_in_state, 0, count * sizeof(u64));
  36. memset(stats->trans_table, 0, count * count * sizeof(int));
  37. stats->last_time = get_jiffies_64();
  38. stats->total_trans = 0;
  39. /* Adjust for the time elapsed since reset was requested */
  40. WRITE_ONCE(stats->reset_pending, 0);
  41. /*
  42. * Prevent the reset_time read from being reordered before the
  43. * reset_pending accesses in cpufreq_stats_record_transition().
  44. */
  45. smp_rmb();
  46. cpufreq_stats_update(stats, READ_ONCE(stats->reset_time));
  47. }
  48. static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  49. {
  50. struct cpufreq_stats *stats = policy->stats;
  51. if (READ_ONCE(stats->reset_pending))
  52. return sprintf(buf, "%d\n", 0);
  53. else
  54. return sprintf(buf, "%u\n", stats->total_trans);
  55. }
  56. cpufreq_freq_attr_ro(total_trans);
  57. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  58. {
  59. struct cpufreq_stats *stats = policy->stats;
  60. bool pending = READ_ONCE(stats->reset_pending);
  61. unsigned long long time;
  62. ssize_t len = 0;
  63. int i;
  64. for (i = 0; i < stats->state_num; i++) {
  65. if (pending) {
  66. if (i == stats->last_index) {
  67. /*
  68. * Prevent the reset_time read from occurring
  69. * before the reset_pending read above.
  70. */
  71. smp_rmb();
  72. time = get_jiffies_64() - READ_ONCE(stats->reset_time);
  73. } else {
  74. time = 0;
  75. }
  76. } else {
  77. time = stats->time_in_state[i];
  78. if (i == stats->last_index)
  79. time += get_jiffies_64() - stats->last_time;
  80. }
  81. len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
  82. jiffies_64_to_clock_t(time));
  83. }
  84. return len;
  85. }
  86. cpufreq_freq_attr_ro(time_in_state);
  87. /* We don't care what is written to the attribute */
  88. static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
  89. size_t count)
  90. {
  91. struct cpufreq_stats *stats = policy->stats;
  92. /*
  93. * Defer resetting of stats to cpufreq_stats_record_transition() to
  94. * avoid races.
  95. */
  96. WRITE_ONCE(stats->reset_time, get_jiffies_64());
  97. /*
  98. * The memory barrier below is to prevent the readers of reset_time from
  99. * seeing a stale or partially updated value.
  100. */
  101. smp_wmb();
  102. WRITE_ONCE(stats->reset_pending, 1);
  103. return count;
  104. }
  105. cpufreq_freq_attr_wo(reset);
  106. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  107. {
  108. struct cpufreq_stats *stats = policy->stats;
  109. bool pending = READ_ONCE(stats->reset_pending);
  110. ssize_t len = 0;
  111. int i, j, count;
  112. len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  113. len += scnprintf(buf + len, PAGE_SIZE - len, " : ");
  114. for (i = 0; i < stats->state_num; i++) {
  115. if (len >= PAGE_SIZE)
  116. break;
  117. len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ",
  118. stats->freq_table[i]);
  119. }
  120. if (len >= PAGE_SIZE)
  121. return PAGE_SIZE;
  122. len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
  123. for (i = 0; i < stats->state_num; i++) {
  124. if (len >= PAGE_SIZE)
  125. break;
  126. len += scnprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  127. stats->freq_table[i]);
  128. for (j = 0; j < stats->state_num; j++) {
  129. if (len >= PAGE_SIZE)
  130. break;
  131. if (pending)
  132. count = 0;
  133. else
  134. count = stats->trans_table[i * stats->max_state + j];
  135. len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ", count);
  136. }
  137. if (len >= PAGE_SIZE)
  138. break;
  139. len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
  140. }
  141. if (len >= PAGE_SIZE) {
  142. pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
  143. return -EFBIG;
  144. }
  145. return len;
  146. }
  147. cpufreq_freq_attr_ro(trans_table);
  148. static struct attribute *default_attrs[] = {
  149. &total_trans.attr,
  150. &time_in_state.attr,
  151. &reset.attr,
  152. &trans_table.attr,
  153. NULL
  154. };
  155. static const struct attribute_group stats_attr_group = {
  156. .attrs = default_attrs,
  157. .name = "stats"
  158. };
  159. static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
  160. {
  161. int index;
  162. for (index = 0; index < stats->max_state; index++)
  163. if (stats->freq_table[index] == freq)
  164. return index;
  165. return -1;
  166. }
  167. void cpufreq_stats_free_table(struct cpufreq_policy *policy)
  168. {
  169. struct cpufreq_stats *stats = policy->stats;
  170. /* Already freed */
  171. if (!stats)
  172. return;
  173. pr_debug("%s: Free stats table\n", __func__);
  174. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  175. kfree(stats->time_in_state);
  176. kfree(stats);
  177. policy->stats = NULL;
  178. }
  179. void cpufreq_stats_create_table(struct cpufreq_policy *policy)
  180. {
  181. unsigned int i = 0, count = 0, ret = -ENOMEM;
  182. struct cpufreq_stats *stats;
  183. unsigned int alloc_size;
  184. struct cpufreq_frequency_table *pos;
  185. count = cpufreq_table_count_valid_entries(policy);
  186. if (!count)
  187. return;
  188. /* stats already initialized */
  189. if (policy->stats)
  190. return;
  191. stats = kzalloc(sizeof(*stats), GFP_KERNEL);
  192. if (!stats)
  193. return;
  194. alloc_size = count * sizeof(int) + count * sizeof(u64);
  195. alloc_size += count * count * sizeof(int);
  196. /* Allocate memory for time_in_state/freq_table/trans_table in one go */
  197. stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  198. if (!stats->time_in_state)
  199. goto free_stat;
  200. stats->freq_table = (unsigned int *)(stats->time_in_state + count);
  201. stats->trans_table = stats->freq_table + count;
  202. stats->max_state = count;
  203. /* Find valid-unique entries */
  204. cpufreq_for_each_valid_entry(pos, policy->freq_table)
  205. if (freq_table_get_index(stats, pos->frequency) == -1)
  206. stats->freq_table[i++] = pos->frequency;
  207. stats->state_num = i;
  208. stats->last_time = get_jiffies_64();
  209. stats->last_index = freq_table_get_index(stats, policy->cur);
  210. policy->stats = stats;
  211. ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
  212. if (!ret)
  213. return;
  214. /* We failed, release resources */
  215. policy->stats = NULL;
  216. kfree(stats->time_in_state);
  217. free_stat:
  218. kfree(stats);
  219. }
  220. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  221. unsigned int new_freq)
  222. {
  223. struct cpufreq_stats *stats = policy->stats;
  224. int old_index, new_index;
  225. if (unlikely(!stats))
  226. return;
  227. if (unlikely(READ_ONCE(stats->reset_pending)))
  228. cpufreq_stats_reset_table(stats);
  229. old_index = stats->last_index;
  230. new_index = freq_table_get_index(stats, new_freq);
  231. /* We can't do stats->time_in_state[-1]= .. */
  232. if (unlikely(old_index == -1 || new_index == -1 || old_index == new_index))
  233. return;
  234. cpufreq_stats_update(stats, stats->last_time);
  235. stats->last_index = new_index;
  236. stats->trans_table[old_index * stats->max_state + new_index]++;
  237. stats->total_trans++;
  238. }