VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/fs/FsPerf.cpp@ 101645

最後變更 在這個檔案從101645是 101645,由 vboxsync 提交於 17 月 前

ValidationKit/utils/FsPerf: Allow building on linux.arm64, bugref:10541

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 288.2 KB
 
1/* $Id: FsPerf.cpp 101645 2023-10-30 09:28:02Z vboxsync $ */
2/** @file
3 * FsPerf - File System (Shared Folders) Performance Benchmark.
4 */
5
6/*
7 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#ifdef RT_OS_OS2
42# define INCL_BASE
43# include <os2.h>
44# undef RT_MAX
45#endif
46#include <iprt/alloca.h>
47#include <iprt/asm.h>
48#include <iprt/assert.h>
49#include <iprt/err.h>
50#include <iprt/dir.h>
51#include <iprt/file.h>
52#include <iprt/getopt.h>
53#include <iprt/initterm.h>
54#include <iprt/list.h>
55#include <iprt/mem.h>
56#include <iprt/message.h>
57#include <iprt/param.h>
58#include <iprt/path.h>
59#ifdef RT_OS_LINUX
60# include <iprt/pipe.h>
61#endif
62#include <iprt/process.h>
63#include <iprt/rand.h>
64#include <iprt/string.h>
65#include <iprt/stream.h>
66#include <iprt/system.h>
67#include <iprt/tcp.h>
68#include <iprt/test.h>
69#include <iprt/time.h>
70#include <iprt/thread.h>
71#include <iprt/zero.h>
72
73#ifdef RT_OS_WINDOWS
74# include <iprt/nt/nt-and-windows.h>
75#else
76# include <errno.h>
77# include <unistd.h>
78# include <limits.h>
79# include <sys/types.h>
80# include <sys/fcntl.h>
81# ifndef RT_OS_OS2
82# include <sys/mman.h>
83# include <sys/uio.h>
84# endif
85# include <sys/socket.h>
86# include <signal.h>
87# ifdef RT_OS_LINUX
88# include <sys/sendfile.h>
89# include <sys/syscall.h>
90# endif
91# ifdef RT_OS_DARWIN
92# include <sys/uio.h>
93# endif
94#endif
95
96
97/*********************************************************************************************************************************
98* Defined Constants And Macros *
99*********************************************************************************************************************************/
100/** Used for cutting the the -d parameter value short and avoid a number of buffer overflow checks. */
101#define FSPERF_MAX_NEEDED_PATH 224
102/** The max path used by this code.
103 * It greatly exceeds the RTPATH_MAX so we can push the limits on windows. */
104#define FSPERF_MAX_PATH (_32K)
105
106/** EOF marker character used by the master/slave comms. */
107#define FSPERF_EOF 0x1a
108/** EOF marker character used by the master/slave comms, string version. */
109#define FSPERF_EOF_STR "\x1a"
110
111/** @def FSPERF_TEST_SENDFILE
112 * Whether to enable the sendfile() tests. */
113#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
114# define FSPERF_TEST_SENDFILE
115#endif
116
117/**
118 * Macro for profiling @a a_fnCall (typically forced inline) for about @a a_cNsTarget ns.
119 *
120 * Always does an even number of iterations.
121 */
122#define PROFILE_FN(a_fnCall, a_cNsTarget, a_szDesc) \
123 do { \
124 /* Estimate how many iterations we need to fill up the given timeslot: */ \
125 fsPerfYield(); \
126 uint64_t nsStart = RTTimeNanoTS(); \
127 uint64_t nsPrf; \
128 do \
129 nsPrf = RTTimeNanoTS(); \
130 while (nsPrf == nsStart); \
131 nsStart = nsPrf; \
132 \
133 uint64_t iIteration = 0; \
134 do \
135 { \
136 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
137 iIteration++; \
138 nsPrf = RTTimeNanoTS() - nsStart; \
139 } while (nsPrf < RT_NS_10MS || (iIteration & 1)); \
140 nsPrf /= iIteration; \
141 if (nsPrf > g_nsPerNanoTSCall + 32) \
142 nsPrf -= g_nsPerNanoTSCall; \
143 \
144 uint64_t cIterations = (a_cNsTarget) / nsPrf; \
145 if (cIterations <= 1) \
146 cIterations = 2; \
147 else if (cIterations & 1) \
148 cIterations++; \
149 \
150 /* Do the actual profiling: */ \
151 fsPerfYield(); \
152 iIteration = 0; \
153 nsStart = RTTimeNanoTS(); \
154 for (; iIteration < cIterations; iIteration++) \
155 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
156 nsPrf = RTTimeNanoTS() - nsStart; \
157 RTTestIValue(a_szDesc, nsPrf / cIterations, RTTESTUNIT_NS_PER_OCCURRENCE); \
158 if (g_fShowDuration) \
159 RTTestIValueF(nsPrf, RTTESTUNIT_NS, "%s duration", a_szDesc); \
160 if (g_fShowIterations) \
161 RTTestIValueF(iIteration, RTTESTUNIT_OCCURRENCES, "%s iterations", a_szDesc); \
162 } while (0)
163
164
165/**
166 * Macro for profiling an operation on each file in the manytree directory tree.
167 *
168 * Always does an even number of tree iterations.
169 */
170#define PROFILE_MANYTREE_FN(a_szPath, a_fnCall, a_cEstimationIterations, a_cNsTarget, a_szDesc) \
171 do { \
172 if (!g_fManyFiles) \
173 break; \
174 \
175 /* Estimate how many iterations we need to fill up the given timeslot: */ \
176 fsPerfYield(); \
177 uint64_t nsStart = RTTimeNanoTS(); \
178 uint64_t ns; \
179 do \
180 ns = RTTimeNanoTS(); \
181 while (ns == nsStart); \
182 nsStart = ns; \
183 \
184 PFSPERFNAMEENTRY pCur; \
185 uint64_t iIteration = 0; \
186 do \
187 { \
188 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
189 { \
190 memcpy(a_szPath, pCur->szName, pCur->cchName); \
191 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
192 { \
193 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
194 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
195 } \
196 } \
197 iIteration++; \
198 ns = RTTimeNanoTS() - nsStart; \
199 } while (ns < RT_NS_10MS || (iIteration & 1)); \
200 ns /= iIteration; \
201 if (ns > g_nsPerNanoTSCall + 32) \
202 ns -= g_nsPerNanoTSCall; \
203 \
204 uint32_t cIterations = (a_cNsTarget) / ns; \
205 if (cIterations <= 1) \
206 cIterations = 2; \
207 else if (cIterations & 1) \
208 cIterations++; \
209 \
210 /* Do the actual profiling: */ \
211 fsPerfYield(); \
212 uint32_t cCalls = 0; \
213 nsStart = RTTimeNanoTS(); \
214 for (iIteration = 0; iIteration < cIterations; iIteration++) \
215 { \
216 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
217 { \
218 memcpy(a_szPath, pCur->szName, pCur->cchName); \
219 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
220 { \
221 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
222 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
223 cCalls++; \
224 } \
225 } \
226 } \
227 ns = RTTimeNanoTS() - nsStart; \
228 RTTestIValueF(ns / cCalls, RTTESTUNIT_NS_PER_OCCURRENCE, a_szDesc); \
229 if (g_fShowDuration) \
230 RTTestIValueF(ns, RTTESTUNIT_NS, "%s duration", a_szDesc); \
231 if (g_fShowIterations) \
232 RTTestIValueF(iIteration, RTTESTUNIT_OCCURRENCES, "%s iterations", a_szDesc); \
233 } while (0)
234
235
236/**
237 * Execute a_fnCall for each file in the manytree.
238 */
239#define DO_MANYTREE_FN(a_szPath, a_fnCall) \
240 do { \
241 PFSPERFNAMEENTRY pCur; \
242 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
243 { \
244 memcpy(a_szPath, pCur->szName, pCur->cchName); \
245 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
246 { \
247 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
248 a_fnCall; \
249 } \
250 } \
251 } while (0)
252
253
254/** @def FSPERF_VERR_PATH_NOT_FOUND
255 * Hides the fact that we only get VERR_PATH_NOT_FOUND on non-unix systems. */
256#if defined(RT_OS_WINDOWS) //|| defined(RT_OS_OS2) - using posix APIs IIRC, so lost in translation.
257# define FSPERF_VERR_PATH_NOT_FOUND VERR_PATH_NOT_FOUND
258#else
259# define FSPERF_VERR_PATH_NOT_FOUND VERR_FILE_NOT_FOUND
260#endif
261
262#ifdef RT_OS_WINDOWS
263/** @def CHECK_WINAPI
264 * Checks a windows API call, reporting the last error on failure. */
265# define CHECK_WINAPI_CALL(a_CallAndTestExpr) \
266 if (!(a_CallAndTestExpr)) { \
267 RTTestIFailed("line %u: %s failed - last error %u, last status %#x", \
268 __LINE__, #a_CallAndTestExpr, GetLastError(), RTNtLastStatusValue()); \
269 } else do {} while (0)
270#endif
271
272
273/*********************************************************************************************************************************
274* Structures and Typedefs *
275*********************************************************************************************************************************/
276typedef struct FSPERFNAMEENTRY
277{
278 RTLISTNODE Entry;
279 uint16_t cchName;
280 RT_FLEXIBLE_ARRAY_EXTENSION
281 char szName[RT_FLEXIBLE_ARRAY];
282} FSPERFNAMEENTRY;
283typedef FSPERFNAMEENTRY *PFSPERFNAMEENTRY;
284
285
286enum
287{
288 kCmdOpt_First = 128,
289
290 kCmdOpt_ManyFiles = kCmdOpt_First,
291 kCmdOpt_NoManyFiles,
292 kCmdOpt_Open,
293 kCmdOpt_NoOpen,
294 kCmdOpt_FStat,
295 kCmdOpt_NoFStat,
296#ifdef RT_OS_WINDOWS
297 kCmdOpt_NtQueryInfoFile,
298 kCmdOpt_NoNtQueryInfoFile,
299 kCmdOpt_NtQueryVolInfoFile,
300 kCmdOpt_NoNtQueryVolInfoFile,
301#endif
302 kCmdOpt_FChMod,
303 kCmdOpt_NoFChMod,
304 kCmdOpt_FUtimes,
305 kCmdOpt_NoFUtimes,
306 kCmdOpt_Stat,
307 kCmdOpt_NoStat,
308 kCmdOpt_ChMod,
309 kCmdOpt_NoChMod,
310 kCmdOpt_Utimes,
311 kCmdOpt_NoUtimes,
312 kCmdOpt_Rename,
313 kCmdOpt_NoRename,
314 kCmdOpt_DirOpen,
315 kCmdOpt_NoDirOpen,
316 kCmdOpt_DirEnum,
317 kCmdOpt_NoDirEnum,
318 kCmdOpt_MkRmDir,
319 kCmdOpt_NoMkRmDir,
320 kCmdOpt_StatVfs,
321 kCmdOpt_NoStatVfs,
322 kCmdOpt_Rm,
323 kCmdOpt_NoRm,
324 kCmdOpt_ChSize,
325 kCmdOpt_NoChSize,
326 kCmdOpt_ReadPerf,
327 kCmdOpt_NoReadPerf,
328 kCmdOpt_ReadTests,
329 kCmdOpt_NoReadTests,
330#ifdef FSPERF_TEST_SENDFILE
331 kCmdOpt_SendFile,
332 kCmdOpt_NoSendFile,
333#endif
334#ifdef RT_OS_LINUX
335 kCmdOpt_Splice,
336 kCmdOpt_NoSplice,
337#endif
338 kCmdOpt_WritePerf,
339 kCmdOpt_NoWritePerf,
340 kCmdOpt_WriteTests,
341 kCmdOpt_NoWriteTests,
342 kCmdOpt_Seek,
343 kCmdOpt_NoSeek,
344 kCmdOpt_FSync,
345 kCmdOpt_NoFSync,
346 kCmdOpt_MMap,
347 kCmdOpt_NoMMap,
348 kCmdOpt_MMapCoherency,
349 kCmdOpt_NoMMapCoherency,
350 kCmdOpt_MMapPlacement,
351 kCmdOpt_IgnoreNoCache,
352 kCmdOpt_NoIgnoreNoCache,
353 kCmdOpt_IoFileSize,
354 kCmdOpt_SetBlockSize,
355 kCmdOpt_AddBlockSize,
356 kCmdOpt_Copy,
357 kCmdOpt_NoCopy,
358 kCmdOpt_Remote,
359 kCmdOpt_NoRemote,
360
361 kCmdOpt_ShowDuration,
362 kCmdOpt_NoShowDuration,
363 kCmdOpt_ShowIterations,
364 kCmdOpt_NoShowIterations,
365
366 kCmdOpt_ManyTreeFilesPerDir,
367 kCmdOpt_ManyTreeSubdirsPerDir,
368 kCmdOpt_ManyTreeDepth,
369
370 kCmdOpt_MaxBufferSize,
371
372 kCmdOpt_End
373};
374
375
376/*********************************************************************************************************************************
377* Global Variables *
378*********************************************************************************************************************************/
379/** Command line parameters */
380static const RTGETOPTDEF g_aCmdOptions[] =
381{
382 { "--dir", 'd', RTGETOPT_REQ_STRING },
383 { "--relative-dir", 'r', RTGETOPT_REQ_NOTHING },
384 { "--comms-dir", 'c', RTGETOPT_REQ_STRING },
385 { "--comms-slave", 'C', RTGETOPT_REQ_NOTHING },
386 { "--seconds", 's', RTGETOPT_REQ_UINT32 },
387 { "--milliseconds", 'm', RTGETOPT_REQ_UINT64 },
388
389 { "--enable-all", 'e', RTGETOPT_REQ_NOTHING },
390 { "--disable-all", 'z', RTGETOPT_REQ_NOTHING },
391
392 { "--many-files", kCmdOpt_ManyFiles, RTGETOPT_REQ_UINT32 },
393 { "--no-many-files", kCmdOpt_NoManyFiles, RTGETOPT_REQ_NOTHING },
394 { "--files-per-dir", kCmdOpt_ManyTreeFilesPerDir, RTGETOPT_REQ_UINT32 },
395 { "--subdirs-per-dir", kCmdOpt_ManyTreeSubdirsPerDir, RTGETOPT_REQ_UINT32 },
396 { "--tree-depth", kCmdOpt_ManyTreeDepth, RTGETOPT_REQ_UINT32 },
397 { "--max-buffer-size", kCmdOpt_MaxBufferSize, RTGETOPT_REQ_UINT32 },
398 { "--mmap-placement", kCmdOpt_MMapPlacement, RTGETOPT_REQ_STRING },
399 /// @todo { "--timestamp-style", kCmdOpt_TimestampStyle, RTGETOPT_REQ_STRING },
400
401 { "--open", kCmdOpt_Open, RTGETOPT_REQ_NOTHING },
402 { "--no-open", kCmdOpt_NoOpen, RTGETOPT_REQ_NOTHING },
403 { "--fstat", kCmdOpt_FStat, RTGETOPT_REQ_NOTHING },
404 { "--no-fstat", kCmdOpt_NoFStat, RTGETOPT_REQ_NOTHING },
405#ifdef RT_OS_WINDOWS
406 { "--nt-query-info-file", kCmdOpt_NtQueryInfoFile, RTGETOPT_REQ_NOTHING },
407 { "--no-nt-query-info-file", kCmdOpt_NoNtQueryInfoFile, RTGETOPT_REQ_NOTHING },
408 { "--nt-query-vol-info-file", kCmdOpt_NtQueryVolInfoFile, RTGETOPT_REQ_NOTHING },
409 { "--no-nt-query-vol-info-file",kCmdOpt_NoNtQueryVolInfoFile, RTGETOPT_REQ_NOTHING },
410#endif
411 { "--fchmod", kCmdOpt_FChMod, RTGETOPT_REQ_NOTHING },
412 { "--no-fchmod", kCmdOpt_NoFChMod, RTGETOPT_REQ_NOTHING },
413 { "--futimes", kCmdOpt_FUtimes, RTGETOPT_REQ_NOTHING },
414 { "--no-futimes", kCmdOpt_NoFUtimes, RTGETOPT_REQ_NOTHING },
415 { "--stat", kCmdOpt_Stat, RTGETOPT_REQ_NOTHING },
416 { "--no-stat", kCmdOpt_NoStat, RTGETOPT_REQ_NOTHING },
417 { "--chmod", kCmdOpt_ChMod, RTGETOPT_REQ_NOTHING },
418 { "--no-chmod", kCmdOpt_NoChMod, RTGETOPT_REQ_NOTHING },
419 { "--utimes", kCmdOpt_Utimes, RTGETOPT_REQ_NOTHING },
420 { "--no-utimes", kCmdOpt_NoUtimes, RTGETOPT_REQ_NOTHING },
421 { "--rename", kCmdOpt_Rename, RTGETOPT_REQ_NOTHING },
422 { "--no-rename", kCmdOpt_NoRename, RTGETOPT_REQ_NOTHING },
423 { "--dir-open", kCmdOpt_DirOpen, RTGETOPT_REQ_NOTHING },
424 { "--no-dir-open", kCmdOpt_NoDirOpen, RTGETOPT_REQ_NOTHING },
425 { "--dir-enum", kCmdOpt_DirEnum, RTGETOPT_REQ_NOTHING },
426 { "--no-dir-enum", kCmdOpt_NoDirEnum, RTGETOPT_REQ_NOTHING },
427 { "--mk-rm-dir", kCmdOpt_MkRmDir, RTGETOPT_REQ_NOTHING },
428 { "--no-mk-rm-dir", kCmdOpt_NoMkRmDir, RTGETOPT_REQ_NOTHING },
429 { "--stat-vfs", kCmdOpt_StatVfs, RTGETOPT_REQ_NOTHING },
430 { "--no-stat-vfs", kCmdOpt_NoStatVfs, RTGETOPT_REQ_NOTHING },
431 { "--rm", kCmdOpt_Rm, RTGETOPT_REQ_NOTHING },
432 { "--no-rm", kCmdOpt_NoRm, RTGETOPT_REQ_NOTHING },
433 { "--chsize", kCmdOpt_ChSize, RTGETOPT_REQ_NOTHING },
434 { "--no-chsize", kCmdOpt_NoChSize, RTGETOPT_REQ_NOTHING },
435 { "--read-tests", kCmdOpt_ReadTests, RTGETOPT_REQ_NOTHING },
436 { "--no-read-tests", kCmdOpt_NoReadTests, RTGETOPT_REQ_NOTHING },
437 { "--read-perf", kCmdOpt_ReadPerf, RTGETOPT_REQ_NOTHING },
438 { "--no-read-perf", kCmdOpt_NoReadPerf, RTGETOPT_REQ_NOTHING },
439#ifdef FSPERF_TEST_SENDFILE
440 { "--sendfile", kCmdOpt_SendFile, RTGETOPT_REQ_NOTHING },
441 { "--no-sendfile", kCmdOpt_NoSendFile, RTGETOPT_REQ_NOTHING },
442#endif
443#ifdef RT_OS_LINUX
444 { "--splice", kCmdOpt_Splice, RTGETOPT_REQ_NOTHING },
445 { "--no-splice", kCmdOpt_NoSplice, RTGETOPT_REQ_NOTHING },
446#endif
447 { "--write-tests", kCmdOpt_WriteTests, RTGETOPT_REQ_NOTHING },
448 { "--no-write-tests", kCmdOpt_NoWriteTests, RTGETOPT_REQ_NOTHING },
449 { "--write-perf", kCmdOpt_WritePerf, RTGETOPT_REQ_NOTHING },
450 { "--no-write-perf", kCmdOpt_NoWritePerf, RTGETOPT_REQ_NOTHING },
451 { "--seek", kCmdOpt_Seek, RTGETOPT_REQ_NOTHING },
452 { "--no-seek", kCmdOpt_NoSeek, RTGETOPT_REQ_NOTHING },
453 { "--fsync", kCmdOpt_FSync, RTGETOPT_REQ_NOTHING },
454 { "--no-fsync", kCmdOpt_NoFSync, RTGETOPT_REQ_NOTHING },
455 { "--mmap", kCmdOpt_MMap, RTGETOPT_REQ_NOTHING },
456 { "--no-mmap", kCmdOpt_NoMMap, RTGETOPT_REQ_NOTHING },
457 { "--mmap-coherency", kCmdOpt_MMapCoherency, RTGETOPT_REQ_NOTHING },
458 { "--no-mmap-coherency", kCmdOpt_NoMMapCoherency, RTGETOPT_REQ_NOTHING },
459 { "--ignore-no-cache", kCmdOpt_IgnoreNoCache, RTGETOPT_REQ_NOTHING },
460 { "--no-ignore-no-cache", kCmdOpt_NoIgnoreNoCache, RTGETOPT_REQ_NOTHING },
461 { "--io-file-size", kCmdOpt_IoFileSize, RTGETOPT_REQ_UINT64 },
462 { "--set-block-size", kCmdOpt_SetBlockSize, RTGETOPT_REQ_UINT32 },
463 { "--add-block-size", kCmdOpt_AddBlockSize, RTGETOPT_REQ_UINT32 },
464 { "--copy", kCmdOpt_Copy, RTGETOPT_REQ_NOTHING },
465 { "--no-copy", kCmdOpt_NoCopy, RTGETOPT_REQ_NOTHING },
466 { "--remote", kCmdOpt_Remote, RTGETOPT_REQ_NOTHING },
467 { "--no-remote", kCmdOpt_NoRemote, RTGETOPT_REQ_NOTHING },
468
469 { "--show-duration", kCmdOpt_ShowDuration, RTGETOPT_REQ_NOTHING },
470 { "--no-show-duration", kCmdOpt_NoShowDuration, RTGETOPT_REQ_NOTHING },
471 { "--show-iterations", kCmdOpt_ShowIterations, RTGETOPT_REQ_NOTHING },
472 { "--no-show-iterations", kCmdOpt_NoShowIterations, RTGETOPT_REQ_NOTHING },
473
474 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
475 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
476 { "--version", 'V', RTGETOPT_REQ_NOTHING },
477 { "--help", 'h', RTGETOPT_REQ_NOTHING } /* for Usage() */
478};
479
480/** The test handle. */
481static RTTEST g_hTest;
482/** The page size of the system. */
483static uint32_t g_cbPage;
484/** Page offset mask. */
485static uintptr_t g_fPageOffset;
486/** Page shift in bits. */
487static uint32_t g_cPageShift;
488/** The number of nanoseconds a RTTimeNanoTS call takes.
489 * This is used for adjusting loop count estimates. */
490static uint64_t g_nsPerNanoTSCall = 1;
491/** Whether or not to display the duration of each profile run.
492 * This is chiefly for verify the estimate phase. */
493static bool g_fShowDuration = false;
494/** Whether or not to display the iteration count for each profile run.
495 * This is chiefly for verify the estimate phase. */
496static bool g_fShowIterations = false;
497/** Verbosity level. */
498static uint32_t g_uVerbosity = 0;
499/** Max buffer size, UINT32_MAX for unlimited.
500 * This is for making sure we don't run into the MDL limit on windows, which
501 * a bit less than 64 MiB. */
502#if defined(RT_OS_WINDOWS)
503static uint32_t g_cbMaxBuffer = _32M;
504#else
505static uint32_t g_cbMaxBuffer = UINT32_MAX;
506#endif
507/** When to place the mmap test. */
508static int g_iMMapPlacement = 0;
509
510/** @name Selected subtest
511 * @{ */
512static bool g_fManyFiles = true;
513static bool g_fOpen = true;
514static bool g_fFStat = true;
515#ifdef RT_OS_WINDOWS
516static bool g_fNtQueryInfoFile = true;
517static bool g_fNtQueryVolInfoFile = true;
518#endif
519static bool g_fFChMod = true;
520static bool g_fFUtimes = true;
521static bool g_fStat = true;
522static bool g_fChMod = true;
523static bool g_fUtimes = true;
524static bool g_fRename = true;
525static bool g_fDirOpen = true;
526static bool g_fDirEnum = true;
527static bool g_fMkRmDir = true;
528static bool g_fStatVfs = true;
529static bool g_fRm = true;
530static bool g_fChSize = true;
531static bool g_fReadTests = true;
532static bool g_fReadPerf = true;
533#ifdef FSPERF_TEST_SENDFILE
534static bool g_fSendFile = true;
535#endif
536#ifdef RT_OS_LINUX
537static bool g_fSplice = true;
538#endif
539static bool g_fWriteTests = true;
540static bool g_fWritePerf = true;
541static bool g_fSeek = true;
542static bool g_fFSync = true;
543static bool g_fMMap = true;
544static bool g_fMMapCoherency = true;
545static bool g_fCopy = true;
546static bool g_fRemote = true;
547/** @} */
548
549/** The length of each test run. */
550static uint64_t g_nsTestRun = RT_NS_1SEC_64 * 10;
551
552/** For the 'manyfiles' subdir. */
553static uint32_t g_cManyFiles = 10000;
554
555/** Number of files in the 'manytree' directory tree. */
556static uint32_t g_cManyTreeFiles = 640 + 16*640 /*10880*/;
557/** Number of files per directory in the 'manytree' construct. */
558static uint32_t g_cManyTreeFilesPerDir = 640;
559/** Number of subdirs per directory in the 'manytree' construct. */
560static uint32_t g_cManyTreeSubdirsPerDir = 16;
561/** The depth of the 'manytree' directory tree. */
562static uint32_t g_cManyTreeDepth = 1;
563/** List of directories in the many tree, creation order. */
564static RTLISTANCHOR g_ManyTreeHead;
565
566/** Number of configured I/O block sizes. */
567static uint32_t g_cIoBlocks = 8;
568/** Configured I/O block sizes. */
569static uint32_t g_acbIoBlocks[16] = { 1, 512, 4096, 16384, 65536, _1M, _32M, _128M };
570/** The desired size of the test file we use for I/O. */
571static uint64_t g_cbIoFile = _512M;
572/** Whether to be less strict with non-cache file handle. */
573static bool g_fIgnoreNoCache = false;
574
575/** Set if g_szDir and friends are path relative to CWD rather than absolute. */
576static bool g_fRelativeDir = false;
577/** The length of g_szDir. */
578static size_t g_cchDir;
579/** The length of g_szEmptyDir. */
580static size_t g_cchEmptyDir;
581/** The length of g_szDeepDir. */
582static size_t g_cchDeepDir;
583
584/** The length of g_szCommsDir. */
585static size_t g_cchCommsDir;
586/** The length of g_szCommsSubDir. */
587static size_t g_cchCommsSubDir;
588
589/** The test directory (absolute). This will always have a trailing slash. */
590static char g_szDir[FSPERF_MAX_PATH];
591/** The test directory (absolute), 2nd copy for use with InDir2(). */
592static char g_szDir2[FSPERF_MAX_PATH];
593/** The empty test directory (absolute). This will always have a trailing slash. */
594static char g_szEmptyDir[FSPERF_MAX_PATH];
595/** The deep test directory (absolute). This will always have a trailing slash. */
596static char g_szDeepDir[FSPERF_MAX_PATH + _1K];
597
598/** The communcations directory. This will always have a trailing slash. */
599static char g_szCommsDir[FSPERF_MAX_PATH];
600/** The communcations subdirectory used for the actual communication. This will
601 * always have a trailing slash. */
602static char g_szCommsSubDir[FSPERF_MAX_PATH];
603
604/**
605 * Yield the CPU and stuff before starting a test run.
606 */
607DECLINLINE(void) fsPerfYield(void)
608{
609 RTThreadYield();
610 RTThreadYield();
611}
612
613
614/**
615 * Profiles the RTTimeNanoTS call, setting g_nsPerNanoTSCall.
616 */
617static void fsPerfNanoTS(void)
618{
619 fsPerfYield();
620
621 /* Make sure we start off on a changing timestamp on platforms will low time resoultion. */
622 uint64_t nsStart = RTTimeNanoTS();
623 uint64_t ns;
624 do
625 ns = RTTimeNanoTS();
626 while (ns == nsStart);
627 nsStart = ns;
628
629 /* Call it for 10 ms. */
630 uint32_t i = 0;
631 do
632 {
633 i++;
634 ns = RTTimeNanoTS();
635 }
636 while (ns - nsStart < RT_NS_10MS);
637
638 g_nsPerNanoTSCall = (ns - nsStart) / i;
639}
640
641
642/**
643 * Construct a path relative to the base test directory.
644 *
645 * @returns g_szDir.
646 * @param pszAppend What to append.
647 * @param cchAppend How much to append.
648 */
649DECLINLINE(char *) InDir(const char *pszAppend, size_t cchAppend)
650{
651 Assert(g_szDir[g_cchDir - 1] == RTPATH_SLASH);
652 memcpy(&g_szDir[g_cchDir], pszAppend, cchAppend);
653 g_szDir[g_cchDir + cchAppend] = '\0';
654 return &g_szDir[0];
655}
656
657
658/**
659 * Construct a path relative to the base test directory, 2nd copy.
660 *
661 * @returns g_szDir2.
662 * @param pszAppend What to append.
663 * @param cchAppend How much to append.
664 */
665DECLINLINE(char *) InDir2(const char *pszAppend, size_t cchAppend)
666{
667 Assert(g_szDir[g_cchDir - 1] == RTPATH_SLASH);
668 memcpy(g_szDir2, g_szDir, g_cchDir);
669 memcpy(&g_szDir2[g_cchDir], pszAppend, cchAppend);
670 g_szDir2[g_cchDir + cchAppend] = '\0';
671 return &g_szDir2[0];
672}
673
674
675/**
676 * Construct a path relative to the empty directory.
677 *
678 * @returns g_szEmptyDir.
679 * @param pszAppend What to append.
680 * @param cchAppend How much to append.
681 */
682DECLINLINE(char *) InEmptyDir(const char *pszAppend, size_t cchAppend)
683{
684 Assert(g_szEmptyDir[g_cchEmptyDir - 1] == RTPATH_SLASH);
685 memcpy(&g_szEmptyDir[g_cchEmptyDir], pszAppend, cchAppend);
686 g_szEmptyDir[g_cchEmptyDir + cchAppend] = '\0';
687 return &g_szEmptyDir[0];
688}
689
690
691/**
692 * Construct a path relative to the deep test directory.
693 *
694 * @returns g_szDeepDir.
695 * @param pszAppend What to append.
696 * @param cchAppend How much to append.
697 */
698DECLINLINE(char *) InDeepDir(const char *pszAppend, size_t cchAppend)
699{
700 Assert(g_szDeepDir[g_cchDeepDir - 1] == RTPATH_SLASH);
701 memcpy(&g_szDeepDir[g_cchDeepDir], pszAppend, cchAppend);
702 g_szDeepDir[g_cchDeepDir + cchAppend] = '\0';
703 return &g_szDeepDir[0];
704}
705
706
707
708/*********************************************************************************************************************************
709* Slave FsPerf Instance Interaction. *
710*********************************************************************************************************************************/
711
712/**
713 * Construct a path relative to the comms directory.
714 *
715 * @returns g_szCommsDir.
716 * @param pszAppend What to append.
717 * @param cchAppend How much to append.
718 */
719DECLINLINE(char *) InCommsDir(const char *pszAppend, size_t cchAppend)
720{
721 Assert(g_szCommsDir[g_cchCommsDir - 1] == RTPATH_SLASH);
722 memcpy(&g_szCommsDir[g_cchCommsDir], pszAppend, cchAppend);
723 g_szCommsDir[g_cchCommsDir + cchAppend] = '\0';
724 return &g_szCommsDir[0];
725}
726
727
728/**
729 * Construct a path relative to the comms sub-directory.
730 *
731 * @returns g_szCommsSubDir.
732 * @param pszAppend What to append.
733 * @param cchAppend How much to append.
734 */
735DECLINLINE(char *) InCommsSubDir(const char *pszAppend, size_t cchAppend)
736{
737 Assert(g_szCommsSubDir[g_cchCommsSubDir - 1] == RTPATH_SLASH);
738 memcpy(&g_szCommsSubDir[g_cchCommsSubDir], pszAppend, cchAppend);
739 g_szCommsSubDir[g_cchCommsSubDir + cchAppend] = '\0';
740 return &g_szCommsSubDir[0];
741}
742
743
744/**
745 * Creates a file under g_szCommsDir with the given content.
746 *
747 * Will modify g_szCommsDir to contain the given filename.
748 *
749 * @returns IPRT status code (fully bitched).
750 * @param pszFilename The filename.
751 * @param cchFilename The length of the filename.
752 * @param pszContent The file content.
753 * @param cchContent The length of the file content.
754 */
755static int FsPerfCommsWriteFile(const char *pszFilename, size_t cchFilename, const char *pszContent, size_t cchContent)
756{
757 RTFILE hFile;
758 int rc = RTFileOpen(&hFile, InCommsDir(pszFilename, cchFilename),
759 RTFILE_O_WRITE | RTFILE_O_DENY_NONE | RTFILE_O_CREATE_REPLACE);
760 if (RT_SUCCESS(rc))
761 {
762 rc = RTFileWrite(hFile, pszContent, cchContent, NULL);
763 if (RT_FAILURE(rc))
764 RTMsgError("Error writing %#zx bytes to '%s': %Rrc", cchContent, g_szCommsDir, rc);
765
766 int rc2 = RTFileClose(hFile);
767 if (RT_FAILURE(rc2))
768 {
769 RTMsgError("Error closing to '%s': %Rrc", g_szCommsDir, rc);
770 rc = rc2;
771 }
772 if (RT_SUCCESS(rc) && g_uVerbosity >= 3)
773 RTMsgInfo("comms: wrote '%s'\n", g_szCommsDir);
774 if (RT_FAILURE(rc))
775 RTFileDelete(g_szCommsDir);
776 }
777 else
778 RTMsgError("Failed to create '%s': %Rrc", g_szCommsDir, rc);
779 return rc;
780}
781
782
783/**
784 * Creates a file under g_szCommsDir with the given content, then renames it
785 * into g_szCommsSubDir.
786 *
787 * Will modify g_szCommsSubDir to contain the final filename and g_szCommsDir to
788 * hold the temporary one.
789 *
790 * @returns IPRT status code (fully bitched).
791 * @param pszFilename The filename.
792 * @param cchFilename The length of the filename.
793 * @param pszContent The file content.
794 * @param cchContent The length of the file content.
795 */
796static int FsPerfCommsWriteFileAndRename(const char *pszFilename, size_t cchFilename, const char *pszContent, size_t cchContent)
797{
798 int rc = FsPerfCommsWriteFile(pszFilename, cchFilename, pszContent, cchContent);
799 if (RT_SUCCESS(rc))
800 {
801 rc = RTFileRename(g_szCommsDir, InCommsSubDir(pszFilename, cchFilename), RTPATHRENAME_FLAGS_REPLACE);
802 if (RT_SUCCESS(rc) && g_uVerbosity >= 3)
803 RTMsgInfo("comms: placed '%s'\n", g_szCommsSubDir);
804 if (RT_FAILURE(rc))
805 {
806 RTMsgError("Error renaming '%s' to '%s': %Rrc", g_szCommsDir, g_szCommsSubDir, rc);
807 RTFileDelete(g_szCommsDir);
808 }
809 }
810 return rc;
811}
812
813
814/**
815 * Reads the given file from the comms subdir, ensuring that it is terminated by
816 * an EOF (0x1a) character.
817 *
818 * @returns IPRT status code.
819 * @retval VERR_TRY_AGAIN if the file is incomplete.
820 * @retval VERR_FILE_TOO_BIG if the file is considered too big.
821 * @retval VERR_FILE_NOT_FOUND if not found.
822 *
823 * @param iSeqNo The sequence number.
824 * @param pszSuffix The filename suffix.
825 * @param ppszContent Where to return the content.
826 */
827static int FsPerfCommsReadFile(uint32_t iSeqNo, const char *pszSuffix, char **ppszContent)
828{
829 *ppszContent = NULL;
830
831 RTStrPrintf(&g_szCommsSubDir[g_cchCommsSubDir], sizeof(g_szCommsSubDir) - g_cchCommsSubDir, "%u%s", iSeqNo, pszSuffix);
832 RTFILE hFile;
833 int rc = RTFileOpen(&hFile, g_szCommsSubDir, RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
834 if (RT_SUCCESS(rc))
835 {
836 size_t cbUsed = 0;
837 size_t cbAlloc = 1024;
838 char *pszBuf = (char *)RTMemAllocZ(cbAlloc);
839 for (;;)
840 {
841 /* Do buffer resizing. */
842 size_t cbMaxRead = cbAlloc - cbUsed - 1;
843 if (cbMaxRead < 8)
844 {
845 if (cbAlloc < _1M)
846 {
847 cbAlloc *= 2;
848 void *pvRealloced = RTMemRealloc(pszBuf, cbAlloc);
849 if (!pvRealloced)
850 {
851 rc = VERR_NO_MEMORY;
852 break;
853 }
854 pszBuf = (char *)pvRealloced;
855 RT_BZERO(&pszBuf[cbAlloc / 2], cbAlloc);
856 cbMaxRead = cbAlloc - cbUsed - 1;
857 }
858 else
859 {
860 RTMsgError("File '%s' is too big - giving up at 1MB", g_szCommsSubDir);
861 rc = VERR_FILE_TOO_BIG;
862 break;
863 }
864 }
865
866 /* Do the reading. */
867 size_t cbActual = 0;
868 rc = RTFileRead(hFile, &pszBuf[cbUsed], cbMaxRead, &cbActual);
869 if (RT_SUCCESS(rc))
870 cbUsed += cbActual;
871 else
872 {
873 RTMsgError("Failed to read '%s': %Rrc", g_szCommsSubDir, rc);
874 break;
875 }
876
877 /* EOF? */
878 if (cbActual < cbMaxRead)
879 break;
880 }
881
882 RTFileClose(hFile);
883
884 /*
885 * Check if the file ends with the EOF marker.
886 */
887 if ( RT_SUCCESS(rc)
888 && ( cbUsed == 0
889 || pszBuf[cbUsed - 1] != FSPERF_EOF))
890 rc = VERR_TRY_AGAIN;
891
892 /*
893 * Return or free the content we've read.
894 */
895 if (RT_SUCCESS(rc))
896 *ppszContent = pszBuf;
897 else
898 RTMemFree(pszBuf);
899 }
900 else if (rc != VERR_FILE_NOT_FOUND && rc != VERR_SHARING_VIOLATION)
901 RTMsgError("Failed to open '%s': %Rrc", g_szCommsSubDir, rc);
902 return rc;
903}
904
905
906/**
907 * FsPerfCommsReadFile + renaming from the comms subdir to the comms dir.
908 *
909 * g_szCommsSubDir holds the original filename and g_szCommsDir the final
910 * filename on success.
911 */
912static int FsPerfCommsReadFileAndRename(uint32_t iSeqNo, const char *pszSuffix, const char *pszRenameSuffix, char **ppszContent)
913{
914 RTStrPrintf(&g_szCommsDir[g_cchCommsDir], sizeof(g_szCommsDir) - g_cchCommsDir, "%u%s", iSeqNo, pszRenameSuffix);
915 int rc = FsPerfCommsReadFile(iSeqNo, pszSuffix, ppszContent);
916 if (RT_SUCCESS(rc))
917 {
918 rc = RTFileRename(g_szCommsSubDir, g_szCommsDir, RTPATHRENAME_FLAGS_REPLACE);
919 if (RT_FAILURE(rc))
920 {
921 RTMsgError("Error renaming '%s' to '%s': %Rrc", g_szCommsSubDir, g_szCommsDir, rc);
922 RTMemFree(*ppszContent);
923 *ppszContent = NULL;
924 }
925 }
926 return rc;
927}
928
929
930/** The comms master sequence number. */
931static uint32_t g_iSeqNoMaster = 0;
932
933
934/**
935 * Sends a script to the remote comms slave.
936 *
937 * @returns IPRT status code giving the scripts execution status.
938 * @param pszScript The script.
939 */
940static int FsPerfCommsSend(const char *pszScript)
941{
942 /*
943 * Make sure the script is correctly terminated with an EOF control character.
944 */
945 size_t const cchScript = strlen(pszScript);
946 AssertReturn(cchScript > 0 && pszScript[cchScript - 1] == FSPERF_EOF, VERR_INVALID_PARAMETER);
947
948 /*
949 * Make sure the comms slave is running.
950 */
951 if (!RTFileExists(InCommsDir(RT_STR_TUPLE("slave.pid"))))
952 return VERR_PIPE_NOT_CONNECTED;
953
954 /*
955 * Format all the names we might want to check for.
956 */
957 char szSendNm[32];
958 size_t const cchSendNm = RTStrPrintf(szSendNm, sizeof(szSendNm), "%u-order.send", g_iSeqNoMaster);
959
960 char szAckNm[64];
961 size_t const cchAckNm = RTStrPrintf(szAckNm, sizeof(szAckNm), "%u-order.ack", g_iSeqNoMaster);
962
963 /*
964 * Produce the script file and submit it.
965 */
966 int rc = FsPerfCommsWriteFileAndRename(szSendNm, cchSendNm, pszScript, cchScript);
967 if (RT_SUCCESS(rc))
968 {
969 g_iSeqNoMaster++;
970
971 /*
972 * Wait for the result.
973 */
974 uint64_t const msTimeout = RT_MS_1MIN / 2;
975 uint64_t msStart = RTTimeMilliTS();
976 uint32_t msSleepX4 = 4;
977 for (;;)
978 {
979 /* Try read the result file: */
980 char *pszContent = NULL;
981 rc = FsPerfCommsReadFile(g_iSeqNoMaster - 1, "-order.done", &pszContent);
982 if (RT_SUCCESS(rc))
983 {
984 /* Split the result content into status code and error text: */
985 char *pszErrorText = strchr(pszContent, '\n');
986 if (pszErrorText)
987 {
988 *pszErrorText = '\0';
989 pszErrorText++;
990 }
991 else
992 {
993 char *pszEnd = strchr(pszContent, '\0');
994 Assert(pszEnd[-1] == FSPERF_EOF);
995 pszEnd[-1] = '\0';
996 }
997
998 /* Parse the status code: */
999 int32_t rcRemote = VERR_GENERAL_FAILURE;
1000 rc = RTStrToInt32Full(pszContent, 0, &rcRemote);
1001 if (rc != VINF_SUCCESS)
1002 {
1003 RTTestIFailed("FsPerfCommsSend: Failed to convert status code '%s'", pszContent);
1004 rcRemote = VERR_GENERAL_FAILURE;
1005 }
1006
1007 /* Display or return the text? */
1008 if (RT_SUCCESS(rc) && g_uVerbosity >= 2)
1009 RTMsgInfo("comms: order #%u: %Rrc%s%s\n",
1010 g_iSeqNoMaster - 1, rcRemote, *pszErrorText ? " - " : "", pszErrorText);
1011
1012 RTMemFree(pszContent);
1013 return rcRemote;
1014 }
1015
1016 if (rc == VERR_TRY_AGAIN)
1017 msSleepX4 = 4;
1018
1019 /* Check for timeout. */
1020 if (RTTimeMilliTS() - msStart > msTimeout)
1021 {
1022 if (RT_SUCCESS(rc) && g_uVerbosity >= 2)
1023 RTMsgInfo("comms: timed out waiting for order #%u'\n", g_iSeqNoMaster - 1);
1024
1025 rc = RTFileDelete(InCommsSubDir(szSendNm, cchSendNm));
1026 if (RT_SUCCESS(rc))
1027 {
1028 g_iSeqNoMaster--;
1029 rc = VERR_TIMEOUT;
1030 }
1031 else if (RTFileExists(InCommsDir(szAckNm, cchAckNm)))
1032 rc = VERR_PIPE_BUSY;
1033 else
1034 rc = VERR_PIPE_IO_ERROR;
1035 break;
1036 }
1037
1038 /* Sleep a little while. */
1039 msSleepX4++;
1040 RTThreadSleep(msSleepX4 / 4);
1041 }
1042 }
1043 return rc;
1044}
1045
1046
1047/**
1048 * Shuts down the comms slave if it exists.
1049 */
1050static void FsPerfCommsShutdownSlave(void)
1051{
1052 static bool s_fAlreadyShutdown = false;
1053 if (g_szCommsDir[0] != '\0' && !s_fAlreadyShutdown)
1054 {
1055 s_fAlreadyShutdown = true;
1056 FsPerfCommsSend("exit" FSPERF_EOF_STR);
1057
1058 g_szCommsDir[g_cchCommsDir] = '\0';
1059 int rc = RTDirRemoveRecursive(g_szCommsDir, RTDIRRMREC_F_CONTENT_AND_DIR | (g_fRelativeDir ? RTDIRRMREC_F_NO_ABS_PATH : 0));
1060 if (RT_FAILURE(rc))
1061 RTTestFailed(g_hTest, "RTDirRemoveRecursive(%s,) -> %Rrc\n", g_szCommsDir, rc);
1062 }
1063}
1064
1065
1066
1067/*********************************************************************************************************************************
1068* Comms Slave *
1069*********************************************************************************************************************************/
1070
1071typedef struct FSPERFCOMMSSLAVESTATE
1072{
1073 uint32_t iSeqNo;
1074 bool fTerminate;
1075 RTEXITCODE rcExit;
1076 RTFILE ahFiles[8];
1077 char *apszFilenames[8];
1078
1079 /** The current command. */
1080 const char *pszCommand;
1081 /** The current line number. */
1082 uint32_t iLineNo;
1083 /** The current line content. */
1084 const char *pszLine;
1085 /** Where to return extra error info text. */
1086 RTERRINFOSTATIC ErrInfo;
1087} FSPERFCOMMSSLAVESTATE;
1088
1089
1090static void FsPerfSlaveStateInit(FSPERFCOMMSSLAVESTATE *pState)
1091{
1092 pState->iSeqNo = 0;
1093 pState->fTerminate = false;
1094 pState->rcExit = RTEXITCODE_SUCCESS;
1095 unsigned i = RT_ELEMENTS(pState->ahFiles);
1096 while (i-- > 0)
1097 {
1098 pState->ahFiles[i] = NIL_RTFILE;
1099 pState->apszFilenames[i] = NULL;
1100 }
1101 RTErrInfoInitStatic(&pState->ErrInfo);
1102}
1103
1104
1105static void FsPerfSlaveStateCleanup(FSPERFCOMMSSLAVESTATE *pState)
1106{
1107 unsigned i = RT_ELEMENTS(pState->ahFiles);
1108 while (i-- > 0)
1109 {
1110 if (pState->ahFiles[i] != NIL_RTFILE)
1111 {
1112 RTFileClose(pState->ahFiles[i]);
1113 pState->ahFiles[i] = NIL_RTFILE;
1114 }
1115 if (pState->apszFilenames[i] != NULL)
1116 {
1117 RTStrFree(pState->apszFilenames[i]);
1118 pState->apszFilenames[i] = NULL;
1119 }
1120 }
1121}
1122
1123
1124/** Helper reporting a error. */
1125static int FsPerfSlaveError(FSPERFCOMMSSLAVESTATE *pState, int rc, const char *pszError, ...)
1126{
1127 va_list va;
1128 va_start(va, pszError);
1129 RTErrInfoSetF(&pState->ErrInfo.Core, VERR_PARSE_ERROR, "line %u: %s: error: %N",
1130 pState->iLineNo, pState->pszCommand, pszError, &va);
1131 va_end(va);
1132 return rc;
1133}
1134
1135
1136/** Helper reporting a syntax error. */
1137static int FsPerfSlaveSyntax(FSPERFCOMMSSLAVESTATE *pState, const char *pszError, ...)
1138{
1139 va_list va;
1140 va_start(va, pszError);
1141 RTErrInfoSetF(&pState->ErrInfo.Core, VERR_PARSE_ERROR, "line %u: %s: syntax error: %N",
1142 pState->iLineNo, pState->pszCommand, pszError, &va);
1143 va_end(va);
1144 return VERR_PARSE_ERROR;
1145}
1146
1147
1148/** Helper for parsing an unsigned 64-bit integer argument. */
1149static int FsPerfSlaveParseU64(FSPERFCOMMSSLAVESTATE *pState, const char *pszArg, const char *pszName,
1150 unsigned uBase, uint64_t uMin, uint64_t uLast, uint64_t *puValue)
1151{
1152 *puValue = uMin;
1153 uint64_t uValue;
1154 int rc = RTStrToUInt64Full(pszArg, uBase, &uValue);
1155 if (RT_FAILURE(rc))
1156 return FsPerfSlaveSyntax(pState, "invalid %s: %s (RTStrToUInt64Full -> %Rrc)", pszName, pszArg, rc);
1157 if (uValue < uMin || uValue > uLast)
1158 return FsPerfSlaveSyntax(pState, "%s is out of range: %u, valid range %u..%u", pszName, uValue, uMin, uLast);
1159 *puValue = uValue;
1160 return VINF_SUCCESS;
1161}
1162
1163
1164/** Helper for parsing an unsigned 32-bit integer argument. */
1165static int FsPerfSlaveParseU32(FSPERFCOMMSSLAVESTATE *pState, const char *pszArg, const char *pszName,
1166 unsigned uBase, uint32_t uMin, uint32_t uLast, uint32_t *puValue)
1167{
1168 *puValue = uMin;
1169 uint32_t uValue;
1170 int rc = RTStrToUInt32Full(pszArg, uBase, &uValue);
1171 if (RT_FAILURE(rc))
1172 return FsPerfSlaveSyntax(pState, "invalid %s: %s (RTStrToUInt32Full -> %Rrc)", pszName, pszArg, rc);
1173 if (uValue < uMin || uValue > uLast)
1174 return FsPerfSlaveSyntax(pState, "%s is out of range: %u, valid range %u..%u", pszName, uValue, uMin, uLast);
1175 *puValue = uValue;
1176 return VINF_SUCCESS;
1177}
1178
1179
1180/** Helper for parsing a file handle index argument. */
1181static int FsPerfSlaveParseFileIdx(FSPERFCOMMSSLAVESTATE *pState, const char *pszArg, uint32_t *pidxFile)
1182{
1183 return FsPerfSlaveParseU32(pState, pszArg, "file index", 0, 0, RT_ELEMENTS(pState->ahFiles) - 1, pidxFile);
1184}
1185
1186
1187/**
1188 * 'open {idxFile} {filename} {access} {disposition} [sharing] [mode]'
1189 */
1190static int FsPerfSlaveHandleOpen(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1191{
1192 /*
1193 * Parse parameters.
1194 */
1195 if (cArgs > 1 + 6 || cArgs < 1 + 4)
1196 return FsPerfSlaveSyntax(pState, "takes four to six arguments, not %u", cArgs);
1197
1198 uint32_t idxFile;
1199 int rc = FsPerfSlaveParseFileIdx(pState, papszArgs[1], &idxFile);
1200 if (RT_FAILURE(rc))
1201 return rc;
1202
1203 const char *pszFilename = papszArgs[2];
1204
1205 uint64_t fOpen = 0;
1206 rc = RTFileModeToFlagsEx(papszArgs[3], papszArgs[4], papszArgs[5], &fOpen);
1207 if (RT_FAILURE(rc))
1208 return FsPerfSlaveSyntax(pState, "failed to parse access (%s), disposition (%s) and sharing (%s): %Rrc",
1209 papszArgs[3], papszArgs[4], papszArgs[5] ? papszArgs[5] : "", rc);
1210
1211 uint32_t uMode = 0660;
1212 if (cArgs >= 1 + 6)
1213 {
1214 rc = FsPerfSlaveParseU32(pState, papszArgs[6], "mode", 8, 0, 0777, &uMode);
1215 if (RT_FAILURE(rc))
1216 return rc;
1217 fOpen |= uMode << RTFILE_O_CREATE_MODE_SHIFT;
1218 }
1219
1220 /*
1221 * Is there already a file assigned to the file handle index?
1222 */
1223 if (pState->ahFiles[idxFile] != NIL_RTFILE)
1224 return FsPerfSlaveError(pState, VERR_RESOURCE_BUSY, "handle #%u is already in use for '%s'",
1225 idxFile, pState->apszFilenames[idxFile]);
1226
1227 /*
1228 * Check the filename length.
1229 */
1230 size_t const cchFilename = strlen(pszFilename);
1231 if (g_cchDir + cchFilename >= sizeof(g_szDir))
1232 return FsPerfSlaveError(pState, VERR_FILENAME_TOO_LONG, "'%.*s%s'", g_cchDir, g_szDir, pszFilename);
1233
1234 /*
1235 * Duplicate the name and execute the command.
1236 */
1237 char *pszDup = RTStrDup(pszFilename);
1238 if (!pszDup)
1239 return FsPerfSlaveError(pState, VERR_NO_STR_MEMORY, "out of memory");
1240
1241 RTFILE hFile = NIL_RTFILE;
1242 rc = RTFileOpen(&hFile, InDir(pszFilename, cchFilename), fOpen);
1243 if (RT_SUCCESS(rc))
1244 {
1245 pState->ahFiles[idxFile] = hFile;
1246 pState->apszFilenames[idxFile] = pszDup;
1247 }
1248 else
1249 {
1250 RTStrFree(pszDup);
1251 rc = FsPerfSlaveError(pState, rc, "%s: %Rrc", pszFilename, rc);
1252 }
1253 return rc;
1254}
1255
1256
1257/**
1258 * 'close {idxFile}'
1259 */
1260static int FsPerfSlaveHandleClose(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1261{
1262 /*
1263 * Parse parameters.
1264 */
1265 if (cArgs > 1 + 1)
1266 return FsPerfSlaveSyntax(pState, "takes exactly one argument, not %u", cArgs);
1267
1268 uint32_t idxFile;
1269 int rc = FsPerfSlaveParseFileIdx(pState, papszArgs[1], &idxFile);
1270 if (RT_SUCCESS(rc))
1271 {
1272 /*
1273 * Do it.
1274 */
1275 rc = RTFileClose(pState->ahFiles[idxFile]);
1276 if (RT_SUCCESS(rc))
1277 {
1278 pState->ahFiles[idxFile] = NIL_RTFILE;
1279 RTStrFree(pState->apszFilenames[idxFile]);
1280 pState->apszFilenames[idxFile] = NULL;
1281 }
1282 }
1283 return rc;
1284}
1285
1286/** @name Patterns for 'writepattern'
1287 * @{ */
1288static uint8_t const g_abPattern0[] = { 0xf0 };
1289static uint8_t const g_abPattern1[] = { 0xf1 };
1290static uint8_t const g_abPattern2[] = { 0xf2 };
1291static uint8_t const g_abPattern3[] = { 0xf3 };
1292static uint8_t const g_abPattern4[] = { 0xf4 };
1293static uint8_t const g_abPattern5[] = { 0xf5 };
1294static uint8_t const g_abPattern6[] = { 0xf6 };
1295static uint8_t const g_abPattern7[] = { 0xf7 };
1296static uint8_t const g_abPattern8[] = { 0xf8 };
1297static uint8_t const g_abPattern9[] = { 0xf9 };
1298static uint8_t const g_abPattern10[] = { 0x1f, 0x4e, 0x99, 0xec, 0x71, 0x71, 0x48, 0x0f, 0xa7, 0x5c, 0xb4, 0x5a, 0x1f, 0xc7, 0xd0, 0x93 };
1299static struct
1300{
1301 uint8_t const *pb;
1302 uint32_t cb;
1303} const g_aPatterns[] =
1304{
1305 { g_abPattern0, sizeof(g_abPattern0) },
1306 { g_abPattern1, sizeof(g_abPattern1) },
1307 { g_abPattern2, sizeof(g_abPattern2) },
1308 { g_abPattern3, sizeof(g_abPattern3) },
1309 { g_abPattern4, sizeof(g_abPattern4) },
1310 { g_abPattern5, sizeof(g_abPattern5) },
1311 { g_abPattern6, sizeof(g_abPattern6) },
1312 { g_abPattern7, sizeof(g_abPattern7) },
1313 { g_abPattern8, sizeof(g_abPattern8) },
1314 { g_abPattern9, sizeof(g_abPattern9) },
1315 { g_abPattern10, sizeof(g_abPattern10) },
1316};
1317/** @} */
1318
1319/**
1320 * 'writepattern {idxFile} {offFile} {idxPattern} {cbToWrite}'
1321 */
1322static int FsPerfSlaveHandleWritePattern(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1323{
1324 /*
1325 * Parse parameters.
1326 */
1327 if (cArgs > 1 + 4)
1328 return FsPerfSlaveSyntax(pState, "takes exactly four arguments, not %u", cArgs);
1329
1330 uint32_t idxFile;
1331 int rc = FsPerfSlaveParseFileIdx(pState, papszArgs[1], &idxFile);
1332 if (RT_FAILURE(rc))
1333 return rc;
1334
1335 uint64_t offFile;
1336 rc = FsPerfSlaveParseU64(pState, papszArgs[2], "file offset", 0, 0, UINT64_MAX / 4, &offFile);
1337 if (RT_FAILURE(rc))
1338 return rc;
1339
1340 uint32_t idxPattern;
1341 rc = FsPerfSlaveParseU32(pState, papszArgs[3], "pattern index", 0, 0, RT_ELEMENTS(g_aPatterns) - 1, &idxPattern);
1342 if (RT_FAILURE(rc))
1343 return rc;
1344
1345 uint64_t cbToWrite;
1346 rc = FsPerfSlaveParseU64(pState, papszArgs[4], "number of bytes to write", 0, 0, _1G, &cbToWrite);
1347 if (RT_FAILURE(rc))
1348 return rc;
1349
1350 if (pState->ahFiles[idxFile] == NIL_RTFILE)
1351 return FsPerfSlaveError(pState, VERR_INVALID_HANDLE, "no open file at index #%u", idxFile);
1352
1353 /*
1354 * Allocate a suitable buffer.
1355 */
1356 size_t cbMaxBuf = RT_MIN(_2M, g_cbMaxBuffer);
1357 size_t cbBuf = cbToWrite >= cbMaxBuf ? cbMaxBuf : RT_ALIGN_Z((size_t)cbToWrite, 512);
1358 uint8_t *pbBuf = (uint8_t *)RTMemTmpAlloc(cbBuf);
1359 if (!pbBuf)
1360 {
1361 cbBuf = _4K;
1362 pbBuf = (uint8_t *)RTMemTmpAlloc(cbBuf);
1363 if (!pbBuf)
1364 return FsPerfSlaveError(pState, VERR_NO_TMP_MEMORY, "failed to allocate 4KB for buffers");
1365 }
1366
1367 /*
1368 * Fill 1 byte patterns before we start looping.
1369 */
1370 if (g_aPatterns[idxPattern].cb == 1)
1371 memset(pbBuf, g_aPatterns[idxPattern].pb[0], cbBuf);
1372
1373 /*
1374 * The write loop.
1375 */
1376 uint32_t offPattern = 0;
1377 while (cbToWrite > 0)
1378 {
1379 /*
1380 * Fill the buffer if multi-byte pattern (single byte patterns are handled before the loop):
1381 */
1382 if (g_aPatterns[idxPattern].cb > 1)
1383 {
1384 uint32_t const cbSrc = g_aPatterns[idxPattern].cb;
1385 uint8_t const * const pbSrc = g_aPatterns[idxPattern].pb;
1386 size_t cbDst = cbBuf;
1387 uint8_t *pbDst = pbBuf;
1388
1389 /* first iteration, potential partial pattern. */
1390 if (offPattern >= cbSrc)
1391 offPattern = 0;
1392 size_t cbThis1 = RT_MIN(g_aPatterns[idxPattern].cb - offPattern, cbToWrite);
1393 memcpy(pbDst, &pbSrc[offPattern], cbThis1);
1394 cbDst -= cbThis1;
1395 if (cbDst > 0)
1396 {
1397 pbDst += cbThis1;
1398 offPattern = 0;
1399
1400 /* full patterns */
1401 while (cbDst >= cbSrc)
1402 {
1403 memcpy(pbDst, pbSrc, cbSrc);
1404 pbDst += cbSrc;
1405 cbDst -= cbSrc;
1406 }
1407
1408 /* partial final copy */
1409 if (cbDst > 0)
1410 {
1411 memcpy(pbDst, pbSrc, cbDst);
1412 offPattern = (uint32_t)cbDst;
1413 }
1414 }
1415 }
1416
1417 /*
1418 * Write.
1419 */
1420 size_t const cbThisWrite = (size_t)RT_MIN(cbToWrite, cbBuf);
1421 rc = RTFileWriteAt(pState->ahFiles[idxFile], offFile, pbBuf, cbThisWrite, NULL);
1422 if (RT_FAILURE(rc))
1423 {
1424 FsPerfSlaveError(pState, rc, "error writing %#zx bytes at %#RX64: %Rrc (file: %s)",
1425 cbThisWrite, offFile, rc, pState->apszFilenames[idxFile]);
1426 break;
1427 }
1428
1429 offFile += cbThisWrite;
1430 cbToWrite -= cbThisWrite;
1431 }
1432
1433 RTMemTmpFree(pbBuf);
1434 return rc;
1435}
1436
1437
1438/**
1439 * 'truncate {idxFile} {cbFile}'
1440 */
1441static int FsPerfSlaveHandleTruncate(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1442{
1443 /*
1444 * Parse parameters.
1445 */
1446 if (cArgs != 1 + 2)
1447 return FsPerfSlaveSyntax(pState, "takes exactly two arguments, not %u", cArgs);
1448
1449 uint32_t idxFile;
1450 int rc = FsPerfSlaveParseFileIdx(pState, papszArgs[1], &idxFile);
1451 if (RT_FAILURE(rc))
1452 return rc;
1453
1454 uint64_t cbFile;
1455 rc = FsPerfSlaveParseU64(pState, papszArgs[2], "new file size", 0, 0, UINT64_MAX / 4, &cbFile);
1456 if (RT_FAILURE(rc))
1457 return rc;
1458
1459 if (pState->ahFiles[idxFile] == NIL_RTFILE)
1460 return FsPerfSlaveError(pState, VERR_INVALID_HANDLE, "no open file at index #%u", idxFile);
1461
1462 /*
1463 * Execute.
1464 */
1465 rc = RTFileSetSize(pState->ahFiles[idxFile], cbFile);
1466 if (RT_FAILURE(rc))
1467 return FsPerfSlaveError(pState, rc, "failed to set file size to %#RX64: %Rrc (file: %s)",
1468 cbFile, rc, pState->apszFilenames[idxFile]);
1469 return VINF_SUCCESS;
1470}
1471
1472
1473/**
1474 * 'futimes {idxFile} {modified|0} [access|0] [change|0] [birth|0]'
1475 */
1476static int FsPerfSlaveHandleFUTimes(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1477{
1478 /*
1479 * Parse parameters.
1480 */
1481 if (cArgs < 1 + 2 || cArgs > 1 + 5)
1482 return FsPerfSlaveSyntax(pState, "takes between two and five arguments, not %u", cArgs);
1483
1484 uint32_t idxFile;
1485 int rc = FsPerfSlaveParseFileIdx(pState, papszArgs[1], &idxFile);
1486 if (RT_FAILURE(rc))
1487 return rc;
1488
1489 uint64_t nsModifiedTime;
1490 rc = FsPerfSlaveParseU64(pState, papszArgs[2], "modified time", 0, 0, UINT64_MAX, &nsModifiedTime);
1491 if (RT_FAILURE(rc))
1492 return rc;
1493
1494 uint64_t nsAccessTime = 0;
1495 if (cArgs >= 1 + 3)
1496 {
1497 rc = FsPerfSlaveParseU64(pState, papszArgs[3], "access time", 0, 0, UINT64_MAX, &nsAccessTime);
1498 if (RT_FAILURE(rc))
1499 return rc;
1500 }
1501
1502 uint64_t nsChangeTime = 0;
1503 if (cArgs >= 1 + 4)
1504 {
1505 rc = FsPerfSlaveParseU64(pState, papszArgs[4], "change time", 0, 0, UINT64_MAX, &nsChangeTime);
1506 if (RT_FAILURE(rc))
1507 return rc;
1508 }
1509
1510 uint64_t nsBirthTime = 0;
1511 if (cArgs >= 1 + 5)
1512 {
1513 rc = FsPerfSlaveParseU64(pState, papszArgs[4], "birth time", 0, 0, UINT64_MAX, &nsBirthTime);
1514 if (RT_FAILURE(rc))
1515 return rc;
1516 }
1517
1518 if (pState->ahFiles[idxFile] == NIL_RTFILE)
1519 return FsPerfSlaveError(pState, VERR_INVALID_HANDLE, "no open file at index #%u", idxFile);
1520
1521 /*
1522 * Execute.
1523 */
1524 RTTIMESPEC ModifiedTime;
1525 RTTIMESPEC AccessTime;
1526 RTTIMESPEC ChangeTime;
1527 RTTIMESPEC BirthTime;
1528 rc = RTFileSetTimes(pState->ahFiles[idxFile],
1529 nsAccessTime ? RTTimeSpecSetNano(&AccessTime, nsAccessTime) : NULL,
1530 nsModifiedTime ? RTTimeSpecSetNano(&ModifiedTime, nsModifiedTime) : NULL,
1531 nsChangeTime ? RTTimeSpecSetNano(&ChangeTime, nsChangeTime) : NULL,
1532 nsBirthTime ? RTTimeSpecSetNano(&BirthTime, nsBirthTime) : NULL);
1533 if (RT_FAILURE(rc))
1534 return FsPerfSlaveError(pState, rc, "failed to set file times to %RI64, %RI64, %RI64, %RI64: %Rrc (file: %s)",
1535 nsModifiedTime, nsAccessTime, nsChangeTime, nsBirthTime, rc, pState->apszFilenames[idxFile]);
1536 return VINF_SUCCESS;
1537}
1538
1539
1540/**
1541 * 'fchmod {idxFile} {cbFile}'
1542 */
1543static int FsPerfSlaveHandleFChMod(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1544{
1545 /*
1546 * Parse parameters.
1547 */
1548 if (cArgs != 1 + 2)
1549 return FsPerfSlaveSyntax(pState, "takes exactly two arguments, not %u", cArgs);
1550
1551 uint32_t idxFile;
1552 int rc = FsPerfSlaveParseFileIdx(pState, papszArgs[1], &idxFile);
1553 if (RT_FAILURE(rc))
1554 return rc;
1555
1556 uint32_t fAttribs;
1557 rc = FsPerfSlaveParseU32(pState, papszArgs[2], "new file attributes", 0, 0, UINT32_MAX, &fAttribs);
1558 if (RT_FAILURE(rc))
1559 return rc;
1560
1561 if (pState->ahFiles[idxFile] == NIL_RTFILE)
1562 return FsPerfSlaveError(pState, VERR_INVALID_HANDLE, "no open file at index #%u", idxFile);
1563
1564 /*
1565 * Execute.
1566 */
1567 rc = RTFileSetMode(pState->ahFiles[idxFile], fAttribs);
1568 if (RT_FAILURE(rc))
1569 return FsPerfSlaveError(pState, rc, "failed to set file mode to %#RX32: %Rrc (file: %s)",
1570 fAttribs, rc, pState->apszFilenames[idxFile]);
1571 return VINF_SUCCESS;
1572}
1573
1574
1575/**
1576 * 'reset'
1577 */
1578static int FsPerfSlaveHandleReset(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1579{
1580 /*
1581 * Parse parameters.
1582 */
1583 if (cArgs > 1)
1584 return FsPerfSlaveSyntax(pState, "takes zero arguments, not %u", cArgs);
1585 RT_NOREF(papszArgs);
1586
1587 /*
1588 * Execute the command.
1589 */
1590 FsPerfSlaveStateCleanup(pState);
1591 return VINF_SUCCESS;
1592}
1593
1594
1595/**
1596 * 'exit [exitcode]'
1597 */
1598static int FsPerfSlaveHandleExit(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs)
1599{
1600 /*
1601 * Parse parameters.
1602 */
1603 if (cArgs > 1 + 1)
1604 return FsPerfSlaveSyntax(pState, "takes zero or one argument, not %u", cArgs);
1605
1606 if (cArgs >= 1 + 1)
1607 {
1608 uint32_t uExitCode;
1609 int rc = FsPerfSlaveParseU32(pState, papszArgs[1], "exit code", 0, 0, 127, &uExitCode);
1610 if (RT_FAILURE(rc))
1611 return rc;
1612
1613 /*
1614 * Execute the command.
1615 */
1616 pState->rcExit = (RTEXITCODE)uExitCode;
1617 }
1618 pState->fTerminate = true;
1619 return VINF_SUCCESS;
1620}
1621
1622
1623/**
1624 * Executes a script line.
1625 */
1626static int FsPerfSlaveExecuteLine(FSPERFCOMMSSLAVESTATE *pState, char *pszLine)
1627{
1628 /*
1629 * Parse the command line using bourne shell quoting style.
1630 */
1631 char **papszArgs;
1632 int cArgs;
1633 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
1634 if (RT_FAILURE(rc))
1635 return RTErrInfoSetF(&pState->ErrInfo.Core, rc, "Failed to parse line %u: %s", pState->iLineNo, pszLine);
1636 if (cArgs <= 0)
1637 {
1638 RTGetOptArgvFree(papszArgs);
1639 return RTErrInfoSetF(&pState->ErrInfo.Core, rc, "No command found on line %u: %s", pState->iLineNo, pszLine);
1640 }
1641
1642 /*
1643 * Execute the command.
1644 */
1645 static const struct
1646 {
1647 const char *pszCmd;
1648 size_t cchCmd;
1649 int (*pfnHandler)(FSPERFCOMMSSLAVESTATE *pState, char **papszArgs, int cArgs);
1650 } s_aHandlers[] =
1651 {
1652 { RT_STR_TUPLE("open"), FsPerfSlaveHandleOpen },
1653 { RT_STR_TUPLE("close"), FsPerfSlaveHandleClose },
1654 { RT_STR_TUPLE("writepattern"), FsPerfSlaveHandleWritePattern },
1655 { RT_STR_TUPLE("truncate"), FsPerfSlaveHandleTruncate },
1656 { RT_STR_TUPLE("futimes"), FsPerfSlaveHandleFUTimes},
1657 { RT_STR_TUPLE("fchmod"), FsPerfSlaveHandleFChMod },
1658 { RT_STR_TUPLE("reset"), FsPerfSlaveHandleReset },
1659 { RT_STR_TUPLE("exit"), FsPerfSlaveHandleExit },
1660 };
1661 const char * const pszCmd = papszArgs[0];
1662 size_t const cchCmd = strlen(pszCmd);
1663 for (size_t i = 0; i < RT_ELEMENTS(s_aHandlers); i++)
1664 if ( s_aHandlers[i].cchCmd == cchCmd
1665 && memcmp(pszCmd, s_aHandlers[i].pszCmd, cchCmd) == 0)
1666 {
1667 pState->pszCommand = s_aHandlers[i].pszCmd;
1668 rc = s_aHandlers[i].pfnHandler(pState, papszArgs, cArgs);
1669 RTGetOptArgvFree(papszArgs);
1670 return rc;
1671 }
1672
1673 rc = RTErrInfoSetF(&pState->ErrInfo.Core, VERR_NOT_FOUND, "Command on line %u not found: %s", pState->iLineNo, pszLine);
1674 RTGetOptArgvFree(papszArgs);
1675 return rc;
1676}
1677
1678
1679/**
1680 * Executes a script.
1681 */
1682static int FsPerfSlaveExecuteScript(FSPERFCOMMSSLAVESTATE *pState, char *pszContent)
1683{
1684 /*
1685 * Validate the encoding.
1686 */
1687 int rc = RTStrValidateEncoding(pszContent);
1688 if (RT_FAILURE(rc))
1689 return RTErrInfoSetF(&pState->ErrInfo.Core, rc, "Invalid UTF-8 encoding");
1690
1691 /*
1692 * Work the script content line by line.
1693 */
1694 pState->iLineNo = 0;
1695 while (*pszContent != FSPERF_EOF && *pszContent != '\0')
1696 {
1697 pState->iLineNo++;
1698
1699 /* Figure the current line and move pszContent ahead: */
1700 char *pszLine = RTStrStripL(pszContent);
1701 char *pszEol = strchr(pszLine, '\n');
1702 if (pszEol)
1703 pszContent = pszEol + 1;
1704 else
1705 {
1706 pszEol = strchr(pszLine, FSPERF_EOF);
1707 AssertStmt(pszEol, pszEol = strchr(pszLine, '\0'));
1708 pszContent = pszEol;
1709 }
1710
1711 /* Terminate and strip it: */
1712 *pszEol = '\0';
1713 pszLine = RTStrStrip(pszLine);
1714
1715 /* Skip empty lines and comment lines: */
1716 if (*pszLine == '\0' || *pszLine == '#')
1717 continue;
1718
1719 /* Execute the line: */
1720 pState->pszLine = pszLine;
1721 rc = FsPerfSlaveExecuteLine(pState, pszLine);
1722 if (RT_FAILURE(rc))
1723 break;
1724 }
1725 return rc;
1726}
1727
1728
1729/**
1730 * Communication slave.
1731 *
1732 * @returns exit code.
1733 */
1734static int FsPerfCommsSlave(void)
1735{
1736 /*
1737 * Make sure we've got a directory and create it and it's subdir.
1738 */
1739 if (g_cchCommsDir == 0)
1740 return RTMsgError("no communcation directory was specified (-C)");
1741
1742 int rc = RTDirCreateFullPath(g_szCommsSubDir, 0775);
1743 if (RT_FAILURE(rc))
1744 return RTMsgError("Failed to create '%s': %Rrc", g_szCommsSubDir, rc);
1745
1746 /*
1747 * Signal that we're here.
1748 */
1749 char szTmp[_4K];
1750 rc = FsPerfCommsWriteFile(RT_STR_TUPLE("slave.pid"), szTmp, RTStrPrintf(szTmp, sizeof(szTmp),
1751 "%u" FSPERF_EOF_STR, RTProcSelf()));
1752 if (RT_FAILURE(rc))
1753 return RTEXITCODE_FAILURE;
1754
1755 /*
1756 * Processing loop.
1757 */
1758 FSPERFCOMMSSLAVESTATE State;
1759 FsPerfSlaveStateInit(&State);
1760 uint32_t msSleep = 1;
1761 while (!State.fTerminate)
1762 {
1763 /*
1764 * Try read the next command script.
1765 */
1766 char *pszContent = NULL;
1767 rc = FsPerfCommsReadFileAndRename(State.iSeqNo, "-order.send", "-order.ack", &pszContent);
1768 if (RT_SUCCESS(rc))
1769 {
1770 /*
1771 * Execute it.
1772 */
1773 RTErrInfoInitStatic(&State.ErrInfo);
1774 rc = FsPerfSlaveExecuteScript(&State, pszContent);
1775
1776 /*
1777 * Write the result.
1778 */
1779 char szResult[64];
1780 size_t cchResult = RTStrPrintf(szResult, sizeof(szResult), "%u-order.done", State.iSeqNo);
1781 size_t cchTmp = RTStrPrintf(szTmp, sizeof(szTmp), "%d\n%s" FSPERF_EOF_STR,
1782 rc, RTErrInfoIsSet(&State.ErrInfo.Core) ? State.ErrInfo.Core.pszMsg : "");
1783 FsPerfCommsWriteFileAndRename(szResult, cchResult, szTmp, cchTmp);
1784 State.iSeqNo++;
1785
1786 msSleep = 1;
1787 }
1788
1789 /*
1790 * Wait a little and check again.
1791 */
1792 RTThreadSleep(msSleep);
1793 if (msSleep < 128)
1794 msSleep++;
1795 }
1796
1797 /*
1798 * Remove the we're here indicator and quit.
1799 */
1800 RTFileDelete(InCommsDir(RT_STR_TUPLE("slave.pid")));
1801 FsPerfSlaveStateCleanup(&State);
1802 return State.rcExit;
1803}
1804
1805
1806
1807/*********************************************************************************************************************************
1808* Tests *
1809*********************************************************************************************************************************/
1810
1811/**
1812 * Prepares the test area.
1813 * @returns VBox status code.
1814 */
1815static int fsPrepTestArea(void)
1816{
1817 /* The empty subdir and associated globals: */
1818 static char s_szEmpty[] = "empty";
1819 memcpy(g_szEmptyDir, g_szDir, g_cchDir);
1820 memcpy(&g_szEmptyDir[g_cchDir], s_szEmpty, sizeof(s_szEmpty));
1821 g_cchEmptyDir = g_cchDir + sizeof(s_szEmpty) - 1;
1822 RTTESTI_CHECK_RC_RET(RTDirCreate(g_szEmptyDir, 0755, 0), VINF_SUCCESS, rcCheck);
1823 g_szEmptyDir[g_cchEmptyDir++] = RTPATH_SLASH;
1824 g_szEmptyDir[g_cchEmptyDir] = '\0';
1825 RTTestIPrintf(RTTESTLVL_ALWAYS, "Empty dir: %s\n", g_szEmptyDir);
1826
1827 /* Deep directory: */
1828 memcpy(g_szDeepDir, g_szDir, g_cchDir);
1829 g_cchDeepDir = g_cchDir;
1830 do
1831 {
1832 static char const s_szSub[] = "d" RTPATH_SLASH_STR;
1833 memcpy(&g_szDeepDir[g_cchDeepDir], s_szSub, sizeof(s_szSub));
1834 g_cchDeepDir += sizeof(s_szSub) - 1;
1835 int rc = RTDirCreate(g_szDeepDir, 0755, 0);
1836 if (RT_FAILURE(rc))
1837 {
1838 RTTestIFailed("RTDirCreate(g_szDeepDir=%s) -> %Rrc\n", g_szDeepDir, rc);
1839 return rc;
1840 }
1841 } while (g_cchDeepDir < 176);
1842 RTTestIPrintf(RTTESTLVL_ALWAYS, "Deep dir: %s\n", g_szDeepDir);
1843
1844 /* Create known file in both deep and shallow dirs: */
1845 RTFILE hKnownFile;
1846 RTTESTI_CHECK_RC_RET(RTFileOpen(&hKnownFile, InDir(RT_STR_TUPLE("known-file")),
1847 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE),
1848 VINF_SUCCESS, rcCheck);
1849 RTTESTI_CHECK_RC_RET(RTFileClose(hKnownFile), VINF_SUCCESS, rcCheck);
1850
1851 RTTESTI_CHECK_RC_RET(RTFileOpen(&hKnownFile, InDeepDir(RT_STR_TUPLE("known-file")),
1852 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE),
1853 VINF_SUCCESS, rcCheck);
1854 RTTESTI_CHECK_RC_RET(RTFileClose(hKnownFile), VINF_SUCCESS, rcCheck);
1855
1856 return VINF_SUCCESS;
1857}
1858
1859
1860/**
1861 * Create a name list entry.
1862 * @returns Pointer to the entry, NULL if out of memory.
1863 * @param pchName The name.
1864 * @param cchName The name length.
1865 */
1866static PFSPERFNAMEENTRY fsPerfCreateNameEntry(const char *pchName, size_t cchName)
1867{
1868 PFSPERFNAMEENTRY pEntry = (PFSPERFNAMEENTRY)RTMemAllocVar(RT_UOFFSETOF_DYN(FSPERFNAMEENTRY, szName[cchName + 1]));
1869 if (pEntry)
1870 {
1871 RTListInit(&pEntry->Entry);
1872 pEntry->cchName = (uint16_t)cchName;
1873 memcpy(pEntry->szName, pchName, cchName);
1874 pEntry->szName[cchName] = '\0';
1875 }
1876 return pEntry;
1877}
1878
1879
1880static int fsPerfManyTreeRecursiveDirCreator(size_t cchDir, uint32_t iDepth)
1881{
1882 PFSPERFNAMEENTRY pEntry = fsPerfCreateNameEntry(g_szDir, cchDir);
1883 RTTESTI_CHECK_RET(pEntry, VERR_NO_MEMORY);
1884 RTListAppend(&g_ManyTreeHead, &pEntry->Entry);
1885
1886 RTTESTI_CHECK_RC_RET(RTDirCreate(g_szDir, 0755, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL),
1887 VINF_SUCCESS, rcCheck);
1888
1889 if (iDepth < g_cManyTreeDepth)
1890 for (uint32_t i = 0; i < g_cManyTreeSubdirsPerDir; i++)
1891 {
1892 size_t cchSubDir = RTStrPrintf(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, "d%02u" RTPATH_SLASH_STR, i);
1893 RTTESTI_CHECK_RC_RET(fsPerfManyTreeRecursiveDirCreator(cchDir + cchSubDir, iDepth + 1), VINF_SUCCESS, rcCheck);
1894 }
1895
1896 return VINF_SUCCESS;
1897}
1898
1899
1900static void fsPerfManyFiles(void)
1901{
1902 RTTestISub("manyfiles");
1903
1904 /*
1905 * Create a sub-directory with like 10000 files in it.
1906 *
1907 * This does push the directory organization of the underlying file system,
1908 * which is something we might not want to profile with shared folders. It
1909 * is however useful for directory enumeration.
1910 */
1911 RTTESTI_CHECK_RC_RETV(RTDirCreate(InDir(RT_STR_TUPLE("manyfiles")), 0755,
1912 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL),
1913 VINF_SUCCESS);
1914
1915 size_t offFilename = strlen(g_szDir);
1916 g_szDir[offFilename++] = RTPATH_SLASH;
1917
1918 fsPerfYield();
1919 RTFILE hFile;
1920 uint64_t const nsStart = RTTimeNanoTS();
1921 for (uint32_t i = 0; i < g_cManyFiles; i++)
1922 {
1923 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
1924 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, g_szDir, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1925 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
1926 }
1927 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
1928 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Creating %u empty files in single directory", g_cManyFiles);
1929 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Create empty file (single dir)");
1930
1931 /*
1932 * Create a bunch of directories with exacly 32 files in each, hoping to
1933 * avoid any directory organization artifacts.
1934 */
1935 /* Create the directories first, building a list of them for simplifying iteration: */
1936 RTListInit(&g_ManyTreeHead);
1937 InDir(RT_STR_TUPLE("manytree" RTPATH_SLASH_STR));
1938 RTTESTI_CHECK_RC_RETV(fsPerfManyTreeRecursiveDirCreator(strlen(g_szDir), 0), VINF_SUCCESS);
1939
1940 /* Create the zero byte files: */
1941 fsPerfYield();
1942 uint64_t const nsStart2 = RTTimeNanoTS();
1943 uint32_t cFiles = 0;
1944 PFSPERFNAMEENTRY pCur;
1945 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry)
1946 {
1947 char szPath[FSPERF_MAX_PATH];
1948 memcpy(szPath, pCur->szName, pCur->cchName);
1949 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++)
1950 {
1951 RTStrFormatU32(&szPath[pCur->cchName], sizeof(szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD);
1952 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, szPath, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1953 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
1954 cFiles++;
1955 }
1956 }
1957 uint64_t const cNsElapsed2 = RTTimeNanoTS() - nsStart2;
1958 RTTestIValueF(cNsElapsed2, RTTESTUNIT_NS, "Creating %u empty files in tree", cFiles);
1959 RTTestIValueF(cNsElapsed2 / cFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Create empty file (tree)");
1960 RTTESTI_CHECK(g_cManyTreeFiles == cFiles);
1961}
1962
1963
1964DECL_FORCE_INLINE(int) fsPerfOpenExistingOnceReadonly(const char *pszFile)
1965{
1966 RTFILE hFile;
1967 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS, rcCheck);
1968 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
1969 return VINF_SUCCESS;
1970}
1971
1972
1973DECL_FORCE_INLINE(int) fsPerfOpenExistingOnceWriteonly(const char *pszFile)
1974{
1975 RTFILE hFile;
1976 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS, rcCheck);
1977 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
1978 return VINF_SUCCESS;
1979}
1980
1981
1982/** @note tstRTFileOpenEx-1.cpp has a copy of this code. */
1983static void tstOpenExTest(unsigned uLine, int cbExist, int cbNext, const char *pszFilename, uint64_t fAction,
1984 int rcExpect, RTFILEACTION enmActionExpected)
1985{
1986 uint64_t const fCreateMode = (0644 << RTFILE_O_CREATE_MODE_SHIFT);
1987 RTFILE hFile;
1988 int rc;
1989
1990 /*
1991 * File existence and size.
1992 */
1993 bool fOkay = false;
1994 RTFSOBJINFO ObjInfo;
1995 rc = RTPathQueryInfoEx(pszFilename, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1996 if (RT_SUCCESS(rc))
1997 fOkay = cbExist == (int64_t)ObjInfo.cbObject;
1998 else
1999 fOkay = rc == VERR_FILE_NOT_FOUND && cbExist < 0;
2000 if (!fOkay)
2001 {
2002 if (cbExist >= 0)
2003 {
2004 rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | fCreateMode);
2005 if (RT_SUCCESS(rc))
2006 {
2007 while (cbExist > 0)
2008 {
2009 int cbToWrite = (int)strlen(pszFilename);
2010 if (cbToWrite > cbExist)
2011 cbToWrite = cbExist;
2012 rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
2013 if (RT_FAILURE(rc))
2014 {
2015 RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
2016 break;
2017 }
2018 cbExist -= cbToWrite;
2019 }
2020
2021 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
2022 }
2023 else
2024 RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
2025
2026 }
2027 else
2028 {
2029 rc = RTFileDelete(pszFilename);
2030 if (rc != VINF_SUCCESS && rc != VERR_FILE_NOT_FOUND)
2031 RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
2032 }
2033 }
2034
2035 /*
2036 * The actual test.
2037 */
2038 RTFILEACTION enmActuallyTaken = RTFILEACTION_END;
2039 hFile = NIL_RTFILE;
2040 rc = RTFileOpenEx(pszFilename, fAction | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | fCreateMode, &hFile, &enmActuallyTaken);
2041 if ( rc != rcExpect
2042 || enmActuallyTaken != enmActionExpected
2043 || (RT_SUCCESS(rc) ? hFile == NIL_RTFILE : hFile != NIL_RTFILE))
2044 RTTestIFailed("%u: RTFileOpenEx(%s, %#llx) -> %Rrc + %d (hFile=%p), expected %Rrc + %d\n",
2045 uLine, pszFilename, fAction, rc, enmActuallyTaken, hFile, rcExpect, enmActionExpected);
2046 if (RT_SUCCESS(rc))
2047 {
2048 if ( enmActionExpected == RTFILEACTION_REPLACED
2049 || enmActionExpected == RTFILEACTION_TRUNCATED)
2050 {
2051 uint8_t abBuf[16];
2052 rc = RTFileRead(hFile, abBuf, 1, NULL);
2053 if (rc != VERR_EOF)
2054 RTTestIFailed("%u: RTFileRead(%s,,1,) -> %Rrc, expected VERR_EOF\n", uLine, pszFilename, rc);
2055 }
2056
2057 while (cbNext > 0)
2058 {
2059 int cbToWrite = (int)strlen(pszFilename);
2060 if (cbToWrite > cbNext)
2061 cbToWrite = cbNext;
2062 rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
2063 if (RT_FAILURE(rc))
2064 {
2065 RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
2066 break;
2067 }
2068 cbNext -= cbToWrite;
2069 }
2070
2071 rc = RTFileClose(hFile);
2072 if (RT_FAILURE(rc))
2073 RTTestIFailed("%u: RTFileClose(%p) -> %Rrc\n", uLine, hFile, rc);
2074 }
2075}
2076
2077
2078static void fsPerfOpen(void)
2079{
2080 RTTestISub("open");
2081
2082 /* Opening non-existing files. */
2083 RTFILE hFile;
2084 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InEmptyDir(RT_STR_TUPLE("no-such-file")),
2085 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_FILE_NOT_FOUND);
2086 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
2087 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), FSPERF_VERR_PATH_NOT_FOUND);
2088 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
2089 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_PATH_NOT_FOUND);
2090
2091 /*
2092 * The following is copied from tstRTFileOpenEx-1.cpp:
2093 */
2094 InDir(RT_STR_TUPLE("file1"));
2095 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN, VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
2096 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN_CREATE, VINF_SUCCESS, RTFILEACTION_CREATED);
2097 tstOpenExTest(__LINE__, 0, 0, g_szDir, RTFILE_O_OPEN_CREATE, VINF_SUCCESS, RTFILEACTION_OPENED);
2098 tstOpenExTest(__LINE__, 0, 0, g_szDir, RTFILE_O_OPEN, VINF_SUCCESS, RTFILEACTION_OPENED);
2099
2100 tstOpenExTest(__LINE__, 0, 0, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
2101 tstOpenExTest(__LINE__, 0, 10, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
2102 tstOpenExTest(__LINE__, 10, 10, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
2103 tstOpenExTest(__LINE__, 10, -1, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
2104 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
2105 tstOpenExTest(__LINE__, -1, 0, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
2106
2107 tstOpenExTest(__LINE__, 0, -1, g_szDir, RTFILE_O_CREATE_REPLACE, VINF_SUCCESS, RTFILEACTION_REPLACED);
2108 tstOpenExTest(__LINE__, -1, 0, g_szDir, RTFILE_O_CREATE_REPLACE, VINF_SUCCESS, RTFILEACTION_CREATED);
2109 tstOpenExTest(__LINE__, 0, -1, g_szDir, RTFILE_O_CREATE, VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
2110 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_CREATE, VINF_SUCCESS, RTFILEACTION_CREATED);
2111
2112 tstOpenExTest(__LINE__, -1, 10, g_szDir, RTFILE_O_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
2113 tstOpenExTest(__LINE__, 10, 10, g_szDir, RTFILE_O_CREATE | RTFILE_O_TRUNCATE, VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
2114 tstOpenExTest(__LINE__, 10, -1, g_szDir, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_REPLACED);
2115 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
2116
2117 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
2118
2119 /*
2120 * Create file1 and then try exclusivly creating it again.
2121 * Then profile opening it for reading.
2122 */
2123 RTFILE hFile1;
2124 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file1")),
2125 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2126 RTTESTI_CHECK_RC(RTFileOpen(&hFile, g_szDir, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VERR_ALREADY_EXISTS);
2127 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2128
2129 PROFILE_FN(fsPerfOpenExistingOnceReadonly(g_szDir), g_nsTestRun, "RTFileOpen/Close/Readonly");
2130 PROFILE_FN(fsPerfOpenExistingOnceWriteonly(g_szDir), g_nsTestRun, "RTFileOpen/Close/Writeonly");
2131
2132 /*
2133 * Profile opening in the deep directory too.
2134 */
2135 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file1")),
2136 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2137 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2138 PROFILE_FN(fsPerfOpenExistingOnceReadonly(g_szDeepDir), g_nsTestRun, "RTFileOpen/Close/deep/readonly");
2139 PROFILE_FN(fsPerfOpenExistingOnceWriteonly(g_szDeepDir), g_nsTestRun, "RTFileOpen/Close/deep/writeonly");
2140
2141 /* Manytree: */
2142 char szPath[FSPERF_MAX_PATH];
2143 PROFILE_MANYTREE_FN(szPath, fsPerfOpenExistingOnceReadonly(szPath), 1, g_nsTestRun, "RTFileOpen/Close/manytree/readonly");
2144}
2145
2146
2147static void fsPerfFStat(void)
2148{
2149 RTTestISub("fstat");
2150 RTFILE hFile1;
2151 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file2")),
2152 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2153 RTFSOBJINFO ObjInfo = {0};
2154 PROFILE_FN(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_NOTHING), g_nsTestRun, "RTFileQueryInfo/NOTHING");
2155 PROFILE_FN(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_UNIX), g_nsTestRun, "RTFileQueryInfo/UNIX");
2156
2157 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2158}
2159
2160#ifdef RT_OS_WINDOWS
2161/**
2162 * Nt(Query|Set|QueryDir)Information(File|) information class info.
2163 */
2164static const struct
2165{
2166 const char *pszName;
2167 int enmValue;
2168 bool fQuery;
2169 bool fSet;
2170 bool fQueryDir;
2171 uint8_t cbMin;
2172} g_aNtQueryInfoFileClasses[] =
2173{
2174#define E(a_enmValue, a_fQuery, a_fSet, a_fQueryDir, a_cbMin) \
2175 { #a_enmValue, a_enmValue, a_fQuery, a_fSet, a_fQueryDir, a_cbMin }
2176 { "invalid0", 0, false, false, false, 0 },
2177 E(FileDirectoryInformation, false, false, true, sizeof(FILE_DIRECTORY_INFORMATION)), // 0x00, 0x00, 0x48
2178 E(FileFullDirectoryInformation, false, false, true, sizeof(FILE_FULL_DIR_INFORMATION)), // 0x00, 0x00, 0x48
2179 E(FileBothDirectoryInformation, false, false, true, sizeof(FILE_BOTH_DIR_INFORMATION)), // 0x00, 0x00, 0x60
2180 E(FileBasicInformation, true, true, false, sizeof(FILE_BASIC_INFORMATION)),
2181 E(FileStandardInformation, true, false, false, sizeof(FILE_STANDARD_INFORMATION)),
2182 E(FileInternalInformation, true, false, false, sizeof(FILE_INTERNAL_INFORMATION)),
2183 E(FileEaInformation, true, false, false, sizeof(FILE_EA_INFORMATION)),
2184 E(FileAccessInformation, true, false, false, sizeof(FILE_ACCESS_INFORMATION)),
2185 E(FileNameInformation, true, false, false, sizeof(FILE_NAME_INFORMATION)),
2186 E(FileRenameInformation, false, true, false, sizeof(FILE_RENAME_INFORMATION)),
2187 E(FileLinkInformation, false, true, false, sizeof(FILE_LINK_INFORMATION)),
2188 E(FileNamesInformation, false, false, true, sizeof(FILE_NAMES_INFORMATION)), // 0x00, 0x00, 0x10
2189 E(FileDispositionInformation, false, true, false, sizeof(FILE_DISPOSITION_INFORMATION)), // 0x00, 0x01,
2190 E(FilePositionInformation, true, true, false, sizeof(FILE_POSITION_INFORMATION)), // 0x08, 0x08,
2191 E(FileFullEaInformation, false, false, false, sizeof(FILE_FULL_EA_INFORMATION)), // 0x00, 0x00,
2192 E(FileModeInformation, true, true, false, sizeof(FILE_MODE_INFORMATION)), // 0x04, 0x04,
2193 E(FileAlignmentInformation, true, false, false, sizeof(FILE_ALIGNMENT_INFORMATION)), // 0x04, 0x00,
2194 E(FileAllInformation, true, false, false, sizeof(FILE_ALL_INFORMATION)), // 0x68, 0x00,
2195 E(FileAllocationInformation, false, true, false, sizeof(FILE_ALLOCATION_INFORMATION)), // 0x00, 0x08,
2196 E(FileEndOfFileInformation, false, true, false, sizeof(FILE_END_OF_FILE_INFORMATION)), // 0x00, 0x08,
2197 E(FileAlternateNameInformation, true, false, false, sizeof(FILE_NAME_INFORMATION)), // 0x08, 0x00,
2198 E(FileStreamInformation, true, false, false, sizeof(FILE_STREAM_INFORMATION)), // 0x20, 0x00,
2199 E(FilePipeInformation, true, true, false, sizeof(FILE_PIPE_INFORMATION)), // 0x08, 0x08,
2200 E(FilePipeLocalInformation, true, false, false, sizeof(FILE_PIPE_LOCAL_INFORMATION)), // 0x28, 0x00,
2201 E(FilePipeRemoteInformation, true, true, false, sizeof(FILE_PIPE_REMOTE_INFORMATION)), // 0x10, 0x10,
2202 E(FileMailslotQueryInformation, true, false, false, sizeof(FILE_MAILSLOT_QUERY_INFORMATION)), // 0x18, 0x00,
2203 E(FileMailslotSetInformation, false, true, false, sizeof(FILE_MAILSLOT_SET_INFORMATION)), // 0x00, 0x08,
2204 E(FileCompressionInformation, true, false, false, sizeof(FILE_COMPRESSION_INFORMATION)), // 0x10, 0x00,
2205 E(FileObjectIdInformation, true, true, true, sizeof(FILE_OBJECTID_INFORMATION)), // 0x48, 0x48,
2206 E(FileCompletionInformation, false, true, false, sizeof(FILE_COMPLETION_INFORMATION)), // 0x00, 0x10,
2207 E(FileMoveClusterInformation, false, true, false, sizeof(FILE_MOVE_CLUSTER_INFORMATION)), // 0x00, 0x18,
2208 E(FileQuotaInformation, true, true, true, sizeof(FILE_QUOTA_INFORMATION)), // 0x38, 0x38, 0x38
2209 E(FileReparsePointInformation, true, false, true, sizeof(FILE_REPARSE_POINT_INFORMATION)), // 0x10, 0x00, 0x10
2210 E(FileNetworkOpenInformation, true, false, false, sizeof(FILE_NETWORK_OPEN_INFORMATION)), // 0x38, 0x00,
2211 E(FileAttributeTagInformation, true, false, false, sizeof(FILE_ATTRIBUTE_TAG_INFORMATION)), // 0x08, 0x00,
2212 E(FileTrackingInformation, false, true, false, sizeof(FILE_TRACKING_INFORMATION)), // 0x00, 0x10,
2213 E(FileIdBothDirectoryInformation, false, false, true, sizeof(FILE_ID_BOTH_DIR_INFORMATION)), // 0x00, 0x00, 0x70
2214 E(FileIdFullDirectoryInformation, false, false, true, sizeof(FILE_ID_FULL_DIR_INFORMATION)), // 0x00, 0x00, 0x58
2215 E(FileValidDataLengthInformation, false, true, false, sizeof(FILE_VALID_DATA_LENGTH_INFORMATION)), // 0x00, 0x08,
2216 E(FileShortNameInformation, false, true, false, sizeof(FILE_NAME_INFORMATION)), // 0x00, 0x08,
2217 E(FileIoCompletionNotificationInformation, true, true, false, sizeof(FILE_IO_COMPLETION_NOTIFICATION_INFORMATION)), // 0x04, 0x04,
2218 E(FileIoStatusBlockRangeInformation, false, true, false, sizeof(IO_STATUS_BLOCK) /*?*/), // 0x00, 0x10,
2219 E(FileIoPriorityHintInformation, true, true, false, sizeof(FILE_IO_PRIORITY_HINT_INFORMATION)), // 0x04, 0x04,
2220 E(FileSfioReserveInformation, true, true, false, sizeof(FILE_SFIO_RESERVE_INFORMATION)), // 0x14, 0x14,
2221 E(FileSfioVolumeInformation, true, false, false, sizeof(FILE_SFIO_VOLUME_INFORMATION)), // 0x0C, 0x00,
2222 E(FileHardLinkInformation, true, false, false, sizeof(FILE_LINKS_INFORMATION)), // 0x20, 0x00,
2223 E(FileProcessIdsUsingFileInformation, true, false, false, sizeof(FILE_PROCESS_IDS_USING_FILE_INFORMATION)), // 0x10, 0x00,
2224 E(FileNormalizedNameInformation, true, false, false, sizeof(FILE_NAME_INFORMATION)), // 0x08, 0x00,
2225 E(FileNetworkPhysicalNameInformation, true, false, false, sizeof(FILE_NETWORK_PHYSICAL_NAME_INFORMATION)), // 0x08, 0x00,
2226 E(FileIdGlobalTxDirectoryInformation, false, false, true, sizeof(FILE_ID_GLOBAL_TX_DIR_INFORMATION)), // 0x00, 0x00, 0x60
2227 E(FileIsRemoteDeviceInformation, true, false, false, sizeof(FILE_IS_REMOTE_DEVICE_INFORMATION)), // 0x01, 0x00,
2228 E(FileUnusedInformation, false, false, false, 0), // 0x00, 0x00,
2229 E(FileNumaNodeInformation, true, false, false, sizeof(FILE_NUMA_NODE_INFORMATION)), // 0x02, 0x00,
2230 E(FileStandardLinkInformation, true, false, false, sizeof(FILE_STANDARD_LINK_INFORMATION)), // 0x0C, 0x00,
2231 E(FileRemoteProtocolInformation, true, false, false, sizeof(FILE_REMOTE_PROTOCOL_INFORMATION)), // 0x74, 0x00,
2232 E(FileRenameInformationBypassAccessCheck, false, false, false, 0 /*kernel mode only*/), // 0x00, 0x00,
2233 E(FileLinkInformationBypassAccessCheck, false, false, false, 0 /*kernel mode only*/), // 0x00, 0x00,
2234 E(FileVolumeNameInformation, true, false, false, sizeof(FILE_VOLUME_NAME_INFORMATION)), // 0x08, 0x00,
2235 E(FileIdInformation, true, false, false, sizeof(FILE_ID_INFORMATION)), // 0x18, 0x00,
2236 E(FileIdExtdDirectoryInformation, false, false, true, sizeof(FILE_ID_EXTD_DIR_INFORMATION)), // 0x00, 0x00, 0x60
2237 E(FileReplaceCompletionInformation, false, true, false, sizeof(FILE_COMPLETION_INFORMATION)), // 0x00, 0x10,
2238 E(FileHardLinkFullIdInformation, true, false, false, sizeof(FILE_LINK_ENTRY_FULL_ID_INFORMATION)), // 0x24, 0x00,
2239 E(FileIdExtdBothDirectoryInformation, false, false, true, sizeof(FILE_ID_EXTD_BOTH_DIR_INFORMATION)), // 0x00, 0x00, 0x78
2240 E(FileDispositionInformationEx, false, true, false, sizeof(FILE_DISPOSITION_INFORMATION_EX)), // 0x00, 0x04,
2241 E(FileRenameInformationEx, false, true, false, sizeof(FILE_RENAME_INFORMATION)), // 0x00, 0x18,
2242 E(FileRenameInformationExBypassAccessCheck, false, false, false, 0 /*kernel mode only*/), // 0x00, 0x00,
2243 E(FileDesiredStorageClassInformation, true, true, false, sizeof(FILE_DESIRED_STORAGE_CLASS_INFORMATION)), // 0x08, 0x08,
2244 E(FileStatInformation, true, false, false, sizeof(FILE_STAT_INFORMATION)), // 0x48, 0x00,
2245 E(FileMemoryPartitionInformation, false, true, false, 0x10), // 0x00, 0x10,
2246 E(FileStatLxInformation, true, false, false, sizeof(FILE_STAT_LX_INFORMATION)), // 0x60, 0x00,
2247 E(FileCaseSensitiveInformation, true, true, false, sizeof(FILE_CASE_SENSITIVE_INFORMATION)), // 0x04, 0x04,
2248 E(FileLinkInformationEx, false, true, false, sizeof(FILE_LINK_INFORMATION)), // 0x00, 0x18,
2249 E(FileLinkInformationExBypassAccessCheck, false, false, false, 0 /*kernel mode only*/), // 0x00, 0x00,
2250 E(FileStorageReserveIdInformation, true, true, false, 0x04), // 0x04, 0x04,
2251 E(FileCaseSensitiveInformationForceAccessCheck, true, true, false, sizeof(FILE_CASE_SENSITIVE_INFORMATION)), // 0x04, 0x04,
2252#undef E
2253};
2254
2255void fsPerfNtQueryInfoFileWorker(HANDLE hNtFile1, uint32_t fType)
2256{
2257 char const chType = fType == RTFS_TYPE_DIRECTORY ? 'd' : 'r';
2258
2259 /** @todo may run out of buffer for really long paths? */
2260 union
2261 {
2262 uint8_t ab[4096];
2263 FILE_ACCESS_INFORMATION Access;
2264 FILE_ALIGNMENT_INFORMATION Align;
2265 FILE_ALL_INFORMATION All;
2266 FILE_ALLOCATION_INFORMATION Alloc;
2267 FILE_ATTRIBUTE_TAG_INFORMATION AttribTag;
2268 FILE_BASIC_INFORMATION Basic;
2269 FILE_BOTH_DIR_INFORMATION BothDir;
2270 FILE_CASE_SENSITIVE_INFORMATION CaseSensitivity;
2271 FILE_COMPLETION_INFORMATION Completion;
2272 FILE_COMPRESSION_INFORMATION Compression;
2273 FILE_DESIRED_STORAGE_CLASS_INFORMATION StorageClass;
2274 FILE_DIRECTORY_INFORMATION Dir;
2275 FILE_DISPOSITION_INFORMATION Disp;
2276 FILE_DISPOSITION_INFORMATION_EX DispEx;
2277 FILE_EA_INFORMATION Ea;
2278 FILE_END_OF_FILE_INFORMATION EndOfFile;
2279 FILE_FULL_DIR_INFORMATION FullDir;
2280 FILE_FULL_EA_INFORMATION FullEa;
2281 FILE_ID_BOTH_DIR_INFORMATION IdBothDir;
2282 FILE_ID_EXTD_BOTH_DIR_INFORMATION ExtIdBothDir;
2283 FILE_ID_EXTD_DIR_INFORMATION ExtIdDir;
2284 FILE_ID_FULL_DIR_INFORMATION IdFullDir;
2285 FILE_ID_GLOBAL_TX_DIR_INFORMATION IdGlobalTx;
2286 FILE_ID_INFORMATION IdInfo;
2287 FILE_INTERNAL_INFORMATION Internal;
2288 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION IoCompletion;
2289 FILE_IO_PRIORITY_HINT_INFORMATION IoPrioHint;
2290 FILE_IS_REMOTE_DEVICE_INFORMATION IsRemoteDev;
2291 FILE_LINK_ENTRY_FULL_ID_INFORMATION LinkFullId;
2292 FILE_LINK_INFORMATION Link;
2293 FILE_MAILSLOT_QUERY_INFORMATION MailslotQuery;
2294 FILE_MAILSLOT_SET_INFORMATION MailslotSet;
2295 FILE_MODE_INFORMATION Mode;
2296 FILE_MOVE_CLUSTER_INFORMATION MoveCluster;
2297 FILE_NAME_INFORMATION Name;
2298 FILE_NAMES_INFORMATION Names;
2299 FILE_NETWORK_OPEN_INFORMATION NetOpen;
2300 FILE_NUMA_NODE_INFORMATION Numa;
2301 FILE_OBJECTID_INFORMATION ObjId;
2302 FILE_PIPE_INFORMATION Pipe;
2303 FILE_PIPE_LOCAL_INFORMATION PipeLocal;
2304 FILE_PIPE_REMOTE_INFORMATION PipeRemote;
2305 FILE_POSITION_INFORMATION Pos;
2306 FILE_PROCESS_IDS_USING_FILE_INFORMATION Pids;
2307 FILE_QUOTA_INFORMATION Quota;
2308 FILE_REMOTE_PROTOCOL_INFORMATION RemoteProt;
2309 FILE_RENAME_INFORMATION Rename;
2310 FILE_REPARSE_POINT_INFORMATION Reparse;
2311 FILE_SFIO_RESERVE_INFORMATION SfiRes;
2312 FILE_SFIO_VOLUME_INFORMATION SfioVol;
2313 FILE_STANDARD_INFORMATION Std;
2314 FILE_STANDARD_LINK_INFORMATION StdLink;
2315 FILE_STAT_INFORMATION Stat;
2316 FILE_STAT_LX_INFORMATION StatLx;
2317 FILE_STREAM_INFORMATION Stream;
2318 FILE_TRACKING_INFORMATION Tracking;
2319 FILE_VALID_DATA_LENGTH_INFORMATION ValidDataLen;
2320 FILE_VOLUME_NAME_INFORMATION VolName;
2321 } uBuf;
2322
2323 IO_STATUS_BLOCK const VirginIos = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2324 for (unsigned i = 0; i < RT_ELEMENTS(g_aNtQueryInfoFileClasses); i++)
2325 {
2326 FILE_INFORMATION_CLASS const enmClass = (FILE_INFORMATION_CLASS)g_aNtQueryInfoFileClasses[i].enmValue;
2327 const char * const pszClass = g_aNtQueryInfoFileClasses[i].pszName;
2328
2329 memset(&uBuf, 0xff, sizeof(uBuf));
2330 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2331 ULONG cbBuf = sizeof(uBuf);
2332 NTSTATUS rcNt = NtQueryInformationFile(hNtFile1, &Ios, &uBuf, cbBuf, enmClass);
2333 if (NT_SUCCESS(rcNt))
2334 {
2335 if (Ios.Status == VirginIos.Status || Ios.Information == VirginIos.Information)
2336 RTTestIFailed("%s/%#x: I/O status block was not modified: %#x %#zx", pszClass, cbBuf, Ios.Status, Ios.Information);
2337 else if (!g_aNtQueryInfoFileClasses[i].fQuery)
2338 RTTestIFailed("%s/%#x: This isn't supposed to be queriable! (rcNt=%#x)", pszClass, cbBuf, rcNt);
2339 else
2340 {
2341 ULONG const cbActualMin = enmClass != FileStorageReserveIdInformation ? Ios.Information : 4; /* weird */
2342
2343 switch (enmClass)
2344 {
2345 case FileNameInformation:
2346 case FileAlternateNameInformation:
2347 case FileShortNameInformation:
2348 case FileNormalizedNameInformation:
2349 case FileNetworkPhysicalNameInformation:
2350 if ( RT_UOFFSETOF_DYN(FILE_NAME_INFORMATION, FileName[uBuf.Name.FileNameLength / sizeof(WCHAR)])
2351 != cbActualMin)
2352 RTTestIFailed("%s/%#x: Wrong FileNameLength=%#x vs cbActual=%#x",
2353 pszClass, cbActualMin, uBuf.Name.FileNameLength, cbActualMin);
2354 if (uBuf.Name.FileName[uBuf.Name.FileNameLength / sizeof(WCHAR) - 1] == '\0')
2355 RTTestIFailed("%s/%#x: Zero terminated name!", pszClass, cbActualMin);
2356 if (g_uVerbosity > 1)
2357 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#x: FileNameLength=%#x FileName='%.*ls'\n",
2358 pszClass, cbActualMin, uBuf.Name.FileNameLength,
2359 uBuf.Name.FileNameLength / sizeof(WCHAR), uBuf.Name.FileName);
2360 break;
2361
2362 case FileVolumeNameInformation:
2363 if (RT_UOFFSETOF_DYN(FILE_VOLUME_NAME_INFORMATION,
2364 DeviceName[uBuf.VolName.DeviceNameLength / sizeof(WCHAR)]) != cbActualMin)
2365 RTTestIFailed("%s/%#x: Wrong DeviceNameLength=%#x vs cbActual=%#x",
2366 pszClass, cbActualMin, uBuf.VolName.DeviceNameLength, cbActualMin);
2367 if (uBuf.VolName.DeviceName[uBuf.VolName.DeviceNameLength / sizeof(WCHAR) - 1] == '\0')
2368 RTTestIFailed("%s/%#x: Zero terminated name!", pszClass, cbActualMin);
2369 if (g_uVerbosity > 1)
2370 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#x: DeviceNameLength=%#x DeviceName='%.*ls'\n",
2371 pszClass, cbActualMin, uBuf.VolName.DeviceNameLength,
2372 uBuf.VolName.DeviceNameLength / sizeof(WCHAR), uBuf.VolName.DeviceName);
2373 break;
2374 default:
2375 break;
2376 }
2377
2378 ULONG const cbMin = g_aNtQueryInfoFileClasses[i].cbMin;
2379 ULONG const cbMax = RT_MIN(cbActualMin + 64, sizeof(uBuf));
2380 for (cbBuf = 0; cbBuf < cbMax; cbBuf++)
2381 {
2382 memset(&uBuf, 0xfe, sizeof(uBuf));
2383 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2384 rcNt = NtQueryInformationFile(hNtFile1, &Ios, &uBuf, cbBuf, enmClass);
2385 if (!ASMMemIsAllU8(&uBuf.ab[cbBuf], sizeof(uBuf) - cbBuf, 0xfe))
2386 RTTestIFailed("%s/%#x: Touched memory beyond end of buffer (rcNt=%#x)", pszClass, cbBuf, rcNt);
2387 if (cbBuf < cbMin)
2388 {
2389 if (rcNt != STATUS_INFO_LENGTH_MISMATCH)
2390 RTTestIFailed("%s/%#x: %#x, expected STATUS_INFO_LENGTH_MISMATCH", pszClass, cbBuf, rcNt);
2391 if (Ios.Status != VirginIos.Status || Ios.Information != VirginIos.Information)
2392 RTTestIFailed("%s/%#x: I/O status block was modified (STATUS_INFO_LENGTH_MISMATCH): %#x %#zx",
2393 pszClass, cbBuf, Ios.Status, Ios.Information);
2394 }
2395 else if (cbBuf < cbActualMin)
2396 {
2397 if ( rcNt != STATUS_BUFFER_OVERFLOW
2398 /* RDR2/w10 returns success if the buffer can hold exactly the share name: */
2399 && !( rcNt == STATUS_SUCCESS
2400 && enmClass == FileNetworkPhysicalNameInformation)
2401 )
2402 RTTestIFailed("%s/%#x: %#x, expected STATUS_BUFFER_OVERFLOW", pszClass, cbBuf, rcNt);
2403 /** @todo check name and length fields */
2404 }
2405 else
2406 {
2407 if ( !ASMMemIsAllU8(&uBuf.ab[cbActualMin], sizeof(uBuf) - cbActualMin, 0xfe)
2408 && enmClass != FileStorageReserveIdInformation /* NTFS bug? */ )
2409 RTTestIFailed("%s/%#x: Touched memory beyond returned length (cbActualMin=%#x, rcNt=%#x)",
2410 pszClass, cbBuf, cbActualMin, rcNt);
2411
2412 }
2413 }
2414 }
2415 }
2416 else
2417 {
2418 if (!g_aNtQueryInfoFileClasses[i].fQuery)
2419 {
2420 if ( rcNt != STATUS_INVALID_INFO_CLASS
2421 && ( rcNt != STATUS_INVALID_PARAMETER /* w7rtm-32 result */
2422 || enmClass != FileUnusedInformation))
2423 RTTestIFailed("%s/%#x/%c: %#x, expected STATUS_INVALID_INFO_CLASS", pszClass, cbBuf, chType, rcNt);
2424 }
2425 else if ( rcNt != STATUS_INVALID_INFO_CLASS
2426 && rcNt != STATUS_INVALID_PARAMETER
2427 && !(rcNt == STATUS_OBJECT_NAME_NOT_FOUND && enmClass == FileAlternateNameInformation)
2428 && !( rcNt == STATUS_ACCESS_DENIED
2429 && ( enmClass == FileIoPriorityHintInformation
2430 || enmClass == FileSfioReserveInformation
2431 || enmClass == FileStatLxInformation))
2432 && !(rcNt == STATUS_NO_SUCH_DEVICE && enmClass == FileNumaNodeInformation)
2433 && !( rcNt == STATUS_NOT_SUPPORTED /* RDR2/W10-17763 */
2434 && ( enmClass == FileMailslotQueryInformation
2435 || enmClass == FileObjectIdInformation
2436 || enmClass == FileReparsePointInformation
2437 || enmClass == FileSfioVolumeInformation
2438 || enmClass == FileHardLinkInformation
2439 || enmClass == FileStandardLinkInformation
2440 || enmClass == FileHardLinkFullIdInformation
2441 || enmClass == FileDesiredStorageClassInformation
2442 || enmClass == FileStatInformation
2443 || enmClass == FileCaseSensitiveInformation
2444 || enmClass == FileStorageReserveIdInformation
2445 || enmClass == FileCaseSensitiveInformationForceAccessCheck)
2446 || ( fType == RTFS_TYPE_DIRECTORY
2447 && (enmClass == FileSfioReserveInformation || enmClass == FileStatLxInformation)))
2448 && !(rcNt == STATUS_INVALID_DEVICE_REQUEST && fType == RTFS_TYPE_FILE)
2449 )
2450 RTTestIFailed("%s/%#x/%c: %#x", pszClass, cbBuf, chType, rcNt);
2451 if ( (Ios.Status != VirginIos.Status || Ios.Information != VirginIos.Information)
2452 && !(fType == RTFS_TYPE_DIRECTORY && Ios.Status == rcNt && Ios.Information == 0) /* NTFS/W10-17763 */
2453 && !( enmClass == FileUnusedInformation
2454 && Ios.Status == rcNt && Ios.Information == sizeof(uBuf)) /* NTFS/VBoxSF/w7rtm */ )
2455 RTTestIFailed("%s/%#x/%c: I/O status block was modified: %#x %#zx",
2456 pszClass, cbBuf, chType, Ios.Status, Ios.Information);
2457 if (!ASMMemIsAllU8(&uBuf, sizeof(uBuf), 0xff))
2458 RTTestIFailed("%s/%#x/%c: Buffer was touched in failure case!", pszClass, cbBuf, chType);
2459 }
2460 }
2461}
2462
2463void fsPerfNtQueryInfoFile(void)
2464{
2465 RTTestISub("NtQueryInformationFile");
2466
2467 /* On a regular file: */
2468 RTFILE hFile1;
2469 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file2qif")),
2470 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
2471 fsPerfNtQueryInfoFileWorker((HANDLE)RTFileToNative(hFile1), RTFS_TYPE_FILE);
2472 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2473
2474 /* On a directory: */
2475 HANDLE hDir1 = INVALID_HANDLE_VALUE;
2476 RTTESTI_CHECK_RC_RETV(RTNtPathOpenDir(InDir(RT_STR_TUPLE("")), GENERIC_READ | SYNCHRONIZE | FILE_SYNCHRONOUS_IO_NONALERT,
2477 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
2478 FILE_OPEN, 0, &hDir1, NULL), VINF_SUCCESS);
2479 fsPerfNtQueryInfoFileWorker(hDir1, RTFS_TYPE_DIRECTORY);
2480 RTTESTI_CHECK(CloseHandle(hDir1) == TRUE);
2481}
2482
2483
2484/**
2485 * Nt(Query|Set)VolumeInformationFile) information class info.
2486 */
2487static const struct
2488{
2489 const char *pszName;
2490 int enmValue;
2491 bool fQuery;
2492 bool fSet;
2493 uint8_t cbMin;
2494} g_aNtQueryVolInfoFileClasses[] =
2495{
2496#define E(a_enmValue, a_fQuery, a_fSet, a_cbMin) \
2497 { #a_enmValue, a_enmValue, a_fQuery, a_fSet, a_cbMin }
2498 { "invalid0", 0, false, false, 0 },
2499 E(FileFsVolumeInformation, 1, 0, sizeof(FILE_FS_VOLUME_INFORMATION)),
2500 E(FileFsLabelInformation, 0, 1, sizeof(FILE_FS_LABEL_INFORMATION)),
2501 E(FileFsSizeInformation, 1, 0, sizeof(FILE_FS_SIZE_INFORMATION)),
2502 E(FileFsDeviceInformation, 1, 0, sizeof(FILE_FS_DEVICE_INFORMATION)),
2503 E(FileFsAttributeInformation, 1, 0, sizeof(FILE_FS_ATTRIBUTE_INFORMATION)),
2504 E(FileFsControlInformation, 1, 1, sizeof(FILE_FS_CONTROL_INFORMATION)),
2505 E(FileFsFullSizeInformation, 1, 0, sizeof(FILE_FS_FULL_SIZE_INFORMATION)),
2506 E(FileFsObjectIdInformation, 1, 1, sizeof(FILE_FS_OBJECTID_INFORMATION)),
2507 E(FileFsDriverPathInformation, 1, 0, sizeof(FILE_FS_DRIVER_PATH_INFORMATION)),
2508 E(FileFsVolumeFlagsInformation, 1, 1, sizeof(FILE_FS_VOLUME_FLAGS_INFORMATION)),
2509 E(FileFsSectorSizeInformation, 1, 0, sizeof(FILE_FS_SECTOR_SIZE_INFORMATION)),
2510 E(FileFsDataCopyInformation, 1, 0, sizeof(FILE_FS_DATA_COPY_INFORMATION)),
2511 E(FileFsMetadataSizeInformation, 1, 0, sizeof(FILE_FS_METADATA_SIZE_INFORMATION)),
2512 E(FileFsFullSizeInformationEx, 1, 0, sizeof(FILE_FS_FULL_SIZE_INFORMATION_EX)),
2513#undef E
2514};
2515
2516void fsPerfNtQueryVolInfoFileWorker(HANDLE hNtFile1, uint32_t fType)
2517{
2518 char const chType = fType == RTFS_TYPE_DIRECTORY ? 'd' : 'r';
2519 union
2520 {
2521 uint8_t ab[4096];
2522 FILE_FS_VOLUME_INFORMATION Vol;
2523 FILE_FS_LABEL_INFORMATION Label;
2524 FILE_FS_SIZE_INFORMATION Size;
2525 FILE_FS_DEVICE_INFORMATION Dev;
2526 FILE_FS_ATTRIBUTE_INFORMATION Attrib;
2527 FILE_FS_CONTROL_INFORMATION Ctrl;
2528 FILE_FS_FULL_SIZE_INFORMATION FullSize;
2529 FILE_FS_OBJECTID_INFORMATION ObjId;
2530 FILE_FS_DRIVER_PATH_INFORMATION DrvPath;
2531 FILE_FS_VOLUME_FLAGS_INFORMATION VolFlags;
2532 FILE_FS_SECTOR_SIZE_INFORMATION SectorSize;
2533 FILE_FS_DATA_COPY_INFORMATION DataCopy;
2534 FILE_FS_METADATA_SIZE_INFORMATION Metadata;
2535 FILE_FS_FULL_SIZE_INFORMATION_EX FullSizeEx;
2536 } uBuf;
2537
2538 IO_STATUS_BLOCK const VirginIos = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2539 for (unsigned i = 0; i < RT_ELEMENTS(g_aNtQueryVolInfoFileClasses); i++)
2540 {
2541 FS_INFORMATION_CLASS const enmClass = (FS_INFORMATION_CLASS)g_aNtQueryVolInfoFileClasses[i].enmValue;
2542 const char * const pszClass = g_aNtQueryVolInfoFileClasses[i].pszName;
2543
2544 memset(&uBuf, 0xff, sizeof(uBuf));
2545 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2546 ULONG cbBuf = sizeof(uBuf);
2547 NTSTATUS rcNt = NtQueryVolumeInformationFile(hNtFile1, &Ios, &uBuf, cbBuf, enmClass);
2548 if (g_uVerbosity > 3)
2549 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#04x/%c: rcNt=%#x Ios.Status=%#x Info=%#zx\n",
2550 pszClass, cbBuf, chType, rcNt, Ios.Status, Ios.Information);
2551 if (NT_SUCCESS(rcNt))
2552 {
2553 if (Ios.Status == VirginIos.Status || Ios.Information == VirginIos.Information)
2554 RTTestIFailed("%s/%#x/%c: I/O status block was not modified: %#x %#zx",
2555 pszClass, cbBuf, chType, Ios.Status, Ios.Information);
2556 else if (!g_aNtQueryVolInfoFileClasses[i].fQuery)
2557 RTTestIFailed("%s/%#x/%c: This isn't supposed to be queriable! (rcNt=%#x)", pszClass, cbBuf, chType, rcNt);
2558 else
2559 {
2560 ULONG const cbActualMin = Ios.Information;
2561 ULONG *pcbName = NULL;
2562 ULONG offName = 0;
2563
2564 switch (enmClass)
2565 {
2566 case FileFsVolumeInformation:
2567 pcbName = &uBuf.Vol.VolumeLabelLength;
2568 offName = RT_UOFFSETOF(FILE_FS_VOLUME_INFORMATION, VolumeLabel);
2569 if (RT_UOFFSETOF_DYN(FILE_FS_VOLUME_INFORMATION,
2570 VolumeLabel[uBuf.Vol.VolumeLabelLength / sizeof(WCHAR)]) != cbActualMin)
2571 RTTestIFailed("%s/%#x/%c: Wrong VolumeLabelLength=%#x vs cbActual=%#x",
2572 pszClass, cbActualMin, chType, uBuf.Vol.VolumeLabelLength, cbActualMin);
2573 if (uBuf.Vol.VolumeLabel[uBuf.Vol.VolumeLabelLength / sizeof(WCHAR) - 1] == '\0')
2574 RTTestIFailed("%s/%#x/%c: Zero terminated name!", pszClass, cbActualMin, chType);
2575 if (g_uVerbosity > 1)
2576 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#04x/%c: VolumeLabelLength=%#x VolumeLabel='%.*ls'\n",
2577 pszClass, cbActualMin, chType, uBuf.Vol.VolumeLabelLength,
2578 uBuf.Vol.VolumeLabelLength / sizeof(WCHAR), uBuf.Vol.VolumeLabel);
2579 break;
2580
2581 case FileFsAttributeInformation:
2582 pcbName = &uBuf.Attrib.FileSystemNameLength;
2583 offName = RT_UOFFSETOF(FILE_FS_ATTRIBUTE_INFORMATION, FileSystemName);
2584 if (RT_UOFFSETOF_DYN(FILE_FS_ATTRIBUTE_INFORMATION,
2585 FileSystemName[uBuf.Attrib.FileSystemNameLength / sizeof(WCHAR)]) != cbActualMin)
2586 RTTestIFailed("%s/%#x/%c: Wrong FileSystemNameLength=%#x vs cbActual=%#x",
2587 pszClass, cbActualMin, chType, uBuf.Attrib.FileSystemNameLength, cbActualMin);
2588 if (uBuf.Attrib.FileSystemName[uBuf.Attrib.FileSystemNameLength / sizeof(WCHAR) - 1] == '\0')
2589 RTTestIFailed("%s/%#x/%c: Zero terminated name!", pszClass, cbActualMin, chType);
2590 if (g_uVerbosity > 1)
2591 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#04x/%c: FileSystemNameLength=%#x FileSystemName='%.*ls' Attribs=%#x MaxCompName=%#x\n",
2592 pszClass, cbActualMin, chType, uBuf.Attrib.FileSystemNameLength,
2593 uBuf.Attrib.FileSystemNameLength / sizeof(WCHAR), uBuf.Attrib.FileSystemName,
2594 uBuf.Attrib.FileSystemAttributes, uBuf.Attrib.MaximumComponentNameLength);
2595 break;
2596
2597 case FileFsDriverPathInformation:
2598 pcbName = &uBuf.DrvPath.DriverNameLength;
2599 offName = RT_UOFFSETOF(FILE_FS_DRIVER_PATH_INFORMATION, DriverName);
2600 if (RT_UOFFSETOF_DYN(FILE_FS_DRIVER_PATH_INFORMATION,
2601 DriverName[uBuf.DrvPath.DriverNameLength / sizeof(WCHAR)]) != cbActualMin)
2602 RTTestIFailed("%s/%#x/%c: Wrong DriverNameLength=%#x vs cbActual=%#x",
2603 pszClass, cbActualMin, chType, uBuf.DrvPath.DriverNameLength, cbActualMin);
2604 if (uBuf.DrvPath.DriverName[uBuf.DrvPath.DriverNameLength / sizeof(WCHAR) - 1] == '\0')
2605 RTTestIFailed("%s/%#x/%c: Zero terminated name!", pszClass, cbActualMin, chType);
2606 if (g_uVerbosity > 1)
2607 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#04x/%c: DriverNameLength=%#x DriverName='%.*ls'\n",
2608 pszClass, cbActualMin, chType, uBuf.DrvPath.DriverNameLength,
2609 uBuf.DrvPath.DriverNameLength / sizeof(WCHAR), uBuf.DrvPath.DriverName);
2610 break;
2611
2612 case FileFsSectorSizeInformation:
2613 if (g_uVerbosity > 1)
2614 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#04x/%c: Flags=%#x log=%#x atomic=%#x perf=%#x eff=%#x offSec=%#x offPart=%#x\n",
2615 pszClass, cbActualMin, chType, uBuf.SectorSize.Flags,
2616 uBuf.SectorSize.LogicalBytesPerSector,
2617 uBuf.SectorSize.PhysicalBytesPerSectorForAtomicity,
2618 uBuf.SectorSize.PhysicalBytesPerSectorForPerformance,
2619 uBuf.SectorSize.FileSystemEffectivePhysicalBytesPerSectorForAtomicity,
2620 uBuf.SectorSize.ByteOffsetForSectorAlignment,
2621 uBuf.SectorSize.ByteOffsetForPartitionAlignment);
2622 break;
2623
2624 default:
2625 if (g_uVerbosity > 2)
2626 RTTestIPrintf(RTTESTLVL_ALWAYS, "%+34s/%#04x/%c:\n", pszClass, cbActualMin, chType);
2627 break;
2628 }
2629 ULONG const cbName = pcbName ? *pcbName : 0;
2630 uint8_t abNameCopy[4096];
2631 RT_ZERO(abNameCopy);
2632 if (pcbName)
2633 memcpy(abNameCopy, &uBuf.ab[offName], cbName);
2634
2635 ULONG const cbMin = g_aNtQueryVolInfoFileClasses[i].cbMin;
2636 ULONG const cbMax = RT_MIN(cbActualMin + 64, sizeof(uBuf));
2637 for (cbBuf = 0; cbBuf < cbMax; cbBuf++)
2638 {
2639 memset(&uBuf, 0xfe, sizeof(uBuf));
2640 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2641 rcNt = NtQueryVolumeInformationFile(hNtFile1, &Ios, &uBuf, cbBuf, enmClass);
2642 if (!ASMMemIsAllU8(&uBuf.ab[cbBuf], sizeof(uBuf) - cbBuf, 0xfe))
2643 RTTestIFailed("%s/%#x/%c: Touched memory beyond end of buffer (rcNt=%#x)", pszClass, cbBuf, chType, rcNt);
2644 if (cbBuf < cbMin)
2645 {
2646 if (rcNt != STATUS_INFO_LENGTH_MISMATCH)
2647 RTTestIFailed("%s/%#x/%c: %#x, expected STATUS_INFO_LENGTH_MISMATCH", pszClass, cbBuf, chType, rcNt);
2648 if (Ios.Status != VirginIos.Status || Ios.Information != VirginIos.Information)
2649 RTTestIFailed("%s/%#x/%c: I/O status block was modified (STATUS_INFO_LENGTH_MISMATCH): %#x %#zx",
2650 pszClass, cbBuf, chType, Ios.Status, Ios.Information);
2651 }
2652 else if (cbBuf < cbActualMin)
2653 {
2654 if (rcNt != STATUS_BUFFER_OVERFLOW)
2655 RTTestIFailed("%s/%#x/%c: %#x, expected STATUS_BUFFER_OVERFLOW", pszClass, cbBuf, chType, rcNt);
2656 if (pcbName)
2657 {
2658 size_t const cbNameAlt = offName < cbBuf ? cbBuf - offName : 0;
2659 if ( *pcbName != cbName
2660 && !( *pcbName == cbNameAlt
2661 && (enmClass == FileFsAttributeInformation /*NTFS,FAT*/)))
2662 RTTestIFailed("%s/%#x/%c: Wrong name length: %#x, expected %#x (or %#x)",
2663 pszClass, cbBuf, chType, *pcbName, cbName, cbNameAlt);
2664 if (memcmp(abNameCopy, &uBuf.ab[offName], cbNameAlt) != 0)
2665 RTTestIFailed("%s/%#x/%c: Wrong partial name: %.*Rhxs",
2666 pszClass, cbBuf, chType, cbNameAlt, &uBuf.ab[offName]);
2667 }
2668 if (Ios.Information != cbBuf)
2669 RTTestIFailed("%s/%#x/%c: Ios.Information = %#x, expected %#x",
2670 pszClass, cbBuf, chType, Ios.Information, cbBuf);
2671 }
2672 else
2673 {
2674 if ( !ASMMemIsAllU8(&uBuf.ab[cbActualMin], sizeof(uBuf) - cbActualMin, 0xfe)
2675 && enmClass != FileStorageReserveIdInformation /* NTFS bug? */ )
2676 RTTestIFailed("%s/%#x/%c: Touched memory beyond returned length (cbActualMin=%#x, rcNt=%#x)",
2677 pszClass, cbBuf, chType, cbActualMin, rcNt);
2678 if (pcbName && *pcbName != cbName)
2679 RTTestIFailed("%s/%#x/%c: Wrong name length: %#x, expected %#x",
2680 pszClass, cbBuf, chType, *pcbName, cbName);
2681 if (pcbName && memcmp(abNameCopy, &uBuf.ab[offName], cbName) != 0)
2682 RTTestIFailed("%s/%#x/%c: Wrong name: %.*Rhxs",
2683 pszClass, cbBuf, chType, cbName, &uBuf.ab[offName]);
2684 }
2685 }
2686 }
2687 }
2688 else
2689 {
2690 if (!g_aNtQueryVolInfoFileClasses[i].fQuery)
2691 {
2692 if (rcNt != STATUS_INVALID_INFO_CLASS)
2693 RTTestIFailed("%s/%#x/%c: %#x, expected STATUS_INVALID_INFO_CLASS", pszClass, cbBuf, chType, rcNt);
2694 }
2695 else if ( rcNt != STATUS_INVALID_INFO_CLASS
2696 && rcNt != STATUS_INVALID_PARAMETER
2697 && !(rcNt == STATUS_ACCESS_DENIED && enmClass == FileFsControlInformation /* RDR2/W10 */)
2698 && !(rcNt == STATUS_OBJECT_NAME_NOT_FOUND && enmClass == FileFsObjectIdInformation /* RDR2/W10 */)
2699 )
2700 RTTestIFailed("%s/%#x/%c: %#x", pszClass, cbBuf, chType, rcNt);
2701 if ( (Ios.Status != VirginIos.Status || Ios.Information != VirginIos.Information)
2702 && !( Ios.Status == 0 && Ios.Information == 0
2703 && fType == RTFS_TYPE_DIRECTORY
2704 && ( enmClass == FileFsObjectIdInformation /* RDR2+NTFS on W10 */
2705 || enmClass == FileFsControlInformation /* RDR2 on W10 */
2706 || enmClass == FileFsVolumeFlagsInformation /* RDR2+NTFS on W10 */
2707 || enmClass == FileFsDataCopyInformation /* RDR2 on W10 */
2708 || enmClass == FileFsMetadataSizeInformation /* RDR2+NTFS on W10 */
2709 || enmClass == FileFsFullSizeInformationEx /* RDR2 on W10 */
2710 ) )
2711 )
2712 RTTestIFailed("%s/%#x/%c: I/O status block was modified: %#x %#zx (rcNt=%#x)",
2713 pszClass, cbBuf, chType, Ios.Status, Ios.Information, rcNt);
2714 if (!ASMMemIsAllU8(&uBuf, sizeof(uBuf), 0xff))
2715 RTTestIFailed("%s/%#x/%c: Buffer was touched in failure case!", pszClass, cbBuf, chType);
2716 }
2717 }
2718 RT_NOREF(fType);
2719}
2720
2721void fsPerfNtQueryVolInfoFile(void)
2722{
2723 RTTestISub("NtQueryVolumeInformationFile");
2724
2725 /* On a regular file: */
2726 RTFILE hFile1;
2727 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file2qvif")),
2728 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
2729 fsPerfNtQueryVolInfoFileWorker((HANDLE)RTFileToNative(hFile1), RTFS_TYPE_FILE);
2730 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2731
2732 /* On a directory: */
2733 HANDLE hDir1 = INVALID_HANDLE_VALUE;
2734 RTTESTI_CHECK_RC_RETV(RTNtPathOpenDir(InDir(RT_STR_TUPLE("")), GENERIC_READ | SYNCHRONIZE | FILE_SYNCHRONOUS_IO_NONALERT,
2735 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
2736 FILE_OPEN, 0, &hDir1, NULL), VINF_SUCCESS);
2737 fsPerfNtQueryVolInfoFileWorker(hDir1, RTFS_TYPE_DIRECTORY);
2738 RTTESTI_CHECK(CloseHandle(hDir1) == TRUE);
2739
2740 /* On a regular file opened for reading: */
2741 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file2qvif")),
2742 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
2743 fsPerfNtQueryVolInfoFileWorker((HANDLE)RTFileToNative(hFile1), RTFS_TYPE_FILE);
2744 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2745}
2746
2747#endif /* RT_OS_WINDOWS */
2748
2749static void fsPerfFChMod(void)
2750{
2751 RTTestISub("fchmod");
2752 RTFILE hFile1;
2753 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file4")),
2754 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2755 RTFSOBJINFO ObjInfo = {0};
2756 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
2757 RTFMODE const fEvenMode = (ObjInfo.Attr.fMode & ~RTFS_UNIX_ALL_ACCESS_PERMS) | RTFS_DOS_READONLY | 0400;
2758 RTFMODE const fOddMode = (ObjInfo.Attr.fMode & ~(RTFS_UNIX_ALL_ACCESS_PERMS | RTFS_DOS_READONLY)) | 0640;
2759 PROFILE_FN(RTFileSetMode(hFile1, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTFileSetMode");
2760
2761 RTFileSetMode(hFile1, ObjInfo.Attr.fMode);
2762 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2763}
2764
2765
2766static void fsPerfFUtimes(void)
2767{
2768 RTTestISub("futimes");
2769 RTFILE hFile1;
2770 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file5")),
2771 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2772 RTTIMESPEC Time1;
2773 RTTimeNow(&Time1);
2774 RTTIMESPEC Time2 = Time1;
2775 RTTimeSpecSubSeconds(&Time2, 3636);
2776
2777 RTFSOBJINFO ObjInfo0 = {0};
2778 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo0, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
2779
2780 /* Modify modification time: */
2781 RTTESTI_CHECK_RC(RTFileSetTimes(hFile1, NULL, &Time2, NULL, NULL), VINF_SUCCESS);
2782 RTFSOBJINFO ObjInfo1 = {0};
2783 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo1, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
2784 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo1.ModificationTime) >> 2) == (RTTimeSpecGetSeconds(&Time2) >> 2));
2785 char sz1[RTTIME_STR_LEN], sz2[RTTIME_STR_LEN]; /* Div by 1000 here for posix impl. using timeval. */
2786 RTTESTI_CHECK_MSG(RTTimeSpecGetNano(&ObjInfo1.AccessTime) / 1000 == RTTimeSpecGetNano(&ObjInfo0.AccessTime) / 1000,
2787 ("%s, expected %s", RTTimeSpecToString(&ObjInfo1.AccessTime, sz1, sizeof(sz1)),
2788 RTTimeSpecToString(&ObjInfo0.AccessTime, sz2, sizeof(sz2))));
2789
2790 /* Modify access time: */
2791 RTTESTI_CHECK_RC(RTFileSetTimes(hFile1, &Time1, NULL, NULL, NULL), VINF_SUCCESS);
2792 RTFSOBJINFO ObjInfo2 = {0};
2793 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo2, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
2794 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo2.AccessTime) >> 2) == (RTTimeSpecGetSeconds(&Time1) >> 2));
2795 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo2.ModificationTime) / 1000 == RTTimeSpecGetNano(&ObjInfo1.ModificationTime) / 1000);
2796
2797 /* Benchmark it: */
2798 PROFILE_FN(RTFileSetTimes(hFile1, NULL, iIteration & 1 ? &Time1 : &Time2, NULL, NULL), g_nsTestRun, "RTFileSetTimes");
2799
2800 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2801}
2802
2803
2804static void fsPerfStat(void)
2805{
2806 RTTestISub("stat");
2807 RTFSOBJINFO ObjInfo;
2808
2809 /* Non-existing files. */
2810 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InEmptyDir(RT_STR_TUPLE("no-such-file")),
2811 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VERR_FILE_NOT_FOUND);
2812 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
2813 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), FSPERF_VERR_PATH_NOT_FOUND);
2814 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
2815 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VERR_PATH_NOT_FOUND);
2816
2817 /* Shallow: */
2818 RTFILE hFile1;
2819 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file3")),
2820 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2821 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2822
2823 PROFILE_FN(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), g_nsTestRun,
2824 "RTPathQueryInfoEx/NOTHING");
2825 PROFILE_FN(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK), g_nsTestRun,
2826 "RTPathQueryInfoEx/UNIX");
2827
2828
2829 /* Deep: */
2830 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file3")),
2831 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2832 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2833
2834 PROFILE_FN(RTPathQueryInfoEx(g_szDeepDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), g_nsTestRun,
2835 "RTPathQueryInfoEx/deep/NOTHING");
2836 PROFILE_FN(RTPathQueryInfoEx(g_szDeepDir, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK), g_nsTestRun,
2837 "RTPathQueryInfoEx/deep/UNIX");
2838
2839 /* Manytree: */
2840 char szPath[FSPERF_MAX_PATH];
2841 PROFILE_MANYTREE_FN(szPath, RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK),
2842 1, g_nsTestRun, "RTPathQueryInfoEx/manytree/NOTHING");
2843 PROFILE_MANYTREE_FN(szPath, RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK),
2844 1, g_nsTestRun, "RTPathQueryInfoEx/manytree/UNIX");
2845}
2846
2847
2848static void fsPerfChmod(void)
2849{
2850 RTTestISub("chmod");
2851
2852 /* Non-existing files. */
2853 RTTESTI_CHECK_RC(RTPathSetMode(InEmptyDir(RT_STR_TUPLE("no-such-file")), 0665),
2854 VERR_FILE_NOT_FOUND);
2855 RTTESTI_CHECK_RC(RTPathSetMode(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), 0665),
2856 FSPERF_VERR_PATH_NOT_FOUND);
2857 RTTESTI_CHECK_RC(RTPathSetMode(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), 0665), VERR_PATH_NOT_FOUND);
2858
2859 /* Shallow: */
2860 RTFILE hFile1;
2861 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file14")),
2862 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2863 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2864
2865 RTFSOBJINFO ObjInfo;
2866 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
2867 RTFMODE const fEvenMode = (ObjInfo.Attr.fMode & ~RTFS_UNIX_ALL_ACCESS_PERMS) | RTFS_DOS_READONLY | 0400;
2868 RTFMODE const fOddMode = (ObjInfo.Attr.fMode & ~(RTFS_UNIX_ALL_ACCESS_PERMS | RTFS_DOS_READONLY)) | 0640;
2869 PROFILE_FN(RTPathSetMode(g_szDir, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTPathSetMode");
2870 RTPathSetMode(g_szDir, ObjInfo.Attr.fMode);
2871
2872 /* Deep: */
2873 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file14")),
2874 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2875 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2876
2877 PROFILE_FN(RTPathSetMode(g_szDeepDir, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTPathSetMode/deep");
2878 RTPathSetMode(g_szDeepDir, ObjInfo.Attr.fMode);
2879
2880 /* Manytree: */
2881 char szPath[FSPERF_MAX_PATH];
2882 PROFILE_MANYTREE_FN(szPath, RTPathSetMode(szPath, iIteration & 1 ? fOddMode : fEvenMode), 1, g_nsTestRun,
2883 "RTPathSetMode/manytree");
2884 DO_MANYTREE_FN(szPath, RTPathSetMode(szPath, ObjInfo.Attr.fMode));
2885}
2886
2887
2888static void fsPerfUtimes(void)
2889{
2890 RTTestISub("utimes");
2891
2892 RTTIMESPEC Time1;
2893 RTTimeNow(&Time1);
2894 RTTIMESPEC Time2 = Time1;
2895 RTTimeSpecSubSeconds(&Time2, 3636);
2896
2897 /* Non-existing files. */
2898 RTTESTI_CHECK_RC(RTPathSetTimesEx(InEmptyDir(RT_STR_TUPLE("no-such-file")), NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
2899 VERR_FILE_NOT_FOUND);
2900 RTTESTI_CHECK_RC(RTPathSetTimesEx(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
2901 NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
2902 FSPERF_VERR_PATH_NOT_FOUND);
2903 RTTESTI_CHECK_RC(RTPathSetTimesEx(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
2904 NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
2905 VERR_PATH_NOT_FOUND);
2906
2907 /* Shallow: */
2908 RTFILE hFile1;
2909 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file15")),
2910 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2911 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2912
2913 RTFSOBJINFO ObjInfo0 = {0};
2914 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo0, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
2915
2916 /* Modify modification time: */
2917 RTTESTI_CHECK_RC(RTPathSetTimesEx(g_szDir, NULL, &Time2, NULL, NULL, RTPATH_F_ON_LINK), VINF_SUCCESS);
2918 RTFSOBJINFO ObjInfo1;
2919 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo1, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
2920 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo1.ModificationTime) >> 2) == (RTTimeSpecGetSeconds(&Time2) >> 2));
2921 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo1.AccessTime) / 1000 == RTTimeSpecGetNano(&ObjInfo0.AccessTime) / 1000 /* posix timeval */);
2922
2923 /* Modify access time: */
2924 RTTESTI_CHECK_RC(RTPathSetTimesEx(g_szDir, &Time1, NULL, NULL, NULL, RTPATH_F_ON_LINK), VINF_SUCCESS);
2925 RTFSOBJINFO ObjInfo2 = {0};
2926 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo2, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
2927 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo2.AccessTime) >> 2) == (RTTimeSpecGetSeconds(&Time1) >> 2));
2928 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo2.ModificationTime) / 1000 == RTTimeSpecGetNano(&ObjInfo1.ModificationTime) / 1000 /* posix timeval */);
2929
2930 /* Profile shallow: */
2931 PROFILE_FN(RTPathSetTimesEx(g_szDir, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
2932 NULL, NULL, RTPATH_F_ON_LINK),
2933 g_nsTestRun, "RTPathSetTimesEx");
2934
2935 /* Deep: */
2936 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file15")),
2937 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2938 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2939
2940 PROFILE_FN(RTPathSetTimesEx(g_szDeepDir, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
2941 NULL, NULL, RTPATH_F_ON_LINK),
2942 g_nsTestRun, "RTPathSetTimesEx/deep");
2943
2944 /* Manytree: */
2945 char szPath[FSPERF_MAX_PATH];
2946 PROFILE_MANYTREE_FN(szPath, RTPathSetTimesEx(szPath, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
2947 NULL, NULL, RTPATH_F_ON_LINK),
2948 1, g_nsTestRun, "RTPathSetTimesEx/manytree");
2949}
2950
2951
2952DECL_FORCE_INLINE(int) fsPerfRenameMany(const char *pszFile, uint32_t iIteration)
2953{
2954 char szRenamed[FSPERF_MAX_PATH];
2955 strcat(strcpy(szRenamed, pszFile), "-renamed");
2956 if (!(iIteration & 1))
2957 return RTPathRename(pszFile, szRenamed, 0);
2958 return RTPathRename(szRenamed, pszFile, 0);
2959}
2960
2961
2962static void fsPerfRename(void)
2963{
2964 RTTestISub("rename");
2965 char szPath[FSPERF_MAX_PATH];
2966
2967/** @todo rename directories too! */
2968/** @todo check overwriting files and directoris (empty ones should work on
2969 * unix). */
2970
2971 /* Non-existing files. */
2972 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("other-no-such-file")));
2973 RTTESTI_CHECK_RC(RTPathRename(InEmptyDir(RT_STR_TUPLE("no-such-file")), szPath, 0), VERR_FILE_NOT_FOUND);
2974 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "other-no-such-file")));
2975 RTTESTI_CHECK_RC(RTPathRename(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), szPath, 0),
2976 FSPERF_VERR_PATH_NOT_FOUND);
2977 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("other-no-such-file")));
2978 RTTESTI_CHECK_RC(RTPathRename(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), szPath, 0), VERR_PATH_NOT_FOUND);
2979
2980 RTFILE hFile1;
2981 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file16")),
2982 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2983 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2984 strcat(strcpy(szPath, g_szDir), "-no-such-dir" RTPATH_SLASH_STR "file16");
2985 RTTESTI_CHECK_RC(RTPathRename(szPath, g_szDir, 0), FSPERF_VERR_PATH_NOT_FOUND);
2986 RTTESTI_CHECK_RC(RTPathRename(g_szDir, szPath, 0), FSPERF_VERR_PATH_NOT_FOUND);
2987
2988 /* Shallow: */
2989 strcat(strcpy(szPath, g_szDir), "-other");
2990 PROFILE_FN(RTPathRename(iIteration & 1 ? szPath : g_szDir, iIteration & 1 ? g_szDir : szPath, 0), g_nsTestRun, "RTPathRename");
2991
2992 /* Deep: */
2993 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file15")),
2994 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
2995 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2996
2997 strcat(strcpy(szPath, g_szDeepDir), "-other");
2998 PROFILE_FN(RTPathRename(iIteration & 1 ? szPath : g_szDeepDir, iIteration & 1 ? g_szDeepDir : szPath, 0),
2999 g_nsTestRun, "RTPathRename/deep");
3000
3001 /* Manytree: */
3002 PROFILE_MANYTREE_FN(szPath, fsPerfRenameMany(szPath, iIteration), 2, g_nsTestRun, "RTPathRename/manytree");
3003}
3004
3005
3006/**
3007 * Wrapper around RTDirOpen/RTDirOpenFiltered which takes g_fRelativeDir into
3008 * account.
3009 */
3010DECL_FORCE_INLINE(int) fsPerfOpenDirWrap(PRTDIR phDir, const char *pszPath)
3011{
3012 if (!g_fRelativeDir)
3013 return RTDirOpen(phDir, pszPath);
3014 return RTDirOpenFiltered(phDir, pszPath, RTDIRFILTER_NONE, RTDIR_F_NO_ABS_PATH);
3015}
3016
3017
3018DECL_FORCE_INLINE(int) fsPerfOpenClose(const char *pszDir)
3019{
3020 RTDIR hDir;
3021 RTTESTI_CHECK_RC_RET(fsPerfOpenDirWrap(&hDir, pszDir), VINF_SUCCESS, rcCheck);
3022 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
3023 return VINF_SUCCESS;
3024}
3025
3026
3027static void vsPerfDirOpen(void)
3028{
3029 RTTestISub("dir open");
3030 RTDIR hDir;
3031
3032 /*
3033 * Non-existing files.
3034 */
3035 RTTESTI_CHECK_RC(fsPerfOpenDirWrap(&hDir, InEmptyDir(RT_STR_TUPLE("no-such-file"))), VERR_FILE_NOT_FOUND);
3036 RTTESTI_CHECK_RC(fsPerfOpenDirWrap(&hDir, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
3037 RTTESTI_CHECK_RC(fsPerfOpenDirWrap(&hDir, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
3038
3039 /*
3040 * Check that open + close works.
3041 */
3042 g_szEmptyDir[g_cchEmptyDir] = '\0';
3043 RTTESTI_CHECK_RC_RETV(fsPerfOpenDirWrap(&hDir, g_szEmptyDir), VINF_SUCCESS);
3044 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
3045
3046
3047 /*
3048 * Profile empty dir and dir with many files.
3049 */
3050 g_szEmptyDir[g_cchEmptyDir] = '\0';
3051 PROFILE_FN(fsPerfOpenClose(g_szEmptyDir), g_nsTestRun, "RTDirOpen/Close empty");
3052 if (g_fManyFiles)
3053 {
3054 InDir(RT_STR_TUPLE("manyfiles"));
3055 PROFILE_FN(fsPerfOpenClose(g_szDir), g_nsTestRun, "RTDirOpen/Close manyfiles");
3056 }
3057}
3058
3059
3060DECL_FORCE_INLINE(int) fsPerfEnumEmpty(void)
3061{
3062 RTDIR hDir;
3063 g_szEmptyDir[g_cchEmptyDir] = '\0';
3064 RTTESTI_CHECK_RC_RET(fsPerfOpenDirWrap(&hDir, g_szEmptyDir), VINF_SUCCESS, rcCheck);
3065
3066 RTDIRENTRY Entry;
3067 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
3068 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
3069 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
3070
3071 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
3072 return VINF_SUCCESS;
3073}
3074
3075
3076DECL_FORCE_INLINE(int) fsPerfEnumManyFiles(void)
3077{
3078 RTDIR hDir;
3079 RTTESTI_CHECK_RC_RET(fsPerfOpenDirWrap(&hDir, InDir(RT_STR_TUPLE("manyfiles"))), VINF_SUCCESS, rcCheck);
3080 uint32_t cLeft = g_cManyFiles + 2;
3081 for (;;)
3082 {
3083 RTDIRENTRY Entry;
3084 if (cLeft > 0)
3085 RTTESTI_CHECK_RC_BREAK(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
3086 else
3087 {
3088 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
3089 break;
3090 }
3091 cLeft--;
3092 }
3093 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
3094 return VINF_SUCCESS;
3095}
3096
3097
3098static void vsPerfDirEnum(void)
3099{
3100 RTTestISub("dir enum");
3101 RTDIR hDir;
3102
3103 /*
3104 * The empty directory.
3105 */
3106 g_szEmptyDir[g_cchEmptyDir] = '\0';
3107 RTTESTI_CHECK_RC_RETV(fsPerfOpenDirWrap(&hDir, g_szEmptyDir), VINF_SUCCESS);
3108
3109 uint32_t fDots = 0;
3110 RTDIRENTRY Entry;
3111 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
3112 RTTESTI_CHECK(RTDirEntryIsStdDotLink(&Entry));
3113 fDots |= RT_BIT_32(Entry.cbName - 1);
3114
3115 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
3116 RTTESTI_CHECK(RTDirEntryIsStdDotLink(&Entry));
3117 fDots |= RT_BIT_32(Entry.cbName - 1);
3118 RTTESTI_CHECK(fDots == 3);
3119
3120 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
3121
3122 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
3123
3124 /*
3125 * The directory with many files in it.
3126 */
3127 if (g_fManyFiles)
3128 {
3129 fDots = 0;
3130 uint32_t const cBitmap = RT_ALIGN_32(g_cManyFiles, 64);
3131 void *pvBitmap = alloca(cBitmap / 8);
3132 RT_BZERO(pvBitmap, cBitmap / 8);
3133 for (uint32_t i = g_cManyFiles; i < cBitmap; i++)
3134 ASMBitSet(pvBitmap, i);
3135
3136 uint32_t cFiles = 0;
3137 RTTESTI_CHECK_RC_RETV(fsPerfOpenDirWrap(&hDir, InDir(RT_STR_TUPLE("manyfiles"))), VINF_SUCCESS);
3138 for (;;)
3139 {
3140 int rc = RTDirRead(hDir, &Entry, NULL);
3141 if (rc == VINF_SUCCESS)
3142 {
3143 if (Entry.szName[0] == '.')
3144 {
3145 if (Entry.szName[1] == '.')
3146 {
3147 RTTESTI_CHECK(!(fDots & 2));
3148 fDots |= 2;
3149 }
3150 else
3151 {
3152 RTTESTI_CHECK(Entry.szName[1] == '\0');
3153 RTTESTI_CHECK(!(fDots & 1));
3154 fDots |= 1;
3155 }
3156 }
3157 else
3158 {
3159 uint32_t iFile = UINT32_MAX;
3160 RTTESTI_CHECK_RC(RTStrToUInt32Full(Entry.szName, 10, &iFile), VINF_SUCCESS);
3161 if ( iFile < g_cManyFiles
3162 && !ASMBitTest(pvBitmap, iFile))
3163 {
3164 ASMBitSet(pvBitmap, iFile);
3165 cFiles++;
3166 }
3167 else
3168 RTTestFailed(g_hTest, "line %u: iFile=%u g_cManyFiles=%u\n", __LINE__, iFile, g_cManyFiles);
3169 }
3170 }
3171 else if (rc == VERR_NO_MORE_FILES)
3172 break;
3173 else
3174 {
3175 RTTestFailed(g_hTest, "RTDirRead failed enumerating manyfiles: %Rrc\n", rc);
3176 RTDirClose(hDir);
3177 return;
3178 }
3179 }
3180 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
3181 RTTESTI_CHECK(fDots == 3);
3182 RTTESTI_CHECK(cFiles == g_cManyFiles);
3183 RTTESTI_CHECK(ASMMemIsAllU8(pvBitmap, cBitmap / 8, 0xff));
3184 }
3185
3186 /*
3187 * Profile.
3188 */
3189 PROFILE_FN(fsPerfEnumEmpty(),g_nsTestRun, "RTDirOpen/Read/Close empty");
3190 if (g_fManyFiles)
3191 PROFILE_FN(fsPerfEnumManyFiles(), g_nsTestRun, "RTDirOpen/Read/Close manyfiles");
3192}
3193
3194
3195static void fsPerfMkRmDir(void)
3196{
3197 RTTestISub("mkdir/rmdir");
3198
3199 /* Non-existing directories: */
3200 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir"))), VERR_FILE_NOT_FOUND);
3201 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR))), VERR_FILE_NOT_FOUND);
3202 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
3203 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file" RTPATH_SLASH_STR))), FSPERF_VERR_PATH_NOT_FOUND);
3204 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
3205 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file" RTPATH_SLASH_STR))), VERR_PATH_NOT_FOUND);
3206
3207 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), 0755, 0), FSPERF_VERR_PATH_NOT_FOUND);
3208 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), 0755, 0), VERR_PATH_NOT_FOUND);
3209
3210 /* Already existing directories and files: */
3211 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE(".")), 0755, 0), VERR_ALREADY_EXISTS);
3212 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE("..")), 0755, 0), VERR_ALREADY_EXISTS);
3213
3214 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("known-file"))), VERR_NOT_A_DIRECTORY);
3215 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR))), VERR_NOT_A_DIRECTORY);
3216
3217 /* Remove directory with subdirectories: */
3218#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
3219 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("."))), VERR_DIR_NOT_EMPTY);
3220#else
3221 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER); /* EINVAL for '.' */
3222#endif
3223#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
3224 int rc = RTDirRemove(InDir(RT_STR_TUPLE("..")));
3225# ifdef RT_OS_WINDOWS
3226 if (rc != VERR_DIR_NOT_EMPTY /*ntfs root*/ && rc != VERR_SHARING_VIOLATION /*ntfs weird*/ && rc != VERR_ACCESS_DENIED /*fat32 root*/)
3227 RTTestIFailed("RTDirRemove(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY, VERR_SHARING_VIOLATION or VERR_ACCESS_DENIED", g_szDir, rc);
3228# else
3229 if (rc != VERR_DIR_NOT_EMPTY && rc != VERR_RESOURCE_BUSY /*IPRT/kLIBC fun*/)
3230 RTTestIFailed("RTDirRemove(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY or VERR_RESOURCE_BUSY", g_szDir, rc);
3231
3232 APIRET orc;
3233 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE(".")))) == ERROR_ACCESS_DENIED,
3234 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
3235 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("..")))) == ERROR_ACCESS_DENIED,
3236 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
3237 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("")))) == ERROR_PATH_NOT_FOUND, /* a little weird (fsrouter) */
3238 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_PATH_NOT_FOUND));
3239
3240# endif
3241#else
3242 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(".."))), VERR_DIR_NOT_EMPTY);
3243#endif
3244 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(""))), VERR_DIR_NOT_EMPTY);
3245
3246 /* Create a directory and remove it: */
3247 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("subdir-1")), 0755, 0), VINF_SUCCESS);
3248 RTTESTI_CHECK_RC(RTDirRemove(g_szDir), VINF_SUCCESS);
3249
3250 /* Create a file and try remove it or create a directory with the same name: */
3251 RTFILE hFile1;
3252 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file18")),
3253 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
3254 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3255 RTTESTI_CHECK_RC(RTDirRemove(g_szDir), VERR_NOT_A_DIRECTORY);
3256 RTTESTI_CHECK_RC(RTDirCreate(g_szDir, 0755, 0), VERR_ALREADY_EXISTS);
3257 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("file18" RTPATH_SLASH_STR "subdir")), 0755, 0), VERR_PATH_NOT_FOUND);
3258
3259 /*
3260 * Profile alternately creating and removing a bunch of directories.
3261 */
3262 RTTESTI_CHECK_RC_RETV(RTDirCreate(InDir(RT_STR_TUPLE("subdir-2")), 0755, 0), VINF_SUCCESS);
3263 size_t cchDir = strlen(g_szDir);
3264 g_szDir[cchDir++] = RTPATH_SLASH;
3265 g_szDir[cchDir++] = 's';
3266
3267 uint32_t cCreated = 0;
3268 uint64_t nsCreate = 0;
3269 uint64_t nsRemove = 0;
3270 for (;;)
3271 {
3272 /* Create a bunch: */
3273 uint64_t nsStart = RTTimeNanoTS();
3274 for (uint32_t i = 0; i < 998; i++)
3275 {
3276 RTStrFormatU32(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, i, 10, 3, 3, RTSTR_F_ZEROPAD);
3277 RTTESTI_CHECK_RC_RETV(RTDirCreate(g_szDir, 0755, 0), VINF_SUCCESS);
3278 }
3279 nsCreate += RTTimeNanoTS() - nsStart;
3280 cCreated += 998;
3281
3282 /* Remove the bunch: */
3283 nsStart = RTTimeNanoTS();
3284 for (uint32_t i = 0; i < 998; i++)
3285 {
3286 RTStrFormatU32(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, i, 10, 3, 3, RTSTR_F_ZEROPAD);
3287 RTTESTI_CHECK_RC_RETV(RTDirRemove(g_szDir), VINF_SUCCESS);
3288 }
3289 nsRemove = RTTimeNanoTS() - nsStart;
3290
3291 /* Check if we got time for another round: */
3292 if ( ( nsRemove >= g_nsTestRun
3293 && nsCreate >= g_nsTestRun)
3294 || nsCreate + nsRemove >= g_nsTestRun * 3)
3295 break;
3296 }
3297 RTTestIValue("RTDirCreate", nsCreate / cCreated, RTTESTUNIT_NS_PER_OCCURRENCE);
3298 RTTestIValue("RTDirRemove", nsRemove / cCreated, RTTESTUNIT_NS_PER_OCCURRENCE);
3299}
3300
3301
3302static void fsPerfStatVfs(void)
3303{
3304 RTTestISub("statvfs");
3305
3306 g_szEmptyDir[g_cchEmptyDir] = '\0';
3307 RTFOFF cbTotal;
3308 RTFOFF cbFree;
3309 uint32_t cbBlock;
3310 uint32_t cbSector;
3311 RTTESTI_CHECK_RC(RTFsQuerySizes(g_szEmptyDir, &cbTotal, &cbFree, &cbBlock, &cbSector), VINF_SUCCESS);
3312
3313 uint32_t uSerial;
3314 RTTESTI_CHECK_RC(RTFsQuerySerial(g_szEmptyDir, &uSerial), VINF_SUCCESS);
3315
3316 RTFSPROPERTIES Props;
3317 RTTESTI_CHECK_RC(RTFsQueryProperties(g_szEmptyDir, &Props), VINF_SUCCESS);
3318
3319 RTFSTYPE enmType;
3320 RTTESTI_CHECK_RC(RTFsQueryType(g_szEmptyDir, &enmType), VINF_SUCCESS);
3321
3322 g_szDeepDir[g_cchDeepDir] = '\0';
3323 PROFILE_FN(RTFsQuerySizes(g_szEmptyDir, &cbTotal, &cbFree, &cbBlock, &cbSector), g_nsTestRun, "RTFsQuerySize/empty");
3324 PROFILE_FN(RTFsQuerySizes(g_szDeepDir, &cbTotal, &cbFree, &cbBlock, &cbSector), g_nsTestRun, "RTFsQuerySize/deep");
3325}
3326
3327
3328static void fsPerfRm(void)
3329{
3330 RTTestISub("rm");
3331
3332 /* Non-existing files. */
3333 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-file"))), VERR_FILE_NOT_FOUND);
3334 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-file" RTPATH_SLASH_STR))), VERR_FILE_NOT_FOUND);
3335 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
3336 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file" RTPATH_SLASH_STR))), FSPERF_VERR_PATH_NOT_FOUND);
3337 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
3338 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file" RTPATH_SLASH_STR))), VERR_PATH_NOT_FOUND);
3339
3340 /* Existing file but specified as if it was a directory: */
3341#if defined(RT_OS_WINDOWS)
3342 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR ))), VERR_INVALID_NAME);
3343#else
3344 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR))), VERR_PATH_NOT_FOUND);
3345#endif
3346
3347 /* Directories: */
3348#if defined(RT_OS_WINDOWS)
3349 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_ACCESS_DENIED);
3350 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_ACCESS_DENIED);
3351 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
3352#elif defined(RT_OS_DARWIN) /* unlink() on xnu 16.7.0 is behaviour totally werid: */
3353 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER);
3354 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VINF_SUCCESS /*WTF?!?*/);
3355 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
3356#elif defined(RT_OS_OS2) /* OS/2 has a busted unlink, it think it should remove directories too. */
3357 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("."))), VERR_DIR_NOT_EMPTY);
3358 int rc = RTFileDelete(InDir(RT_STR_TUPLE("..")));
3359 if (rc != VERR_DIR_NOT_EMPTY && rc != VERR_FILE_NOT_FOUND && rc != VERR_RESOURCE_BUSY)
3360 RTTestIFailed("RTFileDelete(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY or VERR_FILE_NOT_FOUND or VERR_RESOURCE_BUSY", g_szDir, rc);
3361 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE(""))), VERR_DIR_NOT_EMPTY);
3362 APIRET orc;
3363 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE(".")))) == ERROR_ACCESS_DENIED,
3364 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
3365 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("..")))) == ERROR_ACCESS_DENIED,
3366 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
3367 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("")))) == ERROR_PATH_NOT_FOUND,
3368 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_PATH_NOT_FOUND)); /* hpfs+jfs; weird. */
3369
3370#else
3371 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_IS_A_DIRECTORY);
3372 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_IS_A_DIRECTORY);
3373 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_IS_A_DIRECTORY);
3374#endif
3375
3376 /* Shallow: */
3377 RTFILE hFile1;
3378 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file19")),
3379 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
3380 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3381 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
3382 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VERR_FILE_NOT_FOUND);
3383
3384 if (g_fManyFiles)
3385 {
3386 /*
3387 * Profile the deletion of the manyfiles content.
3388 */
3389 {
3390 InDir(RT_STR_TUPLE("manyfiles" RTPATH_SLASH_STR));
3391 size_t const offFilename = strlen(g_szDir);
3392 fsPerfYield();
3393 uint64_t const nsStart = RTTimeNanoTS();
3394 for (uint32_t i = 0; i < g_cManyFiles; i++)
3395 {
3396 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
3397 RTTESTI_CHECK_RC_RETV(RTFileDelete(g_szDir), VINF_SUCCESS);
3398 }
3399 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
3400 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files from a single directory", g_cManyFiles);
3401 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (single dir)");
3402 }
3403
3404 /*
3405 * Ditto for the manytree.
3406 */
3407 {
3408 char szPath[FSPERF_MAX_PATH];
3409 uint64_t const nsStart = RTTimeNanoTS();
3410 DO_MANYTREE_FN(szPath, RTTESTI_CHECK_RC_RETV(RTFileDelete(szPath), VINF_SUCCESS));
3411 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
3412 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files in tree", g_cManyTreeFiles);
3413 RTTestIValueF(cNsElapsed / g_cManyTreeFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (tree)");
3414 }
3415 }
3416}
3417
3418
3419static void fsPerfChSize(void)
3420{
3421 RTTestISub("chsize");
3422
3423 /*
3424 * We need some free space to perform this test.
3425 */
3426 g_szDir[g_cchDir] = '\0';
3427 RTFOFF cbFree = 0;
3428 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
3429 if (cbFree < _1M)
3430 {
3431 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 1MB", cbFree);
3432 return;
3433 }
3434
3435 /*
3436 * Create a file and play around with it's size.
3437 * We let the current file position follow the end position as we make changes.
3438 */
3439 RTFILE hFile1;
3440 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file20")),
3441 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
3442 uint64_t cbFile = UINT64_MAX;
3443 RTTESTI_CHECK_RC(RTFileQuerySize(hFile1, &cbFile), VINF_SUCCESS);
3444 RTTESTI_CHECK(cbFile == 0);
3445
3446 uint8_t abBuf[4096];
3447 static uint64_t const s_acbChanges[] =
3448 {
3449 1023, 1024, 1024, 1025, 8192, 11111, _1M, _8M, _8M,
3450 _4M, _2M + 1, _1M - 1, 65537, 65536, 32768, 8000, 7999, 7998, 1024, 1, 0
3451 };
3452 uint64_t cbOld = 0;
3453 for (unsigned i = 0; i < RT_ELEMENTS(s_acbChanges); i++)
3454 {
3455 uint64_t cbNew = s_acbChanges[i];
3456 if (cbNew + _64K >= (uint64_t)cbFree)
3457 continue;
3458
3459 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, cbNew), VINF_SUCCESS);
3460 RTTESTI_CHECK_RC(RTFileQuerySize(hFile1, &cbFile), VINF_SUCCESS);
3461 RTTESTI_CHECK_MSG(cbFile == cbNew, ("cbFile=%#RX64 cbNew=%#RX64\n", cbFile, cbNew));
3462
3463 if (cbNew > cbOld)
3464 {
3465 /* Check that the extension is all zeroed: */
3466 uint64_t cbLeft = cbNew - cbOld;
3467 while (cbLeft > 0)
3468 {
3469 memset(abBuf, 0xff, sizeof(abBuf));
3470 size_t cbToRead = sizeof(abBuf);
3471 if (cbToRead > cbLeft)
3472 cbToRead = (size_t)cbLeft;
3473 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, cbToRead, NULL), VINF_SUCCESS);
3474 RTTESTI_CHECK(ASMMemIsZero(abBuf, cbToRead));
3475 cbLeft -= cbToRead;
3476 }
3477 }
3478 else
3479 {
3480 /* Check that reading fails with EOF because current position is now beyond the end: */
3481 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
3482
3483 /* Keep current position at the end of the file: */
3484 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbNew, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3485 }
3486 cbOld = cbNew;
3487 }
3488
3489 /*
3490 * Profile just the file setting operation itself, keeping the changes within
3491 * an allocation unit to avoid needing to adjust the actual (host) FS allocation.
3492 * ASSUMES allocation unit >= 512 and power of two.
3493 */
3494 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, _64K), VINF_SUCCESS);
3495 PROFILE_FN(RTFileSetSize(hFile1, _64K - (iIteration & 255) - 128), g_nsTestRun, "RTFileSetSize/noalloc");
3496
3497 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
3498 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3499 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
3500}
3501
3502
3503static int fsPerfIoPrepFileWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf)
3504{
3505 /*
3506 * Fill the file with 0xf6 and insert offset markers with 1KB intervals.
3507 */
3508 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3509 memset(pbBuf, 0xf6, cbBuf);
3510 uint64_t cbLeft = cbFile;
3511 uint64_t off = 0;
3512 while (cbLeft > 0)
3513 {
3514 Assert(!(off & (_1K - 1)));
3515 Assert(!(cbBuf & (_1K - 1)));
3516 for (size_t offBuf = 0; offBuf < cbBuf; offBuf += _1K, off += _1K)
3517 *(uint64_t *)&pbBuf[offBuf] = off;
3518
3519 size_t cbToWrite = cbBuf;
3520 if (cbToWrite > cbLeft)
3521 cbToWrite = (size_t)cbLeft;
3522
3523 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbToWrite, NULL), VINF_SUCCESS, rcCheck);
3524 cbLeft -= cbToWrite;
3525 }
3526 return VINF_SUCCESS;
3527}
3528
3529static int fsPerfIoPrepFile(RTFILE hFile1, uint64_t cbFile, uint8_t **ppbFree)
3530{
3531 /*
3532 * Seek to the end - 4K and write the last 4K.
3533 * This should have the effect of filling the whole file with zeros.
3534 */
3535 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, cbFile - _4K, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3536 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, g_abRTZero4K, _4K, NULL), VINF_SUCCESS, rcCheck);
3537
3538 /*
3539 * Check that the space we searched across actually is zero filled.
3540 */
3541 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3542 size_t cbBuf = RT_MIN(_1M, g_cbMaxBuffer);
3543 uint8_t *pbBuf = *ppbFree = (uint8_t *)RTMemAlloc(cbBuf);
3544 RTTESTI_CHECK_RET(pbBuf != NULL, VERR_NO_MEMORY);
3545 uint64_t cbLeft = cbFile;
3546 while (cbLeft > 0)
3547 {
3548 size_t cbToRead = cbBuf;
3549 if (cbToRead > cbLeft)
3550 cbToRead = (size_t)cbLeft;
3551 pbBuf[cbToRead - 1] = 0xff;
3552
3553 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBuf, cbToRead, NULL), VINF_SUCCESS, rcCheck);
3554 RTTESTI_CHECK_RET(ASMMemIsZero(pbBuf, cbToRead), VERR_MISMATCH);
3555
3556 cbLeft -= cbToRead;
3557 }
3558
3559 /*
3560 * Fill the file with 0xf6 and insert offset markers with 1KB intervals.
3561 */
3562 return fsPerfIoPrepFileWorker(hFile1, cbFile, pbBuf, cbBuf);
3563}
3564
3565/**
3566 * Used in relation to the mmap test when in non-default position.
3567 */
3568static int fsPerfReinitFile(RTFILE hFile1, uint64_t cbFile)
3569{
3570 size_t cbBuf = RT_MIN(_1M, g_cbMaxBuffer);
3571 uint8_t *pbBuf = (uint8_t *)RTMemAlloc(cbBuf);
3572 RTTESTI_CHECK_RET(pbBuf != NULL, VERR_NO_MEMORY);
3573
3574 int rc = fsPerfIoPrepFileWorker(hFile1, cbFile, pbBuf, cbBuf);
3575
3576 RTMemFree(pbBuf);
3577 return rc;
3578}
3579
3580/**
3581 * Checks the content read from the file fsPerfIoPrepFile() prepared.
3582 */
3583static bool fsPerfCheckReadBuf(unsigned uLineNo, uint64_t off, uint8_t const *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
3584{
3585 uint32_t cMismatches = 0;
3586 size_t offBuf = 0;
3587 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
3588 while (offBuf < cbBuf)
3589 {
3590 /*
3591 * Check the offset marker:
3592 */
3593 if (offBlock < sizeof(uint64_t))
3594 {
3595 RTUINT64U uMarker;
3596 uMarker.u = off + offBuf - offBlock;
3597 unsigned offMarker = offBlock & (sizeof(uint64_t) - 1);
3598 while (offMarker < sizeof(uint64_t) && offBuf < cbBuf)
3599 {
3600 if (uMarker.au8[offMarker] != pbBuf[offBuf])
3601 {
3602 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#x",
3603 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], uMarker.au8[offMarker]);
3604 if (cMismatches++ > 32)
3605 return false;
3606 }
3607 offMarker++;
3608 offBuf++;
3609 }
3610 offBlock = sizeof(uint64_t);
3611 }
3612
3613 /*
3614 * Check the filling:
3615 */
3616 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf - offBuf);
3617 if ( cbFilling == 0
3618 || ASMMemIsAllU8(&pbBuf[offBuf], cbFilling, bFiller))
3619 offBuf += cbFilling;
3620 else
3621 {
3622 /* Some mismatch, locate it/them: */
3623 while (cbFilling > 0 && offBuf < cbBuf)
3624 {
3625 if (pbBuf[offBuf] != bFiller)
3626 {
3627 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#04x",
3628 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], bFiller);
3629 if (cMismatches++ > 32)
3630 return false;
3631 }
3632 offBuf++;
3633 cbFilling--;
3634 }
3635 }
3636 offBlock = 0;
3637 }
3638 return cMismatches == 0;
3639}
3640
3641
3642/**
3643 * Sets up write buffer with offset markers and fillers.
3644 */
3645static void fsPerfFillWriteBuf(uint64_t off, uint8_t *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
3646{
3647 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
3648 while (cbBuf > 0)
3649 {
3650 /* The marker. */
3651 if (offBlock < sizeof(uint64_t))
3652 {
3653 RTUINT64U uMarker;
3654 uMarker.u = off + offBlock;
3655 if (cbBuf > sizeof(uMarker) - offBlock)
3656 {
3657 memcpy(pbBuf, &uMarker.au8[offBlock], sizeof(uMarker) - offBlock);
3658 pbBuf += sizeof(uMarker) - offBlock;
3659 cbBuf -= sizeof(uMarker) - offBlock;
3660 off += sizeof(uMarker) - offBlock;
3661 }
3662 else
3663 {
3664 memcpy(pbBuf, &uMarker.au8[offBlock], cbBuf);
3665 return;
3666 }
3667 offBlock = sizeof(uint64_t);
3668 }
3669
3670 /* Do the filling. */
3671 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf);
3672 memset(pbBuf, bFiller, cbFilling);
3673 pbBuf += cbFilling;
3674 cbBuf -= cbFilling;
3675 off += cbFilling;
3676
3677 offBlock = 0;
3678 }
3679}
3680
3681
3682
3683static void fsPerfIoSeek(RTFILE hFile1, uint64_t cbFile)
3684{
3685 /*
3686 * Do a bunch of search tests, most which are random.
3687 */
3688 struct
3689 {
3690 int rc;
3691 uint32_t uMethod;
3692 int64_t offSeek;
3693 uint64_t offActual;
3694
3695 } aSeeks[9 + 64] =
3696 {
3697 { VINF_SUCCESS, RTFILE_SEEK_BEGIN, 0, 0 },
3698 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
3699 { VINF_SUCCESS, RTFILE_SEEK_END, 0, cbFile },
3700 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -4096, cbFile - 4096 },
3701 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 4096 - (int64_t)cbFile, 0 },
3702 { VINF_SUCCESS, RTFILE_SEEK_END, -(int64_t)cbFile/2, cbFile / 2 + (cbFile & 1) },
3703 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -(int64_t)cbFile/2, 0 },
3704#if defined(RT_OS_WINDOWS)
3705 { VERR_NEGATIVE_SEEK, RTFILE_SEEK_CURRENT, -1, 0 },
3706#else
3707 { VERR_INVALID_PARAMETER, RTFILE_SEEK_CURRENT, -1, 0 },
3708#endif
3709 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
3710 };
3711
3712 uint64_t offActual = 0;
3713 for (unsigned i = 9; i < RT_ELEMENTS(aSeeks); i++)
3714 {
3715 switch (RTRandU32Ex(RTFILE_SEEK_BEGIN, RTFILE_SEEK_END))
3716 {
3717 default: AssertFailedBreak();
3718 case RTFILE_SEEK_BEGIN:
3719 aSeeks[i].uMethod = RTFILE_SEEK_BEGIN;
3720 aSeeks[i].rc = VINF_SUCCESS;
3721 aSeeks[i].offSeek = RTRandU64Ex(0, cbFile + cbFile / 8);
3722 aSeeks[i].offActual = offActual = aSeeks[i].offSeek;
3723 break;
3724
3725 case RTFILE_SEEK_CURRENT:
3726 aSeeks[i].uMethod = RTFILE_SEEK_CURRENT;
3727 aSeeks[i].rc = VINF_SUCCESS;
3728 aSeeks[i].offSeek = (int64_t)RTRandU64Ex(0, cbFile + cbFile / 8) - (int64_t)offActual;
3729 aSeeks[i].offActual = offActual += aSeeks[i].offSeek;
3730 break;
3731
3732 case RTFILE_SEEK_END:
3733 aSeeks[i].uMethod = RTFILE_SEEK_END;
3734 aSeeks[i].rc = VINF_SUCCESS;
3735 aSeeks[i].offSeek = -(int64_t)RTRandU64Ex(0, cbFile);
3736 aSeeks[i].offActual = offActual = cbFile + aSeeks[i].offSeek;
3737 break;
3738 }
3739 }
3740
3741 for (unsigned iDoReadCheck = 0; iDoReadCheck < 2; iDoReadCheck++)
3742 {
3743 for (uint32_t i = 0; i < RT_ELEMENTS(aSeeks); i++)
3744 {
3745 offActual = UINT64_MAX;
3746 int rc = RTFileSeek(hFile1, aSeeks[i].offSeek, aSeeks[i].uMethod, &offActual);
3747 if (rc != aSeeks[i].rc)
3748 RTTestIFailed("Seek #%u: Expected %Rrc, got %Rrc", i, aSeeks[i].rc, rc);
3749 if (RT_SUCCESS(rc) && offActual != aSeeks[i].offActual)
3750 RTTestIFailed("Seek #%u: offActual %#RX64, expected %#RX64", i, offActual, aSeeks[i].offActual);
3751 if (RT_SUCCESS(rc))
3752 {
3753 uint64_t offTell = RTFileTell(hFile1);
3754 if (offTell != offActual)
3755 RTTestIFailed("Seek #%u: offActual %#RX64, RTFileTell %#RX64", i, offActual, offTell);
3756 }
3757
3758 if (RT_SUCCESS(rc) && offActual + _2K <= cbFile && iDoReadCheck)
3759 {
3760 uint8_t abBuf[_2K];
3761 RTTESTI_CHECK_RC(rc = RTFileRead(hFile1, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
3762 if (RT_SUCCESS(rc))
3763 {
3764 size_t offMarker = (size_t)(RT_ALIGN_64(offActual, _1K) - offActual);
3765 uint64_t uMarker = *(uint64_t *)&abBuf[offMarker]; /** @todo potentially unaligned access */
3766 if (uMarker != offActual + offMarker)
3767 RTTestIFailed("Seek #%u: Invalid marker value (@ %#RX64): %#RX64, expected %#RX64",
3768 i, offActual, uMarker, offActual + offMarker);
3769
3770 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -(int64_t)sizeof(abBuf), RTFILE_SEEK_CURRENT, NULL), VINF_SUCCESS);
3771 }
3772 }
3773 }
3774 }
3775
3776
3777 /*
3778 * Profile seeking relative to the beginning of the file and relative
3779 * to the end. The latter might be more expensive in a SF context.
3780 */
3781 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? iIteration : iIteration % cbFile, RTFILE_SEEK_BEGIN, NULL),
3782 g_nsTestRun, "RTFileSeek/BEGIN");
3783 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? -(int64_t)iIteration : -(int64_t)(iIteration % cbFile), RTFILE_SEEK_END, NULL),
3784 g_nsTestRun, "RTFileSeek/END");
3785
3786}
3787
3788#ifdef FSPERF_TEST_SENDFILE
3789
3790/**
3791 * Send file thread arguments.
3792 */
3793typedef struct FSPERFSENDFILEARGS
3794{
3795 uint64_t offFile;
3796 size_t cbSend;
3797 uint64_t cbSent;
3798 size_t cbBuf;
3799 uint8_t *pbBuf;
3800 uint8_t bFiller;
3801 bool fCheckBuf;
3802 RTSOCKET hSocket;
3803 uint64_t volatile tsThreadDone;
3804} FSPERFSENDFILEARGS;
3805
3806/** Thread receiving the bytes from a sendfile() call. */
3807static DECLCALLBACK(int) fsPerfSendFileThread(RTTHREAD hSelf, void *pvUser)
3808{
3809 FSPERFSENDFILEARGS *pArgs = (FSPERFSENDFILEARGS *)pvUser;
3810 int rc = VINF_SUCCESS;
3811
3812 if (pArgs->fCheckBuf)
3813 RTTestSetDefault(g_hTest, NULL);
3814
3815 uint64_t cbReceived = 0;
3816 while (cbReceived < pArgs->cbSent)
3817 {
3818 size_t const cbToRead = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbReceived);
3819 size_t cbActual = 0;
3820 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTTcpRead(pArgs->hSocket, pArgs->pbBuf, cbToRead, &cbActual), VINF_SUCCESS);
3821 RTTEST_CHECK_BREAK(g_hTest, cbActual != 0);
3822 RTTEST_CHECK(g_hTest, cbActual <= cbToRead);
3823 if (pArgs->fCheckBuf)
3824 fsPerfCheckReadBuf(__LINE__, pArgs->offFile + cbReceived, pArgs->pbBuf, cbActual, pArgs->bFiller);
3825 cbReceived += cbActual;
3826 }
3827
3828 pArgs->tsThreadDone = RTTimeNanoTS();
3829
3830 if (cbReceived == pArgs->cbSent && RT_SUCCESS(rc))
3831 {
3832 size_t cbActual = 0;
3833 rc = RTSocketReadNB(pArgs->hSocket, pArgs->pbBuf, 1, &cbActual);
3834 if (rc != VINF_SUCCESS && rc != VINF_TRY_AGAIN)
3835 RTTestFailed(g_hTest, "RTSocketReadNB(sendfile client socket) -> %Rrc; expected VINF_SUCCESS or VINF_TRY_AGAIN\n", rc);
3836 else if (cbActual != 0)
3837 RTTestFailed(g_hTest, "sendfile client socket still contains data when done!\n");
3838 }
3839
3840 RTTEST_CHECK_RC(g_hTest, RTSocketClose(pArgs->hSocket), VINF_SUCCESS);
3841 pArgs->hSocket = NIL_RTSOCKET;
3842
3843 RT_NOREF(hSelf);
3844 return rc;
3845}
3846
3847
3848static uint64_t fsPerfSendFileOne(FSPERFSENDFILEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
3849 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckBuf, unsigned iLine)
3850{
3851 /* Copy parameters to the argument structure: */
3852 pArgs->offFile = offFile;
3853 pArgs->cbSend = cbSend;
3854 pArgs->cbSent = cbSent;
3855 pArgs->bFiller = bFiller;
3856 pArgs->fCheckBuf = fCheckBuf;
3857
3858 /* Create a socket pair. */
3859 pArgs->hSocket = NIL_RTSOCKET;
3860 RTSOCKET hServer = NIL_RTSOCKET;
3861 RTTESTI_CHECK_RC_RET(RTTcpCreatePair(&hServer, &pArgs->hSocket, 0), VINF_SUCCESS, 0);
3862
3863 /* Create the receiving thread: */
3864 int rc;
3865 RTTHREAD hThread = NIL_RTTHREAD;
3866 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSendFileThread, pArgs, 0,
3867 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "sendfile"), VINF_SUCCESS);
3868 if (RT_SUCCESS(rc))
3869 {
3870 uint64_t const tsStart = RTTimeNanoTS();
3871
3872# if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
3873 /* SystemV sendfile: */
3874 loff_t offFileSf = pArgs->offFile;
3875 ssize_t cbActual = sendfile((int)RTSocketToNative(hServer), (int)RTFileToNative(hFile1), &offFileSf, pArgs->cbSend);
3876 int const iErr = errno;
3877 if (cbActual < 0)
3878 RTTestIFailed("%u: sendfile(socket, file, &%#X64, %#zx) failed (%zd): %d (%Rrc), offFileSf=%#RX64\n",
3879 iLine, pArgs->offFile, pArgs->cbSend, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileSf);
3880 else if ((uint64_t)cbActual != pArgs->cbSent)
3881 RTTestIFailed("%u: sendfile(socket, file, &%#RX64, %#zx): %#zx, expected %#RX64 (offFileSf=%#RX64)\n",
3882 iLine, pArgs->offFile, pArgs->cbSend, cbActual, pArgs->cbSent, (uint64_t)offFileSf);
3883 else if ((uint64_t)offFileSf != pArgs->offFile + pArgs->cbSent)
3884 RTTestIFailed("%u: sendfile(socket, file, &%#RX64, %#zx): %#zx; offFileSf=%#RX64, expected %#RX64\n",
3885 iLine, pArgs->offFile, pArgs->cbSend, cbActual, (uint64_t)offFileSf, pArgs->offFile + pArgs->cbSent);
3886#else
3887 /* BSD sendfile: */
3888# ifdef SF_SYNC
3889 int fSfFlags = SF_SYNC;
3890# else
3891 int fSfFlags = 0;
3892# endif
3893 off_t cbActual = pArgs->cbSend;
3894 rc = sendfile((int)RTFileToNative(hFile1), (int)RTSocketToNative(hServer),
3895# ifdef RT_OS_DARWIN
3896 pArgs->offFile, &cbActual, NULL, fSfFlags);
3897# else
3898 pArgs->offFile, cbActual, NULL, &cbActual, fSfFlags);
3899# endif
3900 int const iErr = errno;
3901 if (rc != 0)
3902 RTTestIFailed("%u: sendfile(file, socket, %#RX64, %#zx, NULL,, %#x) failed (%d): %d (%Rrc), cbActual=%#RX64\n",
3903 iLine, pArgs->offFile, (size_t)pArgs->cbSend, rc, iErr, RTErrConvertFromErrno(iErr), (uint64_t)cbActual);
3904 if ((uint64_t)cbActual != pArgs->cbSent)
3905 RTTestIFailed("%u: sendfile(file, socket, %#RX64, %#zx, NULL,, %#x): cbActual=%#RX64, expected %#RX64 (rc=%d, errno=%d)\n",
3906 iLine, pArgs->offFile, (size_t)pArgs->cbSend, (uint64_t)cbActual, pArgs->cbSent, rc, iErr);
3907# endif
3908 RTTESTI_CHECK_RC(RTSocketClose(hServer), VINF_SUCCESS);
3909 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
3910
3911 if (pArgs->tsThreadDone >= tsStart)
3912 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
3913 }
3914 return 0;
3915}
3916
3917
3918static void fsPerfSendFile(RTFILE hFile1, uint64_t cbFile)
3919{
3920 RTTestISub("sendfile");
3921# ifdef RT_OS_LINUX
3922 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - g_fPageOffset);
3923# else
3924 uint64_t const cbFileMax = RT_MIN(cbFile, SSIZE_MAX - g_fPageOffset);
3925# endif
3926 signal(SIGPIPE, SIG_IGN);
3927
3928 /*
3929 * Allocate a buffer.
3930 */
3931 FSPERFSENDFILEARGS Args;
3932 Args.cbBuf = RT_MIN(RT_MIN(cbFileMax, _16M), g_cbMaxBuffer);
3933 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
3934 while (!Args.pbBuf)
3935 {
3936 Args.cbBuf /= 8;
3937 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
3938 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
3939 }
3940
3941 /*
3942 * First iteration with default buffer content.
3943 */
3944 fsPerfSendFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
3945 if (cbFileMax == cbFile)
3946 fsPerfSendFileOne(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
3947 else
3948 fsPerfSendFileOne(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
3949
3950 /*
3951 * Write a block using the regular API and then send it, checking that
3952 * the any caching that sendfile does is correctly updated.
3953 */
3954 uint8_t bFiller = 0xf6;
3955 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
3956 do
3957 {
3958 fsPerfSendFileOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
3959
3960 bFiller += 1;
3961 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
3962 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
3963
3964 fsPerfSendFileOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
3965
3966 cbToSend /= 2;
3967 } while (cbToSend >= g_cbPage && ((unsigned)bFiller - 0xf7U) < 64);
3968
3969 /*
3970 * Restore buffer content
3971 */
3972 bFiller = 0xf6;
3973 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
3974 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
3975
3976 /*
3977 * Do 128 random sends.
3978 */
3979 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
3980 for (uint32_t iTest = 0; iTest < 128; iTest++)
3981 {
3982 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
3983 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
3984 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
3985
3986 fsPerfSendFileOne(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
3987 }
3988
3989 /*
3990 * Benchmark it.
3991 */
3992 uint32_t cIterations = 0;
3993 uint64_t nsElapsed = 0;
3994 for (;;)
3995 {
3996 uint64_t cNsThis = fsPerfSendFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
3997 nsElapsed += cNsThis;
3998 cIterations++;
3999 if (!cNsThis || nsElapsed >= g_nsTestRun)
4000 break;
4001 }
4002 uint64_t cbTotal = cbFileMax * cIterations;
4003 RTTestIValue("latency", nsElapsed / cIterations, RTTESTUNIT_NS_PER_CALL);
4004 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
4005 RTTestIValue("calls", cIterations, RTTESTUNIT_CALLS);
4006 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
4007 if (g_fShowDuration)
4008 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
4009
4010 /*
4011 * Cleanup.
4012 */
4013 RTMemFree(Args.pbBuf);
4014}
4015
4016#endif /* FSPERF_TEST_SENDFILE */
4017#ifdef RT_OS_LINUX
4018
4019#ifndef __NR_splice
4020# if defined(RT_ARCH_AMD64)
4021# define __NR_splice 275
4022# elif defined(RT_ARCH_X86)
4023# define __NR_splice 313
4024# else
4025# error "fix me"
4026# endif
4027#endif
4028
4029/** FsPerf is built against ancient glibc, so make the splice syscall ourselves. */
4030DECLINLINE(ssize_t) syscall_splice(int fdIn, loff_t *poffIn, int fdOut, loff_t *poffOut, size_t cbChunk, unsigned fFlags)
4031{
4032 return syscall(__NR_splice, fdIn, poffIn, fdOut, poffOut, cbChunk, fFlags);
4033}
4034
4035
4036/**
4037 * Send file thread arguments.
4038 */
4039typedef struct FSPERFSPLICEARGS
4040{
4041 uint64_t offFile;
4042 size_t cbSend;
4043 uint64_t cbSent;
4044 size_t cbBuf;
4045 uint8_t *pbBuf;
4046 uint8_t bFiller;
4047 bool fCheckBuf;
4048 uint32_t cCalls;
4049 RTPIPE hPipe;
4050 uint64_t volatile tsThreadDone;
4051} FSPERFSPLICEARGS;
4052
4053
4054/** Thread receiving the bytes from a splice() call. */
4055static DECLCALLBACK(int) fsPerfSpliceToPipeThread(RTTHREAD hSelf, void *pvUser)
4056{
4057 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
4058 int rc = VINF_SUCCESS;
4059
4060 if (pArgs->fCheckBuf)
4061 RTTestSetDefault(g_hTest, NULL);
4062
4063 uint64_t cbReceived = 0;
4064 while (cbReceived < pArgs->cbSent)
4065 {
4066 size_t const cbToRead = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbReceived);
4067 size_t cbActual = 0;
4068 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeReadBlocking(pArgs->hPipe, pArgs->pbBuf, cbToRead, &cbActual), VINF_SUCCESS);
4069 RTTEST_CHECK_BREAK(g_hTest, cbActual != 0);
4070 RTTEST_CHECK(g_hTest, cbActual <= cbToRead);
4071 if (pArgs->fCheckBuf)
4072 fsPerfCheckReadBuf(__LINE__, pArgs->offFile + cbReceived, pArgs->pbBuf, cbActual, pArgs->bFiller);
4073 cbReceived += cbActual;
4074 }
4075
4076 pArgs->tsThreadDone = RTTimeNanoTS();
4077
4078 if (cbReceived == pArgs->cbSent && RT_SUCCESS(rc))
4079 {
4080 size_t cbActual = 0;
4081 rc = RTPipeRead(pArgs->hPipe, pArgs->pbBuf, 1, &cbActual);
4082 if (rc != VINF_SUCCESS && rc != VINF_TRY_AGAIN && rc != VERR_BROKEN_PIPE)
4083 RTTestFailed(g_hTest, "RTPipeReadBlocking() -> %Rrc; expected VINF_SUCCESS or VINF_TRY_AGAIN\n", rc);
4084 else if (cbActual != 0)
4085 RTTestFailed(g_hTest, "splice read pipe still contains data when done!\n");
4086 }
4087
4088 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
4089 pArgs->hPipe = NIL_RTPIPE;
4090
4091 RT_NOREF(hSelf);
4092 return rc;
4093}
4094
4095
4096/** Sends hFile1 to a pipe via the Linux-specific splice() syscall. */
4097static uint64_t fsPerfSpliceToPipeOne(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
4098 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckBuf, unsigned iLine)
4099{
4100 /* Copy parameters to the argument structure: */
4101 pArgs->offFile = offFile;
4102 pArgs->cbSend = cbSend;
4103 pArgs->cbSent = cbSent;
4104 pArgs->bFiller = bFiller;
4105 pArgs->fCheckBuf = fCheckBuf;
4106
4107 /* Create a socket pair. */
4108 pArgs->hPipe = NIL_RTPIPE;
4109 RTPIPE hPipeW = NIL_RTPIPE;
4110 RTTESTI_CHECK_RC_RET(RTPipeCreate(&pArgs->hPipe, &hPipeW, 0 /*fFlags*/), VINF_SUCCESS, 0);
4111
4112 /* Create the receiving thread: */
4113 int rc;
4114 RTTHREAD hThread = NIL_RTTHREAD;
4115 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceToPipeThread, pArgs, 0,
4116 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
4117 if (RT_SUCCESS(rc))
4118 {
4119 uint64_t const tsStart = RTTimeNanoTS();
4120 size_t cbLeft = cbSend;
4121 size_t cbTotal = 0;
4122 do
4123 {
4124 loff_t offFileIn = offFile;
4125 ssize_t cbActual = syscall_splice((int)RTFileToNative(hFile1), &offFileIn, (int)RTPipeToNative(hPipeW), NULL,
4126 cbLeft, 0 /*fFlags*/);
4127 int const iErr = errno;
4128 if (RT_UNLIKELY(cbActual < 0))
4129 {
4130 if (iErr == EPIPE && cbTotal == pArgs->cbSent)
4131 break;
4132 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0) failed (%zd): %d (%Rrc), offFileIn=%#RX64\n",
4133 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileIn);
4134 break;
4135 }
4136 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
4137 if ((uint64_t)offFileIn != offFile + (uint64_t)cbActual)
4138 {
4139 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0): %#zx; offFileIn=%#RX64, expected %#RX64\n",
4140 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileIn, offFile + (uint64_t)cbActual);
4141 break;
4142 }
4143 if (cbActual > 0)
4144 {
4145 pArgs->cCalls++;
4146 offFile += (size_t)cbActual;
4147 cbTotal += (size_t)cbActual;
4148 cbLeft -= (size_t)cbActual;
4149 }
4150 else
4151 break;
4152 } while (cbLeft > 0);
4153
4154 if (cbTotal != pArgs->cbSent)
4155 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
4156
4157 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
4158 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
4159
4160 if (pArgs->tsThreadDone >= tsStart)
4161 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
4162 }
4163 return 0;
4164}
4165
4166
4167static void fsPerfSpliceToPipe(RTFILE hFile1, uint64_t cbFile)
4168{
4169 RTTestISub("splice/to-pipe");
4170
4171 /*
4172 * splice was introduced in 2.6.17 according to the man-page.
4173 */
4174 char szRelease[64];
4175 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
4176 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
4177 {
4178 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
4179 return;
4180 }
4181
4182 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - g_fPageOffset);
4183 signal(SIGPIPE, SIG_IGN);
4184
4185 /*
4186 * Allocate a buffer.
4187 */
4188 FSPERFSPLICEARGS Args;
4189 Args.cbBuf = RT_MIN(RT_MIN(cbFileMax, _16M), g_cbMaxBuffer);
4190 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4191 while (!Args.pbBuf)
4192 {
4193 Args.cbBuf /= 8;
4194 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
4195 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4196 }
4197
4198 /*
4199 * First iteration with default buffer content.
4200 */
4201 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
4202 if (cbFileMax == cbFile)
4203 fsPerfSpliceToPipeOne(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
4204 else
4205 fsPerfSpliceToPipeOne(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
4206
4207 /*
4208 * Write a block using the regular API and then send it, checking that
4209 * the any caching that sendfile does is correctly updated.
4210 */
4211 uint8_t bFiller = 0xf6;
4212 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
4213 do
4214 {
4215 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
4216
4217 bFiller += 1;
4218 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
4219 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
4220
4221 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
4222
4223 cbToSend /= 2;
4224 } while (cbToSend >= g_cbPage && ((unsigned)bFiller - 0xf7U) < 64);
4225
4226 /*
4227 * Restore buffer content
4228 */
4229 bFiller = 0xf6;
4230 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
4231 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
4232
4233 /*
4234 * Do 128 random sends.
4235 */
4236 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
4237 for (uint32_t iTest = 0; iTest < 128; iTest++)
4238 {
4239 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
4240 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
4241 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
4242
4243 fsPerfSpliceToPipeOne(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
4244 }
4245
4246 /*
4247 * Benchmark it.
4248 */
4249 Args.cCalls = 0;
4250 uint32_t cIterations = 0;
4251 uint64_t nsElapsed = 0;
4252 for (;;)
4253 {
4254 uint64_t cNsThis = fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
4255 nsElapsed += cNsThis;
4256 cIterations++;
4257 if (!cNsThis || nsElapsed >= g_nsTestRun)
4258 break;
4259 }
4260 uint64_t cbTotal = cbFileMax * cIterations;
4261 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
4262 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
4263 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
4264 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
4265 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
4266 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
4267 if (g_fShowDuration)
4268 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
4269
4270 /*
4271 * Cleanup.
4272 */
4273 RTMemFree(Args.pbBuf);
4274}
4275
4276
4277/** Thread sending the bytes to a splice() call. */
4278static DECLCALLBACK(int) fsPerfSpliceToFileThread(RTTHREAD hSelf, void *pvUser)
4279{
4280 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
4281 int rc = VINF_SUCCESS;
4282
4283 uint64_t offFile = pArgs->offFile;
4284 uint64_t cbTotalSent = 0;
4285 while (cbTotalSent < pArgs->cbSent)
4286 {
4287 size_t const cbToSend = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbTotalSent);
4288 fsPerfFillWriteBuf(offFile, pArgs->pbBuf, cbToSend, pArgs->bFiller);
4289 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeWriteBlocking(pArgs->hPipe, pArgs->pbBuf, cbToSend, NULL), VINF_SUCCESS);
4290 offFile += cbToSend;
4291 cbTotalSent += cbToSend;
4292 }
4293
4294 pArgs->tsThreadDone = RTTimeNanoTS();
4295
4296 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
4297 pArgs->hPipe = NIL_RTPIPE;
4298
4299 RT_NOREF(hSelf);
4300 return rc;
4301}
4302
4303
4304/** Fill hFile1 via a pipe and the Linux-specific splice() syscall. */
4305static uint64_t fsPerfSpliceToFileOne(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
4306 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckFile, unsigned iLine)
4307{
4308 /* Copy parameters to the argument structure: */
4309 pArgs->offFile = offFile;
4310 pArgs->cbSend = cbSend;
4311 pArgs->cbSent = cbSent;
4312 pArgs->bFiller = bFiller;
4313 pArgs->fCheckBuf = false;
4314
4315 /* Create a socket pair. */
4316 pArgs->hPipe = NIL_RTPIPE;
4317 RTPIPE hPipeR = NIL_RTPIPE;
4318 RTTESTI_CHECK_RC_RET(RTPipeCreate(&hPipeR, &pArgs->hPipe, 0 /*fFlags*/), VINF_SUCCESS, 0);
4319
4320 /* Create the receiving thread: */
4321 int rc;
4322 RTTHREAD hThread = NIL_RTTHREAD;
4323 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceToFileThread, pArgs, 0,
4324 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
4325 if (RT_SUCCESS(rc))
4326 {
4327 /*
4328 * Do the splicing.
4329 */
4330 uint64_t const tsStart = RTTimeNanoTS();
4331 size_t cbLeft = cbSend;
4332 size_t cbTotal = 0;
4333 do
4334 {
4335 loff_t offFileOut = offFile;
4336 ssize_t cbActual = syscall_splice((int)RTPipeToNative(hPipeR), NULL, (int)RTFileToNative(hFile1), &offFileOut,
4337 cbLeft, 0 /*fFlags*/);
4338 int const iErr = errno;
4339 if (RT_UNLIKELY(cbActual < 0))
4340 {
4341 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0) failed (%zd): %d (%Rrc), offFileOut=%#RX64\n",
4342 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileOut);
4343 break;
4344 }
4345 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
4346 if ((uint64_t)offFileOut != offFile + (uint64_t)cbActual)
4347 {
4348 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0): %#zx; offFileOut=%#RX64, expected %#RX64\n",
4349 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileOut, offFile + (uint64_t)cbActual);
4350 break;
4351 }
4352 if (cbActual > 0)
4353 {
4354 pArgs->cCalls++;
4355 offFile += (size_t)cbActual;
4356 cbTotal += (size_t)cbActual;
4357 cbLeft -= (size_t)cbActual;
4358 }
4359 else
4360 break;
4361 } while (cbLeft > 0);
4362 uint64_t const nsElapsed = RTTimeNanoTS() - tsStart;
4363
4364 if (cbTotal != pArgs->cbSent)
4365 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
4366
4367 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
4368 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
4369
4370 /* Check the file content. */
4371 if (fCheckFile && cbTotal == pArgs->cbSent)
4372 {
4373 offFile = pArgs->offFile;
4374 cbLeft = cbSent;
4375 while (cbLeft > 0)
4376 {
4377 size_t cbToRead = RT_MIN(cbLeft, pArgs->cbBuf);
4378 RTTESTI_CHECK_RC_BREAK(RTFileReadAt(hFile1, offFile, pArgs->pbBuf, cbToRead, NULL), VINF_SUCCESS);
4379 if (!fsPerfCheckReadBuf(iLine, offFile, pArgs->pbBuf, cbToRead, pArgs->bFiller))
4380 break;
4381 offFile += cbToRead;
4382 cbLeft -= cbToRead;
4383 }
4384 }
4385 return nsElapsed;
4386 }
4387 return 0;
4388}
4389
4390
4391static void fsPerfSpliceToFile(RTFILE hFile1, uint64_t cbFile)
4392{
4393 RTTestISub("splice/to-file");
4394
4395 /*
4396 * splice was introduced in 2.6.17 according to the man-page.
4397 */
4398 char szRelease[64];
4399 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
4400 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
4401 {
4402 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
4403 return;
4404 }
4405
4406 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - g_fPageOffset);
4407 signal(SIGPIPE, SIG_IGN);
4408
4409 /*
4410 * Allocate a buffer.
4411 */
4412 FSPERFSPLICEARGS Args;
4413 Args.cbBuf = RT_MIN(RT_MIN(cbFileMax, _16M), g_cbMaxBuffer);
4414 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4415 while (!Args.pbBuf)
4416 {
4417 Args.cbBuf /= 8;
4418 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
4419 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4420 }
4421
4422 /*
4423 * Do the whole file.
4424 */
4425 uint8_t bFiller = 0x76;
4426 fsPerfSpliceToFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, bFiller, true /*fCheckFile*/, __LINE__);
4427
4428 /*
4429 * Do 64 random chunks (this is slower).
4430 */
4431 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
4432 for (uint32_t iTest = 0; iTest < 64; iTest++)
4433 {
4434 size_t const cbToWrite = (size_t)RTRandU64Ex(1, iTest < 24 ? cbSmall : cbFileMax);
4435 uint64_t const offToWriteAt = RTRandU64Ex(0, cbFile - cbToWrite);
4436 uint64_t const cbTryRead = cbToWrite + (iTest & 1 ? RTRandU32Ex(0, _64K) : 0);
4437
4438 bFiller++;
4439 fsPerfSpliceToFileOne(&Args, hFile1, offToWriteAt, cbTryRead, cbToWrite, bFiller, true /*fCheckFile*/, __LINE__);
4440 }
4441
4442 /*
4443 * Benchmark it.
4444 */
4445 Args.cCalls = 0;
4446 uint32_t cIterations = 0;
4447 uint64_t nsElapsed = 0;
4448 for (;;)
4449 {
4450 uint64_t cNsThis = fsPerfSpliceToFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
4451 nsElapsed += cNsThis;
4452 cIterations++;
4453 if (!cNsThis || nsElapsed >= g_nsTestRun)
4454 break;
4455 }
4456 uint64_t cbTotal = cbFileMax * cIterations;
4457 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
4458 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
4459 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
4460 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
4461 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
4462 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
4463 if (g_fShowDuration)
4464 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
4465
4466 /*
4467 * Cleanup.
4468 */
4469 RTMemFree(Args.pbBuf);
4470}
4471
4472#endif /* RT_OS_LINUX */
4473
4474/** For fsPerfIoRead and fsPerfIoWrite. */
4475#define PROFILE_IO_FN(a_szOperation, a_fnCall) \
4476 do \
4477 { \
4478 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); \
4479 uint64_t offActual = 0; \
4480 uint32_t cSeeks = 0; \
4481 \
4482 /* Estimate how many iterations we need to fill up the given timeslot: */ \
4483 fsPerfYield(); \
4484 uint64_t nsStart = RTTimeNanoTS(); \
4485 uint64_t ns; \
4486 do \
4487 ns = RTTimeNanoTS(); \
4488 while (ns == nsStart); \
4489 nsStart = ns; \
4490 \
4491 uint64_t iIteration = 0; \
4492 do \
4493 { \
4494 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4495 iIteration++; \
4496 ns = RTTimeNanoTS() - nsStart; \
4497 } while (ns < RT_NS_10MS); \
4498 ns /= iIteration; \
4499 if (ns > g_nsPerNanoTSCall + 32) \
4500 ns -= g_nsPerNanoTSCall; \
4501 uint64_t cIterations = g_nsTestRun / ns; \
4502 if (cIterations < 2) \
4503 cIterations = 2; \
4504 else if (cIterations & 1) \
4505 cIterations++; \
4506 \
4507 /* Do the actual profiling: */ \
4508 cSeeks = 0; \
4509 iIteration = 0; \
4510 fsPerfYield(); \
4511 nsStart = RTTimeNanoTS(); \
4512 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
4513 { \
4514 for (; iIteration < cIterations; iIteration++)\
4515 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4516 ns = RTTimeNanoTS() - nsStart;\
4517 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
4518 break; \
4519 cIterations += cIterations / 4; \
4520 if (cIterations & 1) \
4521 cIterations++; \
4522 nsStart += g_nsPerNanoTSCall; \
4523 } \
4524 RTTestIValueF(ns / iIteration, \
4525 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation "/seq/%RU32 latency", cbBlock); \
4526 RTTestIValueF((uint64_t)((double)(iIteration * cbBlock) / ((double)ns / RT_NS_1SEC)), \
4527 RTTESTUNIT_BYTES_PER_SEC, a_szOperation "/seq/%RU32 throughput", cbBlock); \
4528 RTTestIValueF(iIteration, \
4529 RTTESTUNIT_CALLS, a_szOperation "/seq/%RU32 calls", cbBlock); \
4530 RTTestIValueF((uint64_t)iIteration * cbBlock, \
4531 RTTESTUNIT_BYTES, a_szOperation "/seq/%RU32 bytes", cbBlock); \
4532 RTTestIValueF(cSeeks, \
4533 RTTESTUNIT_OCCURRENCES, a_szOperation "/seq/%RU32 seeks", cbBlock); \
4534 if (g_fShowDuration) \
4535 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation "/seq/%RU32 duration", cbBlock); \
4536 } while (0)
4537
4538
4539/**
4540 * One RTFileRead profiling iteration.
4541 */
4542DECL_FORCE_INLINE(int) fsPerfIoReadWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
4543 uint64_t *poffActual, uint32_t *pcSeeks)
4544{
4545 /* Do we need to seek back to the start? */
4546 if (*poffActual + cbBlock <= cbFile)
4547 { /* likely */ }
4548 else
4549 {
4550 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
4551 *pcSeeks += 1;
4552 *poffActual = 0;
4553 }
4554
4555 size_t cbActuallyRead = 0;
4556 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBlock, cbBlock, &cbActuallyRead), VINF_SUCCESS, rcCheck);
4557 if (cbActuallyRead == cbBlock)
4558 {
4559 *poffActual += cbActuallyRead;
4560 return VINF_SUCCESS;
4561 }
4562 RTTestIFailed("RTFileRead at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyRead, cbBlock);
4563 *poffActual += cbActuallyRead;
4564 return VERR_READ_ERROR;
4565}
4566
4567
4568static void fsPerfIoReadBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
4569{
4570 RTTestISubF("IO - Sequential read %RU32", cbBlock);
4571 if (cbBlock <= cbFile)
4572 {
4573
4574 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
4575 if (pbBuf)
4576 {
4577 memset(pbBuf, 0xf7, cbBlock);
4578 PROFILE_IO_FN("RTFileRead", fsPerfIoReadWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
4579 RTMemPageFree(pbBuf, cbBlock);
4580 }
4581 else
4582 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
4583 }
4584 else
4585 RTTestSkipped(g_hTest, "test file too small");
4586}
4587
4588
4589/** preadv is too new to be useful, so we use the readv api via this wrapper. */
4590DECLINLINE(int) myFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
4591{
4592 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
4593 if (RT_SUCCESS(rc))
4594 rc = RTFileSgRead(hFile, pSgBuf, cbToRead, pcbRead);
4595 return rc;
4596}
4597
4598
4599static void fsPerfRead(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
4600{
4601 RTTestISubF("IO - RTFileRead");
4602
4603 /*
4604 * Allocate a big buffer we can play around with. Min size is 1MB.
4605 */
4606 size_t cbMaxBuf = RT_MIN(_64M, g_cbMaxBuffer);
4607 size_t cbBuf = cbFile < cbMaxBuf ? (size_t)cbFile : cbMaxBuf;
4608 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
4609 while (!pbBuf)
4610 {
4611 cbBuf /= 2;
4612 RTTESTI_CHECK_RETV(cbBuf >= _1M);
4613 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
4614 }
4615
4616#if 1
4617 /*
4618 * Start at the beginning and read the full buffer in random small chunks, thereby
4619 * checking that unaligned buffer addresses, size and file offsets work fine.
4620 */
4621 struct
4622 {
4623 uint64_t offFile;
4624 uint32_t cbMax;
4625 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
4626 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++)
4627 {
4628 memset(pbBuf, 0x55, cbBuf);
4629 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4630 for (size_t offBuf = 0; offBuf < cbBuf; )
4631 {
4632 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
4633 uint32_t const cbToRead = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
4634 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
4635 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
4636 size_t cbActual = 0;
4637 RTTESTI_CHECK_RC(RTFileRead(hFile1, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
4638 if (cbActual == cbToRead)
4639 {
4640 offBuf += cbActual;
4641 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
4642 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
4643 }
4644 else
4645 {
4646 RTTestIFailed("Attempting to read %#x bytes at %#zx, only got %#x bytes back! (cbLeft=%#x cbBuf=%#zx)\n",
4647 cbToRead, offBuf, cbActual, cbLeft, cbBuf);
4648 if (cbActual)
4649 offBuf += cbActual;
4650 else
4651 pbBuf[offBuf++] = 0x11;
4652 }
4653 }
4654 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf);
4655 }
4656
4657 /*
4658 * Test reading beyond the end of the file.
4659 */
4660 size_t const acbMax[] = { cbBuf, _64K, _16K, _4K, 256 };
4661 uint32_t const aoffFromEos[] =
4662 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 63, 64, 127, 128, 255, 254, 256, 1023, 1024, 2048,
4663 4092, 4093, 4094, 4095, 4096, 4097, 4098, 4099, 4100, 8192, 16384, 32767, 32768, 32769, 65535, 65536, _1M - 1
4664 };
4665 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
4666 {
4667 size_t const cbMaxRead = acbMax[iMax];
4668 for (uint32_t iOffFromEos = 0; iOffFromEos < RT_ELEMENTS(aoffFromEos); iOffFromEos++)
4669 {
4670 uint32_t off = aoffFromEos[iOffFromEos];
4671 if (off >= cbMaxRead)
4672 continue;
4673 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4674 size_t cbActual = ~(size_t)0;
4675 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
4676 RTTESTI_CHECK(cbActual == off);
4677
4678 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4679 cbActual = ~(size_t)0;
4680 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, off, &cbActual), VINF_SUCCESS);
4681 RTTESTI_CHECK_MSG(cbActual == off, ("%#zx vs %#zx\n", cbActual, off));
4682
4683 cbActual = ~(size_t)0;
4684 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, 1, &cbActual), VINF_SUCCESS);
4685 RTTESTI_CHECK_MSG(cbActual == 0, ("cbActual=%zu\n", cbActual));
4686
4687 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
4688
4689 /* Repeat using native APIs in case IPRT or other layers hide status codes: */
4690#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
4691 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4692# ifdef RT_OS_OS2
4693 ULONG cbActual2 = ~(ULONG)0;
4694 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
4695 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
4696 RTTESTI_CHECK_MSG(cbActual2 == off, ("%#x vs %#x\n", cbActual2, off));
4697# else
4698 IO_STATUS_BLOCK const IosVirgin = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4699 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4700 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4701 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
4702 if (off == 0)
4703 {
4704 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
4705 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/ || Ios.Status == STATUS_END_OF_FILE /*fastio?*/,
4706 ("%#x vs %x/%#x; off=%#x\n", Ios.Status, IosVirgin.Status, STATUS_END_OF_FILE, off));
4707 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/ || Ios.Information == 0 /*fastio?*/,
4708 ("%#zx vs %zx/0; off=%#x\n", Ios.Information, IosVirgin.Information, off));
4709 }
4710 else
4711 {
4712 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x, expected 0 (off=%#x cbMaxRead=%#zx)\n", rcNt, off, cbMaxRead));
4713 RTTESTI_CHECK_MSG(Ios.Status == STATUS_SUCCESS, ("%#x; off=%#x\n", Ios.Status, off));
4714 RTTESTI_CHECK_MSG(Ios.Information == off, ("%#zx vs %#x\n", Ios.Information, off));
4715 }
4716# endif
4717
4718# ifdef RT_OS_OS2
4719 cbActual2 = ~(ULONG)0;
4720 orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, 1, &cbActual2);
4721 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
4722 RTTESTI_CHECK_MSG(cbActual2 == 0, ("cbActual2=%u\n", cbActual2));
4723# else
4724 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
4725 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4726 &Ios, pbBuf, 1, NULL /*poffFile*/, NULL /*Key*/);
4727 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
4728# endif
4729
4730#endif
4731 }
4732 }
4733
4734 /*
4735 * Test reading beyond end of the file.
4736 */
4737 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
4738 {
4739 size_t const cbMaxRead = acbMax[iMax];
4740 for (uint32_t off = 0; off < 256; off++)
4741 {
4742 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4743 size_t cbActual = ~(size_t)0;
4744 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
4745 RTTESTI_CHECK(cbActual == 0);
4746
4747 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
4748
4749 /* Repeat using native APIs in case IPRT or other layers hid status codes: */
4750#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
4751 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4752# ifdef RT_OS_OS2
4753 ULONG cbActual2 = ~(ULONG)0;
4754 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
4755 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
4756 RTTESTI_CHECK_MSG(cbActual2 == 0, ("%#x vs %#x\n", cbActual2, off));
4757# else
4758 IO_STATUS_BLOCK const IosVirgin = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4759 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4760 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4761 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
4762 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
4763 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/ || Ios.Status == STATUS_END_OF_FILE /*fastio?*/,
4764 ("%#x vs %x/%#x; off=%#x\n", Ios.Status, IosVirgin.Status, STATUS_END_OF_FILE, off));
4765 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/ || Ios.Information == 0 /*fastio?*/,
4766 ("%#zx vs %zx/0; off=%#x\n", Ios.Information, IosVirgin.Information, off));
4767
4768 /* Need to work with sector size on uncached, but might be worth it for non-fastio path. */
4769 uint32_t cbSector = 0x1000;
4770 uint32_t off2 = off * cbSector + (cbFile & (cbSector - 1) ? cbSector - (cbFile & (cbSector - 1)) : 0);
4771 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, cbFile + off2, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4772 size_t const cbMaxRead2 = RT_ALIGN_Z(cbMaxRead, cbSector);
4773 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
4774 rcNt = NtReadFile((HANDLE)RTFileToNative(hFileNoCache), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4775 &Ios, pbBuf, (ULONG)cbMaxRead2, NULL /*poffFile*/, NULL /*Key*/);
4776 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE,
4777 ("rcNt=%#x, expected %#x; off2=%x cbMaxRead2=%#x\n", rcNt, STATUS_END_OF_FILE, off2, cbMaxRead2));
4778 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/,
4779 ("%#x vs %x; off2=%#x cbMaxRead2=%#x\n", Ios.Status, IosVirgin.Status, off2, cbMaxRead2));
4780 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/,
4781 ("%#zx vs %zx; off2=%#x cbMaxRead2=%#x\n", Ios.Information, IosVirgin.Information, off2, cbMaxRead2));
4782# endif
4783#endif
4784 }
4785 }
4786
4787 /*
4788 * Do uncached access, must be page aligned.
4789 */
4790 memset(pbBuf, 0x66, cbBuf);
4791 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
4792 {
4793 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4794 for (size_t offBuf = 0; offBuf < cbBuf; )
4795 {
4796 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / g_cbPage);
4797 uint32_t const cPagesToRead = RTRandU32Ex(1, cPagesLeft);
4798 size_t const cbToRead = cPagesToRead * (size_t)g_cbPage;
4799 size_t cbActual = 0;
4800 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
4801 if (cbActual == cbToRead)
4802 offBuf += cbActual;
4803 else
4804 {
4805 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x bytes back!\n", cbToRead, offBuf, cbActual);
4806 if (cbActual)
4807 offBuf += cbActual;
4808 else
4809 {
4810 memset(&pbBuf[offBuf], 0x11, g_cbPage);
4811 offBuf += g_cbPage;
4812 }
4813 }
4814 }
4815 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf);
4816 }
4817
4818 /*
4819 * Check reading zero bytes at the end of the file.
4820 * Requires native call because RTFileWrite doesn't call kernel on zero byte reads.
4821 */
4822 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
4823# ifdef RT_OS_WINDOWS
4824 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4825 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
4826 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
4827 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
4828 RTTESTI_CHECK(Ios.Information == 0);
4829
4830 IO_STATUS_BLOCK const IosVirgin = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4831 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
4832 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 1, NULL, NULL);
4833 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x", rcNt));
4834 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/ || Ios.Status == STATUS_END_OF_FILE /*fastio?*/,
4835 ("%#x vs %x/%#x\n", Ios.Status, IosVirgin.Status, STATUS_END_OF_FILE));
4836 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/ || Ios.Information == 0 /*fastio?*/,
4837 ("%#zx vs %zx/0\n", Ios.Information, IosVirgin.Information));
4838# else
4839 ssize_t cbRead = read((int)RTFileToNative(hFile1), pbBuf, 0);
4840 RTTESTI_CHECK(cbRead == 0);
4841# endif
4842
4843#else
4844 RT_NOREF(hFileNoCache);
4845#endif
4846
4847 /*
4848 * Scatter read function operation.
4849 */
4850#ifdef RT_OS_WINDOWS
4851 /** @todo RTFileSgReadAt is just a RTFileReadAt loop for windows NT. Need
4852 * to use ReadFileScatter (nocache + page aligned). */
4853#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
4854
4855# ifdef UIO_MAXIOV
4856 RTSGSEG aSegs[UIO_MAXIOV];
4857# else
4858 RTSGSEG aSegs[512];
4859# endif
4860 RTSGBUF SgBuf;
4861 uint32_t cIncr = 1;
4862 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr)
4863 {
4864 size_t const cbSeg = cbBuf / cSegs;
4865 size_t const cbToRead = cbSeg * cSegs;
4866 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4867 {
4868 aSegs[iSeg].cbSeg = cbSeg;
4869 aSegs[iSeg].pvSeg = &pbBuf[cbToRead - (iSeg + 1) * cbSeg];
4870 }
4871 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4872 int rc = myFileSgReadAt(hFile1, 0, &SgBuf, cbToRead, NULL);
4873 if (RT_SUCCESS(rc))
4874 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4875 {
4876 if (!fsPerfCheckReadBuf(__LINE__, iSeg * cbSeg, &pbBuf[cbToRead - (iSeg + 1) * cbSeg], cbSeg))
4877 {
4878 cSegs = RT_ELEMENTS(aSegs);
4879 break;
4880 }
4881 }
4882 else
4883 {
4884 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToRead=%#zx", rc, cSegs, cbSeg, cbToRead);
4885 break;
4886 }
4887 if (cSegs == 16)
4888 cIncr = 7;
4889 else if (cSegs == 16 * 7 + 16 /*= 128*/)
4890 cIncr = 64;
4891 }
4892
4893 for (uint32_t iTest = 0; iTest < 128; iTest++)
4894 {
4895 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
4896 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
4897 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
4898 size_t cbToRead = 0;
4899 size_t cbLeft = cbBuf;
4900 uint8_t *pbCur = &pbBuf[cbBuf];
4901 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4902 {
4903 uint32_t iAlign = RTRandU32Ex(0, 3);
4904 if (iAlign & 2) /* end is page aligned */
4905 {
4906 cbLeft -= (uintptr_t)pbCur & g_fPageOffset;
4907 pbCur -= (uintptr_t)pbCur & g_fPageOffset;
4908 }
4909
4910 size_t cbSegOthers = (cSegs - iSeg) * _8K;
4911 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
4912 : cbLeft > cSegs ? cbLeft - cSegs
4913 : cbLeft;
4914 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
4915 if (iAlign & 1) /* start is page aligned */
4916 cbSeg += ((uintptr_t)pbCur - cbSeg) & g_fPageOffset;
4917
4918 if (iSeg - iZeroSeg < cZeroSegs)
4919 cbSeg = 0;
4920
4921 cbToRead += cbSeg;
4922 cbLeft -= cbSeg;
4923 pbCur -= cbSeg;
4924 aSegs[iSeg].cbSeg = cbSeg;
4925 aSegs[iSeg].pvSeg = pbCur;
4926 }
4927
4928 uint64_t offFile = cbToRead < cbFile ? RTRandU64Ex(0, cbFile - cbToRead) : 0;
4929 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4930 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
4931 if (RT_SUCCESS(rc))
4932 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4933 {
4934 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg))
4935 {
4936 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbToRead=%#zx\n", iSeg, cSegs, aSegs[iSeg].cbSeg, cbToRead);
4937 iTest = _16K;
4938 break;
4939 }
4940 offFile += aSegs[iSeg].cbSeg;
4941 }
4942 else
4943 {
4944 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx", rc, cSegs, cbToRead);
4945 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4946 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
4947 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
4948 break;
4949 }
4950 }
4951
4952 /* reading beyond the end of the file */
4953 for (uint32_t cSegs = 1; cSegs < 6; cSegs++)
4954 for (uint32_t iTest = 0; iTest < 128; iTest++)
4955 {
4956 uint32_t const cbToRead = RTRandU32Ex(0, cbBuf);
4957 uint32_t const cbBeyond = cbToRead ? RTRandU32Ex(0, cbToRead) : 0;
4958 uint32_t const cbSeg = cbToRead / cSegs;
4959 uint32_t cbLeft = cbToRead;
4960 uint8_t *pbCur = &pbBuf[cbToRead];
4961 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4962 {
4963 aSegs[iSeg].cbSeg = iSeg + 1 < cSegs ? cbSeg : cbLeft;
4964 aSegs[iSeg].pvSeg = pbCur -= aSegs[iSeg].cbSeg;
4965 cbLeft -= aSegs[iSeg].cbSeg;
4966 }
4967 Assert(pbCur == pbBuf);
4968
4969 uint64_t offFile = cbFile + cbBeyond - cbToRead;
4970 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4971 int rcExpect = cbBeyond == 0 || cbToRead == 0 ? VINF_SUCCESS : VERR_EOF;
4972 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
4973 if (rc != rcExpect)
4974 {
4975 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx\n", rc, cSegs, cbToRead, cbBeyond);
4976 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4977 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
4978 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
4979 }
4980
4981 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4982 size_t cbActual = 0;
4983 rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, &cbActual);
4984 if (rc != VINF_SUCCESS || cbActual != cbToRead - cbBeyond)
4985 RTTestIFailed("myFileSgReadAt failed: %Rrc cbActual=%#zu - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx expected %#zx\n",
4986 rc, cbActual, cSegs, cbToRead, cbBeyond, cbToRead - cbBeyond);
4987 if (RT_SUCCESS(rc) && cbActual > 0)
4988 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4989 {
4990 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, RT_MIN(cbActual, aSegs[iSeg].cbSeg)))
4991 {
4992 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbActual%#zx cbToRead=%#zx cbBeyond=%#zx\n",
4993 iSeg, cSegs, aSegs[iSeg].cbSeg, cbActual, cbToRead, cbBeyond);
4994 iTest = _16K;
4995 break;
4996 }
4997 if (cbActual <= aSegs[iSeg].cbSeg)
4998 break;
4999 cbActual -= aSegs[iSeg].cbSeg;
5000 offFile += aSegs[iSeg].cbSeg;
5001 }
5002 }
5003
5004#endif
5005
5006 /*
5007 * Other OS specific stuff.
5008 */
5009#ifdef RT_OS_WINDOWS
5010 /* Check that reading at an offset modifies the position: */
5011 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
5012 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
5013
5014 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
5015 LARGE_INTEGER offNt;
5016 offNt.QuadPart = cbFile / 2;
5017 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
5018 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
5019 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
5020 RTTESTI_CHECK(Ios.Information == _4K);
5021 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
5022 fsPerfCheckReadBuf(__LINE__, cbFile / 2, pbBuf, _4K);
5023#endif
5024
5025
5026 RTMemPageFree(pbBuf, cbBuf);
5027}
5028
5029
5030/**
5031 * One RTFileWrite profiling iteration.
5032 */
5033DECL_FORCE_INLINE(int) fsPerfIoWriteWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
5034 uint64_t *poffActual, uint32_t *pcSeeks)
5035{
5036 /* Do we need to seek back to the start? */
5037 if (*poffActual + cbBlock <= cbFile)
5038 { /* likely */ }
5039 else
5040 {
5041 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
5042 *pcSeeks += 1;
5043 *poffActual = 0;
5044 }
5045
5046 size_t cbActuallyWritten = 0;
5047 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBlock, cbBlock, &cbActuallyWritten), VINF_SUCCESS, rcCheck);
5048 if (cbActuallyWritten == cbBlock)
5049 {
5050 *poffActual += cbActuallyWritten;
5051 return VINF_SUCCESS;
5052 }
5053 RTTestIFailed("RTFileWrite at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyWritten, cbBlock);
5054 *poffActual += cbActuallyWritten;
5055 return VERR_WRITE_ERROR;
5056}
5057
5058
5059static void fsPerfIoWriteBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
5060{
5061 RTTestISubF("IO - Sequential write %RU32", cbBlock);
5062
5063 if (cbBlock <= cbFile)
5064 {
5065 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
5066 if (pbBuf)
5067 {
5068 memset(pbBuf, 0xf7, cbBlock);
5069 PROFILE_IO_FN("RTFileWrite", fsPerfIoWriteWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
5070 RTMemPageFree(pbBuf, cbBlock);
5071 }
5072 else
5073 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
5074 }
5075 else
5076 RTTestSkipped(g_hTest, "test file too small");
5077}
5078
5079
5080/** pwritev is too new to be useful, so we use the writev api via this wrapper. */
5081DECLINLINE(int) myFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
5082{
5083 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
5084 if (RT_SUCCESS(rc))
5085 rc = RTFileSgWrite(hFile, pSgBuf, cbToWrite, pcbWritten);
5086 return rc;
5087}
5088
5089
5090static void fsPerfWrite(RTFILE hFile1, RTFILE hFileNoCache, RTFILE hFileWriteThru, uint64_t cbFile)
5091{
5092 RTTestISubF("IO - RTFileWrite");
5093
5094 /*
5095 * Allocate a big buffer we can play around with. Min size is 1MB.
5096 */
5097 size_t cbMaxBuf = RT_MIN(_64M, g_cbMaxBuffer);
5098 size_t cbBuf = cbFile < cbMaxBuf ? (size_t)cbFile : cbMaxBuf;
5099 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5100 while (!pbBuf)
5101 {
5102 cbBuf /= 2;
5103 RTTESTI_CHECK_RETV(cbBuf >= _1M);
5104 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
5105 }
5106
5107 uint8_t bFiller = 0x88;
5108
5109#if 1
5110 /*
5111 * Start at the beginning and write out the full buffer in random small chunks, thereby
5112 * checking that unaligned buffer addresses, size and file offsets work fine.
5113 */
5114 struct
5115 {
5116 uint64_t offFile;
5117 uint32_t cbMax;
5118 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
5119 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++, bFiller)
5120 {
5121 fsPerfFillWriteBuf(aRuns[i].offFile, pbBuf, cbBuf, bFiller);
5122 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
5123
5124 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5125 for (size_t offBuf = 0; offBuf < cbBuf; )
5126 {
5127 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
5128 uint32_t const cbToWrite = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
5129 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
5130 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
5131 size_t cbActual = 0;
5132 RTTESTI_CHECK_RC(RTFileWrite(hFile1, &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
5133 if (cbActual == cbToWrite)
5134 {
5135 offBuf += cbActual;
5136 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
5137 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
5138 }
5139 else
5140 {
5141 RTTestIFailed("Attempting to write %#x bytes at %#zx (%#x left), only got %#x written!\n",
5142 cbToWrite, offBuf, cbLeft, cbActual);
5143 if (cbActual)
5144 offBuf += cbActual;
5145 else
5146 pbBuf[offBuf++] = 0x11;
5147 }
5148 }
5149
5150 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, aRuns[i].offFile, pbBuf, cbBuf, NULL), VINF_SUCCESS);
5151 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
5152 }
5153
5154
5155 /*
5156 * Do uncached and write-thru accesses, must be page aligned.
5157 */
5158 RTFILE ahFiles[2] = { hFileWriteThru, hFileNoCache };
5159 for (unsigned iFile = 0; iFile < RT_ELEMENTS(ahFiles); iFile++, bFiller++)
5160 {
5161 if (g_fIgnoreNoCache && ahFiles[iFile] == NIL_RTFILE)
5162 continue;
5163
5164 fsPerfFillWriteBuf(0, pbBuf, cbBuf, bFiller);
5165 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
5166 RTTESTI_CHECK_RC(RTFileSeek(ahFiles[iFile], 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5167
5168 for (size_t offBuf = 0; offBuf < cbBuf; )
5169 {
5170 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / g_cbPage);
5171 uint32_t const cPagesToWrite = RTRandU32Ex(1, cPagesLeft);
5172 size_t const cbToWrite = cPagesToWrite * (size_t)g_cbPage;
5173 size_t cbActual = 0;
5174 RTTESTI_CHECK_RC(RTFileWrite(ahFiles[iFile], &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
5175 if (cbActual == cbToWrite)
5176 {
5177 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offBuf, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5178 fsPerfCheckReadBuf(__LINE__, offBuf, pbBuf, cbToWrite, bFiller);
5179 offBuf += cbActual;
5180 }
5181 else
5182 {
5183 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x written!\n", cbToWrite, offBuf, cbActual);
5184 if (cbActual)
5185 offBuf += cbActual;
5186 else
5187 {
5188 memset(&pbBuf[offBuf], 0x11, g_cbPage);
5189 offBuf += g_cbPage;
5190 }
5191 }
5192 }
5193
5194 RTTESTI_CHECK_RC(RTFileReadAt(ahFiles[iFile], 0, pbBuf, cbBuf, NULL), VINF_SUCCESS);
5195 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
5196 }
5197
5198 /*
5199 * Check the behavior of writing zero bytes to the file _4K from the end
5200 * using native API. In the olden days zero sized write have been known
5201 * to be used to truncate a file.
5202 */
5203 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -_4K, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
5204# ifdef RT_OS_WINDOWS
5205 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
5206 NTSTATUS rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
5207 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
5208 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
5209 RTTESTI_CHECK(Ios.Information == 0);
5210# else
5211 ssize_t cbWritten = write((int)RTFileToNative(hFile1), pbBuf, 0);
5212 RTTESTI_CHECK(cbWritten == 0);
5213# endif
5214 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, _4K, NULL), VINF_SUCCESS);
5215 fsPerfCheckReadBuf(__LINE__, cbFile - _4K, pbBuf, _4K, pbBuf[0x8]);
5216
5217#else
5218 RT_NOREF(hFileNoCache, hFileWriteThru);
5219#endif
5220
5221 /*
5222 * Gather write function operation.
5223 */
5224#ifdef RT_OS_WINDOWS
5225 /** @todo RTFileSgWriteAt is just a RTFileWriteAt loop for windows NT. Need
5226 * to use WriteFileGather (nocache + page aligned). */
5227#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
5228
5229# ifdef UIO_MAXIOV
5230 RTSGSEG aSegs[UIO_MAXIOV];
5231# else
5232 RTSGSEG aSegs[512];
5233# endif
5234 RTSGBUF SgBuf;
5235 uint32_t cIncr = 1;
5236 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr, bFiller++)
5237 {
5238 size_t const cbSeg = cbBuf / cSegs;
5239 size_t const cbToWrite = cbSeg * cSegs;
5240 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
5241 {
5242 aSegs[iSeg].cbSeg = cbSeg;
5243 aSegs[iSeg].pvSeg = &pbBuf[cbToWrite - (iSeg + 1) * cbSeg];
5244 fsPerfFillWriteBuf(iSeg * cbSeg, (uint8_t *)aSegs[iSeg].pvSeg, cbSeg, bFiller);
5245 }
5246 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
5247 int rc = myFileSgWriteAt(hFile1, 0, &SgBuf, cbToWrite, NULL);
5248 if (RT_SUCCESS(rc))
5249 {
5250 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, 0, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5251 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbToWrite, bFiller);
5252 }
5253 else
5254 {
5255 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToWrite=%#zx", rc, cSegs, cbSeg, cbToWrite);
5256 break;
5257 }
5258 if (cSegs == 16)
5259 cIncr = 7;
5260 else if (cSegs == 16 * 7 + 16 /*= 128*/)
5261 cIncr = 64;
5262 }
5263
5264 /* random stuff, including zero segments. */
5265 for (uint32_t iTest = 0; iTest < 128; iTest++, bFiller++)
5266 {
5267 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
5268 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
5269 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
5270 size_t cbToWrite = 0;
5271 size_t cbLeft = cbBuf;
5272 uint8_t *pbCur = &pbBuf[cbBuf];
5273 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
5274 {
5275 uint32_t iAlign = RTRandU32Ex(0, 3);
5276 if (iAlign & 2) /* end is page aligned */
5277 {
5278 cbLeft -= (uintptr_t)pbCur & g_fPageOffset;
5279 pbCur -= (uintptr_t)pbCur & g_fPageOffset;
5280 }
5281
5282 size_t cbSegOthers = (cSegs - iSeg) * _8K;
5283 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
5284 : cbLeft > cSegs ? cbLeft - cSegs
5285 : cbLeft;
5286 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
5287 if (iAlign & 1) /* start is page aligned */
5288 cbSeg += ((uintptr_t)pbCur - cbSeg) & g_fPageOffset;
5289
5290 if (iSeg - iZeroSeg < cZeroSegs)
5291 cbSeg = 0;
5292
5293 cbToWrite += cbSeg;
5294 cbLeft -= cbSeg;
5295 pbCur -= cbSeg;
5296 aSegs[iSeg].cbSeg = cbSeg;
5297 aSegs[iSeg].pvSeg = pbCur;
5298 }
5299
5300 uint64_t const offFile = cbToWrite < cbFile ? RTRandU64Ex(0, cbFile - cbToWrite) : 0;
5301 uint64_t offFill = offFile;
5302 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
5303 if (aSegs[iSeg].cbSeg)
5304 {
5305 fsPerfFillWriteBuf(offFill, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg, bFiller);
5306 offFill += aSegs[iSeg].cbSeg;
5307 }
5308
5309 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
5310 int rc = myFileSgWriteAt(hFile1, offFile, &SgBuf, cbToWrite, NULL);
5311 if (RT_SUCCESS(rc))
5312 {
5313 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offFile, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5314 fsPerfCheckReadBuf(__LINE__, offFile, pbBuf, cbToWrite, bFiller);
5315 }
5316 else
5317 {
5318 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%#x cbToWrite=%#zx", rc, cSegs, cbToWrite);
5319 break;
5320 }
5321 }
5322
5323#endif
5324
5325 /*
5326 * Other OS specific stuff.
5327 */
5328#ifdef RT_OS_WINDOWS
5329 /* Check that reading at an offset modifies the position: */
5330 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, cbFile / 2, pbBuf, _4K, NULL), VINF_SUCCESS);
5331 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
5332 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
5333
5334 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
5335 LARGE_INTEGER offNt;
5336 offNt.QuadPart = cbFile / 2;
5337 rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
5338 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
5339 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
5340 RTTESTI_CHECK(Ios.Information == _4K);
5341 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
5342#endif
5343
5344 RTMemPageFree(pbBuf, cbBuf);
5345}
5346
5347
5348/**
5349 * Worker for testing RTFileFlush.
5350 */
5351DECL_FORCE_INLINE(int) fsPerfFSyncWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf, uint64_t *poffFile)
5352{
5353 if (*poffFile + cbBuf <= cbFile)
5354 { /* likely */ }
5355 else
5356 {
5357 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5358 *poffFile = 0;
5359 }
5360
5361 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbBuf, NULL), VINF_SUCCESS, rcCheck);
5362 RTTESTI_CHECK_RC_RET(RTFileFlush(hFile1), VINF_SUCCESS, rcCheck);
5363
5364 *poffFile += cbBuf;
5365 return VINF_SUCCESS;
5366}
5367
5368
5369static void fsPerfFSync(RTFILE hFile1, uint64_t cbFile)
5370{
5371 RTTestISub("fsync");
5372
5373 RTTESTI_CHECK_RC(RTFileFlush(hFile1), VINF_SUCCESS);
5374
5375 PROFILE_FN(RTFileFlush(hFile1), g_nsTestRun, "RTFileFlush");
5376
5377 size_t cbBuf = g_cbPage;
5378 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5379 RTTESTI_CHECK_RETV(pbBuf != NULL);
5380 memset(pbBuf, 0xf4, cbBuf);
5381
5382 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5383 uint64_t offFile = 0;
5384 PROFILE_FN(fsPerfFSyncWorker(hFile1, cbFile, pbBuf, cbBuf, &offFile), g_nsTestRun, "RTFileWrite[Page]/RTFileFlush");
5385
5386 RTMemPageFree(pbBuf, cbBuf);
5387}
5388
5389
5390#ifndef RT_OS_OS2
5391/**
5392 * Worker for profiling msync.
5393 */
5394DECL_FORCE_INLINE(int) fsPerfMSyncWorker(uint8_t *pbMapping, size_t offMapping, size_t cbFlush, size_t *pcbFlushed)
5395{
5396 uint8_t *pbCur = &pbMapping[offMapping];
5397 for (size_t offFlush = 0; offFlush < cbFlush; offFlush += g_cbPage)
5398 *(size_t volatile *)&pbCur[offFlush + 8] = cbFlush;
5399# ifdef RT_OS_WINDOWS
5400 CHECK_WINAPI_CALL(FlushViewOfFile(pbCur, cbFlush) == TRUE);
5401# else
5402 RTTESTI_CHECK(msync(pbCur, cbFlush, MS_SYNC) == 0);
5403# endif
5404 if (*pcbFlushed < offMapping + cbFlush)
5405 *pcbFlushed = offMapping + cbFlush;
5406 return VINF_SUCCESS;
5407}
5408#endif /* !RT_OS_OS2 */
5409
5410
5411static void fsPerfMMap(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
5412{
5413 RTTestISub("mmap");
5414#if !defined(RT_OS_OS2)
5415 static const char * const s_apszStates[] = { "readonly", "writecopy", "readwrite" };
5416 enum { kMMap_ReadOnly = 0, kMMap_WriteCopy, kMMap_ReadWrite, kMMap_End };
5417 for (int enmState = kMMap_ReadOnly; enmState < kMMap_End; enmState++)
5418 {
5419 /*
5420 * Do the mapping.
5421 */
5422 size_t cbMapping = (size_t)cbFile;
5423 if (cbMapping != cbFile)
5424 cbMapping = _256M;
5425 uint8_t *pbMapping;
5426
5427# ifdef RT_OS_WINDOWS
5428 HANDLE hSection;
5429 pbMapping = NULL;
5430 for (;; cbMapping /= 2)
5431 {
5432 hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile1), NULL,
5433 enmState == kMMap_ReadOnly ? PAGE_READONLY
5434 : enmState == kMMap_WriteCopy ? PAGE_WRITECOPY : PAGE_READWRITE,
5435 (uint32_t)((uint64_t)cbMapping >> 32), (uint32_t)cbMapping, NULL);
5436 DWORD dwErr1 = GetLastError();
5437 DWORD dwErr2 = 0;
5438 if (hSection != NULL)
5439 {
5440 pbMapping = (uint8_t *)MapViewOfFile(hSection,
5441 enmState == kMMap_ReadOnly ? FILE_MAP_READ
5442 : enmState == kMMap_WriteCopy ? FILE_MAP_COPY
5443 : FILE_MAP_WRITE,
5444 0, 0, cbMapping);
5445 if (pbMapping)
5446 break;
5447 dwErr2 = GetLastError();
5448 CHECK_WINAPI_CALL(CloseHandle(hSection) == TRUE);
5449 }
5450 if (cbMapping <= _2M)
5451 {
5452 RTTestIFailed("%u/%s: CreateFileMapping or MapViewOfFile failed: %u, %u",
5453 enmState, s_apszStates[enmState], dwErr1, dwErr2);
5454 break;
5455 }
5456 }
5457# else
5458 for (;; cbMapping /= 2)
5459 {
5460 pbMapping = (uint8_t *)mmap(NULL, cbMapping,
5461 enmState == kMMap_ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE,
5462 enmState == kMMap_WriteCopy ? MAP_PRIVATE : MAP_SHARED,
5463 (int)RTFileToNative(hFile1), 0);
5464 if ((void *)pbMapping != MAP_FAILED)
5465 break;
5466 if (cbMapping <= _2M)
5467 {
5468 RTTestIFailed("%u/%s: mmap failed: %s (%u)", enmState, s_apszStates[enmState], strerror(errno), errno);
5469 break;
5470 }
5471 }
5472# endif
5473 if (cbMapping <= _2M)
5474 continue;
5475
5476 /*
5477 * Time page-ins just for fun.
5478 */
5479 size_t const cPages = cbMapping >> g_cPageShift;
5480 size_t uDummy = 0;
5481 uint64_t ns = RTTimeNanoTS();
5482 for (size_t iPage = 0; iPage < cPages; iPage++)
5483 uDummy += ASMAtomicReadU8(&pbMapping[iPage << g_cPageShift]);
5484 ns = RTTimeNanoTS() - ns;
5485 RTTestIValueF(ns / cPages, RTTESTUNIT_NS_PER_OCCURRENCE, "page-in %s", s_apszStates[enmState]);
5486
5487 /* Check the content. */
5488 fsPerfCheckReadBuf(__LINE__, 0, pbMapping, cbMapping);
5489
5490 if (enmState != kMMap_ReadOnly)
5491 {
5492 /* Write stuff to the first two megabytes. In the COW case, we'll detect
5493 corruption of shared data during content checking of the RW iterations. */
5494 fsPerfFillWriteBuf(0, pbMapping, _2M, 0xf7);
5495 if (enmState == kMMap_ReadWrite && g_fMMapCoherency)
5496 {
5497 /* For RW we can try read back from the file handle and check if we get
5498 a match there first. */
5499 uint8_t abBuf[_4K];
5500 for (uint32_t off = 0; off < _2M; off += sizeof(abBuf))
5501 {
5502 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, off, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
5503 fsPerfCheckReadBuf(__LINE__, off, abBuf, sizeof(abBuf), 0xf7);
5504 }
5505# ifdef RT_OS_WINDOWS
5506 CHECK_WINAPI_CALL(FlushViewOfFile(pbMapping, _2M) == TRUE);
5507# else
5508 RTTESTI_CHECK(msync(pbMapping, _2M, MS_SYNC) == 0);
5509# endif
5510 }
5511
5512 /*
5513 * Time modifying and flushing a few different number of pages.
5514 */
5515 if (enmState == kMMap_ReadWrite)
5516 {
5517 static size_t const s_acbFlush[] = { g_cbPage, g_cbPage * 2, g_cbPage * 3, g_cbPage * 8, g_cbPage * 16, _2M };
5518 for (unsigned iFlushSize = 0 ; iFlushSize < RT_ELEMENTS(s_acbFlush); iFlushSize++)
5519 {
5520 size_t const cbFlush = s_acbFlush[iFlushSize];
5521 if (cbFlush > cbMapping)
5522 continue;
5523
5524 char szDesc[80];
5525 RTStrPrintf(szDesc, sizeof(szDesc), "touch/flush/%zu", cbFlush);
5526 size_t const cFlushes = cbMapping / cbFlush;
5527 size_t const cbMappingUsed = cFlushes * cbFlush;
5528 size_t cbFlushed = 0;
5529 PROFILE_FN(fsPerfMSyncWorker(pbMapping, (iIteration * cbFlush) % cbMappingUsed, cbFlush, &cbFlushed),
5530 g_nsTestRun, szDesc);
5531
5532 /*
5533 * Check that all the changes made it thru to the file:
5534 */
5535 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
5536 {
5537 size_t cbBuf = RT_MIN(_2M, g_cbMaxBuffer);
5538 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5539 if (!pbBuf)
5540 {
5541 cbBuf = _4K;
5542 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5543 }
5544 RTTESTI_CHECK(pbBuf != NULL);
5545 if (pbBuf)
5546 {
5547 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5548 size_t const cbToCheck = RT_MIN(cFlushes * cbFlush, cbFlushed);
5549 unsigned cErrors = 0;
5550 for (size_t offBuf = 0; cErrors < 32 && offBuf < cbToCheck; offBuf += cbBuf)
5551 {
5552 size_t cbToRead = RT_MIN(cbBuf, cbToCheck - offBuf);
5553 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, pbBuf, cbToRead, NULL), VINF_SUCCESS);
5554
5555 for (size_t offFlush = 0; offFlush < cbToRead; offFlush += g_cbPage)
5556 if (*(size_t volatile *)&pbBuf[offFlush + 8] != cbFlush)
5557 {
5558 RTTestIFailed("Flush issue at offset #%zx: %#zx, expected %#zx (cbFlush=%#zx, %#RX64)",
5559 offBuf + offFlush + 8, *(size_t volatile *)&pbBuf[offFlush + 8],
5560 cbFlush, cbFlush, *(uint64_t volatile *)&pbBuf[offFlush]);
5561 if (++cErrors > 32)
5562 break;
5563 }
5564 }
5565 RTMemPageFree(pbBuf, cbBuf);
5566 }
5567 }
5568 }
5569
5570# if 0 /* not needed, very very slow */
5571 /*
5572 * Restore the file to 0xf6 state for the next test.
5573 */
5574 RTTestIPrintf(RTTESTLVL_ALWAYS, "Restoring content...\n");
5575 fsPerfFillWriteBuf(0, pbMapping, cbMapping, 0xf6);
5576# ifdef RT_OS_WINDOWS
5577 CHECK_WINAPI_CALL(FlushViewOfFile(pbMapping, cbMapping) == TRUE);
5578# else
5579 RTTESTI_CHECK(msync(pbMapping, cbMapping, MS_SYNC) == 0);
5580# endif
5581 RTTestIPrintf(RTTESTLVL_ALWAYS, "... done\n");
5582# endif
5583 }
5584 }
5585
5586 /*
5587 * Observe how regular writes affects a read-only or readwrite mapping.
5588 * These should ideally be immediately visible in the mapping, at least
5589 * when not performed thru an no-cache handle.
5590 */
5591 if ( (enmState == kMMap_ReadOnly || enmState == kMMap_ReadWrite)
5592 && g_fMMapCoherency)
5593 {
5594 size_t cbBuf = RT_MIN(RT_MIN(_2M, cbMapping / 2), g_cbMaxBuffer);
5595 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5596 if (!pbBuf)
5597 {
5598 cbBuf = _4K;
5599 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5600 }
5601 RTTESTI_CHECK(pbBuf != NULL);
5602 if (pbBuf)
5603 {
5604 /* Do a number of random writes to the file (using hFile1).
5605 Immediately undoing them. */
5606 for (uint32_t i = 0; i < 128; i++)
5607 {
5608 /* Generate a randomly sized write at a random location, making
5609 sure it differs from whatever is there already before writing. */
5610 uint32_t const cbToWrite = RTRandU32Ex(1, (uint32_t)cbBuf);
5611 uint64_t const offToWrite = RTRandU64Ex(0, cbMapping - cbToWrite);
5612
5613 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf8);
5614 pbBuf[0] = ~pbBuf[0];
5615 if (cbToWrite > 1)
5616 pbBuf[cbToWrite - 1] = ~pbBuf[cbToWrite - 1];
5617 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5618
5619 /* Check the mapping. */
5620 if (memcmp(&pbMapping[(size_t)offToWrite], pbBuf, cbToWrite) != 0)
5621 {
5622 RTTestIFailed("Write #%u @ %#RX64 LB %#x was not reflected in the mapping!\n", i, offToWrite, cbToWrite);
5623 }
5624
5625 /* Restore */
5626 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf6);
5627 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5628 }
5629
5630 RTMemPageFree(pbBuf, cbBuf);
5631 }
5632 }
5633
5634 /*
5635 * Unmap it.
5636 */
5637# ifdef RT_OS_WINDOWS
5638 CHECK_WINAPI_CALL(UnmapViewOfFile(pbMapping) == TRUE);
5639 CHECK_WINAPI_CALL(CloseHandle(hSection) == TRUE);
5640# else
5641 RTTESTI_CHECK(munmap(pbMapping, cbMapping) == 0);
5642# endif
5643 }
5644
5645 /*
5646 * Memory mappings without open handles (pretty common).
5647 */
5648 char *pbContentUnaligned = (char *)RTMemAlloc(256*1024 + g_cbPage - 1);
5649 RTTESTI_CHECK(pbContentUnaligned != NULL);
5650 if (pbContentUnaligned)
5651 {
5652 for (uint32_t i = 0; i < 32; i++)
5653 {
5654 /* Create a new file, 256 KB in size, and fill it with random bytes.
5655 Try uncached access if we can to force the page-in to do actual reads. */
5656 char szFile2[FSPERF_MAX_PATH + 32];
5657 memcpy(szFile2, g_szDir, g_cchDir);
5658 RTStrPrintf(&szFile2[g_cchDir], sizeof(szFile2) - g_cchDir, "mmap-%u.noh", i);
5659 RTFILE hFile2 = NIL_RTFILE;
5660 int rc = (i & 3) == 3 ? VERR_TRY_AGAIN
5661 : RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_NO_CACHE);
5662 if (RT_FAILURE(rc))
5663 {
5664 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
5665 VINF_SUCCESS);
5666 }
5667
5668 char * const pbContent = &pbContentUnaligned[g_cbPage - ((uintptr_t)&pbContentUnaligned[0] & g_fPageOffset)];
5669 size_t const cbContent = 256*1024;
5670 RTRandBytes(pbContent, cbContent);
5671 RTTESTI_CHECK_RC(rc = RTFileWrite(hFile2, pbContent, cbContent, NULL), VINF_SUCCESS);
5672 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5673 if (RT_SUCCESS(rc))
5674 {
5675 /* Reopen the file with normal caching. Every second time, we also
5676 does a read-only open of it to confuse matters. */
5677 RTFILE hFile3 = NIL_RTFILE;
5678 if ((i & 3) == 3)
5679 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
5680 hFile2 = NIL_RTFILE;
5681 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE),
5682 VINF_SUCCESS);
5683 if ((i & 3) == 1)
5684 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
5685
5686 /* Memory map it read-write (no COW). */
5687#ifdef RT_OS_WINDOWS
5688 HANDLE hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile2), NULL, PAGE_READWRITE, 0, cbContent, NULL);
5689 CHECK_WINAPI_CALL(hSection != NULL);
5690 uint8_t *pbMapping = (uint8_t *)MapViewOfFile(hSection, FILE_MAP_WRITE, 0, 0, cbContent);
5691 CHECK_WINAPI_CALL(pbMapping != NULL);
5692 CHECK_WINAPI_CALL(CloseHandle(hSection) == TRUE);
5693# else
5694 uint8_t *pbMapping = (uint8_t *)mmap(NULL, cbContent, PROT_READ | PROT_WRITE, MAP_SHARED,
5695 (int)RTFileToNative(hFile2), 0);
5696 if ((void *)pbMapping == MAP_FAILED)
5697 pbMapping = NULL;
5698 RTTESTI_CHECK_MSG(pbMapping != NULL, ("errno=%s (%d)\n", strerror(errno), errno));
5699# endif
5700
5701 /* Close the file handles. */
5702 if ((i & 7) == 7)
5703 {
5704 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
5705 hFile3 = NIL_RTFILE;
5706 }
5707 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5708 if ((i & 7) == 5)
5709 {
5710 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
5711 hFile3 = NIL_RTFILE;
5712 }
5713 if (pbMapping)
5714 {
5715 RTThreadSleep(2); /* fudge for cleanup/whatever */
5716
5717 /* Page in the mapping by comparing with the content we wrote above. */
5718 RTTESTI_CHECK(memcmp(pbMapping, pbContent, cbContent) == 0);
5719
5720 /* Now dirty everything by inverting everything. */
5721 size_t *puCur = (size_t *)pbMapping;
5722 size_t cLeft = cbContent / sizeof(*puCur);
5723 while (cLeft-- > 0)
5724 {
5725 *puCur = ~*puCur;
5726 puCur++;
5727 }
5728
5729 /* Sync it all. */
5730# ifdef RT_OS_WINDOWS
5731 //CHECK_WINAPI_CALL(FlushViewOfFile(pbMapping, cbContent) == TRUE);
5732 SetLastError(0);
5733 if (FlushViewOfFile(pbMapping, cbContent) != TRUE)
5734 RTTestIFailed("line %u, i=%u: FlushViewOfFile(%p, %#zx) failed: %u / %#x", __LINE__, i,
5735 pbMapping, cbContent, GetLastError(), RTNtLastStatusValue());
5736# else
5737 RTTESTI_CHECK(msync(pbMapping, cbContent, MS_SYNC) == 0);
5738# endif
5739
5740 /* Unmap it. */
5741# ifdef RT_OS_WINDOWS
5742 CHECK_WINAPI_CALL(UnmapViewOfFile(pbMapping) == TRUE);
5743# else
5744 RTTESTI_CHECK(munmap(pbMapping, cbContent) == 0);
5745# endif
5746 }
5747
5748 if (hFile3 != NIL_RTFILE)
5749 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
5750 }
5751 RTTESTI_CHECK_RC(RTFileDelete(szFile2), VINF_SUCCESS);
5752 }
5753 }
5754
5755#else
5756 RTTestSkipped(g_hTest, "not supported/implemented");
5757 RT_NOREF(hFile1, hFileNoCache, cbFile);
5758#endif
5759}
5760
5761
5762/**
5763 * This does the read, write and seek tests.
5764 */
5765static void fsPerfIo(void)
5766{
5767 RTTestISub("I/O");
5768
5769 /*
5770 * Determin the size of the test file.
5771 */
5772 g_szDir[g_cchDir] = '\0';
5773 RTFOFF cbFree = 0;
5774 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
5775 uint64_t cbFile = g_cbIoFile;
5776 if (cbFile + _16M < (uint64_t)cbFree)
5777 cbFile = RT_ALIGN_64(cbFile, _64K);
5778 else if (cbFree < _32M)
5779 {
5780 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
5781 return;
5782 }
5783 else
5784 {
5785 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
5786 cbFile = RT_ALIGN_64(cbFile, _64K);
5787 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
5788 }
5789 if (cbFile < _64K)
5790 {
5791 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 64KB", cbFile);
5792 return;
5793 }
5794
5795 /*
5796 * Create a cbFile sized test file.
5797 */
5798 RTFILE hFile1;
5799 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file21")),
5800 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
5801 RTFILE hFileNoCache;
5802 if (!g_fIgnoreNoCache)
5803 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileNoCache, g_szDir,
5804 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE),
5805 VINF_SUCCESS);
5806 else
5807 {
5808 int rc = RTFileOpen(&hFileNoCache, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE);
5809 if (RT_FAILURE(rc))
5810 {
5811 RTTestIPrintf(RTTESTLVL_ALWAYS, "Unable to open I/O file with non-cache flag (%Rrc), skipping related tests.\n", rc);
5812 hFileNoCache = NIL_RTFILE;
5813 }
5814 }
5815 RTFILE hFileWriteThru;
5816 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileWriteThru, g_szDir,
5817 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_WRITE_THROUGH),
5818 VINF_SUCCESS);
5819
5820 uint8_t *pbFree = NULL;
5821 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
5822 RTMemFree(pbFree);
5823 if (RT_SUCCESS(rc))
5824 {
5825 /*
5826 * Do the testing & profiling.
5827 */
5828 if (g_fSeek)
5829 fsPerfIoSeek(hFile1, cbFile);
5830
5831 if (g_fMMap && g_iMMapPlacement < 0)
5832 {
5833 fsPerfMMap(hFile1, hFileNoCache, cbFile);
5834 fsPerfReinitFile(hFile1, cbFile);
5835 }
5836
5837 if (g_fReadTests)
5838 fsPerfRead(hFile1, hFileNoCache, cbFile);
5839 if (g_fReadPerf)
5840 for (unsigned i = 0; i < g_cIoBlocks; i++)
5841 fsPerfIoReadBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
5842#ifdef FSPERF_TEST_SENDFILE
5843 if (g_fSendFile)
5844 fsPerfSendFile(hFile1, cbFile);
5845#endif
5846#ifdef RT_OS_LINUX
5847 if (g_fSplice)
5848 fsPerfSpliceToPipe(hFile1, cbFile);
5849#endif
5850 if (g_fMMap && g_iMMapPlacement == 0)
5851 fsPerfMMap(hFile1, hFileNoCache, cbFile);
5852
5853 /* This is destructive to the file content. */
5854 if (g_fWriteTests)
5855 fsPerfWrite(hFile1, hFileNoCache, hFileWriteThru, cbFile);
5856 if (g_fWritePerf)
5857 for (unsigned i = 0; i < g_cIoBlocks; i++)
5858 fsPerfIoWriteBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
5859#ifdef RT_OS_LINUX
5860 if (g_fSplice)
5861 fsPerfSpliceToFile(hFile1, cbFile);
5862#endif
5863 if (g_fFSync)
5864 fsPerfFSync(hFile1, cbFile);
5865
5866 if (g_fMMap && g_iMMapPlacement > 0)
5867 {
5868 fsPerfReinitFile(hFile1, cbFile);
5869 fsPerfMMap(hFile1, hFileNoCache, cbFile);
5870 }
5871 }
5872
5873 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
5874 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
5875 if (hFileNoCache != NIL_RTFILE || !g_fIgnoreNoCache)
5876 RTTESTI_CHECK_RC(RTFileClose(hFileNoCache), VINF_SUCCESS);
5877 RTTESTI_CHECK_RC(RTFileClose(hFileWriteThru), VINF_SUCCESS);
5878 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
5879}
5880
5881
5882DECL_FORCE_INLINE(int) fsPerfCopyWorker1(const char *pszSrc, const char *pszDst)
5883{
5884 RTFileDelete(pszDst);
5885 return RTFileCopy(pszSrc, pszDst);
5886}
5887
5888
5889#ifdef RT_OS_LINUX
5890DECL_FORCE_INLINE(int) fsPerfCopyWorkerSendFile(RTFILE hFile1, RTFILE hFile2, size_t cbFile)
5891{
5892 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile2, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
5893
5894 loff_t off = 0;
5895 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &off, cbFile);
5896 if (cbSent > 0 && (size_t)cbSent == cbFile)
5897 return 0;
5898
5899 int rc = VERR_GENERAL_FAILURE;
5900 if (cbSent < 0)
5901 {
5902 rc = RTErrConvertFromErrno(errno);
5903 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)", cbFile, cbSent, errno, rc);
5904 }
5905 else
5906 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
5907 cbFile, cbSent, cbFile, cbSent - cbFile);
5908 return rc;
5909}
5910#endif /* RT_OS_LINUX */
5911
5912
5913static void fsPerfCopy(void)
5914{
5915 RTTestISub("copy");
5916
5917 /*
5918 * Non-existing files.
5919 */
5920 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-file")),
5921 InDir2(RT_STR_TUPLE("whatever"))), VERR_FILE_NOT_FOUND);
5922 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
5923 InDir2(RT_STR_TUPLE("no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
5924 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
5925 InDir2(RT_STR_TUPLE("whatever"))), VERR_PATH_NOT_FOUND);
5926
5927 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
5928 InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
5929 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
5930 InDir2(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
5931
5932 /*
5933 * Determin the size of the test file.
5934 * We want to be able to make 1 copy of it.
5935 */
5936 g_szDir[g_cchDir] = '\0';
5937 RTFOFF cbFree = 0;
5938 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
5939 uint64_t cbFile = g_cbIoFile;
5940 if (cbFile + _16M < (uint64_t)cbFree)
5941 cbFile = RT_ALIGN_64(cbFile, _64K);
5942 else if (cbFree < _32M)
5943 {
5944 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
5945 return;
5946 }
5947 else
5948 {
5949 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
5950 cbFile = RT_ALIGN_64(cbFile, _64K);
5951 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
5952 }
5953 if (cbFile < _512K * 2)
5954 {
5955 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 1MB", cbFile);
5956 return;
5957 }
5958 cbFile /= 2;
5959
5960 /*
5961 * Create a cbFile sized test file.
5962 */
5963 RTFILE hFile1;
5964 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file22")),
5965 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
5966 uint8_t *pbFree = NULL;
5967 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
5968 RTMemFree(pbFree);
5969 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
5970 if (RT_SUCCESS(rc))
5971 {
5972 /*
5973 * Make copies.
5974 */
5975 /* plain */
5976 RTFileDelete(InDir2(RT_STR_TUPLE("file23")));
5977 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VINF_SUCCESS);
5978 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VERR_ALREADY_EXISTS);
5979 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
5980
5981 /* by handle */
5982 hFile1 = NIL_RTFILE;
5983 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
5984 RTFILE hFile2 = NIL_RTFILE;
5985 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
5986 RTTESTI_CHECK_RC(RTFileCopyByHandles(hFile1, hFile2), VINF_SUCCESS);
5987 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5988 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
5989 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
5990
5991 /* copy part */
5992 hFile1 = NIL_RTFILE;
5993 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
5994 hFile2 = NIL_RTFILE;
5995 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
5996 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, 0, hFile2, 0, cbFile / 2, 0, NULL), VINF_SUCCESS);
5997 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, cbFile / 2, hFile2, cbFile / 2, cbFile - cbFile / 2, 0, NULL), VINF_SUCCESS);
5998 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5999 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6000 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6001
6002#ifdef RT_OS_LINUX
6003 /*
6004 * On linux we can also use sendfile between two files, except for 2.5.x to 2.6.33.
6005 */
6006 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_C(0x7ffff000));
6007 char szRelease[64];
6008 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
6009 bool const fSendFileBetweenFiles = RTStrVersionCompare(szRelease, "2.5.0") < 0
6010 || RTStrVersionCompare(szRelease, "2.6.33") >= 0;
6011 if (fSendFileBetweenFiles)
6012 {
6013 /* Copy the whole file: */
6014 hFile1 = NIL_RTFILE;
6015 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6016 RTFileDelete(g_szDir2);
6017 hFile2 = NIL_RTFILE;
6018 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6019 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbFile);
6020 if (cbSent < 0)
6021 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
6022 cbFile, cbSent, errno, RTErrConvertFromErrno(errno));
6023 else if ((size_t)cbSent != cbFileMax)
6024 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
6025 cbFile, cbSent, cbFileMax, cbSent - cbFileMax);
6026 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6027 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6028 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6029
6030 /* Try copy a little bit too much: */
6031 if (cbFile == cbFileMax)
6032 {
6033 hFile1 = NIL_RTFILE;
6034 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6035 RTFileDelete(g_szDir2);
6036 hFile2 = NIL_RTFILE;
6037 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6038 size_t cbToCopy = cbFile + RTRandU32Ex(1, _64M);
6039 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbToCopy);
6040 if (cbSent < 0)
6041 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
6042 cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
6043 else if ((size_t)cbSent != cbFile)
6044 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
6045 cbToCopy, cbSent, cbFile, cbSent - cbFile);
6046 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6047 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6048 }
6049
6050 /* Do partial copy: */
6051 hFile2 = NIL_RTFILE;
6052 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6053 for (uint32_t i = 0; i < 64; i++)
6054 {
6055 size_t cbToCopy = RTRandU32Ex(0, cbFileMax - 1);
6056 uint32_t const offFile = RTRandU32Ex(1, (uint64_t)RT_MIN(cbFileMax - cbToCopy, UINT32_MAX));
6057 RTTESTI_CHECK_RC_BREAK(RTFileSeek(hFile2, offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6058 loff_t offFile2 = offFile;
6059 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &offFile2, cbToCopy);
6060 if (cbSent < 0)
6061 RTTestIFailed("sendfile(file,file,%#x,%#zx) failed (%zd): %d (%Rrc)",
6062 offFile, cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
6063 else if ((size_t)cbSent != cbToCopy)
6064 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx, expected %#zx (diff %zd)",
6065 offFile, cbToCopy, cbSent, cbToCopy, cbSent - cbToCopy);
6066 else if (offFile2 != (loff_t)(offFile + cbToCopy))
6067 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx + off=%#RX64, expected off %#x",
6068 offFile, cbToCopy, cbSent, offFile2, offFile + cbToCopy);
6069 }
6070 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6071 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6072 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6073 }
6074#endif
6075
6076 /*
6077 * Do some benchmarking.
6078 */
6079#define PROFILE_COPY_FN(a_szOperation, a_fnCall) \
6080 do \
6081 { \
6082 /* Estimate how many iterations we need to fill up the given timeslot: */ \
6083 fsPerfYield(); \
6084 uint64_t nsStart = RTTimeNanoTS(); \
6085 uint64_t ns; \
6086 do \
6087 ns = RTTimeNanoTS(); \
6088 while (ns == nsStart); \
6089 nsStart = ns; \
6090 \
6091 uint64_t iIteration = 0; \
6092 do \
6093 { \
6094 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
6095 iIteration++; \
6096 ns = RTTimeNanoTS() - nsStart; \
6097 } while (ns < RT_NS_10MS); \
6098 ns /= iIteration; \
6099 if (ns > g_nsPerNanoTSCall + 32) \
6100 ns -= g_nsPerNanoTSCall; \
6101 uint64_t cIterations = g_nsTestRun / ns; \
6102 if (cIterations < 2) \
6103 cIterations = 2; \
6104 else if (cIterations & 1) \
6105 cIterations++; \
6106 \
6107 /* Do the actual profiling: */ \
6108 iIteration = 0; \
6109 fsPerfYield(); \
6110 nsStart = RTTimeNanoTS(); \
6111 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
6112 { \
6113 for (; iIteration < cIterations; iIteration++)\
6114 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
6115 ns = RTTimeNanoTS() - nsStart;\
6116 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
6117 break; \
6118 cIterations += cIterations / 4; \
6119 if (cIterations & 1) \
6120 cIterations++; \
6121 nsStart += g_nsPerNanoTSCall; \
6122 } \
6123 RTTestIValueF(ns / iIteration, \
6124 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation " latency"); \
6125 RTTestIValueF((uint64_t)((double)(iIteration * cbFile) / ((double)ns / RT_NS_1SEC)), \
6126 RTTESTUNIT_BYTES_PER_SEC, a_szOperation " throughput"); \
6127 RTTestIValueF((uint64_t)iIteration * cbFile, \
6128 RTTESTUNIT_BYTES, a_szOperation " bytes"); \
6129 RTTestIValueF(iIteration, \
6130 RTTESTUNIT_OCCURRENCES, a_szOperation " iterations"); \
6131 if (g_fShowDuration) \
6132 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation " duration"); \
6133 } while (0)
6134
6135 PROFILE_COPY_FN("RTFileCopy/Replace", fsPerfCopyWorker1(g_szDir, g_szDir2));
6136
6137 hFile1 = NIL_RTFILE;
6138 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6139 RTFileDelete(g_szDir2);
6140 hFile2 = NIL_RTFILE;
6141 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6142 PROFILE_COPY_FN("RTFileCopyByHandles/Overwrite", RTFileCopyByHandles(hFile1, hFile2));
6143 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6144 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6145
6146 /* We could benchmark RTFileCopyPart with various block sizes and whatnot...
6147 But it's currently well covered by the two previous operations. */
6148
6149#ifdef RT_OS_LINUX
6150 if (fSendFileBetweenFiles)
6151 {
6152 hFile1 = NIL_RTFILE;
6153 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6154 RTFileDelete(g_szDir2);
6155 hFile2 = NIL_RTFILE;
6156 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6157 PROFILE_COPY_FN("sendfile/overwrite", fsPerfCopyWorkerSendFile(hFile1, hFile2, cbFileMax));
6158 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6159 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6160 }
6161#endif
6162 }
6163
6164 /*
6165 * Clean up.
6166 */
6167 RTFileDelete(InDir2(RT_STR_TUPLE("file22c1")));
6168 RTFileDelete(InDir2(RT_STR_TUPLE("file22c2")));
6169 RTFileDelete(InDir2(RT_STR_TUPLE("file22c3")));
6170 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
6171}
6172
6173
6174static void fsPerfRemote(void)
6175{
6176 RTTestISub("remote");
6177 uint8_t abBuf[16384];
6178
6179
6180 /*
6181 * Create a file on the remote end and check that we can immediately see it.
6182 */
6183 RTTESTI_CHECK_RC_RETV(FsPerfCommsSend("reset\n"
6184 "open 0 'file30' 'w' 'ca'\n"
6185 "writepattern 0 0 0 4096" FSPERF_EOF_STR), VINF_SUCCESS);
6186
6187 RTFILEACTION enmActuallyTaken = RTFILEACTION_END;
6188 RTFILE hFile0 = NIL_RTFILE;
6189 RTTESTI_CHECK_RC(RTFileOpenEx(InDir(RT_STR_TUPLE("file30")), RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
6190 &hFile0, &enmActuallyTaken), VINF_SUCCESS);
6191 RTTESTI_CHECK(enmActuallyTaken == RTFILEACTION_OPENED);
6192 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 4096, NULL), VINF_SUCCESS);
6193 AssertCompile(RT_ELEMENTS(g_abPattern0) == 1);
6194 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 4096, g_abPattern0[0]));
6195 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6196
6197 /*
6198 * Append a little to it on the host and see that we can read it.
6199 */
6200 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 4096 1 1024" FSPERF_EOF_STR), VINF_SUCCESS);
6201 AssertCompile(RT_ELEMENTS(g_abPattern1) == 1);
6202 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1024, NULL), VINF_SUCCESS);
6203 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 1024, g_abPattern1[0]));
6204 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6205
6206 /*
6207 * Have the host truncate the file.
6208 */
6209 RTTESTI_CHECK_RC(FsPerfCommsSend("truncate 0 1024" FSPERF_EOF_STR), VINF_SUCCESS);
6210 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6211 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6212 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1024, NULL), VINF_SUCCESS);
6213 AssertCompile(RT_ELEMENTS(g_abPattern0) == 1);
6214 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 4096, g_abPattern0[0]));
6215 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6216
6217 /*
6218 * Write a bunch of stuff to the file here, then truncate it to a given size,
6219 * then have the host add more, finally test that we can successfully chop off
6220 * what the host added by reissuing the same truncate call as before (issue of
6221 * RDBSS using cached size to noop out set-eof-to-same-size).
6222 */
6223 memset(abBuf, 0xe9, sizeof(abBuf));
6224 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6225 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 16384, NULL), VINF_SUCCESS);
6226 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 8000), VINF_SUCCESS);
6227 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 8000 0 1000" FSPERF_EOF_STR), VINF_SUCCESS);
6228 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 8000), VINF_SUCCESS);
6229 uint64_t cbFile = 0;
6230 RTTESTI_CHECK_RC(RTFileQuerySize(hFile0, &cbFile), VINF_SUCCESS);
6231 RTTESTI_CHECK_MSG(cbFile == 8000, ("cbFile=%u\n", cbFile));
6232 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6233
6234 /* Same, but using RTFileRead to find out and RTFileWrite to define the size. */
6235 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6236 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 0), VINF_SUCCESS);
6237 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 5000, NULL), VINF_SUCCESS);
6238 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 5000 0 1000" FSPERF_EOF_STR), VINF_SUCCESS);
6239 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 5000), VINF_SUCCESS);
6240 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6241 RTTESTI_CHECK_RC(RTFileQuerySize(hFile0, &cbFile), VINF_SUCCESS);
6242 RTTESTI_CHECK_MSG(cbFile == 5000, ("cbFile=%u\n", cbFile));
6243
6244 /* Same, but host truncates rather than adding stuff. */
6245 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6246 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 16384, NULL), VINF_SUCCESS);
6247 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 10000), VINF_SUCCESS);
6248 RTTESTI_CHECK_RC(FsPerfCommsSend("truncate 0 4000" FSPERF_EOF_STR), VINF_SUCCESS);
6249 RTTESTI_CHECK_RC(RTFileQuerySize(hFile0, &cbFile), VINF_SUCCESS);
6250 RTTESTI_CHECK_MSG(cbFile == 4000, ("cbFile=%u\n", cbFile));
6251 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6252
6253 /*
6254 * Test noticing remote size changes when opening a file. Need to keep hFile0
6255 * open here so we're sure to have an inode/FCB for the file in question.
6256 */
6257 memset(abBuf, 0xe7, sizeof(abBuf));
6258 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6259 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 0), VINF_SUCCESS);
6260 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 12288, NULL), VINF_SUCCESS);
6261 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 12288), VINF_SUCCESS);
6262
6263 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 12288 2 4096" FSPERF_EOF_STR), VINF_SUCCESS);
6264
6265 enmActuallyTaken = RTFILEACTION_END;
6266 RTFILE hFile1 = NIL_RTFILE;
6267 RTTESTI_CHECK_RC(RTFileOpenEx(InDir(RT_STR_TUPLE("file30")), RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
6268 &hFile1, &enmActuallyTaken), VINF_SUCCESS);
6269 RTTESTI_CHECK(enmActuallyTaken == RTFILEACTION_OPENED);
6270 AssertCompile(sizeof(abBuf) >= 16384);
6271 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 16384, NULL), VINF_SUCCESS);
6272 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 12288, 0xe7));
6273 AssertCompile(RT_ELEMENTS(g_abPattern2) == 1);
6274 RTTESTI_CHECK(ASMMemIsAllU8(&abBuf[12288], 4096, g_abPattern2[0]));
6275 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
6276 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6277
6278 /* Same, but remote end truncates the file: */
6279 memset(abBuf, 0xe6, sizeof(abBuf));
6280 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6281 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 0), VINF_SUCCESS);
6282 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 12288, NULL), VINF_SUCCESS);
6283 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 12288), VINF_SUCCESS);
6284
6285 RTTESTI_CHECK_RC(FsPerfCommsSend("truncate 0 7500" FSPERF_EOF_STR), VINF_SUCCESS);
6286
6287 enmActuallyTaken = RTFILEACTION_END;
6288 hFile1 = NIL_RTFILE;
6289 RTTESTI_CHECK_RC(RTFileOpenEx(InDir(RT_STR_TUPLE("file30")), RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
6290 &hFile1, &enmActuallyTaken), VINF_SUCCESS);
6291 RTTESTI_CHECK(enmActuallyTaken == RTFILEACTION_OPENED);
6292 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 7500, NULL), VINF_SUCCESS);
6293 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 7500, 0xe6));
6294 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
6295 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6296
6297 RTTESTI_CHECK_RC(RTFileClose(hFile0), VINF_SUCCESS);
6298}
6299
6300
6301
6302/**
6303 * Display the usage to @a pStrm.
6304 */
6305static void Usage(PRTSTREAM pStrm)
6306{
6307 char szExec[FSPERF_MAX_PATH];
6308 RTStrmPrintf(pStrm, "usage: %s <-d <testdir>> [options]\n",
6309 RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
6310 RTStrmPrintf(pStrm, "\n");
6311 RTStrmPrintf(pStrm, "options: \n");
6312
6313 for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
6314 {
6315 char szHelp[80];
6316 const char *pszHelp;
6317 switch (g_aCmdOptions[i].iShort)
6318 {
6319 case 'd': pszHelp = "The directory to use for testing. default: CWD/fstestdir"; break;
6320 case 'r': pszHelp = "Don't abspath test dir (good for deep dirs). default: disabled"; break;
6321 case 'e': pszHelp = "Enables all tests. default: -e"; break;
6322 case 'z': pszHelp = "Disables all tests. default: -e"; break;
6323 case 's': pszHelp = "Set benchmark duration in seconds. default: 10 sec"; break;
6324 case 'm': pszHelp = "Set benchmark duration in milliseconds. default: 10000 ms"; break;
6325 case 'v': pszHelp = "More verbose execution."; break;
6326 case 'q': pszHelp = "Quiet execution."; break;
6327 case 'h': pszHelp = "Displays this help and exit"; break;
6328 case 'V': pszHelp = "Displays the program revision"; break;
6329 case kCmdOpt_ShowDuration: pszHelp = "Show duration of profile runs. default: --no-show-duration"; break;
6330 case kCmdOpt_NoShowDuration: pszHelp = "Hide duration of profile runs. default: --no-show-duration"; break;
6331 case kCmdOpt_ShowIterations: pszHelp = "Show iteration count for profile runs. default: --no-show-iterations"; break;
6332 case kCmdOpt_NoShowIterations: pszHelp = "Hide iteration count for profile runs. default: --no-show-iterations"; break;
6333 case kCmdOpt_ManyFiles: pszHelp = "Count of files in big test dir. default: --many-files 10000"; break;
6334 case kCmdOpt_NoManyFiles: pszHelp = "Skip big test dir with many files. default: --many-files 10000"; break;
6335 case kCmdOpt_ManyTreeFilesPerDir: pszHelp = "Count of files per directory in test tree. default: 640"; break;
6336 case kCmdOpt_ManyTreeSubdirsPerDir: pszHelp = "Count of subdirs per directory in test tree. default: 16"; break;
6337 case kCmdOpt_ManyTreeDepth: pszHelp = "Depth of test tree (not counting root). default: 1"; break;
6338#if defined(RT_OS_WINDOWS)
6339 case kCmdOpt_MaxBufferSize: pszHelp = "For avoiding the MDL limit on windows. default: 32MiB"; break;
6340#else
6341 case kCmdOpt_MaxBufferSize: pszHelp = "For avoiding the MDL limit on windows. default: 0"; break;
6342#endif
6343 case kCmdOpt_MMapPlacement: pszHelp = "When to do mmap testing (caching effects): first, between (default), last "; break;
6344 case kCmdOpt_IgnoreNoCache: pszHelp = "Ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
6345 case kCmdOpt_NoIgnoreNoCache: pszHelp = "Do not ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
6346 case kCmdOpt_IoFileSize: pszHelp = "Size of file used for I/O tests. default: 512 MB"; break;
6347 case kCmdOpt_SetBlockSize: pszHelp = "Sets single I/O block size (in bytes)."; break;
6348 case kCmdOpt_AddBlockSize: pszHelp = "Adds an I/O block size (in bytes)."; break;
6349 default:
6350 if (g_aCmdOptions[i].iShort >= kCmdOpt_First)
6351 {
6352 if (RTStrStartsWith(g_aCmdOptions[i].pszLong, "--no-"))
6353 RTStrPrintf(szHelp, sizeof(szHelp), "Disables the '%s' test.", g_aCmdOptions[i].pszLong + 5);
6354 else
6355 RTStrPrintf(szHelp, sizeof(szHelp), "Enables the '%s' test.", g_aCmdOptions[i].pszLong + 2);
6356 pszHelp = szHelp;
6357 }
6358 else
6359 pszHelp = "Option undocumented";
6360 break;
6361 }
6362 if ((unsigned)g_aCmdOptions[i].iShort < 127U)
6363 {
6364 char szOpt[64];
6365 RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
6366 RTStrmPrintf(pStrm, " %-19s %s\n", szOpt, pszHelp);
6367 }
6368 else
6369 RTStrmPrintf(pStrm, " %-19s %s\n", g_aCmdOptions[i].pszLong, pszHelp);
6370 }
6371}
6372
6373
6374static uint32_t fsPerfCalcManyTreeFiles(void)
6375{
6376 uint32_t cDirs = 1;
6377 for (uint32_t i = 0, cDirsAtLevel = 1; i < g_cManyTreeDepth; i++)
6378 {
6379 cDirs += cDirsAtLevel * g_cManyTreeSubdirsPerDir;
6380 cDirsAtLevel *= g_cManyTreeSubdirsPerDir;
6381 }
6382 return g_cManyTreeFilesPerDir * cDirs;
6383}
6384
6385
6386int main(int argc, char *argv[])
6387{
6388 /*
6389 * Init IPRT and globals.
6390 */
6391 int rc = RTTestInitAndCreate("FsPerf", &g_hTest);
6392 if (rc)
6393 return rc;
6394 RTListInit(&g_ManyTreeHead);
6395
6396 /* Query page size, offset mask and page shift of the system. */
6397 g_cbPage = RTSystemGetPageSize();
6398 g_fPageOffset = RTSystemGetPageOffsetMask();
6399 g_cPageShift = RTSystemGetPageShift();
6400
6401 /*
6402 * Default values.
6403 */
6404 char szDefaultDir[RTPATH_MAX];
6405 const char *pszDir = szDefaultDir;
6406
6407 /* As default retrieve the system's temporary directory and create a test directory beneath it,
6408 * as this binary might get executed from a read-only medium such as ${CDROM}. */
6409 rc = RTPathTemp(szDefaultDir, sizeof(szDefaultDir));
6410 if (RT_SUCCESS(rc))
6411 {
6412 char szDirName[32];
6413 RTStrPrintf2(szDirName, sizeof(szDirName), "fstestdir-%u" RTPATH_SLASH_STR, RTProcSelf());
6414 rc = RTPathAppend(szDefaultDir, sizeof(szDefaultDir), szDirName);
6415 if (RT_FAILURE(rc))
6416 {
6417 RTTestFailed(g_hTest, "Unable to append dir name in temp dir, rc=%Rrc\n", rc);
6418 return RTTestSummaryAndDestroy(g_hTest);
6419 }
6420 }
6421 else
6422 {
6423 RTTestFailed(g_hTest, "Unable to retrieve temp dir, rc=%Rrc\n", rc);
6424 return RTTestSummaryAndDestroy(g_hTest);
6425 }
6426
6427 RTTestIPrintf(RTTESTLVL_INFO, "Default directory is: %s\n", szDefaultDir);
6428
6429 bool fCommsSlave = false;
6430
6431 RTGETOPTUNION ValueUnion;
6432 RTGETOPTSTATE GetState;
6433 RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
6434 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
6435 {
6436 switch (rc)
6437 {
6438 case 'c':
6439 if (!g_fRelativeDir)
6440 rc = RTPathAbs(ValueUnion.psz, g_szCommsDir, sizeof(g_szCommsDir) - 128);
6441 else
6442 rc = RTStrCopy(g_szCommsDir, sizeof(g_szCommsDir) - 128, ValueUnion.psz);
6443 if (RT_FAILURE(rc))
6444 {
6445 RTTestFailed(g_hTest, "%s(%s) failed: %Rrc\n", g_fRelativeDir ? "RTStrCopy" : "RTAbsPath", pszDir, rc);
6446 return RTTestSummaryAndDestroy(g_hTest);
6447 }
6448 RTPathEnsureTrailingSeparator(g_szCommsDir, sizeof(g_szCommsDir));
6449 g_cchCommsDir = strlen(g_szCommsDir);
6450
6451 rc = RTPathJoin(g_szCommsSubDir, sizeof(g_szCommsSubDir) - 128, g_szCommsDir, "comms" RTPATH_SLASH_STR);
6452 if (RT_FAILURE(rc))
6453 {
6454 RTTestFailed(g_hTest, "RTPathJoin(%s,,'comms/') failed: %Rrc\n", g_szCommsDir, rc);
6455 return RTTestSummaryAndDestroy(g_hTest);
6456 }
6457 g_cchCommsSubDir = strlen(g_szCommsSubDir);
6458 break;
6459
6460 case 'C':
6461 fCommsSlave = true;
6462 break;
6463
6464 case 'd':
6465 pszDir = ValueUnion.psz;
6466 break;
6467
6468 case 'r':
6469 g_fRelativeDir = true;
6470 break;
6471
6472 case 's':
6473 if (ValueUnion.u32 == 0)
6474 g_nsTestRun = RT_NS_1SEC_64 * 10;
6475 else
6476 g_nsTestRun = ValueUnion.u32 * RT_NS_1SEC_64;
6477 break;
6478
6479 case 'm':
6480 if (ValueUnion.u64 == 0)
6481 g_nsTestRun = RT_NS_1SEC_64 * 10;
6482 else
6483 g_nsTestRun = ValueUnion.u64 * RT_NS_1MS;
6484 break;
6485
6486 case 'e':
6487 g_fManyFiles = true;
6488 g_fOpen = true;
6489 g_fFStat = true;
6490#ifdef RT_OS_WINDOWS
6491 g_fNtQueryInfoFile = true;
6492 g_fNtQueryVolInfoFile = true;
6493#endif
6494 g_fFChMod = true;
6495 g_fFUtimes = true;
6496 g_fStat = true;
6497 g_fChMod = true;
6498 g_fUtimes = true;
6499 g_fRename = true;
6500 g_fDirOpen = true;
6501 g_fDirEnum = true;
6502 g_fMkRmDir = true;
6503 g_fStatVfs = true;
6504 g_fRm = true;
6505 g_fChSize = true;
6506 g_fReadTests = true;
6507 g_fReadPerf = true;
6508#ifdef FSPERF_TEST_SENDFILE
6509 g_fSendFile = true;
6510#endif
6511#ifdef RT_OS_LINUX
6512 g_fSplice = true;
6513#endif
6514 g_fWriteTests = true;
6515 g_fWritePerf = true;
6516 g_fSeek = true;
6517 g_fFSync = true;
6518 g_fMMap = true;
6519 g_fMMapCoherency = true;
6520 g_fCopy = true;
6521 g_fRemote = true;
6522 break;
6523
6524 case 'z':
6525 g_fManyFiles = false;
6526 g_fOpen = false;
6527 g_fFStat = false;
6528#ifdef RT_OS_WINDOWS
6529 g_fNtQueryInfoFile = false;
6530 g_fNtQueryVolInfoFile = false;
6531#endif
6532 g_fFChMod = false;
6533 g_fFUtimes = false;
6534 g_fStat = false;
6535 g_fChMod = false;
6536 g_fUtimes = false;
6537 g_fRename = false;
6538 g_fDirOpen = false;
6539 g_fDirEnum = false;
6540 g_fMkRmDir = false;
6541 g_fStatVfs = false;
6542 g_fRm = false;
6543 g_fChSize = false;
6544 g_fReadTests = false;
6545 g_fReadPerf = false;
6546#ifdef FSPERF_TEST_SENDFILE
6547 g_fSendFile = false;
6548#endif
6549#ifdef RT_OS_LINUX
6550 g_fSplice = false;
6551#endif
6552 g_fWriteTests = false;
6553 g_fWritePerf = false;
6554 g_fSeek = false;
6555 g_fFSync = false;
6556 g_fMMap = false;
6557 g_fMMapCoherency = false;
6558 g_fCopy = false;
6559 g_fRemote = false;
6560 break;
6561
6562#define CASE_OPT(a_Stem) \
6563 case RT_CONCAT(kCmdOpt_,a_Stem): RT_CONCAT(g_f,a_Stem) = true; break; \
6564 case RT_CONCAT(kCmdOpt_No,a_Stem): RT_CONCAT(g_f,a_Stem) = false; break
6565 CASE_OPT(Open);
6566 CASE_OPT(FStat);
6567#ifdef RT_OS_WINDOWS
6568 CASE_OPT(NtQueryInfoFile);
6569 CASE_OPT(NtQueryVolInfoFile);
6570#endif
6571 CASE_OPT(FChMod);
6572 CASE_OPT(FUtimes);
6573 CASE_OPT(Stat);
6574 CASE_OPT(ChMod);
6575 CASE_OPT(Utimes);
6576 CASE_OPT(Rename);
6577 CASE_OPT(DirOpen);
6578 CASE_OPT(DirEnum);
6579 CASE_OPT(MkRmDir);
6580 CASE_OPT(StatVfs);
6581 CASE_OPT(Rm);
6582 CASE_OPT(ChSize);
6583 CASE_OPT(ReadTests);
6584 CASE_OPT(ReadPerf);
6585#ifdef FSPERF_TEST_SENDFILE
6586 CASE_OPT(SendFile);
6587#endif
6588#ifdef RT_OS_LINUX
6589 CASE_OPT(Splice);
6590#endif
6591 CASE_OPT(WriteTests);
6592 CASE_OPT(WritePerf);
6593 CASE_OPT(Seek);
6594 CASE_OPT(FSync);
6595 CASE_OPT(MMap);
6596 CASE_OPT(MMapCoherency);
6597 CASE_OPT(IgnoreNoCache);
6598 CASE_OPT(Copy);
6599 CASE_OPT(Remote);
6600
6601 CASE_OPT(ShowDuration);
6602 CASE_OPT(ShowIterations);
6603#undef CASE_OPT
6604
6605 case kCmdOpt_ManyFiles:
6606 g_fManyFiles = ValueUnion.u32 > 0;
6607 g_cManyFiles = ValueUnion.u32;
6608 break;
6609
6610 case kCmdOpt_NoManyFiles:
6611 g_fManyFiles = false;
6612 break;
6613
6614 case kCmdOpt_ManyTreeFilesPerDir:
6615 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= _64M)
6616 {
6617 g_cManyTreeFilesPerDir = ValueUnion.u32;
6618 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
6619 break;
6620 }
6621 RTTestFailed(g_hTest, "Out of range --files-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6622 return RTTestSummaryAndDestroy(g_hTest);
6623
6624 case kCmdOpt_ManyTreeSubdirsPerDir:
6625 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= 1024)
6626 {
6627 g_cManyTreeSubdirsPerDir = ValueUnion.u32;
6628 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
6629 break;
6630 }
6631 RTTestFailed(g_hTest, "Out of range --subdirs-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6632 return RTTestSummaryAndDestroy(g_hTest);
6633
6634 case kCmdOpt_ManyTreeDepth:
6635 if (ValueUnion.u32 <= 8)
6636 {
6637 g_cManyTreeDepth = ValueUnion.u32;
6638 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
6639 break;
6640 }
6641 RTTestFailed(g_hTest, "Out of range --tree-depth value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6642 return RTTestSummaryAndDestroy(g_hTest);
6643
6644 case kCmdOpt_MaxBufferSize:
6645 if (ValueUnion.u32 >= 4096)
6646 g_cbMaxBuffer = ValueUnion.u32;
6647 else if (ValueUnion.u32 == 0)
6648 g_cbMaxBuffer = UINT32_MAX;
6649 else
6650 {
6651 RTTestFailed(g_hTest, "max buffer size is less than 4KB: %#x\n", ValueUnion.u32);
6652 return RTTestSummaryAndDestroy(g_hTest);
6653 }
6654 break;
6655
6656 case kCmdOpt_IoFileSize:
6657 if (ValueUnion.u64 == 0)
6658 g_cbIoFile = _512M;
6659 else
6660 g_cbIoFile = ValueUnion.u64;
6661 break;
6662
6663 case kCmdOpt_SetBlockSize:
6664 if (ValueUnion.u32 > 0)
6665 {
6666 g_cIoBlocks = 1;
6667 g_acbIoBlocks[0] = ValueUnion.u32;
6668 }
6669 else
6670 {
6671 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6672 return RTTestSummaryAndDestroy(g_hTest);
6673 }
6674 break;
6675
6676 case kCmdOpt_AddBlockSize:
6677 if (g_cIoBlocks >= RT_ELEMENTS(g_acbIoBlocks))
6678 RTTestFailed(g_hTest, "Too many I/O block sizes: max %u\n", RT_ELEMENTS(g_acbIoBlocks));
6679 else if (ValueUnion.u32 == 0)
6680 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6681 else
6682 {
6683 g_acbIoBlocks[g_cIoBlocks++] = ValueUnion.u32;
6684 break;
6685 }
6686 return RTTestSummaryAndDestroy(g_hTest);
6687
6688 case kCmdOpt_MMapPlacement:
6689 if (strcmp(ValueUnion.psz, "first") == 0)
6690 g_iMMapPlacement = -1;
6691 else if ( strcmp(ValueUnion.psz, "between") == 0
6692 || strcmp(ValueUnion.psz, "default") == 0)
6693 g_iMMapPlacement = 0;
6694 else if (strcmp(ValueUnion.psz, "last") == 0)
6695 g_iMMapPlacement = 1;
6696 else
6697 {
6698 RTTestFailed(g_hTest,
6699 "Invalid --mmap-placment directive '%s'! Expected 'first', 'last', 'between' or 'default'.\n",
6700 ValueUnion.psz);
6701 return RTTestSummaryAndDestroy(g_hTest);
6702 }
6703 break;
6704
6705 case 'q':
6706 g_uVerbosity = 0;
6707 break;
6708
6709 case 'v':
6710 g_uVerbosity++;
6711 break;
6712
6713 case 'h':
6714 Usage(g_pStdOut);
6715 return RTEXITCODE_SUCCESS;
6716
6717 case 'V':
6718 {
6719 char szRev[] = "$Revision: 101645 $";
6720 szRev[RT_ELEMENTS(szRev) - 2] = '\0';
6721 RTPrintf(RTStrStrip(strchr(szRev, ':') + 1));
6722 return RTEXITCODE_SUCCESS;
6723 }
6724
6725 default:
6726 return RTGetOptPrintError(rc, &ValueUnion);
6727 }
6728 }
6729
6730 /*
6731 * Populate g_szDir.
6732 */
6733 if (!g_fRelativeDir)
6734 rc = RTPathAbs(pszDir, g_szDir, sizeof(g_szDir) - FSPERF_MAX_NEEDED_PATH);
6735 else
6736 rc = RTStrCopy(g_szDir, sizeof(g_szDir) - FSPERF_MAX_NEEDED_PATH, pszDir);
6737 if (RT_FAILURE(rc))
6738 {
6739 RTTestFailed(g_hTest, "%s(%s) failed: %Rrc\n", g_fRelativeDir ? "RTStrCopy" : "RTAbsPath", pszDir, rc);
6740 return RTTestSummaryAndDestroy(g_hTest);
6741 }
6742 RTPathEnsureTrailingSeparator(g_szDir, sizeof(g_szDir));
6743 g_cchDir = strlen(g_szDir);
6744
6745 /*
6746 * If communication slave, go do that and be done.
6747 */
6748 if (fCommsSlave)
6749 {
6750 if (pszDir == szDefaultDir)
6751 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The slave must have a working directory specified (-d)!");
6752 return FsPerfCommsSlave();
6753 }
6754
6755 /*
6756 * Create the test directory with an 'empty' subdirectory under it,
6757 * execute the tests, and remove directory when done.
6758 */
6759 RTTestBanner(g_hTest);
6760 if (!RTPathExists(g_szDir))
6761 {
6762 /* The base dir: */
6763 rc = RTDirCreate(g_szDir, 0755,
6764 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
6765 if (RT_SUCCESS(rc))
6766 {
6767 RTTestIPrintf(RTTESTLVL_ALWAYS, "Test dir: %s\n", g_szDir);
6768 rc = fsPrepTestArea();
6769 if (RT_SUCCESS(rc))
6770 {
6771 /* Profile RTTimeNanoTS(). */
6772 fsPerfNanoTS();
6773
6774 /* Do tests: */
6775 if (g_fManyFiles)
6776 fsPerfManyFiles();
6777 if (g_fOpen)
6778 fsPerfOpen();
6779 if (g_fFStat)
6780 fsPerfFStat();
6781#ifdef RT_OS_WINDOWS
6782 if (g_fNtQueryInfoFile)
6783 fsPerfNtQueryInfoFile();
6784 if (g_fNtQueryVolInfoFile)
6785 fsPerfNtQueryVolInfoFile();
6786#endif
6787 if (g_fFChMod)
6788 fsPerfFChMod();
6789 if (g_fFUtimes)
6790 fsPerfFUtimes();
6791 if (g_fStat)
6792 fsPerfStat();
6793 if (g_fChMod)
6794 fsPerfChmod();
6795 if (g_fUtimes)
6796 fsPerfUtimes();
6797 if (g_fRename)
6798 fsPerfRename();
6799 if (g_fDirOpen)
6800 vsPerfDirOpen();
6801 if (g_fDirEnum)
6802 vsPerfDirEnum();
6803 if (g_fMkRmDir)
6804 fsPerfMkRmDir();
6805 if (g_fStatVfs)
6806 fsPerfStatVfs();
6807 if (g_fRm || g_fManyFiles)
6808 fsPerfRm(); /* deletes manyfiles and manytree */
6809 if (g_fChSize)
6810 fsPerfChSize();
6811 if ( g_fReadPerf || g_fReadTests || g_fWritePerf || g_fWriteTests
6812#ifdef FSPERF_TEST_SENDFILE
6813 || g_fSendFile
6814#endif
6815#ifdef RT_OS_LINUX
6816 || g_fSplice
6817#endif
6818 || g_fSeek || g_fFSync || g_fMMap)
6819 fsPerfIo();
6820 if (g_fCopy)
6821 fsPerfCopy();
6822 if (g_fRemote && g_szCommsDir[0] != '\0')
6823 fsPerfRemote();
6824 }
6825
6826 /*
6827 * Cleanup:
6828 */
6829 FsPerfCommsShutdownSlave();
6830
6831 g_szDir[g_cchDir] = '\0';
6832 rc = RTDirRemoveRecursive(g_szDir, RTDIRRMREC_F_CONTENT_AND_DIR | (g_fRelativeDir ? RTDIRRMREC_F_NO_ABS_PATH : 0));
6833 if (RT_FAILURE(rc))
6834 RTTestFailed(g_hTest, "RTDirRemoveRecursive(%s,) -> %Rrc\n", g_szDir, rc);
6835 }
6836 else
6837 RTTestFailed(g_hTest, "RTDirCreate(%s) -> %Rrc\n", g_szDir, rc);
6838 }
6839 else
6840 RTTestFailed(g_hTest, "Test directory already exists: %s\n", g_szDir);
6841
6842 FsPerfCommsShutdownSlave();
6843
6844 return RTTestSummaryAndDestroy(g_hTest);
6845}
6846
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette