VirtualBox

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

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

FsPerf: Added file copy tests and benchmark. bugref:9172

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