summaryrefslogtreecommitdiff
path: root/tools/intel_gpu_top.c
blob: a0707b6fc7730d9cf804caffa1f2042b9a393f2a (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
 * Copyright © 2007 Intel Corporation
 * Copyright © 2011 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.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *    Eugeni Dodonov <eugeni.dodonov@intel.com>
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <string.h>
#include "intel_gpu_tools.h"
#include "instdone.h"
#include "config.h"

#define  FORCEWAKE	    0xA18C
#define  FORCEWAKE_ACK	    0x130090

#define SAMPLES_PER_SEC             10000
#define SAMPLES_TO_PERCENT_RATIO    (SAMPLES_PER_SEC / 100)

#define MAX_NUM_TOP_BITS            100

#define HAS_STATS_REGS(devid)		IS_965(devid)

struct top_bit {
	struct instdone_bit *bit;
	int count;
} top_bits[MAX_NUM_TOP_BITS];
struct top_bit *top_bits_sorted[MAX_NUM_TOP_BITS];

static uint32_t instdone, instdone1;
static uint32_t devid;

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

enum stats_counts {
	IA_VERTICES,
	IA_PRIMITIVES,
	VS_INVOCATION,
	GS_INVOCATION,
	GS_PRIMITIVES,
	CL_INVOCATION,
	CL_PRIMITIVES,
	PS_INVOCATION,
	PS_DEPTH,
	STATS_COUNT
};

const uint32_t stats_regs[STATS_COUNT] = {
	IA_VERTICES_COUNT_QW,
	IA_PRIMITIVES_COUNT_QW,
	VS_INVOCATION_COUNT_QW,
	GS_INVOCATION_COUNT_QW,
	GS_PRIMITIVES_COUNT_QW,
	CL_INVOCATION_COUNT_QW,
	CL_PRIMITIVES_COUNT_QW,
	PS_INVOCATION_COUNT_QW,
	PS_DEPTH_COUNT_QW,
};

const char *stats_reg_names[STATS_COUNT] = {
	"vert-fetch",
	"prim-fetch",
	"VS-invocations",
	"GS-invocations",
	"GS-prims",
	"CL-invocations",
	"CL-prims",
	"PS-invocations",
	"PS-depth-pass",
};

uint64_t stats[STATS_COUNT];
uint64_t last_stats[STATS_COUNT];

/*
 * Architecture-specific monitoring facilities.
 *
 * Currently, they only exist for Linux, but let's not allow their usage block
 * the tools usage on other systems.
 */

struct cpudata
{
	unsigned long user, system, idle, nice, total;
};

struct proc_cpudata
{
	unsigned long userproc, systemproc, vmem, rmem;
	char pname[255];
};

struct powerdata
{
	char *battery_file;
	char *battery_file_ext;
	float watts;
	float watts_chipset;
	float watts_gfx;
};


static void
get_cpu_stat(struct cpudata *cpu)
{
#ifdef __linux__
	FILE *file;
	char temp[10];
	char line[255];

	/* CPU */
	file = fopen("/proc/stat", "r");
	fgets(line, 255, file);
	fclose(file);
	sscanf(line, "%s %lu %lu %lu %lu", temp, &cpu->user, &cpu->nice, &cpu->system,
			&cpu->idle);
	cpu->total = cpu->user + cpu->system + cpu->idle + cpu->nice;
#else
	/* We are not on Linux, give a warning on compile */
#warning Non-Linux OS detected, CPU statistics will not be collected!
	cpu->total = cpu->user = cpu->system = cpu->idle = cpu->nice = 0;
#endif

}

static void
power_stat_init(struct powerdata *power)
{
#ifdef __linux__
	/* detect the correct location for the acpi/battery and i915_emon_status */
	int i, ret;
	power->battery_file = (char *)malloc(FILENAME_MAX);
	for (i=0, ret=-1; i < 16; i++) {
		/* Discover battery */
		snprintf(power->battery_file, FILENAME_MAX, "/proc/acpi/battery/BAT%d/state", i);
		if ((ret = access(power->battery_file, R_OK)) == 0)
			break;
	}
	if (ret < 0) {
		free(power->battery_file);
		power->battery_file = NULL;
	}

	power->battery_file_ext = (char *)malloc(FILENAME_MAX);
	for (i=0, ret=-1; i < 16; i++) {
		/* Try in /sys/kernel/debug */
		snprintf(power->battery_file_ext, FILENAME_MAX, "/sys/kernel/debug/dri/%d/i915_emon_status", i);
		if ((ret = access(power->battery_file_ext, R_OK)) == 0)
			break;
		/* Try in /debug */
		snprintf(power->battery_file_ext, FILENAME_MAX, "/debug/dri/%d/i915_emon_status", i);
		if ((ret = access(power->battery_file_ext, R_OK)) == 0)
			break;
	}
	if (ret < 0) {
		free(power->battery_file_ext);
		power->battery_file_ext = NULL;
	}
#else
#warning Non-Linux OS detected, POWER initialization not done.
#endif

}

/* Reading from /sys/kernel/debug/dri/0/i915_emon_status can cause a division by zero within kernel, at least as of 3.2-rc1.
 *
 * So disabling it for now.
 *
 * You can use the patch at http://lists.freedesktop.org/archives/intel-gfx/2011-November/013198.html or
 * http://lists.freedesktop.org/archives/intel-gfx/2011-July/011380.html to fix it.
 */

#define USE_I915_EMON_STATUS 0

static void
get_power_stat(struct powerdata *power, float elapsed_time)
{
#ifdef __linux__
	FILE *file;
	char line[255];
	long rate, voltage;

	/* Reset values */
	power->watts = power->watts_chipset = power->watts_gfx = -1;

	if (power->battery_file) {
		file = fopen(power->battery_file, "r");
		if (!file) {
			/* Unable to read battery information */
			return;
		}
		/* Reading ACPI-adquired power
		 * Sample /proc/acpi/battery/BAT?/state file format:
		 * present:                 yes
		 * capacity state:          ok
		 * charging state:          discharging
		 * present rate:            1746 mA
		 * remaining capacity:      5301 mAh
		 * present voltage:         11788 mV
		 */
		fgets(line, 255, file);
		fgets(line, 255, file);
		fgets(line, 255, file);
		fgets(line, 255, file);
		sscanf(line, "present rate:\t%lu mAh\n", &rate);
		fgets(line, 255, file);
		fgets(line, 255, file);
		sscanf(line, "present voltage:\t%lu mV\n", &voltage);
		fclose(file);

		power->watts = (float) (rate * voltage) / 1000000;
		power->watts *= elapsed_time;
	}

	/* Going deeper and reading GPU power */
	if (power->battery_file_ext && USE_I915_EMON_STATUS) {
		long tmp;
		file = fopen(power->battery_file_ext, "r");
		if (!file) {
			/* Unable to read debugfs file */
			return;
		}
		/* Reading i915 debugfs data
		 * Sample /sys/kernel/debug/dri/0/i915_emon_status file format:
		 * GMCH temp: 44
		 * Chipset power: 3063
		 * GFX power: 2393
		 * Total power: 5456
		 */
		fgets(line, 255, file);
		fgets(line, 255, file);
		sscanf(line, "Chipset power: %lu\n", &tmp);
		power->watts_chipset = (float) tmp / 1000;
		fgets(line, 255, file);
		sscanf(line, "GFX power: %lu\n", &tmp);
		power->watts_gfx = (float) tmp / 1000;
		fclose(file);
	}
#else
	/* We are not on Linux, give a warning on compile */
#warning Non-Linux OS detected, POWER statistics will not be collected!
	power->watts = power->watts_gfx = power->watts_chipset = -1;
#endif
}

static void system_print_header(FILE *out)
{
	fprintf(out, "utime\tstime\tpower\tpower.chipset\tpower.gfx\t");
}

static void system_print(FILE *out, struct cpudata *cpu, struct cpudata *oldcpu,
		struct powerdata *power)
{
	float uload=-1, sload=-1;
	if (cpu->total > 0) {
		uload = (float) ((cpu->user - oldcpu->user) * 100) / (cpu->total - oldcpu->total);
		sload = (float) ((cpu->system - oldcpu->system) * 100) / (cpu->total - oldcpu->total);
	}
	fprintf(out, "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t",
			uload, sload, power->watts, power->watts_chipset, power->watts_gfx);
}

static unsigned long
gettime(void)
{
	struct timeval t;
	gettimeofday(&t, NULL);
	return (t.tv_usec + (t.tv_sec * 1000000));
}

static int
top_bits_sort(const void *a, const void *b)
{
	struct top_bit * const *bit_a = a;
	struct top_bit * const *bit_b = b;
	int a_count = (*bit_a)->count;
	int b_count = (*bit_b)->count;

	if (a_count < b_count)
		return 1;
	else if (a_count == b_count)
		return 0;
	else
		return -1;
}

static void
update_idle_bit(struct top_bit *top_bit)
{
	uint32_t reg_val;

	if (top_bit->bit->reg == INST_DONE_1)
		reg_val = instdone1;
	else
		reg_val = instdone;

	if ((reg_val & top_bit->bit->bit) == 0)
		top_bit->count++;
}

static void
print_clock(char *name, int clock) {
	if (clock == -1)
		printf("%s clock: unknown", name);
	else
		printf("%s clock: %d Mhz", name, clock);
}

static int
print_clock_info(struct pci_device *pci_dev)
{
	uint32_t devid = pci_dev->device_id;
	uint16_t gcfgc;

	if (IS_GM45(devid)) {
		int core_clock = -1;

		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);

		switch (gcfgc & 0xf) {
		case 8:
			core_clock = 266;
			break;
		case 9:
			core_clock = 320;
			break;
		case 11:
			core_clock = 400;
			break;
		case 13:
			core_clock = 533;
			break;
		}
		print_clock("core", core_clock);
	} else if (IS_965(devid) && IS_MOBILE(devid)) {
		int render_clock = -1, sampler_clock = -1;

		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);

		switch (gcfgc & 0xf) {
		case 2:
			render_clock = 250; sampler_clock = 267;
			break;
		case 3:
			render_clock = 320; sampler_clock = 333;
			break;
		case 4:
			render_clock = 400; sampler_clock = 444;
			break;
		case 5:
			render_clock = 500; sampler_clock = 533;
			break;
		}

		print_clock("render", render_clock);
		printf("  ");
		print_clock("sampler", sampler_clock);
	} else if (IS_945(devid) && IS_MOBILE(devid)) {
		int render_clock = -1, display_clock = -1;

		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);

		switch (gcfgc & 0x7) {
		case 0:
			render_clock = 166;
			break;
		case 1:
			render_clock = 200;
			break;
		case 3:
			render_clock = 250;
			break;
		case 5:
			render_clock = 400;
			break;
		}

		switch (gcfgc & 0x70) {
		case 0:
			display_clock = 200;
			break;
		case 4:
			display_clock = 320;
			break;
		}
		if (gcfgc & (1 << 7))
		    display_clock = 133;

		print_clock("render", render_clock);
		printf("  ");
		print_clock("display", display_clock);
	} else if (IS_915(devid) && IS_MOBILE(devid)) {
		int render_clock = -1, display_clock = -1;

		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);

		switch (gcfgc & 0x7) {
		case 0:
			render_clock = 160;
			break;
		case 1:
			render_clock = 190;
			break;
		case 4:
			render_clock = 333;
			break;
		}
		if (gcfgc & (1 << 13))
		    render_clock = 133;

		switch (gcfgc & 0x70) {
		case 0:
			display_clock = 190;
			break;
		case 4:
			display_clock = 333;
			break;
		}
		if (gcfgc & (1 << 7))
		    display_clock = 133;

		print_clock("render", render_clock);
		printf("  ");
		print_clock("display", display_clock);
	}


	printf("\n");
	return -1;
}

