2 * drivers/cpufreq/cpufreq_stats.c
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
5 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
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.
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sysdev.h>
15 #include <linux/cpu.h>
16 #include <linux/sysfs.h>
17 #include <linux/cpufreq.h>
18 #include <linux/jiffies.h>
19 #include <linux/percpu.h>
20 #include <linux/kobject.h>
21 #include <linux/spinlock.h>
23 static spinlock_t cpufreq_stats_lock
;
25 #define CPUFREQ_STATDEVICE_ATTR(_name,_mode,_show) \
26 static struct freq_attr _attr_##_name = {\
27 .attr = {.name = __stringify(_name), .owner = THIS_MODULE, \
33 delta_time(unsigned long old
, unsigned long new)
35 return (old
> new) ? (old
- new): (new + ~old
+ 1);
38 struct cpufreq_stats
{
40 unsigned int total_trans
;
41 unsigned long long last_time
;
42 unsigned int max_state
;
43 unsigned int state_num
;
44 unsigned int last_index
;
45 unsigned long long *time_in_state
;
46 unsigned int *freq_table
;
47 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
48 unsigned int *trans_table
;
52 static struct cpufreq_stats
*cpufreq_stats_table
[NR_CPUS
];
54 struct cpufreq_stats_attribute
{
55 struct attribute attr
;
56 ssize_t(*show
) (struct cpufreq_stats
*, char *);
60 cpufreq_stats_update (unsigned int cpu
)
62 struct cpufreq_stats
*stat
;
63 spin_lock(&cpufreq_stats_lock
);
64 stat
= cpufreq_stats_table
[cpu
];
65 if (stat
->time_in_state
)
66 stat
->time_in_state
[stat
->last_index
] +=
67 delta_time(stat
->last_time
, jiffies
);
68 stat
->last_time
= jiffies
;
69 spin_unlock(&cpufreq_stats_lock
);
74 show_total_trans(struct cpufreq_policy
*policy
, char *buf
)
76 struct cpufreq_stats
*stat
= cpufreq_stats_table
[policy
->cpu
];
79 return sprintf(buf
, "%d\n",
80 cpufreq_stats_table
[stat
->cpu
]->total_trans
);
84 show_time_in_state(struct cpufreq_policy
*policy
, char *buf
)
88 struct cpufreq_stats
*stat
= cpufreq_stats_table
[policy
->cpu
];
91 cpufreq_stats_update(stat
->cpu
);
92 for (i
= 0; i
< stat
->state_num
; i
++) {
93 len
+= sprintf(buf
+ len
, "%u %llu\n",
94 stat
->freq_table
[i
], stat
->time_in_state
[i
]);
99 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
101 show_trans_table(struct cpufreq_policy
*policy
, char *buf
)
106 struct cpufreq_stats
*stat
= cpufreq_stats_table
[policy
->cpu
];
109 cpufreq_stats_update(stat
->cpu
);
110 for (i
= 0; i
< stat
->state_num
; i
++) {
111 if (len
>= PAGE_SIZE
)
113 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "%9u:\t",
114 stat
->freq_table
[i
]);
116 for (j
= 0; j
< stat
->state_num
; j
++) {
117 if (len
>= PAGE_SIZE
)
119 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "%u\t",
120 stat
->trans_table
[i
*stat
->max_state
+j
]);
122 len
+= snprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
126 CPUFREQ_STATDEVICE_ATTR(trans_table
,0444,show_trans_table
);
129 CPUFREQ_STATDEVICE_ATTR(total_trans
,0444,show_total_trans
);
130 CPUFREQ_STATDEVICE_ATTR(time_in_state
,0444,show_time_in_state
);
132 static struct attribute
*default_attrs
[] = {
133 &_attr_total_trans
.attr
,
134 &_attr_time_in_state
.attr
,
135 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
136 &_attr_trans_table
.attr
,
140 static struct attribute_group stats_attr_group
= {
141 .attrs
= default_attrs
,
146 freq_table_get_index(struct cpufreq_stats
*stat
, unsigned int freq
)
149 for (index
= 0; index
< stat
->max_state
; index
++)
150 if (stat
->freq_table
[index
] == freq
)
156 cpufreq_stats_free_table (unsigned int cpu
)
158 struct cpufreq_stats
*stat
= cpufreq_stats_table
[cpu
];
159 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
160 if (policy
&& policy
->cpu
== cpu
)
161 sysfs_remove_group(&policy
->kobj
, &stats_attr_group
);
163 kfree(stat
->time_in_state
);
166 cpufreq_stats_table
[cpu
] = NULL
;
168 cpufreq_cpu_put(policy
);
172 cpufreq_stats_create_table (struct cpufreq_policy
*policy
,
173 struct cpufreq_frequency_table
*table
)
175 unsigned int i
, j
, count
= 0, ret
= 0;
176 struct cpufreq_stats
*stat
;
177 struct cpufreq_policy
*data
;
178 unsigned int alloc_size
;
179 unsigned int cpu
= policy
->cpu
;
180 if (cpufreq_stats_table
[cpu
])
182 if ((stat
= kmalloc(sizeof(struct cpufreq_stats
), GFP_KERNEL
)) == NULL
)
184 memset(stat
, 0, sizeof (struct cpufreq_stats
));
186 data
= cpufreq_cpu_get(cpu
);
187 if ((ret
= sysfs_create_group(&data
->kobj
, &stats_attr_group
)))
191 cpufreq_stats_table
[cpu
] = stat
;
193 for (i
=0; table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
194 unsigned int freq
= table
[i
].frequency
;
195 if (freq
== CPUFREQ_ENTRY_INVALID
)
200 alloc_size
= count
* sizeof(int) + count
* sizeof(long long);
202 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
203 alloc_size
+= count
* count
* sizeof(int);
205 stat
->max_state
= count
;
206 stat
->time_in_state
= kmalloc(alloc_size
, GFP_KERNEL
);
207 if (!stat
->time_in_state
) {
211 memset(stat
->time_in_state
, 0, alloc_size
);
212 stat
->freq_table
= (unsigned int *)(stat
->time_in_state
+ count
);
214 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
215 stat
->trans_table
= stat
->freq_table
+ count
;
218 for (i
= 0; table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
219 unsigned int freq
= table
[i
].frequency
;
220 if (freq
== CPUFREQ_ENTRY_INVALID
)
222 if (freq_table_get_index(stat
, freq
) == -1)
223 stat
->freq_table
[j
++] = freq
;
226 spin_lock(&cpufreq_stats_lock
);
227 stat
->last_time
= jiffies
;
228 stat
->last_index
= freq_table_get_index(stat
, policy
->cur
);
229 spin_unlock(&cpufreq_stats_lock
);
230 cpufreq_cpu_put(data
);
233 cpufreq_cpu_put(data
);
235 cpufreq_stats_table
[cpu
] = NULL
;
240 cpufreq_stat_notifier_policy (struct notifier_block
*nb
, unsigned long val
,
244 struct cpufreq_policy
*policy
= data
;
245 struct cpufreq_frequency_table
*table
;
246 unsigned int cpu
= policy
->cpu
;
247 if (val
!= CPUFREQ_NOTIFY
)
249 table
= cpufreq_frequency_get_table(cpu
);
252 if ((ret
= cpufreq_stats_create_table(policy
, table
)))
258 cpufreq_stat_notifier_trans (struct notifier_block
*nb
, unsigned long val
,
261 struct cpufreq_freqs
*freq
= data
;
262 struct cpufreq_stats
*stat
;
263 int old_index
, new_index
;
265 if (val
!= CPUFREQ_POSTCHANGE
)
268 stat
= cpufreq_stats_table
[freq
->cpu
];
271 old_index
= freq_table_get_index(stat
, freq
->old
);
272 new_index
= freq_table_get_index(stat
, freq
->new);
274 cpufreq_stats_update(freq
->cpu
);
275 if (old_index
== new_index
)
278 spin_lock(&cpufreq_stats_lock
);
279 stat
->last_index
= new_index
;
280 #ifdef CONFIG_CPU_FREQ_STAT_DETAILS
281 stat
->trans_table
[old_index
* stat
->max_state
+ new_index
]++;
284 spin_unlock(&cpufreq_stats_lock
);
288 static struct notifier_block notifier_policy_block
= {
289 .notifier_call
= cpufreq_stat_notifier_policy
292 static struct notifier_block notifier_trans_block
= {
293 .notifier_call
= cpufreq_stat_notifier_trans
297 __init
cpufreq_stats_init(void)
301 spin_lock_init(&cpufreq_stats_lock
);
302 if ((ret
= cpufreq_register_notifier(¬ifier_policy_block
,
303 CPUFREQ_POLICY_NOTIFIER
)))
306 if ((ret
= cpufreq_register_notifier(¬ifier_trans_block
,
307 CPUFREQ_TRANSITION_NOTIFIER
))) {
308 cpufreq_unregister_notifier(¬ifier_policy_block
,
309 CPUFREQ_POLICY_NOTIFIER
);
314 cpufreq_update_policy(cpu
);
318 __exit
cpufreq_stats_exit(void)
321 cpufreq_unregister_notifier(¬ifier_policy_block
,
322 CPUFREQ_POLICY_NOTIFIER
);
323 cpufreq_unregister_notifier(¬ifier_trans_block
,
324 CPUFREQ_TRANSITION_NOTIFIER
);
326 cpufreq_stats_free_table(cpu
);
329 MODULE_AUTHOR ("Zou Nan hai <nanhai.zou@intel.com>");
330 MODULE_DESCRIPTION ("'cpufreq_stats' - A driver to export cpufreq stats through sysfs filesystem");
331 MODULE_LICENSE ("GPL");
333 module_init(cpufreq_stats_init
);
334 module_exit(cpufreq_stats_exit
);