]>
Commit | Line | Data |
---|---|---|
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 | 18 | static DEFINE_SPINLOCK(cpufreq_stats_lock); |
1da177e4 | 19 | |
1da177e4 | 20 | struct 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 | 33 | static 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 | ||
0a829c5a | 44 | static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 45 | { |
a9aaf291 | 46 | return sprintf(buf, "%d\n", policy->stats->total_trans); |
1da177e4 LT |
47 | } |
48 | ||
0a829c5a | 49 | static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 50 | { |
50941607 | 51 | struct cpufreq_stats *stats = policy->stats; |
1da177e4 LT |
52 | ssize_t len = 0; |
53 | int i; | |
a9aaf291 | 54 | |
1aefc75b RW |
55 | if (policy->fast_switch_enabled) |
56 | return 0; | |
57 | ||
50941607 VK |
58 | cpufreq_stats_update(stats); |
59 | for (i = 0; i < stats->state_num; i++) { | |
60 | len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i], | |
0a829c5a | 61 | (unsigned long long) |
50941607 | 62 | jiffies_64_to_clock_t(stats->time_in_state[i])); |
1da177e4 LT |
63 | } |
64 | return len; | |
65 | } | |
66 | ||
67 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS | |
0a829c5a | 68 | static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 69 | { |
50941607 | 70 | struct cpufreq_stats *stats = policy->stats; |
1da177e4 LT |
71 | ssize_t len = 0; |
72 | int i, j; | |
73 | ||
1aefc75b RW |
74 | if (policy->fast_switch_enabled) |
75 | return 0; | |
76 | ||
58f1df25 VP |
77 | len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n"); |
78 | len += snprintf(buf + len, PAGE_SIZE - len, " : "); | |
50941607 | 79 | for (i = 0; i < stats->state_num; i++) { |
58f1df25 VP |
80 | if (len >= PAGE_SIZE) |
81 | break; | |
82 | len += snprintf(buf + len, PAGE_SIZE - len, "%9u ", | |
50941607 | 83 | stats->freq_table[i]); |
58f1df25 VP |
84 | } |
85 | if (len >= PAGE_SIZE) | |
25aca347 | 86 | return PAGE_SIZE; |
58f1df25 VP |
87 | |
88 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); | |
89 | ||
50941607 | 90 | for (i = 0; i < stats->state_num; i++) { |
1da177e4 LT |
91 | if (len >= PAGE_SIZE) |
92 | break; | |
58f1df25 VP |
93 | |
94 | len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ", | |
50941607 | 95 | stats->freq_table[i]); |
1da177e4 | 96 | |
50941607 | 97 | for (j = 0; j < stats->state_num; j++) { |
1da177e4 LT |
98 | if (len >= PAGE_SIZE) |
99 | break; | |
58f1df25 | 100 | len += snprintf(buf + len, PAGE_SIZE - len, "%9u ", |
50941607 | 101 | stats->trans_table[i*stats->max_state+j]); |
1da177e4 | 102 | } |
25aca347 CEB |
103 | if (len >= PAGE_SIZE) |
104 | break; | |
1da177e4 LT |
105 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); |
106 | } | |
25aca347 CEB |
107 | if (len >= PAGE_SIZE) |
108 | return PAGE_SIZE; | |
1da177e4 LT |
109 | return len; |
110 | } | |
df18e504 | 111 | cpufreq_freq_attr_ro(trans_table); |
1da177e4 LT |
112 | #endif |
113 | ||
df18e504 VK |
114 | cpufreq_freq_attr_ro(total_trans); |
115 | cpufreq_freq_attr_ro(time_in_state); | |
1da177e4 LT |
116 | |
117 | static struct attribute *default_attrs[] = { | |
df18e504 VK |
118 | &total_trans.attr, |
119 | &time_in_state.attr, | |
1da177e4 | 120 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
df18e504 | 121 | &trans_table.attr, |
1da177e4 LT |
122 | #endif |
123 | NULL | |
124 | }; | |
125 | static struct attribute_group stats_attr_group = { | |
126 | .attrs = default_attrs, | |
127 | .name = "stats" | |
128 | }; | |
129 | ||
50941607 | 130 | static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq) |
1da177e4 LT |
131 | { |
132 | int index; | |
50941607 VK |
133 | for (index = 0; index < stats->max_state; index++) |
134 | if (stats->freq_table[index] == freq) | |
1da177e4 LT |
135 | return index; |
136 | return -1; | |
137 | } | |
138 | ||
1aefc75b | 139 | void cpufreq_stats_free_table(struct cpufreq_policy *policy) |
1da177e4 | 140 | { |
50941607 | 141 | struct cpufreq_stats *stats = policy->stats; |
b8eed8af | 142 | |
a9aaf291 | 143 | /* Already freed */ |
50941607 | 144 | if (!stats) |
2d13594d VK |
145 | return; |
146 | ||
50941607 | 147 | pr_debug("%s: Free stats table\n", __func__); |
2d13594d VK |
148 | |
149 | sysfs_remove_group(&policy->kobj, &stats_attr_group); | |
50941607 VK |
150 | kfree(stats->time_in_state); |
151 | kfree(stats); | |
a9aaf291 | 152 | policy->stats = NULL; |
98586ed8 | 153 | } |
154 | ||
1aefc75b | 155 | void cpufreq_stats_create_table(struct cpufreq_policy *policy) |
1da177e4 | 156 | { |
a685c6d0 | 157 | unsigned int i = 0, count = 0, ret = -ENOMEM; |
50941607 | 158 | struct cpufreq_stats *stats; |
1da177e4 LT |
159 | unsigned int alloc_size; |
160 | unsigned int cpu = policy->cpu; | |
041526f9 | 161 | struct cpufreq_frequency_table *pos, *table; |
ad4c2302 | 162 | |
a685c6d0 | 163 | /* We need cpufreq table for creating stats table */ |
ad4c2302 SK |
164 | table = cpufreq_frequency_get_table(cpu); |
165 | if (unlikely(!table)) | |
1aefc75b | 166 | return; |
ad4c2302 | 167 | |
b8c67448 | 168 | /* stats already initialized */ |
a9aaf291 | 169 | if (policy->stats) |
1aefc75b | 170 | return; |
b8c67448 | 171 | |
50941607 | 172 | stats = kzalloc(sizeof(*stats), GFP_KERNEL); |
a685c6d0 | 173 | if (!stats) |
1aefc75b | 174 | return; |
1da177e4 | 175 | |
a685c6d0 | 176 | /* Find total allocation size */ |
041526f9 | 177 | cpufreq_for_each_valid_entry(pos, table) |
1da177e4 | 178 | count++; |
1da177e4 | 179 | |
1e7586a1 | 180 | alloc_size = count * sizeof(int) + count * sizeof(u64); |
1da177e4 LT |
181 | |
182 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS | |
183 | alloc_size += count * count * sizeof(int); | |
184 | #endif | |
a685c6d0 VK |
185 | |
186 | /* Allocate memory for time_in_state/freq_table/trans_table in one go */ | |
50941607 | 187 | stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL); |
a685c6d0 VK |
188 | if (!stats->time_in_state) |
189 | goto free_stat; | |
190 | ||
50941607 | 191 | stats->freq_table = (unsigned int *)(stats->time_in_state + count); |
1da177e4 LT |
192 | |
193 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS | |
50941607 | 194 | stats->trans_table = stats->freq_table + count; |
1da177e4 | 195 | #endif |
a685c6d0 VK |
196 | |
197 | stats->max_state = count; | |
198 | ||
199 | /* Find valid-unique entries */ | |
041526f9 | 200 | cpufreq_for_each_valid_entry(pos, table) |
50941607 VK |
201 | if (freq_table_get_index(stats, pos->frequency) == -1) |
202 | stats->freq_table[i++] = pos->frequency; | |
a685c6d0 | 203 | |
490285c6 | 204 | stats->state_num = i; |
50941607 VK |
205 | stats->last_time = get_jiffies_64(); |
206 | stats->last_index = freq_table_get_index(stats, policy->cur); | |
a685c6d0 VK |
207 | |
208 | policy->stats = stats; | |
209 | ret = sysfs_create_group(&policy->kobj, &stats_attr_group); | |
210 | if (!ret) | |
1aefc75b | 211 | return; |
a685c6d0 VK |
212 | |
213 | /* We failed, release resources */ | |
a9aaf291 | 214 | policy->stats = NULL; |
a685c6d0 VK |
215 | kfree(stats->time_in_state); |
216 | free_stat: | |
217 | kfree(stats); | |
b3f9ff88 VK |
218 | } |
219 | ||
1aefc75b RW |
220 | void cpufreq_stats_record_transition(struct cpufreq_policy *policy, |
221 | unsigned int new_freq) | |
1da177e4 | 222 | { |
1aefc75b | 223 | struct cpufreq_stats *stats = policy->stats; |
1da177e4 LT |
224 | int old_index, new_index; |
225 | ||
1aefc75b | 226 | if (!stats) { |
a9aaf291 | 227 | pr_debug("%s: No stats found\n", __func__); |
1aefc75b | 228 | return; |
a9aaf291 VK |
229 | } |
230 | ||
50941607 | 231 | old_index = stats->last_index; |
1aefc75b | 232 | new_index = freq_table_get_index(stats, new_freq); |
1da177e4 | 233 | |
50941607 | 234 | /* We can't do stats->time_in_state[-1]= .. */ |
1aefc75b RW |
235 | if (old_index == -1 || new_index == -1 || old_index == new_index) |
236 | return; | |
8edc59d9 | 237 | |
e7347694 VK |
238 | cpufreq_stats_update(stats); |
239 | ||
50941607 | 240 | stats->last_index = new_index; |
1da177e4 | 241 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
50941607 | 242 | stats->trans_table[old_index * stats->max_state + new_index]++; |
1da177e4 | 243 | #endif |
50941607 | 244 | stats->total_trans++; |
1da177e4 | 245 | } |