summaryrefslogtreecommitdiffstats
path: root/drm-sched-top.c
blob: 266759febac82c981e384aafba0769241271cd0a (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
/*
 * Copyright (c) 2018 Pengutronix, Lucas Stach <l.stach@pengutronix.de>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#define MAX_RINGS	256

struct ring {
	const char *name;
	int active_time_fd;
	uint64_t last_active_time;
	long last_query_time;
};

struct rings {
	int num_rings;
	struct ring *rings[MAX_RINGS];
};

static int
filename_to_buf(const int fd, char *buf, unsigned int bufsize)
{
	int err;
	ssize_t ret;

	ret = pread(fd, buf, bufsize - 1, 0);
	err = errno;
	if (ret < 1) {
		errno = ret < 0 ? err : ENOMSG;

		return -1;
	}

	if (ret > 1 && buf[ret - 1] == '\n')
		buf[ret - 1] = '\0';
	else
		buf[ret] = '\0';

	return 0;
}

static uint64_t fd_to_u64(const int fd, int base)
{
	char buf[64], *b;

	if (filename_to_buf(fd, buf, sizeof(buf)))
		return 0;

	/*
	 * Handle both single integer and key=value formats by skipping
	 * leading non-digits.
	 */
	b = buf;
	while (*b && !isdigit(*b))
		b++;

	return strtoull(b, NULL, base);
}

static const char *bars[] = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" };

static void
print_percentage_bar(double percent, int max_len)
{
	int bar_len = percent * (8 * (max_len - 2)) / 100.0;
	int i;

	printf("|\e[1;33m");

	for (i = bar_len; i >= 8; i -= 8)
		printf("%s", bars[8]);
	if (i)
		printf("%s", bars[i]);

	for (i = 0; i < (max_len - 2 - (bar_len + 7) / 8); i++)
		putchar(' ');

	printf("\e[0m|");
}

static void __discover_rings(struct rings *ringss, const char *dirname)
{
	struct dirent *dent;
	DIR *ring_dir;

	ring_dir = opendir(dirname);
	if (!ring_dir)
		return;

	while ((dent = readdir(ring_dir)) != NULL) {
		char buf[1024];
		struct ring *ring;

		if (ringss->num_rings >= MAX_RINGS)
			break;

		if (!strncmp(dent->d_name, ".", 1))
			continue;

		ring = malloc(sizeof(*ring));
		if (!ring)
			break;
		memset(ring, 0, sizeof(*ring));

		ring->name = strdup(dent->d_name);

		snprintf(buf, 1024, "%s/%s/active_us", dirname, dent->d_name);
		ring->active_time_fd = open(buf, O_RDONLY);
		if (ring->active_time_fd < 0) {
			free(ring);
			break;
		}

		ringss->rings[ringss->num_rings] = ring;
		ringss->num_rings++;
	}

	closedir(ring_dir);
}

static struct rings *discover_rings(void)
{
	struct rings *ringss;

	ringss = malloc(sizeof(*ringss));
	if (!ringss)
		return NULL;
	memset(ringss, 0, sizeof(*ringss));

	__discover_rings(ringss, "/sys/class/drm/scheduler/rings");
	__discover_rings(ringss, "/sys/class/video4linux/stats");

	if (ringss->num_rings == 0) {
		free(ringss);
		return NULL;
	}

	return ringss;
}

static void usage(const char *appname)
{
	printf("drm-sched-top - Display a top like usage of DRM scheduler driven GPUs\n"
		"\n"
		"Usage: %s [parameters]\n"
		"\n"
		"\tThe following parameters are optional:\n\n"
		"\t[-s <ms>]       Refresh period in milliseconds (default 1000).\n"
		"\t[-h]            Show this help text.\n"
		"\n",
		appname);
}

int main(int argc, char **argv)
{
	unsigned int period_us = 1000000;
	struct rings *ringss;
	DIR *sched_dir;
	int ch;

	/* Parse options */
	while ((ch = getopt(argc, argv, "s:h")) != -1) {
		switch (ch) {
		case 's':
			period_us = atoi(optarg) * 1000;
			break;
		case 'h':
			usage(argv[0]);
			exit(0);
		default:
			fprintf(stderr, "Invalid option %c!\n", (char)optopt);
			usage(argv[0]);
			exit(1);
		}
	}

	/* Sanity check before trying to do anything else */
	sched_dir = opendir("/sys/class/drm/scheduler");
	if (!sched_dir) {
		fprintf(stderr, "could not open scheduler statistics directory\n");
		return -1;
	}

	closedir(sched_dir);

	ringss = discover_rings();
	if (!ringss) {
		fprintf(stderr, "could not find any engines\n");
		exit(1);
	}

	for (;;) {
		struct winsize ws;
		int i;


		ioctl(0, TIOCGWINSZ, &ws);

		printf("\033[H\033[J");
		printf("Engines:\n");

		for (i = 0; i < ringss->num_rings; i++) {
			struct ring *ring = ringss->rings[i];
			uint64_t active_time, query_timestamp;
			double active_percent;
			struct timespec time;
			int len = 0;

			active_time = fd_to_u64(ring->active_time_fd, 10);
			clock_gettime(CLOCK_MONOTONIC, &time);
			query_timestamp = time.tv_sec * 1000000 + time.tv_nsec / 1000;

			active_percent = (active_time - ring->last_active_time) * 100.0;
			active_percent /= (query_timestamp - ring->last_query_time);

			len += printf("  %s", ring->name);
			while(len < 24)
				len += printf(" ");

			print_percentage_bar(active_percent, ws.ws_col - len - 10);
			len += printf(" %3.2f %%", active_percent);
			printf("\n");

			ring->last_active_time = active_time;
			ring->last_query_time = query_timestamp;
		}

		usleep(period_us);
	}

	return 0;
}