1 /*
2 * libosinfo: Required or recommended resources for an (guest) OS
3 *
4 * Copyright (C) 2009-2020 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #include <osinfo/osinfo.h>
22 #include <gio/gio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib/gi18n-lib.h>
26 #include "osinfo_resources_private.h"
27
28 enum {
29 PROP_0,
30
31 PROP_ARCHITECTURE,
32 PROP_CPU,
33 PROP_N_CPUS,
34 PROP_RAM,
35 PROP_STORAGE,
36
37 LAST_PROP
38 };
39 static GParamSpec *properties[LAST_PROP];
40
41 /**
42 * SECTION:osinfo_resources
43 * @short_description: Required or recommended resources for an (guest) OS
44 * @see_also: #OsinfoOs
45 *
46 * #OsinfoResources is an entity representing required or recommended resources
47 * for an (guest) operating system.
48 */
49
50 struct _OsinfoResourcesPrivate
51 {
52 gboolean inherit;
53 };
54
55 G_DEFINE_TYPE_WITH_PRIVATE(OsinfoResources, osinfo_resources, OSINFO_TYPE_ENTITY);
56
57 static void
osinfo_resources_finalize(GObject * object)58 osinfo_resources_finalize(GObject *object)
59 {
60 /* Chain up to the parent class */
61 G_OBJECT_CLASS(osinfo_resources_parent_class)->finalize(object);
62 }
63
64 static void
osinfo_resources_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)65 osinfo_resources_get_property(GObject *object,
66 guint property_id,
67 GValue *value,
68 GParamSpec *pspec)
69 {
70 OsinfoResources *resources = OSINFO_RESOURCES(object);
71
72 switch (property_id) {
73 case PROP_ARCHITECTURE:
74 g_value_set_string(value,
75 osinfo_resources_get_architecture(resources));
76 break;
77
78 case PROP_N_CPUS:
79 g_value_set_int(value,
80 osinfo_resources_get_n_cpus(resources));
81 break;
82
83 case PROP_CPU:
84 g_value_set_int64(value,
85 osinfo_resources_get_cpu(resources));
86 break;
87
88 case PROP_RAM:
89 g_value_set_int64(value,
90 osinfo_resources_get_ram(resources));
91 break;
92
93 case PROP_STORAGE:
94 g_value_set_int64(value,
95 osinfo_resources_get_storage(resources));
96 break;
97
98 default:
99 /* We don't have any other property... */
100 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
101 break;
102 }
103 }
104
105 static void
osinfo_resources_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)106 osinfo_resources_set_property(GObject *object,
107 guint property_id,
108 const GValue *value,
109 GParamSpec *pspec)
110 {
111 OsinfoResources *resources = OSINFO_RESOURCES(object);
112
113 switch (property_id) {
114 case PROP_ARCHITECTURE:
115 osinfo_entity_set_param(OSINFO_ENTITY(resources),
116 OSINFO_RESOURCES_PROP_ARCHITECTURE,
117 g_value_get_string(value));
118 break;
119
120 case PROP_N_CPUS:
121 osinfo_resources_set_n_cpus(resources, g_value_get_int(value));
122 break;
123
124 case PROP_CPU:
125 osinfo_resources_set_cpu(resources, g_value_get_int64(value));
126 break;
127
128 case PROP_RAM:
129 osinfo_resources_set_ram(resources, g_value_get_int64(value));
130 break;
131
132 case PROP_STORAGE:
133 osinfo_resources_set_storage(resources, g_value_get_int64(value));
134 break;
135
136 default:
137 /* We don't have any other property... */
138 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
139 break;
140 }
141 }
142
143 /* Init functions */
144 static void
osinfo_resources_class_init(OsinfoResourcesClass * klass)145 osinfo_resources_class_init(OsinfoResourcesClass *klass)
146 {
147 GObjectClass *g_klass = G_OBJECT_CLASS(klass);
148
149 g_klass->get_property = osinfo_resources_get_property;
150 g_klass->set_property = osinfo_resources_set_property;
151 g_klass->finalize = osinfo_resources_finalize;
152
153 /**
154 * OsinfoResources:architecture:
155 *
156 * The target hardware architecture to which these resources applies.
157 */
158 properties[PROP_ARCHITECTURE] = g_param_spec_string("architecture",
159 "ARCHITECTURE",
160 _("CPU Architecture"),
161 NULL /* default value */,
162 G_PARAM_READWRITE |
163 G_PARAM_CONSTRUCT_ONLY |
164 G_PARAM_STATIC_STRINGS);
165
166 /**
167 * OsinfoResources:cpu:
168 *
169 * The CPU frequency in hertz (Hz).
170 */
171 properties[PROP_CPU] = g_param_spec_int64("cpu",
172 "CPU",
173 _("CPU frequency in hertz (Hz)"),
174 -1,
175 G_MAXINT,
176 -1,
177 G_PARAM_READWRITE |
178 G_PARAM_STATIC_STRINGS);
179
180 /**
181 * OsinfoResources:n-cpus:
182 *
183 * The number of CPUs.
184 */
185 properties[PROP_N_CPUS] = g_param_spec_int("n-cpus",
186 "N-CPUs",
187 _("Number of CPUs"),
188 -1,
189 G_MAXINT,
190 -1,
191 G_PARAM_READWRITE |
192 G_PARAM_STATIC_STRINGS);
193
194 /**
195 * OsinfoResources:ram:
196 *
197 * The amount of Random Access Memory (RAM) in bytes.
198 */
199 properties[PROP_RAM] = g_param_spec_int64("ram",
200 "RAM",
201 _("Amount of Random Access Memory (RAM) in bytes"),
202 -1,
203 G_MAXINT64,
204 -1,
205 G_PARAM_READWRITE |
206 G_PARAM_STATIC_STRINGS);
207
208 /**
209 * OsinfoResources:storage:
210 *
211 * The amount of storage space in bytes.
212 */
213 properties[PROP_STORAGE] = g_param_spec_int64("storage",
214 "Storage",
215 _("Amount of storage space in bytes"),
216 -1,
217 G_MAXINT64,
218 -1,
219 G_PARAM_READWRITE |
220 G_PARAM_STATIC_STRINGS);
221
222 g_object_class_install_properties(g_klass, LAST_PROP, properties);
223 }
224
225 static void
osinfo_resources_init(OsinfoResources * resources)226 osinfo_resources_init(OsinfoResources *resources)
227 {
228 resources->priv = osinfo_resources_get_instance_private(resources);
229 }
230
osinfo_resources_new(const gchar * id,const gchar * architecture)231 OsinfoResources *osinfo_resources_new(const gchar *id,
232 const gchar *architecture)
233 {
234 OsinfoResources *resources;
235
236 resources = g_object_new(OSINFO_TYPE_RESOURCES,
237 "id", id,
238 "architecture", architecture,
239 NULL);
240
241 return resources;
242 }
243
244 /**
245 * osinfo_resources_get_architecture:
246 * @resources: an #OsinfoResources instance
247 *
248 * Retrieves the target hardware architecture to which @resources applies. Some
249 * operating systems specify the same requirements and recommendations for all
250 * architectures. In such cases, the string returned by this call will be
251 * #OSINFO_ARCHITECTURE_ALL.
252 *
253 * Returns: (transfer none): the hardware architecture.
254 */
osinfo_resources_get_architecture(OsinfoResources * resources)255 const gchar *osinfo_resources_get_architecture(OsinfoResources *resources)
256 {
257 return osinfo_entity_get_param_value(OSINFO_ENTITY(resources),
258 OSINFO_RESOURCES_PROP_ARCHITECTURE);
259 }
260
261 /**
262 * osinfo_resources_get_n_cpus:
263 * @resources: an #OsinfoResources instance
264 *
265 * Retrieves the number of CPUs.
266 *
267 * Returns: the number of CPUs, or -1.
268 */
osinfo_resources_get_n_cpus(OsinfoResources * resources)269 gint osinfo_resources_get_n_cpus(OsinfoResources *resources)
270 {
271 return (gint) osinfo_entity_get_param_value_int64
272 (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_N_CPUS);
273 }
274
275 /**
276 * osinfo_resources_get_cpu:
277 * @resources: an #OsinfoResources instance
278 *
279 * Retrieves the CPU frequency in hertz (Hz). Divide the value by #OSINFO_MEGAHERTZ if
280 * you need this value in megahertz (MHz).
281 *
282 * Returns: the CPU frequency, or -1.
283 */
osinfo_resources_get_cpu(OsinfoResources * resources)284 gint64 osinfo_resources_get_cpu(OsinfoResources *resources)
285 {
286 return osinfo_entity_get_param_value_int64
287 (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_CPU);
288 }
289
290 /**
291 * osinfo_resources_get_ram:
292 * @resources: an #OsinfoResources instance
293 *
294 * Retrieves the amount of Random Access Memory (RAM) in bytes. Divide the value
295 * by #OSINFO_MEBIBYTES if you need this value in mebibytes.
296 *
297 * Returns: the amount of RAM, or -1.
298 */
osinfo_resources_get_ram(OsinfoResources * resources)299 gint64 osinfo_resources_get_ram(OsinfoResources *resources)
300 {
301 return osinfo_entity_get_param_value_int64
302 (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_RAM);
303 }
304
305 /**
306 * osinfo_resources_get_storage:
307 * @resources: an #OsinfoResources instance
308 *
309 * Retrieves the amount of storage space in bytes. Divide the value by
310 * #OSINFO_GIBIBYTES if you need this value in gibibytes.
311 *
312 * Returns: the amount of storage, or -1.
313 */
osinfo_resources_get_storage(OsinfoResources * resources)314 gint64 osinfo_resources_get_storage(OsinfoResources *resources)
315 {
316 return osinfo_entity_get_param_value_int64
317 (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_STORAGE);
318 }
319
320 /**
321 * osinfo_resources_get_inherit
322 * @resources: an #OsinfoResources instance
323 *
324 * Returns whether its values are inherited
325 *
326 * Mind that this method is *private*!
327 */
osinfo_resources_get_inherit(OsinfoResources * resources)328 gboolean osinfo_resources_get_inherit(OsinfoResources *resources)
329 {
330 g_return_val_if_fail(OSINFO_IS_RESOURCES(resources), FALSE);
331 return resources->priv->inherit;
332 }
333
334 /**
335 * osinfo_resources_set_n_cpus:
336 * @resources: an #OsinfoResources instance
337 * @n_cpus: the number of CPUs
338 *
339 * Sets the number of CPUs.
340 */
osinfo_resources_set_n_cpus(OsinfoResources * resources,gint n_cpus)341 void osinfo_resources_set_n_cpus(OsinfoResources *resources, gint n_cpus)
342 {
343 osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
344 OSINFO_RESOURCES_PROP_N_CPUS,
345 n_cpus);
346 }
347
348 /**
349 * osinfo_resources_set_cpu:
350 * @resources: an #OsinfoResources instance
351 * @cpu: the CPU frequency in hertz (Hz)
352 *
353 * Sets the CPU frequency.
354 */
osinfo_resources_set_cpu(OsinfoResources * resources,gint64 cpu)355 void osinfo_resources_set_cpu(OsinfoResources *resources, gint64 cpu)
356 {
357 osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
358 OSINFO_RESOURCES_PROP_CPU,
359 cpu);
360 }
361
362 /**
363 * osinfo_resources_set_ram:
364 * @resources: an #OsinfoResources instance
365 * @ram: the amount of ram in bytes
366 *
367 * Sets the amount of RAM in bytes.
368 */
osinfo_resources_set_ram(OsinfoResources * resources,gint64 ram)369 void osinfo_resources_set_ram(OsinfoResources *resources, gint64 ram)
370 {
371 osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
372 OSINFO_RESOURCES_PROP_RAM,
373 ram);
374 }
375
376 /**
377 * osinfo_resources_set_storage:
378 * @resources: an #OsinfoResources instance
379 * @storage: the amount of storage in bytes
380 *
381 * Sets the amount of storage space.
382 */
osinfo_resources_set_storage(OsinfoResources * resources,gint64 storage)383 void osinfo_resources_set_storage(OsinfoResources *resources, gint64 storage)
384 {
385 osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
386 OSINFO_RESOURCES_PROP_STORAGE,
387 storage);
388 }
389
390 /**
391 * osinfo_resources_set_inherit
392 * @resources: an #OsinfoResources instance
393 * @inherit: whether its values are inherited
394 *
395 * Sets whether the resources values are inherited
396 *
397 * Mind that this method is *private*!
398 */
osinfo_resources_set_inherit(OsinfoResources * resources,gboolean inherit)399 void osinfo_resources_set_inherit(OsinfoResources *resources, gboolean inherit)
400 {
401 g_return_if_fail(OSINFO_IS_RESOURCES(resources));
402
403 resources->priv->inherit = inherit;
404 }
405