VirtualBox

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

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

FsPerf: Do the splice() syscall ourselves (forgot) so we can build this against really ancient glibc versions. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 187.0 KB
Line 
1/* $Id: FsPerf.cpp 77928 2019-03-28 00:05:23Z 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) fsPerfSpliceRecvThread(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 fsPerfSpliceSendFile(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, fsPerfSpliceRecvThread, 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 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0) failed (%zd): %d (%Rrc), offFileIn=%#RX64\n",
2294 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileIn);
2295 break;
2296 }
2297 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
2298 if ((uint64_t)offFileIn != offFile + (uint64_t)cbActual)
2299 {
2300 RTTestIFailed("%u: splice(file, &%#RX64, pipe, NULL, %#zx, 0): %#zx; offFileIn=%#RX64, expected %#RX64\n",
2301 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileIn, offFile + (uint64_t)cbActual);
2302 break;
2303 }
2304 if (cbActual > 0)
2305 {
2306 pArgs->cCalls++;
2307 offFile += (size_t)cbActual;
2308 cbTotal += (size_t)cbActual;
2309 cbLeft -= (size_t)cbActual;
2310 }
2311 else
2312 break;
2313 } while (cbLeft > 0);
2314
2315 if (cbTotal != pArgs->cbSent)
2316 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
2317
2318 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
2319 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
2320
2321 if (pArgs->tsThreadDone >= tsStart)
2322 return RT_MAX(pArgs->tsThreadDone - tsStart, 1);
2323 }
2324 return 0;
2325}
2326
2327
2328static void fsPerfSpliceToPipe(RTFILE hFile1, uint64_t cbFile)
2329{
2330 RTTestISub("splice/to-pipe");
2331
2332 /*
2333 * splice was introduced in 2.6.17 according to the man-page.
2334 */
2335 char szRelease[64];
2336 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
2337 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
2338 {
2339 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
2340 return;
2341 }
2342
2343 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - PAGE_OFFSET_MASK);
2344 signal(SIGPIPE, SIG_IGN);
2345
2346 /*
2347 * Allocate a buffer.
2348 */
2349 FSPERFSPLICEARGS Args;
2350 Args.cbBuf = RT_MIN(cbFileMax, _16M);
2351 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2352 while (!Args.pbBuf)
2353 {
2354 Args.cbBuf /= 8;
2355 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
2356 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2357 }
2358
2359 /*
2360 * First iteration with default buffer content.
2361 */
2362 fsPerfSpliceSendFile(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, true /*fCheckBuf*/, __LINE__);
2363 if (cbFileMax == cbFile)
2364 fsPerfSpliceSendFile(&Args, hFile1, 63, cbFileMax, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
2365 else
2366 fsPerfSpliceSendFile(&Args, hFile1, 63, cbFileMax - 63, cbFileMax - 63, 0xf6, true /*fCheckBuf*/, __LINE__);
2367
2368 /*
2369 * Write a block using the regular API and then send it, checking that
2370 * the any caching that sendfile does is correctly updated.
2371 */
2372 uint8_t bFiller = 0xf6;
2373 size_t cbToSend = RT_MIN(cbFileMax, Args.cbBuf);
2374 do
2375 {
2376 fsPerfSpliceSendFile(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__); /* prime cache */
2377
2378 bFiller += 1;
2379 fsPerfFillWriteBuf(0, Args.pbBuf, cbToSend, bFiller);
2380 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, cbToSend, NULL), VINF_SUCCESS);
2381
2382 fsPerfSpliceSendFile(&Args, hFile1, 0, cbToSend, cbToSend, bFiller, true /*fCheckBuf*/, __LINE__);
2383
2384 cbToSend /= 2;
2385 } while (cbToSend >= PAGE_SIZE && ((unsigned)bFiller - 0xf7U) < 64);
2386
2387 /*
2388 * Restore buffer content
2389 */
2390 bFiller = 0xf6;
2391 fsPerfFillWriteBuf(0, Args.pbBuf, Args.cbBuf, bFiller);
2392 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, 0, Args.pbBuf, Args.cbBuf, NULL), VINF_SUCCESS);
2393
2394 /*
2395 * Do 128 random sends.
2396 */
2397 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
2398 for (uint32_t iTest = 0; iTest < 128; iTest++)
2399 {
2400 cbToSend = (size_t)RTRandU64Ex(1, iTest < 64 ? cbSmall : cbFileMax);
2401 uint64_t const offToSendFrom = RTRandU64Ex(0, cbFile - 1);
2402 uint64_t const cbSent = offToSendFrom + cbToSend <= cbFile ? cbToSend : cbFile - offToSendFrom;
2403
2404 fsPerfSpliceSendFile(&Args, hFile1, offToSendFrom, cbToSend, cbSent, bFiller, true /*fCheckBuf*/, __LINE__);
2405 }
2406
2407 /*
2408 * Benchmark it.
2409 */
2410 Args.cCalls = 0;
2411 uint32_t cIterations = 0;
2412 uint64_t nsElapsed = 0;
2413 for (;;)
2414 {
2415 uint64_t cNsThis = fsPerfSpliceSendFile(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
2416 nsElapsed += cNsThis;
2417 cIterations++;
2418 if (!cNsThis || nsElapsed >= g_nsTestRun)
2419 break;
2420 }
2421 uint64_t cbTotal = cbFileMax * cIterations;
2422 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
2423 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
2424 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
2425 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
2426 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
2427 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
2428 if (g_fShowDuration)
2429 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
2430
2431 /*
2432 * Cleanup.
2433 */
2434 RTMemFree(Args.pbBuf);
2435}
2436
2437
2438/** Thread sending the bytes to a splice() call. */
2439static DECLCALLBACK(int) fsPerfSpliceSendThread(RTTHREAD hSelf, void *pvUser)
2440{
2441 FSPERFSPLICEARGS *pArgs = (FSPERFSPLICEARGS *)pvUser;
2442 int rc = VINF_SUCCESS;
2443
2444 uint64_t offFile = pArgs->offFile;
2445 uint64_t cbTotalSent = 0;
2446 while (cbTotalSent < pArgs->cbSent)
2447 {
2448 size_t const cbToSend = RT_MIN(pArgs->cbBuf, pArgs->cbSent - cbTotalSent);
2449 fsPerfFillWriteBuf(offFile, pArgs->pbBuf, cbToSend, pArgs->bFiller);
2450 RTTEST_CHECK_RC_BREAK(g_hTest, rc = RTPipeWriteBlocking(pArgs->hPipe, pArgs->pbBuf, cbToSend, NULL), VINF_SUCCESS);
2451 offFile += cbToSend;
2452 cbTotalSent += cbToSend;
2453 }
2454
2455 pArgs->tsThreadDone = RTTimeNanoTS();
2456
2457 RTTEST_CHECK_RC(g_hTest, RTPipeClose(pArgs->hPipe), VINF_SUCCESS);
2458 pArgs->hPipe = NIL_RTPIPE;
2459
2460 RT_NOREF(hSelf);
2461 return rc;
2462}
2463
2464
2465/** Fill hFile1 via a pipe and the Linux-specific splice() syscall. */
2466static uint64_t fsPerfSpliceWriteFile(FSPERFSPLICEARGS *pArgs, RTFILE hFile1, uint64_t offFile,
2467 size_t cbSend, uint64_t cbSent, uint8_t bFiller, bool fCheckFile, unsigned iLine)
2468{
2469 /* Copy parameters to the argument structure: */
2470 pArgs->offFile = offFile;
2471 pArgs->cbSend = cbSend;
2472 pArgs->cbSent = cbSent;
2473 pArgs->bFiller = bFiller;
2474 pArgs->fCheckBuf = false;
2475
2476 /* Create a socket pair. */
2477 pArgs->hPipe = NIL_RTPIPE;
2478 RTPIPE hPipeR = NIL_RTPIPE;
2479 RTTESTI_CHECK_RC_RET(RTPipeCreate(&hPipeR, &pArgs->hPipe, 0 /*fFlags*/), VINF_SUCCESS, 0);
2480
2481 /* Create the receiving thread: */
2482 int rc;
2483 RTTHREAD hThread = NIL_RTTHREAD;
2484 RTTESTI_CHECK_RC(rc = RTThreadCreate(&hThread, fsPerfSpliceSendThread, pArgs, 0,
2485 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "splicerecv"), VINF_SUCCESS);
2486 if (RT_SUCCESS(rc))
2487 {
2488 /*
2489 * Do the splicing.
2490 */
2491 uint64_t const tsStart = RTTimeNanoTS();
2492 size_t cbLeft = cbSend;
2493 size_t cbTotal = 0;
2494 do
2495 {
2496 loff_t offFileOut = offFile;
2497 ssize_t cbActual = syscall_splice((int)RTPipeToNative(hPipeR), NULL, (int)RTFileToNative(hFile1), &offFileOut,
2498 cbLeft, 0 /*fFlags*/);
2499 int const iErr = errno;
2500 if (RT_UNLIKELY(cbActual < 0))
2501 {
2502 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0) failed (%zd): %d (%Rrc), offFileOut=%#RX64\n",
2503 iLine, offFile, cbLeft, cbActual, iErr, RTErrConvertFromErrno(iErr), (uint64_t)offFileOut);
2504 break;
2505 }
2506 RTTESTI_CHECK_BREAK((uint64_t)cbActual <= cbLeft);
2507 if ((uint64_t)offFileOut != offFile + (uint64_t)cbActual)
2508 {
2509 RTTestIFailed("%u: splice(pipe, NULL, file, &%#RX64, %#zx, 0): %#zx; offFileOut=%#RX64, expected %#RX64\n",
2510 iLine, offFile, cbLeft, cbActual, (uint64_t)offFileOut, offFile + (uint64_t)cbActual);
2511 break;
2512 }
2513 if (cbActual > 0)
2514 {
2515 pArgs->cCalls++;
2516 offFile += (size_t)cbActual;
2517 cbTotal += (size_t)cbActual;
2518 cbLeft -= (size_t)cbActual;
2519 }
2520 else
2521 break;
2522 } while (cbLeft > 0);
2523 uint64_t const nsElapsed = RTTimeNanoTS() - tsStart;
2524
2525 if (cbTotal != pArgs->cbSent)
2526 RTTestIFailed("%u: spliced a total of %#zx bytes, expected %#zx!\n", iLine, cbTotal, pArgs->cbSent);
2527
2528 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
2529 RTTESTI_CHECK_RC(RTThreadWait(hThread, 30 * RT_NS_1SEC, NULL), VINF_SUCCESS);
2530
2531 /* Check the file content. */
2532 if (fCheckFile && cbTotal == pArgs->cbSent)
2533 {
2534 offFile = pArgs->offFile;
2535 cbLeft = cbSent;
2536 while (cbLeft > 0)
2537 {
2538 size_t cbToRead = RT_MIN(cbLeft, pArgs->cbBuf);
2539 RTTESTI_CHECK_RC_BREAK(RTFileReadAt(hFile1, offFile, pArgs->pbBuf, cbToRead, NULL), VINF_SUCCESS);
2540 if (!fsPerfCheckReadBuf(iLine, offFile, pArgs->pbBuf, cbToRead, pArgs->bFiller))
2541 break;
2542 offFile += cbToRead;
2543 cbLeft -= cbToRead;
2544 }
2545 }
2546 return nsElapsed;
2547 }
2548 return 0;
2549}
2550
2551
2552static void fsPerfSpliceToFile(RTFILE hFile1, uint64_t cbFile)
2553{
2554 RTTestISub("splice/to-file");
2555
2556 /*
2557 * splice was introduced in 2.6.17 according to the man-page.
2558 */
2559 char szRelease[64];
2560 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
2561 if (RTStrVersionCompare(szRelease, "2.6.17") < 0)
2562 {
2563 RTTestPassed(g_hTest, "too old kernel (%s)", szRelease);
2564 return;
2565 }
2566
2567 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_MAX - PAGE_OFFSET_MASK);
2568 signal(SIGPIPE, SIG_IGN);
2569
2570 /*
2571 * Allocate a buffer.
2572 */
2573 FSPERFSPLICEARGS Args;
2574 Args.cbBuf = RT_MIN(cbFileMax, _16M);
2575 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2576 while (!Args.pbBuf)
2577 {
2578 Args.cbBuf /= 8;
2579 RTTESTI_CHECK_RETV(Args.cbBuf >= _64K);
2580 Args.pbBuf = (uint8_t *)RTMemAlloc(Args.cbBuf);
2581 }
2582
2583 /*
2584 * Do the whole file.
2585 */
2586 uint8_t bFiller = 0x76;
2587 fsPerfSpliceWriteFile(&Args, hFile1, 0, cbFileMax, cbFileMax, bFiller, true /*fCheckFile*/, __LINE__);
2588
2589 /*
2590 * Do 64 random chunks (this is slower).
2591 */
2592 uint64_t const cbSmall = RT_MIN(_256K, cbFileMax / 16);
2593 for (uint32_t iTest = 0; iTest < 64; iTest++)
2594 {
2595 size_t const cbToWrite = (size_t)RTRandU64Ex(1, iTest < 24 ? cbSmall : cbFileMax);
2596 uint64_t const offToWriteAt = RTRandU64Ex(0, cbFile - cbToWrite);
2597 uint64_t const cbTryRead = cbToWrite + (iTest & 1 ? RTRandU32Ex(0, _64K) : 0);
2598
2599 bFiller++;
2600 fsPerfSpliceWriteFile(&Args, hFile1, offToWriteAt, cbTryRead, cbToWrite, bFiller, true /*fCheckFile*/, __LINE__);
2601 }
2602
2603 /*
2604 * Benchmark it.
2605 */
2606 Args.cCalls = 0;
2607 uint32_t cIterations = 0;
2608 uint64_t nsElapsed = 0;
2609 for (;;)
2610 {
2611 uint64_t cNsThis = fsPerfSpliceWriteFile(&Args, hFile1, 0, cbFileMax, cbFileMax, 0xf6, false /*fCheckBuf*/, __LINE__);
2612 nsElapsed += cNsThis;
2613 cIterations++;
2614 if (!cNsThis || nsElapsed >= g_nsTestRun)
2615 break;
2616 }
2617 uint64_t cbTotal = cbFileMax * cIterations;
2618 RTTestIValue("latency", nsElapsed / Args.cCalls, RTTESTUNIT_NS_PER_CALL);
2619 RTTestIValue("throughput", (uint64_t)(cbTotal / ((double)nsElapsed / RT_NS_1SEC)), RTTESTUNIT_BYTES_PER_SEC);
2620 RTTestIValue("calls", Args.cCalls, RTTESTUNIT_CALLS);
2621 RTTestIValue("bytes/call", cbTotal / Args.cCalls, RTTESTUNIT_BYTES);
2622 RTTestIValue("iterations", cIterations, RTTESTUNIT_NONE);
2623 RTTestIValue("bytes", cbTotal, RTTESTUNIT_BYTES);
2624 if (g_fShowDuration)
2625 RTTestIValue("duration", nsElapsed, RTTESTUNIT_NS);
2626
2627 /*
2628 * Cleanup.
2629 */
2630 RTMemFree(Args.pbBuf);
2631}
2632
2633#endif /* RT_OS_LINUX */
2634
2635/** For fsPerfIoRead and fsPerfIoWrite. */
2636#define PROFILE_IO_FN(a_szOperation, a_fnCall) \
2637 do \
2638 { \
2639 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); \
2640 uint64_t offActual = 0; \
2641 uint32_t cSeeks = 0; \
2642 \
2643 /* Estimate how many iterations we need to fill up the given timeslot: */ \
2644 fsPerfYield(); \
2645 uint64_t nsStart = RTTimeNanoTS(); \
2646 uint64_t ns; \
2647 do \
2648 ns = RTTimeNanoTS(); \
2649 while (ns == nsStart); \
2650 nsStart = ns; \
2651 \
2652 uint64_t iIteration = 0; \
2653 do \
2654 { \
2655 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
2656 iIteration++; \
2657 ns = RTTimeNanoTS() - nsStart; \
2658 } while (ns < RT_NS_10MS); \
2659 ns /= iIteration; \
2660 if (ns > g_nsPerNanoTSCall + 32) \
2661 ns -= g_nsPerNanoTSCall; \
2662 uint64_t cIterations = g_nsTestRun / ns; \
2663 if (cIterations < 2) \
2664 cIterations = 2; \
2665 else if (cIterations & 1) \
2666 cIterations++; \
2667 \
2668 /* Do the actual profiling: */ \
2669 cSeeks = 0; \
2670 iIteration = 0; \
2671 fsPerfYield(); \
2672 nsStart = RTTimeNanoTS(); \
2673 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
2674 { \
2675 for (; iIteration < cIterations; iIteration++)\
2676 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
2677 ns = RTTimeNanoTS() - nsStart;\
2678 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
2679 break; \
2680 cIterations += cIterations / 4; \
2681 if (cIterations & 1) \
2682 cIterations++; \
2683 nsStart += g_nsPerNanoTSCall; \
2684 } \
2685 RTTestIValueF(ns / iIteration, \
2686 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation "/seq/%RU32 latency", cbBlock); \
2687 RTTestIValueF((uint64_t)((uint64_t)iIteration * cbBlock / ((double)ns / RT_NS_1SEC)), \
2688 RTTESTUNIT_BYTES_PER_SEC, a_szOperation "/seq/%RU32 throughput", cbBlock); \
2689 RTTestIValueF(iIteration, \
2690 RTTESTUNIT_CALLS, a_szOperation "/seq/%RU32 calls", cbBlock); \
2691 RTTestIValueF((uint64_t)iIteration * cbBlock, \
2692 RTTESTUNIT_BYTES, a_szOperation "/seq/%RU32 bytes", cbBlock); \
2693 RTTestIValueF(cSeeks, \
2694 RTTESTUNIT_OCCURRENCES, a_szOperation "/seq/%RU32 seeks", cbBlock); \
2695 if (g_fShowDuration) \
2696 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation "/seq/%RU32 duration", cbBlock); \
2697 } while (0)
2698
2699
2700/**
2701 * One RTFileRead profiling iteration.
2702 */
2703DECL_FORCE_INLINE(int) fsPerfIoReadWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
2704 uint64_t *poffActual, uint32_t *pcSeeks)
2705{
2706 /* Do we need to seek back to the start? */
2707 if (*poffActual + cbBlock <= cbFile)
2708 { /* likely */ }
2709 else
2710 {
2711 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
2712 *pcSeeks += 1;
2713 *poffActual = 0;
2714 }
2715
2716 size_t cbActuallyRead = 0;
2717 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBlock, cbBlock, &cbActuallyRead), VINF_SUCCESS, rcCheck);
2718 if (cbActuallyRead == cbBlock)
2719 {
2720 *poffActual += cbActuallyRead;
2721 return VINF_SUCCESS;
2722 }
2723 RTTestIFailed("RTFileRead at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyRead, cbBlock);
2724 *poffActual += cbActuallyRead;
2725 return VERR_READ_ERROR;
2726}
2727
2728
2729void fsPerfIoReadBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
2730{
2731 RTTestISubF("IO - Sequential read %RU32", cbBlock);
2732
2733 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
2734 if (pbBuf)
2735 {
2736 memset(pbBuf, 0xf7, cbBlock);
2737 PROFILE_IO_FN("RTFileRead", fsPerfIoReadWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
2738 RTMemPageFree(pbBuf, cbBlock);
2739 }
2740 else
2741 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
2742}
2743
2744
2745/** preadv is too new to be useful, so we use the readv api via this wrapper. */
2746DECLINLINE(int) myFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
2747{
2748 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
2749 if (RT_SUCCESS(rc))
2750 rc = RTFileSgRead(hFile, pSgBuf, cbToRead, pcbRead);
2751 return rc;
2752}
2753
2754
2755void fsPerfRead(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
2756{
2757 RTTestISubF("IO - RTFileRead");
2758
2759 /*
2760 * Allocate a big buffer we can play around with. Min size is 1MB.
2761 */
2762 size_t cbBuf = cbFile < _64M ? (size_t)cbFile : _64M;
2763 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
2764 while (!pbBuf)
2765 {
2766 cbBuf /= 2;
2767 RTTESTI_CHECK_RETV(cbBuf >= _1M);
2768 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
2769 }
2770
2771#if 1
2772 /*
2773 * Start at the beginning and read the full buffer in random small chunks, thereby
2774 * checking that unaligned buffer addresses, size and file offsets work fine.
2775 */
2776 struct
2777 {
2778 uint64_t offFile;
2779 uint32_t cbMax;
2780 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
2781 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++)
2782 {
2783 memset(pbBuf, 0x55, cbBuf);
2784 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2785 for (size_t offBuf = 0; offBuf < cbBuf; )
2786 {
2787 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
2788 uint32_t const cbToRead = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
2789 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
2790 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
2791 size_t cbActual = 0;
2792 RTTESTI_CHECK_RC(RTFileRead(hFile1, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
2793 if (cbActual == cbToRead)
2794 {
2795 offBuf += cbActual;
2796 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
2797 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
2798 }
2799 else
2800 {
2801 RTTestIFailed("Attempting to read %#x bytes at %#zx, only got %#x bytes back! (cbLeft=%#x cbBuf=%#zx)\n",
2802 cbToRead, offBuf, cbActual, cbLeft, cbBuf);
2803 if (cbActual)
2804 offBuf += cbActual;
2805 else
2806 pbBuf[offBuf++] = 0x11;
2807 }
2808 }
2809 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf);
2810 }
2811
2812 /*
2813 * Test reading beyond the end of the file.
2814 */
2815 size_t const acbMax[] = { cbBuf, _64K, _16K, _4K, 256 };
2816 uint32_t const aoffFromEos[] =
2817 { 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,
2818 4092, 4093, 4094, 4095, 4096, 4097, 4098, 4099, 4100, 8192, 16384, 32767, 32768, 32769, 65535, 65536, _1M - 1
2819 };
2820 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
2821 {
2822 size_t const cbMaxRead = acbMax[iMax];
2823 for (uint32_t iOffFromEos = 0; iOffFromEos < RT_ELEMENTS(aoffFromEos); iOffFromEos++)
2824 {
2825 uint32_t off = aoffFromEos[iOffFromEos];
2826 if (off >= cbMaxRead)
2827 continue;
2828 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2829 size_t cbActual = ~(size_t)0;
2830 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
2831 RTTESTI_CHECK(cbActual == off);
2832
2833 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2834 cbActual = ~(size_t)0;
2835 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, off, &cbActual), VINF_SUCCESS);
2836 RTTESTI_CHECK_MSG(cbActual == off, ("%#zx vs %#zx\n", cbActual, off));
2837
2838 cbActual = ~(size_t)0;
2839 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, 1, &cbActual), VINF_SUCCESS);
2840 RTTESTI_CHECK_MSG(cbActual == 0, ("cbActual=%zu\n", cbActual));
2841
2842 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
2843
2844 /* Repeat using native APIs in case IPRT or other layers hid status codes: */
2845#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
2846 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2847# ifdef RT_OS_OS2
2848 ULONG cbActual2 = ~(ULONG)0;
2849 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
2850 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
2851 RTTESTI_CHECK_MSG(cbActual2 == off, ("%#x vs %#x\n", cbActual2, off));
2852# else
2853 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2854 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
2855 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
2856 if (off == 0)
2857 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
2858 else
2859 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x, expected 0 (off=%#x cbMaxRead=%#zx)\n", rcNt, off, cbMaxRead));
2860 RTTESTI_CHECK_MSG(Ios.Information == off, ("%#zx vs %#x\n", Ios.Information, off));
2861# endif
2862
2863# ifdef RT_OS_OS2
2864 cbActual2 = ~(ULONG)0;
2865 orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, 1, &cbActual2);
2866 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
2867 RTTESTI_CHECK_MSG(cbActual2 == 0, ("cbActual2=%u\n", cbActual2));
2868# else
2869 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2870 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
2871 &Ios, pbBuf, 1, NULL /*poffFile*/, NULL /*Key*/);
2872 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
2873# endif
2874
2875#endif
2876 }
2877 }
2878
2879 /*
2880 * Test reading beyond end of the file.
2881 */
2882 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
2883 {
2884 size_t const cbMaxRead = acbMax[iMax];
2885 for (uint32_t off = 0; off < 256; off++)
2886 {
2887 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2888 size_t cbActual = ~(size_t)0;
2889 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
2890 RTTESTI_CHECK(cbActual == 0);
2891
2892 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
2893
2894 /* Repeat using native APIs in case IPRT or other layers hid status codes: */
2895#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
2896 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2897# ifdef RT_OS_OS2
2898 ULONG cbActual2 = ~(ULONG)0;
2899 APIRET orc = DosRead((HFILE)RTFileToNative(hFile1), pbBuf, cbMaxRead, &cbActual2);
2900 RTTESTI_CHECK_MSG(orc == NO_ERROR, ("orc=%u, expected 0\n", orc));
2901 RTTESTI_CHECK_MSG(cbActual2 == 0, ("%#x vs %#x\n", cbActual2, off));
2902# else
2903 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2904 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL /*hEvent*/, NULL /*ApcRoutine*/, NULL /*ApcContext*/,
2905 &Ios, pbBuf, (ULONG)cbMaxRead, NULL /*poffFile*/, NULL /*Key*/);
2906 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x, expected %#x\n", rcNt, STATUS_END_OF_FILE));
2907# endif
2908#endif
2909 }
2910 }
2911
2912 /*
2913 * Do uncached access, must be page aligned.
2914 */
2915 uint32_t cbPage = PAGE_SIZE;
2916 memset(pbBuf, 0x66, cbBuf);
2917 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
2918 {
2919 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2920 for (size_t offBuf = 0; offBuf < cbBuf; )
2921 {
2922 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / cbPage);
2923 uint32_t const cPagesToRead = RTRandU32Ex(1, cPagesLeft);
2924 size_t const cbToRead = cPagesToRead * (size_t)cbPage;
2925 size_t cbActual = 0;
2926 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
2927 if (cbActual == cbToRead)
2928 offBuf += cbActual;
2929 else
2930 {
2931 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x bytes back!\n", cbToRead, offBuf, cbActual);
2932 if (cbActual)
2933 offBuf += cbActual;
2934 else
2935 {
2936 memset(&pbBuf[offBuf], 0x11, cbPage);
2937 offBuf += cbPage;
2938 }
2939 }
2940 }
2941 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf);
2942 }
2943
2944 /*
2945 * Check reading zero bytes at the end of the file.
2946 * Requires native call because RTFileWrite doesn't call kernel on zero byte reads.
2947 */
2948 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
2949# ifdef RT_OS_WINDOWS
2950 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2951 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
2952 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
2953 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
2954 RTTESTI_CHECK(Ios.Information == 0);
2955
2956 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2957 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 1, NULL, NULL);
2958 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x", rcNt));
2959 RTTESTI_CHECK(Ios.Status == STATUS_END_OF_FILE);
2960 RTTESTI_CHECK(Ios.Information == 0);
2961# else
2962 ssize_t cbRead = read((int)RTFileToNative(hFile1), pbBuf, 0);
2963 RTTESTI_CHECK(cbRead == 0);
2964# endif
2965
2966#else
2967 RT_NOREF(hFileNoCache);
2968#endif
2969
2970 /*
2971 * Scatter read function operation.
2972 */
2973#ifdef RT_OS_WINDOWS
2974 /** @todo RTFileSgReadAt is just a RTFileReadAt loop for windows NT. Need
2975 * to use ReadFileScatter (nocache + page aligned). */
2976#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
2977
2978# ifdef UIO_MAXIOV
2979 RTSGSEG aSegs[UIO_MAXIOV];
2980# else
2981 RTSGSEG aSegs[512];
2982# endif
2983 RTSGBUF SgBuf;
2984 uint32_t cIncr = 1;
2985 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr)
2986 {
2987 size_t const cbSeg = cbBuf / cSegs;
2988 size_t const cbToRead = cbSeg * cSegs;
2989 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
2990 {
2991 aSegs[iSeg].cbSeg = cbSeg;
2992 aSegs[iSeg].pvSeg = &pbBuf[cbToRead - (iSeg + 1) * cbSeg];
2993 }
2994 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
2995 int rc = myFileSgReadAt(hFile1, 0, &SgBuf, cbToRead, NULL);
2996 if (RT_SUCCESS(rc))
2997 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
2998 {
2999 if (!fsPerfCheckReadBuf(__LINE__, iSeg * cbSeg, &pbBuf[cbToRead - (iSeg + 1) * cbSeg], cbSeg))
3000 {
3001 cSegs = RT_ELEMENTS(aSegs);
3002 break;
3003 }
3004 }
3005 else
3006 {
3007 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToRead=%#zx", rc, cSegs, cbSeg, cbToRead);
3008 break;
3009 }
3010 if (cSegs == 16)
3011 cIncr = 7;
3012 else if (cSegs == 16 * 7 + 16 /*= 128*/)
3013 cIncr = 64;
3014 }
3015
3016 for (uint32_t iTest = 0; iTest < 128; iTest++)
3017 {
3018 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
3019 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
3020 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
3021 size_t cbToRead = 0;
3022 size_t cbLeft = cbBuf;
3023 uint8_t *pbCur = &pbBuf[cbBuf];
3024 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3025 {
3026 uint32_t iAlign = RTRandU32Ex(0, 3);
3027 if (iAlign & 2) /* end is page aligned */
3028 {
3029 cbLeft -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3030 pbCur -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3031 }
3032
3033 size_t cbSegOthers = (cSegs - iSeg) * _8K;
3034 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
3035 : cbLeft > cSegs ? cbLeft - cSegs
3036 : cbLeft;
3037 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
3038 if (iAlign & 1) /* start is page aligned */
3039 cbSeg += ((uintptr_t)pbCur - cbSeg) & PAGE_OFFSET_MASK;
3040
3041 if (iSeg - iZeroSeg < cZeroSegs)
3042 cbSeg = 0;
3043
3044 cbToRead += cbSeg;
3045 cbLeft -= cbSeg;
3046 pbCur -= cbSeg;
3047 aSegs[iSeg].cbSeg = cbSeg;
3048 aSegs[iSeg].pvSeg = pbCur;
3049 }
3050
3051 uint64_t offFile = cbToRead < cbFile ? RTRandU64Ex(0, cbFile - cbToRead) : 0;
3052 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3053 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
3054 if (RT_SUCCESS(rc))
3055 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3056 {
3057 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg))
3058 {
3059 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbToRead=%#zx\n", iSeg, cSegs, aSegs[iSeg].cbSeg, cbToRead);
3060 iTest = _16K;
3061 break;
3062 }
3063 offFile += aSegs[iSeg].cbSeg;
3064 }
3065 else
3066 {
3067 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx", rc, cSegs, cbToRead);
3068 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3069 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
3070 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
3071 break;
3072 }
3073 }
3074
3075 /* reading beyond the end of the file */
3076 for (uint32_t cSegs = 1; cSegs < 6; cSegs++)
3077 for (uint32_t iTest = 0; iTest < 128; iTest++)
3078 {
3079 uint32_t const cbToRead = RTRandU32Ex(0, cbBuf);
3080 uint32_t const cbBeyond = cbToRead ? RTRandU32Ex(0, cbToRead) : 0;
3081 uint32_t const cbSeg = cbToRead / cSegs;
3082 uint32_t cbLeft = cbToRead;
3083 uint8_t *pbCur = &pbBuf[cbToRead];
3084 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3085 {
3086 aSegs[iSeg].cbSeg = iSeg + 1 < cSegs ? cbSeg : cbLeft;
3087 aSegs[iSeg].pvSeg = pbCur -= aSegs[iSeg].cbSeg;
3088 cbLeft -= aSegs[iSeg].cbSeg;
3089 }
3090 Assert(pbCur == pbBuf);
3091
3092 uint64_t offFile = cbFile + cbBeyond - cbToRead;
3093 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3094 int rcExpect = cbBeyond == 0 || cbToRead == 0 ? VINF_SUCCESS : VERR_EOF;
3095 int rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, NULL);
3096 if (rc != rcExpect)
3097 {
3098 RTTestIFailed("myFileSgReadAt failed: %Rrc - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx\n", rc, cSegs, cbToRead, cbBeyond);
3099 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3100 RTTestIFailureDetails("aSeg[%u] = %p LB %#zx (last %p)\n", iSeg, aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg,
3101 (uint8_t *)aSegs[iSeg].pvSeg + aSegs[iSeg].cbSeg - 1);
3102 }
3103
3104 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3105 size_t cbActual = 0;
3106 rc = myFileSgReadAt(hFile1, offFile, &SgBuf, cbToRead, &cbActual);
3107 if (rc != VINF_SUCCESS || cbActual != cbToRead - cbBeyond)
3108 RTTestIFailed("myFileSgReadAt failed: %Rrc cbActual=%#zu - cSegs=%#x cbToRead=%#zx cbBeyond=%#zx expected %#zx\n",
3109 rc, cbActual, cSegs, cbToRead, cbBeyond, cbToRead - cbBeyond);
3110 if (RT_SUCCESS(rc) && cbActual > 0)
3111 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3112 {
3113 if (!fsPerfCheckReadBuf(__LINE__, offFile, (uint8_t *)aSegs[iSeg].pvSeg, RT_MIN(cbActual, aSegs[iSeg].cbSeg)))
3114 {
3115 RTTestIFailureDetails("iSeg=%#x cSegs=%#x cbSeg=%#zx cbActual%#zx cbToRead=%#zx cbBeyond=%#zx\n",
3116 iSeg, cSegs, aSegs[iSeg].cbSeg, cbActual, cbToRead, cbBeyond);
3117 iTest = _16K;
3118 break;
3119 }
3120 if (cbActual <= aSegs[iSeg].cbSeg)
3121 break;
3122 cbActual -= aSegs[iSeg].cbSeg;
3123 offFile += aSegs[iSeg].cbSeg;
3124 }
3125 }
3126
3127#endif
3128
3129 /*
3130 * Other OS specific stuff.
3131 */
3132#ifdef RT_OS_WINDOWS
3133 /* Check that reading at an offset modifies the position: */
3134 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
3135 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
3136
3137 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
3138 LARGE_INTEGER offNt;
3139 offNt.QuadPart = cbFile / 2;
3140 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
3141 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
3142 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
3143 RTTESTI_CHECK(Ios.Information == _4K);
3144 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
3145 fsPerfCheckReadBuf(__LINE__, cbFile / 2, pbBuf, _4K);
3146#endif
3147
3148
3149 RTMemPageFree(pbBuf, cbBuf);
3150}
3151
3152
3153/**
3154 * One RTFileWrite profiling iteration.
3155 */
3156DECL_FORCE_INLINE(int) fsPerfIoWriteWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
3157 uint64_t *poffActual, uint32_t *pcSeeks)
3158{
3159 /* Do we need to seek back to the start? */
3160 if (*poffActual + cbBlock <= cbFile)
3161 { /* likely */ }
3162 else
3163 {
3164 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3165 *pcSeeks += 1;
3166 *poffActual = 0;
3167 }
3168
3169 size_t cbActuallyWritten = 0;
3170 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBlock, cbBlock, &cbActuallyWritten), VINF_SUCCESS, rcCheck);
3171 if (cbActuallyWritten == cbBlock)
3172 {
3173 *poffActual += cbActuallyWritten;
3174 return VINF_SUCCESS;
3175 }
3176 RTTestIFailed("RTFileWrite at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyWritten, cbBlock);
3177 *poffActual += cbActuallyWritten;
3178 return VERR_WRITE_ERROR;
3179}
3180
3181
3182void fsPerfIoWriteBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
3183{
3184 RTTestISubF("IO - Sequential write %RU32", cbBlock);
3185
3186 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
3187 if (pbBuf)
3188 {
3189 memset(pbBuf, 0xf7, cbBlock);
3190 PROFILE_IO_FN("RTFileWrite", fsPerfIoWriteWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
3191 RTMemPageFree(pbBuf, cbBlock);
3192 }
3193 else
3194 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
3195}
3196
3197
3198/** pwritev is too new to be useful, so we use the writev api via this wrapper. */
3199DECLINLINE(int) myFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
3200{
3201 int rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
3202 if (RT_SUCCESS(rc))
3203 rc = RTFileSgWrite(hFile, pSgBuf, cbToWrite, pcbWritten);
3204 return rc;
3205}
3206
3207
3208void fsPerfWrite(RTFILE hFile1, RTFILE hFileNoCache, RTFILE hFileWriteThru, uint64_t cbFile)
3209{
3210 RTTestISubF("IO - RTFileWrite");
3211
3212 /*
3213 * Allocate a big buffer we can play around with. Min size is 1MB.
3214 */
3215 size_t cbBuf = cbFile < _64M ? (size_t)cbFile : _64M;
3216 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3217 while (!pbBuf)
3218 {
3219 cbBuf /= 2;
3220 RTTESTI_CHECK_RETV(cbBuf >= _1M);
3221 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
3222 }
3223
3224 uint8_t bFiller = 0x88;
3225
3226#if 1
3227 /*
3228 * Start at the beginning and write out the full buffer in random small chunks, thereby
3229 * checking that unaligned buffer addresses, size and file offsets work fine.
3230 */
3231 struct
3232 {
3233 uint64_t offFile;
3234 uint32_t cbMax;
3235 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
3236 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++, bFiller)
3237 {
3238 fsPerfFillWriteBuf(aRuns[i].offFile, pbBuf, cbBuf, bFiller);
3239 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
3240
3241 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3242 for (size_t offBuf = 0; offBuf < cbBuf; )
3243 {
3244 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
3245 uint32_t const cbToWrite = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
3246 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
3247 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
3248 size_t cbActual = 0;
3249 RTTESTI_CHECK_RC(RTFileWrite(hFile1, &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
3250 if (cbActual == cbToWrite)
3251 {
3252 offBuf += cbActual;
3253 RTTESTI_CHECK_MSG(RTFileTell(hFile1) == aRuns[i].offFile + offBuf,
3254 ("%#RX64, expected %#RX64\n", RTFileTell(hFile1), aRuns[i].offFile + offBuf));
3255 }
3256 else
3257 {
3258 RTTestIFailed("Attempting to write %#x bytes at %#zx (%#x left), only got %#x written!\n",
3259 cbToWrite, offBuf, cbLeft, cbActual);
3260 if (cbActual)
3261 offBuf += cbActual;
3262 else
3263 pbBuf[offBuf++] = 0x11;
3264 }
3265 }
3266
3267 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, aRuns[i].offFile, pbBuf, cbBuf, NULL), VINF_SUCCESS);
3268 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
3269 }
3270
3271
3272 /*
3273 * Do uncached and write-thru accesses, must be page aligned.
3274 */
3275 RTFILE ahFiles[2] = { hFileWriteThru, hFileNoCache };
3276 for (unsigned iFile = 0; iFile < RT_ELEMENTS(ahFiles); iFile++, bFiller++)
3277 {
3278 if (g_fIgnoreNoCache && ahFiles[iFile] == NIL_RTFILE)
3279 continue;
3280
3281 fsPerfFillWriteBuf(0, pbBuf, cbBuf, bFiller);
3282 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
3283 RTTESTI_CHECK_RC(RTFileSeek(ahFiles[iFile], 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3284
3285 uint32_t cbPage = PAGE_SIZE;
3286 for (size_t offBuf = 0; offBuf < cbBuf; )
3287 {
3288 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / cbPage);
3289 uint32_t const cPagesToWrite = RTRandU32Ex(1, cPagesLeft);
3290 size_t const cbToWrite = cPagesToWrite * (size_t)cbPage;
3291 size_t cbActual = 0;
3292 RTTESTI_CHECK_RC(RTFileWrite(ahFiles[iFile], &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
3293 if (cbActual == cbToWrite)
3294 {
3295 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offBuf, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3296 fsPerfCheckReadBuf(__LINE__, offBuf, pbBuf, cbToWrite, bFiller);
3297 offBuf += cbActual;
3298 }
3299 else
3300 {
3301 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x written!\n", cbToWrite, offBuf, cbActual);
3302 if (cbActual)
3303 offBuf += cbActual;
3304 else
3305 {
3306 memset(&pbBuf[offBuf], 0x11, cbPage);
3307 offBuf += cbPage;
3308 }
3309 }
3310 }
3311
3312 RTTESTI_CHECK_RC(RTFileReadAt(ahFiles[iFile], 0, pbBuf, cbBuf, NULL), VINF_SUCCESS);
3313 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
3314 }
3315
3316 /*
3317 * Check the behavior of writing zero bytes to the file _4K from the end
3318 * using native API. In the olden days zero sized write have been known
3319 * to be used to truncate a file.
3320 */
3321 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -_4K, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
3322# ifdef RT_OS_WINDOWS
3323 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
3324 NTSTATUS rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
3325 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
3326 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
3327 RTTESTI_CHECK(Ios.Information == 0);
3328# else
3329 ssize_t cbWritten = write((int)RTFileToNative(hFile1), pbBuf, 0);
3330 RTTESTI_CHECK(cbWritten == 0);
3331# endif
3332 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, _4K, NULL), VINF_SUCCESS);
3333 fsPerfCheckReadBuf(__LINE__, cbFile - _4K, pbBuf, _4K, pbBuf[0x8]);
3334
3335#else
3336 RT_NOREF(hFileNoCache, hFileWriteThru);
3337#endif
3338
3339 /*
3340 * Gather write function operation.
3341 */
3342#ifdef RT_OS_WINDOWS
3343 /** @todo RTFileSgWriteAt is just a RTFileWriteAt loop for windows NT. Need
3344 * to use WriteFileGather (nocache + page aligned). */
3345#elif !defined(RT_OS_OS2) /** @todo implement RTFileSg using list i/o */
3346
3347# ifdef UIO_MAXIOV
3348 RTSGSEG aSegs[UIO_MAXIOV];
3349# else
3350 RTSGSEG aSegs[512];
3351# endif
3352 RTSGBUF SgBuf;
3353 uint32_t cIncr = 1;
3354 for (uint32_t cSegs = 1; cSegs <= RT_ELEMENTS(aSegs); cSegs += cIncr, bFiller++)
3355 {
3356 size_t const cbSeg = cbBuf / cSegs;
3357 size_t const cbToWrite = cbSeg * cSegs;
3358 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3359 {
3360 aSegs[iSeg].cbSeg = cbSeg;
3361 aSegs[iSeg].pvSeg = &pbBuf[cbToWrite - (iSeg + 1) * cbSeg];
3362 fsPerfFillWriteBuf(iSeg * cbSeg, (uint8_t *)aSegs[iSeg].pvSeg, cbSeg, bFiller);
3363 }
3364 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3365 int rc = myFileSgWriteAt(hFile1, 0, &SgBuf, cbToWrite, NULL);
3366 if (RT_SUCCESS(rc))
3367 {
3368 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, 0, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3369 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbToWrite, bFiller);
3370 }
3371 else
3372 {
3373 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%u cbSegs=%#zx cbToWrite=%#zx", rc, cSegs, cbSeg, cbToWrite);
3374 break;
3375 }
3376 if (cSegs == 16)
3377 cIncr = 7;
3378 else if (cSegs == 16 * 7 + 16 /*= 128*/)
3379 cIncr = 64;
3380 }
3381
3382 /* random stuff, including zero segments. */
3383 for (uint32_t iTest = 0; iTest < 128; iTest++, bFiller++)
3384 {
3385 uint32_t cSegs = RTRandU32Ex(1, RT_ELEMENTS(aSegs));
3386 uint32_t iZeroSeg = cSegs > 10 ? RTRandU32Ex(0, cSegs - 1) : UINT32_MAX / 2;
3387 uint32_t cZeroSegs = cSegs > 10 ? RTRandU32Ex(1, RT_MIN(cSegs - iZeroSeg, 25)) : 0;
3388 size_t cbToWrite = 0;
3389 size_t cbLeft = cbBuf;
3390 uint8_t *pbCur = &pbBuf[cbBuf];
3391 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3392 {
3393 uint32_t iAlign = RTRandU32Ex(0, 3);
3394 if (iAlign & 2) /* end is page aligned */
3395 {
3396 cbLeft -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3397 pbCur -= (uintptr_t)pbCur & PAGE_OFFSET_MASK;
3398 }
3399
3400 size_t cbSegOthers = (cSegs - iSeg) * _8K;
3401 size_t cbSegMax = cbLeft > cbSegOthers ? cbLeft - cbSegOthers
3402 : cbLeft > cSegs ? cbLeft - cSegs
3403 : cbLeft;
3404 size_t cbSeg = cbLeft != 0 ? RTRandU32Ex(0, cbSegMax) : 0;
3405 if (iAlign & 1) /* start is page aligned */
3406 cbSeg += ((uintptr_t)pbCur - cbSeg) & PAGE_OFFSET_MASK;
3407
3408 if (iSeg - iZeroSeg < cZeroSegs)
3409 cbSeg = 0;
3410
3411 cbToWrite += cbSeg;
3412 cbLeft -= cbSeg;
3413 pbCur -= cbSeg;
3414 aSegs[iSeg].cbSeg = cbSeg;
3415 aSegs[iSeg].pvSeg = pbCur;
3416 }
3417
3418 uint64_t const offFile = cbToWrite < cbFile ? RTRandU64Ex(0, cbFile - cbToWrite) : 0;
3419 uint64_t offFill = offFile;
3420 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
3421 if (aSegs[iSeg].cbSeg)
3422 {
3423 fsPerfFillWriteBuf(offFill, (uint8_t *)aSegs[iSeg].pvSeg, aSegs[iSeg].cbSeg, bFiller);
3424 offFill += aSegs[iSeg].cbSeg;
3425 }
3426
3427 RTSgBufInit(&SgBuf, &aSegs[0], cSegs);
3428 int rc = myFileSgWriteAt(hFile1, offFile, &SgBuf, cbToWrite, NULL);
3429 if (RT_SUCCESS(rc))
3430 {
3431 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offFile, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3432 fsPerfCheckReadBuf(__LINE__, offFile, pbBuf, cbToWrite, bFiller);
3433 }
3434 else
3435 {
3436 RTTestIFailed("myFileSgWriteAt failed: %Rrc - cSegs=%#x cbToWrite=%#zx", rc, cSegs, cbToWrite);
3437 break;
3438 }
3439 }
3440
3441#endif
3442
3443 /*
3444 * Other OS specific stuff.
3445 */
3446#ifdef RT_OS_WINDOWS
3447 /* Check that reading at an offset modifies the position: */
3448 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, cbFile / 2, pbBuf, _4K, NULL), VINF_SUCCESS);
3449 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
3450 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
3451
3452 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
3453 LARGE_INTEGER offNt;
3454 offNt.QuadPart = cbFile / 2;
3455 rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
3456 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
3457 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
3458 RTTESTI_CHECK(Ios.Information == _4K);
3459 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
3460#endif
3461
3462 RTMemPageFree(pbBuf, cbBuf);
3463}
3464
3465
3466/**
3467 * Worker for testing RTFileFlush.
3468 */
3469DECL_FORCE_INLINE(int) fsPerfFSyncWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf, uint64_t *poffFile)
3470{
3471 if (*poffFile + cbBuf <= cbFile)
3472 { /* likely */ }
3473 else
3474 {
3475 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3476 *poffFile = 0;
3477 }
3478
3479 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbBuf, NULL), VINF_SUCCESS, rcCheck);
3480 RTTESTI_CHECK_RC_RET(RTFileFlush(hFile1), VINF_SUCCESS, rcCheck);
3481
3482 *poffFile += cbBuf;
3483 return VINF_SUCCESS;
3484}
3485
3486
3487void fsPerfFSync(RTFILE hFile1, uint64_t cbFile)
3488{
3489 RTTestISub("fsync");
3490
3491 RTTESTI_CHECK_RC(RTFileFlush(hFile1), VINF_SUCCESS);
3492
3493 PROFILE_FN(RTFileFlush(hFile1), g_nsTestRun, "RTFileFlush");
3494
3495 size_t cbBuf = PAGE_SIZE;
3496 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3497 RTTESTI_CHECK_RETV(pbBuf != NULL);
3498 memset(pbBuf, 0xf4, cbBuf);
3499
3500 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3501 uint64_t offFile = 0;
3502 PROFILE_FN(fsPerfFSyncWorker(hFile1, cbFile, pbBuf, cbBuf, &offFile), g_nsTestRun, "RTFileWrite[Page]/RTFileFlush");
3503
3504 RTMemPageFree(pbBuf, cbBuf);
3505}
3506
3507
3508#ifndef RT_OS_OS2
3509/**
3510 * Worker for profiling msync.
3511 */
3512DECL_FORCE_INLINE(int) fsPerfMSyncWorker(uint8_t *pbMapping, size_t offMapping, size_t cbFlush, size_t *pcbFlushed)
3513{
3514 uint8_t *pbCur = &pbMapping[offMapping];
3515 for (size_t offFlush = 0; offFlush < cbFlush; offFlush += PAGE_SIZE)
3516 *(size_t volatile *)&pbCur[offFlush + 8] = cbFlush;
3517# ifdef RT_OS_WINDOWS
3518 RTTESTI_CHECK(FlushViewOfFile(pbCur, cbFlush));
3519# else
3520 RTTESTI_CHECK(msync(pbCur, cbFlush, MS_SYNC) == 0);
3521# endif
3522 if (*pcbFlushed < offMapping + cbFlush)
3523 *pcbFlushed = offMapping + cbFlush;
3524 return VINF_SUCCESS;
3525}
3526#endif /* !RT_OS_OS2 */
3527
3528
3529void fsPerfMMap(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
3530{
3531 RTTestISub("mmap");
3532#if !defined(RT_OS_OS2)
3533 static const char * const s_apszStates[] = { "readonly", "writecopy", "readwrite" };
3534 enum { kMMap_ReadOnly = 0, kMMap_WriteCopy, kMMap_ReadWrite, kMMap_End };
3535 for (int enmState = kMMap_ReadOnly; enmState < kMMap_End; enmState++)
3536 {
3537 /*
3538 * Do the mapping.
3539 */
3540 size_t cbMapping = (size_t)cbFile;
3541 if (cbMapping != cbFile)
3542 cbMapping = _256M;
3543 uint8_t *pbMapping;
3544
3545# ifdef RT_OS_WINDOWS
3546 HANDLE hSection;
3547 pbMapping = NULL;
3548 for (;; cbMapping /= 2)
3549 {
3550 hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile1), NULL,
3551 enmState == kMMap_ReadOnly ? PAGE_READONLY
3552 : enmState == kMMap_WriteCopy ? PAGE_WRITECOPY : PAGE_READWRITE,
3553 (uint32_t)((uint64_t)cbMapping >> 32), (uint32_t)cbMapping, NULL);
3554 DWORD dwErr1 = GetLastError();
3555 DWORD dwErr2 = 0;
3556 if (hSection != NULL)
3557 {
3558 pbMapping = (uint8_t *)MapViewOfFile(hSection,
3559 enmState == kMMap_ReadOnly ? FILE_MAP_READ
3560 : enmState == kMMap_WriteCopy ? FILE_MAP_COPY
3561 : FILE_MAP_WRITE,
3562 0, 0, cbMapping);
3563 if (pbMapping)
3564 break;
3565 dwErr2 = GetLastError();
3566 CloseHandle(hSection);
3567 }
3568 if (cbMapping <= _2M)
3569 {
3570 RTTestIFailed("%u/%s: CreateFileMapping or MapViewOfFile failed: %u, %u",
3571 enmState, s_apszStates[enmState], dwErr1, dwErr2);
3572 break;
3573 }
3574 }
3575# else
3576 for (;; cbMapping /= 2)
3577 {
3578 pbMapping = (uint8_t *)mmap(NULL, cbMapping,
3579 enmState == kMMap_ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE,
3580 enmState == kMMap_WriteCopy ? MAP_PRIVATE : MAP_SHARED,
3581 (int)RTFileToNative(hFile1), 0);
3582 if ((void *)pbMapping != MAP_FAILED)
3583 break;
3584 if (cbMapping <= _2M)
3585 {
3586 RTTestIFailed("%u/%s: mmap failed: %s (%u)", enmState, s_apszStates[enmState], strerror(errno), errno);
3587 break;
3588 }
3589 }
3590# endif
3591 if (cbMapping <= _2M)
3592 continue;
3593
3594 /*
3595 * Time page-ins just for fun.
3596 */
3597 size_t const cPages = cbMapping >> PAGE_SHIFT;
3598 size_t uDummy = 0;
3599 uint64_t ns = RTTimeNanoTS();
3600 for (size_t iPage = 0; iPage < cPages; iPage++)
3601 uDummy += ASMAtomicReadU8(&pbMapping[iPage << PAGE_SHIFT]);
3602 ns = RTTimeNanoTS() - ns;
3603 RTTestIValueF(ns / cPages, RTTESTUNIT_NS_PER_OCCURRENCE, "page-in %s", s_apszStates[enmState]);
3604
3605 /* Check the content. */
3606 fsPerfCheckReadBuf(__LINE__, 0, pbMapping, cbMapping);
3607
3608 if (enmState != kMMap_ReadOnly)
3609 {
3610 /* Write stuff to the first two megabytes. In the COW case, we'll detect
3611 corruption of shared data during content checking of the RW iterations. */
3612 fsPerfFillWriteBuf(0, pbMapping, _2M, 0xf7);
3613 if (enmState == kMMap_ReadWrite)
3614 {
3615 /* For RW we can try read back from the file handle and check if we get
3616 a match there first. */
3617 uint8_t abBuf[_4K];
3618 for (uint32_t off = 0; off < _2M; off += sizeof(abBuf))
3619 {
3620 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, off, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
3621 fsPerfCheckReadBuf(__LINE__, off, abBuf, sizeof(abBuf), 0xf7);
3622 }
3623# ifdef RT_OS_WINDOWS
3624 RTTESTI_CHECK(FlushViewOfFile(pbMapping, _2M));
3625# else
3626 RTTESTI_CHECK(msync(pbMapping, _2M, MS_SYNC) == 0);
3627# endif
3628
3629 /*
3630 * Time modifying and flushing a few different number of pages.
3631 */
3632 static size_t const s_acbFlush[] = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 3, PAGE_SIZE * 8, PAGE_SIZE * 16, _2M };
3633 for (unsigned iFlushSize = 0 ; iFlushSize < RT_ELEMENTS(s_acbFlush); iFlushSize++)
3634 {
3635 size_t const cbFlush = s_acbFlush[iFlushSize];
3636 if (cbFlush > cbMapping)
3637 continue;
3638
3639 char szDesc[80];
3640 RTStrPrintf(szDesc, sizeof(szDesc), "touch/flush/%zu", cbFlush);
3641 size_t const cFlushes = cbMapping / cbFlush;
3642 size_t const cbMappingUsed = cFlushes * cbFlush;
3643 size_t cbFlushed = 0;
3644 PROFILE_FN(fsPerfMSyncWorker(pbMapping, (iIteration * cbFlush) % cbMappingUsed, cbFlush, &cbFlushed),
3645 g_nsTestRun, szDesc);
3646
3647 /*
3648 * Check that all the changes made it thru to the file:
3649 */
3650 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
3651 {
3652 size_t cbBuf = _2M;
3653 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3654 if (!pbBuf)
3655 {
3656 cbBuf = _4K;
3657 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3658 }
3659 RTTESTI_CHECK(pbBuf != NULL);
3660 if (pbBuf)
3661 {
3662 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
3663 size_t const cbToCheck = RT_MIN(cFlushes * cbFlush, cbFlushed);
3664 unsigned cErrors = 0;
3665 for (size_t offBuf = 0; cErrors < 32 && offBuf < cbToCheck; offBuf += cbBuf)
3666 {
3667 size_t cbToRead = RT_MIN(cbBuf, cbToCheck - offBuf);
3668 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, pbBuf, cbToRead, NULL), VINF_SUCCESS);
3669
3670 for (size_t offFlush = 0; offFlush < cbToRead; offFlush += PAGE_SIZE)
3671 if (*(size_t volatile *)&pbBuf[offFlush + 8] != cbFlush)
3672 {
3673 RTTestIFailed("Flush issue at offset #%zx: %#zx, expected %#zx (cbFlush=%#zx, %#RX64)",
3674 offBuf + offFlush + 8, *(size_t volatile *)&pbBuf[offFlush + 8],
3675 cbFlush, cbFlush, *(uint64_t volatile *)&pbBuf[offFlush]);
3676 if (++cErrors > 32)
3677 break;
3678 }
3679 }
3680 RTMemPageFree(pbBuf, cbBuf);
3681 }
3682 }
3683 }
3684
3685# if 0 /* not needed, very very slow */
3686 /*
3687 * Restore the file to 0xf6 state for the next test.
3688 */
3689 RTTestIPrintf(RTTESTLVL_ALWAYS, "Restoring content...\n");
3690 fsPerfFillWriteBuf(0, pbMapping, cbMapping, 0xf6);
3691# ifdef RT_OS_WINDOWS
3692 RTTESTI_CHECK(FlushViewOfFile(pbMapping, cbMapping));
3693# else
3694 RTTESTI_CHECK(msync(pbMapping, cbMapping, MS_SYNC) == 0);
3695# endif
3696 RTTestIPrintf(RTTESTLVL_ALWAYS, "... done\n");
3697# endif
3698 }
3699 }
3700
3701 /*
3702 * Observe how regular writes affects a read-only or readwrite mapping.
3703 * These should ideally be immediately visible in the mapping, at least
3704 * when not performed thru an no-cache handle.
3705 */
3706 if (enmState == kMMap_ReadOnly || enmState == kMMap_ReadWrite)
3707 {
3708 size_t cbBuf = RT_MIN(_2M, cbMapping / 2);
3709 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3710 if (!pbBuf)
3711 {
3712 cbBuf = _4K;
3713 pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
3714 }
3715 RTTESTI_CHECK(pbBuf != NULL);
3716 if (pbBuf)
3717 {
3718 /* Do a number of random writes to the file (using hFile1).
3719 Immediately undoing them. */
3720 for (uint32_t i = 0; i < 128; i++)
3721 {
3722 /* Generate a randomly sized write at a random location, making
3723 sure it differs from whatever is there already before writing. */
3724 uint32_t const cbToWrite = RTRandU32Ex(1, (uint32_t)cbBuf);
3725 uint64_t const offToWrite = RTRandU64Ex(0, cbMapping - cbToWrite);
3726
3727 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf8);
3728 pbBuf[0] = ~pbBuf[0];
3729 if (cbToWrite > 1)
3730 pbBuf[cbToWrite - 1] = ~pbBuf[cbToWrite - 1];
3731 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3732
3733 /* Check the mapping. */
3734 if (memcmp(&pbMapping[(size_t)offToWrite], pbBuf, cbToWrite) != 0)
3735 {
3736 RTTestIFailed("Write #%u @ %#RX64 LB %#x was not reflected in the mapping!\n", i, offToWrite, cbToWrite);
3737 }
3738
3739 /* Restore */
3740 fsPerfFillWriteBuf(offToWrite, pbBuf, cbToWrite, 0xf6);
3741 RTTESTI_CHECK_RC(RTFileWriteAt(hFile1, offToWrite, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
3742 }
3743
3744 RTMemPageFree(pbBuf, cbBuf);
3745 }
3746 }
3747
3748 /*
3749 * Unmap it.
3750 */
3751# ifdef RT_OS_WINDOWS
3752 RTTESTI_CHECK(UnmapViewOfFile(pbMapping));
3753 RTTESTI_CHECK(CloseHandle(hSection));
3754# else
3755 RTTESTI_CHECK(munmap(pbMapping, cbMapping) == 0);
3756# endif
3757 }
3758
3759 /*
3760 * Memory mappings without open handles (pretty common).
3761 */
3762 for (uint32_t i = 0; i < 32; i++)
3763 {
3764 /* Create a new file, 256 KB in size, and fill it with random bytes.
3765 Try uncached access if we can to force the page-in to do actual reads. */
3766 char szFile2[RTPATH_MAX + 32];
3767 memcpy(szFile2, g_szDir, g_cchDir);
3768 RTStrPrintf(&szFile2[g_cchDir], sizeof(szFile2) - g_cchDir, "mmap-%u.noh", i);
3769 RTFILE hFile2 = NIL_RTFILE;
3770 int rc = (i & 3) == 3 ? VERR_TRY_AGAIN
3771 : RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_NO_CACHE);
3772 if (RT_FAILURE(rc))
3773 {
3774 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
3775 VINF_SUCCESS);
3776 }
3777
3778 static char s_abContent[256*1024];
3779 RTRandBytes(s_abContent, sizeof(s_abContent));
3780 RTTESTI_CHECK_RC(RTFileWrite(hFile2, s_abContent, sizeof(s_abContent), NULL), VINF_SUCCESS);
3781 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
3782
3783 /* Reopen the file with normal caching. Every second time, we also
3784 does a read-only open of it to confuse matters. */
3785 RTFILE hFile3 = NIL_RTFILE;
3786 if ((i & 3) == 3)
3787 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
3788 hFile2 = NIL_RTFILE;
3789 RTTESTI_CHECK_RC_BREAK(RTFileOpen(&hFile2, szFile2, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE),
3790 VINF_SUCCESS);
3791 if ((i & 3) == 1)
3792 RTTESTI_CHECK_RC(RTFileOpen(&hFile3, szFile2, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE), VINF_SUCCESS);
3793
3794 /* Memory map it read-write (no COW). */
3795#ifdef RT_OS_WINDOWS
3796 HANDLE hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile2), NULL, PAGE_READWRITE, 0, sizeof(s_abContent), NULL);
3797 RTTESTI_CHECK_MSG(hSection != NULL, ("last error %u\n", GetLastError));
3798 uint8_t *pbMapping = (uint8_t *)MapViewOfFile(hSection, FILE_MAP_WRITE, 0, 0, sizeof(s_abContent));
3799 RTTESTI_CHECK_MSG(pbMapping != NULL, ("last error %u\n", GetLastError));
3800 RTTESTI_CHECK_MSG(CloseHandle(hSection), ("last error %u\n", GetLastError));
3801# else
3802 uint8_t *pbMapping = (uint8_t *)mmap(NULL, sizeof(s_abContent), PROT_READ | PROT_WRITE, MAP_SHARED,
3803 (int)RTFileToNative(hFile2), 0);
3804 if ((void *)pbMapping == MAP_FAILED)
3805 pbMapping = NULL;
3806 RTTESTI_CHECK_MSG(pbMapping != NULL, ("errno=%s (%d)\n", strerror(errno), errno));
3807# endif
3808
3809 /* Close the file handles. */
3810 if ((i & 7) == 7)
3811 {
3812 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
3813 hFile3 = NIL_RTFILE;
3814 }
3815 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
3816 if ((i & 7) == 5)
3817 {
3818 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
3819 hFile3 = NIL_RTFILE;
3820 }
3821 if (pbMapping)
3822 {
3823 RTThreadSleep(2); /* fudge for cleanup/whatever */
3824
3825 /* Page in the mapping by comparing with the content we wrote above. */
3826 RTTESTI_CHECK(memcmp(pbMapping, s_abContent, sizeof(s_abContent)) == 0);
3827
3828 /* Now dirty everything by inverting everything. */
3829 size_t *puCur = (size_t *)pbMapping;
3830 size_t cLeft = sizeof(s_abContent) / sizeof(*puCur);
3831 while (cLeft-- > 0)
3832 {
3833 *puCur = ~*puCur;
3834 puCur++;
3835 }
3836
3837 /* Sync it all. */
3838# ifdef RT_OS_WINDOWS
3839 RTTESTI_CHECK(FlushViewOfFile(pbMapping, sizeof(s_abContent)));
3840# else
3841 RTTESTI_CHECK(msync(pbMapping, sizeof(s_abContent), MS_SYNC) == 0);
3842# endif
3843
3844 /* Unmap it. */
3845# ifdef RT_OS_WINDOWS
3846 RTTESTI_CHECK(UnmapViewOfFile(pbMapping));
3847# else
3848 RTTESTI_CHECK(munmap(pbMapping, sizeof(s_abContent)) == 0);
3849# endif
3850 }
3851
3852 if (hFile3 != NIL_RTFILE)
3853 RTTESTI_CHECK_RC(RTFileClose(hFile3), VINF_SUCCESS);
3854 RTTESTI_CHECK_RC(RTFileDelete(szFile2), VINF_SUCCESS);
3855 }
3856
3857
3858#else
3859 RTTestSkipped(g_hTest, "not supported/implemented");
3860 RT_NOREF(hFile1, hFileNoCache, cbFile);
3861#endif
3862}
3863
3864
3865/**
3866 * This does the read, write and seek tests.
3867 */
3868void fsPerfIo(void)
3869{
3870 RTTestISub("I/O");
3871
3872 /*
3873 * Determin the size of the test file.
3874 */
3875 g_szDir[g_cchDir] = '\0';
3876 RTFOFF cbFree = 0;
3877 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
3878 uint64_t cbFile = g_cbIoFile;
3879 if (cbFile + _16M < (uint64_t)cbFree)
3880 cbFile = RT_ALIGN_64(cbFile, _64K);
3881 else if (cbFree < _32M)
3882 {
3883 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
3884 return;
3885 }
3886 else
3887 {
3888 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
3889 cbFile = RT_ALIGN_64(cbFile, _64K);
3890 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
3891 }
3892 if (cbFile < _64K)
3893 {
3894 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 64KB", cbFile);
3895 return;
3896 }
3897
3898 /*
3899 * Create a cbFile sized test file.
3900 */
3901 RTFILE hFile1;
3902 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file21")),
3903 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
3904 RTFILE hFileNoCache;
3905 if (!g_fIgnoreNoCache)
3906 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileNoCache, g_szDir,
3907 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE),
3908 VINF_SUCCESS);
3909 else
3910 {
3911 int rc = RTFileOpen(&hFileNoCache, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE);
3912 if (RT_FAILURE(rc))
3913 {
3914 RTTestIPrintf(RTTESTLVL_ALWAYS, "Unable to open I/O file with non-cache flag (%Rrc), skipping related tests.\n", rc);
3915 hFileNoCache = NIL_RTFILE;
3916 }
3917 }
3918 RTFILE hFileWriteThru;
3919 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileWriteThru, g_szDir,
3920 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_WRITE_THROUGH),
3921 VINF_SUCCESS);
3922
3923 uint8_t *pbFree = NULL;
3924 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
3925 RTMemFree(pbFree);
3926 if (RT_SUCCESS(rc))
3927 {
3928 /*
3929 * Do the testing & profiling.
3930 */
3931 if (g_fSeek)
3932 fsPerfIoSeek(hFile1, cbFile);
3933
3934 if (g_fReadTests)
3935 fsPerfRead(hFile1, hFileNoCache, cbFile);
3936 if (g_fReadPerf)
3937 for (unsigned i = 0; i < g_cIoBlocks; i++)
3938 fsPerfIoReadBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
3939#ifdef FSPERF_TEST_SENDFILE
3940 if (g_fSendFile)
3941 fsPerfSendFile(hFile1, cbFile);
3942#endif
3943#ifdef RT_OS_LINUX
3944 if (g_fSplice)
3945 fsPerfSpliceToPipe(hFile1, cbFile);
3946#endif
3947 if (g_fMMap)
3948 fsPerfMMap(hFile1, hFileNoCache, cbFile);
3949
3950 /* This is destructive to the file content. */
3951 if (g_fWriteTests)
3952 fsPerfWrite(hFile1, hFileNoCache, hFileWriteThru, cbFile);
3953 if (g_fWritePerf)
3954 for (unsigned i = 0; i < g_cIoBlocks; i++)
3955 fsPerfIoWriteBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
3956#ifdef RT_OS_LINUX
3957 if (g_fSplice)
3958 fsPerfSpliceToFile(hFile1, cbFile);
3959#endif
3960 if (g_fFSync)
3961 fsPerfFSync(hFile1, cbFile);
3962 }
3963
3964 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
3965 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
3966 if (hFileNoCache != NIL_RTFILE || !g_fIgnoreNoCache)
3967 RTTESTI_CHECK_RC(RTFileClose(hFileNoCache), VINF_SUCCESS);
3968 RTTESTI_CHECK_RC(RTFileClose(hFileWriteThru), VINF_SUCCESS);
3969 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
3970}
3971
3972
3973DECL_FORCE_INLINE(int) fsPerfCopyWorker1(const char *pszSrc, const char *pszDst)
3974{
3975 RTFileDelete(pszDst);
3976 return RTFileCopy(pszSrc, pszDst);
3977}
3978
3979
3980#ifdef RT_OS_LINUX
3981DECL_FORCE_INLINE(int) fsPerfCopyWorkerSendFile(RTFILE hFile1, RTFILE hFile2, size_t cbFile)
3982{
3983 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile2, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
3984
3985 loff_t off = 0;
3986 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &off, cbFile);
3987 if (cbSent > 0 && (size_t)cbSent == cbFile)
3988 return 0;
3989
3990 int rc = VERR_GENERAL_FAILURE;
3991 if (cbSent < 0)
3992 {
3993 rc = RTErrConvertFromErrno(errno);
3994 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)", cbFile, cbSent, errno, rc);
3995 }
3996 else
3997 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
3998 cbFile, cbSent, cbFile, cbSent - cbFile);
3999 return rc;
4000}
4001#endif /* RT_OS_LINUX */
4002
4003
4004static void fsPerfCopy(void)
4005{
4006 RTTestISub("copy");
4007
4008 /*
4009 * Non-existing files.
4010 */
4011 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-file")),
4012 InDir2(RT_STR_TUPLE("whatever"))), VERR_FILE_NOT_FOUND);
4013 RTTESTI_CHECK_RC(RTFileCopy(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
4014 InDir2(RT_STR_TUPLE("no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
4015 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
4016 InDir2(RT_STR_TUPLE("whatever"))), VERR_PATH_NOT_FOUND);
4017
4018 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
4019 InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
4020 RTTESTI_CHECK_RC(RTFileCopy(InDir(RT_STR_TUPLE("known-file")),
4021 InDir2(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
4022
4023 /*
4024 * Determin the size of the test file.
4025 * We want to be able to make 1 copy of it.
4026 */
4027 g_szDir[g_cchDir] = '\0';
4028 RTFOFF cbFree = 0;
4029 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
4030 uint64_t cbFile = g_cbIoFile;
4031 if (cbFile + _16M < (uint64_t)cbFree)
4032 cbFile = RT_ALIGN_64(cbFile, _64K);
4033 else if (cbFree < _32M)
4034 {
4035 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
4036 return;
4037 }
4038 else
4039 {
4040 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
4041 cbFile = RT_ALIGN_64(cbFile, _64K);
4042 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
4043 }
4044 if (cbFile < _512K * 2)
4045 {
4046 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 1MB", cbFile);
4047 return;
4048 }
4049 cbFile /= 2;
4050
4051 /*
4052 * Create a cbFile sized test file.
4053 */
4054 RTFILE hFile1;
4055 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file22")),
4056 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
4057 uint8_t *pbFree = NULL;
4058 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
4059 RTMemFree(pbFree);
4060 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4061 if (RT_SUCCESS(rc))
4062 {
4063 /*
4064 * Make copies.
4065 */
4066 /* plain */
4067 RTFileDelete(InDir2(RT_STR_TUPLE("file23")));
4068 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VINF_SUCCESS);
4069 RTTESTI_CHECK_RC(RTFileCopy(g_szDir, g_szDir2), VERR_ALREADY_EXISTS);
4070 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4071
4072 /* by handle */
4073 hFile1 = NIL_RTFILE;
4074 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4075 RTFILE hFile2 = NIL_RTFILE;
4076 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4077 RTTESTI_CHECK_RC(RTFileCopyByHandles(hFile1, hFile2), VINF_SUCCESS);
4078 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4079 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4080 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4081
4082 /* copy part */
4083 hFile1 = NIL_RTFILE;
4084 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4085 hFile2 = NIL_RTFILE;
4086 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4087 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, 0, hFile2, 0, cbFile / 2, 0, NULL), VINF_SUCCESS);
4088 RTTESTI_CHECK_RC(RTFileCopyPart(hFile1, cbFile / 2, hFile2, cbFile / 2, cbFile - cbFile / 2, 0, NULL), VINF_SUCCESS);
4089 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4090 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4091 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4092
4093#ifdef RT_OS_LINUX
4094 /*
4095 * On linux we can also use sendfile between two files, except for 2.5.x to 2.6.33.
4096 */
4097 uint64_t const cbFileMax = RT_MIN(cbFile, UINT32_C(0x7ffff000));
4098 char szRelease[64];
4099 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szRelease, sizeof(szRelease));
4100 bool const fSendFileBetweenFiles = RTStrVersionCompare(szRelease, "2.5.0") < 0
4101 || RTStrVersionCompare(szRelease, "2.6.33") >= 0;
4102 if (fSendFileBetweenFiles)
4103 {
4104 /* Copy the whole file: */
4105 hFile1 = NIL_RTFILE;
4106 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4107 RTFileDelete(g_szDir2);
4108 hFile2 = NIL_RTFILE;
4109 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4110 ssize_t cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbFile);
4111 if (cbSent < 0)
4112 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
4113 cbFile, cbSent, errno, RTErrConvertFromErrno(errno));
4114 else if ((size_t)cbSent != cbFileMax)
4115 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
4116 cbFile, cbSent, cbFileMax, cbSent - cbFileMax);
4117 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4118 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4119 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4120
4121 /* Try copy a little bit too much: */
4122 if (cbFile == cbFileMax)
4123 {
4124 hFile1 = NIL_RTFILE;
4125 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4126 RTFileDelete(g_szDir2);
4127 hFile2 = NIL_RTFILE;
4128 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4129 size_t cbToCopy = cbFile + RTRandU32Ex(1, _64M);
4130 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), NULL, cbToCopy);
4131 if (cbSent < 0)
4132 RTTestIFailed("sendfile(file,file,NULL,%#zx) failed (%zd): %d (%Rrc)",
4133 cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
4134 else if ((size_t)cbSent != cbFile)
4135 RTTestIFailed("sendfile(file,file,NULL,%#zx) returned %#zx, expected %#zx (diff %zd)",
4136 cbToCopy, cbSent, cbFile, cbSent - cbFile);
4137 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4138 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4139 }
4140
4141 /* Do partial copy: */
4142 hFile2 = NIL_RTFILE;
4143 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4144 for (uint32_t i = 0; i < 64; i++)
4145 {
4146 size_t cbToCopy = RTRandU32Ex(0, cbFileMax - 1);
4147 uint32_t const offFile = RTRandU32Ex(1, (uint64_t)RT_MIN(cbFileMax - cbToCopy, UINT32_MAX));
4148 RTTESTI_CHECK_RC_BREAK(RTFileSeek(hFile2, offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
4149 loff_t offFile2 = offFile;
4150 cbSent = sendfile((int)RTFileToNative(hFile2), (int)RTFileToNative(hFile1), &offFile2, cbToCopy);
4151 if (cbSent < 0)
4152 RTTestIFailed("sendfile(file,file,%#x,%#zx) failed (%zd): %d (%Rrc)",
4153 offFile, cbToCopy, cbSent, errno, RTErrConvertFromErrno(errno));
4154 else if ((size_t)cbSent != cbToCopy)
4155 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx, expected %#zx (diff %zd)",
4156 offFile, cbToCopy, cbSent, cbToCopy, cbSent - cbToCopy);
4157 else if (offFile2 != (loff_t)(offFile + cbToCopy))
4158 RTTestIFailed("sendfile(file,file,%#x,%#zx) returned %#zx + off=%#RX64, expected off %#x",
4159 offFile, cbToCopy, cbSent, offFile2, offFile + cbToCopy);
4160 }
4161 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4162 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4163 RTTESTI_CHECK_RC(RTFileCompare(g_szDir, g_szDir2), VINF_SUCCESS);
4164 }
4165#endif
4166
4167 /*
4168 * Do some benchmarking.
4169 */
4170#define PROFILE_COPY_FN(a_szOperation, a_fnCall) \
4171 do \
4172 { \
4173 /* Estimate how many iterations we need to fill up the given timeslot: */ \
4174 fsPerfYield(); \
4175 uint64_t nsStart = RTTimeNanoTS(); \
4176 uint64_t ns; \
4177 do \
4178 ns = RTTimeNanoTS(); \
4179 while (ns == nsStart); \
4180 nsStart = ns; \
4181 \
4182 uint64_t iIteration = 0; \
4183 do \
4184 { \
4185 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4186 iIteration++; \
4187 ns = RTTimeNanoTS() - nsStart; \
4188 } while (ns < RT_NS_10MS); \
4189 ns /= iIteration; \
4190 if (ns > g_nsPerNanoTSCall + 32) \
4191 ns -= g_nsPerNanoTSCall; \
4192 uint64_t cIterations = g_nsTestRun / ns; \
4193 if (cIterations < 2) \
4194 cIterations = 2; \
4195 else if (cIterations & 1) \
4196 cIterations++; \
4197 \
4198 /* Do the actual profiling: */ \
4199 iIteration = 0; \
4200 fsPerfYield(); \
4201 nsStart = RTTimeNanoTS(); \
4202 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
4203 { \
4204 for (; iIteration < cIterations; iIteration++)\
4205 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
4206 ns = RTTimeNanoTS() - nsStart;\
4207 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
4208 break; \
4209 cIterations += cIterations / 4; \
4210 if (cIterations & 1) \
4211 cIterations++; \
4212 nsStart += g_nsPerNanoTSCall; \
4213 } \
4214 RTTestIValueF(ns / iIteration, \
4215 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation " latency"); \
4216 RTTestIValueF((uint64_t)((uint64_t)iIteration * cbFile / ((double)ns / RT_NS_1SEC)), \
4217 RTTESTUNIT_BYTES_PER_SEC, a_szOperation " throughput"); \
4218 RTTestIValueF((uint64_t)iIteration * cbFile, \
4219 RTTESTUNIT_BYTES, a_szOperation " bytes"); \
4220 RTTestIValueF(iIteration, \
4221 RTTESTUNIT_OCCURRENCES, a_szOperation " iterations"); \
4222 if (g_fShowDuration) \
4223 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation " duration"); \
4224 } while (0)
4225
4226 PROFILE_COPY_FN("RTFileCopy/Replace", fsPerfCopyWorker1(g_szDir, g_szDir2));
4227
4228 hFile1 = NIL_RTFILE;
4229 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4230 RTFileDelete(g_szDir2);
4231 hFile2 = NIL_RTFILE;
4232 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4233 PROFILE_COPY_FN("RTFileCopyByHandles/Overwrite", RTFileCopyByHandles(hFile1, hFile2));
4234 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4235 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4236
4237 /* We could benchmark RTFileCopyPart with various block sizes and whatnot...
4238 But it's currently well covered by the two previous operations. */
4239
4240#ifdef RT_OS_LINUX
4241 if (fSendFileBetweenFiles)
4242 {
4243 hFile1 = NIL_RTFILE;
4244 RTTESTI_CHECK_RC(RTFileOpen(&hFile1, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS);
4245 RTFileDelete(g_szDir2);
4246 hFile2 = NIL_RTFILE;
4247 RTTESTI_CHECK_RC(RTFileOpen(&hFile2, g_szDir2, RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
4248 PROFILE_COPY_FN("sendfile/overwrite", fsPerfCopyWorkerSendFile(hFile1, hFile2, cbFileMax));
4249 RTTESTI_CHECK_RC(RTFileClose(hFile2), VINF_SUCCESS);
4250 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
4251 }
4252#endif
4253 }
4254
4255 /*
4256 * Clean up.
4257 */
4258 RTFileDelete(InDir2(RT_STR_TUPLE("file22c1")));
4259 RTFileDelete(InDir2(RT_STR_TUPLE("file22c2")));
4260 RTFileDelete(InDir2(RT_STR_TUPLE("file22c3")));
4261 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
4262}
4263
4264
4265/**
4266 * Display the usage to @a pStrm.
4267 */
4268static void Usage(PRTSTREAM pStrm)
4269{
4270 char szExec[RTPATH_MAX];
4271 RTStrmPrintf(pStrm, "usage: %s <-d <testdir>> [options]\n",
4272 RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
4273 RTStrmPrintf(pStrm, "\n");
4274 RTStrmPrintf(pStrm, "options: \n");
4275
4276 for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
4277 {
4278 char szHelp[80];
4279 const char *pszHelp;
4280 switch (g_aCmdOptions[i].iShort)
4281 {
4282 case 'd': pszHelp = "The directory to use for testing. default: CWD/fstestdir"; break;
4283 case 'e': pszHelp = "Enables all tests. default: -e"; break;
4284 case 'z': pszHelp = "Disables all tests. default: -e"; break;
4285 case 's': pszHelp = "Set benchmark duration in seconds. default: 10 sec"; break;
4286 case 'm': pszHelp = "Set benchmark duration in milliseconds. default: 10000 ms"; break;
4287 case 'v': pszHelp = "More verbose execution."; break;
4288 case 'q': pszHelp = "Quiet execution."; break;
4289 case 'h': pszHelp = "Displays this help and exit"; break;
4290 case 'V': pszHelp = "Displays the program revision"; break;
4291 case kCmdOpt_ShowDuration: pszHelp = "Show duration of profile runs. default: --no-show-duration"; break;
4292 case kCmdOpt_NoShowDuration: pszHelp = "Hide duration of profile runs. default: --no-show-duration"; break;
4293 case kCmdOpt_ShowIterations: pszHelp = "Show iteration count for profile runs. default: --no-show-iterations"; break;
4294 case kCmdOpt_NoShowIterations: pszHelp = "Hide iteration count for profile runs. default: --no-show-iterations"; break;
4295 case kCmdOpt_ManyFiles: pszHelp = "Count of files in big test dir. default: --many-files 10000"; break;
4296 case kCmdOpt_NoManyFiles: pszHelp = "Skip big test dir with many files. default: --many-files 10000"; break;
4297 case kCmdOpt_ManyTreeFilesPerDir: pszHelp = "Count of files per directory in test tree. default: 640"; break;
4298 case kCmdOpt_ManyTreeSubdirsPerDir: pszHelp = "Count of subdirs per directory in test tree. default: 16"; break;
4299 case kCmdOpt_ManyTreeDepth: pszHelp = "Depth of test tree (not counting root). default: 1"; break;
4300 case kCmdOpt_IgnoreNoCache: pszHelp = "Ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
4301 case kCmdOpt_NoIgnoreNoCache: pszHelp = "Do not ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
4302 case kCmdOpt_IoFileSize: pszHelp = "Size of file used for I/O tests. default: 512 MB"; break;
4303 case kCmdOpt_SetBlockSize: pszHelp = "Sets single I/O block size (in bytes)."; break;
4304 case kCmdOpt_AddBlockSize: pszHelp = "Adds an I/O block size (in bytes)."; break;
4305 default:
4306 if (g_aCmdOptions[i].iShort >= kCmdOpt_First)
4307 {
4308 if (RTStrStartsWith(g_aCmdOptions[i].pszLong, "--no-"))
4309 RTStrPrintf(szHelp, sizeof(szHelp), "Disables the '%s' test.", g_aCmdOptions[i].pszLong + 5);
4310 else
4311 RTStrPrintf(szHelp, sizeof(szHelp), "Enables the '%s' test.", g_aCmdOptions[i].pszLong + 2);
4312 pszHelp = szHelp;
4313 }
4314 else
4315 pszHelp = "Option undocumented";
4316 break;
4317 }
4318 if ((unsigned)g_aCmdOptions[i].iShort < 127U)
4319 {
4320 char szOpt[64];
4321 RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
4322 RTStrmPrintf(pStrm, " %-19s %s\n", szOpt, pszHelp);
4323 }
4324 else
4325 RTStrmPrintf(pStrm, " %-19s %s\n", g_aCmdOptions[i].pszLong, pszHelp);
4326 }
4327}
4328
4329
4330static uint32_t fsPerfCalcManyTreeFiles(void)
4331{
4332 uint32_t cDirs = 1;
4333 for (uint32_t i = 0, cDirsAtLevel = 1; i < g_cManyTreeDepth; i++)
4334 {
4335 cDirs += cDirsAtLevel * g_cManyTreeSubdirsPerDir;
4336 cDirsAtLevel *= g_cManyTreeSubdirsPerDir;
4337 }
4338 return g_cManyTreeFilesPerDir * cDirs;
4339}
4340
4341
4342int main(int argc, char *argv[])
4343{
4344 /*
4345 * Init IPRT and globals.
4346 */
4347 int rc = RTTestInitAndCreate("FsPerf", &g_hTest);
4348 if (rc)
4349 return rc;
4350 RTListInit(&g_ManyTreeHead);
4351
4352 /*
4353 * Default values.
4354 */
4355 rc = RTPathGetCurrent(g_szDir, sizeof(g_szDir) / 2);
4356 if (RT_SUCCESS(rc))
4357 rc = RTPathAppend(g_szDir, sizeof(g_szDir) / 2, "fstestdir-");
4358 if (RT_SUCCESS(rc))
4359 {
4360 g_cchDir = strlen(g_szDir);
4361 g_cchDir += RTStrPrintf(&g_szDir[g_cchDir], sizeof(g_szDir) - g_cchDir, "%u" RTPATH_SLASH_STR, RTProcSelf());
4362 }
4363 else
4364 {
4365 RTTestFailed(g_hTest, "RTPathGetCurrent (or RTPathAppend) failed: %Rrc\n", rc);
4366 return RTTestSummaryAndDestroy(g_hTest);
4367 }
4368
4369 RTGETOPTUNION ValueUnion;
4370 RTGETOPTSTATE GetState;
4371 RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
4372 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
4373 {
4374 switch (rc)
4375 {
4376 case 'd':
4377 rc = RTPathAbs(ValueUnion.psz, g_szDir, sizeof(g_szDir) / 2);
4378 if (RT_SUCCESS(rc))
4379 {
4380 RTPathEnsureTrailingSeparator(g_szDir, sizeof(g_szDir));
4381 g_cchDir = strlen(g_szDir);
4382 break;
4383 }
4384 RTTestFailed(g_hTest, "RTPathAbs(%s) failed: %Rrc\n", ValueUnion.psz, rc);
4385 return RTTestSummaryAndDestroy(g_hTest);
4386
4387 case 's':
4388 if (ValueUnion.u32 == 0)
4389 g_nsTestRun = RT_NS_1SEC_64 * 10;
4390 else
4391 g_nsTestRun = ValueUnion.u32 * RT_NS_1SEC_64;
4392 break;
4393
4394 case 'm':
4395 if (ValueUnion.u64 == 0)
4396 g_nsTestRun = RT_NS_1SEC_64 * 10;
4397 else
4398 g_nsTestRun = ValueUnion.u64 * RT_NS_1MS;
4399 break;
4400
4401 case 'e':
4402 g_fManyFiles = true;
4403 g_fOpen = true;
4404 g_fFStat = true;
4405 g_fFChMod = true;
4406 g_fFUtimes = true;
4407 g_fStat = true;
4408 g_fChMod = true;
4409 g_fUtimes = true;
4410 g_fRename = true;
4411 g_fDirOpen = true;
4412 g_fDirEnum = true;
4413 g_fMkRmDir = true;
4414 g_fStatVfs = true;
4415 g_fRm = true;
4416 g_fChSize = true;
4417 g_fReadTests = true;
4418 g_fReadPerf = true;
4419#ifdef FSPERF_TEST_SENDFILE
4420 g_fSendFile = true;
4421#endif
4422#ifdef RT_OS_LINUX
4423 g_fSplice = true;
4424#endif
4425 g_fWriteTests= true;
4426 g_fWritePerf = true;
4427 g_fSeek = true;
4428 g_fFSync = true;
4429 g_fMMap = true;
4430 g_fCopy = true;
4431 break;
4432
4433 case 'z':
4434 g_fManyFiles = false;
4435 g_fOpen = false;
4436 g_fFStat = false;
4437 g_fFChMod = false;
4438 g_fFUtimes = false;
4439 g_fStat = false;
4440 g_fChMod = false;
4441 g_fUtimes = false;
4442 g_fRename = false;
4443 g_fDirOpen = false;
4444 g_fDirEnum = false;
4445 g_fMkRmDir = false;
4446 g_fStatVfs = false;
4447 g_fRm = false;
4448 g_fChSize = false;
4449 g_fReadTests = false;
4450 g_fReadPerf = false;
4451#ifdef FSPERF_TEST_SENDFILE
4452 g_fSendFile = false;
4453#endif
4454#ifdef RT_OS_LINUX
4455 g_fSplice = false;
4456#endif
4457 g_fWriteTests= false;
4458 g_fWritePerf = false;
4459 g_fSeek = false;
4460 g_fFSync = false;
4461 g_fMMap = false;
4462 g_fCopy = false;
4463 break;
4464
4465#define CASE_OPT(a_Stem) \
4466 case RT_CONCAT(kCmdOpt_,a_Stem): RT_CONCAT(g_f,a_Stem) = true; break; \
4467 case RT_CONCAT(kCmdOpt_No,a_Stem): RT_CONCAT(g_f,a_Stem) = false; break
4468 CASE_OPT(Open);
4469 CASE_OPT(FStat);
4470 CASE_OPT(FChMod);
4471 CASE_OPT(FUtimes);
4472 CASE_OPT(Stat);
4473 CASE_OPT(ChMod);
4474 CASE_OPT(Utimes);
4475 CASE_OPT(Rename);
4476 CASE_OPT(DirOpen);
4477 CASE_OPT(DirEnum);
4478 CASE_OPT(MkRmDir);
4479 CASE_OPT(StatVfs);
4480 CASE_OPT(Rm);
4481 CASE_OPT(ChSize);
4482 CASE_OPT(ReadTests);
4483 CASE_OPT(ReadPerf);
4484#ifdef FSPERF_TEST_SENDFILE
4485 CASE_OPT(SendFile);
4486#endif
4487#ifdef RT_OS_LINUX
4488 CASE_OPT(Splice);
4489#endif
4490 CASE_OPT(WriteTests);
4491 CASE_OPT(WritePerf);
4492 CASE_OPT(Seek);
4493 CASE_OPT(FSync);
4494 CASE_OPT(MMap);
4495 CASE_OPT(IgnoreNoCache);
4496 CASE_OPT(Copy);
4497
4498 CASE_OPT(ShowDuration);
4499 CASE_OPT(ShowIterations);
4500#undef CASE_OPT
4501
4502 case kCmdOpt_ManyFiles:
4503 g_fManyFiles = ValueUnion.u32 > 0;
4504 g_cManyFiles = ValueUnion.u32;
4505 break;
4506
4507 case kCmdOpt_NoManyFiles:
4508 g_fManyFiles = false;
4509 break;
4510
4511 case kCmdOpt_ManyTreeFilesPerDir:
4512 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= _64M)
4513 {
4514 g_cManyTreeFilesPerDir = ValueUnion.u32;
4515 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
4516 break;
4517 }
4518 RTTestFailed(g_hTest, "Out of range --files-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4519 return RTTestSummaryAndDestroy(g_hTest);
4520
4521 case kCmdOpt_ManyTreeSubdirsPerDir:
4522 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= 1024)
4523 {
4524 g_cManyTreeSubdirsPerDir = ValueUnion.u32;
4525 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
4526 break;
4527 }
4528 RTTestFailed(g_hTest, "Out of range --subdirs-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4529 return RTTestSummaryAndDestroy(g_hTest);
4530
4531 case kCmdOpt_ManyTreeDepth:
4532 if (ValueUnion.u32 <= 8)
4533 {
4534 g_cManyTreeDepth = ValueUnion.u32;
4535 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
4536 break;
4537 }
4538 RTTestFailed(g_hTest, "Out of range --tree-depth value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4539 return RTTestSummaryAndDestroy(g_hTest);
4540
4541 case kCmdOpt_IoFileSize:
4542 if (ValueUnion.u64 == 0)
4543 g_cbIoFile = _512M;
4544 else
4545 g_cbIoFile = ValueUnion.u64;
4546 break;
4547
4548 case kCmdOpt_SetBlockSize:
4549 if (ValueUnion.u32 > 0)
4550 {
4551 g_cIoBlocks = 1;
4552 g_acbIoBlocks[0] = ValueUnion.u32;
4553 }
4554 else
4555 {
4556 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4557 return RTTestSummaryAndDestroy(g_hTest);
4558 }
4559 break;
4560
4561 case kCmdOpt_AddBlockSize:
4562 if (g_cIoBlocks >= RT_ELEMENTS(g_acbIoBlocks))
4563 RTTestFailed(g_hTest, "Too many I/O block sizes: max %u\n", RT_ELEMENTS(g_acbIoBlocks));
4564 else if (ValueUnion.u32 == 0)
4565 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
4566 else
4567 {
4568 g_acbIoBlocks[g_cIoBlocks++] = ValueUnion.u32;
4569 break;
4570 }
4571 return RTTestSummaryAndDestroy(g_hTest);
4572
4573 case 'q':
4574 g_uVerbosity = 0;
4575 break;
4576
4577 case 'v':
4578 g_uVerbosity++;
4579 break;
4580
4581 case 'h':
4582 Usage(g_pStdOut);
4583 return RTEXITCODE_SUCCESS;
4584
4585 case 'V':
4586 {
4587 char szRev[] = "$Revision: 77928 $";
4588 szRev[RT_ELEMENTS(szRev) - 2] = '\0';
4589 RTPrintf(RTStrStrip(strchr(szRev, ':') + 1));
4590 return RTEXITCODE_SUCCESS;
4591 }
4592
4593 default:
4594 return RTGetOptPrintError(rc, &ValueUnion);
4595 }
4596 }
4597
4598 /*
4599 * Create the test directory with an 'empty' subdirectory under it,
4600 * execute the tests, and remove directory when done.
4601 */
4602 RTTestBanner(g_hTest);
4603 if (!RTPathExists(g_szDir))
4604 {
4605 /* The base dir: */
4606 rc = RTDirCreate(g_szDir, 0755,
4607 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
4608 if (RT_SUCCESS(rc))
4609 {
4610 RTTestIPrintf(RTTESTLVL_ALWAYS, "Test dir: %s\n", g_szDir);
4611 rc = fsPrepTestArea();
4612 if (RT_SUCCESS(rc))
4613 {
4614 /* Profile RTTimeNanoTS(). */
4615 fsPerfNanoTS();
4616
4617 /* Do tests: */
4618 if (g_fManyFiles)
4619 fsPerfManyFiles();
4620 if (g_fOpen)
4621 fsPerfOpen();
4622 if (g_fFStat)
4623 fsPerfFStat();
4624 if (g_fFChMod)
4625 fsPerfFChMod();
4626 if (g_fFUtimes)
4627 fsPerfFUtimes();
4628 if (g_fStat)
4629 fsPerfStat();
4630 if (g_fChMod)
4631 fsPerfChmod();
4632 if (g_fUtimes)
4633 fsPerfUtimes();
4634 if (g_fRename)
4635 fsPerfRename();
4636 if (g_fDirOpen)
4637 vsPerfDirOpen();
4638 if (g_fDirEnum)
4639 vsPerfDirEnum();
4640 if (g_fMkRmDir)
4641 fsPerfMkRmDir();
4642 if (g_fStatVfs)
4643 fsPerfStatVfs();
4644 if (g_fRm || g_fManyFiles)
4645 fsPerfRm(); /* deletes manyfiles and manytree */
4646 if (g_fChSize)
4647 fsPerfChSize();
4648 if ( g_fReadPerf || g_fReadTests || g_fWritePerf || g_fWriteTests
4649#ifdef FSPERF_TEST_SENDFILE
4650 || g_fSendFile
4651#endif
4652#ifdef RT_OS_LINUX
4653 || g_fSplice
4654#endif
4655 || g_fSeek || g_fFSync || g_fMMap)
4656 fsPerfIo();
4657 if (g_fCopy)
4658 fsPerfCopy();
4659 }
4660
4661 /* Cleanup: */
4662 g_szDir[g_cchDir] = '\0';
4663 rc = RTDirRemoveRecursive(g_szDir, RTDIRRMREC_F_CONTENT_AND_DIR);
4664 if (RT_FAILURE(rc))
4665 RTTestFailed(g_hTest, "RTDirRemoveRecursive(%s,) -> %Rrc\n", g_szDir, rc);
4666 }
4667 else
4668 RTTestFailed(g_hTest, "RTDirCreate(%s) -> %Rrc\n", g_szDir, rc);
4669 }
4670 else
4671 RTTestFailed(g_hTest, "Test directory already exists: %s\n", g_szDir);
4672
4673 return RTTestSummaryAndDestroy(g_hTest);
4674}
4675
Note: See TracBrowser for help on using the repository browser.

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