Commit 40c3bd4c authored by Viresh Kumar's avatar Viresh Kumar Committed by Rafael J. Wysocki

cpufreq: stats: Defer stats update to cpufreq_stats_record_transition()

In order to prepare for lock-less stats update, add support to defer any
updates to it until cpufreq_stats_record_transition() is called.

The stats were updated from two places earlier:

- show_time_in_state(): This can be easily deferred, all we need is to
  calculate the delta duration again in this routine to show the current
  state's time-in-state.

- store_reset(): This is a bit tricky as we need to clear the stats
  here and avoid races with simultaneous call to
  cpufreq_stats_record_transition().

Fix that by deferring the reset of the stats (within the code) to the
next call to cpufreq_stats_record_transition(), but since we need to
keep showing the right stats until that time, we capture the reset
time and account for the time since last time reset was called until
the time cpufreq_stats_record_transition() update the stats.

User space will continue seeing the stats correctly, everything will
be 0 after the stats are reset, apart from the time-in-state of the
current state, until the time a frequency switch happens.
Signed-off-by: default avatarViresh Kumar <viresh.kumar@linaro.org>
[ rjw: Minor changelog edits ]
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent fccd2f0e
...@@ -22,17 +22,22 @@ struct cpufreq_stats { ...@@ -22,17 +22,22 @@ struct cpufreq_stats {
spinlock_t lock; spinlock_t lock;
unsigned int *freq_table; unsigned int *freq_table;
unsigned int *trans_table; unsigned int *trans_table;
/* Deferred reset */
unsigned int reset_pending;
unsigned long long reset_time;
}; };
static void cpufreq_stats_update(struct cpufreq_stats *stats) static void cpufreq_stats_update(struct cpufreq_stats *stats,
unsigned long long time)
{ {
unsigned long long cur_time = get_jiffies_64(); unsigned long long cur_time = get_jiffies_64();
stats->time_in_state[stats->last_index] += cur_time - stats->last_time; stats->time_in_state[stats->last_index] += cur_time - time;
stats->last_time = cur_time; stats->last_time = cur_time;
} }
static void cpufreq_stats_clear_table(struct cpufreq_stats *stats) static void cpufreq_stats_reset_table(struct cpufreq_stats *stats)
{ {
unsigned int count = stats->max_state; unsigned int count = stats->max_state;
...@@ -41,42 +46,67 @@ static void cpufreq_stats_clear_table(struct cpufreq_stats *stats) ...@@ -41,42 +46,67 @@ static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
memset(stats->trans_table, 0, count * count * sizeof(int)); memset(stats->trans_table, 0, count * count * sizeof(int));
stats->last_time = get_jiffies_64(); stats->last_time = get_jiffies_64();
stats->total_trans = 0; stats->total_trans = 0;
/* Adjust for the time elapsed since reset was requested */
WRITE_ONCE(stats->reset_pending, 0);
cpufreq_stats_update(stats, READ_ONCE(stats->reset_time));
spin_unlock(&stats->lock); spin_unlock(&stats->lock);
} }
static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf) static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
{ {
return sprintf(buf, "%d\n", policy->stats->total_trans); struct cpufreq_stats *stats = policy->stats;
if (READ_ONCE(stats->reset_pending))
return sprintf(buf, "%d\n", 0);
else
return sprintf(buf, "%d\n", stats->total_trans);
} }
cpufreq_freq_attr_ro(total_trans); cpufreq_freq_attr_ro(total_trans);
static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf) static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
{ {
struct cpufreq_stats *stats = policy->stats; struct cpufreq_stats *stats = policy->stats;
bool pending = READ_ONCE(stats->reset_pending);
unsigned long long time;
ssize_t len = 0; ssize_t len = 0;
int i; int i;
if (policy->fast_switch_enabled) if (policy->fast_switch_enabled)
return 0; return 0;
spin_lock(&stats->lock);
cpufreq_stats_update(stats);
spin_unlock(&stats->lock);
for (i = 0; i < stats->state_num; i++) { for (i = 0; i < stats->state_num; i++) {
if (pending) {
if (i == stats->last_index)
time = get_jiffies_64() - READ_ONCE(stats->reset_time);
else
time = 0;
} else {
time = stats->time_in_state[i];
if (i == stats->last_index)
time += get_jiffies_64() - stats->last_time;
}
len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i], len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
(unsigned long long) jiffies_64_to_clock_t(time));
jiffies_64_to_clock_t(stats->time_in_state[i]));
} }
return len; return len;
} }
cpufreq_freq_attr_ro(time_in_state); cpufreq_freq_attr_ro(time_in_state);
/* We don't care what is written to the attribute */
static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf, static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
size_t count) size_t count)
{ {
/* We don't care what is written to the attribute. */ struct cpufreq_stats *stats = policy->stats;
cpufreq_stats_clear_table(policy->stats);
/*
* Defer resetting of stats to cpufreq_stats_record_transition() to
* avoid races.
*/
WRITE_ONCE(stats->reset_time, get_jiffies_64());
WRITE_ONCE(stats->reset_pending, 1);
return count; return count;
} }
cpufreq_freq_attr_wo(reset); cpufreq_freq_attr_wo(reset);
...@@ -84,8 +114,9 @@ cpufreq_freq_attr_wo(reset); ...@@ -84,8 +114,9 @@ cpufreq_freq_attr_wo(reset);
static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
{ {
struct cpufreq_stats *stats = policy->stats; struct cpufreq_stats *stats = policy->stats;
bool pending = READ_ONCE(stats->reset_pending);
ssize_t len = 0; ssize_t len = 0;
int i, j; int i, j, count;
if (policy->fast_switch_enabled) if (policy->fast_switch_enabled)
return 0; return 0;
...@@ -113,8 +144,13 @@ static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) ...@@ -113,8 +144,13 @@ static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
for (j = 0; j < stats->state_num; j++) { for (j = 0; j < stats->state_num; j++) {
if (len >= PAGE_SIZE) if (len >= PAGE_SIZE)
break; break;
len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ",
stats->trans_table[i*stats->max_state+j]); if (pending)
count = 0;
else
count = stats->trans_table[i * stats->max_state + j];
len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ", count);
} }
if (len >= PAGE_SIZE) if (len >= PAGE_SIZE)
break; break;
...@@ -228,10 +264,11 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy, ...@@ -228,10 +264,11 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
struct cpufreq_stats *stats = policy->stats; struct cpufreq_stats *stats = policy->stats;
int old_index, new_index; int old_index, new_index;
if (!stats) { if (!stats)
pr_debug("%s: No stats found\n", __func__);
return; return;
}
if (unlikely(READ_ONCE(stats->reset_pending)))
cpufreq_stats_reset_table(stats);
old_index = stats->last_index; old_index = stats->last_index;
new_index = freq_table_get_index(stats, new_freq); new_index = freq_table_get_index(stats, new_freq);
...@@ -241,7 +278,7 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy, ...@@ -241,7 +278,7 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
return; return;
spin_lock(&stats->lock); spin_lock(&stats->lock);
cpufreq_stats_update(stats); cpufreq_stats_update(stats, stats->last_time);
stats->last_index = new_index; stats->last_index = new_index;
stats->trans_table[old_index * stats->max_state + new_index]++; stats->trans_table[old_index * stats->max_state + new_index]++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment