VirtualBox

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

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

FsPerf: Working on adding a slave that runs on the host and so we can check whether the guest sees host changes as they occur. bugref:9172

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