cpufreq_stats.c 10 KB

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