VirtualBox

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

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

Build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 112.9 KB
Line 
1/* $Id: FsPerf.cpp 77007 2019-01-26 18:46:50Z 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#include <iprt/alloca.h>
32#include <iprt/asm.h>
33#include <iprt/assert.h>
34#include <iprt/err.h>
35#include <iprt/dir.h>
36#include <iprt/file.h>
37#include <iprt/getopt.h>
38#include <iprt/initterm.h>
39#include <iprt/list.h>
40#include <iprt/mem.h>
41#include <iprt/message.h>
42#include <iprt/param.h>
43#include <iprt/path.h>
44#include <iprt/process.h>
45#include <iprt/rand.h>
46#include <iprt/string.h>
47#include <iprt/stream.h>
48#include <iprt/test.h>
49#include <iprt/time.h>
50#include <iprt/thread.h>
51#include <iprt/zero.h>
52
53#ifdef RT_OS_WINDOWS
54# include <iprt/nt/nt-and-windows.h>
55#else
56# include <errno.h>
57# include <unistd.h>
58# include <sys/types.h>
59# include <sys/fcntl.h>
60# ifndef RT_OS_OS2
61# include <sys/mman.h>
62# endif
63#endif
64
65
66/*********************************************************************************************************************************
67* Defined Constants And Macros *
68*********************************************************************************************************************************/
69/**
70 * Macro for profiling @a a_fnCall (typically forced inline) for about @a a_cNsTarget ns.
71 *
72 * Always does an even number of iterations.
73 */
74#define PROFILE_FN(a_fnCall, a_cNsTarget, a_szDesc) \
75 do { \
76 /* Estimate how many iterations we need to fill up the given timeslot: */ \
77 fsPerfYield(); \
78 uint64_t nsStart = RTTimeNanoTS(); \
79 uint64_t nsPrf; \
80 do \
81 nsPrf = RTTimeNanoTS(); \
82 while (nsPrf == nsStart); \
83 nsStart = nsPrf; \
84 \
85 uint64_t iIteration = 0; \
86 do \
87 { \
88 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
89 iIteration++; \
90 nsPrf = RTTimeNanoTS() - nsStart; \
91 } while (nsPrf < RT_NS_10MS || (iIteration & 1)); \
92 nsPrf /= iIteration; \
93 if (nsPrf > g_nsPerNanoTSCall + 32) \
94 nsPrf -= g_nsPerNanoTSCall; \
95 \
96 uint64_t cIterations = (a_cNsTarget) / nsPrf; \
97 if (cIterations <= 1) \
98 cIterations = 2; \
99 else if (cIterations & 1) \
100 cIterations++; \
101 \
102 /* Do the actual profiling: */ \
103 fsPerfYield(); \
104 iIteration = 0; \
105 nsStart = RTTimeNanoTS(); \
106 for (; iIteration < cIterations; iIteration++) \
107 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
108 nsPrf = RTTimeNanoTS() - nsStart; \
109 RTTestIValue(a_szDesc, nsPrf / cIterations, RTTESTUNIT_NS_PER_OCCURRENCE); \
110 if (g_fShowDuration) \
111 RTTestIValueF(nsPrf, RTTESTUNIT_NS, "%s duration", a_szDesc); \
112 if (g_fShowIterations) \
113 RTTestIValueF(iIteration, RTTESTUNIT_OCCURRENCES, "%s iterations", a_szDesc); \
114 } while (0)
115
116
117/**
118 * Macro for profiling an operation on each file in the manytree directory tree.
119 *
120 * Always does an even number of tree iterations.
121 */
122#define PROFILE_MANYTREE_FN(a_szPath, a_fnCall, a_cEstimationIterations, a_cNsTarget, a_szDesc) \
123 do { \
124 if (!g_fManyFiles) \
125 break; \
126 \
127 /* Estimate how many iterations we need to fill up the given timeslot: */ \
128 fsPerfYield(); \
129 uint64_t nsStart = RTTimeNanoTS(); \
130 uint64_t ns; \
131 do \
132 ns = RTTimeNanoTS(); \
133 while (ns == nsStart); \
134 nsStart = ns; \
135 \
136 PFSPERFNAMEENTRY pCur; \
137 uint64_t iIteration = 0; \
138 do \
139 { \
140 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
141 { \
142 memcpy(a_szPath, pCur->szName, pCur->cchName); \
143 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
144 { \
145 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
146 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
147 } \
148 } \
149 iIteration++; \
150 ns = RTTimeNanoTS() - nsStart; \
151 } while (ns < RT_NS_10MS || (iIteration & 1)); \
152 ns /= iIteration; \
153 if (ns > g_nsPerNanoTSCall + 32) \
154 ns -= g_nsPerNanoTSCall; \
155 \
156 uint32_t cIterations = (a_cNsTarget) / ns; \
157 if (cIterations <= 1) \
158 cIterations = 2; \
159 else if (cIterations & 1) \
160 cIterations++; \
161 \
162 /* Do the actual profiling: */ \
163 fsPerfYield(); \
164 uint32_t cCalls = 0; \
165 nsStart = RTTimeNanoTS(); \
166 for (iIteration = 0; iIteration < cIterations; iIteration++) \
167 { \
168 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
169 { \
170 memcpy(a_szPath, pCur->szName, pCur->cchName); \
171 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
172 { \
173 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
174 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
175 cCalls++; \
176 } \
177 } \
178 } \
179 ns = RTTimeNanoTS() - nsStart; \
180 RTTestIValueF(ns / cCalls, RTTESTUNIT_NS_PER_OCCURRENCE, a_szDesc); \
181 if (g_fShowDuration) \
182 RTTestIValueF(ns, RTTESTUNIT_NS, "%s duration", a_szDesc); \
183 if (g_fShowIterations) \
184 RTTestIValueF(iIteration, RTTESTUNIT_OCCURRENCES, "%s iterations", a_szDesc); \
185 } while (0)
186
187
188/**
189 * Execute a_fnCall for each file in the manytree.
190 */
191#define DO_MANYTREE_FN(a_szPath, a_fnCall) \
192 do { \
193 PFSPERFNAMEENTRY pCur; \
194 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry) \
195 { \
196 memcpy(a_szPath, pCur->szName, pCur->cchName); \
197 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++) \
198 { \
199 RTStrFormatU32(&a_szPath[pCur->cchName], sizeof(a_szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD); \
200 a_fnCall; \
201 } \
202 } \
203 } while (0)
204
205
206/** @def FSPERF_VERR_PATH_NOT_FOUND
207 * Hides the fact that we only get VERR_PATH_NOT_FOUND on non-unix systems. */
208#if defined(RT_OS_WINDOWS) //|| defined(RT_OS_OS2) - using posix APIs IIRC, so lost in translation.
209# define FSPERF_VERR_PATH_NOT_FOUND VERR_PATH_NOT_FOUND
210#else
211# define FSPERF_VERR_PATH_NOT_FOUND VERR_FILE_NOT_FOUND
212#endif
213
214
215/*********************************************************************************************************************************
216* Structures and Typedefs *
217*********************************************************************************************************************************/
218typedef struct FSPERFNAMEENTRY
219{
220 RTLISTNODE Entry;
221 uint16_t cchName;
222 char szName[RT_FLEXIBLE_ARRAY];
223} FSPERFNAMEENTRY;
224typedef FSPERFNAMEENTRY *PFSPERFNAMEENTRY;
225
226
227enum
228{
229 kCmdOpt_First = 128,
230
231 kCmdOpt_ManyFiles = kCmdOpt_First,
232 kCmdOpt_NoManyFiles,
233 kCmdOpt_Open,
234 kCmdOpt_NoOpen,
235 kCmdOpt_FStat,
236 kCmdOpt_NoFStat,
237 kCmdOpt_FChMod,
238 kCmdOpt_NoFChMod,
239 kCmdOpt_FUtimes,
240 kCmdOpt_NoFUtimes,
241 kCmdOpt_Stat,
242 kCmdOpt_NoStat,
243 kCmdOpt_ChMod,
244 kCmdOpt_NoChMod,
245 kCmdOpt_Utimes,
246 kCmdOpt_NoUtimes,
247 kCmdOpt_Rename,
248 kCmdOpt_NoRename,
249 kCmdOpt_DirEnum,
250 kCmdOpt_NoDirEnum,
251 kCmdOpt_MkRmDir,
252 kCmdOpt_NoMkRmDir,
253 kCmdOpt_StatVfs,
254 kCmdOpt_NoStatVfs,
255 kCmdOpt_Rm,
256 kCmdOpt_NoRm,
257 kCmdOpt_ChSize,
258 kCmdOpt_NoChSize,
259 kCmdOpt_ReadPerf,
260 kCmdOpt_NoReadPerf,
261 kCmdOpt_ReadTests,
262 kCmdOpt_NoReadTests,
263 kCmdOpt_WritePerf,
264 kCmdOpt_NoWritePerf,
265 kCmdOpt_WriteTests,
266 kCmdOpt_NoWriteTests,
267 kCmdOpt_Seek,
268 kCmdOpt_NoSeek,
269 kCmdOpt_FSync,
270 kCmdOpt_NoFSync,
271 kCmdOpt_MMap,
272 kCmdOpt_NoMMap,
273 kCmdOpt_IgnoreNoCache,
274 kCmdOpt_NoIgnoreNoCache,
275 kCmdOpt_IoFileSize,
276 kCmdOpt_SetBlockSize,
277 kCmdOpt_AddBlockSize,
278
279 kCmdOpt_ShowDuration,
280 kCmdOpt_NoShowDuration,
281 kCmdOpt_ShowIterations,
282 kCmdOpt_NoShowIterations,
283
284 kCmdOpt_ManyTreeFilesPerDir,
285 kCmdOpt_ManyTreeSubdirsPerDir,
286 kCmdOpt_ManyTreeDepth,
287
288 kCmdOpt_End
289};
290
291
292/*********************************************************************************************************************************
293* Global Variables *
294*********************************************************************************************************************************/
295/** Command line parameters */
296static const RTGETOPTDEF g_aCmdOptions[] =
297{
298 { "--dir", 'd', RTGETOPT_REQ_STRING },
299 { "--seconds", 's', RTGETOPT_REQ_UINT32 },
300 { "--milliseconds", 'm', RTGETOPT_REQ_UINT64 },
301
302 { "--enable-all", 'e', RTGETOPT_REQ_NOTHING },
303 { "--disable-all", 'z', RTGETOPT_REQ_NOTHING },
304
305 { "--many-files", kCmdOpt_ManyFiles, RTGETOPT_REQ_UINT32 },
306 { "--no-many-files", kCmdOpt_NoManyFiles, RTGETOPT_REQ_NOTHING },
307 { "--files-per-dir", kCmdOpt_ManyTreeFilesPerDir, RTGETOPT_REQ_UINT32 },
308 { "--subdirs-per-dir", kCmdOpt_ManyTreeSubdirsPerDir, RTGETOPT_REQ_UINT32 },
309 { "--tree-depth", kCmdOpt_ManyTreeDepth, RTGETOPT_REQ_UINT32 },
310
311 { "--open", kCmdOpt_Open, RTGETOPT_REQ_NOTHING },
312 { "--no-open", kCmdOpt_NoOpen, RTGETOPT_REQ_NOTHING },
313 { "--fstat", kCmdOpt_FStat, RTGETOPT_REQ_NOTHING },
314 { "--no-fstat", kCmdOpt_NoFStat, RTGETOPT_REQ_NOTHING },
315 { "--fchmod", kCmdOpt_FChMod, RTGETOPT_REQ_NOTHING },
316 { "--no-fchmod", kCmdOpt_NoFChMod, RTGETOPT_REQ_NOTHING },
317 { "--futimes", kCmdOpt_FUtimes, RTGETOPT_REQ_NOTHING },
318 { "--no-futimes", kCmdOpt_NoFUtimes, RTGETOPT_REQ_NOTHING },
319 { "--stat", kCmdOpt_Stat, RTGETOPT_REQ_NOTHING },
320 { "--no-stat", kCmdOpt_NoStat, RTGETOPT_REQ_NOTHING },
321 { "--chmod", kCmdOpt_ChMod, RTGETOPT_REQ_NOTHING },
322 { "--no-chmod", kCmdOpt_NoChMod, RTGETOPT_REQ_NOTHING },
323 { "--utimes", kCmdOpt_Utimes, RTGETOPT_REQ_NOTHING },
324 { "--no-utimes", kCmdOpt_NoUtimes, RTGETOPT_REQ_NOTHING },
325 { "--rename", kCmdOpt_Rename, RTGETOPT_REQ_NOTHING },
326 { "--no-rename", kCmdOpt_NoRename, RTGETOPT_REQ_NOTHING },
327 { "--dir-enum", kCmdOpt_DirEnum, RTGETOPT_REQ_NOTHING },
328 { "--no-dir-enum", kCmdOpt_NoDirEnum, RTGETOPT_REQ_NOTHING },
329 { "--mk-rm-dir", kCmdOpt_MkRmDir, RTGETOPT_REQ_NOTHING },
330 { "--no-mk-rm-dir", kCmdOpt_NoMkRmDir, RTGETOPT_REQ_NOTHING },
331 { "--stat-vfs", kCmdOpt_StatVfs, RTGETOPT_REQ_NOTHING },
332 { "--no-stat-vfs", kCmdOpt_NoStatVfs, RTGETOPT_REQ_NOTHING },
333 { "--rm", kCmdOpt_Rm, RTGETOPT_REQ_NOTHING },
334 { "--no-rm", kCmdOpt_NoRm, RTGETOPT_REQ_NOTHING },
335 { "--chsize", kCmdOpt_ChSize, RTGETOPT_REQ_NOTHING },
336 { "--no-chsize", kCmdOpt_NoChSize, RTGETOPT_REQ_NOTHING },
337 { "--read-tests", kCmdOpt_ReadTests, RTGETOPT_REQ_NOTHING },
338 { "--no-read-tests", kCmdOpt_NoReadTests, RTGETOPT_REQ_NOTHING },
339 { "--read-perf", kCmdOpt_ReadPerf, RTGETOPT_REQ_NOTHING },
340 { "--no-read-perf", kCmdOpt_NoReadPerf, RTGETOPT_REQ_NOTHING },
341 { "--write-tests", kCmdOpt_WriteTests, RTGETOPT_REQ_NOTHING },
342 { "--no-write-tests", kCmdOpt_NoWriteTests, RTGETOPT_REQ_NOTHING },
343 { "--write-perf", kCmdOpt_WritePerf, RTGETOPT_REQ_NOTHING },
344 { "--no-write-perf", kCmdOpt_NoWritePerf, RTGETOPT_REQ_NOTHING },
345 { "--seek", kCmdOpt_Seek, RTGETOPT_REQ_NOTHING },
346 { "--no-seek", kCmdOpt_NoSeek, RTGETOPT_REQ_NOTHING },
347 { "--fsync", kCmdOpt_FSync, RTGETOPT_REQ_NOTHING },
348 { "--no-fsync", kCmdOpt_NoFSync, RTGETOPT_REQ_NOTHING },
349 { "--mmap", kCmdOpt_MMap, RTGETOPT_REQ_NOTHING },
350 { "--no-mmap", kCmdOpt_NoMMap, RTGETOPT_REQ_NOTHING },
351 { "--ignore-no-cache", kCmdOpt_IgnoreNoCache, RTGETOPT_REQ_NOTHING },
352 { "--no-ignore-no-cache", kCmdOpt_NoIgnoreNoCache, RTGETOPT_REQ_NOTHING },
353 { "--io-file-size", kCmdOpt_IoFileSize, RTGETOPT_REQ_UINT64 },
354 { "--set-block-size", kCmdOpt_SetBlockSize, RTGETOPT_REQ_UINT32 },
355 { "--add-block-size", kCmdOpt_AddBlockSize, RTGETOPT_REQ_UINT32 },
356
357 { "--show-duration", kCmdOpt_ShowDuration, RTGETOPT_REQ_NOTHING },
358 { "--no-show-duration", kCmdOpt_NoShowDuration, RTGETOPT_REQ_NOTHING },
359 { "--show-iterations", kCmdOpt_ShowIterations, RTGETOPT_REQ_NOTHING },
360 { "--no-show-iterations", kCmdOpt_NoShowIterations, RTGETOPT_REQ_NOTHING },
361
362 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
363 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
364 { "--version", 'V', RTGETOPT_REQ_NOTHING },
365 { "--help", 'h', RTGETOPT_REQ_NOTHING } /* for Usage() */
366};
367
368/** The test handle. */
369static RTTEST g_hTest;
370/** The number of nanoseconds a RTTimeNanoTS call takes.
371 * This is used for adjusting loop count estimates. */
372static uint64_t g_nsPerNanoTSCall = 1;
373/** Whether or not to display the duration of each profile run.
374 * This is chiefly for verify the estimate phase. */
375static bool g_fShowDuration = false;
376/** Whether or not to display the iteration count for each profile run.
377 * This is chiefly for verify the estimate phase. */
378static bool g_fShowIterations = false;
379/** Verbosity level. */
380static uint32_t g_uVerbosity = 0;
381
382/** @name Selected subtest
383 * @{ */
384static bool g_fManyFiles = true;
385static bool g_fOpen = true;
386static bool g_fFStat = true;
387static bool g_fFChMod = true;
388static bool g_fFUtimes = true;
389static bool g_fStat = true;
390static bool g_fChMod = true;
391static bool g_fUtimes = true;
392static bool g_fRename = true;
393static bool g_fDirEnum = true;
394static bool g_fMkRmDir = true;
395static bool g_fStatVfs = true;
396static bool g_fRm = true;
397static bool g_fChSize = true;
398static bool g_fReadTests = true;
399static bool g_fReadPerf = true;
400static bool g_fWriteTests= true;
401static bool g_fWritePerf = true;
402static bool g_fSeek = true;
403static bool g_fFSync = true;
404static bool g_fMMap = true;
405/** @} */
406
407/** The length of each test run. */
408static uint64_t g_nsTestRun = RT_NS_1SEC_64 * 10;
409
410/** For the 'manyfiles' subdir. */
411static uint32_t g_cManyFiles = 10000;
412
413/** Number of files in the 'manytree' directory tree. */
414static uint32_t g_cManyTreeFiles = 640 + 16*640 /*10880*/;
415/** Number of files per directory in the 'manytree' construct. */
416static uint32_t g_cManyTreeFilesPerDir = 640;
417/** Number of subdirs per directory in the 'manytree' construct. */
418static uint32_t g_cManyTreeSubdirsPerDir = 16;
419/** The depth of the 'manytree' directory tree. */
420static uint32_t g_cManyTreeDepth = 1;
421/** List of directories in the many tree, creation order. */
422static RTLISTANCHOR g_ManyTreeHead;
423
424/** Number of configured I/O block sizes. */
425static uint32_t g_cIoBlocks = 8;
426/** Configured I/O block sizes. */
427static uint32_t g_acbIoBlocks[16] = { 1, 512, 4096, 16384, 65536, _1M, _32M, _128M };
428/** The desired size of the test file we use for I/O. */
429static uint64_t g_cbIoFile = _512M;
430/** Whether to be less strict with non-cache file handle. */
431static bool g_fIgnoreNoCache = false;
432
433/** The length of g_szDir. */
434static size_t g_cchDir;
435/** The length of g_szEmptyDir. */
436static size_t g_cchEmptyDir;
437/** The length of g_szDeepDir. */
438static size_t g_cchDeepDir;
439
440/** The test directory (absolute). This will always have a trailing slash. */
441static char g_szDir[RTPATH_MAX];
442/** The empty test directory (absolute). This will always have a trailing slash. */
443static char g_szEmptyDir[RTPATH_MAX];
444/** The deep test directory (absolute). This will always have a trailing slash. */
445static char g_szDeepDir[RTPATH_MAX];
446
447
448/**
449 * Yield the CPU and stuff before starting a test run.
450 */
451DECLINLINE(void) fsPerfYield(void)
452{
453 RTThreadYield();
454 RTThreadYield();
455}
456
457
458/**
459 * Profiles the RTTimeNanoTS call, setting g_nsPerNanoTSCall.
460 */
461static void fsPerfNanoTS(void)
462{
463 fsPerfYield();
464
465 /* Make sure we start off on a changing timestamp on platforms will low time resoultion. */
466 uint64_t nsStart = RTTimeNanoTS();
467 uint64_t ns;
468 do
469 ns = RTTimeNanoTS();
470 while (ns == nsStart);
471 nsStart = ns;
472
473 /* Call it for 10 ms. */
474 uint32_t i = 0;
475 do
476 {
477 i++;
478 ns = RTTimeNanoTS();
479 }
480 while (ns - nsStart < RT_NS_10MS);
481
482 g_nsPerNanoTSCall = (ns - nsStart) / i;
483}
484
485
486/**
487 * Construct a path relative to the base test directory.
488 *
489 * @returns g_szDir.
490 * @param pszAppend What to append.
491 * @param cchAppend How much to append.
492 */
493DECLINLINE(char *) InDir(const char *pszAppend, size_t cchAppend)
494{
495 Assert(g_szDir[g_cchDir - 1] == RTPATH_SLASH);
496 memcpy(&g_szDir[g_cchDir], pszAppend, cchAppend);
497 g_szDir[g_cchDir + cchAppend] = '\0';
498 return &g_szDir[0];
499}
500
501
502/**
503 * Construct a path relative to the empty directory.
504 *
505 * @returns g_szEmptyDir.
506 * @param pszAppend What to append.
507 * @param cchAppend How much to append.
508 */
509DECLINLINE(char *) InEmptyDir(const char *pszAppend, size_t cchAppend)
510{
511 Assert(g_szEmptyDir[g_cchEmptyDir - 1] == RTPATH_SLASH);
512 memcpy(&g_szEmptyDir[g_cchEmptyDir], pszAppend, cchAppend);
513 g_szEmptyDir[g_cchEmptyDir + cchAppend] = '\0';
514 return &g_szEmptyDir[0];
515}
516
517
518/**
519 * Construct a path relative to the deep test directory.
520 *
521 * @returns g_szDeepDir.
522 * @param pszAppend What to append.
523 * @param cchAppend How much to append.
524 */
525DECLINLINE(char *) InDeepDir(const char *pszAppend, size_t cchAppend)
526{
527 Assert(g_szDeepDir[g_cchDeepDir - 1] == RTPATH_SLASH);
528 memcpy(&g_szDeepDir[g_cchDeepDir], pszAppend, cchAppend);
529 g_szDeepDir[g_cchDeepDir + cchAppend] = '\0';
530 return &g_szDeepDir[0];
531}
532
533
534/**
535 * Prepares the test area.
536 * @returns VBox status code.
537 */
538static int fsPrepTestArea(void)
539{
540 /* The empty subdir and associated globals: */
541 static char s_szEmpty[] = "empty";
542 memcpy(g_szEmptyDir, g_szDir, g_cchDir);
543 memcpy(&g_szEmptyDir[g_cchDir], s_szEmpty, sizeof(s_szEmpty));
544 g_cchEmptyDir = g_cchDir + sizeof(s_szEmpty) - 1;
545 RTTESTI_CHECK_RC_RET(RTDirCreate(g_szEmptyDir, 0755, 0), VINF_SUCCESS, rcCheck);
546 g_szEmptyDir[g_cchEmptyDir++] = RTPATH_SLASH;
547 g_szEmptyDir[g_cchEmptyDir] = '\0';
548 RTTestIPrintf(RTTESTLVL_ALWAYS, "Empty dir: %s\n", g_szEmptyDir);
549
550 /* Deep directory: */
551 memcpy(g_szDeepDir, g_szDir, g_cchDir);
552 g_cchDeepDir = g_cchDir;
553 do
554 {
555 static char const s_szSub[] = "d" RTPATH_SLASH_STR;
556 memcpy(&g_szDeepDir[g_cchDeepDir], s_szSub, sizeof(s_szSub));
557 g_cchDeepDir += sizeof(s_szSub) - 1;
558 RTTESTI_CHECK_RC_RET( RTDirCreate(g_szDeepDir, 0755, 0), VINF_SUCCESS, rcCheck);
559 } while (g_cchDeepDir < 176);
560 RTTestIPrintf(RTTESTLVL_ALWAYS, "Deep dir: %s\n", g_szDeepDir);
561
562 /* Create known file in both deep and shallow dirs: */
563 RTFILE hKnownFile;
564 RTTESTI_CHECK_RC_RET(RTFileOpen(&hKnownFile, InDir(RT_STR_TUPLE("known-file")),
565 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE),
566 VINF_SUCCESS, rcCheck);
567 RTTESTI_CHECK_RC_RET(RTFileClose(hKnownFile), VINF_SUCCESS, rcCheck);
568
569 RTTESTI_CHECK_RC_RET(RTFileOpen(&hKnownFile, InDeepDir(RT_STR_TUPLE("known-file")),
570 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE),
571 VINF_SUCCESS, rcCheck);
572 RTTESTI_CHECK_RC_RET(RTFileClose(hKnownFile), VINF_SUCCESS, rcCheck);
573
574 return VINF_SUCCESS;
575}
576
577
578/**
579 * Create a name list entry.
580 * @returns Pointer to the entry, NULL if out of memory.
581 * @param pchName The name.
582 * @param cchName The name length.
583 */
584PFSPERFNAMEENTRY fsPerfCreateNameEntry(const char *pchName, size_t cchName)
585{
586 PFSPERFNAMEENTRY pEntry = (PFSPERFNAMEENTRY)RTMemAllocVar(RT_UOFFSETOF_DYN(FSPERFNAMEENTRY, szName[cchName + 1]));
587 if (pEntry)
588 {
589 RTListInit(&pEntry->Entry);
590 pEntry->cchName = (uint16_t)cchName;
591 memcpy(pEntry->szName, pchName, cchName);
592 pEntry->szName[cchName] = '\0';
593 }
594 return pEntry;
595}
596
597
598static int fsPerfManyTreeRecursiveDirCreator(size_t cchDir, uint32_t iDepth)
599{
600 PFSPERFNAMEENTRY pEntry = fsPerfCreateNameEntry(g_szDir, cchDir);
601 RTTESTI_CHECK_RET(pEntry, VERR_NO_MEMORY);
602 RTListAppend(&g_ManyTreeHead, &pEntry->Entry);
603
604 RTTESTI_CHECK_RC_RET(RTDirCreate(g_szDir, 0755, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL),
605 VINF_SUCCESS, rcCheck);
606
607 if (iDepth < g_cManyTreeDepth)
608 for (uint32_t i = 0; i < g_cManyTreeSubdirsPerDir; i++)
609 {
610 size_t cchSubDir = RTStrPrintf(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, "d%02u" RTPATH_SLASH_STR, i);
611 RTTESTI_CHECK_RC_RET(fsPerfManyTreeRecursiveDirCreator(cchDir + cchSubDir, iDepth + 1), VINF_SUCCESS, rcCheck);
612 }
613
614 return VINF_SUCCESS;
615}
616
617
618void fsPerfManyFiles(void)
619{
620 RTTestISub("manyfiles");
621
622 /*
623 * Create a sub-directory with like 10000 files in it.
624 *
625 * This does push the directory organization of the underlying file system,
626 * which is something we might not want to profile with shared folders. It
627 * is however useful for directory enumeration.
628 */
629 RTTESTI_CHECK_RC_RETV(RTDirCreate(InDir(RT_STR_TUPLE("manyfiles")), 0755,
630 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL),
631 VINF_SUCCESS);
632
633 size_t offFilename = strlen(g_szDir);
634 g_szDir[offFilename++] = RTPATH_SLASH;
635
636 fsPerfYield();
637 RTFILE hFile;
638 uint64_t const nsStart = RTTimeNanoTS();
639 for (uint32_t i = 0; i < g_cManyFiles; i++)
640 {
641 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
642 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, g_szDir, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
643 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
644 }
645 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
646 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Creating %u empty files in single directory", g_cManyFiles);
647 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Create empty file (single dir)");
648
649 /*
650 * Create a bunch of directories with exacly 32 files in each, hoping to
651 * avoid any directory organization artifacts.
652 */
653 /* Create the directories first, building a list of them for simplifying iteration: */
654 RTListInit(&g_ManyTreeHead);
655 InDir(RT_STR_TUPLE("manytree" RTPATH_SLASH_STR));
656 RTTESTI_CHECK_RC_RETV(fsPerfManyTreeRecursiveDirCreator(strlen(g_szDir), 0), VINF_SUCCESS);
657
658 /* Create the zero byte files: */
659 fsPerfYield();
660 uint64_t const nsStart2 = RTTimeNanoTS();
661 uint32_t cFiles = 0;
662 PFSPERFNAMEENTRY pCur;
663 RTListForEach(&g_ManyTreeHead, pCur, FSPERFNAMEENTRY, Entry)
664 {
665 char szPath[RTPATH_MAX];
666 memcpy(szPath, pCur->szName, pCur->cchName);
667 for (uint32_t i = 0; i < g_cManyTreeFilesPerDir; i++)
668 {
669 RTStrFormatU32(&szPath[pCur->cchName], sizeof(szPath) - pCur->cchName, i, 10, 5, 5, RTSTR_F_ZEROPAD);
670 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile, szPath, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
671 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
672 cFiles++;
673 }
674 }
675 uint64_t const cNsElapsed2 = RTTimeNanoTS() - nsStart2;
676 RTTestIValueF(cNsElapsed2, RTTESTUNIT_NS, "Creating %u empty files in tree", cFiles);
677 RTTestIValueF(cNsElapsed2 / cFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Create empty file (tree)");
678 RTTESTI_CHECK(g_cManyTreeFiles == cFiles);
679}
680
681
682DECL_FORCE_INLINE(int) fsPerfOpenExistingOnceReadonly(const char *pszFile)
683{
684 RTFILE hFile;
685 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VINF_SUCCESS, rcCheck);
686 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
687 return VINF_SUCCESS;
688}
689
690
691DECL_FORCE_INLINE(int) fsPerfOpenExistingOnceWriteonly(const char *pszFile)
692{
693 RTFILE hFile;
694 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS, rcCheck);
695 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
696 return VINF_SUCCESS;
697}
698
699
700void fsPerfOpen(void)
701{
702 RTTestISub("open");
703
704 /* Opening non-existing files. */
705 RTFILE hFile;
706 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InEmptyDir(RT_STR_TUPLE("no-such-file")),
707 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_FILE_NOT_FOUND);
708 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
709 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), FSPERF_VERR_PATH_NOT_FOUND);
710 RTTESTI_CHECK_RC(RTFileOpen(&hFile, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
711 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_PATH_NOT_FOUND);
712
713 /*
714 * Create file1 and then try exclusivly creating it again.
715 * Then profile opening it for reading.
716 */
717 RTFILE hFile1;
718 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file1")),
719 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
720 RTTESTI_CHECK_RC(RTFileOpen(&hFile, g_szDir, RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VERR_ALREADY_EXISTS);
721 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
722
723 PROFILE_FN(fsPerfOpenExistingOnceReadonly(g_szDir), g_nsTestRun, "RTFileOpen/Close/Readonly");
724 PROFILE_FN(fsPerfOpenExistingOnceWriteonly(g_szDir), g_nsTestRun, "RTFileOpen/Close/Writeonly");
725
726 /*
727 * Profile opening in the deep directory too.
728 */
729 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file1")),
730 RTFILE_O_CREATE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
731 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
732 PROFILE_FN(fsPerfOpenExistingOnceReadonly(g_szDeepDir), g_nsTestRun, "RTFileOpen/Close/deep/readonly");
733 PROFILE_FN(fsPerfOpenExistingOnceWriteonly(g_szDeepDir), g_nsTestRun, "RTFileOpen/Close/deep/writeonly");
734
735 /* Manytree: */
736 char szPath[RTPATH_MAX];
737 PROFILE_MANYTREE_FN(szPath, fsPerfOpenExistingOnceReadonly(szPath), 1, g_nsTestRun, "RTFileOpen/Close/manytree/readonly");
738}
739
740
741void fsPerfFStat(void)
742{
743 RTTestISub("fstat");
744 RTFILE hFile1;
745 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file2")),
746 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
747 RTFSOBJINFO ObjInfo = {0};
748 PROFILE_FN(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_NOTHING), g_nsTestRun, "RTFileQueryInfo/NOTHING");
749 PROFILE_FN(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_UNIX), g_nsTestRun, "RTFileQueryInfo/UNIX");
750
751 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
752}
753
754
755void fsPerfFChMod(void)
756{
757 RTTestISub("fchmod");
758 RTFILE hFile1;
759 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file4")),
760 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
761 RTFSOBJINFO ObjInfo = {0};
762 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
763 RTFMODE const fEvenMode = (ObjInfo.Attr.fMode & ~RTFS_UNIX_ALL_ACCESS_PERMS) | RTFS_DOS_READONLY | 0400;
764 RTFMODE const fOddMode = (ObjInfo.Attr.fMode & ~(RTFS_UNIX_ALL_ACCESS_PERMS | RTFS_DOS_READONLY)) | 0640;
765 PROFILE_FN(RTFileSetMode(hFile1, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTFileSetMode");
766
767 RTFileSetMode(hFile1, ObjInfo.Attr.fMode);
768 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
769}
770
771
772void fsPerfFUtimes(void)
773{
774 RTTestISub("futimes");
775 RTFILE hFile1;
776 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file5")),
777 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
778 RTTIMESPEC Time1;
779 RTTimeNow(&Time1);
780 RTTIMESPEC Time2 = Time1;
781 RTTimeSpecSubSeconds(&Time2, 3636);
782
783 RTFSOBJINFO ObjInfo0 = {0};
784 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo0, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
785
786 /* Modify modification time: */
787 RTTESTI_CHECK_RC(RTFileSetTimes(hFile1, NULL, &Time2, NULL, NULL), VINF_SUCCESS);
788 RTFSOBJINFO ObjInfo1 = {0};
789 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo1, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
790 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo1.ModificationTime) >> 2) == (RTTimeSpecGetSeconds(&Time2) >> 2));
791 char sz1[RTTIME_STR_LEN], sz2[RTTIME_STR_LEN]; /* Div by 1000 here for posix impl. using timeval. */
792 RTTESTI_CHECK_MSG(RTTimeSpecGetNano(&ObjInfo1.AccessTime) / 1000 == RTTimeSpecGetNano(&ObjInfo0.AccessTime) / 1000,
793 ("%s, expected %s", RTTimeSpecToString(&ObjInfo1.AccessTime, sz1, sizeof(sz1)),
794 RTTimeSpecToString(&ObjInfo0.AccessTime, sz2, sizeof(sz2))));
795
796 /* Modify access time: */
797 RTTESTI_CHECK_RC(RTFileSetTimes(hFile1, &Time1, NULL, NULL, NULL), VINF_SUCCESS);
798 RTFSOBJINFO ObjInfo2 = {0};
799 RTTESTI_CHECK_RC(RTFileQueryInfo(hFile1, &ObjInfo2, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS);
800 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo2.AccessTime) >> 2) == (RTTimeSpecGetSeconds(&Time1) >> 2));
801 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo2.ModificationTime) / 1000 == RTTimeSpecGetNano(&ObjInfo1.ModificationTime) / 1000);
802
803 /* Benchmark it: */
804 PROFILE_FN(RTFileSetTimes(hFile1, NULL, iIteration & 1 ? &Time1 : &Time2, NULL, NULL), g_nsTestRun, "RTFileSetTimes");
805
806 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
807}
808
809
810void fsPerfStat(void)
811{
812 RTTestISub("stat");
813 RTFSOBJINFO ObjInfo;
814
815 /* Non-existing files. */
816 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InEmptyDir(RT_STR_TUPLE("no-such-file")),
817 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VERR_FILE_NOT_FOUND);
818 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
819 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), FSPERF_VERR_PATH_NOT_FOUND);
820 RTTESTI_CHECK_RC(RTPathQueryInfoEx(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
821 &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VERR_PATH_NOT_FOUND);
822
823 /* Shallow: */
824 RTFILE hFile1;
825 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file3")),
826 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
827 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
828
829 PROFILE_FN(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), g_nsTestRun,
830 "RTPathQueryInfoEx/NOTHING");
831 PROFILE_FN(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK), g_nsTestRun,
832 "RTPathQueryInfoEx/UNIX");
833
834
835 /* Deep: */
836 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file3")),
837 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
838 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
839
840 PROFILE_FN(RTPathQueryInfoEx(g_szDeepDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), g_nsTestRun,
841 "RTPathQueryInfoEx/deep/NOTHING");
842 PROFILE_FN(RTPathQueryInfoEx(g_szDeepDir, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK), g_nsTestRun,
843 "RTPathQueryInfoEx/deep/UNIX");
844
845 /* Manytree: */
846 char szPath[RTPATH_MAX];
847 PROFILE_MANYTREE_FN(szPath, RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK),
848 1, g_nsTestRun, "RTPathQueryInfoEx/manytree/NOTHING");
849 PROFILE_MANYTREE_FN(szPath, RTPathQueryInfoEx(szPath, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK),
850 1, g_nsTestRun, "RTPathQueryInfoEx/manytree/UNIX");
851}
852
853
854void fsPerfChmod(void)
855{
856 RTTestISub("chmod");
857
858 /* Non-existing files. */
859 RTTESTI_CHECK_RC(RTPathSetMode(InEmptyDir(RT_STR_TUPLE("no-such-file")), 0665),
860 VERR_FILE_NOT_FOUND);
861 RTTESTI_CHECK_RC(RTPathSetMode(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), 0665),
862 FSPERF_VERR_PATH_NOT_FOUND);
863 RTTESTI_CHECK_RC(RTPathSetMode(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), 0665), VERR_PATH_NOT_FOUND);
864
865 /* Shallow: */
866 RTFILE hFile1;
867 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file14")),
868 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
869 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
870
871 RTFSOBJINFO ObjInfo;
872 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
873 RTFMODE const fEvenMode = (ObjInfo.Attr.fMode & ~RTFS_UNIX_ALL_ACCESS_PERMS) | RTFS_DOS_READONLY | 0400;
874 RTFMODE const fOddMode = (ObjInfo.Attr.fMode & ~(RTFS_UNIX_ALL_ACCESS_PERMS | RTFS_DOS_READONLY)) | 0640;
875 PROFILE_FN(RTPathSetMode(g_szDir, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTPathSetMode");
876 RTPathSetMode(g_szDir, ObjInfo.Attr.fMode);
877
878 /* Deep: */
879 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file14")),
880 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
881 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
882
883 PROFILE_FN(RTPathSetMode(g_szDeepDir, iIteration & 1 ? fOddMode : fEvenMode), g_nsTestRun, "RTPathSetMode/deep");
884 RTPathSetMode(g_szDeepDir, ObjInfo.Attr.fMode);
885
886 /* Manytree: */
887 char szPath[RTPATH_MAX];
888 PROFILE_MANYTREE_FN(szPath, RTPathSetMode(szPath, iIteration & 1 ? fOddMode : fEvenMode), 1, g_nsTestRun,
889 "RTPathSetMode/manytree");
890 DO_MANYTREE_FN(szPath, RTPathSetMode(szPath, ObjInfo.Attr.fMode));
891}
892
893
894void fsPerfUtimes(void)
895{
896 RTTestISub("utimes");
897
898 RTTIMESPEC Time1;
899 RTTimeNow(&Time1);
900 RTTIMESPEC Time2 = Time1;
901 RTTimeSpecSubSeconds(&Time2, 3636);
902
903 /* Non-existing files. */
904 RTTESTI_CHECK_RC(RTPathSetTimesEx(InEmptyDir(RT_STR_TUPLE("no-such-file")), NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
905 VERR_FILE_NOT_FOUND);
906 RTTESTI_CHECK_RC(RTPathSetTimesEx(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")),
907 NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
908 FSPERF_VERR_PATH_NOT_FOUND);
909 RTTESTI_CHECK_RC(RTPathSetTimesEx(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
910 NULL, &Time1, NULL, NULL, RTPATH_F_ON_LINK),
911 VERR_PATH_NOT_FOUND);
912
913 /* Shallow: */
914 RTFILE hFile1;
915 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file15")),
916 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
917 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
918
919 RTFSOBJINFO ObjInfo0 = {0};
920 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo0, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
921
922 /* Modify modification time: */
923 RTTESTI_CHECK_RC(RTPathSetTimesEx(g_szDir, NULL, &Time2, NULL, NULL, RTPATH_F_ON_LINK), VINF_SUCCESS);
924 RTFSOBJINFO ObjInfo1;
925 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo1, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
926 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo1.ModificationTime) >> 2) == (RTTimeSpecGetSeconds(&Time2) >> 2));
927 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo1.AccessTime) / 1000 == RTTimeSpecGetNano(&ObjInfo0.AccessTime) / 1000 /* posix timeval */);
928
929 /* Modify access time: */
930 RTTESTI_CHECK_RC(RTPathSetTimesEx(g_szDir, &Time1, NULL, NULL, NULL, RTPATH_F_ON_LINK), VINF_SUCCESS);
931 RTFSOBJINFO ObjInfo2 = {0};
932 RTTESTI_CHECK_RC(RTPathQueryInfoEx(g_szDir, &ObjInfo2, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS);
933 RTTESTI_CHECK((RTTimeSpecGetSeconds(&ObjInfo2.AccessTime) >> 2) == (RTTimeSpecGetSeconds(&Time1) >> 2));
934 RTTESTI_CHECK(RTTimeSpecGetNano(&ObjInfo2.ModificationTime) / 1000 == RTTimeSpecGetNano(&ObjInfo1.ModificationTime) / 1000 /* posix timeval */);
935
936 /* Profile shallow: */
937 PROFILE_FN(RTPathSetTimesEx(g_szDir, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
938 NULL, NULL, RTPATH_F_ON_LINK),
939 g_nsTestRun, "RTPathSetTimesEx");
940
941 /* Deep: */
942 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file15")),
943 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
944 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
945
946 PROFILE_FN(RTPathSetTimesEx(g_szDeepDir, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
947 NULL, NULL, RTPATH_F_ON_LINK),
948 g_nsTestRun, "RTPathSetTimesEx/deep");
949
950 /* Manytree: */
951 char szPath[RTPATH_MAX];
952 PROFILE_MANYTREE_FN(szPath, RTPathSetTimesEx(szPath, iIteration & 1 ? &Time1 : &Time2, iIteration & 1 ? &Time2 : &Time1,
953 NULL, NULL, RTPATH_F_ON_LINK),
954 1, g_nsTestRun, "RTPathSetTimesEx/manytree");
955}
956
957
958DECL_FORCE_INLINE(int) fsPerfRenameMany(const char *pszFile, uint32_t iIteration)
959{
960 char szRenamed[RTPATH_MAX];
961 strcat(strcpy(szRenamed, pszFile), "-renamed");
962 if (!(iIteration & 1))
963 return RTPathRename(pszFile, szRenamed, 0);
964 return RTPathRename(szRenamed, pszFile, 0);
965}
966
967
968void fsPerfRename(void)
969{
970 RTTestISub("rename");
971 char szPath[RTPATH_MAX];
972
973 /* Non-existing files. */
974 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("other-no-such-file")));
975 RTTESTI_CHECK_RC(RTPathRename(InEmptyDir(RT_STR_TUPLE("no-such-file")), szPath, 0), VERR_FILE_NOT_FOUND);
976 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "other-no-such-file")));
977 RTTESTI_CHECK_RC(RTPathRename(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), szPath, 0),
978 FSPERF_VERR_PATH_NOT_FOUND);
979 strcpy(szPath, InEmptyDir(RT_STR_TUPLE("other-no-such-file")));
980 RTTESTI_CHECK_RC(RTPathRename(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), szPath, 0), VERR_PATH_NOT_FOUND);
981
982 RTFILE hFile1;
983 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file16")),
984 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
985 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
986 strcat(strcpy(szPath, g_szDir), "-no-such-dir" RTPATH_SLASH_STR "file16");
987 RTTESTI_CHECK_RC(RTPathRename(szPath, g_szDir, 0), FSPERF_VERR_PATH_NOT_FOUND);
988 RTTESTI_CHECK_RC(RTPathRename(g_szDir, szPath, 0), FSPERF_VERR_PATH_NOT_FOUND);
989
990 /* Shallow: */
991 strcat(strcpy(szPath, g_szDir), "-other");
992 PROFILE_FN(RTPathRename(iIteration & 1 ? szPath : g_szDir, iIteration & 1 ? g_szDir : szPath, 0), g_nsTestRun, "RTPathRename");
993
994 /* Deep: */
995 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDeepDir(RT_STR_TUPLE("file15")),
996 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
997 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
998
999 strcat(strcpy(szPath, g_szDeepDir), "-other");
1000 PROFILE_FN(RTPathRename(iIteration & 1 ? szPath : g_szDeepDir, iIteration & 1 ? g_szDeepDir : szPath, 0),
1001 g_nsTestRun, "RTPathRename/deep");
1002
1003 /* Manytree: */
1004 PROFILE_MANYTREE_FN(szPath, fsPerfRenameMany(szPath, iIteration), 2, g_nsTestRun, "RTPathRename/manytree");
1005}
1006
1007
1008DECL_FORCE_INLINE(int) fsPerfEnumEmpty(void)
1009{
1010 RTDIR hDir;
1011 g_szEmptyDir[g_cchEmptyDir] = '\0';
1012 RTTESTI_CHECK_RC_RET(RTDirOpen(&hDir, g_szEmptyDir), VINF_SUCCESS, rcCheck);
1013
1014 RTDIRENTRY Entry;
1015 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1016 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1017 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
1018
1019 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1020 return VINF_SUCCESS;
1021}
1022
1023
1024DECL_FORCE_INLINE(int) fsPerfEnumManyFiles(void)
1025{
1026 RTDIR hDir;
1027 RTTESTI_CHECK_RC_RET(RTDirOpen(&hDir, InDir(RT_STR_TUPLE("manyfiles"))), VINF_SUCCESS, rcCheck);
1028 uint32_t cLeft = g_cManyFiles + 2;
1029 for (;;)
1030 {
1031 RTDIRENTRY Entry;
1032 if (cLeft > 0)
1033 RTTESTI_CHECK_RC_BREAK(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1034 else
1035 {
1036 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
1037 break;
1038 }
1039 cLeft--;
1040 }
1041 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1042 return VINF_SUCCESS;
1043}
1044
1045
1046void vsPerfDirEnum(void)
1047{
1048 RTTestISub("dir enum");
1049 RTDIR hDir;
1050
1051 /* Non-existing files. */
1052 RTTESTI_CHECK_RC(RTDirOpen(&hDir, InEmptyDir(RT_STR_TUPLE("no-such-file"))), VERR_FILE_NOT_FOUND);
1053 RTTESTI_CHECK_RC(RTDirOpen(&hDir, InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
1054 RTTESTI_CHECK_RC(RTDirOpen(&hDir, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
1055
1056 /*
1057 * The empty directory.
1058 */
1059 g_szEmptyDir[g_cchEmptyDir] = '\0';
1060 RTTESTI_CHECK_RC_RETV(RTDirOpen(&hDir, g_szEmptyDir), VINF_SUCCESS);
1061
1062 uint32_t fDots = 0;
1063 RTDIRENTRY Entry;
1064 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1065 RTTESTI_CHECK(RTDirEntryIsStdDotLink(&Entry));
1066 fDots |= RT_BIT_32(Entry.cbName - 1);
1067
1068 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VINF_SUCCESS);
1069 RTTESTI_CHECK(RTDirEntryIsStdDotLink(&Entry));
1070 fDots |= RT_BIT_32(Entry.cbName - 1);
1071 RTTESTI_CHECK(fDots == 3);
1072
1073 RTTESTI_CHECK_RC(RTDirRead(hDir, &Entry, NULL), VERR_NO_MORE_FILES);
1074
1075 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1076
1077 /*
1078 * The directory with many files in it.
1079 */
1080 if (g_fManyFiles)
1081 {
1082 fDots = 0;
1083 uint32_t const cBitmap = RT_ALIGN_32(g_cManyFiles, 64);
1084 void *pvBitmap = alloca(cBitmap / 8);
1085 RT_BZERO(pvBitmap, cBitmap / 8);
1086 for (uint32_t i = g_cManyFiles; i < cBitmap; i++)
1087 ASMBitSet(pvBitmap, i);
1088
1089 uint32_t cFiles = 0;
1090 RTTESTI_CHECK_RC_RETV(RTDirOpen(&hDir, InDir(RT_STR_TUPLE("manyfiles"))), VINF_SUCCESS);
1091 for (;;)
1092 {
1093 int rc = RTDirRead(hDir, &Entry, NULL);
1094 if (rc == VINF_SUCCESS)
1095 {
1096 if (Entry.szName[0] == '.')
1097 {
1098 if (Entry.szName[1] == '.')
1099 {
1100 RTTESTI_CHECK(!(fDots & 2));
1101 fDots |= 2;
1102 }
1103 else
1104 {
1105 RTTESTI_CHECK(Entry.szName[1] == '\0');
1106 RTTESTI_CHECK(!(fDots & 1));
1107 fDots |= 1;
1108 }
1109 }
1110 else
1111 {
1112 uint32_t iFile = UINT32_MAX;
1113 RTTESTI_CHECK_RC(RTStrToUInt32Full(Entry.szName, 10, &iFile), VINF_SUCCESS);
1114 if ( iFile < g_cManyFiles
1115 && !ASMBitTest(pvBitmap, iFile))
1116 {
1117 ASMBitSet(pvBitmap, iFile);
1118 cFiles++;
1119 }
1120 else
1121 RTTestFailed(g_hTest, "line %u: iFile=%u g_cManyFiles=%u\n", __LINE__, iFile, g_cManyFiles);
1122 }
1123 }
1124 else if (rc == VERR_NO_MORE_FILES)
1125 break;
1126 else
1127 {
1128 RTTestFailed(g_hTest, "RTDirRead failed enumerating manyfiles: %Rrc\n", rc);
1129 RTDirClose(hDir);
1130 return;
1131 }
1132 }
1133 RTTESTI_CHECK_RC(RTDirClose(hDir), VINF_SUCCESS);
1134 RTTESTI_CHECK(fDots == 3);
1135 RTTESTI_CHECK(cFiles == g_cManyFiles);
1136 RTTESTI_CHECK(ASMMemIsAllU8(pvBitmap, cBitmap / 8, 0xff));
1137 }
1138
1139 /*
1140 * Profile.
1141 */
1142 PROFILE_FN(fsPerfEnumEmpty(),g_nsTestRun, "RTDirOpen/Read/Close empty");
1143 if (g_fManyFiles)
1144 PROFILE_FN(fsPerfEnumManyFiles(), g_nsTestRun, "RTDirOpen/Read/Close manyfiles");
1145}
1146
1147
1148void fsPerfMkRmDir(void)
1149{
1150 RTTestISub("mkdir/rmdir");
1151
1152 /* Non-existing directories: */
1153 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir"))), VERR_FILE_NOT_FOUND);
1154 RTTESTI_CHECK_RC(RTDirRemove(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
1155 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
1156 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file")), 0755, 0), FSPERF_VERR_PATH_NOT_FOUND);
1157 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")), 0755, 0), VERR_PATH_NOT_FOUND);
1158
1159 /** @todo check what happens if non-final path component isn't a directory. unix
1160 * should return ENOTDIR and IPRT translates that to VERR_PATH_NOT_FOUND.
1161 * Curious what happens on windows. */
1162
1163 /* Already existing directories and files: */
1164 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE(".")), 0755, 0), VERR_ALREADY_EXISTS);
1165 RTTESTI_CHECK_RC(RTDirCreate(InEmptyDir(RT_STR_TUPLE("..")), 0755, 0), VERR_ALREADY_EXISTS);
1166
1167 /* Remove directory with subdirectories: */
1168#if defined(RT_OS_WINDOWS)
1169 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("."))), VERR_DIR_NOT_EMPTY);
1170#else
1171 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER); /* EINVAL for '.' */
1172#endif
1173#if defined(RT_OS_WINDOWS)
1174 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(".."))), VERR_SHARING_VIOLATION); /* weird */
1175#else
1176 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(".."))), VERR_DIR_NOT_EMPTY);
1177#endif
1178 RTTESTI_CHECK_RC(RTDirRemove(InDir(RT_STR_TUPLE(""))), VERR_DIR_NOT_EMPTY);
1179
1180 /* Create a directory and remove it: */
1181 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("subdir-1")), 0755, 0), VINF_SUCCESS);
1182 RTTESTI_CHECK_RC(RTDirRemove(g_szDir), VINF_SUCCESS);
1183
1184 /* Create a file and try remove it or create a directory with the same name: */
1185 RTFILE hFile1;
1186 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file18")),
1187 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1188 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1189 RTTESTI_CHECK_RC(RTDirRemove(g_szDir), VERR_NOT_A_DIRECTORY);
1190 RTTESTI_CHECK_RC(RTDirCreate(g_szDir, 0755, 0), VERR_ALREADY_EXISTS);
1191 RTTESTI_CHECK_RC(RTDirCreate(InDir(RT_STR_TUPLE("file18" RTPATH_SLASH_STR "subdir")), 0755, 0), VERR_PATH_NOT_FOUND);
1192
1193 /*
1194 * Profile alternately creating and removing a bunch of directories.
1195 */
1196 RTTESTI_CHECK_RC_RETV(RTDirCreate(InDir(RT_STR_TUPLE("subdir-2")), 0755, 0), VINF_SUCCESS);
1197 size_t cchDir = strlen(g_szDir);
1198 g_szDir[cchDir++] = RTPATH_SLASH;
1199 g_szDir[cchDir++] = 's';
1200
1201 uint32_t cCreated = 0;
1202 uint64_t nsCreate = 0;
1203 uint64_t nsRemove = 0;
1204 for (;;)
1205 {
1206 /* Create a bunch: */
1207 uint64_t nsStart = RTTimeNanoTS();
1208 for (uint32_t i = 0; i < 998; i++)
1209 {
1210 RTStrFormatU32(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, i, 10, 3, 3, RTSTR_F_ZEROPAD);
1211 RTTESTI_CHECK_RC_RETV(RTDirCreate(g_szDir, 0755, 0), VINF_SUCCESS);
1212 }
1213 nsCreate += RTTimeNanoTS() - nsStart;
1214 cCreated += 998;
1215
1216 /* Remove the bunch: */
1217 nsStart = RTTimeNanoTS();
1218 for (uint32_t i = 0; i < 998; i++)
1219 {
1220 RTStrFormatU32(&g_szDir[cchDir], sizeof(g_szDir) - cchDir, i, 10, 3, 3, RTSTR_F_ZEROPAD);
1221 RTTESTI_CHECK_RC_RETV(RTDirRemove(g_szDir), VINF_SUCCESS);
1222 }
1223 nsRemove = RTTimeNanoTS() - nsStart;
1224
1225 /* Check if we got time for another round: */
1226 if ( ( nsRemove >= g_nsTestRun
1227 && nsCreate >= g_nsTestRun)
1228 || nsCreate + nsRemove >= g_nsTestRun * 3)
1229 break;
1230 }
1231 RTTestIValue("RTDirCreate", nsCreate / cCreated, RTTESTUNIT_NS_PER_OCCURRENCE);
1232 RTTestIValue("RTDirRemove", nsRemove / cCreated, RTTESTUNIT_NS_PER_OCCURRENCE);
1233}
1234
1235
1236void fsPerfStatVfs(void)
1237{
1238 RTTestISub("statvfs");
1239
1240 g_szEmptyDir[g_cchEmptyDir] = '\0';
1241 RTFOFF cbTotal;
1242 RTFOFF cbFree;
1243 uint32_t cbBlock;
1244 uint32_t cbSector;
1245 RTTESTI_CHECK_RC(RTFsQuerySizes(g_szEmptyDir, &cbTotal, &cbFree, &cbBlock, &cbSector), VINF_SUCCESS);
1246
1247 uint32_t uSerial;
1248 RTTESTI_CHECK_RC(RTFsQuerySerial(g_szEmptyDir, &uSerial), VINF_SUCCESS);
1249
1250 RTFSPROPERTIES Props;
1251 RTTESTI_CHECK_RC(RTFsQueryProperties(g_szEmptyDir, &Props), VINF_SUCCESS);
1252
1253 RTFSTYPE enmType;
1254 RTTESTI_CHECK_RC(RTFsQueryType(g_szEmptyDir, &enmType), VINF_SUCCESS);
1255
1256}
1257
1258
1259void fsPerfRm(void)
1260{
1261 RTTestISub("rm");
1262
1263 /* Non-existing files. */
1264 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-file"))), VERR_FILE_NOT_FOUND);
1265 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("no-such-dir" RTPATH_SLASH_STR "no-such-file"))), FSPERF_VERR_PATH_NOT_FOUND);
1266 RTTESTI_CHECK_RC(RTFileDelete(InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file"))), VERR_PATH_NOT_FOUND);
1267
1268 /* Directories: */
1269#if defined(RT_OS_WINDOWS)
1270 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_ACCESS_DENIED);
1271 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_ACCESS_DENIED);
1272 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
1273#elif defined(RT_OS_DARWIN) /* unlink() on xnu 16.7.0 is behaviour totally werid: */
1274 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_INVALID_PARAMETER);
1275 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VINF_SUCCESS /*WTF?!?*/);
1276 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_ACCESS_DENIED);
1277#else
1278 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE("."))), VERR_IS_A_DIRECTORY);
1279 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(".."))), VERR_IS_A_DIRECTORY);
1280 RTTESTI_CHECK_RC(RTFileDelete(InEmptyDir(RT_STR_TUPLE(""))), VERR_IS_A_DIRECTORY);
1281#endif
1282
1283 /* Shallow: */
1284 RTFILE hFile1;
1285 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file19")),
1286 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_WRITE), VINF_SUCCESS);
1287 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1288 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
1289 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VERR_FILE_NOT_FOUND);
1290
1291 if (g_fManyFiles)
1292 {
1293 /*
1294 * Profile the deletion of the manyfiles content.
1295 */
1296 {
1297 InDir(RT_STR_TUPLE("manyfiles" RTPATH_SLASH_STR));
1298 size_t const offFilename = strlen(g_szDir);
1299 fsPerfYield();
1300 uint64_t const nsStart = RTTimeNanoTS();
1301 for (uint32_t i = 0; i < g_cManyFiles; i++)
1302 {
1303 RTStrFormatU32(&g_szDir[offFilename], sizeof(g_szDir) - offFilename, i, 10, 5, 5, RTSTR_F_ZEROPAD);
1304 RTTESTI_CHECK_RC_RETV(RTFileDelete(g_szDir), VINF_SUCCESS);
1305 }
1306 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
1307 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files from a single directory", g_cManyFiles);
1308 RTTestIValueF(cNsElapsed / g_cManyFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (single dir)");
1309 }
1310
1311 /*
1312 * Ditto for the manytree.
1313 */
1314 {
1315 char szPath[RTPATH_MAX];
1316 uint64_t const nsStart = RTTimeNanoTS();
1317 DO_MANYTREE_FN(szPath, RTTESTI_CHECK_RC_RETV(RTFileDelete(szPath), VINF_SUCCESS));
1318 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
1319 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "Deleted %u empty files in tree", g_cManyTreeFiles);
1320 RTTestIValueF(cNsElapsed / g_cManyTreeFiles, RTTESTUNIT_NS_PER_OCCURRENCE, "Delete file (tree)");
1321 }
1322 }
1323}
1324
1325
1326void fsPerfChSize(void)
1327{
1328 RTTestISub("chsize");
1329
1330 /*
1331 * We need some free space to perform this test.
1332 */
1333 g_szDir[g_cchDir] = '\0';
1334 RTFOFF cbFree = 0;
1335 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
1336 if (cbFree < _1M)
1337 {
1338 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 1MB", cbFree);
1339 return;
1340 }
1341
1342 /*
1343 * Create a file and play around with it's size.
1344 * We let the current file position follow the end position as we make changes.
1345 */
1346 RTFILE hFile1;
1347 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file20")),
1348 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
1349 uint64_t cbFile = UINT64_MAX;
1350 RTTESTI_CHECK_RC(RTFileGetSize(hFile1, &cbFile), VINF_SUCCESS);
1351 RTTESTI_CHECK(cbFile == 0);
1352
1353 uint8_t abBuf[4096];
1354 static uint64_t const s_acbChanges[] =
1355 {
1356 1023, 1024, 1024, 1025, 8192, 11111, _1M, _8M, _8M,
1357 _4M, _2M + 1, _1M - 1, 65537, 65536, 32768, 8000, 7999, 7998, 1024, 1, 0
1358 };
1359 uint64_t cbOld = 0;
1360 for (unsigned i = 0; i < RT_ELEMENTS(s_acbChanges); i++)
1361 {
1362 uint64_t cbNew = s_acbChanges[i];
1363 if (cbNew + _64K >= (uint64_t)cbFree)
1364 continue;
1365
1366 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, cbNew), VINF_SUCCESS);
1367 RTTESTI_CHECK_RC(RTFileGetSize(hFile1, &cbFile), VINF_SUCCESS);
1368 RTTESTI_CHECK_MSG(cbFile == cbNew, ("cbFile=%#RX64 cbNew=%#RX64\n", cbFile, cbNew));
1369
1370 if (cbNew > cbOld)
1371 {
1372 /* Check that the extension is all zeroed: */
1373 uint64_t cbLeft = cbNew - cbOld;
1374 while (cbLeft > 0)
1375 {
1376 memset(abBuf, 0xff, sizeof(abBuf));
1377 size_t cbToRead = sizeof(abBuf);
1378 if (cbToRead > cbLeft)
1379 cbToRead = (size_t)cbLeft;
1380 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, cbToRead, NULL), VINF_SUCCESS);
1381 RTTESTI_CHECK(ASMMemIsZero(abBuf, cbToRead));
1382 cbLeft -= cbToRead;
1383 }
1384 }
1385 else
1386 {
1387 /* Check that reading fails with EOF because current position is now beyond the end: */
1388 RTTESTI_CHECK_RC(RTFileRead(hFile1, abBuf, 1, NULL), VERR_EOF);
1389
1390 /* Keep current position at the end of the file: */
1391 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbNew, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1392 }
1393 cbOld = cbNew;
1394 }
1395
1396 /*
1397 * Profile just the file setting operation itself, keeping the changes within
1398 * an allocation unit to avoid needing to adjust the actual (host) FS allocation.
1399 * ASSUMES allocation unit >= 512 and power of two.
1400 */
1401 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, _64K), VINF_SUCCESS);
1402 PROFILE_FN(RTFileSetSize(hFile1, _64K - (iIteration & 255) - 128), g_nsTestRun, "RTFileSetSize/noalloc");
1403
1404 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
1405 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
1406 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
1407}
1408
1409
1410int fsPerfIoPrepFile(RTFILE hFile1, uint64_t cbFile, uint8_t **ppbFree)
1411{
1412 /*
1413 * Seek to the end - 4K and write the last 4K.
1414 * This should have the effect of filling the whole file with zeros.
1415 */
1416 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, cbFile - _4K, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1417 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, g_abRTZero4K, _4K, NULL), VINF_SUCCESS, rcCheck);
1418
1419 /*
1420 * Check that the space we searched across actually is zero filled.
1421 */
1422 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1423 size_t cbBuf = _1M;
1424 uint8_t *pbBuf = *ppbFree = (uint8_t *)RTMemAlloc(cbBuf);
1425 RTTESTI_CHECK_RET(pbBuf != NULL, VERR_NO_MEMORY);
1426 uint64_t cbLeft = cbFile;
1427 while (cbLeft > 0)
1428 {
1429 size_t cbToRead = cbBuf;
1430 if (cbToRead > cbLeft)
1431 cbToRead = (size_t)cbLeft;
1432 pbBuf[cbToRead] = 0xff;
1433
1434 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBuf, cbToRead, NULL), VINF_SUCCESS, rcCheck);
1435 RTTESTI_CHECK_RET(ASMMemIsZero(pbBuf, cbToRead), VERR_MISMATCH);
1436
1437 cbLeft -= cbToRead;
1438 }
1439
1440 /*
1441 * Fill the file with 0xf6 and insert offset markers with 1KB intervals.
1442 */
1443 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1444 memset(pbBuf, 0xf6, cbBuf);
1445 cbLeft = cbFile;
1446 uint64_t off = 0;
1447 while (cbLeft > 0)
1448 {
1449 Assert(!(off & (_1K - 1)));
1450 Assert(!(cbBuf & (_1K - 1)));
1451 for (size_t offBuf = 0; offBuf < cbBuf; offBuf += _1K, off += _1K)
1452 *(uint64_t *)&pbBuf[offBuf] = off;
1453
1454 size_t cbToWrite = cbBuf;
1455 if (cbToWrite > cbLeft)
1456 cbToWrite = (size_t)cbLeft;
1457
1458 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbToWrite, NULL), VINF_SUCCESS, rcCheck);
1459
1460 cbLeft -= cbToWrite;
1461 }
1462
1463 return VINF_SUCCESS;
1464}
1465
1466
1467/**
1468 * Checks the content read from the file fsPerfIoPrepFile() prepared.
1469 */
1470bool fsPerfCheckReadBuf(unsigned uLineNo, uint64_t off, uint8_t const *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
1471{
1472 uint32_t cMismatches = 0;
1473 size_t offBuf = 0;
1474 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
1475 while (offBuf < cbBuf)
1476 {
1477 /*
1478 * Check the offset marker:
1479 */
1480 if (offBlock < sizeof(uint64_t))
1481 {
1482 RTUINT64U uMarker;
1483 uMarker.u = off + offBuf - offBlock;
1484 unsigned offMarker = offBlock & (sizeof(uint64_t) - 1);
1485 while (offMarker < sizeof(uint64_t) && offBuf < cbBuf)
1486 {
1487 if (uMarker.au8[offMarker] != pbBuf[offBuf])
1488 {
1489 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#x",
1490 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], uMarker.au8[offMarker]);
1491 if (cMismatches++ > 32)
1492 return false;
1493 }
1494 offMarker++;
1495 offBuf++;
1496 }
1497 offBlock = sizeof(uint64_t);
1498 }
1499
1500 /*
1501 * Check the filling:
1502 */
1503 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf - offBuf);
1504 if ( cbFilling == 0
1505 || ASMMemIsAllU8(&pbBuf[offBuf], cbFilling, bFiller))
1506 offBuf += cbFilling;
1507 else
1508 {
1509 /* Some mismatch, locate it/them: */
1510 while (cbFilling > 0 && offBuf < cbBuf)
1511 {
1512 if (pbBuf[offBuf] != bFiller)
1513 {
1514 RTTestIFailed("%u: Mismatch at buffer/file offset %#zx/%#RX64: %#x, expected %#04x",
1515 uLineNo, offBuf, off + offBuf, pbBuf[offBuf], bFiller);
1516 if (cMismatches++ > 32)
1517 return false;
1518 }
1519 offBuf++;
1520 cbFilling--;
1521 }
1522 }
1523 offBlock = 0;
1524 }
1525 return cMismatches == 0;
1526}
1527
1528
1529/**
1530 * Sets up write buffer with offset markers and fillers.
1531 */
1532void fsPerfFillWriteBuf(uint64_t off, uint8_t *pbBuf, size_t cbBuf, uint8_t bFiller = 0xf6)
1533{
1534 uint32_t offBlock = (uint32_t)(off & (_1K - 1));
1535 while (cbBuf > 0)
1536 {
1537 /* The marker. */
1538 if (offBlock < sizeof(uint64_t))
1539 {
1540 RTUINT64U uMarker;
1541 uMarker.u = off + offBlock;
1542 if (cbBuf > sizeof(uMarker) - offBlock)
1543 {
1544 memcpy(pbBuf, &uMarker.au8[offBlock], sizeof(uMarker) - offBlock);
1545 pbBuf += sizeof(uMarker) - offBlock;
1546 cbBuf -= sizeof(uMarker) - offBlock;
1547 off += sizeof(uMarker) - offBlock;
1548 }
1549 else
1550 {
1551 memcpy(pbBuf, &uMarker.au8[offBlock], cbBuf);
1552 return;
1553 }
1554 offBlock = sizeof(uint64_t);
1555 }
1556
1557 /* Do the filling. */
1558 size_t cbFilling = RT_MIN(_1K - offBlock, cbBuf);
1559 memset(pbBuf, bFiller, cbFilling);
1560 pbBuf += cbFilling;
1561 cbBuf -= cbFilling;
1562 off += cbFilling;
1563
1564 offBlock = 0;
1565 }
1566}
1567
1568
1569
1570void fsPerfIoSeek(RTFILE hFile1, uint64_t cbFile)
1571{
1572 /*
1573 * Do a bunch of search tests, most which are random.
1574 */
1575 struct
1576 {
1577 int rc;
1578 uint32_t uMethod;
1579 int64_t offSeek;
1580 uint64_t offActual;
1581
1582 } aSeeks[9 + 64] =
1583 {
1584 { VINF_SUCCESS, RTFILE_SEEK_BEGIN, 0, 0 },
1585 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
1586 { VINF_SUCCESS, RTFILE_SEEK_END, 0, cbFile },
1587 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -4096, cbFile - 4096 },
1588 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 4096 - (int64_t)cbFile, 0 },
1589 { VINF_SUCCESS, RTFILE_SEEK_END, -(int64_t)cbFile/2, cbFile / 2 + (cbFile & 1) },
1590 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, -(int64_t)cbFile/2, 0 },
1591#if defined(RT_OS_WINDOWS)
1592 { VERR_NEGATIVE_SEEK, RTFILE_SEEK_CURRENT, -1, 0 },
1593#else
1594 { VERR_INVALID_PARAMETER, RTFILE_SEEK_CURRENT, -1, 0 },
1595#endif
1596 { VINF_SUCCESS, RTFILE_SEEK_CURRENT, 0, 0 },
1597 };
1598
1599 uint64_t offActual = 0;
1600 for (unsigned i = 9; i < RT_ELEMENTS(aSeeks); i++)
1601 {
1602 switch (RTRandU32Ex(RTFILE_SEEK_BEGIN, RTFILE_SEEK_END))
1603 {
1604 default: AssertFailedBreak();
1605 case RTFILE_SEEK_BEGIN:
1606 aSeeks[i].uMethod = RTFILE_SEEK_BEGIN;
1607 aSeeks[i].rc = VINF_SUCCESS;
1608 aSeeks[i].offSeek = RTRandU64Ex(0, cbFile + cbFile / 8);
1609 aSeeks[i].offActual = offActual = aSeeks[i].offSeek;
1610 break;
1611
1612 case RTFILE_SEEK_CURRENT:
1613 aSeeks[i].uMethod = RTFILE_SEEK_CURRENT;
1614 aSeeks[i].rc = VINF_SUCCESS;
1615 aSeeks[i].offSeek = (int64_t)RTRandU64Ex(0, cbFile + cbFile / 8) - (int64_t)offActual;
1616 aSeeks[i].offActual = offActual += aSeeks[i].offSeek;
1617 break;
1618
1619 case RTFILE_SEEK_END:
1620 aSeeks[i].uMethod = RTFILE_SEEK_END;
1621 aSeeks[i].rc = VINF_SUCCESS;
1622 aSeeks[i].offSeek = -(int64_t)RTRandU64Ex(0, cbFile);
1623 aSeeks[i].offActual = offActual = cbFile + aSeeks[i].offSeek;
1624 break;
1625 }
1626 }
1627
1628 for (unsigned iDoReadCheck = 0; iDoReadCheck < 2; iDoReadCheck++)
1629 {
1630 for (uint32_t i = 0; i < RT_ELEMENTS(aSeeks); i++)
1631 {
1632 offActual = UINT64_MAX;
1633 int rc = RTFileSeek(hFile1, aSeeks[i].offSeek, aSeeks[i].uMethod, &offActual);
1634 if (rc != aSeeks[i].rc)
1635 RTTestIFailed("Seek #%u: Expected %Rrc, got %Rrc", i, aSeeks[i].rc, rc);
1636 if (RT_SUCCESS(rc) && offActual != aSeeks[i].offActual)
1637 RTTestIFailed("Seek #%u: offActual %#RX64, expected %#RX64", i, offActual, aSeeks[i].offActual);
1638 if (RT_SUCCESS(rc))
1639 {
1640 uint64_t offTell = RTFileTell(hFile1);
1641 if (offTell != offActual)
1642 RTTestIFailed("Seek #%u: offActual %#RX64, RTFileTell %#RX64", i, offActual, offTell);
1643 }
1644
1645 if (RT_SUCCESS(rc) && offActual + _2K <= cbFile && iDoReadCheck)
1646 {
1647 uint8_t abBuf[_2K];
1648 RTTESTI_CHECK_RC(rc = RTFileRead(hFile1, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
1649 if (RT_SUCCESS(rc))
1650 {
1651 size_t offMarker = (size_t)(RT_ALIGN_64(offActual, _1K) - offActual);
1652 uint64_t uMarker = *(uint64_t *)&abBuf[offMarker]; /** @todo potentially unaligned access */
1653 if (uMarker != offActual + offMarker)
1654 RTTestIFailed("Seek #%u: Invalid marker value (@ %#RX64): %#RX64, expected %#RX64",
1655 i, offActual, uMarker, offActual + offMarker);
1656
1657 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -(int64_t)sizeof(abBuf), RTFILE_SEEK_CURRENT, NULL), VINF_SUCCESS);
1658 }
1659 }
1660 }
1661 }
1662
1663
1664 /*
1665 * Profile seeking relative to the beginning of the file and relative
1666 * to the end. The latter might be more expensive in a SF context.
1667 */
1668 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? iIteration : iIteration % cbFile, RTFILE_SEEK_BEGIN, NULL),
1669 g_nsTestRun, "RTFileSeek/BEGIN");
1670 PROFILE_FN(RTFileSeek(hFile1, iIteration < cbFile ? -(int64_t)iIteration : -(int64_t)(iIteration % cbFile), RTFILE_SEEK_END, NULL),
1671 g_nsTestRun, "RTFileSeek/END");
1672
1673}
1674
1675
1676/** For fsPerfIoRead and fsPerfIoWrite. */
1677#define PROFILE_IO_FN(a_szOperation, a_fnCall) \
1678 do \
1679 { \
1680 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); \
1681 uint64_t offActual = 0; \
1682 uint32_t cSeeks = 0; \
1683 \
1684 /* Estimate how many iterations we need to fill up the given timeslot: */ \
1685 fsPerfYield(); \
1686 uint64_t nsStart = RTTimeNanoTS(); \
1687 uint64_t ns; \
1688 do \
1689 ns = RTTimeNanoTS(); \
1690 while (ns == nsStart); \
1691 nsStart = ns; \
1692 \
1693 uint64_t iIteration = 0; \
1694 do \
1695 { \
1696 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
1697 iIteration++; \
1698 ns = RTTimeNanoTS() - nsStart; \
1699 } while (ns < RT_NS_10MS); \
1700 ns /= iIteration; \
1701 if (ns > g_nsPerNanoTSCall + 32) \
1702 ns -= g_nsPerNanoTSCall; \
1703 uint64_t cIterations = g_nsTestRun / ns; \
1704 if (cIterations < 2) \
1705 cIterations = 2; \
1706 else if (cIterations & 1) \
1707 cIterations++; \
1708 \
1709 /* Do the actual profiling: */ \
1710 cSeeks = 0; \
1711 iIteration = 0; \
1712 fsPerfYield(); \
1713 nsStart = RTTimeNanoTS(); \
1714 for (uint32_t iAdjust = 0; iAdjust < 4; iAdjust++) \
1715 { \
1716 for (; iIteration < cIterations; iIteration++)\
1717 RTTESTI_CHECK_RC(a_fnCall, VINF_SUCCESS); \
1718 ns = RTTimeNanoTS() - nsStart;\
1719 if (ns >= g_nsTestRun - (g_nsTestRun / 10)) \
1720 break; \
1721 cIterations += cIterations / 4; \
1722 if (cIterations & 1) \
1723 cIterations++; \
1724 nsStart += g_nsPerNanoTSCall; \
1725 } \
1726 RTTestIValueF(ns / iIteration, \
1727 RTTESTUNIT_NS_PER_OCCURRENCE, a_szOperation "/seq/%RU32 latency", cbBlock); \
1728 RTTestIValueF((uint64_t)((uint64_t)iIteration * cbBlock / ((double)ns / RT_NS_1SEC)), \
1729 RTTESTUNIT_BYTES_PER_SEC, a_szOperation "/seq/%RU32 throughput", cbBlock); \
1730 RTTestIValueF(iIteration, \
1731 RTTESTUNIT_CALLS, a_szOperation "/seq/%RU32 calls", cbBlock); \
1732 RTTestIValueF((uint64_t)iIteration * cbBlock, \
1733 RTTESTUNIT_BYTES, a_szOperation "/seq/%RU32 bytes", cbBlock); \
1734 RTTestIValueF(cSeeks, \
1735 RTTESTUNIT_OCCURRENCES, a_szOperation "/seq/%RU32 seeks", cbBlock); \
1736 if (g_fShowDuration) \
1737 RTTestIValueF(ns, RTTESTUNIT_NS, a_szOperation "/seq/%RU32 duration", cbBlock); \
1738 } while (0)
1739
1740
1741/**
1742 * One RTFileRead profiling iteration.
1743 */
1744DECL_FORCE_INLINE(int) fsPerfIoReadWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
1745 uint64_t *poffActual, uint32_t *pcSeeks)
1746{
1747 /* Do we need to seek back to the start? */
1748 if (*poffActual + cbBlock <= cbFile)
1749 { /* likely */ }
1750 else
1751 {
1752 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1753 *pcSeeks += 1;
1754 *poffActual = 0;
1755 }
1756
1757 size_t cbActuallyRead = 0;
1758 RTTESTI_CHECK_RC_RET(RTFileRead(hFile1, pbBlock, cbBlock, &cbActuallyRead), VINF_SUCCESS, rcCheck);
1759 if (cbActuallyRead == cbBlock)
1760 {
1761 *poffActual += cbActuallyRead;
1762 return VINF_SUCCESS;
1763 }
1764 RTTestIFailed("RTFileRead at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyRead, cbBlock);
1765 *poffActual += cbActuallyRead;
1766 return VERR_READ_ERROR;
1767}
1768
1769
1770void fsPerfIoReadBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
1771{
1772 RTTestISubF("IO - Sequential read %RU32", cbBlock);
1773
1774 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
1775 if (pbBuf)
1776 {
1777 memset(pbBuf, 0xf7, cbBlock);
1778 PROFILE_IO_FN("RTFileRead", fsPerfIoReadWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
1779 RTMemPageFree(pbBuf, cbBlock);
1780 }
1781 else
1782 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
1783}
1784
1785
1786void fsPerfRead(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
1787{
1788 RTTestISubF("IO - RTFileRead");
1789
1790 /*
1791 * Allocate a big buffer we can play around with. Min size is 1MB.
1792 */
1793 size_t cbBuf = cbFile < _64M ? (size_t)cbFile : _64M;
1794 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
1795 while (!pbBuf)
1796 {
1797 cbBuf /= 2;
1798 RTTESTI_CHECK_RETV(cbBuf >= _1M);
1799 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
1800 }
1801
1802#if 1
1803 /*
1804 * Start at the beginning and read the full buffer in random small chunks, thereby
1805 * checking that unaligned buffer addresses, size and file offsets work fine.
1806 */
1807 struct
1808 {
1809 uint64_t offFile;
1810 uint32_t cbMax;
1811 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
1812 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++)
1813 {
1814 memset(pbBuf, 0x55, cbBuf);
1815 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1816 for (size_t offBuf = 0; offBuf < cbBuf; )
1817 {
1818 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
1819 uint32_t const cbToRead = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
1820 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
1821 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
1822 size_t cbActual = 0;
1823 RTTESTI_CHECK_RC(RTFileRead(hFile1, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
1824 if (cbActual == cbToRead)
1825 offBuf += cbActual;
1826 else
1827 {
1828 RTTestIFailed("Attempting to read %#x bytes at %#zx, only got %#x bytes back! (cbLeft=%#x cbBuf=%#zx)\n",
1829 cbToRead, offBuf, cbActual, cbLeft, cbBuf);
1830 if (cbActual)
1831 offBuf += cbActual;
1832 else
1833 pbBuf[offBuf++] = 0x11;
1834 }
1835 }
1836 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf);
1837 }
1838#endif
1839
1840 /*
1841 * Test reading beyond the end of the file.
1842 */
1843 size_t const acbMax[] = { cbBuf, _64K, _16K, _4K, 256 };
1844 uint32_t const aoffFromEos[] =
1845 { 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,
1846 4092, 4093, 4094, 4095, 4096, 4097, 4098, 4099, 4100, 8192, 16384, 32767, 32768, 32769, 65535, 65536, _1M - 1
1847 };
1848 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
1849 {
1850 size_t const cbMaxRead = acbMax[iMax];
1851 for (uint32_t iOffFromEos = 0; iOffFromEos < RT_ELEMENTS(aoffFromEos); iOffFromEos++)
1852 {
1853 uint32_t off = aoffFromEos[iOffFromEos];
1854 if (off >= cbMaxRead)
1855 continue;
1856 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1857 size_t cbActual = ~(size_t)0;
1858 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
1859 RTTESTI_CHECK(cbActual == off);
1860
1861 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile - off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1862 cbActual = ~(size_t)0;
1863 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, off, &cbActual), VINF_SUCCESS);
1864 RTTESTI_CHECK_MSG(cbActual == off, ("%#zx vs %#zx", cbActual, off));
1865
1866 cbActual = ~(size_t)0;
1867 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, 1, &cbActual), VINF_SUCCESS);
1868 RTTESTI_CHECK(cbActual == 0);
1869
1870 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
1871 }
1872 }
1873
1874 /*
1875 * Test reading beyond end of the file.
1876 */
1877 for (unsigned iMax = 0; iMax < RT_ELEMENTS(acbMax); iMax++)
1878 {
1879 size_t const cbMaxRead = acbMax[iMax];
1880 for (uint32_t off = 0; off < 256; off++)
1881 {
1882 RTTESTI_CHECK_RC(RTFileSeek(hFile1, cbFile + off, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1883 size_t cbActual = ~(size_t)0;
1884 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, &cbActual), VINF_SUCCESS);
1885 RTTESTI_CHECK(cbActual == 0);
1886
1887 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, cbMaxRead, NULL), VERR_EOF);
1888 }
1889 }
1890
1891 /*
1892 * Do uncached access, must be page aligned.
1893 */
1894 uint32_t cbPage = PAGE_SIZE;
1895 memset(pbBuf, 0x66, cbBuf);
1896 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
1897 {
1898 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
1899 for (size_t offBuf = 0; offBuf < cbBuf; )
1900 {
1901 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / cbPage);
1902 uint32_t const cPagesToRead = RTRandU32Ex(1, cPagesLeft);
1903 size_t const cbToRead = cPagesToRead * (size_t)cbPage;
1904 size_t cbActual = 0;
1905 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, &pbBuf[offBuf], cbToRead, &cbActual), VINF_SUCCESS);
1906 if (cbActual == cbToRead)
1907 offBuf += cbActual;
1908 else
1909 {
1910 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x bytes back!\n", cbToRead, offBuf, cbActual);
1911 if (cbActual)
1912 offBuf += cbActual;
1913 else
1914 {
1915 memset(&pbBuf[offBuf], 0x11, cbPage);
1916 offBuf += cbPage;
1917 }
1918 }
1919 }
1920 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf);
1921 }
1922
1923 /*
1924 * Check reading zero bytes at the end of the file.
1925 * Requires native call because RTFileWrite doesn't call kernel on zero byte reads.
1926 */
1927 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
1928#ifdef RT_OS_WINDOWS
1929 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
1930 NTSTATUS rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
1931 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
1932 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
1933 RTTESTI_CHECK(Ios.Information == 0);
1934
1935 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
1936 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 1, NULL, NULL);
1937 RTTESTI_CHECK_MSG(rcNt == STATUS_END_OF_FILE, ("rcNt=%#x", rcNt));
1938 RTTESTI_CHECK(Ios.Status == STATUS_END_OF_FILE);
1939 RTTESTI_CHECK(Ios.Information == 0);
1940#else
1941 ssize_t cbRead = read((int)RTFileToNative(hFile1), pbBuf, 0);
1942 RTTESTI_CHECK(cbRead == 0);
1943#endif
1944
1945 /*
1946 * Other OS specific stuff.
1947 */
1948#ifdef RT_OS_WINDOWS
1949 /* Check that reading at an offset modifies the position: */
1950 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
1951 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
1952
1953 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
1954 LARGE_INTEGER offNt;
1955 offNt.QuadPart = cbFile / 2;
1956 rcNt = NtReadFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
1957 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
1958 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
1959 RTTESTI_CHECK(Ios.Information == _4K);
1960 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
1961 fsPerfCheckReadBuf(__LINE__, cbFile / 2, pbBuf, _4K);
1962#endif
1963
1964 RTMemPageFree(pbBuf, cbBuf);
1965}
1966
1967
1968/**
1969 * One RTFileWrite profiling iteration.
1970 */
1971DECL_FORCE_INLINE(int) fsPerfIoWriteWorker(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock, uint8_t *pbBlock,
1972 uint64_t *poffActual, uint32_t *pcSeeks)
1973{
1974 /* Do we need to seek back to the start? */
1975 if (*poffActual + cbBlock <= cbFile)
1976 { /* likely */ }
1977 else
1978 {
1979 RTTESTI_CHECK_RC_RET(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS, rcCheck);
1980 *pcSeeks += 1;
1981 *poffActual = 0;
1982 }
1983
1984 size_t cbActuallyWritten = 0;
1985 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBlock, cbBlock, &cbActuallyWritten), VINF_SUCCESS, rcCheck);
1986 if (cbActuallyWritten == cbBlock)
1987 {
1988 *poffActual += cbActuallyWritten;
1989 return VINF_SUCCESS;
1990 }
1991 RTTestIFailed("RTFileWrite at %#RX64 returned just %#x bytes, expected %#x", *poffActual, cbActuallyWritten, cbBlock);
1992 *poffActual += cbActuallyWritten;
1993 return VERR_WRITE_ERROR;
1994}
1995
1996
1997void fsPerfIoWriteBlockSize(RTFILE hFile1, uint64_t cbFile, uint32_t cbBlock)
1998{
1999 RTTestISubF("IO - Sequential write %RU32", cbBlock);
2000
2001 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBlock);
2002 if (pbBuf)
2003 {
2004 memset(pbBuf, 0xf7, cbBlock);
2005 PROFILE_IO_FN("RTFileWrite", fsPerfIoWriteWorker(hFile1, cbFile, cbBlock, pbBuf, &offActual, &cSeeks));
2006 RTMemPageFree(pbBuf, cbBlock);
2007 }
2008 else
2009 RTTestSkipped(g_hTest, "insufficient (virtual) memory available");
2010}
2011
2012
2013void fsPerfWrite(RTFILE hFile1, RTFILE hFileNoCache, RTFILE hFileWriteThru, uint64_t cbFile)
2014{
2015 RTTestISubF("IO - RTFileWrite");
2016
2017 /*
2018 * Allocate a big buffer we can play around with. Min size is 1MB.
2019 */
2020 size_t cbBuf = cbFile < _64M ? (size_t)cbFile : _64M;
2021 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
2022 while (!pbBuf)
2023 {
2024 cbBuf /= 2;
2025 RTTESTI_CHECK_RETV(cbBuf >= _1M);
2026 pbBuf = (uint8_t *)RTMemPageAlloc(_32M);
2027 }
2028
2029 /*
2030 * Start at the beginning and write out the full buffer in random small chunks, thereby
2031 * checking that unaligned buffer addresses, size and file offsets work fine.
2032 */
2033 struct
2034 {
2035 uint64_t offFile;
2036 uint32_t cbMax;
2037 } aRuns[] = { { 0, 127 }, { cbFile - cbBuf, UINT32_MAX }, { 0, UINT32_MAX -1 }};
2038 uint8_t bFiller = 0x88;
2039 for (uint32_t i = 0; i < RT_ELEMENTS(aRuns); i++, bFiller)
2040 {
2041 fsPerfFillWriteBuf(aRuns[i].offFile, pbBuf, cbBuf, bFiller);
2042 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
2043
2044 RTTESTI_CHECK_RC(RTFileSeek(hFile1, aRuns[i].offFile, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2045 for (size_t offBuf = 0; offBuf < cbBuf; )
2046 {
2047 uint32_t const cbLeft = (uint32_t)(cbBuf - offBuf);
2048 uint32_t const cbToWrite = aRuns[i].cbMax < UINT32_MAX / 2 ? RTRandU32Ex(1, RT_MIN(aRuns[i].cbMax, cbLeft))
2049 : aRuns[i].cbMax == UINT32_MAX ? RTRandU32Ex(RT_MAX(cbLeft / 4, 1), cbLeft)
2050 : RTRandU32Ex(cbLeft >= _8K ? _8K : 1, RT_MIN(_1M, cbLeft));
2051 size_t cbActual = 0;
2052 RTTESTI_CHECK_RC(RTFileWrite(hFile1, &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
2053 if (cbActual == cbToWrite)
2054 offBuf += cbActual;
2055 else
2056 {
2057 RTTestIFailed("Attempting to write %#x bytes at %#zx (%#x left), only got %#x written!\n",
2058 cbToWrite, offBuf, cbLeft, cbActual);
2059 if (cbActual)
2060 offBuf += cbActual;
2061 else
2062 pbBuf[offBuf++] = 0x11;
2063 }
2064 }
2065
2066 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, aRuns[i].offFile, pbBuf, cbBuf, NULL), VINF_SUCCESS);
2067 fsPerfCheckReadBuf(__LINE__, aRuns[i].offFile, pbBuf, cbBuf, bFiller);
2068 }
2069
2070
2071 /*
2072 * Do uncached and write-thru accesses, must be page aligned.
2073 */
2074 RTFILE ahFiles[2] = { hFileWriteThru, hFileNoCache };
2075 for (unsigned iFile = 0; iFile < RT_ELEMENTS(ahFiles); iFile++, bFiller++)
2076 {
2077 if (g_fIgnoreNoCache && ahFiles[iFile] == NIL_RTFILE)
2078 continue;
2079
2080 fsPerfFillWriteBuf(0, pbBuf, cbBuf, bFiller);
2081 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
2082 RTTESTI_CHECK_RC(RTFileSeek(ahFiles[iFile], 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2083
2084 uint32_t cbPage = PAGE_SIZE;
2085 for (size_t offBuf = 0; offBuf < cbBuf; )
2086 {
2087 uint32_t const cPagesLeft = (uint32_t)((cbBuf - offBuf) / cbPage);
2088 uint32_t const cPagesToWrite = RTRandU32Ex(1, cPagesLeft);
2089 size_t const cbToWrite = cPagesToWrite * (size_t)cbPage;
2090 size_t cbActual = 0;
2091 RTTESTI_CHECK_RC(RTFileWrite(ahFiles[iFile], &pbBuf[offBuf], cbToWrite, &cbActual), VINF_SUCCESS);
2092 if (cbActual == cbToWrite)
2093 {
2094 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, offBuf, pbBuf, cbToWrite, NULL), VINF_SUCCESS);
2095 fsPerfCheckReadBuf(__LINE__, offBuf, pbBuf, cbToWrite, bFiller);
2096 offBuf += cbActual;
2097 }
2098 else
2099 {
2100 RTTestIFailed("Attempting to read %#zx bytes at %#zx, only got %#x written!\n", cbToWrite, offBuf, cbActual);
2101 if (cbActual)
2102 offBuf += cbActual;
2103 else
2104 {
2105 memset(&pbBuf[offBuf], 0x11, cbPage);
2106 offBuf += cbPage;
2107 }
2108 }
2109 }
2110
2111 RTTESTI_CHECK_RC(RTFileReadAt(ahFiles[iFile], 0, pbBuf, cbBuf, NULL), VINF_SUCCESS);
2112 fsPerfCheckReadBuf(__LINE__, 0, pbBuf, cbBuf, bFiller);
2113 }
2114
2115 /*
2116 * Check the behavior of writing zero bytes to the file _4K from the end
2117 * using native API. In the olden days zero sized write have been known
2118 * to be used to truncate a file.
2119 */
2120 RTTESTI_CHECK_RC(RTFileSeek(hFile1, -_4K, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
2121#ifdef RT_OS_WINDOWS
2122 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
2123 NTSTATUS rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, 0, NULL, NULL);
2124 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
2125 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
2126 RTTESTI_CHECK(Ios.Information == 0);
2127#else
2128 ssize_t cbWritten = write((int)RTFileToNative(hFile1), pbBuf, 0);
2129 RTTESTI_CHECK(cbWritten == 0);
2130#endif
2131 RTTESTI_CHECK_RC(RTFileRead(hFile1, pbBuf, _4K, NULL), VINF_SUCCESS);
2132 fsPerfCheckReadBuf(__LINE__, cbFile - _4K, pbBuf, _4K, pbBuf[0x8]);
2133
2134 /*
2135 * Other OS specific stuff.
2136 */
2137#ifdef RT_OS_WINDOWS
2138 /* Check that reading at an offset modifies the position: */
2139 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, cbFile / 2, pbBuf, _4K, NULL), VINF_SUCCESS);
2140 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_END, NULL), VINF_SUCCESS);
2141 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile);
2142
2143 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
2144 LARGE_INTEGER offNt;
2145 offNt.QuadPart = cbFile / 2;
2146 rcNt = NtWriteFile((HANDLE)RTFileToNative(hFile1), NULL, NULL, NULL, &Ios, pbBuf, _4K, &offNt, NULL);
2147 RTTESTI_CHECK_MSG(rcNt == STATUS_SUCCESS, ("rcNt=%#x", rcNt));
2148 RTTESTI_CHECK(Ios.Status == STATUS_SUCCESS);
2149 RTTESTI_CHECK(Ios.Information == _4K);
2150 RTTESTI_CHECK(RTFileTell(hFile1) == cbFile / 2 + _4K);
2151#endif
2152
2153 RTMemPageFree(pbBuf, cbBuf);
2154}
2155
2156
2157/**
2158 * Worker for testing RTFileFlush.
2159 */
2160DECL_FORCE_INLINE(int) fsPerfFSyncWorker(RTFILE hFile1, uint64_t cbFile, uint8_t *pbBuf, size_t cbBuf, uint64_t *poffFile)
2161{
2162 if (*poffFile + cbBuf <= cbFile)
2163 { /* likely */ }
2164 else
2165 {
2166 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2167 *poffFile = 0;
2168 }
2169
2170 RTTESTI_CHECK_RC_RET(RTFileWrite(hFile1, pbBuf, cbBuf, NULL), VINF_SUCCESS, rcCheck);
2171 RTTESTI_CHECK_RC_RET(RTFileFlush(hFile1), VINF_SUCCESS, rcCheck);
2172
2173 *poffFile += cbBuf;
2174 return VINF_SUCCESS;
2175}
2176
2177
2178void fsPerfFSync(RTFILE hFile1, uint64_t cbFile)
2179{
2180 RTTestISub("fsync");
2181
2182 RTTESTI_CHECK_RC(RTFileFlush(hFile1), VINF_SUCCESS);
2183
2184 PROFILE_FN(RTFileFlush(hFile1), g_nsTestRun, "RTFileFlush");
2185
2186 size_t cbBuf = PAGE_SIZE;
2187 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(cbBuf);
2188 RTTESTI_CHECK_RETV(pbBuf != NULL);
2189 memset(pbBuf, 0xf4, cbBuf);
2190
2191 RTTESTI_CHECK_RC(RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2192 uint64_t offFile = 0;
2193 PROFILE_FN(fsPerfFSyncWorker(hFile1, cbFile, pbBuf, cbBuf, &offFile), g_nsTestRun, "RTFileWrite[Page]/RTFileFlush");
2194
2195 RTMemPageFree(pbBuf, cbBuf);
2196}
2197
2198
2199#ifndef RT_OS_OS2
2200/**
2201 * Worker for profiling msync.
2202 */
2203DECL_FORCE_INLINE(int) fsPerfMSyncWorker(uint8_t *pbMapping, size_t offMapping, size_t cbFlush, size_t *pcbFlushed)
2204{
2205 uint8_t *pbCur = &pbMapping[offMapping];
2206 for (size_t offFlush = 0; offFlush < cbFlush; offFlush += PAGE_SIZE)
2207 *(size_t volatile *)&pbCur[offFlush + 8] = cbFlush;
2208# ifdef RT_OS_WINDOWS
2209 RTTESTI_CHECK(FlushViewOfFile(pbCur, cbFlush));
2210# else
2211 RTTESTI_CHECK(msync(pbCur, cbFlush, MS_SYNC) == 0);
2212# endif
2213 if (*pcbFlushed < offMapping + cbFlush)
2214 *pcbFlushed = offMapping + cbFlush;
2215 return VINF_SUCCESS;
2216}
2217#endif /* !RT_OS_OS2 */
2218
2219
2220void fsPerfMMap(RTFILE hFile1, RTFILE hFileNoCache, uint64_t cbFile)
2221{
2222 RTTestISub("mmap");
2223#if !defined(RT_OS_OS2)
2224 static const char * const s_apszStates[] = { "readonly", "writecopy", "readwrite" };
2225 enum { kMMap_ReadOnly = 0, kMMap_WriteCopy, kMMap_ReadWrite, kMMap_End };
2226 for (int enmState = kMMap_ReadOnly; enmState < kMMap_End; enmState++)
2227 {
2228 /*
2229 * Do the mapping.
2230 */
2231 size_t cbMapping = (size_t)cbFile;
2232 if (cbMapping != cbFile)
2233 cbMapping = _256M;
2234 uint8_t *pbMapping;
2235
2236# ifdef RT_OS_WINDOWS
2237 HANDLE hSection;
2238 pbMapping = NULL;
2239 for (;; cbMapping /= 2)
2240 {
2241 hSection = CreateFileMapping((HANDLE)RTFileToNative(hFile1), NULL,
2242 enmState == kMMap_ReadOnly ? PAGE_READONLY
2243 : enmState == kMMap_WriteCopy ? PAGE_WRITECOPY : PAGE_READWRITE,
2244 (uint32_t)((uint64_t)cbMapping >> 32), (uint32_t)cbMapping, NULL);
2245 DWORD dwErr1 = GetLastError();
2246 DWORD dwErr2 = 0;
2247 if (hSection != NULL)
2248 {
2249 pbMapping = (uint8_t *)MapViewOfFile(hSection,
2250 enmState == kMMap_ReadOnly ? FILE_MAP_READ
2251 : enmState == kMMap_WriteCopy ? FILE_MAP_COPY
2252 : FILE_MAP_WRITE,
2253 0, 0, cbMapping);
2254 if (pbMapping)
2255 break;
2256 dwErr2 = GetLastError();
2257 CloseHandle(hSection);
2258 }
2259 if (cbMapping <= _2M)
2260 {
2261 RTTestIFailed("%u/%s: CreateFileMapping or MapViewOfFile failed: %u, %u",
2262 enmState, s_apszStates[enmState], dwErr1, dwErr2);
2263 break;
2264 }
2265 }
2266# else
2267 for (;; cbMapping /= 2)
2268 {
2269 pbMapping = (uint8_t *)mmap(NULL, cbMapping,
2270 enmState == kMMap_ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE,
2271 enmState == kMMap_WriteCopy ? MAP_PRIVATE : MAP_SHARED,
2272 (int)RTFileToNative(hFile1), 0);
2273 if ((void *)pbMapping != MAP_FAILED)
2274 break;
2275 if (cbMapping <= _2M)
2276 {
2277 RTTestIFailed("%u/%s: mmap failed: %s (%u)", enmState, s_apszStates[enmState], strerror(errno), errno);
2278 break;
2279 }
2280 }
2281# endif
2282 if (cbMapping <= _2M)
2283 continue;
2284
2285 /*
2286 * Time page-ins just for fun.
2287 */
2288 size_t const cPages = cbMapping >> PAGE_SHIFT;
2289 size_t uDummy = 0;
2290 uint64_t ns = RTTimeNanoTS();
2291 for (size_t iPage = 0; iPage < cPages; iPage++)
2292 uDummy += ASMAtomicReadU8(&pbMapping[iPage << PAGE_SHIFT]);
2293 ns = RTTimeNanoTS() - ns;
2294 RTTestIValueF(ns / cPages, RTTESTUNIT_NS_PER_OCCURRENCE, "page-in %s", s_apszStates[enmState]);
2295
2296 /* Check the content. */
2297 fsPerfCheckReadBuf(__LINE__, 0, pbMapping, cbMapping);
2298
2299 if (enmState != kMMap_ReadOnly)
2300 {
2301 /* Write stuff to the first two megabytes. In the COW case, we'll detect
2302 corruption of shared data during content checking of the RW iterations. */
2303 fsPerfFillWriteBuf(0, pbMapping, _2M, 0xf7);
2304 if (enmState == kMMap_ReadWrite)
2305 {
2306 /* For RW we can try read back from the file handle and check if we get
2307 a match there first. */
2308 uint8_t abBuf[_4K];
2309 for (uint32_t off = 0; off < _2M; off += sizeof(abBuf))
2310 {
2311 RTTESTI_CHECK_RC(RTFileReadAt(hFile1, off, abBuf, sizeof(abBuf), NULL), VINF_SUCCESS);
2312 fsPerfCheckReadBuf(__LINE__, off, abBuf, sizeof(abBuf), 0xf7);
2313 }
2314# ifdef RT_OS_WINDOWS
2315 RTTESTI_CHECK(FlushViewOfFile(pbMapping, _2M));
2316# else
2317 RTTESTI_CHECK(msync(pbMapping, _2M, MS_SYNC) == 0);
2318# endif
2319
2320 /*
2321 * Time modifying and flushing a few different number of pages.
2322 */
2323 static size_t const s_acbFlush[] = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 3, PAGE_SIZE * 8, PAGE_SIZE * 16, _2M };
2324 for (unsigned iFlushSize = 0 ; iFlushSize < RT_ELEMENTS(s_acbFlush); iFlushSize++)
2325 {
2326 size_t const cbFlush = s_acbFlush[iFlushSize];
2327 if (cbFlush > cbMapping)
2328 continue;
2329
2330 char szDesc[80];
2331 RTStrPrintf(szDesc, sizeof(szDesc), "touch/flush/%zu", cbFlush);
2332 size_t const cFlushes = cbMapping / cbFlush;
2333 size_t const cbMappingUsed = cFlushes * cbFlush;
2334 size_t cbFlushed = 0;
2335 PROFILE_FN(fsPerfMSyncWorker(pbMapping, (iIteration * cbFlush) % cbMappingUsed, cbFlush, &cbFlushed),
2336 g_nsTestRun, szDesc);
2337
2338 /*
2339 * Check that all the changes made it thru to the file:
2340 */
2341 if (!g_fIgnoreNoCache || hFileNoCache != NIL_RTFILE)
2342 {
2343 size_t cbBuf = _2M;
2344 uint8_t *pbBuf = (uint8_t *)RTMemPageAlloc(_2M);
2345 if (!pbBuf)
2346 {
2347 cbBuf = _4K;
2348 pbBuf = (uint8_t *)RTMemPageAlloc(_2M);
2349 }
2350 if (pbBuf)
2351 {
2352 RTTESTI_CHECK_RC(RTFileSeek(hFileNoCache, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
2353 size_t const cbToCheck = RT_MIN(cFlushes * cbFlush, cbFlushed);
2354 unsigned cErrors = 0;
2355 for (size_t offBuf = 0; cErrors < 32 && offBuf < cbToCheck; offBuf += cbBuf)
2356 {
2357 size_t cbToRead = RT_MIN(cbBuf, cbToCheck - offBuf);
2358 RTTESTI_CHECK_RC(RTFileRead(hFileNoCache, pbBuf, cbToRead, NULL), VINF_SUCCESS);
2359
2360 for (size_t offFlush = 0; offFlush < cbToRead; offFlush += PAGE_SIZE)
2361 if (*(size_t volatile *)&pbBuf[offFlush + 8] != cbFlush)
2362 {
2363 RTTestIFailed("Flush issue at offset #%zx: %#zx, expected %#zx (cbFlush=%#zx)",
2364 offBuf, *(size_t volatile *)&pbBuf[offFlush + 8], cbFlush, cbFlush);
2365 if (++cErrors > 32)
2366 break;
2367 }
2368 }
2369 RTMemPageFree(pbBuf, cbBuf);
2370 }
2371 }
2372 }
2373 }
2374 }
2375
2376 /*
2377 * Unmap it.
2378 */
2379# ifdef RT_OS_WINDOWS
2380 RTTESTI_CHECK(UnmapViewOfFile(pbMapping));
2381 RTTESTI_CHECK(CloseHandle(hSection));
2382# else
2383 RTTESTI_CHECK(munmap(pbMapping, cbMapping) == 0);
2384# endif
2385 }
2386
2387 RT_NOREF(hFileNoCache);
2388#else
2389 RTTestSkipped(g_hTest, "not supported/implemented");
2390 RT_NOREF(hFile1, hFileNoCache, cbFile);
2391#endif
2392}
2393
2394
2395/**
2396 * This does the read, write and seek tests.
2397 */
2398void fsPerfIo(void)
2399{
2400 RTTestISub("I/O");
2401
2402 /*
2403 * Determin the size of the test file.
2404 */
2405 g_szDir[g_cchDir] = '\0';
2406 RTFOFF cbFree = 0;
2407 RTTESTI_CHECK_RC_RETV(RTFsQuerySizes(g_szDir, NULL, &cbFree, NULL, NULL), VINF_SUCCESS);
2408 uint64_t cbFile = g_cbIoFile;
2409 if (cbFile + _16M < (uint64_t)cbFree)
2410 cbFile = RT_ALIGN_64(cbFile, _64K);
2411 else
2412 {
2413 if (cbFree < _32M)
2414 {
2415 RTTestSkipped(g_hTest, "Insufficent free space: %'RU64 bytes, requires >= 32MB", cbFree);
2416 return;
2417 }
2418 cbFile = cbFree - (cbFree > _128M ? _64M : _16M);
2419 cbFile = RT_ALIGN_64(cbFile, _64K);
2420 RTTestIPrintf(RTTESTLVL_ALWAYS, "Adjusted file size to %'RU64 bytes, due to %'RU64 bytes free.\n", cbFile, cbFree);
2421 }
2422 if (cbFile < _64K)
2423 {
2424 RTTestSkipped(g_hTest, "Specified test file size too small: %'RU64 bytes, requires >= 64KB", cbFile);
2425 return;
2426 }
2427
2428 /*
2429 * Create a cbFile sized test file.
2430 */
2431 RTFILE hFile1;
2432 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFile1, InDir(RT_STR_TUPLE("file21")),
2433 RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE), VINF_SUCCESS);
2434 RTFILE hFileNoCache;
2435 if (!g_fIgnoreNoCache)
2436 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileNoCache, g_szDir,
2437 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE),
2438 VINF_SUCCESS);
2439 else
2440 {
2441 int rc = RTFileOpen(&hFileNoCache, g_szDir, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_NO_CACHE);
2442 if (RT_FAILURE(rc))
2443 {
2444 RTTestIPrintf(RTTESTLVL_ALWAYS, "Unable to open I/O file with non-cache flag (%Rrc), skipping related tests.\n", rc);
2445 hFileNoCache = NIL_RTFILE;
2446 }
2447 }
2448 RTFILE hFileWriteThru;
2449 RTTESTI_CHECK_RC_RETV(RTFileOpen(&hFileWriteThru, g_szDir,
2450 RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE | RTFILE_O_WRITE_THROUGH),
2451 VINF_SUCCESS);
2452
2453 uint8_t *pbFree = NULL;
2454 int rc = fsPerfIoPrepFile(hFile1, cbFile, &pbFree);
2455 RTMemFree(pbFree);
2456 if (RT_SUCCESS(rc))
2457 {
2458 /*
2459 * Do the testing & profiling.
2460 */
2461 if (g_fSeek)
2462 fsPerfIoSeek(hFile1, cbFile);
2463
2464 if (g_fReadTests)
2465 fsPerfRead(hFile1, hFileNoCache, cbFile);
2466 if (g_fReadPerf)
2467 for (unsigned i = 0; i < g_cIoBlocks; i++)
2468 fsPerfIoReadBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
2469
2470 if (g_fMMap)
2471 fsPerfMMap(hFile1, hFileNoCache, cbFile);
2472
2473 /* This is destructive to the file content. */
2474 if (g_fWriteTests)
2475 fsPerfWrite(hFile1, hFileNoCache, hFileWriteThru, cbFile);
2476 if (g_fWritePerf)
2477 for (unsigned i = 0; i < g_cIoBlocks; i++)
2478 fsPerfIoWriteBlockSize(hFile1, cbFile, g_acbIoBlocks[i]);
2479
2480 if (g_fFSync)
2481 fsPerfFSync(hFile1, cbFile);
2482 }
2483
2484 RTTESTI_CHECK_RC(RTFileSetSize(hFile1, 0), VINF_SUCCESS);
2485 RTTESTI_CHECK_RC(RTFileClose(hFile1), VINF_SUCCESS);
2486 if (hFileNoCache != NIL_RTFILE || !g_fIgnoreNoCache)
2487 RTTESTI_CHECK_RC(RTFileClose(hFileNoCache), VINF_SUCCESS);
2488 RTTESTI_CHECK_RC(RTFileClose(hFileWriteThru), VINF_SUCCESS);
2489 RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
2490}
2491
2492
2493/**
2494 * Display the usage to @a pStrm.
2495 */
2496static void Usage(PRTSTREAM pStrm)
2497{
2498 char szExec[RTPATH_MAX];
2499 RTStrmPrintf(pStrm, "usage: %s <-d <testdir>> [options]\n",
2500 RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
2501 RTStrmPrintf(pStrm, "\n");
2502 RTStrmPrintf(pStrm, "options: \n");
2503
2504 for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
2505 {
2506 char szHelp[80];
2507 const char *pszHelp;
2508 switch (g_aCmdOptions[i].iShort)
2509 {
2510 case 'd': pszHelp = "The directory to use for testing. default: CWD/fstestdir"; break;
2511 case 'e': pszHelp = "Enables all tests. default: -e"; break;
2512 case 'z': pszHelp = "Disables all tests. default: -e"; break;
2513 case 's': pszHelp = "Set benchmark duration in seconds. default: 10 sec"; break;
2514 case 'm': pszHelp = "Set benchmark duration in milliseconds. default: 10000 ms"; break;
2515 case 'v': pszHelp = "More verbose execution."; break;
2516 case 'q': pszHelp = "Quiet execution."; break;
2517 case 'h': pszHelp = "Displays this help and exit"; break;
2518 case 'V': pszHelp = "Displays the program revision"; break;
2519 case kCmdOpt_ShowDuration: pszHelp = "Show duration of profile runs. default: --no-show-duration"; break;
2520 case kCmdOpt_NoShowDuration: pszHelp = "Hide duration of profile runs. default: --no-show-duration"; break;
2521 case kCmdOpt_ShowIterations: pszHelp = "Show iteration count for profile runs. default: --no-show-iterations"; break;
2522 case kCmdOpt_NoShowIterations: pszHelp = "Hide iteration count for profile runs. default: --no-show-iterations"; break;
2523 case kCmdOpt_ManyFiles: pszHelp = "Count of files in big test dir. default: --many-files 10000"; break;
2524 case kCmdOpt_NoManyFiles: pszHelp = "Skip big test dir with many files. default: --many-files 10000"; break;
2525 case kCmdOpt_ManyTreeFilesPerDir: pszHelp = "Count of files per directory in test tree. default: 640"; break;
2526 case kCmdOpt_ManyTreeSubdirsPerDir: pszHelp = "Count of subdirs per directory in test tree. default: 16"; break;
2527 case kCmdOpt_ManyTreeDepth: pszHelp = "Depth of test tree (not counting root). default: 1"; break;
2528 case kCmdOpt_IgnoreNoCache: pszHelp = "Ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
2529 case kCmdOpt_NoIgnoreNoCache: pszHelp = "Do not ignore error wrt no-cache handle. default: --no-ignore-no-cache"; break;
2530 case kCmdOpt_IoFileSize: pszHelp = "Size of file used for I/O tests. default: 512 MB"; break;
2531 case kCmdOpt_SetBlockSize: pszHelp = "Sets single I/O block size (in bytes)."; break;
2532 case kCmdOpt_AddBlockSize: pszHelp = "Adds an I/O block size (in bytes)."; break;
2533 default:
2534 if (g_aCmdOptions[i].iShort >= kCmdOpt_First)
2535 {
2536 if (RTStrStartsWith(g_aCmdOptions[i].pszLong, "--no-"))
2537 RTStrPrintf(szHelp, sizeof(szHelp), "Disables the '%s' test.", g_aCmdOptions[i].pszLong + 5);
2538 else
2539 RTStrPrintf(szHelp, sizeof(szHelp), "Enables the '%s' test.", g_aCmdOptions[i].pszLong + 2);
2540 pszHelp = szHelp;
2541 }
2542 else
2543 pszHelp = "Option undocumented";
2544 break;
2545 }
2546 if ((unsigned)g_aCmdOptions[i].iShort < 127U)
2547 {
2548 char szOpt[64];
2549 RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
2550 RTStrmPrintf(pStrm, " %-19s %s\n", szOpt, pszHelp);
2551 }
2552 else
2553 RTStrmPrintf(pStrm, " %-19s %s\n", g_aCmdOptions[i].pszLong, pszHelp);
2554 }
2555}
2556
2557
2558static uint32_t fsPerfCalcManyTreeFiles(void)
2559{
2560 uint32_t cDirs = 1;
2561 for (uint32_t i = 0, cDirsAtLevel = 1; i < g_cManyTreeDepth; i++)
2562 {
2563 cDirs += cDirsAtLevel * g_cManyTreeSubdirsPerDir;
2564 cDirsAtLevel *= g_cManyTreeSubdirsPerDir;
2565 }
2566 return g_cManyTreeFilesPerDir * cDirs;
2567}
2568
2569
2570int main(int argc, char *argv[])
2571{
2572 /*
2573 * Init IPRT and globals.
2574 */
2575 int rc = RTTestInitAndCreate("FsPerf", &g_hTest);
2576 if (rc)
2577 return rc;
2578 RTListInit(&g_ManyTreeHead);
2579
2580 /*
2581 * Default values.
2582 */
2583 rc = RTPathGetCurrent(g_szDir, sizeof(g_szDir) / 2);
2584 if (RT_SUCCESS(rc))
2585 rc = RTPathAppend(g_szDir, sizeof(g_szDir) / 2, "fstestdir-");
2586 if (RT_SUCCESS(rc))
2587 {
2588 g_cchDir = strlen(g_szDir);
2589 g_cchDir += RTStrPrintf(&g_szDir[g_cchDir], sizeof(g_szDir) - g_cchDir, "%u" RTPATH_SLASH_STR, RTProcSelf());
2590 }
2591 else
2592 {
2593 RTTestFailed(g_hTest, "RTPathGetCurrent (or RTPathAppend) failed: %Rrc\n", rc);
2594 return RTTestSummaryAndDestroy(g_hTest);
2595 }
2596
2597 RTGETOPTUNION ValueUnion;
2598 RTGETOPTSTATE GetState;
2599 RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
2600 while ((rc = RTGetOpt(&GetState, &ValueUnion)) != 0)
2601 {
2602 switch (rc)
2603 {
2604 case 'd':
2605 rc = RTPathAbs(ValueUnion.psz, g_szDir, sizeof(g_szDir) / 2);
2606 if (RT_SUCCESS(rc))
2607 {
2608 RTPathEnsureTrailingSeparator(g_szDir, sizeof(g_szDir));
2609 g_cchDir = strlen(g_szDir);
2610 break;
2611 }
2612 RTTestFailed(g_hTest, "RTPathAbs(%s) failed: %Rrc\n", ValueUnion.psz, rc);
2613 return RTTestSummaryAndDestroy(g_hTest);
2614
2615 case 's':
2616 if (ValueUnion.u32 == 0)
2617 g_nsTestRun = RT_NS_1SEC_64 * 10;
2618 else
2619 g_nsTestRun = ValueUnion.u32 * RT_NS_1SEC_64;
2620 break;
2621
2622 case 'm':
2623 if (ValueUnion.u64 == 0)
2624 g_nsTestRun = RT_NS_1SEC_64 * 10;
2625 else
2626 g_nsTestRun = ValueUnion.u64 * RT_NS_1MS;
2627 break;
2628
2629 case 'e':
2630 g_fManyFiles = true;
2631 g_fOpen = true;
2632 g_fFStat = true;
2633 g_fFChMod = true;
2634 g_fFUtimes = true;
2635 g_fStat = true;
2636 g_fChMod = true;
2637 g_fUtimes = true;
2638 g_fRename = true;
2639 g_fDirEnum = true;
2640 g_fMkRmDir = true;
2641 g_fStatVfs = true;
2642 g_fRm = true;
2643 g_fChSize = true;
2644 g_fReadTests = true;
2645 g_fReadPerf = true;
2646 g_fWriteTests= true;
2647 g_fWritePerf = true;
2648 g_fSeek = true;
2649 g_fFSync = true;
2650 g_fMMap = true;
2651 break;
2652
2653 case 'z':
2654 g_fManyFiles = false;
2655 g_fOpen = false;
2656 g_fFStat = false;
2657 g_fFChMod = false;
2658 g_fFUtimes = false;
2659 g_fStat = false;
2660 g_fChMod = false;
2661 g_fUtimes = false;
2662 g_fRename = false;
2663 g_fDirEnum = false;
2664 g_fMkRmDir = false;
2665 g_fStatVfs = false;
2666 g_fRm = false;
2667 g_fChSize = false;
2668 g_fReadTests = false;
2669 g_fReadPerf = false;
2670 g_fWriteTests= false;
2671 g_fWritePerf = false;
2672 g_fSeek = false;
2673 g_fFSync = false;
2674 g_fMMap = false;
2675 break;
2676
2677#define CASE_OPT(a_Stem) \
2678 case RT_CONCAT(kCmdOpt_,a_Stem): RT_CONCAT(g_f,a_Stem) = true; break; \
2679 case RT_CONCAT(kCmdOpt_No,a_Stem): RT_CONCAT(g_f,a_Stem) = false; break
2680 CASE_OPT(Open);
2681 CASE_OPT(FStat);
2682 CASE_OPT(FChMod);
2683 CASE_OPT(FUtimes);
2684 CASE_OPT(Stat);
2685 CASE_OPT(ChMod);
2686 CASE_OPT(Utimes);
2687 CASE_OPT(Rename);
2688 CASE_OPT(DirEnum);
2689 CASE_OPT(MkRmDir);
2690 CASE_OPT(StatVfs);
2691 CASE_OPT(Rm);
2692 CASE_OPT(ChSize);
2693 CASE_OPT(ReadTests);
2694 CASE_OPT(ReadPerf);
2695 CASE_OPT(WriteTests);
2696 CASE_OPT(WritePerf);
2697 CASE_OPT(Seek);
2698 CASE_OPT(FSync);
2699 CASE_OPT(MMap);
2700 CASE_OPT(IgnoreNoCache);
2701
2702 CASE_OPT(ShowDuration);
2703 CASE_OPT(ShowIterations);
2704#undef CASE_OPT
2705
2706 case kCmdOpt_ManyFiles:
2707 g_fManyFiles = ValueUnion.u32 > 0;
2708 g_cManyFiles = ValueUnion.u32;
2709 break;
2710
2711 case kCmdOpt_NoManyFiles:
2712 g_fManyFiles = false;
2713 break;
2714
2715 case kCmdOpt_ManyTreeFilesPerDir:
2716 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= _64M)
2717 {
2718 g_cManyTreeFilesPerDir = ValueUnion.u32;
2719 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
2720 break;
2721 }
2722 RTTestFailed(g_hTest, "Out of range --files-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
2723 return RTTestSummaryAndDestroy(g_hTest);
2724
2725 case kCmdOpt_ManyTreeSubdirsPerDir:
2726 if (ValueUnion.u32 > 0 && ValueUnion.u32 <= 1024)
2727 {
2728 g_cManyTreeSubdirsPerDir = ValueUnion.u32;
2729 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
2730 break;
2731 }
2732 RTTestFailed(g_hTest, "Out of range --subdirs-per-dir value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
2733 return RTTestSummaryAndDestroy(g_hTest);
2734
2735 case kCmdOpt_ManyTreeDepth:
2736 if (ValueUnion.u32 <= 8)
2737 {
2738 g_cManyTreeDepth = ValueUnion.u32;
2739 g_cManyTreeFiles = fsPerfCalcManyTreeFiles();
2740 break;
2741 }
2742 RTTestFailed(g_hTest, "Out of range --tree-depth value: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
2743 return RTTestSummaryAndDestroy(g_hTest);
2744
2745 case kCmdOpt_IoFileSize:
2746 if (ValueUnion.u64 == 0)
2747 g_cbIoFile = _512M;
2748 else
2749 g_cbIoFile = ValueUnion.u64;
2750 break;
2751
2752 case kCmdOpt_SetBlockSize:
2753 if (ValueUnion.u32 > 0)
2754 {
2755 g_cIoBlocks = 1;
2756 g_acbIoBlocks[0] = ValueUnion.u32;
2757 }
2758 else
2759 {
2760 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
2761 return RTTestSummaryAndDestroy(g_hTest);
2762 }
2763 break;
2764
2765 case kCmdOpt_AddBlockSize:
2766 if (g_cIoBlocks >= RT_ELEMENTS(g_acbIoBlocks))
2767 RTTestFailed(g_hTest, "Too many I/O block sizes: max %u\n", RT_ELEMENTS(g_acbIoBlocks));
2768 else if (ValueUnion.u32 == 0)
2769 RTTestFailed(g_hTest, "Invalid I/O block size: %u (%#x)\n", ValueUnion.u32, ValueUnion.u32);
2770 else
2771 {
2772 g_acbIoBlocks[g_cIoBlocks++] = ValueUnion.u32;
2773 break;
2774 }
2775 return RTTestSummaryAndDestroy(g_hTest);
2776
2777 case 'q':
2778 g_uVerbosity = 0;
2779 break;
2780
2781 case 'v':
2782 g_uVerbosity++;
2783 break;
2784
2785 case 'h':
2786 Usage(g_pStdOut);
2787 return RTEXITCODE_SUCCESS;
2788
2789 case 'V':
2790 {
2791 char szRev[] = "$Revision: 77007 $";
2792 szRev[RT_ELEMENTS(szRev) - 2] = '\0';
2793 RTPrintf(RTStrStrip(strchr(szRev, ':') + 1));
2794 return RTEXITCODE_SUCCESS;
2795 }
2796
2797 default:
2798 return RTGetOptPrintError(rc, &ValueUnion);
2799 }
2800 }
2801
2802 /*
2803 * Create the test directory with an 'empty' subdirectory under it,
2804 * execute the tests, and remove directory when done.
2805 */
2806 RTTestBanner(g_hTest);
2807 if (!RTPathExists(g_szDir))
2808 {
2809 /* The base dir: */
2810 rc = RTDirCreate(g_szDir, 0755,
2811 RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET | RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL);
2812 if (RT_SUCCESS(rc))
2813 {
2814 RTTestIPrintf(RTTESTLVL_ALWAYS, "Test dir: %s\n", g_szDir);
2815 rc = fsPrepTestArea();
2816 if (RT_SUCCESS(rc))
2817 {
2818 /* Profile RTTimeNanoTS(). */
2819 fsPerfNanoTS();
2820
2821 /* Do tests: */
2822 if (g_fManyFiles)
2823 fsPerfManyFiles();
2824 if (g_fOpen)
2825 fsPerfOpen();
2826 if (g_fFStat)
2827 fsPerfFStat();
2828 if (g_fFChMod)
2829 fsPerfFChMod();
2830 if (g_fFUtimes)
2831 fsPerfFUtimes();
2832 if (g_fStat)
2833 fsPerfStat();
2834 if (g_fChMod)
2835 fsPerfChmod();
2836 if (g_fUtimes)
2837 fsPerfUtimes();
2838 if (g_fRename)
2839 fsPerfRename();
2840 if (g_fDirEnum)
2841 vsPerfDirEnum();
2842 if (g_fMkRmDir)
2843 fsPerfMkRmDir();
2844 if (g_fStatVfs)
2845 fsPerfStatVfs();
2846 if (g_fRm || g_fManyFiles)
2847 fsPerfRm(); /* deletes manyfiles and manytree */
2848 if (g_fChSize)
2849 fsPerfChSize();
2850 if (g_fReadPerf || g_fReadTests || g_fWritePerf || g_fWriteTests || g_fSeek || g_fFSync || g_fMMap)
2851 fsPerfIo();
2852 }
2853
2854 /* Cleanup: */
2855 g_szDir[g_cchDir] = '\0';
2856 rc = RTDirRemoveRecursive(g_szDir, RTDIRRMREC_F_CONTENT_AND_DIR);
2857 if (RT_FAILURE(rc))
2858 RTTestFailed(g_hTest, "RTDirRemoveRecursive(%s,) -> %Rrc\n", g_szDir, rc);
2859 }
2860 else
2861 RTTestFailed(g_hTest, "RTDirCreate(%s) -> %Rrc\n", g_szDir, rc);
2862 }
2863 else
2864 RTTestFailed(g_hTest, "Test directory already exists: %s\n", g_szDir);
2865
2866 return RTTestSummaryAndDestroy(g_hTest);
2867}
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