cpufreq_stats.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 spinlock_t 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 ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  39. {
  40. return sprintf(buf, "%d\n", policy->stats->total_trans);
  41. }
  42. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  43. {
  44. struct cpufreq_stats *stats = policy->stats;
  45. ssize_t len = 0;
  46. int i;
  47. cpufreq_stats_update(stats);
  48. for (i = 0; i < stats->state_num; i++) {
  49. len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
  50. (unsigned long long)
  51. jiffies_64_to_clock_t(stats->time_in_state[i]));
  52. }
  53. return len;
  54. }
  55. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  56. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  57. {
  58. struct cpufreq_stats *stats = policy->stats;
  59. ssize_t len = 0;
  60. int i, j;
  61. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  62. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  63. for (i = 0; i < stats->state_num; i++) {
  64. if (len >= PAGE_SIZE)
  65. break;
  66. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  67. stats->freq_table[i]);
  68. }
  69. if (len >= PAGE_SIZE)
  70. return PAGE_SIZE;
  71. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  72. for (i = 0; i < stats->state_num; i++) {
  73. if (len >= PAGE_SIZE)
  74. break;
  75. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  76. stats->freq_table[i]);
  77. for (j = 0; j < stats->state_num; j++) {
  78. if (len >= PAGE_SIZE)
  79. break;
  80. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  81. stats->trans_table[i*stats->max_state+j]);
  82. }
  83. if (len >= PAGE_SIZE)
  84. break;
  85. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  86. }
  87. if (len >= PAGE_SIZE)
  88. return PAGE_SIZE;
  89. return len;
  90. }
  91. cpufreq_freq_attr_ro(trans_table);
  92. #endif
  93. cpufreq_freq_attr_ro(total_trans);
  94. cpufreq_freq_attr_ro(time_in_state);
  95. static struct attribute *default_attrs[] = {
  96. &total_trans.attr,
  97. &time_in_state.attr,
  98. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  99. &trans_table.attr,
  100. #endif
  101. NULL
  102. };
  103. static struct attribute_group stats_attr_group = {
  104. .attrs = default_attrs,
  105. .name = "stats"
  106. };
  107. static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
  108. {
  109. int index;
  110. for (index = 0; index < stats->max_state; index++)
  111. if (stats->freq_table[index] == freq)
  112. return index;
  113. return -1;
  114. }
  115. static void __cpufreq_stats_free_table(struct cpufreq_policy *policy)
  116. {
  117. struct cpufreq_stats *stats = policy->stats;
  118. /* Already freed */
  119. if (!stats)
  120. return;
  121. pr_debug("%s: Free stats table\n", __func__);
  122. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  123. kfree(stats->time_in_state);
  124. kfree(stats);
  125. policy->stats = NULL;
  126. }
  127. static void cpufreq_stats_free_table(unsigned int cpu)
  128. {
  129. struct cpufreq_policy *policy;
  130. policy = cpufreq_cpu_get(cpu);
  131. if (!policy)
  132. return;
  133. __cpufreq_stats_free_table(policy);
  134. cpufreq_cpu_put(policy);
  135. }
  136. static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
  137. {
  138. unsigned int i = 0, count = 0, ret = -ENOMEM;
  139. struct cpufreq_stats *stats;
  140. unsigned int alloc_size;
  141. unsigned int cpu = policy->cpu;
  142. struct cpufreq_frequency_table *pos, *table;
  143. /* We need cpufreq table for creating stats table */
  144. table = cpufreq_frequency_get_table(cpu);
  145. if (unlikely(!table))
  146. return 0;
  147. /* stats already initialized */
  148. if (policy->stats)
  149. return -EEXIST;
  150. stats = kzalloc(sizeof(*stats), GFP_KERNEL);
  151. if (!stats)
  152. return -ENOMEM;
  153. /* Find total allocation size */
  154. cpufreq_for_each_valid_entry(pos, table)
  155. count++;
  156. alloc_size = count * sizeof(int) + count * sizeof(u64);
  157. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  158. alloc_size += count * count * sizeof(int);
  159. #endif
  160. /* Allocate memory for time_in_state/freq_table/trans_table in one go */
  161. stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  162. if (!stats->time_in_state)
  163. goto free_stat;
  164. stats->freq_table = (unsigned int *)(stats->time_in_state + count);
  165. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  166. stats->trans_table = stats->freq_table + count;
  167. #endif
  168. stats->max_state = count;
  169. /* Find valid-unique entries */
  170. cpufreq_for_each_valid_entry(pos, table)
  171. if (freq_table_get_index(stats, pos->frequency) == -1)
  172. stats->freq_table[i++] = pos->frequency;
  173. stats->state_num = i;
  174. stats->last_time = get_jiffies_64();
  175. stats->last_index = freq_table_get_index(stats, policy->cur);
  176. policy->stats = stats;
  177. ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
  178. if (!ret)
  179. return 0;
  180. /* We failed, release resources */
  181. policy->stats = NULL;
  182. kfree(stats->time_in_state);
  183. free_stat:
  184. kfree(stats);
  185. return ret;
  186. }
  187. static void cpufreq_stats_create_table(unsigned int cpu)
  188. {
  189. struct cpufreq_policy *policy;
  190. /*
  191. * "likely(!policy)" because normally cpufreq_stats will be registered
  192. * before cpufreq driver
  193. */
  194. policy = cpufreq_cpu_get(cpu);
  195. if (likely(!policy))
  196. return;
  197. __cpufreq_stats_create_table(policy);
  198. cpufreq_cpu_put(policy);
  199. }
  200. static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
  201. unsigned long val, void *data)
  202. {
  203. int ret = 0;
  204. struct cpufreq_policy *policy = data;
  205. if (val == CPUFREQ_CREATE_POLICY)
  206. ret = __cpufreq_stats_create_table(policy);
  207. else if (val == CPUFREQ_REMOVE_POLICY)
  208. __cpufreq_stats_free_table(policy);
  209. return ret;
  210. }
  211. static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
  212. unsigned long val, void *data)
  213. {
  214. struct cpufreq_freqs *freq = data;
  215. struct cpufreq_policy *policy = cpufreq_cpu_get(freq->cpu);
  216. struct cpufreq_stats *stats;
  217. int old_index, new_index;
  218. if (!policy) {
  219. pr_err("%s: No policy found\n", __func__);
  220. return 0;
  221. }
  222. if (val != CPUFREQ_POSTCHANGE)
  223. goto put_policy;
  224. if (!policy->stats) {
  225. pr_debug("%s: No stats found\n", __func__);
  226. goto put_policy;
  227. }
  228. stats = policy->stats;
  229. old_index = stats->last_index;
  230. new_index = freq_table_get_index(stats, freq->new);
  231. /* We can't do stats->time_in_state[-1]= .. */
  232. if (old_index == -1 || new_index == -1)
  233. goto put_policy;
  234. if (old_index == new_index)
  235. goto put_policy;
  236. cpufreq_stats_update(stats);
  237. stats->last_index = new_index;
  238. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  239. stats->trans_table[old_index * stats->max_state + new_index]++;
  240. #endif
  241. stats->total_trans++;
  242. put_policy:
  243. cpufreq_cpu_put(policy);
  244. return 0;
  245. }
  246. static struct notifier_block notifier_policy_block = {
  247. .notifier_call = cpufreq_stat_notifier_policy
  248. };
  249. static struct notifier_block notifier_trans_block = {
  250. .notifier_call = cpufreq_stat_notifier_trans
  251. };
  252. static int __init cpufreq_stats_init(void)
  253. {
  254. int ret;
  255. unsigned int cpu;
  256. spin_lock_init(&cpufreq_stats_lock);
  257. ret = cpufreq_register_notifier(&notifier_policy_block,
  258. CPUFREQ_POLICY_NOTIFIER);
  259. if (ret)
  260. return ret;
  261. for_each_online_cpu(cpu)
  262. cpufreq_stats_create_table(cpu);
  263. ret = cpufreq_register_notifier(&notifier_trans_block,
  264. CPUFREQ_TRANSITION_NOTIFIER);
  265. if (ret) {
  266. cpufreq_unregister_notifier(&notifier_policy_block,
  267. CPUFREQ_POLICY_NOTIFIER);
  268. for_each_online_cpu(cpu)
  269. cpufreq_stats_free_table(cpu);
  270. return ret;
  271. }
  272. return 0;
  273. }
  274. static void __exit cpufreq_stats_exit(void)
  275. {
  276. unsigned int cpu;
  277. cpufreq_unregister_notifier(&notifier_policy_block,
  278. CPUFREQ_POLICY_NOTIFIER);
  279. cpufreq_unregister_notifier(&notifier_trans_block,
  280. CPUFREQ_TRANSITION_NOTIFIER);
  281. for_each_online_cpu(cpu)
  282. cpufreq_stats_free_table(cpu);
  283. }
  284. MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
  285. MODULE_DESCRIPTION("Export cpufreq stats via sysfs");
  286. MODULE_LICENSE("GPL");
  287. module_init(cpufreq_stats_init);
  288. module_exit(cpufreq_stats_exit);