cpufreq_stats.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. static void __cpufreq_stats_free_table(struct cpufreq_policy *policy)
  136. {
  137. struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
  138. if (!stat)
  139. return;
  140. pr_debug("%s: Free stat table\n", __func__);
  141. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  142. kfree(stat->time_in_state);
  143. kfree(stat);
  144. per_cpu(cpufreq_stats_table, policy->cpu) = NULL;
  145. }
  146. static void cpufreq_stats_free_table(unsigned int cpu)
  147. {
  148. struct cpufreq_policy *policy;
  149. policy = cpufreq_cpu_get(cpu);
  150. if (!policy)
  151. return;
  152. if (cpufreq_frequency_get_table(policy->cpu))
  153. __cpufreq_stats_free_table(policy);
  154. cpufreq_cpu_put(policy);
  155. }
  156. static int __cpufreq_stats_create_table(struct cpufreq_policy *policy,
  157. struct cpufreq_frequency_table *table)
  158. {
  159. unsigned int i, j, count = 0, ret = 0;
  160. struct cpufreq_stats *stat;
  161. struct cpufreq_policy *current_policy;
  162. unsigned int alloc_size;
  163. unsigned int cpu = policy->cpu;
  164. if (per_cpu(cpufreq_stats_table, cpu))
  165. return -EBUSY;
  166. stat = kzalloc(sizeof(*stat), GFP_KERNEL);
  167. if ((stat) == NULL)
  168. return -ENOMEM;
  169. current_policy = cpufreq_cpu_get(cpu);
  170. if (current_policy == NULL) {
  171. ret = -EINVAL;
  172. goto error_get_fail;
  173. }
  174. ret = sysfs_create_group(&current_policy->kobj, &stats_attr_group);
  175. if (ret)
  176. goto error_out;
  177. stat->cpu = cpu;
  178. per_cpu(cpufreq_stats_table, cpu) = stat;
  179. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  180. unsigned int freq = table[i].frequency;
  181. if (freq == CPUFREQ_ENTRY_INVALID)
  182. continue;
  183. count++;
  184. }
  185. alloc_size = count * sizeof(int) + count * sizeof(u64);
  186. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  187. alloc_size += count * count * sizeof(int);
  188. #endif
  189. stat->max_state = count;
  190. stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
  191. if (!stat->time_in_state) {
  192. ret = -ENOMEM;
  193. goto error_out;
  194. }
  195. stat->freq_table = (unsigned int *)(stat->time_in_state + count);
  196. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  197. stat->trans_table = stat->freq_table + count;
  198. #endif
  199. j = 0;
  200. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  201. unsigned int freq = table[i].frequency;
  202. if (freq == CPUFREQ_ENTRY_INVALID)
  203. continue;
  204. if (freq_table_get_index(stat, freq) == -1)
  205. stat->freq_table[j++] = freq;
  206. }
  207. stat->state_num = j;
  208. spin_lock(&cpufreq_stats_lock);
  209. stat->last_time = get_jiffies_64();
  210. stat->last_index = freq_table_get_index(stat, policy->cur);
  211. spin_unlock(&cpufreq_stats_lock);
  212. cpufreq_cpu_put(current_policy);
  213. return 0;
  214. error_out:
  215. cpufreq_cpu_put(current_policy);
  216. error_get_fail:
  217. kfree(stat);
  218. per_cpu(cpufreq_stats_table, cpu) = NULL;
  219. return ret;
  220. }
  221. static void cpufreq_stats_create_table(unsigned int cpu)
  222. {
  223. struct cpufreq_policy *policy;
  224. struct cpufreq_frequency_table *table;
  225. /*
  226. * "likely(!policy)" because normally cpufreq_stats will be registered
  227. * before cpufreq driver
  228. */
  229. policy = cpufreq_cpu_get(cpu);
  230. if (likely(!policy))
  231. return;
  232. table = cpufreq_frequency_get_table(policy->cpu);
  233. if (likely(table))
  234. __cpufreq_stats_create_table(policy, table);
  235. cpufreq_cpu_put(policy);
  236. }
  237. static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
  238. {
  239. struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
  240. policy->last_cpu);
  241. pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
  242. policy->cpu, policy->last_cpu);
  243. per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
  244. policy->last_cpu);
  245. per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
  246. stat->cpu = policy->cpu;
  247. }
  248. static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
  249. unsigned long val, void *data)
  250. {
  251. int ret = 0;
  252. struct cpufreq_policy *policy = data;
  253. struct cpufreq_frequency_table *table;
  254. unsigned int cpu = policy->cpu;
  255. if (val == CPUFREQ_UPDATE_POLICY_CPU) {
  256. cpufreq_stats_update_policy_cpu(policy);
  257. return 0;
  258. }
  259. table = cpufreq_frequency_get_table(cpu);
  260. if (!table)
  261. return 0;
  262. if (val == CPUFREQ_CREATE_POLICY)
  263. ret = __cpufreq_stats_create_table(policy, table);
  264. else if (val == CPUFREQ_REMOVE_POLICY)
  265. __cpufreq_stats_free_table(policy);
  266. return ret;
  267. }
  268. static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
  269. unsigned long val, void *data)
  270. {
  271. struct cpufreq_freqs *freq = data;
  272. struct cpufreq_stats *stat;
  273. int old_index, new_index;
  274. if (val != CPUFREQ_POSTCHANGE)
  275. return 0;
  276. stat = per_cpu(cpufreq_stats_table, freq->cpu);
  277. if (!stat)
  278. return 0;
  279. old_index = stat->last_index;
  280. new_index = freq_table_get_index(stat, freq->new);
  281. /* We can't do stat->time_in_state[-1]= .. */
  282. if (old_index == -1 || new_index == -1)
  283. return 0;
  284. cpufreq_stats_update(freq->cpu);
  285. if (old_index == new_index)
  286. return 0;
  287. spin_lock(&cpufreq_stats_lock);
  288. stat->last_index = new_index;
  289. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  290. stat->trans_table[old_index * stat->max_state + new_index]++;
  291. #endif
  292. stat->total_trans++;
  293. spin_unlock(&cpufreq_stats_lock);
  294. return 0;
  295. }
  296. static struct notifier_block notifier_policy_block = {
  297. .notifier_call = cpufreq_stat_notifier_policy
  298. };
  299. static struct notifier_block notifier_trans_block = {
  300. .notifier_call = cpufreq_stat_notifier_trans
  301. };
  302. static int __init cpufreq_stats_init(void)
  303. {
  304. int ret;
  305. unsigned int cpu;
  306. spin_lock_init(&cpufreq_stats_lock);
  307. ret = cpufreq_register_notifier(&notifier_policy_block,
  308. CPUFREQ_POLICY_NOTIFIER);
  309. if (ret)
  310. return ret;
  311. for_each_online_cpu(cpu)
  312. cpufreq_stats_create_table(cpu);
  313. ret = cpufreq_register_notifier(&notifier_trans_block,
  314. CPUFREQ_TRANSITION_NOTIFIER);
  315. if (ret) {
  316. cpufreq_unregister_notifier(&notifier_policy_block,
  317. CPUFREQ_POLICY_NOTIFIER);
  318. for_each_online_cpu(cpu)
  319. cpufreq_stats_free_table(cpu);
  320. return ret;
  321. }
  322. return 0;
  323. }
  324. static void __exit cpufreq_stats_exit(void)
  325. {
  326. unsigned int cpu;
  327. cpufreq_unregister_notifier(&notifier_policy_block,
  328. CPUFREQ_POLICY_NOTIFIER);
  329. cpufreq_unregister_notifier(&notifier_trans_block,
  330. CPUFREQ_TRANSITION_NOTIFIER);
  331. for_each_online_cpu(cpu)
  332. cpufreq_stats_free_table(cpu);
  333. }
  334. MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
  335. MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
  336. "through sysfs filesystem");
  337. MODULE_LICENSE("GPL");
  338. module_init(cpufreq_stats_init);
  339. module_exit(cpufreq_stats_exit);