[libvirt] [PATCH v3] qemu: argv: parse qemu commandline memory arguments

Kothapally Madhu Pavan posted 1 patch 6 years, 6 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/1507569389-66819-1-git-send-email-kmp@linux.vnet.ibm.com
src/qemu/qemu_parse_command.c                      | 89 +++++++++++++++++++---
.../qemuargv2xml-mem-scale-maxmemory.args          | 22 ++++++
.../qemuargv2xml-mem-scale-maxmemory.xml           | 38 +++++++++
tests/qemuargv2xmltest.c                           |  1 +
4 files changed, 138 insertions(+), 12 deletions(-)
create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml
[libvirt] [PATCH v3] qemu: argv: parse qemu commandline memory arguments
Posted by Kothapally Madhu Pavan 6 years, 6 months ago
Existing qemuParseCommandLineMem() will parse "-m 4G" format string.
This patch allows it to parse "-m size=8126464k,slots=32,maxmem=33554432k"
format along with existing format. And adds a testcase to validate the changes.

Signed-off-by: Kothapally Madhu Pavan <kmp@linux.vnet.ibm.com>
---
 src/qemu/qemu_parse_command.c                      | 89 +++++++++++++++++++---
 .../qemuargv2xml-mem-scale-maxmemory.args          | 22 ++++++
 .../qemuargv2xml-mem-scale-maxmemory.xml           | 38 +++++++++
 tests/qemuargv2xmltest.c                           |  1 +
 4 files changed, 138 insertions(+), 12 deletions(-)
 create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
 create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml

diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c
index 37e1149..cd2a32a 100644
--- a/src/qemu/qemu_parse_command.c
+++ b/src/qemu/qemu_parse_command.c
@@ -1629,26 +1629,91 @@ static int
 qemuParseCommandLineMem(virDomainDefPtr dom,
                         const char *val)
 {
-    unsigned long long mem;
+    unsigned long long mem = 0;
+    unsigned long long size = 0;
+    unsigned long long maxmem = 0;
+    unsigned int slots = 0;
     char *end;
+    size_t i;
+    int nkws;
+    char **kws;
+    char **vals;
+    int n;
+    int ret = -1;
 
-    if (virStrToLong_ull(val, &end, 10, &mem) < 0) {
+    if (qemuParseKeywords(val, &kws, &vals, &nkws, 1) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot parse memory level '%s'"), val);
-        return -1;
+                       _("cannot parse memory '%s'"), val);
+        goto cleanup;
     }
 
-    if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("cannot scale memory: %s"),
-                       virGetLastErrorMessage());
-        return -1;
+    for (i = 0; i < nkws; i++) {
+        if (vals[i] == NULL) {
+            if (i > 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("cannot parse memory '%s'"), val);
+                goto cleanup;
+            }
+            if (virStrToLong_ull(kws[i], &end, 10, &mem) < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("cannot parse memory level '%s'"), kws[i]);
+                goto cleanup;
+            }
+            if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("cannot scale memory: %s"),
+                               virGetLastErrorMessage());
+                goto cleanup;
+            }
+
+            size = mem;
+
+        } else {
+            if (STREQ(kws[i], "size") || STREQ(kws[i], "maxmem")) {
+                if (virStrToLong_ull(vals[i], &end, 10, &mem) < 0) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("cannot parse memory level '%s'"), vals[i]);
+                    goto cleanup;
+                }
+                if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("cannot scale memory: %s"),
+                                   virGetLastErrorMessage());
+                    goto cleanup;
+                }
+
+                STREQ(kws[i], "size") ? (size = mem) : (maxmem = mem);
+
+            }
+            if (STREQ(kws[i], "slots")) {
+                if (virStrToLong_i(vals[i], &end, 10, &n) < 0) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("cannot parse slots value '%s'"), vals[i]);
+                    goto cleanup;
+                }
+
+               slots = n;
+
+            }
+        }
     }
 
-    virDomainDefSetMemoryTotal(dom, mem / 1024);
-    dom->mem.cur_balloon = mem / 1024;
+    virDomainDefSetMemoryTotal(dom, size / 1024);
+    dom->mem.cur_balloon = size / 1024;
+    dom->mem.memory_slots = slots;
+    dom->mem.max_memory = maxmem / 1024;
 
-    return 0;
+    ret = 0;
+
+ cleanup:
+    for (i = 0; i < nkws; i++) {
+        VIR_FREE(kws[i]);
+        VIR_FREE(vals[i]);
+    }
+    VIR_FREE(kws);
+    VIR_FREE(vals);
+
+    return ret;
 }
 
 
diff --git a/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
new file mode 100644
index 0000000..7bce841
--- /dev/null
+++ b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
@@ -0,0 +1,22 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/home/test \
+USER=test \
+LOGNAME=test \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-i686 \
+-name QEMUGuest1 \
+-S \
+-M pc \
+-m 8G,slots=16,maxmem=16G \
+-smp 1,maxcpus=2,sockets=2,cores=1,threads=1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-nographic \
+-monitor unix:/tmp/test-monitor,server,nowait \
+-no-acpi \
+-boot c \
+-usb \
+-drive file=/dev/HostVG/QEMUGuest1,format=raw,if=ide,bus=0,unit=0 \
+-net none \
+-serial none \
+-parallel none
diff --git a/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml
new file mode 100644
index 0000000..44431d8
--- /dev/null
+++ b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml
@@ -0,0 +1,38 @@
+<domain type='qemu'>
+  <name>QEMUGuest1</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <maxMemory slots='16' unit='KiB'>16777216</maxMemory>
+  <memory unit='KiB'>8388608</memory>
+  <currentMemory unit='KiB'>8388608</currentMemory>
+  <vcpu placement='static' current='1'>2</vcpu>
+  <os>
+    <type arch='i686' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu>
+    <topology sockets='2' cores='1' threads='1'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu-system-i686</emulator>
+    <disk type='block' device='disk'>
+      <driver name='qemu' type='raw'/>
+      <source dev='/dev/HostVG/QEMUGuest1'/>
+      <target dev='hda' bus='ide'/>
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
+    </disk>
+    <controller type='usb' index='0'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
+    </controller>
+    <controller type='pci' index='0' model='pci-root'/>
+    <controller type='ide' index='0'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
+    </controller>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <memballoon model='none'/>
+  </devices>
+</domain>
diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c
index 1adbcfe..e35726e 100644
--- a/tests/qemuargv2xmltest.c
+++ b/tests/qemuargv2xmltest.c
@@ -265,6 +265,7 @@ mymain(void)
     DO_TEST("hostdev-pci-address");
 
     DO_TEST("mem-scale");
+    DO_TEST("mem-scale-maxmemory");
     DO_TEST("smp");
 
     DO_TEST("hyperv");
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v3] qemu: argv: parse qemu commandline memory arguments
Posted by John Ferlan 6 years, 6 months ago

On 10/09/2017 01:16 PM, Kothapally Madhu Pavan wrote:
> Existing qemuParseCommandLineMem() will parse "-m 4G" format string.
> This patch allows it to parse "-m size=8126464k,slots=32,maxmem=33554432k"
> format along with existing format. And adds a testcase to validate the changes.
> 
> Signed-off-by: Kothapally Madhu Pavan <kmp@linux.vnet.ibm.com>
> ---
>  src/qemu/qemu_parse_command.c                      | 89 +++++++++++++++++++---
>  .../qemuargv2xml-mem-scale-maxmemory.args          | 22 ++++++
>  .../qemuargv2xml-mem-scale-maxmemory.xml           | 38 +++++++++
>  tests/qemuargv2xmltest.c                           |  1 +
>  4 files changed, 138 insertions(+), 12 deletions(-)
>  create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
>  create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml
> 
> diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c
> index 37e1149..cd2a32a 100644
> --- a/src/qemu/qemu_parse_command.c
> +++ b/src/qemu/qemu_parse_command.c
> @@ -1629,26 +1629,91 @@ static int
>  qemuParseCommandLineMem(virDomainDefPtr dom,
>                          const char *val)
>  {
> -    unsigned long long mem;
> +    unsigned long long mem = 0;
> +    unsigned long long size = 0;
> +    unsigned long long maxmem = 0;
> +    unsigned int slots = 0;
>      char *end;
> +    size_t i;
> +    int nkws;
> +    char **kws;
> +    char **vals;
> +    int n;
> +    int ret = -1;
>  
> -    if (virStrToLong_ull(val, &end, 10, &mem) < 0) {
> +    if (qemuParseKeywords(val, &kws, &vals, &nkws, 1) < 0) {
>          virReportError(VIR_ERR_INTERNAL_ERROR,
> -                       _("cannot parse memory level '%s'"), val);
> -        return -1;
> +                       _("cannot parse memory '%s'"), val);
> +        goto cleanup;
>      }
>  
> -    if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) {
> -        virReportError(VIR_ERR_INTERNAL_ERROR,
> -                       _("cannot scale memory: %s"),
> -                       virGetLastErrorMessage());
> -        return -1;
> +    for (i = 0; i < nkws; i++) {
> +        if (vals[i] == NULL) {
> +            if (i > 0) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("cannot parse memory '%s'"), val);
> +                goto cleanup;
> +            }
> +            if (virStrToLong_ull(kws[i], &end, 10, &mem) < 0) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("cannot parse memory level '%s'"), kws[i]);

I think this should have been "memory value" not "memory level"... IOW:
If someone provided "-m size" they'd get "cannot parse memory value 'size'"

Since we're adjusting the logic, I'll fix the syntax for both...


