summaryrefstree
path: root/kernel/drivers/cpuidle/cpuidle_profiler.c
blob: 956a207ddac316d37f7a1d7bd89291ee1eed8a4d (plain)
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
/*
 * 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 <linux/slab.h>

#include <asm/page.h>
#include <asm/cputype.h>
#include <asm/smp_plat.h>
#include <asm/topology.h>

#include <mach/exynos-powermode.h>

#include "cpuidle_profiler.h"

static bool profile_ongoing;

static DEFINE_PER_CPU(struct cpuidle_profile_info, profile_info);
static struct cpuidle_profile_info cpd_info[NUM_CLUSTER];
static struct cpuidle_profile_info lpc_info;
static struct cpuidle_profile_info lpm_info;

/*********************************************************************
 *                         helper function                           *
 *********************************************************************/
#define state_entered(state)	(((int)state < (int)0) ? 0 : 1)

static void profile_start(struct cpuidle_profile_info *info , int state, ktime_t now)
{
	if (state_entered(info->cur_state))
		return;

	info->cur_state = state;
	info->last_entry_time = now;

	info->usage[state].entry_count++;
}

static void count_earlywakeup(struct cpuidle_profile_info *info , int state)
{
	if (!state_entered(info->cur_state))
		return;

	info->cur_state = -EINVAL;
	info->usage[state].early_wakeup_count++;
}

static void update_time(struct cpuidle_profile_info *info , int state, ktime_t now)
{
	s64 diff;

	if (!state_entered(info->cur_state))
		return;

	info->cur_state = -EINVAL;

	diff = ktime_to_us(ktime_sub(now, info->last_entry_time));
	info->usage[state].time += diff;
}

/*********************************************************************
 *                    sub state exclusive function                   *
 *********************************************************************/
#define is_state_cpd(state)		((state & CPD_STATE) ? 1 : 0)
#define to_cluster(cpu)			MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1)

static void cpd_profile_start(int state, ktime_t now, int cpu)
{
	if (!is_state_cpd(state))
		return;

	profile_start(&cpd_info[to_cluster(cpu)], 0, now);
}

static void cpd_earlywakeup(int cpu)
{
	count_earlywakeup(&cpd_info[to_cluster(cpu)], 0);
}

static void cpd_update_time(ktime_t now, int cpu)
{
	update_time(&cpd_info[to_cluster(cpu)], 0, now);
}

#define is_state_lpc(state)		((state & LPC_STATE) ? 1 : 0)

static void lpc_profile_start(int state, ktime_t now)
{
	if (!is_state_lpc(state))
		return;

	profile_start(&lpc_info, 0, now);
}

static void lpc_earlywakeup(void)
{
	count_earlywakeup(&lpc_info, 0);
}

static void lpc_update_time(ktime_t now)
{
	update_time(&lpc_info, 0, now);
}

#define get_lpm_substate(state)		(state >> LPM_SUB_STATE_OFFSET)

static void lpm_profile_start(int state, ktime_t now)
{
	if ((state & MAJOR_STATE) != LPM_STATE)
		return;

	profile_start(&lpm_info, get_lpm_substate(state), now);
}

static void lpm_earlywakeup(void)
{
	count_earlywakeup(&lpm_info, lpm_info.cur_state);
}

static void lpm_update_time(ktime_t now)
{
	update_time(&lpm_info, lpm_info.cur_state, now);
}

/*********************************************************************
 *                Information gathering function                     *
 *********************************************************************/
static DEFINE_SPINLOCK(substate_lock);

void cpuidle_profile_start(int cpu, int state)
{
	struct cpuidle_profile_info *info;
	ktime_t now;

	if (!profile_ongoing)
		return;

	now = ktime_get();
	info = &per_cpu(profile_info, cpu);

	profile_start(info, state & MAJOR_STATE, now);

	spin_lock(&substate_lock);
	cpd_profile_start(state, now, cpu);
	lpc_profile_start(state, now);
	lpm_profile_start(state, now);
	spin_unlock(&substate_lock);
}

