summaryrefslogtreecommitdiff
path: root/harness/cases/18.t
blob: 5587ceb65fce739fa9695c6759c472659298f968 (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
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

/*
 * Author: Anatol Pomozov <anatol.pomozov@gmail.com>
 *
 * Description: This code tests to make sure that when io_destroy
 * returns, all outstanding I/Os have been completed.  It does this by
 * issuing one I/O and then calling io_destroy (without calling
 * io_getevents).  After the call to io_destroy, the buffer is checked
 * to ensure that the data was retrieved.  This is done simultaneously
 * from 100 threads.
 */

#define _GNU_SOURCE

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <libaio.h>
#include <stdbool.h>

#define FILENAME "tempfile"
#define FILEPATTERN '1'
#define DESTROY_PATTERN '2'

#define THREADS_NUM 100

void
aio_worker(void *ptr)
{
	int i, j, fd;
	char buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));

	fd = open(FILENAME, O_DIRECT|O_RDONLY);
	assert(fd >= 0);

	for (i = 0; i < 1000; i++) {
		io_context_t ctx;
		struct iocb cb;
		struct iocb *cbs[1];

		assert(!io_queue_init(1, &ctx));
		io_prep_pread(&cb, fd, buffer, PAGE_SIZE, 0);
		cbs[0] = &cb;

		memset(buffer, '0', PAGE_SIZE);
		assert(io_submit(ctx, 1, &cbs[0]) == 1);
		// wait random time (0-500ms) ?

		io_destroy(ctx);
		memset(buffer, DESTROY_PATTERN, PAGE_SIZE);
		// wait random for (0-500ms) ?

		// check it is still DESTROY_PATTERN
		for (j = 0; j < PAGE_SIZE; j++) {
			if (buffer[j] != DESTROY_PATTERN) {
				fprintf(stderr,
					"Buffer has unexpected character: %c\n",
					buffer[j]);
				exit(EXIT_FAILURE);
			}
		}
	}

	close(fd);
}

int
test_main(void)
{
	int i, fd, ret;
	char buffer[PAGE_SIZE];
	pthread_t threads[THREADS_NUM];

	fd = open(FILENAME, O_CREAT|O_TRUNC|O_APPEND|O_RDWR, S_IRUSR|S_IWUSR);
	assert(fd != -1);

	memset(buffer, FILEPATTERN, PAGE_SIZE);
	ret = write(fd, buffer, PAGE_SIZE);
	assert(ret == PAGE_SIZE);
	close(fd);

	for (i = 0; i < THREADS_NUM; i++) {
		ret = pthread_create(&threads[i], NULL,
				     (void *)&aio_worker, NULL);
		assert(ret == 0);
	}
	for (i = 0; i < THREADS_NUM; i++) {
		ret = pthread_join(threads[i], NULL);
		assert(ret == 0);
	}

	return EXIT_SUCCESS;
}
/*
 * Local Variables:
 *  mode: c
 *  c-basic-offset: 8
 * End:
 */