2 * drivers/cpufreq/cpufreq_stats.c
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
5 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
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.
12 #include <linux/cpu.h>
13 #include <linux/cpufreq.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/cputime.h>
18 static spinlock_t cpufreq_stats_lock;
20 struct cpufreq_stats {
21 unsigned int total_trans;
22 unsigned long long last_time;
23 unsigned int max_state;
24 unsigned int state_num;
25 unsigned int last_index;
27 unsigned int *freq_table;
28 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
29 unsigned int *trans_table;
33 static int cpufreq_stats_update(struct cpufreq_stats *stats)
35 unsigned long long cur_time = get_jiffies_64();
37 spin_lock(&cpufreq_stats_lock);
38 if (stats->time_in_state)
39 stats->time_in_state[stats->last_index] +=
40 cur_time - stats->last_time;
41 stats->last_time = cur_time;
42 spin_unlock(&cpufreq_stats_lock);
46 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
48 return sprintf(buf, "%d\n", policy->stats->total_trans);
51 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
53 struct cpufreq_stats *stats = policy->stats;
57 cpufreq_stats_update(stats);
58 for (i = 0; i < stats->state_num; i++) {
59 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
61 jiffies_64_to_clock_t(stats->time_in_state[i]));
66 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
67 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
69 struct cpufreq_stats *stats = policy->stats;
73 cpufreq_stats_update(stats);
74 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
75 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
76 for (i = 0; i < stats->state_num; i++) {
79 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
80 stats->freq_table[i]);
85 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
87 for (i = 0; i < stats->state_num; i++) {
91 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
92 stats->freq_table[i]);
94 for (j = 0; j < stats->state_num; j++) {
97 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
98 stats->trans_table[i*stats->max_state+j]);
100 if (len >= PAGE_SIZE)
102 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
104 if (len >= PAGE_SIZE)
108 cpufreq_freq_attr_ro(trans_table);
111 cpufreq_freq_attr_ro(total_trans);
112 cpufreq_freq_attr_ro(time_in_state);
114 static struct attribute *default_attrs[] = {
117 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
122 static struct attribute_group stats_attr_group = {
123 .attrs = default_attrs,
127 static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
130 for (index = 0; index < stats->max_state; index++)
131 if (stats->freq_table[index] == freq)
136 static void __cpufreq_stats_free_table(struct cpufreq_policy *policy)
138 struct cpufreq_stats *stats = policy->stats;
144 pr_debug("%s: Free stats table\n", __func__);
146 sysfs_remove_group(&policy->kobj, &stats_attr_group);
147 kfree(stats->time_in_state);
149 policy->stats = NULL;
152 static void cpufreq_stats_free_table(unsigned int cpu)
154 struct cpufreq_policy *policy;
156 policy = cpufreq_cpu_get(cpu);
160 __cpufreq_stats_free_table(policy);
162 cpufreq_cpu_put(policy);
165 static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
167 unsigned int i, count = 0, ret = 0;
168 struct cpufreq_stats *stats;
169 unsigned int alloc_size;
170 unsigned int cpu = policy->cpu;
171 struct cpufreq_frequency_table *pos, *table;
173 table = cpufreq_frequency_get_table(cpu);
174 if (unlikely(!table))
177 /* stats already initialized */
181 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
185 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
189 policy->stats = stats;
191 cpufreq_for_each_valid_entry(pos, table)
194 alloc_size = count * sizeof(int) + count * sizeof(u64);
196 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
197 alloc_size += count * count * sizeof(int);
199 stats->max_state = count;
200 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
201 if (!stats->time_in_state) {
205 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
207 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
208 stats->trans_table = stats->freq_table + count;
211 cpufreq_for_each_valid_entry(pos, table)
212 if (freq_table_get_index(stats, pos->frequency) == -1)
213 stats->freq_table[i++] = pos->frequency;
214 stats->state_num = i;
215 spin_lock(&cpufreq_stats_lock);
216 stats->last_time = get_jiffies_64();
217 stats->last_index = freq_table_get_index(stats, policy->cur);
218 spin_unlock(&cpufreq_stats_lock);
221 sysfs_remove_group(&policy->kobj, &stats_attr_group);
224 policy->stats = NULL;
228 static void cpufreq_stats_create_table(unsigned int cpu)
230 struct cpufreq_policy *policy;
233 * "likely(!policy)" because normally cpufreq_stats will be registered
234 * before cpufreq driver
236 policy = cpufreq_cpu_get(cpu);
240 __cpufreq_stats_create_table(policy);
242 cpufreq_cpu_put(policy);
245 static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
246 unsigned long val, void *data)
249 struct cpufreq_policy *policy = data;
251 if (val == CPUFREQ_CREATE_POLICY)
252 ret = __cpufreq_stats_create_table(policy);
253 else if (val == CPUFREQ_REMOVE_POLICY)
254 __cpufreq_stats_free_table(policy);
259 static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
260 unsigned long val, void *data)
262 struct cpufreq_freqs *freq = data;
263 struct cpufreq_policy *policy = cpufreq_cpu_get(freq->cpu);
264 struct cpufreq_stats *stats;
265 int old_index, new_index;
268 pr_err("%s: No policy found\n", __func__);
272 if (val != CPUFREQ_POSTCHANGE)
275 if (!policy->stats) {
276 pr_debug("%s: No stats found\n", __func__);
280 stats = policy->stats;
282 old_index = stats->last_index;
283 new_index = freq_table_get_index(stats, freq->new);
285 /* We can't do stats->time_in_state[-1]= .. */
286 if (old_index == -1 || new_index == -1)
289 cpufreq_stats_update(stats);
291 if (old_index == new_index)
294 spin_lock(&cpufreq_stats_lock);
295 stats->last_index = new_index;
296 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
297 stats->trans_table[old_index * stats->max_state + new_index]++;
299 stats->total_trans++;
300 spin_unlock(&cpufreq_stats_lock);
303 cpufreq_cpu_put(policy);
307 static struct notifier_block notifier_policy_block = {
308 .notifier_call = cpufreq_stat_notifier_policy
311 static struct notifier_block notifier_trans_block = {
312 .notifier_call = cpufreq_stat_notifier_trans
315 static int __init cpufreq_stats_init(void)
320 spin_lock_init(&cpufreq_stats_lock);
321 ret = cpufreq_register_notifier(¬ifier_policy_block,
322 CPUFREQ_POLICY_NOTIFIER);
326 for_each_online_cpu(cpu)
327 cpufreq_stats_create_table(cpu);
329 ret = cpufreq_register_notifier(¬ifier_trans_block,
330 CPUFREQ_TRANSITION_NOTIFIER);
332 cpufreq_unregister_notifier(¬ifier_policy_block,
333 CPUFREQ_POLICY_NOTIFIER);
334 for_each_online_cpu(cpu)
335 cpufreq_stats_free_table(cpu);
341 static void __exit cpufreq_stats_exit(void)
345 cpufreq_unregister_notifier(¬ifier_policy_block,
346 CPUFREQ_POLICY_NOTIFIER);
347 cpufreq_unregister_notifier(¬ifier_trans_block,
348 CPUFREQ_TRANSITION_NOTIFIER);
349 for_each_online_cpu(cpu)
350 cpufreq_stats_free_table(cpu);
353 MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
354 MODULE_DESCRIPTION("Export cpufreq stats via sysfs");
355 MODULE_LICENSE("GPL");
357 module_init(cpufreq_stats_init);
358 module_exit(cpufreq_stats_exit);