Skip to contentSkip to conversation

qdev: handle global properties after all instance_init calls

PATCH Superseded Eduardo Habkost 3 participants 17 messages Oct 3, 2012 – Jul 28, 2013

lore
Share & tools
lore
#0 lore raw

cover qdev: handle global properties after all instance_init calls

Summary:
- Object properties are registered by the classes' instance_init() functions
- qdev_prop_set_globals() needs all properties to be registered before being called.
- Hence, qdev_prop_set_globals() can't be called from device_initfn().

Reference:
http://article.gmane.org/gmane.comp.emulators.qemu/173753

This series is a larger than the single-patch suggestion sent by Igor (URL above), just because I wanted to include a unit test for the new code. To do that, I pulled the qdev-split code sent previously to the list, so a qdev unit test could be written without pulling too many dependencies.

Patches 1-3 are just code movement, patch 3 adds the qdev unit test, patches 5-6 finally introduce post_init(), and move the qdev_prop_set_globals() call to post_init().

Anthony Liguori (1):
qdev: split up header so it can be used in cpu.h

Eduardo Habkost (4):
qdev: separate core from the code used only by qemu-system-*
tests: unit tests for qdev global-properties handling
qom: introduce post_init() function
qdev: set globals on post_init() function

Igor Mammedov (1):
qapi-types.h doesn't really need to include qemu-common.h

 hw/Makefile.objs               |   1 +
 hw/irq.h                       |   2 +
 hw/mc146818rtc.c               |   1 +
 hw/qdev-addr.c                 |   1 +
 hw/qdev-core.h                 | 240 ++++++++++++++++++++++++++
 hw/qdev-monitor.h              |  16 ++
 hw/qdev-properties-system.c    | 329 ++++++++++++++++++++++++++++++++++++
 hw/qdev-properties.c           | 321 +----------------------------------
 hw/qdev-properties.h           | 131 +++++++++++++++
 hw/qdev-system.c               |  93 +++++++++++
 hw/qdev.c                      | 102 +----------
 hw/qdev.h                      | 371 +----------------------------------------
 include/qemu/object.h          |   3 +
 qom/object.c                   |  14 ++
 scripts/qapi-types.py          |   3 +-
 tests/Makefile                 |   6 +
 tests/fake-qdev.c              |  52 ++++++
 tests/test-qdev-global-props.c | 178 ++++++++++++++++++++
 18 files changed, 1083 insertions(+), 781 deletions(-)
 create mode 100644 hw/qdev-core.h
 create mode 100644 hw/qdev-monitor.h
 create mode 100644 hw/qdev-properties-system.c
 create mode 100644 hw/qdev-properties.h
 create mode 100644 hw/qdev-system.c
 create mode 100644 tests/fake-qdev.c
 create mode 100644 tests/test-qdev-global-props.c
Signature · 1 line
1.7.11.4
#2 lore raw

PATCH 2/6 qapi-types.h doesn't really need to include qemu-common.h

From: Igor Mammedov <[email protected]>

needed to prevent build breakage when CPU becomes a child of DeviceState

[ehabkost: include <stdbool.h> too]

---
 scripts/qapi-types.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/qapi-types.py +2 -1
index 49ef569..6e0e6ba 100644
@@ -273,7 +273,8 @@ fdecl.write(mcgen('''
 #ifndef %(guard)s
 #define %(guard)s
 
-#include "qemu-common.h"
+#include <stdbool.h>
+#include <stdint.h>
 
 ''',
                   guard=guardname(h_file)))
Signature · 1 line
1.7.11.4
#5 lore raw

PATCH 5/6 qom: introduce post_init() function

In reply to Eduardo Habkost

This will allow classes to specify a function to be called after all instance_init() functions were called.

This will be used by DeviceState to call qdev_prop_set_globals() at the right moment.

---
 include/qemu/object.h |  3 +++
 qom/object.c          | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
include/qemu/object.h +3 -0
index cc75fee..ca7e38b 100644
@@ -276,6 +276,8 @@ struct Object
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
+ * @instance_post_init: This function is called to finish initialization of
+ *   an object, after all instance_init functions were called.
  * @instance_finalize: This function is called during object destruction.  This
  *   is called before the parent @instance_finalize function has been called.
  *   An object should only free the members that are unique to its type in this
@@ -311,6 +313,7 @@ struct TypeInfo
 
     size_t instance_size;
     void (*instance_init)(Object *obj);
+    void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
 
     bool abstract;
qom/object.c +14 -0
index e3e9242..eed5e05 100644
@@ -49,6 +49,7 @@ struct TypeImpl
     void *class_data;
 
     void (*instance_init)(Object *obj);
+    void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
 
     bool abstract;
@@ -109,6 +110,7 @@ static TypeImpl *type_register_internal(const TypeInfo *info)
     ti->class_data = info->class_data;
 
     ti->instance_init = info->instance_init;
+    ti->instance_post_init = info->instance_post_init;
     ti->instance_finalize = info->instance_finalize;
 
     ti->abstract = info->abstract;
@@ -295,6 +297,17 @@ static void object_init_with_type(Object *obj, TypeImpl *ti)
     }
 }
 
