os-cmpi-xen

view src/Xen_MemoryPool_Resource.c @ 121:4868ace2726b

Add initial consistence checks in test suite for Xen_MemoryPool, Xen_ProcessorPool and Xen_VirtualSystemManagementService.

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents f5aee5f0c44f
children
line source
1 // Copyright (C) 2006 IBM Corporation
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 // ============================================================================
17 // Authors: Dr. Gareth S. Bestor, <bestor@us.ibm.com>
18 // Contributors: Jim Fehlig, <jfehlig@novell.com>
19 // Description:
20 // ============================================================================
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include "Xen_MemoryPool_Resource.h"
28 /* Include the required CMPI data types, function headers, and macros. */
29 #include "cmpidt.h"
30 #include "cmpift.h"
31 #include "cmpimacs.h"
33 // ----------------------------------------------------------------------------
35 static int populate_resource(_RESOURCE *resource)
36 {
37 /* Get the total host memory from 'xm info' fields. */
38 /*
39 * TODO:
40 * What if we can't determine the total memory or free
41 * memory on host?
42 */
43 FILE * cmd;
44 unsigned long long totalmemory = 0;
45 if ((cmd = popen("xm info | gawk '/^total_memory/ {print $3}'","r"))) {
46 fscanf(cmd, "%llu", &totalmemory);
47 pclose(cmd);
48 }
49 resource->capacity = totalmemory << 20; /* MB -> Bytes */
51 /* Get the unallocated available memory for DomU's.
52 * Use the max_free_memory field if it exists, otherwise use
53 * free_memory.
54 */
55 unsigned long long freememory = 0;
56 if ((cmd = popen("xm info | gawk '/^max_free_memory/ {print $3}'","r"))) {
57 if (fscanf(cmd, "%llu", &freememory) != 1) {
58 pclose(cmd);
59 if ((cmd = popen("xm info | gawk '/^free_memory/ {print $3}'","r")))
60 fscanf(cmd, "%llu", &freememory);
61 }
62 }
63 pclose(cmd);
65 /*
66 * TODO:
67 * What if freememory > totalmemory? For now we'll say all memory
68 * is reserved and avoid propogating some strange situation.
69 */
70 if (freememory > totalmemory)
71 resource->reserved = resource->capacity;
72 else
73 resource->reserved = (totalmemory - freememory) << 20; /* MB -> Bytes */
75 return 1;
76 }
78 /* Get a handle to the list of all system resources for this class. */
79 int Xen_MemoryPool_getResources( _RESOURCES ** resources )
80 {
81 /* malloc a new handle for the resources list. */
82 *resources = (_RESOURCES *)malloc(sizeof(_RESOURCES));
83 if (*resources == NULL) return 0;
85 (*resources)->finished = 0;
87 return 1;
88 }
90 // ----------------------------------------------------------------------------
92 /* Free/deallocate/cleanup the resources list after use. */
93 int Xen_MemoryPool_freeResources( _RESOURCES * resources )
94 {
95 if (resources != NULL) {
96 free(resources);
97 resources = NULL;
98 }
100 return 1;
101 }
103 // ----------------------------------------------------------------------------
105 /* Iterator to get the next resource from the resources list. */
106 int Xen_MemoryPool_getNextResource( _RESOURCES * resources, _RESOURCE ** resource )
107 {
108 if (resources == NULL) return 0;
109 if (resources->finished) return 0;
111 /* Generate raw data for the single resource. */
112 *resource = (_RESOURCE *)malloc(sizeof(_RESOURCE));
113 if (*resource == NULL) return 0;
115 if (!populate_resource(*resource)) {
116 free(*resource);
117 return 0;
118 }
120 /* Single resource so no more resources in the list. */
121 resources->finished = 1;
123 return 1;
124 }
126 // ----------------------------------------------------------------------------
128 /* Get the specific resource that matches the CMPI object path. */
129 int Xen_MemoryPool_getResourceForObjectPath( _RESOURCES * resources, _RESOURCE ** resource, const CMPIObjectPath * objectpath )
130 {
131 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
133 if (resources == NULL) return 0;
134 if (CMIsNullObject(objectpath)) return 0;
136 /* Obtain the name from the CMPIObjectPath key. */
137 CMPIData namedata = CMGetKey(objectpath, "InstanceID", &status);
138 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(namedata)) return 0;
140 /* Extract the name string from the CMPIString. */
141 char * name = CMGetCharPtr(namedata.value.string);
142 if ((name == NULL) || (*name == '\0')) return 0;
144 /* Check that the name is correct. */
145 if (strcmp(name, "XenMemoryPool") != 0) return 0;
147 /* Generate raw data for the single resource. */
148 *resource = (_RESOURCE *)malloc(sizeof(_RESOURCE));
149 if (*resource == NULL) return 0;
151 if (!populate_resource(*resource)) {
152 free(*resource);
153 return 0;
154 }
156 return 1;
157 }
159 // ----------------------------------------------------------------------------
161 /* Free/deallocate/cleanup the resource after use. */
162 int Xen_MemoryPool_freeResource( _RESOURCE * resource )
163 {
164 if (resource != NULL) {
165 free(resource);
166 resource = NULL;
167 }
169 return 1;
170 }
172 // ----------------------------------------------------------------------------
174 /* Set the property values of a CMPI instance from a specific resource. */
175 int Xen_MemoryPool_setInstanceFromResource( _RESOURCE * resource, const CMPIInstance * instance, const CMPIBroker * broker )
176 {
177 if (resource == NULL) return 0;
178 if (CMIsNullObject(instance)) return 0;
180 /* Set the CMPIInstance properties from the resource data. */
181 CMSetProperty(instance, "InstanceID",(CMPIValue *)"XenMemoryPool", CMPI_chars);
182 CMSetProperty(instance, "PoolID",(CMPIValue *)"XenMemoryPool", CMPI_chars);
184 int type = 4; /* 4 == Memory */
185 CMSetProperty(instance, "ResourceType",(CMPIValue *)&type, CMPI_uint16);
186 CMSetProperty(instance, "AllocationUnits",(CMPIValue *)"Bytes", CMPI_chars);
188 CMSetProperty(instance, "Capacity",(CMPIValue *)&(resource->capacity), CMPI_uint64);
189 CMSetProperty(instance, "Reserved",(CMPIValue *)&(resource->reserved), CMPI_uint64);
191 int primordial = 1;
192 CMSetProperty(instance, "Primordial" , (CMPIValue *)&primordial, CMPI_boolean);
194 return 1;
195 }
197 // ----------------------------------------------------------------------------
199 /* Delete the specified resource from the system. */
200 int Xen_MemoryPool_deleteResource( _RESOURCES * resources, _RESOURCE * resource )
201 {
202 /* Unsupported. */
203 return -1;
204 }
206 // ----------------------------------------------------------------------------
208 /* Modify the specified resource using the property values of a CMPI instance. */
209 int Xen_MemoryPool_setResourceFromInstance( _RESOURCE * resource, const CMPIInstance * instance, const CMPIBroker * broker )
210 {
211 /* Unsupported. */
212 return -1;
213 }
215 // ----------------------------------------------------------------------------
217 /* Create a new resource using the property values of a CMPI instance. */
218 int Xen_MemoryPool_createResourceFromInstance( _RESOURCES * resources, _RESOURCE ** resource, const CMPIInstance * instance, const CMPIBroker * broker )
219 {
220 /* Unsupported. */
221 return -1;
222 }