cpufreq_stats.c 9.2 KB

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