summaryrefstree
path: root/kernel/drivers/cpufreq/cpu_load_metric.c
blob: ab780e2f26d047814cb2877ee416f1831b22c8bf (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
/*
 *  ARM Intelligent Power Allocation
 *
 *  Copyright (C) 2013 ARM Limited.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/cpufreq.h>
#include <linux/cpumask.h>
#include <linux/kernel.h>
#include <linux/percpu.h>
#include <linux/types.h>

#include <trace/events/power.h>

#include "cpu_load_metric.h"

struct cpu_load
{
	unsigned int frequency;
	unsigned int load;
	u64 last_update;
};
static DEFINE_PER_CPU(struct cpu_load, cpuload);

void update_cpu_metric(int cpu, u64 now, u64 delta_idle, u64 delta_time,
		       struct cpufreq_policy *policy)
{
	struct cpu_load *pcpuload = &per_cpu(cpuload, cpu);
	unsigned int load;

	/*
	 * Calculate the active time in the previous time window
	 *
	 * load = active time / total_time * 100
	 */
	if (delta_time <= delta_idle)
		load = 0;
	else
		load = div64_u64((100 * (delta_time - delta_idle)), delta_time);

	pcpuload->load = load;
	pcpuload->frequency = policy->cur;
	pcpuload->last_update = now;
#ifdef CONFIG_CPU_THERMAL_IPA_DEBUG
	trace_printk("cpu_load: cpu: %d freq: %u load: %u\n", cpu, policy->cur, load);
#endif
}

void cpu_load_metric_get(int *load, int *freq)
{
	int _load = 0, _freq = 0;
	int cpu;

	for_each_online_cpu(cpu) {
		struct cpu_load *pcpuload = &per_cpu(cpuload, cpu);

		_load += pcpuload->load;
		_freq = pcpuload->frequency;
	}

	*load = _load;
	*freq = _freq;
}

static void get_cluster_stat(struct cluster_stats *cl)
{
	int util = 0, freq = 0;
	int cpu, i = 0;

	for_each_cpu(cpu, cl->mask) {
		struct cpu_load *pcpuload = &per_cpu(cpuload, cpu);
		int load = (cpu_online(cpu)) ?  pcpuload->load : 0;

		util += load;
		cl->utils[i++] = load;
		freq = pcpuload->frequency;
	}

	cl->util = util;
	cl->freq = freq;
}

void get_cluster_stats(struct cluster_stats *clstats)
{
	get_cluster_stat(&clstats[0]);
	get_cluster_stat(&clstats[1]);
}