Skip to contentSkip to conversation

cpu: Add /machine/cpus[<index>] links

PATCH In review Eduardo Habkost 2 participants 6 messages May 1, 2015 – May 5, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover cpu: Add /machine/cpus[<index>] links

Changes v1 -> v2:
* Implement a object_property_add_const_link() function
* Use "/machine/cpus[<index>]" instead of "/machine/cpus/<index>"
* Grab reference to object before adding link

Eduardo Habkost (2):
qom: Implement object_property_add_const_link()
cpu: Register QOM links at /machine/cpus[<index>]

 exec.c               | 15 +++++++++++++++
 include/qom/object.h | 23 +++++++++++++++++++++++
 qom/object.c         | 15 +++++++++++++++
 3 files changed, 53 insertions(+)
Signature · 1 line
2.1.0
#1 lore raw

PATCH v2 1/2 qom: Implement object_property_add_const_link()

It can be used in simpler cases where a read-only property is needed.

---
 include/qom/object.h | 23 +++++++++++++++++++++++
 qom/object.c         | 15 +++++++++++++++
 2 files changed, 38 insertions(+)
include/qom/object.h +23 -0
index d2d7748..78bb941 100644
@@ -1123,6 +1123,8 @@ void object_property_add_child(Object *obj, const char *name,
 typedef enum {
     /* Unref the link pointer when the property is deleted */
     OBJ_PROP_LINK_UNREF_ON_RELEASE = 0x1,
+    /* Free the child pointer passed to object_property_add_link() */
+    OBJ_PROP_LINK_FREE_CHILD_POINTER = 0x2,
 } ObjectPropertyLinkFlags;
 
 /**
@@ -1162,6 +1164,8 @@ void object_property_allow_set_link(Object *, const char *,
  * property is deleted with object_property_del().  If the
  * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set,
  * the reference count is decremented when the property is deleted.
+ * If the <code>OBJ_PROP_LINK_FREE_CHILD_POINTER</code> bit is set,
+ * @child is freed when the property is deleted.
  */
 void object_property_add_link(Object *obj, const char *name,
                               const char *type, Object **child,
@@ -1170,6 +1174,25 @@ void object_property_add_link(Object *obj, const char *name,
                               ObjectPropertyLinkFlags flags,
                               Error **errp);
 
+
+/**
+ * object_property_add_const_link:
+ * @obj: the object to add a property to
+ * @name: the name of the property
+ * @type: the qobj type of the link
+ * @child: pointer to the linked object
+ * @flags: additional options for the link
+ * @errp: if an error occurs, a pointer to an area to store the error
+ *
+ * Like object_property_add_link(), but adds a link property that will
+ * always point to the same object.
+ */
+void object_property_add_const_link(Object *obj, const char *name,
+                                       const char *type, Object *child,
+                                       ObjectPropertyLinkFlags flags,
+                                       Error **errp);
+
+
 /**
  * object_property_add_str:
  * @obj: the object to add a property to
qom/object.c +15 -0
index b8dff43..7d4a46d 100644
@@ -1227,6 +1227,9 @@ static void object_release_link_property(Object *obj, const char *name,
     if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
         object_unref(*prop->child);
     }
+    if (prop->flags & OBJ_PROP_LINK_FREE_CHILD_POINTER) {
+        g_free(prop->child);
+    }
     g_free(prop);
 }
 
@@ -1266,6 +1269,18 @@ out:
     g_free(full_type);
 }
 
+void object_property_add_const_link(Object *obj, const char *name,
+                                       const char *type, Object *child,
+                                       ObjectPropertyLinkFlags flags,
+                                       Error **errp)
+{
+    Object **childp = g_new0(Object*, 1);
+
+    *childp = child;
+    object_property_add_link(obj, name, type, childp, NULL,
+                             flags | OBJ_PROP_LINK_FREE_CHILD_POINTER, errp);
+}
+
 gchar *object_get_canonical_path_component(Object *obj)
 {
     ObjectProperty *prop = NULL;
Signature · 1 line
2.1.0
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] qom: Implement object_property_add_const_link()

Review

Quoted 12 lines · Eduardo Habkost
(quoted diff) +12 -0
+void object_property_add_const_link(Object *obj, const char *name,
+                                       const char *type, Object *child,
+                                       ObjectPropertyLinkFlags flags,
+                                       Error **errp)
+{
+    Object **childp = g_new0(Object*, 1);
+
+    *childp = child;
+    object_property_add_link(obj, name, type, childp, NULL,
+                             flags | OBJ_PROP_LINK_FREE_CHILD_POINTER, errp);
+}
+

This works, but is the extra functionality needed, compared to an alias? Namely, when is flags going to be != 0?

FWIW, here is my ./.. patch. I'm all for adding a helper like object_property_add_const_link on top if we go for it.

Another possibility is to not introduce any of our patches and reuse the child<> getter and resolve functions in object_property_add_const_link.

-------------------- 8< ---------------

Quoted 1 line

From 653ca80e93fcaa94c7fa172edbaef75457d4952d Mon Sep 17 00:00:00 2001

From: Paolo Bonzini <[email protected]>
Date: Tue, 31 Mar 2015 13:56:24 +0200
Subject: [PATCH] qom: add . and .. properties

---
 qom/object.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
qom/object.c +42 -0
index b8dff43..3dd87b7 100644
@@ -1783,9 +1783,51 @@ void object_property_set_description(Object *obj, const char *name,
     op->description = g_strdup(description);
 }
 
+static void property_get_dot(Object *obj, struct Visitor *v, void *opaque,
+                             const char *name, Error **errp)
+{
+    gchar *path = object_get_canonical_path(obj);
+    visit_type_str(v, &path, name, errp);
+    g_free(path);
+}
+
+static Object *property_resolve_dot(Object *obj, void *opaque,
+                                      const gchar *part)
+{
+    return obj;
+}
+
+static void property_get_dotdot(Object *obj, struct Visitor *v, void *opaque,
+                                const char *name, Error **errp)
+{
+    if (obj->parent) {
+        gchar *path = object_get_canonical_path(obj);
+        visit_type_str(v, &path, name, errp);
+        g_free(path);
+    } else {
+        gchar *path = (gchar *)"";
+        visit_type_str(v, &path, name, errp);
+    }
+}
+
+static Object *property_resolve_dotdot(Object *obj, void *opaque,
+                                      const gchar *part)
+{
+    return obj->parent;
+}
+
 static void object_instance_init(Object *obj)
 {
+    ObjectProperty *op;
     object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
+
+    op = object_property_add(obj, ".", "link<Object>", property_get_dot,
+                             NULL, NULL, NULL, NULL);
+    op->resolve = property_resolve_dot;
+
+    op = object_property_add(obj, "..", "link<Object>", property_get_dotdot,
+                             NULL, NULL, NULL, NULL);
+    op->resolve = property_resolve_dotdot;
 }
 
 static void register_types(void)
Signature · 1 line
2.3.5
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] qom: Implement object_property_add_const_link()

Quoted 16 lines · Paolo Bonzini

Review

Quoted 12 lines · Eduardo Habkost
(quoted diff) +12 -0
+void object_property_add_const_link(Object *obj, const char *name,
+                                       const char *type, Object *child,
+                                       ObjectPropertyLinkFlags flags,
+                                       Error **errp)
+{
+    Object **childp = g_new0(Object*, 1);
+
+    *childp = child;
+    object_property_add_link(obj, name, type, childp, NULL,
+                             flags | OBJ_PROP_LINK_FREE_CHILD_POINTER, errp);
+}
+

This works, but is the extra functionality needed, compared to an alias? Namely, when is flags going to be != 0?

Flags is going to be != 0 if the caller grabs a reference to the target object (to ensure it won't disappear) and wants it to be automatically dropped when the property is removed. It is not strictly necessary, but I thought it could be useful.

But to be honest, I don't love the flags argument in object_property_add_link(), either. I mean: why do we need flags in object_property_add_link() and not in object_property_add_alias()?

Quoted 3 lines

FWIW, here is my ./.. patch. I'm all for adding a helper like object_property_add_const_link on top if we go for it.

Looks good to me.

Quoted 4 lines

Another possibility is to not introduce any of our patches and reuse the child<> getter and resolve functions in object_property_add_const_link.

Using the "." property would allow object_property_add_const_link() to be a one-liner, so it sounds better to me.

Quoted 71 lines

-------------------- 8< ---------------
From 653ca80e93fcaa94c7fa172edbaef75457d4952d Mon Sep 17 00:00:00 2001
From: Paolo Bonzini <[email protected]>
Date: Tue, 31 Mar 2015 13:56:24 +0200
Subject: [PATCH] qom: add . and .. properties

---
 qom/object.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
qom/object.c +42 -0
index b8dff43..3dd87b7 100644
@@ -1783,9 +1783,51 @@ void object_property_set_description(Object *obj, const char *name,
     op->description = g_strdup(description);
 }
 
+static void property_get_dot(Object *obj, struct Visitor *v, void *opaque,
+                             const char *name, Error **errp)
+{
+    gchar *path = object_get_canonical_path(obj);
+    visit_type_str(v, &path, name, errp);
+    g_free(path);
+}
+
+static Object *property_resolve_dot(Object *obj, void *opaque,
+                                      const gchar *part)
+{
+    return obj;
+}
+
+static void property_get_dotdot(Object *obj, struct Visitor *v, void *opaque,
+                                const char *name, Error **errp)
+{
+    if (obj->parent) {
+        gchar *path = object_get_canonical_path(obj);
+        visit_type_str(v, &path, name, errp);
+        g_free(path);
+    } else {
+        gchar *path = (gchar *)"";
+        visit_type_str(v, &path, name, errp);
+    }
+}
+
+static Object *property_resolve_dotdot(Object *obj, void *opaque,
+                                      const gchar *part)
+{
+    return obj->parent;
+}
+
 static void object_instance_init(Object *obj)
 {
+    ObjectProperty *op;
     object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
+
+    op = object_property_add(obj, ".", "link<Object>", property_get_dot,
+                             NULL, NULL, NULL, NULL);
+    op->resolve = property_resolve_dot;
+
+    op = object_property_add(obj, "..", "link<Object>", property_get_dotdot,
+                             NULL, NULL, NULL, NULL);
+    op->resolve = property_resolve_dotdot;
 }
 
 static void register_types(void)
Signature · 1 line
2.3.5
Signature · 1 line
Eduardo
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] qom: Implement object_property_add_const_link()

Quoted 29 lines · Eduardo Habkost
Quoted 16 lines · Paolo Bonzini
Quoted 8 lines · Eduardo Habkost
Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+void object_property_add_const_link(Object *obj, const char *name,
+                                       const char *type, Object *child,
+                                       ObjectPropertyLinkFlags flags,
+                                       Error **errp)
+{
+    Object **childp = g_new0(Object*, 1);
+
Quoted 8 lines
Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+    *childp = child;
+    object_property_add_link(obj, name, type, childp, NULL,
+                             flags | OBJ_PROP_LINK_FREE_CHILD_POINTER, errp);
+}
+

This works, but is the extra functionality needed, compared to an alias? Namely, when is flags going to be != 0?

Flags is going to be != 0 if the caller grabs a reference to the target object (to ensure it won't disappear) and wants it to be automatically dropped when the property is removed. It is not strictly necessary, but I thought it could be useful.

But to be honest, I don't love the flags argument in object_property_add_link(), either. I mean: why do we need flags in object_property_add_link() and not in object_property_add_alias()?

Quoted 3 lines
Quoted 3 lines

FWIW, here is my ./.. patch. I'm all for adding a helper like object_property_add_const_link on top if we go for it.

Quoted 9 lines

Looks good to me.

Quoted 4 lines
Quoted 4 lines

Another possibility is to not introduce any of our patches and reuse the child<> getter and resolve functions in object_property_add_const_link.

Using the "." property would allow object_property_add_const_link() to be a one-liner, so it sounds better to me.

You don't need this function anymore though, right? So I'll just adjust my series to use object_property_add_const_link().

Paolo

#5 lore raw

PATCH v2 2/2 cpu: Register QOM links at /machine/cpus[<index>]

In reply to Eduardo Habkost

This will provide a predictable path for the CPU objects, and a more powerful alternative for the query-cpus QMP command, as now every QOM property on CPU objects can be easily queried.

---
Changes v1 -> v2:
* Use object_property_add_const_link()
* Use "/machine/cpus[<index>]" instead of "/machine/cpus/<index>"
* Grab reference to object before adding link

Note that this doesn't replace any future topology enumeration mechanisms we may choose to implement. It just replaces the existing topology-unaware VCPU enumeration mechanism that is query-cpus.

References to previous discussions:

Date: Thu, 23 Apr 2015 10:17:36 -0300
From: Eduardo Habkost <[email protected]>
Subject: Re: [Qemu-devel] cpu modelling and hotplug (was: [PATCH RFC 0/4] target-i386: PC socket/core/thread modeling, part 1)
Message-ID: <[email protected]>

Date: Tue, 31 Mar 2015 10:16:23 -0300
From: Eduardo Habkost <[email protected]>
Subject: Re: [Qemu-devel] [PATCH v4 11/15] target-s390x: New QMP command query-cpu-model
Message-ID: <[email protected]>

Date: Fri, 16 May 2014 12:16:41 -0300
From: Eduardo Habkost <[email protected]>
Subject: Re: [Qemu-devel] [RFC 0/5] Allow object-add on X86CPU subclasses, for CPU model probing
Message-ID: <[email protected]>

---
 exec.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
exec.c +15 -0
index ae37b98..a68f0af 100644
@@ -519,6 +519,19 @@ void tcg_cpu_address_space_init(CPUState *cpu, AddressSpace *as)
 }
 #endif
 
+static void cpu_add_qom_link(CPUState *cpu)
+{
+#if !defined(CONFIG_USER_ONLY)
+    Object *obj = OBJECT(cpu);
+    char *path = g_strdup_printf("cpus[%d]", cpu->cpu_index);
+
+    object_ref(obj);
+    object_property_add_const_link(OBJECT(current_machine), path, TYPE_CPU,
+                                      obj, 0, &error_abort);
+    g_free(path);
+#endif
+}
+
 void cpu_exec_init(CPUArchState *env)
 {
     CPUState *cpu = ENV_GET_CPU(env);
@@ -558,6 +571,8 @@ void cpu_exec_init(CPUArchState *env)
     if (cc->vmsd != NULL) {
         vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
     }
+
+    cpu_add_qom_link(cpu);
 }
 
 #if defined(CONFIG_USER_ONLY)
Signature · 1 line
2.1.0