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