1
/*
2
 * Copyright © 2006 Red Hat, Inc.
3
 * Copyright © 2009 Chris Wilson
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of the
10
 * copyright holders not be used in advertising or publicity
11
 * pertaining to distribution of the software without specific,
12
 * written prior permission. The copyright holders make no
13
 * representations about the suitability of this software for any
14
 * purpose.  It is provided "as is" without express or implied
15
 * warranty.
16
 *
17
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24
 * SOFTWARE.
25
 *
26
 * Authors: Carl Worth <cworth@cworth.org>
27
 *	    Chris Wilson <chris@chris-wilson.co.uk>
28
 */
29

            
30
#include "config.h"
31

            
32
#include "cairo-perf.h"
33
#include "cairo-stats.h"
34

            
35
#include <stdio.h>
36

            
37
#if HAVE_UNISTD_H && HAVE_SYS_IOCTL_H
38
#define USE_TERMINAL_SIZE 1
39
#else
40
#define USE_TERMINAL_SIZE 0
41
#endif
42

            
43
#if USE_TERMINAL_SIZE
44
#include <unistd.h>
45
#include <sys/ioctl.h>
46
#endif
47

            
48
static void
49
report_print (const cairo_perf_report_t *report,
50
	      int show_histogram)
51
{
52
    const test_report_t *test;
53
    cairo_histogram_t h;
54

            
55
    if (show_histogram) {
56
	int num_rows = 23;
57
	int num_cols = 80;
58

            
59
#if USE_TERMINAL_SIZE
60
	int fd = fileno(stdout);
61
	if (isatty(fd)) {
62
	    struct winsize ws;
63

            
64
	    if(ioctl(fd, TIOCGWINSZ, &ws) == 0 ) {
65
		num_rows = ws.ws_row - 1;
66
		num_cols = ws.ws_col;
67
	    }
68
	}
69
#endif
70

            
71
	if (!_cairo_histogram_init (&h, num_cols, num_rows))
72
	    show_histogram = 0;
73
    }
74

            
75
    for (test = report->tests; test->name != NULL; test++) {
76
	if (test->stats.iterations == 0)
77
	    continue;
78

            
79
	if (show_histogram) {
80
	    const cairo_time_t *values;
81
	    int num_values;
82

            
83
	    if (show_histogram > 1) {
84
		values = test->stats.values;
85
		num_values = test->stats.iterations;
86
	    } else {
87
		values = test->samples;
88
		num_values = test->samples_count;
89
	    }
90

            
91
	    if (_cairo_histogram_compute (&h, values, num_values))
92
		_cairo_histogram_printf (&h, stdout);
93
	}
94

            
95
	if (test->size) {
96
	    printf ("%5s-%-4s %26s-%-3d  ",
97
		    test->backend, test->content,
98
		    test->name, test->size);
99
	} else {
100
	    printf ("%5s %26s  ", test->backend, test->name);
101
	}
102
	printf("%6.2f %4.2f%% (%d/%d)\n",
103
	       test->stats.median_ticks / test->stats.ticks_per_ms,
104
	       test->stats.std_dev * 100,
105
	       test->stats.iterations, test->samples_count);
106
    }
107

            
108
    if (show_histogram)
109
	_cairo_histogram_fini (&h);
110
}
111

            
112
int
113
main (int	  argc,
114
      const char *argv[])
115
{
116
    cairo_bool_t show_histogram = 0;
117
    int i;
118

            
119
    for (i = 1; i < argc; i++ ) {
120
	cairo_perf_report_t report;
121

            
122
	if (strcmp(argv[i], "--histogram") == 0) {
123
	    show_histogram = 1;
124
	    continue;
125
	}
126

            
127
	if (strcmp(argv[i], "--short-histogram") == 0) {
128
	    show_histogram = 2;
129
	    continue;
130
	}
131

            
132
	cairo_perf_report_load (&report, argv[i], i, NULL);
133
	report_print (&report, show_histogram);
134
    }
135

            
136
    return 0;
137
}