[libvirt] [libvirt-glib 1/2] API to get node information about the connection

From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> Mostly just a wrapper around virNodeGetInfo() and virNodeInfo struct. --- libvirt-gobject/libvirt-gobject-connection.c | 49 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 17 +++++++++ libvirt-gobject/libvirt-gobject.sym | 1 + 3 files changed, 67 insertions(+), 0 deletions(-) diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index cb19e9d..c2691f7 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -71,6 +71,21 @@ gvir_connection_error_quark(void) return g_quark_from_static_string("gvir-connection"); } +static GVirNodeInfo * +gvir_node_info_copy(GVirNodeInfo *info) +{ + return g_slice_dup(GVirNodeInfo, info); +} + +static void +gvir_node_info_free(GVirNodeInfo *info) +{ + g_slice_free(GVirNodeInfo, info); +} + +G_DEFINE_BOXED_TYPE(GVirNodeInfo, gvir_node_info, + gvir_node_info_copy, gvir_node_info_free) + static void gvir_connection_get_property(GObject *object, guint prop_id, GValue *value, @@ -1338,3 +1353,37 @@ GVirStoragePool *gvir_connection_create_storage_pool return g_object_ref(pool); } + +/** + * gvir_connection_get_node_info: + * @conn: the connection + * @err: return location for any #GError + * + * Returns: (transfer full): the info + */ +GVirNodeInfo *gvir_connection_get_node_info(GVirConnection *conn, + GError **err) +{ + GVirConnectionPrivate *priv = conn->priv; + virNodeInfo info; + GVirNodeInfo *ret; + + if (virNodeGetInfo(priv->conn, &info) < 0) { + gvir_set_error_literal(err, GVIR_CONNECTION_ERROR, + 0, + "Unable to get node info"); + return NULL; + } + + ret = g_slice_new(GVirNodeInfo); + g_memmove (ret->model, info.model, sizeof (ret->model)); + ret->memory = info.memory; + ret->cpus = info.cpus; + ret->mhz = info.mhz; + ret->nodes = info.nodes; + ret->sockets = info.sockets; + ret->cores = info.cores; + ret->threads = info.threads; + + return ret; +} diff --git a/libvirt-gobject/libvirt-gobject-connection.h b/libvirt-gobject/libvirt-gobject-connection.h index 477a0c3..3cc60a2 100644 --- a/libvirt-gobject/libvirt-gobject-connection.h +++ b/libvirt-gobject/libvirt-gobject-connection.h @@ -38,6 +38,19 @@ G_BEGIN_DECLS #define GVIR_TYPE_CONNECTION_HANDLE (gvir_connection_handle_get_type ()) +typedef struct _GVirNodeInfo GVirNodeInfo; +struct _GVirNodeInfo +{ + gchar model[32]; /* string indicating the CPU model */ + gulong memory; /* memory size in kilobytes */ + guint cpus; /* the number of active CPUs */ + guint mhz; /* expected CPU frequency */ + guint nodes; /* the number of NUMA cell, 1 for unusual NUMA topologies or uniform memo */ + guint sockets; /* number of CPU sockets per node if nodes > 1, total number of CPU socke */ + guint cores; /* number of cores per socket */ + guint threads; /* number of threads per core */ +}; + typedef struct _GVirConnection GVirConnection; typedef struct _GVirConnectionPrivate GVirConnectionPrivate; typedef struct _GVirConnectionClass GVirConnectionClass; @@ -69,6 +82,7 @@ struct _GVirConnectionClass GType gvir_connection_get_type(void); GType gvir_connection_handle_get_type(void); +GType gvir_node_info_get_type(void); GVirConnection *gvir_connection_new(const char *uri); gboolean gvir_connection_open(GVirConnection *conn, @@ -174,6 +188,9 @@ GVirStoragePool *gvir_connection_create_storage_pool GVirStream *gvir_connection_get_stream(GVirConnection *conn, guint flags); +GVirNodeInfo *gvir_connection_get_node_info(GVirConnection *conn, + GError **err); + G_END_DECLS #endif /* __LIBVIRT_GOBJECT_CONNECTION_H__ */ diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 7a2f65d..9e63773 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -4,6 +4,7 @@ LIBVIRT_GOBJECT_0.0.4 { gvir_init_object_check; gvir_connection_get_type; + gvir_node_info_get_type; gvir_connection_new; gvir_connection_open; gvir_connection_open_async; -- 1.7.7.6

