Skip to contentSkip to conversation

Monitor: make output buffer dynamic

PATCH Superseded Luiz Capitulino 5 participants 12 messages Mar 25, 2013 – Apr 9, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Monitor: make output buffer dynamic

Hi,

This series fixes an easy to reproduce assertion in the Monitor code. The second patch contains all the relevant details.

Gerd, I'd like a reviewed-by from you before merging this.

Luiz Capitulino (2):
qstring: add qobject_get_length()
Monitor: Make output buffer dynamic

 include/qapi/qmp/qstring.h |  1 +
 monitor.c                  | 42 +++++++++++++++++++++++++-----------------
 qobject/qstring.c          |  8 ++++++++
 3 files changed, 34 insertions(+), 17 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/2 qstring: add qobject_get_length()

Long overdue.

---
 include/qapi/qmp/qstring.h | 1 +
 qobject/qstring.c          | 8 ++++++++
 2 files changed, 9 insertions(+)
include/qapi/qmp/qstring.h +1 -0
index 0e690f4..1bc3666 100644
@@ -26,6 +26,7 @@ typedef struct QString {
 QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 QString *qstring_from_substr(const char *str, int start, int end);
+size_t qstring_get_length(const QString *qstring);
 const char *qstring_get_str(const QString *qstring);
 void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
qobject/qstring.c +8 -0
index 5f7376c..607b7a1 100644
@@ -32,6 +32,14 @@ QString *qstring_new(void)
 }
 
 /**
+ * qstring_get_length(): Get the length of a QString
+ */
+size_t qstring_get_length(const QString *qstring)
+{
+    return qstring->length;
+}
+
+/**
  * qstring_from_substr(): Create a new QString from a C string substring
  *
  * Return string reference
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] qstring: add qobject_get_length()

'add qobject_get_length()' in the subject line but you are actually adding qstring_get_length()

Quoted 41 lines · Luiz Capitulino

Long overdue.

---
 include/qapi/qmp/qstring.h | 1 +
 qobject/qstring.c          | 8 ++++++++
 2 files changed, 9 insertions(+)
include/qapi/qmp/qstring.h +1 -0
index 0e690f4..1bc3666 100644
@@ -26,6 +26,7 @@ typedef struct QString {
 QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 QString *qstring_from_substr(const char *str, int start, int end);
+size_t qstring_get_length(const QString *qstring);
 const char *qstring_get_str(const QString *qstring);
 void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
qobject/qstring.c +8 -0
index 5f7376c..607b7a1 100644
@@ -32,6 +32,14 @@ QString *qstring_new(void)
 }
 
 /**
+ * qstring_get_length(): Get the length of a QString
+ */
+size_t qstring_get_length(const QString *qstring)
+{
+    return qstring->length;
+}
+
+/**
  * qstring_from_substr(): Create a new QString from a C string substring
  *
  * Return string reference
Signature · 1 line
1.8.1.4
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] qstring: add qobject_get_length()

Quoted 2 lines · On Tue, 9 Apr 2013 11:02:15 +0800 Hu Tao <[email protected]> wrote:

'add qobject_get_length()' in the subject line but you are actually adding qstring_get_length()

Thanks, but this was corrected in v2 and it's what got merged.

Quoted 44 lines · Luiz Capitulino
Quoted 41 lines

Long overdue.

---
 include/qapi/qmp/qstring.h | 1 +
 qobject/qstring.c          | 8 ++++++++
 2 files changed, 9 insertions(+)
include/qapi/qmp/qstring.h +1 -0
index 0e690f4..1bc3666 100644
@@ -26,6 +26,7 @@ typedef struct QString {
 QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 QString *qstring_from_substr(const char *str, int start, int end);
+size_t qstring_get_length(const QString *qstring);
 const char *qstring_get_str(const QString *qstring);
 void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
qobject/qstring.c +8 -0
index 5f7376c..607b7a1 100644
@@ -32,6 +32,14 @@ QString *qstring_new(void)
 }
 
 /**
+ * qstring_get_length(): Get the length of a QString
+ */
+size_t qstring_get_length(const QString *qstring)
+{
+    return qstring->length;
+}
+
+/**
  * qstring_from_substr(): Create a new QString from a C string substring
  *
  * Return string reference
Signature · 1 line
1.8.1.4
#4 lore raw

PATCH 2/2 Monitor: Make output buffer dynamic

In reply to Luiz Capitulino

Commit f628926bb423fa8a7e0b114511400ea9df38b76a changed monitor_flush() to retry on qemu_chr_fe_write() errors. However, the Monitor's output buffer can keep growing while the retry is not issued and this can cause the buffer to overflow.

To reproduce this issue, just start qemu and type on the Monitor:

(qemu) ?

This will cause the assertion to trig.

To fix this problem this commit makes the Monitor buffer dynamic, which means that it can grow as much as needed.

---
 monitor.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)
monitor.c +25 -17
index 2d9e887..c93dfe7 100644
@@ -188,8 +188,7 @@ struct Monitor {
     int reset_seen;
     int flags;
     int suspend_cnt;
-    uint8_t outbuf[1024];
-    int outbuf_index;
+    QString *outbuf;
     ReadLineState *rs;
     MonitorControl *mc;
     CPUArchState *mon_cpu;
@@ -271,45 +270,52 @@ static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
 void monitor_flush(Monitor *mon)
 {
     int rc;
+    size_t len;
+    const char *buf;
+
+    buf = qstring_get_str(mon->outbuf);
+    len = qstring_get_length(mon->outbuf);
 
-    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
-        rc = qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
-        if (rc == mon->outbuf_index) {
+    if (mon && len && !mon->mux_out) {
+        rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
+        if (rc == len) {
             /* all flushed */
