overlay.c 23.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2013 Intel Corporation
 *
 * 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 (including the next
 * paragraph) 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.
 *
 */

Chris Wilson's avatar
Chris Wilson committed
25 26 27 28 29 30 31 32
#include <sys/types.h>
#include <sys/mman.h>
#include <cairo.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
33
#include <fcntl.h>
Chris Wilson's avatar
Chris Wilson committed
34
#include <errno.h>
35
#include <signal.h>
36
#include <getopt.h>
37
#include <time.h>
Chris Wilson's avatar
Chris Wilson committed
38 39

#include "overlay.h"
40
#include "chart.h"
41
#include "config.h"
42
#include "cpu-top.h"
43
#include "debugfs.h"
Chris Wilson's avatar
Chris Wilson committed
44
#include "gem-interrupts.h"
Chris Wilson's avatar
Chris Wilson committed
45 46
#include "gem-objects.h"
#include "gpu-freq.h"
Chris Wilson's avatar
Chris Wilson committed
47
#include "gpu-top.h"
48
#include "gpu-perf.h"
49
#include "power.h"
Chris Wilson's avatar
Chris Wilson committed
50
#include "rc6.h"
Chris Wilson's avatar
Chris Wilson committed
51

52 53
#define is_power_of_two(x)  (((x) & ((x)-1)) == 0)

54 55 56 57
#define PAD 10
#define HALF_PAD 5
#define SIZE_PAD (PAD + HALF_PAD)

Chris Wilson's avatar
Chris Wilson committed
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
const cairo_user_data_key_t overlay_key;

static void overlay_show(cairo_surface_t *surface)
{
	struct overlay *overlay;

	overlay = cairo_surface_get_user_data(surface, &overlay_key);
	if (overlay == NULL)
		return;

	overlay->show(overlay);
}

#if 0
static void overlay_position(cairo_surface_t *surface, enum position p)
{
	struct overlay *overlay;

	overlay = cairo_surface_get_user_data(surface, &overlay_key);
	if (overlay == NULL)
		return;

	overlay->position(overlay, p);
}

static void overlay_hide(cairo_surface_t *surface)
{
	struct overlay *overlay;

	overlay = cairo_surface_get_user_data(surface, &overlay_key);
	if (overlay == NULL)
		return;

	overlay->hide(overlay);
}
#endif

struct overlay_gpu_top {
	struct gpu_top gpu_top;
97
	struct cpu_top cpu_top;
Chris Wilson's avatar
Chris Wilson committed
98 99
	struct chart busy[MAX_RINGS];
	struct chart wait[MAX_RINGS];
100
	struct chart cpu;
Chris Wilson's avatar
Chris Wilson committed
101 102
};

Chris Wilson's avatar
Chris Wilson committed
103 104 105 106 107 108
struct overlay_gpu_perf {
	struct gpu_perf gpu_perf;
};

struct overlay_gpu_freq {
	struct gpu_freq gpu_freq;
Chris Wilson's avatar
Chris Wilson committed
109
	struct rc6 rc6;
Chris Wilson's avatar
Chris Wilson committed
110
	struct gem_interrupts irqs;
111
	struct power power;
Chris Wilson's avatar
Chris Wilson committed
112 113
	struct chart current;
	struct chart request;
114
	struct chart power_chart;
115
	double power_max;
Chris Wilson's avatar
Chris Wilson committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
};

struct overlay_gem_objects {
	struct gem_objects gem_objects;
	struct chart aperture;
	struct chart gtt;
	int error;
};

struct overlay_context {
	cairo_surface_t *surface;
	cairo_t *cr;
	int width, height;

	struct overlay_gpu_top gpu_top;
	struct overlay_gpu_perf gpu_perf;
	struct overlay_gpu_freq gpu_freq;
	struct overlay_gem_objects gem_objects;
};

static void init_gpu_top(struct overlay_context *ctx,
			 struct overlay_gpu_top *gt)
