[libvirt] [dbus PATCH v2 04/22] Implement MemoryStats for Domain Interface

Katerina Koukiou posted 22 patches 7 years, 1 month ago
There is a newer version of this series
[libvirt] [dbus PATCH v2 04/22] Implement MemoryStats for Domain Interface
Posted by Katerina Koukiou 7 years, 1 month ago
This method is not tested for now since the test driver
doesn't support this API.

Signed-off-by: Katerina Koukiou <kkoukiou@redhat.com>
---
 data/org.libvirt.Domain.xml |  7 ++++++
 src/domain.c                | 52 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/data/org.libvirt.Domain.xml b/data/org.libvirt.Domain.xml
index dbeafce..6795d30 100644
--- a/data/org.libvirt.Domain.xml
+++ b/data/org.libvirt.Domain.xml
@@ -69,6 +69,13 @@
       <arg name="flags" type="u" direction="in"/>
       <arg name="xml" type="s" direction="out"/>
     </method>
+    <method name="MemoryStats">
+      <annotation name="org.gtk.GDBus.DocString"
+        value="See https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainMemoryStats"/>
+      <arg name="nr_stats" type="u" direction="in"/>
+      <arg name="flags" type="u" direction="in"/>
+      <arg name="stats" type="a{st}" direction="out"/>
+    </method>
     <method name="Reboot">
       <annotation name="org.gtk.GDBus.DocString"
         value="See https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainReboot"/>
diff --git a/src/domain.c b/src/domain.c
index 9b3de57..59118b9 100644
--- a/src/domain.c
+++ b/src/domain.c
@@ -17,6 +17,24 @@ VIRT_DBUS_ENUM_IMPL(virtDBusDomainMemoryStat,
                     "usable",
                     "last_update")
 
