VirtualBox

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

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

ValidationKit: Fix some unused expression warnings, bugref:3409

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

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