VirtualBox

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

Last change on this file since 82878 was 80915, checked in by vboxsync, 5 years ago

FsPerf: Even better error reporting on that windows flush problem. [fix] bugref:9172

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