VirtualBox

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

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

FsPerf: Try get more info on GA test failure issue. bugref:9172

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

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