#include <linux/cpufreq.h>
#include <linux/module.h>
static DEFINE_SPINLOCK(cpufreq_stats_lock);
unsigned int total_trans;
unsigned long long last_time;
unsigned int *freq_table;
unsigned int *trans_table;
static void cpufreq_stats_update(struct cpufreq_stats *stats)
unsigned long long cur_time = get_jiffies_64();
stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
stats->last_time = cur_time;
static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
unsigned int count = stats->max_state;
spin_lock(&cpufreq_stats_lock);
memset(stats->time_in_state, 0, count * sizeof(u64));
memset(stats->trans_table, 0, count * count * sizeof(int));
stats->last_time = get_jiffies_64();
spin_unlock(&cpufreq_stats_lock);
static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
return sprintf(buf, "%d\n", policy->stats->total_trans);
cpufreq_freq_attr_ro(total_trans);
static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
struct cpufreq_stats *stats = policy->stats;
if (policy->fast_switch_enabled)
spin_lock(&cpufreq_stats_lock);
cpufreq_stats_update(stats);
spin_unlock(&cpufreq_stats_lock);
for (i = 0; i < stats->state_num; i++) {
len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
jiffies_64_to_clock_t(stats->time_in_state[i]));
cpufreq_freq_attr_ro(time_in_state);
static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
cpufreq_stats_clear_table(policy->stats);
cpufreq_freq_attr_wo(reset);
static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
struct cpufreq_stats *stats = policy->stats;