VirtualBox

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

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

FsPerf: Added '--relative-dir' option to work around PATH_MAX limitations on linux. bugref:9172

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