]>
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 | 159 | unsigned int alloc_size; |
041526f9 | 160 | struct cpufreq_frequency_table *pos, *table; |
ad4c2302 | 161 | |
a685c6d0 | 162 | /* We need cpufreq table for creating stats table */ |
f8bfc116 | 163 | table = policy->freq_table; |
ad4c2302 | 164 | if (unlikely(!table)) |
1aefc75b | 165 | return; |
ad4c2302 | 166 | |
b8c67448 | 167 | /* stats already initialized */ |
a9aaf291 | 168 | if (policy->stats) |
1aefc75b | 169 | return; |
b8c67448 | 170 | |
50941607 | 171 | stats = kzalloc(sizeof(*stats), GFP_KERNEL); |
a685c6d0 | 172 | if (!stats) |
1aefc75b | 173 | return; |
1da177e4 | 174 | |
a685c6d0 | 175 | /* Find total allocation size */ |
041526f9 | 176 | cpufreq_for_each_valid_entry(pos, table) |
1da177e4 | 177 | count++; |
1da177e4 | 178 | |
1e7586a1 | 179 | alloc_size = count * sizeof(int) + count * sizeof(u64); |
1da177e4 LT |
180 | |
181 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS | |
182 | alloc_size += count * count * sizeof(int); | |
183 | #endif | |
a685c6d0 VK |
184 | |
185 | /* Allocate memory for time_in_state/freq_table/trans_table in one go */ | |
50941607 | 186 | stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL); |
a685c6d0 VK |
187 | if (!stats->time_in_state) |
188 | goto free_stat; | |
189 | ||
50941607 | 190 | stats->freq_table = (unsigned int *)(stats->time_in_state + count); |
1da177e4 LT |
191 | |
192 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS | |
50941607 | 193 | stats->trans_table = stats->freq_table + count; |
1da177e4 | 194 | #endif |
a685c6d0 VK |
195 | |
196 | stats->max_state = count; | |
197 | ||
198 | /* Find valid-unique entries */ | |
041526f9 | 199 | cpufreq_for_each_valid_entry(pos, table) |
50941607 VK |
200 | if (freq_table_get_index(stats, pos->frequency) == -1) |
201 | stats->freq_table[i++] = pos->frequency; | |
a685c6d0 | 202 | |
490285c6 | 203 | stats->state_num = i; |
50941607 VK |
204 | stats->last_time = get_jiffies_64(); |
205 | stats->last_index = freq_table_get_index(stats, policy->cur); | |
a685c6d0 VK |
206 | |
207 | policy->stats = stats; | |
208 | ret = sysfs_create_group(&policy->kobj, &stats_attr_group); | |
209 | if (!ret) | |
1aefc75b | 210 | return; |
a685c6d0 VK |
211 | |
212 | /* We failed, release resources */ | |
a9aaf291 | 213 | policy->stats = NULL; |
a685c6d0 VK |
214 | kfree(stats->time_in_state); |
215 | free_stat: | |
216 | kfree(stats); | |
b3f9ff88 VK |
217 | } |
218 | ||
1aefc75b RW |
219 | void cpufreq_stats_record_transition(struct cpufreq_policy *policy, |
220 | unsigned int new_freq) | |
1da177e4 | 221 | { |
1aefc75b | 222 | struct cpufreq_stats *stats = policy->stats; |
1da177e4 LT |
223 | int old_index, new_index; |
224 | ||
1aefc75b | 225 | if (!stats) { |
a9aaf291 | 226 | pr_debug("%s: No stats found\n", __func__); |
1aefc75b | 227 | return; |
a9aaf291 VK |
228 | } |
229 | ||
50941607 | 230 | old_index = stats->last_index; |
1aefc75b | 231 | new_index = freq_table_get_index(stats, new_freq); |
1da177e4 | 232 | |
50941607 | 233 | /* We can't do stats->time_in_state[-1]= .. */ |
1aefc75b RW |
234 | if (old_index == -1 || new_index == -1 || old_index == new_index) |
235 | return; | |
8edc59d9 | 236 | |
e7347694 VK |
237 | cpufreq_stats_update(stats); |
238 | ||
50941607 | 239 | stats->last_index = new_index; |
1da177e4 | 240 | #ifdef CONFIG_CPU_FREQ_STAT_DETAILS |
50941607 | 241 | stats->trans_table[old_index * stats->max_state + new_index]++; |
1da177e4 | 242 | #endif |
50941607 | 243 | stats->total_trans++; |
1da177e4 | 244 | } |