From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> --- libvirt-gobject/libvirt-gobject-domain.h | 14 ++++++++++++++ libvirt-gobject/libvirt-gobject.sym | 1 + 2 files changed, 15 insertions(+), 0 deletions(-) diff --git a/libvirt-gobject/libvirt-gobject-domain.h b/libvirt-gobject/libvirt-gobject-domain.h index bdff32e..56500a8 100644 --- a/libvirt-gobject/libvirt-gobject-domain.h +++ b/libvirt-gobject/libvirt-gobject-domain.h @@ -30,6 +30,7 @@ G_BEGIN_DECLS #include <libvirt-gobject/libvirt-gobject-stream.h> +#include <libvirt/libvirt.h> #define GVIR_TYPE_DOMAIN (gvir_domain_get_type ()) #define GVIR_DOMAIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_DOMAIN, GVirDomain)) @@ -88,6 +89,19 @@ typedef enum { GVIR_DOMAIN_START_FORCE_BOOT = (1 << 3), } GVirDomainStartFlags; +/** + * GVirDomainDeleteFlags: + * @GVIR_DOMAIN_DELETE_NONE: No flags + * @GVIR_DOMAIN_DELETE_SAVED_STATE: Also remove associated saved state (if present). + * @GVIR_DOMAIN_DELETE_SNAPSHOTS_METADATA: If last use of domain, then also + * remove any snapshot metadata. + */ +typedef enum { + GVIR_DOMAIN_DELETE_NONE = 0, + GVIR_DOMAIN_DELETE_SAVED_STATE = VIR_DOMAIN_UNDEFINE_MANAGED_SAVE, + GVIR_DOMAIN_DELETE_SNAPSHOTS_METADATA = VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, +} GVirDomainDeleteFlags; + typedef struct _GVirDomainInfo GVirDomainInfo; struct _GVirDomainInfo { diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 9e63773..917ee99 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -46,6 +46,7 @@ LIBVIRT_GOBJECT_0.0.4 { gvir_domain_info_get_type; gvir_domain_state_get_type; gvir_domain_start_flags_get_type; + gvir_domain_delete_flags_get_type; gvir_domain_get_name; gvir_domain_get_uuid; gvir_domain_get_id; -- 1.7.7.6

