/drivers/cpufreq/cpufreq_stats.c

https://bitbucket.org/evzijst/gittest · C · 334 lines · 287 code · 37 blank · 10 comment · 43 complexity · bd5e21b26fe91f51c2b6f7f7065846ef MD5 · raw file

  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. static spinlock_t cpufreq_stats_lock;
  22. #define CPUFREQ_STATDEVICE_ATTR(_name,_mode,_show) \
  23. static struct freq_attr _attr_##_name = {\
  24. .attr = {.name = __stringify(_name), .owner = THIS_MODULE, \
  25. .mode = _mode, }, \
  26. .show = _show,\
  27. };
  28. static unsigned long
  29. delta_time(unsigned long old, unsigned long new)
  30. {
  31. return (old > new) ? (old - new): (new + ~old + 1);
  32. }
  33. struct cpufreq_stats {
  34. unsigned int cpu;
  35. unsigned int total_trans;
  36. unsigned long long last_time;
  37. unsigned int max_state;
  38. unsigned int state_num;
  39. unsigned int last_index;
  40. unsigned long long *time_in_state;
  41. unsigned int *freq_table;
  42. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  43. unsigned int *trans_table;
  44. #endif
  45. };
  46. static struct cpufreq_stats *cpufreq_stats_table[NR_CPUS];
  47. struct cpufreq_stats_attribute {
  48. struct attribute attr;
  49. ssize_t(*show) (struct cpufreq_stats *, char *);
  50. };
  51. static int
  52. cpufreq_stats_update (unsigned int cpu)
  53. {
  54. struct cpufreq_stats *stat;
  55. spin_lock(&cpufreq_stats_lock);
  56. stat = cpufreq_stats_table[cpu];
  57. if (stat->time_in_state)
  58. stat->time_in_state[stat->last_index] +=
  59. delta_time(stat->last_time, jiffies);
  60. stat->last_time = jiffies;
  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",
  84. stat->freq_table[i], 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. for (i = 0; i < stat->state_num; i++) {
  99. if (len >= PAGE_SIZE)
  100. break;
  101. len += snprintf(buf + len, PAGE_SIZE - len, "%9u:\t",
  102. stat->freq_table[i]);
  103. for (j = 0; j < stat->state_num; j++) {
  104. if (len >= PAGE_SIZE)
  105. break;
  106. len += snprintf(buf + len, PAGE_SIZE - len, "%u\t",
  107. stat->trans_table[i*stat->max_state+j]);
  108. }
  109. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  110. }
  111. return len;
  112. }
  113. CPUFREQ_STATDEVICE_ATTR(trans_table,0444,show_trans_table);
  114. #endif
  115. CPUFREQ_STATDEVICE_ATTR(total_trans,0444,show_total_trans);
  116. CPUFREQ_STATDEVICE_ATTR(time_in_state,0444,show_time_in_state);
  117. static struct attribute *default_attrs[] = {
  118. &_attr_total_trans.attr,
  119. &_attr_time_in_state.attr,
  120. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  121. &_attr_trans_table.attr,
  122. #endif
  123. NULL
  124. };
  125. static struct attribute_group stats_attr_group = {
  126. .attrs = default_attrs,
  127. .name = "stats"
  128. };
  129. static int
  130. freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
  131. {
  132. int index;
  133. for (index = 0; index < stat->max_state; index++)
  134. if (stat->freq_table[index] == freq)
  135. return index;
  136. return -1;
  137. }
  138. static void
  139. cpufreq_stats_free_table (unsigned int cpu)
  140. {
  141. struct cpufreq_stats *stat = cpufreq_stats_table[cpu];
  142. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  143. if (policy && policy->cpu == cpu)
  144. sysfs_remove_group(&policy->kobj, &stats_attr_group);
  145. if (stat) {
  146. kfree(stat->time_in_state);
  147. kfree(stat);
  148. }
  149. cpufreq_stats_table[cpu] = NULL;
  150. if (policy)
  151. cpufreq_cpu_put(policy);
  152. }
  153. static int
  154. cpufreq_stats_create_table (struct cpufreq_policy *policy,
  155. struct cpufreq_frequency_table *table)
  156. {
  157. unsigned int i, j, count = 0, ret = 0;
  158. struct cpufreq_stats *stat;
  159. struct cpufreq_policy *data;
  160. unsigned int alloc_size;
  161. unsigned int cpu = policy->cpu;
  162. if (cpufreq_stats_table[cpu])
  163. return -EBUSY;
  164. if ((stat = kmalloc(sizeof(struct cpufreq_stats), GFP_KERNEL)) == NULL)
  165. return -ENOMEM;
  166. memset(stat, 0, sizeof (struct cpufreq_stats));
  167. data = cpufreq_cpu_get(cpu);
  168. if ((ret = sysfs_create_group(&data->kobj, &stats_attr_group)))
  169. goto error_out;
  170. stat->cpu = cpu;
  171. cpufreq_stats_table[cpu] = stat;
  172. for (i=0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  173. unsigned int freq = table[i].frequency;
  174. if (freq == CPUFREQ_ENTRY_INVALID)
  175. continue;
  176. count++;
  177. }
  178. alloc_size = count * sizeof(int) + count * sizeof(long long);
  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 = kmalloc(alloc_size, GFP_KERNEL);
  184. if (!stat->time_in_state) {
  185. ret = -ENOMEM;
  186. goto error_out;
  187. }
  188. memset(stat->time_in_state, 0, alloc_size);
  189. stat->freq_table = (unsigned int *)(stat->time_in_state + count);
  190. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  191. stat->trans_table = stat->freq_table + count;
  192. #endif
  193. j = 0;
  194. for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
  195. unsigned int freq = table[i].frequency;
  196. if (freq == CPUFREQ_ENTRY_INVALID)
  197. continue;
  198. if (freq_table_get_index(stat, freq) == -1)
  199. stat->freq_table[j++] = freq;
  200. }
  201. stat->state_num = j;
  202. spin_lock(&cpufreq_stats_lock);
  203. stat->last_time = jiffies;
  204. stat->last_index = freq_table_get_index(stat, policy->cur);
  205. spin_unlock(&cpufreq_stats_lock);
  206. cpufreq_cpu_put(data);
  207. return 0;
  208. error_out:
  209. cpufreq_cpu_put(data);
  210. kfree(stat);
  211. cpufreq_stats_table[cpu] = NULL;
  212. return ret;
  213. }
  214. static int
  215. cpufreq_stat_notifier_policy (struct notifier_block *nb, unsigned long val,
  216. void *data)
  217. {
  218. int ret;
  219. struct cpufreq_policy *policy = data;
  220. struct cpufreq_frequency_table *table;
  221. unsigned int cpu = policy->cpu;
  222. if (val != CPUFREQ_NOTIFY)
  223. return 0;
  224. table = cpufreq_frequency_get_table(cpu);
  225. if (!table)
  226. return 0;
  227. if ((ret = cpufreq_stats_create_table(policy, table)))
  228. return ret;
  229. return 0;
  230. }
  231. static int
  232. cpufreq_stat_notifier_trans (struct notifier_block *nb, unsigned long val,
  233. void *data)
  234. {
  235. struct cpufreq_freqs *freq = data;
  236. struct cpufreq_stats *stat;
  237. int old_index, new_index;
  238. if (val != CPUFREQ_POSTCHANGE)
  239. return 0;
  240. stat = cpufreq_stats_table[freq->cpu];
  241. if (!stat)
  242. return 0;
  243. old_index = freq_table_get_index(stat, freq->old);
  244. new_index = freq_table_get_index(stat, freq->new);
  245. cpufreq_stats_update(freq->cpu);
  246. if (old_index == new_index)
  247. return 0;
  248. spin_lock(&cpufreq_stats_lock);
  249. stat->last_index = new_index;
  250. #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
  251. stat->trans_table[old_index * stat->max_state + new_index]++;
  252. #endif
  253. stat->total_trans++;
  254. spin_unlock(&cpufreq_stats_lock);
  255. return 0;
  256. }
  257. static struct notifier_block notifier_policy_block = {
  258. .notifier_call = cpufreq_stat_notifier_policy
  259. };
  260. static struct notifier_block notifier_trans_block = {
  261. .notifier_call = cpufreq_stat_notifier_trans
  262. };
  263. static int
  264. __init cpufreq_stats_init(void)
  265. {
  266. int ret;
  267. unsigned int cpu;
  268. spin_lock_init(&cpufreq_stats_lock);
  269. if ((ret = cpufreq_register_notifier(&notifier_policy_block,
  270. CPUFREQ_POLICY_NOTIFIER)))
  271. return ret;
  272. if ((ret = cpufreq_register_notifier(&notifier_trans_block,
  273. CPUFREQ_TRANSITION_NOTIFIER))) {
  274. cpufreq_unregister_notifier(&notifier_policy_block,
  275. CPUFREQ_POLICY_NOTIFIER);
  276. return ret;
  277. }
  278. for_each_cpu(cpu)
  279. cpufreq_update_policy(cpu);
  280. return 0;
  281. }
  282. static void
  283. __exit cpufreq_stats_exit(void)
  284. {
  285. unsigned int cpu;
  286. cpufreq_unregister_notifier(&notifier_policy_block,
  287. CPUFREQ_POLICY_NOTIFIER);
  288. cpufreq_unregister_notifier(&notifier_trans_block,
  289. CPUFREQ_TRANSITION_NOTIFIER);
  290. for_each_cpu(cpu)
  291. cpufreq_stats_free_table(cpu);
  292. }
  293. MODULE_AUTHOR ("Zou Nan hai <nanhai.zou@intel.com>");
  294. MODULE_DESCRIPTION ("'cpufreq_stats' - A driver to export cpufreq stats through sysfs filesystem");
  295. MODULE_LICENSE ("GPL");
  296. module_init(cpufreq_stats_init);
  297. module_exit(cpufreq_stats_exit);