cpufreq_stats.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/cpu.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/module.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/percpu.h>
  19. #include <linux/kobject.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/notifier.h>
  22. #include <asm/cputime.h>
  23. static spinlock_t cpufreq_stats_lock;
  24. #define CPUFREQ_STATDEVICE_ATTR(_name, _mode, _show) \
  25. static struct freq_attr _attr_##_name = {\
  26. .attr = {.name = __stringify(_name), .mode = _mode, }, \
  27. .show = _show,\
  28. };
  29. struct cpufreq_stats {
  30. unsigned int cpu;
  31. unsigned int total_trans;
  32. unsigned long long last_time;
  33. unsigned int max_state;
  34. unsigned int state_num;
  35. unsigned int last_index;
  36. cputime64_t *time_in_state;
  37. unsigned int *freq_table;
  38. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  39. unsigned int *trans_table;
  40. #endif
  41. };
  42. static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
  43. struct cpufreq_stats_attribute {
  44. struct attribute attr;
  45. ssize_t(*show) (struct cpufreq_stats *, char *);
  46. };
  47. static int cpufreq_stats_update(unsigned int cpu)
  48. {
  49. struct cpufreq_stats *stat;
  50. unsigned long long cur_time;
  51. cur_time = get_jiffies_64();
  52. spin_lock(&cpufreq_stats_lock);
  53. stat = per_cpu(cpufreq_stats_table, cpu);
  54. if (stat->time_in_state)
  55. stat->time_in_state[stat->last_index] +=
  56. cur_time - stat->last_time;
  57. stat->last_time = cur_time;
  58. spin_unlock(&cpufreq_stats_lock);
  59. return 0;
  60. }
  61. static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
  62. {
  63. struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
  64. if (!stat)
  65. return 0;
  66. return sprintf(buf, "%d\n",
  67. per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
  68. }
  69. static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
  70. {
  71. ssize_t len = 0;
  72. int i;
  73. struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
  74. if (!stat)
  75. return 0;
  76. cpufreq_stats_update(stat->cpu);
  77. for (i = 0; i < stat->state_num; i++) {
  78. len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
  79. (unsigned long long)
  80. cputime64_to_clock_t(stat->time_in_state[i]));
  81. }
  82. return len;
  83. }
  84. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  85. static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
  86. {
  87. ssize_t len = 0;
  88. int i, j;
  89. struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
  90. if (!stat)
  91. return 0;
  92. cpufreq_stats_update(stat->cpu);
  93. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  94. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  95. for (i = 0; i < stat->state_num; i++) {
  96. if (len >= PAGE_SIZE)
  97. break;
  98. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  99. stat->freq_table[i]);
  100. }
  101. if (len >= PAGE_SIZE)
  102. return PAGE_SIZE;
  103. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  104. for (i = 0; i < stat->state_num; i++) {
  105. if (len >= PAGE_SIZE)
  106. break;
  107. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  108. stat->freq_table[i]);
  109. for (j = 0; j < stat->state_num; j++) {
  110. if (len >= PAGE_SIZE)
  111. break;
  112. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  113. stat->trans_table[i*stat->max_state+j]);
  114. }
  115. if (len >= PAGE_SIZE)
  116. break;
  117. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  118. }
  119. if (len >= PAGE_SIZE)
  120. return PAGE_SIZE;
  121. return len;
  122. }
  123. CPUFREQ_STATDEVICE_ATTR(trans_table, 0444, show_trans_table);
  124. #endif
  125. CPUFREQ_STATDEVICE_ATTR(total_trans, 0444, show_total_trans);
  126. CPUFREQ_STATDEVICE_ATTR(time_in_state, 0444, show_time_in_state);
  127. static struct attribute *default_attrs[] = {
  128. &_attr_total_trans.attr,
  129. &_attr_time_in_state.attr,
  130. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  131. &_attr_trans_table.attr,
  132. #endif
  133. NULL
  134. };
  135. static struct attribute_group stats_attr_group = {
  136. .attrs = default_attrs,
  137. .name = "stats"
  138. };
  139. static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
  140. {
  141. int index;
  142. for (index = 0; index < stat->max_state; index++)
  143. if (stat->freq_table[index] == freq)
  144. return index;
  145. return -1;
  146. }
  147. /* should be called late in the CPU removal sequence so that the stats
  148. * memory is still available in case someone tries to use it.
  149. */
  150. static void cpufreq_stats_free_table(unsigned int cpu)
  151. {
  152. struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
  153. if (stat) {
  154. kfree(stat->time_in_state);
  155. kfree(stat);
  156. }
  157. per_cpu(cpufreq_stats_table, cpu) = NULL;
  158. }
  159. /* must be called early in the CPU removal sequence (before
  160. * cpufreq_remove_dev) so that policy is still valid.
  161. */
  162. static void cpufreq_stats_free_sysfs(unsigned int cpu)
  163. {
  164. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  165. if (policy && policy->cpu == cpu)
  166. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  167. if (policy)
  168. cpufreq_cpu_put(policy);
  169. }
  170. static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
  171. struct cpufreq_frequency_table *table)
  172. {
  173. unsigned int i, j, count = 0, ret = 0;
  174. struct cpufreq_stats *stat;
  175. struct cpufreq_policy *data;
  176. unsigned int alloc_size;
  177. unsigned int cpu = policy->cpu;
  178. if (per_cpu(cpufreq_stats_table, cpu))
  179. return -EBUSY;
  180. stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
  181. if ((stat) == NULL)
  182. return -ENOMEM;
  183. data = cpufreq_cpu_get(cpu);
  184. if (data == NULL) {
  185. ret = -EINVAL;
  186. goto error_get_fail;
  187. }
  188. ret = sysfs_create_group(&data->kobj, &stats_attr_group);
  189. if (ret)
  190. goto error_out;
  191. stat->cpu = cpu;
  192. per_cpu(cpufreq_stats_table, cpu) = stat;
  193. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  194. unsigned int freq = table[i].frequency;
  195. if (freq == CPUFREQ_ENTRY_INVALID)
  196. continue;
  197. count++;
  198. }
  199. alloc_size = count * sizeof(int) + count * sizeof(cputime64_t);
  200. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  201. alloc_size += count * count * sizeof(int);
  202. #endif
  203. stat->max_state = count;
  204. stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  205. if (!stat->time_in_state) {
  206. ret = -ENOMEM;
  207. goto error_out;
  208. }
  209. stat->freq_table = (unsigned int *)(stat->time_in_state + count);
  210. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  211. stat->trans_table = stat->freq_table + count;
  212. #endif
  213. j = 0;
  214. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  215. unsigned int freq = table[i].frequency;
  216. if (freq == CPUFREQ_ENTRY_INVALID)
  217. continue;
  218. if (freq_table_get_index(stat, freq) == -1)
  219. stat->freq_table[j++] = freq;
  220. }
  221. stat->state_num = j;
  222. spin_lock(&cpufreq_stats_lock);
  223. stat->last_time = get_jiffies_64();
  224. stat->last_index = freq_table_get_index(stat, policy->cur);
  225. spin_unlock(&cpufreq_stats_lock);
  226. cpufreq_cpu_put(data);
  227. return 0;
  228. error_out:
  229. cpufreq_cpu_put(data);
  230. error_get_fail:
  231. kfree(stat);
  232. per_cpu(cpufreq_stats_table, cpu) = NULL;
  233. return ret;
  234. }
  235. static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
  236. unsigned long val, void *data)
  237. {
  238. int ret;
  239. struct cpufreq_policy *policy = data;
  240. struct cpufreq_frequency_table *table;
  241. unsigned int cpu = policy->cpu;
  242. if (val != CPUFREQ_NOTIFY)
  243. return 0;
  244. table = cpufreq_frequency_get_table(cpu);
  245. if (!table)
  246. return 0;
  247. ret = cpufreq_stats_create_table(policy, table);
  248. if (ret)
  249. return ret;
  250. return 0;
  251. }
  252. static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
  253. unsigned long val, void *data)
  254. {
  255. struct cpufreq_freqs *freq = data;
  256. struct cpufreq_stats *stat;
  257. int old_index, new_index;
  258. if (val != CPUFREQ_POSTCHANGE)
  259. return 0;
  260. stat = per_cpu(cpufreq_stats_table, freq->cpu);
  261. if (!stat)
  262. return 0;
  263. old_index = stat->last_index;
  264. new_index = freq_table_get_index(stat, freq->new);
  265. /* We can't do stat->time_in_state[-1]= .. */
  266. if (old_index == -1 || new_index == -1)
  267. return 0;
  268. cpufreq_stats_update(freq->cpu);
  269. if (old_index == new_index)
  270. return 0;
  271. spin_lock(&cpufreq_stats_lock);
  272. stat->last_index = new_index;
  273. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  274. stat->trans_table[old_index * stat->max_state + new_index]++;
  275. #endif
  276. stat->total_trans++;
  277. spin_unlock(&cpufreq_stats_lock);
  278. return 0;
  279. }
  280. static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
  281. unsigned long action,
  282. void *hcpu)
  283. {
  284. unsigned int cpu = (unsigned long)hcpu;
  285. switch (action) {
  286. case CPU_ONLINE:
  287. case CPU_ONLINE_FROZEN:
  288. cpufreq_update_policy(cpu);
  289. break;
  290. case CPU_DOWN_PREPARE:
  291. cpufreq_stats_free_sysfs(cpu);
  292. break;
  293. case CPU_DEAD:
  294. case CPU_DEAD_FROZEN:
  295. cpufreq_stats_free_table(cpu);
  296. break;
  297. }
  298. return NOTIFY_OK;
  299. }
  300. /* priority=1 so this will get called before cpufreq_remove_dev */
  301. static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
  302. .notifier_call = cpufreq_stat_cpu_callback,
  303. .priority = 1,
  304. };
  305. static struct notifier_block notifier_policy_block = {
  306. .notifier_call = cpufreq_stat_notifier_policy
  307. };
  308. static struct notifier_block notifier_trans_block = {
  309. .notifier_call = cpufreq_stat_notifier_trans
  310. };
  311. static int __init cpufreq_stats_init(void)
  312. {
  313. int ret;
  314. unsigned int cpu;
  315. spin_lock_init(&cpufreq_stats_lock);
  316. ret = cpufreq_register_notifier(&notifier_policy_block,
  317. CPUFREQ_POLICY_NOTIFIER);
  318. if (ret)
  319. return ret;
  320. ret = cpufreq_register_notifier(&notifier_trans_block,
  321. CPUFREQ_TRANSITION_NOTIFIER);
  322. if (ret) {
  323. cpufreq_unregister_notifier(&notifier_policy_block,
  324. CPUFREQ_POLICY_NOTIFIER);
  325. return ret;
  326. }
  327. register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
  328. for_each_online_cpu(cpu) {
  329. cpufreq_update_policy(cpu);
  330. }
  331. return 0;
  332. }
  333. static void __exit cpufreq_stats_exit(void)
  334. {
  335. unsigned int cpu;
  336. cpufreq_unregister_notifier(&notifier_policy_block,
  337. CPUFREQ_POLICY_NOTIFIER);
  338. cpufreq_unregister_notifier(&notifier_trans_block,
  339. CPUFREQ_TRANSITION_NOTIFIER);
  340. unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
  341. for_each_online_cpu(cpu) {
  342. cpufreq_stats_free_table(cpu);
  343. cpufreq_stats_free_sysfs(cpu);
  344. }
  345. }
  346. MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
  347. MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
  348. "through sysfs filesystem");
  349. MODULE_LICENSE("GPL");
  350. module_init(cpufreq_stats_init);
  351. module_exit(cpufreq_stats_exit);