xref: /third_party/ltp/lib/tst_checkpoint.c (revision f08c3bdf)
1/*
2 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#include <stdint.h>
25#include <limits.h>
26#include <errno.h>
27#include <sys/syscall.h>
28
29#include "test.h"
30#include "safe_macros.h"
31#include "lapi/futex.h"
32
33#define DEFAULT_MSEC_TIMEOUT 10000
34
35futex_t *tst_futexes;
36unsigned int tst_max_futexes;
37
38void tst_checkpoint_init(const char *file, const int lineno,
39                         void (*cleanup_fn)(void))
40{
41	int fd;
42	unsigned int page_size;
43
44	if (tst_futexes) {
45		tst_brkm_(file, lineno, TBROK, cleanup_fn,
46			"checkpoints already initialized");
47		return;
48	}
49
50	/*
51	 * The parent test process is responsible for creating the temporary
52	 * directory and therefore must pass non-zero cleanup (to remove the
53	 * directory if something went wrong).
54	 *
55	 * We cannot do this check unconditionally because if we need to init
56	 * the checkpoint from a binary that was started by exec() the
57	 * tst_tmpdir_created() will return false because the tmpdir was
58	 * created by parent. In this case we expect the subprogram can call
59	 * the init as a first function with NULL as cleanup function.
60	 */
61	if (cleanup_fn && !tst_tmpdir_created()) {
62		tst_brkm_(file, lineno, TBROK, cleanup_fn,
63			"You have to create test temporary directory "
64			"first (call tst_tmpdir())");
65		return;
66	}
67
68	page_size = getpagesize();
69
70	fd = SAFE_OPEN(cleanup_fn, "checkpoint_futex_base_file",
71	               O_RDWR | O_CREAT, 0666);
72
73	SAFE_FTRUNCATE(cleanup_fn, fd, page_size);
74
75	tst_futexes = SAFE_MMAP(cleanup_fn, NULL, page_size,
76	                    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
77
78	tst_max_futexes = page_size / sizeof(uint32_t);
79
80	SAFE_CLOSE(cleanup_fn, fd);
81}
82
83int tst_checkpoint_wait(unsigned int id, unsigned int msec_timeout)
84{
85	struct timespec timeout;
86	int ret;
87
88	if (!tst_max_futexes)
89		tst_brkm(TBROK, NULL, "Set test.needs_checkpoints = 1");
90
91	if (id >= tst_max_futexes) {
92		errno = EOVERFLOW;
93		return -1;
94	}
95
96	timeout.tv_sec = msec_timeout/1000;
97	timeout.tv_nsec = (msec_timeout%1000) * 1000000;
98
99	do {
100		ret = syscall(SYS_futex, &tst_futexes[id], FUTEX_WAIT,
101			      tst_futexes[id], &timeout);
102	} while (ret == -1 && errno == EINTR);
103
104	return ret;
105}
106
107int tst_checkpoint_wake(unsigned int id, unsigned int nr_wake,
108                        unsigned int msec_timeout)
109{
110	unsigned int msecs = 0, waked = 0;
111
112	if (!tst_max_futexes)
113		tst_brkm(TBROK, NULL, "Set test.needs_checkpoints = 1");
114
115	if (id >= tst_max_futexes) {
116		errno = EOVERFLOW;
117		return -1;
118	}
119
120	for (;;) {
121		waked += syscall(SYS_futex, &tst_futexes[id], FUTEX_WAKE,
122				 INT_MAX, NULL);
123
124		if (waked == nr_wake)
125			break;
126
127		usleep(1000);
128		msecs++;
129
130		if (msecs >= msec_timeout) {
131			errno = ETIMEDOUT;
132			return -1;
133		}
134	}
135
136	return 0;
137}
138
139void tst_safe_checkpoint_wait(const char *file, const int lineno,
140                              void (*cleanup_fn)(void), unsigned int id,
141			      unsigned int msec_timeout)
142{
143	int ret;
144
145	if (!msec_timeout)
146		msec_timeout = DEFAULT_MSEC_TIMEOUT;
147
148	ret = tst_checkpoint_wait(id, msec_timeout);
149
150	if (ret) {
151		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
152			"tst_checkpoint_wait(%u, %i) failed", id,
153			msec_timeout);
154	}
155}
156
157void tst_safe_checkpoint_wake(const char *file, const int lineno,
158                              void (*cleanup_fn)(void), unsigned int id,
159                              unsigned int nr_wake)
160{
161	int ret = tst_checkpoint_wake(id, nr_wake, DEFAULT_MSEC_TIMEOUT);
162
163	if (ret) {
164		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
165			"tst_checkpoint_wake(%u, %u, %i) failed", id, nr_wake,
166			DEFAULT_MSEC_TIMEOUT);
167	}
168}
169