VirtualBox

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

Last change on this file since 98412 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

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

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