igt_core.h 31.1 KB
Newer Older
Daniel Vetter's avatar
Daniel Vetter committed
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
/*
 * Copyright © 2007,2014 Intel Corporation
 *
 * 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.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *    Daniel Vetter <daniel.vetter@ffwll.ch>
 *
 */


#ifndef IGT_CORE_H
#define IGT_CORE_H

33
#include <assert.h>
34
#include <setjmp.h>
35
#include <stdbool.h>
36
#include <stdint.h>
37
#include <stdlib.h>
38
#include <stdio.h>
39 40
#include <string.h>
#include <sys/types.h>
41
#include <stdarg.h>
42
#include <getopt.h>
43
#include <unistd.h>
44

45 46 47 48 49
#ifndef IGT_LOG_DOMAIN
#define IGT_LOG_DOMAIN (NULL)
#endif


50 51 52 53 54 55 56 57
#ifndef STATIC_ANALYSIS_BUILD
#if defined(__clang_analyzer__) || defined(__COVERITY__) || defined(__KLOCWORK__)
#define STATIC_ANALYSIS_BUILD 1
#else
#define STATIC_ANALYSIS_BUILD 0
#endif
#endif

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/**
 * BUILD_BUG_ON_INVALID:
 * @expr: Expression
 *
 * A macro that takes an expression and generates no code. Used for
 * checking at build-time that an expression is valid code.
 */
#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((long)(e))))

/**
 * igt_assume:
 * @expr: Condition to test
 *
 * An assert-like macro to be used for tautologies to give hints to
 * static analysis of code. No-op if STATIC_ANALYSIS_BUILD is not
 * defined, expands to an assert() if it is.
 */
#if STATIC_ANALYSIS_BUILD
#define igt_assume(e) assert(e)
#else
/* Make sure the expression is still parsed even though it generates no code */
#define igt_assume(e) BUILD_BUG_ON_INVALID(e)
#endif

82
extern const char* __igt_test_description __attribute__((weak));
83
extern bool __igt_plain_output;
84
extern char *igt_frame_dump_path;
85 86 87 88 89 90 91 92 93 94 95

/**
 * IGT_TEST_DESCRIPTION:
 * @str: description string
 *
 * Defines a description for a test. This is used as the output for the
 * "--help-description" option and is also included in the generated
 * documentation.
 */
#define IGT_TEST_DESCRIPTION(str) const char* __igt_test_description = str

96 97 98 99 100 101 102 103 104 105 106 107 108 109
/**
 * IGT_EXIT_SKIP:
 *
 * Exit status indicating the test was skipped.
 */
#define IGT_EXIT_SKIP    77

/**
 * IGT_EXIT_SUCCESS
 *
 * Exit status indicating the test executed successfully.
 */
#define IGT_EXIT_SUCCESS 0

110 111 112 113 114 115 116
/**
 * IGT_EXIT_INVALID
 *
 * Exit status indicating an invalid option or subtest was specified
 */
#define IGT_EXIT_INVALID 79

117 118 119 120 121 122
/**
 * IGT_EXIT_FAILURE
 *
 * Exit status indicating a test failure
 */
#define IGT_EXIT_FAILURE 99
123

Daniel Vetter's avatar
Daniel Vetter committed
124 125 126 127 128 129 130 131 132
bool __igt_fixture(void);
void __igt_fixture_complete(void);
void __igt_fixture_end(void) __attribute__((noreturn));
/**
 * igt_fixture:
 *
 * Annotate global test fixture code
 *
 * Testcase with subtests often need to set up a bunch of global state as the
133 134
 * common test fixture. To avoid such code interfering with the subtest
 * enumeration (e.g. when enumerating on systems without an intel gpu) such
Daniel Vetter's avatar
Daniel Vetter committed
135 136
 * blocks should be annotated with igt_fixture.
 */
137
#define igt_fixture for (volatile int igt_tokencat(__tmpint,__LINE__) = 0; \
Daniel Vetter's avatar
Daniel Vetter committed
138
			 igt_tokencat(__tmpint,__LINE__) < 1 && \
139 140 141
			 (STATIC_ANALYSIS_BUILD || \
			 (__igt_fixture() && \
			 (sigsetjmp(igt_subtest_jmpbuf, 1) == 0))); \
Daniel Vetter's avatar
Daniel Vetter committed
142 143 144 145 146
			 igt_tokencat(__tmpint,__LINE__) ++, \
			 __igt_fixture_complete())

/* subtest infrastructure */
jmp_buf igt_subtest_jmpbuf;
147
typedef int (*igt_opt_handler_t)(int opt, int opt_index, void *data);
148
#ifndef __GTK_DOC_IGNORE__ /* gtkdoc wants to document this forward decl */
Daniel Vetter's avatar
Daniel Vetter committed
149
struct option;
150
#endif
151
int igt_subtest_init_parse_opts(int *argc, char **argv,
Daniel Vetter's avatar
Daniel Vetter committed
152
				const char *extra_short_opts,
153
				const struct option *extra_long_opts,
Daniel Vetter's avatar
Daniel Vetter committed
154
				const char *help_str,
155 156
				igt_opt_handler_t extra_opt_handler,
				void *handler_data);
