cpufreq_stats.c 11 KB

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