VirtualBox

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

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

FsPerf: Bigger path buffers. bugref:9172

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