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