]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/cpufreq/cpufreq_stats.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[mirror_ubuntu-zesty-kernel.git] / drivers / cpufreq / cpufreq_stats.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/cpufreq/cpufreq_stats.c
3 *
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
0a829c5a 5 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
1da177e4
LT
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
1da177e4 12#include <linux/cpu.h>
1da177e4 13#include <linux/cpufreq.h>
5c720d37 14#include <linux/module.h>
5ff0a268 15#include <linux/slab.h>
bfc3f028 16#include <linux/cputime.h>
1da177e4 17
1aefc75b 18static DEFINE_SPINLOCK(cpufreq_stats_lock);
1da177e4 19
1da177e4 20struct cpufreq_stats {
1da177e4 21 unsigned int total_trans;
bb176f7d 22 unsigned long long last_time;
1da177e4
LT
23 unsigned int max_state;
24 unsigned int state_num;
25 unsigned int last_index;
1e7586a1 26 u64 *time_in_state;
1da177e4
LT
27 unsigned int *freq_table;
28#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
29 unsigned int *trans_table;
30#endif
31};
32
50941607 33static int cpufreq_stats_update(struct cpufreq_stats *stats)
1da177e4 34{
9531347c 35 unsigned long long cur_time = get_jiffies_64();
58f1df25 36
1da177e4 37 spin_lock(&cpufreq_stats_lock);
c960f9b2 38 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
50941607 39 stats->last_time = cur_time;
1da177e4
LT
40 spin_unlock(&cpufreq_stats_lock);
41 return 0;
42}
43
ee7930ee
MM
44static void cpufreq_stats_clear_table(struct cpufreq_stats *stats)
45{
46 unsigned int count = stats->max_state;
47
48 memset(stats->time_in_state, 0, count * sizeof(u64));
49#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50 memset(stats->trans_table, 0, count * count * sizeof(int));
51#endif
52 stats->last_time = get_jiffies_64();
53 stats->total_trans = 0;
54}
55
0a829c5a 56static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
1da177e4 57{
a9aaf291 58 return sprintf(buf, "%d\n", policy->stats->total_trans);
1da177e4
LT
59}
60
0a829c5a 61static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
1da177e4 62{
50941607 63 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
64 ssize_t len = 0;
65 int i;
a9aaf291 66
1aefc75b
RW
67 if (policy->fast_switch_enabled)
68 return 0;
69
50941607
VK
70 cpufreq_stats_update(stats);
71 for (i = 0; i < stats->state_num; i++) {
72 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
0a829c5a 73 (unsigned long long)
50941607 74 jiffies_64_to_clock_t(stats->time_in_state[i]));
1da177e4
LT
75 }
76 return len;
77}
78
ee7930ee
MM
79static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
80 size_t count)
81{
82 /* We don't care what is written to the attribute. */
83 cpufreq_stats_clear_table(policy->stats);
84 return count;
85}
86
1da177e4 87#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
0a829c5a 88static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
1da177e4 89{
50941607 90 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
91 ssize_t len = 0;
92 int i, j;
93
1aefc75b
RW
94 if (policy->fast_switch_enabled)
95 return 0;
96
58f1df25
VP
97 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
98 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
50941607 99 for (i = 0; i < stats->state_num; i++) {
58f1df25
VP
100 if (len >= PAGE_SIZE)
101 break;
102 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 103 stats->freq_table[i]);
58f1df25
VP
104 }
105 if (len >= PAGE_SIZE)
25aca347 106 return PAGE_SIZE;
58f1df25
VP
107
108 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
109
50941607 110 for (i = 0; i < stats->state_num; i++) {
1da177e4
LT
111 if (len >= PAGE_SIZE)
112 break;
58f1df25
VP
113
114 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
50941607 115 stats->freq_table[i]);
1da177e4 116
50941607 117 for (j = 0; j < stats->state_num; j++) {
1da177e4
LT
118 if (len >= PAGE_SIZE)
119 break;
58f1df25 120 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
50941607 121 stats->trans_table[i*stats->max_state+j]);
1da177e4 122 }
25aca347
CEB
123 if (len >= PAGE_SIZE)
124 break;
1da177e4
LT
125 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
126 }
25aca347
CEB
127 if (len >= PAGE_SIZE)
128 return PAGE_SIZE;
1da177e4
LT
129 return len;
130}
df18e504 131cpufreq_freq_attr_ro(trans_table);
1da177e4
LT
132#endif
133
df18e504
VK
134cpufreq_freq_attr_ro(total_trans);
135cpufreq_freq_attr_ro(time_in_state);
ee7930ee 136cpufreq_freq_attr_wo(reset);
1da177e4
LT
137
138static struct attribute *default_attrs[] = {
df18e504
VK
139 &total_trans.attr,
140 &time_in_state.attr,
ee7930ee 141 &reset.attr,
1da177e4 142#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
df18e504 143 &trans_table.attr,
1da177e4
LT
144#endif
145 NULL
146};
147static struct attribute_group stats_attr_group = {
148 .attrs = default_attrs,
149 .name = "stats"
150};
151
50941607 152static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
1da177e4
LT
153{
154 int index;
50941607
VK
155 for (index = 0; index < stats->max_state; index++)
156 if (stats->freq_table[index] == freq)
1da177e4
LT
157 return index;
158 return -1;
159}
160
1aefc75b 161void cpufreq_stats_free_table(struct cpufreq_policy *policy)
1da177e4 162{
50941607 163 struct cpufreq_stats *stats = policy->stats;
b8eed8af 164
a9aaf291 165 /* Already freed */
50941607 166 if (!stats)
2d13594d
VK
167 return;
168
50941607 169 pr_debug("%s: Free stats table\n", __func__);
2d13594d
VK
170
171 sysfs_remove_group(&policy->kobj, &stats_attr_group);
50941607
VK
172 kfree(stats->time_in_state);
173 kfree(stats);
a9aaf291 174 policy->stats = NULL;
98586ed8 175}
176
1aefc75b 177void cpufreq_stats_create_table(struct cpufreq_policy *policy)
1da177e4 178{
a685c6d0 179 unsigned int i = 0, count = 0, ret = -ENOMEM;
50941607 180 struct cpufreq_stats *stats;
1da177e4 181 unsigned int alloc_size;
041526f9 182 struct cpufreq_frequency_table *pos, *table;
ad4c2302 183
a685c6d0 184 /* We need cpufreq table for creating stats table */
f8bfc116 185 table = policy->freq_table;
ad4c2302 186 if (unlikely(!table))
1aefc75b 187 return;
ad4c2302 188
b8c67448 189 /* stats already initialized */
a9aaf291 190 if (policy->stats)
1aefc75b 191 return;
b8c67448 192
50941607 193 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
a685c6d0 194 if (!stats)
1aefc75b 195 return;
1da177e4 196
a685c6d0 197 /* Find total allocation size */
041526f9 198 cpufreq_for_each_valid_entry(pos, table)
1da177e4 199 count++;
1da177e4 200
1e7586a1 201 alloc_size = count * sizeof(int) + count * sizeof(u64);
1da177e4
LT
202
203#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
204 alloc_size += count * count * sizeof(int);
205#endif
a685c6d0
VK
206
207 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
50941607 208 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
a685c6d0
VK
209 if (!stats->time_in_state)
210 goto free_stat;
211
50941607 212 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
1da177e4
LT
213
214#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50941607 215 stats->trans_table = stats->freq_table + count;
1da177e4 216#endif
a685c6d0
VK
217
218 stats->max_state = count;
219
220 /* Find valid-unique entries */
041526f9 221 cpufreq_for_each_valid_entry(pos, table)
50941607
VK
222 if (freq_table_get_index(stats, pos->frequency) == -1)
223 stats->freq_table[i++] = pos->frequency;
a685c6d0 224
490285c6 225 stats->state_num = i;
50941607
VK
226 stats->last_time = get_jiffies_64();
227 stats->last_index = freq_table_get_index(stats, policy->cur);
a685c6d0
VK
228
229 policy->stats = stats;
230 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
231 if (!ret)
1aefc75b 232 return;
a685c6d0
VK
233
234 /* We failed, release resources */
a9aaf291 235 policy->stats = NULL;
a685c6d0
VK
236 kfree(stats->time_in_state);
237free_stat:
238 kfree(stats);
b3f9ff88
VK
239}
240
1aefc75b
RW
241void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
242 unsigned int new_freq)
1da177e4 243{
1aefc75b 244 struct cpufreq_stats *stats = policy->stats;
1da177e4
LT
245 int old_index, new_index;
246
1aefc75b 247 if (!stats) {
a9aaf291 248 pr_debug("%s: No stats found\n", __func__);
1aefc75b 249 return;
a9aaf291
VK
250 }
251
50941607 252 old_index = stats->last_index;
1aefc75b 253 new_index = freq_table_get_index(stats, new_freq);
1da177e4 254
50941607 255 /* We can't do stats->time_in_state[-1]= .. */
1aefc75b
RW
256 if (old_index == -1 || new_index == -1 || old_index == new_index)
257 return;
8edc59d9 258
e7347694
VK
259 cpufreq_stats_update(stats);
260
50941607 261 stats->last_index = new_index;
1da177e4 262#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
50941607 263 stats->trans_table[old_index * stats->max_state + new_index]++;
1da177e4 264#endif
50941607 265 stats->total_trans++;
1da177e4 266}