Merge branch 'mesa_7_6_branch'
[mesa.git] / progs / perf / common.c
1 /*
2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /**
23 * Common perf code. This should be re-usable with other APIs.
24 */
25
26 #include "common.h"
27 #include "glmain.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32
33
34 /* Need to add a fflush windows console with mingw, otherwise nothing
35 * shows up until program exit. May want to add logging here.
36 */
37 void
38 perf_printf(const char *format, ...)
39 {
40 va_list ap;
41 va_start(ap, format);
42
43 fflush(stdout);
44 vfprintf(stdout, format, ap);
45 fflush(stdout);
46
47 va_end(ap);
48 }
49
50
51
52 /**
53 * Run function 'f' for enough iterations to reach a steady state.
54 * Return the rate (iterations/second).
55 */
56 double
57 PerfMeasureRate(PerfRateFunc f)
58 {
59 const double minDuration = 1.0;
60 double rate = 0.0, prevRate = 0.0;
61 unsigned subiters;
62
63 /* Compute initial number of iterations to try.
64 * If the test function is pretty slow this helps to avoid
65 * extraordarily long run times.
66 */
67 subiters = 2;
68 {
69 const double t0 = PerfGetTime();
70 double t1;
71 do {
72 f(subiters); /* call the rendering function */
73 t1 = PerfGetTime();
74 subiters *= 2;
75 } while (t1 - t0 < 0.1 * minDuration);
76 }
77 /*perf_printf("initial subIters = %u\n", subiters);*/
78
79 while (1) {
80 const double t0 = PerfGetTime();
81 unsigned iters = 0;
82 double t1;
83
84 do {
85 f(subiters); /* call the rendering function */
86 t1 = PerfGetTime();
87 iters += subiters;
88 } while (t1 - t0 < minDuration);
89
90 rate = iters / (t1 - t0);
91
92 if (0)
93 perf_printf("prevRate %f rate %f ratio %f iters %u\n",
94 prevRate, rate, rate/prevRate, iters);
95
96 /* Try and speed the search up by skipping a few steps:
97 */
98 if (rate > prevRate * 1.6)
99 subiters *= 8;
100 else if (rate > prevRate * 1.2)
101 subiters *= 4;
102 else if (rate > prevRate * 1.05)
103 subiters *= 2;
104 else
105 break;
106
107 prevRate = rate;
108 }
109
110 if (0)
111 perf_printf("%s returning iters %u rate %f\n", __FUNCTION__, subiters, rate);
112 return rate;
113 }
114
115
116 /* Note static buffer, can only use once per printf.
117 */
118 const char *
119 PerfHumanFloat( double d )
120 {
121 static char buf[80];
122
123 if (d > 1000000000.0)
124 snprintf(buf, sizeof(buf), "%.1f billion", d / 1000000000.0);
125 else if (d > 1000000.0)
126 snprintf(buf, sizeof(buf), "%.1f million", d / 1000000.0);
127 else if (d > 1000.0)
128 snprintf(buf, sizeof(buf), "%.1f thousand", d / 1000.0);
129 else
130 snprintf(buf, sizeof(buf), "%.1f", d);
131
132 return buf;
133 }