+static GVariant *
+virtDBusDomainMemoryStatsToGVariant(virDomainMemoryStatPtr stats,
+                                    gint nr_stats)
+{
+    GVariantBuilder builder;
+
+    g_variant_builder_init(&builder, G_VARIANT_TYPE("a{st}"));
+
+    for (gint i = 0; i < nr_stats; i++) {
+        const gchar *memoryStat = virtDBusDomainMemoryStatTypeToString(stats[i].tag);
+        if (!memoryStat)
+            return 0;
+        g_variant_builder_add(&builder, "{st}", memoryStat, stats[i].val);
+    }
+
+    return g_variant_builder_end(&builder);
+}
+
 static virDomainPtr
 virtDBusDomainGetVirDomain(virtDBusConnect *connect,
                            const gchar *objectPath,
@@ -412,6 +430,39 @@ virtDBusDomainGetXMLDesc(GVariant *inArgs,
     *outArgs = g_variant_new("(s)", xml);
 }
 
+static void
+virtDBusDomainMemoryStats(GVariant *inArgs,
+                          GUnixFDList *inFDs G_GNUC_UNUSED,
+                          const gchar *objectPath,
+                          gpointer userData,
+                          GVariant **outArgs,
+                          GUnixFDList **outFDs G_GNUC_UNUSED,
+                          GError **error)
+{
+    virtDBusConnect *connect = userData;
+    g_autoptr(virDomain) domain = NULL;
+    g_autofree virDomainMemoryStatPtr stats = NULL;
+    guint max_stats;
+    gint nr_stats;
+    guint flags;
+    GVariant *gstats;
+
+    g_variant_get(inArgs, "(uu)", &max_stats, &flags);
+
+    domain = virtDBusDomainGetVirDomain(connect, objectPath, error);
+    if (!domain)
+        return;
+
+    stats = g_new0(virDomainMemoryStatStruct, max_stats);
+    nr_stats = virDomainMemoryStats(domain, stats, max_stats, flags);
+    if (nr_stats == -1)
+        return virtDBusUtilSetLastVirtError(error);
+
+    gstats = virtDBusDomainMemoryStatsToGVariant(stats, nr_stats);
+
+    *outArgs = g_variant_new_tuple(&gstats, 1);
+}
+
 static void
 virtDBusDomainReboot(GVariant *inArgs,
                      GUnixFDList *inFDs G_GNUC_UNUSED,
@@ -565,6 +616,7 @@ static virtDBusGDBusMethodTable virtDBusDomainMethodTable[] = {
     { "GetStats", virtDBusDomainGetStats },
     { "GetVcpus", virtDBusDomainGetVcpus },
     { "GetXMLDesc", virtDBusDomainGetXMLDesc },
+    { "MemoryStats", virtDBusDomainMemoryStats },
     { "Reboot", virtDBusDomainReboot },
     { "Reset", virtDBusDomainReset },
     { "Resume", virtDBusDomainResume },
-- 
2.15.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [dbus PATCH v2 04/22] Implement MemoryStats for Domain Interface
Posted by Pavel Hrdina 7 years, 1 month ago
On Thu, Apr 12, 2018 at 04:32:43PM +0200, Katerina Koukiou wrote:
> This method is not tested for now since the test driver
> doesn't support this API.
> 
> Signed-off-by: Katerina Koukiou <kkoukiou@redhat.com>
> ---
>  data/org.libvirt.Domain.xml |  7 ++++++
>  src/domain.c                | 52 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 59 insertions(+)
> 
> diff --git a/data/org.libvirt.Domain.xml b/data/org.libvirt.Domain.xml
> index dbeafce..6795d30 100644
> --- a/data/org.libvirt.Domain.xml
> +++ b/data/org.libvirt.Domain.xml
> @@ -69,6 +69,13 @@
>        <arg name="flags" type="u" direction="in"/>
>        <arg name="xml" type="s" direction="out"/>
>      </method>
> +    <method name="MemoryStats">
> +      <annotation name="org.gtk.GDBus.DocString"
> +        value="See https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainMemoryStats"/>
> +      <arg name="nr_stats" type="u" direction="in"/>
> +      <arg name="flags" type="u" direction="in"/>
> +      <arg name="stats" type="a{st}" direction="out"/>
> +    </method>
>      <method name="Reboot">
>        <annotation name="org.gtk.GDBus.DocString"
>          value="See https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainReboot"/>
> diff --git a/src/domain.c b/src/domain.c
> index 9b3de57..59118b9 100644
> --- a/src/domain.c
> +++ b/src/domain.c
> @@ -17,6 +17,24 @@ VIRT_DBUS_ENUM_IMPL(virtDBusDomainMemoryStat,
>                      "usable",
>                      "last_update")
>  
> +static GVariant *
> +virtDBusDomainMemoryStatsToGVariant(virDomainMemoryStatPtr stats,
> +                                    gint nr_stats)
> +{
> +    GVariantBuilder builder;
> +
> +    g_variant_builder_init(&builder, G_VARIANT_TYPE("a{st}"));
> +
> +    for (gint i = 0; i < nr_stats; i++) {
> +        const gchar *memoryStat = virtDBusDomainMemoryStatTypeToString(stats[i].tag);
> +        if (!memoryStat)
> +            return 0;
> +        g_variant_builder_add(&builder, "{st}", memoryStat, stats[i].val);
> +    }
> +
> +    return g_variant_builder_end(&builder);
> +}
> +
>  static virDomainPtr
>  virtDBusDomainGetVirDomain(virtDBusConnect *connect,
>                             const gchar *objectPath,
> @@ -412,6 +430,39 @@ virtDBusDomainGetXMLDesc(GVariant *inArgs,
>      *outArgs = g_variant_new("(s)", xml);
>  }
>  
> +static void
> +virtDBusDomainMemoryStats(GVariant *inArgs,
> +                          GUnixFDList *inFDs G_GNUC_UNUSED,
> +                          const gchar *objectPath,
> +                          gpointer userData,
> +                          GVariant **outArgs,
> +                          GUnixFDList **outFDs G_GNUC_UNUSED,
> +                          GError **error)
> +{
> +    virtDBusConnect *connect = userData;
> +    g_autoptr(virDomain) domain = NULL;
> +    g_autofree virDomainMemoryStatPtr stats = NULL;
> +    guint max_stats;
> +    gint nr_stats;
> +    guint flags;
> +    GVariant *gstats;
> +
> +    g_variant_get(inArgs, "(uu)", &max_stats, &flags);
> +
> +    domain = virtDBusDomainGetVirDomain(connect, objectPath, error);
> +    if (!domain)
> +        return;
> +
> +    stats = g_new0(virDomainMemoryStatStruct, max_stats);

I think that we can make the life of libvirt-dbus users easier like
libvirt-python does and don't ask for the 'max_stats' at all.  The only
input arg would be 'flags'.

There we can allocate stats to VIR_DOMAIN_MEMORY_STAT_NR size which
would always request all possible stats.

Pavel
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [dbus PATCH v2 04/22] Implement MemoryStats for Domain Interface
Posted by Pavel Hrdina 7 years, 1 month ago
On Thu, Apr 12, 2018 at 04:32:43PM +0200, Katerina Koukiou wrote:
> This method is not tested for now since the test driver
> doesn't support this API.
> 
> Signed-off-by: Katerina Koukiou <kkoukiou@redhat.com>
> ---
>  data/org.libvirt.Domain.xml |  7 ++++++
>  src/domain.c                | 52 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 59 insertions(+)

[...]

> +static void
> +virtDBusDomainMemoryStats(GVariant *inArgs,
> +                          GUnixFDList *inFDs G_GNUC_UNUSED,
> +                          const gchar *objectPath,
> +                          gpointer userData,
> +                          GVariant **outArgs,
> +                          GUnixFDList **outFDs G_GNUC_UNUSED,
> +                          GError **error)
> +{
> +    virtDBusConnect *connect = userData;
> +    g_autoptr(virDomain) domain = NULL;
> +    g_autofree virDomainMemoryStatPtr stats = NULL;
> +    guint max_stats;
> +    gint nr_stats;
> +    guint flags;
> +    GVariant *gstats;
> +
> +    g_variant_get(inArgs, "(uu)", &max_stats, &flags);
> +
> +    domain = virtDBusDomainGetVirDomain(connect, objectPath, error);
> +    if (!domain)
> +        return;
> +
> +    stats = g_new0(virDomainMemoryStatStruct, max_stats);
> +    nr_stats = virDomainMemoryStats(domain, stats, max_stats, flags);
> +    if (nr_stats == -1)

One more thing, 'nr_stats < 0' is preferred form.

Pavel
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [dbus PATCH v2 04/22] Implement MemoryStats for Domain Interface
Posted by Pavel Hrdina 7 years, 1 month ago
On Thu, Apr 12, 2018 at 04:32:43PM +0200, Katerina Koukiou wrote:
> This method is not tested for now since the test driver
> doesn't support this API.
> 
> Signed-off-by: Katerina Koukiou <kkoukiou@redhat.com>
> ---
>  data/org.libvirt.Domain.xml |  7 ++++++
>  src/domain.c                | 52 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 59 insertions(+)
> 
> diff --git a/data/org.libvirt.Domain.xml b/data/org.libvirt.Domain.xml
> index dbeafce..6795d30 100644
> --- a/data/org.libvirt.Domain.xml
> +++ b/data/org.libvirt.Domain.xml
> @@ -69,6 +69,13 @@
>        <arg name="flags" type="u" direction="in"/>
>        <arg name="xml" type="s" direction="out"/>
>      </method>
> +    <method name="MemoryStats">
> +      <annotation name="org.gtk.GDBus.DocString"
> +        value="See https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainMemoryStats"/>
> +      <arg name="nr_stats" type="u" direction="in"/>
> +      <arg name="flags" type="u" direction="in"/>
> +      <arg name="stats" type="a{st}" direction="out"/>
> +    </method>
>      <method name="Reboot">
>        <annotation name="org.gtk.GDBus.DocString"
>          value="See https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainReboot"/>
> diff --git a/src/domain.c b/src/domain.c
> index 9b3de57..59118b9 100644
> --- a/src/domain.c
> +++ b/src/domain.c
> @@ -17,6 +17,24 @@ VIRT_DBUS_ENUM_IMPL(virtDBusDomainMemoryStat,
>                      "usable",
>                      "last_update")
>  
> +static GVariant *
> +virtDBusDomainMemoryStatsToGVariant(virDomainMemoryStatPtr stats,
> +                                    gint nr_stats)
> +{
> +    GVariantBuilder builder;
> +
> +    g_variant_builder_init(&builder, G_VARIANT_TYPE("a{st}"));
> +
> +    for (gint i = 0; i < nr_stats; i++) {
> +        const gchar *memoryStat = virtDBusDomainMemoryStatTypeToString(stats[i].tag);
> +        if (!memoryStat)
> +            return 0;

The return value is pointer so it should be NULL.

> +        g_variant_builder_add(&builder, "{st}", memoryStat, stats[i].val);
> +    }
> +
> +    return g_variant_builder_end(&builder);
> +}
> +
>  static virDomainPtr
>  virtDBusDomainGetVirDomain(virtDBusConnect *connect,
>                             const gchar *objectPath,
> @@ -412,6 +430,39 @@ virtDBusDomainGetXMLDesc(GVariant *inArgs,
>      *outArgs = g_variant_new("(s)", xml);
>  }
>  
> +static void
> +virtDBusDomainMemoryStats(GVariant *inArgs,
> +                          GUnixFDList *inFDs G_GNUC_UNUSED,
> +                          const gchar *objectPath,
> +                          gpointer userData,
> +                          GVariant **outArgs,
> +                          GUnixFDList **outFDs G_GNUC_UNUSED,
> +                          GError **error)
> +{
> +    virtDBusConnect *connect = userData;
> +    g_autoptr(virDomain) domain = NULL;
> +    g_autofree virDomainMemoryStatPtr stats = NULL;
> +    guint max_stats;
> +    gint nr_stats;
> +    guint flags;
> +    GVariant *gstats;
> +
> +    g_variant_get(inArgs, "(uu)", &max_stats, &flags);
> +
> +    domain = virtDBusDomainGetVirDomain(connect, objectPath, error);
> +    if (!domain)
> +        return;
> +
> +    stats = g_new0(virDomainMemoryStatStruct, max_stats);
> +    nr_stats = virDomainMemoryStats(domain, stats, max_stats, flags);
> +    if (nr_stats == -1)
> +        return virtDBusUtilSetLastVirtError(error);
> +
> +    gstats = virtDBusDomainMemoryStatsToGVariant(stats, nr_stats);

This can be NULL, I think that virtDBusDomainMemoryStatsToGVariant needs
to take one more parameter, 'GError **error' and in case of error it
should return NULL and set an error message.

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