VirtualBox

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

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

FsPerf: Another w7rtm adjustment. bugref:9172

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

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