VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/tstVDSnap.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1/* $Id: tstVDSnap.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * Snapshot VBox HDD container test utility.
4 */
5
6/*
7 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include <VBox/vd.h>
29#include <iprt/errcore.h>
30#include <VBox/log.h>
31#include <iprt/asm.h>
32#include <iprt/dir.h>
33#include <iprt/string.h>
34#include <iprt/stream.h>
35#include <iprt/file.h>
36#include <iprt/mem.h>
37#include <iprt/initterm.h>
38#include <iprt/rand.h>
39
40/**
41 * A VD snapshot test.
42 */
43typedef struct VDSNAPTEST
44{
45 /** Backend to use */
46 const char *pcszBackend;
47 /** Base image name */
48 const char *pcszBaseImage;
49 /** Diff image ending */
50 const char *pcszDiffSuff;
51 /** Number of iterations before the test exits */
52 uint32_t cIterations;
53 /** Test pattern size */
54 size_t cbTestPattern;
55 /** Minimum number of disk segments */
56 uint32_t cDiskSegsMin;
57 /** Miaximum number of disk segments */
58 uint32_t cDiskSegsMax;
59 /** Minimum number of diffs needed before a merge
60 * operation can occur */
61 unsigned cDiffsMinBeforeMerge;
62 /** Chance to get create instead of a merge operation */
63 uint32_t uCreateDiffChance;
64 /** Chance to change a segment after a diff was created */
65 uint32_t uChangeSegChance;
66 /** Numer of allocated blocks in the base image in percent */
67 uint32_t uAllocatedBlocks;
68 /** Merge direction */
69 bool fForward;
70} VDSNAPTEST, *PVDSNAPTEST;
71
72/**
73 * Structure defining a disk segment.
74 */
75typedef struct VDDISKSEG
76{
77 /** Start offset in the disk. */
78 uint64_t off;
79 /** Size of the segment. */
80 uint64_t cbSeg;
81 /** Pointer to the start of the data in the test pattern used for the segment. */
82 uint8_t *pbData;
83 /** Pointer to the data for a diff write */
84 uint8_t *pbDataDiff;
85} VDDISKSEG, *PVDDISKSEG;
86
87
88/*********************************************************************************************************************************
89* Global Variables *
90*********************************************************************************************************************************/
91/** The error count. */
92unsigned g_cErrors = 0;
93/** Global RNG state. */
94RTRAND g_hRand;
95
96static DECLCALLBACK(void) tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
97{
98 RT_NOREF1(pvUser);
99 g_cErrors++;
100 RTPrintf("tstVDSnap: Error %Rrc at %s:%u (%s): ", rc, RT_SRC_POS_ARGS);
101 RTPrintfV(pszFormat, va);
102 RTPrintf("\n");
103}
104
105static DECLCALLBACK(int) tstVDMessage(void *pvUser, const char *pszFormat, va_list va)
106{
107 RT_NOREF1(pvUser);
108 RTPrintf("tstVDSnap: ");
109 RTPrintfV(pszFormat, va);
110 return VINF_SUCCESS;
111}
112
113/**
114 * Returns true with the given chance in percent.
115 *
116 * @returns true or false
117 * @param iPercentage The percentage of the chance to return true.
118 */
119static bool tstVDSnapIsTrue(int iPercentage)
120{
121 int uRnd = RTRandAdvU32Ex(g_hRand, 0, 100);
122
123 return (uRnd <= iPercentage); /* This should be enough for our purpose */
124}
125
126static void tstVDSnapSegmentsDice(PVDSNAPTEST pTest, PVDDISKSEG paDiskSeg, uint32_t cDiskSegments,
127 uint8_t *pbTestPattern, size_t cbTestPattern)
128{
129 for (uint32_t i = 0; i < cDiskSegments; i++)
130 {
131 /* Do we want to change the current segment? */
132 if (tstVDSnapIsTrue(pTest->uChangeSegChance))
133 paDiskSeg[i].pbDataDiff = pbTestPattern + RT_ALIGN_64(RTRandAdvU64Ex(g_hRand, 0, cbTestPattern - paDiskSeg[i].cbSeg - 512), 512);
134 }
135}
136
137static int tstVDSnapWrite(PVDISK pVD, PVDDISKSEG paDiskSegments, uint32_t cDiskSegments, uint64_t cbDisk, bool fInit)
138{
139 RT_NOREF1(cbDisk);
140 int rc = VINF_SUCCESS;
141
142 for (uint32_t i = 0; i < cDiskSegments; i++)
143 {
144 if (fInit || paDiskSegments[i].pbDataDiff)
145 {
146 size_t cbWrite = paDiskSegments[i].cbSeg;
147 uint64_t off = paDiskSegments[i].off;
148 uint8_t *pbData = fInit
149 ? paDiskSegments[i].pbData
150 : paDiskSegments[i].pbDataDiff;
151
152 if (pbData)
153 {
154 rc = VDWrite(pVD, off, pbData, cbWrite);
155 if (RT_FAILURE(rc))
156 return rc;
157 }
158 }
159 }
160
161 return rc;
162}
163
164static int tstVDSnapReadVerify(PVDISK pVD, PVDDISKSEG paDiskSegments, uint32_t cDiskSegments, uint64_t cbDisk)
165{
166 RT_NOREF1(cbDisk);
167 int rc = VINF_SUCCESS;
168 uint8_t *pbBuf = (uint8_t *)RTMemAlloc(_1M);
169
170 for (uint32_t i = 0; i < cDiskSegments; i++)
171 {
172 size_t cbRead = paDiskSegments[i].cbSeg;
173 uint64_t off = paDiskSegments[i].off;
174 uint8_t *pbCmp = paDiskSegments[i].pbData;
175
176 Assert(!paDiskSegments[i].pbDataDiff);
177
178 while (cbRead)
179 {
180 size_t cbToRead = RT_MIN(cbRead, _1M);
181
182 rc = VDRead(pVD, off, pbBuf, cbToRead);
183 if (RT_FAILURE(rc))
184 return rc;
185
186 if (pbCmp)
187 {
188 if (memcmp(pbCmp, pbBuf, cbToRead))
189 {
190 for (unsigned iCmp = 0; iCmp < cbToRead; iCmp++)
191 {
192 if (pbCmp[iCmp] != pbBuf[iCmp])
193 {
194 RTPrintf("Unexpected data at %llu expected %#x got %#x\n", off+iCmp, pbCmp[iCmp], pbBuf[iCmp]);
195 break;
196 }
197 }
198 return VERR_INTERNAL_ERROR;
199 }
200 }
201 else
202 {
203 /* Verify that the block is 0 */
204 for (unsigned iCmp = 0; iCmp < cbToRead; iCmp++)
205 {
206 if (pbBuf[iCmp] != 0)
207 {
208 RTPrintf("Zero block contains data at %llu\n", off+iCmp);
209 return VERR_INTERNAL_ERROR;
210 }
211 }
212 }
213
214 cbRead -= cbToRead;
215 off += cbToRead;
216
217 if (pbCmp)
218 pbCmp += cbToRead;
219 }
220 }
221
222 RTMemFree(pbBuf);
223
224 return rc;
225}
226
227static int tstVDOpenCreateWriteMerge(PVDSNAPTEST pTest)
228{
229 int rc;
230 PVDISK pVD = NULL;
231 VDGEOMETRY PCHS = { 0, 0, 0 };
232 VDGEOMETRY LCHS = { 0, 0, 0 };
233 PVDINTERFACE pVDIfs = NULL;
234 VDINTERFACEERROR VDIfError;
235
236 /** Buffer storing the random test pattern. */
237 uint8_t *pbTestPattern = NULL;
238 /** Number of disk segments */
239 uint32_t cDiskSegments;
240 /** Array of disk segments */
241 PVDDISKSEG paDiskSeg = NULL;
242 unsigned cDiffs = 0;
243 unsigned idDiff = 0; /* Diff ID counter for the filename */
244
245 /* Delete all images from a previous run. */
246 RTFileDelete(pTest->pcszBaseImage);
247 for (unsigned i = 0; i < pTest->cIterations; i++)
248 {
249 char *pszDiffFilename = NULL;
250
251 rc = RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", i, pTest->pcszDiffSuff);
252 if (RT_SUCCESS(rc))
253 {
254 if (RTFileExists(pszDiffFilename))
255 RTFileDelete(pszDiffFilename);
256 RTStrFree(pszDiffFilename);
257 }
258 }
259
260 /* Create the virtual disk test data */
261 pbTestPattern = (uint8_t *)RTMemAlloc(pTest->cbTestPattern);
262
263 RTRandAdvBytes(g_hRand, pbTestPattern, pTest->cbTestPattern);
264 cDiskSegments = RTRandAdvU32Ex(g_hRand, pTest->cDiskSegsMin, pTest->cDiskSegsMax);
265
266 uint64_t cbDisk = 0;
267
268 paDiskSeg = (PVDDISKSEG)RTMemAllocZ(cDiskSegments * sizeof(VDDISKSEG));
269 if (!paDiskSeg)
270 {
271 RTPrintf("Failed to allocate memory for random disk segments\n");
272 g_cErrors++;
273 return VERR_NO_MEMORY;
274 }
275
276 for (unsigned i = 0; i < cDiskSegments; i++)
277 {
278 paDiskSeg[i].off = cbDisk;
279 paDiskSeg[i].cbSeg = RT_ALIGN_64(RTRandAdvU64Ex(g_hRand, 512, pTest->cbTestPattern), 512);
280 if (tstVDSnapIsTrue(pTest->uAllocatedBlocks))
281 paDiskSeg[i].pbData = pbTestPattern + RT_ALIGN_64(RTRandAdvU64Ex(g_hRand, 0, pTest->cbTestPattern - paDiskSeg[i].cbSeg - 512), 512);
282 else
283 paDiskSeg[i].pbData = NULL; /* Not allocated initially */
284 cbDisk += paDiskSeg[i].cbSeg;
285 }
286
287 RTPrintf("Disk size is %llu bytes\n", cbDisk);
288
289#define CHECK(str) \
290 do \
291 { \
292 RTPrintf("%s rc=%Rrc\n", str, rc); \
293 if (RT_FAILURE(rc)) \
294 { \
295 if (pbTestPattern) \
296 RTMemFree(pbTestPattern); \
297 if (paDiskSeg) \
298 RTMemFree(paDiskSeg); \
299 VDDestroy(pVD); \
300 g_cErrors++; \
301 return rc; \
302 } \
303 } while (0)
304
305#define CHECK_BREAK(str) \
306 do \
307 { \
308 RTPrintf("%s rc=%Rrc\n", str, rc); \
309 if (RT_FAILURE(rc)) \
310 { \
311 g_cErrors++; \
312 break; \
313 } \
314 } while (0)
315
316 /* Create error interface. */
317 /* Create error interface. */
318 VDIfError.pfnError = tstVDError;
319 VDIfError.pfnMessage = tstVDMessage;
320
321 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
322 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
323 AssertRC(rc);
324
325
326 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
327 CHECK("VDCreate()");
328
329 rc = VDCreateBase(pVD, pTest->pcszBackend, pTest->pcszBaseImage, cbDisk,
330 VD_IMAGE_FLAGS_NONE, "Test image",
331 &PCHS, &LCHS, NULL, VD_OPEN_FLAGS_NORMAL,
332 NULL, NULL);
333 CHECK("VDCreateBase()");
334
335 bool fInit = true;
336 uint32_t cIteration = 0;
337
338 /* Do the real work now */
339 while ( RT_SUCCESS(rc)
340 && cIteration < pTest->cIterations)
341 {
342 /* Write */
343 rc = tstVDSnapWrite(pVD, paDiskSeg, cDiskSegments, cbDisk, fInit);
344 CHECK_BREAK("tstVDSnapWrite()");
345
346 fInit = false;
347
348 /* Write returned, do we want to create a new diff or merge them? */
349 bool fCreate = cDiffs < pTest->cDiffsMinBeforeMerge
350 ? true
351 : tstVDSnapIsTrue(pTest->uCreateDiffChance);
352
353 if (fCreate)
354 {
355 char *pszDiffFilename = NULL;
356
357 RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", idDiff, pTest->pcszDiffSuff);
358 CHECK("RTStrAPrintf()");
359 idDiff++;
360 cDiffs++;
361
362 rc = VDCreateDiff(pVD, pTest->pcszBackend, pszDiffFilename,
363 VD_IMAGE_FLAGS_NONE, "Test diff image", NULL, NULL,
364 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
365 CHECK_BREAK("VDCreateDiff()");
366
367 RTStrFree(pszDiffFilename);
368 VDDumpImages(pVD);
369
370 /* Change data */
371 tstVDSnapSegmentsDice(pTest, paDiskSeg, cDiskSegments, pbTestPattern, pTest->cbTestPattern);
372 }
373 else
374 {
375 uint32_t uStartMerge = RTRandAdvU32Ex(g_hRand, 1, cDiffs - 1);
376 uint32_t uEndMerge = RTRandAdvU32Ex(g_hRand, uStartMerge + 1, cDiffs);
377 RTPrintf("Merging %u diffs from %u to %u...\n",
378 uEndMerge - uStartMerge,
379 uStartMerge,
380 uEndMerge);
381 if (pTest->fForward)
382 rc = VDMerge(pVD, uStartMerge, uEndMerge, NULL);
383 else
384 rc = VDMerge(pVD, uEndMerge, uStartMerge, NULL);
385 CHECK_BREAK("VDMerge()");
386
387 cDiffs -= uEndMerge - uStartMerge;
388
389 VDDumpImages(pVD);
390
391 /* Go through the disk segments and reset pointers. */
392 for (uint32_t i = 0; i < cDiskSegments; i++)
393 {
394 if (paDiskSeg[i].pbDataDiff)
395 {
396 paDiskSeg[i].pbData = paDiskSeg[i].pbDataDiff;
397 paDiskSeg[i].pbDataDiff = NULL;
398 }
399 }
400
401 /* Now compare the result with our test pattern */
402 rc = tstVDSnapReadVerify(pVD, paDiskSeg, cDiskSegments, cbDisk);
403 CHECK_BREAK("tstVDSnapReadVerify()");
404 }
405 cIteration++;
406 }
407
408 VDDumpImages(pVD);
409
410 VDDestroy(pVD);
411 if (paDiskSeg)
412 RTMemFree(paDiskSeg);
413 if (pbTestPattern)
414 RTMemFree(pbTestPattern);
415
416 RTFileDelete(pTest->pcszBaseImage);
417 for (unsigned i = 0; i < idDiff; i++)
418 {
419 char *pszDiffFilename = NULL;
420
421 RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", i, pTest->pcszDiffSuff);
422 RTFileDelete(pszDiffFilename);
423 RTStrFree(pszDiffFilename);
424 }
425#undef CHECK
426 return rc;
427}
428
429int main(int argc, char *argv[])
430{
431 RTR3InitExe(argc, &argv, 0);
432 int rc;
433 VDSNAPTEST Test;
434
435 RTPrintf("tstVDSnap: TESTING...\n");
436
437 rc = RTRandAdvCreateParkMiller(&g_hRand);
438 if (RT_FAILURE(rc))
439 {
440 RTPrintf("tstVDSnap: Creating RNG failed rc=%Rrc\n", rc);
441 return 1;
442 }
443
444 RTRandAdvSeed(g_hRand, 0x12345678);
445
446 Test.pcszBackend = "vmdk";
447 Test.pcszBaseImage = "tstVDSnapBase.vmdk";
448 Test.pcszDiffSuff = "vmdk";
449 Test.cIterations = 30;
450 Test.cbTestPattern = 10 * _1M;
451 Test.cDiskSegsMin = 10;
452 Test.cDiskSegsMax = 50;
453 Test.cDiffsMinBeforeMerge = 5;
454 Test.uCreateDiffChance = 50; /* % */
455 Test.uChangeSegChance = 50; /* % */
456 Test.uAllocatedBlocks = 50; /* 50% allocated */
457 Test.fForward = true;
458 tstVDOpenCreateWriteMerge(&Test);
459
460 /* Same test with backwards merge */
461 Test.fForward = false;
462 tstVDOpenCreateWriteMerge(&Test);
463
464 rc = VDShutdown();
465 if (RT_FAILURE(rc))
466 {
467 RTPrintf("tstVDSnap: unloading backends failed! rc=%Rrc\n", rc);
468 g_cErrors++;
469 }
470 /*
471 * Summary
472 */
473 if (!g_cErrors)
474 RTPrintf("tstVDSnap: SUCCESS\n");
475 else
476 RTPrintf("tstVDSnap: FAILURE - %d errors\n", g_cErrors);
477
478 RTRandAdvDestroy(g_hRand);
479
480 return !!g_cErrors;
481}
482
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