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