[Libvirt-cim] [PATCH] (#2) ProcessorRASD, the class that won't go away

Jay Gagnon grendel at linux.vnet.ibm.com
Mon May 19 22:06:17 UTC 2008


# HG changeset patch
# User Jay Gagnon <grendel at linux.vnet.ibm.com>
# Date 1211234740 14400
# Node ID d9042b4ea61771df5110e97872c9e504f71859e7
# Parent  c949b1845113e11e01329abb5aa81ca6ade8e3ec
(#2) ProcessorRASD, the class that won't go away

Around and round we go on the merry-go-round of indecision, hopefully for our last ride.  The definitely probably absolutely tentative final plan is that the default representation of processors as virt_device structs will be one per domain, with a quantity.  Virt_Devices will just need to be told how to handle that, and all the RASD stuff will be much happier for it.  And then we will never speak of this again.

So today being my last official day on the team, I kind of have to get this out in a not perfect state.  From what I can see it does what it is supposed to do, but it is entirely possible (likely?) that there are a few things in here that aren't really great things to do.  I wish I could polish it more but I think I've at least got it to the point where any work left to be done is generic "not a great idea in C" problems, as opposed to the much more annoying "what exactly are we supposed to do in this case" problems.

Changes from 1 to 2:
Close brace on if block.  That one is really embarrassing; I have no idea what happened there.  I mean that crap won't even compile.
Removed unnecessary proc_found boolean.  That was me being lazy after changing my mind on something.
Cleaned up a couple leftover CU_DEBUG calls.
Addition of vcpu_inst function, which is similar to vcpu_instances but always only works on one instance and takes a dev_id.  This is to prevent all instances from being given a dev_id of zero.  It is unfortunately similar to vcpu_instances in other regards, but I'm not sure if it's worth the overhead of trying to combine the similar parts.
Fixed quantity v number problem in xml_parse_test that I appear to have been failing to address for about six years.

Signed-off-by: Jay Gagnon <grendel at linux.vnet.ibm.com>
Signed-off-by: Kaitlin Rupert <karupert at us.ibm.com>
Signed-off-by: Dan Smith <danms at us.ibm.com>

diff -r c949b1845113 -r d9042b4ea617 libxkutil/device_parsing.c
--- a/libxkutil/device_parsing.c	Thu May 15 17:56:39 2008 -0700
+++ b/libxkutil/device_parsing.c	Mon May 19 18:05:40 2008 -0400
@@ -337,7 +337,6 @@ static int parse_vcpu_device(xmlNode *no
         struct virt_device *list = NULL;
         char *count_str;
         int count;
-        int i;
 
         count_str = get_node_content(node);
         if (count_str == NULL)
@@ -347,24 +346,15 @@ static int parse_vcpu_device(xmlNode *no
 
         free(count_str);
 
-        list = calloc(count, sizeof(*list));
+        list = calloc(1, sizeof(*list));
         if (list == NULL)
                 goto err;
-
-        for (i = 0; i < count; i++) {
-                struct virt_device *vdev = &list[i];
-                struct vcpu_device *cdev = &vdev->dev.vcpu;
-
-                cdev->number = i;
-
-                vdev->type = CIM_RES_TYPE_PROC;
-                if (asprintf(&vdev->id, "%i", i) == -1)
-                        vdev->id = NULL;
-        }
+        
+        list->dev.vcpu.quantity = count;
 
         *vdevs = list;
 
-        return count;
+        return 1;
  err:
         free(list);
 
@@ -620,7 +610,7 @@ struct virt_device *virt_device_dup(stru
                 dev->dev.mem.size = _dev->dev.mem.size;
                 dev->dev.mem.maxsize = _dev->dev.mem.maxsize;
         } else if (dev->type == CIM_RES_TYPE_PROC) {
-                dev->dev.vcpu.number = _dev->dev.vcpu.number;
+                dev->dev.vcpu.quantity = _dev->dev.vcpu.quantity;
         } else if (dev->type == CIM_RES_TYPE_EMU) {
                 DUP_FIELD(dev, _dev, dev.emu.path);
         } else if (dev->type == CIM_RES_TYPE_GRAPHICS) {
@@ -672,6 +662,32 @@ static int _get_mem_device(const char *x
         return 1;
 }
 
+static int _get_proc_device(const char *xml, struct virt_device **list)
+{
+        struct virt_device *proc_devs = NULL;
+        struct virt_device *proc_dev = NULL;
+        int ret;
+
+        ret = parse_devices(xml, &proc_devs, CIM_RES_TYPE_PROC);
+        if (ret <= 0)
+                return ret;
+
+        proc_dev = malloc(sizeof(*proc_dev));
+        if (proc_dev == NULL)
+                return 0;
+
+        memset(proc_dev, 0, sizeof(*proc_dev));
+
+        proc_dev->type = CIM_RES_TYPE_PROC;
+        proc_dev->id = strdup("proc");
+        proc_dev->dev.vcpu.quantity = proc_devs[0].dev.vcpu.quantity;
+        *list = proc_dev;
+
+        cleanup_virt_devices(&proc_devs, ret);
+
+        return 1;
+};
+
 int get_devices(virDomainPtr dom, struct virt_device **list, int type)
 {
         char *xml;
@@ -683,6 +699,8 @@ int get_devices(virDomainPtr dom, struct
 
         if (type == CIM_RES_TYPE_MEM)
                 ret = _get_mem_device(xml, list);
+        else if (type == CIM_RES_TYPE_PROC)
+                ret = _get_proc_device(xml, list);
         else
                 ret = parse_devices(xml, list, type);
 
diff -r c949b1845113 -r d9042b4ea617 libxkutil/device_parsing.h
--- a/libxkutil/device_parsing.h	Thu May 15 17:56:39 2008 -0700
+++ b/libxkutil/device_parsing.h	Mon May 19 18:05:40 2008 -0400
@@ -50,7 +50,7 @@ struct mem_device {
 };
 
 struct vcpu_device {
-        uint64_t number;
+        uint64_t quantity;
 };
 
 struct emu_device {
diff -r c949b1845113 -r d9042b4ea617 libxkutil/xml_parse_test.c
--- a/libxkutil/xml_parse_test.c	Thu May 15 17:56:39 2008 -0700
+++ b/libxkutil/xml_parse_test.c	Mon May 19 18:05:40 2008 -0400
@@ -96,7 +96,7 @@ static void print_dev_vcpu(struct virt_d
 static void print_dev_vcpu(struct virt_device *dev,
                            FILE *d)
 {
-        print_u64(d, "Virtual CPU", dev->dev.vcpu.number);
+        print_u64(d, "Virtual CPU", dev->dev.vcpu.quantity);
 }
 
 static void print_dev_emu(struct virt_device *dev,
diff -r c949b1845113 -r d9042b4ea617 src/Virt_Device.c
--- a/src/Virt_Device.c	Thu May 15 17:56:39 2008 -0700
+++ b/src/Virt_Device.c	Mon May 19 18:05:40 2008 -0400
@@ -174,23 +174,6 @@ static CMPIInstance *mem_instance(const 
         return inst;
 }
 
-static CMPIInstance *vcpu_instance(const CMPIBroker *broker,
-                                   struct vcpu_device *dev,
-                                   const virDomainPtr dom,
-                                   const char *ns)
-{
-        CMPIInstance *inst;
-        virConnectPtr conn;
-
-        conn = virDomainGetConnect(dom);
-        inst = get_typed_instance(broker,
-                                  pfx_from_conn(conn),
-                                  "Processor",
-                                  ns);
-
-        return inst;
-}
-
 static int device_set_devid(CMPIInstance *instance,
                             struct virt_device *dev,
                             const virDomainPtr dom)
@@ -229,43 +212,141 @@ static int device_set_systemname(CMPIIns
         return 1;
 }
 
-static CMPIInstance *device_instance(const CMPIBroker *broker,
-                                     struct virt_device *dev,
-                                     const virDomainPtr dom,
-                                     const char *ns)
+static char *get_vcpu_inst_id(const virDomainPtr dom,
+                              int proc_num)
 {
-        CMPIInstance *instance;
+        int rc;
+        char *id_num = NULL;
+        char *dev_id = NULL;
 
-        if (dev->type == CIM_RES_TYPE_NET)
-                instance = net_instance(broker,
-                                        &dev->dev.net,
-                                        dom,
-                                        ns);
-        else if (dev->type == CIM_RES_TYPE_DISK)
-                instance = disk_instance(broker,
-                                         &dev->dev.disk,
-                                         dom,
-                                         ns);
-        else if (dev->type == CIM_RES_TYPE_MEM)
-                instance = mem_instance(broker,
-                                        &dev->dev.mem,
-                                        dom,
-                                        ns);
-        else if (dev->type == CIM_RES_TYPE_PROC)
-                instance = vcpu_instance(broker,
-                                         &dev->dev.vcpu,
-                                         dom,
-                                         ns);
-        else
-                return NULL;
+        rc = asprintf(&id_num, "%d", proc_num);
+        if (rc == -1) {
+                free(dev_id);
+                dev_id = NULL;
+                goto out;
+        }
+        
+        dev_id = get_fq_devid((char *)virDomainGetName(dom), id_num);
+        free(id_num);
 
-        if (!instance)
-                return NULL;
+ out:
+        return dev_id;
+}                    
 
-        device_set_devid(instance, dev, dom);
-        device_set_systemname(instance, dom);
+static bool vcpu_instances(const CMPIBroker *broker,
+                           const virDomainPtr dom,
+                           const char *ns,
+                           uint64_t proc_count,
+                           struct inst_list *list)
+{
+        int i;
+        char *dev_id;
+        CMPIInstance *inst;
+        virConnectPtr conn = NULL;
 
-        return instance;
+        for (i = 0; i < proc_count; i++) {
+                conn = virDomainGetConnect(dom);
+                inst = get_typed_instance(broker,
+                                          pfx_from_conn(conn),
+                                          "Processor",
+                                          ns);
+                if (inst == NULL)
+                        return false;
+
+                dev_id = get_vcpu_inst_id(dom, i);
+                CMSetProperty(inst, "DeviceID",
+                              (CMPIValue *)dev_id, CMPI_chars);
+                free(dev_id);
+                
+                device_set_systemname(inst, dom);
+                inst_list_add(list, inst);
+        }
+
+        return true;
+}
+
+static bool vcpu_inst(const CMPIBroker *broker,
+                      const virDomainPtr dom,
+                      const char *ns,
+                      int dev_id_num,
+                      struct inst_list *list)
+{
+        char *dev_id;
+        CMPIInstance *inst;
+        virConnectPtr conn = NULL;
+
+        conn = virDomainGetConnect(dom);
+        inst = get_typed_instance(broker,
+                                  pfx_from_conn(conn),
+                                  "Processor",
+                                  ns);
+        if (inst == NULL)
+                return false;
+
+        dev_id = get_vcpu_inst_id(dom, dev_id_num);
+        CMSetProperty(inst, "DeviceID",
+                      (CMPIValue *)dev_id, CMPI_chars);
+        free(dev_id);
+                
+        device_set_systemname(inst, dom);
+        inst_list_add(list, inst);
+
+        return true;
+}
+
+static bool device_instances(const CMPIBroker *broker,
+                             struct virt_device *devs,
+                             int count,
+                             const virDomainPtr dom,
+                             const char *ns,
+                             struct inst_list *list)
+{
+        int i;
+        bool ret;
+        uint64_t proc_count = 0;
+        CMPIInstance *instance = NULL;
+
+        for (i = 0; i < count; i++) {
+                struct virt_device *dev = &devs[i];
+
+                if (dev->type == CIM_RES_TYPE_NET)
+                        instance = net_instance(broker,
+                                                &dev->dev.net,
+                                                dom,
+                                                ns);
+                else if (dev->type == CIM_RES_TYPE_DISK)
+                        instance = disk_instance(broker,
+                                                 &dev->dev.disk,
+                                                 dom,
+                                                 ns);
+                else if (dev->type == CIM_RES_TYPE_MEM)
+                        instance = mem_instance(broker,
+                                                &dev->dev.mem,
+                                                dom,
+                                                ns);
+                else if (dev->type == CIM_RES_TYPE_PROC) {
+                        proc_count = dev->dev.vcpu.quantity;
+                        continue;
+                } else
+                        return false;
+
+                if (!instance)
+                        return false;
+                
+                device_set_devid(instance, dev, dom);
+                device_set_systemname(instance, dom);
+                inst_list_add(list, instance);
+        }
+
+        if (proc_count) {
+                ret = vcpu_instances(broker,
+                                     dom,
+                                     ns,
+                                     proc_count,
+                                     list);
+        }
+
+        return true;
 }
 
 uint16_t res_type_from_device_classname(const char *classname)
@@ -290,25 +371,27 @@ static CMPIStatus _get_devices(const CMP
 {
         CMPIStatus s = {CMPI_RC_OK, NULL};
         int count;
-        int i;
+        bool rc;
         struct virt_device *devs = NULL;
 
         count = get_devices(dom, &devs, type);
         if (count <= 0)
                 goto out;
 
-        for (i = 0; i < count; i++) {
-                CMPIInstance *dev = NULL;
+        rc = device_instances(broker, 
+                              devs, 
+                              count,
+                              dom, 
+                              NAMESPACE(reference),
+                              list);
 
-                dev = device_instance(broker,
-                                      &devs[i],
-                                      dom,
-                                      NAMESPACE(reference));
-                if (dev)
-                        inst_list_add(list, dev);
+        if (!rc) {
+                cu_statusf(broker, &s,
+                           CMPI_RC_ERR_FAILED,
+                           "Couldn't get device instances");
+        }
 
-                cleanup_virt_device(&devs[i]);
-        }
+        cleanup_virt_devices(&devs, count);
 
  out:
         free(devs);
@@ -427,6 +510,29 @@ static int parse_devid(const char *devid
         return 1;
 }
 
+static int proc_dev_list(uint64_t quantity,
+                         struct virt_device **list)
+{
+        int i;
+        int rc;
+        
+
+        *list = (struct virt_device *)calloc(quantity, 
+                                             sizeof(struct virt_device));
+
+        for (i = 0; i < quantity; i++) {
+                char *dev_num;
+
+                rc = asprintf(&dev_num, "%d", i);
+
+                (*list)[i].id = strdup(dev_num);
+
+                free(dev_num);
+        }
+
+        return quantity;
+}
+
 static struct virt_device *find_dom_dev(virDomainPtr dom, 
                                         char *device,
                                         int type)
@@ -439,6 +545,17 @@ static struct virt_device *find_dom_dev(
         count = get_devices(dom, &list, type);
         if (!count)
                 goto out;
+
+        if (type == CIM_RES_TYPE_PROC) {
+                struct virt_device *tmp_list;
+                int tmp_count;
+
+                tmp_count = proc_dev_list(list[0].dev.vcpu.quantity,
+                                          &tmp_list);
+                cleanup_virt_devices(&list, count);
+                list = tmp_list;
+                count = tmp_count;
+        }
 
         for (i = 0; i < count; i++) {
                 if (STREQC(device, list[i].id))
@@ -462,10 +579,13 @@ CMPIStatus get_device_by_name(const CMPI
         CMPIStatus s = {CMPI_RC_OK, NULL};
         char *domain = NULL;
         char *device = NULL;
-        CMPIInstance *instance = NULL;
         virConnectPtr conn = NULL;
         virDomainPtr dom = NULL;
         struct virt_device *dev = NULL;
+        struct inst_list tmp_list;
+        bool rc;
+
+        inst_list_init(&tmp_list);
 
         conn = connect_by_classname(broker, CLASSNAME(reference), &s);
         if (conn == NULL) {
@@ -501,13 +621,30 @@ CMPIStatus get_device_by_name(const CMPI
                 goto err;
         }
 
-        instance = device_instance(broker, 
-                                   dev, 
-                                   dom, 
-                                   NAMESPACE(reference));
+        if (type == CIM_RES_TYPE_PROC) {
+                int ret;
+                int dev_id_num;
+                
+                ret = sscanf(dev->id, "%d", &dev_id_num);
+
+                rc = vcpu_inst(broker,
+                               dom,
+                               NAMESPACE(reference),
+                               dev_id_num,
+                               &tmp_list);
+        } else {
+                
+                rc = device_instances(broker, 
+                                      dev, 
+                                      1,
+                                      dom, 
+                                      NAMESPACE(reference),
+                                      &tmp_list);
+        }
+
         cleanup_virt_device(dev);
 
-        *_inst = instance;
+        *_inst = tmp_list.list[0];
 
  err:
         virDomainFree(dom);
@@ -515,6 +652,7 @@ CMPIStatus get_device_by_name(const CMPI
         free(device);
 
  out:
+        inst_list_free(&tmp_list);
         virConnectClose(conn);
 
         return s;        
diff -r c949b1845113 -r d9042b4ea617 src/Virt_RASD.c
--- a/src/Virt_RASD.c	Thu May 15 17:56:39 2008 -0700
+++ b/src/Virt_RASD.c	Mon May 19 18:05:40 2008 -0400
@@ -173,10 +173,8 @@ static CMPIInstance *rasd_from_vdev(cons
                 CMSetProperty(inst, "Limit",
                               (CMPIValue *)&dev->dev.mem.maxsize, CMPI_uint64);
         } else if (dev->type == CIM_RES_TYPE_PROC) {
-                /* This would be the place to set the virtualquantity. */
-                uint64_t quantity = dev->dev.vcpu.number + 1;
                 CMSetProperty(inst, "VirtualQuantity",
-                              (CMPIValue *)&quantity, CMPI_uint64);
+                              (CMPIValue *)&dev->dev.vcpu.quantity, CMPI_uint64);
         }
 
         /* FIXME: Put the HostResource in place */




More information about the Libvirt-cim mailing list