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/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/cpu.h>
15 #include <linux/sysfs.h>
16 #include <linux/cpufreq.h>
17 #include <linux/module.h>
18 #include <linux/jiffies.h>
19 #include <linux/percpu.h>
20 #include <linux/kobject.h>
21 #include <linux/spinlock.h>
22 #include <linux/notifier.h>
23 #include <asm/cputime.h>
24 #include <asm/bL_switcher.h>
26 static spinlock_t cpufreq_stats_lock;
28 struct cpufreq_stats {
30 unsigned int total_trans;
31 unsigned long long last_time;
32 unsigned int max_state;
33 unsigned int state_num;
34 unsigned int last_index;
36 unsigned int *freq_table;
37 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
38 unsigned int *trans_table;
42 static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
44 struct cpufreq_stats_attribute {
45 struct attribute attr;
46 ssize_t(*show) (struct cpufreq_stats *, char *);
49 static int cpufreq_stats_update(unsigned int cpu)
51 struct cpufreq_stats *stat;
52 unsigned long long cur_time;
54 cur_time = get_jiffies_64();
55 spin_lock(&cpufreq_stats_lock);
56 stat = per_cpu(cpufreq_stats_table, cpu);
57 if (stat->time_in_state)
58 stat->time_in_state[stat->last_index] +=
59 cur_time - stat->last_time;
60 stat->last_time = cur_time;
61 spin_unlock(&cpufreq_stats_lock);
65 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
67 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
70 return sprintf(buf, "%d\n",
71 per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
74 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
78 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
81 cpufreq_stats_update(stat->cpu);
82 for (i = 0; i < stat->state_num; i++) {
83 len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
85 cputime64_to_clock_t(stat->time_in_state[i]));
90 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
91 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
96 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
99 cpufreq_stats_update(stat->cpu);
100 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
101 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
102 for (i = 0; i < stat->state_num; i++) {
103 if (len >= PAGE_SIZE)
105 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
106 stat->freq_table[i]);
108 if (len >= PAGE_SIZE)
111 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
113 for (i = 0; i < stat->state_num; i++) {
114 if (len >= PAGE_SIZE)
117 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
118 stat->freq_table[i]);
120 for (j = 0; j < stat->state_num; j++) {
121 if (len >= PAGE_SIZE)
123 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
124 stat->trans_table[i*stat->max_state+j]);
126 if (len >= PAGE_SIZE)
128 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
130 if (len >= PAGE_SIZE)
134 cpufreq_freq_attr_ro(trans_table);
137 cpufreq_freq_attr_ro(total_trans);
138 cpufreq_freq_attr_ro(time_in_state);
140 static struct attribute *default_attrs[] = {
143 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
148 static struct attribute_group stats_attr_group = {
149 .attrs = default_attrs,
153 static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
156 for (index = 0; index < stat->max_state; index++)
157 if (stat->freq_table[index] == freq)
162 /* should be called late in the CPU removal sequence so that the stats
163 * memory is still available in case someone tries to use it.
165 static void cpufreq_stats_free_table(unsigned int cpu)
167 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
170 pr_debug("%s: Free stat table\n", __func__);
171 kfree(stat->time_in_state);
173 per_cpu(cpufreq_stats_table, cpu) = NULL;
177 /* must be called early in the CPU removal sequence (before
178 * cpufreq_remove_dev) so that policy is still valid.
180 static void cpufreq_stats_free_sysfs(unsigned int cpu)
182 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
187 if (!cpufreq_frequency_get_table(cpu))
190 if (!policy_is_shared(policy)) {
191 pr_debug("%s: Free sysfs stat\n", __func__);
192 sysfs_remove_group(&policy->kobj, &stats_attr_group);
196 cpufreq_cpu_put(policy);
199 static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
200 struct cpufreq_frequency_table *table)
202 unsigned int i, j, count = 0, ret = 0;
203 struct cpufreq_stats *stat;
204 struct cpufreq_policy *data;
205 unsigned int alloc_size;
206 unsigned int cpu = policy->cpu;
207 if (per_cpu(cpufreq_stats_table, cpu))
209 stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
213 data = cpufreq_cpu_get(cpu);
219 ret = sysfs_create_group(&data->kobj, &stats_attr_group);
224 per_cpu(cpufreq_stats_table, cpu) = stat;
226 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
227 unsigned int freq = table[i].frequency;
228 if (freq == CPUFREQ_ENTRY_INVALID)
233 alloc_size = count * sizeof(int) + count * sizeof(u64);
235 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
236 alloc_size += count * count * sizeof(int);
238 stat->max_state = count;
239 stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
240 if (!stat->time_in_state) {
244 stat->freq_table = (unsigned int *)(stat->time_in_state + count);
246 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
247 stat->trans_table = stat->freq_table + count;
250 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
251 unsigned int freq = table[i].frequency;
252 if (freq == CPUFREQ_ENTRY_INVALID)
254 if (freq_table_get_index(stat, freq) == -1)
255 stat->freq_table[j++] = freq;
258 spin_lock(&cpufreq_stats_lock);
259 stat->last_time = get_jiffies_64();
260 stat->last_index = freq_table_get_index(stat, policy->cur);
261 spin_unlock(&cpufreq_stats_lock);
262 cpufreq_cpu_put(data);
265 cpufreq_cpu_put(data);
268 per_cpu(cpufreq_stats_table, cpu) = NULL;
272 static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
274 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
277 pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
278 policy->cpu, policy->last_cpu);
279 per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
281 per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
282 stat->cpu = policy->cpu;
285 static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
286 unsigned long val, void *data)
289 struct cpufreq_policy *policy = data;
290 struct cpufreq_frequency_table *table;
291 unsigned int cpu = policy->cpu;
293 if (val == CPUFREQ_UPDATE_POLICY_CPU) {
294 cpufreq_stats_update_policy_cpu(policy);
298 if (val != CPUFREQ_NOTIFY)
300 table = cpufreq_frequency_get_table(cpu);
303 ret = cpufreq_stats_create_table(policy, table);
309 static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
310 unsigned long val, void *data)
312 struct cpufreq_freqs *freq = data;
313 struct cpufreq_stats *stat;
314 int old_index, new_index;
316 if (val != CPUFREQ_POSTCHANGE)
319 stat = per_cpu(cpufreq_stats_table, freq->cpu);
323 old_index = stat->last_index;
324 new_index = freq_table_get_index(stat, freq->new);
326 /* We can't do stat->time_in_state[-1]= .. */
327 if (old_index == -1 || new_index == -1)
330 cpufreq_stats_update(freq->cpu);
332 if (old_index == new_index)
335 spin_lock(&cpufreq_stats_lock);
336 stat->last_index = new_index;
337 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
338 stat->trans_table[old_index * stat->max_state + new_index]++;
341 spin_unlock(&cpufreq_stats_lock);
345 static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
346 unsigned long action,
349 unsigned int cpu = (unsigned long)hcpu;
353 case CPU_ONLINE_FROZEN:
354 cpufreq_update_policy(cpu);
356 case CPU_DOWN_PREPARE:
357 case CPU_DOWN_PREPARE_FROZEN:
358 cpufreq_stats_free_sysfs(cpu);
361 case CPU_DEAD_FROZEN:
362 cpufreq_stats_free_table(cpu);
368 /* priority=1 so this will get called before cpufreq_remove_dev */
369 static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
370 .notifier_call = cpufreq_stat_cpu_callback,
374 static struct notifier_block notifier_policy_block = {
375 .notifier_call = cpufreq_stat_notifier_policy
378 static struct notifier_block notifier_trans_block = {
379 .notifier_call = cpufreq_stat_notifier_trans
382 static int cpufreq_stats_setup(void)
387 spin_lock_init(&cpufreq_stats_lock);
388 ret = cpufreq_register_notifier(¬ifier_policy_block,
389 CPUFREQ_POLICY_NOTIFIER);
393 register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
394 for_each_online_cpu(cpu)
395 cpufreq_update_policy(cpu);
397 ret = cpufreq_register_notifier(¬ifier_trans_block,
398 CPUFREQ_TRANSITION_NOTIFIER);
400 cpufreq_unregister_notifier(¬ifier_policy_block,
401 CPUFREQ_POLICY_NOTIFIER);
402 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
403 for_each_online_cpu(cpu)
404 cpufreq_stats_free_table(cpu);
411 static void cpufreq_stats_cleanup(void)
415 cpufreq_unregister_notifier(¬ifier_policy_block,
416 CPUFREQ_POLICY_NOTIFIER);
417 cpufreq_unregister_notifier(¬ifier_trans_block,
418 CPUFREQ_TRANSITION_NOTIFIER);
419 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
420 for_each_online_cpu(cpu) {
421 cpufreq_stats_free_table(cpu);
422 cpufreq_stats_free_sysfs(cpu);
426 static int cpufreq_stats_switcher_notifier(struct notifier_block *nfb,
427 unsigned long action, void *_arg)
430 case BL_NOTIFY_PRE_ENABLE:
431 case BL_NOTIFY_PRE_DISABLE:
432 cpufreq_stats_cleanup();
435 case BL_NOTIFY_POST_ENABLE:
436 case BL_NOTIFY_POST_DISABLE:
437 cpufreq_stats_setup();
447 static struct notifier_block switcher_notifier = {
448 .notifier_call = cpufreq_stats_switcher_notifier,
451 static int __init cpufreq_stats_init(void)
454 spin_lock_init(&cpufreq_stats_lock);
456 ret = cpufreq_stats_setup();
458 bL_switcher_register_notifier(&switcher_notifier);
463 static void __exit cpufreq_stats_exit(void)
465 bL_switcher_unregister_notifier(&switcher_notifier);
466 cpufreq_stats_cleanup();
469 MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
470 MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
471 "through sysfs filesystem");
472 MODULE_LICENSE("GPL");
474 module_init(cpufreq_stats_init);
475 module_exit(cpufreq_stats_exit);