+static void object_post_init_with_type(Object *obj, TypeImpl *ti)
+{
+    if (ti->instance_post_init) {
+        ti->instance_post_init(obj);
+    }
+
+    if (type_has_parent(ti)) {
+        object_post_init_with_type(obj, type_get_parent(ti));
+    }
+}
+
 void object_initialize_with_type(void *data, TypeImpl *type)
 {
     Object *obj = data;
@@ -309,6 +322,7 @@ void object_initialize_with_type(void *data, TypeImpl *type)
     obj->class = type->class;
     QTAILQ_INIT(&obj->properties);
     object_init_with_type(obj, type);
+    object_post_init_with_type(obj, type);
 }
 
 void object_initialize(void *data, const char *typename)
Signature · 1 line
1.7.11.4
#4 lore raw

PATCH 4/6 tests: unit tests for qdev global-properties handling

In reply to Eduardo Habkost

This tests the qdev global-properties handling code.

A fake-qdev.c module was added with some stub functions, to keep dependencies under control.

---
 tests/Makefile                |   6 +++
 tests/fake-qdev.c             |  52 ++++++++++++++++++++
 tests/test-qdev-global-prop.c | 109 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+)
 create mode 100644 tests/fake-qdev.c
 create mode 100644 tests/test-qdev-global-prop.c
tests/Makefile +6 -0
index 26a67ce..2433be7 100644
@@ -15,6 +15,7 @@ check-unit-y += tests/test-string-output-visitor$(EXESUF)
 check-unit-y += tests/test-coroutine$(EXESUF)
 check-unit-y += tests/test-visitor-serialization$(EXESUF)
 check-unit-y += tests/test-iov$(EXESUF)
+check-unit-y += tests/test-qdev-global-prop$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -50,6 +51,11 @@ tests/check-qfloat$(EXESUF): tests/check-qfloat.o qfloat.o $(tools-obj-y)
 tests/check-qjson$(EXESUF): tests/check-qjson.o $(qobject-obj-y) $(tools-obj-y)
 tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(coroutine-obj-y) $(tools-obj-y)
 tests/test-iov$(EXESUF): tests/test-iov.o iov.o
+tests/test-qdev-global-prop$(EXESUF): tests/test-qdev-global-prop.o \
+	hw/qdev.o hw/qdev-properties.o \
+	$(test-qapi-obj-y) $(qom-obj-y) \
+	qemu-option.o \
+	tests/fake-qdev.o
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/fake-qdev.c new file +52 -0

52 lines not on this page · raw message

tests/test-qdev-global-prop.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
1.7.11.4
#6 lore raw

PATCH 6/6 qdev: set globals on post_init() function

In reply to Eduardo Habkost

This way, properties registerd in the instance_init() function of children classes will be handled properly by qdev_prop_set_globals(), too.

Includes a new unit test for the new functionality.

---
 hw/qdev.c                      |   9 ++-
 tests/Makefile                 |   4 +-
 tests/test-qdev-global-prop.c  | 109 -------------------------
 tests/test-qdev-global-props.c | 178 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 187 insertions(+), 113 deletions(-)
 delete mode 100644 tests/test-qdev-global-prop.c
 create mode 100644 tests/test-qdev-global-props.c
hw/qdev.c +7 -2
index 9e73655..68c5ccb 100644
@@ -591,12 +591,16 @@ static void device_initfn(Object *obj)
         }
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    qdev_prop_set_globals(dev);
-
     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
                              (Object **)&dev->parent_bus, NULL);
 }
 