-            mon->outbuf_index = 0;
+            QDECREF(mon->outbuf);
+            mon->outbuf = qstring_new();
             return;
         }
         if (rc > 0) {
             /* partinal write */
-            memmove(mon->outbuf, mon->outbuf + rc, mon->outbuf_index - rc);
-            mon->outbuf_index -= rc;
+            QString *tmp = qstring_from_str(buf + rc);
+            QDECREF(mon->outbuf);
+            mon->outbuf = tmp;
         }
         qemu_chr_fe_add_watch(mon->chr, G_IO_OUT, monitor_unblocked, mon);
     }
 }
 
-/* flush at every end of line or if the buffer is full */
+/* flush at every end of line */
 static void monitor_puts(Monitor *mon, const char *str)
 {
     char c;
 
     for(;;) {
-        assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
         c = *str++;
         if (c == '\0')
             break;
-        if (c == '\n')
-            mon->outbuf[mon->outbuf_index++] = '\r';
-        mon->outbuf[mon->outbuf_index++] = c;
-        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
-            || c == '\n')
+        if (c == '\n') {
+            qstring_append_chr(mon->outbuf, '\r');
+        }
+        qstring_append_chr(mon->outbuf, c);
+        if (c == '\n') {
             monitor_flush(mon);
+        }
     }
 }
 
 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
 {
-    char buf[4096];
+    char *buf;
 
     if (!mon)
         return;
@@ -318,8 +324,9 @@ void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
         return;
     }
 
-    vsnprintf(buf, sizeof(buf), fmt, ap);
+    buf = g_strdup_vprintf(fmt, ap);
     monitor_puts(mon, buf);
+    g_free(buf);
 }
 
 void monitor_printf(Monitor *mon, const char *fmt, ...)
@@ -4732,6 +4739,7 @@ void monitor_init(CharDriverState *chr, int flags)
     }
 
     mon = g_malloc0(sizeof(*mon));
+    mon->outbuf = qstring_new();
 
     mon->chr = chr;
     mon->flags = flags;
Signature · 1 line
1.8.1.4
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] Monitor: Make output buffer dynamic

Il 25/03/2013 20:40, Luiz Capitulino ha scritto:

Quoted 5 lines

Commit f628926bb423fa8a7e0b114511400ea9df38b76a changed monitor_flush() to retry on qemu_chr_fe_write() errors. However, the Monitor's output buffer can keep growing while the retry is not issued and this can cause the buffer to overflow.