void cpuidle_profile_finish(int cpu, int early_wakeup)
{
	struct cpuidle_profile_info *info;
	int state;
	ktime_t now;

	if (!profile_ongoing)
		return;

	info = &per_cpu(profile_info, cpu);
	state = info->cur_state;

	if (early_wakeup) {
		count_earlywakeup(info, state);

		spin_lock(&substate_lock);
		cpd_earlywakeup(cpu);
		lpc_earlywakeup();
		lpm_earlywakeup();
		spin_unlock(&substate_lock);

		/*
		 * If cpu cannot enter power mode, residency time
		 * should not be updated.
		 */
		return;
	}

	now = ktime_get();
	update_time(info, state, now);

	spin_lock(&substate_lock);
	cpd_update_time(now, cpu);
	lpc_update_time(now);
	lpm_update_time(now);
	spin_unlock(&substate_lock);
}

#define MAX_NUM_BLOCKER		2

static int lpa_blocker[MAX_NUM_BLOCKER];
void lpa_blocking_counter(int blocker)
{
	if (!profile_ongoing)
		return;

	if (blocker >= MAX_NUM_BLOCKER)
		return;

	lpa_blocker[blocker]++;
}

/*********************************************************************
 *                            Show result                            *
 *********************************************************************/
static ktime_t profile_start_time;
static ktime_t profile_finish_time;
static s64 profile_time;

int state_count;

static char * sys_powerdown_str[NUM_SYS_POWERDOWN] = {
	"AFTR",
	"STOP",
	"DSTOP",
	"DSTOP_PSR",
	"LPD",
	"LPA",
	"ALPA",
	"SLEEP"
};

static char * lpa_blocker_str[MAX_NUM_BLOCKER] = {
	"IP",
	"REGISTER",
};

#define get_sys_powerdown_str(mode)	sys_powerdown_str[mode]
#define get_lpa_blocker_str(mode)	lpa_blocker_str[mode]

static int calculate_percent(s64 residency)
{
	if (!residency)
		return 0;

	residency *= 100;
	do_div(residency, profile_time);

	return residency;
}

static void show_result(void)
{
	int i, cpu;
	struct cpuidle_profile_info *info;

	pr_info("#######################################################################\n");
	pr_info("Profiling Time : %lluus\n", profile_time);

	for (i = 0; i < state_count; i++) {
		pr_info("[state%d]\n", i);
		pr_info("#cpu   #entry   #early      #time    #ratio\n");
		for_each_possible_cpu(cpu) {
			info = &per_cpu(profile_info, cpu);
			pr_info("cpu%d   %5u   %5u   %10lluus   %3u%%\n", cpu,
				info->usage[i].entry_count,
				info->usage[i].early_wakeup_count,
				info->usage[i].time,
				calculate_percent(info->usage[i].time));
		}

		pr_info("\n");
	}

	pr_info("[CPD] - Cluster Power Down\n");
	pr_info("#cluster    #entry   #early      #time     #ratio\n");
	for (i = 0; i < NUM_CLUSTER; i++) {
		pr_info("cluster%d    %5u    %5u  %10lluus    %3u%%\n", i,
			cpd_info[i].usage->entry_count,
			cpd_info[i].usage->early_wakeup_count,
			cpd_info[i].usage->time,
			calculate_percent(cpd_info[i].usage->time));
	}

	pr_info("\n");

	pr_info("[LPC] - Low Power mode with Clock down\n");
	pr_info("            #entry   #early      #time     #ratio\n");
	pr_info("system      %5u    %5u  %10lluus    %3u%%\n",
		lpc_info.usage->entry_count,
		lpc_info.usage->early_wakeup_count,
		lpc_info.usage->time,
		calculate_percent(lpc_info.usage->time));

	pr_info("\n");

	pr_info("[LPM] - Low Power Mode\n");
	pr_info("#mode       #entry   #early      #time     #ratio\n");
	for (i = 0; i < NUM_SYS_POWERDOWN; i++) {
		pr_info("%-9s   %5u    %5u  %10lluus    %3u%%\n",
			get_sys_powerdown_str(i),
			lpm_info.usage[i].entry_count,
			lpm_info.usage[i].early_wakeup_count,
			lpm_info.usage[i].time,
			calculate_percent(lpm_info.usage[i].time));
	}

	pr_info("\n");

	pr_info("[LPA blockers]\n");
	for (i = 0; i < MAX_NUM_BLOCKER; i++)
		pr_info("%-9s: %d\n", get_lpa_blocker_str(i), lpa_blocker[i]);

	pr_info("\n");
	pr_info("#######################################################################\n");
}

