[libvirt] [PATCH v2 0/2] qemu: parse: Introduce qemuParseCommandLineMem

This series moves the parsing of qemu -m memory into a separate function and adds suffix support for the -m option. The first patch introduces the function qemuParseCommandLineMem, and the second patch handles suffixes for the -m values and adds a test for the same. Nishith Shah (2): qemu: parse: Use qemuParseCommandLineMem for -m memory qemu: parse: Handle suffixes for -m memory src/qemu/qemu_parse_command.c | 38 ++++++++++++++++++---- tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args | 22 +++++++++++++ tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml | 33 +++++++++++++++++++ tests/qemuargv2xmltest.c | 1 + 4 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml -- 2.1.4

Move the parsing of -m memory to a new function, qemuParseCommandLineMem Signed-off-by: Nishith Shah <nishithshah.2211@gmail.com> --- src/qemu/qemu_parse_command.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c index e30586f..334dcf8 100644 --- a/src/qemu/qemu_parse_command.c +++ b/src/qemu/qemu_parse_command.c @@ -1633,6 +1633,28 @@ qemuParseCommandLineCPU(virDomainDefPtr dom, static int +qemuParseCommandLineMem(virDomainDefPtr dom, + const char *val) +{ + unsigned long long mem; + char *end; + if (virStrToLong_ull(val, &end, 10, &mem) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("cannot parse memory level '%s'"), val); + goto error; + } + + virDomainDefSetMemoryTotal(dom, mem * 1024); + dom->mem.cur_balloon = mem * 1024; + + return 0; + + error: + return -1; +} + + +static int qemuParseCommandLineSmp(virDomainDefPtr dom, const char *val) { @@ -1869,15 +1891,9 @@ qemuParseCommandLine(virCapsPtr caps, } else if (STREQ(arg, "-sdl")) { have_sdl = true; } else if (STREQ(arg, "-m")) { - int mem; WANT_VALUE(); - if (virStrToLong_i(val, NULL, 10, &mem) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, \ - _("cannot parse memory level '%s'"), val); + if (qemuParseCommandLineMem(def, val) < 0) goto error; - } - virDomainDefSetMemoryTotal(def, mem * 1024); - def->mem.cur_balloon = mem * 1024; } else if (STREQ(arg, "-smp")) { WANT_VALUE(); if (qemuParseCommandLineSmp(def, val) < 0) -- 2.1.4

On 05/18/2016 02:36 AM, Nishith Shah wrote:
Move the parsing of -m memory to a new function, qemuParseCommandLineMem
Signed-off-by: Nishith Shah <nishithshah.2211@gmail.com> --- src/qemu/qemu_parse_command.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c index e30586f..334dcf8 100644 --- a/src/qemu/qemu_parse_command.c +++ b/src/qemu/qemu_parse_command.c @@ -1633,6 +1633,28 @@ qemuParseCommandLineCPU(virDomainDefPtr dom,
static int +qemuParseCommandLineMem(virDomainDefPtr dom, + const char *val) +{ + unsigned long long mem; + char *end; + if (virStrToLong_ull(val, &end, 10, &mem) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("cannot parse memory level '%s'"), val); + goto error; + } + + virDomainDefSetMemoryTotal(dom, mem * 1024); + dom->mem.cur_balloon = mem * 1024; + + return 0; + + error: + return -1; +} +
Typically when we use the 'goto error' pattern, what we do is: int ret = -1; if (condition) goto error; ret = 0; error: return ret; So there's only one 'return' That said, in this function there isn't any special error handling, so I suggest dropping the 'error' label entirely and just 'return -1' at the goto error calls Thanks, Cole

On Thu, May 19, 2016 at 08:08:39AM -0400, Cole Robinson wrote:
On 05/18/2016 02:36 AM, Nishith Shah wrote:
+ virDomainDefSetMemoryTotal(dom, mem * 1024); + dom->mem.cur_balloon = mem * 1024; + + return 0; + + error: + return -1; +} +
Typically when we use the 'goto error' pattern, what we do is:
int ret = -1;
if (condition) goto error;
ret = 0; error: return ret;
So there's only one 'return'
When the path is taken both on error and success, 'cleanup' is the preferred label name: http://libvirt.org/hacking.html#goto Jan

On 05/19/2016 08:20 AM, Ján Tomko wrote:
On Thu, May 19, 2016 at 08:08:39AM -0400, Cole Robinson wrote:
On 05/18/2016 02:36 AM, Nishith Shah wrote:
+ virDomainDefSetMemoryTotal(dom, mem * 1024); + dom->mem.cur_balloon = mem * 1024; + + return 0; + + error: + return -1; +} +
Typically when we use the 'goto error' pattern, what we do is:
int ret = -1;
if (condition) goto error;
ret = 0; error: return ret;
So there's only one 'return'
When the path is taken both on error and success, 'cleanup' is the preferred label name: http://libvirt.org/hacking.html#goto
Thanks for the correction, I need to make a note about that... - Cole

