VirtualBox

Changeset 38657 in vbox for trunk/src/VBox/Storage/VDI.cpp


Ignore:
Timestamp:
Sep 6, 2011 2:06:56 PM (13 years ago)
Author:
vboxsync
Message:

VD+VDI: Implement optimization for discard to avoid reading the complete data all the time and bug fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Storage/VDI.cpp

    r38647 r38657  
    888888
    889889    return rc;
     890}
     891
     892/**
     893 * Internal: Creates a allocation bitmap from the given data.
     894 * Sectors which contain only 0 are marked as unallocated and sectors with
     895 * other data as allocated.
     896 *
     897 * @returns Pointer to the allocation bitmap or NULL on failure.
     898 * @param   pvData    The data to create the allocation bitmap for.
     899 * @param   cbData    Number of bytes in the buffer.
     900 */
     901static void *vdiAllocationBitmapCreate(void *pvData, size_t cbData)
     902{
     903    unsigned cSectors = cbData / 512;
     904    unsigned uSectorCur = 0;
     905    void *pbmAllocationBitmap = NULL;
     906
     907    Assert(!(cbData % 512));
     908    Assert(!(cSectors % 8));
     909
     910    pbmAllocationBitmap = RTMemAllocZ(cSectors / 8);
     911
     912    while (uSectorCur < cSectors)
     913    {
     914        int idxSet = ASMBitFirstSet((uint8_t *)pvData + uSectorCur * 512, cbData * 8);
     915
     916        if (idxSet != -1)
     917        {
     918            unsigned idxSectorAlloc = idxSet / 8 / 512;
     919            ASMBitSet(pbmAllocationBitmap, uSectorCur + idxSectorAlloc);
     920
     921            uSectorCur += idxSectorAlloc + 1;
     922            cbData     -= (idxSectorAlloc + 1) * 512;
     923        }
     924        else
     925            break;
     926    }
     927
     928    return pbmAllocationBitmap;
    890929}
    891930
     
    25602599    Assert(!(cbDiscard % 512));
    25612600
     2601    AssertMsgReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
     2602                    ("Image is readonly\n"), VERR_VD_IMAGE_READ_ONLY);
     2603    AssertMsgReturn(   uOffset + cbDiscard <= getImageDiskSize(&pImage->Header)
     2604                    && cbDiscard,
     2605                    ("Invalid parameters uOffset=%llu cbDiscard=%zu\n",
     2606                     uOffset, cbDiscard),
     2607                     VERR_INVALID_PARAMETER);
     2608
    25622609    do
    25632610    {
     
    26692716                rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, cbImage - pImage->cbTotalBlockData);
    26702717            }
    2671             else /* if (fDiscard & VD_DISCARD_MARK_UNUSED) */
     2718            else if (fDiscard & VD_DISCARD_MARK_UNUSED)
    26722719            {
    26732720                /* Write changed data to the image. */
     
    26752722                                            pbBlockData + offDiscard, cbDiscard, NULL);
    26762723            }
    2677 #if 0
    26782724            else
    26792725            {
     
    26812727                *pcbPreAllocated = cbPreAllocated;
    26822728                *pcbPostAllocated = cbPostAllocated;
    2683                 *ppbmAllocationBitmap = vdAllocationBitmapCreate(pvBlock, getImageBlockSize(&pImage->Header));
     2729                *ppbmAllocationBitmap = vdiAllocationBitmapCreate(pbBlockData, getImageBlockSize(&pImage->Header));
    26842730                if (RT_UNLIKELY(!*ppbmAllocationBitmap))
    26852731                    rc = VERR_NO_MEMORY;
     
    26872733                    rc = VERR_VD_DISCARD_ALIGNMENT_NOT_MET;
    26882734            }
    2689 #endif
    26902735        }
    26912736        /* else: nothing to do. */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette