xref: /linux/drivers/cpufreq/cpufreq_stats.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  drivers/cpufreq/cpufreq_stats.c
4  *
5  *  Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6  *  (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
7  */
8 
9 #include <linux/cpu.h>
10 #include <linux/cpufreq.h>
11 #include <linux/module.h>
12 #include <linux/sched/clock.h>
13 #include <linux/slab.h>
14 
15 struct cpufreq_stats {
16 	unsigned int total_trans;
17 	unsigned long long last_time;
18 	unsigned int max_state;
19 	unsigned int state_num;
20 	unsigned int last_index;
21 	u64 *time_in_state;
22 	unsigned int *freq_table;
23 	unsigned int *trans_table;
24 
25 	/* Deferred reset */
26 	unsigned int reset_pending;
27 	unsigned long long reset_time;
28 };
29 
30 static void cpufreq_stats_update(struct cpufreq_stats *stats,
31 				 unsigned long long time)
32 {
33 	unsigned long long cur_time = local_clock();
34 
35 	stats->time_in_state[stats->last_index] += cur_time - time;
36 	stats->last_time = cur_time;
37 }
38 
39 static void cpufreq_stats_reset_table(struct cpufreq_stats *stats)
40 {
41 	unsigned int count = stats->max_state;
42 
43 	memset(stats->time_in_state, 0, count * sizeof(u64));
44 	memset(stats->trans_table, 0, count * count * sizeof(int));
45 	stats->last_time = local_clock();
46 	stats->total_trans = 0;
47 
48 	/* Adjust for the time elapsed since reset was requested */
49 	WRITE_ONCE(stats->reset_pending, 0);
50 	/*
51 	 * Prevent the reset_time read from being reordered before the
52 	 * reset_pending accesses in cpufreq_stats_record_transition().
53 	 */
54 	smp_rmb();
55 
56 	if (stats->last_index == -1)
57 		return;
58 
59 	cpufreq_stats_update(stats, READ_ONCE(stats->reset_time));
60 }
61 
62 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
63 {
64 	struct cpufreq_stats *stats = policy->stats;
65 
66 	if (READ_ONCE(stats->reset_pending))
67 		return sprintf(buf, "%d\n", 0);
68 	else
69 		return sprintf(buf, "%u\n", stats->total_trans);
70 }
71 cpufreq_freq_attr_ro(total_trans);
72 
73 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
74 {
75 	struct cpufreq_stats *stats = policy->stats;
76 	bool pending = READ_ONCE(stats->reset_pending);
77 	unsigned long long time;
78 	ssize_t len = 0;
79 	int i;
80 
81 	for (i = 0; i < stats->state_num; i++) {
82 		if (pending) {
83 			if (i == stats->last_index) {
84 				/*
85 				 * Prevent the reset_time read from occurring
86 				 * before the reset_pending read above.
87 				 */
88 				smp_rmb();
89 				time = local_clock() - READ_ONCE(stats->reset_time);
90 			} else {
91 				time = 0;
92 			}
93 		} else {
94 			time = stats->time_in_state[i];
95 			if (i == stats->last_index)
96 				time += local_clock() - stats->last_time;
97 		}
98 
99 		len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
100 			       nsec_to_clock_t(time));
101 	}
102 	return len;
103 }
104 cpufreq_freq_attr_ro(time_in_state);
105 
106 /* We don't care what is written to the attribute */
107 static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
108 			   size_t count)
109 {
110 	struct cpufreq_stats *stats = policy->stats;
111 
112 	/*
113 	 * Defer resetting of stats to cpufreq_stats_record_transition() to
114 	 * avoid races.
115 	 */
116 	WRITE_ONCE(stats->reset_time, local_clock());
117 	/*
118 	 * The memory barrier below is to prevent the readers of reset_time from
119 	 * seeing a stale or partially updated value.
120 	 */
121 	smp_wmb();
122 	WRITE_ONCE(stats->reset_pending, 1);
123 
124 	return count;
125 }
126 cpufreq_freq_attr_wo(reset);
127 
128 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
129 {
130 	struct cpufreq_stats *stats = policy->stats;
131 	bool pending = READ_ONCE(stats->reset_pending);
132 	ssize_t len = 0;
133 	int i, j, count;
134 
135 	len += sysfs_emit_at(buf, len, "   From  :    To\n");
136 	len += sysfs_emit_at(buf, len, "         : ");
137 	for (i = 0; i < stats->state_num; i++) {
138 		if (len >= PAGE_SIZE - 1)
139 			break;
140 		len += sysfs_emit_at(buf, len, "%9u ", stats->freq_table[i]);
141 	}
142 	if (len >= PAGE_SIZE - 1)
143 		return PAGE_SIZE - 1;
144 
145 	len += sysfs_emit_at(buf, len, "\n");
146 
147 	for (i = 0; i < stats->state_num; i++) {
148 		if (len >= PAGE_SIZE - 1)
149 			break;
150 
151 		len += sysfs_emit_at(buf, len, "%9u: ", stats->freq_table[i]);
152 
153 		for (j = 0; j < stats->state_num; j++) {
154 			if (len >= PAGE_SIZE - 1)
155 				break;
156 
157 			if (pending)
158 				count = 0;
159 			else
160 				count = stats->trans_table[i * stats->max_state + j];
161 
162 			len += sysfs_emit_at(buf, len, "%9u ", count);
163 		}
164 		if (len >= PAGE_SIZE - 1)
165 			break;
166 		len += sysfs_emit_at(buf, len, "\n");
167 	}
168 
169 	if (len >= PAGE_SIZE - 1) {
170 		pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
171 		return -EFBIG;
172 	}
173 	return len;
174 }
175 cpufreq_freq_attr_ro(trans_table);
176 
177 static struct attribute *default_attrs[] = {
178 	&total_trans.attr,
179 	&time_in_state.attr,
180 	&reset.attr,
181 	&trans_table.attr,
182 	NULL
183 };
184 static const struct attribute_group stats_attr_group = {
185 	.attrs = default_attrs,
186 	.name = "stats"
187 };
188 
189 static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
190 {
191 	int index;
192 	for (index = 0; index < stats->max_state; index++)
193 		if (stats->freq_table[index] == freq)
194 			return index;
195 	return -1;
196 }
197 
198 void cpufreq_stats_free_table(struct cpufreq_policy *policy)
199 {
200 	struct cpufreq_stats *stats = policy->stats;
201 
202 	/* Already freed */
203 	if (!stats)
204 		return;
205 
206 	pr_debug("%s: Free stats table\n", __func__);
207 
208 	sysfs_remove_group(&policy->kobj, &stats_attr_group);
209 	kfree(stats->time_in_state);
210 	kfree(stats);
211 	policy->stats = NULL;
212 }
213 
214 void cpufreq_stats_create_table(struct cpufreq_policy *policy)
215 {
216 	unsigned int i = 0, count;
217 	struct cpufreq_stats *stats;
218 	unsigned int alloc_size;
219 	struct cpufreq_frequency_table *pos;
220 
221 	count = cpufreq_table_count_valid_entries(policy);
222 	if (!count)
223 		return;
224 
225 	/* stats already initialized */
226 	if (policy->stats)
227 		return;
228 
229 	stats = kzalloc_obj(*stats);
230 	if (!stats)
231 		return;
232 
233 	alloc_size = count * sizeof(int) + count * sizeof(u64);
234 
235 	alloc_size += count * count * sizeof(int);
236 
237 	/* Allocate memory for time_in_state/freq_table/trans_table in one go */
238 	stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
239 	if (!stats->time_in_state)
240 		goto free_stat;
241 
242 	stats->freq_table = (unsigned int *)(stats->time_in_state + count);
243 
244 	stats->trans_table = stats->freq_table + count;
245 
246 	stats->max_state = count;
247 
248 	/* Find valid-unique entries */
249 	cpufreq_for_each_valid_entry(pos, policy->freq_table)
250 		if (policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED ||
251 		    freq_table_get_index(stats, pos->frequency) == -1)
252 			stats->freq_table[i++] = pos->frequency;
253 
254 	stats->state_num = i;
255 	stats->last_time = local_clock();
256 	stats->last_index = freq_table_get_index(stats, policy->cur);
257 
258 	policy->stats = stats;
259 	if (!sysfs_create_group(&policy->kobj, &stats_attr_group))
260 		return;
261 
262 	/* We failed, release resources */
263 	policy->stats = NULL;
264 	kfree(stats->time_in_state);
265 free_stat:
266 	kfree(stats);
267 }
268 
269 void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
270 				     unsigned int new_freq)
271 {
272 	struct cpufreq_stats *stats = policy->stats;
273 	int old_index, new_index;
274 
275 	if (unlikely(!stats))
276 		return;
277 
278 	if (unlikely(READ_ONCE(stats->reset_pending)))
279 		cpufreq_stats_reset_table(stats);
280 
281 	old_index = stats->last_index;
282 	new_index = freq_table_get_index(stats, new_freq);
283 
284 	/* We can't do stats->time_in_state[-1]= .. */
285 	if (unlikely(old_index == -1 || new_index == -1 || old_index == new_index))
286 		return;
287 
288 	cpufreq_stats_update(stats, stats->last_time);
289 
290 	stats->last_index = new_index;
291 	stats->trans_table[old_index * stats->max_state + new_index]++;
292 	stats->total_trans++;
293 }
294