According to QEMU docs, the '-m' option for specifying RAM is by default in MiB, and a suffix of "M" or "G" may be passed for values in MiB and GiB respectively. This commit adds support and a test for the same. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=812295 Signed-off-by: Nishith Shah <nishithshah.2211@gmail.com> --- src/qemu/qemu_parse_command.c | 12 ++++++-- tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args | 22 +++++++++++++++ tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml | 33 ++++++++++++++++++++++ tests/qemuargv2xmltest.c | 1 + 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c index 334dcf8..f027d51 100644 --- a/src/qemu/qemu_parse_command.c +++ b/src/qemu/qemu_parse_command.c @@ -1638,14 +1638,22 @@ qemuParseCommandLineMem(virDomainDefPtr dom, { unsigned long long mem; char *end; + if (virStrToLong_ull(val, &end, 10, &mem) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("cannot parse memory level '%s'"), val); goto error; } - virDomainDefSetMemoryTotal(dom, mem * 1024); - dom->mem.cur_balloon = mem * 1024; + if (virScaleInteger(&mem, end, 1024*1024, ULLONG_MAX) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("cannot scale memory: %s"), + virGetLastErrorMessage()); + goto error; + } + + virDomainDefSetMemoryTotal(dom, mem / 1024); + dom->mem.cur_balloon = mem / 1024; return 0; diff --git a/tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args new file mode 100644 index 0000000..90da94d --- /dev/null +++ b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args @@ -0,0 +1,22 @@ +LC_ALL=C \ +PATH=/bin \ +HOME=/home/test \ +USER=test \ +LOGNAME=test \ +QEMU_AUDIO_DRV=none \ +/usr/bin/qemu \ +-name QEMUGuest1 \ +-S \ +-M pc \ +-m 2G \ +-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.xml b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml new file mode 100644 index 0000000..8002e00 --- /dev/null +++ b/tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml @@ -0,0 +1,33 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>2097152</memory> + <currentMemory unit='KiB'>2097152</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</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'/> + <controller type='pci' index='0' model='pci-root'/> + <controller type='ide' index='0'/> + <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 48c83ea..30fa310 100644 --- a/tests/qemuargv2xmltest.c +++ b/tests/qemuargv2xmltest.c @@ -264,6 +264,7 @@ mymain(void) DO_TEST("hostdev-pci-address"); + DO_TEST("mem-scale"); DO_TEST("smp"); DO_TEST("hyperv"); -- 2.1.4

On 05/18/2016 02:36 AM, Nishith Shah wrote:
According to QEMU docs, the '-m' option for specifying RAM is by default in MiB, and a suffix of "M" or "G" may be passed for values in MiB and GiB respectively. This commit adds support and a test for the same.
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=812295
Signed-off-by: Nishith Shah <nishithshah.2211@gmail.com> --- src/qemu/qemu_parse_command.c | 12 ++++++-- tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args | 22 +++++++++++++++ tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml | 33 ++++++++++++++++++++++ tests/qemuargv2xmltest.c | 1 + 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale.args create mode 100644 tests/qemuargv2xmldata/qemuargv2xml-mem-scale.xml
diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c index 334dcf8..f027d51 100644 --- a/src/qemu/qemu_parse_command.c +++ b/src/qemu/qemu_parse_command.c @@ -1638,14 +1638,22 @@ qemuParseCommandLineMem(virDomainDefPtr dom, { unsigned long long mem; char *end; +
This whitespace change should be part of patch #1 Otherwise the patches look good to me, bring on v3 :) Thanks, Cole

On Thu, May 19, 2016 at 5:40 PM, Cole Robinson <crobinso@redhat.com> wrote:
On 05/18/2016 02:36 AM, Nishith Shah wrote:
diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c index 334dcf8..f027d51 100644 --- a/src/qemu/qemu_parse_command.c +++ b/src/qemu/qemu_parse_command.c @@ -1638,14 +1638,22 @@ qemuParseCommandLineMem(virDomainDefPtr dom, { unsigned long long mem; char *end; +
This whitespace change should be part of patch #1
Otherwise the patches look good to me, bring on v3 :)
Thanks, Cole
Working on it :)
participants (3)
-
Cole Robinson
-
Ján Tomko
-
Nishith Shah