cpufreq_stats.c 10 KB

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