VirtualBox

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

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

IPRT,FsPerf: Added RTDIR_F_NO_ABS_PATH and RTDIRRMREC_F_NO_ABS_PATH to help FsPerf work below PATH_MAX on linux. bugref:9172

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

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