Skip to contentSkip to conversation

Qemu: Enable dynamic cache change through qemu monitor

RFC PATCH Superseded Supriya Kannery 5 participants 12 messages May 16, 2011 – May 20, 2011

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

cover Qemu: Enable dynamic cache change through qemu monitor


Currently cache setting of a block device cannot be changed without restarting a running VM. Following patchset is for enabling dynamic change of cache setting for block devices through qemu monitor. Code changes are based on patches from Christoph Hellwig and Prerna Saxena.

Monitor command "info block" is extended to display cache setting of block devices. New monitor command "cache_set" is added using which cache setting can be dynamically changed
Usage:
"cache_set <device> <cache>"
<device> = block device
<cache> = "off"/"none", "on"/"writeback", "writethrough", "unsafe"

1/3 Enhance "info block" to display cache setting 2/3 New error classes for file reopen and device insertion 3/3 Add monitor command "cache_set" for dynamic cache change

 qemu/block.c         |   71
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 qemu/block.h         |    2 +
 qemu/blockdev.c      |   20 +++++++++++++++++++
 qemu/blockdev.h      |    1
 qemu/hmp-commands.hx |   14 +++++++++++++
 qemu/qerror.c        |    8 +++++++
 qemu/qerror.h        |    6 +++++
 qemu/qmp-commands.hx |   28 ++++++++++++++++++++++++++
 8 files changed, 148 insertions(+), 2 deletions(-)

~

#1 lore raw

RFC PATCH 1/3 Qemu: Enhance "info block" to display cache setting

Enhance "info block" to display cache setting

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Enhanced to include "cache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 cache=none file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

---
 block.c |   18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
block.c +16 -2
Index: qemu/block.c
===================================================================
@@ -1713,6 +1713,19 @@ static void bdrv_print_dict(QObject *obj
         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
     }
 
