cpufreq_stats.c 9.5 KB

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