0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/cpu.h>
0010 #include <linux/cpufreq.h>
0011 #include <linux/module.h>
0012 #include <linux/sched/clock.h>
0013 #include <linux/slab.h>
0014
0015 struct cpufreq_stats {
0016 unsigned int total_trans;
0017 unsigned long long last_time;
0018 unsigned int max_state;
0019 unsigned int state_num;
0020 unsigned int last_index;
0021 u64 *time_in_state;
0022 unsigned int *freq_table;
0023 unsigned int *trans_table;
0024
0025
0026 unsigned int reset_pending;
0027 unsigned long long reset_time;
0028 };
0029
0030 static void cpufreq_stats_update(struct cpufreq_stats *stats,
0031 unsigned long long time)
0032 {
0033 unsigned long long cur_time = local_clock();
0034
0035 stats->time_in_state[stats->last_index] += cur_time - time;
0036 stats->last_time = cur_time;
0037 }
0038
0039 static void cpufreq_stats_reset_table(struct cpufreq_stats *stats)
0040 {
0041 unsigned int count = stats->max_state;
0042
0043 memset(stats->time_in_state, 0, count * sizeof(u64));
0044 memset(stats->trans_table, 0, count * count * sizeof(int));
0045 stats->last_time = local_clock();
0046 stats->total_trans = 0;
0047
0048
0049 WRITE_ONCE(stats->reset_pending, 0);
0050
0051
0052
0053
0054 smp_rmb();
0055 cpufreq_stats_update(stats, READ_ONCE(stats->reset_time));
0056 }
0057
0058 static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
0059 {
0060 struct cpufreq_stats *stats = policy->stats;
0061
0062 if (READ_ONCE(stats->reset_pending))
0063 return sprintf(buf, "%d\n", 0);
0064 else
0065 return sprintf(buf, "%u\n", stats->total_trans);
0066 }
0067 cpufreq_freq_attr_ro(total_trans);
0068
0069 static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
0070 {
0071 struct cpufreq_stats *stats = policy->stats;
0072 bool pending = READ_ONCE(stats->reset_pending);
0073 unsigned long long time;
0074 ssize_t len = 0;
0075 int i;
0076
0077 for (i = 0; i < stats->state_num; i++) {
0078 if (pending) {
0079 if (i == stats->last_index) {
0080
0081
0082
0083
0084 smp_rmb();
0085 time = local_clock() - READ_ONCE(stats->reset_time);
0086 } else {
0087 time = 0;
0088 }
0089 } else {
0090 time = stats->time_in_state[i];
0091 if (i == stats->last_index)
0092 time += local_clock() - stats->last_time;
0093 }
0094
0095 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
0096 nsec_to_clock_t(time));
0097 }
0098 return len;
0099 }
0100 cpufreq_freq_attr_ro(time_in_state);
0101
0102
0103 static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf,
0104 size_t count)
0105 {
0106 struct cpufreq_stats *stats = policy->stats;
0107
0108
0109
0110
0111
0112 WRITE_ONCE(stats->reset_time, local_clock());
0113
0114
0115
0116
0117 smp_wmb();
0118 WRITE_ONCE(stats->reset_pending, 1);
0119
0120 return count;
0121 }
0122 cpufreq_freq_attr_wo(reset);
0123
0124 static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
0125 {
0126 struct cpufreq_stats *stats = policy->stats;
0127 bool pending = READ_ONCE(stats->reset_pending);
0128 ssize_t len = 0;
0129 int i, j, count;
0130
0131 len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n");
0132 len += scnprintf(buf + len, PAGE_SIZE - len, " : ");
0133 for (i = 0; i < stats->state_num; i++) {
0134 if (len >= PAGE_SIZE)
0135 break;
0136 len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ",
0137 stats->freq_table[i]);
0138 }
0139 if (len >= PAGE_SIZE)
0140 return PAGE_SIZE;
0141
0142 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
0143
0144 for (i = 0; i < stats->state_num; i++) {
0145 if (len >= PAGE_SIZE)
0146 break;
0147
0148 len += scnprintf(buf + len, PAGE_SIZE - len, "%9u: ",
0149 stats->freq_table[i]);
0150
0151 for (j = 0; j < stats->state_num; j++) {
0152 if (len >= PAGE_SIZE)
0153 break;
0154
0155 if (pending)
0156 count = 0;
0157 else
0158 count = stats->trans_table[i * stats->max_state + j];
0159
0160 len += scnprintf(buf + len, PAGE_SIZE - len, "%9u ", count);
0161 }
0162 if (len >= PAGE_SIZE)
0163 break;
0164 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
0165 }
0166
0167 if (len >= PAGE_SIZE) {
0168 pr_warn_once("cpufreq transition table exceeds PAGE_SIZE. Disabling\n");
0169 return -EFBIG;
0170 }
0171 return len;
0172 }
0173 cpufreq_freq_attr_ro(trans_table);
0174
0175 static struct attribute *default_attrs[] = {
0176 &total_trans.attr,
0177 &time_in_state.attr,
0178 &reset.attr,
0179 &trans_table.attr,
0180 NULL
0181 };
0182 static const struct attribute_group stats_attr_group = {
0183 .attrs = default_attrs,
0184 .name = "stats"
0185 };
0186
0187 static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
0188 {
0189 int index;
0190 for (index = 0; index < stats->max_state; index++)
0191 if (stats->freq_table[index] == freq)
0192 return index;
0193 return -1;
0194 }
0195
0196 void cpufreq_stats_free_table(struct cpufreq_policy *policy)
0197 {
0198 struct cpufreq_stats *stats = policy->stats;
0199
0200
0201 if (!stats)
0202 return;
0203
0204 pr_debug("%s: Free stats table\n", __func__);
0205
0206 sysfs_remove_group(&policy->kobj, &stats_attr_group);
0207 kfree(stats->time_in_state);
0208 kfree(stats);
0209 policy->stats = NULL;
0210 }
0211
0212 void cpufreq_stats_create_table(struct cpufreq_policy *policy)
0213 {
0214 unsigned int i = 0, count;
0215 struct cpufreq_stats *stats;
0216 unsigned int alloc_size;
0217 struct cpufreq_frequency_table *pos;
0218
0219 count = cpufreq_table_count_valid_entries(policy);
0220 if (!count)
0221 return;
0222
0223
0224 if (policy->stats)
0225 return;
0226
0227 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
0228 if (!stats)
0229 return;
0230
0231 alloc_size = count * sizeof(int) + count * sizeof(u64);
0232
0233 alloc_size += count * count * sizeof(int);
0234
0235
0236 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
0237 if (!stats->time_in_state)
0238 goto free_stat;
0239
0240 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
0241
0242 stats->trans_table = stats->freq_table + count;
0243
0244 stats->max_state = count;
0245
0246
0247 cpufreq_for_each_valid_entry(pos, policy->freq_table)
0248 if (freq_table_get_index(stats, pos->frequency) == -1)
0249 stats->freq_table[i++] = pos->frequency;
0250
0251 stats->state_num = i;
0252 stats->last_time = local_clock();
0253 stats->last_index = freq_table_get_index(stats, policy->cur);
0254
0255 policy->stats = stats;
0256 if (!sysfs_create_group(&policy->kobj, &stats_attr_group))
0257 return;
0258
0259
0260 policy->stats = NULL;
0261 kfree(stats->time_in_state);
0262 free_stat:
0263 kfree(stats);
0264 }
0265
0266 void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
0267 unsigned int new_freq)
0268 {
0269 struct cpufreq_stats *stats = policy->stats;
0270 int old_index, new_index;
0271
0272 if (unlikely(!stats))
0273 return;
0274
0275 if (unlikely(READ_ONCE(stats->reset_pending)))
0276 cpufreq_stats_reset_table(stats);
0277
0278 old_index = stats->last_index;
0279 new_index = freq_table_get_index(stats, new_freq);
0280
0281
0282 if (unlikely(old_index == -1 || new_index == -1 || old_index == new_index))
0283 return;
0284
0285 cpufreq_stats_update(stats, stats->last_time);
0286
0287 stats->last_index = new_index;
0288 stats->trans_table[old_index * stats->max_state + new_index]++;
0289 stats->total_trans++;
0290 }