diff mbox

[3/3] QMP: Introduce BLOCK_WATERMARK event

Message ID 1268175216-3600-4-git-send-email-lcapitulino@redhat.com
State New
Headers show

Commit Message

Luiz Capitulino March 9, 2010, 10:53 p.m. UTC
Emitted whenever the watermark value set by the Monitor
'block_watermark' command is reached.

This value is not permanently stored and it's automatically
set to zero when the event is emitted.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 QMP/qmp-events.txt     |   26 ++++++++++++++++++++++++++
 block/qcow2-refcount.c |   20 ++++++++++++++++++++
 monitor.c              |    3 +++
 monitor.h              |    1 +
 4 files changed, 50 insertions(+), 0 deletions(-)
diff mbox

Patch

diff --git a/QMP/qmp-events.txt b/QMP/qmp-events.txt
index a94e9b4..f1fe3aa 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -26,6 +26,32 @@  Example:
 Note: If action is "stop", a STOP event will eventually follow the
 BLOCK_IO_ERROR event.
 
+BLOCK_WATERMARK
+---------------
+
+Emitted when the watermark value for a device is reached.
+
+Data:
+
+- "device": device name (json-string)
+- "type": watermark type (json-string, only "high" currently)
+- "mark": current device's high watermark value (json-int)
+- "allocation": current allocation (json-int)
+
+Example:
+
+{ "event": "BLOCK_WATERMARK",
+    "data": { "device": "ide0-hd0",
+              "ide0-hd0",
+              "mark": 1048576,
+              "allocation": 1376256 },
+    "timestamp": { "seconds": 1268170711, "microseconds": 698676 } }
+
+Note: (1) The watermark value is set by the 'block_watermark' Monitor's command
+      (2) The device's watermark value is set to zero when the event is
+          emitted. Thus, this event is emitted only once.
+      (3) Currently only the high watermark value is supported
+
 RESET
 -----
 
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 9cb38c8..684e0ff 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -23,6 +23,8 @@ 
  */
 
 #include "qemu-common.h"
+#include "qjson.h"
+#include "monitor.h"
 #include "block_int.h"
 #include "block/qcow2.h"
 
@@ -531,6 +533,17 @@  static int update_cluster_refcount(BlockDriverState *bs,
 /* cluster allocation functions */
 
 
+static void watermark_mon_event(const BlockDriverState *bs, int64_t ha)
+{
+    QObject *data;
+
+    data = qobject_from_jsonf("{ 'device': %s, 'type': 'high', "
+                              "'allocation': %" PRId64 ", 'mark': %" PRId64 "}",
+                              bs->device_name, ha, bs->high_watermark);
+
+    monitor_protocol_event(QEVENT_BLOCK_WATERMARK, data);
+    qobject_decref(data);
+}
 
 /* return < 0 if error */
 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
@@ -552,6 +565,13 @@  retry:
 #endif
     if (s->highest_alloc < s->free_cluster_index) {
         s->highest_alloc = s->free_cluster_index;
+        if (bs->high_watermark) {
+            uint64_t ha = (s->highest_alloc << s->cluster_bits);
+            if (ha >= bs->high_watermark) {
+                watermark_mon_event(bs, ha);
+                bs->high_watermark = 0;
+            }
+        }
     }
     return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
 }
diff --git a/monitor.c b/monitor.c
index 595bd62..3f74994 100644
--- a/monitor.c
+++ b/monitor.c
@@ -423,6 +423,9 @@  void monitor_protocol_event(MonitorEvent event, QObject *data)
         case QEVENT_BLOCK_IO_ERROR:
             event_name = "BLOCK_IO_ERROR";
             break;
+        case QEVENT_BLOCK_WATERMARK:
+            event_name = "BLOCK_WATERMARK";
+            break;
         case QEVENT_RTC_CHANGE:
             event_name = "RTC_CHANGE";
             break;
diff --git a/monitor.h b/monitor.h
index e5f2d2b..e767990 100644
--- a/monitor.h
+++ b/monitor.h
@@ -23,6 +23,7 @@  typedef enum MonitorEvent {
     QEVENT_VNC_INITIALIZED,
     QEVENT_VNC_DISCONNECTED,
     QEVENT_BLOCK_IO_ERROR,
+    QEVENT_BLOCK_WATERMARK,
     QEVENT_RTC_CHANGE,
     QEVENT_WATCHDOG,
     QEVENT_MAX,