VirtualBox

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

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

FsPerf: Skip block sizes that exceeds the file size. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 187.7 KB
Line 
1/* $Id: FsPerf.cpp 77969 2019-04-01 01:20: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/** @def FSPERF_TEST_SENDFILE
91 * Whether to enable the sendfile() tests. */
92#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
93# define FSPERF_TEST_SENDFILE
94#endif
95
96/**
97 * Macro for profiling @a a_fnCall (typically forced inline) for about @a a_cNsTarget ns.
98 *
99 * Always does an even number of iterations.
100 */
101#define PROFILE_FN(a_fnCall, a_cNsTarget, a_szDesc) \
102 do { \
103 /* Estimate how many iterations we need to fill up the given timeslot: */ \
104 fsPerfYield(); \
105 uint64_t nsStart = RTTimeNanoTS(); \
106 uint64_t nsPrf; \
107 do \
108 nsPrf = RTTimeNanoTS(); \
109 while (nsPrf == nsStart); \
110 nsStart = nsPrf; \
111 \
112 uint64_t iIteration = 0; \
113 do \
114 { \
115 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
116 iIteration++; \
117 nsPrf = RTTimeNanoTS() - nsStart; \
118 } while (nsPrf < RT_NS_10MS || (iIteration & 1)); \
119 nsPrf /= iIteration; \
120 if (nsPrf > g_nsPerNanoTSCall + 32) \
121 nsPrf -= g_nsPerNanoTSCall; \
122 \
123 uint64_t cIterations = (a_cNsTarget) / nsPrf; \
124 if (cIterations <= 1) \
125 cIterations = 2; \
126 else if (cIterations & 1) \
127 cIterations++; \
128 \
129 /* Do the actual profiling: */ \
130 fsPerfYield(); \
131 iIteration = 0; \
132 nsStart = RTTimeNanoTS(); \
133 for (; iIteration < cIterations; iIteration++) \
134 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
135 nsPrf = RTTimeNanoTS() - nsStart; \
136 RTTestIValue(a_szDesc, nsPrf / cIterations, RTTESTUNIT_NS_PER_OCCURRENCE); \
137 if (g_fShowDuration) \
138 RTTestIValueF(nsPrf, RTTESTUNIT_NS, "%s duration", a_szDesc); \
139 if (g_fShowIterations) \
140 RTTestIValueF(iIteration, RTTESTUNIT_OCCURRENCES, "%s iterations", a_szDesc); \
141 } while (0)
142
143
144/**
145 * Macro for profiling an operation on each file in the manytree directory tree.
146 *
147 * Always does an even number of tree iterations.
148 */
149#define PROFILE_MANYTREE_FN(a_szPath, a_fnCall, a_cEstimationIterations, a_cNsTarget, a_szDesc) \
150 do { \
151 if (!g_fManyFiles) \
152 break; \
153 \
154 /* Estimate how many iterations we need to fill up the given timeslot: */ \
155 fsPerfYield(); \
156 uint64_t nsStart = RTTimeNanoTS(); \
157 uint64_t ns; \
158 do \
159 ns = RTTimeNanoTS(); \
160 while (ns == nsStart); \
161 nsStart = ns; \
162 \
163 PFSPERFNAMEENTRY pCur; \
164 uint64_t iIteration = 0; \
165 do \
166 { \
167 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
168 { \
169 memcpy(a_szPath, pCur->szName, pCur->cchName); \
170 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
171 { \
172 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
173 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
174 } \
175 } \
176 iIteration++; \
177 ns = RTTimeNanoTS() - nsStart; \
178 } while (ns < RT_NS_10MS || (iIteration & 1)); \
179 ns /= iIteration; \
180 if (ns > g_nsPerNanoTSCall + 32) \
181 ns -= g_nsPerNanoTSCall; \
182 \
183 uint32_t cIterations = (a_cNsTarget) / ns; \
184 if (cIterations <= 1) \
185 cIterations = 2; \
186 else if (cIterations & 1) \
187 cIterations++; \
188 \
189 /* Do the actual profiling: */ \
190 fsPerfYield(); \
191 uint32_t cCalls = 0; \
192 nsStart = RTTimeNanoTS(); \
193 for (iIteration = 0; iIteration < cIterations; iIteration++) \
194 { \
195 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
196 { \
197 memcpy(a_szPath, pCur->szName, pCur->cchName); \
198 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
199 { \
200 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
201 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
202 cCalls++; \
203 } \
204 } \
205 } \
206 ns = RTTimeNanoTS() - nsStart; \
207 RTTestIValueF(ns / cCalls, RTTESTUNIT_NS_PER_OCCURRENCE, a_szDesc); \
208 if (g_fShowDuration) \
209 RTTestIValueF(ns, RTTESTUNIT_NS, "%s duration", a_szDesc); \
210 if (g_fShowIterations) \
211 RTTestIValueF(iIteration, RTTESTUNIT_OCCURRENCES, "%s iterations", a_szDesc); \
212 } while (0)
213
214
215/**
216 * Execute a_fnCall for each file in the manytree.
217 */
218#define DO_MANYTREE_FN(a_szPath, a_fnCall) \
219 do { \
220 PFSPERFNAMEENTRY pCur; \
221 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
222 { \
223 memcpy(a_szPath, pCur->szName, pCur->cchName); \
224 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
225 { \
226 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
227 a_fnCall; \
228 } \
229 } \
230 } while (0)
231
232
233/** @def FSPERF_VERR_PATH_NOT_FOUND
234 * Hides the fact that we only get VERR_PATH_NOT_FOUND on non-unix systems. */
235#if defined(RT_OS_WINDOWS) //|| defined(RT_OS_OS2) - using posix APIs IIRC, so lost in translation.
236# define FSPERF_VERR_PATH_NOT_FOUND VERR_PATH_NOT_FOUND
237#else
238# define FSPERF_VERR_PATH_NOT_FOUND VERR_FILE_NOT_FOUND
239#endif
240
241
242/*********************************************************************************************************************************
243* Structures and Typedefs *
244*********************************************************************************************************************************/
245typedef struct FSPERFNAMEENTRY
246{
247 RTLISTNODE Entry;
248 uint16_t cchName;
249 char szName[RT_FLEXIBLE_ARRAY];
250} FSPERFNAMEENTRY;
251typedef FSPERFNAMEENTRY *PFSPERFNAMEENTRY;
252
253
254enum
255{
256 kCmdOpt_First = 128,
257
258 kCmdOpt_ManyFiles = kCmdOpt_First,
259 kCmdOpt_NoManyFiles,
260 kCmdOpt_Open,
261 kCmdOpt_NoOpen,
262 kCmdOpt_FStat,
263 kCmdOpt_NoFStat,
264 kCmdOpt_FChMod,
265 kCmdOpt_NoFChMod,
266 kCmdOpt_FUtimes,
267 kCmdOpt_NoFUtimes,
268 kCmdOpt_Stat,
269 kCmdOpt_NoStat,
270 kCmdOpt_ChMod,
271 kCmdOpt_NoChMod,
272 kCmdOpt_Utimes,
273 kCmdOpt_NoUtimes,
274 kCmdOpt_Rename,
275 kCmdOpt_NoRename,
276 kCmdOpt_DirOpen,
277 kCmdOpt_NoDirOpen,
278 kCmdOpt_DirEnum,
279 kCmdOpt_NoDirEnum,
280 kCmdOpt_MkRmDir,
281 kCmdOpt_NoMkRmDir,
282 kCmdOpt_StatVfs,
283 kCmdOpt_NoStatVfs,
284 kCmdOpt_Rm,
285 kCmdOpt_NoRm,
286 kCmdOpt_ChSize,
287 kCmdOpt_NoChSize,
288 kCmdOpt_ReadPerf,
289 kCmdOpt_NoReadPerf,
290 kCmdOpt_ReadTests,
291 kCmdOpt_NoReadTests,
292#ifdef FSPERF_TEST_SENDFILE
293 kCmdOpt_SendFile,
294 kCmdOpt_NoSendFile,
295#endif
296#ifdef RT_OS_LINUX
297 kCmdOpt_Splice,
298 kCmdOpt_NoSplice,
299#endif
300 kCmdOpt_WritePerf,
301 kCmdOpt_NoWritePerf,
302 kCmdOpt_WriteTests,
303 kCmdOpt_NoWriteTests,
304 kCmdOpt_Seek,
305 kCmdOpt_NoSeek,
306 kCmdOpt_FSync,
307 kCmdOpt_NoFSync,
308 kCmdOpt_MMap,
309 kCmdOpt_NoMMap,
310 kCmdOpt_IgnoreNoCache,
311 kCmdOpt_NoIgnoreNoCache,
312 kCmdOpt_IoFileSize,
313 kCmdOpt_SetBlockSize,
314 kCmdOpt_AddBlockSize,
315 kCmdOpt_Copy,
316 kCmdOpt_NoCopy,
317
318 kCmdOpt_ShowDuration,
319 kCmdOpt_NoShowDuration,
320 kCmdOpt_ShowIterations,
321 kCmdOpt_NoShowIterations,
322
323 kCmdOpt_ManyTreeFilesPerDir,
324 kCmdOpt_ManyTreeSubdirsPerDir,
325 kCmdOpt_ManyTreeDepth,
326
327 kCmdOpt_End
328};
329
330
331/*********************************************************************************************************************************
332* Global Variables *
333*********************************************************************************************************************************/
334/** Command line parameters */
335static const RTGETOPTDEF g_aCmdOptions[] =
336{
337 { "--dir", 'd', RTGETOPT_REQ_STRING },
338 { "--seconds", 's', RTGETOPT_REQ_UINT32 },
339 { "--milliseconds", 'm', RTGETOPT_REQ_UINT64 },
340
341 { "--enable-all", 'e', RTGETOPT_REQ_NOTHING },
342 { "--disable-all", 'z', RTGETOPT_REQ_NOTHING },
343
344 { "--many-files", kCmdOpt_ManyFiles, RTGETOPT_REQ_UINT32 },
345 { "--no-many-files", kCmdOpt_NoManyFiles, RTGETOPT_REQ_NOTHING },
346 { "--files-per-dir", kCmdOpt_ManyTreeFilesPerDir, RTGETOPT_REQ_UINT32 },
347 { "--subdirs-per-dir", kCmdOpt_ManyTreeSubdirsPerDir, RTGETOPT_REQ_UINT32 },
348 { "--tree-depth", kCmdOpt_ManyTreeDepth, RTGETOPT_REQ_UINT32 },
349
350 { "--open", kCmdOpt_Open, RTGETOPT_REQ_NOTHING },
351 { "--no-open", kCmdOpt_NoOpen, RTGETOPT_REQ_NOTHING },
352 { "--fstat", kCmdOpt_FStat, RTGETOPT_REQ_NOTHING },
353 { "--no-fstat", kCmdOpt_NoFStat, RTGETOPT_REQ_NOTHING },
354 { "--fchmod", kCmdOpt_FChMod, RTGETOPT_REQ_NOTHING },
355 { "--no-fchmod", kCmdOpt_NoFChMod, RTGETOPT_REQ_NOTHING },
356 { "--futimes", kCmdOpt_FUtimes, RTGETOPT_REQ_NOTHING },
357 { "--no-futimes", kCmdOpt_NoFUtimes, RTGETOPT_REQ_NOTHING },
358 { "--stat", kCmdOpt_Stat, RTGETOPT_REQ_NOTHING },
359 { "--no-stat", kCmdOpt_NoStat, RTGETOPT_REQ_NOTHING },
360 { "--chmod", kCmdOpt_ChMod, RTGETOPT_REQ_NOTHING },
361 { "--no-chmod", kCmdOpt_NoChMod, RTGETOPT_REQ_NOTHING },
362 { "--utimes", kCmdOpt_Utimes, RTGETOPT_REQ_NOTHING },
363 { "--no-utimes", kCmdOpt_NoUtimes, RTGETOPT_REQ_NOTHING },
364 { "--rename", kCmdOpt_Rename, RTGETOPT_REQ_NOTHING },
365 { "--no-rename", kCmdOpt_NoRename, RTGETOPT_REQ_NOTHING },
366 { "--dir-open", kCmdOpt_DirOpen, RTGETOPT_REQ_NOTHING },
367 { "--no-dir-open", kCmdOpt_NoDirOpen, RTGETOPT_REQ_NOTHING },
368 { "--dir-enum", kCmdOpt_DirEnum, RTGETOPT_REQ_NOTHING },
369 { "--no-dir-enum", kCmdOpt_NoDirEnum, RTGETOPT_REQ_NOTHING },
370 { "--mk-rm-dir", kCmdOpt_MkRmDir, RTGETOPT_REQ_NOTHING },
371 { "--no-mk-rm-dir", kCmdOpt_NoMkRmDir, RTGETOPT_REQ_NOTHING },
372 { "--stat-vfs", kCmdOpt_StatVfs, RTGETOPT_REQ_NOTHING },
373 { "--no-stat-vfs", kCmdOpt_NoStatVfs, RTGETOPT_REQ_NOTHING },
374 { "--rm", kCmdOpt_Rm, RTGETOPT_REQ_NOTHING },
375 { "--no-rm", kCmdOpt_NoRm, RTGETOPT_REQ_NOTHING },
376 { "--chsize", kCmdOpt_ChSize, RTGETOPT_REQ_NOTHING },
377 { "--no-chsize", kCmdOpt_NoChSize, RTGETOPT_REQ_NOTHING },
378 { "--read-tests", kCmdOpt_ReadTests, RTGETOPT_REQ_NOTHING },
379 { "--no-read-tests", kCmdOpt_NoReadTests, RTGETOPT_REQ_NOTHING },
380 { "--read-perf", kCmdOpt_ReadPerf, RTGETOPT_REQ_NOTHING },
381 { "--no-read-perf", kCmdOpt_NoReadPerf, RTGETOPT_REQ_NOTHING },
382#ifdef FSPERF_TEST_SENDFILE
383 { "--sendfile", kCmdOpt_SendFile, RTGETOPT_REQ_NOTHING },
384 { "--no-sendfile", kCmdOpt_NoSendFile, RTGETOPT_REQ_NOTHING },
385#endif
386#ifdef RT_OS_LINUX
387 { "--splice", kCmdOpt_Splice, RTGETOPT_REQ_NOTHING },
388 { "--no-splice", kCmdOpt_NoSplice, RTGETOPT_REQ_NOTHING },
389#endif
390 { "--write-tests", kCmdOpt_WriteTests, RTGETOPT_REQ_NOTHING },
391 { "--no-write-tests", kCmdOpt_NoWriteTests, RTGETOPT_REQ_NOTHING },
392 { "--write-perf", kCmdOpt_WritePerf, RTGETOPT_REQ_NOTHING },
393 { "--no-write-perf", kCmdOpt_NoWritePerf, RTGETOPT_REQ_NOTHING },
394 { "--seek", kCmdOpt_Seek, RTGETOPT_REQ_NOTHING },
395 { "--no-seek", kCmdOpt_NoSeek, RTGETOPT_REQ_NOTHING },
396 { "--fsync", kCmdOpt_FSync, RTGETOPT_REQ_NOTHING },
397 { "--no-fsync", kCmdOpt_NoFSync, RTGETOPT_REQ_NOTHING },
398 { "--mmap", kCmdOpt_MMap, RTGETOPT_REQ_NOTHING },
399 { "--no-mmap", kCmdOpt_NoMMap, RTGETOPT_REQ_NOTHING },
400 { "--ignore-no-cache", kCmdOpt_IgnoreNoCache, RTGETOPT_REQ_NOTHING },
401 { "--no-ignore-no-cache", kCmdOpt_NoIgnoreNoCache, RTGETOPT_REQ_NOTHING },
402 { "--io-file-size", kCmdOpt_IoFileSize, RTGETOPT_REQ_UINT64 },
403 { "--set-block-size", kCmdOpt_SetBlockSize, RTGETOPT_REQ_UINT32 },
404 { "--add-block-size", kCmdOpt_AddBlockSize, RTGETOPT_REQ_UINT32 },
405 { "--copy", kCmdOpt_Copy, RTGETOPT_REQ_NOTHING },
406 { "--no-copy", kCmdOpt_NoCopy, RTGETOPT_REQ_NOTHING },
407
408 { "--show-duration", kCmdOpt_ShowDuration, RTGETOPT_REQ_NOTHING },
409 { "--no-show-duration", kCmdOpt_NoShowDuration, RTGETOPT_REQ_NOTHING },
410 { "--show-iterations", kCmdOpt_ShowIterations, RTGETOPT_REQ_NOTHING },
411 { "--no-show-iterations", kCmdOpt_NoShowIterations, RTGETOPT_REQ_NOTHING },
412
413 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
414 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
415 { "--version", 'V', RTGETOPT_REQ_NOTHING },
416 { "--help", 'h', RTGETOPT_REQ_NOTHING } /* for Usage() */
417};
418
419/** The test handle. */
420static RTTEST g_hTest;
421/** The number of nanoseconds a RTTimeNanoTS call takes.
422 * This is used for adjusting loop count estimates. */
423static uint64_t g_nsPerNanoTSCall = 1;
424/** Whether or not to display the duration of each profile run.
425 * This is chiefly for verify the estimate phase. */
426static bool g_fShowDuration = false;
427/** Whether or not to display the iteration count for each profile run.
428 * This is chiefly for verify the estimate phase. */
429static bool g_fShowIterations = false;
430/** Verbosity level. */
431static uint32_t g_uVerbosity = 0;
432
433/** @name Selected subtest
434 * @{ */
435static bool g_fManyFiles = true;
436static bool g_fOpen = true;
437static bool g_fFStat = true;
438static bool g_fFChMod = true;
439static bool g_fFUtimes = true;
440static bool g_fStat = true;
441static bool g_fChMod = true;
442static bool g_fUtimes = true;
443static bool g_fRename = true;
444static bool g_fDirOpen = true;
445static bool g_fDirEnum = true;
446static bool g_fMkRmDir = true;
447static bool g_fStatVfs = true;
448static bool g_fRm = true;
449static bool g_fChSize = true;
450static bool g_fReadTests = true;
451static bool g_fReadPerf = true;
452#ifdef FSPERF_TEST_SENDFILE
453static bool g_fSendFile = true;
454#endif
455#ifdef RT_OS_LINUX
456static bool g_fSplice = true;
457#endif
458static bool g_fWriteTests= true;
459static bool g_fWritePerf = true;
460static bool g_fSeek = true;
461static bool g_fFSync = true;
462static bool g_fMMap = true;
463static bool g_fCopy = true;
464/** @} */
465
466/** The length of each test run. */
467static uint64_t g_nsTestRun = RT_NS_1SEC_64 * 10;
468
469/** For the 'manyfiles' subdir. */
470static uint32_t g_cManyFiles = 10000;
471
472/** Number of files in the 'manytree' directory tree. */
473static uint32_t g_cManyTreeFiles = 640 + 16*640 /*10880*/;
474/** Number of files per directory in the 'manytree' construct. */
475static uint32_t g_cManyTreeFilesPerDir = 640;
476/** Number of subdirs per directory in the 'manytree' construct. */
477static uint32_t g_cManyTreeSubdirsPerDir = 16;
478/** The depth of the 'manytree' directory tree. */
479static uint32_t g_cManyTreeDepth = 1;
480/** List of directories in the many tree, creation order. */
481static RTLISTANCHOR g_ManyTreeHead;
482
483/** Number of configured I/O block sizes. */
484static uint32_t g_cIoBlocks = 8;
485/** Configured I/O block sizes. */
486static uint32_t g_acbIoBlocks[16] = { 1, 512, 4096, 16384, 65536, _1M, _32M, _128M };
487/** The desired size of the test file we use for I/O. */
488static uint64_t g_cbIoFile = _512M;
489/** Whether to be less strict with non-cache file handle. */
490static bool g_fIgnoreNoCache = false;
491
492/** The length of g_szDir. */
493static size_t g_cchDir;
494/** The length of g_szEmptyDir. */
495static size_t g_cchEmptyDir;
496/** The length of g_szDeepDir. */
497static size_t g_cchDeepDir;
498
499/** The test directory (absolute). This will always have a trailing slash. */
500static char g_szDir[RTPATH_MAX];
501/** The test directory (absolute), 2nd copy for use with InDir2(). */
502static char g_szDir2[RTPATH_MAX];
503/** The empty test directory (absolute). This will always have a trailing slash. */
504static char g_szEmptyDir[RTPATH_MAX];
505/** The deep test directory (absolute). This will always have a trailing slash. */
506static char g_szDeepDir[RTPATH_MAX];
507
508
509/**
510 * Yield the CPU and stuff before starting a test run.
511 */
512DECLINLINE(void) fsPerfYield(void)
513{
514 RTThreadYield();
515 RTThreadYield();
516}
517
518
519/**
520 * Profiles the RTTimeNanoTS call, setting g_nsPerNanoTSCall.
521 */
522static void fsPerfNanoTS(void)
523{
524 fsPerfYield();
525
526 /* Make sure we start off on a changing timestamp on platforms will low time resoultion. */
527 uint64_t nsStart = RTTimeNanoTS();
528 uint64_t ns;
529 do
530 ns = RTTimeNanoTS();
531 while (ns == nsStart);
532 nsStart = ns;
533
534 /* Call it for 10 ms. */
535 uint32_t i = 0;
536 do
537 {
538 i++;
539 ns = RTTimeNanoTS();
540 }
541 while (ns - nsStart < RT_NS_10MS);
542
543 g_nsPerNanoTSCall = (ns - nsStart) / i;
544}
545
546
547/**
548 * Construct a path relative to the base test directory.
549 *
550 * @returns g_szDir.
551 * @param pszAppend What to append.
552 * @param cchAppend How much to append.
553 */
554DECLINLINE(char *) InDir(const char *pszAppend, size_t cchAppend)
555{
556 Assert(g_szDir[g_cchDir - 1] == RTPATH_SLASH);
557 memcpy(&g_szDir[g_cchDir], pszAppend, cchAppend);
558 g_szDir[g_cchDir + cchAppend] = '\0';
559 return &g_szDir[0];
560}
561
562
563/**
564 * Construct a path relative to the base test directory, 2nd copy.
565 *
566 * @returns g_szDir2.
567 * @param pszAppend What to append.
568 * @param cchAppend How much to append.
569 */
570DECLINLINE(char *) InDir2(const char *pszAppend, size_t cchAppend)
571{
572 Assert(g_szDir[g_cchDir - 1] == RTPATH_SLASH);
573 memcpy(g_szDir2, g_szDir, g_cchDir);
574 memcpy(&g_szDir2[g_cchDir], pszAppend, cchAppend);
575 g_szDir2[g_cchDir + cchAppend] = '\0';
576 return &g_szDir2[0];
577}
578
579
580/**
581 * Construct a path relative to the empty directory.
582 *
583 * @returns g_szEmptyDir.
584 * @param pszAppend What to append.
585 * @param cchAppend How much to append.
586 */
587DECLINLINE(char *) InEmptyDir(const char *pszAppend, size_t cchAppend)
588{
589 Assert(g_szEmptyDir[g_cchEmptyDir - 1] == RTPATH_SLASH);
590 memcpy(&g_szEmptyDir[g_cchEmptyDir], pszAppend, cchAppend);
591 g_szEmptyDir[g_cchEmptyDir + cchAppend] = '\0';
592 return &g_szEmptyDir[0];
593}
594
595
596/**
597 * Construct a path relative to the deep test directory.
598 *
599 * @returns g_szDeepDir.
600 * @param pszAppend What to append.
601 * @param cchAppend How much to append.
602 */
603DECLINLINE(char *) InDeepDir(const char *pszAppend, size_t cchAppend)
604{
605 Assert(g_szDeepDir[g_cchDeepDir - 1] == RTPATH_SLASH);
606 memcpy(&g_szDeepDir[g_cchDeepDir], pszAppend, cchAppend);
607 g_szDeepDir[g_cchDeepDir + cchAppend] = '\0';
608 return &g_szDeepDir[0];
609}
610
611
612/**
613 * Prepares the test area.
614 * @returns VBox status code.
615 */
616static int fsPrepTestArea(void)
617{
618 /* The empty subdir and associated globals: */
619 static char s_szEmpty[] = "empty";
620 memcpy(g_szEmptyDir, g_szDir, g_cchDir);
621 memcpy(&g_szEmptyDir[g_cchDir], s_szEmpty, sizeof(s_szEmpty));
622 g_cchEmptyDir = g_cchDir + sizeof(s_szEmpty) - 1;
623 RTTESTI_CHECK_RC_RET(RTDirCreate(g_szEmptyDir, 0755, 0), VINF_SUCCESS, rcCheck);
624 g_szEmptyDir[g_cchEmptyDir++] = RTPATH_SLASH;
625 g_szEmptyDir[g_cchEmptyDir] = '\0';
626 RTTestIPrintf(RTTESTLVL_ALWAYS, "Empty dir: %s\n", g_szEmptyDir);
627
628 /* Deep directory: */
629 memcpy(g_szDeepDir, g_szDir, g_cchDir);
630 g_cchDeepDir = g_cchDir;
631 do
632 {
633 static char const s_szSub[] = "d" RTPATH_SLASH_STR;
634 memcpy(&g_szDeepDir[g_cchDeepDir], s_szSub, sizeof(s_szSub));
635 g_cchDeepDir += sizeof(s_szSub) - 1;
636 RTTESTI_CHECK_RC_RET( RTDirCreate(g_szDeepDir, 0755, 0), VINF_SUCCESS, rcCheck);
637 } while (g_cchDeepDir < 176);
638 RTTestIPrintf(RTTESTLVL_ALWAYS, "Deep dir: %s\n", g_szDeepDir);
639
640 /* Create known file in both deep and shallow dirs: */
641 RTFILE hKnownFile;
642 RTTESTI_CHECK_RC_RET(RTFileOpen(&hKnownFile, InDir(RT_STR_TUPLE("known-file")),
643 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE),
644 VINF_SUCCESS, rcCheck);
645 RTTESTI_CHECK_RC_RET(RTFileClose(hKnownFile), VINF_SUCCESS, rcCheck);
646
647 RTTESTI_CHECK_RC_RET(RTFileOpen(&hKnownFile, InDeepDir(RT_STR_TUPLE("known-file")),
648 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE),
649 VINF_SUCCESS, rcCheck);
650 RTTESTI_CHECK_RC_RET(RTFileClose(hKnownFile), VINF_SUCCESS, rcCheck);
651
652 return VINF_SUCCESS;
653}
654
655
656/**
657 * Create a name list entry.
658 * @returns Pointer to the entry, NULL if out of memory.
659 * @param pchName The name.
660 * @param cchName The name length.
661 */
662PFSPERFNAMEENTRY fsPerfCreateNameEntry(const char *pchName, size_t cchName)
663{
664 PFSPERFNAMEENTRY pEntry = (PFSPERFNAMEENTRY)RTMemAllocVar(RT_UOFFSETOF_DYN(FSPERFNAMEENTRY, szName[cchName + 1]));
665 if (pEntry)
666 {
667 RTListInit(&pEntry->Entry);
668 pEntry->cchName = (uint16_t)cchName;
669 memcpy(pEntry->szName, pchName, cchName);
670 pEntry->szName[cchName] = '\0';
671 }
672 return pEntry;
673}
674
675
676static int fsPerfManyTreeRecursiveDirCreator(size_t cchDir, uint32_t iDepth)
677{
678 PFSPERFNAMEENTRY pEntry = fsPerfCreateNameEntry(g_szDir, cchDir);
679 RTTESTI_CHECK_RET(pEntry, VERR_NO_MEMORY);
680 RTListAppend(&g_ManyTreeHead, &pEntry->Entry);
681
682 RTTESTI_CHECK_RC_RET(RTDirCreate(g_szDir, 0755, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL),
683 VINF_SUCCESS, rcCheck);
684
685 if (iDepth < g_cManyTreeDepth)
686 for (uint32_t i = 0; i < g_cManyTreeSubdirsPerDir; i++)
687 {
688 size_t cchSubDir = RTStrPrintf(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, "d%02u" RTPATH_SLASH_STR, i);
689 RTTESTI_CHECK_RC_RET(fsPerfManyTreeRecursiveDirCreator(cchDir + cchSubDir, iDepth + 1), VINF_SUCCESS, rcCheck);
690 }
691
692 return VINF_SUCCESS;
693}
694
695
696void fsPerfManyFiles(void)
697{
698 RTTestISub("manyfiles");
699
700 /*
701 * Create a sub-directory with like 10000 files in it.
702 *
703 * This does push the directory organization of the underlying file system,
704 * which is something we might not want to profile with shared folders. It
705 * is however useful for directory enumeration.
706 */
707 RTTESTI_CHECK_RC_RETV(RTDirCreate(InDir(RT_STR_TUPLE("manyfiles")), 0755,
708 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL),
709 VINF_SUCCESS);
710
711 size_t offFilename = strlen(g_szDir);
712 g_szDir[offFilename++] = RTPATH_SLASH;
713
714 fsPerfYield();
715 RTFILE hFile;
716 uint64_t const nsStart = RTTimeNanoTS();
717 for (uint32_t i = 0; i < g_cManyFiles; i++)
718 {
719 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
720 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, g_szDir, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
721 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
722 }
723 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
724 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Creating %u empty files in single directory", g_cManyFiles);
725 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Create empty file (single dir)");
726
727 /*
728 * Create a bunch of directories with exacly 32 files in each, hoping to
729 * avoid any directory organization artifacts.
730 */
731 /* Create the directories first, building a list of them for simplifying iteration: */
732 RTListInit(&g_ManyTreeHead);
733 InDir(RT_STR_TUPLE("manytree" RTPATH_SLASH_STR));
734 RTTESTI_CHECK_RC_RETV(fsPerfManyTreeRecursiveDirCreator(strlen(g_szDir), 0), VINF_SUCCESS);
735
736 /* Create the zero byte files: */
737 fsPerfYield();
738 uint64_t const nsStart2 = RTTimeNanoTS();
739 uint32_t cFiles = 0;
740 PFSPERFNAMEENTRY pCur;
741 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry)
742 {
743 char szPath[RTPATH_MAX];
744 memcpy(szPath, pCur->szName, pCur->cchName);
745 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++)
746 {
747 RTStrFormatU32(&szPath[pCur->cchName], sizeof(szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD);
748 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, szPath, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
749 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
750 cFiles++;
751 }
752 }
753 uint64_t const cNsElapsed2 = RTTimeNanoTS() - nsStart2;
754 RTTestIValueF(cNsElapsed2, RTTESTUNIT_NS, "Creating %u empty files in tree", cFiles);
755 RTTestIValueF(cNsElapsed2 / cFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Create empty file (tree)");
756 RTTESTI_CHECK(g_cManyTreeFiles == cFiles);
757}
758
759
760DECL_FORCE_INLINE(int) fsPerfOpenExistingOnceReadonly(const char *pszFile)
761{
762 RTFILE hFile;
763 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS, rcCheck);
764 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
765 return VINF_SUCCESS;
766}
767
768
769DECL_FORCE_INLINE(int) fsPerfOpenExistingOnceWriteonly(const char *pszFile)
770{
771 RTFILE hFile;
772 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS, rcCheck);
773 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
774 return VINF_SUCCESS;
775}
776
777
778/** @note tstRTFileOpenEx-1.cpp has a copy of this code. */
779static void tstOpenExTest(unsigned uLine, int cbExist, int cbNext, const char *pszFilename, uint64_t fAction,
780 int rcExpect, RTFILEACTION enmActionExpected)
781{
782 uint64_t const fCreateMode = (0644 << RTFILE_O_CREATE_MODE_SHIFT);
783 RTFILE hFile;
784 int rc;
785
786 /*
787 * File existence and size.
788 */
789 bool fOkay = false;
790 RTFSOBJINFO ObjInfo;
791 rc = RTPathQueryInfoEx(pszFilename, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
792 if (RT_SUCCESS(rc))
793 fOkay = cbExist == (int64_t)ObjInfo.cbObject;
794 else
795 fOkay = rc == VERR_FILE_NOT_FOUND && cbExist < 0;
796 if (!fOkay)
797 {
798 if (cbExist >= 0)
799 {
800 rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | fCreateMode);
801 if (RT_SUCCESS(rc))
802 {
803 while (cbExist > 0)
804 {
805 int cbToWrite = (int)strlen(pszFilename);
806 if (cbToWrite > cbExist)
807 cbToWrite = cbExist;
808 rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
809 if (RT_FAILURE(rc))
810 {
811 RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
812 break;
813 }
814 cbExist -= cbToWrite;
815 }
816
817 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
818 }
819 else
820 RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
821
822 }
823 else
824 {
825 rc = RTFileDelete(pszFilename);
826 if (rc != VINF_SUCCESS && rc != VERR_FILE_NOT_FOUND)
827 RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
828 }
829 }
830
831 /*
832 * The actual test.
833 */
834 RTFILEACTION enmActuallyTaken = RTFILEACTION_END;
835 hFile = NIL_RTFILE;
836 rc = RTFileOpenEx(pszFilename, fAction | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | fCreateMode, &hFile, &enmActuallyTaken);
837 if ( rc != rcExpect
838 || enmActuallyTaken != enmActionExpected
839 || (RT_SUCCESS(rc) ? hFile == NIL_RTFILE : hFile != NIL_RTFILE))
840 RTTestIFailed("%u: RTFileOpenEx(%s, %#llx) -> %Rrc + %d (hFile=%p), expected %Rrc + %d\n",
841 uLine, pszFilename, fAction, rc, enmActuallyTaken, hFile, rcExpect, enmActionExpected);
842 if (RT_SUCCESS(rc))
843 {
844 if ( enmActionExpected == RTFILEACTION_REPLACED
845 || enmActionExpected == RTFILEACTION_TRUNCATED)
846 {
847 uint8_t abBuf[16];
848 rc = RTFileRead(hFile, abBuf, 1, NULL);
849 if (rc != VERR_EOF)
850 RTTestIFailed("%u: RTFileRead(%s,,1,) -> %Rrc, expected VERR_EOF\n", uLine, pszFilename, rc);
851 }
852
853 while (cbNext > 0)
854 {
855 int cbToWrite = (int)strlen(pszFilename);
856 if (cbToWrite > cbNext)
857 cbToWrite = cbNext;
858 rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
859 if (RT_FAILURE(rc))
860 {
861 RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
862 break;
863 }
864 cbNext -= cbToWrite;
865 }
866
867 rc = RTFileClose(hFile);
868 if (RT_FAILURE(rc))
869 RTTestIFailed("%u: RTFileClose(%p) -> %Rrc\n", uLine, hFile, rc);
870 }
871}
872
873
874void fsPerfOpen(void)
875{
876 RTTestISub("open");
877
878 /* Opening non-existing files. */
879 RTFILE hFile;
880 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InEmptyDir(RT_STR_TUPLE("no-such-file")),
881 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_FILE_NOT_FOUND);
882 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
883 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), FSPERF_VERR_PATH_NOT_FOUND);
884 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
885 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_PATH_NOT_FOUND);
886
887 /*
888 * The following is copied from tstRTFileOpenEx-1.cpp:
889 */
890 InDir(RT_STR_TUPLE("file1"));
891 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN, VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
892 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN_CREATE, VINF_SUCCESS, RTFILEACTION_CREATED);
893 tstOpenExTest(__LINE__, 0, 0, g_szDir, RTFILE_O_OPEN_CREATE, VINF_SUCCESS, RTFILEACTION_OPENED);
894 tstOpenExTest(__LINE__, 0, 0, g_szDir, RTFILE_O_OPEN, VINF_SUCCESS, RTFILEACTION_OPENED);
895
896 tstOpenExTest(__LINE__, 0, 0, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
897 tstOpenExTest(__LINE__, 0, 10, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
898 tstOpenExTest(__LINE__, 10, 10, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
899 tstOpenExTest(__LINE__, 10, -1, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_TRUNCATED);
900 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE, VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
901 tstOpenExTest(__LINE__, -1, 0, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
902
903 tstOpenExTest(__LINE__, 0, -1, g_szDir, RTFILE_O_CREATE_REPLACE, VINF_SUCCESS, RTFILEACTION_REPLACED);
904 tstOpenExTest(__LINE__, -1, 0, g_szDir, RTFILE_O_CREATE_REPLACE, VINF_SUCCESS, RTFILEACTION_CREATED);
905 tstOpenExTest(__LINE__, 0, -1, g_szDir, RTFILE_O_CREATE, VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
906 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_CREATE, VINF_SUCCESS, RTFILEACTION_CREATED);
907
908 tstOpenExTest(__LINE__, -1, 10, g_szDir, RTFILE_O_CREATE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
909 tstOpenExTest(__LINE__, 10, 10, g_szDir, RTFILE_O_CREATE | RTFILE_O_TRUNCATE, VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
910 tstOpenExTest(__LINE__, 10, -1, g_szDir, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_REPLACED);
911 tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
912
913 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
914
915 /*
916 * Create file1 and then try exclusivly creating it again.
917 * Then profile opening it for reading.
918 */
919 RTFILE hFile1;
920 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file1")),
921 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
922 RTTESTI_CHECK_RC(RTFileOpen(&hFile, g_szDir, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VERR_ALREADY_EXISTS);
923 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
924
925 PROFILE_FN(fsPerfOpenExistingOnceReadonly(g_szDir), g_nsTestRun, "RTFileOpen/Close/Readonly");
926 PROFILE_FN(fsPerfOpenExistingOnceWriteonly(g_szDir), g_nsTestRun, "RTFileOpen/Close/Writeonly");
927
928 /*
929 * Profile opening in the deep directory too.
930 */
931 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file1")),
932 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
933 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
934 PROFILE_FN(fsPerfOpenExistingOnceReadonly(g_szDeepDir), g_nsTestRun, "RTFileOpen/Close/deep/readonly");
935 PROFILE_FN(fsPerfOpenExistingOnceWriteonly(g_szDeepDir), g_nsTestRun, "RTFileOpen/Close/deep/writeonly");
936
937 /* Manytree: */
938 char szPath[RTPATH_MAX];
939 PROFILE_MANYTREE_FN(szPath, fsPerfOpenExistingOnceReadonly(szPath), 1, g_nsTestRun, "RTFileOpen/Close/manytree/readonly");
940}
941
942
943void fsPerfFStat(void)
944{
945 RTTestISub("fstat");
946 RTFILE hFile1;
947 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file2")),
948 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
949 RTFSOBJINFO ObjInfo = {0};
950 PROFILE_FN(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_NOTHING), g_nsTestRun, "RTFileQueryInfo/NOTHING");
951 PROFILE_FN(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_UNIX), g_nsTestRun, "RTFileQueryInfo/UNIX");
952
953 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
954}
955
956
957void fsPerfFChMod(void)
958{
959 RTTestISub("fchmod");
960 RTFILE hFile1;
961 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file4")),
962 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
963 RTFSOBJINFO ObjInfo = {0};
964 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
965 RTFMODE const fEvenMode = (ObjInfo.Attr.fMode & ~RTFS_UNIX_ALL_ACCESS_PERMS) | RTFS_DOS_READONLY | 0400;
966 RTFMODE const fOddMode = (ObjInfo.Attr.fMode & ~(RTFS_UNIX_ALL_ACCESS_PERMS | RTFS_DOS_READONLY)) | 0640;
967 PROFILE_FN(RTFileSetMode(hFile1, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTFileSetMode");
968
969 RTFileSetMode(hFile1, ObjInfo.Attr.fMode);
970 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
971}
972
973
974void fsPerfFUtimes(void)
975{
976 RTTestISub("futimes");
977 RTFILE hFile1;
978 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file5")),
979 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
980 RTTIMESPEC Time1;
981 RTTimeNow(&Time1);
982 RTTIMESPEC Time2 = Time1;
983 RTTimeSpecSubSeconds(&Time2, 3636);
984
985 RTFSOBJINFO ObjInfo0 = {0};
986 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo0, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
987
988 /* Modify modification time: */
989 RTTESTI_CHECK_RC(RTFileSetTimes(hFile1, NULL, &Time2, NULL, NULL), VINF_SUCCESS);
990 RTFSOBJINFO ObjInfo1 = {0};
991 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo1, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
992 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo1.ModificationTime) >> 2) == (RTTimeSpecGetSeconds(&Time2) >> 2));
993 char sz1[RTTIME_STR_LEN], sz2[RTTIME_STR_LEN]; /* Div by 1000 here for posix impl. using timeval. */
994 RTTESTI_CHECK_MSG(RTTimeSpecGetNano(&ObjInfo1.AccessTime) / 1000 == RTTimeSpecGetNano(&ObjInfo0.AccessTime) / 1000,
995 ("%s, expected %s", RTTimeSpecToString(&ObjInfo1.AccessTime, sz1, sizeof(sz1)),
996 RTTimeSpecToString(&ObjInfo0.AccessTime, sz2, sizeof(sz2))));
997
998 /* Modify access time: */
999 RTTESTI_CHECK_RC(RTFileSetTimes(hFile1, &Time1, NULL, NULL, NULL), VINF_SUCCESS);
1000 RTFSOBJINFO ObjInfo2 = {0};
1001 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo2, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
1002 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo2.AccessTime) >> 2) == (RTTimeSpecGetSeconds(&Time1) >> 2));
1003 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo2.ModificationTime) / 1000 == RTTimeSpecGetNano(&ObjInfo1.ModificationTime) / 1000);
1004
1005 /* Benchmark it: */
1006 PROFILE_FN(RTFileSetTimes(hFile1, NULL, iIteration & 1 ? &Time1 : &Time2, NULL, NULL), g_nsTestRun, "RTFileSetTimes");
1007
1008 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1009}
1010
1011
1012void fsPerfStat(void)
1013{
1014 RTTestISub("stat");
1015 RTFSOBJINFO ObjInfo;
1016
1017 /* Non-existing files. */
1018 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InEmptyDir(RT_STR_TUPLE("no-such-file")),
1019 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VERR_FILE_NOT_FOUND);
1020 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
1021 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), FSPERF_VERR_PATH_NOT_FOUND);
1022 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
1023 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VERR_PATH_NOT_FOUND);
1024
1025 /* Shallow: */
1026 RTFILE hFile1;
1027 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file3")),
1028 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1029 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1030
1031 PROFILE_FN(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), g_nsTestRun,
1032 "RTPathQueryInfoEx/NOTHING");
1033 PROFILE_FN(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK), g_nsTestRun,
1034 "RTPathQueryInfoEx/UNIX");
1035
1036
1037 /* Deep: */
1038 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file3")),
1039 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1040 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1041
1042 PROFILE_FN(RTPathQueryInfoEx(g_szDeepDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), g_nsTestRun,
1043 "RTPathQueryInfoEx/deep/NOTHING");
1044 PROFILE_FN(RTPathQueryInfoEx(g_szDeepDir, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK), g_nsTestRun,
1045 "RTPathQueryInfoEx/deep/UNIX");
1046
1047 /* Manytree: */
1048 char szPath[RTPATH_MAX];
1049 PROFILE_MANYTREE_FN(szPath, RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK),
1050 1, g_nsTestRun, "RTPathQueryInfoEx/manytree/NOTHING");
1051 PROFILE_MANYTREE_FN(szPath, RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK),
1052 1, g_nsTestRun, "RTPathQueryInfoEx/manytree/UNIX");
1053}
1054
1055
1056void fsPerfChmod(void)
1057{
1058 RTTestISub("chmod");
1059
1060 /* Non-existing files. */
1061 RTTESTI_CHECK_RC(RTPathSetMode(InEmptyDir(RT_STR_TUPLE("no-such-file")), 0665),
1062 VERR_FILE_NOT_FOUND);
1063 RTTESTI_CHECK_RC(RTPathSetMode(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), 0665),
1064 FSPERF_VERR_PATH_NOT_FOUND);
1065 RTTESTI_CHECK_RC(RTPathSetMode(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), 0665), VERR_PATH_NOT_FOUND);
1066
1067 /* Shallow: */
1068 RTFILE hFile1;
1069 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file14")),
1070 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1071 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1072
1073 RTFSOBJINFO ObjInfo;
1074 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
1075 RTFMODE const fEvenMode = (ObjInfo.Attr.fMode & ~RTFS_UNIX_ALL_ACCESS_PERMS) | RTFS_DOS_READONLY | 0400;
1076 RTFMODE const fOddMode = (ObjInfo.Attr.fMode & ~(RTFS_UNIX_ALL_ACCESS_PERMS | RTFS_DOS_READONLY)) | 0640;
1077 PROFILE_FN(RTPathSetMode(g_szDir, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTPathSetMode");
1078 RTPathSetMode(g_szDir, ObjInfo.Attr.fMode);
1079
1080 /* Deep: */
1081 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file14")),
1082 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1083 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1084
1085 PROFILE_FN(RTPathSetMode(g_szDeepDir, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTPathSetMode/deep");
1086 RTPathSetMode(g_szDeepDir, ObjInfo.Attr.fMode);
1087
1088 /* Manytree: */
1089 char szPath[RTPATH_MAX];
1090 PROFILE_MANYTREE_FN(szPath, RTPathSetMode(szPath, iIteration & 1 ? fOddMode : fEvenMode), 1, g_nsTestRun,
1091 "RTPathSetMode/manytree");
1092 DO_MANYTREE_FN(szPath, RTPathSetMode(szPath, ObjInfo.Attr.fMode));
1093}
1094
1095
1096void fsPerfUtimes(void)
1097{
1098 RTTestISub("utimes");
1099
1100 RTTIMESPEC Time1;
1101 RTTimeNow(&Time1);
1102 RTTIMESPEC Time2 = Time1;
1103 RTTimeSpecSubSeconds(&Time2, 3636);
1104
1105 /* Non-existing files. */
1106 RTTESTI_CHECK_RC(RTPathSetTimesEx(InEmptyDir(RT_STR_TUPLE("no-such-file")), NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
1107 VERR_FILE_NOT_FOUND);
1108 RTTESTI_CHECK_RC(RTPathSetTimesEx(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
1109 NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
1110 FSPERF_VERR_PATH_NOT_FOUND);
1111 RTTESTI_CHECK_RC(RTPathSetTimesEx(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
1112 NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
1113 VERR_PATH_NOT_FOUND);
1114
1115 /* Shallow: */
1116 RTFILE hFile1;
1117 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file15")),
1118 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1119 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1120
1121 RTFSOBJINFO ObjInfo0 = {0};
1122 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo0, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
1123
1124 /* Modify modification time: */
1125 RTTESTI_CHECK_RC(RTPathSetTimesEx(g_szDir, NULL, &Time2, NULL, NULL, RTPATH_F_ON_LINK), VINF_SUCCESS);
1126 RTFSOBJINFO ObjInfo1;
1127 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo1, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
1128 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo1.ModificationTime) >> 2) == (RTTimeSpecGetSeconds(&Time2) >> 2));
1129 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo1.AccessTime) / 1000 == RTTimeSpecGetNano(&ObjInfo0.AccessTime) / 1000 /* posix timeval */);
1130
1131 /* Modify access time: */
1132 RTTESTI_CHECK_RC(RTPathSetTimesEx(g_szDir, &Time1, NULL, NULL, NULL, RTPATH_F_ON_LINK), VINF_SUCCESS);
1133 RTFSOBJINFO ObjInfo2 = {0};
1134 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo2, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
1135 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo2.AccessTime) >> 2) == (RTTimeSpecGetSeconds(&Time1) >> 2));
1136 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo2.ModificationTime) / 1000 == RTTimeSpecGetNano(&ObjInfo1.ModificationTime) / 1000 /* posix timeval */);
1137
1138 /* Profile shallow: */
1139 PROFILE_FN(RTPathSetTimesEx(g_szDir, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
1140 NULL, NULL, RTPATH_F_ON_LINK),
1141 g_nsTestRun, "RTPathSetTimesEx");
1142
1143 /* Deep: */
1144 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file15")),
1145 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1146 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1147
1148 PROFILE_FN(RTPathSetTimesEx(g_szDeepDir, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
1149 NULL, NULL, RTPATH_F_ON_LINK),
1150 g_nsTestRun, "RTPathSetTimesEx/deep");
1151
1152 /* Manytree: */
1153 char szPath[RTPATH_MAX];
1154 PROFILE_MANYTREE_FN(szPath, RTPathSetTimesEx(szPath, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
1155 NULL, NULL, RTPATH_F_ON_LINK),
1156 1, g_nsTestRun, "RTPathSetTimesEx/manytree");
1157}
1158
1159
1160DECL_FORCE_INLINE(int) fsPerfRenameMany(const char *pszFile, uint32_t iIteration)
1161{
1162 char szRenamed[RTPATH_MAX];
1163 strcat(strcpy(szRenamed, pszFile), "-renamed");
1164 if (!(iIteration & 1))
1165 return RTPathRename(pszFile, szRenamed, 0);
1166 return RTPathRename(szRenamed, pszFile, 0);
1167}
1168
1169
1170void fsPerfRename(void)
1171{
1172 RTTestISub("rename");
1173 char szPath[RTPATH_MAX];
1174
1175/** @todo rename directories too! */
1176/** @todo check overwriting files and directoris (empty ones should work on
1177 * unix). */
1178
1179 /* Non-existing files. */
1180 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("other-no-such-file")));
1181 RTTESTI_CHECK_RC(RTPathRename(InEmptyDir(RT_STR_TUPLE("no-such-file")), szPath, 0), VERR_FILE_NOT_FOUND);
1182 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "other-no-such-file")));
1183 RTTESTI_CHECK_RC(RTPathRename(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), szPath, 0),
1184 FSPERF_VERR_PATH_NOT_FOUND);
1185 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("other-no-such-file")));
1186 RTTESTI_CHECK_RC(RTPathRename(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), szPath, 0), VERR_PATH_NOT_FOUND);
1187
1188 RTFILE hFile1;
1189 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file16")),
1190 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1191 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1192 strcat(strcpy(szPath, g_szDir), "-no-such-dir" RTPATH_SLASH_STR "file16");
1193 RTTESTI_CHECK_RC(RTPathRename(szPath, g_szDir, 0), FSPERF_VERR_PATH_NOT_FOUND);
1194 RTTESTI_CHECK_RC(RTPathRename(g_szDir, szPath, 0), FSPERF_VERR_PATH_NOT_FOUND);
1195
1196 /* Shallow: */
1197 strcat(strcpy(szPath, g_szDir), "-other");
1198 PROFILE_FN(RTPathRename(iIteration & 1 ? szPath : g_szDir, iIteration & 1 ? g_szDir : szPath, 0), g_nsTestRun, "RTPathRename");
1199
1200 /* Deep: */
1201 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file15")),
1202 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1203 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1204
1205 strcat(strcpy(szPath, g_szDeepDir), "-other");
1206 PROFILE_FN(RTPathRename(iIteration & 1 ? szPath : g_szDeepDir, iIteration & 1 ? g_szDeepDir : szPath, 0),
1207 g_nsTestRun, "RTPathRename/deep");
1208
1209 /* Manytree: */
1210 PROFILE_MANYTREE_FN(szPath, fsPerfRenameMany(szPath, iIteration), 2, g_nsTestRun, "RTPathRename/manytree");
1211}
1212
1213
1214DECL_FORCE_INLINE(int) fsPerfOpenClose(const char *pszDir)
1215{
1216 RTDIR hDir;
1217 RTTESTI_CHECK_RC_RET(RTDirOpen(&hDir, pszDir), VINF_SUCCESS, rcCheck);
1218 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1219 return VINF_SUCCESS;
1220}
1221
1222
1223void vsPerfDirOpen(void)
1224{
1225 RTTestISub("dir open");
1226 RTDIR hDir;
1227
1228 /*
1229 * Non-existing files.
1230 */
1231 RTTESTI_CHECK_RC(RTDirOpen(&hDir, InEmptyDir(RT_STR_TUPLE("no-such-file"))), VERR_FILE_NOT_FOUND);
1232 RTTESTI_CHECK_RC(RTDirOpen(&hDir, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
1233 RTTESTI_CHECK_RC(RTDirOpen(&hDir, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
1234
1235 /*
1236 * Check that open + close works.
1237 */
1238 g_szEmptyDir[g_cchEmptyDir] = '\0';
1239 RTTESTI_CHECK_RC_RETV(RTDirOpen(&hDir, g_szEmptyDir), VINF_SUCCESS);
1240 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1241
1242
1243 /*
1244 * Profile empty dir and dir with many files.
1245 */
1246 g_szEmptyDir[g_cchEmptyDir] = '\0';
1247 PROFILE_FN(fsPerfOpenClose(g_szEmptyDir), g_nsTestRun, "RTDirOpen/Close empty");
1248 if (g_fManyFiles)
1249 {
1250 InDir(RT_STR_TUPLE("manyfiles"));
1251 PROFILE_FN(fsPerfOpenClose(g_szDir), g_nsTestRun, "RTDirOpen/Close manyfiles");
1252 }
1253}
1254
1255
1256DECL_FORCE_INLINE(int) fsPerfEnumEmpty(void)
1257{
1258 RTDIR hDir;
1259 g_szEmptyDir[g_cchEmptyDir] = '\0';
1260 RTTESTI_CHECK_RC_RET(RTDirOpen(&hDir, g_szEmptyDir), VINF_SUCCESS, rcCheck);
1261
1262 RTDIRENTRY Entry;
1263 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1264 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1265 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
1266
1267 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1268 return VINF_SUCCESS;
1269}
1270
1271
1272DECL_FORCE_INLINE(int) fsPerfEnumManyFiles(void)
1273{
1274 RTDIR hDir;
1275 RTTESTI_CHECK_RC_RET(RTDirOpen(&hDir, InDir(RT_STR_TUPLE("manyfiles"))), VINF_SUCCESS, rcCheck);
1276 uint32_t cLeft = g_cManyFiles + 2;
1277 for (;;)
1278 {
1279 RTDIRENTRY Entry;
1280 if (cLeft > 0)
1281 RTTESTI_CHECK_RC_BREAK(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1282 else
1283 {
1284 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
1285 break;
1286 }
1287 cLeft--;
1288 }
1289 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1290 return VINF_SUCCESS;
1291}
1292
1293
1294void vsPerfDirEnum(void)
1295{
1296 RTTestISub("dir enum");
1297 RTDIR hDir;
1298
1299 /*
1300 * The empty directory.
1301 */
1302 g_szEmptyDir[g_cchEmptyDir] = '\0';
1303 RTTESTI_CHECK_RC_RETV(RTDirOpen(&hDir, g_szEmptyDir), VINF_SUCCESS);
1304
1305 uint32_t fDots = 0;
1306 RTDIRENTRY Entry;
1307 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1308 RTTESTI_CHECK(RTDirEntryIsStdDotLink(&Entry));
1309 fDots |= RT_BIT_32(Entry.cbName - 1);
1310
1311 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1312 RTTESTI_CHECK(RTDirEntryIsStdDotLink(&Entry));
1313 fDots |= RT_BIT_32(Entry.cbName - 1);
1314 RTTESTI_CHECK(fDots == 3);
1315
1316 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
1317
1318 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1319
1320 /*
1321 * The directory with many files in it.
1322 */
1323 if (g_fManyFiles)
1324 {
1325 fDots = 0;
1326 uint32_t const cBitmap = RT_ALIGN_32(g_cManyFiles, 64);
1327 void *pvBitmap = alloca(cBitmap / 8);
1328 RT_BZERO(pvBitmap, cBitmap / 8);
1329 for (uint32_t i = g_cManyFiles; i < cBitmap; i++)
1330 ASMBitSet(pvBitmap, i);
1331
1332 uint32_t cFiles = 0;
1333 RTTESTI_CHECK_RC_RETV(RTDirOpen(&hDir, InDir(RT_STR_TUPLE("manyfiles"))), VINF_SUCCESS);
1334 for (;;)
1335 {
1336 int rc = RTDirRead(hDir, &Entry, NULL);
1337 if (rc == VINF_SUCCESS)
1338 {
1339 if (Entry.szName[0] == '.')
1340 {
1341 if (Entry.szName[1] == '.')
1342 {
1343 RTTESTI_CHECK(!(fDots & 2));
1344 fDots |= 2;
1345 }
1346 else
1347 {
1348 RTTESTI_CHECK(Entry.szName[1] == '\0');
1349 RTTESTI_CHECK(!(fDots & 1));
1350 fDots |= 1;
1351 }
1352 }
1353 else
1354 {
1355 uint32_t iFile = UINT32_MAX;
1356 RTTESTI_CHECK_RC(RTStrToUInt32Full(Entry.szName, 10, &iFile), VINF_SUCCESS);
1357 if ( iFile < g_cManyFiles
1358 && !ASMBitTest(pvBitmap, iFile))
1359 {
1360 ASMBitSet(pvBitmap, iFile);
1361 cFiles++;
1362 }
1363 else
1364 RTTestFailed(g_hTest, "line %u: iFile=%u g_cManyFiles=%u\n", __LINE__, iFile, g_cManyFiles);
1365 }
1366 }
1367 else if (rc == VERR_NO_MORE_FILES)
1368 break;
1369 else
1370 {
1371 RTTestFailed(g_hTest, "RTDirRead failed enumerating manyfiles: %Rrc\n", rc);
1372 RTDirClose(hDir);
1373 return;
1374 }
1375 }
1376 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1377 RTTESTI_CHECK(fDots == 3);
1378 RTTESTI_CHECK(cFiles == g_cManyFiles);
1379 RTTESTI_CHECK(ASMMemIsAllU8(pvBitmap, cBitmap / 8, 0xff));
1380 }
1381
1382 /*
1383 * Profile.
1384 */
1385 PROFILE_FN(fsPerfEnumEmpty(),g_nsTestRun, "RTDirOpen/Read/Close empty");
1386 if (g_fManyFiles)
1387 PROFILE_FN(fsPerfEnumManyFiles(), g_nsTestRun, "RTDirOpen/Read/Close manyfiles");
1388}
1389
1390
1391void fsPerfMkRmDir(void)
1392{
1393 RTTestISub("mkdir/rmdir");
1394
1395 /* Non-existing directories: */
1396 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir"))), VERR_FILE_NOT_FOUND);
1397 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
1398 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
1399 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), 0755, 0), FSPERF_VERR_PATH_NOT_FOUND);
1400 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), 0755, 0), VERR_PATH_NOT_FOUND);
1401
1402 /** @todo check what happens if non-final path component isn't a directory. unix
1403 * should return ENOTDIR and IPRT translates that to VERR_PATH_NOT_FOUND.
1404 * Curious what happens on windows. */
1405
1406 /* Already existing directories and files: */
1407 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE(".")), 0755, 0), VERR_ALREADY_EXISTS);
1408 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE("..")), 0755, 0), VERR_ALREADY_EXISTS);
1409
1410 /* Remove directory with subdirectories: */
1411#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1412 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("."))), VERR_DIR_NOT_EMPTY);
1413#else
1414 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER); /* EINVAL for '.' */
1415#endif
1416#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1417 int rc = RTDirRemove(InDir(RT_STR_TUPLE("..")));
1418# ifdef RT_OS_WINDOWS
1419 if (rc != VERR_DIR_NOT_EMPTY /*ntfs root*/ && rc != VERR_SHARING_VIOLATION /*ntfs weird*/)
1420 RTTestIFailed("RTDirRemove(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY or VERR_SHARING_VIOLATION", g_szDir, rc);
1421# else
1422 if (rc != VERR_DIR_NOT_EMPTY && rc != VERR_RESOURCE_BUSY /*IPRT/kLIBC fun*/)
1423 RTTestIFailed("RTDirRemove(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY or VERR_RESOURCE_BUSY", g_szDir, rc);
1424
1425 APIRET orc;
1426 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE(".")))) == ERROR_ACCESS_DENIED,
1427 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
1428 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("..")))) == ERROR_ACCESS_DENIED,
1429 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
1430 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("")))) == ERROR_PATH_NOT_FOUND, /* a little weird (fsrouter) */
1431 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_PATH_NOT_FOUND));
1432
1433# endif
1434#else
1435 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(".."))), VERR_DIR_NOT_EMPTY);
1436#endif
1437 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(""))), VERR_DIR_NOT_EMPTY);
1438
1439 /* Create a directory and remove it: */
1440 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("subdir-1")), 0755, 0), VINF_SUCCESS);
1441 RTTESTI_CHECK_RC(RTDirRemove(g_szDir), VINF_SUCCESS);
1442
1443 /* Create a file and try remove it or create a directory with the same name: */
1444 RTFILE hFile1;
1445 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file18")),
1446 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1447 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1448 RTTESTI_CHECK_RC(RTDirRemove(g_szDir), VERR_NOT_A_DIRECTORY);
1449 RTTESTI_CHECK_RC(RTDirCreate(g_szDir, 0755, 0), VERR_ALREADY_EXISTS);
1450 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("file18" RTPATH_SLASH_STR "subdir")), 0755, 0), VERR_PATH_NOT_FOUND);
1451
1452 /*
1453 * Profile alternately creating and removing a bunch of directories.
1454 */
1455 RTTESTI_CHECK_RC_RETV(RTDirCreate(InDir(RT_STR_TUPLE("subdir-2")), 0755, 0), VINF_SUCCESS);
1456 size_t cchDir = strlen(g_szDir);
1457 g_szDir[cchDir++] = RTPATH_SLASH;
1458 g_szDir[cchDir++] = 's';
1459
1460 uint32_t cCreated = 0;
1461 uint64_t nsCreate = 0;
1462 uint64_t nsRemove = 0;
1463 for (;;)
1464 {
1465 /* Create a bunch: */
1466 uint64_t nsStart = RTTimeNanoTS();
1467 for (uint32_t i = 0; i < 998; i++)
1468 {
1469 RTStrFormatU32(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, i, 10, 3, 3, RTSTR_F_ZEROPAD);
1470 RTTESTI_CHECK_RC_RETV(RTDirCreate(g_szDir, 0755, 0), VINF_SUCCESS);
1471 }
1472 nsCreate += RTTimeNanoTS() - nsStart;
1473 cCreated += 998;
1474
1475 /* Remove the bunch: */
1476 nsStart = RTTimeNanoTS();
1477 for (uint32_t i = 0; i < 998; i++)
1478 {
1479 RTStrFormatU32(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, i, 10, 3, 3, RTSTR_F_ZEROPAD);
1480 RTTESTI_CHECK_RC_RETV(RTDirRemove(g_szDir), VINF_SUCCESS);
1481 }
1482 nsRemove = RTTimeNanoTS() - nsStart;
1483
1484 /* Check if we got time for another round: */
1485 if ( ( nsRemove >= g_nsTestRun
1486 && nsCreate >= g_nsTestRun)
1487 || nsCreate + nsRemove >= g_nsTestRun * 3)
1488 break;
1489 }
1490 RTTestIValue("RTDirCreate", nsCreate / cCreated, RTTESTUNIT_NS_PER_OCCURRENCE);
1491 RTTestIValue("RTDirRemove", nsRemove / cCreated, RTTESTUNIT_NS_PER_OCCURRENCE);
1492}
1493
1494
1495void fsPerfStatVfs(void)
1496{
1497 RTTestISub("statvfs");
1498
1499 g_szEmptyDir[g_cchEmptyDir] = '\0';
1500 RTFOFF cbTotal;
1501 RTFOFF cbFree;
1502 uint32_t cbBlock;
1503 uint32_t cbSector;
1504 RTTESTI_CHECK_RC(RTFsQuerySizes(g_szEmptyDir, &cbTotal, &cbFree, &cbBlock, &cbSector), VINF_SUCCESS);
1505
1506 uint32_t uSerial;
1507 RTTESTI_CHECK_RC(RTFsQuerySerial(g_szEmptyDir, &uSerial), VINF_SUCCESS);
1508
1509 RTFSPROPERTIES Props;
1510 RTTESTI_CHECK_RC(RTFsQueryProperties(g_szEmptyDir, &Props), VINF_SUCCESS);
1511
1512 RTFSTYPE enmType;
1513 RTTESTI_CHECK_RC(RTFsQueryType(g_szEmptyDir, &enmType), VINF_SUCCESS);
1514
1515 g_szDeepDir[g_cchDeepDir] = '\0';
1516 PROFILE_FN(RTFsQuerySizes(g_szEmptyDir, &cbTotal, &cbFree, &cbBlock, &cbSector), g_nsTestRun, "RTFsQuerySize/empty");
1517 PROFILE_FN(RTFsQuerySizes(g_szDeepDir, &cbTotal, &cbFree, &cbBlock, &cbSector), g_nsTestRun, "RTFsQuerySize/deep");
1518}
1519
1520
1521void fsPerfRm(void)
1522{
1523 RTTestISub("rm");
1524
1525 /* Non-existing files. */
1526 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-file"))), VERR_FILE_NOT_FOUND);
1527 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
1528 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
1529
1530 /* Directories: */
1531#if defined(RT_OS_WINDOWS)
1532 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_ACCESS_DENIED);
1533 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_ACCESS_DENIED);
1534 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
1535#elif defined(RT_OS_DARWIN) /* unlink() on xnu 16.7.0 is behaviour totally werid: */
1536 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER);
1537 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VINF_SUCCESS /*WTF?!?*/);
1538 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
1539#elif defined(RT_OS_OS2) /* OS/2 has a busted unlink, it think it should remove directories too. */
1540 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("."))), VERR_DIR_NOT_EMPTY);
1541 int rc = RTFileDelete(InDir(RT_STR_TUPLE("..")));
1542 if (rc != VERR_DIR_NOT_EMPTY && rc != VERR_FILE_NOT_FOUND && rc != VERR_RESOURCE_BUSY)
1543 RTTestIFailed("RTFileDelete(%s) -> %Rrc, expected VERR_DIR_NOT_EMPTY or VERR_FILE_NOT_FOUND or VERR_RESOURCE_BUSY", g_szDir, rc);
1544 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE(""))), VERR_DIR_NOT_EMPTY);
1545 APIRET orc;
1546 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE(".")))) == ERROR_ACCESS_DENIED,
1547 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
1548 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("..")))) == ERROR_ACCESS_DENIED,
1549 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_ACCESS_DENIED));
1550 RTTESTI_CHECK_MSG((orc = DosDelete((PCSZ)InEmptyDir(RT_STR_TUPLE("")))) == ERROR_PATH_NOT_FOUND,
1551 ("DosDelete(%s) -> %u, expected %u\n", g_szEmptyDir, orc, ERROR_PATH_NOT_FOUND)); /* hpfs+jfs; weird. */
1552
1553#else
1554 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_IS_A_DIRECTORY);
1555 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_IS_A_DIRECTORY);
1556 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_IS_A_DIRECTORY);
1557#endif
1558
1559 /* Shallow: */
1560 RTFILE hFile1;
1561 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file19")),
1562 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1563 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1564 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
1565 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VERR_FILE_NOT_FOUND);
1566
1567 if (g_fManyFiles)
1568 {
1569 /*
1570 * Profile the deletion of the manyfiles content.
1571 */
1572 {
1573 InDir(RT_STR_TUPLE("manyfiles" RTPATH_SLASH_STR));
1574 size_t const offFilename = strlen(g_szDir);
1575 fsPerfYield();
1576 uint64_t const nsStart = RTTimeNanoTS();
1577 for (uint32_t i = 0; i < g_cManyFiles; i++)
1578 {
1579 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
1580 RTTESTI_CHECK_RC_RETV(RTFileDelete(g_szDir), VINF_SUCCESS);
1581 }
1582 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
1583 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files from a single directory", g_cManyFiles);
1584 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (single dir)");
1585 }
1586
1587 /*
1588 * Ditto for the manytree.
1589 */
1590 {
1591 char szPath[RTPATH_MAX];
1592 uint64_t const nsStart = RTTimeNanoTS();
1593 DO_MANYTREE_FN(szPath, RTTESTI_CHECK_RC_RETV(RTFileDelete(szPath), VINF_SUCCESS));
1594 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
1595 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files in tree", g_cManyTreeFiles);
1596 RTTestIValueF(cNsElapsed / g_cManyTreeFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (tree)");
1597 }
1598 }
1599}
1600
1601
1602void fsPerfChSize(void)
1603{
1604 RTTestISub("chsize");
1605
1606 /*
1607 * We need some free space to perform this test.
1608 */
1609 g_szDir[g_cchDir] = '\0';
1610 RTFOFF cbFree = 0;
1611 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
1612 if (cbFree < _1M)
1613 {
1614 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 1MB", cbFree);
1615 return;
1616 }
1617
1618 /*
1619 * Create a file and play around with it's size.
1620 * We let the current file position follow the end position as we make changes.
1621 */
1622 RTFILE hFile1;
1623 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file20")),
1624 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
1625 uint64_t cbFile = UINT64_MAX;
1626 RTTESTI_CHECK_RC(RTFileGetSize(hFile1, &cbFile), VINF_SUCCESS);
1627 RTTESTI_CHECK(cbFile == 0);
1628
1629 uint8_t abBuf[4096];
1630 static uint64_t const s_acbChanges[] =
1631 {
1632 1023, 1024, 1024, 1025, 8192, 11111, _1M, _8M, _8M,
1633 _4M, _2M + 1, _1M - 1, 65537, 65536, 32768, 8000, 7999, 7998, 1024, 1, 0
1634 };
1635 uint64_t cbOld = 0;
1636 for (unsigned i = 0; i < RT_ELEMENTS(s_acbChanges); i++)
1637 {
1638 uint64_t cbNew = s_acbChanges[i];
1639 if (cbNew + _64K >= (uint64_t)cbFree)
1640 continue;
1641
1642 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, cbNew), VINF_SUCCESS);
1643 RTTESTI_CHECK_RC(RTFileGetSize(hFile1, &cbFile), VINF_SUCCESS);
1644 RTTESTI_CHECK_MSG(cbFile == cbNew, ("cbFile=%#RX64 cbNew=%#RX64\n", cbFile, cbNew));
1645
1646 if (cbNew > cbOld)
1647 {
1648 /* Check that the extension is all zeroed: */
1649 uint64_t cbLeft = cbNew - cbOld;
1650 while (cbLeft > 0)
1651 {
1652 memset(abBuf, 0xff, sizeof(abBuf));
1653 size_t cbToRead = sizeof(abBuf);
1654 if (cbToRead > cbLeft)
1655 cbToRead = (size_t)cbLeft;
1656 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, cbToRead, NULL), VINF_SUCCESS);
1657 RTTESTI_CHECK(ASMMemIsZero(abBuf, cbToRead));
1658 cbLeft -= cbToRead;
1659 }
1660 }
1661 else
1662 {
1663 /* Check that reading fails with EOF because current position is now beyond the end: */
1664 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
1665
1666 /* Keep current position at the end of the file: */
1667 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbNew, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1668 }
1669 cbOld = cbNew;
1670 }
1671
1672 /*
1673 * Profile just the file setting operation itself, keeping the changes within
1674 * an allocation unit to avoid needing to adjust the actual (host) FS allocation.
1675 * ASSUMES allocation unit >= 512 and power of two.
1676 */
1677 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, _64K), VINF_SUCCESS);
1678 PROFILE_FN(RTFileSetSize(hFile1, _64K - (iIteration & 255) - 128), g_nsTestRun, "RTFileSetSize/noalloc");
1679
1680 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
1681 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1682 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
1683}
1684
1685
1686int fsPerfIoPrepFile(RTFILE hFile1, uint64_t cbFile, uint8_t **ppbFree)
1687{
1688 /*
1689 * Seek to the end - 4K and write the last 4K.
1690 * This should have the effect of filling the whole file with zeros.
1691 */
1692 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, cbFile - _4K, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1693 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, g_abRTZero4K, _4K, NULL), VINF_SUCCESS, rcCheck);
1694
1695 /*
1696 * Check that the space we searched across actually is zero filled.
1697 */
1698 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1699 size_t cbBuf = _1M;
1700 uint8_t *pbBuf = *ppbFree = (uint8_t *)RTMemAlloc(cbBuf);
1701 RTTESTI_CHECK_RET(pbBuf != NULL, VERR_NO_MEMORY);
1702 uint64_t cbLeft = cbFile;
1703 while (cbLeft > 0)
1704 {
1705 size_t cbToRead = cbBuf;
1706 if (cbToRead > cbLeft)
1707 cbToRead = (size_t)cbLeft;
1708 pbBuf[cbToRead] = 0xff;
1709
1710 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBuf, cbToRead, NULL), VINF_SUCCESS, rcCheck);
1711 RTTESTI_CHECK_RET(ASMMemIsZero(pbBuf, cbToRead), VERR_MISMATCH);
1712
1713 cbLeft -= cbToRead;
1714 }
1715
1716 /*
1717 * Fill the file with 0xf6 and insert offset markers with 1KB intervals.
1718 */
1719 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1720 memset(pbBuf, 0xf6, cbBuf);
1721 cbLeft = cbFile;
1722 uint64_t off = 0;
1723 while (cbLeft > 0)
1724 {
1725 Assert(!(off & (_1K - 1)));
1726 Assert(!(cbBuf & (_1K - 1)));
1727 for (size_t offBuf = 0; offBuf < cbBuf; offBuf += _1K, off += _1K)
1728 *(uint64_t *)&pbBuf[offBuf] = off;
1729
1730 size_t cbToWrite = cbBuf;
1731 if (cbToWrite > cbLeft)
1732 cbToWrite = (size_t)cbLeft;
1733
1734 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbToWrite, NULL), VINF_SUCCESS, rcCheck);
1735
1736 cbLeft -= cbToWrite;
1737 }
1738
1739 return VINF_SUCCESS;
1740}
1741
1742
1743/**
1744 * Checks the content read from the file fsPerfIoPrepFile() prepared.
1745 */
1746bool fsPerfCheckReadBuf(unsigned uLineNo, uint64_t off, uint8_t const *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
1747{
1748 uint32_t cMismatches = 0;
1749 size_t offBuf = 0;
1750 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
1751 while (offBuf < cbBuf)
1752 {
1753 /*
1754 * Check the offset marker:
1755 */
1756 if (offBlock < sizeof(uint64_t))
1757 {
1758 RTUINT64U uMarker;
1759 uMarker.u = off + offBuf - offBlock;
1760 unsigned offMarker = offBlock & (sizeof(uint64_t) - 1);
1761 while (offMarker < sizeof(uint64_t) && offBuf < cbBuf)
1762 {
1763 if (uMarker.au8[offMarker] != pbBuf[offBuf])
1764 {
1765 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#x",
1766 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], uMarker.au8[offMarker]);
1767 if (cMismatches++ > 32)
1768 return false;
1769 }
1770 offMarker++;
1771 offBuf++;
1772 }
1773 offBlock = sizeof(uint64_t);
1774 }
1775
1776 /*
1777 * Check the filling:
1778 */
1779 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf - offBuf);
1780 if ( cbFilling == 0
1781 || ASMMemIsAllU8(&pbBuf[offBuf], cbFilling, bFiller))
1782 offBuf += cbFilling;
1783 else
1784 {
1785 /* Some mismatch, locate it/them: */
1786 while (cbFilling > 0 && offBuf < cbBuf)
1787 {
1788 if (pbBuf[offBuf] != bFiller)
1789 {
1790 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#04x",
1791 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], bFiller);
1792 if (cMismatches++ > 32)
1793 return false;
1794 }
1795 offBuf++;
1796 cbFilling--;
1797 }
1798 }
1799 offBlock = 0;
1800 }
1801 return cMismatches == 0;
1802}
1803
1804
1805/**
1806 * Sets up write buffer with offset markers and fillers.
1807 */
1808void fsPerfFillWriteBuf(uint64_t off, uint8_t *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
1809{
1810 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
1811 while (cbBuf > 0)
1812 {
1813 /* The marker. */
1814 if (offBlock < sizeof(uint64_t))
1815 {
1816 RTUINT64U uMarker;
1817 uMarker.u = off + offBlock;
1818 if (cbBuf > sizeof(uMarker) - offBlock)
1819 {
1820 memcpy(pbBuf, &uMarker.au8[offBlock], sizeof(uMarker) - offBlock);
1821 pbBuf += sizeof(uMarker) - offBlock;
1822 cbBuf -= sizeof(uMarker) - offBlock;
1823 off += sizeof(uMarker) - offBlock;
1824 }
1825 else
1826 {
1827 memcpy(pbBuf, &uMarker.au8[offBlock], cbBuf);
1828 return;
1829 }
1830 offBlock = sizeof(uint64_t);
1831 }
1832
1833 /* Do the filling. */
1834 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf);
1835 memset(pbBuf, bFiller, cbFilling);
1836 pbBuf += cbFilling;
1837 cbBuf -= cbFilling;
1838 off += cbFilling;
1839
1840 offBlock = 0;
1841 }
1842}
1843
1844
1845
1846void fsPerfIoSeek(RTFILE hFile1, uint64_t cbFile)
1847{
1848 /*
1849 * Do a bunch of search tests, most which are random.
1850 */
1851 struct
1852 {
1853 int rc;
1854 uint32_t uMethod;
1855 int64_t offSeek;
1856 uint64_t offActual;
1857
1858 } aSeeks[9 + 64] =
1859 {
1860 { VINF_SUCCESS, RTFILE_SEEK_BEGIN, 0, 0 },
1861 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
1862 { VINF_SUCCESS, RTFILE_SEEK_END, 0, cbFile },
1863 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -4096, cbFile - 4096 },
1864 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 4096 - (int64_t)cbFile, 0 },
1865 { VINF_SUCCESS, RTFILE_SEEK_END, -(int64_t)cbFile/2, cbFile / 2 + (cbFile & 1) },
1866 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -(int64_t)cbFile/2, 0 },
1867#if defined(RT_OS_WINDOWS)
1868 { VERR_NEGATIVE_SEEK, RTFILE_SEEK_CURRENT, -1, 0 },
1869#else
1870 { VERR_INVALID_PARAMETER, RTFILE_SEEK_CURRENT, -1, 0 },
1871#endif
1872 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
1873 };
1874
1875 uint64_t offActual = 0;
1876 for (unsigned i = 9; i < RT_ELEMENTS(aSeeks); i++)
1877 {
1878 switch (RTRandU32Ex(RTFILE_SEEK_BEGIN, RTFILE_SEEK_END))
1879 {
1880 default: AssertFailedBreak();
1881 case RTFILE_SEEK_BEGIN:
1882 aSeeks[i].uMethod = RTFILE_SEEK_BEGIN;
1883 aSeeks[i].rc = VINF_SUCCESS;
1884 aSeeks[i].offSeek = RTRandU64Ex(0, cbFile + cbFile / 8);
1885 aSeeks[i].offActual = offActual = aSeeks[i].offSeek;
1886 break;
1887
1888 case RTFILE_SEEK_CURRENT:
1889 aSeeks[i].uMethod = RTFILE_SEEK_CURRENT;
1890 aSeeks[i].rc = VINF_SUCCESS;
1891 aSeeks[i].offSeek = (int64_t)RTRandU64Ex(0, cbFile + cbFile / 8) - (int64_t)offActual;
1892 aSeeks[i].offActual = offActual += aSeeks[i].offSeek;
1893 break;
1894
1895 case RTFILE_SEEK_END:
1896 aSeeks[i].uMethod = RTFILE_SEEK_END;
1897 aSeeks[i].rc = VINF_SUCCESS;
1898 aSeeks[i].offSeek = -(int64_t)RTRandU64Ex(0, cbFile);
1899 aSeeks[i].offActual = offActual = cbFile + aSeeks[i].offSeek;
1900 break;
1901 }
1902 }
1903
1904 for (unsigned iDoReadCheck = 0; iDoReadCheck < 2; iDoReadCheck++)
1905 {
1906 for (uint32_t i = 0; i < RT_ELEMENTS(aSeeks); i++)
1907 {
1908 offActual = UINT64_MAX;
1909 int rc = RTFileSeek(hFile1, aSeeks[i].offSeek, aSeeks[i].uMethod, &offActual);
1910 if (rc != aSeeks[i].rc)
1911 RTTestIFailed("Seek #%u: Expected %Rrc, got %Rrc", i, aSeeks[i].rc, rc);
1912 if (RT_SUCCESS(rc) && offActual != aSeeks[i].offActual)
1913 RTTestIFailed("Seek #%u: offActual %#RX64, expected %#RX64", i, offActual, aSeeks[i].offActual);
1914 if (RT_SUCCESS(rc))
1915 {
1916 uint64_t offTell = RTFileTell(hFile1);
1917 if (offTell != offActual)
1918 RTTestIFailed("Seek #%u: offActual %#RX64, RTFileTell %#RX64", i, offActual, offTell);
1919 }
1920
1921 if (RT_SUCCESS(rc) && offActual + _2K <= cbFile && iDoReadCheck)
1922 {
1923 uint8_t abBuf[_2K];
1924 RTTESTI_CHECK_RC(rc = RTFileRead(hFile1, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
1925 if (RT_SUCCESS(rc))
1926 {
1927 size_t offMarker = (size_t)(RT_ALIGN_64(offActual, _1K) - offActual);
1928 uint64_t uMarker = *(uint64_t *)&abBuf[offMarker]; /** @todo potentially unaligned access */
1929 if (uMarker != offActual + offMarker)
1930 RTTestIFailed("Seek #%u: Invalid marker value (@ %#RX64): %#RX64, expected %#RX64",
1931 i, offActual, uMarker, offActual + offMarker);
1932
1933 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -(int64_t)sizeof(abBuf), RTFILE_SEEK_CURRENT, NULL), VINF_SUCCESS);
1934 }
1935 }
1936 }
1937 }
1938
1939
1940 /*
1941 * Profile seeking relative to the beginning of the file and relative
1942 * to the end. The latter might be more expensive in a SF context.
1943 */
1944 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? iIteration : iIteration % cbFile, RTFILE_SEEK_BEGIN, NULL),
1945 g_nsTestRun, "RTFileSeek/BEGIN");
1946 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? -(int64_t)iIteration : -(int64_t)(iIteration % cbFile), RTFILE_SEEK_END, NULL),
1947 g_nsTestRun, "RTFileSeek/END");
1948
1949}
1950
1951#ifdef FSPERF_TEST_SENDFILE
1952
1953/**
1954 * Send file thread arguments.
1955 */
1956typedef struct FSPERFSENDFILEARGS
1957{
1958 uint64_t offFile;
1959 size_t cbSend;
1960 uint64_t cbSent;
1961 size_t cbBuf;
1962 uint8_t *pbBuf;
1963 uint8_t bFiller;
1964 bool fCheckBuf;
1965 RTSOCKET hSocket;
1966 uint64_t volatile tsThreadDone;
1967} FSPERFSENDFILEARGS;
1968
1969/** Thread receiving the bytes from a sendfile() call. */
1970static DECLCALLBACK(int) fsPerfSendFileThread(RTTHREAD hSelf, void *pvUser)
1971{
1972 FSPERFSENDFILEARGS *pArgs = (FSPERFSENDFILEARGS *)pvUser;
1973 int rc = VINF_SUCCESS;
1974
1975 if (pArgs->fCheckBuf)
1976 RTTestSetDefault(g_hTest, NULL);
1977
1978 uint64_t cbReceived = 0;
1979 while (cbReceived < pArgs->cbSent)
1980 {
1981 size_t const cbToRead = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbReceived);
1982 size_t cbActual = 0;
1983 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTTcpRead(pArgs->hSocket, pArgs->pbBuf, cbToRead, &cbActual), VINF_SUCCESS);
1984 RTTEST_CHECK_BREAK(g_hTest, cbActual != 0);
1985 RTTEST_CHECK(g_hTest, cbActual <= cbToRead);
1986 if (pArgs->fCheckBuf)
1987 fsPerfCheckReadBuf(__LINE__, pArgs->offFile + cbReceived, pArgs->pbBuf, cbActual, pArgs->bFiller);
1988 cbReceived += cbActual;
1989 }
1990
1991 pArgs->tsThreadDone = RTTimeNanoTS();
1992
1993 if (cbReceived == pArgs->cbSent && RT_SUCCESS(rc))
1994 {
1995 size_t cbActual = 0;
1996 rc = RTSocketReadNB(pArgs->hSocket, pArgs->pbBuf, 1, &cbActual);
1997 if (rc != VINF_SUCCESS && rc != VINF_TRY_AGAIN)
1998 RTTestFailed(g_hTest, "RTSocketReadNB(sendfile client socket) -> %Rrc; expected VINF_SUCCESS or VINF_TRY_AGAIN\n", rc);
1999 else if (cbActual != 0)
2000 RTTestFailed(g_hTest, "sendfile client socket still contains data when done!\n");
2001 }
2002
2003 RTTEST_CHECK_RC(g_hTest, RTSocketClose(pArgs->hSocket), VINF_SUCCESS);
2004 pArgs->hSocket = NIL_RTSOCKET;
2005
2006 RT_NOREF(hSelf);
2007 return rc;
2008}
2009
2010
2011static uint64_t fsPerfSendFileOne(FSPERFSENDFILEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
2012 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckBuf, unsigned iLine)
2013{
2014 /* Copy parameters to the argument structure: */
2015 pArgs->offFile = offFile;
2016 pArgs->cbSend = cbSend;
2017 pArgs->cbSent = cbSent;
2018 pArgs->bFiller = bFiller;
2019 pArgs->fCheckBuf = fCheckBuf;
2020
2021 /* Create a socket pair. */
2022 pArgs->hSocket = NIL_RTSOCKET;
2023 RTSOCKET hServer = NIL_RTSOCKET;
2024 RTTESTI_CHECK_RC_RET(RTTcpCreatePair(&hServer, &pArgs->hSocket, 0), VINF_SUCCESS, 0);
2025
2026 /* Create the receiving thread: */
2027 int rc;
2028 RTTHREAD hThread = NIL_RTTHREAD;
2029 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSendFileThread, pArgs, 0,
2030 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "sendfile"), VINF_SUCCESS);
2031 if (RT_SUCCESS(rc))
2032 {
2033 uint64_t const tsStart = RTTimeNanoTS();
2034
2035# if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
2036 /* SystemV sendfile: */
2037 loff_t offFileSf = pArgs->offFile;
2038 ssize_t cbActual = sendfile((int)RTSocketToNative(hServer), (int)RTFileToNative(hFile1), &offFileSf, pArgs->cbSend);
2039 int const iErr = errno;
2040 if (cbActual < 0)
2041 RTTestIFailed("%u: sendfile(socket, file, &%#X64, %#zx) failed (%zd): %d (%Rrc), offFileSf=%#RX64\n",
2042 iLine, pArgs->offFile, pArgs->cbSend, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileSf);
2043 else if ((uint64_t)cbActual != pArgs->cbSent)
2044 RTTestIFailed("%u: sendfile(socket, file, &%#RX64, %#zx): %#zx, expected %#RX64 (offFileSf=%#RX64)\n",
2045 iLine, pArgs->offFile, pArgs->cbSend, cbActual, pArgs->cbSent, (uint64_t)offFileSf);
2046 else if ((uint64_t)offFileSf != pArgs->offFile + pArgs->cbSent)
2047 RTTestIFailed("%u: sendfile(socket, file, &%#RX64, %#zx): %#zx; offFileSf=%#RX64, expected %#RX64\n",
2048 iLine, pArgs->offFile, pArgs->cbSend, cbActual, (uint64_t)offFileSf, pArgs->offFile + pArgs->cbSent);
2049#else
2050 /* BSD sendfile: */
2051# ifdef SF_SYNC
2052 int fSfFlags = SF_SYNC;
2053# else
2054 int fSfFlags = 0;
2055# endif
2056 off_t cbActual = pArgs->cbSend;
2057 rc = sendfile((int)RTFileToNative(hFile1), (int)RTSocketToNative(hServer),
2058# ifdef RT_OS_DARWIN
2059 pArgs->offFile, &cbActual, NULL, fSfFlags);
2060# else
2061 pArgs->offFile, cbActual, NULL, &cbActual, fSfFlags);
2062# endif
2063 int const iErr = errno;
2064 if (rc != 0)
2065 RTTestIFailed("%u: sendfile(file, socket, %#RX64, %#zx, NULL,, %#x) failed (%d): %d (%Rrc), cbActual=%#RX64\n",
2066 iLine, pArgs->offFile, (size_t)pArgs->cbSend, rc, iErr, RTErrConvertFromErrno(iErr), (uint64_t)cbActual);
2067 if ((uint64_t)cbActual != pArgs->cbSent)
2068 RTTestIFailed("%u: sendfile(file, socket, %#RX64, %#zx, NULL,, %#x): cbActual=%#RX64, expected %#RX64 (rc=%d, errno=%d)\n",
2069 iLine, pArgs->offFile, (size_t)pArgs->cbSend, (uint64_t)cbActual, pArgs->cbSent, rc, iErr);
2070# endif
2071 RTTESTI_CHECK_RC(RTSocketClose(hServer), VINF_SUCCESS);
2072 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
2073
2074 if (pArgs->tsThreadDone >= tsStart)
2075 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
2076 }
2077 return 0;
2078}
2079
2080
2081static void fsPerfSendFile(RTFILE hFile1, uint64_t cbFile)
2082{
2083 RTTestISub("sendfile");
2084# ifdef RT_OS_LINUX
2085 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - PAGE_OFFSET_MASK);
2086# else
2087 uint64_t const cbFileMax = RT_MIN(cbFile, SSIZE_MAX - PAGE_OFFSET_MASK);
2088# endif
2089 signal(SIGPIPE, SIG_IGN);
2090
2091 /*
2092 * Allocate a buffer.
2093 */
2094 FSPERFSENDFILEARGS Args;
2095 Args.cbBuf = RT_MIN(cbFileMax, _16M);
2096 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2097 while (!Args.pbBuf)
2098 {
2099 Args.cbBuf /= 8;
2100 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
2101 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2102 }
2103
2104 /*
2105 * First iteration with default buffer content.
2106 */
2107 fsPerfSendFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
2108 if (cbFileMax == cbFile)
2109 fsPerfSendFileOne(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
2110 else
2111 fsPerfSendFileOne(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
2112
2113 /*
2114 * Write a block using the regular API and then send it, checking that
2115 * the any caching that sendfile does is correctly updated.
2116 */
2117 uint8_t bFiller = 0xf6;
2118 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
2119 do
2120 {
2121 fsPerfSendFileOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
2122
2123 bFiller += 1;
2124 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
2125 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
2126
2127 fsPerfSendFileOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
2128
2129 cbToSend /= 2;
2130 } while (cbToSend >= PAGE_SIZE && ((unsigned)bFiller - 0xf7U) < 64);
2131
2132 /*
2133 * Restore buffer content
2134 */
2135 bFiller = 0xf6;
2136 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
2137 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
2138
2139 /*
2140 * Do 128 random sends.
2141 */
2142 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
2143 for (uint32_t iTest = 0; iTest < 128; iTest++)
2144 {
2145 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
2146 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
2147 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
2148
2149 fsPerfSendFileOne(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
2150 }
2151
2152 /*
2153 * Benchmark it.
2154 */
2155 uint32_t cIterations = 0;
2156 uint64_t nsElapsed = 0;
2157 for (;;)
2158 {
2159 uint64_t cNsThis = fsPerfSendFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
2160 nsElapsed += cNsThis;
2161 cIterations++;
2162 if (!cNsThis || nsElapsed >= g_nsTestRun)
2163 break;
2164 }
2165 uint64_t cbTotal = cbFileMax * cIterations;
2166 RTTestIValue("latency", nsElapsed / cIterations, RTTESTUNIT_NS_PER_CALL);
2167 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
2168 RTTestIValue("calls", cIterations, RTTESTUNIT_CALLS);
2169 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
2170 if (g_fShowDuration)
2171 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
2172
2173 /*
2174 * Cleanup.
2175 */
2176 RTMemFree(Args.pbBuf);
2177}
2178
2179#endif /* FSPERF_TEST_SENDFILE */
2180#ifdef RT_OS_LINUX
2181
2182#ifndef __NR_splice
2183# if defined(RT_ARCH_AMD64)
2184# define __NR_splice 275
2185# elif defined(RT_ARCH_X86)
2186# define __NR_splice 313
2187# else
2188# error "fix me"
2189# endif
2190#endif
2191
2192/** FsPerf is built against ancient glibc, so make the splice syscall ourselves. */
2193DECLINLINE(ssize_t) syscall_splice(int fdIn, loff_t *poffIn, int fdOut, loff_t *poffOut, size_t cbChunk, unsigned fFlags)
2194{
2195 return syscall(__NR_splice, fdIn, poffIn, fdOut, poffOut, cbChunk, fFlags);
2196}
2197
2198
2199/**
2200 * Send file thread arguments.
2201 */
2202typedef struct FSPERFSPLICEARGS
2203{
2204 uint64_t offFile;
2205 size_t cbSend;
2206 uint64_t cbSent;
2207 size_t cbBuf;
2208 uint8_t *pbBuf;
2209 uint8_t bFiller;
2210 bool fCheckBuf;
2211 uint32_t cCalls;
2212 RTPIPE hPipe;
2213 uint64_t volatile tsThreadDone;
2214} FSPERFSPLICEARGS;
2215
2216
2217/** Thread receiving the bytes from a splice() call. */
2218static DECLCALLBACK(int) fsPerfSpliceToPipeThread(RTTHREAD hSelf, void *pvUser)
2219{
2220 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
2221 int rc = VINF_SUCCESS;
2222
2223 if (pArgs->fCheckBuf)
2224 RTTestSetDefault(g_hTest, NULL);
2225
2226 uint64_t cbReceived = 0;
2227 while (cbReceived < pArgs->cbSent)
2228 {
2229 size_t const cbToRead = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbReceived);
2230 size_t cbActual = 0;
2231 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeReadBlocking(pArgs->hPipe, pArgs->pbBuf, cbToRead, &cbActual), VINF_SUCCESS);
2232 RTTEST_CHECK_BREAK(g_hTest, cbActual != 0);
2233 RTTEST_CHECK(g_hTest, cbActual <= cbToRead);
2234 if (pArgs->fCheckBuf)
2235 fsPerfCheckReadBuf(__LINE__, pArgs->offFile + cbReceived, pArgs->pbBuf, cbActual, pArgs->bFiller);
2236 cbReceived += cbActual;
2237 }
2238
2239 pArgs->tsThreadDone = RTTimeNanoTS();
2240
2241 if (cbReceived == pArgs->cbSent && RT_SUCCESS(rc))
2242 {
2243 size_t cbActual = 0;
2244 rc = RTPipeRead(pArgs->hPipe, pArgs->pbBuf, 1, &cbActual);
2245 if (rc != VINF_SUCCESS && rc != VINF_TRY_AGAIN && rc != VERR_BROKEN_PIPE)
2246 RTTestFailed(g_hTest, "RTPipeReadBlocking() -> %Rrc; expected VINF_SUCCESS or VINF_TRY_AGAIN\n", rc);
2247 else if (cbActual != 0)
2248 RTTestFailed(g_hTest, "splice read pipe still contains data when done!\n");
2249 }
2250
2251 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
2252 pArgs->hPipe = NIL_RTPIPE;
2253
2254 RT_NOREF(hSelf);
2255 return rc;
2256}
2257
2258
2259/** Sends hFile1 to a pipe via the Linux-specific splice() syscall. */
2260static uint64_t fsPerfSpliceToPipeOne(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
2261 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckBuf, unsigned iLine)
2262{
2263 /* Copy parameters to the argument structure: */
2264 pArgs->offFile = offFile;
2265 pArgs->cbSend = cbSend;
2266 pArgs->cbSent = cbSent;
2267 pArgs->bFiller = bFiller;
2268 pArgs->fCheckBuf = fCheckBuf;
2269
2270 /* Create a socket pair. */
2271 pArgs->hPipe = NIL_RTPIPE;
2272 RTPIPE hPipeW = NIL_RTPIPE;
2273 RTTESTI_CHECK_RC_RET(RTPipeCreate(&pArgs->hPipe, &hPipeW, 0 /*fFlags*/), VINF_SUCCESS, 0);
2274
2275 /* Create the receiving thread: */
2276 int rc;
2277 RTTHREAD hThread = NIL_RTTHREAD;
2278 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceToPipeThread, pArgs, 0,
2279 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
2280 if (RT_SUCCESS(rc))
2281 {
2282 uint64_t const tsStart = RTTimeNanoTS();
2283 size_t cbLeft = cbSend;
2284 size_t cbTotal = 0;
2285 do
2286 {
2287 loff_t offFileIn = offFile;
2288 ssize_t cbActual = syscall_splice((int)RTFileToNative(hFile1), &offFileIn, (int)RTPipeToNative(hPipeW), NULL,
2289 cbLeft, 0 /*fFlags*/);
2290 int const iErr = errno;
2291 if (RT_UNLIKELY(cbActual < 0))
2292 {
2293 if (iErr == EPIPE && cbTotal == pArgs->cbSent)
2294 break;
2295 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0) failed (%zd): %d (%Rrc), offFileIn=%#RX64\n",
2296 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileIn);
2297 break;
2298 }
2299 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
2300 if ((uint64_t)offFileIn != offFile + (uint64_t)cbActual)
2301 {
2302 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0): %#zx; offFileIn=%#RX64, expected %#RX64\n",
2303 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileIn, offFile + (uint64_t)cbActual);
2304 break;
2305 }
2306 if (cbActual > 0)
2307 {
2308 pArgs->cCalls++;
2309 offFile += (size_t)cbActual;
2310 cbTotal += (size_t)cbActual;
2311 cbLeft -= (size_t)cbActual;
2312 }
2313 else
2314 break;
2315 } while (cbLeft > 0);
2316
2317 if (cbTotal != pArgs->cbSent)
2318 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
2319
2320 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
2321 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
2322
2323 if (pArgs->tsThreadDone >= tsStart)
2324 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
2325 }
2326 return 0;
2327}
2328
2329
2330static void fsPerfSpliceToPipe(RTFILE hFile1, uint64_t cbFile)
2331{
2332 RTTestISub("splice/to-pipe");
2333
2334 /*
2335 * splice was introduced in 2.6.17 according to the man-page.
2336 */
2337 char szRelease[64];
2338 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
2339 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
2340 {
2341 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
2342 return;
2343 }
2344
2345 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - PAGE_OFFSET_MASK);
2346 signal(SIGPIPE, SIG_IGN);
2347
2348 /*
2349 * Allocate a buffer.
2350 */
2351 FSPERFSPLICEARGS Args;
2352 Args.cbBuf = RT_MIN(cbFileMax, _16M);
2353 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2354 while (!Args.pbBuf)
2355 {
2356 Args.cbBuf /= 8;
2357 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
2358 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2359 }
2360
2361 /*
2362 * First iteration with default buffer content.
2363 */
2364 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
2365 if (cbFileMax == cbFile)
2366 fsPerfSpliceToPipeOne(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
2367 else
2368 fsPerfSpliceToPipeOne(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
2369
2370 /*
2371 * Write a block using the regular API and then send it, checking that
2372 * the any caching that sendfile does is correctly updated.
2373 */
2374 uint8_t bFiller = 0xf6;
2375 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
2376 do
2377 {
2378 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
2379
2380 bFiller += 1;
2381 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
2382 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
2383
2384 fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
2385
2386 cbToSend /= 2;
2387 } while (cbToSend >= PAGE_SIZE && ((unsigned)bFiller - 0xf7U) < 64);
2388
2389 /*
2390 * Restore buffer content
2391 */
2392 bFiller = 0xf6;
2393 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
2394 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
2395
2396 /*
2397 * Do 128 random sends.
2398 */
2399 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
2400 for (uint32_t iTest = 0; iTest < 128; iTest++)
2401 {
2402 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
2403 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
2404 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
2405
2406 fsPerfSpliceToPipeOne(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
2407 }
2408
2409 /*
2410 * Benchmark it.
2411 */
2412 Args.cCalls = 0;
2413 uint32_t cIterations = 0;
2414 uint64_t nsElapsed = 0;
2415 for (;;)
2416 {
2417 uint64_t cNsThis = fsPerfSpliceToPipeOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
2418 nsElapsed += cNsThis;
2419 cIterations++;
2420 if (!cNsThis || nsElapsed >= g_nsTestRun)
2421 break;
2422 }
2423 uint64_t cbTotal = cbFileMax * cIterations;
2424 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
2425 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
2426 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
2427 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
2428 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
2429 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
2430 if (g_fShowDuration)
2431 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
2432
2433 /*
2434 * Cleanup.
2435 */
2436 RTMemFree(Args.pbBuf);
2437}
2438
2439
2440/** Thread sending the bytes to a splice() call. */
2441static DECLCALLBACK(int) fsPerfSpliceToFileThread(RTTHREAD hSelf, void *pvUser)
2442{
2443 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
2444 int rc = VINF_SUCCESS;
2445
2446 uint64_t offFile = pArgs->offFile;
2447 uint64_t cbTotalSent = 0;
2448 while (cbTotalSent < pArgs->cbSent)
2449 {
2450 size_t const cbToSend = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbTotalSent);
2451 fsPerfFillWriteBuf(offFile, pArgs->pbBuf, cbToSend, pArgs->bFiller);
2452 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeWriteBlocking(pArgs->hPipe, pArgs->pbBuf, cbToSend, NULL), VINF_SUCCESS);
2453 offFile += cbToSend;
2454 cbTotalSent += cbToSend;
2455 }
2456
2457 pArgs->tsThreadDone = RTTimeNanoTS();
2458
2459 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
2460 pArgs->hPipe = NIL_RTPIPE;
2461
2462 RT_NOREF(hSelf);
2463 return rc;
2464}
2465
2466
2467/** Fill hFile1 via a pipe and the Linux-specific splice() syscall. */
2468static uint64_t fsPerfSpliceToFileOne(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
2469 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckFile, unsigned iLine)
2470{
2471 /* Copy parameters to the argument structure: */
2472 pArgs->offFile = offFile;
2473 pArgs->cbSend = cbSend;
2474 pArgs->cbSent = cbSent;
2475 pArgs->bFiller = bFiller;
2476 pArgs->fCheckBuf = false;
2477
2478 /* Create a socket pair. */
2479 pArgs->hPipe = NIL_RTPIPE;
2480 RTPIPE hPipeR = NIL_RTPIPE;
2481 RTTESTI_CHECK_RC_RET(RTPipeCreate(&hPipeR, &pArgs->hPipe, 0 /*fFlags*/), VINF_SUCCESS, 0);
2482
2483 /* Create the receiving thread: */
2484 int rc;
2485 RTTHREAD hThread = NIL_RTTHREAD;
2486 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceToFileThread, pArgs, 0,
2487 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
2488 if (RT_SUCCESS(rc))
2489 {
2490 /*
2491 * Do the splicing.
2492 */
2493 uint64_t const tsStart = RTTimeNanoTS();
2494 size_t cbLeft = cbSend;
2495 size_t cbTotal = 0;
2496 do
2497 {
2498 loff_t offFileOut = offFile;
2499 ssize_t cbActual = syscall_splice((int)RTPipeToNative(hPipeR), NULL, (int)RTFileToNative(hFile1), &offFileOut,
2500 cbLeft, 0 /*fFlags*/);
2501 int const iErr = errno;
2502 if (RT_UNLIKELY(cbActual < 0))
2503 {
2504 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0) failed (%zd): %d (%Rrc), offFileOut=%#RX64\n",
2505 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileOut);
2506 break;
2507 }
2508 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
2509 if ((uint64_t)offFileOut != offFile + (uint64_t)cbActual)
2510 {
2511 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0): %#zx; offFileOut=%#RX64, expected %#RX64\n",
2512 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileOut, offFile + (uint64_t)cbActual);
2513 break;
2514 }
2515 if (cbActual > 0)
2516 {
2517 pArgs->cCalls++;
2518 offFile += (size_t)cbActual;
2519 cbTotal += (size_t)cbActual;
2520 cbLeft -= (size_t)cbActual;
2521 }
2522 else
2523 break;
2524 } while (cbLeft > 0);
2525 uint64_t const nsElapsed = RTTimeNanoTS() - tsStart;
2526
2527 if (cbTotal != pArgs->cbSent)
2528 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
2529
2530 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
2531 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
2532
2533 /* Check the file content. */
2534 if (fCheckFile && cbTotal == pArgs->cbSent)
2535 {
2536 offFile = pArgs->offFile;
2537 cbLeft = cbSent;
2538 while (cbLeft > 0)
2539 {
2540 size_t cbToRead = RT_MIN(cbLeft, pArgs->cbBuf);
2541 RTTESTI_CHECK_RC_BREAK(RTFileReadAt(hFile1, offFile, pArgs->pbBuf, cbToRead, NULL), VINF_SUCCESS);
2542 if (!fsPerfCheckReadBuf(iLine, offFile, pArgs->pbBuf, cbToRead, pArgs->bFiller))
2543 break;
2544 offFile += cbToRead;
2545 cbLeft -= cbToRead;
2546 }
2547 }
2548 return nsElapsed;
2549 }
2550 return 0;
2551}
2552
2553
2554static void fsPerfSpliceToFile(RTFILE hFile1, uint64_t cbFile)
2555{
2556 RTTestISub("splice/to-file");
2557
2558 /*
2559 * splice was introduced in 2.6.17 according to the man-page.
2560 */
2561 char szRelease[64];
2562 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
2563 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
2564 {
2565 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
2566 return;
2567 }
2568
2569 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - PAGE_OFFSET_MASK);
2570 signal(SIGPIPE, SIG_IGN);
2571
2572 /*
2573 * Allocate a buffer.
2574 */
2575 FSPERFSPLICEARGS Args;
2576 Args.cbBuf = RT_MIN(cbFileMax, _16M);
2577 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2578 while (!Args.pbBuf)
2579 {
2580 Args.cbBuf /= 8;
2581 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
2582 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2583 }
2584
2585 /*
2586 * Do the whole file.
2587 */
2588 uint8_t bFiller = 0x76;
2589 fsPerfSpliceToFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, bFiller, true /*fCheckFile*/, __LINE__);
2590
2591 /*
2592 * Do 64 random chunks (this is slower).
2593 */
2594 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
2595 for (uint32_t iTest = 0; iTest < 64; iTest++)
2596 {
2597 size_t const cbToWrite = (size_t)RTRandU64Ex(1, iTest < 24 ? cbSmall : cbFileMax);
2598 uint64_t const offToWriteAt = RTRandU64Ex(0, cbFile - cbToWrite);
2599 uint64_t const cbTryRead = cbToWrite + (iTest & 1 ? RTRandU32Ex(0, _64K) : 0);
2600
2601 bFiller++;
2602 fsPerfSpliceToFileOne(&Args, hFile1, offToWriteAt, cbTryRead, cbToWrite, bFiller, true /*fCheckFile*/, __LINE__);
2603 }
2604
2605 /*
2606 * Benchmark it.
2607 */
2608 Args.cCalls = 0;
2609 uint32_t cIterations = 0;
2610 uint64_t nsElapsed = 0;
2611 for (;;)
2612 {
2613 uint64_t cNsThis = fsPerfSpliceToFileOne(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
2614 nsElapsed += cNsThis;
2615 cIterations++;
2616 if (!cNsThis || nsElapsed >= g_nsTestRun)
2617 break;
2618 }
2619 uint64_t cbTotal = cbFileMax * cIterations;
2620 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
2621 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
2622 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
2623 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
2624 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
2625 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
2626 if (g_fShowDuration)
2627 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
2628
2629 /*
2630 * Cleanup.
2631 */
2632 RTMemFree(Args.pbBuf);
2633}
2634
2635#endif /* RT_OS_LINUX */
2636
2637/** For fsPerfIoRead and fsPerfIoWrite. */
2638#define PROFILE_IO_FN(a_szOperation, a_fnCall) \
2639 do \
2640 { \
2641 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); \
2642 uint64_t offActual = 0; \
2643 uint32_t cSeeks = 0; \
2644 \
2645 /* Estimate how many iterations we need to fill up the given timeslot: */ \
2646 fsPerfYield(); \
2647 uint64_t nsStart = RTTimeNanoTS(); \
2648 uint64_t ns; \
2649 do \
2650 ns = RTTimeNanoTS(); \
2651 while (ns == nsStart); \
2652 nsStart = ns; \
2653 \
2654 uint64_t iIteration = 0; \
2655 do \
2656 { \
2657 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
2658 iIteration++; \
2659 ns = RTTimeNanoTS() - nsStart; \
2660 } while (ns < RT_NS_10MS); \
2661 ns /= iIteration; \
2662 if (ns > g_nsPerNanoTSCall + 32) \
2663 ns -= g_nsPerNanoTSCall; \
2664 uint64_t cIterations = g_nsTestRun / ns; \
2665 if (cIterations < 2) \
2666 cIterations = 2; \
2667 else if (cIterations & 1) \
2668 cIterations++; \
2669 \
2670 /* Do the actual profiling: */ \
2671 cSeeks = 0; \
2672 iIteration = 0; \
2673 fsPerfYield(); \
2674 nsStart = RTTimeNanoTS(); \
2675 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
2676 { \
2677 for (; iIteration < cIterations; iIteration++)\
2678 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
2679 ns = RTTimeNanoTS() - nsStart;\
2680 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
2681 break; \
2682 cIterations += cIterations / 4; \
2683 if (cIterations & 1) \
2684 cIterations++; \
2685 nsStart += g_nsPerNanoTSCall; \
2686 } \
2687 RTTestIValueF(ns / iIteration, \
2688 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation "/seq/%RU32 latency", cbBlock); \
2689 RTTestIValueF((uint64_t)((uint64_t)iIteration * cbBlock / ((double)ns / RT_NS_1SEC)), \
2690 RTTESTUNIT_BYTES_PER_SEC, a_szOperation "/seq/%RU32 throughput", cbBlock); \
2691 RTTestIValueF(iIteration, \
2692 RTTESTUNIT_CALLS, a_szOperation "/seq/%RU32 calls", cbBlock); \
2693 RTTestIValueF((uint64_t)iIteration * cbBlock, \
2694 RTTESTUNIT_BYTES, a_szOperation "/seq/%RU32 bytes", cbBlock); \
2695 RTTestIValueF(cSeeks, \
2696 RTTESTUNIT_OCCURRENCES, a_szOperation "/seq/%RU32 seeks", cbBlock); \
2697 if (g_fShowDuration) \
2698 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation "/seq/%RU32 duration", cbBlock); \
2699 } while (0)
2700
2701
2702/**
2703 * One RTFileRead profiling iteration.
2704 */
2705DECL_FORCE_INLINE(int) fsPerfIoReadWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
2706 uint64_t *poffActual, uint32_t *pcSeeks)
2707{
2708 /* Do we need to seek back to the start? */
2709 if (*poffActual + cbBlock <= cbFile)
2710 { /* likely */ }
2711 else
2712 {
2713 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
2714 *pcSeeks += 1;
2715 *poffActual = 0;
2716 }
2717
2718 size_t cbActuallyRead = 0;
2719 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBlock, cbBlock, &cbActuallyRead), VINF_SUCCESS, rcCheck);
2720 if (cbActuallyRead == cbBlock)
2721 {
2722 *poffActual += cbActuallyRead;
2723 return VINF_SUCCESS;
2724 }
2725 RTTestIFailed("RTFileRead at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyRead, cbBlock);
2726 *poffActual += cbActuallyRead;
2727 return VERR_READ_ERROR;
2728}
2729
2730
2731void fsPerfIoReadBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
2732{
2733 RTTestISubF("IO - Sequential read %RU32", cbBlock);
2734 if (cbBlock <= cbFile)
2735 {
2736
2737 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
2738 if (pbBuf)
2739 {
2740 memset(pbBuf, 0xf7, cbBlock);
2741 PROFILE_IO_FN("RTFileRead", fsPerfIoReadWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
2742 RTMemPageFree(pbBuf, cbBlock);
2743 }
2744 else
2745 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
2746 }
2747 else
2748 RTTestSkipped(g_hTest, "test file too small");
2749}
2750
2751
2752/** preadv is too new to be useful, so we use the readv api via this wrapper. */
2753DECLINLINE(int) myFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
2754{
2755 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
2756 if (RT_SUCCESS(rc))
2757 rc = RTFileSgRead(hFile, pSgBuf, cbToRead, pcbRead);
2758 return rc;
2759}
2760
2761
2762void fsPerfRead(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
2763{
2764 RTTestISubF("IO - RTFileRead");
2765
2766 /*
2767 * Allocate a big buffer we can play around with. Min size is 1MB.
2768 */
2769 size_t cbBuf = cbFile < _64M ? (size_t)cbFile : _64M;
2770 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
2771 while (!pbBuf)
2772 {
2773 cbBuf /= 2;
2774 RTTESTI_CHECK_RETV(cbBuf >= _1M);
2775 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
2776 }
2777
2778#if 1
2779 /*
2780 * Start at the beginning and read the full buffer in random small chunks, thereby
2781 * checking that unaligned buffer addresses, size and file offsets work fine.
2782 */
2783 struct
2784 {
2785 uint64_t offFile;
2786 uint32_t cbMax;
2787 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
2788 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++)
2789 {
2790 memset(pbBuf, 0x55, cbBuf);
2791 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2792 for (size_t offBuf = 0; offBuf < cbBuf; )
2793 {
2794 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
2795 uint32_t const cbToRead = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
2796 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
2797 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
2798 size_t cbActual = 0;
2799 RTTESTI_CHECK_RC(RTFileRead(hFile1, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
2800 if (cbActual == cbToRead)
2801 {
2802 offBuf += cbActual;
2803 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
2804 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
2805 }
2806 else
2807 {
2808 RTTestIFailed("Attempting to read %#x bytes at %#zx, only got %#x bytes back! (cbLeft=%#x cbBuf=%#zx)\n",
2809 cbToRead, offBuf, cbActual, cbLeft, cbBuf);
2810 if (cbActual)
2811 offBuf += cbActual;
2812 else
2813 pbBuf[offBuf++] = 0x11;
2814 }
2815 }
2816 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf);
2817 }
2818
2819 /*
2820 * Test reading beyond the end of the file.
2821 */
2822 size_t const acbMax[] = { cbBuf, _64K, _16K, _4K, 256 };
2823 uint32_t const aoffFromEos[] =
2824 { 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,
2825 4092, 4093, 4094, 4095, 4096, 4097, 4098, 4099, 4100, 8192, 16384, 32767, 32768, 32769, 65535, 65536, _1M - 1
2826 };
2827 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
2828 {
2829 size_t const cbMaxRead = acbMax[iMax];
2830 for (uint32_t iOffFromEos = 0; iOffFromEos < RT_ELEMENTS(aoffFromEos); iOffFromEos++)
2831 {
2832 uint32_t off = aoffFromEos[iOffFromEos];
2833 if (off >= cbMaxRead)
2834 continue;
2835 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2836 size_t cbActual = ~(size_t)0;
2837 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
2838 RTTESTI_CHECK(cbActual == off);
2839
2840 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2841 cbActual = ~(size_t)0;
2842 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, off, &cbActual), VINF_SUCCESS);
2843 RTTESTI_CHECK_MSG(cbActual == off, ("%#zx vs %#zx\n", cbActual, off));
2844
2845 cbActual = ~(size_t)0;
2846 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, 1, &cbActual), VINF_SUCCESS);
2847 RTTESTI_CHECK_MSG(cbActual == 0, ("cbActual=%zu\n", cbActual));
2848
2849 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
2850
2851 /* Repeat using native APIs in case IPRT or other layers hid status codes: */
2852#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
2853 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2854# ifdef RT_OS_OS2
2855 ULONG cbActual2 = ~(ULONG)0;
2856 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
2857 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
2858 RTTESTI_CHECK_MSG(cbActual2 == off, ("%#x vs %#x\n", cbActual2, off));
2859# else
2860 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2861 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
2862 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
2863 if (off == 0)
2864 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
2865 else
2866 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x, expected 0 (off=%#x cbMaxRead=%#zx)\n", rcNt, off, cbMaxRead));
2867 RTTESTI_CHECK_MSG(Ios.Information == off, ("%#zx vs %#x\n", Ios.Information, off));
2868# endif
2869
2870# ifdef RT_OS_OS2
2871 cbActual2 = ~(ULONG)0;
2872 orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, 1, &cbActual2);
2873 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
2874 RTTESTI_CHECK_MSG(cbActual2 == 0, ("cbActual2=%u\n", cbActual2));
2875# else
2876 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2877 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
2878 &Ios, pbBuf, 1, NULL /*poffFile*/, NULL /*Key*/);
2879 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
2880# endif
2881
2882#endif
2883 }
2884 }
2885
2886 /*
2887 * Test reading beyond end of the file.
2888 */
2889 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
2890 {
2891 size_t const cbMaxRead = acbMax[iMax];
2892 for (uint32_t off = 0; off < 256; off++)
2893 {
2894 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2895 size_t cbActual = ~(size_t)0;
2896 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
2897 RTTESTI_CHECK(cbActual == 0);
2898
2899 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
2900
2901 /* Repeat using native APIs in case IPRT or other layers hid status codes: */
2902#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
2903 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2904# ifdef RT_OS_OS2
2905 ULONG cbActual2 = ~(ULONG)0;
2906 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
2907 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
2908 RTTESTI_CHECK_MSG(cbActual2 == 0, ("%#x vs %#x\n", cbActual2, off));
2909# else
2910 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2911 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
2912 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
2913 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
2914# endif
2915#endif
2916 }
2917 }
2918
2919 /*
2920 * Do uncached access, must be page aligned.
2921 */
2922 uint32_t cbPage = PAGE_SIZE;
2923 memset(pbBuf, 0x66, cbBuf);
2924 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
2925 {
2926 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2927 for (size_t offBuf = 0; offBuf < cbBuf; )
2928 {
2929 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / cbPage);
2930 uint32_t const cPagesToRead = RTRandU32Ex(1, cPagesLeft);
2931 size_t const cbToRead = cPagesToRead * (size_t)cbPage;
2932 size_t cbActual = 0;
2933 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
2934 if (cbActual == cbToRead)
2935 offBuf += cbActual;
2936 else
2937 {
2938 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x bytes back!\n", cbToRead, offBuf, cbActual);
2939 if (cbActual)
2940 offBuf += cbActual;
2941 else
2942 {
2943 memset(&pbBuf[offBuf], 0x11, cbPage);
2944 offBuf += cbPage;
2945 }
2946 }
2947 }
2948 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf);
2949 }
2950
2951 /*
2952 * Check reading zero bytes at the end of the file.
2953 * Requires native call because RTFileWrite doesn't call kernel on zero byte reads.
2954 */
2955 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
2956# ifdef RT_OS_WINDOWS
2957 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2958 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
2959 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
2960 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
2961 RTTESTI_CHECK(Ios.Information == 0);
2962
2963 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2964 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 1, NULL, NULL);
2965 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x", rcNt));
2966 RTTESTI_CHECK(Ios.Status == STATUS_END_OF_FILE);
2967 RTTESTI_CHECK(Ios.Information == 0);
2968# else
2969 ssize_t cbRead = read((int)RTFileToNative(hFile1), pbBuf, 0);
2970 RTTESTI_CHECK(cbRead == 0);
2971# endif
2972
2973#else
2974 RT_NOREF(hFileNoCache);
2975#endif
2976
2977 /*
2978 * Scatter read function operation.
2979 */
2980#ifdef RT_OS_WINDOWS
2981 /** @todo RTFileSgReadAt is just a RTFileReadAt loop for windows NT. Need
2982 * to use ReadFileScatter (nocache + page aligned). */
2983#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
2984
2985# ifdef UIO_MAXIOV
2986 RTSGSEG aSegs[UIO_MAXIOV];
2987# else
2988 RTSGSEG aSegs[512];
2989# endif
2990 RTSGBUF SgBuf;
2991 uint32_t cIncr = 1;
2992 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr)
2993 {
2994 size_t const cbSeg = cbBuf / cSegs;
2995 size_t const cbToRead = cbSeg * cSegs;
2996 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
2997 {
2998 aSegs[iSeg].cbSeg = cbSeg;
2999 aSegs[iSeg].pvSeg = &pbBuf[cbToRead - (iSeg + 1) * cbSeg];
3000 }
3001 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3002 int rc = myFileSgReadAt(hFile1, 0, &SgBuf, cbToRead, NULL);
3003 if (RT_SUCCESS(rc))
3004 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3005 {
3006 if (!fsPerfCheckReadBuf(__LINE__, iSeg * cbSeg, &pbBuf[cbToRead - (iSeg + 1) * cbSeg], cbSeg))
3007 {
3008 cSegs = RT_ELEMENTS(aSegs);
3009 break;
3010 }
3011 }
3012 else
3013 {
3014 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToRead=%#zx", rc, cSegs, cbSeg, cbToRead);
3015 break;
3016 }
3017 if (cSegs == 16)
3018 cIncr = 7;
3019 else if (cSegs == 16 * 7 + 16 /*= 128*/)
3020 cIncr = 64;
3021 }
3022
3023 for (uint32_t iTest = 0; iTest < 128; iTest++)
3024 {
3025 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
3026 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
3027 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
3028 size_t cbToRead = 0;
3029 size_t cbLeft = cbBuf;
3030 uint8_t *pbCur = &pbBuf[cbBuf];
3031 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3032 {
3033 uint32_t iAlign = RTRandU32Ex(0, 3);
3034 if (iAlign & 2) /* end is page aligned */
3035 {
3036 cbLeft -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3037 pbCur -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3038 }
3039
3040 size_t cbSegOthers = (cSegs - iSeg) * _8K;
3041 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
3042 : cbLeft > cSegs ? cbLeft - cSegs
3043 : cbLeft;
3044 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
3045 if (iAlign & 1) /* start is page aligned */
3046 cbSeg += ((uintptr_t)pbCur - cbSeg) & PAGE_OFFSET_MASK;
3047
3048 if (iSeg - iZeroSeg < cZeroSegs)
3049 cbSeg = 0;
3050
3051 cbToRead += cbSeg;
3052 cbLeft -= cbSeg;
3053 pbCur -= cbSeg;
3054 aSegs[iSeg].cbSeg = cbSeg;
3055 aSegs[iSeg].pvSeg = pbCur;
3056 }
3057
3058 uint64_t offFile = cbToRead < cbFile ? RTRandU64Ex(0, cbFile - cbToRead) : 0;
3059 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3060 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
3061 if (RT_SUCCESS(rc))
3062 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3063 {
3064 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg))
3065 {
3066 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbToRead=%#zx\n", iSeg, cSegs, aSegs[iSeg].cbSeg, cbToRead);
3067 iTest = _16K;
3068 break;
3069 }
3070 offFile += aSegs[iSeg].cbSeg;
3071 }
3072 else
3073 {
3074 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx", rc, cSegs, cbToRead);
3075 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3076 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
3077 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
3078 break;
3079 }
3080 }
3081
3082 /* reading beyond the end of the file */
3083 for (uint32_t cSegs = 1; cSegs < 6; cSegs++)
3084 for (uint32_t iTest = 0; iTest < 128; iTest++)
3085 {
3086 uint32_t const cbToRead = RTRandU32Ex(0, cbBuf);
3087 uint32_t const cbBeyond = cbToRead ? RTRandU32Ex(0, cbToRead) : 0;
3088 uint32_t const cbSeg = cbToRead / cSegs;
3089 uint32_t cbLeft = cbToRead;
3090 uint8_t *pbCur = &pbBuf[cbToRead];
3091 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3092 {
3093 aSegs[iSeg].cbSeg = iSeg + 1 < cSegs ? cbSeg : cbLeft;
3094 aSegs[iSeg].pvSeg = pbCur -= aSegs[iSeg].cbSeg;
3095 cbLeft -= aSegs[iSeg].cbSeg;
3096 }
3097 Assert(pbCur == pbBuf);
3098
3099 uint64_t offFile = cbFile + cbBeyond - cbToRead;
3100 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3101 int rcExpect = cbBeyond == 0 || cbToRead == 0 ? VINF_SUCCESS : VERR_EOF;
3102 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
3103 if (rc != rcExpect)
3104 {
3105 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx\n", rc, cSegs, cbToRead, cbBeyond);
3106 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3107 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
3108 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
3109 }
3110
3111 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3112 size_t cbActual = 0;
3113 rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, &cbActual);
3114 if (rc != VINF_SUCCESS || cbActual != cbToRead - cbBeyond)
3115 RTTestIFailed("myFileSgReadAt failed: %Rrc cbActual=%#zu - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx expected %#zx\n",
3116 rc, cbActual, cSegs, cbToRead, cbBeyond, cbToRead - cbBeyond);
3117 if (RT_SUCCESS(rc) && cbActual > 0)
3118 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3119 {
3120 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, RT_MIN(cbActual, aSegs[iSeg].cbSeg)))
3121 {
3122 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbActual%#zx cbToRead=%#zx cbBeyond=%#zx\n",
3123 iSeg, cSegs, aSegs[iSeg].cbSeg, cbActual, cbToRead, cbBeyond);
3124 iTest = _16K;
3125 break;
3126 }
3127 if (cbActual <= aSegs[iSeg].cbSeg)
3128 break;
3129 cbActual -= aSegs[iSeg].cbSeg;
3130 offFile += aSegs[iSeg].cbSeg;
3131 }
3132 }
3133
3134#endif
3135
3136 /*
3137 * Other OS specific stuff.
3138 */
3139#ifdef RT_OS_WINDOWS
3140 /* Check that reading at an offset modifies the position: */
3141 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
3142 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
3143
3144 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
3145 LARGE_INTEGER offNt;
3146 offNt.QuadPart = cbFile / 2;
3147 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
3148 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
3149 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
3150 RTTESTI_CHECK(Ios.Information == _4K);
3151 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
3152 fsPerfCheckReadBuf(__LINE__, cbFile / 2, pbBuf, _4K);
3153#endif
3154
3155
3156 RTMemPageFree(pbBuf, cbBuf);
3157}
3158
3159
3160/**
3161 * One RTFileWrite profiling iteration.
3162 */
3163DECL_FORCE_INLINE(int) fsPerfIoWriteWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
3164 uint64_t *poffActual, uint32_t *pcSeeks)
3165{
3166 /* Do we need to seek back to the start? */
3167 if (*poffActual + cbBlock <= cbFile)
3168 { /* likely */ }
3169 else
3170 {
3171 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3172 *pcSeeks += 1;
3173 *poffActual = 0;
3174 }
3175
3176 size_t cbActuallyWritten = 0;
3177 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBlock, cbBlock, &cbActuallyWritten), VINF_SUCCESS, rcCheck);
3178 if (cbActuallyWritten == cbBlock)
3179 {
3180 *poffActual += cbActuallyWritten;
3181 return VINF_SUCCESS;
3182 }
3183 RTTestIFailed("RTFileWrite at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyWritten, cbBlock);
3184 *poffActual += cbActuallyWritten;
3185 return VERR_WRITE_ERROR;
3186}
3187
3188
3189void fsPerfIoWriteBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
3190{
3191 RTTestISubF("IO - Sequential write %RU32", cbBlock);
3192
3193 if (cbBlock <= cbFile)
3194 {
3195 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
3196 if (pbBuf)
3197 {
3198 memset(pbBuf, 0xf7, cbBlock);
3199 PROFILE_IO_FN("RTFileWrite", fsPerfIoWriteWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
3200 RTMemPageFree(pbBuf, cbBlock);
3201 }
3202 else
3203 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
3204 }
3205 else
3206 RTTestSkipped(g_hTest, "test file too small");
3207}
3208
3209
3210/** pwritev is too new to be useful, so we use the writev api via this wrapper. */
3211DECLINLINE(int) myFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
3212{
3213 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
3214 if (RT_SUCCESS(rc))
3215 rc = RTFileSgWrite(hFile, pSgBuf, cbToWrite, pcbWritten);
3216 return rc;
3217}
3218
3219
3220void fsPerfWrite(RTFILE hFile1, RTFILE hFileNoCache, RTFILE hFileWriteThru, uint64_t cbFile)
3221{
3222 RTTestISubF("IO - RTFileWrite");
3223
3224 /*
3225 * Allocate a big buffer we can play around with. Min size is 1MB.
3226 */
3227 size_t cbBuf = cbFile < _64M ? (size_t)cbFile : _64M;
3228 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3229 while (!pbBuf)
3230 {
3231 cbBuf /= 2;
3232 RTTESTI_CHECK_RETV(cbBuf >= _1M);
3233 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
3234 }
3235
3236 uint8_t bFiller = 0x88;
3237
3238#if 1
3239 /*
3240 * Start at the beginning and write out the full buffer in random small chunks, thereby
3241 * checking that unaligned buffer addresses, size and file offsets work fine.
3242 */
3243 struct
3244 {
3245 uint64_t offFile;
3246 uint32_t cbMax;
3247 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
3248 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++, bFiller)
3249 {
3250 fsPerfFillWriteBuf(aRuns[i].offFile, pbBuf, cbBuf, bFiller);
3251 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
3252
3253 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3254 for (size_t offBuf = 0; offBuf < cbBuf; )
3255 {
3256 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
3257 uint32_t const cbToWrite = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
3258 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
3259 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
3260 size_t cbActual = 0;
3261 RTTESTI_CHECK_RC(RTFileWrite(hFile1, &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
3262 if (cbActual == cbToWrite)
3263 {
3264 offBuf += cbActual;
3265 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
3266 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
3267 }
3268 else
3269 {
3270 RTTestIFailed("Attempting to write %#x bytes at %#zx (%#x left), only got %#x written!\n",
3271 cbToWrite, offBuf, cbLeft, cbActual);
3272 if (cbActual)
3273 offBuf += cbActual;
3274 else
3275 pbBuf[offBuf++] = 0x11;
3276 }
3277 }
3278
3279 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, aRuns[i].offFile, pbBuf, cbBuf, NULL), VINF_SUCCESS);
3280 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
3281 }
3282
3283
3284 /*
3285 * Do uncached and write-thru accesses, must be page aligned.
3286 */
3287 RTFILE ahFiles[2] = { hFileWriteThru, hFileNoCache };
3288 for (unsigned iFile = 0; iFile < RT_ELEMENTS(ahFiles); iFile++, bFiller++)
3289 {
3290 if (g_fIgnoreNoCache && ahFiles[iFile] == NIL_RTFILE)
3291 continue;
3292
3293 fsPerfFillWriteBuf(0, pbBuf, cbBuf, bFiller);
3294 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
3295 RTTESTI_CHECK_RC(RTFileSeek(ahFiles[iFile], 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3296
3297 uint32_t cbPage = PAGE_SIZE;
3298 for (size_t offBuf = 0; offBuf < cbBuf; )
3299 {
3300 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / cbPage);
3301 uint32_t const cPagesToWrite = RTRandU32Ex(1, cPagesLeft);
3302 size_t const cbToWrite = cPagesToWrite * (size_t)cbPage;
3303 size_t cbActual = 0;
3304 RTTESTI_CHECK_RC(RTFileWrite(ahFiles[iFile], &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
3305 if (cbActual == cbToWrite)
3306 {
3307 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offBuf, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3308 fsPerfCheckReadBuf(__LINE__, offBuf, pbBuf, cbToWrite, bFiller);
3309 offBuf += cbActual;
3310 }
3311 else
3312 {
3313 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x written!\n", cbToWrite, offBuf, cbActual);
3314 if (cbActual)
3315 offBuf += cbActual;
3316 else
3317 {
3318 memset(&pbBuf[offBuf], 0x11, cbPage);
3319 offBuf += cbPage;
3320 }
3321 }
3322 }
3323
3324 RTTESTI_CHECK_RC(RTFileReadAt(ahFiles[iFile], 0, pbBuf, cbBuf, NULL), VINF_SUCCESS);
3325 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
3326 }
3327
3328 /*
3329 * Check the behavior of writing zero bytes to the file _4K from the end
3330 * using native API. In the olden days zero sized write have been known
3331 * to be used to truncate a file.
3332 */
3333 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -_4K, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
3334# ifdef RT_OS_WINDOWS
3335 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
3336 NTSTATUS rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
3337 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
3338 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
3339 RTTESTI_CHECK(Ios.Information == 0);
3340# else
3341 ssize_t cbWritten = write((int)RTFileToNative(hFile1), pbBuf, 0);
3342 RTTESTI_CHECK(cbWritten == 0);
3343# endif
3344 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, _4K, NULL), VINF_SUCCESS);
3345 fsPerfCheckReadBuf(__LINE__, cbFile - _4K, pbBuf, _4K, pbBuf[0x8]);
3346
3347#else
3348 RT_NOREF(hFileNoCache, hFileWriteThru);
3349#endif
3350
3351 /*
3352 * Gather write function operation.
3353 */
3354#ifdef RT_OS_WINDOWS
3355 /** @todo RTFileSgWriteAt is just a RTFileWriteAt loop for windows NT. Need
3356 * to use WriteFileGather (nocache + page aligned). */
3357#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
3358
3359# ifdef UIO_MAXIOV
3360 RTSGSEG aSegs[UIO_MAXIOV];
3361# else
3362 RTSGSEG aSegs[512];
3363# endif
3364 RTSGBUF SgBuf;
3365 uint32_t cIncr = 1;
3366 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr, bFiller++)
3367 {
3368 size_t const cbSeg = cbBuf / cSegs;
3369 size_t const cbToWrite = cbSeg * cSegs;
3370 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3371 {
3372 aSegs[iSeg].cbSeg = cbSeg;
3373 aSegs[iSeg].pvSeg = &pbBuf[cbToWrite - (iSeg + 1) * cbSeg];
3374 fsPerfFillWriteBuf(iSeg * cbSeg, (uint8_t *)aSegs[iSeg].pvSeg, cbSeg, bFiller);
3375 }
3376 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3377 int rc = myFileSgWriteAt(hFile1, 0, &SgBuf, cbToWrite, NULL);
3378 if (RT_SUCCESS(rc))
3379 {
3380 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, 0, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3381 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbToWrite, bFiller);
3382 }
3383 else
3384 {
3385 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToWrite=%#zx", rc, cSegs, cbSeg, cbToWrite);
3386 break;
3387 }
3388 if (cSegs == 16)
3389 cIncr = 7;
3390 else if (cSegs == 16 * 7 + 16 /*= 128*/)
3391 cIncr = 64;
3392 }
3393
3394 /* random stuff, including zero segments. */
3395 for (uint32_t iTest = 0; iTest < 128; iTest++, bFiller++)
3396 {
3397 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
3398 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
3399 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
3400 size_t cbToWrite = 0;
3401 size_t cbLeft = cbBuf;
3402 uint8_t *pbCur = &pbBuf[cbBuf];
3403 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3404 {
3405 uint32_t iAlign = RTRandU32Ex(0, 3);
3406 if (iAlign & 2) /* end is page aligned */
3407 {
3408 cbLeft -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3409 pbCur -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3410 }
3411
3412 size_t cbSegOthers = (cSegs - iSeg) * _8K;
3413 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
3414 : cbLeft > cSegs ? cbLeft - cSegs
3415 : cbLeft;
3416 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
3417 if (iAlign & 1) /* start is page aligned */
3418 cbSeg += ((uintptr_t)pbCur - cbSeg) & PAGE_OFFSET_MASK;
3419
3420 if (iSeg - iZeroSeg < cZeroSegs)
3421 cbSeg = 0;
3422
3423 cbToWrite += cbSeg;
3424 cbLeft -= cbSeg;
3425 pbCur -= cbSeg;
3426 aSegs[iSeg].cbSeg = cbSeg;
3427 aSegs[iSeg].pvSeg = pbCur;
3428 }
3429
3430 uint64_t const offFile = cbToWrite < cbFile ? RTRandU64Ex(0, cbFile - cbToWrite) : 0;
3431 uint64_t offFill = offFile;
3432 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3433 if (aSegs[iSeg].cbSeg)
3434 {
3435 fsPerfFillWriteBuf(offFill, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg, bFiller);
3436 offFill += aSegs[iSeg].cbSeg;
3437 }
3438
3439 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3440 int rc = myFileSgWriteAt(hFile1, offFile, &SgBuf, cbToWrite, NULL);
3441 if (RT_SUCCESS(rc))
3442 {
3443 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offFile, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3444 fsPerfCheckReadBuf(__LINE__, offFile, pbBuf, cbToWrite, bFiller);
3445 }
3446 else
3447 {
3448 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%#x cbToWrite=%#zx", rc, cSegs, cbToWrite);
3449 break;
3450 }
3451 }
3452
3453#endif
3454
3455 /*
3456 * Other OS specific stuff.
3457 */
3458#ifdef RT_OS_WINDOWS
3459 /* Check that reading at an offset modifies the position: */
3460 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, cbFile / 2, pbBuf, _4K, NULL), VINF_SUCCESS);
3461 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
3462 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
3463
3464 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
3465 LARGE_INTEGER offNt;
3466 offNt.QuadPart = cbFile / 2;
3467 rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
3468 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
3469 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
3470 RTTESTI_CHECK(Ios.Information == _4K);
3471 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
3472#endif
3473
3474 RTMemPageFree(pbBuf, cbBuf);
3475}
3476
3477
3478/**
3479 * Worker for testing RTFileFlush.
3480 */
3481DECL_FORCE_INLINE(int) fsPerfFSyncWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf, uint64_t *poffFile)
3482{
3483 if (*poffFile + cbBuf <= cbFile)
3484 { /* likely */ }
3485 else
3486 {
3487 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3488 *poffFile = 0;
3489 }
3490
3491 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbBuf, NULL), VINF_SUCCESS, rcCheck);
3492 RTTESTI_CHECK_RC_RET(RTFileFlush(hFile1), VINF_SUCCESS, rcCheck);
3493
3494 *poffFile += cbBuf;
3495 return VINF_SUCCESS;
3496}
3497
3498
3499void fsPerfFSync(RTFILE hFile1, uint64_t cbFile)
3500{
3501 RTTestISub("fsync");
3502
3503 RTTESTI_CHECK_RC(RTFileFlush(hFile1), VINF_SUCCESS);
3504
3505 PROFILE_FN(RTFileFlush(hFile1), g_nsTestRun, "RTFileFlush");
3506
3507 size_t cbBuf = PAGE_SIZE;
3508 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3509 RTTESTI_CHECK_RETV(pbBuf != NULL);
3510 memset(pbBuf, 0xf4, cbBuf);
3511
3512 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3513 uint64_t offFile = 0;
3514 PROFILE_FN(fsPerfFSyncWorker(hFile1, cbFile, pbBuf, cbBuf, &offFile), g_nsTestRun, "RTFileWrite[Page]/RTFileFlush");
3515
3516 RTMemPageFree(pbBuf, cbBuf);
3517}
3518
3519
3520#ifndef RT_OS_OS2
3521/**
3522 * Worker for profiling msync.
3523 */
3524DECL_FORCE_INLINE(int) fsPerfMSyncWorker(uint8_t *pbMapping, size_t offMapping, size_t cbFlush, size_t *pcbFlushed)
3525{
3526 uint8_t *pbCur = &pbMapping[offMapping];
3527 for (size_t offFlush = 0; offFlush < cbFlush; offFlush += PAGE_SIZE)
3528 *(size_t volatile *)&pbCur[offFlush + 8] = cbFlush;
3529# ifdef RT_OS_WINDOWS
3530 RTTESTI_CHECK(FlushViewOfFile(pbCur, cbFlush));
3531# else
3532 RTTESTI_CHECK(msync(pbCur, cbFlush, MS_SYNC) == 0);
3533# endif
3534 if (*pcbFlushed < offMapping + cbFlush)
3535 *pcbFlushed = offMapping + cbFlush;
3536 return VINF_SUCCESS;
3537}
3538#endif /* !RT_OS_OS2 */
3539
3540
3541void fsPerfMMap(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
3542{
3543 RTTestISub("mmap");
3544#if !defined(RT_OS_OS2)
3545 static const char * const s_apszStates[] = { "readonly", "writecopy", "readwrite" };
3546 enum { kMMap_ReadOnly = 0, kMMap_WriteCopy, kMMap_ReadWrite, kMMap_End };
3547 for (int enmState = kMMap_ReadOnly; enmState < kMMap_End; enmState++)
3548 {
3549 /*
3550 * Do the mapping.
3551 */
3552 size_t cbMapping = (size_t)cbFile;
3553 if (cbMapping != cbFile)
3554 cbMapping = _256M;
3555 uint8_t *pbMapping;
3556
3557# ifdef RT_OS_WINDOWS
3558 HANDLE hSection;
3559 pbMapping = NULL;
3560 for (;; cbMapping /= 2)
3561 {
3562 hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile1), NULL,
3563 enmState == kMMap_ReadOnly ? PAGE_READONLY
3564 : enmState == kMMap_WriteCopy ? PAGE_WRITECOPY : PAGE_READWRITE,
3565 (uint32_t)((uint64_t)cbMapping >> 32), (uint32_t)cbMapping, NULL);
3566 DWORD dwErr1 = GetLastError();
3567 DWORD dwErr2 = 0;
3568 if (hSection != NULL)
3569 {
3570 pbMapping = (uint8_t *)MapViewOfFile(hSection,
3571 enmState == kMMap_ReadOnly ? FILE_MAP_READ
3572 : enmState == kMMap_WriteCopy ? FILE_MAP_COPY
3573 : FILE_MAP_WRITE,
3574 0, 0, cbMapping);
3575 if (pbMapping)
3576 break;
3577 dwErr2 = GetLastError();
3578 CloseHandle(hSection);
3579 }
3580 if (cbMapping <= _2M)
3581 {
3582 RTTestIFailed("%u/%s: CreateFileMapping or MapViewOfFile failed: %u, %u",
3583 enmState, s_apszStates[enmState], dwErr1, dwErr2);
3584 break;
3585 }
3586 }
3587# else
3588 for (;; cbMapping /= 2)
3589 {
3590 pbMapping = (uint8_t *)mmap(NULL, cbMapping,
3591 enmState == kMMap_ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE,
3592 enmState == kMMap_WriteCopy ? MAP_PRIVATE : MAP_SHARED,
3593 (int)RTFileToNative(hFile1), 0);
3594 if ((void *)pbMapping != MAP_FAILED)
3595 break;
3596 if (cbMapping <= _2M)
3597 {
3598 RTTestIFailed("%u/%s: mmap failed: %s (%u)", enmState, s_apszStates[enmState], strerror(errno), errno);
3599 break;
3600 }
3601 }
3602# endif
3603 if (cbMapping <= _2M)
3604 continue;
3605
3606 /*
3607 * Time page-ins just for fun.
3608 */
3609 size_t const cPages = cbMapping >> PAGE_SHIFT;
3610 size_t uDummy = 0;
3611 uint64_t ns = RTTimeNanoTS();
3612 for (size_t iPage = 0; iPage < cPages; iPage++)
3613 uDummy += ASMAtomicReadU8(&pbMapping[iPage << PAGE_SHIFT]);
3614 ns = RTTimeNanoTS() - ns;
3615 RTTestIValueF(ns / cPages, RTTESTUNIT_NS_PER_OCCURRENCE, "page-in %s", s_apszStates[enmState]);
3616
3617 /* Check the content. */
3618 fsPerfCheckReadBuf(__LINE__, 0, pbMapping, cbMapping);
3619
3620 if (enmState != kMMap_ReadOnly)
3621 {
3622 /* Write stuff to the first two megabytes. In the COW case, we'll detect
3623 corruption of shared data during content checking of the RW iterations. */
3624 fsPerfFillWriteBuf(0, pbMapping, _2M, 0xf7);
3625 if (enmState == kMMap_ReadWrite)
3626 {
3627 /* For RW we can try read back from the file handle and check if we get
3628 a match there first. */
3629 uint8_t abBuf[_4K];
3630 for (uint32_t off = 0; off < _2M; off += sizeof(abBuf))
3631 {
3632 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, off, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
3633 fsPerfCheckReadBuf(__LINE__, off, abBuf, sizeof(abBuf), 0xf7);
3634 }
3635# ifdef RT_OS_WINDOWS
3636 RTTESTI_CHECK(FlushViewOfFile(pbMapping, _2M));
3637# else
3638 RTTESTI_CHECK(msync(pbMapping, _2M, MS_SYNC) == 0);
3639# endif
3640
3641 /*
3642 * Time modifying and flushing a few different number of pages.
3643 */
3644 static size_t const s_acbFlush[] = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 3, PAGE_SIZE * 8, PAGE_SIZE * 16, _2M };
3645 for (unsigned iFlushSize = 0 ; iFlushSize < RT_ELEMENTS(s_acbFlush); iFlushSize++)
3646 {
3647 size_t const cbFlush = s_acbFlush[iFlushSize];
3648 if (cbFlush > cbMapping)
3649 continue;
3650
3651 char szDesc[80];
3652 RTStrPrintf(szDesc, sizeof(szDesc), "touch/flush/%zu", cbFlush);
3653 size_t const cFlushes = cbMapping / cbFlush;
3654 size_t const cbMappingUsed = cFlushes * cbFlush;
3655 size_t cbFlushed = 0;
3656 PROFILE_FN(fsPerfMSyncWorker(pbMapping, (iIteration * cbFlush) % cbMappingUsed, cbFlush, &cbFlushed),
3657 g_nsTestRun, szDesc);
3658
3659 /*
3660 * Check that all the changes made it thru to the file:
3661 */
3662 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
3663 {
3664 size_t cbBuf = _2M;
3665 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3666 if (!pbBuf)
3667 {
3668 cbBuf = _4K;
3669 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3670 }
3671 RTTESTI_CHECK(pbBuf != NULL);
3672 if (pbBuf)
3673 {
3674 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3675 size_t const cbToCheck = RT_MIN(cFlushes * cbFlush, cbFlushed);
3676 unsigned cErrors = 0;
3677 for (size_t offBuf = 0; cErrors < 32 && offBuf < cbToCheck; offBuf += cbBuf)
3678 {
3679 size_t cbToRead = RT_MIN(cbBuf, cbToCheck - offBuf);
3680 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, pbBuf, cbToRead, NULL), VINF_SUCCESS);
3681
3682 for (size_t offFlush = 0; offFlush < cbToRead; offFlush += PAGE_SIZE)
3683 if (*(size_t volatile *)&pbBuf[offFlush + 8] != cbFlush)
3684 {
3685 RTTestIFailed("Flush issue at offset #%zx: %#zx, expected %#zx (cbFlush=%#zx, %#RX64)",
3686 offBuf + offFlush + 8, *(size_t volatile *)&pbBuf[offFlush + 8],
3687 cbFlush, cbFlush, *(uint64_t volatile *)&pbBuf[offFlush]);
3688 if (++cErrors > 32)
3689 break;
3690 }
3691 }
3692 RTMemPageFree(pbBuf, cbBuf);
3693 }
3694 }
3695 }
3696
3697# if 0 /* not needed, very very slow */
3698 /*
3699 * Restore the file to 0xf6 state for the next test.
3700 */
3701 RTTestIPrintf(RTTESTLVL_ALWAYS, "Restoring content...\n");
3702 fsPerfFillWriteBuf(0, pbMapping, cbMapping, 0xf6);
3703# ifdef RT_OS_WINDOWS
3704 RTTESTI_CHECK(FlushViewOfFile(pbMapping, cbMapping));
3705# else
3706 RTTESTI_CHECK(msync(pbMapping, cbMapping, MS_SYNC) == 0);
3707# endif
3708 RTTestIPrintf(RTTESTLVL_ALWAYS, "... done\n");
3709# endif
3710 }
3711 }
3712
3713 /*
3714 * Observe how regular writes affects a read-only or readwrite mapping.
3715 * These should ideally be immediately visible in the mapping, at least
3716 * when not performed thru an no-cache handle.
3717 */
3718 if (enmState == kMMap_ReadOnly || enmState == kMMap_ReadWrite)
3719 {
3720 size_t cbBuf = RT_MIN(_2M, cbMapping / 2);
3721 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3722 if (!pbBuf)
3723 {
3724 cbBuf = _4K;
3725 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3726 }
3727 RTTESTI_CHECK(pbBuf != NULL);
3728 if (pbBuf)
3729 {
3730 /* Do a number of random writes to the file (using hFile1).
3731 Immediately undoing them. */
3732 for (uint32_t i = 0; i < 128; i++)
3733 {
3734 /* Generate a randomly sized write at a random location, making
3735 sure it differs from whatever is there already before writing. */
3736 uint32_t const cbToWrite = RTRandU32Ex(1, (uint32_t)cbBuf);
3737 uint64_t const offToWrite = RTRandU64Ex(0, cbMapping - cbToWrite);
3738
3739 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf8);
3740 pbBuf[0] = ~pbBuf[0];
3741 if (cbToWrite > 1)
3742 pbBuf[cbToWrite - 1] = ~pbBuf[cbToWrite - 1];
3743 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3744
3745 /* Check the mapping. */
3746 if (memcmp(&pbMapping[(size_t)offToWrite], pbBuf, cbToWrite) != 0)
3747 {
3748 RTTestIFailed("Write #%u @ %#RX64 LB %#x was not reflected in the mapping!\n", i, offToWrite, cbToWrite);
3749 }
3750
3751 /* Restore */
3752 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf6);
3753 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3754 }
3755
3756 RTMemPageFree(pbBuf, cbBuf);
3757 }
3758 }
3759
3760 /*
3761 * Unmap it.
3762 */
3763# ifdef RT_OS_WINDOWS
3764 RTTESTI_CHECK(UnmapViewOfFile(pbMapping));
3765 RTTESTI_CHECK(CloseHandle(hSection));
3766# else
3767 RTTESTI_CHECK(munmap(pbMapping, cbMapping) == 0);
3768# endif
3769 }
3770
3771 /*
3772 * Memory mappings without open handles (pretty common).
3773 */
3774 for (uint32_t i = 0; i < 32; i++)
3775 {
3776 /* Create a new file, 256 KB in size, and fill it with random bytes.
3777 Try uncached access if we can to force the page-in to do actual reads. */
3778 char szFile2[RTPATH_MAX + 32];
3779 memcpy(szFile2, g_szDir, g_cchDir);
3780 RTStrPrintf(&szFile2[g_cchDir], sizeof(szFile2) - g_cchDir, "mmap-%u.noh", i);
3781 RTFILE hFile2 = NIL_RTFILE;
3782 int rc = (i & 3) == 3 ? VERR_TRY_AGAIN
3783 : RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_NO_CACHE);
3784 if (RT_FAILURE(rc))
3785 {
3786 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
3787 VINF_SUCCESS);
3788 }
3789
3790 static char s_abContentUnaligned[256*1024 + PAGE_SIZE - 1];
3791 char * const pbContent = &s_abContentUnaligned[PAGE_SIZE - ((uintptr_t)&s_abContentUnaligned[0] & PAGE_OFFSET_MASK)];
3792 size_t const cbContent = 256*1024;
3793 RTRandBytes(pbContent, cbContent);
3794 RTTESTI_CHECK_RC(rc = RTFileWrite(hFile2, pbContent, cbContent, NULL), VINF_SUCCESS);
3795 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
3796 if (RT_SUCCESS(rc))
3797 {
3798 /* Reopen the file with normal caching. Every second time, we also
3799 does a read-only open of it to confuse matters. */
3800 RTFILE hFile3 = NIL_RTFILE;
3801 if ((i & 3) == 3)
3802 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
3803 hFile2 = NIL_RTFILE;
3804 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE),
3805 VINF_SUCCESS);
3806 if ((i & 3) == 1)
3807 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
3808
3809 /* Memory map it read-write (no COW). */
3810#ifdef RT_OS_WINDOWS
3811 HANDLE hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile2), NULL, PAGE_READWRITE, 0, cbContent, NULL);
3812 RTTESTI_CHECK_MSG(hSection != NULL, ("last error %u\n", GetLastError));
3813 uint8_t *pbMapping = (uint8_t *)MapViewOfFile(hSection, FILE_MAP_WRITE, 0, 0, cbContent);
3814 RTTESTI_CHECK_MSG(pbMapping != NULL, ("last error %u\n", GetLastError));
3815 RTTESTI_CHECK_MSG(CloseHandle(hSection), ("last error %u\n", GetLastError));
3816# else
3817 uint8_t *pbMapping = (uint8_t *)mmap(NULL, cbContent, PROT_READ | PROT_WRITE, MAP_SHARED,
3818 (int)RTFileToNative(hFile2), 0);
3819 if ((void *)pbMapping == MAP_FAILED)
3820 pbMapping = NULL;
3821 RTTESTI_CHECK_MSG(pbMapping != NULL, ("errno=%s (%d)\n", strerror(errno), errno));
3822# endif
3823
3824 /* Close the file handles. */
3825 if ((i & 7) == 7)
3826 {
3827 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
3828 hFile3 = NIL_RTFILE;
3829 }
3830 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
3831 if ((i & 7) == 5)
3832 {
3833 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
3834 hFile3 = NIL_RTFILE;
3835 }
3836 if (pbMapping)
3837 {
3838 RTThreadSleep(2); /* fudge for cleanup/whatever */
3839
3840 /* Page in the mapping by comparing with the content we wrote above. */
3841 RTTESTI_CHECK(memcmp(pbMapping, pbContent, cbContent) == 0);
3842
3843 /* Now dirty everything by inverting everything. */
3844 size_t *puCur = (size_t *)pbMapping;
3845 size_t cLeft = cbContent / sizeof(*puCur);
3846 while (cLeft-- > 0)
3847 {
3848 *puCur = ~*puCur;
3849 puCur++;
3850 }
3851
3852 /* Sync it all. */
3853# ifdef RT_OS_WINDOWS
3854 RTTESTI_CHECK(FlushViewOfFile(pbMapping, cbContent));
3855# else
3856 RTTESTI_CHECK(msync(pbMapping, cbContent, MS_SYNC) == 0);
3857# endif
3858
3859 /* Unmap it. */
3860# ifdef RT_OS_WINDOWS
3861 RTTESTI_CHECK(UnmapViewOfFile(pbMapping));
3862# else
3863 RTTESTI_CHECK(munmap(pbMapping, cbContent) == 0);
3864# endif
3865 }
3866
3867 if (hFile3 != NIL_RTFILE)
3868 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
3869 }
3870 RTTESTI_CHECK_RC(RTFileDelete(szFile2), VINF_SUCCESS);
3871 }
3872
3873
3874#else
3875 RTTestSkipped(g_hTest, "not supported/implemented");
3876 RT_NOREF(hFile1, hFileNoCache, cbFile);
3877#endif
3878}
3879
3880
3881/**
3882 * This does the read, write and seek tests.
3883 */
3884void fsPerfIo(void)
3885{
3886 RTTestISub("I/O");
3887
3888 /*
3889 * Determin the size of the test file.
3890 */
3891 g_szDir[g_cchDir] = '\0';
3892 RTFOFF cbFree = 0;
3893 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
3894 uint64_t cbFile = g_cbIoFile;
3895 if (cbFile + _16M < (uint64_t)cbFree)
3896 cbFile = RT_ALIGN_64(cbFile, _64K);
3897 else if (cbFree < _32M)
3898 {
3899 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
3900 return;
3901 }
3902 else
3903 {
3904 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
3905 cbFile = RT_ALIGN_64(cbFile, _64K);
3906 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
3907 }
3908 if (cbFile < _64K)
3909 {
3910 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 64KB", cbFile);
3911 return;
3912 }
3913
3914 /*
3915 * Create a cbFile sized test file.
3916 */
3917 RTFILE hFile1;
3918 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file21")),
3919 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
3920 RTFILE hFileNoCache;
3921 if (!g_fIgnoreNoCache)
3922 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileNoCache, g_szDir,
3923 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE),
3924 VINF_SUCCESS);
3925 else
3926 {
3927 int rc = RTFileOpen(&hFileNoCache, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE);
3928 if (RT_FAILURE(rc))
3929 {
3930 RTTestIPrintf(RTTESTLVL_ALWAYS, "Unable to open I/O file with non-cache flag (%Rrc), skipping related tests.\n", rc);
3931 hFileNoCache = NIL_RTFILE;
3932 }
3933 }
3934 RTFILE hFileWriteThru;
3935 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileWriteThru, g_szDir,
3936 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_WRITE_THROUGH),
3937 VINF_SUCCESS);
3938
3939 uint8_t *pbFree = NULL;
3940 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
3941 RTMemFree(pbFree);
3942 if (RT_SUCCESS(rc))
3943 {
3944 /*
3945 * Do the testing & profiling.
3946 */
3947 if (g_fSeek)
3948 fsPerfIoSeek(hFile1, cbFile);
3949
3950 if (g_fReadTests)
3951 fsPerfRead(hFile1, hFileNoCache, cbFile);
3952 if (g_fReadPerf)
3953 for (unsigned i = 0; i < g_cIoBlocks; i++)
3954 fsPerfIoReadBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
3955#ifdef FSPERF_TEST_SENDFILE
3956 if (g_fSendFile)
3957 fsPerfSendFile(hFile1, cbFile);
3958#endif
3959#ifdef RT_OS_LINUX
3960 if (g_fSplice)
3961 fsPerfSpliceToPipe(hFile1, cbFile);
3962#endif
3963 if (g_fMMap)
3964 fsPerfMMap(hFile1, hFileNoCache, cbFile);
3965
3966 /* This is destructive to the file content. */
3967 if (g_fWriteTests)
3968 fsPerfWrite(hFile1, hFileNoCache, hFileWriteThru, cbFile);
3969 if (g_fWritePerf)
3970 for (unsigned i = 0; i < g_cIoBlocks; i++)
3971 fsPerfIoWriteBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
3972#ifdef RT_OS_LINUX
3973 if (g_fSplice)
3974 fsPerfSpliceToFile(hFile1, cbFile);
3975#endif
3976 if (g_fFSync)
3977 fsPerfFSync(hFile1, cbFile);
3978 }
3979
3980 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
3981 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3982 if (hFileNoCache != NIL_RTFILE || !g_fIgnoreNoCache)
3983 RTTESTI_CHECK_RC(RTFileClose(hFileNoCache), VINF_SUCCESS);
3984 RTTESTI_CHECK_RC(RTFileClose(hFileWriteThru), VINF_SUCCESS);
3985 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
3986}
3987
3988
3989DECL_FORCE_INLINE(int) fsPerfCopyWorker1(const char *pszSrc, const char *pszDst)
3990{
3991 RTFileDelete(pszDst);
3992 return RTFileCopy(pszSrc, pszDst);
3993}
3994
3995
3996#ifdef RT_OS_LINUX
3997DECL_FORCE_INLINE(int) fsPerfCopyWorkerSendFile(RTFILE hFile1, RTFILE hFile2, size_t cbFile)
3998{
3999 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile2, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
4000
4001 loff_t off = 0;
4002 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &off, cbFile);
4003 if (cbSent > 0 && (size_t)cbSent == cbFile)
4004 return 0;
4005
4006 int rc = VERR_GENERAL_FAILURE;
4007 if (cbSent < 0)
4008 {
4009 rc = RTErrConvertFromErrno(errno);
4010 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)", cbFile, cbSent, errno, rc);
4011 }
4012 else
4013 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
4014 cbFile, cbSent, cbFile, cbSent - cbFile);
4015 return rc;
4016}
4017#endif /* RT_OS_LINUX */
4018
4019
4020static void fsPerfCopy(void)
4021{
4022 RTTestISub("copy");
4023
4024 /*
4025 * Non-existing files.
4026 */
4027 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-file")),
4028 InDir2(RT_STR_TUPLE("whatever"))), VERR_FILE_NOT_FOUND);
4029 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
4030 InDir2(RT_STR_TUPLE("no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
4031 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
4032 InDir2(RT_STR_TUPLE("whatever"))), VERR_PATH_NOT_FOUND);
4033
4034 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
4035 InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
4036 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
4037 InDir2(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
4038
4039 /*
4040 * Determin the size of the test file.
4041 * We want to be able to make 1 copy of it.
4042 */
4043 g_szDir[g_cchDir] = '\0';
4044 RTFOFF cbFree = 0;
4045 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
4046 uint64_t cbFile = g_cbIoFile;
4047 if (cbFile + _16M < (uint64_t)cbFree)
4048 cbFile = RT_ALIGN_64(cbFile, _64K);
4049 else if (cbFree < _32M)
4050 {
4051 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
4052 return;
4053 }
4054 else
4055 {
4056 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
4057 cbFile = RT_ALIGN_64(cbFile, _64K);
4058 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
4059 }
4060 if (cbFile < _512K * 2)
4061 {
4062 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 1MB", cbFile);
4063 return;
4064 }
4065 cbFile /= 2;
4066
4067 /*
4068 * Create a cbFile sized test file.
4069 */
4070 RTFILE hFile1;
4071 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file22")),
4072 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
4073 uint8_t *pbFree = NULL;
4074 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
4075 RTMemFree(pbFree);
4076 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4077 if (RT_SUCCESS(rc))
4078 {
4079 /*
4080 * Make copies.
4081 */
4082 /* plain */
4083 RTFileDelete(InDir2(RT_STR_TUPLE("file23")));
4084 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VINF_SUCCESS);
4085 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VERR_ALREADY_EXISTS);
4086 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4087
4088 /* by handle */
4089 hFile1 = NIL_RTFILE;
4090 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4091 RTFILE hFile2 = NIL_RTFILE;
4092 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4093 RTTESTI_CHECK_RC(RTFileCopyByHandles(hFile1, hFile2), VINF_SUCCESS);
4094 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4095 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4096 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4097
4098 /* copy part */
4099 hFile1 = NIL_RTFILE;
4100 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4101 hFile2 = NIL_RTFILE;
4102 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4103 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, 0, hFile2, 0, cbFile / 2, 0, NULL), VINF_SUCCESS);
4104 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, cbFile / 2, hFile2, cbFile / 2, cbFile - cbFile / 2, 0, NULL), VINF_SUCCESS);
4105 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4106 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4107 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4108
4109#ifdef RT_OS_LINUX
4110 /*
4111 * On linux we can also use sendfile between two files, except for 2.5.x to 2.6.33.
4112 */
4113 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_C(0x7ffff000));
4114 char szRelease[64];
4115 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
4116 bool const fSendFileBetweenFiles = RTStrVersionCompare(szRelease, "2.5.0") < 0
4117 || RTStrVersionCompare(szRelease, "2.6.33") >= 0;
4118 if (fSendFileBetweenFiles)
4119 {
4120 /* Copy the whole file: */
4121 hFile1 = NIL_RTFILE;
4122 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4123 RTFileDelete(g_szDir2);
4124 hFile2 = NIL_RTFILE;
4125 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4126 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbFile);
4127 if (cbSent < 0)
4128 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
4129 cbFile, cbSent, errno, RTErrConvertFromErrno(errno));
4130 else if ((size_t)cbSent != cbFileMax)
4131 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
4132 cbFile, cbSent, cbFileMax, cbSent - cbFileMax);
4133 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4134 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4135 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4136
4137 /* Try copy a little bit too much: */
4138 if (cbFile == cbFileMax)
4139 {
4140 hFile1 = NIL_RTFILE;
4141 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4142 RTFileDelete(g_szDir2);
4143 hFile2 = NIL_RTFILE;
4144 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4145 size_t cbToCopy = cbFile + RTRandU32Ex(1, _64M);
4146 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbToCopy);
4147 if (cbSent < 0)
4148 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
4149 cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
4150 else if ((size_t)cbSent != cbFile)
4151 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
4152 cbToCopy, cbSent, cbFile, cbSent - cbFile);
4153 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4154 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4155 }
4156
4157 /* Do partial copy: */
4158 hFile2 = NIL_RTFILE;
4159 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4160 for (uint32_t i = 0; i < 64; i++)
4161 {
4162 size_t cbToCopy = RTRandU32Ex(0, cbFileMax - 1);
4163 uint32_t const offFile = RTRandU32Ex(1, (uint64_t)RT_MIN(cbFileMax - cbToCopy, UINT32_MAX));
4164 RTTESTI_CHECK_RC_BREAK(RTFileSeek(hFile2, offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4165 loff_t offFile2 = offFile;
4166 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &offFile2, cbToCopy);
4167 if (cbSent < 0)
4168 RTTestIFailed("sendfile(file,file,%#x,%#zx) failed (%zd): %d (%Rrc)",
4169 offFile, cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
4170 else if ((size_t)cbSent != cbToCopy)
4171 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx, expected %#zx (diff %zd)",
4172 offFile, cbToCopy, cbSent, cbToCopy, cbSent - cbToCopy);
4173 else if (offFile2 != (loff_t)(offFile + cbToCopy))
4174 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx + off=%#RX64, expected off %#x",
4175 offFile, cbToCopy, cbSent, offFile2, offFile + cbToCopy);
4176 }
4177 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4178 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4179 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4180 }
4181#endif
4182
4183 /*
4184 * Do some benchmarking.
4185 */
4186#define PROFILE_COPY_FN(a_szOperation, a_fnCall) \
4187 do \
4188 { \
4189 /* Estimate how many iterations we need to fill up the given timeslot: */ \
4190 fsPerfYield(); \
4191 uint64_t nsStart = RTTimeNanoTS(); \
4192 uint64_t ns; \
4193 do \
4194 ns = RTTimeNanoTS(); \
4195 while (ns == nsStart); \
4196 nsStart = ns; \
4197 \
4198 uint64_t iIteration = 0; \
4199 do \
4200 { \
4201 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4202 iIteration++; \
4203 ns = RTTimeNanoTS() - nsStart; \
4204 } while (ns < RT_NS_10MS); \
4205 ns /= iIteration; \
4206 if (ns > g_nsPerNanoTSCall + 32) \
4207 ns -= g_nsPerNanoTSCall; \
4208 uint64_t cIterations = g_nsTestRun / ns; \
4209 if (cIterations < 2) \
4210 cIterations = 2; \
4211 else if (cIterations & 1) \
4212 cIterations++; \
4213 \
4214 /* Do the actual profiling: */ \
4215 iIteration = 0; \
4216 fsPerfYield(); \
4217 nsStart = RTTimeNanoTS(); \
4218 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
4219 { \
4220 for (; iIteration < cIterations; iIteration++)\
4221 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4222 ns = RTTimeNanoTS() - nsStart;\
4223 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
4224 break; \
4225 cIterations += cIterations / 4; \
4226 if (cIterations & 1) \
4227 cIterations++; \
4228 nsStart += g_nsPerNanoTSCall; \
4229 } \
4230 RTTestIValueF(ns / iIteration, \
4231 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation " latency"); \
4232 RTTestIValueF((uint64_t)((uint64_t)iIteration * cbFile / ((double)ns / RT_NS_1SEC)), \
4233 RTTESTUNIT_BYTES_PER_SEC, a_szOperation " throughput"); \
4234 RTTestIValueF((uint64_t)iIteration * cbFile, \
4235 RTTESTUNIT_BYTES, a_szOperation " bytes"); \
4236 RTTestIValueF(iIteration, \
4237 RTTESTUNIT_OCCURRENCES, a_szOperation " iterations"); \
4238 if (g_fShowDuration) \
4239 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation " duration"); \
4240 } while (0)
4241
4242 PROFILE_COPY_FN("RTFileCopy/Replace", fsPerfCopyWorker1(g_szDir, g_szDir2));
4243
4244 hFile1 = NIL_RTFILE;
4245 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4246 RTFileDelete(g_szDir2);
4247 hFile2 = NIL_RTFILE;
4248 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4249 PROFILE_COPY_FN("RTFileCopyByHandles/Overwrite", RTFileCopyByHandles(hFile1, hFile2));
4250 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4251 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4252
4253 /* We could benchmark RTFileCopyPart with various block sizes and whatnot...
4254 But it's currently well covered by the two previous operations. */
4255
4256#ifdef RT_OS_LINUX
4257 if (fSendFileBetweenFiles)
4258 {
4259 hFile1 = NIL_RTFILE;
4260 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4261 RTFileDelete(g_szDir2);
4262 hFile2 = NIL_RTFILE;
4263 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4264 PROFILE_COPY_FN("sendfile/overwrite", fsPerfCopyWorkerSendFile(hFile1, hFile2, cbFileMax));
4265 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4266 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4267 }
4268#endif
4269 }
4270
4271 /*
4272 * Clean up.
4273 */
4274 RTFileDelete(InDir2(RT_STR_TUPLE("file22c1")));
4275 RTFileDelete(InDir2(RT_STR_TUPLE("file22c2")));
4276 RTFileDelete(InDir2(RT_STR_TUPLE("file22c3")));
4277 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
4278}
4279
4280
4281/**
4282 * Display the usage to @a pStrm.
4283 */
4284static void Usage(PRTSTREAM pStrm)
4285{
4286 char szExec[RTPATH_MAX];
4287 RTStrmPrintf(pStrm, "usage: %s <-d <testdir>> [options]\n",
4288 RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
4289 RTStrmPrintf(pStrm, "\n");
4290 RTStrmPrintf(pStrm, "options: \n");
4291
4292 for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
4293 {
4294 char szHelp[80];
4295 const char *pszHelp;
4296 switch (g_aCmdOptions[i].iShort)
4297 {
4298 case 'd': pszHelp = "The directory to use for testing. default: CWD/fstestdir"; break;
4299 case 'e': pszHelp = "Enables all tests. default: -e"; break;
4300 case 'z': pszHelp = "Disables all tests. default: -e"; break;
4301 case 's': pszHelp = "Set benchmark duration in seconds. default: 10 sec"; break;
4302 case 'm': pszHelp = "Set benchmark duration in milliseconds. default: 10000 ms"; break;
4303 case 'v': pszHelp = "More verbose execution."; break;
4304 case 'q': pszHelp = "Quiet execution."; break;
4305 case 'h': pszHelp = "Displays this help and exit"; break;
4306 case 'V': pszHelp = "Displays the program revision"; break;
4307 case kCmdOpt_ShowDuration: pszHelp = "Show duration of profile runs. default: --no-show-duration"; break;
4308 case kCmdOpt_NoShowDuration: pszHelp = "Hide duration of profile runs. default: --no-show-duration"; break;
4309 case kCmdOpt_ShowIterations: pszHelp = "Show iteration count for profile runs. default: --no-show-iterations"; break;
4310 case kCmdOpt_NoShowIterations: pszHelp = "Hide iteration count for profile runs. default: --no-show-iterations"; break;
4311 case kCmdOpt_ManyFiles: pszHelp = "Count of files in big test dir. default: --many-files 10000"; break;
4312 case kCmdOpt_NoManyFiles: pszHelp = "Skip big test dir with many files. default: --many-files 10000"; break;
4313 case kCmdOpt_ManyTreeFilesPerDir: pszHelp = "Count of files per directory in test tree. default: 640"; break;
4314 case kCmdOpt_ManyTreeSubdirsPerDir: pszHelp = "Count of subdirs per directory in test tree. default: 16"; break;
4315 case kCmdOpt_ManyTreeDepth: pszHelp = "Depth of test tree (not counting root). default: 1"; break;
4316 case kCmdOpt_IgnoreNoCache: pszHelp = "Ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
4317 case kCmdOpt_NoIgnoreNoCache: pszHelp = "Do not ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
4318 case kCmdOpt_IoFileSize: pszHelp = "Size of file used for I/O tests. default: 512 MB"; break;
4319 case kCmdOpt_SetBlockSize: pszHelp = "Sets single I/O block size (in bytes)."; break;
4320 case kCmdOpt_AddBlockSize: pszHelp = "Adds an I/O block size (in bytes)."; break;
4321 default:
4322 if (g_aCmdOptions[i].iShort >= kCmdOpt_First)
4323 {
4324 if (RTStrStartsWith(g_aCmdOptions[i].pszLong, "--no-"))
4325 RTStrPrintf(szHelp, sizeof(szHelp), "Disables the '%s' test.", g_aCmdOptions[i].pszLong + 5);
4326 else
4327 RTStrPrintf(szHelp, sizeof(szHelp), "Enables the '%s' test.", g_aCmdOptions[i].pszLong + 2);
4328 pszHelp = szHelp;
4329 }
4330 else
4331 pszHelp = "Option undocumented";
4332 break;
4333 }
4334 if ((unsigned)g_aCmdOptions[i].iShort < 127U)
4335 {
4336 char szOpt[64];
4337 RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
4338 RTStrmPrintf(pStrm, " %-19s %s\n", szOpt, pszHelp);
4339 }
4340 else
4341 RTStrmPrintf(pStrm, " %-19s %s\n", g_aCmdOptions[i].pszLong, pszHelp);
4342 }
4343}
4344
4345
4346static uint32_t fsPerfCalcManyTreeFiles(void)
4347{
4348 uint32_t cDirs = 1;
4349 for (uint32_t i = 0, cDirsAtLevel = 1; i < g_cManyTreeDepth; i++)
4350 {
4351 cDirs += cDirsAtLevel * g_cManyTreeSubdirsPerDir;
4352 cDirsAtLevel *= g_cManyTreeSubdirsPerDir;
4353 }
4354 return g_cManyTreeFilesPerDir * cDirs;
4355}
4356
4357
4358int main(int argc, char *argv[])
4359{
4360 /*
4361 * Init IPRT and globals.
4362 */
4363 int rc = RTTestInitAndCreate("FsPerf", &g_hTest);
4364 if (rc)
4365 return rc;
4366 RTListInit(&g_ManyTreeHead);
4367
4368 /*
4369 * Default values.
4370 */
4371 rc = RTPathGetCurrent(g_szDir, sizeof(g_szDir) / 2);
4372 if (RT_SUCCESS(rc))
4373 rc = RTPathAppend(g_szDir, sizeof(g_szDir) / 2, "fstestdir-");
4374 if (RT_SUCCESS(rc))
4375 {
4376 g_cchDir = strlen(g_szDir);
4377 g_cchDir += RTStrPrintf(&g_szDir[g_cchDir], sizeof(g_szDir) - g_cchDir, "%u" RTPATH_SLASH_STR, RTProcSelf());
4378 }
4379 else
4380 {
4381 RTTestFailed(g_hTest, "RTPathGetCurrent (or RTPathAppend) failed: %Rrc\n", rc);
4382 return RTTestSummaryAndDestroy(g_hTest);
4383 }
4384
4385 RTGETOPTUNION ValueUnion;
4386 RTGETOPTSTATE GetState;
4387 RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
4388 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
4389 {
4390 switch (rc)
4391 {
4392 case 'd':
4393 rc = RTPathAbs(ValueUnion.psz, g_szDir, sizeof(g_szDir) / 2);
4394 if (RT_SUCCESS(rc))
4395 {
4396 RTPathEnsureTrailingSeparator(g_szDir, sizeof(g_szDir));
4397 g_cchDir = strlen(g_szDir);
4398 break;
4399 }
4400 RTTestFailed(g_hTest, "RTPathAbs(%s) failed: %Rrc\n", ValueUnion.psz, rc);
4401 return RTTestSummaryAndDestroy(g_hTest);
4402
4403 case 's':
4404 if (ValueUnion.u32 == 0)
4405 g_nsTestRun = RT_NS_1SEC_64 * 10;
4406 else
4407 g_nsTestRun = ValueUnion.u32 * RT_NS_1SEC_64;
4408 break;
4409
4410 case 'm':
4411 if (ValueUnion.u64 == 0)
4412 g_nsTestRun = RT_NS_1SEC_64 * 10;
4413 else
4414 g_nsTestRun = ValueUnion.u64 * RT_NS_1MS;
4415 break;
4416
4417 case 'e':
4418 g_fManyFiles = true;
4419 g_fOpen = true;
4420 g_fFStat = true;
4421 g_fFChMod = true;
4422 g_fFUtimes = true;
4423 g_fStat = true;
4424 g_fChMod = true;
4425 g_fUtimes = true;
4426 g_fRename = true;
4427 g_fDirOpen = true;
4428 g_fDirEnum = true;
4429 g_fMkRmDir = true;
4430 g_fStatVfs = true;
4431 g_fRm = true;
4432 g_fChSize = true;
4433 g_fReadTests = true;
4434 g_fReadPerf = true;
4435#ifdef FSPERF_TEST_SENDFILE
4436 g_fSendFile = true;
4437#endif
4438#ifdef RT_OS_LINUX
4439 g_fSplice = true;
4440#endif
4441 g_fWriteTests= true;
4442 g_fWritePerf = true;
4443 g_fSeek = true;
4444 g_fFSync = true;
4445 g_fMMap = true;
4446 g_fCopy = true;
4447 break;
4448
4449 case 'z':
4450 g_fManyFiles = false;
4451 g_fOpen = false;
4452 g_fFStat = false;
4453 g_fFChMod = false;
4454 g_fFUtimes = false;
4455 g_fStat = false;
4456 g_fChMod = false;
4457 g_fUtimes = false;
4458 g_fRename = false;
4459 g_fDirOpen = false;
4460 g_fDirEnum = false;
4461 g_fMkRmDir = false;
4462 g_fStatVfs = false;
4463 g_fRm = false;
4464 g_fChSize = false;
4465 g_fReadTests = false;
4466 g_fReadPerf = false;
4467#ifdef FSPERF_TEST_SENDFILE
4468 g_fSendFile = false;
4469#endif
4470#ifdef RT_OS_LINUX
4471 g_fSplice = false;
4472#endif
4473 g_fWriteTests= false;
4474 g_fWritePerf = false;
4475 g_fSeek = false;
4476 g_fFSync = false;
4477 g_fMMap = false;
4478 g_fCopy = false;
4479 break;
4480
4481#define CASE_OPT(a_Stem) \
4482 case RT_CONCAT(kCmdOpt_,a_Stem): RT_CONCAT(g_f,a_Stem) = true; break; \
4483 case RT_CONCAT(kCmdOpt_No,a_Stem): RT_CONCAT(g_f,a_Stem) = false; break
4484 CASE_OPT(Open);
4485 CASE_OPT(FStat);
4486 CASE_OPT(FChMod);
4487 CASE_OPT(FUtimes);
4488 CASE_OPT(Stat);
4489 CASE_OPT(ChMod);
4490 CASE_OPT(Utimes);
4491 CASE_OPT(Rename);
4492 CASE_OPT(DirOpen);
4493 CASE_OPT(DirEnum);
4494 CASE_OPT(MkRmDir);
4495 CASE_OPT(StatVfs);
4496 CASE_OPT(Rm);
4497 CASE_OPT(ChSize);
4498 CASE_OPT(ReadTests);
4499 CASE_OPT(ReadPerf);
4500#ifdef FSPERF_TEST_SENDFILE
4501 CASE_OPT(SendFile);
4502#endif
4503#ifdef RT_OS_LINUX
4504 CASE_OPT(Splice);
4505#endif
4506 CASE_OPT(WriteTests);
4507 CASE_OPT(WritePerf);
4508 CASE_OPT(Seek);
4509 CASE_OPT(FSync);
4510 CASE_OPT(MMap);
4511 CASE_OPT(IgnoreNoCache);
4512 CASE_OPT(Copy);
4513
4514 CASE_OPT(ShowDuration);
4515 CASE_OPT(ShowIterations);
4516#undef CASE_OPT
4517
4518 case kCmdOpt_ManyFiles:
4519 g_fManyFiles = ValueUnion.u32 > 0;
4520 g_cManyFiles = ValueUnion.u32;
4521 break;
4522
4523 case kCmdOpt_NoManyFiles:
4524 g_fManyFiles = false;
4525 break;
4526
4527 case kCmdOpt_ManyTreeFilesPerDir:
4528 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= _64M)
4529 {
4530 g_cManyTreeFilesPerDir = ValueUnion.u32;
4531 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
4532 break;
4533 }
4534 RTTestFailed(g_hTest, "Out of range --files-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4535 return RTTestSummaryAndDestroy(g_hTest);
4536
4537 case kCmdOpt_ManyTreeSubdirsPerDir:
4538 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= 1024)
4539 {
4540 g_cManyTreeSubdirsPerDir = ValueUnion.u32;
4541 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
4542 break;
4543 }
4544 RTTestFailed(g_hTest, "Out of range --subdirs-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4545 return RTTestSummaryAndDestroy(g_hTest);
4546
4547 case kCmdOpt_ManyTreeDepth:
4548 if (ValueUnion.u32 <= 8)
4549 {
4550 g_cManyTreeDepth = ValueUnion.u32;
4551 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
4552 break;
4553 }
4554 RTTestFailed(g_hTest, "Out of range --tree-depth value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4555 return RTTestSummaryAndDestroy(g_hTest);
4556
4557 case kCmdOpt_IoFileSize:
4558 if (ValueUnion.u64 == 0)
4559 g_cbIoFile = _512M;
4560 else
4561 g_cbIoFile = ValueUnion.u64;
4562 break;
4563
4564 case kCmdOpt_SetBlockSize:
4565 if (ValueUnion.u32 > 0)
4566 {
4567 g_cIoBlocks = 1;
4568 g_acbIoBlocks[0] = ValueUnion.u32;
4569 }
4570 else
4571 {
4572 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4573 return RTTestSummaryAndDestroy(g_hTest);
4574 }
4575 break;
4576
4577 case kCmdOpt_AddBlockSize:
4578 if (g_cIoBlocks >= RT_ELEMENTS(g_acbIoBlocks))
4579 RTTestFailed(g_hTest, "Too many I/O block sizes: max %u\n", RT_ELEMENTS(g_acbIoBlocks));
4580 else if (ValueUnion.u32 == 0)
4581 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4582 else
4583 {
4584 g_acbIoBlocks[g_cIoBlocks++] = ValueUnion.u32;
4585 break;
4586 }
4587 return RTTestSummaryAndDestroy(g_hTest);
4588
4589 case 'q':
4590 g_uVerbosity = 0;
4591 break;
4592
4593 case 'v':
4594 g_uVerbosity++;
4595 break;
4596
4597 case 'h':
4598 Usage(g_pStdOut);
4599 return RTEXITCODE_SUCCESS;
4600
4601 case 'V':
4602 {
4603 char szRev[] = "$Revision: 77969 $";
4604 szRev[RT_ELEMENTS(szRev) - 2] = '\0';
4605 RTPrintf(RTStrStrip(strchr(szRev, ':') + 1));
4606 return RTEXITCODE_SUCCESS;
4607 }
4608
4609 default:
4610 return RTGetOptPrintError(rc, &ValueUnion);
4611 }
4612 }
4613
4614 /*
4615 * Create the test directory with an 'empty' subdirectory under it,
4616 * execute the tests, and remove directory when done.
4617 */
4618 RTTestBanner(g_hTest);
4619 if (!RTPathExists(g_szDir))
4620 {
4621 /* The base dir: */
4622 rc = RTDirCreate(g_szDir, 0755,
4623 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
4624 if (RT_SUCCESS(rc))
4625 {
4626 RTTestIPrintf(RTTESTLVL_ALWAYS, "Test dir: %s\n", g_szDir);
4627 rc = fsPrepTestArea();
4628 if (RT_SUCCESS(rc))
4629 {
4630 /* Profile RTTimeNanoTS(). */
4631 fsPerfNanoTS();
4632
4633 /* Do tests: */
4634 if (g_fManyFiles)
4635 fsPerfManyFiles();
4636 if (g_fOpen)
4637 fsPerfOpen();
4638 if (g_fFStat)
4639 fsPerfFStat();
4640 if (g_fFChMod)
4641 fsPerfFChMod();
4642 if (g_fFUtimes)
4643 fsPerfFUtimes();
4644 if (g_fStat)
4645 fsPerfStat();
4646 if (g_fChMod)
4647 fsPerfChmod();
4648 if (g_fUtimes)
4649 fsPerfUtimes();
4650 if (g_fRename)
4651 fsPerfRename();
4652 if (g_fDirOpen)
4653 vsPerfDirOpen();
4654 if (g_fDirEnum)
4655 vsPerfDirEnum();
4656 if (g_fMkRmDir)
4657 fsPerfMkRmDir();
4658 if (g_fStatVfs)
4659 fsPerfStatVfs();
4660 if (g_fRm || g_fManyFiles)
4661 fsPerfRm(); /* deletes manyfiles and manytree */
4662 if (g_fChSize)
4663 fsPerfChSize();
4664 if ( g_fReadPerf || g_fReadTests || g_fWritePerf || g_fWriteTests
4665#ifdef FSPERF_TEST_SENDFILE
4666 || g_fSendFile
4667#endif
4668#ifdef RT_OS_LINUX
4669 || g_fSplice
4670#endif
4671 || g_fSeek || g_fFSync || g_fMMap)
4672 fsPerfIo();
4673 if (g_fCopy)
4674 fsPerfCopy();
4675 }
4676
4677 /* Cleanup: */
4678 g_szDir[g_cchDir] = '\0';
4679 rc = RTDirRemoveRecursive(g_szDir, RTDIRRMREC_F_CONTENT_AND_DIR);
4680 if (RT_FAILURE(rc))
4681 RTTestFailed(g_hTest, "RTDirRemoveRecursive(%s,) -> %Rrc\n", g_szDir, rc);
4682 }
4683 else
4684 RTTestFailed(g_hTest, "RTDirCreate(%s) -> %Rrc\n", g_szDir, rc);
4685 }
4686 else
4687 RTTestFailed(g_hTest, "Test directory already exists: %s\n", g_szDir);
4688
4689 return RTTestSummaryAndDestroy(g_hTest);
4690}
4691
Note: See TracBrowser for help on using the repository browser.

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