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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
/*
* Copyright (c) 2018 Park Bumgyu, Samsung Electronics Co., Ltd <bumgyu.park@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.
*
* CPUIDLE profiler for Exynos
*/
#include <linux/device.h>
#include <linux/kobject.h>
#include <linux/cpuidle.h>
#include <linux/slab.h>
#include <linux/kdev_t.h>
#include <soc/samsung/exynos-cpupm.h>
#include <soc/samsung/exynos_perf_cpuidle.h>
/* whether profiling has started */
static bool profile_started;
static bool auto_profile_started;
/*
* Represents statistic of idle state.
* All idle states are mapped 1:1 with cpuidle_stats.
*/
struct cpuidle_stats {
/* time to enter idle state */
ktime_t idle_entry_time;
/* number of times an idle state is entered */
unsigned int entry_count;
/* number of times the entry into idle state is canceled */
unsigned int cancel_count;
/* time in idle state */
unsigned long long time;
};
/* description length of idle state */
#define DESC_LEN 32
/*
* Manages idle state where cpu enters individually. One cpu_idle_state
* structure manages a idle state for each cpu to enter, and the number
* of structure is determined by cpuidle driver.
*/
struct cpu_idle_state {
/* description of idle state */
char desc[DESC_LEN];
/* idle state statstics for each cpu */
struct cpuidle_stats stats[NR_CPUS];
};
/* cpu idle state list and length of cpu idle state list */
static struct cpu_idle_state *cpu_idle_state;
static int cpu_idle_state_count;
/*
* Manages idle state in which multiple cpus unit enter. Each idle state
* has one group_idle_state structure.
*/
struct group_idle_state {
/* idle state id, it must be unique */
int id;
/* description of idle state */
char desc[DESC_LEN];
/* idle state statstics */
struct cpuidle_stats stats;
};
/*
* To easily manage group_idle_state dynamically, manage the list as an
* list. Currently, the maximum number of group idle states supported is 5,
* which is unlikely to exceed the number of states empirically.
*/
#define MAX_GROUP_IDLE_STATE 5
/* group idle state list and length of group idle state list */
static struct group_idle_state *group_idle_state[MAX_GROUP_IDLE_STATE];
static int group_idle_state_count;
/* delayed_work for cpuidle auto profiling */
struct delayed_work auto_profile_work;
/************************************************************************
* Profiling *
************************************************************************/
static void idle_enter(struct cpuidle_stats *stats)
{
stats->idle_entry_time = ktime_get();
stats->entry_count++;
}
static void idle_exit(struct cpuidle_stats *stats, int cancel)
{
s64 diff;
/*
* If profiler is started with cpu already in idle state,
* idle_entry_time is 0 because entry event is not recorded.
* From the start of the profile to cpu wakeup is the idle time,
* but ignore this because it is complex to handle it and the
* time is not large.
*/
if (!stats->idle_entry_time)
return;
if (cancel) {
stats->cancel_count++;
return;
}
diff = ktime_to_us(ktime_sub(ktime_get(), stats->idle_entry_time));
stats->time += diff;
stats->idle_entry_time = 0;
}
/*
* cpuidle_profile_cpu_idle_enter/cpuidle_profile_cpu_idle_exit
* : profilie for cpu idle state
*/
void cpuidle_profile_cpu_idle_enter(int cpu, int index)
{
if (!profile_started)
return;
idle_enter(&cpu_idle_state[index].stats[cpu]);
exynos_perf_cpu_idle_enter(cpu, index);
}
void cpuidle_profile_cpu_idle_exit(int cpu, int index, int cancel)
{
if (!profile_started)
return;
exynos_perf_cpu_idle_exit(cpu, index, cancel);
idle_exit(&cpu_idle_state[index].stats[cpu], cancel);
}
/*
* cpuidle_profile_group_idle_enter/cpuidle_profile_group_idle_exit
* : profilie for group idle state
*/
void cpuidle_profile_group_idle_enter(int id)
{
int i;
if (!profile_started)
return;
for (i = 0; i < group_idle_state_count; i++)
if (group_idle_state[i]->id == id)
break;
idle_enter(&group_idle_state[i]->stats);
}
void cpuidle_profile_group_idle_exit(int id, int cancel)
{
int i;
if (!profile_started)
return;
for (i = 0; i < group_idle_state_count; i++)
if (group_idle_state[i]->id == id)
break;
idle_exit(&group_idle_state[i]->stats, cancel);
}
/************************************************************************
* Profile start/stop *
************************************************************************/
/* totoal profiling time */
static s64 profile_time;
/* start time of profile */
static ktime_t profile_start_time;
/* idle-ip */
#define FIX_IDLE_IP_MAX 32
extern struct list_head idle_ip_list;
extern struct fix_idle_ip fix_idle_ip_arr[];
static void clear_stats(struct cpuidle_stats *stats)
{
if (!stats)
return;
stats->idle_entry_time = 0;
stats->entry_count = 0;
stats->cancel_count = 0;
stats->time = 0;
}
static void reset_profile(void)
{
struct idle_ip *ip;
int cpu, i;
profile_start_time = 0;
for (i = 0; i < cpu_idle_state_count; i++)
for_each_possible_cpu(cpu)
clear_stats(&cpu_idle_state[i].stats[cpu]);
for (i = 0; i < group_idle_state_count; i++)
clear_stats(&group_idle_state[i]->stats);
/* Initialize idle-ip count */
for (i = 0; i < FIX_IDLE_IP_MAX; i++)
fix_idle_ip_arr[i].count = 0;
list_for_each_entry(ip, &idle_ip_list, list)
ip->count = 0;
}
static void do_nothing(void *unused)
{
}
static void cpuidle_profile_start(void)
{
if (profile_started) {
pr_err("CPUIDLE: Profile is ongoing\n");
return;
}
exynos_perf_cpuidle_start();
reset_profile();
profile_start_time = ktime_get();
profile_started = 1;
preempt_disable();
/* wakeup all cpus to start profile */
smp_call_function(do_nothing, NULL, 1);
preempt_enable();
pr_info("CPUIDLE: Profile start!!\n");
}
static void cpuidle_profile_stop(void)
{
if (!profile_started) {
pr_err("CPUIDLE: Profile does not start yet\n");
return;
}
if (auto_profile_started) {
pr_err("CPUIDLE: Auto Profile is ongoing\n");
return;
}
exynos_perf_cpuidle_stop();
pr_info("CPUIDLE: Profile is done!!\n");
preempt_disable();
/* wakeup all cpus to stop profile */
smp_call_function(do_nothing, NULL, 1);
preempt_enable();
profile_started = 0;
profile_time = ktime_to_us(ktime_sub(ktime_get(), profile_start_time));
}
static void cpuidle_auto_profile_start(int timeout)
{
if (profile_started) {
pr_err("CPUIDLE: Profile is ongoing\n");
return;
}
cpuidle_profile_start();
auto_profile_started = 1;
schedule_delayed_work(&auto_profile_work,
msecs_to_jiffies(timeout * MSEC_PER_SEC));
}
static void cpuidle_auto_profile_stop(void)
{
if (!profile_started) {
pr_err("CPUIDLE: Profile does not start yet\n");
return;
}
cancel_delayed_work_sync(&auto_profile_work);
auto_profile_started = 0;
cpuidle_profile_stop();
}
static void print_result(void);
static void cpuidle_auto_profile_work(struct work_struct *work)
{
auto_profile_started = 0;
cpuidle_profile_stop();
print_result();
}
/************************************************************************
* IDLE IP *
************************************************************************/
void cpuidle_profile_idle_ip(unsigned long long val)
{
struct idle_ip *ip;
/*
* Return if profile is not started
*/
if (!profile_started)
return;
list_for_each_entry(ip, &idle_ip_list, list) {
/*
* Profile non-idle IP using @val
*
* A bit of val is
* == 0, it means idle.
* == 1, it means non-idle.
* So, profiler only count IP with a bit value of 1.
*/
if (val & ((unsigned long long)1 << ip->index))
ip->count++;
}
}
void cpuidle_profile_fix_idle_ip(unsigned int fix_idle_ip, int max_index)
{
int i;
unsigned int val = 1;
/*
* Return if profile is not started
*/
if (!profile_started)
return;
for (i = 0; i < max_index; i++)
if (fix_idle_ip & val << i)
fix_idle_ip_arr[i].count++;
}
/************************************************************************
* Show result *
************************************************************************/
static int calculate_percent(s64 residency)
{
if (!residency)
return 0;
residency *= 100;
do_div(residency, profile_time);
return residency;
}
static unsigned long long cpu_idle_time(int cpu)
{
unsigned long long idle_time = 0;
int i;
for (i = 0; i < cpu_idle_state_count; i++)
idle_time += cpu_idle_state[i].stats[cpu].time;
return idle_time;
}
static int cpu_idle_ratio(int cpu)
{
return calculate_percent(cpu_idle_time(cpu));
}
static ssize_t show_result(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct idle_ip *ip;
int ret = 0;
int cpu, i;
if (profile_started)
return snprintf(buf, PAGE_SIZE, "CPUIDLE: Profile is ongoing\n");
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"#############################################################\n");
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"Profiling Time : %lluus\n", profile_time);
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"\n[total idle ratio]\n");
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"#cpu #time #ratio\n");
for_each_possible_cpu(cpu)
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"cpu%d %10lluus %3u%%\n",
cpu, cpu_idle_time(cpu), cpu_idle_ratio(cpu));
/*
* Example of cpu idle state profile result.
* Below is an example from the quad core architecture. The number of
* rows depends on the number of cpu.
*
* [state : {desc}]
* #cpu #entry #cancel #time #ratio
* cpu0 985 8 8808916us 87%
* cpu1 340 2 8311318us 82%
* cpu2 270 7 8744801us 87%
* cpu3 330 2 9001329us 89%
*/
for (i = 0; i < cpu_idle_state_count; i++) {
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"\n[state : %s]\n", cpu_idle_state[i].desc);
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"#cpu #entry #cancel #time #ratio\n");
for_each_possible_cpu(cpu) {
struct cpuidle_stats *stats = &cpu_idle_state[i].stats[cpu];
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"cpu%d %5u %5u %10lluus %3u%%\n",
cpu,
stats->entry_count,
stats->cancel_count,
stats->time,
calculate_percent(stats->time));
}
}
/*
* Example of group idle state profile result.
* The number of results depends on the number of group idle state.
*
* [state : {desc}]
* #entry #cancel #time #ratio
* 52 1 4296397us 42%
*
* [state : {desc}]
* #entry #cancel #time #ratio
* 20 0 2230528us 22%
*/
for (i = 0; i < group_idle_state_count; i++) {
struct cpuidle_stats *stats = &group_idle_state[i]->stats;
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"\n[state : %s]\n", group_idle_state[i]->desc);
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"#entry #cancel #time #ratio\n");
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"%5u %5u %10lluus %3u%%\n",
stats->entry_count,
stats->cancel_count,
stats->time,
calculate_percent(stats->time));
}
ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n[IDLE-IP statistics]\n");
/* fix-idle-ip */
for (i = 0; i < FIX_IDLE_IP_MAX; i++)
if (fix_idle_ip_arr[i].count)
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"busy IP : %s(count = %d)\n",
fix_idle_ip_arr[i].name, fix_idle_ip_arr[i].count);
/* idle-ip */
list_for_each_entry(ip, &idle_ip_list, list)
if (ip->count)
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"busy IP : %s(count = %d)\n",
ip->name, ip->count);
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"#############################################################\n");
return ret;
}
static void print_result(void)
{
struct idle_ip *ip;
int cpu, i;
if (profile_started) {
pr_info("CPUIDLE: Profile is ongoing\n");
return;
}
pr_info("#############################################################\n");
pr_info("Profiling Time : %lluus\n", profile_time);
/* total idle ratio */
pr_info("\n[total idle ratio]\n");
pr_info("#cpu #time #ratio\n");
for_each_possible_cpu(cpu)
pr_info("cpu%d %10lluus %3u%%\n",
cpu, cpu_idle_time(cpu), cpu_idle_ratio(cpu));
/* cpu idle state */
for (i = 0; i < cpu_idle_state_count; i++) {
pr_info("\n[state : %s]\n", cpu_idle_state[i].desc);
pr_info("#cpu #entry #cancel #time #ratio\n");
for_each_possible_cpu(cpu) {
struct cpuidle_stats *stats = &cpu_idle_state[i].stats[cpu];
pr_info("cpu%d %5u %5u %10lluus %3u%%\n",
cpu,
stats->entry_count,
stats->cancel_count,
stats->time,
calculate_percent(stats->time));
}
}
/* group idle state (clsuter or system) */
for (i = 0; i < group_idle_state_count; i++) {
struct cpuidle_stats *stats = &group_idle_state[i]->stats;
pr_info("\n[state : %s]\n", group_idle_state[i]->desc);
pr_info("#entry #cancel #time #ratio\n");
pr_info("%5u %5u %10lluus %3u%%\n",
stats->entry_count,
stats->cancel_count,
stats->time,
calculate_percent(stats->time));
}
pr_info("\n[IDLE-IP statistics]\n");
/* fix-idle-ip */
for (i = 0; i < FIX_IDLE_IP_MAX; i++)
if (fix_idle_ip_arr[i].count)
pr_info("busy IP : %s(count = %d)\n",
fix_idle_ip_arr[i].name, fix_idle_ip_arr[i].count);
/* idle-ip */
list_for_each_entry(ip, &idle_ip_list, list)
if (ip->count)
pr_info("busy IP : %s(count = %d)\n",
ip->name, ip->count);
pr_info("#############################################################\n");
}
/*********************************************************************
* Sysfs interface *
*********************************************************************/
static ssize_t show_cpuidle_profile(struct device *dev,
struct device_attribute *attr,
char *buf)
{
if (profile_started)
return snprintf(buf, PAGE_SIZE, "CPUIDLE: Profile is ongoing\n");
else
return show_result(dev, attr, buf);
}
static ssize_t store_cpuidle_profile(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int input;
if (!sscanf(buf, "%1d", &input))
return -EINVAL;
if (!!input)
cpuidle_profile_start();
else
cpuidle_profile_stop();
return count;
}
static ssize_t show_cpuidle_auto_profile(struct device *dev,
struct device_attribute *attr,
char *buf)
{
if (profile_started)
return snprintf(buf, PAGE_SIZE, "CPUIDLE: Auto profile is ongoing\n");
else
return snprintf(buf, PAGE_SIZE, "CPUIDLE: Auto profile is done\n");
}
static ssize_t store_cpuidle_auto_profile(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned int timeout;
if (!sscanf(buf, "%u", &timeout))
return -EINVAL;
if (timeout)
cpuidle_auto_profile_start(timeout);
else
cpuidle_auto_profile_stop();
return count;
}
static DEVICE_ATTR(profile, 0644, show_cpuidle_profile, store_cpuidle_profile);
static DEVICE_ATTR(auto_profile, 0644, show_cpuidle_auto_profile, store_cpuidle_auto_profile);
static struct attribute *cpuidle_profile_attrs[] = {
&dev_attr_profile.attr,
&dev_attr_auto_profile.attr,
NULL,
};
static const struct attribute_group cpuidle_profile_group = {
.attrs = cpuidle_profile_attrs,
};
/*********************************************************************
* Initialize cpuidle profiler *
*********************************************************************/
void __init
cpuidle_profile_cpu_idle_register(struct cpuidle_driver *drv)
{
struct cpu_idle_state *state;
int state_count = drv->state_count;
int i;
state = kzalloc(sizeof(struct cpu_idle_state) * state_count,
GFP_KERNEL);
if (!state) {
pr_err("%s: Failed to allocate memory\n", __func__);
return;
}
for (i = 0; i < state_count; i++)
strncpy(state[i].desc, drv->states[i].desc, DESC_LEN - 1);
cpu_idle_state = state;
cpu_idle_state_count = state_count;
exynos_perf_cpu_idle_register(drv);
}
void __init
cpuidle_profile_group_idle_register(int id, const char *name)
{
struct group_idle_state *state;
state = kzalloc(sizeof(struct group_idle_state), GFP_KERNEL);
if (!state) {
pr_err("%s: Failed to allocate memory\n", __func__);
return;
}
state->id = id;
strncpy(state->desc, name, DESC_LEN - 1);
group_idle_state[group_idle_state_count] = state;
group_idle_state_count++;
}
extern struct class *idle_class;
static int __init cpuidle_profile_init(void)
{
struct device *dev;
int ret = 0;
dev = device_create(idle_class, NULL, MKDEV(0, 0), NULL, "cpuidle_profiler");
ret = sysfs_create_group(&dev->kobj, &cpuidle_profile_group);
if (ret)
pr_err("%s: failed to create sysfs group", __func__);
else
INIT_DELAYED_WORK(&auto_profile_work, cpuidle_auto_profile_work);
return ret;
}
late_initcall(cpuidle_profile_init);
|