blob: b084708fd113b21d38f9a9edff84873fe994b80f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/cpufreq/cpufreq_stats.c
3 *
4 * Copyright (C) 2003-2004 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
Dave Jones0a829c52009-01-18 01:49:04 -05005 * (C) 2004 Zou Nan hai <nanhai.zou@intel.com>.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/cpufreq.h>
Paul Gortmaker5c720d372011-05-27 13:23:32 -040014#include <linux/module.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020017static DEFINE_SPINLOCK(cpufreq_stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019struct cpufreq_stats {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 unsigned int total_trans;
Viresh Kumarbb176f72013-06-19 14:19:33 +053021 unsigned long long last_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 unsigned int max_state;
23 unsigned int state_num;
24 unsigned int last_index;
Viresh Kumar1e7586a2012-10-26 00:51:21 +020025 u64 *time_in_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 unsigned int *freq_table;
27#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
28 unsigned int *trans_table;
29#endif
30};
31
Viresh Kumar50941602015-01-06 21:09:07 +053032static int cpufreq_stats_update(struct cpufreq_stats *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Viresh Kumar95313472015-01-06 21:09:03 +053034 unsigned long long cur_time = get_jiffies_64();
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 spin_lock(&cpufreq_stats_lock);
Viresh Kumarc960f9b2015-01-06 21:09:12 +053037 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
Viresh Kumar50941602015-01-06 21:09:07 +053038 stats->last_time = cur_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 spin_unlock(&cpufreq_stats_lock);
40 return 0;
41}
42
Dave Jones0a829c52009-01-18 01:49:04 -050043static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Viresh Kumara9aaf292015-01-13 11:34:00 +053045 return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Dave Jones0a829c52009-01-18 01:49:04 -050048static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Viresh Kumar50941602015-01-06 21:09:07 +053050 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 ssize_t len = 0;
52 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053053
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020054 if (policy->fast_switch_enabled)
55 return 0;
56
Viresh Kumar50941602015-01-06 21:09:07 +053057 cpufreq_stats_update(stats);
58 for (i = 0; i < stats->state_num; i++) {
59 len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i],
Dave Jones0a829c52009-01-18 01:49:04 -050060 (unsigned long long)
Viresh Kumar50941602015-01-06 21:09:07 +053061 jiffies_64_to_clock_t(stats->time_in_state[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63 return len;
64}
65
66#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Dave Jones0a829c52009-01-18 01:49:04 -050067static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Viresh Kumar50941602015-01-06 21:09:07 +053069 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 ssize_t len = 0;
71 int i, j;
72
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020073 if (policy->fast_switch_enabled)
74 return 0;
75
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070076 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
77 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +053078 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (len >= PAGE_SIZE)
80 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070081 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +053082 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070083 }
84 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -020085 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070086
87 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
88
Viresh Kumar50941602015-01-06 21:09:07 +053089 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070090 if (len >= PAGE_SIZE)
91 break;
92
93 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +053094 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Viresh Kumar50941602015-01-06 21:09:07 +053096 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (len >= PAGE_SIZE)
98 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070099 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530100 stats->trans_table[i*stats->max_state+j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200102 if (len >= PAGE_SIZE)
103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
105 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200106 if (len >= PAGE_SIZE)
107 return PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return len;
109}
Viresh Kumardf18e502013-02-04 11:38:52 +0000110cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112
Viresh Kumardf18e502013-02-04 11:38:52 +0000113cpufreq_freq_attr_ro(total_trans);
114cpufreq_freq_attr_ro(time_in_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000117 &total_trans.attr,
118 &time_in_state.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumardf18e502013-02-04 11:38:52 +0000120 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122 NULL
123};
124static struct attribute_group stats_attr_group = {
125 .attrs = default_attrs,
126 .name = "stats"
127};
128
Viresh Kumar50941602015-01-06 21:09:07 +0530129static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530132 for (index = 0; index < stats->max_state; index++)
133 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return index;
135 return -1;
136}
137
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200138void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Viresh Kumar50941602015-01-06 21:09:07 +0530140 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000141
Viresh Kumara9aaf292015-01-13 11:34:00 +0530142 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530143 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530144 return;
145
Viresh Kumar50941602015-01-06 21:09:07 +0530146 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530147
148 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530149 kfree(stats->time_in_state);
150 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530151 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700152}
153
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200154void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530156 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530157 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned int alloc_size;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300159 struct cpufreq_frequency_table *pos, *table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800160
Viresh Kumara685c6d2015-01-06 21:09:11 +0530161 /* We need cpufreq table for creating stats table */
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530162 table = policy->freq_table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800163 if (unlikely(!table))
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200164 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800165
Viresh Kumarb8c67442015-01-06 21:09:01 +0530166 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530167 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200168 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530169
Viresh Kumar50941602015-01-06 21:09:07 +0530170 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530171 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Viresh Kumara685c6d2015-01-06 21:09:11 +0530174 /* Find total allocation size */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300175 cpufreq_for_each_valid_entry(pos, table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200178 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
181 alloc_size += count * count * sizeof(int);
182#endif
Viresh Kumara685c6d2015-01-06 21:09:11 +0530183
184 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530185 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530186 if (!stats->time_in_state)
187 goto free_stat;
188
Viresh Kumar50941602015-01-06 21:09:07 +0530189 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumar50941602015-01-06 21:09:07 +0530192 stats->trans_table = stats->freq_table + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#endif
Viresh Kumara685c6d2015-01-06 21:09:11 +0530194
195 stats->max_state = count;
196
197 /* Find valid-unique entries */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300198 cpufreq_for_each_valid_entry(pos, table)
Viresh Kumar50941602015-01-06 21:09:07 +0530199 if (freq_table_get_index(stats, pos->frequency) == -1)
200 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530201
Viresh Kumar490285c2015-01-06 21:09:15 +0530202 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530203 stats->last_time = get_jiffies_64();
204 stats->last_index = freq_table_get_index(stats, policy->cur);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530205
206 policy->stats = stats;
207 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
208 if (!ret)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200209 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530210
211 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530212 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530213 kfree(stats->time_in_state);
214free_stat:
215 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200218void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
219 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530220{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200221 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int old_index, new_index;
223
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200224 if (!stats) {
Viresh Kumara9aaf292015-01-13 11:34:00 +0530225 pr_debug("%s: No stats found\n", __func__);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200226 return;
Viresh Kumara9aaf292015-01-13 11:34:00 +0530227 }
228
Viresh Kumar50941602015-01-06 21:09:07 +0530229 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200230 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Viresh Kumar50941602015-01-06 21:09:07 +0530232 /* We can't do stats->time_in_state[-1]= .. */
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200233 if (old_index == -1 || new_index == -1 || old_index == new_index)
234 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800235
Viresh Kumare7347692015-01-06 21:09:14 +0530236 cpufreq_stats_update(stats);
237
Viresh Kumar50941602015-01-06 21:09:07 +0530238 stats->last_index = new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumar50941602015-01-06 21:09:07 +0530240 stats->trans_table[old_index * stats->max_state + new_index]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#endif
Viresh Kumar50941602015-01-06 21:09:07 +0530242 stats->total_trans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}