VirtualBox

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

Last change on this file since 53654 was 46614, checked in by vboxsync, 12 years ago

Storage/testcase: Increment error count

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 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 g_cErrors++; \
279 return rc; \
280 } \
281 } while (0)
282
283#define CHECK_BREAK(str) \
284 do \
285 { \
286 RTPrintf("%s rc=%Rrc\n", str, rc); \
287 if (RT_FAILURE(rc)) \
288 { \
289 g_cErrors++; \
290 break; \
291 } \
292 } while (0)
293
294 /* Create error interface. */
295 /* Create error interface. */
296 VDIfError.pfnError = tstVDError;
297 VDIfError.pfnMessage = tstVDMessage;
298
299 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
300 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
301 AssertRC(rc);
302
303
304 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
305 CHECK("VDCreate()");
306
307 rc = VDCreateBase(pVD, pTest->pcszBackend, pTest->pcszBaseImage, cbDisk,
308 VD_IMAGE_FLAGS_NONE, "Test image",
309 &PCHS, &LCHS, NULL, VD_OPEN_FLAGS_NORMAL,
310 NULL, NULL);
311 CHECK("VDCreateBase()");
312
313 bool fInit = true;
314 uint32_t cIteration = 0;
315
316 /* Do the real work now */
317 while ( RT_SUCCESS(rc)
318 && cIteration < pTest->cIterations)
319 {
320 /* Write */
321 rc = tstVDSnapWrite(pVD, paDiskSeg, cDiskSegments, cbDisk, fInit);
322 CHECK_BREAK("tstVDSnapWrite()");
323
324 fInit = false;
325
326 /* Write returned, do we want to create a new diff or merge them? */
327 bool fCreate = cDiffs < pTest->cDiffsMinBeforeMerge
328 ? true
329 : tstVDSnapIsTrue(pTest->uCreateDiffChance);
330
331 if (fCreate)
332 {
333 char *pszDiffFilename = NULL;
334
335 RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", idDiff, pTest->pcszDiffSuff);
336 CHECK("RTStrAPrintf()");
337 idDiff++;
338 cDiffs++;
339
340 rc = VDCreateDiff(pVD, pTest->pcszBackend, pszDiffFilename,
341 VD_IMAGE_FLAGS_NONE, "Test diff image", NULL, NULL,
342 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
343 CHECK_BREAK("VDCreateDiff()");
344
345 RTStrFree(pszDiffFilename);
346 VDDumpImages(pVD);
347
348 /* Change data */
349 tstVDSnapSegmentsDice(pTest, paDiskSeg, cDiskSegments, pbTestPattern, pTest->cbTestPattern);
350 }
351 else
352 {
353 uint32_t uStartMerge = RTRandAdvU32Ex(g_hRand, 1, cDiffs - 1);
354 uint32_t uEndMerge = RTRandAdvU32Ex(g_hRand, uStartMerge + 1, cDiffs);
355 RTPrintf("Merging %u diffs from %u to %u...\n",
356 uEndMerge - uStartMerge,
357 uStartMerge,
358 uEndMerge);
359 if (pTest->fForward)
360 rc = VDMerge(pVD, uStartMerge, uEndMerge, NULL);
361 else
362 rc = VDMerge(pVD, uEndMerge, uStartMerge, NULL);
363 CHECK_BREAK("VDMerge()");
364
365 cDiffs -= uEndMerge - uStartMerge;
366
367 VDDumpImages(pVD);
368
369 /* Go through the disk segments and reset pointers. */
370 for (uint32_t i = 0; i < cDiskSegments; i++)
371 {
372 if (paDiskSeg[i].pbDataDiff)
373 {
374 paDiskSeg[i].pbData = paDiskSeg[i].pbDataDiff;
375 paDiskSeg[i].pbDataDiff = NULL;
376 }
377 }
378
379 /* Now compare the result with our test pattern */
380 rc = tstVDSnapReadVerify(pVD, paDiskSeg, cDiskSegments, cbDisk);
381 CHECK_BREAK("tstVDSnapReadVerify()");
382 }
383 cIteration++;
384 }
385
386 VDDumpImages(pVD);
387
388 VDDestroy(pVD);
389 if (pbTestPattern)
390 RTMemFree(pbTestPattern);
391
392 RTFileDelete(pTest->pcszBaseImage);
393 for (unsigned i = 0; i < idDiff; i++)
394 {
395 char *pszDiffFilename = NULL;
396
397 RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", i, pTest->pcszDiffSuff);
398 RTFileDelete(pszDiffFilename);
399 RTStrFree(pszDiffFilename);
400 }
401#undef CHECK
402 return rc;
403}
404
405int main(int argc, char *argv[])
406{
407 RTR3InitExe(argc, &argv, 0);
408 int rc;
409 VDSNAPTEST Test;
410
411 RTPrintf("tstVDSnap: TESTING...\n");
412
413 rc = RTRandAdvCreateParkMiller(&g_hRand);
414 if (RT_FAILURE(rc))
415 {
416 RTPrintf("tstVDSnap: Creating RNG failed rc=%Rrc\n", rc);
417 return 1;
418 }
419
420 RTRandAdvSeed(g_hRand, 0x12345678);
421
422 Test.pcszBackend = "vmdk";
423 Test.pcszBaseImage = "tstVDSnapBase.vmdk";
424 Test.pcszDiffSuff = "vmdk";
425 Test.cIterations = 30;
426 Test.cbTestPattern = 10 * _1M;
427 Test.cDiskSegsMin = 10;
428 Test.cDiskSegsMax = 50;
429 Test.cDiffsMinBeforeMerge = 5;
430 Test.uCreateDiffChance = 50; /* % */
431 Test.uChangeSegChance = 50; /* % */
432 Test.uAllocatedBlocks = 50; /* 50% allocated */
433 Test.fForward = true;
434 tstVDOpenCreateWriteMerge(&Test);
435
436 /* Same test with backwards merge */
437 Test.fForward = false;
438 tstVDOpenCreateWriteMerge(&Test);
439
440 rc = VDShutdown();
441 if (RT_FAILURE(rc))
442 {
443 RTPrintf("tstVDSnap: unloading backends failed! rc=%Rrc\n", rc);
444 g_cErrors++;
445 }
446 /*
447 * Summary
448 */
449 if (!g_cErrors)
450 RTPrintf("tstVDSnap: SUCCESS\n");
451 else
452 RTPrintf("tstVDSnap: FAILURE - %d errors\n", g_cErrors);
453
454 RTRandAdvDestroy(g_hRand);
455
456 return !!g_cErrors;
457}
458
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