/*********************************************************************
 *                      Main profile function                        *
 *********************************************************************/
static void clear_profile_state_usage(struct cpuidle_profile_state_usage *usage)
{
	usage->entry_count = 0;
	usage->early_wakeup_count = 0;
	usage->time = 0;
}

static void clear_profile_info(struct cpuidle_profile_info *info, int state_num)
{
	int state;

	for (state = 0; state < state_num; state++)
		clear_profile_state_usage(&info->usage[state]);

	info->cur_state = -EINVAL;
	info->last_entry_time.tv64 = 0;
}

static void clear_time(void)
{
	int i;

	profile_start_time.tv64 = 0;
	profile_finish_time.tv64 = 0;

	for_each_possible_cpu(i)
		clear_profile_info(&per_cpu(profile_info, i), state_count);

	for (i = 0; i < NUM_CLUSTER; i++)
		clear_profile_info(&cpd_info[i], 1);

	clear_profile_info(&lpc_info, 1);
	clear_profile_info(&lpm_info, NUM_SYS_POWERDOWN);

	for (i = 0; i < MAX_NUM_BLOCKER; i++)
		lpa_blocker[i] = 0;
}

static void call_cpu_start_profile(void *p) {};
static void call_cpu_finish_profile(void *p) {};

static void cpuidle_profile_main_start(void)
{
	if (profile_ongoing) {
		pr_err("cpuidle profile is ongoing\n");
		return;
	}

	clear_time();
	profile_start_time = ktime_get();

	profile_ongoing = 1;

	/* Wakeup all cpus and clear own profile data to start profile */
	preempt_disable();
	clear_profile_info(&per_cpu(profile_info, smp_processor_id()), state_count);
	smp_call_function(call_cpu_start_profile, NULL, 1);
	preempt_enable();

	pr_info("cpuidle profile start\n");
}

static void cpuidle_profile_main_finish(void)
{
	if (!profile_ongoing) {
		pr_err("CPUIDLE profile does not start yet\n");
		return;
	}

	pr_info("cpuidle profile finish\n");

	/* Wakeup all cpus to update own profile data to finish profile */
	preempt_disable();
	smp_call_function(call_cpu_finish_profile, NULL, 1);
	preempt_enable();

	profile_ongoing = 0;

	profile_finish_time = ktime_get();
	profile_time = ktime_to_us(ktime_sub(profile_finish_time,
						profile_start_time));

	show_result();
}

/*********************************************************************
 *                          Sysfs interface                          *
 *********************************************************************/
static ssize_t show_cpuidle_profile(struct kobject *kobj,
				     struct kobj_attribute *attr,
				     char *buf)
{
	int ret = 0;

	if (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;

	if (!!input)
		cpuidle_profile_main_start();
	else
		cpuidle_profile_main_finish();

	return count;
}

static ssize_t show_cpuidle_result(struct kobject *kobj,
				     struct kobj_attribute *attr,
				     char *buf)
{
	int ret = 0;
	int i, cpu;
	struct cpuidle_profile_info *info;

	if (profile_ongoing) {
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
				"CPUIDLE profile is ongoing\n");
		return ret;
	}

	if (profile_time == 0) {
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
				"CPUIDLE profiler has not started yet\n");
		return ret;
	}

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"#######################################################################\n");
	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"Profiling Time : %lluus\n", profile_time);

	for (i = 0; i < state_count; i++) {
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
						"[state%d]\n", i);
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
						"#cpu   #entry   #early      #time    #ratio\n");
		for_each_possible_cpu(cpu) {
			info = &per_cpu(profile_info, cpu);
			ret += snprintf(buf + ret, PAGE_SIZE - ret,
							"cpu%d   %5u   %5u   %10lluus   %3u%%\n", cpu,
				info->usage[i].entry_count,
				info->usage[i].early_wakeup_count,
				info->usage[i].time,
				calculate_percent(info->usage[i].time));
		}

		ret += snprintf(buf + ret, PAGE_SIZE - ret,
						"\n");
	}

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"[CPD] - Cluster Power Down\n");
	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"#cluster    #entry   #early      #time     #ratio\n");
	for (i = 0; i < NUM_CLUSTER; i++) {
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
						"cluster%d    %5u    %5u  %10lluus    %3u%%\n", i,
			cpd_info[i].usage->entry_count,
			cpd_info[i].usage->early_wakeup_count,
			cpd_info[i].usage->time,
			calculate_percent(cpd_info[i].usage->time));
	}

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"\n");

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"[LPC] - Low Power mode with Clock down\n");
	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"            #entry   #early      #time     #ratio\n");
	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"system      %5u    %5u  %10lluus    %3u%%\n",
		lpc_info.usage->entry_count,
		lpc_info.usage->early_wakeup_count,
		lpc_info.usage->time,
		calculate_percent(lpc_info.usage->time));

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"\n");

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"[LPM] - Low Power Mode\n");
	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"#mode       #entry   #early      #time     #ratio\n");
	for (i = 0; i < NUM_SYS_POWERDOWN; i++) {
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
						"%-9s   %5u    %5u  %10lluus    %3u%%\n",
			get_sys_powerdown_str(i),
			lpm_info.usage[i].entry_count,
			lpm_info.usage[i].early_wakeup_count,
			lpm_info.usage[i].time,
			calculate_percent(lpm_info.usage[i].time));
	}

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"\n");

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"[LPA blockers]\n");
	for (i = 0; i < MAX_NUM_BLOCKER; i++)
		ret += snprintf(buf + ret, PAGE_SIZE - ret,
						"%-9s: %d\n", get_lpa_blocker_str(i), lpa_blocker[i]);

	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"\n");
	ret += snprintf(buf + ret, PAGE_SIZE - ret,
					"#######################################################################\n");

	return ret;
}

