VirtualBox

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

Last change on this file since 33524 was 33524, checked in by vboxsync, 14 years ago

Storage: Implement offical support for other disk types like DVD and floppy images. DMG images can be used now without hacks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/** @file
2 *
3 * Snapshot VBox HDD container test utility.
4 */
5
6/*
7 * Copyright (C) 2010 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/VBoxHDD.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 VDINTERFACE VDIError;
222 VDINTERFACEERROR VDIErrorCallbacks;
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 /* Create the virtual disk test data */
233 pbTestPattern = (uint8_t *)RTMemAlloc(pTest->cbTestPattern);
234
235 RTRandAdvBytes(g_hRand, pbTestPattern, pTest->cbTestPattern);
236 cDiskSegments = RTRandAdvU32Ex(g_hRand, pTest->cDiskSegsMin, pTest->cDiskSegsMax);
237
238 uint64_t cbDisk = 0;
239
240 paDiskSeg = (PVDDISKSEG)RTMemAllocZ(cDiskSegments * sizeof(VDDISKSEG));
241 for (unsigned i = 0; i < cDiskSegments; i++)
242 {
243 paDiskSeg[i].off = cbDisk;
244 paDiskSeg[i].cbSeg = RT_ALIGN_64(RTRandAdvU64Ex(g_hRand, 512, pTest->cbTestPattern), 512);
245 if (tstVDSnapIsTrue(pTest->uAllocatedBlocks))
246 paDiskSeg[i].pbData = pbTestPattern + RT_ALIGN_64(RTRandAdvU64Ex(g_hRand, 0, pTest->cbTestPattern - paDiskSeg[i].cbSeg - 512), 512);
247 else
248 paDiskSeg[i].pbData = NULL; /* Not allocated initially */
249 cbDisk += paDiskSeg[i].cbSeg;
250 }
251
252 RTPrintf("Disk size is %llu bytes\n", cbDisk);
253
254#define CHECK(str) \
255 do \
256 { \
257 RTPrintf("%s rc=%Rrc\n", str, rc); \
258 if (RT_FAILURE(rc)) \
259 { \
260 if (pbTestPattern) \
261 RTMemFree(pbTestPattern); \
262 VDDestroy(pVD); \
263 return rc; \
264 } \
265 } while (0)
266
267 /* Create error interface. */
268 VDIErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
269 VDIErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
270 VDIErrorCallbacks.pfnError = tstVDError;
271 VDIErrorCallbacks.pfnMessage = tstVDMessage;
272
273 rc = VDInterfaceAdd(&VDIError, "tstVD_Error", VDINTERFACETYPE_ERROR, &VDIErrorCallbacks,
274 NULL, &pVDIfs);
275 AssertRC(rc);
276
277
278 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
279 CHECK("VDCreate()");
280
281 rc = VDCreateBase(pVD, pTest->pcszBackend, pTest->pcszBaseImage, cbDisk,
282 VD_IMAGE_FLAGS_NONE, "Test image",
283 &PCHS, &LCHS, NULL, VD_OPEN_FLAGS_NORMAL,
284 NULL, NULL);
285 CHECK("VDCreateBase()");
286
287 bool fInit = true;
288 uint32_t cIteration = 0;
289
290 /* Do the real work now */
291 while ( RT_SUCCESS(rc)
292 && cIteration < pTest->cIterations)
293 {
294 /* Write */
295 rc = tstVDSnapWrite(pVD, paDiskSeg, cDiskSegments, cbDisk, fInit);
296 CHECK("tstVDSnapWrite()");
297
298 fInit = false;
299
300 /* Write returned, do we want to create a new diff or merge them? */
301 bool fCreate = cDiffs < pTest->cDiffsMinBeforeMerge
302 ? true
303 : tstVDSnapIsTrue(pTest->uCreateDiffChance);
304
305 if (fCreate)
306 {
307 char *pszDiffFilename = NULL;
308
309 RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", idDiff, pTest->pcszDiffSuff);
310 CHECK("RTStrAPrintf()");
311 idDiff++;
312 cDiffs++;
313
314 rc = VDCreateDiff(pVD, pTest->pcszBackend, pszDiffFilename,
315 VD_IMAGE_FLAGS_NONE, "Test diff image", NULL, NULL,
316 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
317 CHECK("VDCreateDiff()");
318
319 RTStrFree(pszDiffFilename);
320 VDDumpImages(pVD);
321
322 /* Change data */
323 tstVDSnapSegmentsDice(pTest, paDiskSeg, cDiskSegments, pbTestPattern, pTest->cbTestPattern);
324 }
325 else
326 {
327 uint32_t uStartMerge = RTRandAdvU32Ex(g_hRand, 1, cDiffs - 1);
328 uint32_t uEndMerge = RTRandAdvU32Ex(g_hRand, uStartMerge + 1, cDiffs);
329 RTPrintf("Merging %u diffs from %u to %u...\n",
330 uEndMerge - uStartMerge,
331 uStartMerge,
332 uEndMerge);
333 if (pTest->fForward)
334 rc = VDMerge(pVD, uStartMerge, uEndMerge, NULL);
335 else
336 rc = VDMerge(pVD, uEndMerge, uStartMerge, NULL);
337 CHECK("VDMerge()");
338
339 cDiffs -= uEndMerge - uStartMerge;
340
341 VDDumpImages(pVD);
342
343 /* Go through the disk segments and reset pointers. */
344 for (uint32_t i = 0; i < cDiskSegments; i++)
345 {
346 if (paDiskSeg[i].pbDataDiff)
347 {
348 paDiskSeg[i].pbData = paDiskSeg[i].pbDataDiff;
349 paDiskSeg[i].pbDataDiff = NULL;
350 }
351 }
352
353 /* Now compare the result with our test pattern */
354 rc = tstVDSnapReadVerify(pVD, paDiskSeg, cDiskSegments, cbDisk);
355 CHECK("tstVDSnapReadVerify()");
356 }
357 cIteration++;
358 }
359
360 VDDumpImages(pVD);
361
362 VDDestroy(pVD);
363 if (pbTestPattern)
364 RTMemFree(pbTestPattern);
365
366 RTFileDelete(pTest->pcszBaseImage);
367 for (unsigned i = 0; i < idDiff; i++)
368 {
369 char *pszDiffFilename = NULL;
370
371 RTStrAPrintf(&pszDiffFilename, "tstVDSnapDiff%u.%s", i, pTest->pcszDiffSuff);
372 RTFileDelete(pszDiffFilename);
373 RTStrFree(pszDiffFilename);
374 }
375#undef CHECK
376 return 0;
377}
378
379int main(int argc, char *argv[])
380{
381 RTR3Init();
382 int rc;
383 VDSNAPTEST Test;
384
385 RTPrintf("tstVDSnap: TESTING...\n");
386
387 rc = RTRandAdvCreateParkMiller(&g_hRand);
388 if (RT_FAILURE(rc))
389 {
390 RTPrintf("tstVDSnap: Creating RNG failed rc=%Rrc\n", rc);
391 return 1;
392 }
393
394 RTRandAdvSeed(g_hRand, 0x12345678);
395
396 Test.pcszBackend = "vmdk";
397 Test.pcszBaseImage = "tstVDSnapBase.vmdk";
398 Test.pcszDiffSuff = "vmdk";
399 Test.cIterations = 30;
400 Test.cbTestPattern = 10 * _1M;
401 Test.cDiskSegsMin = 10;
402 Test.cDiskSegsMax = 50;
403 Test.cDiffsMinBeforeMerge = 5;
404 Test.uCreateDiffChance = 50; /* % */
405 Test.uChangeSegChance = 50; /* % */
406 Test.uAllocatedBlocks = 50; /* 50% allocated */
407 Test.fForward = true;
408 tstVDOpenCreateWriteMerge(&Test);
409
410 /* Same test with backwards merge */
411 Test.fForward = false;
412 tstVDOpenCreateWriteMerge(&Test);
413
414 rc = VDShutdown();
415 if (RT_FAILURE(rc))
416 {
417 RTPrintf("tstVDSnap: unloading backends failed! rc=%Rrc\n", rc);
418 g_cErrors++;
419 }
420 /*
421 * Summary
422 */
423 if (!g_cErrors)
424 RTPrintf("tstVDSnap: SUCCESS\n");
425 else
426 RTPrintf("tstVDSnap: FAILURE - %d errors\n", g_cErrors);
427
428 RTRandAdvDestroy(g_hRand);
429
430 return !!g_cErrors;
431}
432
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use