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