static struct kobj_attribute cpuidle_profile_attr =
	__ATTR(profile, 0644, show_cpuidle_profile, store_cpuidle_profile);

static struct kobj_attribute cpuidle_result_attr =
	__ATTR(result, 0444, show_cpuidle_result, NULL);


static struct attribute *cpuidle_profile_attrs[] = {
	&cpuidle_profile_attr.attr,
	&cpuidle_result_attr.attr,
	NULL,
};

static const struct attribute_group cpuidle_profile_group = {
	.attrs = cpuidle_profile_attrs,
};


/*********************************************************************
 *                   Initialize cpuidle profiler                     *
 *********************************************************************/
static void __init cpuidle_profile_usage_init(void)
{
	int i, size;
	struct cpuidle_profile_info *info;

	if (!state_count) {
		pr_err("%s: cannot get the number of cpuidle state\n", __func__);
		return;
	}

	size = sizeof(struct cpuidle_profile_state_usage) * state_count;
	for_each_possible_cpu(i) {
		info = &per_cpu(profile_info, i);

		info->usage = kmalloc(size, GFP_KERNEL);
		if (!info->usage) {
			pr_err("%s:%d: Memory allocation failed\n", __func__, __LINE__);
			return;
		}
	}

	size = sizeof(struct cpuidle_profile_state_usage);

	for (i = 0; i < NUM_CLUSTER; i++) {
		cpd_info[i].usage = kmalloc(size, GFP_KERNEL);
		if (!cpd_info[i].usage) {
			pr_err("%s:%d: Memory allocation failed\n", __func__, __LINE__);
			return;
		}
	}

	lpc_info.usage = kmalloc(size, GFP_KERNEL);
	if (!lpc_info.usage) {
		pr_err("%s:%d: Memory allocation failed\n", __func__, __LINE__);
		return;
	}

	size = sizeof(struct cpuidle_profile_state_usage) * NUM_SYS_POWERDOWN;
	lpm_info.usage = kmalloc(size, GFP_KERNEL);
	if (!lpm_info.usage) {
		pr_err("%s:%d: Memory allocation failed\n", __func__, __LINE__);
		return;
	}
}

void __init cpuidle_profile_state_init(struct cpuidle_driver *drv)
{
	state_count = drv->state_count;

	cpuidle_profile_usage_init();
}

static int __init cpuidle_profile_init(void)
{
	struct class *class;
	struct device *dev;

	class = class_create(THIS_MODULE, "cpuidle");
	dev = device_create(class, NULL, 0, NULL, "cpuidle_profiler");

	if (sysfs_create_group(&dev->kobj, &cpuidle_profile_group)) {
		pr_err("CPUIDLE Profiler : error to create sysfs\n");
		return -EINVAL;
	}

	return 0;
}
late_initcall(cpuidle_profile_init);