ACK On Sat, Feb 18, 2012 at 07:24:04PM +0200, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
--- libvirt-gobject/libvirt-gobject-domain.h | 14 ++++++++++++++ libvirt-gobject/libvirt-gobject.sym | 1 + 2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/libvirt-gobject/libvirt-gobject-domain.h b/libvirt-gobject/libvirt-gobject-domain.h index bdff32e..56500a8 100644 --- a/libvirt-gobject/libvirt-gobject-domain.h +++ b/libvirt-gobject/libvirt-gobject-domain.h @@ -30,6 +30,7 @@ G_BEGIN_DECLS
#include <libvirt-gobject/libvirt-gobject-stream.h> +#include <libvirt/libvirt.h>
#define GVIR_TYPE_DOMAIN (gvir_domain_get_type ()) #define GVIR_DOMAIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_DOMAIN, GVirDomain)) @@ -88,6 +89,19 @@ typedef enum { GVIR_DOMAIN_START_FORCE_BOOT = (1 << 3), } GVirDomainStartFlags;
+/** + * GVirDomainDeleteFlags: + * @GVIR_DOMAIN_DELETE_NONE: No flags + * @GVIR_DOMAIN_DELETE_SAVED_STATE: Also remove associated saved state (if present). + * @GVIR_DOMAIN_DELETE_SNAPSHOTS_METADATA: If last use of domain, then also + * remove any snapshot metadata. + */ +typedef enum { + GVIR_DOMAIN_DELETE_NONE = 0, + GVIR_DOMAIN_DELETE_SAVED_STATE = VIR_DOMAIN_UNDEFINE_MANAGED_SAVE, + GVIR_DOMAIN_DELETE_SNAPSHOTS_METADATA = VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, +} GVirDomainDeleteFlags; + typedef struct _GVirDomainInfo GVirDomainInfo; struct _GVirDomainInfo { diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 9e63773..917ee99 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -46,6 +46,7 @@ LIBVIRT_GOBJECT_0.0.4 { gvir_domain_info_get_type; gvir_domain_state_get_type; gvir_domain_start_flags_get_type; + gvir_domain_delete_flags_get_type; gvir_domain_get_name; gvir_domain_get_uuid; gvir_domain_get_id; -- 1.7.7.6
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On Sat, Feb 18, 2012 at 07:24:03PM +0200, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
Mostly just a wrapper around virNodeGetInfo() and virNodeInfo struct.
Apart from the 2 small comments below, I'm wondering if we should introduce a GVirNode class to wrap the virNode* methods. There are currently 5 to 10 such methods (forgot the exact numbers), if more are to come, this would "bloat" the GVirConnection class. Any thoughts on that? Christophe
--- libvirt-gobject/libvirt-gobject-connection.c | 49 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 17 +++++++++ libvirt-gobject/libvirt-gobject.sym | 1 + 3 files changed, 67 insertions(+), 0 deletions(-)
diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index cb19e9d..c2691f7 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -71,6 +71,21 @@ gvir_connection_error_quark(void) return g_quark_from_static_string("gvir-connection"); }
+static GVirNodeInfo * +gvir_node_info_copy(GVirNodeInfo *info) +{ + return g_slice_dup(GVirNodeInfo, info); +} + +static void +gvir_node_info_free(GVirNodeInfo *info) +{ + g_slice_free(GVirNodeInfo, info); +} + +G_DEFINE_BOXED_TYPE(GVirNodeInfo, gvir_node_info, + gvir_node_info_copy, gvir_node_info_free) + static void gvir_connection_get_property(GObject *object, guint prop_id, GValue *value, @@ -1338,3 +1353,37 @@ GVirStoragePool *gvir_connection_create_storage_pool
return g_object_ref(pool); } + +/** + * gvir_connection_get_node_info: + * @conn: the connection + * @err: return location for any #GError + * + * Returns: (transfer full): the info + */ +GVirNodeInfo *gvir_connection_get_node_info(GVirConnection *conn, + GError **err) +{ + GVirConnectionPrivate *priv = conn->priv; + virNodeInfo info; + GVirNodeInfo *ret; + + if (virNodeGetInfo(priv->conn, &info) < 0) { + gvir_set_error_literal(err, GVIR_CONNECTION_ERROR, + 0, + "Unable to get node info"); + return NULL; + } + + ret = g_slice_new(GVirNodeInfo); + g_memmove (ret->model, info.model, sizeof (ret->model));
Why not g_strncpy here?
+ ret->memory = info.memory; + ret->cpus = info.cpus; + ret->mhz = info.mhz; + ret->nodes = info.nodes; + ret->sockets = info.sockets; + ret->cores = info.cores; + ret->threads = info.threads; + + return ret; +} diff --git a/libvirt-gobject/libvirt-gobject-connection.h b/libvirt-gobject/libvirt-gobject-connection.h index 477a0c3..3cc60a2 100644 --- a/libvirt-gobject/libvirt-gobject-connection.h +++ b/libvirt-gobject/libvirt-gobject-connection.h @@ -38,6 +38,19 @@ G_BEGIN_DECLS
#define GVIR_TYPE_CONNECTION_HANDLE (gvir_connection_handle_get_type ())
+typedef struct _GVirNodeInfo GVirNodeInfo; +struct _GVirNodeInfo +{ + gchar model[32]; /* string indicating the CPU model */ + gulong memory; /* memory size in kilobytes */ + guint cpus; /* the number of active CPUs */ + guint mhz; /* expected CPU frequency */ + guint nodes; /* the number of NUMA cell, 1 for unusual NUMA topologies or uniform memo */ + guint sockets; /* number of CPU sockets per node if nodes > 1, total number of CPU socke */ + guint cores; /* number of cores per socket */ + guint threads; /* number of threads per core */ +}; + typedef struct _GVirConnection GVirConnection; typedef struct _GVirConnectionPrivate GVirConnectionPrivate; typedef struct _GVirConnectionClass GVirConnectionClass; @@ -69,6 +82,7 @@ struct _GVirConnectionClass
GType gvir_connection_get_type(void); GType gvir_connection_handle_get_type(void); +GType gvir_node_info_get_type(void);
GVirConnection *gvir_connection_new(const char *uri); gboolean gvir_connection_open(GVirConnection *conn, @@ -174,6 +188,9 @@ GVirStoragePool *gvir_connection_create_storage_pool GVirStream *gvir_connection_get_stream(GVirConnection *conn, guint flags);
+GVirNodeInfo *gvir_connection_get_node_info(GVirConnection *conn, + GError **err); + G_END_DECLS
#endif /* __LIBVIRT_GOBJECT_CONNECTION_H__ */ diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 7a2f65d..9e63773 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -4,6 +4,7 @@ LIBVIRT_GOBJECT_0.0.4 { gvir_init_object_check;
gvir_connection_get_type; + gvir_node_info_get_type; gvir_connection_new; gvir_connection_open; gvir_connection_open_async;
gvir_connection_get_node_info is missing here.
-- 1.7.7.6
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On Sun, Feb 19, 2012 at 2:50 PM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Sat, Feb 18, 2012 at 07:24:03PM +0200, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
Mostly just a wrapper around virNodeGetInfo() and virNodeInfo struct.
Apart from the 2 small comments below, I'm wondering if we should introduce a GVirNode class to wrap the virNode* methods. There are currently 5 to 10 such methods (forgot the exact numbers), if more are to come, this would "bloat" the GVirConnection class. Any thoughts on that?
While I agree with you about the "bloat" part, I think the app developer experience is more important. Since I don't know why libvirt has put these functions under a different prefix (you couldn't figure either IIRC?), I wonder if any app developer will be able to figure why he is asked to create/get a different object here (especially after he looks at underlying libvirt API). In this particular case, he'll be asked to make 3 calls just to get the amount of RAM on the host. Looking at the following commented-out declarations in libvirt-gobject/libvirt-gobject-connection.h: GList *gvir_connection_get_node_devices(GVirConnection *conn); GVirNodeDevice *gvir_connection_get_node_device(GVirConnection *conn, const gchar *name); I'm guessing Daniel intended to handle it the way my patch is doing.
+ ret = g_slice_new(GVirNodeInfo); + g_memmove (ret->model, info.model, sizeof (ret->model));
Why not g_strncpy here?
Cause I always forget which one is most appropriate. :) Thanks for pointing out.
#endif /* __LIBVIRT_GOBJECT_CONNECTION_H__ */ diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 7a2f65d..9e63773 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -4,6 +4,7 @@ LIBVIRT_GOBJECT_0.0.4 { gvir_init_object_check;
gvir_connection_get_type; + gvir_node_info_get_type; gvir_connection_new; gvir_connection_open; gvir_connection_open_async;
gvir_connection_get_node_info is missing here.
Yup. -- Regards, Zeeshan Ali (Khattak) FSF member#5124

On Sun, Feb 19, 2012 at 05:57:00PM +0200, Zeeshan Ali (Khattak) wrote:
Looking at the following commented-out declarations in libvirt-gobject/libvirt-gobject-connection.h:
GList *gvir_connection_get_node_devices(GVirConnection *conn); GVirNodeDevice *gvir_connection_get_node_device(GVirConnection *conn, const gchar *name);
I'm guessing Daniel intended to handle it the way my patch is doing.
These 2 functions will be used to get GVirNodeInfo objects which already exist as boilerplate, and this will wrap the virNodeDevice* API which is unrelated to the virNode* API (there's a virNodeDevice class for one). Christophe
participants (2)
-
Christophe Fergeau
-
Zeeshan Ali (Khattak)