Daniel Vetter's avatar
Daniel Vetter committed
157

158 159 160 161 162 163 164

/**
 * igt_subtest_init:
 * @argc: argc from the test's main()
 * @argv: argv from the test's main()
 *
 * This initializes the for tests with subtests without the need for additional
165
 * command line options. It is just a simplified version of
166 167 168 169 170 171
 * igt_subtest_init_parse_opts().
 *
 * If there's not a reason to the contrary it's less error prone to just use an
 * #igt_main block instead of stitching the test's main() function together
 * manually.
 */
172 173
#define igt_subtest_init(argc, argv) \
	igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL);
174

Daniel Vetter's avatar
Daniel Vetter committed
175
bool __igt_run_subtest(const char *subtest_name);
176 177 178 179 180 181 182 183 184 185 186 187
#define __igt_tokencat2(x, y) x ## y

/**
 * igt_tokencat:
 * @x: first variable
 * @y: second variable
 *
 * C preprocessor helper to concatenate two variables while properly expanding
 * them.
 */
#define igt_tokencat(x, y) __igt_tokencat2(x, y)

Daniel Vetter's avatar
Daniel Vetter committed
188 189
/**
 * igt_subtest:
190
 * @name: name of the subtest
Daniel Vetter's avatar
Daniel Vetter committed
191
 *
192
 * This is a magic control flow block which denotes a subtest code block. Within
193
 * that code block igt_skip|success will only bail out of the subtest. The _f
194 195
 * variant accepts a printf format string, which is useful for constructing
 * combinatorial tests.
Daniel Vetter's avatar
Daniel Vetter committed
196
 *
197
 * This is a simpler version of igt_subtest_f()
Daniel Vetter's avatar
Daniel Vetter committed
198
 */
199
#define igt_subtest(name) for (; __igt_run_subtest((name)) && \
200
				   (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \
201
				   igt_success())
Daniel Vetter's avatar
Daniel Vetter committed
202 203 204 205 206
#define __igt_subtest_f(tmp, format...) \
	for (char tmp [256]; \
	     snprintf( tmp , sizeof( tmp ), \
		      format), \
	     __igt_run_subtest( tmp ) && \
207
	     (sigsetjmp(igt_subtest_jmpbuf, 1) == 0); \
Daniel Vetter's avatar
Daniel Vetter committed
208 209 210 211
	     igt_success())

/**
 * igt_subtest_f:
212
 * @...: format string and optional arguments
Daniel Vetter's avatar
Daniel Vetter committed
213
 *
214
 * This is a magic control flow block which denotes a subtest code block. Within
215
 * that code block igt_skip|success will only bail out of the subtest. The _f
216 217
 * variant accepts a printf format string, which is useful for constructing
 * combinatorial tests.
Daniel Vetter's avatar
Daniel Vetter committed
218
 *
219 220
 * Like igt_subtest(), but also accepts a printf format string instead of a
 * static string.
Daniel Vetter's avatar
Daniel Vetter committed
221 222 223
 */
#define igt_subtest_f(f...) \
	__igt_subtest_f(igt_tokencat(__tmpchar, __LINE__), f)
224

Daniel Vetter's avatar
Daniel Vetter committed
225
const char *igt_subtest_name(void);
226 227
bool igt_only_list_subtests(void);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
void __igt_subtest_group_save(int *);
void __igt_subtest_group_restore(int);
/**
 * igt_subtest_group:
 *
 * Group a set of subtests together with their common setup code
 *
 * Testcase with subtests often need to set up a bunch of shared state as the
 * common test fixture. But if there are multiple with different requirements
 * the commont setup code can't be extracted, since a test condition failure in
 * e.g. igt_require() would result in all subsequent tests skipping. Even those
 * from a different group.
 *
 * This macro allows to group together a set of #igt_fixture and #igt_subtest
 * clauses. If any common setup in a fixture fails, only the subtests in this
 * group will fail or skip. Subtest groups can be arbitrarily nested.
 */
#define igt_subtest_group for (int igt_tokencat(__tmpint,__LINE__) = 0, \
			       igt_tokencat(__save,__LINE__) = 0; \
			       igt_tokencat(__tmpint,__LINE__) < 1 && \
			       (__igt_subtest_group_save(& igt_tokencat(__save,__LINE__) ), true); \
			       igt_tokencat(__tmpint,__LINE__) ++, \
			       __igt_subtest_group_restore(igt_tokencat(__save,__LINE__) ))

252 253 254 255 256
/**
 * igt_main:
 *
 * This is a magic control flow block used instead of a main() function for
 * tests with subtests. Open-coding the main() function is only recommended if
257
 * the test needs to parse additional command line arguments of its own.
258
 */
Daniel Vetter's avatar
Daniel Vetter committed
259 260 261
#define igt_main \
	static void igt_tokencat(__real_main, __LINE__)(void); \
	int main(int argc, char **argv) { \
262 263
		igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, \
					    NULL, NULL); \
Daniel Vetter's avatar
Daniel Vetter committed
264 265 266 267 268
		igt_tokencat(__real_main, __LINE__)(); \
		igt_exit(); \
	} \
	static void igt_tokencat(__real_main, __LINE__)(void) \

269

270
const char *igt_test_name(void);
271
void igt_simple_init_parse_opts(int *argc, char **argv,
272
				const char *extra_short_opts,
273
				const struct option *extra_long_opts,
274
				const char *help_str,
275 276
				igt_opt_handler_t extra_opt_handler,
				void *handler_data);
277

278 279 280 281 282 283 284 285 286 287 288
/**
 * igt_simple_init:
 * @argc: argc from the test's main()
 * @argv: argv from the test's main()
 *
 * This initializes a simple test without any support for subtests.
 *
 * If there's not a reason to the contrary it's less error prone to just use an
 * #igt_simple_main block instead of stitching the test's main() function together
 * manually.
 */
289 290
#define igt_simple_init(argc, argv) \
	igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL);
291

292 293 294 295 296
/**
 * igt_simple_main:
 *
 * This is a magic control flow block used instead of a main() function for
 * simple tests. Open-coding the main() function is only recommended if
297
 * the test needs to parse additional command line arguments of its own.
298
 */
Daniel Vetter's avatar
Daniel Vetter committed
299 300 301
#define igt_simple_main \
	static void igt_tokencat(__real_main, __LINE__)(void); \
	int main(int argc, char **argv) { \
302 303
		igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, \
					   NULL, NULL); \
Daniel Vetter's avatar
Daniel Vetter committed
304
		igt_tokencat(__real_main, __LINE__)(); \
305
		igt_exit(); \
Daniel Vetter's avatar
Daniel Vetter committed
306 307 308
	} \
	static void igt_tokencat(__real_main, __LINE__)(void) \

309 310 311 312 313 314 315 316 317 318 319 320 321
/**
 * igt_constructor:
 *
 * Convenience macro to run the provided code block when igt first starts,
 * before any tests have been run. This should be used for parts of the igt
 * library that require initialization of objects with global context.
 *
 * This code block will be executed exactly once.
 */
#define igt_constructor \
	__attribute__((constructor)) \
	static void igt_tokencat(__igt_constructor_l, __LINE__)(void)

Daniel Vetter's avatar
Daniel Vetter committed
322 323 324 325 326 327
__attribute__((format(printf, 1, 2)))
void igt_skip(const char *f, ...) __attribute__((noreturn));
__attribute__((format(printf, 5, 6)))
void __igt_skip_check(const char *file, const int line,
		      const char *func, const char *check,
		      const char *format, ...) __attribute__((noreturn));
328
#define igt_skip_check(E, F...) \
329
	__igt_skip_check(__FILE__, __LINE__, __func__, E, F)
Daniel Vetter's avatar
Daniel Vetter committed
330
void igt_success(void);
Daniel Vetter's avatar
Daniel Vetter committed
331 332

bool igt_can_fail(void);
Daniel Vetter's avatar
Daniel Vetter committed
333 334

void igt_fail(int exitcode) __attribute__((noreturn));
335 336
__attribute__((format(printf, 6, 7)))
void __igt_fail_assert(const char *domain, const char *file,
Daniel Vetter's avatar
Daniel Vetter committed
337 338 339 340
		       const int line, const char *func, const char *assertion,
		       const char *format, ...)
	__attribute__((noreturn));
void igt_exit(void) __attribute__((noreturn));
341
void igt_fatal_error(void) __attribute__((noreturn));
342

343 344 345 346 347 348 349 350 351 352 353
/**
 * igt_ignore_warn:
 * @expr: condition to ignore
 *
 *
 * Stops the compiler warning about an unused return value.
 */
static inline void igt_ignore_warn(bool value)
{
}

Daniel Vetter's avatar
Daniel Vetter committed
354 355
/**
 * igt_assert:
356
 * @expr: condition to test
Daniel Vetter's avatar
Daniel Vetter committed
357
 *
358
 * Fails (sub-)test if the condition is not met.
Daniel Vetter's avatar
Daniel Vetter committed
359 360 361 362 363
 *
 * Should be used everywhere where a test checks results.
 */
#define igt_assert(expr) \
	do { if (!(expr)) \
364
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , NULL); \
Daniel Vetter's avatar
Daniel Vetter committed
365
	} while (0)
366 367 368 369 370 371

/**
 * igt_assert_f:
 * @expr: condition to test
 * @...: format string and optional arguments
 *
372
 * Fails (sub-)test if the condition is not met.
373 374 375 376 377 378
 *
 * Should be used everywhere where a test checks results.
 *
 * In addition to the plain igt_assert() helper this allows to print additional
 * information to help debugging test failures.
 */
Daniel Vetter's avatar
Daniel Vetter committed
379 380
#define igt_assert_f(expr, f...) \
	do { if (!(expr)) \
381
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , f); \
Daniel Vetter's avatar
Daniel Vetter committed
382
	} while (0)
383

384 385 386 387 388 389 390 391 392 393 394
/**
 * igt_fail_on:
 * @expr: condition to test
 *
 * Fails (sub-)test if the condition is met.
 *
 * Should be used everywhere where a test checks results.
 */
#define igt_fail_on(expr) igt_assert(!(expr))

/**
395
 * igt_fail_on_f:
396 397 398 399 400 401 402 403 404 405 406 407
 * @expr: condition to test
 * @...: format string and optional arguments
 *
 * Fails (sub-)test if the condition is met.
 *
 * Should be used everywhere where a test checks results.
 *
 * In addition to the plain igt_assert() helper this allows to print additional
 * information to help debugging test failures.
 */
#define igt_fail_on_f(expr, f...) igt_assert_f(!(expr), f)

Daniel Vetter's avatar
Daniel Vetter committed
408
/**
409 410 411
 * igt_assert_cmpint:
 * @n1: first value
 * @cmp: compare operator
412
 * @ncmp: negated version of @cmp
413 414 415 416 417
 * @n2: second value
 *
 * Fails (sub-)test if the condition is not met
 *
 * Should be used everywhere where a test compares two integer values.
Daniel Vetter's avatar
Daniel Vetter committed
418
 *
419 420
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
Daniel Vetter's avatar
Daniel Vetter committed
421
 */
422
#define igt_assert_cmpint(n1, cmp, ncmp, n2) \
Daniel Vetter's avatar
Daniel Vetter committed
423 424 425
	do { \
		int __n1 = (n1), __n2 = (n2); \
		if (__n1 cmp __n2) ; else \
426
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
Daniel Vetter's avatar
Daniel Vetter committed
427
				  #n1 " " #cmp " " #n2, \
428
				  "error: %d " #ncmp " %d\n", __n1, __n2); \
429 430
	} while (0)

431 432 433 434 435 436 437
/**
 * igt_assert_cmpuint:
 * @n1: first value
 * @cmp: compare operator
 * @ncmp: negated version of @cmp
 * @n2: second value
 *
438
 * Like igt_assert_cmpint(), but for unsigned ints.
439
 */
440 441 442 443
#define igt_assert_cmpuint(n1, cmp, ncmp, n2) \
	do { \
		uint32_t __n1 = (n1), __n2 = (n2); \
		if (__n1 cmp __n2) ; else \
444
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
445
				  #n1 " " #cmp " " #n2, \
446
				  "error: %#x " #ncmp " %#x\n", __n1, __n2); \
Daniel Vetter's avatar
Daniel Vetter committed
447 448
	} while (0)

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/**
 * igt_assert_cmps64:
 * @n1: first value
 * @cmp: compare operator
 * @ncmp: negated version of @cmp
 * @n2: second value
 *
 * Like igt_assert_cmpuint(), but for larger signed ints.
 */
#define igt_assert_cmps64(n1, cmp, ncmp, n2) \
	do { \
		int64_t __n1 = (n1), __n2 = (n2); \
		if (__n1 cmp __n2) ; else \
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
				  #n1 " " #cmp " " #n2, \
				  "error: %lld " #ncmp " %lld\n", (long long)__n1, (long long)__n2); \
	} while (0)

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
/**
 * igt_assert_cmpu64:
 * @n1: first value
 * @cmp: compare operator
 * @ncmp: negated version of @cmp
 * @n2: second value
 *
 * Like igt_assert_cmpuint(), but for larger ints.
 */
#define igt_assert_cmpu64(n1, cmp, ncmp, n2) \
	do { \
		uint64_t __n1 = (n1), __n2 = (n2); \
		if (__n1 cmp __n2) ; else \
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
				  #n1 " " #cmp " " #n2, \
				  "error: %#llx " #ncmp " %#llx\n", (long long)__n1, (long long)__n2); \
	} while (0)

485 486 487 488 489 490 491
/**
 * igt_assert_cmpdouble:
 * @n1: first value
 * @cmp: compare operator
 * @ncmp: negated version of @cmp
 * @n2: second value
 *
492
 * Like igt_assert_cmpint(), but for doubles.
493 494 495 496 497 498 499 500 501 502
 */