+static void device_post_init(Object *obj)
+{
+    DeviceState *dev = DEVICE(obj);
+    qdev_prop_set_globals(dev);
+}
+
 /* Unlink device from bus and free the structure.  */
 static void device_finalize(Object *obj)
 {
@@ -659,6 +663,7 @@ static TypeInfo device_type_info = {
     .parent = TYPE_OBJECT,
     .instance_size = sizeof(DeviceState),
     .instance_init = device_initfn,
+    .instance_post_init = device_post_init,
     .instance_finalize = device_finalize,
     .class_base_init = device_class_base_init,
     .abstract = true,
tests/Makefile +2 -2
index 2433be7..9f86162 100644
@@ -15,7 +15,7 @@ check-unit-y += tests/test-string-output-visitor$(EXESUF)
 check-unit-y += tests/test-coroutine$(EXESUF)
 check-unit-y += tests/test-visitor-serialization$(EXESUF)
 check-unit-y += tests/test-iov$(EXESUF)
-check-unit-y += tests/test-qdev-global-prop$(EXESUF)
+check-unit-y += tests/test-qdev-global-props$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -51,7 +51,7 @@ tests/check-qfloat$(EXESUF): tests/check-qfloat.o qfloat.o $(tools-obj-y)
 tests/check-qjson$(EXESUF): tests/check-qjson.o $(qobject-obj-y) $(tools-obj-y)
 tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(coroutine-obj-y) $(tools-obj-y)
 tests/test-iov$(EXESUF): tests/test-iov.o iov.o
-tests/test-qdev-global-prop$(EXESUF): tests/test-qdev-global-prop.o \
+tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
 	hw/qdev.o hw/qdev-properties.o \
 	$(test-qapi-obj-y) $(qom-obj-y) \
 	qemu-option.o \
tests/test-qdev-global-prop.c deleted +0 -109

109 lines not on this page · raw message

tests/test-qdev-global-props.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
1.7.11.4
#3 lore raw

PATCH 3/6 qdev: separate core from the code used only by qemu-system-*

In reply to Eduardo Habkost

This change should help on two things:
- Allowing DeviceState to be used by *-user;
- Writing qdev unit tests without pulling too many dependencies.

Note that there are two parts that depend on code compiled only on qemu-system-*, but are still inside qdev.c:
- vmstate handling
- reset function registration.

Those two parts will need to be handled somehow (by being moved outside of qdev-core, or compiled out, or by adding equivalent implementations to *-user), to allow the qdev core to be used by *-user.

---
 hw/Makefile.objs            |   1 +
 hw/qdev-properties-system.c | 329 ++++++++++++++++++++++++++++++++++++++++++++
 hw/qdev-properties.c        | 320 +-----------------------------------------
 hw/qdev-properties.h        |   1 +
 hw/qdev-system.c            |  93 +++++++++++++
 hw/qdev.c                   |  92 -------------
 6 files changed, 425 insertions(+), 411 deletions(-)
 create mode 100644 hw/qdev-properties-system.c
 create mode 100644 hw/qdev-system.c
hw/Makefile.objs +1 -0
index ecdbe44..030cb2d 100644
@@ -179,6 +179,7 @@ common-obj-y += bt.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o
 common-obj-y += bt-hci-csr.o
 common-obj-y += msmouse.o ps2.o
 common-obj-y += qdev.o qdev-properties.o qdev-monitor.o
+common-obj-y += qdev-system.o qdev-properties-system.o
 common-obj-$(CONFIG_BRLAPI) += baum.o
 
 # xen backend driver support
hw/qdev-properties-system.c new file +329 -0

329 lines not on this page · raw message

hw/qdev-properties.c +1 -319

347 lines not on this page · raw message

hw/qdev-properties.h +1 -0
index 5b046ab..ddcf774 100644
@@ -116,6 +116,7 @@ void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
 /* FIXME: Remove opaque pointer properties.  */
 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
 
+void qdev_prop_register_global(GlobalProperty *prop);
 void qdev_prop_register_global_list(GlobalProperty *props);
 void qdev_prop_set_globals(DeviceState *dev);
 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
hw/qdev-system.c new file +93 -0

93 lines not on this page · raw message

hw/qdev.c +0 -92

110 lines not on this page · raw message

Signature · 1 line
1.7.11.4
#1 lore raw

PATCH 1/6 qdev: split up header so it can be used in cpu.h

In reply to Eduardo Habkost

From: Anthony Liguori <[email protected]>

Header file dependency is a frickin' nightmare right now. cpu.h tends to get included in our 'include everything' header files but qdev also needs to include those headers mainly for qdev-properties since it knows about CharDriverState and friends.

We can solve this for now by splitting out qdev.h along the same lines that we previously split the C file. Then cpu.h just needs to include qdev-core.h

[ehabkost: re-add DEFINE_PROP_PCI_HOST_DEVADDR, that was removed on the
original patch (by mistake, I guess)]
[ehabkost: kill qdev_prop_set_vlan() declaration]

---
 hw/irq.h             |   2 +
 hw/mc146818rtc.c     |   1 +
 hw/qdev-addr.c       |   1 +
 hw/qdev-core.h       | 240 +++++++++++++++++++++++++++++++++
 hw/qdev-monitor.h    |  16 +++
 hw/qdev-properties.c |   1 +
 hw/qdev-properties.h | 130 ++++++++++++++++++
 hw/qdev.c            |   1 +
 hw/qdev.h            | 371 +--------------------------------------------------
 9 files changed, 396 insertions(+), 367 deletions(-)
 create mode 100644 hw/qdev-core.h
 create mode 100644 hw/qdev-monitor.h
 create mode 100644 hw/qdev-properties.h
hw/irq.h +2 -0
index 56c55f0..1339a3a 100644
@@ -3,6 +3,8 @@
 
 /* Generic IRQ/GPIO pin infrastructure.  */
 
+typedef struct IRQState *qemu_irq;
+
 typedef void (*qemu_irq_handler)(void *opaque, int n, int level);
 
 void qemu_set_irq(qemu_irq irq, int level);
hw/mc146818rtc.c +1 -0
index d63554f..1718a57 100644
@@ -25,6 +25,7 @@
 #include "qemu-timer.h"
 #include "sysemu.h"
 #include "mc146818rtc.h"
+#include "qapi/qapi-visit-core.h"
 
 #ifdef TARGET_I386
 #include "apic.h"
hw/qdev-addr.c +1 -0
index b711b6b..5b5d38f 100644
@@ -1,6 +1,7 @@
 #include "qdev.h"
 #include "qdev-addr.h"
 #include "targphys.h"
+#include "qapi/qapi-visit-core.h"
 
 /* --- target physical address --- */
 
hw/qdev-core.h new file +240 -0

240 lines not on this page · raw message

hw/qdev-monitor.h new file +16 -0
new file mode 100644
index 0000000..220ceba
@@ -0,0 +1,16 @@
+#ifndef QEMU_QDEV_MONITOR_H
+#define QEMU_QDEV_MONITOR_H
+
+#include "qdev-core.h"
+#include "monitor.h"
+
+/*** monitor commands ***/
+
+void do_info_qtree(Monitor *mon);
+void do_info_qdm(Monitor *mon);
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int qdev_device_help(QemuOpts *opts);
+DeviceState *qdev_device_add(QemuOpts *opts);
+
+#endif
hw/qdev-properties.c +1 -0
index 8aca0d4..81d901c 100644
@@ -4,6 +4,7 @@
 #include "blockdev.h"
 #include "hw/block-common.h"
 #include "net/hub.h"
+#include "qapi/qapi-visit-core.h"
 
 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
 {
hw/qdev-properties.h new file +130 -0

130 lines not on this page · raw message

hw/qdev.c +1 -0
index b5a52ac..d0775f6 100644
@@ -29,6 +29,7 @@
 #include "qdev.h"
 #include "sysemu.h"
 #include "error.h"
+#include "qapi/qapi-visit-core.h"
 
 int qdev_hotplug = 0;
 static bool qdev_hot_added = false;
hw/qdev.h +4 -367

376 lines not on this page · raw message

Signature · 1 line
1.7.11.4
#7 lore raw
In reply to Eduardo Habkost

Ping for KVM call discussion

Am 03.10.2012 19:48, schrieb Eduardo Habkost:

Quoted 58 lines

Summary:
- Object properties are registered by the classes' instance_init() functions
- qdev_prop_set_globals() needs all properties to be registered before being called.
- Hence, qdev_prop_set_globals() can't be called from device_initfn().

Reference:
http://article.gmane.org/gmane.comp.emulators.qemu/173753

This series is a larger than the single-patch suggestion sent by Igor (URL above), just because I wanted to include a unit test for the new code. To do that, I pulled the qdev-split code sent previously to the list, so a qdev unit test could be written without pulling too many dependencies.

Patches 1-3 are just code movement, patch 3 adds the qdev unit test, patches 5-6 finally introduce post_init(), and move the qdev_prop_set_globals() call to post_init().

Anthony Liguori (1):
qdev: split up header so it can be used in cpu.h

Eduardo Habkost (4):
qdev: separate core from the code used only by qemu-system-*
tests: unit tests for qdev global-properties handling
qom: introduce post_init() function
qdev: set globals on post_init() function

Igor Mammedov (1):
qapi-types.h doesn't really need to include qemu-common.h

 hw/Makefile.objs               |   1 +
 hw/irq.h                       |   2 +
 hw/mc146818rtc.c               |   1 +
 hw/qdev-addr.c                 |   1 +
 hw/qdev-core.h                 | 240 ++++++++++++++++++++++++++
 hw/qdev-monitor.h              |  16 ++
 hw/qdev-properties-system.c    | 329 ++++++++++++++++++++++++++++++++++++
 hw/qdev-properties.c           | 321 +----------------------------------
 hw/qdev-properties.h           | 131 +++++++++++++++
 hw/qdev-system.c               |  93 +++++++++++
 hw/qdev.c                      | 102 +----------
 hw/qdev.h                      | 371 +----------------------------------------
 include/qemu/object.h          |   3 +
 qom/object.c                   |  14 ++
 scripts/qapi-types.py          |   3 +-
 tests/Makefile                 |   6 +
 tests/fake-qdev.c              |  52 ++++++
 tests/test-qdev-global-props.c | 178 ++++++++++++++++++++
 18 files changed, 1083 insertions(+), 781 deletions(-)
 create mode 100644 hw/qdev-core.h
 create mode 100644 hw/qdev-monitor.h
 create mode 100644 hw/qdev-properties-system.c
 create mode 100644 hw/qdev-properties.h
 create mode 100644 hw/qdev-system.c
 create mode 100644 tests/fake-qdev.c
 create mode 100644 tests/test-qdev-global-props.c
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw

v2 cover qdev: handle global properties after all instance_init calls

Updated version of the series sent in October 2012.

References:
http://article.gmane.org/gmane.comp.emulators.qemu/173753
(old discussion)
http://article.gmane.org/gmane.comp.emulators.qemu/173782
(previous version of this series)
http://article.gmane.org/gmane.comp.emulators.kvm.devel/112380
(Minutes of last call where this was discussed)

Eduardo Habkost (3):
tests: unit tests for qdev global-properties handling
qom: introduce post_init() function
qdev: set globals on post_init() function

 hw/core/qdev.c                 |  10 ++-
 include/qom/object.h           |   3 +
 qom/object.c                   |  14 ++++
 tests/.gitignore               |   1 +
 tests/Makefile                 |   7 ++
 tests/test-qdev-global-props.c | 178 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 212 insertions(+), 1 deletion(-)
 create mode 100644 tests/test-qdev-global-props.c
Signature · 1 line
1.8.1.4
#9 lore raw

RFC v2 1/3 tests: unit tests for qdev global-properties handling

This tests the qdev global-properties handling code.

---
 tests/.gitignore               |   1 +
 tests/Makefile                 |   7 +++
 tests/test-qdev-global-props.c | 109 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 tests/test-qdev-global-props.c
tests/.gitignore +1 -0
index fb05c2a..3586416 100644
@@ -18,4 +18,5 @@ test-qmp-marshal.c
 test-thread-pool
 test-x86-cpuid
 test-xbzrle
+test-qdev-global-props
 *-test
tests/Makefile +7 -0
index 279d5f8..9849441 100644
@@ -47,6 +47,7 @@ gcov-files-test-mul64-y = util/host-utils.c
 check-unit-y += tests/test-int128$(EXESUF)
 # all code tested by test-int128 is inside int128.h
 gcov-files-test-int128-y =
+check-unit-y += tests/test-qdev-global-props$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -102,6 +103,12 @@ tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o
 tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o xbzrle.o page_cache.o libqemuutil.a
 tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o
 tests/test-int128$(EXESUF): tests/test-int128.o
+tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
+	hw/core/qdev.o hw/core/qdev-properties.o \
+	hw/core/irq.o \
+	qom/object.o qom/container.o qom/qom-qobject.o \
+	$(test-qapi-obj-y) \
+	libqemuutil.a libqemustub.a
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-qdev-global-props.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#10 lore raw

RFC v2 2/3 qom: introduce post_init() function

In reply to Eduardo Habkost

This will allow classes to specify a function to be called after all instance_init() functions were called.

This will be used by DeviceState to call qdev_prop_set_globals() at the right moment.

---
 include/qom/object.h |  3 +++
 qom/object.c         | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
include/qom/object.h +3 -0
index 23fc048..1a54f64 100644
@@ -398,6 +398,8 @@ struct Object
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
+ * @instance_post_init: This function is called to finish initialization of
+ *   an object, after all instance_init functions were called.
  * @instance_finalize: This function is called during object destruction.  This
  *   is called before the parent @instance_finalize function has been called.
  *   An object should only free the members that are unique to its type in this
@@ -433,6 +435,7 @@ struct TypeInfo
 
     size_t instance_size;
     void (*instance_init)(Object *obj);
+    void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
 
     bool abstract;
qom/object.c +14 -0
index b2479d1..74fd241 100644
@@ -51,6 +51,7 @@ struct TypeImpl
     void *class_data;
 
     void (*instance_init)(Object *obj);
+    void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
 
     bool abstract;
@@ -111,6 +112,7 @@ static TypeImpl *type_register_internal(const TypeInfo *info)
     ti->class_data = info->class_data;
 
     ti->instance_init = info->instance_init;
+    ti->instance_post_init = info->instance_post_init;
     ti->instance_finalize = info->instance_finalize;
 
     ti->abstract = info->abstract;
@@ -298,6 +300,17 @@ static void object_init_with_type(Object *obj, TypeImpl *ti)
     }
 }
 
+static void object_post_init_with_type(Object *obj, TypeImpl *ti)
+{
+    if (ti->instance_post_init) {
+        ti->instance_post_init(obj);
+    }
+
+    if (type_has_parent(ti)) {
+        object_post_init_with_type(obj, type_get_parent(ti));
+    }
+}
+
 void object_initialize_with_type(void *data, TypeImpl *type)
 {
     Object *obj = data;
@@ -313,6 +326,7 @@ void object_initialize_with_type(void *data, TypeImpl *type)
     object_ref(obj);
     QTAILQ_INIT(&obj->properties);
     object_init_with_type(obj, type);
+    object_post_init_with_type(obj, type);
 }
 
 void object_initialize(void *data, const char *typename)
Signature · 1 line
1.8.1.4
#13 lore raw

RFC v2 3/3 qdev: set globals on post_init() function

In reply to Eduardo Habkost

This way, properties registered in the instance_init() function of children classes will be handled properly by qdev_prop_set_globals(), too.

Includes a new unit test for the new functionality.

---
 hw/core/qdev.c                 | 10 +++++-
 tests/test-qdev-global-props.c | 71 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 79 insertions(+), 2 deletions(-)
hw/core/qdev.c +9 -1
index 9190a7e..e0acb6c 100644
@@ -752,7 +752,6 @@ static void device_initfn(Object *obj)
         }
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    qdev_prop_set_globals(dev, &err);
     if (err != NULL) {
         qerror_report_err(err);
         error_free(err);
@@ -764,6 +763,14 @@ static void device_initfn(Object *obj)
     assert_no_error(err);
 }
 