+    if (qdict_haskey(bs_dict, "open_flags")) {
+        int open_flags = qdict_get_int(bs_dict, "open_flags");
+        if (open_flags & BDRV_O_NOCACHE) {
+            monitor_printf(mon, " cache=none");
+        } else if (open_flags & BDRV_O_CACHE_WB) {
+            if (open_flags & BDRV_O_NO_FLUSH)
+                monitor_printf(mon, " cache=unsafe");
+            else
+                monitor_printf(mon, " cache=writeback");
+        } else
+            monitor_printf(mon, " cache=writethrough");
+    }
+
     if (qdict_haskey(bs_dict, "inserted")) {
         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
 
@@ -1762,9 +1775,10 @@ void bdrv_info(Monitor *mon, QObject **r
         }
 
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
-                                    "'removable': %i, 'locked': %i }",
+                                    "'removable': %i, 'locked': %i, "
+                                    "'open_flags': %d }",
                                     bs->device_name, type, bs->removable,
-                                    bs->locked);
+                                    bs->locked, bs->open_flags);
 
         if (bs->drv) {
             QObject *obj;
#2 lore raw

Re: [Qemu-devel] [RFC Patch 1/3]Qemu: Enhance "info block" to display cache setting

Am 16.05.2011 20:10, schrieb Supriya Kannery:

Quoted 46 lines

Enhance "info block" to display cache setting

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Enhanced to include "cache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 cache=none file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

---
 block.c |   18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

Index: qemu/block.c
===================================================================

block.c +13 -0
@@ -1713,6 +1713,19 @@ static void bdrv_print_dict(QObject *obj
         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
     }
 
+    if (qdict_haskey(bs_dict, "open_flags")) {
+        int open_flags = qdict_get_int(bs_dict, "open_flags");
+        if (open_flags & BDRV_O_NOCACHE) {
+            monitor_printf(mon, " cache=none");
+        } else if (open_flags & BDRV_O_CACHE_WB) {
+            if (open_flags & BDRV_O_NO_FLUSH)
+                monitor_printf(mon, " cache=unsafe");
+            else
+                monitor_printf(mon, " cache=writeback");
+        } else
+            monitor_printf(mon, " cache=writethrough");
+    }
+
     if (qdict_haskey(bs_dict, "inserted")) {
         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
 
@@ -1762,9 +1775,10 @@ void bdrv_info(Monitor *mon, QObject **r
         }
 

block.c void bdrv_info(Monitor *mon, QObject **r · line 1784

Quoted 10 lines
block.c +3 -2
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
-                                    "'removable': %i, 'locked': %i }",
+                                    "'removable': %i, 'locked': %i, "
+                                    "'open_flags': %d }",
                                     bs->device_name, type, bs->removable,
-                                    bs->locked);
+                                    bs->locked, bs->open_flags);
 
         if (bs->drv) {
             QObject *obj;

bs->open_flags is a purely internal thing and its meaning is not guaranteed to be stable. Exposing it to the user is wrong.

Kevin

#3 lore raw

Re: [Qemu-devel] [RFC Patch 1/3]Qemu: Enhance "info block" to display cache setting

Quoted 23 lines · Kevin Wolf

Am 16.05.2011 20:10, schrieb Supriya Kannery:

Quoted 21 lines

Enhance "info block" to display cache setting

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Enhanced to include "cache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 cache=none file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
block.c +3 -2
-                                    "'removable': %i, 'locked': %i }",
+                                    "'removable': %i, 'locked': %i, "
+                                    "'open_flags': %d }",
                                     bs->device_name, type, bs->removable,
-                                    bs->locked);
+                                    bs->locked, bs->open_flags);
Quoted 8 lines
Quoted 3 lines
         if (bs->drv) {
             QObject *obj;

bs->open_flags is a purely internal thing and its meaning is not guaranteed to be stable. Exposing it to the user is wrong.

ok. Pls suggest what could a better approach to expose the cache setting.

Quoted 3 lines

Kevin

#4 lore raw

RFC PATCH 2/3 Qemu: New error classes for file reopen and device insertion

In reply to Supriya Kannery

New errors defined for device insertion and file reopen

---
 qerror.c |    8 ++++++++
 qerror.h |    6 ++++++
 2 files changed, 14 insertions(+)
qerror.c +8 -0
Index: qemu/qerror.c
===================================================================
@@ -93,6 +93,10 @@ static const QErrorStringTable qerror_ta
         .desc      = "Device '%(device)' not found",
     },
     {
+        .error_fmt = QERR_DEVICE_NOT_INSERTED,
+        .desc      = "Device '%(device)' has not been inserted",
+    },
+    {
         .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
         .desc      = "Device '%(device)' is not removable",
     },
@@ -161,6 +165,10 @@ static const QErrorStringTable qerror_ta
         .desc      = "Could not open '%(filename)'",
     },
     {
+        .error_fmt = QERR_REOPEN_FILE_FAILED,
+        .desc      = "Could not reopen '%(filename)'",
+    },
+    {
         .error_fmt = QERR_PROPERTY_NOT_FOUND,
         .desc      = "Property '%(device).%(property)' not found",
     },
