aboutsummaryrefslogtreecommitdiff
path: root/genpd.c
blob: 7b69dbbdb7a283c1faa88bdfefff6da350833950 (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
/*
 * Powerdebug : power debugging tool
 *
 * Copyright (C) 2016, Linaro Limited.
 *
 * Author:
 * Thara Gopinath <thara.gopinath@linaro.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 */
#define DEBUGFS_GENPD "/sys/kernel/debug/pm_genpd"
#define NAME_MAX	16
#define DEVICE_NAME_MAX	256

#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <sys/types.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "display.h"
#include "powerdebug.h"
#include "tree.h"
#include "utils.h"

struct genpd_idle_state {
	char name[NAME_MAX];
	long long idle_time;
};

struct genpd_info {
	long long active_time;
	long long total_idle_time;
	char current_state[NAME_MAX];
	struct genpd_idle_state *idle_states;
	char (*devices)[DEVICE_NAME_MAX];
	char (*sub_domains)[NAME_MAX];
	int nr_devices;
	int nr_subdomains;
	int nr_states;
};

static struct tree *genpd_tree;

static struct genpd_info *genpd_alloc(void)
{
	struct genpd_info *genpd;

	genpd = malloc(sizeof(*genpd));
	if (genpd)
		memset(genpd, 0, sizeof(*genpd));

	return genpd;
}

static int genpd_filter_cb(const char *name)
{
	/* Ignore the summary directory */
	if (!strcmp(name, "pm_genpd_summary"))
		return -1;
	if (!strcmp(name, "pm_genpd"))
		return -1;

	return 0;
}

static int genpd_dump_cb(struct tree *t, void *data)
{
	struct genpd_info *genpd = t->private;
	int i;

	if (!t->parent)
		return 0;

	printf("\n%s:\n", t->name);
	printf("current_state: %s\n", genpd->current_state);
	printf("active_time: %lld ms\n", genpd->active_time);
	printf("total_idle_time: %lld ms\n", genpd->total_idle_time);
	printf("Idle States:\n");
	for (i = 0; i < genpd->nr_states; i++) {
		struct genpd_idle_state state = genpd->idle_states[i];

		if (!i)
			printf("%*s State %*s Time\n", 12, "", 10, "");
		printf("%*s %-16s %lld\n", 12, "", state.name,
							state.idle_time);
	}
	printf("Devices:\n");
	for (i = 0; i < genpd->nr_devices; i++)
		printf("%*s %s\n", 8, "", genpd->devices[i]);
	printf("Subdomains:\n");
	for (i = 0; i < genpd->nr_subdomains; i++)
		printf("%*s %s\n", 11, "", genpd->sub_domains[i]);

	return 0;
}

static int genpd_display_cb(struct tree *t, void *data)
{
	struct genpd_info *genpd = t->private;
	int *line = data;
	int nr_states = 0, nr_devices = 0, nr_domains = 0, i = 0;
	char *buf;

	if (!t->parent)
		return 0;

	while (1) {
		char *state_buf, *device_buf, *domain_buf;

		if ((i) && (nr_states == genpd->nr_states) &&
			(nr_devices == genpd->nr_devices) &&
			(nr_domains == genpd->nr_subdomains)) {
			display_print_line(GENPD, *line, " ", 1, t);
			(*line)++;
			break;
		}

		if (nr_states < genpd->nr_states) {
			if (asprintf(&state_buf, "%-10s %lld",
				genpd->idle_states[nr_states].name,
				genpd->idle_states[nr_states].idle_time) < 0)
				return -1;
			nr_states++;
		} else {
			if (asprintf(&state_buf, "%s", "") < 0)
				return -1;
		}

		if (nr_devices < genpd->nr_devices) {
			if (asprintf(&device_buf, "%s",
				genpd->devices[nr_devices]) < 0)
				return -1;
			nr_devices++;
		} else {
			if (asprintf(&device_buf, "%s", "") < 0)
				return -1;
		}

		if (nr_domains < genpd->nr_subdomains) {
			if (asprintf(&domain_buf, "%s",
				genpd->sub_domains[nr_domains]) < 0)
				return -1;
			nr_domains++;
		} else {
			if (asprintf(&domain_buf, "%s", "") < 0)
				return -1;
		}

		if (!i) {
			if (asprintf(&buf, "%-9s %-18s %-20lld %-24lld %-34s "
				"%-50s %-15s", t->name, genpd->current_state,
				genpd->active_time, genpd->total_idle_time,
				state_buf, device_buf, domain_buf) < 0)
				return -1;
		} else {
			if (asprintf(&buf, "%-74s %-34s %-50s %-15s",
				"", state_buf, device_buf, domain_buf) < 0)
				return -1;
		}

		display_print_line(GENPD, *line, buf, 1, t);
		(*line)++;
		free(buf);
		free(state_buf);
		free(device_buf);
		free(domain_buf);
		i++;
	}

	return 0;
}

static int genpd_print_header(void)
{
	char *buf;
	int ret;

	if (asprintf(&buf, "%-9s %-18s %-20s %-24s %-34s %-50s %-15s", "Name",
		"Current State", "Active Time(ms)", "Total Idle Time(ms)",
		"Idle States(State,Time ms)", "Devices", "Subdomains") < 0)
		return -1;

	ret = display_column_name(buf);
	free(buf);

	return ret;
}

static int genpd_print_info(struct tree *t)
{
	int ret, line = 0;

	display_reset_cursor(GENPD);

	genpd_print_header();

	ret = tree_for_each(t, genpd_display_cb, &line);

	display_refresh_pad(GENPD);

	return ret;
}

static int read_genpd_cb(struct tree *t, void *data)
{
	struct genpd_info *genpd = t->private;
	FILE *fp;
	char line[256];
	int nr_states = 0, nr_devices = 0, nr_sub_domains = 0;

	file_read_value(t->path, "active_time", "%lld", &genpd->active_time);
	file_read_value(t->path, "total_idle_time", "%lld",
						&genpd->total_idle_time);
	file_read_value(t->path, "current_state", "%s", &genpd->current_state);
	file_open(&fp, t->path, "idle_states", "r");
	while (!(file_read_line(&fp, line, sizeof(line)))) {
		if (!strncmp(line, "State", 5))
			continue;

		genpd->idle_states = realloc(genpd->idle_states,
			sizeof(struct genpd_idle_state) * (nr_states + 1));
		if (!genpd->idle_states)
			continue;
		sscanf(line, "%s %lld", genpd->idle_states[nr_states].name,
				&(genpd->idle_states[nr_states].idle_time));

		nr_states++;
	}
	file_close(&fp);

	file_open(&fp, t->path, "devices", "r");
	while (!(file_read_line(&fp, line, sizeof(line)))) {
		int len;

		genpd->devices = realloc(genpd->devices,
			sizeof(*(genpd->devices)) * (nr_devices + 1));
		if (!genpd->devices)
			continue;

		len = strlen(line);
		line[len - 1] = '\0';
		strcpy(genpd->devices[nr_devices], line);
		nr_devices++;
	}
	file_close(&fp);

	file_open(&fp, t->path, "sub_domains", "r");
	while (!(file_read_line(&fp, line, sizeof(line)))) {
		int len;

		genpd->sub_domains = realloc(genpd->sub_domains,
			sizeof(*(genpd->sub_domains)) * (nr_sub_domains + 1));
		if (!genpd->sub_domains)
			continue;

		len = strlen(line);
		line[len - 1] = '\0';
		strcpy(genpd->sub_domains[nr_sub_domains], line);
		nr_sub_domains++;
	}

	genpd->nr_states = nr_states;
	genpd->nr_devices = nr_devices;
	genpd->nr_subdomains = nr_sub_domains;

	return 0;
}

static int read_genpd_info(struct tree *t)
{
	return tree_for_each(t, read_genpd_cb, NULL);
}

static int fill_genpd_cb(struct tree *t, void *data)
{
	struct genpd_info *genpd;

	genpd = genpd_alloc();
	if (!genpd) {
		printf("error: unable to allocate memory for genpd\n");
		return -1;
	}

	t->private = genpd;

	return read_genpd_cb(t, data);
}

static int fill_genpd_tree(void)
{
	return tree_for_each(genpd_tree, fill_genpd_cb, NULL);
}

static int genpd_info_load(void)
{
	if (genpd_tree)
		return 0;

	genpd_tree = tree_load(DEBUGFS_GENPD, genpd_filter_cb, false);
	if (!genpd_tree)
		return -1;

	if (fill_genpd_tree())
		return -1;

	return 0;
}

int genpd_dump(void)
{
	if (!genpd_tree)
		genpd_info_load();
	else
		read_genpd_info(genpd_tree);

	return tree_for_each(genpd_tree, genpd_dump_cb, NULL);

	return 0;
}

static int genpd_display(bool refresh)
{
	if (genpd_info_load()) {
		display_print_error(GENPD, 0, "Failed to read genpd info");
		return 0;
	}

	if (refresh && read_genpd_info(genpd_tree))
		return -1;

	return genpd_print_info(genpd_tree);
}

static struct display_ops genpd_ops = {
	.display = genpd_display,
};

int genpd_init(struct powerdebug_options *options)
{
	if (!(options->flags & GENPD_OPTION))
		return 0;

	return display_register(GENPD, &genpd_ops);
}