PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/cpufreq/cpufreq_stats.c

https://bitbucket.org/evzijst/gittest
C | 334 lines | 287 code | 37 blank | 10 comment | 43 complexity | bd5e21b26fe91f51c2b6f7f7065846ef MD5 | raw file
  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/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>
 22
 23static spinlock_t cpufreq_stats_lock;
 24
 25#define CPUFREQ_STATDEVICE_ATTR(_name,_mode,_show) \
 26static struct freq_attr _attr_##_name = {\
 27	.attr = {.name = __stringify(_name), .owner = THIS_MODULE, \
 28		.mode = _mode, }, \
 29	.show = _show,\
 30};
 31
 32static unsigned long
 33delta_time(unsigned long old, unsigned long new)
 34{
 35	return (old > new) ? (old - new): (new + ~old + 1);
 36}
 37
 38struct cpufreq_stats {
 39	unsigned int cpu;
 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;
 49#endif
 50};
 51
 52static struct cpufreq_stats *cpufreq_stats_table[NR_CPUS];
 53
 54struct cpufreq_stats_attribute {
 55	struct attribute attr;
 56	ssize_t(*show) (struct cpufreq_stats *, char *);
 57};
 58
 59static int
 60cpufreq_stats_update (unsigned int cpu)
 61{
 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);
 70	return 0;
 71}
 72
 73static ssize_t
 74show_total_trans(struct cpufreq_policy *policy, char *buf)
 75{
 76	struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu];
 77	if(!stat)
 78		return 0;
 79	return sprintf(buf, "%d\n",
 80			cpufreq_stats_table[stat->cpu]->total_trans);
 81}
 82
 83static ssize_t
 84show_time_in_state(struct cpufreq_policy *policy, char *buf)
 85{
 86	ssize_t len = 0;
 87	int i;
 88	struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu];
 89	if(!stat)
 90		return 0;
 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]);
 95	}
 96	return len;
 97}
 98
 99#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