qerror.h +6 -0
Index: qemu/qerror.h
===================================================================
@@ -84,6 +84,9 @@ QError *qobject_to_qerror(const QObject 
 #define QERR_DEVICE_NOT_FOUND \
     "{ 'class': 'DeviceNotFound', 'data': { 'device': %s } }"
 
+#define QERR_DEVICE_NOT_INSERTED \
+    "{ 'class': 'DeviceNotInserted', 'data': { 'device': %s } }"
+
 #define QERR_DEVICE_NOT_REMOVABLE \
     "{ 'class': 'DeviceNotRemovable', 'data': { 'device': %s } }"
 
@@ -135,6 +138,9 @@ QError *qobject_to_qerror(const QObject 
 #define QERR_OPEN_FILE_FAILED \
     "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }"
 
+#define QERR_REOPEN_FILE_FAILED \
+    "{ 'class': 'ReopenFileFailed', 'data': { 'filename': %s } }"
+
 #define QERR_PROPERTY_NOT_FOUND \
     "{ 'class': 'PropertyNotFound', 'data': { 'device': %s, 'property': %s } }"
 
#5 lore raw

RFC PATCH 3/3 Qemu: Add command "cache_set" for dynamic cache change

In reply to Supriya Kannery

Add monitor command "cache_set" for dynamic cache change

---
 block.c         |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 block.h         |    2 ++
 blockdev.c      |   20 ++++++++++++++++++++
 blockdev.h      |    1 +
 hmp-commands.hx |   14 ++++++++++++++
 qmp-commands.hx |   28 ++++++++++++++++++++++++++++
 6 files changed, 118 insertions(+)
hmp-commands.hx +14 -0
Index: qemu/hmp-commands.hx
===================================================================
@@ -70,6 +70,20 @@ but should be used with extreme caution.
 resizes image files, it can not resize block devices like LVM volumes.
 ETEXI
 
+    {
+        .name       = "cache_set",
+        .args_type  = "device:B,cache:s",
+        .params     = "device cache",
+        .help       = "change cache setting for device",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_cache_set,
+    },
+
+STEXI
+@item cache_set
+@findex cache_set
+Change cache options for a block device while guest is running.
+ETEXI
 
     {
         .name       = "eject",
block.c +53 -0

62 lines not on this page · raw message

blockdev.c +20 -0
Index: qemu/blockdev.c
===================================================================
@@ -796,3 +796,23 @@ int do_block_resize(Monitor *mon, const 
 
     return 0;
 }
+
+int do_cache_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    const char *cache = qdict_get_str(qdict, "cache");
+    BlockDriverState *bs;
+
+    bs = bdrv_find(device);
+    if (!bs) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, device);
+        return -1;
+    }
+
+    if(bdrv_is_inserted(bs)) {
+       return(bdrv_change_cache(bs, cache));
+    } else {
+       qerror_report(QERR_DEVICE_NOT_INSERTED, device);
+       return -1;
+    }
+}
block.h +2 -0
Index: qemu/block.h
===================================================================
@@ -71,6 +71,7 @@ void bdrv_delete(BlockDriverState *bs);
 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
 int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
               BlockDriver *drv);
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags);
 void bdrv_close(BlockDriverState *bs);
 int bdrv_attach(BlockDriverState *bs, DeviceState *qdev);
 void bdrv_detach(BlockDriverState *bs, DeviceState *qdev);
@@ -96,6 +97,7 @@ void bdrv_commit_all(void);
 int bdrv_change_backing_file(BlockDriverState *bs,
     const char *backing_file, const char *backing_fmt);
 void bdrv_register(BlockDriver *bdrv);
+int bdrv_change_cache(BlockDriverState *bs, const char *cache);
 
 
 typedef struct BdrvCheckResult {
blockdev.h +1 -0
Index: qemu/blockdev.h
===================================================================
@@ -64,5 +64,6 @@ int do_change_block(Monitor *mon, const 
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_cache_set(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 #endif
qmp-commands.hx +28 -0
Index: qemu/qmp-commands.hx
===================================================================
@@ -664,6 +664,34 @@ Example:
 -> { "execute": "block_resize", "arguments": { "device": "scratch", "size": 1073741824 } }
 <- { "return": {} }
 
+
+EQMP
+
+    {
+        .name       = "cache_set",
+        .args_type  = "device:B,cache:s",
+        .params     = "device cache",
+        .help       = "change cache setting for device",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_cache_set,
+    },
+
+SQMP
+cache_set
+---------
+
+Change cache setting while a guest is running.
+
+Arguments:
+
+- "device": the device's ID, must be unique (json-string)
+- "cache": new cache string like none, writethrough etc.. (json-string)
+
+Example:
+
+-> { "execute": "cache_set", "arguments": { "device": "ide0-hd0", "cache": "writeback" } }
+<- { "return": {} }
+
 EQMP
 
     {
#6 lore raw
In reply to Supriya Kannery

Why are you even trying this again? As explained very clearly last time you can't change from a writeback-style to a write-through style I/O from the monitor without creating massive data integrity problems. See my patchset that allows changing this from the guest for how it should be done - I just need to get back and revisit the virtio protocol support for it.

#7 lore raw
Quoted 3 lines · Christoph Hellwig

Why are you even trying this again? As explained very clearly last time you can't change from a writeback-style to a write-through style I/O from the monitor without creating massive data integrity problems.

To further clarify:

Today cache=none|writethrough|writeback does two things. It:

1) Changes the WCE flag that's visible to the guest

2) Determines whether the host page cache is used for doing guest I/O

As Christoph is very correct in pointing out, we cannot change (1) at run time because this is guest visible. You will break a guest if you do this.

But it's still desirable to be able to change (2) at run time. Before we can do this properly though, we need to separate out the logic for setting (1) vs. (2).

And ideally, we would allow (1) to be changed by the guest itself at run time which allows for full dynamic control. This is what he's referring to below.

Regards,

Anthony Liguori

See my

Quoted 5 lines

patchset that allows changing this from the guest for how it should be done - I just need to get back and revisit the virtio protocol support for it.

#8 lore raw
Quoted 7 lines · Anthony Liguori
Quoted 3 lines · Christoph Hellwig

Why are you even trying this again? As explained very clearly last time you can't change from a writeback-style to a write-through style I/O from the monitor without creating massive data integrity problems.

To further clarify:

Quoted 8 lines

Today cache=none|writethrough|writeback does two things. It:

1) Changes the WCE flag that's visible to the guest

2) Determines whether the host page cache is used for doing guest I/O

