VirtualBox

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

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

FsPerf: More command line options for tweaking the test parameters. bugref:9172

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

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