VirtualBox

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

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

FsPerf: Build fix. bugref:9172

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