> +                goto cleanup;
> +            }
> +            if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("cannot scale memory: %s"),
> +                               virGetLastErrorMessage());
> +                goto cleanup;
> +            }
> +
> +            size = mem;
> +
> +        } else {
> +            if (STREQ(kws[i], "size") || STREQ(kws[i], "maxmem")) {
> +                if (virStrToLong_ull(vals[i], &end, 10, &mem) < 0) {
> +                    virReportError(VIR_ERR_INTERNAL_ERROR,
> +                                   _("cannot parse memory level '%s'"), vals[i]);

e.g. here too.

> +                    goto cleanup;
> +                }
> +                if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) {
> +                    virReportError(VIR_ERR_INTERNAL_ERROR,
> +                                   _("cannot scale memory: %s"),
> +                                   virGetLastErrorMessage());
> +                    goto cleanup;
> +                }
> +
> +                STREQ(kws[i], "size") ? (size = mem) : (maxmem = mem);
> +
> +            }
> +            if (STREQ(kws[i], "slots")) {
> +                if (virStrToLong_i(vals[i], &end, 10, &n) < 0) {
> +                    virReportError(VIR_ERR_INTERNAL_ERROR,
> +                                   _("cannot parse slots value '%s'"), vals[i]);
> +                    goto cleanup;
> +                }
> +
> +               slots = n;
> +
> +            }
> +        }
>      }
>  
> -    virDomainDefSetMemoryTotal(dom, mem / 1024);
> -    dom->mem.cur_balloon = mem / 1024;
> +    virDomainDefSetMemoryTotal(dom, size / 1024);
> +    dom->mem.cur_balloon = size / 1024;
> +    dom->mem.memory_slots = slots;
> +    dom->mem.max_memory = maxmem / 1024;
>  
> -    return 0;
> +    ret = 0;
> +
> + cleanup:
> +    for (i = 0; i < nkws; i++) {
> +        VIR_FREE(kws[i]);
> +        VIR_FREE(vals[i]);
> +    }
> +    VIR_FREE(kws);
> +    VIR_FREE(vals);

As a followup - why not move "qemuFreeKeywords" into qemu_parse_command
and then make it callable from qemu_monitor_json as well... Then for
each of the consumers in qemu_parse_command, call it rather than inline
code in multiple places...

Just a thought...

> +
> +    return ret;
>  }
>  
>  
> diff --git a/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
> new file mode 100644
> index 0000000..7bce841
> --- /dev/null
> +++ b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.args
> @@ -0,0 +1,22 @@
> +LC_ALL=C \
> +PATH=/bin \
> +HOME=/home/test \
> +USER=test \
> +LOGNAME=test \
> +QEMU_AUDIO_DRV=none \
> +/usr/bin/qemu-system-i686 \
> +-name QEMUGuest1 \
> +-S \
> +-M pc \
> +-m 8G,slots=16,maxmem=16G \

In order to do what you really set out to do from the commit message
this should be "-m size=8G,slots=16,maxmem=16G"

I've made that change locally before pushing...

Reviewed-by: John Ferlan <jferlan@redhat.com>

I'll push in a bit.

John

> +-smp 1,maxcpus=2,sockets=2,cores=1,threads=1 \
> +-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
> +-nographic \
> +-monitor unix:/tmp/test-monitor,server,nowait \
> +-no-acpi \
> +-boot c \
> +-usb \
> +-drive file=/dev/HostVG/QEMUGuest1,format=raw,if=ide,bus=0,unit=0 \
> +-net none \
> +-serial none \
> +-parallel none
> diff --git a/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml
> new file mode 100644
> index 0000000..44431d8
> --- /dev/null
> +++ b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale-maxmemory.xml
> @@ -0,0 +1,38 @@
> +<domain type='qemu'>
> +  <name>QEMUGuest1</name>
> +  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
> +  <maxMemory slots='16' unit='KiB'>16777216</maxMemory>
> +  <memory unit='KiB'>8388608</memory>
> +  <currentMemory unit='KiB'>8388608</currentMemory>
> +  <vcpu placement='static' current='1'>2</vcpu>
> +  <os>
> +    <type arch='i686' machine='pc'>hvm</type>
> +    <boot dev='hd'/>
> +  </os>
> +  <cpu>
> +    <topology sockets='2' cores='1' threads='1'/>
> +  </cpu>
> +  <clock offset='utc'/>
> +  <on_poweroff>destroy</on_poweroff>
> +  <on_reboot>restart</on_reboot>
> +  <on_crash>destroy</on_crash>
> +  <devices>
> +    <emulator>/usr/bin/qemu-system-i686</emulator>
> +    <disk type='block' device='disk'>
> +      <driver name='qemu' type='raw'/>
> +      <source dev='/dev/HostVG/QEMUGuest1'/>
> +      <target dev='hda' bus='ide'/>
> +      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
> +    </disk>
> +    <controller type='usb' index='0'>
> +      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
> +    </controller>
> +    <controller type='pci' index='0' model='pci-root'/>
> +    <controller type='ide' index='0'>
> +      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
> +    </controller>
> +    <input type='mouse' bus='ps2'/>
> +    <input type='keyboard' bus='ps2'/>
> +    <memballoon model='none'/>
> +  </devices>
> +</domain>
> diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c
> index 1adbcfe..e35726e 100644
> --- a/tests/qemuargv2xmltest.c
> +++ b/tests/qemuargv2xmltest.c
> @@ -265,6 +265,7 @@ mymain(void)
>      DO_TEST("hostdev-pci-address");
>  
>      DO_TEST("mem-scale");
> +    DO_TEST("mem-scale-maxmemory");
>      DO_TEST("smp");
>  
>      DO_TEST("hyperv");
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list