blob: 06d3abdffd3a39e391f27d644ec8b2d19a7e0fb5 [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
Dave Jones0a829c52009-01-18 01:49:04 -050044static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Viresh Kumara9aaf292015-01-13 11:34:00 +053046 return sprintf(buf, "%d\n", policy->stats->total_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Dave Jones0a829c52009-01-18 01:49:04 -050049static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Viresh Kumar50941602015-01-06 21:09:07 +053051 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 ssize_t len = 0;
53 int i;
Viresh Kumara9aaf292015-01-13 11:34:00 +053054
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020055 if (policy->fast_switch_enabled)
56 return 0;
57
Viresh Kumar50941602015-01-06 21:09:07 +053058 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],
Dave Jones0a829c52009-01-18 01:49:04 -050061 (unsigned long long)
Viresh Kumar50941602015-01-06 21:09:07 +053062 jiffies_64_to_clock_t(stats->time_in_state[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64 return len;
65}
66
67#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Dave Jones0a829c52009-01-18 01:49:04 -050068static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Viresh Kumar50941602015-01-06 21:09:07 +053070 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 ssize_t len = 0;
72 int i, j;
73
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +020074 if (policy->fast_switch_enabled)
75 return 0;
76
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070077 len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
78 len += snprintf(buf + len, PAGE_SIZE - len, " : ");
Viresh Kumar50941602015-01-06 21:09:07 +053079 for (i = 0; i < stats->state_num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (len >= PAGE_SIZE)
81 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070082 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +053083 stats->freq_table[i]);
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070084 }
85 if (len >= PAGE_SIZE)
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -020086 return PAGE_SIZE;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070087
88 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
89
Viresh Kumar50941602015-01-06 21:09:07 +053090 for (i = 0; i < stats->state_num; i++) {
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -070091 if (len >= PAGE_SIZE)
92 break;
93
94 len += snprintf(buf + len, PAGE_SIZE - len, "%9u: ",
Viresh Kumar50941602015-01-06 21:09:07 +053095 stats->freq_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Viresh Kumar50941602015-01-06 21:09:07 +053097 for (j = 0; j < stats->state_num; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (len >= PAGE_SIZE)
99 break;
Venkatesh Pallipadi58f1df22005-05-25 14:46:50 -0700100 len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
Viresh Kumar50941602015-01-06 21:09:07 +0530101 stats->trans_table[i*stats->max_state+j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200103 if (len >= PAGE_SIZE)
104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
106 }
Cesar Eduardo Barros25aca342008-02-16 08:41:25 -0200107 if (len >= PAGE_SIZE)
108 return PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return len;
110}
Viresh Kumardf18e502013-02-04 11:38:52 +0000111cpufreq_freq_attr_ro(trans_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113
Viresh Kumardf18e502013-02-04 11:38:52 +0000114cpufreq_freq_attr_ro(total_trans);
115cpufreq_freq_attr_ro(time_in_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static struct attribute *default_attrs[] = {
Viresh Kumardf18e502013-02-04 11:38:52 +0000118 &total_trans.attr,
119 &time_in_state.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumardf18e502013-02-04 11:38:52 +0000121 &trans_table.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123 NULL
124};
125static struct attribute_group stats_attr_group = {
126 .attrs = default_attrs,
127 .name = "stats"
128};
129
Viresh Kumar50941602015-01-06 21:09:07 +0530130static int freq_table_get_index(struct cpufreq_stats *stats, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 int index;
Viresh Kumar50941602015-01-06 21:09:07 +0530133 for (index = 0; index < stats->max_state; index++)
134 if (stats->freq_table[index] == freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return index;
136 return -1;
137}
138
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200139void cpufreq_stats_free_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Viresh Kumar50941602015-01-06 21:09:07 +0530141 struct cpufreq_stats *stats = policy->stats;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000142
Viresh Kumara9aaf292015-01-13 11:34:00 +0530143 /* Already freed */
Viresh Kumar50941602015-01-06 21:09:07 +0530144 if (!stats)
Viresh Kumar2d135942014-01-07 07:10:12 +0530145 return;
146
Viresh Kumar50941602015-01-06 21:09:07 +0530147 pr_debug("%s: Free stats table\n", __func__);
Viresh Kumar2d135942014-01-07 07:10:12 +0530148
149 sysfs_remove_group(&policy->kobj, &stats_attr_group);
Viresh Kumar50941602015-01-06 21:09:07 +0530150 kfree(stats->time_in_state);
151 kfree(stats);
Viresh Kumara9aaf292015-01-13 11:34:00 +0530152 policy->stats = NULL;
steven finney98586ed2011-05-02 11:29:17 -0700153}
154
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200155void cpufreq_stats_create_table(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Viresh Kumara685c6d2015-01-06 21:09:11 +0530157 unsigned int i = 0, count = 0, ret = -ENOMEM;
Viresh Kumar50941602015-01-06 21:09:07 +0530158 struct cpufreq_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned int alloc_size;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300160 struct cpufreq_frequency_table *pos, *table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800161
Viresh Kumara685c6d2015-01-06 21:09:11 +0530162 /* We need cpufreq table for creating stats table */
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530163 table = policy->freq_table;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800164 if (unlikely(!table))
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200165 return;
Saravana Kannanad4c2302014-02-27 17:58:36 -0800166
Viresh Kumarb8c67442015-01-06 21:09:01 +0530167 /* stats already initialized */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530168 if (policy->stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200169 return;
Viresh Kumarb8c67442015-01-06 21:09:01 +0530170
Viresh Kumar50941602015-01-06 21:09:07 +0530171 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530172 if (!stats)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Viresh Kumara685c6d2015-01-06 21:09:11 +0530175 /* Find total allocation size */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300176 cpufreq_for_each_valid_entry(pos, table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Viresh Kumar1e7586a2012-10-26 00:51:21 +0200179 alloc_size = count * sizeof(int) + count * sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
182 alloc_size += count * count * sizeof(int);
183#endif
Viresh Kumara685c6d2015-01-06 21:09:11 +0530184
185 /* Allocate memory for time_in_state/freq_table/trans_table in one go */
Viresh Kumar50941602015-01-06 21:09:07 +0530186 stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530187 if (!stats->time_in_state)
188 goto free_stat;
189
Viresh Kumar50941602015-01-06 21:09:07 +0530190 stats->freq_table = (unsigned int *)(stats->time_in_state + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumar50941602015-01-06 21:09:07 +0530193 stats->trans_table = stats->freq_table + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#endif
Viresh Kumara685c6d2015-01-06 21:09:11 +0530195
196 stats->max_state = count;
197
198 /* Find valid-unique entries */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300199 cpufreq_for_each_valid_entry(pos, table)
Viresh Kumar50941602015-01-06 21:09:07 +0530200 if (freq_table_get_index(stats, pos->frequency) == -1)
201 stats->freq_table[i++] = pos->frequency;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530202
Viresh Kumar490285c2015-01-06 21:09:15 +0530203 stats->state_num = i;
Viresh Kumar50941602015-01-06 21:09:07 +0530204 stats->last_time = get_jiffies_64();
205 stats->last_index = freq_table_get_index(stats, policy->cur);
Viresh Kumara685c6d2015-01-06 21:09:11 +0530206
207 policy->stats = stats;
208 ret = sysfs_create_group(&policy->kobj, &stats_attr_group);
209 if (!ret)
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200210 return;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530211
212 /* We failed, release resources */
Viresh Kumara9aaf292015-01-13 11:34:00 +0530213 policy->stats = NULL;
Viresh Kumara685c6d2015-01-06 21:09:11 +0530214 kfree(stats->time_in_state);
215free_stat:
216 kfree(stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200219void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
220 unsigned int new_freq)
Viresh Kumarb3f9ff82014-01-07 07:10:13 +0530221{
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200222 struct cpufreq_stats *stats = policy->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int old_index, new_index;
224
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200225 if (!stats) {
Viresh Kumara9aaf292015-01-13 11:34:00 +0530226 pr_debug("%s: No stats found\n", __func__);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200227 return;
Viresh Kumara9aaf292015-01-13 11:34:00 +0530228 }
229
Viresh Kumar50941602015-01-06 21:09:07 +0530230 old_index = stats->last_index;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200231 new_index = freq_table_get_index(stats, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Viresh Kumar50941602015-01-06 21:09:07 +0530233 /* We can't do stats->time_in_state[-1]= .. */
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200234 if (old_index == -1 || new_index == -1 || old_index == new_index)
235 return;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800236
Viresh Kumare7347692015-01-06 21:09:14 +0530237 cpufreq_stats_update(stats);
238
Viresh Kumar50941602015-01-06 21:09:07 +0530239 stats->last_index = new_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#ifdef CONFIG_CPU_FREQ_STAT_DETAILS
Viresh Kumar50941602015-01-06 21:09:07 +0530241 stats->trans_table[old_index * stats->max_state + new_index]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#endif
Viresh Kumar50941602015-01-06 21:09:07 +0530243 stats->total_trans++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}