[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH libvirt-glib 3/3] Add GVirDomainDi sk



---
 libvirt-gobject/Makefile.am                   |    2 +
 libvirt-gobject/libvirt-gobject-domain-disk.c |  210 +++++++++++++++++++++++++
 libvirt-gobject/libvirt-gobject-domain-disk.h |   78 +++++++++
 libvirt-gobject/libvirt-gobject.h             |    1 +
 libvirt-gobject/libvirt-gobject.sym           |    4 +
 5 files changed, 295 insertions(+), 0 deletions(-)
 create mode 100644 libvirt-gobject/libvirt-gobject-domain-disk.c
 create mode 100644 libvirt-gobject/libvirt-gobject-domain-disk.h

diff --git a/libvirt-gobject/Makefile.am b/libvirt-gobject/Makefile.am
index 8b59109..101007e 100644
--- a/libvirt-gobject/Makefile.am
+++ b/libvirt-gobject/Makefile.am
@@ -8,6 +8,7 @@ GOBJECT_HEADER_FILES = \
 			libvirt-gobject-main.h \
 			libvirt-gobject-domain-snapshot.h \
 			libvirt-gobject-domain-device.h \
+			libvirt-gobject-domain-disk.h \
 			libvirt-gobject-domain-interface.h \
 			libvirt-gobject-domain.h \
 			libvirt-gobject-interface.h \
@@ -24,6 +25,7 @@ GOBJECT_SOURCE_FILES = \
 			libvirt-gobject-main.c \
 			libvirt-gobject-domain-snapshot.c \
 			libvirt-gobject-domain-device.c \
+			libvirt-gobject-domain-disk.c \
 			libvirt-gobject-domain-interface.c \
 			libvirt-gobject-domain.c \
 			libvirt-gobject-interface.c \
