1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
* http://www.samsung.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/device.h>
#include <linux/kobject.h>
#include <linux/cpuidle.h>
#include <asm/page.h>
#include <asm/cputype.h>
#include <asm/smp_plat.h>
#include <asm/topology.h>
#include <mach/cpuidle_profiler.h>
#define to_cluster_id(cpu) MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1)
static DEFINE_PER_CPU(struct cpuidle_profile_info, profile_info);
static struct cpuidle_profile_info cpd_info[NUM_CLUSTER];
static bool cpuidle_profile_ongoing;
static ktime_t profile_start_time;
static ktime_t profile_finish_time;
static struct cpuidle_profile_info* cpuidle_profile_get_info(unsigned int state,
unsigned int cpu)
{
/*
* Get profiling information.
* In case of cluster power down, it has own information.
*/
if (state == CPUIDLE_PROFILE_CPD)
return &cpd_info[to_cluster_id(cpu)];
else
return &per_cpu(profile_info, cpu);
}
static void __cpuidle_profile_start(struct cpuidle_profile_info *info,
unsigned int state, ktime_t cur_time)
{
info->entry_count[state]++;
info->entry_time = cur_time;
}
void cpuidle_profile_start(unsigned int state, unsigned int cpu)
{
struct cpuidle_profile_info *info;
ktime_t cur_time;
if (!cpuidle_profile_ongoing)
return;
info = cpuidle_profile_get_info(state, cpu);
info->cur_state = state;
info->entered = 1;
cur_time = ktime_get();
__cpuidle_profile_start(info, state, cur_time);
}
static void __cpuidle_profile_finish(struct cpuidle_profile_info *info,
unsigned int state, ktime_t cur_time)
{
s64 diff;
if (info->entry_time.tv64 <= 0)
return;
diff = ktime_to_us(ktime_sub(cur_time, info->entry_time));
if (diff >= 0)
info->residency[state] += diff;
}
void cpuidle_profile_finish(unsigned int state, unsigned int cpu,
bool early_wakeup)
{
struct cpuidle_profile_info *info;
ktime_t cur_time;
if (!cpuidle_profile_ongoing)
return;
info = cpuidle_profile_get_info(state, cpu);
if (!info->entered)
return;
info->entered = 0;
if (early_wakeup) {
info->early_wakeup_count[state]++;
/*
* If cpu cannot enter power mode, residency time
* should not be updated.
*/
return;
}
cur_time = ktime_get();
__cpuidle_profile_finish(info, state, cur_time);
}
static unsigned int calculate_percent(struct cpuidle_profile_info *info,
unsigned int state)
{
s64 residency, total_time;
residency = info->residency[state];
total_time = ktime_to_us(ktime_sub(profile_finish_time, profile_start_time));
if (residency) {
residency = residency * 100;
do_div(residency, total_time);
}
return residency;
}
static void cpuidle_profile_show_result(void)
{
struct cpuidle_profile_info *info;
unsigned int cpu, cluster;
pr_info("==========================================================================\n");
pr_info("| Total profiling Time = %10lldus |\n",
ktime_to_us(ktime_sub(profile_finish_time, profile_start_time)));
pr_info("--------------------------------------------------------------------------\n");
pr_info("| | C1 entry | C1 residency | C2 entry(early) | C2 residency |\n");
pr_info("--------------------------------------------------------------------------\n");
for_each_possible_cpu(cpu) {
info = &per_cpu(profile_info, cpu);
pr_info("| cpu%u | %8u | %10llu(%3u%%) | %9u(%4u) | %10llu(%3u%%) |\n",
cpu,
info->entry_count[CPUIDLE_PROFILE_C1],
info->residency[CPUIDLE_PROFILE_C1],
calculate_percent(info, CPUIDLE_PROFILE_C1),
info->entry_count[CPUIDLE_PROFILE_C2],
info->early_wakeup_count[CPUIDLE_PROFILE_C2],
info->residency[CPUIDLE_PROFILE_C2],
calculate_percent(info, CPUIDLE_PROFILE_C2));
}
pr_info("==========================================================================\n");
pr_info("| | Cluster power down | |\n");
pr_info("--------------------------------------------------------------------------\n");
for (cluster = 0; cluster < NUM_CLUSTER; cluster++) {
info = &cpd_info[cluster];
pr_info("| %6s | %12llu(%3u%%) | |\n",
cluster ? "LITTLE" : "BIG",
info->residency[CPUIDLE_PROFILE_CPD],
calculate_percent(info, CPUIDLE_PROFILE_CPD));
}
pr_info("==========================================================================\n");
}
static void cpuidle_profile_clear_single(struct cpuidle_profile_info *info)
{
unsigned int state;
for (state = 0; state < NUM_STATE; state++) {
info->entry_count[state] = 0;
info->early_wakeup_count[state] = 0;
info->residency[state] = 0;
}
info->entered = 0;
info->entry_time.tv64 = 0;
}
static void cpuidle_profile_single_start(void *p)
{
unsigned int cpu = smp_processor_id();
struct cpuidle_profile_info *info = &per_cpu(profile_info, cpu);
cpuidle_profile_clear_single(info);
/* The first cpu in cluster clear own cluster data */
if (cpumask_first(cpu_coregroup_mask(cpu))) {
info = &cpd_info[to_cluster_id(cpu)];
cpuidle_profile_clear_single(info);
}
}
static void cpuidle_profile_single_finish(void *p)
{
/* Nothing to do */
return;
}
static void cpuidle_profile_main_start(void)
{
if (cpuidle_profile_ongoing) {
pr_err("CPUIDLE profile is ongoing\n");
return;
}
pr_info("CPUIDLE profile start\n");
/* Save profile starting time */
profile_start_time = ktime_get();
/* Set flag which indicate that profile is ongoing */
cpuidle_profile_ongoing = 1;
/* Wakeup all cpus and clear own profile data to start profile */
preempt_disable();
cpuidle_profile_clear_single(&per_cpu(profile_info, smp_processor_id()));
smp_call_function(cpuidle_profile_single_start, NULL, 1);
preempt_enable();
}
static void cpuidle_profile_main_finish(void)
{
if (!cpuidle_profile_ongoing) {
pr_err("CPUIDLE profile does not start yet\n");
return;
}
pr_info("CPUIDLE profile finish\n");
/* Wakeup all cpus and update own profile data to finish profile */
preempt_disable();
smp_call_function(cpuidle_profile_single_finish, NULL, 1);
preempt_enable();
/* Clear flag which indicate that profile is ongoing */
cpuidle_profile_ongoing = 0;
/* Get profile finish time to calculate profile time */
profile_finish_time = ktime_get();
/* Show and clear profile result */
cpuidle_profile_show_result();
/* Clear starting and finish time */
profile_start_time.tv64 = 0;
profile_finish_time.tv64 = 0;
}
/*********************************************************************
* SYSFS INTERFACE *
*********************************************************************/
static ssize_t show_cpuidle_profile(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int ret = 0;
if (cpuidle_profile_ongoing)
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"CPUIDLE profile is ongoing\n");
else
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"echo <1/0> > profile\n");
return ret;
}
static ssize_t store_cpuidle_profile(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
int input;
if (!sscanf(buf, "%1d", &input))
return -EINVAL;
switch (input) {
case 1:
cpuidle_profile_main_start();
break;
case 0:
cpuidle_profile_main_finish();
break;
default:
pr_info("echo <1/0> > profile\n");
break;
}
return count;
}
static struct kobj_attribute cpuidle_profile_attr =
__ATTR(profile, 0644, show_cpuidle_profile, store_cpuidle_profile);
static struct attribute *cpuidle_profile_attrs[] = {
&cpuidle_profile_attr.attr,
NULL,
};
static const struct attribute_group cpuidle_profile_group = {
.attrs = cpuidle_profile_attrs,
};
/*********************************************************************
* Initialize cpuidle profiler *
*********************************************************************/
static int __init cpuidle_profile_init(void)
{
struct class *class;
struct device *dev;
int ret;
class = class_create(THIS_MODULE, "cpuidle");
dev = device_create(class, NULL, 0, NULL, "cpuidle_profiler");
ret = sysfs_create_group(&dev->kobj, &cpuidle_profile_group);
if (ret) {
pr_err("CPUIDLE Profiler : error to create sysfs\n");
return -EINVAL;
}
return 0;
}
late_initcall(cpuidle_profile_init);
|