VirtualBox

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

Last change on this file since 80908 was 80908, checked in by vboxsync, 6 years ago

FsPerf: Better error reporting on that windows flush problem. bugref:9172

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

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