Patchwork [RFC,v4,07/30] Add SIZE type to qdev properties

login
register
mail settings
Submitter Vasilis Liaskovitis
Date Dec. 18, 2012, 12:41 p.m.
Message ID <1355834518-17989-8-git-send-email-vasilis.liaskovitis@profitbricks.com>
Download mbox | patch
Permalink /patch/207112/
State New
Headers show

Comments

Vasilis Liaskovitis - Dec. 18, 2012, 12:41 p.m.
This patch adds a 'SIZE' type property to qdev.

It will make dimm description more convenient by allowing sizes to be specified
with K,M,G,T prefixes instead of number of bytes e.g.:
-device dimm,id=mem0,size=2G,bus=membus.0

Credits go to Ian Molton for original patch. See:
http://patchwork.ozlabs.org/patch/38835/

Signed-off-by: Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com>
---
 hw/qdev-properties.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/qdev-properties.h |    3 ++
 qemu-option.c        |    2 +-
 qemu-option.h        |    2 +
 4 files changed, 66 insertions(+), 1 deletions(-)
liguang - March 20, 2013, 6:06 a.m.
在 2012-12-18二的 13:41 +0100,Vasilis Liaskovitis写道:
> This patch adds a 'SIZE' type property to qdev.
> 
> It will make dimm description more convenient by allowing sizes to be specified
> with K,M,G,T prefixes instead of number of bytes e.g.:
> -device dimm,id=mem0,size=2G,bus=membus.0
> 
> Credits go to Ian Molton for original patch. See:
> http://patchwork.ozlabs.org/patch/38835/
> 
> Signed-off-by: Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com>
> ---
>  hw/qdev-properties.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  hw/qdev-properties.h |    3 ++
>  qemu-option.c        |    2 +-
>  qemu-option.h        |    2 +
>  4 files changed, 66 insertions(+), 1 deletions(-)
> 
> diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
> index 81d901c..a77f760 100644
> --- a/hw/qdev-properties.c
> +++ b/hw/qdev-properties.c
> @@ -1279,3 +1279,63 @@ void qemu_add_globals(void)
>  {
>      qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);
>  }
> +
> +/* --- 64bit unsigned int 'size' type --- */
> +
> +static void get_size(Object *obj, Visitor *v, void *opaque,
> +                       const char *name, Error **errp)
> +{
> +    DeviceState *dev = DEVICE(obj);
> +    Property *prop = opaque;
> +    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
> +
> +    visit_type_size(v, ptr, name, errp);
> +}
> +
> +static void set_size(Object *obj, Visitor *v, void *opaque,
> +                       const char *name, Error **errp)
> +{
> +    DeviceState *dev = DEVICE(obj);
> +    Property *prop = opaque;
> +    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
> +
> +    if (dev->state != DEV_STATE_CREATED) {
> +        error_set(errp, QERR_PERMISSION_DENIED);
> +        return;
> +    }
> +
> +    visit_type_size(v, ptr, name, errp);
> +}
> +
> +static int parse_size(DeviceState *dev, Property *prop, const char *str)
> +{
> +    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
> +    Error *errp = NULL;
> +
> +    if (str != NULL) {
> +        parse_option_size(prop->name, str, ptr, &errp);
> +    }
> +    assert_no_error(errp);
> +    return 0;
> +}
> +
> +static int print_size(DeviceState *dev, Property *prop, char *dest, size_t len)
> +{
> +    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
> +    char suffixes[] = {'T', 'G', 'M', 'K', 'B'};
> +    int i = 0;
> +    uint64_t div;
> +
> +    for (div = (long int)1 << 40; !(*ptr / div) ; div >>= 10) {
> +        i++;
> +    }
> +    return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
                                    ^^^^^^     ^^^^^^^^^^^^^^^  
> +}
> +

IMHO, you may need (double)(*ptr/div), for type cast is right
associated.

> +PropertyInfo qdev_prop_size = {
> +    .name  = "size",
> +    .parse = parse_size,
> +    .print = print_size,
> +    .get = get_size,
> +    .set = set_size,
> +};
> diff --git a/hw/qdev-properties.h b/hw/qdev-properties.h
> index 5b046ab..0182bef 100644
> --- a/hw/qdev-properties.h
> +++ b/hw/qdev-properties.h
> @@ -14,6 +14,7 @@ extern PropertyInfo qdev_prop_uint64;
>  extern PropertyInfo qdev_prop_hex8;
>  extern PropertyInfo qdev_prop_hex32;
>  extern PropertyInfo qdev_prop_hex64;
> +extern PropertyInfo qdev_prop_size;
>  extern PropertyInfo qdev_prop_string;
>  extern PropertyInfo qdev_prop_chr;
>  extern PropertyInfo qdev_prop_ptr;
> @@ -67,6 +68,8 @@ extern PropertyInfo qdev_prop_pci_host_devaddr;
>      DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
>  #define DEFINE_PROP_HEX64(_n, _s, _f, _d)                       \
>      DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
> +#define DEFINE_PROP_SIZE(_n, _s, _f, _d)                       \
> +    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_size, uint64_t)
>  #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
>      DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
>  
> diff --git a/qemu-option.c b/qemu-option.c
> index 27891e7..38e0a11 100644
> --- a/qemu-option.c
> +++ b/qemu-option.c
> @@ -203,7 +203,7 @@ static void parse_option_number(const char *name, const char *value,
>      }
>  }
>  
> -static void parse_option_size(const char *name, const char *value,
> +void parse_option_size(const char *name, const char *value,
>                                uint64_t *ret, Error **errp)
>  {
>      char *postfix;
> diff --git a/qemu-option.h b/qemu-option.h
> index ca72986..b8ee5b3 100644
> --- a/qemu-option.h
> +++ b/qemu-option.h
> @@ -152,5 +152,7 @@ typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
>  int qemu_opts_print(QemuOpts *opts, void *dummy);
>  int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
>                        int abort_on_failure);
> +void parse_option_size(const char *name, const char *value,
> +                              uint64_t *ret, Error **errp);
>  
>  #endif
Eric Blake - March 20, 2013, 2:24 p.m.
On 03/20/2013 12:06 AM, li guang wrote:

>> +    return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
>                                     ^^^^^^     ^^^^^^^^^^^^^^^  
>> +}
>> +
> 
> IMHO, you may need (double)(*ptr/div), for type cast is right
> associated.

