[CPUFREQ] cpufreq stat output in sysfs From: "Zou, Nanhai" Export cpufreq transition information for drivers using the freq-table helpers via sysfs. Two minor updates from Dominik Brodowski: s/translation/transition Kconfig re-ordering --- drivers/cpufreq/Kconfig | 16 + drivers/cpufreq/Makefile | 2 drivers/cpufreq/cpufreq.c | 6 drivers/cpufreq/cpufreq_stats.c | 334 ++++++++++++++++++++++++++++++++++++++++ drivers/cpufreq/freq_table.c | 5 include/linux/cpufreq.h | 5 6 files changed, 366 insertions(+), 2 deletions(-) Index: 2.6.10-rc3/drivers/cpufreq/Kconfig =================================================================== --- 2.6.10-rc3.orig/drivers/cpufreq/Kconfig 2004-12-13 19:19:05.995558603 +0100 +++ 2.6.10-rc3/drivers/cpufreq/Kconfig 2004-12-13 19:20:22.982805539 +0100 @@ -27,6 +27,22 @@ 2 to activate CPUfreq drivers debugging, and 4 to activate CPUfreq governor debugging +config CPU_FREQ_STAT + tristate "CPU frequency translation statistics" + depends on CPU_FREQ && CPU_FREQ_TABLE + default y + help + This driver exports CPU frequency statistics information through sysfs + file system + +config CPU_FREQ_STAT_DETAILS + bool "CPU frequency translation statistics details" + depends on CPU_FREQ && CPU_FREQ_STAT + default n + help + This will show detail CPU frequency translation table in sysfs file + system + config CPU_FREQ_PROC_INTF tristate "/proc/cpufreq interface (deprecated)" depends on CPU_FREQ && PROC_FS Index: 2.6.10-rc3/drivers/cpufreq/Makefile =================================================================== --- 2.6.10-rc3.orig/drivers/cpufreq/Makefile 2004-12-13 19:19:05.996558463 +0100 +++ 2.6.10-rc3/drivers/cpufreq/Makefile 2004-12-13 19:20:22.982805539 +0100 @@ -1,5 +1,7 @@ # CPUfreq core obj-$(CONFIG_CPU_FREQ) += cpufreq.o +# CPUfreq stats +obj-$(CONFIG_CPU_FREQ_STAT) += cpufreq_stats.o # CPUfreq governors obj-$(CONFIG_CPU_FREQ_GOV_PERFORMANCE) += cpufreq_performance.o Index: 2.6.10-rc3/drivers/cpufreq/cpufreq.c =================================================================== --- 2.6.10-rc3.orig/drivers/cpufreq/cpufreq.c 2004-12-13 19:20:18.437440405 +0100 +++ 2.6.10-rc3/drivers/cpufreq/cpufreq.c 2004-12-13 19:20:22.980805818 +0100 @@ -63,7 +63,7 @@ static LIST_HEAD(cpufreq_governor_list); static DECLARE_MUTEX (cpufreq_governor_sem); -static struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu) +struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu) { struct cpufreq_policy *data; unsigned long flags; @@ -102,12 +102,14 @@ err_out: return NULL; } +EXPORT_SYMBOL_GPL(cpufreq_cpu_get); -static void cpufreq_cpu_put(struct cpufreq_policy *data) +void cpufreq_cpu_put(struct cpufreq_policy *data) { kobject_put(&data->kobj); module_put(cpufreq_driver->owner); } +EXPORT_SYMBOL_GPL(cpufreq_cpu_put); /********************************************************************* Index: 2.6.10-rc3/drivers/cpufreq/cpufreq_stats.c =================================================================== --- 2.6.10-rc3.orig/drivers/cpufreq/cpufreq_stats.c 2004-12-13 18:37:01.720111248 +0100 +++ 2.6.10-rc3/drivers/cpufreq/cpufreq_stats.c 2004-12-13 19:20:22.981805678 +0100 @@ -0,0 +1,334 @@ +/* + * drivers/cpufreq/cpufreq_stats.c + * + * Copyright (C) 2003-2004 Venkatesh Pallipadi . + * (C) 2004 Zou Nan hai . + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static spinlock_t cpufreq_stats_lock; + +#define CPUFREQ_STATDEVICE_ATTR(_name,_mode,_show) \ +static struct freq_attr _attr_##_name = {\ + .attr = {.name = __stringify(_name), .owner = THIS_MODULE, \ + .mode = _mode, }, \ + .show = _show,\ +}; + +static unsigned long +delta_time(unsigned long old, unsigned long new) +{ + return (old > new) ? (old - new): (new + ~old + 1); +} + +struct cpufreq_stats { + unsigned int cpu; + unsigned int total_trans; + unsigned long long last_time; + unsigned int max_state; + unsigned int state_num; + unsigned int last_index; + unsigned long long *time_in_state; + unsigned int *freq_table; +#ifdef CONFIG_CPU_FREQ_STAT_DETAILS + unsigned int *trans_table; +#endif +}; + +static struct cpufreq_stats *cpufreq_stats_table[NR_CPUS]; + +struct cpufreq_stats_attribute { + struct attribute attr; + ssize_t(*show) (struct cpufreq_stats *, char *); +}; + +static int +cpufreq_stats_update (unsigned int cpu) +{ + struct cpufreq_stats *stat; + spin_lock(&cpufreq_stats_lock); + stat = cpufreq_stats_table[cpu]; + if (stat->time_in_state) + stat->time_in_state[stat->last_index] += + delta_time(stat->last_time, jiffies); + stat->last_time = jiffies; + spin_unlock(&cpufreq_stats_lock); + return 0; +} + +static ssize_t +show_total_trans(struct cpufreq_policy *policy, char *buf) +{ + struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu]; + if(!stat) + return 0; + return sprintf(buf, "%d\n", + cpufreq_stats_table[stat->cpu]->total_trans); +} + +static ssize_t +show_time_in_state(struct cpufreq_policy *policy, char *buf) +{ + ssize_t len = 0; + int i; + struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu]; + if(!stat) + return 0; + cpufreq_stats_update(stat->cpu); + for (i = 0; i < stat->state_num; i++) { + len += sprintf(buf + len, "%u %llu\n", + stat->freq_table[i], stat->time_in_state[i]); + } + return len; +} + +#ifdef CONFIG_CPU_FREQ_STAT_DETAILS +static ssize_t +show_trans_table(struct cpufreq_policy *policy, char *buf) +{ + ssize_t len = 0; + int i, j; + + struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu]; + if(!stat) + return 0; + cpufreq_stats_update(stat->cpu); + for (i = 0; i < stat->state_num; i++) { + if (len >= PAGE_SIZE) + break; + len += snprintf(buf + len, PAGE_SIZE - len, "%9u:\t", + stat->freq_table[i]); + + for (j = 0; j < stat->state_num; j++) { + if (len >= PAGE_SIZE) + break; + len += snprintf(buf + len, PAGE_SIZE - len, "%u\t", + stat->trans_table[i*stat->max_state+j]); + } + len += snprintf(buf + len, PAGE_SIZE - len, "\n"); + } + return len; +} +CPUFREQ_STATDEVICE_ATTR(trans_table,0444,show_trans_table); +#endif + +CPUFREQ_STATDEVICE_ATTR(total_trans,0444,show_total_trans); +CPUFREQ_STATDEVICE_ATTR(time_in_state,0444,show_time_in_state); + +static struct attribute *default_attrs[] = { + &_attr_total_trans.attr, + &_attr_time_in_state.attr, +#ifdef CONFIG_CPU_FREQ_STAT_DETAILS + &_attr_trans_table.attr, +#endif + NULL +}; +static struct attribute_group stats_attr_group = { + .attrs = default_attrs, + .name = "stats" +}; + +static int +freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq) +{ + int index; + for (index = 0; index < stat->max_state; index++) + if (stat->freq_table[index] == freq) + return index; + return -1; +} + +static void +cpufreq_stats_free_table (unsigned int cpu) +{ + struct cpufreq_stats *stat = cpufreq_stats_table[cpu]; + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); + if (policy && policy->cpu == cpu) + sysfs_remove_group(&policy->kobj, &stats_attr_group); + if (stat) { + kfree(stat->time_in_state); + kfree(stat); + } + cpufreq_stats_table[cpu] = NULL; + if (policy) + cpufreq_cpu_put(policy); +} + +static int +cpufreq_stats_create_table (struct cpufreq_policy *policy, + struct cpufreq_frequency_table *table) +{ + unsigned int i, j, count = 0, ret = 0; + struct cpufreq_stats *stat; + struct cpufreq_policy *data; + unsigned int alloc_size; + unsigned int cpu = policy->cpu; + if (cpufreq_stats_table[cpu]) + return -EBUSY; + if ((stat = kmalloc(sizeof(struct cpufreq_stats), GFP_KERNEL)) == NULL) + return -ENOMEM; + memset(stat, 0, sizeof (struct cpufreq_stats)); + + data = cpufreq_cpu_get(cpu); + if ((ret = sysfs_create_group(&data->kobj, &stats_attr_group))) + goto error_out; + + stat->cpu = cpu; + cpufreq_stats_table[cpu] = stat; + + for (i=0; table[i].frequency != CPUFREQ_TABLE_END; i++) { + unsigned int freq = table[i].frequency; + if (freq == CPUFREQ_ENTRY_INVALID) + continue; + count++; + } + + alloc_size = count * sizeof(int) + count * sizeof(long long); + +#ifdef CONFIG_CPU_FREQ_STAT_DETAILS + alloc_size += count * count * sizeof(int); +#endif + stat->max_state = count; + stat->time_in_state = kmalloc(alloc_size, GFP_KERNEL); + if (!stat->time_in_state) { + ret = -ENOMEM; + goto error_out; + } + memset(stat->time_in_state, 0, alloc_size); + stat->freq_table = (unsigned int *)(stat->time_in_state + count); + +#ifdef CONFIG_CPU_FREQ_STAT_DETAILS + stat->trans_table = stat->freq_table + count; +#endif + j = 0; + for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { + unsigned int freq = table[i].frequency; + if (freq == CPUFREQ_ENTRY_INVALID) + continue; + if (freq_table_get_index(stat, freq) == -1) + stat->freq_table[j++] = freq; + } + stat->state_num = j; + spin_lock(&cpufreq_stats_lock); + stat->last_time = jiffies; + stat->last_index = freq_table_get_index(stat, policy->cur); + spin_unlock(&cpufreq_stats_lock); + cpufreq_cpu_put(data); + return 0; +error_out: + cpufreq_cpu_put(data); + kfree(stat); + cpufreq_stats_table[cpu] = NULL; + return ret; +} + +static int +cpufreq_stat_notifier_policy (struct notifier_block *nb, unsigned long val, + void *data) +{ + int ret; + struct cpufreq_policy *policy = data; + struct cpufreq_frequency_table *table; + unsigned int cpu = policy->cpu; + if (val != CPUFREQ_NOTIFY) + return 0; + table = cpufreq_frequency_get_table(cpu); + if (!table) + return 0; + if ((ret = cpufreq_stats_create_table(policy, table))) + return ret; + return 0; +} + +static int +cpufreq_stat_notifier_trans (struct notifier_block *nb, unsigned long val, + void *data) +{ + struct cpufreq_freqs *freq = data; + struct cpufreq_stats *stat; + int old_index, new_index; + + if (val != CPUFREQ_POSTCHANGE) + return 0; + + stat = cpufreq_stats_table[freq->cpu]; + if (!stat) + return 0; + old_index = freq_table_get_index(stat, freq->old); + new_index = freq_table_get_index(stat, freq->new); + + cpufreq_stats_update(freq->cpu); + if (old_index == new_index) + return 0; + + spin_lock(&cpufreq_stats_lock); + stat->last_index = new_index; +#ifdef CONFIG_CPU_FREQ_STAT_DETAILS + stat->trans_table[old_index * stat->max_state + new_index]++; +#endif + stat->total_trans++; + spin_unlock(&cpufreq_stats_lock); + return 0; +} + +static struct notifier_block notifier_policy_block = { + .notifier_call = cpufreq_stat_notifier_policy +}; + +static struct notifier_block notifier_trans_block = { + .notifier_call = cpufreq_stat_notifier_trans +}; + +static int +__init cpufreq_stats_init(void) +{ + int ret; + unsigned int cpu; + spin_lock_init(&cpufreq_stats_lock); + if ((ret = cpufreq_register_notifier(¬ifier_policy_block, + CPUFREQ_POLICY_NOTIFIER))) + return ret; + + if ((ret = cpufreq_register_notifier(¬ifier_trans_block, + CPUFREQ_TRANSITION_NOTIFIER))) { + cpufreq_unregister_notifier(¬ifier_policy_block, + CPUFREQ_POLICY_NOTIFIER); + return ret; + } + + for_each_cpu(cpu) + cpufreq_update_policy(cpu); + return 0; +} +static void +__exit cpufreq_stats_exit(void) +{ + unsigned int cpu; + cpufreq_unregister_notifier(¬ifier_policy_block, + CPUFREQ_POLICY_NOTIFIER); + cpufreq_unregister_notifier(¬ifier_trans_block, + CPUFREQ_TRANSITION_NOTIFIER); + for_each_cpu(cpu) + cpufreq_stats_free_table(cpu); +} + +MODULE_AUTHOR ("Zou Nan hai "); +MODULE_DESCRIPTION ("'cpufreq_stats' - A driver to export cpufreq stats through sysfs filesystem"); +MODULE_LICENSE ("GPL"); + +module_init(cpufreq_stats_init); +module_exit(cpufreq_stats_exit); Index: 2.6.10-rc3/drivers/cpufreq/freq_table.c =================================================================== --- 2.6.10-rc3.orig/drivers/cpufreq/freq_table.c 2004-12-13 19:19:05.995558603 +0100 +++ 2.6.10-rc3/drivers/cpufreq/freq_table.c 2004-12-13 19:20:22.981805678 +0100 @@ -214,6 +214,11 @@ } EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr); +struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) +{ + return show_table[cpu]; +} +EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); MODULE_AUTHOR ("Dominik Brodowski "); MODULE_DESCRIPTION ("CPUfreq frequency table helpers"); Index: 2.6.10-rc3/include/linux/cpufreq.h =================================================================== --- 2.6.10-rc3.orig/include/linux/cpufreq.h 2004-12-13 19:19:05.997558323 +0100 +++ 2.6.10-rc3/include/linux/cpufreq.h 2004-12-13 19:20:22.983805399 +0100 @@ -351,6 +351,11 @@ unsigned int relation, unsigned int *index); +/* the following 3 funtions are for cpufreq core use only */ +struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu); +struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu); +void cpufreq_cpu_put (struct cpufreq_policy *data); + /* the following are really really optional */ extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;