blob: ac284e66839c6f19c9156432c6f5c4995e6525fb [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>
Frederic Weisbeckerbfc3f022014-03-05 16:33:42 +010016#include <linux/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020018static DEFINE_SPINLOCK(cpufreq_stats_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020struct cpufreq_stats {
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 unsigned int total_trans;
Viresh Kumarbb176f72013-06-19 14:19:33 +053022 unsigned long long last_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 unsigned int max_state;
24 unsigned int state_num;
25 unsigned int last_index;
Viresh Kumar1e7586a2012-10-26 00:51:21 +020026 u64 *time_in_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 unsigned int *freq_table;
28#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
29 unsigned int *trans_table;
30#endif
31};
32
Viresh Kumar50941602015-01-06 21:09:07 +053033static int cpufreq_stats_update(struct cpufreq_stats *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Viresh Kumar95313472015-01-06 21:09:03 +053035 unsigned long long cur_time = get_jiffies_64();
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 spin_lock(&cpufreq_stats_lock);
Viresh Kumarc960f9b2015-01-06 21:09:12 +053038 stats->time_in_state[stats->last_index] += cur_time - stats->last_time;
Viresh Kumar50941602015-01-06 21:09:07 +053039 stats->last_time = cur_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 spin_unlock(&cpufreq_stats_lock);
41 return 0;
42}
43
Markus Mayeree7930e2016-11-07 10:02:23 -080044static 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
Dave Jones0a829c52009-01-18 01:49:04 -050056static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Viresh Kumara9aaf292015-01-13 11:34:00 +053058 return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Dave Jones0a829c52009-01-18 01:49:04 -050061static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Viresh Kumar50941602015-01-06 21:09:07 +053063 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ssize_t len = 0;
65 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053066
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020067 if (policy->fast_switch_enabled)
68 return 0;
69
Viresh Kumar50941602015-01-06 21:09:07 +053070 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],
Dave Jones0a829c52009-01-18 01:49:04 -050073 (unsigned long long)
Viresh Kumar50941602015-01-06 21:09:07 +053074 jiffies_64_to_clock_t(stats->time_in_state[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 return len;
77}
78
Markus Mayeree7930e2016-11-07 10:02:23 -080079static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Dave Jones0a829c52009-01-18 01:49:04 -050088static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Viresh Kumar50941602015-01-06 21:09:07 +053090 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ssize_t len = 0;
92 int i, j;
93
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020094 if (policy->fast_switch_enabled)
95 return 0;
96
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070097 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
98 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +053099 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (len >= PAGE_SIZE)
101 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700102 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530103 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700104 }
105 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200106 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700107
108 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
109
Viresh Kumar50941602015-01-06 21:09:07 +0530110 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700111 if (len >= PAGE_SIZE)
112 break;
113
114 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +0530115 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Viresh Kumar50941602015-01-06 21:09:07 +0530117 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (len >= PAGE_SIZE)
119 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700120 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530121 stats->trans_table[i*stats->max_state+j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200123 if (len >= PAGE_SIZE)
124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
126 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200127 if (len >= PAGE_SIZE)
128 return PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return len;
130}
Viresh Kumardf18e502013-02-04 11:38:52 +0000131cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif
133
Viresh Kumardf18e502013-02-04 11:38:52 +0000134cpufreq_freq_attr_ro(total_trans);
135cpufreq_freq_attr_ro(time_in_state);
Markus Mayeree7930e2016-11-07 10:02:23 -0800136cpufreq_freq_attr_wo(reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000139 &total_trans.attr,
140 &time_in_state.attr,
Markus Mayeree7930e2016-11-07 10:02:23 -0800141 &reset.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumardf18e502013-02-04 11:38:52 +0000143 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#endif
145 NULL
146};
147static struct attribute_group stats_attr_group = {
148 .attrs = default_attrs,
149 .name = "stats"
150};
151
Viresh Kumar50941602015-01-06 21:09:07 +0530152static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530155 for (index = 0; index < stats->max_state; index++)
156 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return index;
158 return -1;
159}
160
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200161void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Viresh Kumar50941602015-01-06 21:09:07 +0530163 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000164
Viresh Kumara9aaf292015-01-13 11:34:00 +0530165 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530166 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530167 return;
168
Viresh Kumar50941602015-01-06 21:09:07 +0530169 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530170
171 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530172 kfree(stats->time_in_state);
173 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530174 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700175}
176
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200177void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530179 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530180 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned int alloc_size;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300182 struct cpufreq_frequency_table *pos, *table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800183
Viresh Kumara685c6d2015-01-06 21:09:11 +0530184 /* We need cpufreq table for creating stats table */
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530185 table = policy->freq_table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800186 if (unlikely(!table))
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200187 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800188
Viresh Kumarb8c67442015-01-06 21:09:01 +0530189 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530190 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200191 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530192
Viresh Kumar50941602015-01-06 21:09:07 +0530193 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530194 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Viresh Kumara685c6d2015-01-06 21:09:11 +0530197 /* Find total allocation size */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300198 cpufreq_for_each_valid_entry(pos, table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200201 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
204 alloc_size += count * count * sizeof(int);
205#endif
Viresh Kumara685c6d2015-01-06 21:09:11 +0530206
207 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530208 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530209 if (!stats->time_in_state)
210 goto free_stat;
211
Viresh Kumar50941602015-01-06 21:09:07 +0530212 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumar50941602015-01-06 21:09:07 +0530215 stats->trans_table = stats->freq_table + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
Viresh Kumara685c6d2015-01-06 21:09:11 +0530217
218 stats->max_state = count;
219
220 /* Find valid-unique entries */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300221 cpufreq_for_each_valid_entry(pos, table)
Viresh Kumar50941602015-01-06 21:09:07 +0530222 if (freq_table_get_index(stats, pos->frequency) == -1)
223 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530224
Viresh Kumar490285c2015-01-06 21:09:15 +0530225 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530226 stats->last_time = get_jiffies_64();
227 stats->last_index = freq_table_get_index(stats, policy->cur);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530228
229 policy->stats = stats;
230 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
231 if (!ret)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200232 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530233
234 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530235 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530236 kfree(stats->time_in_state);
237free_stat:
238 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200241void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
242 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530243{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200244 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int old_index, new_index;
246
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200247 if (!stats) {
Viresh Kumara9aaf292015-01-13 11:34:00 +0530248 pr_debug("%s: No stats found\n", __func__);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200249 return;
Viresh Kumara9aaf292015-01-13 11:34:00 +0530250 }
251
Viresh Kumar50941602015-01-06 21:09:07 +0530252 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200253 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Viresh Kumar50941602015-01-06 21:09:07 +0530255 /* We can't do stats->time_in_state[-1]= .. */
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200256 if (old_index == -1 || new_index == -1 || old_index == new_index)
257 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800258
Viresh Kumare7347692015-01-06 21:09:14 +0530259 cpufreq_stats_update(stats);
260
Viresh Kumar50941602015-01-06 21:09:07 +0530261 stats->last_index = new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumar50941602015-01-06 21:09:07 +0530263 stats->trans_table[old_index * stats->max_state + new_index]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#endif
Viresh Kumar50941602015-01-06 21:09:07 +0530265 stats->total_trans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}