2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
3 * Copyright (C) 2021 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Common perf code. This should be re-usable with other tests.
27 #include "piglit-util-gl.h"
31 measure_rate(perf_rate_func f
, double duration
, unsigned initial_iterations
,
32 double (*measure_time
)(perf_rate_func f
, unsigned iterations
))
34 unsigned iterations
= initial_iterations
;
37 double t
= measure_time(f
, iterations
);
39 /* Allow iterations to go 5% over. */
40 double time_adjusted
= t
- t
* 0.05;
41 double multiplier
= time_adjusted
? duration
/ time_adjusted
: 0;
43 bool accepted
= t
>= duration
;
44 bool converging
= (unsigned)(iterations
* multiplier
) > iterations
;
47 printf("\niterations %7u, time %f, multiplier %f%s", iterations
, t
, multiplier
,
48 accepted
? " - accepted" : (converging
? "" : " - stuck"));
51 /* Find the time it takes to run the test at duration. */
54 iterations
*= multiplier
;
56 /* Force convergence. */
58 iterations
*= iterations
/ 20;
64 /* Return iterations per second. */
65 return iterations
/ t
;
70 measure_cpu_time(perf_rate_func f
, unsigned iterations
)
72 int64_t t0
= piglit_time_get_nano();
73 f(iterations
); /* call the rendering function */
75 return (piglit_time_get_nano() - t0
) * 0.000000001;
79 measure_gpu_time(perf_rate_func f
, unsigned iterations
)
81 /* Run a few iterations to wake up power management - this helps AMD. */
85 /* Measure the execution time. */
87 glGenQueries(1, &query
);
88 glBeginQuery(GL_TIME_ELAPSED
, query
);
93 glEndQuery(GL_TIME_ELAPSED
);
94 glGetQueryObjecti64v(query
, GL_QUERY_RESULT
, &nsecs
);
95 glDeleteQueries(1, &query
);
97 return nsecs
* 0.000000001;
101 * Return iterations/second for the duration.
102 * Use a longer duration if you want more precision.
105 perf_measure_cpu_rate(perf_rate_func f
, double duration
)
107 return measure_rate(f
, duration
, 1, measure_cpu_time
);
111 perf_measure_gpu_rate(perf_rate_func f
, double duration
)
113 return measure_rate(f
, duration
, 5, measure_gpu_time
);