Quoted 8 lines

To reproduce this issue, just start qemu and type on the Monitor:

(qemu) ?

This will cause the assertion to trig.

To fix this problem this commit makes the Monitor buffer dynamic, which means that it can grow as much as needed.

What about using a GString instead?

Paolo

Quoted 106 lines
---
 monitor.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)
monitor.c +25 -17
index 2d9e887..c93dfe7 100644
@@ -188,8 +188,7 @@ struct Monitor {
     int reset_seen;
     int flags;
     int suspend_cnt;
-    uint8_t outbuf[1024];
-    int outbuf_index;
+    QString *outbuf;
     ReadLineState *rs;
     MonitorControl *mc;
     CPUArchState *mon_cpu;
@@ -271,45 +270,52 @@ static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
 void monitor_flush(Monitor *mon)
 {
     int rc;
+    size_t len;
+    const char *buf;
+
+    buf = qstring_get_str(mon->outbuf);
+    len = qstring_get_length(mon->outbuf);
 
-    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
-        rc = qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
-        if (rc == mon->outbuf_index) {
+    if (mon && len && !mon->mux_out) {
+        rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
+        if (rc == len) {
             /* all flushed */
-            mon->outbuf_index = 0;
+            QDECREF(mon->outbuf);
+            mon->outbuf = qstring_new();
             return;
         }
         if (rc > 0) {
             /* partinal write */
-            memmove(mon->outbuf, mon->outbuf + rc, mon->outbuf_index - rc);
-            mon->outbuf_index -= rc;
+            QString *tmp = qstring_from_str(buf + rc);
+            QDECREF(mon->outbuf);
+            mon->outbuf = tmp;
         }
         qemu_chr_fe_add_watch(mon->chr, G_IO_OUT, monitor_unblocked, mon);
     }
 }
 
-/* flush at every end of line or if the buffer is full */
+/* flush at every end of line */
 static void monitor_puts(Monitor *mon, const char *str)
 {
     char c;
 
     for(;;) {
-        assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
         c = *str++;
         if (c == '\0')
             break;
-        if (c == '\n')
-            mon->outbuf[mon->outbuf_index++] = '\r';
-        mon->outbuf[mon->outbuf_index++] = c;
-        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
-            || c == '\n')
+        if (c == '\n') {
+            qstring_append_chr(mon->outbuf, '\r');
+        }
+        qstring_append_chr(mon->outbuf, c);
+        if (c == '\n') {
             monitor_flush(mon);
+        }
     }
 }
 
 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
 {
-    char buf[4096];
+    char *buf;
 
     if (!mon)
         return;
@@ -318,8 +324,9 @@ void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
         return;
     }
 
-    vsnprintf(buf, sizeof(buf), fmt, ap);
+    buf = g_strdup_vprintf(fmt, ap);
     monitor_puts(mon, buf);
+    g_free(buf);
 }
 
 void monitor_printf(Monitor *mon, const char *fmt, ...)
@@ -4732,6 +4739,7 @@ void monitor_init(CharDriverState *chr, int flags)
     }
 
     mon = g_malloc0(sizeof(*mon));
+    mon->outbuf = qstring_new();
 
     mon->chr = chr;
     mon->flags = flags;
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] Monitor: Make output buffer dynamic

Quoted 8 lines · On Mon, 25 Mar 2013 22:07:36 +0100 Paolo Bonzini <[email protected]> wrote:

Il 25/03/2013 20:40, Luiz Capitulino ha scritto:

Quoted 7 lines

Commit f628926bb423fa8a7e0b114511400ea9df38b76a changed monitor_flush() to retry on qemu_chr_fe_write() errors. However, the Monitor's output buffer can keep growing while the retry is not issued and this can cause the buffer to overflow.

To reproduce this issue, just start qemu and type on the Monitor:

Quoted 8 lines
Quoted 6 lines

(qemu) ?

This will cause the assertion to trig.

To fix this problem this commit makes the Monitor buffer dynamic, which means that it can grow as much as needed.

What about using a GString instead?

