VirtualBox

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

Last change on this file since 45761 was 45200, checked in by vboxsync, 12 years ago

Storage/testcase/tstVDSnap: Don't return immediately when something fails but cleanup first

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