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 <linux/sort.h>
24 #include <linux/err.h>
26 #include <asm/cputime.h>
28 static spinlock_t cpufreq_stats_lock;
30 struct cpufreq_stats {
32 unsigned int total_trans;
33 unsigned long long last_time;
34 unsigned int max_state;
35 unsigned int state_num;
36 unsigned int last_index;
38 unsigned int *freq_table;
39 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
40 unsigned int *trans_table;
44 struct all_cpufreq_stats {
45 unsigned int state_num;
46 cputime64_t *time_in_state;
47 unsigned int *freq_table;
50 struct cpufreq_power_stats {
51 unsigned int state_num;
53 unsigned int *freq_table;
56 struct all_freq_table {
57 unsigned int *freq_table;
58 unsigned int table_size;
61 static struct all_freq_table *all_freq_table;
63 static DEFINE_PER_CPU(struct all_cpufreq_stats *, all_cpufreq_stats);
64 static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
65 static DEFINE_PER_CPU(struct cpufreq_power_stats *, cpufreq_power_stats);
67 struct cpufreq_stats_attribute {
68 struct attribute attr;
69 ssize_t(*show) (struct cpufreq_stats *, char *);
72 static int cpufreq_stats_update(unsigned int cpu)
74 struct cpufreq_stats *stat;
75 struct all_cpufreq_stats *all_stat;
76 unsigned long long cur_time;
78 cur_time = get_jiffies_64();
79 spin_lock(&cpufreq_stats_lock);
80 stat = per_cpu(cpufreq_stats_table, cpu);
81 all_stat = per_cpu(all_cpufreq_stats, cpu);
83 spin_unlock(&cpufreq_stats_lock);
86 if (stat->time_in_state) {
87 stat->time_in_state[stat->last_index] +=
88 cur_time - stat->last_time;
90 all_stat->time_in_state[stat->last_index] +=
91 cur_time - stat->last_time;
93 stat->last_time = cur_time;
94 spin_unlock(&cpufreq_stats_lock);
98 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
100 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
103 return sprintf(buf, "%d\n",
104 per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
107 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
111 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
114 cpufreq_stats_update(stat->cpu);
115 for (i = 0; i < stat->state_num; i++) {
116 len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
118 cputime64_to_clock_t(stat->time_in_state[i]));
123 static int get_index_all_cpufreq_stat(struct all_cpufreq_stats *all_stat,
129 for (i = 0; i < all_stat->state_num; i++) {
130 if (all_stat->freq_table[i] == freq)
136 static ssize_t show_current_in_state(struct kobject *kobj,
137 struct kobj_attribute *attr, char *buf)
141 struct cpufreq_power_stats *powerstats;
143 spin_lock(&cpufreq_stats_lock);
144 for_each_possible_cpu(cpu) {
145 powerstats = per_cpu(cpufreq_power_stats, cpu);
148 len += scnprintf(buf + len, PAGE_SIZE - len, "CPU%d:", cpu);
149 for (i = 0; i < powerstats->state_num; i++)
150 len += scnprintf(buf + len, PAGE_SIZE - len,
151 "%d=%d ", powerstats->freq_table[i],
152 powerstats->curr[i]);
153 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
155 spin_unlock(&cpufreq_stats_lock);
159 static ssize_t show_all_time_in_state(struct kobject *kobj,
160 struct kobj_attribute *attr, char *buf)
163 unsigned int i, cpu, freq, index;
164 struct all_cpufreq_stats *all_stat;
165 struct cpufreq_policy *policy;
167 len += scnprintf(buf + len, PAGE_SIZE - len, "freq\t\t");
168 for_each_possible_cpu(cpu) {
169 len += scnprintf(buf + len, PAGE_SIZE - len, "cpu%d\t\t", cpu);
171 cpufreq_stats_update(cpu);
176 for (i = 0; i < all_freq_table->table_size; i++) {
177 freq = all_freq_table->freq_table[i];
178 len += scnprintf(buf + len, PAGE_SIZE - len, "\n%u\t\t", freq);
179 for_each_possible_cpu(cpu) {
180 policy = cpufreq_cpu_get(cpu);
183 all_stat = per_cpu(all_cpufreq_stats, policy->cpu);
184 index = get_index_all_cpufreq_stat(all_stat, freq);
186 len += scnprintf(buf + len, PAGE_SIZE - len,
187 "%llu\t\t", (unsigned long long)
188 cputime64_to_clock_t(all_stat->time_in_state[index]));
190 len += scnprintf(buf + len, PAGE_SIZE - len,
193 cpufreq_cpu_put(policy);
198 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
202 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
203 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
208 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
211 cpufreq_stats_update(stat->cpu);
212 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
213 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
214 for (i = 0; i < stat->state_num; i++) {
215 if (len >= PAGE_SIZE)
217 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
218 stat->freq_table[i]);
220 if (len >= PAGE_SIZE)
223 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
225 for (i = 0; i < stat->state_num; i++) {
226 if (len >= PAGE_SIZE)
229 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
230 stat->freq_table[i]);
232 for (j = 0; j < stat->state_num; j++) {
233 if (len >= PAGE_SIZE)
235 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
236 stat->trans_table[i*stat->max_state+j]);
238 if (len >= PAGE_SIZE)
240 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
242 if (len >= PAGE_SIZE)
246 cpufreq_freq_attr_ro(trans_table);
249 cpufreq_freq_attr_ro(total_trans);
250 cpufreq_freq_attr_ro(time_in_state);
252 static struct attribute *default_attrs[] = {
255 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
260 static struct attribute_group stats_attr_group = {
261 .attrs = default_attrs,
265 static struct kobj_attribute _attr_all_time_in_state = __ATTR(all_time_in_state,
266 0444, show_all_time_in_state, NULL);
268 static struct kobj_attribute _attr_current_in_state = __ATTR(current_in_state,
269 0444, show_current_in_state, NULL);
271 static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
274 for (index = 0; index < stat->max_state; index++)
275 if (stat->freq_table[index] == freq)
280 /* should be called late in the CPU removal sequence so that the stats
281 * memory is still available in case someone tries to use it.
283 static void cpufreq_stats_free_table(unsigned int cpu)
285 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, cpu);
288 pr_debug("%s: Free stat table\n", __func__);
289 kfree(stat->time_in_state);
291 per_cpu(cpufreq_stats_table, cpu) = NULL;
295 /* must be called early in the CPU removal sequence (before
296 * cpufreq_remove_dev) so that policy is still valid.
298 static void cpufreq_stats_free_sysfs(unsigned int cpu)
300 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
305 if (!cpufreq_frequency_get_table(cpu))
308 if (!policy_is_shared(policy)) {
309 pr_debug("%s: Free sysfs stat\n", __func__);
310 sysfs_remove_group(&policy->kobj, &stats_attr_group);
314 cpufreq_cpu_put(policy);
317 static void cpufreq_allstats_free(void)
320 struct all_cpufreq_stats *all_stat;
322 sysfs_remove_file(cpufreq_global_kobject,
323 &_attr_all_time_in_state.attr);
325 for_each_possible_cpu(cpu) {
326 all_stat = per_cpu(all_cpufreq_stats, cpu);
329 kfree(all_stat->time_in_state);
331 per_cpu(all_cpufreq_stats, cpu) = NULL;
333 if (all_freq_table) {
334 kfree(all_freq_table->freq_table);
335 kfree(all_freq_table);
336 all_freq_table = NULL;
340 static void cpufreq_powerstats_free(void)
343 struct cpufreq_power_stats *powerstats;
345 sysfs_remove_file(cpufreq_global_kobject, &_attr_current_in_state.attr);
347 for_each_possible_cpu(cpu) {
348 powerstats = per_cpu(cpufreq_power_stats, cpu);
351 kfree(powerstats->curr);
353 per_cpu(cpufreq_power_stats, cpu) = NULL;
357 static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
358 struct cpufreq_frequency_table *table, int count)
360 unsigned int i, j, ret = 0;
361 struct cpufreq_stats *stat;
362 struct cpufreq_policy *data;
363 unsigned int alloc_size;
364 unsigned int cpu = policy->cpu;
365 if (per_cpu(cpufreq_stats_table, cpu))
367 stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
371 data = cpufreq_cpu_get(cpu);
377 ret = sysfs_create_group(&data->kobj, &stats_attr_group);
382 per_cpu(cpufreq_stats_table, cpu) = stat;
385 alloc_size = count * sizeof(int) + count * sizeof(u64);
387 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
388 alloc_size += count * count * sizeof(int);
390 stat->max_state = count;
391 stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
392 if (!stat->time_in_state) {
396 stat->freq_table = (unsigned int *)(stat->time_in_state + count);
398 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
399 stat->trans_table = stat->freq_table + count;
402 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
403 unsigned int freq = table[i].frequency;
404 if (freq == CPUFREQ_ENTRY_INVALID)
406 if (freq_table_get_index(stat, freq) == -1)
407 stat->freq_table[j++] = freq;
410 spin_lock(&cpufreq_stats_lock);
411 stat->last_time = get_jiffies_64();
412 stat->last_index = freq_table_get_index(stat, policy->cur);
413 spin_unlock(&cpufreq_stats_lock);
414 cpufreq_cpu_put(data);
417 cpufreq_cpu_put(data);
420 per_cpu(cpufreq_stats_table, cpu) = NULL;
424 static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
426 struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
429 pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
430 policy->cpu, policy->last_cpu);
431 per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
433 per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
434 stat->cpu = policy->cpu;
437 static void cpufreq_powerstats_create(unsigned int cpu,
438 struct cpufreq_frequency_table *table, int count) {
439 unsigned int alloc_size, i = 0, j = 0, ret = 0;
440 struct cpufreq_power_stats *powerstats;
441 struct device_node *cpu_node;
442 char device_path[16];
444 powerstats = kzalloc(sizeof(struct cpufreq_power_stats),
449 /* Allocate memory for freq table per cpu as well as clockticks per
451 alloc_size = count * sizeof(unsigned int) +
452 count * sizeof(unsigned int);
453 powerstats->curr = kzalloc(alloc_size, GFP_KERNEL);
454 if (!powerstats->curr) {
458 powerstats->freq_table = powerstats->curr + count;
460 spin_lock(&cpufreq_stats_lock);
461 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END && j < count; i++) {
462 unsigned int freq = table[i].frequency;
464 if (freq == CPUFREQ_ENTRY_INVALID)
466 powerstats->freq_table[j++] = freq;
468 powerstats->state_num = j;
470 snprintf(device_path, sizeof(device_path), "/cpus/cpu@%d", cpu);
471 cpu_node = of_find_node_by_path(device_path);
473 ret = of_property_read_u32_array(cpu_node, "current",
474 powerstats->curr, count);
476 kfree(powerstats->curr);
481 per_cpu(cpufreq_power_stats, cpu) = powerstats;
482 spin_unlock(&cpufreq_stats_lock);
485 static int compare_for_sort(const void *lhs_ptr, const void *rhs_ptr)
487 unsigned int lhs = *(const unsigned int *)(lhs_ptr);
488 unsigned int rhs = *(const unsigned int *)(rhs_ptr);
496 static bool check_all_freq_table(unsigned int freq)
499 for (i = 0; i < all_freq_table->table_size; i++) {
500 if (freq == all_freq_table->freq_table[i])
506 static void create_all_freq_table(void)
508 all_freq_table = kzalloc(sizeof(struct all_freq_table),
511 pr_warn("could not allocate memory for all_freq_table\n");
515 static void add_all_freq_table(unsigned int freq)
518 size = sizeof(unsigned int) * (all_freq_table->table_size + 1);
519 all_freq_table->freq_table = krealloc(all_freq_table->freq_table,
521 if (IS_ERR(all_freq_table->freq_table)) {
522 pr_warn("Could not reallocate memory for freq_table\n");
523 all_freq_table->freq_table = NULL;
526 all_freq_table->freq_table[all_freq_table->table_size++] = freq;
529 static void cpufreq_allstats_create(unsigned int cpu,
530 struct cpufreq_frequency_table *table, int count)
533 unsigned int alloc_size;
534 struct all_cpufreq_stats *all_stat;
535 bool sort_needed = false;
537 all_stat = kzalloc(sizeof(struct all_cpufreq_stats),
540 pr_warn("Cannot allocate memory for cpufreq stats\n");
544 /*Allocate memory for freq table per cpu as well as clockticks per freq*/
545 alloc_size = count * sizeof(int) + count * sizeof(cputime64_t);
546 all_stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
547 if (!all_stat->time_in_state) {
548 pr_warn("Cannot allocate memory for cpufreq time_in_state\n");
553 all_stat->freq_table = (unsigned int *)
554 (all_stat->time_in_state + count);
556 spin_lock(&cpufreq_stats_lock);
557 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
558 unsigned int freq = table[i].frequency;
559 if (freq == CPUFREQ_ENTRY_INVALID)
561 all_stat->freq_table[j++] = freq;
562 if (all_freq_table && !check_all_freq_table(freq)) {
563 add_all_freq_table(freq);
568 sort(all_freq_table->freq_table, all_freq_table->table_size,
569 sizeof(unsigned int), &compare_for_sort, NULL);
570 all_stat->state_num = j;
571 per_cpu(all_cpufreq_stats, cpu) = all_stat;
572 spin_unlock(&cpufreq_stats_lock);
575 static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
576 unsigned long val, void *data)
578 int ret, count = 0, i;
579 struct cpufreq_policy *policy = data;
580 struct cpufreq_frequency_table *table;
581 unsigned int cpu = policy->cpu;
583 if (val == CPUFREQ_UPDATE_POLICY_CPU) {
584 cpufreq_stats_update_policy_cpu(policy);
588 if (val != CPUFREQ_NOTIFY)
590 table = cpufreq_frequency_get_table(cpu);
594 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
595 unsigned int freq = table[i].frequency;
597 if (freq == CPUFREQ_ENTRY_INVALID)
602 if (!per_cpu(all_cpufreq_stats, cpu))
603 cpufreq_allstats_create(cpu, table, count);
605 if (!per_cpu(cpufreq_power_stats, cpu))
606 cpufreq_powerstats_create(cpu, table, count);
608 ret = cpufreq_stats_create_table(policy, table, count);
614 static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
615 unsigned long val, void *data)
617 struct cpufreq_freqs *freq = data;
618 struct cpufreq_stats *stat;
619 int old_index, new_index;
621 if (val != CPUFREQ_POSTCHANGE)
624 stat = per_cpu(cpufreq_stats_table, freq->cpu);
628 old_index = stat->last_index;
629 new_index = freq_table_get_index(stat, freq->new);
631 /* We can't do stat->time_in_state[-1]= .. */
632 if (old_index == -1 || new_index == -1)
635 cpufreq_stats_update(freq->cpu);
637 if (old_index == new_index)
640 spin_lock(&cpufreq_stats_lock);
641 stat->last_index = new_index;
642 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
643 stat->trans_table[old_index * stat->max_state + new_index]++;
646 spin_unlock(&cpufreq_stats_lock);
650 static int cpufreq_stats_create_table_cpu(unsigned int cpu)
652 struct cpufreq_policy *policy;
653 struct cpufreq_frequency_table *table;
654 int ret = -ENODEV, i, count = 0;
656 policy = cpufreq_cpu_get(cpu);
660 table = cpufreq_frequency_get_table(cpu);
664 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
665 unsigned int freq = table[i].frequency;
667 if (freq == CPUFREQ_ENTRY_INVALID)
672 if (!per_cpu(all_cpufreq_stats, cpu))
673 cpufreq_allstats_create(cpu, table, count);
675 if (!per_cpu(cpufreq_power_stats, cpu))
676 cpufreq_powerstats_create(cpu, table, count);
678 ret = cpufreq_stats_create_table(policy, table, count);
681 cpufreq_cpu_put(policy);
685 static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
686 unsigned long action,
689 unsigned int cpu = (unsigned long)hcpu;
693 cpufreq_update_policy(cpu);
695 case CPU_DOWN_PREPARE:
696 cpufreq_stats_free_sysfs(cpu);
699 cpufreq_stats_free_table(cpu);
701 case CPU_UP_CANCELED_FROZEN:
702 cpufreq_stats_free_sysfs(cpu);
703 cpufreq_stats_free_table(cpu);
705 case CPU_DOWN_FAILED:
706 case CPU_DOWN_FAILED_FROZEN:
707 cpufreq_stats_create_table_cpu(cpu);
713 /* priority=1 so this will get called before cpufreq_remove_dev */
714 static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
715 .notifier_call = cpufreq_stat_cpu_callback,
719 static struct notifier_block notifier_policy_block = {
720 .notifier_call = cpufreq_stat_notifier_policy
723 static struct notifier_block notifier_trans_block = {
724 .notifier_call = cpufreq_stat_notifier_trans
727 static int __init cpufreq_stats_init(void)
732 spin_lock_init(&cpufreq_stats_lock);
733 ret = cpufreq_register_notifier(¬ifier_policy_block,
734 CPUFREQ_POLICY_NOTIFIER);
738 register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
739 for_each_online_cpu(cpu)
740 cpufreq_update_policy(cpu);
742 ret = cpufreq_register_notifier(¬ifier_trans_block,
743 CPUFREQ_TRANSITION_NOTIFIER);
745 cpufreq_unregister_notifier(¬ifier_policy_block,
746 CPUFREQ_POLICY_NOTIFIER);
747 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
748 for_each_online_cpu(cpu)
749 cpufreq_stats_free_table(cpu);
753 create_all_freq_table();
754 ret = sysfs_create_file(cpufreq_global_kobject,
755 &_attr_all_time_in_state.attr);
757 pr_warn("Cannot create sysfs file for cpufreq stats\n");
759 ret = sysfs_create_file(cpufreq_global_kobject,
760 &_attr_current_in_state.attr);
762 pr_warn("Cannot create sysfs file for cpufreq current stats\n");
766 static void __exit cpufreq_stats_exit(void)
770 cpufreq_unregister_notifier(¬ifier_policy_block,
771 CPUFREQ_POLICY_NOTIFIER);
772 cpufreq_unregister_notifier(¬ifier_trans_block,
773 CPUFREQ_TRANSITION_NOTIFIER);
774 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
775 for_each_online_cpu(cpu) {
776 cpufreq_stats_free_table(cpu);
777 cpufreq_stats_free_sysfs(cpu);
779 cpufreq_allstats_free();
780 cpufreq_powerstats_free();
783 MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
784 MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
785 "through sysfs filesystem");
786 MODULE_LICENSE("GPL");
788 module_init(cpufreq_stats_init);
789 module_exit(cpufreq_stats_exit);