+static void device_post_init(Object *obj)
+{
+    Error *err = NULL;
+    DeviceState *dev = DEVICE(obj);
+    qdev_prop_set_globals(dev, &err);
+    assert_no_error(err);
+}
+
 /* Unlink device from bus and free the structure.  */
 static void device_finalize(Object *obj)
 {
@@ -853,6 +860,7 @@ static const TypeInfo device_type_info = {
     .parent = TYPE_OBJECT,
     .instance_size = sizeof(DeviceState),
     .instance_init = device_initfn,
+    .instance_post_init = device_post_init,
     .instance_finalize = device_finalize,
     .class_base_init = device_class_base_init,
     .class_init = device_class_init,
tests/test-qdev-global-props.c +70 -1

98 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#11 lore raw

Re: [Qemu-devel] [RFC 2/3 v2] qom: introduce post_init() function

In reply to Eduardo Habkost
Quoted 26 lines · On Wed, 10 Jul 2013 17:08:41 -0300 Eduardo Habkost <[email protected]> wrote:

This will allow classes to specify a function to be called after all instance_init() functions were called.

This will be used by DeviceState to call qdev_prop_set_globals() at the right moment.

---
 include/qom/object.h |  3 +++
 qom/object.c         | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
include/qom/object.h +2 -0
index 23fc048..1a54f64 100644
@@ -398,6 +398,8 @@ struct Object
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
+ * @instance_post_init: This function is called to finish initialization of
+ *   an object, after all instance_init functions were called.
  * @instance_finalize: This function is called during object destruction.  This
  *   is called before the parent @instance_finalize function has been called.
  *   An object should only free the members that are unique to its type in this
@@ -433,6 +435,7 @@ struct TypeInfo

[...]

include/qom/object.h

Quoted 10 lines
include/qom/object.h +9 -0
+static void object_post_init_with_type(Object *obj, TypeImpl *ti)
+{
+    if (ti->instance_post_init) {
+        ti->instance_post_init(obj);
+    }
+
+    if (type_has_parent(ti)) {
+        object_post_init_with_type(obj, type_get_parent(ti));
+    }

Is there a reason why post init called in opposite order than instance_init() ?

Quoted 13 lines
include/qom/object.h +3 -0
+}
+
 void object_initialize_with_type(void *data, TypeImpl *type)
 {
     Object *obj = data;
@@ -313,6 +326,7 @@ void object_initialize_with_type(void *data, TypeImpl *type)
     object_ref(obj);
     QTAILQ_INIT(&obj->properties);
     object_init_with_type(obj, type);
+    object_post_init_with_type(obj, type);
 }
 
 void object_initialize(void *data, const char *typename)
#14 lore raw

Re: [Qemu-devel] [RFC 3/3 v2] qdev: set globals on post_init() function

In reply to Eduardo Habkost
Quoted 61 lines · On Wed, 10 Jul 2013 17:08:42 -0300 Eduardo Habkost <[email protected]> wrote:

This way, properties registered in the instance_init() function of children classes will be handled properly by qdev_prop_set_globals(), too.

Includes a new unit test for the new functionality.

---
 hw/core/qdev.c                 | 10 +++++-
 tests/test-qdev-global-props.c | 71 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 79 insertions(+), 2 deletions(-)
hw/core/qdev.c +9 -1
index 9190a7e..e0acb6c 100644
@@ -752,7 +752,6 @@ static void device_initfn(Object *obj)
         }
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    qdev_prop_set_globals(dev, &err);
     if (err != NULL) {
         qerror_report_err(err);
         error_free(err);
@@ -764,6 +763,14 @@ static void device_initfn(Object *obj)
     assert_no_error(err);
 }
 
+static void device_post_init(Object *obj)
+{
+    Error *err = NULL;
+    DeviceState *dev = DEVICE(obj);
+    qdev_prop_set_globals(dev, &err);
+    assert_no_error(err);
+}
+
 /* Unlink device from bus and free the structure.  */
 static void device_finalize(Object *obj)
 {
@@ -853,6 +860,7 @@ static const TypeInfo device_type_info = {
     .parent = TYPE_OBJECT,
     .instance_size = sizeof(DeviceState),
     .instance_init = device_initfn,
+    .instance_post_init = device_post_init,
     .instance_finalize = device_finalize,
     .class_base_init = device_class_base_init,
     .class_init = device_class_init,
tests/test-qdev-global-props.c +2 -0
index 4c50156..7b5eb88 100644
@@ -26,6 +26,8 @@
 #include <stdint.h>
 
 #include "hw/qdev.h"
+#include "qemu/object.h"
+#include "qapi-visit.h"
 
 
 #define TYPE_STATIC_PROPS "static_prop_type"

tests/test-qdev-global-props.c static void test_static_prop(void) · line 88

Quoted 8 lines
tests/test-qdev-global-props.c +1 -1
@@ -80,7 +82,7 @@ static void test_static_prop(void)
 static void test_static_globalprop(void)
 {
     MyType *mt;
-    GlobalProperty props[] = {
+    static GlobalProperty props[] = {
         {TYPE_STATIC_PROPS, "prop1", "200"},
         {}
     };

hunk belongs to the 1st patch?

Quoted 50 lines
tests/test-qdev-global-props.c +46 -0
@@ -93,15 +95,82 @@ static void test_static_globalprop(void)
     g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
 }
 
+#define TYPE_DYNAMIC_PROPS "dynamic_prop_type"
+#define DYNAMIC_TYPE(obj) \
+     OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
+
+static void prop1_acessor(Object *obj,
+                          struct Visitor *v,
+                          void *opaque,
+                          const char *name,
+                          struct Error **errp)
+{
+    MyType *mt = DYNAMIC_TYPE(obj);
+    visit_type_uint32(v, &mt->prop1, name, errp);
+}
+
+static void prop2_acessor(Object *obj,
+                          struct Visitor *v,
+                          void *opaque,
+                          const char *name,
+                          struct Error **errp)
+{
+    MyType *mt = DYNAMIC_TYPE(obj);
+    visit_type_uint32(v, &mt->prop2, name, errp);
+}
+
+static void dynamic_instance_init(Object *obj)
+{
+    object_property_add(obj, "prop1", "uint32", prop1_acessor, prop1_acessor,
+                        NULL, NULL, NULL);
+    object_property_add(obj, "prop2", "uint32", prop2_acessor, prop2_acessor,
+                        NULL, NULL, NULL);
+}
+
+static void dynamic_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->init = mytype_init;
+}
+
+
+static TypeInfo dynamic_prop_type = {
+    .name = TYPE_DYNAMIC_PROPS,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(MyType),
+    .instance_init = dynamic_instance_init,
+    .class_init = dynamic_class_init,
+};

tests/test-qdev-global-props.c static void test_static_globalprop(void) · line 153

Quoted 10 lines
tests/test-qdev-global-props.c +10 -0
+
+/* Test setting of static property using global properties */
+static void test_dynamic_globalprop(void)
+{
+    MyType *mt;
+    static GlobalProperty props[] = {
+        {TYPE_DYNAMIC_PROPS, "prop1", "101"},
+        {TYPE_DYNAMIC_PROPS, "prop2", "102"},
+        {}
+    };

this list cloud be the same for static and dynamic tests, no need to duplicate

and perhaps one property is enough, like it's done for static one?

Quoted 23 lines
tests/test-qdev-global-props.c +11 -0
+    qdev_prop_register_global_list(props);
+
+    mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
+    qdev_init_nofail(DEVICE(mt));
+
+    g_assert_cmpuint(mt->prop1, ==, 101);
+    g_assert_cmpuint(mt->prop2, ==, 102);
+}
+
 int main(int argc, char **argv)
 {
     module_call_init(MODULE_INIT_QOM);
     type_register_static(&static_prop_type);
+    type_register_static(&dynamic_prop_type);
 
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/qdev/properties/static/default", test_static_prop);
     g_test_add_func("/qdev/properties/static/global", test_static_globalprop);
+    g_test_add_func("/qdev/properties/dynamic/global", test_dynamic_globalprop);
 
     g_test_run();
 
#12 lore raw

Re: [Qemu-devel] [RFC 2/3 v2] qom: introduce post_init() function

In reply to Igor Mammedov
Quoted 31 lines · Igor Mammedov
Quoted 26 lines · On Wed, 10 Jul 2013 17:08:41 -0300 Eduardo Habkost <[email protected]> wrote:

This will allow classes to specify a function to be called after all instance_init() functions were called.

This will be used by DeviceState to call qdev_prop_set_globals() at the right moment.

---
 include/qom/object.h |  3 +++
 qom/object.c         | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
include/qom/object.h +2 -0
index 23fc048..1a54f64 100644
@@ -398,6 +398,8 @@ struct Object
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
+ * @instance_post_init: This function is called to finish initialization of
+ *   an object, after all instance_init functions were called.
  * @instance_finalize: This function is called during object destruction.  This
  *   is called before the parent @instance_finalize function has been called.
  *   An object should only free the members that are unique to its type in this
@@ -433,6 +435,7 @@ struct TypeInfo

[...]

Quoted 1 line
Quoted 11 lines

include/qom/object.h

Quoted 9 lines
include/qom/object.h +9 -0
+static void object_post_init_with_type(Object *obj, TypeImpl *ti)
+{
+    if (ti->instance_post_init) {
+        ti->instance_post_init(obj);
+    }
+
+    if (type_has_parent(ti)) {
+        object_post_init_with_type(obj, type_get_parent(ti));
+    }

Is there a reason why post init called in opposite order than instance_init() ?

Because it seems more useful: if parent types want to initialize something before subclasses, they can use instance_init(). If they want to initialize something _after_ subclasses, then they can use instance_post_init().

Quoted 15 lines
Quoted 14 lines
include/qom/object.h +3 -0
+}
+
 void object_initialize_with_type(void *data, TypeImpl *type)
 {
     Object *obj = data;
@@ -313,6 +326,7 @@ void object_initialize_with_type(void *data, TypeImpl *type)
     object_ref(obj);
     QTAILQ_INIT(&obj->properties);
     object_init_with_type(obj, type);
+    object_post_init_with_type(obj, type);
 }
 
 void object_initialize(void *data, const char *typename)
Signature · 1 line
Eduardo
#15 lore raw

Re: [Qemu-devel] [RFC 3/3 v2] qdev: set globals on post_init() function

In reply to Igor Mammedov

On Thu, Jul 11, 2013 at 08:48:44AM +0200, Igor Mammedov wrote:
[...]

Quoted 11 lines

tests/test-qdev-global-props.c static void test_static_prop(void) · line 88

Quoted 10 lines

#define TYPE_STATIC_PROPS "static_prop_type"

tests/test-qdev-global-props.c +1 -1
@@ -80,7 +82,7 @@ static void test_static_prop(void)
 static void test_static_globalprop(void)
 {
     MyType *mt;
-    GlobalProperty props[] = {
+    static GlobalProperty props[] = {
         {TYPE_STATIC_PROPS, "prop1", "200"},
         {}
     };

hunk belongs to the 1st patch?

Yes, thanks for noticing.

Quoted 63 lines
Quoted 51 lines
tests/test-qdev-global-props.c +46 -0
@@ -93,15 +95,82 @@ static void test_static_globalprop(void)
     g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
 }
 
+#define TYPE_DYNAMIC_PROPS "dynamic_prop_type"
+#define DYNAMIC_TYPE(obj) \
+     OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
+
+static void prop1_acessor(Object *obj,
+                          struct Visitor *v,
+                          void *opaque,
+                          const char *name,
+                          struct Error **errp)
+{
+    MyType *mt = DYNAMIC_TYPE(obj);
+    visit_type_uint32(v, &mt->prop1, name, errp);
+}
+
+static void prop2_acessor(Object *obj,
+                          struct Visitor *v,
+                          void *opaque,
+                          const char *name,
+                          struct Error **errp)
+{
+    MyType *mt = DYNAMIC_TYPE(obj);
+    visit_type_uint32(v, &mt->prop2, name, errp);
+}
+
+static void dynamic_instance_init(Object *obj)
+{
+    object_property_add(obj, "prop1", "uint32", prop1_acessor, prop1_acessor,
+                        NULL, NULL, NULL);
+    object_property_add(obj, "prop2", "uint32", prop2_acessor, prop2_acessor,
+                        NULL, NULL, NULL);
+}
+
+static void dynamic_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->init = mytype_init;
+}
+
+
+static TypeInfo dynamic_prop_type = {
+    .name = TYPE_DYNAMIC_PROPS,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(MyType),
+    .instance_init = dynamic_instance_init,
+    .class_init = dynamic_class_init,
+};

tests/test-qdev-global-props.c static void test_static_globalprop(void) · line 153

Quoted 10 lines
tests/test-qdev-global-props.c +10 -0
+
+/* Test setting of static property using global properties */
+static void test_dynamic_globalprop(void)
+{
+    MyType *mt;
+    static GlobalProperty props[] = {
+        {TYPE_DYNAMIC_PROPS, "prop1", "101"},
+        {TYPE_DYNAMIC_PROPS, "prop2", "102"},
+        {}
+    };

this list cloud be the same for static and dynamic tests, no need to duplicate

Quoted 1 line

and perhaps one property is enough, like it's done for static one?

I don't know. I like the fact that both test cases are exercising the property-setting API in different ways and are likely to catch different types of bugs.

Quoted 26 lines
Quoted 25 lines
tests/test-qdev-global-props.c +11 -0
+    qdev_prop_register_global_list(props);
+
+    mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
+    qdev_init_nofail(DEVICE(mt));
+
+    g_assert_cmpuint(mt->prop1, ==, 101);
+    g_assert_cmpuint(mt->prop2, ==, 102);
+}
+
 int main(int argc, char **argv)
 {
     module_call_init(MODULE_INIT_QOM);
     type_register_static(&static_prop_type);
+    type_register_static(&dynamic_prop_type);
 
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/qdev/properties/static/default", test_static_prop);
     g_test_add_func("/qdev/properties/static/global", test_static_globalprop);
+    g_test_add_func("/qdev/properties/dynamic/global", test_dynamic_globalprop);
 
     g_test_run();
 
Signature · 1 line
Eduardo
#16 lore raw
In reply to Eduardo Habkost

Am 10.07.2013 22:08, schrieb Eduardo Habkost:

Quoted 10 lines

Updated version of the series sent in October 2012.

References:
http://article.gmane.org/gmane.comp.emulators.qemu/173753
(old discussion)
http://article.gmane.org/gmane.comp.emulators.qemu/173782
(previous version of this series)
http://article.gmane.org/gmane.comp.emulators.kvm.devel/112380
(Minutes of last call where this was discussed)

Quoted 4 lines

Eduardo Habkost (3):
tests: unit tests for qdev global-properties handling
qom: introduce post_init() function
qdev: set globals on post_init() function

Thanks, I have applied these with some style, spelling and build fixes to qom-cpu-next:
https://github.com/afaerber/qemu-cpu/commits/qom-cpu-next

Since Igor and me have agreed to continue work on x86 properties on qom-cpu-next during Hard Freeze, I do not see a need to merge these patches Last Minute through qom-next for 1.6. If I'm missing some use case other than x86 subclasses, please let me know.

Regards,
Andreas

Quoted 8 lines
 hw/core/qdev.c                 |  10 ++-
 include/qom/object.h           |   3 +
 qom/object.c                   |  14 ++++
 tests/.gitignore               |   1 +
 tests/Makefile                 |   7 ++
 tests/test-qdev-global-props.c | 178 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 212 insertions(+), 1 deletion(-)
 create mode 100644 tests/test-qdev-global-props.c
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg