1 | /* $Id: tstFileAio.cpp 19185 2009-04-26 09:04:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - File Async I/O.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/types.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/mem.h>
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 |
|
---|
43 | /** @todo make configurable through cmd line. */
|
---|
44 | #define TSTFILEAIO_MAX_REQS_IN_FLIGHT 64
|
---|
45 | #define TSTFILEAIO_BUFFER_SIZE 64*_1K
|
---|
46 |
|
---|
47 | /* Global error counter. */
|
---|
48 | int cErrors = 0;
|
---|
49 |
|
---|
50 | void tstFileAioTestReadWriteBasic(RTFILE File, bool fWrite, void *pvTestBuf, size_t cbTestBuf, size_t cbTestFile)
|
---|
51 | {
|
---|
52 | int rc = VINF_SUCCESS;
|
---|
53 | RTFILEAIOCTX hAioContext;
|
---|
54 | uint64_t NanoTS = RTTimeNanoTS();
|
---|
55 |
|
---|
56 | RTPrintf("tstFileAio: Starting simple %s test...\n", fWrite ? "write" : "read");
|
---|
57 |
|
---|
58 | rc = RTFileAioCtxCreate(&hAioContext, TSTFILEAIO_MAX_REQS_IN_FLIGHT);
|
---|
59 | if (RT_SUCCESS(rc))
|
---|
60 | {
|
---|
61 | RTFILEAIOREQ aReqs[TSTFILEAIO_MAX_REQS_IN_FLIGHT];
|
---|
62 | void *apvBuf[TSTFILEAIO_MAX_REQS_IN_FLIGHT];
|
---|
63 | RTFOFF Offset = 0;
|
---|
64 | size_t cbLeft = cbTestFile;
|
---|
65 | int cRun = 0;
|
---|
66 |
|
---|
67 | /* Associate file with context.*/
|
---|
68 | rc = RTFileAioCtxAssociateWithFile(hAioContext, File);
|
---|
69 | if (RT_SUCCESS(rc))
|
---|
70 | {
|
---|
71 | /* Initialize buffers. */
|
---|
72 | for (unsigned i = 0; i < RT_ELEMENTS(apvBuf); i++)
|
---|
73 | {
|
---|
74 | apvBuf[i] = RTMemPageAllocZ(cbTestBuf);
|
---|
75 |
|
---|
76 | if (fWrite)
|
---|
77 | memcpy(apvBuf[i], pvTestBuf, cbTestBuf);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* Initialize requests. */
|
---|
81 | for (unsigned i = 0; i < RT_ELEMENTS(aReqs); i++)
|
---|
82 | RTFileAioReqCreate(&aReqs[i]);
|
---|
83 |
|
---|
84 | while (cbLeft)
|
---|
85 | {
|
---|
86 | int cReqs = 0;
|
---|
87 | size_t cReqsSubmitted = 0;
|
---|
88 |
|
---|
89 | for (unsigned i = 0; i < RT_ELEMENTS(aReqs); i++)
|
---|
90 | {
|
---|
91 | size_t cbTransfer = (cbLeft < cbTestBuf) ? cbLeft : cbTestBuf;
|
---|
92 |
|
---|
93 | if (!cbTransfer)
|
---|
94 | break;
|
---|
95 |
|
---|
96 | if (fWrite)
|
---|
97 | rc = RTFileAioReqPrepareWrite(aReqs[i], File, Offset, apvBuf[i],
|
---|
98 | cbTransfer, apvBuf[i]);
|
---|
99 | else
|
---|
100 | rc = RTFileAioReqPrepareRead(aReqs[i], File, Offset, apvBuf[i],
|
---|
101 | cbTransfer, apvBuf[i]);
|
---|
102 |
|
---|
103 | cbLeft -= cbTransfer;
|
---|
104 | Offset += cbTransfer;
|
---|
105 | cReqs++;
|
---|
106 | }
|
---|
107 |
|
---|
108 | rc = RTFileAioCtxSubmit(hAioContext, aReqs, cReqs, &cReqsSubmitted);
|
---|
109 | if (RT_FAILURE(rc))
|
---|
110 | {
|
---|
111 | RTPrintf("tstFileAio: FATAL ERROR - Failed to submit tasks after %d runs. rc=%Rrc\n", cRun, rc);
|
---|
112 | cErrors++;
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | else if (cReqs != cReqsSubmitted)
|
---|
116 | {
|
---|
117 | RTPrintf("tstFileAio: FATAL ERROR - Submitted tasks but the result is not equal to the number of submitted tasks\n", rc);
|
---|
118 | cErrors++;
|
---|
119 | break;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* Wait */
|
---|
123 | RTFILEAIOREQ aReqsCompleted[TSTFILEAIO_MAX_REQS_IN_FLIGHT];
|
---|
124 | uint32_t cCompleted = 0;
|
---|
125 | rc = RTFileAioCtxWait(hAioContext, cReqs, RT_INDEFINITE_WAIT,
|
---|
126 | aReqsCompleted, TSTFILEAIO_MAX_REQS_IN_FLIGHT,
|
---|
127 | &cCompleted);
|
---|
128 | if (RT_FAILURE(rc))
|
---|
129 | {
|
---|
130 | RTPrintf("tstFileAio: FATAL ERROR - Waiting failed. rc=%Rrc\n", rc);
|
---|
131 | cErrors++;
|
---|
132 | break;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!fWrite)
|
---|
136 | {
|
---|
137 | for (uint32_t i = 0; i < cCompleted; i++)
|
---|
138 | {
|
---|
139 | /* Compare that we read the right stuff. */
|
---|
140 | void *pvBuf = RTFileAioReqGetUser(aReqsCompleted[i]);
|
---|
141 |
|
---|
142 | size_t cbTransfered;
|
---|
143 | int rcReq = RTFileAioReqGetRC(aReqsCompleted[i], &cbTransfered);
|
---|
144 | if (RT_FAILURE(rcReq) || (cbTransfered != cbTestBuf))
|
---|
145 | {
|
---|
146 | RTPrintf("tstFileAio: FATAL ERROR - Request %d failed with rc=%Rrc cbTransfered=%d.\n",
|
---|
147 | i, rcReq, cbTransfered);
|
---|
148 | cErrors++;
|
---|
149 | rc = rcReq;
|
---|
150 | break;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (memcmp(pvBuf, pvTestBuf, cbTestBuf) != 0)
|
---|
154 | {
|
---|
155 | RTPrintf("tstFileAio: FATAL ERROR - Unexpected content in memory.\n");
|
---|
156 | cErrors++;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | memset(pvBuf, 0, cbTestBuf);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | cRun++;
|
---|
163 | if (RT_FAILURE(rc))
|
---|
164 | break;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* Free buffers. */
|
---|
168 | for (unsigned i = 0; i < RT_ELEMENTS(apvBuf); i++)
|
---|
169 | RTMemPageFree(apvBuf[i]);
|
---|
170 |
|
---|
171 | /* Free requests. */
|
---|
172 | for (unsigned i = 0; i < RT_ELEMENTS(aReqs); i++)
|
---|
173 | RTFileAioReqDestroy(aReqs[i]);
|
---|
174 |
|
---|
175 | NanoTS = RTTimeNanoTS() - NanoTS;
|
---|
176 | unsigned SpeedKBs = cbTestFile / (NanoTS / 1000000000.0) / 1024;
|
---|
177 |
|
---|
178 | RTPrintf("tstFileAio: Completed simple %s test: %d.%03d MB/sec\n",
|
---|
179 | fWrite ? "write" : "read",
|
---|
180 | SpeedKBs / 1000,
|
---|
181 | SpeedKBs % 1000);
|
---|
182 | }
|
---|
183 | else
|
---|
184 | {
|
---|
185 | RTPrintf("tstFileAio: FATAL ERROR - Failed to asssociate file with async I/O context. rc=%Rrc\n", rc);
|
---|
186 | cErrors++;
|
---|
187 | }
|
---|
188 |
|
---|
189 | rc = RTFileAioCtxDestroy(hAioContext);
|
---|
190 | if (RT_FAILURE(rc))
|
---|
191 | {
|
---|
192 | RTPrintf("tstFileAio: FATAL ERROR - Failed to destroy async I/O context. rc=%Rrc\n", rc);
|
---|
193 | cErrors++;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | RTPrintf("tstFileAio: FATAL ERROR - Failed to create async I/O context. rc=%Rrc\n", rc);
|
---|
199 | cErrors++;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | int main()
|
---|
204 | {
|
---|
205 | RTPrintf("tstFileAio: TESTING\n");
|
---|
206 | RTR3Init();
|
---|
207 |
|
---|
208 | RTFILE File;
|
---|
209 | void *pvTestBuf = NULL;
|
---|
210 | int rc = RTFileOpen(&File, "tstFileAio#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO);
|
---|
211 | if (RT_FAILURE(rc))
|
---|
212 | {
|
---|
213 | RTPrintf("tstFileAio: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
|
---|
214 | return 1;
|
---|
215 | }
|
---|
216 |
|
---|
217 | pvTestBuf = RTMemAllocZ(64 * _1K);
|
---|
218 | for (unsigned i = 0; i < 64*_1K; i++)
|
---|
219 | ((char *)pvTestBuf)[i] = i % 256;
|
---|
220 |
|
---|
221 | /* Basic write test. */
|
---|
222 | RTPrintf("tstFileAio: Preparing test file, this can take some time and needs quite a bit of harddisk\n");
|
---|
223 | tstFileAioTestReadWriteBasic(File, true, pvTestBuf, 64*_1K, 100*_1M);
|
---|
224 | /* Reopen the file. */
|
---|
225 | RTFileClose(File);
|
---|
226 | rc = RTFileOpen(&File, "tstFileAio#1.tst", RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO);
|
---|
227 | if (RT_SUCCESS(rc))
|
---|
228 | {
|
---|
229 | tstFileAioTestReadWriteBasic(File, false, pvTestBuf, 64*_1K, 100*_1M);
|
---|
230 | RTFileClose(File);
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | RTPrintf("tstFileAio: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
|
---|
235 | cErrors++;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* Cleanup */
|
---|
239 | RTMemFree(pvTestBuf);
|
---|
240 | RTFileDelete("tstFileAio#1.tst");
|
---|
241 | /*
|
---|
242 | * Summary
|
---|
243 | */
|
---|
244 | if (cErrors == 0)
|
---|
245 | RTPrintf("tstFileAio: SUCCESS\n");
|
---|
246 | else
|
---|
247 | RTPrintf("tstFileAio: FAILURE - %d errors\n", cErrors);
|
---|
248 | return !!cErrors;
|
---|
249 | }
|
---|
250 |
|
---|