blob: 695b8a99d9d077ff7f9ad4bb556a516339d36aa8 [file] [log] [blame]
Brian Paul9abbeda2009-09-16 19:33:01 -06001/*
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
Keith Whitwelle95a3a22009-09-17 12:08:04 +010029#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 */
37void
38perf_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
Brian Paul9abbeda2009-09-16 19:33:01 -060051
52/**
53 * Run function 'f' for enough iterations to reach a steady state.
54 * Return the rate (iterations/second).
55 */
56double
57PerfMeasureRate(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 }
Keith Whitwelle95a3a22009-09-17 12:08:04 +010077 /*perf_printf("initial subIters = %u\n", subiters);*/
Brian Paul9abbeda2009-09-16 19:33:01 -060078
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)
Keith Whitwelle95a3a22009-09-17 12:08:04 +010093 perf_printf("prevRate %f rate %f ratio %f iters %u\n",
Brian Paul9abbeda2009-09-16 19:33:01 -060094 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)
Keith Whitwelle95a3a22009-09-17 12:08:04 +0100111 perf_printf("%s returning iters %u rate %f\n", __FUNCTION__, subiters, rate);
Brian Paul9abbeda2009-09-16 19:33:01 -0600112 return rate;
113}
114
115