Makes no difference for me (although I don't enjoy mixing QString and GString in the same module), but if we do this with the goal of stopping QString proliferation then we should note somewhere that it shouldn't be used in new code anymore.

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] Monitor: Make output buffer dynamic

In reply to Luiz Capitulino

Hi, Luiz
Personally I hope reduce the dynamic allocated buffer which brings
fragments and unexpected memory grow. Instead, how about sacrifice some time to wait output complete, since monitor is not time critical? in this case static buffer's size can decide how many work can be postponded. Following is my suggestion:

monitor.c +0 -0
@@ -293,17 +293,28 @@ static void monitor_puts(Monitor *mon, const char

*str)

 {
     char c;

+ /* if mux do not put in any thing to buffer */
+ if (mon->mux_out) {
+ return;
+ }
+

     for(;;) {

- assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
+ if (mon->outbuf_index >= sizeof(mon->outbuf) - 1) {
+ /* when buffer is full, flush it and retry. If buffer is
bigger, more
+ work can be postponed. */

+            monitor_flush(mon);
+            usleep(1);
+            continue;
+        }
         c = *str++;
         if (c == '\0')
             break;
         if (c == '\n')
             mon->outbuf[mon->outbuf_index++] = '\r';
         mon->outbuf[mon->outbuf_index++] = c;
-        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
-            || c == '\n')
+        if (c == '\n') {
             monitor_flush(mon);
+        }
     }

}

Quoted 19 lines

Commit f628926bb423fa8a7e0b114511400ea9df38b76a changed monitor_flush() to retry on qemu_chr_fe_write() errors. However, the Monitor's output buffer can keep growing while the retry is not issued and this can cause the buffer to overflow.

To reproduce this issue, just start qemu and type on the Monitor:

(qemu) ?

This will cause the assertion to trig.

To fix this problem this commit makes the Monitor buffer dynamic, which means that it can grow as much as needed.

---
  monitor.c | 42 +++++++++++++++++++++++++-----------------
  1 file changed, 25 insertions(+), 17 deletions(-)
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] Monitor: Make output buffer dynamic

Quoted 6 lines · On Wed, 27 Mar 2013 14:45:53 +0800 Wenchao Xia <[email protected]>…

Hi, Luiz
Personally I hope reduce the dynamic allocated buffer which brings
fragments and unexpected memory grow. Instead, how about sacrifice some time to wait output complete, since monitor is not time critical? in this case static buffer's size can decide how many work can be postponded. Following is my suggestion:

I'd be fine with it if you find a good way to postpone work, but sleeping on random timers is not a good way.

Also, QEMU allocates fragments of memory in so many places that I doubt this is an valid argument. You're right that long QMP output can cause unexpected memory growth, but I expected this to be really rare and if this really bother us, we can ask monitor_puts() to flush at, say, every 4096 bytes.

Quoted 57 lines
monitor.c +7 -1
@@ -293,17 +293,28 @@ static void monitor_puts(Monitor *mon, const char
 *str)
 {
     char c;
 
+    /* if mux do not put in any thing to buffer */
+    if (mon->mux_out) {
+        return;
+    }
+
     for(;;) {
-        assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
+        if (mon->outbuf_index >= sizeof(mon->outbuf) - 1) {
+            /* when buffer is full, flush it and retry. If buffer is

bigger, more

monitor.c +7 -2
+               work can be postponed. */
+            monitor_flush(mon);
+            usleep(1);
+            continue;
+        }
         c = *str++;
         if (c == '\0')
             break;
         if (c == '\n')
             mon->outbuf[mon->outbuf_index++] = '\r';
         mon->outbuf[mon->outbuf_index++] = c;
-        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
-            || c == '\n')
+        if (c == '\n') {
             monitor_flush(mon);
+        }
     }
 }
Quoted 20 lines

Commit f628926bb423fa8a7e0b114511400ea9df38b76a changed monitor_flush() to retry on qemu_chr_fe_write() errors. However, the Monitor's output buffer can keep growing while the retry is not issued and this can cause the buffer to overflow.