Chris Wilson's avatar
Chris Wilson committed
138 139
{
	const double rgba[][4] = {
140 141 142
		{ 1, 0.25, 0.25, 1 },
		{ 0.25, 1, 0.25, 1 },
		{ 0.25, 0.25, 1, 1 },
Chris Wilson's avatar
Chris Wilson committed
143 144 145 146
		{ 1, 1, 1, 1 },
	};
	int n;

147
	cpu_top_init(&gt->cpu_top);
Chris Wilson's avatar
Chris Wilson committed
148
	gpu_top_init(&gt->gpu_top);
149 150

	chart_init(&gt->cpu, "CPU", 120);
151 152
	chart_set_position(&gt->cpu, PAD, PAD);
	chart_set_size(&gt->cpu, ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
153 154 155
	chart_set_stroke_rgba(&gt->cpu, 0.75, 0.25, 0.75, 1.);
	chart_set_mode(&gt->cpu, CHART_STROKE);
	chart_set_range(&gt->cpu, 0, 100);
Chris Wilson's avatar
Chris Wilson committed
156 157

	for (n = 0; n < gt->gpu_top.num_rings; n++) {
Chris Wilson's avatar
Chris Wilson committed
158
		chart_init(&gt->busy[n],
Chris Wilson's avatar
Chris Wilson committed
159 160
			   gt->gpu_top.ring[n].name,
			   120);
161 162
		chart_set_position(&gt->busy[n], PAD, PAD);
		chart_set_size(&gt->busy[n], ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
Chris Wilson's avatar
Chris Wilson committed
163 164 165 166 167 168 169 170 171 172
		chart_set_stroke_rgba(&gt->busy[n],
				    rgba[n][0], rgba[n][1], rgba[n][2], rgba[n][3]);
		chart_set_mode(&gt->busy[n], CHART_STROKE);
		chart_set_range(&gt->busy[n], 0, 100);
	}

	for (n = 0; n < gt->gpu_top.num_rings; n++) {
		chart_init(&gt->wait[n],
			   gt->gpu_top.ring[n].name,
			   120);
173 174
		chart_set_position(&gt->wait[n], PAD, PAD);
		chart_set_size(&gt->wait[n], ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
Chris Wilson's avatar
Chris Wilson committed
175 176 177 178
		chart_set_fill_rgba(&gt->wait[n],
				    rgba[n][0], rgba[n][1], rgba[n][2], rgba[n][3] * 0.70);
		chart_set_mode(&gt->wait[n], CHART_FILL);
		chart_set_range(&gt->wait[n], 0, 100);
Chris Wilson's avatar
Chris Wilson committed
179 180 181
	}
}

182
static void show_gpu_top(struct overlay_context *ctx, struct overlay_gpu_top *gt)
Chris Wilson's avatar
Chris Wilson committed
183
{
184 185
	int y, y1, y2, n, update, len;
	cairo_pattern_t *linear;
186
	char txt[160];
187 188
	int rewind;
	int do_rewind;
Chris Wilson's avatar
Chris Wilson committed
189 190

	update = gpu_top_update(&gt->gpu_top);
191

192
	cairo_rectangle(ctx->cr, PAD-.5, PAD-.5, ctx->width/2-SIZE_PAD+1, ctx->height/2-SIZE_PAD+1);
193 194 195 196
	cairo_set_source_rgb(ctx->cr, .15, .15, .15);
	cairo_set_line_width(ctx->cr, 1);
	cairo_stroke(ctx->cr);

197 198 199
	if (update && cpu_top_update(&gt->cpu_top) == 0)
		chart_add_sample(&gt->cpu, gt->cpu_top.busy);

Chris Wilson's avatar
Chris Wilson committed
200 201
	for (n = 0; n < gt->gpu_top.num_rings; n++) {
		if (update)
Chris Wilson's avatar
Chris Wilson committed
202 203
			chart_add_sample(&gt->wait[n],
					 gt->gpu_top.ring[n].u.u.wait + gt->gpu_top.ring[n].u.u.sema);
204
		chart_draw(&gt->wait[n], ctx->cr);
Chris Wilson's avatar
Chris Wilson committed
205 206 207 208
	}
	for (n = 0; n < gt->gpu_top.num_rings; n++) {
		if (update)
			chart_add_sample(&gt->busy[n],
Chris Wilson's avatar
Chris Wilson committed
209
					 gt->gpu_top.ring[n].u.u.busy);
210
		chart_draw(&gt->busy[n], ctx->cr);
Chris Wilson's avatar
Chris Wilson committed
211
	}
212
	chart_draw(&gt->cpu, ctx->cr);
Chris Wilson's avatar
Chris Wilson committed
213

214
	y1 = PAD - 2;
215 216
	y2 = y1 + (gt->gpu_top.num_rings+1) * 14 + 4;

217 218
	cairo_rectangle(ctx->cr, PAD, y1, ctx->width/2-SIZE_PAD, y2-y1);
	linear = cairo_pattern_create_linear(PAD, 0, PAD+ctx->width/2-SIZE_PAD, 0);
219 220 221 222 223
	cairo_pattern_add_color_stop_rgba(linear, 0, 0, 0, 0, .5);
	cairo_pattern_add_color_stop_rgba(linear, 1, 0, 0, 0, .0);
	cairo_set_source(ctx->cr, linear);
	cairo_pattern_destroy(linear);
	cairo_fill(ctx->cr);
Chris Wilson's avatar
Chris Wilson committed
224

225
	y = PAD + 12 - 2;
226
	cairo_set_source_rgba(ctx->cr, 0.75, 0.25, 0.75, 1.);
227
	cairo_move_to(ctx->cr, PAD, y);
228 229 230 231 232 233 234 235 236 237 238 239 240 241
	rewind = len = sprintf(txt, "CPU: %3d%% busy", gt->cpu_top.busy * gt->cpu_top.nr_cpu);
	do_rewind = 1;
	len += sprintf(txt + len, " (");
	if (gt->cpu_top.nr_cpu > 1) {
		len += sprintf(txt + len, "%s%d cores", do_rewind ? "" : ", ", gt->cpu_top.nr_cpu);
		do_rewind = 0;
	}
	if (gt->cpu_top.nr_running) {
		len += sprintf(txt + len, "%s%d processes", do_rewind ? "" : ", ", gt->cpu_top.nr_running);
		do_rewind = 0;
	}
	sprintf(txt + len, ")");
	if (do_rewind)
		txt[rewind] = '\0';
242 243 244
	cairo_show_text(ctx->cr, txt);
	y += 14;

Chris Wilson's avatar
Chris Wilson committed
245
	for (n = 0; n < gt->gpu_top.num_rings; n++) {
246
		struct chart *c =&gt->busy[n];
Chris Wilson's avatar
Chris Wilson committed
247

248
		len = sprintf(txt, "%s: %3d%% busy",
Chris Wilson's avatar
Chris Wilson committed
249 250 251 252 253 254 255 256 257
			      gt->gpu_top.ring[n].name,
			      gt->gpu_top.ring[n].u.u.busy);
		if (gt->gpu_top.ring[n].u.u.wait)
			len += sprintf(txt + len, ", %d%% wait",
				       gt->gpu_top.ring[n].u.u.wait);
		if (gt->gpu_top.ring[n].u.u.sema)
			len += sprintf(txt + len, ", %d%% sema",
				       gt->gpu_top.ring[n].u.u.sema);

258 259 260 261 262
		cairo_set_source_rgba(ctx->cr,
				      c->stroke_rgb[0],
				      c->stroke_rgb[1],
				      c->stroke_rgb[2],
				      c->stroke_rgb[3]);
263
		cairo_move_to(ctx->cr, PAD, y);
264
		cairo_show_text(ctx->cr, txt);
Chris Wilson's avatar
Chris Wilson committed
265 266 267 268
		y += 14;
	}
}

Chris Wilson's avatar
Chris Wilson committed
269 270
static void init_gpu_perf(struct overlay_context *ctx,
			  struct overlay_gpu_perf *gp)
271 272 273 274
{
	gpu_perf_init(&gp->gpu_perf, 0);
}

275 276 277 278 279 280 281 282 283 284
static char *get_comm(pid_t pid, char *comm, int len)
{
	char filename[1024];
	int fd;

	*comm = '\0';
	snprintf(filename, sizeof(filename), "/proc/%d/comm", pid);

	fd = open(filename, 0);
	if (fd >= 0) {
Chris Wilson's avatar
Chris Wilson committed
285
		len = read(fd, comm, len);
286 287 288 289 290 291 292 293 294
		if (len >= 0)
			comm[len-1] = '\0';
		close(fd);
	}

	return comm;
}

static void show_gpu_perf(struct overlay_context *ctx, struct overlay_gpu_perf *gp)
295
{
296 297 298 299 300 301 302
	static int last_color;
	const double rgba[][4] = {
		{ 1, 0.25, 0.25, 1 },
		{ 0.25, 1, 0.25, 1 },
		{ 0.25, 0.25, 1, 1 },
		{ 1, 1, 1, 1 },
	};
303 304
	struct gpu_perf_comm *comm, **prev;
	const char *ring_name[] = {
305 306 307
		"R",
		"V",
		"B",
308
	};
309
	double range[2];
310
	char buf[1024];
311 312
	cairo_pattern_t *linear;
	int x, y, y1, y2, n;
313

314
	cairo_rectangle(ctx->cr, ctx->width/2+HALF_PAD-.5, PAD-.5, ctx->width/2-SIZE_PAD+1, ctx->height/2-SIZE_PAD+1);
315 316 317 318 319 320 321 322
	cairo_set_source_rgb(ctx->cr, .15, .15, .15);
	cairo_set_line_width(ctx->cr, 1);
	cairo_stroke(ctx->cr);

	if (gp->gpu_perf.error) {
		cairo_text_extents_t extents;
		cairo_text_extents(ctx->cr, gp->gpu_perf.error, &extents);
		cairo_move_to(ctx->cr,
323 324
			      ctx->width/2+HALF_PAD + (ctx->width/2-SIZE_PAD - extents.width)/2.,
			      PAD + (ctx->height/2-SIZE_PAD + extents.height)/2.);
325 326 327 328
		cairo_show_text(ctx->cr, gp->gpu_perf.error);
		return;
	}

329 330
	gpu_perf_update(&gp->gpu_perf);

331 332
	y = PAD + 12 - 2;
	x = ctx->width/2 + HALF_PAD;
333

334

335 336 337 338 339 340 341 342 343
	for (comm = gp->gpu_perf.comm; comm; comm = comm->next) {
		int total;

		if (comm->user_data == NULL) {
			comm->user_data = malloc(sizeof(struct chart));
			if (comm->user_data == NULL)
				continue;

			chart_init(comm->user_data, comm->name, 120);
344 345
			chart_set_position(comm->user_data, ctx->width/2+HALF_PAD, PAD);
			chart_set_size(comm->user_data, ctx->width/2-SIZE_PAD, ctx->height/2 - SIZE_PAD);
346 347 348 349 350 351
			chart_set_mode(comm->user_data, CHART_STROKE);
			chart_set_stroke_rgba(comm->user_data,
					      rgba[last_color][0],
					      rgba[last_color][1],
					      rgba[last_color][2],
					      rgba[last_color][3]);
352
			last_color = (last_color + 1) % 4;
353 354 355 356 357 358 359 360 361 362 363 364
			chart_set_stroke_width(comm->user_data, 1);
		}

		total = 0;
		for (n = 0; n < 3; n++)
			total += comm->nr_requests[n];
		chart_add_sample(comm->user_data, total);
	}

	range[0] = range[1] = 0;
	for (comm = gp->gpu_perf.comm; comm; comm = comm->next)
		chart_get_range(comm->user_data, range);
365 366

	y2 = y1 = y;
367 368 369
	for (comm = gp->gpu_perf.comm; comm; comm = comm->next) {
		chart_set_range(comm->user_data, range[0], range[1]);
		chart_draw(comm->user_data, ctx->cr);
370
		y2 += 14;
371
	}
372 373 374
	y1 += -12 - 2;
	y2 += 14 - 14 + 4;

375 376
	cairo_rectangle(ctx->cr, x, y1, ctx->width/2-SIZE_PAD, y2-y1);
	linear = cairo_pattern_create_linear(x, 0, x + ctx->width/2-SIZE_PAD, 0);
377 378 379 380 381
	cairo_pattern_add_color_stop_rgba(linear, 0, 0, 0, 0, .5);
	cairo_pattern_add_color_stop_rgba(linear, 1, 0, 0, 0, .0);
	cairo_set_source(ctx->cr, linear);
	cairo_pattern_destroy(linear);
	cairo_fill(ctx->cr);
382

383
	for (prev = &gp->gpu_perf.comm; (comm = *prev) != NULL; ) {
384
		int need_comma = 0, len;
385

386 387 388 389
		if (comm->name[0] == '\0')
			goto skip_comm;

		len = sprintf(buf, "%s:", comm->name);
390 391 392
		for (n = 0; n < 3; n++) {
			if (comm->nr_requests[n] == 0)
				continue;
393
			len += sprintf(buf + len, "%s %d%s", need_comma ? "," : "", comm->nr_requests[n], ring_name[n]);
394 395 396
			need_comma = true;
		}
		if (comm->wait_time) {
397
			if (comm->wait_time > 1000*1000) {
398 399 400
				len += sprintf(buf + len, "%s %.1fms waits",
					       need_comma ? "," : "",
					       comm->wait_time / (1000*1000.));
401
			} else if (comm->wait_time > 100) {
402 403 404
				len += sprintf(buf + len, "%s %.1fus waits",
					       need_comma ? "," : "",
					       comm->wait_time / 1000.);
405
			} else {
406 407 408
				len += sprintf(buf, "%s %.0fns waits",
					       need_comma ? "," : "",
					       (double)comm->wait_time);
409
			}
410
			need_comma = true;
411
			comm->wait_time = 0;
412
		}
413 414 415 416 417 418
		if (comm->nr_sema) {
			len += sprintf(buf + len, "%s %d syncs",
				       need_comma ? "," : "",
				       comm->nr_sema);
			need_comma = true;
			comm->nr_sema = 0;
Chris Wilson's avatar
Chris Wilson committed
419
		}
420 421 422 423 424 425 426 427 428 429 430 431

		if (comm->user_data) {
			struct chart *c = comm->user_data;
			cairo_set_source_rgba(ctx->cr,
					      c->stroke_rgb[0],
					      c->stroke_rgb[1],
					      c->stroke_rgb[2],
					      c->stroke_rgb[3]);
		} else
			cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
		cairo_move_to(ctx->cr, x, y);
		cairo_show_text(ctx->cr, buf);
432 433
		y += 14;

434
skip_comm:
435 436 437
		memset(comm->nr_requests, 0, sizeof(comm->nr_requests));
		if (strcmp(comm->name, get_comm(comm->pid, buf, sizeof(buf)))) {
			*prev = comm->next;
438 439 440 441
			if (comm->user_data) {
				chart_fini(comm->user_data);
				free(comm->user_data);
			}
442 443 444 445
			free(comm);
		} else
			prev = &comm->next;
	}
446

Chris Wilson's avatar
Chris Wilson committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	{
		int has_flips = 0, len;
		for (n = 0; n < 4; n++) {
			if (gp->gpu_perf.flip_complete[n])
				has_flips = n + 1;
		}
		if (has_flips) {
			len = sprintf(buf, "Flips:");
			for (n = 0; n < has_flips; n++)
				len += sprintf(buf + len, "%s %d",
					       n ? "," : "",
					       gp->gpu_perf.flip_complete[n]);
		} else {
			sprintf(buf, "Flips: 0");
		}
		memset(gp->gpu_perf.flip_complete, 0, sizeof(gp->gpu_perf.flip_complete));
	}
464
	cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
465
	cairo_move_to(ctx->cr, x, y);
466
	cairo_show_text(ctx->cr, buf);
467 468 469
	y += 14;
}

Chris Wilson's avatar
Chris Wilson committed
470 471
static void init_gpu_freq(struct overlay_context *ctx,
			  struct overlay_gpu_freq *gf)
Chris Wilson's avatar
Chris Wilson committed
472
{
473 474
	if (gpu_freq_init(&gf->gpu_freq) == 0) {
		chart_init(&gf->current, "current", 120);
475 476
		chart_set_position(&gf->current, PAD, ctx->height/2 + HALF_PAD);
		chart_set_size(&gf->current, ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
477 478 479 480 481 482
		chart_set_stroke_rgba(&gf->current, 0.75, 0.25, 0.50, 1.);
		chart_set_mode(&gf->current, CHART_STROKE);
		chart_set_smooth(&gf->current, CHART_LINE);
		chart_set_range(&gf->current, 0, gf->gpu_freq.max);

		chart_init(&gf->request, "request", 120);
483 484
		chart_set_position(&gf->request, PAD, ctx->height/2 + HALF_PAD);
		chart_set_size(&gf->request, ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
485 486 487 488 489 490 491 492
		chart_set_fill_rgba(&gf->request, 0.25, 0.25, 0.50, 1.);
		chart_set_mode(&gf->request, CHART_FILL);
		chart_set_smooth(&gf->request, CHART_LINE);
		chart_set_range(&gf->request, 0, gf->gpu_freq.max);
	}

	if (power_init(&gf->power) == 0) {
		chart_init(&gf->power_chart, "power", 120);
493 494
		chart_set_position(&gf->power_chart, PAD, ctx->height/2 + HALF_PAD);
		chart_set_size(&gf->power_chart, ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
495
		chart_set_stroke_rgba(&gf->power_chart, 0.45, 0.55, 0.45, 1.);
496
		gf->power_max = 0;
497
	}
Chris Wilson's avatar
Chris Wilson committed
498

Chris Wilson's avatar
Chris Wilson committed
499
	rc6_init(&gf->rc6);
Chris Wilson's avatar
Chris Wilson committed
500
	gem_interrupts_init(&gf->irqs);
Chris Wilson's avatar
Chris Wilson committed
501 502 503 504 505
}

static void show_gpu_freq(struct overlay_context *ctx, struct overlay_gpu_freq *gf)
{
	char buf[160];
506
	int y1, y2, y, len;
Chris Wilson's avatar
Chris Wilson committed
507

508 509 510
	int has_freq = gpu_freq_update(&gf->gpu_freq) == 0;
	int has_rc6 = rc6_update(&gf->rc6) == 0;
	int has_power = power_update(&gf->power) == 0;
Chris Wilson's avatar
Chris Wilson committed
511
	int has_irqs = gem_interrupts_update(&gf->irqs) == 0;
512
	cairo_pattern_t *linear;
Chris Wilson's avatar
Chris Wilson committed
513

514
	cairo_rectangle(ctx->cr, PAD-.5, ctx->height/2+HALF_PAD-.5, ctx->width/2-SIZE_PAD+1, ctx->height/2-SIZE_PAD+1);
515 516 517 518
	cairo_set_source_rgb(ctx->cr, .15, .15, .15);
	cairo_set_line_width(ctx->cr, 1);
	cairo_stroke(ctx->cr);

519 520 521 522 523
	if (gf->gpu_freq.error) {
		const char *txt = "GPU frequency not found in debugfs";
		cairo_text_extents_t extents;
		cairo_text_extents(ctx->cr, txt, &extents);
		cairo_move_to(ctx->cr,
524 525
			      PAD + (ctx->width/2-SIZE_PAD - extents.width)/2.,
			      ctx->height/2+HALF_PAD + (ctx->height/2-SIZE_PAD + extents.height)/2.);
526 527 528 529
		cairo_show_text(ctx->cr, txt);
		return;
	}

530
	if (has_freq) {
531 532 533 534
		if (gf->gpu_freq.current)
			chart_add_sample(&gf->current, gf->gpu_freq.current);
		if (gf->gpu_freq.request)
			chart_add_sample(&gf->request, gf->gpu_freq.request);
Chris Wilson's avatar
Chris Wilson committed
535

536 537
		chart_draw(&gf->request, ctx->cr);
		chart_draw(&gf->current, ctx->cr);
538
	}
Chris Wilson's avatar
Chris Wilson committed
539

540 541 542 543 544 545 546 547 548 549 550
	if (has_power) {
		chart_add_sample(&gf->power_chart, gf->power.power_mW);
		if (gf->power.new_sample) {
			if (gf->power.power_mW > gf->power_max)
				gf->power_max = gf->power.power_mW;
			chart_set_range(&gf->power_chart, 0, gf->power_max);
			gf->power.new_sample = 0;
		}
		chart_draw(&gf->power_chart, ctx->cr);
	}

551
	y = ctx->height/2 + HALF_PAD + 12 - 2;
552 553 554

	y1 = y2 = y;
	if (has_freq) {
555 556
		y2 += 12;
		y2 += 12;
557
	}
Chris Wilson's avatar
Chris Wilson committed
558
	if (has_rc6)
559
		y2 += 14;
Chris Wilson's avatar
Chris Wilson committed
560 561 562
	if (has_power)
		y2 += 14;
	if (has_irqs)
563 564 565 566
		y2 += 14;
	y1 += -12 - 2;
	y2 += -14 + 4;

567 568
	cairo_rectangle(ctx->cr, PAD, y1, ctx->width/2-SIZE_PAD, y2-y1);
	linear = cairo_pattern_create_linear(PAD, 0, PAD+ctx->width/2-SIZE_PAD, 0);
569 570 571 572 573 574 575
	cairo_pattern_add_color_stop_rgba(linear, 0, 0, 0, 0, .5);
	cairo_pattern_add_color_stop_rgba(linear, 1, 0, 0, 0, .0);
	cairo_set_source(ctx->cr, linear);
	cairo_pattern_destroy(linear);
	cairo_fill(ctx->cr);

	if (has_freq) {
576 577
		cairo_text_extents_t extents;

578 579 580
		len = sprintf(buf, "Frequency: %dMHz", gf->gpu_freq.current);
		if (gf->gpu_freq.request)
		cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
581
			sprintf(buf + len, " (requested %dMHz)", gf->gpu_freq.request);
582
		cairo_move_to(ctx->cr, PAD, y);
583
		cairo_show_text(ctx->cr, buf);
584
		y += 12;
Chris Wilson's avatar
Chris Wilson committed
585

586 587
		cairo_text_extents(ctx->cr, "Frequency: ", &extents);

588
		cairo_set_font_size(ctx->cr, 8);
589 590 591
		sprintf(buf, " min: %dMHz, max: %dMHz", gf->gpu_freq.min, gf->gpu_freq.max);
		cairo_set_source_rgba(ctx->cr, .8, .8, .8, 1);
		cairo_move_to(ctx->cr, PAD + extents.width, y);
592
		cairo_show_text(ctx->cr, buf);
593
		cairo_set_font_size(ctx->cr, 10);
594
		y += 12;
595
	}
Chris Wilson's avatar
Chris Wilson committed
596

597
	if (has_rc6) {
598 599
		len = sprintf(buf, "RC6: %d%%", gf->rc6.rc6_combined);
		cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
600
		cairo_move_to(ctx->cr, PAD, y);
601
		if (gf->rc6.rc6_combined) {
602
			int need_comma = 0;
603
			int rewind = len;
604
			len += sprintf(buf + len, " (");
605
			if (gf->rc6.rc6) {
606 607 608
				len += sprintf(buf + len, "%src6=%d%%",
					       need_comma ? ", " : "",
					       gf->rc6.rc6);
609
				need_comma++;
610
			}
611
			if (gf->rc6.rc6p) {
612 613 614
				len += sprintf(buf + len, "%src6p=%d%%",
					       need_comma ? ", " : "",
					       gf->rc6.rc6p);
615
				need_comma++;
616
			}
617
			if (gf->rc6.rc6pp) {
618 619 620
				len += sprintf(buf + len, "%src6pp=%d%%",
					       need_comma ? ", " : "",
					       gf->rc6.rc6pp);
621
				need_comma++;
622
			}
623
			sprintf(buf + len, ")");
624 625
			if (need_comma <= 1)
				buf[rewind] = '\0';
Chris Wilson's avatar
Chris Wilson committed
626
		}
627
		cairo_show_text(ctx->cr, buf);
Chris Wilson's avatar
Chris Wilson committed
628 629
		y += 14;
	}
630

631
	if (has_power) {
632
		sprintf(buf, "Power: %llumW", (long long unsigned)gf->power.power_mW);
633
		cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
634
		cairo_move_to(ctx->cr, PAD, y);
635 636 637
		cairo_show_text(ctx->cr, buf);
		y += 14;
	}
Chris Wilson's avatar
Chris Wilson committed
638 639 640

	if (has_irqs) {
		sprintf(buf, "Interrupts: %llu", (long long unsigned)gf->irqs.delta);
641
		cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
642
		cairo_move_to(ctx->cr, PAD, y);
Chris Wilson's avatar
Chris Wilson committed
643 644 645
		cairo_show_text(ctx->cr, buf);
		y += 14;
	}
Chris Wilson's avatar
Chris Wilson committed
646 647
}

Chris Wilson's avatar
Chris Wilson committed
648 649
static void init_gem_objects(struct overlay_context *ctx,
			     struct overlay_gem_objects *go)
Chris Wilson's avatar
Chris Wilson committed
650
{
Chris Wilson's avatar
Chris Wilson committed
651 652 653 654 655
	go->error = gem_objects_init(&go->gem_objects);
	if (go->error)
		return;

	chart_init(&go->aperture, "aperture", 120);
656 657
	chart_set_position(&go->aperture, ctx->width/2+HALF_PAD, ctx->height/2 + HALF_PAD);
	chart_set_size(&go->aperture, ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
Chris Wilson's avatar
Chris Wilson committed
658 659 660
	chart_set_stroke_rgba(&go->aperture, 0.75, 0.25, 0.50, 1.);
	chart_set_mode(&go->aperture, CHART_STROKE);
	chart_set_range(&go->aperture, 0, go->gem_objects.max_gtt);
Chris Wilson's avatar
Chris Wilson committed
661

Chris Wilson's avatar
Chris Wilson committed
662
	chart_init(&go->gtt, "gtt", 120);
663 664
	chart_set_position(&go->gtt, ctx->width/2+HALF_PAD, ctx->height/2 + HALF_PAD);
	chart_set_size(&go->gtt, ctx->width/2 - SIZE_PAD, ctx->height/2 - SIZE_PAD);
665
	chart_set_fill_rgba(&go->gtt, 0.25, 0.5, 0.5, 1.);
Chris Wilson's avatar
Chris Wilson committed
666 667 668 669 670 671 672 673
	chart_set_mode(&go->gtt, CHART_FILL);
	chart_set_range(&go->gtt, 0, go->gem_objects.max_gtt);
}

static void show_gem_objects(struct overlay_context *ctx, struct overlay_gem_objects *go)
{
	struct gem_objects_comm *comm;
	char buf[160];
674 675
	cairo_pattern_t *linear;
	int x, y, y1, y2;
Chris Wilson's avatar
Chris Wilson committed
676 677 678 679

	if (go->error == 0)
		go->error = gem_objects_update(&go->gem_objects);
	if (go->error)
Chris Wilson's avatar
Chris Wilson committed
680 681
		return;

682
	cairo_rectangle(ctx->cr, ctx->width/2+HALF_PAD-.5, ctx->height/2+HALF_PAD-.5, ctx->width/2-SIZE_PAD+1, ctx->height/2-SIZE_PAD+1);
683 684 685 686
	cairo_set_source_rgb(ctx->cr, .15, .15, .15);
	cairo_set_line_width(ctx->cr, 1);
	cairo_stroke(ctx->cr);

Chris Wilson's avatar
Chris Wilson committed
687 688 689 690 691 692
	chart_add_sample(&go->gtt, go->gem_objects.total_gtt);
	chart_add_sample(&go->aperture, go->gem_objects.total_aperture);

	chart_draw(&go->gtt, ctx->cr);
	chart_draw(&go->aperture, ctx->cr);

693

694 695
	y = ctx->height/2 + HALF_PAD + 12 - 2;
	x = ctx->width/2 + HALF_PAD;
696 697 698 699 700 701

	y2 = y1 = y;
	y2 += 14;
	for (comm = go->gem_objects.comm; comm; comm = comm->next) {
		if ((comm->bytes >> 20) == 0)
			break;
702
		y2 += 12;
703 704
	}
	y1 += -12 - 2;
705
	y2 += -12 + 4;
706

707 708
	cairo_rectangle(ctx->cr, x, y1, ctx->width/2-SIZE_PAD, y2-y1);
	linear = cairo_pattern_create_linear(x, 0, x+ctx->width/2-SIZE_PAD, 0);
709 710 711 712 713 714
	cairo_pattern_add_color_stop_rgba(linear, 0, 0, 0, 0, .5);
	cairo_pattern_add_color_stop_rgba(linear, 1, 0, 0, 0, .0);
	cairo_set_source(ctx->cr, linear);
	cairo_pattern_destroy(linear);
	cairo_fill(ctx->cr);

715
	sprintf(buf, "Total: %ldMB, %ld objects",
716
		go->gem_objects.total_bytes >> 20, go->gem_objects.total_count);
Chris Wilson's avatar
Chris Wilson committed
717
	cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
718
	cairo_move_to(ctx->cr, x, y);
Chris Wilson's avatar
Chris Wilson committed
719
	cairo_show_text(ctx->cr, buf);
720
	y += 12;
Chris Wilson's avatar
Chris Wilson committed
721

722
	cairo_set_source_rgba(ctx->cr, .8, .8, .8, 1);
723
	cairo_set_font_size(ctx->cr, 8);
Chris Wilson's avatar
Chris Wilson committed
724
	for (comm = go->gem_objects.comm; comm; comm = comm->next) {
725 726 727
		if ((comm->bytes >> 20) == 0)
			break;

728
		sprintf(buf, "%s %ldMB, %ld objects",
729
			comm->name, comm->bytes >> 20, comm->count);
730
		cairo_move_to(ctx->cr, x, y);
Chris Wilson's avatar
Chris Wilson committed
731
		cairo_show_text(ctx->cr, buf);
732
		y += 12;
Chris Wilson's avatar
Chris Wilson committed
733 734 735
	}
}

736 737 738 739 740 741 742
static int take_snapshot;

static void signal_snapshot(int sig)
{
	take_snapshot = sig;
}

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
static int get_sample_period(struct config *config)
{
	const char *value;

	value = config_get_value(config, "sampling", "period");
	if (value && atoi(value) > 0)
		return atoi(value);

	value = config_get_value(config, "sampling", "frequency");
	if (value && atoi(value) > 0)
		return 1000000 / atoi(value);

	return 500000;
}

758 759 760 761 762 763 764
static void overlay_snapshot(struct overlay_context *ctx)
{
	char buf[1024];
	sprintf(buf, "/tmp/overlay-snapshot-%ld.png", (long)time(NULL));
	cairo_surface_write_to_png(ctx->surface, buf);
}

Chris Wilson's avatar
Chris Wilson committed
765 766
int main(int argc, char **argv)
{
767 768 769 770
	static struct option long_options[] = {
		{"config", 1, 0, 'c'},
		{"geometry", 1, 0, 'G'},
		{"position", 1, 0, 'P'},
771
		{"size", 1, 0, 'S'},
772 773
		{NULL, 0, 0, 0,}
	};
Chris Wilson's avatar
Chris Wilson committed
774
	struct overlay_context ctx;
775
	struct config config;
776
	int index, sample_period;
Chris Wilson's avatar
Chris Wilson committed
777
	int daemonize = 1;
778 779 780 781 782
	int i;

	config_init(&config);

	opterr = 0;
783
	while ((i = getopt_long(argc, argv, "c:f", long_options, &index)) != -1) {
784 785 786 787 788 789 790 791 792 793
		switch (i) {
		case 'c':
			config_parse_string(&config, optarg);
			break;
		case 'G':
			config_set_value(&config, "window", "geometry", optarg);
			break;
		case 'P':
			config_set_value(&config, "window", "position", optarg);
			break;
794 795 796
		case 'S':
			config_set_value(&config, "window", "size", optarg);
			break;
Chris Wilson's avatar
Chris Wilson committed
797 798 799
		case 'f':
			daemonize = 0;
			break;
800 801
		}
	}
Chris Wilson's avatar
Chris Wilson committed
802

803
	if (argc > optind) {
Chris Wilson's avatar
Chris Wilson committed
804 805 806 807
		x11_overlay_stop();
		return 0;
	}

Chris Wilson's avatar
Chris Wilson committed
808
	ctx.width = 640;
809
	ctx.height = 236;
Chris Wilson's avatar
Chris Wilson committed
810 811 812
	ctx.surface = NULL;
	if (ctx.surface == NULL)
		ctx.surface = x11_overlay_create(&config, &ctx.width, &ctx.height);
813
	if (ctx.surface == NULL)
814
		ctx.surface = x11_window_create(&config, &ctx.width, &ctx.height);
Chris Wilson's avatar
Chris Wilson committed
815 816
	if (ctx.surface == NULL)
		ctx.surface = kms_overlay_create(&config, &ctx.width, &ctx.height);
Chris Wilson's avatar
Chris Wilson committed
817
	if (ctx.surface == NULL)
Chris Wilson's avatar
Chris Wilson committed
818 819
		return ENOMEM;

Chris Wilson's avatar
Chris Wilson committed
820 821 822
	if (daemonize && daemon(0, 0))
		return EINVAL;

823 824
	signal(SIGUSR1, signal_snapshot);

825 826
	debugfs_init();

Chris Wilson's avatar
Chris Wilson committed
827 828 829 830
	init_gpu_top(&ctx, &ctx.gpu_top);
	init_gpu_perf(&ctx, &ctx.gpu_perf);
	init_gpu_freq(&ctx, &ctx.gpu_freq);
	init_gem_objects(&ctx, &ctx.gem_objects);
Chris Wilson's avatar
Chris Wilson committed
831

832 833
	sample_period = get_sample_period(&config);

834
	i = 0;
Chris Wilson's avatar
Chris Wilson committed
835
	while (1) {
Chris Wilson's avatar
Chris Wilson committed
836
		ctx.cr = cairo_create(ctx.surface);
837 838 839
		cairo_set_operator(ctx.cr, CAIRO_OPERATOR_CLEAR);
		cairo_paint(ctx.cr);
		cairo_set_operator(ctx.cr, CAIRO_OPERATOR_OVER);
Chris Wilson's avatar
Chris Wilson committed
840

841 842 843 844 845
		show_gpu_top(&ctx, &ctx.gpu_top);
		show_gpu_perf(&ctx, &ctx.gpu_perf);
		show_gpu_freq(&ctx, &ctx.gpu_freq);
		show_gem_objects(&ctx, &ctx.gem_objects);

Chris Wilson's avatar
Chris Wilson committed
846 847 848
		{
			char buf[80];
			cairo_text_extents_t extents;
849
			gethostname(buf, sizeof(buf));
850
			cairo_set_source_rgb(ctx.cr, .5, .5, .5);
851
			cairo_set_font_size(ctx.cr, PAD-2);
852 853
			cairo_text_extents(ctx.cr, buf, &extents);
			cairo_move_to(ctx.cr,
854 855
				      (ctx.width-extents.width)/2.,
				      1+extents.height);
856
			cairo_show_text(ctx.cr, buf);
Chris Wilson's avatar
Chris Wilson committed
857 858
		}

859
		cairo_destroy(ctx.cr);
Chris Wilson's avatar
Chris Wilson committed
860

Chris Wilson's avatar
Chris Wilson committed
861
		overlay_show(ctx.surface);
862 863

		if (take_snapshot) {
864
			overlay_snapshot(&ctx);
865 866
			take_snapshot = 0;
		}
867 868

		usleep(sample_period);
Chris Wilson's avatar
Chris Wilson committed
869 870 871 872
	}

	return 0;
}