As Christoph is very correct in pointing out, we cannot change (1) at run time because this is guest visible. You will break a guest if you do this.

ok

Quoted 2 lines

But it's still desirable to be able to change (2) at run time. Before we can do this properly though, we need to separate out the logic for setting (1) vs. (2).

Will go through the code in detail to understand handling of (1) and (2).

Quoted 14 lines

And ideally, we would allow (1) to be changed by the guest itself at run time which allows for full dynamic control. This is what he's referring to below.

Regards,

Anthony Liguori

See my

Quoted 5 lines

patchset that allows changing this from the guest for how it should be done - I just need to get back and revisit the virtio protocol support for it.

#9 lore raw
In reply to Anthony Liguori
Quoted 11 lines · Anthony Liguori

To further clarify:

Today cache=none|writethrough|writeback does two things. It:

1) Changes the WCE flag that's visible to the guest

2) Determines whether the host page cache is used for doing guest I/O

As Christoph is very correct in pointing out, we cannot change (1) at run time because this is guest visible. You will break a guest if you do this.

Quoted 7 lines

But it's still desirable to be able to change (2) at run time. Before we can do this properly though, we need to separate out the logic for setting
(1) vs. (2).

And ideally, we would allow (1) to be changed by the guest itself at run time which allows for full dynamic control. This is what he's referring to below.

Exactly. Setting/clearing the BDRV_O_NO_FLUSH also seems useful, maybe in addition to also allowing an equivalent for the writethrough modes.

#10 lore raw
Quoted 16 lines · Christoph Hellwig
Quoted 15 lines · Anthony Liguori

To further clarify:

Today cache=none|writethrough|writeback does two things. It:

1) Changes the WCE flag that's visible to the guest

2) Determines whether the host page cache is used for doing guest I/O

As Christoph is very correct in pointing out, we cannot change (1) at run time because this is guest visible. You will break a guest if you do this.

But it's still desirable to be able to change (2) at run time. Before we can do this properly though, we need to separate out the logic for setting
(1) vs. (2).

Quoted 7 lines
Quoted 3 lines

And ideally, we would allow (1) to be changed by the guest itself at run time which allows for full dynamic control. This is what he's referring to below.

Exactly. Setting/clearing the BDRV_O_NO_FLUSH also seems useful, maybe in addition to also allowing an equivalent for the writethrough modes.

Posted second version of the patchset (RFC) which supports only hostcache setting/clearing from qemu monitor.
http://www.mail-archive.com/[email protected]/msg64658.html
Please comment.

#11 lore raw
In reply to Christoph Hellwig
Quoted 1 line · Christoph Hellwig

Why are you even trying this again?

Enabling control of cache setting from qemu monitor will help users/admins to accomplish cache value change without depending on the guest.

Quoted 9 lines

As explained very clearly last time you
can't change from a writeback-style to a write-through style I/O from the monitor without creating massive data integrity problems. See my patchset that allows changing this from the guest for how it should be done - I just need to get back and revisit the virtio protocol support for it.

ok, sure, I will go through your related patches and work further on this.