[virt-tools-list] [libosinfo 2/6] Add Resources class
Zeeshan Ali (Khattak)
zeeshanak at gnome.org
Sat Sep 10 00:05:00 UTC 2011
From: "Zeeshan Ali (Khattak)" <zeeshanak at gnome.org>
Add a new class to represent resource requirements/recommendations for OSs.
---
docs/reference/Libosinfo-sections.txt | 22 ++++
osinfo/Makefile.am | 4 +-
osinfo/libosinfo.syms | 7 +
osinfo/osinfo.h | 1 +
osinfo/osinfo_resources.c | 196 +++++++++++++++++++++++++++++++++
osinfo/osinfo_resources.h | 101 +++++++++++++++++
6 files changed, 330 insertions(+), 1 deletions(-)
create mode 100644 osinfo/osinfo_resources.c
create mode 100644 osinfo/osinfo_resources.h
diff --git a/docs/reference/Libosinfo-sections.txt b/docs/reference/Libosinfo-sections.txt
index 68464e3..521019f 100644
--- a/docs/reference/Libosinfo-sections.txt
+++ b/docs/reference/Libosinfo-sections.txt
@@ -443,6 +443,28 @@ OSINFO_MEDIALIST_GET_CLASS
</SECTION>
<SECTION>
+<FILE>osinfo_resources</FILE>
+<TITLE>OsinfoResources</TITLE>
+OsinfoResources
+OsinfResourcesoClass
+OsinfResourcesoPrivate
+<SUBSECTION Standard>
+osinfo_resources_new
+osinfo_resources_get_architecture
+osinfo_resources_get_cpu
+osinfo_resources_get_n_cpus
+osinfo_resources_get_ram
+osinfo_resources_get_storage
+OSINFO_RESOURCES
+OSINFO_IS_RESOURCES
+OSINFO_TYPE_RESOURCES
+osinfo_resources_get_type
+OSINFO_RESOURCES_CLASS
+OSINFO_IS_RESOURCES_CLASS
+OSINFO_RESOURCES_GET_CLASS
+</SECTION>
+
+<SECTION>
<FILE>osinfo_devicelink</FILE>
<TITLE>OsinfoDeviceLink</TITLE>
OsinfoDeviceLink
diff --git a/osinfo/Makefile.am b/osinfo/Makefile.am
index 2af7da2..0b59284 100644
--- a/osinfo/Makefile.am
+++ b/osinfo/Makefile.am
@@ -63,7 +63,8 @@ libosinfo_1_0_include_HEADERS = \
osinfo_deployment.h \
osinfo_deploymentlist.h \
osinfo_media.h \
- osinfo_medialist.h
+ osinfo_medialist.h \
+ osinfo_resources.h
libosinfo_1_0_la_SOURCES = \
osinfo_entity.c \
@@ -85,6 +86,7 @@ libosinfo_1_0_la_SOURCES = \
osinfo_deploymentlist.c \
osinfo_media.c \
osinfo_medialist.c \
+ osinfo_resources.c \
osinfo_db.c \
osinfo_loader.c
diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 2be90ee..872f139 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -157,6 +157,13 @@ LIBOSINFO_0.0.1 {
osinfo_medialist_new_filtered;
osinfo_medialist_new_intersection;
osinfo_medialist_new_union;
+ osinfo_resources_get_type;
+ osinfo_resources_new;
+ osinfo_resources_get_architecture;
+ osinfo_resources_get_cpu;
+ osinfo_resources_get_n_cpus;
+ osinfo_resources_get_ram;
+ osinfo_resources_get_storage;
local:
*;
diff --git a/osinfo/osinfo.h b/osinfo/osinfo.h
index 6836c9c..dc33605 100644
--- a/osinfo/osinfo.h
+++ b/osinfo/osinfo.h
@@ -44,6 +44,7 @@
#include <osinfo/osinfo_deployment.h>
#include <osinfo/osinfo_media.h>
#include <osinfo/osinfo_medialist.h>
+#include <osinfo/osinfo_resources.h>
#include <osinfo/osinfo_db.h>
#include <osinfo/osinfo_loader.h>
diff --git a/osinfo/osinfo_resources.c b/osinfo/osinfo_resources.c
new file mode 100644
index 0000000..1d4c9a2
--- /dev/null
+++ b/osinfo/osinfo_resources.c
@@ -0,0 +1,196 @@
+/*
+ * libosinfo: Required or recommended resources for an (guest) OS
+ *
+ * Copyright (C) 2009-2011 Red Hat, Inc
+ *
+ * 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
+ *
+ * Authors:
+ * Zeeshan Ali <zeenix at redhat.com>
+ * Arjun Roy <arroy at redhat.com>
+ * Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#include <osinfo/osinfo.h>
+#include <gio/gio.h>
+#include <stdlib.h>
+#include <string.h>
+
+G_DEFINE_TYPE (OsinfoResources, osinfo_resources, OSINFO_TYPE_ENTITY);
+
+#define OSINFO_RESOURCES_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
+ OSINFO_TYPE_RESOURCES, \
+ OsinfoResourcesPrivate))
+
+/**
+ * SECTION:osinfo_resources
+ * @short_description: Required or recommended resources for an (guest) OS
+ * @see_also: #OsinfoOs
+ *
+ * #OsinfoResources is an entity representing required or recommended resources
+ * for an (guest) operating system.
+ */
+
+struct _OsinfoResourcesPrivate
+{
+ gboolean unused;
+};
+
+static gint64 get_param_as_int64(OsinfoResources *resources,
+ const gchar *key,
+ guint64 multiplier)
+{
+ const gchar *str;
+ gdouble ret;
+
+ str = osinfo_entity_get_param_value(OSINFO_ENTITY(resources), key);
+
+ if (str == NULL)
+ return -1;
+
+ ret = g_ascii_strtod(str, NULL) * multiplier;
+
+ return (gint64) ret;
+}
+
+static void
+osinfo_resources_finalize (GObject *object)
+{
+ /* Chain up to the parent class */
+ G_OBJECT_CLASS (osinfo_resources_parent_class)->finalize (object);
+}
+
+/* Init functions */
+static void
+osinfo_resources_class_init (OsinfoResourcesClass *klass)
+{
+ GObjectClass *g_klass = G_OBJECT_CLASS (klass);
+
+ g_klass->finalize = osinfo_resources_finalize;
+ g_type_class_add_private (klass, sizeof (OsinfoResourcesPrivate));
+}
+
+static void
+osinfo_resources_init (OsinfoResources *resources)
+{
+ OsinfoResourcesPrivate *priv;
+ resources->priv = priv = OSINFO_RESOURCES_GET_PRIVATE(resources);
+}
+
+OsinfoResources *osinfo_resources_new(const gchar *id,
+ const gchar *architecture)
+{
+ OsinfoResources *resources;
+
+ resources = g_object_new(OSINFO_TYPE_RESOURCES,
+ "id", id,
+ NULL);
+
+ if (architecture != NULL)
+ osinfo_entity_set_param(OSINFO_ENTITY(resources),
+ OSINFO_RESOURCES_PROP_ARCHITECTURE,
+ architecture);
+
+ return resources;
+}
+
+/**
+ * osinfo_resources_get_architecture:
+ * @resources: a #OsinfoResources instance
+ *
+ * Retrieves the target hardware architecture to which @resources applies. Some
+ * operating systems specify the same requirements and recommendations for all
+ * architectures. In such cases, the string returned by this call will be
+ * #OSINFO_ARCHITECTURE_ALL.
+ *
+ * Returns: (transfer none): the hardware architecture.
+ */
+const gchar *osinfo_resources_get_architecture(OsinfoResources *resources)
+{
+ return osinfo_entity_get_param_value(OSINFO_ENTITY(resources),
+ OSINFO_RESOURCES_PROP_ARCHITECTURE);
+}
+
+/**
+ * osinfo_resources_get_n_cpus:
+ * @resources: a #OsinfoResources instance
+ *
+ * Retrieves the number of CPUs.
+ *
+ * Returns: the number of CPUs, or -1.
+ */
+gint osinfo_resources_get_n_cpus(OsinfoResources *resources)
+{
+ return (gint) get_param_as_int64(resources,
+ OSINFO_RESOURCES_PROP_N_CPUS,
+ 1);
+}
+
+/**
+ * osinfo_resources_get_cpu:
+ * @resources: a #OsinfoResources instance
+ *
+ * Retrieves the CPU frequency in hertz (Hz). Divide the value by #OSINFO_MEGAHERTZ if
+ * you need this value in megahertz (MHz).
+ *
+ * Returns: the CPU frequency, or -1.
+ */
+gint64 osinfo_resources_get_cpu(OsinfoResources *resources)
+{
+ return get_param_as_int64(resources,
+ OSINFO_RESOURCES_PROP_CPU,
+ OSINFO_MEGAHERTZ);
+}
+
+/**
+ * osinfo_resources_get_ram:
+ * @resources: a #OsinfoResources instance
+ *
+ * Retrieves the amount of Random Access Memory (RAM) in bytes. Divide the value
+ * by #OSINFO_MEBIBYTES if you need this value in mebibytes.
+ *
+ * Returns: the amount of RAM, or -1.
+ */
+gint64 osinfo_resources_get_ram(OsinfoResources *resources)
+{
+ return get_param_as_int64(resources,
+ OSINFO_RESOURCES_PROP_RAM,
+ OSINFO_MEBIBYTES);
+}
+
+/**
+ * osinfo_resources_get_storage:
+ * @resources: a #OsinfoResources instance
+ *
+ * Retrieves the amount of storage space in bytes. Divide the value by
+ * #OSINFO_GIBIBYTES if you need this value in gibibytes.
+ *
+ * Returns: the amount of storage, or -1.
+ */
+gint64 osinfo_resources_get_storage(OsinfoResources *resources)
+{
+ return get_param_as_int64(resources,
+ OSINFO_RESOURCES_PROP_STORAGE,
+ OSINFO_GIBIBYTES);
+}
+
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * End:
+ */
diff --git a/osinfo/osinfo_resources.h b/osinfo/osinfo_resources.h
new file mode 100644
index 0000000..fadbee2
--- /dev/null
+++ b/osinfo/osinfo_resources.h
@@ -0,0 +1,101 @@
+/*
+ * libosinfo: Required or recommended resources for an (guest) OS
+ *
+ * Copyright (C) 2009-2011 Red Hat, Inc
+ *
+ * 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
+ *
+ * Authors:
+ * Zeeshan Ali <zeenix at redhat.com>
+ * Arjun Roy <arroy at redhat.com>
+ * Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <osinfo/osinfo_entity.h>
+
+#ifndef __OSINFO_RESOURCES_H__
+#define __OSINFO_RESOURCES_H__
+
+#define OSINFO_ARCHITECTURE_ALL "all"
+#define OSINFO_MEGAHERTZ 1000000
+#define OSINFO_MEBIBYTES 1048576
+#define OSINFO_GIBIBYTES 1024 * OSINFO_MEBIBYTES
+
+/*
+ * Type macros.
+ */
+#define OSINFO_TYPE_RESOURCES (osinfo_resources_get_type ())
+#define OSINFO_RESOURCES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ OSINFO_TYPE_RESOURCES, OsinfoResources))
+#define OSINFO_IS_RESOURCES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ OSINFO_TYPE_RESOURCES))
+#define OSINFO_RESOURCES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
+ OSINFO_TYPE_RESOURCES, OsinfoResourcesClass))
+#define OSINFO_IS_RESOURCES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+ OSINFO_TYPE_RESOURCES))
+#define OSINFO_RESOURCES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ OSINFO_TYPE_RESOURCES, OsinfoResourcesClass))
+
+typedef struct _OsinfoResources OsinfoResources;
+
+typedef struct _OsinfoResourcesClass OsinfoResourcesClass;
+
+typedef struct _OsinfoResourcesPrivate OsinfoResourcesPrivate;
+
+#define OSINFO_RESOURCES_PROP_ARCHITECTURE "architecture"
+#define OSINFO_RESOURCES_PROP_CPU "cpu"
+#define OSINFO_RESOURCES_PROP_N_CPUS "n-cpus"
+#define OSINFO_RESOURCES_PROP_RAM "ram"
+#define OSINFO_RESOURCES_PROP_STORAGE "storage"
+
+/* object */
+struct _OsinfoResources
+{
+ OsinfoEntity parent_instance;
+
+ /* public */
+
+ /* private */
+ OsinfoResourcesPrivate *priv;
+};
+
+/* class */
+struct _OsinfoResourcesClass
+{
+ OsinfoEntityClass parent_class;
+
+ /* class members */
+};
+
+GType osinfo_resources_get_type(void);
+
+OsinfoResources *osinfo_resources_new(const gchar *id, const gchar *architecture);
+
+const gchar *osinfo_resources_get_architecture(OsinfoResources *resources);
+gint osinfo_resources_get_n_cpus(OsinfoResources *resources);
+gint64 osinfo_resources_get_cpu(OsinfoResources *resources);
+gint64 osinfo_resources_get_ram(OsinfoResources *resources);
+gint64 osinfo_resources_get_storage(OsinfoResources *resources);
+
+#endif /* __OSINFO_RESOURCES_H__ */
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * End:
+ */
--
1.7.6
More information about the virt-tools-list
mailing list