No, the code as written is correct, and your proposal would be wrong.
As written, it is evaluated as:

((double)*ptr) / div

which promotes the division to floating point.  Your proposal would do
the division (*ptr/div) as an integral (truncating) division, and only
then cast the result to double.
liguang - March 21, 2013, 12:39 a.m.
在 2013-03-20三的 08:24 -0600,Eric Blake写道:
> On 03/20/2013 12:06 AM, li guang wrote:
> 
> >> +    return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
> >                                     ^^^^^^     ^^^^^^^^^^^^^^^  
> >> +}
> >> +
> > 
> > IMHO, you may need (double)(*ptr/div), for type cast is right
> > associated.
> 
> No, the code as written is correct, and your proposal would be wrong.
> As written, it is evaluated as:
> 
> ((double)*ptr) / div
> 
> which promotes the division to floating point.  Your proposal would do
> the division (*ptr/div) as an integral (truncating) division, and only
> then cast the result to double.
> 

Yes, you're right,
amazing c type cast!

Patch

diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
index 81d901c..a77f760 100644
--- a/hw/qdev-properties.c
+++ b/hw/qdev-properties.c
@@ -1279,3 +1279,63 @@  void qemu_add_globals(void)
 {
     qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);
 }
+
+/* --- 64bit unsigned int 'size' type --- */
+
+static void get_size(Object *obj, Visitor *v, void *opaque,
+                       const char *name, Error **errp)
+{
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
+
+    visit_type_size(v, ptr, name, errp);
+}
+
+static void set_size(Object *obj, Visitor *v, void *opaque,
+                       const char *name, Error **errp)
+{
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
+
+    if (dev->state != DEV_STATE_CREATED) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+        return;
+    }
+
+    visit_type_size(v, ptr, name, errp);
+}
+
+static int parse_size(DeviceState *dev, Property *prop, const char *str)
+{
+    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
+    Error *errp = NULL;
+
+    if (str != NULL) {
+        parse_option_size(prop->name, str, ptr, &errp);
+    }
+    assert_no_error(errp);
+    return 0;
+}
+
+static int print_size(DeviceState *dev, Property *prop, char *dest, size_t len)
+{
+    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
+    char suffixes[] = {'T', 'G', 'M', 'K', 'B'};
+    int i = 0;
+    uint64_t div;
+
+    for (div = (long int)1 << 40; !(*ptr / div) ; div >>= 10) {
+        i++;
+    }
+    return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
+}
+
+PropertyInfo qdev_prop_size = {
+    .name  = "size",
+    .parse = parse_size,
+    .print = print_size,
+    .get = get_size,
+    .set = set_size,
+};
diff --git a/hw/qdev-properties.h b/hw/qdev-properties.h
index 5b046ab..0182bef 100644
--- a/hw/qdev-properties.h
+++ b/hw/qdev-properties.h
@@ -14,6 +14,7 @@  extern PropertyInfo qdev_prop_uint64;
 extern PropertyInfo qdev_prop_hex8;
 extern PropertyInfo qdev_prop_hex32;
 extern PropertyInfo qdev_prop_hex64;
+extern PropertyInfo qdev_prop_size;
 extern PropertyInfo qdev_prop_string;
 extern PropertyInfo qdev_prop_chr;
 extern PropertyInfo qdev_prop_ptr;
@@ -67,6 +68,8 @@  extern PropertyInfo qdev_prop_pci_host_devaddr;
     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
 #define DEFINE_PROP_HEX64(_n, _s, _f, _d)                       \
     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
+#define DEFINE_PROP_SIZE(_n, _s, _f, _d)                       \
+    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_size, uint64_t)
 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
 
diff --git a/qemu-option.c b/qemu-option.c
index 27891e7..38e0a11 100644
--- a/qemu-option.c
+++ b/qemu-option.c
@@ -203,7 +203,7 @@  static void parse_option_number(const char *name, const char *value,
     }
 }
 
-static void parse_option_size(const char *name, const char *value,
+void parse_option_size(const char *name, const char *value,
                               uint64_t *ret, Error **errp)
 {
     char *postfix;
diff --git a/qemu-option.h b/qemu-option.h
index ca72986..b8ee5b3 100644
--- a/qemu-option.h
+++ b/qemu-option.h
@@ -152,5 +152,7 @@  typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
 int qemu_opts_print(QemuOpts *opts, void *dummy);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure);
+void parse_option_size(const char *name, const char *value,
+                              uint64_t *ret, Error **errp);
 
 #endif