VirtualBox

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

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

FsPerf: Added file-to-file sendfile testcase for linux. bugref:9172

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