VirtualBox

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

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

FsPerf: Added regular sendfile test (currently linux only). bugref:9172

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