]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/cpufreq/cpufreq_stats.c
cpufreq: Clean up header files included in the core
[sojka/nv-tegra/linux-3.10.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 <asm/cputime.h>
17
18 static spinlock_t cpufreq_stats_lock;
19
20 struct cpufreq_stats {
21         unsigned int cpu;
22         unsigned int total_trans;
23         unsigned long long  last_time;
24         unsigned int max_state;
25         unsigned int state_num;
26         unsigned int last_index;
27         u64 *time_in_state;
28         unsigned int *freq_table;
29 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
30         unsigned int *trans_table;
31 #endif
32 };
33
34 static DEFINE_PER_CPU(struct cpufreq_stats *, cpufreq_stats_table);
35
36 struct cpufreq_stats_attribute {
37         struct attribute attr;
38         ssize_t(*show) (struct cpufreq_stats *, char *);
39 };
40
41 static int cpufreq_stats_update(unsigned int cpu)
42 {
43         struct cpufreq_stats *stat;
44         unsigned long long cur_time;
45
46         cur_time = get_jiffies_64();
47         spin_lock(&cpufreq_stats_lock);
48         stat = per_cpu(cpufreq_stats_table, cpu);
49         if (!stat) {
50                 spin_unlock(&cpufreq_stats_lock);
51                 return 0;
52         }
53
54         if (stat->time_in_state && stat->last_index >= 0)
55                 stat->time_in_state[stat->last_index] +=
56                         cur_time - stat->last_time;
57         stat->last_time = cur_time;
58         spin_unlock(&cpufreq_stats_lock);
59         return 0;
60 }
61
62 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
63 {
64         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
65         if (!stat)
66                 return 0;
67         return sprintf(buf, "%d\n",
68                         per_cpu(cpufreq_stats_table, stat->cpu)->total_trans);
69 }
70
71 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
72 {
73         ssize_t len = 0;
74         int i;
75         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
76         if (!stat)
77                 return 0;
78         cpufreq_stats_update(stat->cpu);
79         for (i = 0; i < stat->state_num; i++) {
80                 len += sprintf(buf + len, "%u %llu\n", stat->freq_table[i],
81                         (unsigned long long)
82                         cputime64_to_clock_t(stat->time_in_state[i]));
83         }
84         return len;
85 }
86
87 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
88 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
89 {
90         ssize_t len = 0;
91         int i, j;
92
93         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table, policy->cpu);
94         if (!stat)
95                 return 0;
96         cpufreq_stats_update(stat->cpu);
97         len += snprintf(buf + len, PAGE_SIZE - len, "   From  :    To\n");
98         len += snprintf(buf + len, PAGE_SIZE - len, "         : ");
99         for (i = 0; i < stat->state_num; i++) {
100                 if (len >= PAGE_SIZE)
101                         break;
102                 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
103                                 stat->freq_table[i]);
104         }
105         if (len >= PAGE_SIZE)
106                 return PAGE_SIZE;
107
108         len += snprintf(buf + len, PAGE_SIZE - len, "\n");
109
110         for (i = 0; i < stat->state_num; i++) {
111                 if (len >= PAGE_SIZE)
112                         break;
113
114                 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
115                                 stat->freq_table[i]);
116
117                 for (j = 0; j < stat->state_num; j++)   {
118                         if (len >= PAGE_SIZE)
119                                 break;
120                         len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
121                                         stat->trans_table[i*stat->max_state+j]);
122                 }
123                 if (len >= PAGE_SIZE)
124                         break;
125                 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
126         }
127         if (len >= PAGE_SIZE)
128                 return PAGE_SIZE;
129         return len;
130 }
131 cpufreq_freq_attr_ro(trans_table);
132 #endif
133
134 cpufreq_freq_attr_ro(total_trans);
135 cpufreq_freq_attr_ro(time_in_state);
136
137 static struct attribute *default_attrs[] = {
138         &total_trans.attr,
139         &time_in_state.attr,
140 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
141         &trans_table.attr,
142 #endif
143         NULL
144 };
145 static struct attribute_group stats_attr_group = {
146         .attrs = default_attrs,
147         .name = "stats"
148 };
149
150 static int freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
151 {
152         int index;
153         for (index = 0; index < stat->state_num; index++)
154                 if (stat->freq_table[index] > freq)
155                         break;
156         return index - 1; /* below lowest freq in table: return -1 */
157 }
158
159 /* should be called late in the CPU removal sequence so that the stats
160  * memory is still available in case someone tries to use it.
161  */
162 static void cpufreq_stats_free_table(unsigned int cpu)
163 {
164         struct cpufreq_stats *stat;
165
166         spin_lock(&cpufreq_stats_lock);
167         stat = per_cpu(cpufreq_stats_table, cpu);
168         per_cpu(cpufreq_stats_table, cpu) = NULL;
169         spin_unlock(&cpufreq_stats_lock);
170
171         if (stat) {
172                 pr_debug("%s: Free stat table\n", __func__);
173                 kfree(stat->time_in_state);
174                 kfree(stat);
175                 per_cpu(cpufreq_stats_table, cpu) = NULL;
176         }
177 }
178
179 /* must be called early in the CPU removal sequence (before
180  * cpufreq_remove_dev) so that policy is still valid.
181  */
182 static void cpufreq_stats_free_sysfs(unsigned int cpu)
183 {
184         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
185
186         if (!policy)
187                 return;
188
189         if (!cpufreq_frequency_get_table(cpu))
190                 goto put_ref;
191
192         if (!policy_is_shared(policy)) {
193                 pr_debug("%s: Free sysfs stat\n", __func__);
194                 sysfs_remove_group(&policy->kobj, &stats_attr_group);
195         }
196
197 put_ref:
198         cpufreq_cpu_put(policy);
199 }
200
201 static int cpufreq_stats_create_table(struct cpufreq_policy *policy,
202                 struct cpufreq_frequency_table *table)
203 {
204         unsigned int i, j, k, l, count = 0, ret = 0;
205         struct cpufreq_stats *stat;
206         struct cpufreq_policy *data;
207         unsigned int alloc_size;
208         unsigned int cpu = policy->cpu;
209         if (per_cpu(cpufreq_stats_table, cpu))
210                 return -EBUSY;
211         stat = kzalloc(sizeof(struct cpufreq_stats), GFP_KERNEL);
212         if ((stat) == NULL)
213                 return -ENOMEM;
214
215         data = cpufreq_cpu_get(cpu);
216         if (data == NULL) {
217                 ret = -EINVAL;
218                 goto error_get_fail;
219         }
220
221         ret = sysfs_create_group(&data->kobj, &stats_attr_group);
222         if (ret)
223                 goto error_out;
224
225         stat->cpu = cpu;
226         per_cpu(cpufreq_stats_table, cpu) = stat;
227
228         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
229                 unsigned int freq = table[i].frequency;
230                 if (freq == CPUFREQ_ENTRY_INVALID)
231                         continue;
232                 count++;
233         }
234
235         alloc_size = count * sizeof(int) + count * sizeof(u64);
236
237 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
238         alloc_size += count * count * sizeof(int);
239 #endif
240         stat->max_state = count;
241         stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
242         if (!stat->time_in_state) {
243                 ret = -ENOMEM;
244                 goto error_out;
245         }
246         stat->freq_table = (unsigned int *)(stat->time_in_state + count);
247
248 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
249         stat->trans_table = stat->freq_table + count;
250 #endif
251         j = 0;
252         for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
253                 unsigned int freq = table[i].frequency;
254                 if (freq == CPUFREQ_ENTRY_INVALID)
255                         continue;
256
257                 /* Insert in sorted stat->freq_table */
258                 for (k = 0; k < j && stat->freq_table[k] < freq; k++)
259                         ;
260                 if (stat->freq_table[k] == freq)
261                         continue;
262                 for (l = j; l > k; l--)
263                         stat->freq_table[l] = stat->freq_table[l - 1];
264                 stat->freq_table[k] = freq;
265                 j++;
266         }
267         stat->state_num = j;
268         spin_lock(&cpufreq_stats_lock);
269         stat->last_time = get_jiffies_64();
270         stat->last_index = freq_table_get_index(stat, policy->cur);
271         spin_unlock(&cpufreq_stats_lock);
272         cpufreq_cpu_put(data);
273         return 0;
274 error_out:
275         cpufreq_cpu_put(data);
276 error_get_fail:
277         kfree(stat);
278         per_cpu(cpufreq_stats_table, cpu) = NULL;
279         return ret;
280 }
281
282 static void cpufreq_stats_update_policy_cpu(struct cpufreq_policy *policy)
283 {
284         struct cpufreq_stats *stat = per_cpu(cpufreq_stats_table,
285                         policy->last_cpu);
286
287         pr_debug("Updating stats_table for new_cpu %u from last_cpu %u\n",
288                         policy->cpu, policy->last_cpu);
289         per_cpu(cpufreq_stats_table, policy->cpu) = per_cpu(cpufreq_stats_table,
290                         policy->last_cpu);
291         per_cpu(cpufreq_stats_table, policy->last_cpu) = NULL;
292         stat->cpu = policy->cpu;
293 }
294
295 static int cpufreq_stat_notifier_policy(struct notifier_block *nb,
296                 unsigned long val, void *data)
297 {
298         int ret;
299         struct cpufreq_policy *policy = data;
300         struct cpufreq_frequency_table *table;
301         unsigned int cpu = policy->cpu;
302
303         if (val == CPUFREQ_UPDATE_POLICY_CPU) {
304                 cpufreq_stats_update_policy_cpu(policy);
305                 return 0;
306         }
307
308         if (val != CPUFREQ_NOTIFY)
309                 return 0;
310         table = cpufreq_frequency_get_table(cpu);
311         if (!table)
312                 return 0;
313         ret = cpufreq_stats_create_table(policy, table);
314         if (ret)
315                 return ret;
316         return 0;
317 }
318
319 static int cpufreq_stat_notifier_trans(struct notifier_block *nb,
320                 unsigned long val, void *data)
321 {
322         struct cpufreq_freqs *freq = data;
323         struct cpufreq_stats *stat;
324         int old_index, new_index;
325
326         if (val != CPUFREQ_POSTCHANGE)
327                 return 0;
328
329         cpufreq_stats_update(freq->cpu);
330
331         spin_lock(&cpufreq_stats_lock);
332         stat = per_cpu(cpufreq_stats_table, freq->cpu);
333         if (!stat) {
334                 spin_unlock(&cpufreq_stats_lock);
335                 return 0;
336         }
337
338         old_index = stat->last_index;
339         new_index = freq_table_get_index(stat, freq->new);
340
341         if (old_index == new_index) {
342                 spin_unlock(&cpufreq_stats_lock);
343                 return 0;
344         }
345
346         stat->last_index = new_index;
347 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
348         if (old_index >= 0 && new_index >= 0)
349                 stat->trans_table[old_index * stat->max_state + new_index]++;
350 #endif
351         stat->total_trans++;
352         spin_unlock(&cpufreq_stats_lock);
353         return 0;
354 }
355
356 static int cpufreq_stats_create_table_cpu(unsigned int cpu)
357 {
358         struct cpufreq_policy *policy;
359         struct cpufreq_frequency_table *table;
360         int ret = -ENODEV;
361
362         policy = cpufreq_cpu_get(cpu);
363         if (!policy)
364                 return -ENODEV;
365
366         table = cpufreq_frequency_get_table(cpu);
367         if (!table)
368                 goto out;
369
370         ret = cpufreq_stats_create_table(policy, table);
371
372 out:
373         cpufreq_cpu_put(policy);
374         return ret;
375 }
376
377 static int __cpuinit cpufreq_stat_cpu_callback(struct notifier_block *nfb,
378                                                unsigned long action,
379                                                void *hcpu)
380 {
381         unsigned int cpu = (unsigned long)hcpu;
382
383         switch (action) {
384         case CPU_ONLINE:
385         case CPU_ONLINE_FROZEN:
386                 cpufreq_update_policy(cpu);
387                 break;
388         case CPU_DOWN_PREPARE:
389         case CPU_DOWN_PREPARE_FROZEN:
390                 cpufreq_stats_free_sysfs(cpu);
391                 break;
392         case CPU_DEAD:
393         case CPU_DEAD_FROZEN:
394                 cpufreq_stats_free_table(cpu);
395                 break;
396         case CPU_DOWN_FAILED:
397         case CPU_DOWN_FAILED_FROZEN:
398                 cpufreq_stats_create_table_cpu(cpu);
399                 break;
400         }
401         return NOTIFY_OK;
402 }
403
404 /* priority=1 so this will get called before cpufreq_remove_dev */
405 static struct notifier_block cpufreq_stat_cpu_notifier __refdata = {
406         .notifier_call = cpufreq_stat_cpu_callback,
407         .priority = 1,
408 };
409
410 static struct notifier_block notifier_policy_block = {
411         .notifier_call = cpufreq_stat_notifier_policy
412 };
413
414 static struct notifier_block notifier_trans_block = {
415         .notifier_call = cpufreq_stat_notifier_trans
416 };
417
418 static int __init cpufreq_stats_init(void)
419 {
420         int ret;
421         unsigned int cpu;
422
423         spin_lock_init(&cpufreq_stats_lock);
424         ret = cpufreq_register_notifier(&notifier_policy_block,
425                                 CPUFREQ_POLICY_NOTIFIER);
426         if (ret)
427                 return ret;
428
429         register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
430         for_each_online_cpu(cpu)
431                 cpufreq_update_policy(cpu);
432
433         ret = cpufreq_register_notifier(&notifier_trans_block,
434                                 CPUFREQ_TRANSITION_NOTIFIER);
435         if (ret) {
436                 cpufreq_unregister_notifier(&notifier_policy_block,
437                                 CPUFREQ_POLICY_NOTIFIER);
438                 unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
439                 for_each_online_cpu(cpu)
440                         cpufreq_stats_free_table(cpu);
441                 return ret;
442         }
443
444         return 0;
445 }
446 static void __exit cpufreq_stats_exit(void)
447 {
448         unsigned int cpu;
449
450         cpufreq_unregister_notifier(&notifier_policy_block,
451                         CPUFREQ_POLICY_NOTIFIER);
452         cpufreq_unregister_notifier(&notifier_trans_block,
453                         CPUFREQ_TRANSITION_NOTIFIER);
454         unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
455         for_each_online_cpu(cpu) {
456                 cpufreq_stats_free_table(cpu);
457                 cpufreq_stats_free_sysfs(cpu);
458         }
459 }
460
461 MODULE_AUTHOR("Zou Nan hai <nanhai.zou@intel.com>");
462 MODULE_DESCRIPTION("'cpufreq_stats' - A driver to export cpufreq stats "
463                                 "through sysfs filesystem");
464 MODULE_LICENSE("GPL");
465
466 module_init(cpufreq_stats_init);
467 module_exit(cpufreq_stats_exit);