#define STATS_LEN (20)
#define PERCENTAGE_BAR_END	(79 - STATS_LEN)

static void
print_percentage_bar(float percent, int cur_line_len)
{
	int bar_avail_len = (PERCENTAGE_BAR_END - cur_line_len - 1) * 8;
	int bar_len = bar_avail_len * (percent + .5) / 100.0;
	int i;

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

	/* NB: We can't use a field width with utf8 so we manually
	* guarantee a field with of 45 chars for any bar. */
	printf("%*s", PERCENTAGE_BAR_END - cur_line_len, "");
}

struct ring {
	const char *name;
	uint32_t mmio;
	int head, tail, size;
	uint64_t full;
	int idle;
};

static uint32_t ring_read(struct ring *ring, uint32_t reg)
{
	return INREG(ring->mmio + reg);
}

static void ring_init(struct ring *ring)
{
	ring->size = (((ring_read(ring, RING_LEN) & RING_NR_PAGES) >> 12) + 1) * 4096;
}

static void ring_reset(struct ring *ring)
{
	ring->idle = ring->full = 0;
}

static void ring_sample(struct ring *ring)
{
	int full;

	if (!ring->size)
		return;

	ring->head = ring_read(ring, RING_HEAD) & HEAD_ADDR;
	ring->tail = ring_read(ring, RING_TAIL) & TAIL_ADDR;

	if (ring->tail == ring->head)
		ring->idle++;

	full = ring->tail - ring->head;
	if (full < 0)
		full += ring->size;
	ring->full += full;
}

static void ring_print_header(FILE *out, struct ring *ring)
{
	fprintf(out, "%s\t%s.ops\t",
			ring->name,
			ring->name
		   );
}

static void ring_print(struct ring *ring, unsigned long samples_per_sec)
{
	int percent_busy, len;

	if (!ring->size)
		return;

	percent_busy = 100 - 100 * ring->idle / samples_per_sec;

	len = printf("%25s busy: %3d%%: ", ring->name, percent_busy);
	print_percentage_bar (percent_busy, len);
	printf("%24s space: %d/%d\n",
		   ring->name,
		   (int)(ring->full / samples_per_sec),
		   ring->size);
}

static void ring_log(struct ring *ring, unsigned long samples_per_sec,
		FILE *output)
{
	if (ring->size)
		fprintf(output, "%3d\t%d\t",
			(int)(100 - 100 * ring->idle / samples_per_sec),
			(int)(ring->full / samples_per_sec));
	else
		fprintf(output, "-1\t-1\t");
}

static void
usage(const char *appname)
{
	printf("intel_gpu_top - Display a top-like summary of Intel GPU usage\n"
			"\n"
			"usage: %s [parameters]\n"
			"\n"
			"The following parameters apply:\n"
			"[-s <samples>]       samples per seconds (default %d)\n"
			"[-e <command>]       command to profile\n"
			"[-p]                 profile with kperf (Linux-specific)\n"
			"[-o <file>]          output statistics to file. If file is '-',"
			"                     run in batch mode and output statistics to stdio only \n"
			"[-h]                 show this help screen\n"
			"\n",
			appname,
			SAMPLES_PER_SEC
		  );
	return;
}

