1 | /* $Id: tstFileAio.cpp 19054 2009-04-21 09:23:14Z 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 |
|
---|
66 | /* Initialize buffers. */
|
---|
67 | for (unsigned i = 0; i < RT_ELEMENTS(apvBuf); i++)
|
---|
68 | {
|
---|
69 | apvBuf[i] = RTMemPageAllocZ(cbTestBuf);
|
---|
70 |
|
---|
71 | if (fWrite)
|
---|
72 | memcpy(apvBuf[i], pvTestBuf, cbTestBuf);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Initialize requests. */
|
---|
76 | for (unsigned i = 0; i < RT_ELEMENTS(aReqs); i++)
|
---|
77 | RTFileAioReqCreate(&aReqs[i]);
|
---|
78 |
|
---|
79 | while (cbLeft)
|
---|
80 | {
|
---|
81 | int cReqs = 0;
|
---|
82 |
|
---|
83 | for (unsigned i = 0; i < RT_ELEMENTS(aReqs); i++)
|
---|
84 | {
|
---|
85 | size_t cbTransfer = (cbLeft < cbTestBuf) ? cbLeft : cbTestBuf;
|
---|
86 |
|
---|
87 | if (!cbTransfer)
|
---|
88 | break;
|
---|
89 |
|
---|
90 | if (fWrite)
|
---|
91 | rc = RTFileAioReqPrepareWrite(aReqs[i], File, Offset, apvBuf[i],
|
---|
92 | cbTransfer, apvBuf[i]);
|
---|
93 | else
|
---|
94 | rc = RTFileAioReqPrepareRead(aReqs[i], File, Offset, apvBuf[i],
|
---|
95 | cbTransfer, apvBuf[i]);
|
---|
96 |
|
---|
97 | cbLeft -= cbTransfer;
|
---|
98 | Offset += cbTransfer;
|
---|
99 | cReqs++;
|
---|
100 | }
|
---|
101 |
|
---|
102 | rc = RTFileAioCtxSubmit(hAioContext, aReqs, cReqs);
|
---|
103 | if (RT_FAILURE(rc))
|
---|
104 | {
|
---|
105 | RTPrintf("tstFileAio: FATAL ERROR - Failed to submit tasks. rc=%Rrc\n", rc);
|
---|
106 | cErrors++;
|
---|
107 | break;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Wait */
|
---|
111 | RTFILEAIOREQ aReqsCompleted[TSTFILEAIO_MAX_REQS_IN_FLIGHT];
|
---|
112 | uint32_t cCompleted = 0;
|
---|
113 | rc = RTFileAioCtxWait(hAioContext, cReqs, RT_INDEFINITE_WAIT,
|
---|
114 | aReqsCompleted, TSTFILEAIO_MAX_REQS_IN_FLIGHT,
|
---|
115 | &cCompleted);
|
---|
116 | if (RT_FAILURE(rc))
|
---|
117 | {
|
---|
118 | RTPrintf("tstFileAio: FATAL ERROR - Waiting failed. rc=%Rrc\n", rc);
|
---|
119 | cErrors++;
|
---|
120 | break;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (!fWrite)
|
---|
124 | {
|
---|
125 | for (uint32_t i = 0; i < cCompleted; i++)
|
---|
126 | {
|
---|
127 | /* Compare that we read the right stuff. */
|
---|
128 | void *pvBuf = RTFileAioReqGetUser(aReqsCompleted[i]);
|
---|
129 |
|
---|
130 | if (memcmp(pvBuf, pvTestBuf, cbTestBuf) != 0)
|
---|
131 | {
|
---|
132 | RTPrintf("tstFileAio: FATAL ERROR - Unexpected content in memory.\n");
|
---|
133 | cErrors++;
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | memset(pvBuf, 0, cbTestBuf);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* Free buffers. */
|
---|
142 | for (unsigned i = 0; i < RT_ELEMENTS(apvBuf); i++)
|
---|
143 | RTMemPageFree(apvBuf[i]);
|
---|
144 |
|
---|
145 | /* Free requests. */
|
---|
146 | for (unsigned i = 0; i < RT_ELEMENTS(aReqs); i++)
|
---|
147 | RTFileAioReqDestroy(aReqs[i]);
|
---|
148 |
|
---|
149 | NanoTS = RTTimeNanoTS() - NanoTS;
|
---|
150 | unsigned SpeedKBs = cbTestFile / (NanoTS / 1000000000.0) / 1024;
|
---|
151 |
|
---|
152 | RTPrintf("tstFileAio: Completed simple %s test: %d.%03d MB/sec\n",
|
---|
153 | fWrite ? "write" : "read",
|
---|
154 | SpeedKBs / 1000,
|
---|
155 | SpeedKBs % 1000);
|
---|
156 |
|
---|
157 | rc = RTFileAioCtxDestroy(hAioContext);
|
---|
158 | if (RT_FAILURE(rc))
|
---|
159 | {
|
---|
160 | RTPrintf("tstFileAio: FATAL ERROR - Failed to destroy async I/O context. rc=%Rrc\n", rc);
|
---|
161 | cErrors++;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | RTPrintf("tstFileAio: FATAL ERROR - Failed to create async I/O context. rc=%Rrc\n", rc);
|
---|
167 | cErrors++;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | int main()
|
---|
172 | {
|
---|
173 | RTPrintf("tstFileAio: TESTING\n");
|
---|
174 | RTR3Init();
|
---|
175 |
|
---|
176 | RTFILE File;
|
---|
177 | void *pvTestBuf = NULL;
|
---|
178 | int rc = RTFileOpen(&File, "tstFileAio#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO);
|
---|
179 | if (RT_FAILURE(rc))
|
---|
180 | {
|
---|
181 | RTPrintf("tstFileAio: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
|
---|
182 | return 1;
|
---|
183 | }
|
---|
184 |
|
---|
185 | pvTestBuf = RTMemAllocZ(64 * _1K);
|
---|
186 | for (unsigned i = 0; i < 64*_1K; i++)
|
---|
187 | ((char *)pvTestBuf)[i] = i % 256;
|
---|
188 |
|
---|
189 | /* Basic write test. */
|
---|
190 | RTPrintf("tstFileAio: Preparing test file, this can take some time and needs quite a bit of harddisk\n");
|
---|
191 | tstFileAioTestReadWriteBasic(File, true, pvTestBuf, 64*_1K, 100*_1M);
|
---|
192 | tstFileAioTestReadWriteBasic(File, false, pvTestBuf, 64*_1K, 100*_1M);
|
---|
193 |
|
---|
194 | RTFileClose(File);
|
---|
195 | RTMemFree(pvTestBuf);
|
---|
196 | RTFileDelete("tstFileAio#1.tst");
|
---|
197 | /*
|
---|
198 | * Summary
|
---|
199 | */
|
---|
200 | if (cErrors == 0)
|
---|
201 | RTPrintf("tstFileAio: SUCCESS\n");
|
---|
202 | else
|
---|
203 | RTPrintf("tstFileAio: FAILURE - %d errors\n", cErrors);
|
---|
204 | return !!cErrors;
|
---|
205 | }
|
---|
206 |
|
---|