cpufreq: stats: don't update stats on false notifiers
[deliverable/linux.git] / drivers / cpufreq / cpufreq_stats.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/cpufreq/cpufreq_stats.c
3 *
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
0a829c5a 5 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
1da177e4
LT
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
1da177e4 12#include <linux/cpu.h>
1da177e4 13#include <linux/cpufreq.h>
5c720d37 14#include <linux/module.h>
5ff0a268 15#include <linux/slab.h>
bfc3f028 16#include <linux/cputime.h>
1da177e4
LT
17
18static spinlock_t cpufreq_stats_lock;
19
1da177e4 20struct cpufreq_stats {
1da177e4 21 unsigned int total_trans;
bb176f7d 22 unsigned long long last_time;
1da177e4
LT
23 unsigned int max_state;
24 unsigned int state_num;
25 unsigned int last_index;
1e7586a1 26 u64 *time_in_state;
1da177e4
LT
27 unsigned int *freq_table;
28#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
29 unsigned int *trans_table;
30#endif
31};
32
50941607 33static int cpufreq_stats_update(struct cpufreq_stats *stats)
1da177e4 34{
9531347c 35 unsigned long long cur_time = get_jiffies_64();
58f1df25 36
1da177e4 37 spin_lock(&cpufreq_stats_lock);
c960f9b2 38 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
50941607 39 stats->last_time = cur_time;
1da177e4
LT
40 spin_unlock(&cpufreq_stats_lock);
41 return 0;
42}
43
0a829c5a 44static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
1da177e4 45{
a9aaf291 46 return sprintf(buf, "%d\n", policy->stats->total_trans);
1da177e4
LT
47}
48
0a829c5a 49static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
1da177e4 50{
50941607 51 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
52 ssize_t len = 0;
53 int i;
a9aaf291 54
50941607
VK
55 cpufreq_stats_update(stats);
56 for (i = 0; i < stats->state_num; i++) {
57 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
0a829c5a 58 (unsigned long long)
50941607 59 jiffies_64_to_clock_t(stats->time_in_state[i]));
1da177e4
LT
60 }
61 return len;
62}
63
64#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
0a829c5a 65static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
1da177e4 66{
50941607 67 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
68 ssize_t len = 0;
69 int i, j;
70
58f1df25
VP
71 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
72 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
50941607 73 for (i = 0; i < stats->state_num; i++) {
58f1df25
VP
74 if (len >= PAGE_SIZE)
75 break;
76 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 77 stats->freq_table[i]);
58f1df25
VP
78 }
79 if (len >= PAGE_SIZE)
25aca347 80 return PAGE_SIZE;
58f1df25
VP
81
82 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
83
50941607 84 for (i = 0; i < stats->state_num; i++) {
1da177e4
LT
85 if (len >= PAGE_SIZE)
86 break;
58f1df25
VP
87
88 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
50941607 89 stats->freq_table[i]);
1da177e4 90
50941607 91 for (j = 0; j < stats->state_num; j++) {
1da177e4
LT
92 if (len >= PAGE_SIZE)
93 break;
58f1df25 94 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 95 stats->trans_table[i*stats->max_state+j]);
1da177e4 96 }
25aca347
CEB
97 if (len >= PAGE_SIZE)
98 break;
1da177e4
LT
99 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
100 }
25aca347
CEB
101 if (len >= PAGE_SIZE)
102 return PAGE_SIZE;
1da177e4
LT
103 return len;
104}
df18e504 105cpufreq_freq_attr_ro(trans_table);
1da177e4
LT
106#endif
107
df18e504
VK
108cpufreq_freq_attr_ro(total_trans);
109cpufreq_freq_attr_ro(time_in_state);
1da177e4
LT
110
111static struct attribute *default_attrs[] = {
df18e504
VK
112 &total_trans.attr,
113 &time_in_state.attr,
1da177e4 114#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
df18e504 115 &trans_table.attr,
1da177e4
LT
116#endif
117 NULL
118};
119static struct attribute_group stats_attr_group = {
120 .attrs = default_attrs,
121 .name = "stats"
122};
123
50941607 124static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
1da177e4
LT
125{
126 int index;
50941607
VK
127 for (index = 0; index < stats->max_state; index++)
128 if (stats->freq_table[index] == freq)
1da177e4
LT
129 return index;
130 return -1;
131}
132
2d13594d 133static void __cpufreq_stats_free_table(struct cpufreq_policy *policy)
1da177e4 134{
50941607 135 struct cpufreq_stats *stats = policy->stats;
b8eed8af 136
a9aaf291 137 /* Already freed */
50941607 138 if (!stats)
2d13594d
VK
139 return;
140
50941607 141 pr_debug("%s: Free stats table\n", __func__);
2d13594d
VK
142
143 sysfs_remove_group(&policy->kobj, &stats_attr_group);
50941607
VK
144 kfree(stats->time_in_state);
145 kfree(stats);
a9aaf291 146 policy->stats = NULL;
98586ed8 147}
148
2d13594d 149static void cpufreq_stats_free_table(unsigned int cpu)
98586ed8 150{
2d13594d 151 struct cpufreq_policy *policy;
633d47d6 152
2d13594d 153 policy = cpufreq_cpu_get(cpu);
187da1d9 154 if (!policy)
633d47d6
DB
155 return;
156
f93dbbbd 157 __cpufreq_stats_free_table(policy);
187da1d9 158
187da1d9 159 cpufreq_cpu_put(policy);
1da177e4
LT
160}
161
ad4c2302 162static int __cpufreq_stats_create_table(struct cpufreq_policy *policy)
1da177e4 163{
a685c6d0 164 unsigned int i = 0, count = 0, ret = -ENOMEM;
50941607 165 struct cpufreq_stats *stats;
1da177e4
LT
166 unsigned int alloc_size;
167 unsigned int cpu = policy->cpu;
041526f9 168 struct cpufreq_frequency_table *pos, *table;
ad4c2302 169
a685c6d0 170 /* We need cpufreq table for creating stats table */
ad4c2302
SK
171 table = cpufreq_frequency_get_table(cpu);
172 if (unlikely(!table))
173 return 0;
174
b8c67448 175 /* stats already initialized */
a9aaf291 176 if (policy->stats)
b8c67448
VK
177 return -EEXIST;
178
50941607 179 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
a685c6d0 180 if (!stats)
1da177e4 181 return -ENOMEM;
1da177e4 182
a685c6d0 183 /* Find total allocation size */
041526f9 184 cpufreq_for_each_valid_entry(pos, table)
1da177e4 185 count++;
1da177e4 186
1e7586a1 187 alloc_size = count * sizeof(int) + count * sizeof(u64);
1da177e4
LT
188
189#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
190 alloc_size += count * count * sizeof(int);
191#endif
a685c6d0
VK
192
193 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
50941607 194 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
a685c6d0
VK
195 if (!stats->time_in_state)
196 goto free_stat;
197
50941607 198 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
1da177e4
LT
199
200#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50941607 201 stats->trans_table = stats->freq_table + count;
1da177e4 202#endif
a685c6d0
VK
203
204 stats->max_state = count;
205
206 /* Find valid-unique entries */
041526f9 207 cpufreq_for_each_valid_entry(pos, table)
50941607
VK
208 if (freq_table_get_index(stats, pos->frequency) == -1)
209 stats->freq_table[i++] = pos->frequency;
210 stats->state_num = i;
a685c6d0 211
1da177e4 212 spin_lock(&cpufreq_stats_lock);
50941607
VK
213 stats->last_time = get_jiffies_64();
214 stats->last_index = freq_table_get_index(stats, policy->cur);
1da177e4 215 spin_unlock(&cpufreq_stats_lock);
a685c6d0
VK
216
217 policy->stats = stats;
218 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
219 if (!ret)
220 return 0;
221
222 /* We failed, release resources */
a9aaf291 223 policy->stats = NULL;
a685c6d0
VK
224 kfree(stats->time_in_state);
225free_stat:
226 kfree(stats);
227
1da177e4
LT
228 return ret;
229}
230
b3f9ff88
VK
231static void cpufreq_stats_create_table(unsigned int cpu)
232{
233 struct cpufreq_policy *policy;
b3f9ff88
VK
234
235 /*
236 * "likely(!policy)" because normally cpufreq_stats will be registered
237 * before cpufreq driver
238 */
239 policy = cpufreq_cpu_get(cpu);
240 if (likely(!policy))
241 return;
242
ad4c2302 243 __cpufreq_stats_create_table(policy);
b3f9ff88
VK
244
245 cpufreq_cpu_put(policy);
246}
247
0a829c5a
DJ
248static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
249 unsigned long val, void *data)
1da177e4 250{
fcd7af91 251 int ret = 0;
1da177e4 252 struct cpufreq_policy *policy = data;
b8eed8af 253
fcd7af91 254 if (val == CPUFREQ_CREATE_POLICY)
ad4c2302 255 ret = __cpufreq_stats_create_table(policy);
2d13594d
VK
256 else if (val == CPUFREQ_REMOVE_POLICY)
257 __cpufreq_stats_free_table(policy);
fcd7af91
VK
258
259 return ret;
1da177e4
LT
260}
261
0a829c5a
DJ
262static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
263 unsigned long val, void *data)
1da177e4
LT
264{
265 struct cpufreq_freqs *freq = data;
a9aaf291 266 struct cpufreq_policy *policy = cpufreq_cpu_get(freq->cpu);
50941607 267 struct cpufreq_stats *stats;
1da177e4
LT
268 int old_index, new_index;
269
a9aaf291
VK
270 if (!policy) {
271 pr_err("%s: No policy found\n", __func__);
1da177e4 272 return 0;
a9aaf291 273 }
1da177e4 274
a9aaf291
VK
275 if (val != CPUFREQ_POSTCHANGE)
276 goto put_policy;
277
278 if (!policy->stats) {
279 pr_debug("%s: No stats found\n", __func__);
280 goto put_policy;
281 }
282
50941607 283 stats = policy->stats;
8edc59d9 284
50941607
VK
285 old_index = stats->last_index;
286 new_index = freq_table_get_index(stats, freq->new);
1da177e4 287
50941607 288 /* We can't do stats->time_in_state[-1]= .. */
46a310b8 289 if (old_index == -1 || new_index == -1)
a9aaf291 290 goto put_policy;
1da177e4 291
46a310b8 292 if (old_index == new_index)
a9aaf291 293 goto put_policy;
8edc59d9 294
e7347694
VK
295 cpufreq_stats_update(stats);
296
1da177e4 297 spin_lock(&cpufreq_stats_lock);
50941607 298 stats->last_index = new_index;
1da177e4 299#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50941607 300 stats->trans_table[old_index * stats->max_state + new_index]++;
1da177e4 301#endif
50941607 302 stats->total_trans++;
1da177e4 303 spin_unlock(&cpufreq_stats_lock);
a9aaf291
VK
304
305put_policy:
306 cpufreq_cpu_put(policy);
1da177e4
LT
307 return 0;
308}
309
310static struct notifier_block notifier_policy_block = {
311 .notifier_call = cpufreq_stat_notifier_policy
312};
313
314static struct notifier_block notifier_trans_block = {
315 .notifier_call = cpufreq_stat_notifier_trans
316};
317
0a829c5a 318static int __init cpufreq_stats_init(void)
1da177e4
LT
319{
320 int ret;
321 unsigned int cpu;
c32b6b8e 322
1da177e4 323 spin_lock_init(&cpufreq_stats_lock);
0a829c5a
DJ
324 ret = cpufreq_register_notifier(&notifier_policy_block,
325 CPUFREQ_POLICY_NOTIFIER);
326 if (ret)
1da177e4
LT
327 return ret;
328
b3f9ff88
VK
329 for_each_online_cpu(cpu)
330 cpufreq_stats_create_table(cpu);
331
0a829c5a
DJ
332 ret = cpufreq_register_notifier(&notifier_trans_block,
333 CPUFREQ_TRANSITION_NOTIFIER);
334 if (ret) {
1da177e4
LT
335 cpufreq_unregister_notifier(&notifier_policy_block,
336 CPUFREQ_POLICY_NOTIFIER);
56836fb4
KK
337 for_each_online_cpu(cpu)
338 cpufreq_stats_free_table(cpu);
1da177e4
LT
339 return ret;
340 }
341
1da177e4
LT
342 return 0;
343}
0a829c5a 344static void __exit cpufreq_stats_exit(void)
1da177e4
LT
345{
346 unsigned int cpu;
c32b6b8e 347
1da177e4
LT
348 cpufreq_unregister_notifier(&notifier_policy_block,
349 CPUFREQ_POLICY_NOTIFIER);
350 cpufreq_unregister_notifier(&notifier_trans_block,
351 CPUFREQ_TRANSITION_NOTIFIER);
2d13594d 352 for_each_online_cpu(cpu)
55395ae7 353 cpufreq_stats_free_table(cpu);
1da177e4
LT
354}
355
0a829c5a 356MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
00d0b294 357MODULE_DESCRIPTION("Export cpufreq stats via sysfs");
0a829c5a 358MODULE_LICENSE("GPL");
1da177e4
LT
359
360module_init(cpufreq_stats_init);
361module_exit(cpufreq_stats_exit);
This page took 0.763834 seconds and 5 git commands to generate.