VirtualBox

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

Last change on this file since 103599 was 103423, checked in by vboxsync, 12 months ago

FsPerf: Reverted changes from r161579 again as these are unnecessary - there should be no risk of buffer overflows (see FSPERF_MAX_NEEDED_PATH) and they are inconsistent (continues testing after a path buffer overflow). Besides RTStrCat(RTStrCpy2(...)..) is unsafe and the whole change does not improve the code. bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 288.3 KB
Line 
1/* $Id: FsPerf.cpp 103423 2024-02-19 10:24:09Z vboxsync $ */
2/** @file
3 * FsPerf - File System (Shared Folders) Performance Benchmark.
4 */
5
6/*
7 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.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_WINDOWS)
3350 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_ACCESS_DENIED);
3351 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_ACCESS_DENIED);
3352 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
3353#elif defined(RT_OS_DARWIN) /* unlink() on xnu 16.7.0 is behaviour totally werid: */
3354 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER);
3355 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VINF_SUCCESS /*WTF?!?*/);
3356 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
3357#elif defined(RT_OS_OS2) /* OS/2 has a busted unlink, it think it should remove directories too. */
3358 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("."))), VERR_DIR_NOT_EMPTY);
3359 int rc = RTFileDelete(InDir(RT_STR_TUPLE("..")));
3360 if (rc != VERR_DIR_NOT_EMPTY && rc != VERR_FILE_NOT_FOUND && rc != VERR_RESOURCE_BUSY)
3361 RTTestIFailed("RTFileDelete(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY or VERR_FILE_NOT_FOUND or VERR_RESOURCE_BUSY", g_szDir, rc);
3362 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE(""))), VERR_DIR_NOT_EMPTY);
3363 APIRET orc;
3364 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE(".")))) == ERROR_ACCESS_DENIED,
3365 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
3366 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("..")))) == ERROR_ACCESS_DENIED,
3367 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
3368 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("")))) == ERROR_PATH_NOT_FOUND,
3369 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_PATH_NOT_FOUND)); /* hpfs+jfs; weird. */
3370
3371#else
3372 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_IS_A_DIRECTORY);
3373 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_IS_A_DIRECTORY);
3374 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_IS_A_DIRECTORY);
3375#endif
3376
3377 /* Shallow: */
3378 RTFILE hFile1;
3379 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file19")),
3380 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
3381 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3382 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
3383 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VERR_FILE_NOT_FOUND);
3384
3385 if (g_fManyFiles)
3386 {
3387 /*
3388 * Profile the deletion of the manyfiles content.
3389 */
3390 {
3391 InDir(RT_STR_TUPLE("manyfiles" RTPATH_SLASH_STR));
3392 size_t const offFilename = strlen(g_szDir);
3393 fsPerfYield();
3394 uint64_t const nsStart = RTTimeNanoTS();
3395 for (uint32_t i = 0; i < g_cManyFiles; i++)
3396 {
3397 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
3398 RTTESTI_CHECK_RC_RETV(RTFileDelete(g_szDir), VINF_SUCCESS);
3399 }
3400 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
3401 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files from a single directory", g_cManyFiles);
3402 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (single dir)");
3403 }
3404
3405 /*
3406 * Ditto for the manytree.
3407 */
3408 {
3409 char szPath[FSPERF_MAX_PATH];
3410 uint64_t const nsStart = RTTimeNanoTS();
3411 DO_MANYTREE_FN(szPath, RTTESTI_CHECK_RC_RETV(RTFileDelete(szPath), VINF_SUCCESS));
3412 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
3413 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files in tree", g_cManyTreeFiles);
3414 RTTestIValueF(cNsElapsed / g_cManyTreeFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (tree)");
3415 }
3416 }
3417}
3418
3419
3420static void fsPerfChSize(void)
3421{
3422 RTTestISub("chsize");
3423
3424 /*
3425 * We need some free space to perform this test.
3426 */
3427 g_szDir[g_cchDir] = '\0';
3428 RTFOFF cbFree = 0;
3429 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
3430 if (cbFree < _1M)
3431 {
3432 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 1MB", cbFree);
3433 return;
3434 }
3435
3436 /*
3437 * Create a file and play around with it's size.
3438 * We let the current file position follow the end position as we make changes.
3439 */
3440 RTFILE hFile1;
3441 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file20")),
3442 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
3443 uint64_t cbFile = UINT64_MAX;
3444 RTTESTI_CHECK_RC(RTFileQuerySize(hFile1, &cbFile), VINF_SUCCESS);
3445 RTTESTI_CHECK(cbFile == 0);
3446
3447 uint8_t abBuf[4096];
3448 static uint64_t const s_acbChanges[] =
3449 {
3450 1023, 1024, 1024, 1025, 8192, 11111, _1M, _8M, _8M,
3451 _4M, _2M + 1, _1M - 1, 65537, 65536, 32768, 8000, 7999, 7998, 1024, 1, 0
3452 };
3453 uint64_t cbOld = 0;
3454 for (unsigned i = 0; i < RT_ELEMENTS(s_acbChanges); i++)
3455 {
3456 uint64_t cbNew = s_acbChanges[i];
3457 if (cbNew + _64K >= (uint64_t)cbFree)
3458 continue;
3459
3460 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, cbNew), VINF_SUCCESS);
3461 RTTESTI_CHECK_RC(RTFileQuerySize(hFile1, &cbFile), VINF_SUCCESS);
3462 RTTESTI_CHECK_MSG(cbFile == cbNew, ("cbFile=%#RX64 cbNew=%#RX64\n", cbFile, cbNew));
3463
3464 if (cbNew > cbOld)
3465 {
3466 /* Check that the extension is all zeroed: */
3467 uint64_t cbLeft = cbNew - cbOld;
3468 while (cbLeft > 0)
3469 {
3470 memset(abBuf, 0xff, sizeof(abBuf));
3471 size_t cbToRead = sizeof(abBuf);
3472 if (cbToRead > cbLeft)
3473 cbToRead = (size_t)cbLeft;
3474 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, cbToRead, NULL), VINF_SUCCESS);
3475 RTTESTI_CHECK(ASMMemIsZero(abBuf, cbToRead));
3476 cbLeft -= cbToRead;
3477 }
3478 }
3479 else
3480 {
3481 /* Check that reading fails with EOF because current position is now beyond the end: */
3482 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
3483
3484 /* Keep current position at the end of the file: */
3485 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbNew, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3486 }
3487 cbOld = cbNew;
3488 }
3489
3490 /*
3491 * Profile just the file setting operation itself, keeping the changes within
3492 * an allocation unit to avoid needing to adjust the actual (host) FS allocation.
3493 * ASSUMES allocation unit >= 512 and power of two.
3494 */
3495 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, _64K), VINF_SUCCESS);
3496 PROFILE_FN(RTFileSetSize(hFile1, _64K - (iIteration & 255) - 128), g_nsTestRun, "RTFileSetSize/noalloc");
3497
3498 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
3499 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3500 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
3501}
3502
3503
3504static int fsPerfIoPrepFileWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf)
3505{
3506 /*
3507 * Fill the file with 0xf6 and insert offset markers with 1KB intervals.
3508 */
3509 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3510 memset(pbBuf, 0xf6, cbBuf);
3511 uint64_t cbLeft = cbFile;
3512 uint64_t off = 0;
3513 while (cbLeft > 0)
3514 {
3515 Assert(!(off & (_1K - 1)));
3516 Assert(!(cbBuf & (_1K - 1)));
3517 for (size_t offBuf = 0; offBuf < cbBuf; offBuf += _1K, off += _1K)
3518 *(uint64_t *)&pbBuf[offBuf] = off;
3519
3520 size_t cbToWrite = cbBuf;
3521 if (cbToWrite > cbLeft)
3522 cbToWrite = (size_t)cbLeft;
3523
3524 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbToWrite, NULL), VINF_SUCCESS, rcCheck);
3525 cbLeft -= cbToWrite;
3526 }
3527 return VINF_SUCCESS;
3528}
3529
3530static int fsPerfIoPrepFile(RTFILE hFile1, uint64_t cbFile, uint8_t **ppbFree)
3531{
3532 /*
3533 * Seek to the end - 4K and write the last 4K.
3534 * This should have the effect of filling the whole file with zeros.
3535 */
3536 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, cbFile - _4K, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3537 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, g_abRTZero4K, _4K, NULL), VINF_SUCCESS, rcCheck);
3538
3539 /*
3540 * Check that the space we searched across actually is zero filled.
3541 */
3542 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3543 size_t cbBuf = RT_MIN(_1M, g_cbMaxBuffer);
3544 uint8_t *pbBuf = *ppbFree = (uint8_t *)RTMemAlloc(cbBuf);
3545 RTTESTI_CHECK_RET(pbBuf != NULL, VERR_NO_MEMORY);
3546 uint64_t cbLeft = cbFile;
3547 while (cbLeft > 0)
3548 {
3549 size_t cbToRead = cbBuf;
3550 if (cbToRead > cbLeft)
3551 cbToRead = (size_t)cbLeft;
3552 pbBuf[cbToRead - 1] = 0xff;
3553
3554 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBuf, cbToRead, NULL), VINF_SUCCESS, rcCheck);
3555 RTTESTI_CHECK_RET(ASMMemIsZero(pbBuf, cbToRead), VERR_MISMATCH);
3556
3557 cbLeft -= cbToRead;
3558 }
3559
3560 /*
3561 * Fill the file with 0xf6 and insert offset markers with 1KB intervals.
3562 */
3563 return fsPerfIoPrepFileWorker(hFile1, cbFile, pbBuf, cbBuf);
3564}
3565
3566/**
3567 * Used in relation to the mmap test when in non-default position.
3568 */
3569static int fsPerfReinitFile(RTFILE hFile1, uint64_t cbFile)
3570{
3571 size_t cbBuf = RT_MIN(_1M, g_cbMaxBuffer);
3572 uint8_t *pbBuf = (uint8_t *)RTMemAlloc(cbBuf);
3573 RTTESTI_CHECK_RET(pbBuf != NULL, VERR_NO_MEMORY);
3574
3575 int rc = fsPerfIoPrepFileWorker(hFile1, cbFile, pbBuf, cbBuf);
3576
3577 RTMemFree(pbBuf);
3578 return rc;
3579}
3580
3581/**
3582 * Checks the content read from the file fsPerfIoPrepFile() prepared.
3583 */
3584static bool fsPerfCheckReadBuf(unsigned uLineNo, uint64_t off, uint8_t const *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
3585{
3586 uint32_t cMismatches = 0;
3587 size_t offBuf = 0;
3588 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
3589 while (offBuf < cbBuf)
3590 {
3591 /*
3592 * Check the offset marker:
3593 */
3594 if (offBlock < sizeof(uint64_t))
3595 {
3596 RTUINT64U uMarker;
3597 uMarker.u = off + offBuf - offBlock;
3598 unsigned offMarker = offBlock & (sizeof(uint64_t) - 1);
3599 while (offMarker < sizeof(uint64_t) && offBuf < cbBuf)
3600 {
3601 if (uMarker.au8[offMarker] != pbBuf[offBuf])
3602 {
3603 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#x",
3604 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], uMarker.au8[offMarker]);
3605 if (cMismatches++ > 32)
3606 return false;
3607 }
3608 offMarker++;
3609 offBuf++;
3610 }
3611 offBlock = sizeof(uint64_t);
3612 }
3613
3614 /*
3615 * Check the filling:
3616 */
3617 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf - offBuf);
3618 if ( cbFilling == 0
3619 || ASMMemIsAllU8(&pbBuf[offBuf], cbFilling, bFiller))
3620 offBuf += cbFilling;
3621 else
3622 {
3623 /* Some mismatch, locate it/them: */
3624 while (cbFilling > 0 && offBuf < cbBuf)
3625 {
3626 if (pbBuf[offBuf] != bFiller)
3627 {
3628 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#04x",
3629 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], bFiller);
3630 if (cMismatches++ > 32)
3631 return false;
3632 }
3633 offBuf++;
3634 cbFilling--;
3635 }
3636 }
3637 offBlock = 0;
3638 }
3639 return cMismatches == 0;
3640}
3641
3642
3643/**
3644 * Sets up write buffer with offset markers and fillers.
3645 */
3646static void fsPerfFillWriteBuf(uint64_t off, uint8_t *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
3647{
3648 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
3649 while (cbBuf > 0)
3650 {
3651 /* The marker. */
3652 if (offBlock < sizeof(uint64_t))
3653 {
3654 RTUINT64U uMarker;
3655 uMarker.u = off + offBlock;
3656 if (cbBuf > sizeof(uMarker) - offBlock)
3657 {
3658 memcpy(pbBuf, &uMarker.au8[offBlock], sizeof(uMarker) - offBlock);
3659 pbBuf += sizeof(uMarker) - offBlock;
3660 cbBuf -= sizeof(uMarker) - offBlock;
3661 off += sizeof(uMarker) - offBlock;
3662 }
3663 else
3664 {
3665 memcpy(pbBuf, &uMarker.au8[offBlock], cbBuf);
3666 return;
3667 }
3668 offBlock = sizeof(uint64_t);
3669 }
3670
3671 /* Do the filling. */
3672 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf);
3673 memset(pbBuf, bFiller, cbFilling);
3674 pbBuf += cbFilling;
3675 cbBuf -= cbFilling;
3676 off += cbFilling;
3677
3678 offBlock = 0;
3679 }
3680}
3681
3682
3683
3684static void fsPerfIoSeek(RTFILE hFile1, uint64_t cbFile)
3685{
3686 /*
3687 * Do a bunch of search tests, most which are random.
3688 */
3689 struct
3690 {
3691 int rc;
3692 uint32_t uMethod;
3693 int64_t offSeek;
3694 uint64_t offActual;
3695
3696 } aSeeks[9 + 64] =
3697 {
3698 { VINF_SUCCESS, RTFILE_SEEK_BEGIN, 0, 0 },
3699 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
3700 { VINF_SUCCESS, RTFILE_SEEK_END, 0, cbFile },
3701 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -4096, cbFile - 4096 },
3702 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 4096 - (int64_t)cbFile, 0 },
3703 { VINF_SUCCESS, RTFILE_SEEK_END, -(int64_t)cbFile/2, cbFile / 2 + (cbFile & 1) },
3704 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -(int64_t)cbFile/2, 0 },
3705#if defined(RT_OS_WINDOWS)
3706 { VERR_NEGATIVE_SEEK, RTFILE_SEEK_CURRENT, -1, 0 },
3707#else
3708 { VERR_INVALID_PARAMETER, RTFILE_SEEK_CURRENT, -1, 0 },
3709#endif
3710 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
3711 };
3712
3713 uint64_t offActual = 0;
3714 for (unsigned i = 9; i < RT_ELEMENTS(aSeeks); i++)
3715 {
3716 switch (RTRandU32Ex(RTFILE_SEEK_BEGIN, RTFILE_SEEK_END))
3717 {
3718 default: AssertFailedBreak();
3719 case RTFILE_SEEK_BEGIN:
3720 aSeeks[i].uMethod = RTFILE_SEEK_BEGIN;
3721 aSeeks[i].rc = VINF_SUCCESS;
3722 aSeeks[i].offSeek = RTRandU64Ex(0, cbFile + cbFile / 8);
3723 aSeeks[i].offActual = offActual = aSeeks[i].offSeek;
3724 break;
3725
3726 case RTFILE_SEEK_CURRENT:
3727 aSeeks[i].uMethod = RTFILE_SEEK_CURRENT;
3728 aSeeks[i].rc = VINF_SUCCESS;
3729 aSeeks[i].offSeek = (int64_t)RTRandU64Ex(0, cbFile + cbFile / 8) - (int64_t)offActual;
3730 aSeeks[i].offActual = offActual += aSeeks[i].offSeek;
3731 break;
3732
3733 case RTFILE_SEEK_END:
3734 aSeeks[i].uMethod = RTFILE_SEEK_END;
3735 aSeeks[i].rc = VINF_SUCCESS;
3736 aSeeks[i].offSeek = -(int64_t)RTRandU64Ex(0, cbFile);
3737 aSeeks[i].offActual = offActual = cbFile + aSeeks[i].offSeek;
3738 break;
3739 }
3740 }
3741
3742 for (unsigned iDoReadCheck = 0; iDoReadCheck < 2; iDoReadCheck++)
3743 {
3744 for (uint32_t i = 0; i < RT_ELEMENTS(aSeeks); i++)
3745 {
3746 offActual = UINT64_MAX;
3747 int rc = RTFileSeek(hFile1, aSeeks[i].offSeek, aSeeks[i].uMethod, &offActual);
3748 if (rc != aSeeks[i].rc)
3749 RTTestIFailed("Seek #%u: Expected %Rrc, got %Rrc", i, aSeeks[i].rc, rc);
3750 if (RT_SUCCESS(rc) && offActual != aSeeks[i].offActual)
3751 RTTestIFailed("Seek #%u: offActual %#RX64, expected %#RX64", i, offActual, aSeeks[i].offActual);
3752 if (RT_SUCCESS(rc))
3753 {
3754 uint64_t offTell = RTFileTell(hFile1);
3755 if (offTell != offActual)
3756 RTTestIFailed("Seek #%u: offActual %#RX64, RTFileTell %#RX64", i, offActual, offTell);
3757 }
3758
3759 if (RT_SUCCESS(rc) && offActual + _2K <= cbFile && iDoReadCheck)
3760 {
3761 uint8_t abBuf[_2K];
3762 RTTESTI_CHECK_RC(rc = RTFileRead(hFile1, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
3763 if (RT_SUCCESS(rc))
3764 {
3765 size_t offMarker = (size_t)(RT_ALIGN_64(offActual, _1K) - offActual);
3766 uint64_t uMarker = *(uint64_t *)&abBuf[offMarker]; /** @todo potentially unaligned access */
3767 if (uMarker != offActual + offMarker)
3768 RTTestIFailed("Seek #%u: Invalid marker value (@ %#RX64): %#RX64, expected %#RX64",
3769 i, offActual, uMarker, offActual + offMarker);
3770
3771 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -(int64_t)sizeof(abBuf), RTFILE_SEEK_CURRENT, NULL), VINF_SUCCESS);
3772 }
3773 }
3774 }
3775 }
3776
3777
3778 /*
3779 * Profile seeking relative to the beginning of the file and relative
3780 * to the end. The latter might be more expensive in a SF context.
3781 */
3782 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? iIteration : iIteration % cbFile, RTFILE_SEEK_BEGIN, NULL),
3783 g_nsTestRun, "RTFileSeek/BEGIN");
3784 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? -(int64_t)iIteration : -(int64_t)(iIteration % cbFile), RTFILE_SEEK_END, NULL),
3785 g_nsTestRun, "RTFileSeek/END");
3786
3787}
3788
3789#ifdef FSPERF_TEST_SENDFILE
3790
3791/**
3792 * Send file thread arguments.
3793 */
3794typedef struct FSPERFSENDFILEARGS
3795{
3796 uint64_t offFile;
3797 size_t cbSend;
3798 uint64_t cbSent;
3799 size_t cbBuf;
3800 uint8_t *pbBuf;
3801 uint8_t bFiller;
3802 bool fCheckBuf;
3803 RTSOCKET hSocket;
3804 uint64_t volatile tsThreadDone;
3805} FSPERFSENDFILEARGS;
3806
3807/** Thread receiving the bytes from a sendfile() call. */
3808static DECLCALLBACK(int) fsPerfSendFileThread(RTTHREAD hSelf, void *pvUser)
3809{
3810 FSPERFSENDFILEARGS *pArgs = (FSPERFSENDFILEARGS *)pvUser;
3811 int rc = VINF_SUCCESS;
3812
3813 if (pArgs->fCheckBuf)
3814 RTTestSetDefault(g_hTest, NULL);
3815
3816 uint64_t cbReceived = 0;
3817 while (cbReceived < pArgs->cbSent)
3818 {
3819 size_t const cbToRead = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbReceived);
3820 size_t cbActual = 0;
3821 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTTcpRead(pArgs->hSocket, pArgs->pbBuf, cbToRead, &cbActual), VINF_SUCCESS);
3822 RTTEST_CHECK_BREAK(g_hTest, cbActual != 0);
3823 RTTEST_CHECK(g_hTest, cbActual <= cbToRead);
3824 if (pArgs->fCheckBuf)
3825 fsPerfCheckReadBuf(__LINE__, pArgs->offFile + cbReceived, pArgs->pbBuf, cbActual, pArgs->bFiller);
3826 cbReceived += cbActual;
3827 }
3828
3829 pArgs->tsThreadDone = RTTimeNanoTS();
3830
3831 if (cbReceived == pArgs->cbSent && RT_SUCCESS(rc))
3832 {
3833 size_t cbActual = 0;
3834 rc = RTSocketReadNB(pArgs->hSocket, pArgs->pbBuf, 1, &cbActual);
3835 if (rc != VINF_SUCCESS && rc != VINF_TRY_AGAIN)
3836 RTTestFailed(g_hTest, "RTSocketReadNB(sendfile client socket) -> %Rrc; expected VINF_SUCCESS or VINF_TRY_AGAIN\n", rc);
3837 else if (cbActual != 0)
3838 RTTestFailed(g_hTest, "sendfile client socket still contains data when done!\n");
3839 }
3840
3841 RTTEST_CHECK_RC(g_hTest, RTSocketClose(pArgs->hSocket), VINF_SUCCESS);
3842 pArgs->hSocket = NIL_RTSOCKET;
3843
3844 RT_NOREF(hSelf);
3845 return rc;
3846}
3847
3848
3849static uint64_t fsPerfSendFileOne(FSPERFSENDFILEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
3850 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckBuf, unsigned iLine)
3851{
3852 /* Copy parameters to the argument structure: */
3853 pArgs->offFile = offFile;
3854 pArgs->cbSend = cbSend;
3855 pArgs->cbSent = cbSent;
3856 pArgs->bFiller = bFiller;
3857 pArgs->fCheckBuf = fCheckBuf;
3858
3859 /* Create a socket pair. */
3860 pArgs->hSocket = NIL_RTSOCKET;
3861 RTSOCKET hServer = NIL_RTSOCKET;
3862 RTTESTI_CHECK_RC_RET(RTTcpCreatePair(&hServer, &pArgs->hSocket, 0), VINF_SUCCESS, 0);
3863
3864 /* Create the receiving thread: */
3865 int rc;
3866 RTTHREAD hThread = NIL_RTTHREAD;
3867 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSendFileThread, pArgs, 0,
3868 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "sendfile"), VINF_SUCCESS);
3869 if (RT_SUCCESS(rc))
3870 {
3871 uint64_t const tsStart = RTTimeNanoTS();
3872
3873# if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
3874 /* SystemV sendfile: */
3875 loff_t offFileSf = pArgs->offFile;
3876 ssize_t cbActual = sendfile((int)RTSocketToNative(hServer), (int)RTFileToNative(hFile1), &offFileSf, pArgs->cbSend);
3877 int const iErr = errno;
3878 if (cbActual < 0)
3879 RTTestIFailed("%u: sendfile(socket, file, &%#X64, %#zx) failed (%zd): %d (%Rrc), offFileSf=%#RX64\n",
3880 iLine, pArgs->offFile, pArgs->cbSend, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileSf);
3881 else if ((uint64_t)cbActual != pArgs->cbSent)
3882 RTTestIFailed("%u: sendfile(socket, file, &%#RX64, %#zx): %#zx, expected %#RX64 (offFileSf=%#RX64)\n",
3883 iLine, pArgs->offFile, pArgs->cbSend, cbActual, pArgs->cbSent, (uint64_t)offFileSf);
3884 else if ((uint64_t)offFileSf != pArgs->offFile + pArgs->cbSent)
3885 RTTestIFailed("%u: sendfile(socket, file, &%#RX64, %#zx): %#zx; offFileSf=%#RX64, expected %#RX64\n",
3886 iLine, pArgs->offFile, pArgs->cbSend, cbActual, (uint64_t)offFileSf, pArgs->offFile + pArgs->cbSent);
3887#else
3888 /* BSD sendfile: */
3889# ifdef SF_SYNC
3890 int fSfFlags = SF_SYNC;
3891# else
3892 int fSfFlags = 0;
3893# endif
3894 off_t cbActual = pArgs->cbSend;
3895 rc = sendfile((int)RTFileToNative(hFile1), (int)RTSocketToNative(hServer),
3896# ifdef RT_OS_DARWIN
3897 pArgs->offFile, &cbActual, NULL, fSfFlags);
3898# else
3899 pArgs->offFile, cbActual, NULL, &cbActual, fSfFlags);
3900# endif
3901 int const iErr = errno;
3902 if (rc != 0)
3903 RTTestIFailed("%u: sendfile(file, socket, %#RX64, %#zx, NULL,, %#x) failed (%d): %d (%Rrc), cbActual=%#RX64\n",
3904 iLine, pArgs->offFile, (size_t)pArgs->cbSend, rc, iErr, RTErrConvertFromErrno(iErr), (uint64_t)cbActual);
3905 if ((uint64_t)cbActual != pArgs->cbSent)
3906 RTTestIFailed("%u: sendfile(file, socket, %#RX64, %#zx, NULL,, %#x): cbActual=%#RX64, expected %#RX64 (rc=%d, errno=%d)\n",
3907 iLine, pArgs->offFile, (size_t)pArgs->cbSend, (uint64_t)cbActual, pArgs->cbSent, rc, iErr);
3908# endif
3909 RTTESTI_CHECK_RC(RTSocketClose(hServer), VINF_SUCCESS);
3910 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
3911
3912 if (pArgs->tsThreadDone >= tsStart)
3913 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
3914 }
3915 return 0;
3916}
3917
3918
3919static void fsPerfSendFile(RTFILE hFile1, uint64_t cbFile)
3920{
3921 RTTestISub("sendfile");
3922# ifdef RT_OS_LINUX
3923 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - g_fPageOffset);
3924# else
3925 uint64_t const cbFileMax = RT_MIN(cbFile, SSIZE_MAX - g_fPageOffset);
3926# endif
3927 signal(SIGPIPE, SIG_IGN);
3928
3929 /*
3930 * Allocate a buffer.
3931 */
3932 FSPERFSENDFILEARGS Args;
3933 Args.cbBuf = RT_MIN(RT_MIN(cbFileMax, _16M), g_cbMaxBuffer);
3934 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
3935 while (!Args.pbBuf)
3936 {
3937 Args.cbBuf /= 8;
3938 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
3939 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
3940 }
3941
3942 /*
3943 * First iteration with default buffer content.
3944 */
3945 fsPerfSendFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
3946 if (cbFileMax == cbFile)
3947 fsPerfSendFileOne(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
3948 else
3949 fsPerfSendFileOne(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
3950
3951 /*
3952 * Write a block using the regular API and then send it, checking that
3953 * the any caching that sendfile does is correctly updated.
3954 */
3955 uint8_t bFiller = 0xf6;
3956 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
3957 do
3958 {
3959 fsPerfSendFileOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
3960
3961 bFiller += 1;
3962 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
3963 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
3964
3965 fsPerfSendFileOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
3966
3967 cbToSend /= 2;
3968 } while (cbToSend >= g_cbPage && ((unsigned)bFiller - 0xf7U) < 64);
3969
3970 /*
3971 * Restore buffer content
3972 */
3973 bFiller = 0xf6;
3974 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
3975 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
3976
3977 /*
3978 * Do 128 random sends.
3979 */
3980 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
3981 for (uint32_t iTest = 0; iTest < 128; iTest++)
3982 {
3983 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
3984 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
3985 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
3986
3987 fsPerfSendFileOne(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
3988 }
3989
3990 /*
3991 * Benchmark it.
3992 */
3993 uint32_t cIterations = 0;
3994 uint64_t nsElapsed = 0;
3995 for (;;)
3996 {
3997 uint64_t cNsThis = fsPerfSendFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
3998 nsElapsed += cNsThis;
3999 cIterations++;
4000 if (!cNsThis || nsElapsed >= g_nsTestRun)
4001 break;
4002 }
4003 uint64_t cbTotal = cbFileMax * cIterations;
4004 RTTestIValue("latency", nsElapsed / cIterations, RTTESTUNIT_NS_PER_CALL);
4005 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
4006 RTTestIValue("calls", cIterations, RTTESTUNIT_CALLS);
4007 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
4008 if (g_fShowDuration)
4009 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
4010
4011 /*
4012 * Cleanup.
4013 */
4014 RTMemFree(Args.pbBuf);
4015}
4016
4017#endif /* FSPERF_TEST_SENDFILE */
4018#ifdef RT_OS_LINUX
4019
4020#ifndef __NR_splice
4021# if defined(RT_ARCH_AMD64)
4022# define __NR_splice 275
4023# elif defined(RT_ARCH_X86)
4024# define __NR_splice 313
4025# else
4026# error "fix me"
4027# endif
4028#endif
4029
4030/** FsPerf is built against ancient glibc, so make the splice syscall ourselves. */
4031DECLINLINE(ssize_t) syscall_splice(int fdIn, loff_t *poffIn, int fdOut, loff_t *poffOut, size_t cbChunk, unsigned fFlags)
4032{
4033 return syscall(__NR_splice, fdIn, poffIn, fdOut, poffOut, cbChunk, fFlags);
4034}
4035
4036
4037/**
4038 * Send file thread arguments.
4039 */
4040typedef struct FSPERFSPLICEARGS
4041{
4042 uint64_t offFile;
4043 size_t cbSend;
4044 uint64_t cbSent;
4045 size_t cbBuf;
4046 uint8_t *pbBuf;
4047 uint8_t bFiller;
4048 bool fCheckBuf;
4049 uint32_t cCalls;
4050 RTPIPE hPipe;
4051 uint64_t volatile tsThreadDone;
4052} FSPERFSPLICEARGS;
4053
4054
4055/** Thread receiving the bytes from a splice() call. */
4056static DECLCALLBACK(int) fsPerfSpliceToPipeThread(RTTHREAD hSelf, void *pvUser)
4057{
4058 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
4059 int rc = VINF_SUCCESS;
4060
4061 if (pArgs->fCheckBuf)
4062 RTTestSetDefault(g_hTest, NULL);
4063
4064 uint64_t cbReceived = 0;
4065 while (cbReceived < pArgs->cbSent)
4066 {
4067 size_t const cbToRead = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbReceived);
4068 size_t cbActual = 0;
4069 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeReadBlocking(pArgs->hPipe, pArgs->pbBuf, cbToRead, &cbActual), VINF_SUCCESS);
4070 RTTEST_CHECK_BREAK(g_hTest, cbActual != 0);
4071 RTTEST_CHECK(g_hTest, cbActual <= cbToRead);
4072 if (pArgs->fCheckBuf)
4073 fsPerfCheckReadBuf(__LINE__, pArgs->offFile + cbReceived, pArgs->pbBuf, cbActual, pArgs->bFiller);
4074 cbReceived += cbActual;
4075 }
4076
4077 pArgs->tsThreadDone = RTTimeNanoTS();
4078
4079 if (cbReceived == pArgs->cbSent && RT_SUCCESS(rc))
4080 {
4081 size_t cbActual = 0;
4082 rc = RTPipeRead(pArgs->hPipe, pArgs->pbBuf, 1, &cbActual);
4083 if (rc != VINF_SUCCESS && rc != VINF_TRY_AGAIN && rc != VERR_BROKEN_PIPE)
4084 RTTestFailed(g_hTest, "RTPipeReadBlocking() -> %Rrc; expected VINF_SUCCESS or VINF_TRY_AGAIN\n", rc);
4085 else if (cbActual != 0)
4086 RTTestFailed(g_hTest, "splice read pipe still contains data when done!\n");
4087 }
4088
4089 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
4090 pArgs->hPipe = NIL_RTPIPE;
4091
4092 RT_NOREF(hSelf);
4093 return rc;
4094}
4095
4096
4097/** Sends hFile1 to a pipe via the Linux-specific splice() syscall. */
4098static uint64_t fsPerfSpliceToPipeOne(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
4099 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckBuf, unsigned iLine)
4100{
4101 /* Copy parameters to the argument structure: */
4102 pArgs->offFile = offFile;
4103 pArgs->cbSend = cbSend;
4104 pArgs->cbSent = cbSent;
4105 pArgs->bFiller = bFiller;
4106 pArgs->fCheckBuf = fCheckBuf;
4107
4108 /* Create a socket pair. */
4109 pArgs->hPipe = NIL_RTPIPE;
4110 RTPIPE hPipeW = NIL_RTPIPE;
4111 RTTESTI_CHECK_RC_RET(RTPipeCreate(&pArgs->hPipe, &hPipeW, 0 /*fFlags*/), VINF_SUCCESS, 0);
4112
4113 /* Create the receiving thread: */
4114 int rc;
4115 RTTHREAD hThread = NIL_RTTHREAD;
4116 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceToPipeThread, pArgs, 0,
4117 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
4118 if (RT_SUCCESS(rc))
4119 {
4120 uint64_t const tsStart = RTTimeNanoTS();
4121 size_t cbLeft = cbSend;
4122 size_t cbTotal = 0;
4123 do
4124 {
4125 loff_t offFileIn = offFile;
4126 ssize_t cbActual = syscall_splice((int)RTFileToNative(hFile1), &offFileIn, (int)RTPipeToNative(hPipeW), NULL,
4127 cbLeft, 0 /*fFlags*/);
4128 int const iErr = errno;
4129 if (RT_UNLIKELY(cbActual < 0))
4130 {
4131 if (iErr == EPIPE && cbTotal == pArgs->cbSent)
4132 break;
4133 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0) failed (%zd): %d (%Rrc), offFileIn=%#RX64\n",
4134 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileIn);
4135 break;
4136 }
4137 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
4138 if ((uint64_t)offFileIn != offFile + (uint64_t)cbActual)
4139 {
4140 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0): %#zx; offFileIn=%#RX64, expected %#RX64\n",
4141 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileIn, offFile + (uint64_t)cbActual);
4142 break;
4143 }
4144 if (cbActual > 0)
4145 {
4146 pArgs->cCalls++;
4147 offFile += (size_t)cbActual;
4148 cbTotal += (size_t)cbActual;
4149 cbLeft -= (size_t)cbActual;
4150 }
4151 else
4152 break;
4153 } while (cbLeft > 0);
4154
4155 if (cbTotal != pArgs->cbSent)
4156 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
4157
4158 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
4159 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
4160
4161 if (pArgs->tsThreadDone >= tsStart)
4162 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
4163 }
4164 return 0;
4165}
4166
4167
4168static void fsPerfSpliceToPipe(RTFILE hFile1, uint64_t cbFile)
4169{
4170 RTTestISub("splice/to-pipe");
4171
4172 /*
4173 * splice was introduced in 2.6.17 according to the man-page.
4174 */
4175 char szRelease[64];
4176 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
4177 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
4178 {
4179 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
4180 return;
4181 }
4182
4183 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - g_fPageOffset);
4184 signal(SIGPIPE, SIG_IGN);
4185
4186 /*
4187 * Allocate a buffer.
4188 */
4189 FSPERFSPLICEARGS Args;
4190 Args.cbBuf = RT_MIN(RT_MIN(cbFileMax, _16M), g_cbMaxBuffer);
4191 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4192 while (!Args.pbBuf)
4193 {
4194 Args.cbBuf /= 8;
4195 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
4196 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4197 }
4198
4199 /*
4200 * First iteration with default buffer content.
4201 */
4202 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
4203 if (cbFileMax == cbFile)
4204 fsPerfSpliceToPipeOne(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
4205 else
4206 fsPerfSpliceToPipeOne(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
4207
4208 /*
4209 * Write a block using the regular API and then send it, checking that
4210 * the any caching that sendfile does is correctly updated.
4211 */
4212 uint8_t bFiller = 0xf6;
4213 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
4214 do
4215 {
4216 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
4217
4218 bFiller += 1;
4219 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
4220 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
4221
4222 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
4223
4224 cbToSend /= 2;
4225 } while (cbToSend >= g_cbPage && ((unsigned)bFiller - 0xf7U) < 64);
4226
4227 /*
4228 * Restore buffer content
4229 */
4230 bFiller = 0xf6;
4231 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
4232 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
4233
4234 /*
4235 * Do 128 random sends.
4236 */
4237 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
4238 for (uint32_t iTest = 0; iTest < 128; iTest++)
4239 {
4240 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
4241 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
4242 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
4243
4244 fsPerfSpliceToPipeOne(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
4245 }
4246
4247 /*
4248 * Benchmark it.
4249 */
4250 Args.cCalls = 0;
4251 uint32_t cIterations = 0;
4252 uint64_t nsElapsed = 0;
4253 for (;;)
4254 {
4255 uint64_t cNsThis = fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
4256 nsElapsed += cNsThis;
4257 cIterations++;
4258 if (!cNsThis || nsElapsed >= g_nsTestRun)
4259 break;
4260 }
4261 uint64_t cbTotal = cbFileMax * cIterations;
4262 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
4263 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
4264 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
4265 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
4266 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
4267 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
4268 if (g_fShowDuration)
4269 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
4270
4271 /*
4272 * Cleanup.
4273 */
4274 RTMemFree(Args.pbBuf);
4275}
4276
4277
4278/** Thread sending the bytes to a splice() call. */
4279static DECLCALLBACK(int) fsPerfSpliceToFileThread(RTTHREAD hSelf, void *pvUser)
4280{
4281 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
4282 int rc = VINF_SUCCESS;
4283
4284 uint64_t offFile = pArgs->offFile;
4285 uint64_t cbTotalSent = 0;
4286 while (cbTotalSent < pArgs->cbSent)
4287 {
4288 size_t const cbToSend = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbTotalSent);
4289 fsPerfFillWriteBuf(offFile, pArgs->pbBuf, cbToSend, pArgs->bFiller);
4290 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeWriteBlocking(pArgs->hPipe, pArgs->pbBuf, cbToSend, NULL), VINF_SUCCESS);
4291 offFile += cbToSend;
4292 cbTotalSent += cbToSend;
4293 }
4294
4295 pArgs->tsThreadDone = RTTimeNanoTS();
4296
4297 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
4298 pArgs->hPipe = NIL_RTPIPE;
4299
4300 RT_NOREF(hSelf);
4301 return rc;
4302}
4303
4304
4305/** Fill hFile1 via a pipe and the Linux-specific splice() syscall. */
4306static uint64_t fsPerfSpliceToFileOne(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
4307 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckFile, unsigned iLine)
4308{
4309 /* Copy parameters to the argument structure: */
4310 pArgs->offFile = offFile;
4311 pArgs->cbSend = cbSend;
4312 pArgs->cbSent = cbSent;
4313 pArgs->bFiller = bFiller;
4314 pArgs->fCheckBuf = false;
4315
4316 /* Create a socket pair. */
4317 pArgs->hPipe = NIL_RTPIPE;
4318 RTPIPE hPipeR = NIL_RTPIPE;
4319 RTTESTI_CHECK_RC_RET(RTPipeCreate(&hPipeR, &pArgs->hPipe, 0 /*fFlags*/), VINF_SUCCESS, 0);
4320
4321 /* Create the receiving thread: */
4322 int rc;
4323 RTTHREAD hThread = NIL_RTTHREAD;
4324 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceToFileThread, pArgs, 0,
4325 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
4326 if (RT_SUCCESS(rc))
4327 {
4328 /*
4329 * Do the splicing.
4330 */
4331 uint64_t const tsStart = RTTimeNanoTS();
4332 size_t cbLeft = cbSend;
4333 size_t cbTotal = 0;
4334 do
4335 {
4336 loff_t offFileOut = offFile;
4337 ssize_t cbActual = syscall_splice((int)RTPipeToNative(hPipeR), NULL, (int)RTFileToNative(hFile1), &offFileOut,
4338 cbLeft, 0 /*fFlags*/);
4339 int const iErr = errno;
4340 if (RT_UNLIKELY(cbActual < 0))
4341 {
4342 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0) failed (%zd): %d (%Rrc), offFileOut=%#RX64\n",
4343 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileOut);
4344 break;
4345 }
4346 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
4347 if ((uint64_t)offFileOut != offFile + (uint64_t)cbActual)
4348 {
4349 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0): %#zx; offFileOut=%#RX64, expected %#RX64\n",
4350 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileOut, offFile + (uint64_t)cbActual);
4351 break;
4352 }
4353 if (cbActual > 0)
4354 {
4355 pArgs->cCalls++;
4356 offFile += (size_t)cbActual;
4357 cbTotal += (size_t)cbActual;
4358 cbLeft -= (size_t)cbActual;
4359 }
4360 else
4361 break;
4362 } while (cbLeft > 0);
4363 uint64_t const nsElapsed = RTTimeNanoTS() - tsStart;
4364
4365 if (cbTotal != pArgs->cbSent)
4366 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
4367
4368 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
4369 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
4370
4371 /* Check the file content. */
4372 if (fCheckFile && cbTotal == pArgs->cbSent)
4373 {
4374 offFile = pArgs->offFile;
4375 cbLeft = cbSent;
4376 while (cbLeft > 0)
4377 {
4378 size_t cbToRead = RT_MIN(cbLeft, pArgs->cbBuf);
4379 RTTESTI_CHECK_RC_BREAK(RTFileReadAt(hFile1, offFile, pArgs->pbBuf, cbToRead, NULL), VINF_SUCCESS);
4380 if (!fsPerfCheckReadBuf(iLine, offFile, pArgs->pbBuf, cbToRead, pArgs->bFiller))
4381 break;
4382 offFile += cbToRead;
4383 cbLeft -= cbToRead;
4384 }
4385 }
4386 return nsElapsed;
4387 }
4388 return 0;
4389}
4390
4391
4392static void fsPerfSpliceToFile(RTFILE hFile1, uint64_t cbFile)
4393{
4394 RTTestISub("splice/to-file");
4395
4396 /*
4397 * splice was introduced in 2.6.17 according to the man-page.
4398 */
4399 char szRelease[64];
4400 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
4401 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
4402 {
4403 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
4404 return;
4405 }
4406
4407 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - g_fPageOffset);
4408 signal(SIGPIPE, SIG_IGN);
4409
4410 /*
4411 * Allocate a buffer.
4412 */
4413 FSPERFSPLICEARGS Args;
4414 Args.cbBuf = RT_MIN(RT_MIN(cbFileMax, _16M), g_cbMaxBuffer);
4415 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4416 while (!Args.pbBuf)
4417 {
4418 Args.cbBuf /= 8;
4419 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
4420 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
4421 }
4422
4423 /*
4424 * Do the whole file.
4425 */
4426 uint8_t bFiller = 0x76;
4427 fsPerfSpliceToFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, bFiller, true /*fCheckFile*/, __LINE__);
4428
4429 /*
4430 * Do 64 random chunks (this is slower).
4431 */
4432 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
4433 for (uint32_t iTest = 0; iTest < 64; iTest++)
4434 {
4435 size_t const cbToWrite = (size_t)RTRandU64Ex(1, iTest < 24 ? cbSmall : cbFileMax);
4436 uint64_t const offToWriteAt = RTRandU64Ex(0, cbFile - cbToWrite);
4437 uint64_t const cbTryRead = cbToWrite + (iTest & 1 ? RTRandU32Ex(0, _64K) : 0);
4438
4439 bFiller++;
4440 fsPerfSpliceToFileOne(&Args, hFile1, offToWriteAt, cbTryRead, cbToWrite, bFiller, true /*fCheckFile*/, __LINE__);
4441 }
4442
4443 /*
4444 * Benchmark it.
4445 */
4446 Args.cCalls = 0;
4447 uint32_t cIterations = 0;
4448 uint64_t nsElapsed = 0;
4449 for (;;)
4450 {
4451 uint64_t cNsThis = fsPerfSpliceToFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
4452 nsElapsed += cNsThis;
4453 cIterations++;
4454 if (!cNsThis || nsElapsed >= g_nsTestRun)
4455 break;
4456 }
4457 uint64_t cbTotal = cbFileMax * cIterations;
4458 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
4459 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
4460 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
4461 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
4462 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
4463 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
4464 if (g_fShowDuration)
4465 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
4466
4467 /*
4468 * Cleanup.
4469 */
4470 RTMemFree(Args.pbBuf);
4471}
4472
4473#endif /* RT_OS_LINUX */
4474
4475/** For fsPerfIoRead and fsPerfIoWrite. */
4476#define PROFILE_IO_FN(a_szOperation, a_fnCall) \
4477 do \
4478 { \
4479 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); \
4480 uint64_t offActual = 0; \
4481 uint32_t cSeeks = 0; \
4482 \
4483 /* Estimate how many iterations we need to fill up the given timeslot: */ \
4484 fsPerfYield(); \
4485 uint64_t nsStart = RTTimeNanoTS(); \
4486 uint64_t ns; \
4487 do \
4488 ns = RTTimeNanoTS(); \
4489 while (ns == nsStart); \
4490 nsStart = ns; \
4491 \
4492 uint64_t iIteration = 0; \
4493 do \
4494 { \
4495 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4496 iIteration++; \
4497 ns = RTTimeNanoTS() - nsStart; \
4498 } while (ns < RT_NS_10MS); \
4499 ns /= iIteration; \
4500 if (ns > g_nsPerNanoTSCall + 32) \
4501 ns -= g_nsPerNanoTSCall; \
4502 uint64_t cIterations = g_nsTestRun / ns; \
4503 if (cIterations < 2) \
4504 cIterations = 2; \
4505 else if (cIterations & 1) \
4506 cIterations++; \
4507 \
4508 /* Do the actual profiling: */ \
4509 cSeeks = 0; \
4510 iIteration = 0; \
4511 fsPerfYield(); \
4512 nsStart = RTTimeNanoTS(); \
4513 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
4514 { \
4515 for (; iIteration < cIterations; iIteration++)\
4516 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4517 ns = RTTimeNanoTS() - nsStart;\
4518 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
4519 break; \
4520 cIterations += cIterations / 4; \
4521 if (cIterations & 1) \
4522 cIterations++; \
4523 nsStart += g_nsPerNanoTSCall; \
4524 } \
4525 RTTestIValueF(ns / iIteration, \
4526 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation "/seq/%RU32 latency", cbBlock); \
4527 RTTestIValueF((uint64_t)((double)(iIteration * cbBlock) / ((double)ns / RT_NS_1SEC)), \
4528 RTTESTUNIT_BYTES_PER_SEC, a_szOperation "/seq/%RU32 throughput", cbBlock); \
4529 RTTestIValueF(iIteration, \
4530 RTTESTUNIT_CALLS, a_szOperation "/seq/%RU32 calls", cbBlock); \
4531 RTTestIValueF((uint64_t)iIteration * cbBlock, \
4532 RTTESTUNIT_BYTES, a_szOperation "/seq/%RU32 bytes", cbBlock); \
4533 RTTestIValueF(cSeeks, \
4534 RTTESTUNIT_OCCURRENCES, a_szOperation "/seq/%RU32 seeks", cbBlock); \
4535 if (g_fShowDuration) \
4536 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation "/seq/%RU32 duration", cbBlock); \
4537 } while (0)
4538
4539
4540/**
4541 * One RTFileRead profiling iteration.
4542 */
4543DECL_FORCE_INLINE(int) fsPerfIoReadWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
4544 uint64_t *poffActual, uint32_t *pcSeeks)
4545{
4546 /* Do we need to seek back to the start? */
4547 if (*poffActual + cbBlock <= cbFile)
4548 { /* likely */ }
4549 else
4550 {
4551 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
4552 *pcSeeks += 1;
4553 *poffActual = 0;
4554 }
4555
4556 size_t cbActuallyRead = 0;
4557 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBlock, cbBlock, &cbActuallyRead), VINF_SUCCESS, rcCheck);
4558 if (cbActuallyRead == cbBlock)
4559 {
4560 *poffActual += cbActuallyRead;
4561 return VINF_SUCCESS;
4562 }
4563 RTTestIFailed("RTFileRead at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyRead, cbBlock);
4564 *poffActual += cbActuallyRead;
4565 return VERR_READ_ERROR;
4566}
4567
4568
4569static void fsPerfIoReadBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
4570{
4571 RTTestISubF("IO - Sequential read %RU32", cbBlock);
4572 if (cbBlock <= cbFile)
4573 {
4574
4575 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
4576 if (pbBuf)
4577 {
4578 memset(pbBuf, 0xf7, cbBlock);
4579 PROFILE_IO_FN("RTFileRead", fsPerfIoReadWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
4580 RTMemPageFree(pbBuf, cbBlock);
4581 }
4582 else
4583 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
4584 }
4585 else
4586 RTTestSkipped(g_hTest, "test file too small");
4587}
4588
4589
4590/** preadv is too new to be useful, so we use the readv api via this wrapper. */
4591DECLINLINE(int) myFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
4592{
4593 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
4594 if (RT_SUCCESS(rc))
4595 rc = RTFileSgRead(hFile, pSgBuf, cbToRead, pcbRead);
4596 return rc;
4597}
4598
4599
4600static void fsPerfRead(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
4601{
4602 RTTestISubF("IO - RTFileRead");
4603
4604 /*
4605 * Allocate a big buffer we can play around with. Min size is 1MB.
4606 */
4607 size_t cbMaxBuf = RT_MIN(_64M, g_cbMaxBuffer);
4608 size_t cbBuf = cbFile < cbMaxBuf ? (size_t)cbFile : cbMaxBuf;
4609 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
4610 while (!pbBuf)
4611 {
4612 cbBuf /= 2;
4613 RTTESTI_CHECK_RETV(cbBuf >= _1M);
4614 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
4615 }
4616
4617#if 1
4618 /*
4619 * Start at the beginning and read the full buffer in random small chunks, thereby
4620 * checking that unaligned buffer addresses, size and file offsets work fine.
4621 */
4622 struct
4623 {
4624 uint64_t offFile;
4625 uint32_t cbMax;
4626 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
4627 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++)
4628 {
4629 memset(pbBuf, 0x55, cbBuf);
4630 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4631 for (size_t offBuf = 0; offBuf < cbBuf; )
4632 {
4633 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
4634 uint32_t const cbToRead = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
4635 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
4636 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
4637 size_t cbActual = 0;
4638 RTTESTI_CHECK_RC(RTFileRead(hFile1, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
4639 if (cbActual == cbToRead)
4640 {
4641 offBuf += cbActual;
4642 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
4643 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
4644 }
4645 else
4646 {
4647 RTTestIFailed("Attempting to read %#x bytes at %#zx, only got %#x bytes back! (cbLeft=%#x cbBuf=%#zx)\n",
4648 cbToRead, offBuf, cbActual, cbLeft, cbBuf);
4649 if (cbActual)
4650 offBuf += cbActual;
4651 else
4652 pbBuf[offBuf++] = 0x11;
4653 }
4654 }
4655 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf);
4656 }
4657
4658 /*
4659 * Test reading beyond the end of the file.
4660 */
4661 size_t const acbMax[] = { cbBuf, _64K, _16K, _4K, 256 };
4662 uint32_t const aoffFromEos[] =
4663 { 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,
4664 4092, 4093, 4094, 4095, 4096, 4097, 4098, 4099, 4100, 8192, 16384, 32767, 32768, 32769, 65535, 65536, _1M - 1
4665 };
4666 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
4667 {
4668 size_t const cbMaxRead = acbMax[iMax];
4669 for (uint32_t iOffFromEos = 0; iOffFromEos < RT_ELEMENTS(aoffFromEos); iOffFromEos++)
4670 {
4671 uint32_t off = aoffFromEos[iOffFromEos];
4672 if (off >= cbMaxRead)
4673 continue;
4674 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4675 size_t cbActual = ~(size_t)0;
4676 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
4677 RTTESTI_CHECK(cbActual == off);
4678
4679 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4680 cbActual = ~(size_t)0;
4681 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, off, &cbActual), VINF_SUCCESS);
4682 RTTESTI_CHECK_MSG(cbActual == off, ("%#zx vs %#zx\n", cbActual, off));
4683
4684 cbActual = ~(size_t)0;
4685 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, 1, &cbActual), VINF_SUCCESS);
4686 RTTESTI_CHECK_MSG(cbActual == 0, ("cbActual=%zu\n", cbActual));
4687
4688 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
4689
4690 /* Repeat using native APIs in case IPRT or other layers hide status codes: */
4691#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
4692 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4693# ifdef RT_OS_OS2
4694 ULONG cbActual2 = ~(ULONG)0;
4695 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
4696 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
4697 RTTESTI_CHECK_MSG(cbActual2 == off, ("%#x vs %#x\n", cbActual2, off));
4698# else
4699 IO_STATUS_BLOCK const IosVirgin = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4700 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4701 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4702 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
4703 if (off == 0)
4704 {
4705 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
4706 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/ || Ios.Status == STATUS_END_OF_FILE /*fastio?*/,
4707 ("%#x vs %x/%#x; off=%#x\n", Ios.Status, IosVirgin.Status, STATUS_END_OF_FILE, off));
4708 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/ || Ios.Information == 0 /*fastio?*/,
4709 ("%#zx vs %zx/0; off=%#x\n", Ios.Information, IosVirgin.Information, off));
4710 }
4711 else
4712 {
4713 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x, expected 0 (off=%#x cbMaxRead=%#zx)\n", rcNt, off, cbMaxRead));
4714 RTTESTI_CHECK_MSG(Ios.Status == STATUS_SUCCESS, ("%#x; off=%#x\n", Ios.Status, off));
4715 RTTESTI_CHECK_MSG(Ios.Information == off, ("%#zx vs %#x\n", Ios.Information, off));
4716 }
4717# endif
4718
4719# ifdef RT_OS_OS2
4720 cbActual2 = ~(ULONG)0;
4721 orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, 1, &cbActual2);
4722 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
4723 RTTESTI_CHECK_MSG(cbActual2 == 0, ("cbActual2=%u\n", cbActual2));
4724# else
4725 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
4726 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4727 &Ios, pbBuf, 1, NULL /*poffFile*/, NULL /*Key*/);
4728 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
4729# endif
4730
4731#endif
4732 }
4733 }
4734
4735 /*
4736 * Test reading beyond end of the file.
4737 */
4738 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
4739 {
4740 size_t const cbMaxRead = acbMax[iMax];
4741 for (uint32_t off = 0; off < 256; off++)
4742 {
4743 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4744 size_t cbActual = ~(size_t)0;
4745 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
4746 RTTESTI_CHECK(cbActual == 0);
4747
4748 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
4749
4750 /* Repeat using native APIs in case IPRT or other layers hid status codes: */
4751#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
4752 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4753# ifdef RT_OS_OS2
4754 ULONG cbActual2 = ~(ULONG)0;
4755 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
4756 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
4757 RTTESTI_CHECK_MSG(cbActual2 == 0, ("%#x vs %#x\n", cbActual2, off));
4758# else
4759 IO_STATUS_BLOCK const IosVirgin = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4760 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4761 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4762 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
4763 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
4764 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/ || Ios.Status == STATUS_END_OF_FILE /*fastio?*/,
4765 ("%#x vs %x/%#x; off=%#x\n", Ios.Status, IosVirgin.Status, STATUS_END_OF_FILE, off));
4766 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/ || Ios.Information == 0 /*fastio?*/,
4767 ("%#zx vs %zx/0; off=%#x\n", Ios.Information, IosVirgin.Information, off));
4768
4769 /* Need to work with sector size on uncached, but might be worth it for non-fastio path. */
4770 uint32_t cbSector = 0x1000;
4771 uint32_t off2 = off * cbSector + (cbFile & (cbSector - 1) ? cbSector - (cbFile & (cbSector - 1)) : 0);
4772 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, cbFile + off2, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4773 size_t const cbMaxRead2 = RT_ALIGN_Z(cbMaxRead, cbSector);
4774 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
4775 rcNt = NtReadFile((HANDLE)RTFileToNative(hFileNoCache), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
4776 &Ios, pbBuf, (ULONG)cbMaxRead2, NULL /*poffFile*/, NULL /*Key*/);
4777 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE,
4778 ("rcNt=%#x, expected %#x; off2=%x cbMaxRead2=%#x\n", rcNt, STATUS_END_OF_FILE, off2, cbMaxRead2));
4779 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/,
4780 ("%#x vs %x; off2=%#x cbMaxRead2=%#x\n", Ios.Status, IosVirgin.Status, off2, cbMaxRead2));
4781 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/,
4782 ("%#zx vs %zx; off2=%#x cbMaxRead2=%#x\n", Ios.Information, IosVirgin.Information, off2, cbMaxRead2));
4783# endif
4784#endif
4785 }
4786 }
4787
4788 /*
4789 * Do uncached access, must be page aligned.
4790 */
4791 memset(pbBuf, 0x66, cbBuf);
4792 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
4793 {
4794 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4795 for (size_t offBuf = 0; offBuf < cbBuf; )
4796 {
4797 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / g_cbPage);
4798 uint32_t const cPagesToRead = RTRandU32Ex(1, cPagesLeft);
4799 size_t const cbToRead = cPagesToRead * (size_t)g_cbPage;
4800 size_t cbActual = 0;
4801 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
4802 if (cbActual == cbToRead)
4803 offBuf += cbActual;
4804 else
4805 {
4806 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x bytes back!\n", cbToRead, offBuf, cbActual);
4807 if (cbActual)
4808 offBuf += cbActual;
4809 else
4810 {
4811 memset(&pbBuf[offBuf], 0x11, g_cbPage);
4812 offBuf += g_cbPage;
4813 }
4814 }
4815 }
4816 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf);
4817 }
4818
4819 /*
4820 * Check reading zero bytes at the end of the file.
4821 * Requires native call because RTFileWrite doesn't call kernel on zero byte reads.
4822 */
4823 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
4824# ifdef RT_OS_WINDOWS
4825 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4826 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
4827 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
4828 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
4829 RTTESTI_CHECK(Ios.Information == 0);
4830
4831 IO_STATUS_BLOCK const IosVirgin = RTNT_IO_STATUS_BLOCK_INITIALIZER;
4832 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
4833 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 1, NULL, NULL);
4834 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x", rcNt));
4835 RTTESTI_CHECK_MSG(Ios.Status == IosVirgin.Status /*slow?*/ || Ios.Status == STATUS_END_OF_FILE /*fastio?*/,
4836 ("%#x vs %x/%#x\n", Ios.Status, IosVirgin.Status, STATUS_END_OF_FILE));
4837 RTTESTI_CHECK_MSG(Ios.Information == IosVirgin.Information /*slow*/ || Ios.Information == 0 /*fastio?*/,
4838 ("%#zx vs %zx/0\n", Ios.Information, IosVirgin.Information));
4839# else
4840 ssize_t cbRead = read((int)RTFileToNative(hFile1), pbBuf, 0);
4841 RTTESTI_CHECK(cbRead == 0);
4842# endif
4843
4844#else
4845 RT_NOREF(hFileNoCache);
4846#endif
4847
4848 /*
4849 * Scatter read function operation.
4850 */
4851#ifdef RT_OS_WINDOWS
4852 /** @todo RTFileSgReadAt is just a RTFileReadAt loop for windows NT. Need
4853 * to use ReadFileScatter (nocache + page aligned). */
4854#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
4855
4856# ifdef UIO_MAXIOV
4857 RTSGSEG aSegs[UIO_MAXIOV];
4858# else
4859 RTSGSEG aSegs[512];
4860# endif
4861 RTSGBUF SgBuf;
4862 uint32_t cIncr = 1;
4863 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr)
4864 {
4865 size_t const cbSeg = cbBuf / cSegs;
4866 size_t const cbToRead = cbSeg * cSegs;
4867 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4868 {
4869 aSegs[iSeg].cbSeg = cbSeg;
4870 aSegs[iSeg].pvSeg = &pbBuf[cbToRead - (iSeg + 1) * cbSeg];
4871 }
4872 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4873 int rc = myFileSgReadAt(hFile1, 0, &SgBuf, cbToRead, NULL);
4874 if (RT_SUCCESS(rc))
4875 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4876 {
4877 if (!fsPerfCheckReadBuf(__LINE__, iSeg * cbSeg, &pbBuf[cbToRead - (iSeg + 1) * cbSeg], cbSeg))
4878 {
4879 cSegs = RT_ELEMENTS(aSegs);
4880 break;
4881 }
4882 }
4883 else
4884 {
4885 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToRead=%#zx", rc, cSegs, cbSeg, cbToRead);
4886 break;
4887 }
4888 if (cSegs == 16)
4889 cIncr = 7;
4890 else if (cSegs == 16 * 7 + 16 /*= 128*/)
4891 cIncr = 64;
4892 }
4893
4894 for (uint32_t iTest = 0; iTest < 128; iTest++)
4895 {
4896 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
4897 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
4898 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
4899 size_t cbToRead = 0;
4900 size_t cbLeft = cbBuf;
4901 uint8_t *pbCur = &pbBuf[cbBuf];
4902 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4903 {
4904 uint32_t iAlign = RTRandU32Ex(0, 3);
4905 if (iAlign & 2) /* end is page aligned */
4906 {
4907 cbLeft -= (uintptr_t)pbCur & g_fPageOffset;
4908 pbCur -= (uintptr_t)pbCur & g_fPageOffset;
4909 }
4910
4911 size_t cbSegOthers = (cSegs - iSeg) * _8K;
4912 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
4913 : cbLeft > cSegs ? cbLeft - cSegs
4914 : cbLeft;
4915 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
4916 if (iAlign & 1) /* start is page aligned */
4917 cbSeg += ((uintptr_t)pbCur - cbSeg) & g_fPageOffset;
4918
4919 if (iSeg - iZeroSeg < cZeroSegs)
4920 cbSeg = 0;
4921
4922 cbToRead += cbSeg;
4923 cbLeft -= cbSeg;
4924 pbCur -= cbSeg;
4925 aSegs[iSeg].cbSeg = cbSeg;
4926 aSegs[iSeg].pvSeg = pbCur;
4927 }
4928
4929 uint64_t offFile = cbToRead < cbFile ? RTRandU64Ex(0, cbFile - cbToRead) : 0;
4930 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4931 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
4932 if (RT_SUCCESS(rc))
4933 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4934 {
4935 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg))
4936 {
4937 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbToRead=%#zx\n", iSeg, cSegs, aSegs[iSeg].cbSeg, cbToRead);
4938 iTest = _16K;
4939 break;
4940 }
4941 offFile += aSegs[iSeg].cbSeg;
4942 }
4943 else
4944 {
4945 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx", rc, cSegs, cbToRead);
4946 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4947 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
4948 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
4949 break;
4950 }
4951 }
4952
4953 /* reading beyond the end of the file */
4954 for (uint32_t cSegs = 1; cSegs < 6; cSegs++)
4955 for (uint32_t iTest = 0; iTest < 128; iTest++)
4956 {
4957 uint32_t const cbToRead = RTRandU32Ex(0, cbBuf);
4958 uint32_t const cbBeyond = cbToRead ? RTRandU32Ex(0, cbToRead) : 0;
4959 uint32_t const cbSeg = cbToRead / cSegs;
4960 uint32_t cbLeft = cbToRead;
4961 uint8_t *pbCur = &pbBuf[cbToRead];
4962 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4963 {
4964 aSegs[iSeg].cbSeg = iSeg + 1 < cSegs ? cbSeg : cbLeft;
4965 aSegs[iSeg].pvSeg = pbCur -= aSegs[iSeg].cbSeg;
4966 cbLeft -= aSegs[iSeg].cbSeg;
4967 }
4968 Assert(pbCur == pbBuf);
4969
4970 uint64_t offFile = cbFile + cbBeyond - cbToRead;
4971 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4972 int rcExpect = cbBeyond == 0 || cbToRead == 0 ? VINF_SUCCESS : VERR_EOF;
4973 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
4974 if (rc != rcExpect)
4975 {
4976 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx\n", rc, cSegs, cbToRead, cbBeyond);
4977 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4978 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
4979 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
4980 }
4981
4982 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
4983 size_t cbActual = 0;
4984 rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, &cbActual);
4985 if (rc != VINF_SUCCESS || cbActual != cbToRead - cbBeyond)
4986 RTTestIFailed("myFileSgReadAt failed: %Rrc cbActual=%#zu - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx expected %#zx\n",
4987 rc, cbActual, cSegs, cbToRead, cbBeyond, cbToRead - cbBeyond);
4988 if (RT_SUCCESS(rc) && cbActual > 0)
4989 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
4990 {
4991 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, RT_MIN(cbActual, aSegs[iSeg].cbSeg)))
4992 {
4993 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbActual%#zx cbToRead=%#zx cbBeyond=%#zx\n",
4994 iSeg, cSegs, aSegs[iSeg].cbSeg, cbActual, cbToRead, cbBeyond);
4995 iTest = _16K;
4996 break;
4997 }
4998 if (cbActual <= aSegs[iSeg].cbSeg)
4999 break;
5000 cbActual -= aSegs[iSeg].cbSeg;
5001 offFile += aSegs[iSeg].cbSeg;
5002 }
5003 }
5004
5005#endif
5006
5007 /*
5008 * Other OS specific stuff.
5009 */
5010#ifdef RT_OS_WINDOWS
5011 /* Check that reading at an offset modifies the position: */
5012 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
5013 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
5014
5015 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
5016 LARGE_INTEGER offNt;
5017 offNt.QuadPart = cbFile / 2;
5018 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
5019 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
5020 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
5021 RTTESTI_CHECK(Ios.Information == _4K);
5022 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
5023 fsPerfCheckReadBuf(__LINE__, cbFile / 2, pbBuf, _4K);
5024#endif
5025
5026
5027 RTMemPageFree(pbBuf, cbBuf);
5028}
5029
5030
5031/**
5032 * One RTFileWrite profiling iteration.
5033 */
5034DECL_FORCE_INLINE(int) fsPerfIoWriteWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
5035 uint64_t *poffActual, uint32_t *pcSeeks)
5036{
5037 /* Do we need to seek back to the start? */
5038 if (*poffActual + cbBlock <= cbFile)
5039 { /* likely */ }
5040 else
5041 {
5042 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
5043 *pcSeeks += 1;
5044 *poffActual = 0;
5045 }
5046
5047 size_t cbActuallyWritten = 0;
5048 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBlock, cbBlock, &cbActuallyWritten), VINF_SUCCESS, rcCheck);
5049 if (cbActuallyWritten == cbBlock)
5050 {
5051 *poffActual += cbActuallyWritten;
5052 return VINF_SUCCESS;
5053 }
5054 RTTestIFailed("RTFileWrite at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyWritten, cbBlock);
5055 *poffActual += cbActuallyWritten;
5056 return VERR_WRITE_ERROR;
5057}
5058
5059
5060static void fsPerfIoWriteBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
5061{
5062 RTTestISubF("IO - Sequential write %RU32", cbBlock);
5063
5064 if (cbBlock <= cbFile)
5065 {
5066 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
5067 if (pbBuf)
5068 {
5069 memset(pbBuf, 0xf7, cbBlock);
5070 PROFILE_IO_FN("RTFileWrite", fsPerfIoWriteWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
5071 RTMemPageFree(pbBuf, cbBlock);
5072 }
5073 else
5074 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
5075 }
5076 else
5077 RTTestSkipped(g_hTest, "test file too small");
5078}
5079
5080
5081/** pwritev is too new to be useful, so we use the writev api via this wrapper. */
5082DECLINLINE(int) myFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
5083{
5084 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
5085 if (RT_SUCCESS(rc))
5086 rc = RTFileSgWrite(hFile, pSgBuf, cbToWrite, pcbWritten);
5087 return rc;
5088}
5089
5090
5091static void fsPerfWrite(RTFILE hFile1, RTFILE hFileNoCache, RTFILE hFileWriteThru, uint64_t cbFile)
5092{
5093 RTTestISubF("IO - RTFileWrite");
5094
5095 /*
5096 * Allocate a big buffer we can play around with. Min size is 1MB.
5097 */
5098 size_t cbMaxBuf = RT_MIN(_64M, g_cbMaxBuffer);
5099 size_t cbBuf = cbFile < cbMaxBuf ? (size_t)cbFile : cbMaxBuf;
5100 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5101 while (!pbBuf)
5102 {
5103 cbBuf /= 2;
5104 RTTESTI_CHECK_RETV(cbBuf >= _1M);
5105 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
5106 }
5107
5108 uint8_t bFiller = 0x88;
5109
5110#if 1
5111 /*
5112 * Start at the beginning and write out the full buffer in random small chunks, thereby
5113 * checking that unaligned buffer addresses, size and file offsets work fine.
5114 */
5115 struct
5116 {
5117 uint64_t offFile;
5118 uint32_t cbMax;
5119 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
5120 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++, bFiller++)
5121 {
5122 fsPerfFillWriteBuf(aRuns[i].offFile, pbBuf, cbBuf, bFiller);
5123 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
5124
5125 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5126 for (size_t offBuf = 0; offBuf < cbBuf; )
5127 {
5128 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
5129 uint32_t const cbToWrite = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
5130 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
5131 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
5132 size_t cbActual = 0;
5133 RTTESTI_CHECK_RC(RTFileWrite(hFile1, &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
5134 if (cbActual == cbToWrite)
5135 {
5136 offBuf += cbActual;
5137 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
5138 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
5139 }
5140 else
5141 {
5142 RTTestIFailed("Attempting to write %#x bytes at %#zx (%#x left), only got %#x written!\n",
5143 cbToWrite, offBuf, cbLeft, cbActual);
5144 if (cbActual)
5145 offBuf += cbActual;
5146 else
5147 pbBuf[offBuf++] = 0x11;
5148 }
5149 }
5150
5151 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, aRuns[i].offFile, pbBuf, cbBuf, NULL), VINF_SUCCESS);
5152 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
5153 }
5154
5155
5156 /*
5157 * Do uncached and write-thru accesses, must be page aligned.
5158 */
5159 RTFILE ahFiles[2] = { hFileWriteThru, hFileNoCache };
5160 for (unsigned iFile = 0; iFile < RT_ELEMENTS(ahFiles); iFile++, bFiller++)
5161 {
5162 if (g_fIgnoreNoCache && ahFiles[iFile] == NIL_RTFILE)
5163 continue;
5164
5165 fsPerfFillWriteBuf(0, pbBuf, cbBuf, bFiller);
5166 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
5167 RTTESTI_CHECK_RC(RTFileSeek(ahFiles[iFile], 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5168
5169 for (size_t offBuf = 0; offBuf < cbBuf; )
5170 {
5171 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / g_cbPage);
5172 uint32_t const cPagesToWrite = RTRandU32Ex(1, cPagesLeft);
5173 size_t const cbToWrite = cPagesToWrite * (size_t)g_cbPage;
5174 size_t cbActual = 0;
5175 RTTESTI_CHECK_RC(RTFileWrite(ahFiles[iFile], &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
5176 if (cbActual == cbToWrite)
5177 {
5178 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offBuf, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5179 fsPerfCheckReadBuf(__LINE__, offBuf, pbBuf, cbToWrite, bFiller);
5180 offBuf += cbActual;
5181 }
5182 else
5183 {
5184 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x written!\n", cbToWrite, offBuf, cbActual);
5185 if (cbActual)
5186 offBuf += cbActual;
5187 else
5188 {
5189 memset(&pbBuf[offBuf], 0x11, g_cbPage);
5190 offBuf += g_cbPage;
5191 }
5192 }
5193 }
5194
5195 RTTESTI_CHECK_RC(RTFileReadAt(ahFiles[iFile], 0, pbBuf, cbBuf, NULL), VINF_SUCCESS);
5196 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
5197 }
5198
5199 /*
5200 * Check the behavior of writing zero bytes to the file _4K from the end
5201 * using native API. In the olden days zero sized write have been known
5202 * to be used to truncate a file.
5203 */
5204 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -_4K, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
5205# ifdef RT_OS_WINDOWS
5206 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
5207 NTSTATUS rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
5208 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
5209 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
5210 RTTESTI_CHECK(Ios.Information == 0);
5211# else
5212 ssize_t cbWritten = write((int)RTFileToNative(hFile1), pbBuf, 0);
5213 RTTESTI_CHECK(cbWritten == 0);
5214# endif
5215 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, _4K, NULL), VINF_SUCCESS);
5216 fsPerfCheckReadBuf(__LINE__, cbFile - _4K, pbBuf, _4K, pbBuf[0x8]);
5217
5218#else
5219 RT_NOREF(hFileNoCache, hFileWriteThru);
5220#endif
5221
5222 /*
5223 * Gather write function operation.
5224 */
5225#ifdef RT_OS_WINDOWS
5226 /** @todo RTFileSgWriteAt is just a RTFileWriteAt loop for windows NT. Need
5227 * to use WriteFileGather (nocache + page aligned). */
5228#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
5229
5230# ifdef UIO_MAXIOV
5231 RTSGSEG aSegs[UIO_MAXIOV];
5232# else
5233 RTSGSEG aSegs[512];
5234# endif
5235 RTSGBUF SgBuf;
5236 uint32_t cIncr = 1;
5237 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr, bFiller++)
5238 {
5239 size_t const cbSeg = cbBuf / cSegs;
5240 size_t const cbToWrite = cbSeg * cSegs;
5241 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
5242 {
5243 aSegs[iSeg].cbSeg = cbSeg;
5244 aSegs[iSeg].pvSeg = &pbBuf[cbToWrite - (iSeg + 1) * cbSeg];
5245 fsPerfFillWriteBuf(iSeg * cbSeg, (uint8_t *)aSegs[iSeg].pvSeg, cbSeg, bFiller);
5246 }
5247 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
5248 int rc = myFileSgWriteAt(hFile1, 0, &SgBuf, cbToWrite, NULL);
5249 if (RT_SUCCESS(rc))
5250 {
5251 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, 0, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5252 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbToWrite, bFiller);
5253 }
5254 else
5255 {
5256 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToWrite=%#zx", rc, cSegs, cbSeg, cbToWrite);
5257 break;
5258 }
5259 if (cSegs == 16)
5260 cIncr = 7;
5261 else if (cSegs == 16 * 7 + 16 /*= 128*/)
5262 cIncr = 64;
5263 }
5264
5265 /* random stuff, including zero segments. */
5266 for (uint32_t iTest = 0; iTest < 128; iTest++, bFiller++)
5267 {
5268 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
5269 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
5270 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
5271 size_t cbToWrite = 0;
5272 size_t cbLeft = cbBuf;
5273 uint8_t *pbCur = &pbBuf[cbBuf];
5274 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
5275 {
5276 uint32_t iAlign = RTRandU32Ex(0, 3);
5277 if (iAlign & 2) /* end is page aligned */
5278 {
5279 cbLeft -= (uintptr_t)pbCur & g_fPageOffset;
5280 pbCur -= (uintptr_t)pbCur & g_fPageOffset;
5281 }
5282
5283 size_t cbSegOthers = (cSegs - iSeg) * _8K;
5284 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
5285 : cbLeft > cSegs ? cbLeft - cSegs
5286 : cbLeft;
5287 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
5288 if (iAlign & 1) /* start is page aligned */
5289 cbSeg += ((uintptr_t)pbCur - cbSeg) & g_fPageOffset;
5290
5291 if (iSeg - iZeroSeg < cZeroSegs)
5292 cbSeg = 0;
5293
5294 cbToWrite += cbSeg;
5295 cbLeft -= cbSeg;
5296 pbCur -= cbSeg;
5297 aSegs[iSeg].cbSeg = cbSeg;
5298 aSegs[iSeg].pvSeg = pbCur;
5299 }
5300
5301 uint64_t const offFile = cbToWrite < cbFile ? RTRandU64Ex(0, cbFile - cbToWrite) : 0;
5302 uint64_t offFill = offFile;
5303 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
5304 if (aSegs[iSeg].cbSeg)
5305 {
5306 fsPerfFillWriteBuf(offFill, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg, bFiller);
5307 offFill += aSegs[iSeg].cbSeg;
5308 }
5309
5310 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
5311 int rc = myFileSgWriteAt(hFile1, offFile, &SgBuf, cbToWrite, NULL);
5312 if (RT_SUCCESS(rc))
5313 {
5314 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offFile, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5315 fsPerfCheckReadBuf(__LINE__, offFile, pbBuf, cbToWrite, bFiller);
5316 }
5317 else
5318 {
5319 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%#x cbToWrite=%#zx", rc, cSegs, cbToWrite);
5320 break;
5321 }
5322 }
5323
5324#endif
5325
5326 /*
5327 * Other OS specific stuff.
5328 */
5329#ifdef RT_OS_WINDOWS
5330 /* Check that reading at an offset modifies the position: */
5331 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, cbFile / 2, pbBuf, _4K, NULL), VINF_SUCCESS);
5332 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
5333 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
5334
5335 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
5336 LARGE_INTEGER offNt;
5337 offNt.QuadPart = cbFile / 2;
5338 rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
5339 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
5340 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
5341 RTTESTI_CHECK(Ios.Information == _4K);
5342 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
5343#endif
5344
5345 RTMemPageFree(pbBuf, cbBuf);
5346}
5347
5348
5349/**
5350 * Worker for testing RTFileFlush.
5351 */
5352DECL_FORCE_INLINE(int) fsPerfFSyncWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf, uint64_t *poffFile)
5353{
5354 if (*poffFile + cbBuf <= cbFile)
5355 { /* likely */ }
5356 else
5357 {
5358 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5359 *poffFile = 0;
5360 }
5361
5362 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbBuf, NULL), VINF_SUCCESS, rcCheck);
5363 RTTESTI_CHECK_RC_RET(RTFileFlush(hFile1), VINF_SUCCESS, rcCheck);
5364
5365 *poffFile += cbBuf;
5366 return VINF_SUCCESS;
5367}
5368
5369
5370static void fsPerfFSync(RTFILE hFile1, uint64_t cbFile)
5371{
5372 RTTestISub("fsync");
5373
5374 RTTESTI_CHECK_RC(RTFileFlush(hFile1), VINF_SUCCESS);
5375
5376 PROFILE_FN(RTFileFlush(hFile1), g_nsTestRun, "RTFileFlush");
5377
5378 size_t cbBuf = g_cbPage;
5379 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5380 RTTESTI_CHECK_RETV(pbBuf != NULL);
5381 memset(pbBuf, 0xf4, cbBuf);
5382
5383 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5384 uint64_t offFile = 0;
5385 PROFILE_FN(fsPerfFSyncWorker(hFile1, cbFile, pbBuf, cbBuf, &offFile), g_nsTestRun, "RTFileWrite[Page]/RTFileFlush");
5386
5387 RTMemPageFree(pbBuf, cbBuf);
5388}
5389
5390
5391#ifndef RT_OS_OS2
5392/**
5393 * Worker for profiling msync.
5394 */
5395DECL_FORCE_INLINE(int) fsPerfMSyncWorker(uint8_t *pbMapping, size_t offMapping, size_t cbFlush, size_t *pcbFlushed)
5396{
5397 uint8_t *pbCur = &pbMapping[offMapping];
5398 for (size_t offFlush = 0; offFlush < cbFlush; offFlush += g_cbPage)
5399 *(size_t volatile *)&pbCur[offFlush + 8] = cbFlush;
5400# ifdef RT_OS_WINDOWS
5401 CHECK_WINAPI_CALL(FlushViewOfFile(pbCur, cbFlush) == TRUE);
5402# else
5403 RTTESTI_CHECK(msync(pbCur, cbFlush, MS_SYNC) == 0);
5404# endif
5405 if (*pcbFlushed < offMapping + cbFlush)
5406 *pcbFlushed = offMapping + cbFlush;
5407 return VINF_SUCCESS;
5408}
5409#endif /* !RT_OS_OS2 */
5410
5411
5412static void fsPerfMMap(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
5413{
5414 RTTestISub("mmap");
5415#if !defined(RT_OS_OS2)
5416 static const char * const s_apszStates[] = { "readonly", "writecopy", "readwrite" };
5417 enum { kMMap_ReadOnly = 0, kMMap_WriteCopy, kMMap_ReadWrite, kMMap_End };
5418 for (int enmState = kMMap_ReadOnly; enmState < kMMap_End; enmState++)
5419 {
5420 /*
5421 * Do the mapping.
5422 */
5423 size_t cbMapping = (size_t)cbFile;
5424 if (cbMapping != cbFile)
5425 cbMapping = _256M;
5426 uint8_t *pbMapping;
5427
5428# ifdef RT_OS_WINDOWS
5429 HANDLE hSection;
5430 pbMapping = NULL;
5431 for (;; cbMapping /= 2)
5432 {
5433 hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile1), NULL,
5434 enmState == kMMap_ReadOnly ? PAGE_READONLY
5435 : enmState == kMMap_WriteCopy ? PAGE_WRITECOPY : PAGE_READWRITE,
5436 (uint32_t)((uint64_t)cbMapping >> 32), (uint32_t)cbMapping, NULL);
5437 DWORD dwErr1 = GetLastError();
5438 DWORD dwErr2 = 0;
5439 if (hSection != NULL)
5440 {
5441 pbMapping = (uint8_t *)MapViewOfFile(hSection,
5442 enmState == kMMap_ReadOnly ? FILE_MAP_READ
5443 : enmState == kMMap_WriteCopy ? FILE_MAP_COPY
5444 : FILE_MAP_WRITE,
5445 0, 0, cbMapping);
5446 if (pbMapping)
5447 break;
5448 dwErr2 = GetLastError();
5449 CHECK_WINAPI_CALL(CloseHandle(hSection) == TRUE);
5450 }
5451 if (cbMapping <= _2M)
5452 {
5453 RTTestIFailed("%u/%s: CreateFileMapping or MapViewOfFile failed: %u, %u",
5454 enmState, s_apszStates[enmState], dwErr1, dwErr2);
5455 break;
5456 }
5457 }
5458# else
5459 for (;; cbMapping /= 2)
5460 {
5461 pbMapping = (uint8_t *)mmap(NULL, cbMapping,
5462 enmState == kMMap_ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE,
5463 enmState == kMMap_WriteCopy ? MAP_PRIVATE : MAP_SHARED,
5464 (int)RTFileToNative(hFile1), 0);
5465 if ((void *)pbMapping != MAP_FAILED)
5466 break;
5467 if (cbMapping <= _2M)
5468 {
5469 RTTestIFailed("%u/%s: mmap failed: %s (%u)", enmState, s_apszStates[enmState], strerror(errno), errno);
5470 break;
5471 }
5472 }
5473# endif
5474 if (cbMapping <= _2M)
5475 continue;
5476
5477 /*
5478 * Time page-ins just for fun.
5479 */
5480 size_t const cPages = cbMapping >> g_cPageShift;
5481 size_t uDummy = 0;
5482 uint64_t ns = RTTimeNanoTS();
5483 for (size_t iPage = 0; iPage < cPages; iPage++)
5484 uDummy += ASMAtomicReadU8(&pbMapping[iPage << g_cPageShift]);
5485 ns = RTTimeNanoTS() - ns;
5486 RTTestIValueF(ns / cPages, RTTESTUNIT_NS_PER_OCCURRENCE, "page-in %s", s_apszStates[enmState]);
5487
5488 /* Check the content. */
5489 fsPerfCheckReadBuf(__LINE__, 0, pbMapping, cbMapping);
5490
5491 if (enmState != kMMap_ReadOnly)
5492 {
5493 /* Write stuff to the first two megabytes. In the COW case, we'll detect
5494 corruption of shared data during content checking of the RW iterations. */
5495 fsPerfFillWriteBuf(0, pbMapping, _2M, 0xf7);
5496 if (enmState == kMMap_ReadWrite && g_fMMapCoherency)
5497 {
5498 /* For RW we can try read back from the file handle and check if we get
5499 a match there first. */
5500 uint8_t abBuf[_4K];
5501 for (uint32_t off = 0; off < _2M; off += sizeof(abBuf))
5502 {
5503 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, off, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
5504 fsPerfCheckReadBuf(__LINE__, off, abBuf, sizeof(abBuf), 0xf7);
5505 }
5506# ifdef RT_OS_WINDOWS
5507 CHECK_WINAPI_CALL(FlushViewOfFile(pbMapping, _2M) == TRUE);
5508# else
5509 RTTESTI_CHECK(msync(pbMapping, _2M, MS_SYNC) == 0);
5510# endif
5511 }
5512
5513 /*
5514 * Time modifying and flushing a few different number of pages.
5515 */
5516 if (enmState == kMMap_ReadWrite)
5517 {
5518 size_t const s_acbFlush[] = { g_cbPage, g_cbPage * 2, g_cbPage * 3, g_cbPage * 8, g_cbPage * 16, _2M };
5519 for (unsigned iFlushSize = 0 ; iFlushSize < RT_ELEMENTS(s_acbFlush); iFlushSize++)
5520 {
5521 size_t const cbFlush = s_acbFlush[iFlushSize];
5522 if (cbFlush > cbMapping)
5523 continue;
5524
5525 char szDesc[80];
5526 RTStrPrintf(szDesc, sizeof(szDesc), "touch/flush/%zu", cbFlush);
5527 size_t const cFlushes = cbMapping / cbFlush;
5528 size_t const cbMappingUsed = cFlushes * cbFlush;
5529 size_t cbFlushed = 0;
5530 PROFILE_FN(fsPerfMSyncWorker(pbMapping, (iIteration * cbFlush) % cbMappingUsed, cbFlush, &cbFlushed),
5531 g_nsTestRun, szDesc);
5532
5533 /*
5534 * Check that all the changes made it thru to the file:
5535 */
5536 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
5537 {
5538 size_t cbBuf = RT_MIN(_2M, g_cbMaxBuffer);
5539 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5540 if (!pbBuf)
5541 {
5542 cbBuf = _4K;
5543 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5544 }
5545 RTTESTI_CHECK(pbBuf != NULL);
5546 if (pbBuf)
5547 {
5548 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
5549 size_t const cbToCheck = RT_MIN(cFlushes * cbFlush, cbFlushed);
5550 unsigned cErrors = 0;
5551 for (size_t offBuf = 0; cErrors < 32 && offBuf < cbToCheck; offBuf += cbBuf)
5552 {
5553 size_t cbToRead = RT_MIN(cbBuf, cbToCheck - offBuf);
5554 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, pbBuf, cbToRead, NULL), VINF_SUCCESS);
5555
5556 for (size_t offFlush = 0; offFlush < cbToRead; offFlush += g_cbPage)
5557 if (*(size_t volatile *)&pbBuf[offFlush + 8] != cbFlush)
5558 {
5559 RTTestIFailed("Flush issue at offset #%zx: %#zx, expected %#zx (cbFlush=%#zx, %#RX64)",
5560 offBuf + offFlush + 8, *(size_t volatile *)&pbBuf[offFlush + 8],
5561 cbFlush, cbFlush, *(uint64_t volatile *)&pbBuf[offFlush]);
5562 if (++cErrors > 32)
5563 break;
5564 }
5565 }
5566 RTMemPageFree(pbBuf, cbBuf);
5567 }
5568 }
5569 }
5570
5571# if 0 /* not needed, very very slow */
5572 /*
5573 * Restore the file to 0xf6 state for the next test.
5574 */
5575 RTTestIPrintf(RTTESTLVL_ALWAYS, "Restoring content...\n");
5576 fsPerfFillWriteBuf(0, pbMapping, cbMapping, 0xf6);
5577# ifdef RT_OS_WINDOWS
5578 CHECK_WINAPI_CALL(FlushViewOfFile(pbMapping, cbMapping) == TRUE);
5579# else
5580 RTTESTI_CHECK(msync(pbMapping, cbMapping, MS_SYNC) == 0);
5581# endif
5582 RTTestIPrintf(RTTESTLVL_ALWAYS, "... done\n");
5583# endif
5584 }
5585 }
5586
5587 /*
5588 * Observe how regular writes affects a read-only or readwrite mapping.
5589 * These should ideally be immediately visible in the mapping, at least
5590 * when not performed thru an no-cache handle.
5591 */
5592 if ( (enmState == kMMap_ReadOnly || enmState == kMMap_ReadWrite)
5593 && g_fMMapCoherency)
5594 {
5595 size_t cbBuf = RT_MIN(RT_MIN(_2M, cbMapping / 2), g_cbMaxBuffer);
5596 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5597 if (!pbBuf)
5598 {
5599 cbBuf = _4K;
5600 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
5601 }
5602 RTTESTI_CHECK(pbBuf != NULL);
5603 if (pbBuf)
5604 {
5605 /* Do a number of random writes to the file (using hFile1).
5606 Immediately undoing them. */
5607 for (uint32_t i = 0; i < 128; i++)
5608 {
5609 /* Generate a randomly sized write at a random location, making
5610 sure it differs from whatever is there already before writing. */
5611 uint32_t const cbToWrite = RTRandU32Ex(1, (uint32_t)cbBuf);
5612 uint64_t const offToWrite = RTRandU64Ex(0, cbMapping - cbToWrite);
5613
5614 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf8);
5615 pbBuf[0] = ~pbBuf[0];
5616 if (cbToWrite > 1)
5617 pbBuf[cbToWrite - 1] = ~pbBuf[cbToWrite - 1];
5618 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5619
5620 /* Check the mapping. */
5621 if (memcmp(&pbMapping[(size_t)offToWrite], pbBuf, cbToWrite) != 0)
5622 {
5623 RTTestIFailed("Write #%u @ %#RX64 LB %#x was not reflected in the mapping!\n", i, offToWrite, cbToWrite);
5624 }
5625
5626 /* Restore */
5627 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf6);
5628 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
5629 }
5630
5631 RTMemPageFree(pbBuf, cbBuf);
5632 }
5633 }
5634
5635 /*
5636 * Unmap it.
5637 */
5638# ifdef RT_OS_WINDOWS
5639 CHECK_WINAPI_CALL(UnmapViewOfFile(pbMapping) == TRUE);
5640 CHECK_WINAPI_CALL(CloseHandle(hSection) == TRUE);
5641# else
5642 RTTESTI_CHECK(munmap(pbMapping, cbMapping) == 0);
5643# endif
5644 }
5645
5646 /*
5647 * Memory mappings without open handles (pretty common).
5648 */
5649 char *pbContentUnaligned = (char *)RTMemAlloc(256*1024 + g_cbPage - 1);
5650 RTTESTI_CHECK(pbContentUnaligned != NULL);
5651 if (pbContentUnaligned)
5652 {
5653 for (uint32_t i = 0; i < 32; i++)
5654 {
5655 /* Create a new file, 256 KB in size, and fill it with random bytes.
5656 Try uncached access if we can to force the page-in to do actual reads. */
5657 char szFile2[FSPERF_MAX_PATH + 32];
5658 memcpy(szFile2, g_szDir, g_cchDir);
5659 RTStrPrintf(&szFile2[g_cchDir], sizeof(szFile2) - g_cchDir, "mmap-%u.noh", i);
5660 RTFILE hFile2 = NIL_RTFILE;
5661 int rc = (i & 3) == 3 ? VERR_TRY_AGAIN
5662 : RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_NO_CACHE);
5663 if (RT_FAILURE(rc))
5664 {
5665 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
5666 VINF_SUCCESS);
5667 }
5668
5669 char * const pbContent = &pbContentUnaligned[g_cbPage - ((uintptr_t)&pbContentUnaligned[0] & g_fPageOffset)];
5670 size_t const cbContent = 256*1024;
5671 RTRandBytes(pbContent, cbContent);
5672 RTTESTI_CHECK_RC(rc = RTFileWrite(hFile2, pbContent, cbContent, NULL), VINF_SUCCESS);
5673 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5674 if (RT_SUCCESS(rc))
5675 {
5676 /* Reopen the file with normal caching. Every second time, we also
5677 does a read-only open of it to confuse matters. */
5678 RTFILE hFile3 = NIL_RTFILE;
5679 if ((i & 3) == 3)
5680 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
5681 hFile2 = NIL_RTFILE;
5682 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE),
5683 VINF_SUCCESS);
5684 if ((i & 3) == 1)
5685 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
5686
5687 /* Memory map it read-write (no COW). */
5688#ifdef RT_OS_WINDOWS
5689 HANDLE hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile2), NULL, PAGE_READWRITE, 0, cbContent, NULL);
5690 CHECK_WINAPI_CALL(hSection != NULL);
5691 uint8_t *pbMapping = (uint8_t *)MapViewOfFile(hSection, FILE_MAP_WRITE, 0, 0, cbContent);
5692 CHECK_WINAPI_CALL(pbMapping != NULL);
5693 CHECK_WINAPI_CALL(CloseHandle(hSection) == TRUE);
5694# else
5695 uint8_t *pbMapping = (uint8_t *)mmap(NULL, cbContent, PROT_READ | PROT_WRITE, MAP_SHARED,
5696 (int)RTFileToNative(hFile2), 0);
5697 if ((void *)pbMapping == MAP_FAILED)
5698 pbMapping = NULL;
5699 RTTESTI_CHECK_MSG(pbMapping != NULL, ("errno=%s (%d)\n", strerror(errno), errno));
5700# endif
5701
5702 /* Close the file handles. */
5703 if ((i & 7) == 7)
5704 {
5705 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
5706 hFile3 = NIL_RTFILE;
5707 }
5708 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5709 if ((i & 7) == 5)
5710 {
5711 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
5712 hFile3 = NIL_RTFILE;
5713 }
5714 if (pbMapping)
5715 {
5716 RTThreadSleep(2); /* fudge for cleanup/whatever */
5717
5718 /* Page in the mapping by comparing with the content we wrote above. */
5719 RTTESTI_CHECK(memcmp(pbMapping, pbContent, cbContent) == 0);
5720
5721 /* Now dirty everything by inverting everything. */
5722 size_t *puCur = (size_t *)pbMapping;
5723 size_t cLeft = cbContent / sizeof(*puCur);
5724 while (cLeft-- > 0)
5725 {
5726 *puCur = ~*puCur;
5727 puCur++;
5728 }
5729
5730 /* Sync it all. */
5731# ifdef RT_OS_WINDOWS
5732 //CHECK_WINAPI_CALL(FlushViewOfFile(pbMapping, cbContent) == TRUE);
5733 SetLastError(0);
5734 if (FlushViewOfFile(pbMapping, cbContent) != TRUE)
5735 RTTestIFailed("line %u, i=%u: FlushViewOfFile(%p, %#zx) failed: %u / %#x", __LINE__, i,
5736 pbMapping, cbContent, GetLastError(), RTNtLastStatusValue());
5737# else
5738 RTTESTI_CHECK(msync(pbMapping, cbContent, MS_SYNC) == 0);
5739# endif
5740
5741 /* Unmap it. */
5742# ifdef RT_OS_WINDOWS
5743 CHECK_WINAPI_CALL(UnmapViewOfFile(pbMapping) == TRUE);
5744# else
5745 RTTESTI_CHECK(munmap(pbMapping, cbContent) == 0);
5746# endif
5747 }
5748
5749 if (hFile3 != NIL_RTFILE)
5750 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
5751 }
5752 RTTESTI_CHECK_RC(RTFileDelete(szFile2), VINF_SUCCESS);
5753 }
5754 }
5755
5756#else
5757 RTTestSkipped(g_hTest, "not supported/implemented");
5758 RT_NOREF(hFile1, hFileNoCache, cbFile);
5759#endif
5760}
5761
5762
5763/**
5764 * This does the read, write and seek tests.
5765 */
5766static void fsPerfIo(void)
5767{
5768 RTTestISub("I/O");
5769
5770 /*
5771 * Determin the size of the test file.
5772 */
5773 g_szDir[g_cchDir] = '\0';
5774 RTFOFF cbFree = 0;
5775 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
5776 uint64_t cbFile = g_cbIoFile;
5777 if (cbFile + _16M < (uint64_t)cbFree)
5778 cbFile = RT_ALIGN_64(cbFile, _64K);
5779 else if (cbFree < _32M)
5780 {
5781 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
5782 return;
5783 }
5784 else
5785 {
5786 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
5787 cbFile = RT_ALIGN_64(cbFile, _64K);
5788 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
5789 }
5790 if (cbFile < _64K)
5791 {
5792 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 64KB", cbFile);
5793 return;
5794 }
5795
5796 /*
5797 * Create a cbFile sized test file.
5798 */
5799 RTFILE hFile1;
5800 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file21")),
5801 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
5802 RTFILE hFileNoCache;
5803 if (!g_fIgnoreNoCache)
5804 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileNoCache, g_szDir,
5805 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE),
5806 VINF_SUCCESS);
5807 else
5808 {
5809 int rc = RTFileOpen(&hFileNoCache, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE);
5810 if (RT_FAILURE(rc))
5811 {
5812 RTTestIPrintf(RTTESTLVL_ALWAYS, "Unable to open I/O file with non-cache flag (%Rrc), skipping related tests.\n", rc);
5813 hFileNoCache = NIL_RTFILE;
5814 }
5815 }
5816 RTFILE hFileWriteThru;
5817 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileWriteThru, g_szDir,
5818 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_WRITE_THROUGH),
5819 VINF_SUCCESS);
5820
5821 uint8_t *pbFree = NULL;
5822 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
5823 RTMemFree(pbFree);
5824 if (RT_SUCCESS(rc))
5825 {
5826 /*
5827 * Do the testing & profiling.
5828 */
5829 if (g_fSeek)
5830 fsPerfIoSeek(hFile1, cbFile);
5831
5832 if (g_fMMap && g_iMMapPlacement < 0)
5833 {
5834 fsPerfMMap(hFile1, hFileNoCache, cbFile);
5835 fsPerfReinitFile(hFile1, cbFile);
5836 }
5837
5838 if (g_fReadTests)
5839 fsPerfRead(hFile1, hFileNoCache, cbFile);
5840 if (g_fReadPerf)
5841 for (unsigned i = 0; i < g_cIoBlocks; i++)
5842 fsPerfIoReadBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
5843#ifdef FSPERF_TEST_SENDFILE
5844 if (g_fSendFile)
5845 fsPerfSendFile(hFile1, cbFile);
5846#endif
5847#ifdef RT_OS_LINUX
5848 if (g_fSplice)
5849 fsPerfSpliceToPipe(hFile1, cbFile);
5850#endif
5851 if (g_fMMap && g_iMMapPlacement == 0)
5852 fsPerfMMap(hFile1, hFileNoCache, cbFile);
5853
5854 /* This is destructive to the file content. */
5855 if (g_fWriteTests)
5856 fsPerfWrite(hFile1, hFileNoCache, hFileWriteThru, cbFile);
5857 if (g_fWritePerf)
5858 for (unsigned i = 0; i < g_cIoBlocks; i++)
5859 fsPerfIoWriteBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
5860#ifdef RT_OS_LINUX
5861 if (g_fSplice)
5862 fsPerfSpliceToFile(hFile1, cbFile);
5863#endif
5864 if (g_fFSync)
5865 fsPerfFSync(hFile1, cbFile);
5866
5867 if (g_fMMap && g_iMMapPlacement > 0)
5868 {
5869 fsPerfReinitFile(hFile1, cbFile);
5870 fsPerfMMap(hFile1, hFileNoCache, cbFile);
5871 }
5872 }
5873
5874 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
5875 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
5876 if (hFileNoCache != NIL_RTFILE || !g_fIgnoreNoCache)
5877 RTTESTI_CHECK_RC(RTFileClose(hFileNoCache), VINF_SUCCESS);
5878 RTTESTI_CHECK_RC(RTFileClose(hFileWriteThru), VINF_SUCCESS);
5879 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
5880}
5881
5882
5883DECL_FORCE_INLINE(int) fsPerfCopyWorker1(const char *pszSrc, const char *pszDst)
5884{
5885 RTFileDelete(pszDst);
5886 return RTFileCopy(pszSrc, pszDst);
5887}
5888
5889
5890#ifdef RT_OS_LINUX
5891DECL_FORCE_INLINE(int) fsPerfCopyWorkerSendFile(RTFILE hFile1, RTFILE hFile2, size_t cbFile)
5892{
5893 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile2, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
5894
5895 loff_t off = 0;
5896 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &off, cbFile);
5897 if (cbSent > 0 && (size_t)cbSent == cbFile)
5898 return 0;
5899
5900 int rc = VERR_GENERAL_FAILURE;
5901 if (cbSent < 0)
5902 {
5903 rc = RTErrConvertFromErrno(errno);
5904 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)", cbFile, cbSent, errno, rc);
5905 }
5906 else
5907 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
5908 cbFile, cbSent, cbFile, cbSent - cbFile);
5909 return rc;
5910}
5911#endif /* RT_OS_LINUX */
5912
5913
5914static void fsPerfCopy(void)
5915{
5916 RTTestISub("copy");
5917
5918 /*
5919 * Non-existing files.
5920 */
5921 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-file")),
5922 InDir2(RT_STR_TUPLE("whatever"))), VERR_FILE_NOT_FOUND);
5923 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
5924 InDir2(RT_STR_TUPLE("no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
5925 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
5926 InDir2(RT_STR_TUPLE("whatever"))), VERR_PATH_NOT_FOUND);
5927
5928 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
5929 InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
5930 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
5931 InDir2(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
5932
5933 /*
5934 * Determin the size of the test file.
5935 * We want to be able to make 1 copy of it.
5936 */
5937 g_szDir[g_cchDir] = '\0';
5938 RTFOFF cbFree = 0;
5939 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
5940 uint64_t cbFile = g_cbIoFile;
5941 if (cbFile + _16M < (uint64_t)cbFree)
5942 cbFile = RT_ALIGN_64(cbFile, _64K);
5943 else if (cbFree < _32M)
5944 {
5945 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
5946 return;
5947 }
5948 else
5949 {
5950 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
5951 cbFile = RT_ALIGN_64(cbFile, _64K);
5952 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
5953 }
5954 if (cbFile < _512K * 2)
5955 {
5956 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 1MB", cbFile);
5957 return;
5958 }
5959 cbFile /= 2;
5960
5961 /*
5962 * Create a cbFile sized test file.
5963 */
5964 RTFILE hFile1;
5965 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file22")),
5966 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
5967 uint8_t *pbFree = NULL;
5968 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
5969 RTMemFree(pbFree);
5970 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
5971 if (RT_SUCCESS(rc))
5972 {
5973 /*
5974 * Make copies.
5975 */
5976 /* plain */
5977 RTFileDelete(InDir2(RT_STR_TUPLE("file23")));
5978 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VINF_SUCCESS);
5979 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VERR_ALREADY_EXISTS);
5980 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
5981
5982 /* by handle */
5983 hFile1 = NIL_RTFILE;
5984 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
5985 RTFILE hFile2 = NIL_RTFILE;
5986 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
5987 RTTESTI_CHECK_RC(RTFileCopyByHandles(hFile1, hFile2), VINF_SUCCESS);
5988 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
5989 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
5990 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
5991
5992 /* copy part */
5993 hFile1 = NIL_RTFILE;
5994 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
5995 hFile2 = NIL_RTFILE;
5996 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
5997 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, 0, hFile2, 0, cbFile / 2, 0, NULL), VINF_SUCCESS);
5998 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, cbFile / 2, hFile2, cbFile / 2, cbFile - cbFile / 2, 0, NULL), VINF_SUCCESS);
5999 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6000 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6001 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6002
6003#ifdef RT_OS_LINUX
6004 /*
6005 * On linux we can also use sendfile between two files, except for 2.5.x to 2.6.33.
6006 */
6007 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_C(0x7ffff000));
6008 char szRelease[64];
6009 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
6010 bool const fSendFileBetweenFiles = RTStrVersionCompare(szRelease, "2.5.0") < 0
6011 || RTStrVersionCompare(szRelease, "2.6.33") >= 0;
6012 if (fSendFileBetweenFiles)
6013 {
6014 /* Copy the whole file: */
6015 hFile1 = NIL_RTFILE;
6016 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6017 RTFileDelete(g_szDir2);
6018 hFile2 = NIL_RTFILE;
6019 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6020 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbFile);
6021 if (cbSent < 0)
6022 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
6023 cbFile, cbSent, errno, RTErrConvertFromErrno(errno));
6024 else if ((size_t)cbSent != cbFileMax)
6025 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
6026 cbFile, cbSent, cbFileMax, cbSent - cbFileMax);
6027 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6028 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6029 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6030
6031 /* Try copy a little bit too much: */
6032 if (cbFile == cbFileMax)
6033 {
6034 hFile1 = NIL_RTFILE;
6035 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6036 RTFileDelete(g_szDir2);
6037 hFile2 = NIL_RTFILE;
6038 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6039 size_t cbToCopy = cbFile + RTRandU32Ex(1, _64M);
6040 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbToCopy);
6041 if (cbSent < 0)
6042 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
6043 cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
6044 else if ((size_t)cbSent != cbFile)
6045 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
6046 cbToCopy, cbSent, cbFile, cbSent - cbFile);
6047 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6048 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6049 }
6050
6051 /* Do partial copy: */
6052 hFile2 = NIL_RTFILE;
6053 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6054 for (uint32_t i = 0; i < 64; i++)
6055 {
6056 size_t cbToCopy = RTRandU32Ex(0, cbFileMax - 1);
6057 uint32_t const offFile = RTRandU32Ex(1, (uint64_t)RT_MIN(cbFileMax - cbToCopy, UINT32_MAX));
6058 RTTESTI_CHECK_RC_BREAK(RTFileSeek(hFile2, offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6059 loff_t offFile2 = offFile;
6060 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &offFile2, cbToCopy);
6061 if (cbSent < 0)
6062 RTTestIFailed("sendfile(file,file,%#x,%#zx) failed (%zd): %d (%Rrc)",
6063 offFile, cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
6064 else if ((size_t)cbSent != cbToCopy)
6065 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx, expected %#zx (diff %zd)",
6066 offFile, cbToCopy, cbSent, cbToCopy, cbSent - cbToCopy);
6067 else if (offFile2 != (loff_t)(offFile + cbToCopy))
6068 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx + off=%#RX64, expected off %#x",
6069 offFile, cbToCopy, cbSent, offFile2, offFile + cbToCopy);
6070 }
6071 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6072 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6073 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
6074 }
6075#endif
6076
6077 /*
6078 * Do some benchmarking.
6079 */
6080#define PROFILE_COPY_FN(a_szOperation, a_fnCall) \
6081 do \
6082 { \
6083 /* Estimate how many iterations we need to fill up the given timeslot: */ \
6084 fsPerfYield(); \
6085 uint64_t nsStart = RTTimeNanoTS(); \
6086 uint64_t ns; \
6087 do \
6088 ns = RTTimeNanoTS(); \
6089 while (ns == nsStart); \
6090 nsStart = ns; \
6091 \
6092 uint64_t iIteration = 0; \
6093 do \
6094 { \
6095 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
6096 iIteration++; \
6097 ns = RTTimeNanoTS() - nsStart; \
6098 } while (ns < RT_NS_10MS); \
6099 ns /= iIteration; \
6100 if (ns > g_nsPerNanoTSCall + 32) \
6101 ns -= g_nsPerNanoTSCall; \
6102 uint64_t cIterations = g_nsTestRun / ns; \
6103 if (cIterations < 2) \
6104 cIterations = 2; \
6105 else if (cIterations & 1) \
6106 cIterations++; \
6107 \
6108 /* Do the actual profiling: */ \
6109 iIteration = 0; \
6110 fsPerfYield(); \
6111 nsStart = RTTimeNanoTS(); \
6112 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
6113 { \
6114 for (; iIteration < cIterations; iIteration++)\
6115 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
6116 ns = RTTimeNanoTS() - nsStart;\
6117 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
6118 break; \
6119 cIterations += cIterations / 4; \
6120 if (cIterations & 1) \
6121 cIterations++; \
6122 nsStart += g_nsPerNanoTSCall; \
6123 } \
6124 RTTestIValueF(ns / iIteration, \
6125 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation " latency"); \
6126 RTTestIValueF((uint64_t)((double)(iIteration * cbFile) / ((double)ns / RT_NS_1SEC)), \
6127 RTTESTUNIT_BYTES_PER_SEC, a_szOperation " throughput"); \
6128 RTTestIValueF((uint64_t)iIteration * cbFile, \
6129 RTTESTUNIT_BYTES, a_szOperation " bytes"); \
6130 RTTestIValueF(iIteration, \
6131 RTTESTUNIT_OCCURRENCES, a_szOperation " iterations"); \
6132 if (g_fShowDuration) \
6133 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation " duration"); \
6134 } while (0)
6135
6136 PROFILE_COPY_FN("RTFileCopy/Replace", fsPerfCopyWorker1(g_szDir, g_szDir2));
6137
6138 hFile1 = NIL_RTFILE;
6139 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6140 RTFileDelete(g_szDir2);
6141 hFile2 = NIL_RTFILE;
6142 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6143 PROFILE_COPY_FN("RTFileCopyByHandles/Overwrite", RTFileCopyByHandles(hFile1, hFile2));
6144 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6145 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6146
6147 /* We could benchmark RTFileCopyPart with various block sizes and whatnot...
6148 But it's currently well covered by the two previous operations. */
6149
6150#ifdef RT_OS_LINUX
6151 if (fSendFileBetweenFiles)
6152 {
6153 hFile1 = NIL_RTFILE;
6154 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
6155 RTFileDelete(g_szDir2);
6156 hFile2 = NIL_RTFILE;
6157 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
6158 PROFILE_COPY_FN("sendfile/overwrite", fsPerfCopyWorkerSendFile(hFile1, hFile2, cbFileMax));
6159 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
6160 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6161 }
6162#endif
6163 }
6164
6165 /*
6166 * Clean up.
6167 */
6168 RTFileDelete(InDir2(RT_STR_TUPLE("file22c1")));
6169 RTFileDelete(InDir2(RT_STR_TUPLE("file22c2")));
6170 RTFileDelete(InDir2(RT_STR_TUPLE("file22c3")));
6171 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
6172}
6173
6174
6175static void fsPerfRemote(void)
6176{
6177 RTTestISub("remote");
6178 uint8_t abBuf[16384];
6179
6180
6181 /*
6182 * Create a file on the remote end and check that we can immediately see it.
6183 */
6184 RTTESTI_CHECK_RC_RETV(FsPerfCommsSend("reset\n"
6185 "open 0 'file30' 'w' 'ca'\n"
6186 "writepattern 0 0 0 4096" FSPERF_EOF_STR), VINF_SUCCESS);
6187
6188 RTFILEACTION enmActuallyTaken = RTFILEACTION_END;
6189 RTFILE hFile0 = NIL_RTFILE;
6190 RTTESTI_CHECK_RC(RTFileOpenEx(InDir(RT_STR_TUPLE("file30")), RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
6191 &hFile0, &enmActuallyTaken), VINF_SUCCESS);
6192 RTTESTI_CHECK(enmActuallyTaken == RTFILEACTION_OPENED);
6193 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 4096, NULL), VINF_SUCCESS);
6194 AssertCompile(RT_ELEMENTS(g_abPattern0) == 1);
6195 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 4096, g_abPattern0[0]));
6196 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6197
6198 /*
6199 * Append a little to it on the host and see that we can read it.
6200 */
6201 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 4096 1 1024" FSPERF_EOF_STR), VINF_SUCCESS);
6202 AssertCompile(RT_ELEMENTS(g_abPattern1) == 1);
6203 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1024, NULL), VINF_SUCCESS);
6204 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 1024, g_abPattern1[0]));
6205 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6206
6207 /*
6208 * Have the host truncate the file.
6209 */
6210 RTTESTI_CHECK_RC(FsPerfCommsSend("truncate 0 1024" FSPERF_EOF_STR), VINF_SUCCESS);
6211 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6212 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6213 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1024, NULL), VINF_SUCCESS);
6214 AssertCompile(RT_ELEMENTS(g_abPattern0) == 1);
6215 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 4096, g_abPattern0[0]));
6216 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6217
6218 /*
6219 * Write a bunch of stuff to the file here, then truncate it to a given size,
6220 * then have the host add more, finally test that we can successfully chop off
6221 * what the host added by reissuing the same truncate call as before (issue of
6222 * RDBSS using cached size to noop out set-eof-to-same-size).
6223 */
6224 memset(abBuf, 0xe9, sizeof(abBuf));
6225 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6226 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 16384, NULL), VINF_SUCCESS);
6227 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 8000), VINF_SUCCESS);
6228 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 8000 0 1000" FSPERF_EOF_STR), VINF_SUCCESS);
6229 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 8000), VINF_SUCCESS);
6230 uint64_t cbFile = 0;
6231 RTTESTI_CHECK_RC(RTFileQuerySize(hFile0, &cbFile), VINF_SUCCESS);
6232 RTTESTI_CHECK_MSG(cbFile == 8000, ("cbFile=%u\n", cbFile));
6233 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6234
6235 /* Same, but using RTFileRead to find out and RTFileWrite to define the size. */
6236 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6237 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 0), VINF_SUCCESS);
6238 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 5000, NULL), VINF_SUCCESS);
6239 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 5000 0 1000" FSPERF_EOF_STR), VINF_SUCCESS);
6240 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 5000), VINF_SUCCESS);
6241 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6242 RTTESTI_CHECK_RC(RTFileQuerySize(hFile0, &cbFile), VINF_SUCCESS);
6243 RTTESTI_CHECK_MSG(cbFile == 5000, ("cbFile=%u\n", cbFile));
6244
6245 /* Same, but host truncates rather than adding stuff. */
6246 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6247 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 16384, NULL), VINF_SUCCESS);
6248 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 10000), VINF_SUCCESS);
6249 RTTESTI_CHECK_RC(FsPerfCommsSend("truncate 0 4000" FSPERF_EOF_STR), VINF_SUCCESS);
6250 RTTESTI_CHECK_RC(RTFileQuerySize(hFile0, &cbFile), VINF_SUCCESS);
6251 RTTESTI_CHECK_MSG(cbFile == 4000, ("cbFile=%u\n", cbFile));
6252 RTTESTI_CHECK_RC(RTFileRead(hFile0, abBuf, 1, NULL), VERR_EOF);
6253
6254 /*
6255 * Test noticing remote size changes when opening a file. Need to keep hFile0
6256 * open here so we're sure to have an inode/FCB for the file in question.
6257 */
6258 memset(abBuf, 0xe7, sizeof(abBuf));
6259 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6260 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 0), VINF_SUCCESS);
6261 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 12288, NULL), VINF_SUCCESS);
6262 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 12288), VINF_SUCCESS);
6263
6264 RTTESTI_CHECK_RC(FsPerfCommsSend("writepattern 0 12288 2 4096" FSPERF_EOF_STR), VINF_SUCCESS);
6265
6266 enmActuallyTaken = RTFILEACTION_END;
6267 RTFILE hFile1 = NIL_RTFILE;
6268 RTTESTI_CHECK_RC(RTFileOpenEx(InDir(RT_STR_TUPLE("file30")), RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
6269 &hFile1, &enmActuallyTaken), VINF_SUCCESS);
6270 RTTESTI_CHECK(enmActuallyTaken == RTFILEACTION_OPENED);
6271 AssertCompile(sizeof(abBuf) >= 16384);
6272 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 16384, NULL), VINF_SUCCESS);
6273 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 12288, 0xe7));
6274 AssertCompile(RT_ELEMENTS(g_abPattern2) == 1);
6275 RTTESTI_CHECK(ASMMemIsAllU8(&abBuf[12288], 4096, g_abPattern2[0]));
6276 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
6277 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6278
6279 /* Same, but remote end truncates the file: */
6280 memset(abBuf, 0xe6, sizeof(abBuf));
6281 RTTESTI_CHECK_RC(RTFileSeek(hFile0, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
6282 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 0), VINF_SUCCESS);
6283 RTTESTI_CHECK_RC(RTFileWrite(hFile0, abBuf, 12288, NULL), VINF_SUCCESS);
6284 RTTESTI_CHECK_RC(RTFileSetSize(hFile0, 12288), VINF_SUCCESS);
6285
6286 RTTESTI_CHECK_RC(FsPerfCommsSend("truncate 0 7500" FSPERF_EOF_STR), VINF_SUCCESS);
6287
6288 enmActuallyTaken = RTFILEACTION_END;
6289 hFile1 = NIL_RTFILE;
6290 RTTESTI_CHECK_RC(RTFileOpenEx(InDir(RT_STR_TUPLE("file30")), RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
6291 &hFile1, &enmActuallyTaken), VINF_SUCCESS);
6292 RTTESTI_CHECK(enmActuallyTaken == RTFILEACTION_OPENED);
6293 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 7500, NULL), VINF_SUCCESS);
6294 RTTESTI_CHECK(ASMMemIsAllU8(abBuf, 7500, 0xe6));
6295 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
6296 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
6297
6298 RTTESTI_CHECK_RC(RTFileClose(hFile0), VINF_SUCCESS);
6299}
6300
6301
6302
6303/**
6304 * Display the usage to @a pStrm.
6305 */
6306static void Usage(PRTSTREAM pStrm)
6307{
6308 char szExec[FSPERF_MAX_PATH];
6309 RTStrmPrintf(pStrm, "usage: %s <-d <testdir>> [options]\n",
6310 RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
6311 RTStrmPrintf(pStrm, "\n");
6312 RTStrmPrintf(pStrm, "options: \n");
6313
6314 for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
6315 {
6316 char szHelp[80];
6317 const char *pszHelp;
6318 switch (g_aCmdOptions[i].iShort)
6319 {
6320 case 'd': pszHelp = "The directory to use for testing. default: CWD/fstestdir"; break;
6321 case 'r': pszHelp = "Don't abspath test dir (good for deep dirs). default: disabled"; break;
6322 case 'e': pszHelp = "Enables all tests. default: -e"; break;
6323 case 'z': pszHelp = "Disables all tests. default: -e"; break;
6324 case 's': pszHelp = "Set benchmark duration in seconds. default: 10 sec"; break;
6325 case 'm': pszHelp = "Set benchmark duration in milliseconds. default: 10000 ms"; break;
6326 case 'v': pszHelp = "More verbose execution."; break;
6327 case 'q': pszHelp = "Quiet execution."; break;
6328 case 'h': pszHelp = "Displays this help and exit"; break;
6329 case 'V': pszHelp = "Displays the program revision"; break;
6330 case kCmdOpt_ShowDuration: pszHelp = "Show duration of profile runs. default: --no-show-duration"; break;
6331 case kCmdOpt_NoShowDuration: pszHelp = "Hide duration of profile runs. default: --no-show-duration"; break;
6332 case kCmdOpt_ShowIterations: pszHelp = "Show iteration count for profile runs. default: --no-show-iterations"; break;
6333 case kCmdOpt_NoShowIterations: pszHelp = "Hide iteration count for profile runs. default: --no-show-iterations"; break;
6334 case kCmdOpt_ManyFiles: pszHelp = "Count of files in big test dir. default: --many-files 10000"; break;
6335 case kCmdOpt_NoManyFiles: pszHelp = "Skip big test dir with many files. default: --many-files 10000"; break;
6336 case kCmdOpt_ManyTreeFilesPerDir: pszHelp = "Count of files per directory in test tree. default: 640"; break;
6337 case kCmdOpt_ManyTreeSubdirsPerDir: pszHelp = "Count of subdirs per directory in test tree. default: 16"; break;
6338 case kCmdOpt_ManyTreeDepth: pszHelp = "Depth of test tree (not counting root). default: 1"; break;
6339#if defined(RT_OS_WINDOWS)
6340 case kCmdOpt_MaxBufferSize: pszHelp = "For avoiding the MDL limit on windows. default: 32MiB"; break;
6341#else
6342 case kCmdOpt_MaxBufferSize: pszHelp = "For avoiding the MDL limit on windows. default: 0"; break;
6343#endif
6344 case kCmdOpt_MMapPlacement: pszHelp = "When to do mmap testing (caching effects): first, between (default), last "; break;
6345 case kCmdOpt_IgnoreNoCache: pszHelp = "Ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
6346 case kCmdOpt_NoIgnoreNoCache: pszHelp = "Do not ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
6347 case kCmdOpt_IoFileSize: pszHelp = "Size of file used for I/O tests. default: 512 MB"; break;
6348 case kCmdOpt_SetBlockSize: pszHelp = "Sets single I/O block size (in bytes)."; break;
6349 case kCmdOpt_AddBlockSize: pszHelp = "Adds an I/O block size (in bytes)."; break;
6350 default:
6351 if (g_aCmdOptions[i].iShort >= kCmdOpt_First)
6352 {
6353 if (RTStrStartsWith(g_aCmdOptions[i].pszLong, "--no-"))
6354 RTStrPrintf(szHelp, sizeof(szHelp), "Disables the '%s' test.", g_aCmdOptions[i].pszLong + 5);
6355 else
6356 RTStrPrintf(szHelp, sizeof(szHelp), "Enables the '%s' test.", g_aCmdOptions[i].pszLong + 2);
6357 pszHelp = szHelp;
6358 }
6359 else
6360 pszHelp = "Option undocumented";
6361 break;
6362 }
6363 if ((unsigned)g_aCmdOptions[i].iShort < 127U)
6364 {
6365 char szOpt[64];
6366 RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
6367 RTStrmPrintf(pStrm, " %-19s %s\n", szOpt, pszHelp);
6368 }
6369 else
6370 RTStrmPrintf(pStrm, " %-19s %s\n", g_aCmdOptions[i].pszLong, pszHelp);
6371 }
6372}
6373
6374
6375static uint32_t fsPerfCalcManyTreeFiles(void)
6376{
6377 uint32_t cDirs = 1;
6378 for (uint32_t i = 0, cDirsAtLevel = 1; i < g_cManyTreeDepth; i++)
6379 {
6380 cDirs += cDirsAtLevel * g_cManyTreeSubdirsPerDir;
6381 cDirsAtLevel *= g_cManyTreeSubdirsPerDir;
6382 }
6383 return g_cManyTreeFilesPerDir * cDirs;
6384}
6385
6386
6387int main(int argc, char *argv[])
6388{
6389 /*
6390 * Init IPRT and globals.
6391 */
6392 int rc = RTTestInitAndCreate("FsPerf", &g_hTest);
6393 if (rc)
6394 return rc;
6395 RTListInit(&g_ManyTreeHead);
6396
6397 /* Query page size, offset mask and page shift of the system. */
6398 g_cbPage = RTSystemGetPageSize();
6399 g_fPageOffset = RTSystemGetPageOffsetMask();
6400 g_cPageShift = RTSystemGetPageShift();
6401
6402 /*
6403 * Default values.
6404 */
6405 char szDefaultDir[RTPATH_MAX];
6406 const char *pszDir = szDefaultDir;
6407
6408 /* As default retrieve the system's temporary directory and create a test directory beneath it,
6409 * as this binary might get executed from a read-only medium such as ${CDROM}. */
6410 rc = RTPathTemp(szDefaultDir, sizeof(szDefaultDir));
6411 if (RT_SUCCESS(rc))
6412 {
6413 char szDirName[32];
6414 RTStrPrintf2(szDirName, sizeof(szDirName), "fstestdir-%u" RTPATH_SLASH_STR, RTProcSelf());
6415 rc = RTPathAppend(szDefaultDir, sizeof(szDefaultDir), szDirName);
6416 if (RT_FAILURE(rc))
6417 {
6418 RTTestFailed(g_hTest, "Unable to append dir name in temp dir, rc=%Rrc\n", rc);
6419 return RTTestSummaryAndDestroy(g_hTest);
6420 }
6421 }
6422 else
6423 {
6424 RTTestFailed(g_hTest, "Unable to retrieve temp dir, rc=%Rrc\n", rc);
6425 return RTTestSummaryAndDestroy(g_hTest);
6426 }
6427
6428 RTTestIPrintf(RTTESTLVL_INFO, "Default directory is: %s\n", szDefaultDir);
6429
6430 bool fCommsSlave = false;
6431
6432 RTGETOPTUNION ValueUnion;
6433 RTGETOPTSTATE GetState;
6434 RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
6435 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
6436 {
6437 switch (rc)
6438 {
6439 case 'c':
6440 if (!g_fRelativeDir)
6441 rc = RTPathAbs(ValueUnion.psz, g_szCommsDir, sizeof(g_szCommsDir) - 128);
6442 else
6443 rc = RTStrCopy(g_szCommsDir, sizeof(g_szCommsDir) - 128, ValueUnion.psz);
6444 if (RT_FAILURE(rc))
6445 {
6446 RTTestFailed(g_hTest, "%s(%s) failed: %Rrc\n", g_fRelativeDir ? "RTStrCopy" : "RTAbsPath", pszDir, rc);
6447 return RTTestSummaryAndDestroy(g_hTest);
6448 }
6449 RTPathEnsureTrailingSeparator(g_szCommsDir, sizeof(g_szCommsDir));
6450 g_cchCommsDir = strlen(g_szCommsDir);
6451
6452 rc = RTPathJoin(g_szCommsSubDir, sizeof(g_szCommsSubDir) - 128, g_szCommsDir, "comms" RTPATH_SLASH_STR);
6453 if (RT_FAILURE(rc))
6454 {
6455 RTTestFailed(g_hTest, "RTPathJoin(%s,,'comms/') failed: %Rrc\n", g_szCommsDir, rc);
6456 return RTTestSummaryAndDestroy(g_hTest);
6457 }
6458 g_cchCommsSubDir = strlen(g_szCommsSubDir);
6459 break;
6460
6461 case 'C':
6462 fCommsSlave = true;
6463 break;
6464
6465 case 'd':
6466 pszDir = ValueUnion.psz;
6467 break;
6468
6469 case 'r':
6470 g_fRelativeDir = true;
6471 break;
6472
6473 case 's':
6474 if (ValueUnion.u32 == 0)
6475 g_nsTestRun = RT_NS_1SEC_64 * 10;
6476 else
6477 g_nsTestRun = ValueUnion.u32 * RT_NS_1SEC_64;
6478 break;
6479
6480 case 'm':
6481 if (ValueUnion.u64 == 0)
6482 g_nsTestRun = RT_NS_1SEC_64 * 10;
6483 else
6484 g_nsTestRun = ValueUnion.u64 * RT_NS_1MS;
6485 break;
6486
6487 case 'e':
6488 g_fManyFiles = true;
6489 g_fOpen = true;
6490 g_fFStat = true;
6491#ifdef RT_OS_WINDOWS
6492 g_fNtQueryInfoFile = true;
6493 g_fNtQueryVolInfoFile = true;
6494#endif
6495 g_fFChMod = true;
6496 g_fFUtimes = true;
6497 g_fStat = true;
6498 g_fChMod = true;
6499 g_fUtimes = true;
6500 g_fRename = true;
6501 g_fDirOpen = true;
6502 g_fDirEnum = true;
6503 g_fMkRmDir = true;
6504 g_fStatVfs = true;
6505 g_fRm = true;
6506 g_fChSize = true;
6507 g_fReadTests = true;
6508 g_fReadPerf = true;
6509#ifdef FSPERF_TEST_SENDFILE
6510 g_fSendFile = true;
6511#endif
6512#ifdef RT_OS_LINUX
6513 g_fSplice = true;
6514#endif
6515 g_fWriteTests = true;
6516 g_fWritePerf = true;
6517 g_fSeek = true;
6518 g_fFSync = true;
6519 g_fMMap = true;
6520 g_fMMapCoherency = true;
6521 g_fCopy = true;
6522 g_fRemote = true;
6523 break;
6524
6525 case 'z':
6526 g_fManyFiles = false;
6527 g_fOpen = false;
6528 g_fFStat = false;
6529#ifdef RT_OS_WINDOWS
6530 g_fNtQueryInfoFile = false;
6531 g_fNtQueryVolInfoFile = false;
6532#endif
6533 g_fFChMod = false;
6534 g_fFUtimes = false;
6535 g_fStat = false;
6536 g_fChMod = false;
6537 g_fUtimes = false;
6538 g_fRename = false;
6539 g_fDirOpen = false;
6540 g_fDirEnum = false;
6541 g_fMkRmDir = false;
6542 g_fStatVfs = false;
6543 g_fRm = false;
6544 g_fChSize = false;
6545 g_fReadTests = false;
6546 g_fReadPerf = false;
6547#ifdef FSPERF_TEST_SENDFILE
6548 g_fSendFile = false;
6549#endif
6550#ifdef RT_OS_LINUX
6551 g_fSplice = false;
6552#endif
6553 g_fWriteTests = false;
6554 g_fWritePerf = false;
6555 g_fSeek = false;
6556 g_fFSync = false;
6557 g_fMMap = false;
6558 g_fMMapCoherency = false;
6559 g_fCopy = false;
6560 g_fRemote = false;
6561 break;
6562
6563#define CASE_OPT(a_Stem) \
6564 case RT_CONCAT(kCmdOpt_,a_Stem): RT_CONCAT(g_f,a_Stem) = true; break; \
6565 case RT_CONCAT(kCmdOpt_No,a_Stem): RT_CONCAT(g_f,a_Stem) = false; break
6566 CASE_OPT(Open);
6567 CASE_OPT(FStat);
6568#ifdef RT_OS_WINDOWS
6569 CASE_OPT(NtQueryInfoFile);
6570 CASE_OPT(NtQueryVolInfoFile);
6571#endif
6572 CASE_OPT(FChMod);
6573 CASE_OPT(FUtimes);
6574 CASE_OPT(Stat);
6575 CASE_OPT(ChMod);
6576 CASE_OPT(Utimes);
6577 CASE_OPT(Rename);
6578 CASE_OPT(DirOpen);
6579 CASE_OPT(DirEnum);
6580 CASE_OPT(MkRmDir);
6581 CASE_OPT(StatVfs);
6582 CASE_OPT(Rm);
6583 CASE_OPT(ChSize);
6584 CASE_OPT(ReadTests);
6585 CASE_OPT(ReadPerf);
6586#ifdef FSPERF_TEST_SENDFILE
6587 CASE_OPT(SendFile);
6588#endif
6589#ifdef RT_OS_LINUX
6590 CASE_OPT(Splice);
6591#endif
6592 CASE_OPT(WriteTests);
6593 CASE_OPT(WritePerf);
6594 CASE_OPT(Seek);
6595 CASE_OPT(FSync);
6596 CASE_OPT(MMap);
6597 CASE_OPT(MMapCoherency);
6598 CASE_OPT(IgnoreNoCache);
6599 CASE_OPT(Copy);
6600 CASE_OPT(Remote);
6601
6602 CASE_OPT(ShowDuration);
6603 CASE_OPT(ShowIterations);
6604#undef CASE_OPT
6605
6606 case kCmdOpt_ManyFiles:
6607 g_fManyFiles = ValueUnion.u32 > 0;
6608 g_cManyFiles = ValueUnion.u32;
6609 break;
6610
6611 case kCmdOpt_NoManyFiles:
6612 g_fManyFiles = false;
6613 break;
6614
6615 case kCmdOpt_ManyTreeFilesPerDir:
6616 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= _64M)
6617 {
6618 g_cManyTreeFilesPerDir = ValueUnion.u32;
6619 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
6620 break;
6621 }
6622 RTTestFailed(g_hTest, "Out of range --files-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6623 return RTTestSummaryAndDestroy(g_hTest);
6624
6625 case kCmdOpt_ManyTreeSubdirsPerDir:
6626 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= 1024)
6627 {
6628 g_cManyTreeSubdirsPerDir = ValueUnion.u32;
6629 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
6630 break;
6631 }
6632 RTTestFailed(g_hTest, "Out of range --subdirs-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6633 return RTTestSummaryAndDestroy(g_hTest);
6634
6635 case kCmdOpt_ManyTreeDepth:
6636 if (ValueUnion.u32 <= 8)
6637 {
6638 g_cManyTreeDepth = ValueUnion.u32;
6639 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
6640 break;
6641 }
6642 RTTestFailed(g_hTest, "Out of range --tree-depth value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6643 return RTTestSummaryAndDestroy(g_hTest);
6644
6645 case kCmdOpt_MaxBufferSize:
6646 if (ValueUnion.u32 >= 4096)
6647 g_cbMaxBuffer = ValueUnion.u32;
6648 else if (ValueUnion.u32 == 0)
6649 g_cbMaxBuffer = UINT32_MAX;
6650 else
6651 {
6652 RTTestFailed(g_hTest, "max buffer size is less than 4KB: %#x\n", ValueUnion.u32);
6653 return RTTestSummaryAndDestroy(g_hTest);
6654 }
6655 break;
6656
6657 case kCmdOpt_IoFileSize:
6658 if (ValueUnion.u64 == 0)
6659 g_cbIoFile = _512M;
6660 else
6661 g_cbIoFile = ValueUnion.u64;
6662 break;
6663
6664 case kCmdOpt_SetBlockSize:
6665 if (ValueUnion.u32 > 0)
6666 {
6667 g_cIoBlocks = 1;
6668 g_acbIoBlocks[0] = ValueUnion.u32;
6669 }
6670 else
6671 {
6672 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6673 return RTTestSummaryAndDestroy(g_hTest);
6674 }
6675 break;
6676
6677 case kCmdOpt_AddBlockSize:
6678 if (g_cIoBlocks >= RT_ELEMENTS(g_acbIoBlocks))
6679 RTTestFailed(g_hTest, "Too many I/O block sizes: max %u\n", RT_ELEMENTS(g_acbIoBlocks));
6680 else if (ValueUnion.u32 == 0)
6681 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
6682 else
6683 {
6684 g_acbIoBlocks[g_cIoBlocks++] = ValueUnion.u32;
6685 break;
6686 }
6687 return RTTestSummaryAndDestroy(g_hTest);
6688
6689 case kCmdOpt_MMapPlacement:
6690 if (strcmp(ValueUnion.psz, "first") == 0)
6691 g_iMMapPlacement = -1;
6692 else if ( strcmp(ValueUnion.psz, "between") == 0
6693 || strcmp(ValueUnion.psz, "default") == 0)
6694 g_iMMapPlacement = 0;
6695 else if (strcmp(ValueUnion.psz, "last") == 0)
6696 g_iMMapPlacement = 1;
6697 else
6698 {
6699 RTTestFailed(g_hTest,
6700 "Invalid --mmap-placment directive '%s'! Expected 'first', 'last', 'between' or 'default'.\n",
6701 ValueUnion.psz);
6702 return RTTestSummaryAndDestroy(g_hTest);
6703 }
6704 break;
6705
6706 case 'q':
6707 g_uVerbosity = 0;
6708 break;
6709
6710 case 'v':
6711 g_uVerbosity++;
6712 break;
6713
6714 case 'h':
6715 Usage(g_pStdOut);
6716 return RTEXITCODE_SUCCESS;
6717
6718 case 'V':
6719 {
6720 char szRev[] = "$Revision: 103423 $";
6721 szRev[RT_ELEMENTS(szRev) - 2] = '\0';
6722 RTPrintf(RTStrStrip(strchr(szRev, ':') + 1));
6723 return RTEXITCODE_SUCCESS;
6724 }
6725
6726 default:
6727 return RTGetOptPrintError(rc, &ValueUnion);
6728 }
6729 }
6730
6731 /*
6732 * Populate g_szDir.
6733 */
6734 if (!g_fRelativeDir)
6735 rc = RTPathAbs(pszDir, g_szDir, sizeof(g_szDir) - FSPERF_MAX_NEEDED_PATH);
6736 else
6737 rc = RTStrCopy(g_szDir, sizeof(g_szDir) - FSPERF_MAX_NEEDED_PATH, pszDir);
6738 if (RT_FAILURE(rc))
6739 {
6740 RTTestFailed(g_hTest, "%s(%s) failed: %Rrc\n", g_fRelativeDir ? "RTStrCopy" : "RTAbsPath", pszDir, rc);
6741 return RTTestSummaryAndDestroy(g_hTest);
6742 }
6743 RTPathEnsureTrailingSeparator(g_szDir, sizeof(g_szDir));
6744 g_cchDir = strlen(g_szDir);
6745
6746 /*
6747 * If communication slave, go do that and be done.
6748 */
6749 if (fCommsSlave)
6750 {
6751 if (pszDir == szDefaultDir)
6752 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The slave must have a working directory specified (-d)!");
6753 return FsPerfCommsSlave();
6754 }
6755
6756 /*
6757 * Create the test directory with an 'empty' subdirectory under it,
6758 * execute the tests, and remove directory when done.
6759 */
6760 RTTestBanner(g_hTest);
6761 if (!RTPathExists(g_szDir))
6762 {
6763 /* The base dir: */
6764 rc = RTDirCreate(g_szDir, 0755,
6765 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
6766 if (RT_SUCCESS(rc))
6767 {
6768 RTTestIPrintf(RTTESTLVL_ALWAYS, "Test dir: %s\n", g_szDir);
6769 rc = fsPrepTestArea();
6770 if (RT_SUCCESS(rc))
6771 {
6772 /* Profile RTTimeNanoTS(). */
6773 fsPerfNanoTS();
6774
6775 /* Do tests: */
6776 if (g_fManyFiles)
6777 fsPerfManyFiles();
6778 if (g_fOpen)
6779 fsPerfOpen();
6780 if (g_fFStat)
6781 fsPerfFStat();
6782#ifdef RT_OS_WINDOWS
6783 if (g_fNtQueryInfoFile)
6784 fsPerfNtQueryInfoFile();
6785 if (g_fNtQueryVolInfoFile)
6786 fsPerfNtQueryVolInfoFile();
6787#endif
6788 if (g_fFChMod)
6789 fsPerfFChMod();
6790 if (g_fFUtimes)
6791 fsPerfFUtimes();
6792 if (g_fStat)
6793 fsPerfStat();
6794 if (g_fChMod)
6795 fsPerfChmod();
6796 if (g_fUtimes)
6797 fsPerfUtimes();
6798 if (g_fRename)
6799 fsPerfRename();
6800 if (g_fDirOpen)
6801 vsPerfDirOpen();
6802 if (g_fDirEnum)
6803 vsPerfDirEnum();
6804 if (g_fMkRmDir)
6805 fsPerfMkRmDir();
6806 if (g_fStatVfs)
6807 fsPerfStatVfs();
6808 if (g_fRm || g_fManyFiles)
6809 fsPerfRm(); /* deletes manyfiles and manytree */
6810 if (g_fChSize)
6811 fsPerfChSize();
6812 if ( g_fReadPerf || g_fReadTests || g_fWritePerf || g_fWriteTests
6813#ifdef FSPERF_TEST_SENDFILE
6814 || g_fSendFile
6815#endif
6816#ifdef RT_OS_LINUX
6817 || g_fSplice
6818#endif
6819 || g_fSeek || g_fFSync || g_fMMap)
6820 fsPerfIo();
6821 if (g_fCopy)
6822 fsPerfCopy();
6823 if (g_fRemote && g_szCommsDir[0] != '\0')
6824 fsPerfRemote();
6825 }
6826
6827 /*
6828 * Cleanup:
6829 */
6830 FsPerfCommsShutdownSlave();
6831
6832 g_szDir[g_cchDir] = '\0';
6833 rc = RTDirRemoveRecursive(g_szDir, RTDIRRMREC_F_CONTENT_AND_DIR | (g_fRelativeDir ? RTDIRRMREC_F_NO_ABS_PATH : 0));
6834 if (RT_FAILURE(rc))
6835 RTTestFailed(g_hTest, "RTDirRemoveRecursive(%s,) -> %Rrc\n", g_szDir, rc);
6836 }
6837 else
6838 RTTestFailed(g_hTest, "RTDirCreate(%s) -> %Rrc\n", g_szDir, rc);
6839 }
6840 else
6841 RTTestFailed(g_hTest, "Test directory already exists: %s\n", g_szDir);
6842
6843 FsPerfCommsShutdownSlave();
6844
6845 return RTTestSummaryAndDestroy(g_hTest);
6846}
6847
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