summaryrefslogtreecommitdiff
path: root/drivers/cpuidle/cpuidle-debugfs.c
blob: 237e3a5c57df0682c55b10cf511e1ec6ff90bd80 (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
#include <linux/atomic.h>
#include <linux/cpuidle.h>
#include <linux/cpumask.h>
#include <linux/debugfs.h>
#include <linux/slab.h>

static struct dentry *top_dentry;
static struct dentry *pred_dentry;
static struct dentry *under_dentry;
static struct dentry *over_dentry;
static struct dentry *good_dentry;

struct cpuidle_debugfs_stats {
	struct dentry *under_dentry;
	struct dentry *over_dentry;
	struct dentry *good_dentry;
	struct dentry *state_dentry;
	unsigned long long good;
	unsigned long long under;
	unsigned long long over;
};

void cpuidle_debugfs_update(struct cpuidle_device *dev, int index)
{
	struct cpuidle_driver *drv;
	int diff = dev->last_residency;

	drv = cpuidle_get_cpu_driver(dev);
	if (!drv)
		return;

	/*
	 * If the residency time is:
	 *
	 * - more or equal than the target residency's selected idle
	 *   state and less than the next one, the prediction is correct
	 *
	 * - less than the the target residency's selected idle state,
	 *   the prediction is too late
	 *
	 * - more than the the next idle's target residency, the
	 *   prediction is too early
	 */
	if (drv->states[index].target_residency <= diff) {
		/* It is not the last state, check against the next one */
		if (index < drv->state_count - 1) {
			/* The prediction was shorter than expected */
			if (drv->states[index + 1].target_residency <= diff)
				dev->stats[index].under++;
			else
				dev->stats[index].good++;
		} else {
			dev->stats[index].good++;
		}
	} else {
		dev->stats[index].over++;
	}
}

void cpuidle_debugfs_remove_stats(struct cpuidle_debugfs_stats *stats)
{
	debugfs_remove(stats->under_dentry);
	debugfs_remove(stats->good_dentry);
	debugfs_remove(stats->over_dentry);
}

void cpuidle_debugfs_remove_device(struct cpuidle_device *dev)
{
	struct cpuidle_driver *drv;
	int i;

	drv = cpuidle_get_cpu_driver(dev);
	if (!drv)
		return;

	for (i = 0; i < drv->state_count; i++) {
		cpuidle_debugfs_remove_stats(&dev->stats[i]);
		debugfs_remove(dev->stats[i].state_dentry);
	}

	debugfs_remove(dev->debug);
}

int cpuidle_debugfs_add_stats(struct cpuidle_debugfs_stats *stats)
{
	int ret = -ENOMEM;

	stats->good_dentry = debugfs_create_u64("good", 0444,
						stats->state_dentry,
						&stats->good);
	if (!stats->good_dentry)
		goto out;

	stats->under_dentry = debugfs_create_u64("under", 0444,
						 stats->state_dentry,
						 &stats->under);
	if (!stats->under_dentry)
		goto out_remove_good;

	stats->over_dentry = debugfs_create_u64("over", 0444,
						stats->state_dentry,
						&stats->over);
	if (!stats->over_dentry)
		goto out_remove_under;

	ret = 0;
out:
	return ret;
out_remove_good:
	debugfs_remove(stats->good_dentry);
out_remove_under:
	debugfs_remove(stats->under_dentry);
	goto out;
}

int cpuidle_debugfs_add_device(struct cpuidle_device *dev)
{
	struct cpuidle_driver *drv;
	int ret = -ENOMEM;
	int i;
	char *namedir;

	drv = cpuidle_get_cpu_driver(dev);
	if (!drv)
		return -EINVAL;

	dev->stats = kzalloc(sizeof(*dev->stats) * CPUIDLE_STATE_MAX,
			     GFP_KERNEL);
	if (!dev->stats)
		return -ENOMEM;

	namedir = kasprintf(GFP_KERNEL, "cpu%d", dev->cpu);
	if (!namedir)
		goto out_free_stats;

	dev->debug = debugfs_create_dir(namedir, top_dentry);

	kfree(namedir);

	if (!dev->debug)
		goto out_remove_dir;

	for (i = 0; i < drv->state_count; i++) {
		namedir = kasprintf(GFP_KERNEL, "state%d", i);

		dev->stats[i].state_dentry = debugfs_create_dir(namedir,
								dev->debug);

		kfree(namedir);

		if (!dev->stats[i].state_dentry)
			goto out_remove_states;

		ret = cpuidle_debugfs_add_stats(&dev->stats[i]);
		if (ret) {
			debugfs_remove(dev->stats[i].state_dentry);
			goto out_remove_states;
		}
	}

	ret = 0;
out:
	return ret;

out_remove_states:
	for (i--; i >= 0; i--) {
		cpuidle_debugfs_remove_stats(&dev->stats[i]);
		debugfs_remove(dev->stats[i].state_dentry);
	}
out_remove_dir:
	debugfs_remove(dev->debug);
out_free_stats:
	kfree(dev->stats);
	goto out;
}

static int cpuidle_debugfs_under_show(struct seq_file *s, void *data)
{
	unsigned long long total = 0;
	struct cpuidle_driver *drv;
	struct cpuidle_device *dev;
	int cpu, i;

	for_each_possible_cpu(cpu) {

		dev = per_cpu(cpuidle_devices, cpu);
		if (!dev)
			continue;

		drv = cpuidle_get_cpu_driver(dev);
		if (!drv)
			return -EINVAL;

		for (i = 0; i < drv->state_count; i++)
			total += dev->stats[i].under;
	}

	seq_printf(s, "%llu\n", total);

	return 0;
}

static int cpuidle_debugfs_over_show(struct seq_file *s, void *data)
{
	unsigned long long total = 0;
	struct cpuidle_driver *drv;
	struct cpuidle_device *dev;
	int cpu, i;

	for_each_possible_cpu(cpu) {

		dev = per_cpu(cpuidle_devices, cpu);
		if (!dev)
			continue;

		drv = cpuidle_get_cpu_driver(dev);
		if (!drv)
			return -EINVAL;

		for (i = 0; i < drv->state_count; i++)
			total += dev->stats[i].over;
	}

	seq_printf(s, "%llu\n", total);

	return 0;
}

static int cpuidle_debugfs_good_show(struct seq_file *s, void *data)
{
	unsigned long long total = 0;
	struct cpuidle_driver *drv;
	struct cpuidle_device *dev;
	int cpu, i;

	for_each_possible_cpu(cpu) {

		dev = per_cpu(cpuidle_devices, cpu);
		if (!dev)
			continue;

		drv = cpuidle_get_cpu_driver(dev);
		if (!drv)
			return -EINVAL;

		for (i = 0; i < drv->state_count; i++)
			total += dev->stats[i].good;
	}

	seq_printf(s, "%llu\n", total);

	return 0;
}

#define define_cpuidle_debugfs_open(name)				\
	static int cpuidle_debugfs_##name##_open(struct inode *inode,	\
						 struct file *file)	\
	{								\
		return single_open(file, cpuidle_debugfs_##name##_show,	\
				   inode->i_private);			\
	}

define_cpuidle_debugfs_open(under);
define_cpuidle_debugfs_open(over);
define_cpuidle_debugfs_open(good);

#define define_cpuidle_debugfs_fops(name)			      \
static const struct file_operations cpuidle_debugfs_##name##_fops = { \
	.open = cpuidle_debugfs_##name##_open,			      \
	.read = seq_read,					      \
	.llseek = seq_lseek,					      \
	.release = single_release,				      \
}

define_cpuidle_debugfs_fops(under);
define_cpuidle_debugfs_fops(over);
define_cpuidle_debugfs_fops(good);

#define cpuidle_debugfs_fops(name) cpuidle_debugfs_##name##_fops

int cpuidle_debugfs_init(void)
{
	top_dentry = debugfs_create_dir("cpuidle", NULL);
	if (!top_dentry)
		return -ENOMEM;

	pred_dentry = debugfs_create_dir("predictions", top_dentry);
	if (!pred_dentry)
		goto out_top_dentry;

	under_dentry = debugfs_create_file("under", 0444, pred_dentry, NULL,
					   &cpuidle_debugfs_fops(under));
	if (!under_dentry)
		goto out_pred_dentry;

	over_dentry = debugfs_create_file("over", 0444, pred_dentry, NULL,
					   &cpuidle_debugfs_fops(over));
	if (!over_dentry)
		goto out_under_dentry;

	good_dentry = debugfs_create_file("good", 0444, pred_dentry, NULL,
					  &cpuidle_debugfs_fops(good));
	if (!good_dentry)
		goto out_over_dentry;

	return 0;

out_over_dentry:
	debugfs_remove(over_dentry);
out_under_dentry:
	debugfs_remove(under_dentry);
out_pred_dentry:
	debugfs_remove(pred_dentry);
out_top_dentry:
	debugfs_remove(top_dentry);
	return -ENOMEM;
}