OSDN Git Service

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