cpufreq_stats.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/config.h>
  12. #include <linux/kernel.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), .owner = THIS_MODULE, \
  27. .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 struct cpufreq_stats *cpufreq_stats_table[NR_CPUS];
  44. struct cpufreq_stats_attribute {
  45. struct attribute attr;
  46. ssize_t(*show) (struct cpufreq_stats *, char *);
  47. };
  48. static int
  49. cpufreq_stats_update (unsigned int cpu)
  50. {
  51. struct cpufreq_stats *stat;
  52. unsigned long long cur_time;
  53. cur_time = get_jiffies_64();
  54. spin_lock(&cpufreq_stats_lock);
  55. stat = cpufreq_stats_table[cpu];
  56. if (stat->time_in_state)
  57. stat->time_in_state[stat->last_index] =
  58. cputime64_add(stat->time_in_state[stat->last_index],
  59. cputime_sub(cur_time, stat->last_time));
  60. stat->last_time = cur_time;
  61. spin_unlock(&cpufreq_stats_lock);
  62. return 0;
  63. }
  64. static ssize_t
  65. show_total_trans(struct cpufreq_policy *policy, char *buf)
  66. {
  67. struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu];
  68. if(!stat)
  69. return 0;
  70. return sprintf(buf, "%d\n",
  71. cpufreq_stats_table[stat->cpu]->total_trans);
  72. }
  73. static ssize_t
  74. show_time_in_state(struct cpufreq_policy *policy, char *buf)
  75. {
  76. ssize_t len = 0;
  77. int i;
  78. struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu];
  79. if(!stat)
  80. return 0;
  81. cpufreq_stats_update(stat->cpu);
  82. for (i = 0; i < stat->state_num; i++) {
  83. len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
  84. (unsigned long long)cputime64_to_clock_t(stat->time_in_state[i]));
  85. }
  86. return len;
  87. }
  88. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  89. static ssize_t
  90. show_trans_table(struct cpufreq_policy *policy, char *buf)
  91. {
  92. ssize_t len = 0;
  93. int i, j;
  94. struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu];
  95. if(!stat)
  96. return 0;
  97. cpufreq_stats_update(stat->cpu);
  98. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  99. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  100. for (i = 0; i < stat->state_num; i++) {
  101. if (len >= PAGE_SIZE)
  102. break;
  103. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  104. stat->freq_table[i]);
  105. }
  106. if (len >= PAGE_SIZE)
  107. return len;
  108. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  109. for (i = 0; i < stat->state_num; i++) {
  110. if (len >= PAGE_SIZE)
  111. break;
  112. len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
  113. stat->freq_table[i]);
  114. for (j = 0; j < stat->state_num; j++) {
  115. if (len >= PAGE_SIZE)
  116. break;
  117. len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
  118. stat->trans_table[i*stat->max_state+j]);
  119. }
  120. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  121. }
  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
  141. 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. static void
  150. cpufreq_stats_free_table (unsigned int cpu)
  151. {
  152. struct cpufreq_stats *stat = cpufreq_stats_table[cpu];
  153. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  154. if (policy && policy->cpu == cpu)
  155. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  156. if (stat) {
  157. kfree(stat->time_in_state);
  158. kfree(stat);
  159. }
  160. cpufreq_stats_table[cpu] = NULL;
  161. if (policy)
  162. cpufreq_cpu_put(policy);
  163. }
  164. static int
  165. 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 *data;
  171. unsigned int alloc_size;
  172. unsigned int cpu = policy->cpu;
  173. if (cpufreq_stats_table[cpu])
  174. return -EBUSY;
  175. if ((stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL)) == NULL)
  176. return -ENOMEM;
  177. data = cpufreq_cpu_get(cpu);
  178. if (data == NULL) {
  179. ret = -EINVAL;
  180. goto error_get_fail;
  181. }
  182. if ((ret = sysfs_create_group(&data->kobj, &stats_attr_group)))
  183. goto error_out;
  184. stat->cpu = cpu;
  185. cpufreq_stats_table[cpu] = stat;
  186. for (i=0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  187. unsigned int freq = table[i].frequency;
  188. if (freq == CPUFREQ_ENTRY_INVALID)
  189. continue;
  190. count++;
  191. }
  192. alloc_size = count * sizeof(int) + count * sizeof(cputime64_t);
  193. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  194. alloc_size += count * count * sizeof(int);
  195. #endif
  196. stat->max_state = count;
  197. stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  198. if (!stat->time_in_state) {
  199. ret = -ENOMEM;
  200. goto error_out;
  201. }
  202. stat->freq_table = (unsigned int *)(stat->time_in_state + count);
  203. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  204. stat->trans_table = stat->freq_table + count;
  205. #endif
  206. j = 0;
  207. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  208. unsigned int freq = table[i].frequency;
  209. if (freq == CPUFREQ_ENTRY_INVALID)
  210. continue;
  211. if (freq_table_get_index(stat, freq) == -1)
  212. stat->freq_table[j++] = freq;
  213. }
  214. stat->state_num = j;
  215. spin_lock(&cpufreq_stats_lock);
  216. stat->last_time = get_jiffies_64();
  217. stat->last_index = freq_table_get_index(stat, policy->cur);
  218. spin_unlock(&cpufreq_stats_lock);
  219. cpufreq_cpu_put(data);
  220. return 0;
  221. error_out:
  222. cpufreq_cpu_put(data);
  223. error_get_fail:
  224. kfree(stat);
  225. cpufreq_stats_table[cpu] = NULL;
  226. return ret;
  227. }
  228. static int
  229. cpufreq_stat_notifier_policy (struct notifier_block *nb, unsigned long val,
  230. void *data)
  231. {
  232. int ret;
  233. struct cpufreq_policy *policy = data;
  234. struct cpufreq_frequency_table *table;
  235. unsigned int cpu = policy->cpu;
  236. if (val != CPUFREQ_NOTIFY)
  237. return 0;
  238. table = cpufreq_frequency_get_table(cpu);
  239. if (!table)
  240. return 0;
  241. if ((ret = cpufreq_stats_create_table(policy, table)))
  242. return ret;
  243. return 0;
  244. }
  245. static int
  246. cpufreq_stat_notifier_trans (struct notifier_block *nb, unsigned long val,
  247. void *data)
  248. {
  249. struct cpufreq_freqs *freq = data;
  250. struct cpufreq_stats *stat;
  251. int old_index, new_index;
  252. if (val != CPUFREQ_POSTCHANGE)
  253. return 0;
  254. stat = cpufreq_stats_table[freq->cpu];
  255. if (!stat)
  256. return 0;
  257. old_index = freq_table_get_index(stat, freq->old);
  258. new_index = freq_table_get_index(stat, freq->new);
  259. cpufreq_stats_update(freq->cpu);
  260. if (old_index == new_index)
  261. return 0;
  262. spin_lock(&cpufreq_stats_lock);
  263. stat->last_index = new_index;
  264. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  265. stat->trans_table[old_index * stat->max_state + new_index]++;
  266. #endif
  267. stat->total_trans++;
  268. spin_unlock(&cpufreq_stats_lock);
  269. return 0;
  270. }
  271. static int cpufreq_stat_cpu_callback(struct notifier_block *nfb,
  272. unsigned long action, void *hcpu)
  273. {
  274. unsigned int cpu = (unsigned long)hcpu;
  275. switch (action) {
  276. case CPU_ONLINE:
  277. cpufreq_update_policy(cpu);
  278. break;
  279. case CPU_DEAD:
  280. cpufreq_stats_free_table(cpu);
  281. break;
  282. }
  283. return NOTIFY_OK;
  284. }
  285. static struct notifier_block cpufreq_stat_cpu_notifier =
  286. {
  287. .notifier_call = cpufreq_stat_cpu_callback,
  288. };
  289. static struct notifier_block notifier_policy_block = {
  290. .notifier_call = cpufreq_stat_notifier_policy
  291. };
  292. static struct notifier_block notifier_trans_block = {
  293. .notifier_call = cpufreq_stat_notifier_trans
  294. };
  295. static int
  296. __init cpufreq_stats_init(void)
  297. {
  298. int ret;
  299. unsigned int cpu;
  300. spin_lock_init(&cpufreq_stats_lock);
  301. if ((ret = cpufreq_register_notifier(&notifier_policy_block,
  302. CPUFREQ_POLICY_NOTIFIER)))
  303. return ret;
  304. if ((ret = cpufreq_register_notifier(&notifier_trans_block,
  305. CPUFREQ_TRANSITION_NOTIFIER))) {
  306. cpufreq_unregister_notifier(&notifier_policy_block,
  307. CPUFREQ_POLICY_NOTIFIER);
  308. return ret;
  309. }
  310. register_cpu_notifier(&cpufreq_stat_cpu_notifier);
  311. lock_cpu_hotplug();
  312. for_each_online_cpu(cpu) {
  313. cpufreq_stat_cpu_callback(&cpufreq_stat_cpu_notifier, CPU_ONLINE,
  314. (void *)(long)cpu);
  315. }
  316. unlock_cpu_hotplug();
  317. return 0;
  318. }
  319. static void
  320. __exit cpufreq_stats_exit(void)
  321. {
  322. unsigned int cpu;
  323. cpufreq_unregister_notifier(&notifier_policy_block,
  324. CPUFREQ_POLICY_NOTIFIER);
  325. cpufreq_unregister_notifier(&notifier_trans_block,
  326. CPUFREQ_TRANSITION_NOTIFIER);
  327. unregister_cpu_notifier(&cpufreq_stat_cpu_notifier);
  328. lock_cpu_hotplug();
  329. for_each_online_cpu(cpu) {
  330. cpufreq_stat_cpu_callback(&cpufreq_stat_cpu_notifier, CPU_DEAD,
  331. (void *)(long)cpu);
  332. }
  333. unlock_cpu_hotplug();
  334. }
  335. MODULE_AUTHOR ("Zou Nan hai <nanhai.zou@intel.com>");
  336. MODULE_DESCRIPTION ("'cpufreq_stats' - A driver to export cpufreq stats through sysfs filesystem");
  337. MODULE_LICENSE ("GPL");
  338. module_init(cpufreq_stats_init);
  339. module_exit(cpufreq_stats_exit);