cpufreq_stats.c 5.99 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5
/*
 *  drivers/cpufreq/cpufreq_stats.c
 *
 *  Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6
 *  (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
Linus Torvalds's avatar
Linus Torvalds committed
7 8 9 10
 */

#include <linux/cpu.h>
#include <linux/cpufreq.h>
11
#include <linux/module.h>
12
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15 16


struct cpufreq_stats {
	unsigned int total_trans;
17
	unsigned long long last_time;
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20
	unsigned int max_state;
	unsigned int state_num;
	unsigned int last_index;
21
	u64 *time_in_state;
22
	spinlock_t lock;
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26
	unsigned int *freq_table;
	unsigned int *trans_table;
};

27
static void cpufreq_stats_update(struct cpufreq_stats *stats)
Linus Torvalds's avatar
Linus Torvalds committed
28
{
29
	unsigned long long cur_time = get_jiffies_64();
30

31
	stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
32
	stats->last_time = cur_time;
Linus Torvalds's avatar
Linus Torvalds committed
33 34
}

35 36 37 38
static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
{
	unsigned int count = stats->max_state;

39
	spin_lock(&stats->lock);
40 41 42 43
	memset(stats->time_in_state, 0, count * sizeof(u64));
	memset(stats->trans_table, 0, count * count * sizeof(int));
	stats->last_time = get_jiffies_64();
	stats->total_trans = 0;
44
	spin_unlock(&stats->lock);
45 46
}

47
static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
48
{
49
	return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds's avatar
Linus Torvalds committed
50
}
51
cpufreq_freq_attr_ro(total_trans);
Linus Torvalds's avatar
Linus Torvalds committed
52

53
static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
54
{
55
	struct cpufreq_stats *stats = policy->stats;
Linus Torvalds's avatar
Linus Torvalds committed
56 57
	ssize_t len = 0;
	int i;
58

59 60 61
	if (policy->fast_switch_enabled)
		return 0;

62
	spin_lock(&stats->lock);
63
	cpufreq_stats_update(stats);
64
	spin_unlock(&stats->lock);
65

66 67
	for (i = 0; i < stats->state_num; i++) {
		len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
68
			(unsigned long long)
69
			jiffies_64_to_clock_t(stats->time_in_state[i]));
Linus Torvalds's avatar
Linus Torvalds committed
70 71 72
	}
	return len;
}
73
cpufreq_freq_attr_ro(time_in_state);
Linus Torvalds's avatar
Linus Torvalds committed
74

75 76 77 78 79 80 81
static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
			   size_t count)
{
	/* We don't care what is written to the attribute. */
	cpufreq_stats_clear_table(policy->stats);
	return count;
}
82
cpufreq_freq_attr_wo(reset);
83

84
static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
85
{
86
	struct cpufreq_stats *stats = policy->stats;
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89
	ssize_t len = 0;
	int i, j;

90 91 92
	if (policy->fast_switch_enabled)
		return 0;

93 94
	len += snprintf(buf + len, PAGE_SIZE - len, "   From  :    To\n");
	len += snprintf(buf + len, PAGE_SIZE - len, "         : ");
95
	for (i = 0; i < stats->state_num; i++) {
96 97 98
		if (len >= PAGE_SIZE)
			break;
		len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
99
				stats->freq_table[i]);
100 101
	}
	if (len >= PAGE_SIZE)
102
		return PAGE_SIZE;
103 104 105

	len += snprintf(buf + len, PAGE_SIZE - len, "\n");

106
	for (i = 0; i < stats->state_num; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
107 108
		if (len >= PAGE_SIZE)
			break;
109 110

		len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
111
				stats->freq_table[i]);
Linus Torvalds's avatar
Linus Torvalds committed
112

113
		for (j = 0; j < stats->state_num; j++) {
Linus Torvalds's avatar
Linus Torvalds committed
114 115
			if (len >= PAGE_SIZE)
				break;
116
			len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
117
					stats->trans_table[i*stats->max_state+j]);
Linus Torvalds's avatar
Linus Torvalds committed
118
		}
119 120
		if (len >= PAGE_SIZE)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
121 122
		len += snprintf(buf + len, PAGE_SIZE - len, "\n");
	}
123 124 125 126 127

	if (len >= PAGE_SIZE) {
		pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
		return -EFBIG;
	}
