On 12/06/14 09:14, Eric Blake wrote:
Right now, grabbing blockinfo always calls stat on the disk, then
opens the image to determine the capacity, using a throw-away
virStorageSourcePtr. This has a couple of drawbacks:
1. We are calling stat and opening a file on every invocation of
the API. However, there are cases where the stats should NOT be
changing between successive calls (if a domain is running, no
one should be changing the physical size of a block device or raw
image behind our backs; capacity of read-only files should not
be changing; and we are the gateway to the block-resize command
to know when the capacity of read-write files should be changing).
True, we still have to use stat in some cases (a sparse raw file
changes allocation if it is read-write and the amount of holes is
changing, and a read-write qcow2 image stored in a file changes
physical size if it was not fully pre-allocated). But for
read-only images, even this should be something we can remember
from the previous time, rather than repeating every call.
2. We want to enhance the power of virDomainListGetStats, by
sharing code. But we already have a virStorageSourcePtr for
each disk, and it would be easier to reuse the common structure
than to have to worry about the one-off virDomainBlockInfoPtr.
While this patch does not optimize reuse of information in point
1, it does get us closer to being able to do so; by updating a
structure that survives between consecutive calls.
* src/util/virstoragefile.h (_virStorageSource): Add physical, to
mirror virDomainBlockInfo.
* src/qemu/qemu_driver.c (qemuDomainGetBlockInfo): Store into
storage source, then copy to block info.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/qemu/qemu_driver.c | 42 ++++++++++++++++++++++++++++++++++--------
src/util/virstoragefile.h | 3 ++-
2 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index ae4485a..e873362 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11034,6 +11034,26 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
disk = vm->def->disks[idx];
+ /* FIXME: For an offline domain, we always want to check current
+ * on-disk statistics (as users have been known to change offline
+ * images behind our backs). For a running domain, however, it
+ * would be nice to avoid opening a file (particularly since
+ * reading a file while qemu is writing it risks the reader seeing
+ * bogus data), or even avoid a stat, if the information
+ * remembered from the prevoius run is still viable.
+ *
+ * For read-only disks, nothing should be changing unless the user
+ * has requested a block-commit action. For read-write disks, we
+ * know some special cases: capacity should not change without a
+ * block-resize (where capacity is the only stat that requires
+ * opening a file, and even then, only for non-raw files); and
+ * physical size of a raw image or of a block device should
+ * likewise not be changing without block-resize. On the other
+ * hand, allocation of a raw file can change (if the file is
+ * sparse, but the amount of sparseness changes due to writes or
+ * punching holes), and physical size of a non-raw file can
+ * change.
For a live VM we should grab all of the above directly from the monitor
and not ever touch the files on the disk. We do that already for the
bulk stats and for getting the right size when doing storage migration.
This function unfortunately is legacy code compared to the stuff I've
pointed out
+ */
if (virStorageSourceIsLocalStorage(disk->src)) {
if (!disk->src->path) {
virReportError(VIR_ERR_INVALID_ARG,
@@ -11095,15 +11115,15 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
/* Get info for normal formats */
if (S_ISREG(sb.st_mode) || fd == -1) {
#ifndef WIN32
- info->physical = (unsigned long long)sb.st_blocks *
+ disk->src->physical = (unsigned long long)sb.st_blocks *
(unsigned long long)DEV_BSIZE;
#else
- info->physical = sb.st_size;
+ disk->src->physical = sb.st_size;
#endif
/* Regular files may be sparse, so logical size (capacity) is not same
* as actual physical above
*/
- info->capacity = sb.st_size;
+ disk->src->capacity = sb.st_size;
} else {
/* NB. Because we configure with AC_SYS_LARGEFILE, off_t should
* be 64 bits on all platforms.
@@ -11114,17 +11134,17 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
_("failed to seek to end of %s"), path);
goto endjob;
}
- info->physical = end;
- info->capacity = end;
+ disk->src->physical = end;
+ disk->src->capacity = end;
}
/* If the file we probed has a capacity set, then override
* what we calculated from file/block extents */
if (meta->capacity)
- info->capacity = meta->capacity;
+ disk->src->capacity = meta->capacity;
/* Set default value .. */
- info->allocation = info->physical;
+ disk->src->allocation = disk->src->physical;
/* ..but if guest is not using raw disk format and on a block device,
* then query highest allocated extent from QEMU
@@ -11146,13 +11166,19 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
qemuDomainObjEnterMonitor(driver, vm);
ret = qemuMonitorGetBlockExtent(priv->mon,
disk->info.alias,
- &info->allocation);
+ &disk->src->allocation);
qemuDomainObjExitMonitor(driver, vm);
} else {
ret = 0;
}
+ if (ret == 0) {
+ info->capacity = disk->src->capacity;
+ info->allocation = disk->src->allocation;
+ info->physical = disk->src->physical;
+ }
+
endjob:
if (!qemuDomainObjEndJob(driver, vm))
vm = NULL;
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index e05b843..b4c3808 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -257,8 +257,9 @@ struct _virStorageSource {
virStoragePermsPtr perms;
virStorageTimestampsPtr timestamps;
- unsigned long long allocation; /* in bytes, 0 if unknown */
Spurious move?
unsigned long long capacity; /* in bytes, 0 if unknown */
+ unsigned long long allocation; /* in bytes, 0 if unknown */
+ unsigned long long physical; /* in bytes, 0 if unknown */
size_t nseclabels;
virSecurityDeviceLabelDefPtr *seclabels;
Also an addition to virStorageSourceCopy is missing.
ACK with the tweak to virStorageSourceCopy
Peter