VirtualBox

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

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

FsPerf: Fixed the no-file-handle testcase so it doesn't try write unaligned buffers to RTFILE_O_NO_CACHE handles. Would crash in memcmp later, as the file size was 0. bugref:9172

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