VirtualBox

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

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

FsPerf: Test readv beyond the end of the file. bugref:9172

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

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