To reproduce this issue, just start qemu and type on the Monitor:

(qemu) ?

This will cause the assertion to trig.

To fix this problem this commit makes the Monitor buffer dynamic, which means that it can grow as much as needed.

---
  monitor.c | 42 +++++++++++++++++++++++++-----------------
  1 file changed, 25 insertions(+), 17 deletions(-)
#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] Monitor: Make output buffer dynamic

Quoted 10 lines · Luiz Capitulino
Quoted 6 lines · On Wed, 27 Mar 2013 14:45:53 +0800 Wenchao Xia <[email protected]>…

Hi, Luiz
Personally I hope reduce the dynamic allocated buffer which brings
fragments and unexpected memory grow. Instead, how about sacrifice some time to wait output complete, since monitor is not time critical? in this case static buffer's size can decide how many work can be postponded. Following is my suggestion:

Quoted 9 lines

I'd be fine with it if you find a good way to postpone work, but sleeping on random timers is not a good way.

Also, QEMU allocates fragments of memory in so many places that I doubt this is an valid argument. You're right that long QMP output can cause unexpected memory growth, but I expected this to be really rare and if this really bother us, we can ask monitor_puts() to flush at, say, every 4096 bytes.

If the point was async call, you are right it should not sleep. My
thoughts is reducing dynamic allocation when possible. Two alternative
are: make static buffer larger as 4096 bytes and drop the remain if buffer is full, or dynamic increase the buffer size with a fixed number when buffer overflow avoiding dynamic allocation every time. It is a bit overkill, so I am also OK with your solution.

Quoted 59 lines
Quoted 57 lines
monitor.c +7 -1
@@ -293,17 +293,28 @@ static void monitor_puts(Monitor *mon, const char
 *str)
  {
      char c;
 
+    /* if mux do not put in any thing to buffer */
+    if (mon->mux_out) {
+        return;
+    }
+
      for(;;) {
-        assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
+        if (mon->outbuf_index >= sizeof(mon->outbuf) - 1) {
+            /* when buffer is full, flush it and retry. If buffer is

bigger, more

monitor.c +7 -2
+               work can be postponed. */
+            monitor_flush(mon);
+            usleep(1);
+            continue;
+        }
          c = *str++;
          if (c == '\0')
              break;
          if (c == '\n')
              mon->outbuf[mon->outbuf_index++] = '\r';
          mon->outbuf[mon->outbuf_index++] = c;
-        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
-            || c == '\n')
+        if (c == '\n') {
              monitor_flush(mon);
+        }
      }
  }
Quoted 20 lines

Commit f628926bb423fa8a7e0b114511400ea9df38b76a changed monitor_flush() to retry on qemu_chr_fe_write() errors. However, the Monitor's output buffer can keep growing while the retry is not issued and this can cause the buffer to overflow.

To reproduce this issue, just start qemu and type on the Monitor:

(qemu) ?

This will cause the assertion to trig.

To fix this problem this commit makes the Monitor buffer dynamic, which means that it can grow as much as needed.

---
   monitor.c | 42 +++++++++++++++++++++++++-----------------
   1 file changed, 25 insertions(+), 17 deletions(-)
Signature · 3 lines
Best Regards

Wenchao Xia
#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] Monitor: Make output buffer dynamic

In reply to Wenchao Xia
Quoted 3 lines · Wenchao Xia

Hi, Luiz
Personally I hope reduce the dynamic allocated buffer which brings
fragments and unexpected memory grow.

It's a tradeoff. We can reduce the dynamic allocation, by simply reusing the qstring instead of allocation a new one (after complete/partial write). At the cost of wasting some memory.

Given that qstrings grow expotentially I think the dynamic allocation overhead isn't that bad.

Quoted 2 lines

Instead, how about sacrifice
some time to wait output complete, since monitor is not time critical?

The watch added recently will fire when there is space for writing. Right now it will write out the next chunk from the buffer.

In theory we could change the monitor code to continue generating the response when there is more output space, so we don't have to buffer it in the first place. But I think the complexity simply isn't worth it for the memory savings we can gain.

cheers,
Gerd