diff --git a/libvirt-gobject/libvirt-gobject-domain-disk.c b/libvirt-gobject/libvirt-gobject-domain-disk.c
new file mode 100644
index 0000000..0481967
--- /dev/null
+++ b/libvirt-gobject/libvirt-gobject-domain-disk.c
@@ -0,0 +1,210 @@
+/*
+ * libvirt-gobject-domain-disk.c: libvirt gobject integration
+ *
+ * Copyright (C) 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Marc-André Lureau <marcandre lureau redhat com>
+ */
+
+#include <config.h>
+
+#include <libvirt/virterror.h>
+#include <string.h>
+
+#include "libvirt-glib/libvirt-glib.h"
+#include "libvirt-gobject/libvirt-gobject.h"
+
+#include "libvirt-gobject/libvirt-gobject-domain-device-private.h"
+
+extern gboolean debugFlag;
+
+#define DEBUG(fmt, ...) do { if (G_UNLIKELY(debugFlag)) g_debug(fmt, ## __VA_ARGS__); } while (0)
+
+#define GVIR_DOMAIN_DISK_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_DOMAIN_DISK, GVirDomainDiskPrivate))
+
+struct _GVirDomainDiskPrivate
+{
+    gchar *path;
+};
+
+G_DEFINE_TYPE(GVirDomainDisk, gvir_domain_disk, GVIR_TYPE_DOMAIN_DEVICE);
+
+enum {
+    PROP_0,
+    PROP_PATH,
+};
+
+#define GVIR_DOMAIN_DISK_ERROR gvir_domain_disk_error_quark()
+
+
+static GQuark
+gvir_domain_disk_error_quark(void)
+{
+    return g_quark_from_static_string("gvir-domain-disk");
+}
+
+static void gvir_domain_disk_get_property(GObject *object,
+                                          guint prop_id,
+                                          GValue *value,
+                                          GParamSpec *pspec)
+{
+    GVirDomainDisk *self = GVIR_DOMAIN_DISK(object);
+    GVirDomainDiskPrivate *priv = self->priv;
+
+    switch (prop_id) {
+    case PROP_PATH:
+        g_value_set_string(value, priv->path);
+        break;
+
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+
+static void gvir_domain_disk_set_property(GObject *object,
+                                          guint prop_id,
+                                          const GValue *value,
+                                          GParamSpec *pspec)
+{
+    GVirDomainDisk *self = GVIR_DOMAIN_DISK(object);
+    GVirDomainDiskPrivate *priv = self->priv;
+
+    switch (prop_id) {
+    case PROP_PATH:
+        if (priv->path)
+            g_free(priv->path);
+        priv->path = g_value_dup_string(value);
+        break;
+
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+
+static void gvir_domain_disk_finalize(GObject *object)
+{
+    GVirDomainDisk *self = GVIR_DOMAIN_DISK(object);
+    GVirDomainDiskPrivate *priv = self->priv;
+
+    DEBUG("Finalize GVirDomainDisk=%p", self);
+
+    if (priv->path)
+        g_free(priv->path);
+
+    G_OBJECT_CLASS(gvir_domain_disk_parent_class)->finalize(object);
+}
+
+static void gvir_domain_disk_class_init(GVirDomainDiskClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+    object_class->finalize = gvir_domain_disk_finalize;
+    object_class->get_property = gvir_domain_disk_get_property;
+    object_class->set_property = gvir_domain_disk_set_property;
+
+    g_object_class_install_property(object_class,
+                                    PROP_PATH,
+                                    g_param_spec_string("path",
+                                                        "Path",
+                                                        "The disk path",
+                                                        NULL,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY |
+                                                        G_PARAM_STATIC_STRINGS));
+
+    g_type_class_add_private(klass, sizeof(GVirDomainDiskPrivate));
+}
+
+static void gvir_domain_disk_init(GVirDomainDisk *self)
+{
+    DEBUG("Init GVirDomainDisk=%p", self);
+
+    self->priv = GVIR_DOMAIN_DISK_GET_PRIVATE(self);
+}
+
+static GVirDomainDiskStats *
+gvir_domain_disk_stats_copy(GVirDomainDiskStats *stats)
+{
+    return g_slice_dup(GVirDomainDiskStats, stats);
+}
+
+
+static void
+gvir_domain_disk_stats_free(GVirDomainDiskStats *stats)
+{
+    g_slice_free(GVirDomainDiskStats, stats);
+}
+
+
+GType gvir_domain_disk_stats_get_type(void)
+{
+    static GType stats_type = 0;
+
+    if (G_UNLIKELY(stats_type == 0))
+        stats_type = g_boxed_type_register_static
+            ("GVirDomainDiskStats",
+             (GBoxedCopyFunc)gvir_domain_disk_stats_copy,
+             (GBoxedFreeFunc)gvir_domain_disk_stats_free);
+
+    return stats_type;
+}
+
+/**
+ * gvir_domain_disk_get_stats:
+ * @self: the domain disk
+ * @err: an error
+ *
+ * This function returns network disk stats. Individual fields
+ * within the stats structure may be returned as -1, which indicates
+ * that the hypervisor does not support that particular statistic.
+ *
+ * Returns: (transfer full): the stats or %NULL in case of error
+ **/
+GVirDomainDiskStats *gvir_domain_disk_get_stats(GVirDomainDisk *self, GError **err)
+{
+    GVirDomainDiskStats *ret;
+    virDomainBlockStatsStruct stats;
+    GVirDomainDiskPrivate *priv;
+    virDomainPtr handle;
+
+    g_return_val_if_fail(GVIR_IS_DOMAIN_DISK(self), NULL);
+
+    priv = self->priv;
+    handle = gvir_domain_device_get_domain_handle(GVIR_DOMAIN_DEVICE(self));
+
+    if (virDomainBlockStats(handle, priv->path, &stats, sizeof (stats)) < 0) {
+        if (err)
+            *err = gvir_error_new_literal(GVIR_DOMAIN_DISK_ERROR,
+                                          0,
+                                          "Unable to get domain disk stats");
+        goto end;
+    }
+
+    ret = g_slice_new(GVirDomainDiskStats);
+    ret->rd_req = stats.rd_req;
+    ret->rd_bytes = stats.rd_bytes;
+    ret->wr_req = stats.wr_req;
+    ret->wr_bytes = stats.wr_bytes;
+    ret->errs = stats.errs;
+
+end:
+    virDomainFree(handle);
+    return ret;
+}
diff --git a/libvirt-gobject/libvirt-gobject-domain-disk.h b/libvirt-gobject/libvirt-gobject-domain-disk.h
new file mode 100644
index 0000000..8b59170
--- /dev/null
+++ b/libvirt-gobject/libvirt-gobject-domain-disk.h
@@ -0,0 +1,78 @@
+/*
+ * libvirt-gobject-domain-disk.h: libvirt gobject integration
+ *
+ * Copyright (C) 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Marc-André Lureau <marcandre lureau redhat com>
+ */
+
+#if !defined(__LIBVIRT_GOBJECT_H__) && !defined(LIBVIRT_GOBJECT_BUILD)
+#error "Only <libvirt-gobject/libvirt-gobject.h> can be included directly."
+#endif
+
+#ifndef __LIBVIRT_GOBJECT_DOMAIN_DISK_H__
+#define __LIBVIRT_GOBJECT_DOMAIN_DISK_H__
+
+G_BEGIN_DECLS
+
+#define GVIR_TYPE_DOMAIN_DISK            (gvir_domain_disk_get_type ())
+#define GVIR_DOMAIN_DISK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_DOMAIN_DISK, GVirDomainDisk))
+#define GVIR_DOMAIN_DISK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GVIR_TYPE_DOMAIN_DISK, GVirDomainDiskClass))
+#define GVIR_IS_DOMAIN_DISK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GVIR_TYPE_DOMAIN_DISK))
+#define GVIR_IS_DOMAIN_DISK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GVIR_TYPE_DOMAIN_DISK))
+#define GVIR_DOMAIN_DISK_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GVIR_TYPE_DOMAIN_DISK, GVirDomainDiskClass))
+
+#define GVIR_TYPE_DOMAIN_DISK_STATS       (gvir_domain_disk_stats_get_type())
+
+typedef struct _GVirDomainDiskStats GVirDomainDiskStats;
+struct _GVirDomainDiskStats
+{
+  gint64 rd_req; /* number of read requests */
+  gint64 rd_bytes; /* number of read bytes */
+  gint64 wr_req; /* number of write requests */
+  gint64 wr_bytes; /* number of written bytes */
+  gint64 errs;   /* In Xen this returns the mysterious 'oo_req'. */
+};
+
+typedef struct _GVirDomainDisk GVirDomainDisk;
+typedef struct _GVirDomainDiskPrivate GVirDomainDiskPrivate;
+typedef struct _GVirDomainDiskClass GVirDomainDiskClass;
+
+struct _GVirDomainDisk
+{
+    GVirDomainDevice parent;
+
+    GVirDomainDiskPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _GVirDomainDiskClass
+{
+    GVirDomainDeviceClass parent_class;
+
+    gpointer padding[20];
+};
+
+GType gvir_domain_disk_get_type(void);
+GType gvir_domain_disk_stats_get_type(void);
+
+GVirDomainDiskStats *gvir_domain_disk_get_stats(GVirDomainDisk *self, GError **err);
+
+G_END_DECLS
+
+#endif /* __LIBVIRT_GOBJECT_DOMAIN_DISK_H__ */
diff --git a/libvirt-gobject/libvirt-gobject.h b/libvirt-gobject/libvirt-gobject.h
index 6038036..aef749b 100644
--- a/libvirt-gobject/libvirt-gobject.h
+++ b/libvirt-gobject/libvirt-gobject.h
@@ -31,6 +31,7 @@
 #include <libvirt-gobject/libvirt-gobject-enums.h>
 #include <libvirt-gobject/libvirt-gobject-stream.h>
 #include <libvirt-gobject/libvirt-gobject-domain-device.h>
+#include <libvirt-gobject/libvirt-gobject-domain-disk.h>
 #include <libvirt-gobject/libvirt-gobject-domain-interface.h>
 #include <libvirt-gobject/libvirt-gobject-domain-snapshot.h>
 #include <libvirt-gobject/libvirt-gobject-domain.h>
diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym
index 9118b11..164b6b8 100644
--- a/libvirt-gobject/libvirt-gobject.sym
+++ b/libvirt-gobject/libvirt-gobject.sym
@@ -31,6 +31,10 @@ LIBVIRT_GOBJECT_0.0.1 {
 
 	gvir_domain_device_get_type;
 
+	gvir_domain_disk_get_type;
+	gvir_domain_disk_stats_get_type;
+	gvir_domain_disk_get_stats;
+
 	gvir_domain_interface_get_type;
 	gvir_domain_interface_stats_get_type;
 	gvir_domain_interface_get_stats;
-- 
1.7.7


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]