int main(int argc, char **argv)
{
	struct pci_device *pci_dev;
	struct ring render_ring = {
		.name = "render",
		.mmio = 0x2030,
	}, bsd_ring = {
		.name = "bitstream",
		.mmio = 0x4030,
	}, bsd6_ring = {
		.name = "bitstream6",
		.mmio = 0x12030,
	}, blt_ring = {
		.name = "blitter",
		.mmio = 0x22030,
	};
	int i, ch;
	int samples_per_sec = SAMPLES_PER_SEC;
	FILE *output = NULL;
	double elapsed_time=0, time_diff=0;
	int print_headers=1;
	pid_t child_pid=-1;
	int child_stat;
	char *cmd=NULL;
	int interactive=1;

	struct cpudata oldcpu, cpu;
	struct powerdata power;
#ifdef __linux__
	int do_perf=0;
	char *perf_output=NULL;
#endif

	/* Parse options? */
	while ((ch = getopt(argc, argv, "s:o:e:ph")) != -1) {
		switch (ch) {
		case 'e': cmd = strdup(optarg);
			break;
		case 's': samples_per_sec = atoi(optarg);
			if (samples_per_sec < 100) {
				fprintf(stderr, "Error: samples per second must be >= 100\n");
				exit(1);
			}
			break;
		case 'o':
			if (!strcmp(optarg, "-")) {
				/* Running in non-interactive mode */
				interactive = 0;
				output = stdout;
			}
			else {
				output = fopen(optarg, "w");
#if __linux__
				perf_output = (char *)malloc(strlen(optarg) + 5);
				sprintf(perf_output, "%s.perf", optarg);
#endif
			}
			if (!output)
			{
				perror("fopen");
				exit(1);
			}
			break;
		case 'p':
#ifndef __linux__
			fprintf(stderr, "Error: perf tracing not available on non-Linux OS\n");
			exit(1);
#else
			do_perf=1;
#endif
			break;
		case 'h':
			usage(argv[0]);
			exit(0);
			break;
		default:
			fprintf(stderr, "Invalid flag %c!\n", (char)optopt);
			usage(argv[0]);
			exit(1);
			break;
		}
	}
	argc -= optind;
	argv += optind;

	pci_dev = intel_get_pci_device();
	devid = pci_dev->device_id;
	intel_get_mmio(pci_dev);
	init_instdone_definitions(devid);

#ifdef __linux__
	if (do_perf) {
		int perf_pid = fork();
		if (perf_pid < 0) {
			perror("fork");
			exit(1);
		}
		if (perf_pid > 0) {
			const char *perf_cmd = "perf record -a";
			int res;
			if (perf_output) {
				/* Output to a log file */
				char *cmdline = (char *)malloc(strlen(perf_cmd) + 
						strlen(perf_output) + 4);
				sprintf(cmdline, "%s -o %s", perf_cmd, perf_output);
				res = system(cmdline);
				free(cmdline);
			} else {
				res = system("perf record -a");
			}
			if (res < 0)
				perror("Running perf");
			exit(0);
		}
	}
#endif

	/* Do we have a command to run? */
	if (cmd != NULL) {
		if (output) {
			fprintf(output, "# Profiling: %s\n", cmd);
			fflush(output);
		}
		child_pid = fork();
		if (child_pid < 0) {
			perror("fork");
			exit(1);
		}
		else if (child_pid == 0) {
			int res;
			res = system(cmd);
			free(cmd);
			if (res < 0)
				perror("running command");
			if (output) {
				fflush(output);
				fprintf(output, "# %s exited with status %d\n", cmd, res);
				fflush(output);
			}
			exit(0);
		} else {
			free(cmd);
		}
	}

	for (i = 0; i < num_instdone_bits; i++) {
		top_bits[i].bit = &instdone_bits[i];
		top_bits[i].count = 0;
		top_bits_sorted[i] = &top_bits[i];
	}

	/* Grab access to the registers */
	intel_register_access_init(pci_dev, 1);

	ring_init(&render_ring);
	if (IS_GEN4(devid) || IS_GEN5(devid))
		ring_init(&bsd_ring);
	if (IS_GEN6(devid)) {
		ring_init(&bsd6_ring);
		ring_init(&blt_ring);
	}

		/* Calculate initial stats */
		/* Battery */
		power_stat_init(&power);
		/* CPU */
		get_cpu_stat(&oldcpu);

	/* Initialize GPU stats */
	if (HAS_STATS_REGS(devid)) {
		for (i = 0; i < STATS_COUNT; i++) {
			uint32_t stats_high, stats_low, stats_high_2;

			do {
				stats_high = INREG(stats_regs[i] + 4);
				stats_low = INREG(stats_regs[i]);
				stats_high_2 = INREG(stats_regs[i] + 4);
			} while (stats_high != stats_high_2);

			last_stats[i] = (uint64_t)stats_high << 32 |
				stats_low;
		}
	}

	for (;;) {
		int j;
		unsigned long long t1, ti, tf, t2;
		unsigned long long def_sleep = 1000000 / samples_per_sec;
		unsigned long long last_samples_per_sec = samples_per_sec;
		char clear_screen[] = {0x1b, '[', 'H',
				       0x1b, '[', 'J',
				       0x0};
		int percent;
		int len;

		t1 = gettime();

		ring_reset(&render_ring);
		ring_reset(&bsd_ring);
		ring_reset(&bsd6_ring);
		ring_reset(&blt_ring);

		for (i = 0; i < samples_per_sec; i++) {
			long long interval;
			ti = gettime();
			if (IS_965(devid)) {
				instdone = INREG(INST_DONE_I965);
				instdone1 = INREG(INST_DONE_1);
			} else
				instdone = INREG(INST_DONE);

			for (j = 0; j < num_instdone_bits; j++)
				update_idle_bit(&top_bits[j]);

			ring_sample(&render_ring);
			ring_sample(&bsd_ring);
			ring_sample(&bsd6_ring);
			ring_sample(&blt_ring);

			tf = gettime();
			if (tf - t1 >= 1000000) {
				/* We are out of sync, bail out */
				last_samples_per_sec = i+1;
				break;
			}
			interval = def_sleep - (tf - ti);
			if (interval > 0)
				usleep(interval);
		}

		if (HAS_STATS_REGS(devid)) {
			for (i = 0; i < STATS_COUNT; i++) {
				uint32_t stats_high, stats_low, stats_high_2;

				do {
					stats_high = INREG(stats_regs[i] + 4);
					stats_low = INREG(stats_regs[i]);
					stats_high_2 = INREG(stats_regs[i] + 4);
				} while (stats_high != stats_high_2);

				stats[i] = (uint64_t)stats_high << 32 |
					stats_low;
			}
		}

		qsort(top_bits_sorted, num_instdone_bits,
		      sizeof(struct top_bit *), top_bits_sort);

		/* Limit the number of lines printed to the terminal height so the
		 * most important info (at the top) will stay on screen. */
		unsigned short int max_lines = -1;
		struct winsize ws;
		if (ioctl(0, TIOCGWINSZ, &ws) != -1)
			max_lines = ws.ws_row - 6; /* exclude header lines */
		if (max_lines >= num_instdone_bits)
			max_lines = num_instdone_bits;

		t2 = gettime();
		time_diff = (t2 - t1) / 1000000.0;
		elapsed_time += time_diff;

		/* CPU */
		get_cpu_stat(&cpu);

		/* Power */
		get_power_stat(&power, time_diff);

		if (interactive) {
			printf("%s", clear_screen);
			print_clock_info(pci_dev);

			ring_print(&render_ring, last_samples_per_sec);
			ring_print(&bsd_ring, last_samples_per_sec);
			ring_print(&bsd6_ring, last_samples_per_sec);
			ring_print(&blt_ring, last_samples_per_sec);

			printf("\n%30s  %s\n", "task", "percent busy");
			for (i = 0; i < max_lines; i++) {
				if (top_bits_sorted[i]->count > 0) {
					percent = (top_bits_sorted[i]->count * 100) /
						last_samples_per_sec;
					len = printf("%30s: %3d%%: ",
							 top_bits_sorted[i]->bit->name,
							 percent);
					print_percentage_bar (percent, len);
				} else {
					printf("%*s", PERCENTAGE_BAR_END, "");
				}

				if (i < STATS_COUNT && HAS_STATS_REGS(devid)) {
					printf("%13s: %llu (%lld/sec)",
						   stats_reg_names[i],
						   (long long)stats[i],
						   (long long)(stats[i] - last_stats[i]));
					last_stats[i] = stats[i];
				} else {
					if (!top_bits_sorted[i]->count)
						break;
				}
				printf("\n");
			}
		}
		if (output) {
			/* Print headers for columns at first run */
			if (print_headers) {
				fprintf(output, "# time\t");
				system_print_header(output);
				ring_print_header(output, &render_ring);
				ring_print_header(output, &bsd_ring);
				ring_print_header(output, &bsd6_ring);
				ring_print_header(output, &blt_ring);
				for (i = 0; i < MAX_NUM_TOP_BITS; i++) {
					if (i < STATS_COUNT && HAS_STATS_REGS(devid)) {
						fprintf(output, "%s\t",
							   stats_reg_names[i]
							   );
					}
					if (!top_bits[i].count)
						continue;
				}
				fprintf(output, "\n");
				print_headers = 0;
			}

			/* Print statistics */
			fprintf(output, "%.2f\t", elapsed_time);
			system_print(output, &cpu, &oldcpu, &power);
			oldcpu = cpu;
			ring_log(&render_ring, last_samples_per_sec, output);
			ring_log(&bsd_ring, last_samples_per_sec, output);
			ring_log(&bsd6_ring, last_samples_per_sec, output);
			ring_log(&blt_ring, last_samples_per_sec, output);

			for (i = 0; i < MAX_NUM_TOP_BITS; i++) {
				if (i < STATS_COUNT && HAS_STATS_REGS(devid)) {
					fprintf(output, "%lu\t",
						   stats[i] - last_stats[i]);
					last_stats[i] = stats[i];
				}
					if (!top_bits[i].count)
						continue;
			}
			fprintf(output, "\n");
			fflush(output);
		}

		for (i = 0; i < num_instdone_bits; i++) {
			top_bits_sorted[i]->count = 0;

			if (i < STATS_COUNT)
				last_stats[i] = stats[i];
		}

		/* Check if child has gone */
		if (child_pid > 0) {
			int res;
			if ((res = waitpid(child_pid, &child_stat, WNOHANG)) == -1) {
				perror("waitpid");
				exit(1);
			}
			if (res == 0)
				continue;
			if (WIFEXITED(child_stat))
				break;
		}
	}

	fclose(output);

	intel_register_access_fini();
#if __linux__
	if (perf_output != NULL)
		free(perf_output);
#endif

	return 0;
}