#define igt_assert_cmpdouble(n1, cmp, ncmp, n2) \
	do { \
		double __n1 = (n1), __n2 = (n2); \
		if (__n1 cmp __n2) ; else \
		__igt_fail_assert(IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
				  #n1 " " #cmp " " #n2, \
				  "error: %#lf " #ncmp " %#lf\n", __n1, __n2); \
	} while (0)

503 504 505 506 507 508 509 510 511 512 513
/**
 * igt_assert_eq:
 * @n1: first integer
 * @n2: second integer
 *
 * Fails (sub-)test if the two integers are not equal. Beware that for now this
 * only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
514
#define igt_assert_eq(n1, n2) igt_assert_cmpint(n1, ==, !=, n2)
515 516 517 518 519 520 521 522

/**
 * igt_assert_eq_u32:
 * @n1: first integer
 * @n2: second integer
 *
 * Like igt_assert_eq(), but for uint32_t.
 */
523 524
#define igt_assert_eq_u32(n1, n2) igt_assert_cmpuint(n1, ==, !=, n2)

525 526 527 528 529 530 531 532 533
/**
 * igt_assert_eq_s64:
 * @n1: first integer
 * @n2: second integer
 *
 * Like igt_assert_eq_u32(), but for int64_t.
 */
#define igt_assert_eq_s64(n1, n2) igt_assert_cmps64(n1, ==, !=, n2)

534 535 536 537 538 539 540 541 542
/**
 * igt_assert_eq_u64:
 * @n1: first integer
 * @n2: second integer
 *
 * Like igt_assert_eq_u32(), but for uint64_t.
 */
#define igt_assert_eq_u64(n1, n2) igt_assert_cmpu64(n1, ==, !=, n2)

543 544 545 546 547 548 549 550 551
/**
 * igt_assert_eq_double:
 * @n1: first double
 * @n2: second double
 *
 * Like igt_assert_eq(), but for doubles.
 */
#define igt_assert_eq_double(n1, n2) igt_assert_cmpdouble(n1, ==, !=, n2)

552 553 554 555 556 557 558 559 560 561 562 563 564
/**
 * igt_assert_neq:
 * @n1: first integer
 * @n2: second integer
 *
 * Fails (sub-)test if the two integers are equal. Beware that for now this
 * only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_neq(n1, n2) igt_assert_cmpint(n1, !=, ==, n2)

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
/**
 * igt_assert_neq_u32:
 * @n1: first integer
 * @n2: second integer
 *
 * Like igt_assert_neq(), but for uint32_t.
 */
#define igt_assert_neq_u32(n1, n2) igt_assert_cmpuint(n1, !=, ==, n2)

/**
 * igt_assert_neq_u64:
 * @n1: first integer
 * @n2: second integer
 *
 * Like igt_assert_neq_u32(), but for uint64_t.
 */
#define igt_assert_neq_u64(n1, n2) igt_assert_cmpu64(n1, !=, ==, n2)

/**
 * igt_assert_neq_double:
 * @n1: first double
 * @n2: second double
 *
 * Like igt_assert_neq(), but for doubles.
 */
#define igt_assert_neq_double(n1, n2) igt_assert_cmpdouble(n1, !=, ==, n2)

592 593 594 595 596
/**
 * igt_assert_lte:
 * @n1: first integer
 * @n2: second integer
 *
597
 * Fails (sub-)test if the second integer is strictly smaller than the first.
598 599 600 601 602 603
 * Beware that for now this only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_lte(n1, n2) igt_assert_cmpint(n1, <=, >, n2)
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
/**
 * igt_assert_lte_u64:
 * @n1: first integer
 * @n2: second integer
 *
 * Fails (sub-)test if the second integer is strictly smaller than the first.
 * Beware that for now this only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_lte_u64(n1, n2) igt_assert_cmpu64(n1, <=, >, n2)

/**
 * igt_assert_lte_s64:
 * @n1: first integer
 * @n2: second integer
 *
 * Fails (sub-)test if the second integer is strictly smaller than the first.
 * Beware that for now this only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_lte_s64(n1, n2) igt_assert_cmps64(n1, <=, >, n2)

Daniel Vetter's avatar
Daniel Vetter committed
631 632 633 634 635
/**
 * igt_assert_lt:
 * @n1: first integer
 * @n2: second integer
 *
636
 * Fails (sub-)test if the second integer is smaller than or equal to the first.
Daniel Vetter's avatar
Daniel Vetter committed
637 638 639 640 641 642 643
 * Beware that for now this only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_lt(n1, n2) igt_assert_cmpint(n1, <, >=, n2)

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
/**
 * igt_assert_lt_u64:
 * @n1: first integer
 * @n2: second integer
 *
 * Fails (sub-)test if the second integer is smaller than or equal to the first.
 * Beware that for now this only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_lt_u64(n1, n2) igt_assert_cmpu64(n1, <, >=, n2)

/**
 * igt_assert_lt_s64:
 * @n1: first integer
 * @n2: second integer
 *
 * Fails (sub-)test if the second integer is smaller than or equal to the first.
 * Beware that for now this only works on integers.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_lt_s64(n1, n2) igt_assert_cmps64(n1, <, >=, n2)

670 671 672 673 674 675 676 677 678 679 680 681
/**
 * igt_assert_fd:
 * @fd: file descriptor
 *
 * Fails (sub-) test if the given file descriptor is invalid.
 *
 * Like igt_assert(), but displays the values being compared on failure instead
 * of simply printing the stringified expression.
 */
#define igt_assert_fd(fd) \
	igt_assert_f(fd >= 0, "file descriptor " #fd " failed\n");

Daniel Vetter's avatar
Daniel Vetter committed
682 683
/**
 * igt_require:
684
 * @expr: condition to test
Daniel Vetter's avatar
Daniel Vetter committed
685
 *
686
 * Skip a (sub-)test if a condition is not met.
Daniel Vetter's avatar
Daniel Vetter committed
687
 *
688 689 690
 * Should be used everywhere where a test checks results to decide about
 * skipping. This is useful to streamline the skip logic since it allows for a more flat
 * code control flow, similar to igt_assert()
Daniel Vetter's avatar
Daniel Vetter committed
691
 */
692 693
#define igt_require(expr) do { \
	if (!(expr)) igt_skip_check(#expr , NULL); \
694
	else igt_debug("Test requirement passed: %s\n", #expr); \
695
} while (0)
696 697 698 699 700 701 702 703 704 705 706

/**
 * igt_skip_on:
 * @expr: condition to test
 *
 * Skip a (sub-)test if a condition is met.
 *
 * Should be used everywhere where a test checks results to decide about
 * skipping. This is useful to streamline the skip logic since it allows for a more flat
 * code control flow, similar to igt_assert()
 */
707 708
#define igt_skip_on(expr) do { \
	if ((expr)) igt_skip_check("!(" #expr ")" , NULL); \
709
	else igt_debug("Test requirement passed: !(%s)\n", #expr); \
710
} while (0)
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

/**
 * igt_require_f:
 * @expr: condition to test
 * @...: format string and optional arguments
 *
 * Skip a (sub-)test if a condition is not met.
 *
 * Should be used everywhere where a test checks results to decide about
 * skipping. This is useful to streamline the skip logic since it allows for a more flat
 * code control flow, similar to igt_assert()
 *
 * In addition to the plain igt_require() helper this allows to print additional
 * information to help debugging test failures.
 */
726 727
#define igt_require_f(expr, f...) do { \
	if (!(expr)) igt_skip_check(#expr , f); \
728
	else igt_debug("Test requirement passed: %s\n", #expr); \
729
} while (0)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

/**
 * igt_skip_on_f:
 * @expr: condition to test
 * @...: format string and optional arguments
 *
 * Skip a (sub-)test if a condition is met.
 *
 * Should be used everywhere where a test checks results to decide about
 * skipping. This is useful to streamline the skip logic since it allows for a more flat
 * code control flow, similar to igt_assert()
 *
 * In addition to the plain igt_skip_on() helper this allows to print additional
 * information to help debugging test failures.
 */
745
#define igt_skip_on_f(expr, f...) do { \
746
	if ((expr)) igt_skip_check("!("#expr")", f); \
747
	else igt_debug("Test requirement passed: !(%s)\n", #expr); \
748
} while (0)
Daniel Vetter's avatar
Daniel Vetter committed
749 750 751

/* fork support code */
bool __igt_fork(void);
752

Daniel Vetter's avatar
Daniel Vetter committed
753 754 755 756 757
/**
 * igt_fork:
 * @child: name of the int variable with the child number
 * @num_children: number of children to fork
 *
758 759 760 761 762 763
 * This is a magic control flow block which spawns parallel test threads with
 * fork().
 *
 * The test children execute in parallel to the main test thread. Joining all
 * test threads should be done with igt_waitchildren to ensure that the exit
 * codes of all children are properly reflected in the test status.
Daniel Vetter's avatar
Daniel Vetter committed
764
 *
765 766
 * Note that igt_skip() will not be forwarded, feature tests need to be done
 * before spawning threads with igt_fork().
Daniel Vetter's avatar
Daniel Vetter committed
767 768 769 770
 */
#define igt_fork(child, num_children) \
	for (int child = 0; child < (num_children); child++) \
		for (; __igt_fork(); exit(0))
771
int __igt_waitchildren(void);
Daniel Vetter's avatar
Daniel Vetter committed
772
void igt_waitchildren(void);
773
void igt_waitchildren_timeout(int seconds, const char *reason);
Daniel Vetter's avatar
Daniel Vetter committed
774

775
/**
776
 * igt_helper_process:
777
 * @running: indicates whether the process is currently running
778
 * @use_SIGKILL: whether the helper should be terminated with SIGKILL or SIGTERM
779 780 781 782 783 784
 * @pid: pid of the helper if @running is true
 * @id: internal id
 *
 * Tracking structure for helper processes. Users of the i-g-t library should
 * only set @use_SIGKILL directly.
 */
Daniel Vetter's avatar
Daniel Vetter committed
785 786 787 788 789 790 791
struct igt_helper_process {
	bool running;
	bool use_SIGKILL;
	pid_t pid;
	int id;
};
bool __igt_fork_helper(struct igt_helper_process *proc);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

/**
 * igt_fork_helper:
 * @proc: #igt_helper_process structure
 *
 * This is a magic control flow block which denotes an asynchronous helper
 * process block. The difference compared to igt_fork() is that failures from
 * the child process will not be forwarded, making this construct more suitable
 * for background processes. Common use cases are regular interference of the
 * main test thread through e.g. sending signals or evicting objects through
 * debugfs. Through the explicit #igt_helper_process they can also be controlled
 * in a more fine-grained way than test children spawned through igt_fork().
 *
 * For tests with subtest helper process can be started outside of a
 * #igt_subtest block.
 *
 * Calling igt_wait_helper() joins a helper process and igt_stop_helper()
 * forcefully terminates it.
 */
Daniel Vetter's avatar
Daniel Vetter committed
811 812
#define igt_fork_helper(proc) \
	for (; __igt_fork_helper(proc); exit(0))
813
int igt_wait_helper(struct igt_helper_process *proc);
814
void igt_stop_helper(struct igt_helper_process *proc);
Daniel Vetter's avatar
Daniel Vetter committed
815 816

/* exit handler code */
817 818 819 820 821 822 823 824 825

/**
 * igt_exit_handler_t:
 * @sig: Signal number which caused the exit or 0.
 *
 * Exit handler type used by igt_install_exit_handler(). Note that exit handlers
 * can potentially be run from signal handling contexts, the @sig parameter can
 * be used to figure this out and act accordingly.
 */
Daniel Vetter's avatar
Daniel Vetter committed
826 827 828 829 830 831 832 833
typedef void (*igt_exit_handler_t)(int sig);

/* reliable atexit helpers, also work when killed by a signal (if possible) */
void igt_install_exit_handler(igt_exit_handler_t fn);

/* helpers to automatically reduce test runtime in simulation */
bool igt_run_in_simulation(void);
/**
834 835 836
 * SLOW_QUICK:
 * @slow: value in simulation mode
 * @quick: value in normal mode
Daniel Vetter's avatar
Daniel Vetter committed
837
 *
838 839
 * Simple macro to select between two values (e.g. number of test rounds or test
 * buffer size) depending upon whether i-g-t is run in simulation mode or not.
Daniel Vetter's avatar
Daniel Vetter committed
840
 */
841 842
#define SLOW_QUICK(slow,quick) (igt_run_in_simulation() ? (quick) : (slow))

Daniel Vetter's avatar
Daniel Vetter committed
843 844
void igt_skip_on_simulation(void);

845
extern const char *igt_interactive_debug;
846

847 848 849 850 851 852 853 854 855 856
/**
 * igt_log_level:
 * @IGT_LOG_DEBUG: debug information, not printed by default
 * @IGT_LOG_INFO: informational message, printed by default
 * @IGT_LOG_WARN: non-fatal warnings which should be treated as test failures
 * @IGT_LOG_CRITICAL: critical errors which lead to immediate termination of tests
 * @IGT_LOG_NONE: unused
 *
 * Log levels used by functions like igt_log().
 */
Daniel Vetter's avatar
Daniel Vetter committed
857 858 859 860
enum igt_log_level {
	IGT_LOG_DEBUG,
	IGT_LOG_INFO,
	IGT_LOG_WARN,
861
	IGT_LOG_CRITICAL,
Daniel Vetter's avatar
Daniel Vetter committed
862 863
	IGT_LOG_NONE,
};
Thomas Wood's avatar
Thomas Wood committed
864 865 866 867
__attribute__((format(printf, 3, 4)))
void igt_log(const char *domain, enum igt_log_level level, const char *format, ...);
__attribute__((format(printf, 3, 0)))
void igt_vlog(const char *domain, enum igt_log_level level, const char *format, va_list args);
868 869 870 871 872

/**
 * igt_debug:
 * @...: format string and optional arguments
 *
873
 * Wrapper for igt_log() for message at the IGT_LOG_DEBUG level.
874
 */
Thomas Wood's avatar
Thomas Wood committed
875
#define igt_debug(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_DEBUG, f)
876 877 878 879 880

/**
 * igt_info:
 * @...: format string and optional arguments
 *
881
 * Wrapper for igt_log() for message at the IGT_LOG_INFO level.
882
 */
Thomas Wood's avatar
Thomas Wood committed
883
#define igt_info(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_INFO, f)
884 885 886 887 888

/**
 * igt_warn:
 * @...: format string and optional arguments
 *
889
 * Wrapper for igt_log() for message at the IGT_LOG_WARN level.
890
 */
Thomas Wood's avatar
Thomas Wood committed
891
#define igt_warn(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_WARN, f)
892 893 894 895 896 897 898 899 900

/**
 * igt_critical:
 * @...: format string and optional arguments
 *
 * Wrapper for igt_log() for message at the IGT_LOG_CRITICAL level.
 */
#define igt_critical(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_CRITICAL, f)

901 902 903
typedef bool (*igt_buffer_log_handler_t)(const char *line, void *data);
void igt_log_buffer_inspect(igt_buffer_log_handler_t check, void *data);

Daniel Vetter's avatar
Daniel Vetter committed
904 905
extern enum igt_log_level igt_log_level;

906 907 908 909 910 911 912 913 914
/**
 * igt_warn_on:
 * @condition: condition to test
 *
 * Print a IGT_LOG_WARN level message if a condition is not met.
 *
 * Should be used everywhere where a test checks results to decide about
 * printing warnings. This is useful to streamline the test logic since it
 * allows for a more flat code control flow, similar to igt_assert()
915 916
 *
 * This macro also returns the value of @condition.
917
 */
918 919 920 921
#define igt_warn_on(condition) ({ \
		typeof(condition) ret__ = (condition); \
		if (ret__) \
			igt_warn("Warning on condition %s in function %s, file %s:%i\n", \
Daniel Vetter's avatar
Daniel Vetter committed
922
				 #condition, __func__, __FILE__, __LINE__); \
923 924
		ret__; \
	})
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

/**
 * igt_warn_on_f:
 * @condition: condition to test
 * @...: format string and optional arguments
 *
 * Skip a (sub-)test if a condition is not met.
 *
 * Print a IGT_LOG_WARN level message if a condition is not met.
 *
 * Should be used everywhere where a test checks results to decide about
 * printing warnings. This is useful to streamline the test logic since it
 * allows for a more flat code control flow, similar to igt_assert()
 *
 * In addition to the plain igt_warn_on_f() helper this allows to print
 * additional information (again as warnings) to help debugging test failures.
941 942
 *
 * It also returns the value of @condition.
943
 */
944 945 946 947
#define igt_warn_on_f(condition, f...) ({ \
		typeof(condition) ret__ = (condition); \
		if (ret__) {\
			igt_warn("Warning on condition %s in function %s, file %s:%i\n", \
Daniel Vetter's avatar
Daniel Vetter committed
948 949 950
				 #condition, __func__, __FILE__, __LINE__); \
			igt_warn(f); \
		} \
951 952
		ret__; \
	})
Daniel Vetter's avatar
Daniel Vetter committed
953

954 955
void igt_set_timeout(unsigned int seconds,
		     const char *op);
Thomas Wood's avatar
Thomas Wood committed
956

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
/**
 * igt_gettime:
 * @ts: current monotonic clock reading
 *
 * Reports the current time in the monotonic clock.
 * Returns: 0 on success, -errno on failure.
 */
int igt_gettime(struct timespec *ts);

/**
 * igt_time_elapsed:
 * @then: Earlier timestamp
 * @now: Later timestamp
 *
 * Returns: Time between two timestamps in seconds, as a floating
 * point number.
 */
double igt_time_elapsed(struct timespec *then,
			struct timespec *now);

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
/**
 * igt_nsec_elapsed:
 * @start: measure from this point in time
 *
 * Reports the difference in the monotonic clock from the start time
 * in nanoseconds. On the first invocation, start should be zeroed and will
 * be set by the call.
 *
 * Typical use would be:
 *
 * igt_subtest("test") {
 * 	struct timespec start = {};
 * 	while (igt_nsec_elapsed(&start) < test_timeout_ns)
 *	 	do_test();
 * }
 *
 * A handy approximation is to use nsec >> 30 to convert to seconds,
 * nsec >> 20 to convert to milliseconds - the error is about 8%, acceptable
 * for test run times.
 */
uint64_t igt_nsec_elapsed(struct timespec *start);

/**
 * igt_seconds_elapsed:
 * @start: measure from this point in time
 *
 * A wrapper around igt_nsec_elapsed that reports the approximate (8% error)
 * number of seconds since the start point.
 */
static inline uint32_t igt_seconds_elapsed(struct timespec *start)
{
	return igt_nsec_elapsed(start) >> 30;
}

1011 1012
void igt_reset_timeout(void);

1013 1014 1015 1016 1017 1018
FILE *__igt_fopen_data(const char* igt_srcdir, const char* igt_datadir,
		       const char* filename);
/**
 * igt_fopen_data:
 * @filename: filename to open.
 *
1019 1020
 * Open a datafile for test, first try from installation directory,
 * then from build directory, and finally from current directory.
1021 1022 1023 1024
 */
#define igt_fopen_data(filename) \
	__igt_fopen_data(IGT_SRCDIR, IGT_DATADIR, filename)

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
int igt_system(const char *command);
int igt_system_quiet(const char *command);
#define igt_system_cmd(status, format...) \
	do { \
		char *buf = 0; \
		igt_assert(asprintf(&buf, format) != -1); \
	        status = igt_system(buf); \
		free(buf); \
	} while (0)

Chris Wilson's avatar
Chris Wilson committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
/**
 * igt_kmsg:
 * @format: printf-style format string with optional args
 *
 * Writes a message into the kernel log file (/dev/kmsg).
 */
__attribute__((format(printf, 1, 2)))
void igt_kmsg(const char *format, ...);
#define KMSG_EMER	"<0>[IGT] "
#define KMSG_ALERT	"<1>[IGT] "
#define KMSG_CRIT	"<2>[IGT] "
#define KMSG_ERR	"<3>[IGT] "
#define KMSG_WARNING	"<4>[IGT] "
#define KMSG_NOTICE	"<5>[IGT] "
#define KMSG_INFO	"<6>[IGT] "
#define KMSG_DEBUG	"<7>[IGT] "

1052 1053
#define READ_ONCE(x) (*(volatile typeof(x) *)(&(x)))

1054 1055 1056 1057
#define MSEC_PER_SEC (1000)
#define USEC_PER_SEC (1000*MSEC_PER_SEC)
#define NSEC_PER_SEC (1000*USEC_PER_SEC)

Daniel Vetter's avatar
Daniel Vetter committed
1058
#endif /* IGT_CORE_H */