arb_copy_image: test copying of different mipmap levels of a texture
[piglit.git] / tests / perf / common.c
blobb07bd480c8a2a5fcbc768f828c2c44e65c41919f
1 /*
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.
23 /**
24 * Common perf code. This should be re-usable with other tests.
27 #include "piglit-util-gl.h"
28 #include "common.h"
30 static double
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;
36 while (1) {
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;
46 if (0) {
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. */
52 if (!accepted) {
53 if (converging) {
54 iterations *= multiplier;
55 } else {
56 /* Force convergence. */
57 if (iterations > 20)
58 iterations *= iterations / 20;
59 iterations += 5;
61 continue;
64 /* Return iterations per second. */
65 return iterations / t;
69 static double
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 */
74 glFinish();
75 return (piglit_time_get_nano() - t0) * 0.000000001;
78 static double
79 measure_gpu_time(perf_rate_func f, unsigned iterations)
81 /* Run a few iterations to wake up power management - this helps AMD. */
82 f(iterations);
83 f(iterations);
85 /* Measure the execution time. */
86 GLuint query;
87 glGenQueries(1, &query);
88 glBeginQuery(GL_TIME_ELAPSED, query);
90 f(iterations);
92 int64_t nsecs;
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.
104 double
105 perf_measure_cpu_rate(perf_rate_func f, double duration)
107 return measure_rate(f, duration, 1, measure_cpu_time);
110 double
111 perf_measure_gpu_rate(perf_rate_func f, double duration)
113 return measure_rate(f, duration, 5, measure_gpu_time);