VirtualBox

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

Last change on this file since 88839 was 84912, checked in by vboxsync, 5 years ago

FsPerf: Off by one fix.

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