Linus Torvalds's avatar
Linus Torvalds committed
128 129
	return len;
}
130
cpufreq_freq_attr_ro(trans_table);
Linus Torvalds's avatar
Linus Torvalds committed
131 132

static struct attribute *default_attrs[] = {
133 134
	&total_trans.attr,
	&time_in_state.attr,
135
	&reset.attr,
136
	&trans_table.attr,
Linus Torvalds's avatar
Linus Torvalds committed
137 138
	NULL
};
139
static const struct attribute_group stats_attr_group = {
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143
	.attrs = default_attrs,
	.name = "stats"
};

144
static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds's avatar
Linus Torvalds committed
145 146
{
	int index;
147 148
	for (index = 0; index < stats->max_state; index++)
		if (stats->freq_table[index] == freq)
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152
			return index;
	return -1;
}

153
void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds's avatar
Linus Torvalds committed
154
{
155
	struct cpufreq_stats *stats = policy->stats;
156

157
	/* Already freed */
158
	if (!stats)
159 160
		return;

161
	pr_debug("%s: Free stats table\n", __func__);
162 163

	sysfs_remove_group(&policy->kobj, &stats_attr_group);
164 165
	kfree(stats->time_in_state);
	kfree(stats);
166
	policy->stats = NULL;
167 168
}

169
void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds's avatar
Linus Torvalds committed
170
{
171
	unsigned int i = 0, count = 0, ret = -ENOMEM;
172
	struct cpufreq_stats *stats;
Linus Torvalds's avatar
Linus Torvalds committed
173
	unsigned int alloc_size;
174
	struct cpufreq_frequency_table *pos;
175

176 177
	count = cpufreq_table_count_valid_entries(policy);
	if (!count)
178
		return;
179

180
	/* stats already initialized */
181
	if (policy->stats)
182
		return;
183

184
	stats = kzalloc(sizeof(*stats), GFP_KERNEL);
185
	if (!stats)
186
		return;
Linus Torvalds's avatar
Linus Torvalds committed
187

188
	alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds's avatar
Linus Torvalds committed
189 190

	alloc_size += count * count * sizeof(int);
191 192

	/* Allocate memory for time_in_state/freq_table/trans_table in one go */
193
	stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
194 195 196
	if (!stats->time_in_state)
		goto free_stat;

197
	stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds's avatar
Linus Torvalds committed
198

199
	stats->trans_table = stats->freq_table + count;
200 201 202 203

	stats->max_state = count;

	/* Find valid-unique entries */
204
	cpufreq_for_each_valid_entry(pos, policy->freq_table)
205 206
		if (freq_table_get_index(stats, pos->frequency) == -1)
			stats->freq_table[i++] = pos->frequency;
207

208
	stats->state_num = i;
209 210
	stats->last_time = get_jiffies_64();
	stats->last_index = freq_table_get_index(stats, policy->cur);
211
	spin_lock_init(&stats->lock);
212 213 214 215

	policy->stats = stats;
	ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
	if (!ret)
216
		return;
217 218

	/* We failed, release resources */
219
	policy->stats = NULL;
220 221 222
	kfree(stats->time_in_state);
free_stat:
	kfree(stats);
223 224
}

225 226
void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
				     unsigned int new_freq)
Linus Torvalds's avatar
Linus Torvalds committed
227
{
228
	struct cpufreq_stats *stats = policy->stats;
Linus Torvalds's avatar
Linus Torvalds committed
229 230
	int old_index, new_index;

231
	if (!stats) {
232
		pr_debug("%s: No stats found\n", __func__);
233
		return;
234 235
	}

236
	old_index = stats->last_index;
237
	new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds's avatar
Linus Torvalds committed
238

239
	/* We can't do stats->time_in_state[-1]= .. */
240 241
	if (old_index == -1 || new_index == -1 || old_index == new_index)
		return;
242

243
	spin_lock(&stats->lock);
244 245
	cpufreq_stats_update(stats);

246 247 248
	stats->last_index = new_index;
	stats->trans_table[old_index * stats->max_state + new_index]++;
	stats->total_trans++;
249
	spin_unlock(&stats->lock);
Linus Torvalds's avatar
Linus Torvalds committed
250
}