VirtualBox

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

Last change on this file since 106212 was 106061, checked in by vboxsync, 4 months ago

Copyright year updates by scm.

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

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