100static ssize_t
101show_trans_table(struct cpufreq_policy *policy, char *buf)
102{
103	ssize_t len = 0;
104	int i, j;
105
106	struct cpufreq_stats *stat = cpufreq_stats_table[policy->cpu];
107	if(!stat)
108		return 0;
109	cpufreq_stats_update(stat->cpu);
110	for (i = 0; i < stat->state_num; i++) {
111		if (len >= PAGE_SIZE)
112			break;
113		len += snprintf(buf + len, PAGE_SIZE - len, "%9u:\t",
114				stat->freq_table[i]);
115
116		for (j = 0; j < stat->state_num; j++)   {
117			if (len >= PAGE_SIZE)
118				break;
119			len += snprintf(buf + len, PAGE_SIZE - len, "%u\t",
120					stat->trans_table[i*stat->max_state+j]);
121		}
122		len += snprintf(buf + len, PAGE_SIZE - len, "\n");
123	}
124	return len;
125}
126CPUFREQ_STATDEVICE_ATTR(trans_table,0444,show_trans_table);
127#endif
128
129CPUFREQ_STATDEVICE_ATTR(total_trans,0444,show_total_trans);
130CPUFREQ_STATDEVICE_ATTR(time_in_state,0444,show_time_in_state);
131
132static 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,
137#endif
138	NULL
139};
140static struct attribute_group stats_attr_group = {
141	.attrs = default_attrs,
142	.name = "stats"
143};
144
145static int
146freq_table_get_index(struct cpufreq_stats *stat, unsigned int freq)
147{
148	int index;
149	for (index = 0; index < stat->max_state; index++)
150		if (stat->freq_table[index] == freq)
151			return index;
152	return -1;
153}
154
155static void
156cpufreq_stats_free_table (unsigned int cpu)
157{
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);
162	if (stat) {
163		kfree(stat->time_in_state);
164		kfree(stat);
165	}
166	cpufreq_stats_table[cpu] = NULL;
167	if (policy)
168		cpufreq_cpu_put(policy);
169}
170
171static int
172cpufreq_stats_create_table (struct cpufreq_policy *policy,
173		struct cpufreq_frequency_table *table)
174{
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])
181		return -EBUSY;
182	if ((stat = kmalloc(sizeof(struct cpufreq_stats), GFP_KERNEL)) == NULL)
183		return -ENOMEM;
184	memset(stat, 0, sizeof (struct cpufreq_stats));
185
186	data = cpufreq_cpu_get(cpu);
187	if ((ret = sysfs_create_group(&data->kobj, &stats_attr_group)))
188		goto error_out;
189
190	stat->cpu = cpu;
191	cpufreq_stats_table[cpu] = stat;
192
193	for (i=0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
194		unsigned int freq = table[i].frequency;
195		if (freq == CPUFREQ_ENTRY_INVALID)
196			continue;
197		count++;
198	}
199
200	alloc_size = count * sizeof(int) + count * sizeof(long long);
201
202#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
203	alloc_size += count * count * sizeof(int);
204#endif
205	stat->max_state = count;
206	stat->time_in_state = kmalloc(alloc_size, GFP_KERNEL);
207	if (!stat->time_in_state) {
208		ret = -ENOMEM;
209		goto error_out;
210	}
211	memset(stat->time_in_state, 0, alloc_size);
212	stat->freq_table = (unsigned int *)(stat->time_in_state + count);
213
214#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
215	stat->trans_table = stat->freq_table + count;
216#endif
217	j = 0;
218	for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
219		unsigned int freq = table[i].frequency;
220		if (freq == CPUFREQ_ENTRY_INVALID)
221			continue;
222		if (freq_table_get_index(stat, freq) == -1)
223			stat->freq_table[j++] = freq;
224	}
225	stat->state_num = j;
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);
231	return 0;
232error_out:
233	cpufreq_cpu_put(data);
234	kfree(stat);
235	cpufreq_stats_table[cpu] = NULL;
236	return ret;
237}
238
239static int
240cpufreq_stat_notifier_policy (struct notifier_block *nb, unsigned long val,
241		void *data)
242{
243	int ret;
244	struct cpufreq_policy *policy = data;
245	struct cpufreq_frequency_table *table;
246	unsigned int cpu = policy->cpu;
247	if (val != CPUFREQ_NOTIFY)
248		return 0;
249	table = cpufreq_frequency_get_table(cpu);
250	if (!table)
251		return 0;
252	if ((ret = cpufreq_stats_create_table(policy, table)))
253		return ret;
254	return 0;
255}
256
257static int
258cpufreq_stat_notifier_trans (struct notifier_block *nb, unsigned long val,
259		void *data)
260{
261	struct cpufreq_freqs *freq = data;
262	struct cpufreq_stats *stat;
263	int old_index, new_index;
264
265	if (val != CPUFREQ_POSTCHANGE)
266		return 0;
267
268	stat = cpufreq_stats_table[freq->cpu];
269	if (!stat)
270		return 0;
271	old_index = freq_table_get_index(stat, freq->old);
272	new_index = freq_table_get_index(stat, freq->new);
273
274	cpufreq_stats_update(freq->cpu);
275	if (old_index == new_index)
276		return 0;
277
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]++;
282#endif
283	stat->total_trans++;
284	spin_unlock(&cpufreq_stats_lock);
285	return 0;
286}
287
288static struct notifier_block notifier_policy_block = {
289	.notifier_call = cpufreq_stat_notifier_policy
290};
291
292static struct notifier_block notifier_trans_block = {
293	.notifier_call = cpufreq_stat_notifier_trans
294};
295
296static int
297__init cpufreq_stats_init(void)
298{
299	int ret;
300	unsigned int cpu;
301	spin_lock_init(&cpufreq_stats_lock);
302	if ((ret = cpufreq_register_notifier(&notifier_policy_block,
303				CPUFREQ_POLICY_NOTIFIER)))
304		return ret;
305
306	if ((ret = cpufreq_register_notifier(&notifier_trans_block,
307				CPUFREQ_TRANSITION_NOTIFIER))) {
308		cpufreq_unregister_notifier(&notifier_policy_block,
309				CPUFREQ_POLICY_NOTIFIER);
310		return ret;
311	}
312
313	for_each_cpu(cpu)
314		cpufreq_update_policy(cpu);
315	return 0;
316}
317static void
318__exit cpufreq_stats_exit(void)
319{
320	unsigned int cpu;
321	cpufreq_unregister_notifier(&notifier_policy_block,
322			CPUFREQ_POLICY_NOTIFIER);
323	cpufreq_unregister_notifier(&notifier_trans_block,
324			CPUFREQ_TRANSITION_NOTIFIER);
325	for_each_cpu(cpu)
326		cpufreq_stats_free_table(cpu);
327}
328
329MODULE_AUTHOR ("Zou Nan hai <nanhai.zou@intel.com>");
330MODULE_DESCRIPTION ("'cpufreq_stats' - A driver to export cpufreq stats through sysfs filesystem");
331MODULE_LICENSE ("GPL");
332
333module_init(cpufreq_stats_init);
334module_exit(cpufreq_stats_exit);