summaryrefslogtreecommitdiff
path: root/tests/spec/arb_timer_query/timestamp-get.c
blob: fc48d85b0599a826e7f0db5f2abff9e184475d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright © 2012 Marek Olšák <maraeo@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "piglit-util-gl.h"

#include <inttypes.h>  /* for PRIu64 macro */
#ifdef HAVE_UNISTD_H
#include <unistd.h>    /* for usleep */
#endif

/* GL_TIMESTAMP isn't expected to be reliable for measuring long durations and
 * although the ARB_timer_query spec doesn't stipulate what kind of drifting
 * from wall clock time is acceptable, we at least want a sanity check that
 * things look reasonable...
 */
#define DRIFT_NS_PER_SEC_THRESHOLD	3000000

/**
 * @file timestamp-get.c
 *
 * Test that GL_TIMESTAMP obtained via glGet and glQuery returns roughly
 * the same value, and that durations measured via GL_TIMESTAMP have
 * nanosecond units.
 */

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;
	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;

PIGLIT_GL_TEST_CONFIG_END

static GLint64
get_gpu_time_via_query(GLuint q)
{
	GLint64 time;

	glQueryCounter(q, GL_TIMESTAMP);
	glGetQueryObjecti64v(q, GL_QUERY_RESULT, &time);
	return time;
}

static GLint64
get_gpu_time_via_get(GLuint q)
{
	GLint64 time;

	glGetInteger64v(GL_TIMESTAMP, &time);
	return time;
}

static void
validate_times(GLint64 t1, GLint64 t2, GLint64 tolerance)
{
	if (t1 > t2) {
		printf("old time = %" PRIu64 " us\n", (uint64_t) t1 / 1000);
		printf("new time = %" PRIu64 " us\n", (uint64_t) t2 / 1000);
		puts("old time > new time");
		piglit_report_result(PIGLIT_FAIL);
	}

	/* the tolerance of 1 milisecond seems to be sufficient */
	if (t2 - t1 > tolerance) {
		printf("time 1 = %" PRIu64 " us\n", (uint64_t) t1 / 1000);
		printf("time 2 = %" PRIu64 " us\n", (uint64_t) t2 / 1000);
		puts("too big difference");
		piglit_report_result(PIGLIT_FAIL);
	}
}

static void
validate_delta(GLint64 gl_ts1, GLint64 gl_ts2, GLint64 cpu_delay_ns)
{
	GLint64 gl_ts_delta = gl_ts2 - gl_ts1;
	int64_t drift = llabs(cpu_delay_ns - gl_ts_delta);
	int64_t drift_per_sec = drift * 1000000000LL /  cpu_delay_ns;

	/* XXX: technically we shouldn't be as strict about drift when the gpu
	 * clock is running fast and the duration is longer than expected,
	 * because we can't easily exclude other factors like OS scheduling
	 * affecting the measurements. For now though we don't take this into
	 * account.
	 */
	if (drift_per_sec > DRIFT_NS_PER_SEC_THRESHOLD) {
		printf("GL_TIMESTAMP 1 = %" PRId64 " us\n", gl_ts1 / 1000);
		printf("GL_TIMESTAMP 2 = %" PRId64 " us\n", gl_ts2 / 1000);
		printf("delta  = %" PRId64 " us (expect >= %"PRId64" us)\n",
		       gl_ts_delta / 1000, cpu_delay_ns / 1000);

		piglit_loge("GL_TIMESTAMP drift of %" PRId64 " ns/sec, greater than %" PRId64 " ns/sec",
			    drift_per_sec,
			    (int64_t)DRIFT_NS_PER_SEC_THRESHOLD);
		piglit_report_result(PIGLIT_FAIL);
	} else
		printf("GL_TIMESTAMP drift of approx. %" PRId64 " ns/sec\n",
		       drift_per_sec);
}

enum piglit_result
piglit_display(void)
{
	GLint64 t1, t2;
	GLint64 query_overhead, get_overhead, tolerance;
	GLuint q;
	int64_t delay;

	glGenQueries(1, &q);

	/* this creates the query in the driver */
	get_gpu_time_via_query(q);

	/* compute a reasonable tolerance based on driver overhead */
	t1 = piglit_time_get_nano();
	get_gpu_time_via_query(q);
	query_overhead = piglit_time_get_nano() - t1;

	t1 = piglit_time_get_nano();
	get_gpu_time_via_get(q);
	get_overhead = piglit_time_get_nano() - t1;

	printf("glGet overhead: %" PRIu64 " us\n", (uint64_t) get_overhead / 1000);
	printf("glQuery overhead: %" PRIu64 " us\n", (uint64_t) query_overhead / 1000);

	/* minimum tolerance is 3 ms */
	tolerance = query_overhead + get_overhead + 3000000;

	/* do testing */
	puts("Test: first glQuery, then glGet");
	t1 = get_gpu_time_via_query(q);
	t2 = get_gpu_time_via_get(q);
	validate_times(t1, t2, tolerance);

	piglit_delay_ns(10000000);

	puts("Test: first glGet, then glQuery");
	t1 = get_gpu_time_via_get(q);
	t2 = get_gpu_time_via_query(q);
	validate_times(t1, t2, tolerance);

	puts("Test: wall clock time via glQuery");
	t1 = get_gpu_time_via_query(q);
	delay = piglit_delay_ns(1000000000);
	t2 = get_gpu_time_via_query(q);
	validate_delta(t1, t2, delay);

	puts("Test: wall clock time via glGet");
	t1 = get_gpu_time_via_get(q);
	delay = piglit_delay_ns(1000000000);
	t2 = get_gpu_time_via_get(q);
	validate_delta(t1, t2, delay);

	glDeleteQueries(1, &q);

	return PIGLIT_PASS;
}

void
piglit_init(int argc, char **argv)
{
	piglit_automatic = true;

	piglit_require_gl_version(20);

	piglit_require_extension("GL_ARB_timer_query");
}