Skip to contentSkip to conversation

qtest driver framework (core only)

PATCH In review Paolo Bonzini 4 participants 19 messages Jan 15, 2019 – Jan 18, 2019

lore
Share & tools
lore
#0 lore raw

cover qtest driver framework (core only)

As promised, this is the implementation of the core qtest driver framework API.

The diff of patch 5 from the previous post is at the end of the cover letter.

Emanuele Giuseppe Esposito (3):
tests/libqos: introduce virtio_start_device tests/libqos: rename qpci_init_pc and qpci_init_spapr functions
tests: qgraph API for the qtest driver framework

Paolo Bonzini (2):
tests: remove rule for nonexisting qdev-monitor-test tests/libqos: embed allocators instead of malloc-ing them separately

 configure                      |   2 +-
 include/qemu/module.h          |   2 +
 tests/Makefile.include         |  14 +-
 tests/ahci-test.c              |   6 +-
 tests/e1000e-test.c            |  22 +-
 tests/i440fx-test.c            |   2 +-
 tests/ide-test.c               |  19 +-
 tests/libqos/ahci.c            |   2 +-
 tests/libqos/libqos-pc.c       |   5 +-
 tests/libqos/libqos-spapr.c    |   5 +-
 tests/libqos/libqos.c          |  13 +-
 tests/libqos/libqos.h          |  13 +-
 tests/libqos/malloc-generic.c  |  21 +-
 tests/libqos/malloc-generic.h  |   7 +-
 tests/libqos/malloc-pc.c       |  18 +-
 tests/libqos/malloc-pc.h       |   4 +-
 tests/libqos/malloc-spapr.c    |  19 +-
 tests/libqos/malloc-spapr.h    |   4 +-
 tests/libqos/malloc.c          |  41 +--
 tests/libqos/malloc.h          |  21 +-
 tests/libqos/pci-pc.c          |   2 +-
 tests/libqos/pci-pc.h          |   9 +-
 tests/libqos/pci-spapr.c       |   2 +-
 tests/libqos/pci-spapr.h       |   2 +-
 tests/libqos/qgraph.c          | 756 +++++++++++++++++++++++++++++++++++++++++
 tests/libqos/qgraph.h          | 575 +++++++++++++++++++++++++++++++
 tests/libqos/qgraph_internal.h | 257 ++++++++++++++
 tests/libqos/virtio.c          |   7 +
 tests/libqos/virtio.h          |   1 +
 tests/libqtest.h               |   3 +
 tests/q35-test.c               |   4 +-
 tests/qos-test.c               | 449 ++++++++++++++++++++++++
 tests/rtas-test.c              |   2 +-
 tests/rtl8139-test.c           |   2 +-
 tests/sdhci-test.c             |   2 +-
 tests/tco-test.c               |   2 +-
 tests/test-qgraph.c            | 434 +++++++++++++++++++++++
 tests/usb-hcd-ehci-test.c      |   2 +-
 tests/vhost-user-test.c        |  16 +-
 tests/virtio-9p-test.c         |  16 +-
 tests/virtio-blk-test.c        |  79 +++--
 tests/virtio-net-test.c        |  14 +-
 tests/virtio-scsi-test.c       |  18 +-
 43 files changed, 2650 insertions(+), 244 deletions(-)
 create mode 100644 tests/libqos/qgraph.c
 create mode 100644 tests/libqos/qgraph.h
 create mode 100644 tests/libqos/qgraph_internal.h
 create mode 100644 tests/qos-test.c
 create mode 100644 tests/test-qgraph.c
Signature · 1 line
1.8.3.1
tests/libqos/qgraph.c +11 -13
index 6a750aa..cc42641 100644
@@ -247,7 +247,7 @@ static void create_interface(const char *node)
     if (!interface) {
         create_node(node, QNODE_INTERFACE);
     } else if (interface->type != QNODE_INTERFACE) {
-        printf("Error: Node %s is not an interface\n", node);
+        fprintf(stderr, "Error: Node %s is not an interface\n", node);
         abort();
     }
 }
@@ -265,8 +265,7 @@ static void create_interface(const char *node)
  */
 static void build_machine_cmd_line(QOSGraphNode *node, const char *args)
 {
-    char *arch, *machine;
-    qos_separate_arch_machine(node->name, &arch, &machine);
+    char *machine = qos_get_machine_type(node->name);
     if (args) {
         node->command_line = g_strconcat("-M ", machine, ",", args, NULL);
     } else {
@@ -343,7 +342,7 @@ static void qos_push(QOSGraphNode *el, QOSStackElement *parent,
 /* qos_tos(): returns the top of stack, without popping */
 static QOSStackElement *qos_tos(void)
 {
-    return &qos_node_stack[(qos_node_tos - 1)];
+    return &qos_node_stack[qos_node_tos - 1];
 }
 
 /* qos_pop(): pops an element from the tos, setting it unvisited*/
@@ -419,7 +418,7 @@ static void qos_traverse_graph(QOSGraphNode *root, QOSTestCallback callback)
                 dest_node = search_node(e->dest);
 
                 if (!dest_node) {
-                    printf("node %s in %s -> %s does not exist\n",
+                    fprintf(stderr, "node %s in %s -> %s does not exist\n",
                             e->dest, v->name, e->dest);
                     abort();
                 }
@@ -733,25 +732,24 @@ void qos_object_start_hw(QOSGraphObject *obj)
     }
 }
 
-void qos_separate_arch_machine(char *name, char **arch, char **machine)
+char *qos_get_machine_type(char *name)
 {
-    *arch = name;
     while (*name != '\0' && *name != '/') {
         name++;
     }
 
-    if (*name == '/' && (*name + 1) != '\0') {
-        *machine = name + 1;
-    } else {
-        printf("Machine name has to be of the form <arch>/<machine>\n");
+    if (!*name || !name[1]) {
+        fprintf(stderr, "Machine name has to be of the form <arch>/<machine>\n");
         abort();
     }
+
+    return name + 1;
 }
 
-void qos_delete_abstract_cmd_line(const char *name, bool abstract)
+void qos_delete_cmd_line(const char *name)
 {
     QOSGraphNode *node = search_node(name);
-    if (node && abstract) {
+    if (node) {
         g_free(node->command_line);
         node->command_line = NULL;
     }
tests/libqos/qgraph.h +2 -2
index 8d68314..ccc30ee 100644
@@ -243,7 +243,7 @@ typedef void *(*QOSBeforeTest) (GString *cmd_line, void *arg);
  * There are three types of command line arguments:
  * - in node      : created from the node name. For example, machines will
  *                  have "-M <machine>" to its command line, while devices
- *                  "- device <device>". It is automatically done by the
+ *                  "-device <device>". It is automatically done by the
  *                   framework.
  * - after node   : added as additional argument to the node name.
  *                  This argument is added optionally when creating edges,
@@ -466,7 +466,7 @@ void qos_node_create_driver(const char *name, QOSCreateDriverFunc function);
  * It also has the possibility to add optional NULL-terminated
  * @opts parameters (see %QOSGraphEdgeOptions)
  *
- * This function can be useful whrn there are multiple devices
+ * This function can be useful when there are multiple devices
  * with the same node name contained in a machine/other node
  *
  * For example, if "arm/raspi2" contains 2 "generic-sdhci"
tests/libqos/qgraph_internal.h +9 -15
index 3728a25..2ef748b 100644
@@ -227,32 +227,26 @@ void qos_print_graph(void);
 void qos_graph_foreach_test_path(QOSTestCallback fn);
 
 /**
- * qos_separate_arch_machine(): separate arch from machine.
+ * qos_get_machine_type(): return QEMU machine type for a machine node.
  * This function requires every machine @name to be in the form
  * <arch>/<machine_name>, like "arm/raspi2" or "x86_64/pc".
  *
- * The function will split then the string in two parts,
- * assigning @arch to point to <arch>/<machine_name>, and
- * @machine to <machine_name>.
+ * The function will validate the format and return a pointer to
+ * @machine to <machine_name>.  For example, when passed "x86_64/pc"
+ * it will return "pc".
  *
- * For example, "x86_64/pc" will be split in this way:
- * *arch = "x86_64/pc"
- * *machine = "pc"
- *
- * Note that this function *does not* allocate any new string,
- * but just sets the pointer *arch and *machine to the respective
- * part of the string.
+ * Note that this function *does not* allocate any new string.
  */
-void qos_separate_arch_machine(char *name, char **arch, char **machine);
+char *qos_get_machine_type(char *name);
 
 /**
- * qos_delete_abstract_cmd_line(): if @abstract is #TRUE, delete the
+ * qos_delete_cmd_line(): delete the
  * command line present in node mapped with key @name.
  *
  * This function is called when the QMP query returns a node with
- * { "abstract" : <boolean> } attribute.
+ * { "abstract" : true } attribute.
  */
-void qos_delete_abstract_cmd_line(const char *name, bool abstract);
+void qos_delete_cmd_line(const char *name);
 
 /**
  * qos_graph_node_set_availability(): sets the node identified
tests/qos-test.c +25 -45

125 lines not on this page · raw message

#4 lore raw

PATCH 2/5 tests/libqos: rename qpci_init_pc and qpci_init_spapr functions

From: Emanuele Giuseppe Esposito <[email protected]>

Rename qpci_init_pc in qpci_pc_new and qpci_init_spapr in qpci_spapr_new, since theese function actually allocate a new pci struct and initialize it. Changed QOSOps field name from qpci_init to qpci_new.

---
 tests/e1000e-test.c         | 2 +-
 tests/i440fx-test.c         | 2 +-
 tests/ide-test.c            | 2 +-
 tests/libqos/ahci.c         | 2 +-
 tests/libqos/libqos-pc.c    | 2 +-
 tests/libqos/libqos-spapr.c | 2 +-
 tests/libqos/libqos.c       | 2 +-
 tests/libqos/libqos.h       | 2 +-
 tests/libqos/pci-pc.c       | 2 +-
 tests/libqos/pci-pc.h       | 9 ++++++++-
 tests/libqos/pci-spapr.c    | 2 +-
 tests/libqos/pci-spapr.h    | 2 +-
 tests/q35-test.c            | 4 ++--
 tests/rtl8139-test.c        | 2 +-
 tests/sdhci-test.c          | 2 +-
 tests/tco-test.c            | 2 +-
 tests/usb-hcd-ehci-test.c   | 2 +-
 tests/vhost-user-test.c     | 2 +-
 18 files changed, 26 insertions(+), 19 deletions(-)
tests/e1000e-test.c +1 -1
index c9408a5..5525589 100644
@@ -395,7 +395,7 @@ static void data_test_init(e1000e_device *d)
     test_alloc = pc_alloc_init(global_qtest);
     g_assert_nonnull(test_alloc);
 
-    test_bus = qpci_init_pc(global_qtest, test_alloc);
+    test_bus = qpci_new_pc(global_qtest, test_alloc);
     g_assert_nonnull(test_bus);
 
     e1000e_device_init(test_bus, d);
tests/i440fx-test.c +1 -1
index 4390e55..69205b5 100644
@@ -38,7 +38,7 @@ static QPCIBus *test_start_get_bus(const TestData *s)
     cmdline = g_strdup_printf("-smp %d", s->num_cpus);
     qtest_start(cmdline);
     g_free(cmdline);
-    return qpci_init_pc(global_qtest, NULL);
+    return qpci_new_pc(global_qtest, NULL);
 }
 
 static void test_i440fx_defaults(gconstpointer opaque)
tests/ide-test.c +1 -1
index f0280e6..8f5adae 100644
@@ -157,7 +157,7 @@ static QPCIDevice *get_pci_device(QPCIBar *bmdma_bar, QPCIBar *ide_bar)
     uint16_t vendor_id, device_id;
 
     if (!pcibus) {
-        pcibus = qpci_init_pc(global_qtest, NULL);
+        pcibus = qpci_new_pc(global_qtest, NULL);
     }
 
     /* Find PCI device and verify it's the right one */
tests/libqos/ahci.c +1 -1
index 63fbc9e..cc1b08e 100644
@@ -130,7 +130,7 @@ QPCIDevice *get_ahci_device(QTestState *qts, uint32_t *fingerprint)
     uint32_t ahci_fingerprint;
     QPCIBus *pcibus;
 
-    pcibus = qpci_init_pc(qts, NULL);
+    pcibus = qpci_new_pc(qts, NULL);
 
     /* Find the AHCI PCI device and verify it's the right one. */
     ahci = qpci_device_find(pcibus, QPCI_DEVFN(0x1F, 0x02));
tests/libqos/libqos-pc.c +1 -1
index a9c1ace..293f9b6 100644
@@ -6,7 +6,7 @@
 static QOSOps qos_ops = {
     .init_allocator = pc_alloc_init_flags,
     .uninit_allocator = pc_alloc_uninit,
-    .qpci_init = qpci_init_pc,
+    .qpci_new = qpci_new_pc,
     .qpci_free = qpci_free_pc,
     .shutdown = qtest_pc_shutdown,
 };
tests/libqos/libqos-spapr.c +1 -1
index a37791e..64addfe 100644
@@ -6,7 +6,7 @@
 static QOSOps qos_ops = {
     .init_allocator = spapr_alloc_init_flags,
     .uninit_allocator = spapr_alloc_uninit,
-    .qpci_init = qpci_init_spapr,
+    .qpci_new = qpci_new_spapr,
     .qpci_free = qpci_free_spapr,
     .shutdown = qtest_spapr_shutdown,
 };
tests/libqos/libqos.c +1 -1
index c514187..6c91371 100644
@@ -25,7 +25,7 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
     qs->ops = ops;
     if (ops) {
         qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
-        qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
+        qs->pcibus = ops->qpci_new(qs->qts, qs->alloc);
     }
 
     g_free(cmdline);
tests/libqos/libqos.h +1 -1
index 07d4b93..1af6035 100644
@@ -10,7 +10,7 @@ typedef struct QOSState QOSState;
 typedef struct QOSOps {
     QGuestAllocator *(*init_allocator)(QTestState *qts, QAllocOpts);
     void (*uninit_allocator)(QGuestAllocator *);
-    QPCIBus *(*qpci_init)(QTestState *qts, QGuestAllocator *alloc);
+    QPCIBus *(*qpci_new)(QTestState *qts, QGuestAllocator *alloc);
     void (*qpci_free)(QPCIBus *bus);
     void (*shutdown)(QOSState *);
 } QOSOps;
tests/libqos/pci-pc.c +1 -1
index a4fc02b..43d7082 100644
@@ -116,7 +116,7 @@ static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint3
     qtest_outl(bus->qts, 0xcfc, value);
 }
 
-QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc)
+QPCIBus *qpci_new_pc(QTestState *qts, QGuestAllocator *alloc)
 {
     QPCIBusPC *ret = g_new0(QPCIBusPC, 1);
 
tests/libqos/pci-pc.h +8 -1
index 491eeac..84cc300 100644
@@ -16,7 +16,14 @@
 #include "libqos/pci.h"
 #include "libqos/malloc.h"
 
-QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc);
+/* qpci_new_pc():
+* this function creates a new QPCIBusPC object,
+ * and properly initialize its fields.
+ *
+ * returns the QPCIBus *bus field of a newly
+ * allocated QPCIBusPC.
+ */
+QPCIBus *qpci_new_pc(QTestState *qts, QGuestAllocator *alloc);
 void     qpci_free_pc(QPCIBus *bus);
 
 #endif
tests/libqos/pci-spapr.c +1 -1
index 4c29889..59679f6 100644
@@ -160,7 +160,7 @@ static void qpci_spapr_config_writel(QPCIBus *bus, int devfn, uint8_t offset,
 #define SPAPR_PCI_MMIO32_WIN_SIZE    0x80000000 /* 2 GiB */
 #define SPAPR_PCI_IO_WIN_SIZE        0x10000
 
-QPCIBus *qpci_init_spapr(QTestState *qts, QGuestAllocator *alloc)
+QPCIBus *qpci_new_spapr(QTestState *qts, QGuestAllocator *alloc)
 {
     QPCIBusSPAPR *ret = g_new0(QPCIBusSPAPR, 1);
 
tests/libqos/pci-spapr.h +1 -1
index 387686d..177e8c0 100644
@@ -11,7 +11,7 @@
 #include "libqos/malloc.h"
 #include "libqos/pci.h"
 
-QPCIBus *qpci_init_spapr(QTestState *qts, QGuestAllocator *alloc);
+QPCIBus *qpci_new_spapr(QTestState *qts, QGuestAllocator *alloc);
 void     qpci_free_spapr(QPCIBus *bus);
 
 #endif
tests/q35-test.c +2 -2
index 7ea7acc..34b34bc 100644
@@ -87,7 +87,7 @@ static void test_smram_lock(void)
 
     qtest_start("-M q35");
 
-    pcibus = qpci_init_pc(global_qtest, NULL);
+    pcibus = qpci_new_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);
 
     pcidev = qpci_device_find(pcibus, 0);
@@ -146,7 +146,7 @@ static void test_tseg_size(const void *data)
     g_free(cmdline);
 
     /* locate the DRAM controller */
-    pcibus = qpci_init_pc(global_qtest, NULL);
+    pcibus = qpci_new_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);
     pcidev = qpci_device_find(pcibus, 0);
     g_assert(pcidev != NULL);
tests/rtl8139-test.c +1 -1
index 68bfc42..a85c91f 100644
@@ -35,7 +35,7 @@ static QPCIDevice *get_device(void)
 {
     QPCIDevice *dev;
 
-    pcibus = qpci_init_pc(global_qtest, NULL);
+    pcibus = qpci_new_pc(global_qtest, NULL);
     qpci_device_foreach(pcibus, 0x10ec, 0x8139, save_fn, &dev);
     g_assert(dev != NULL);
 
tests/sdhci-test.c +1 -1
index 982f5eb..28d481b 100644
@@ -187,7 +187,7 @@ static QSDHCI *machine_start(const struct sdhci_t *test)
         global_qtest = qtest_initf("-machine %s -device sdhci-pci",
                                    test->machine);
 
-        s->pci.bus = qpci_init_pc(global_qtest, NULL);
+        s->pci.bus = qpci_new_pc(global_qtest, NULL);
 
         /* Find PCI device and verify it's the right one */
         s->pci.dev = qpci_device_find(s->pci.bus, QPCI_DEVFN(4, 0));
tests/tco-test.c +1 -1
index 6bee9a3..f89a42c 100644
@@ -64,7 +64,7 @@ static void test_init(TestData *d)
     global_qtest = qs;
     qtest_irq_intercept_in(qs, "ioapic");
 
-    d->bus = qpci_init_pc(qs, NULL);
+    d->bus = qpci_new_pc(qs, NULL);
     d->dev = qpci_device_find(d->bus, QPCI_DEVFN(0x1f, 0x00));
     g_assert(d->dev != NULL);
 
tests/usb-hcd-ehci-test.c +1 -1
index f28ea27..8bc3e44 100644
@@ -52,7 +52,7 @@ static void ehci_port_test(struct qhc *hc, int port, uint32_t expect)
 
 static void test_init(void)
 {
-    pcibus = qpci_init_pc(global_qtest, NULL);
+    pcibus = qpci_new_pc(global_qtest, NULL);
     g_assert(pcibus != NULL);
 
     qusb_pci_init_one(pcibus, &uhci1, QPCI_DEVFN(0x1d, 0), 4);
tests/vhost-user-test.c +1 -1
index 04a3a65..8d53482 100644
@@ -196,7 +196,7 @@ static void init_virtio_dev(QTestState *qts, TestServer *s, uint32_t features_ma
     uint32_t features;
     int i;
 
-    s->bus = qpci_init_pc(qts, NULL);
+    s->bus = qpci_new_pc(qts, NULL);
     g_assert_nonnull(s->bus);
 
     s->dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 3/5 tests: remove rule for nonexisting qdev-monitor-test

In reply to Paolo Bonzini

This test was merged into drive_del-test in 2014.

---
 tests/Makefile.include | 1 -
 1 file changed, 1 deletion(-)
tests/Makefile.include +0 -1
index 195af1f..8b88cd5 100644
@@ -737,7 +737,6 @@ tests/qom-test$(EXESUF): tests/qom-test.o
 tests/test-hmp$(EXESUF): tests/test-hmp.o
 tests/machine-none-test$(EXESUF): tests/machine-none-test.o
 tests/drive_del-test$(EXESUF): tests/drive_del-test.o $(libqos-virtio-obj-y)
-tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y)
 tests/nvme-test$(EXESUF): tests/nvme-test.o $(libqos-pc-obj-y)
 tests/pvpanic-test$(EXESUF): tests/pvpanic-test.o
 tests/i82801b11-test$(EXESUF): tests/i82801b11-test.o
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/5 tests/libqos: introduce virtio_start_device

In reply to Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

This function is intended to group all the qvirtio_* functions that start the qvirtio devices.
Applied in all tests using this combination of functions.

---
 tests/libqos/virtio.c    |  7 +++++++
 tests/libqos/virtio.h    |  1 +
 tests/vhost-user-test.c  |  4 +---
 tests/virtio-9p-test.c   |  4 +---
 tests/virtio-blk-test.c  | 10 +++-------
 tests/virtio-net-test.c  |  4 +---
 tests/virtio-scsi-test.c |  4 +---
 7 files changed, 15 insertions(+), 19 deletions(-)
tests/libqos/virtio.c +7 -0
index 0dad5c1..c1ff020 100644
@@ -365,3 +365,10 @@ const char *qvirtio_get_dev_type(void)
         return "pci";
     }
 }
+
+void qvirtio_start_device(QVirtioDevice *vdev)
+{
+    qvirtio_reset(vdev);
+    qvirtio_set_acknowledge(vdev);
+    qvirtio_set_driver(vdev);
+}
tests/libqos/virtio.h +1 -0
index 69b5b13..2c68668 100644
@@ -146,5 +146,6 @@ bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx, uint32_t *len);
 void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx);
 
 const char *qvirtio_get_dev_type(void);
+void qvirtio_start_device(QVirtioDevice *vdev);
 
 #endif
tests/vhost-user-test.c +1 -3
index ee7c2d9..04a3a65 100644
@@ -203,9 +203,7 @@ static void init_virtio_dev(QTestState *qts, TestServer *s, uint32_t features_ma
     g_assert_nonnull(s->dev);
 
     qvirtio_pci_device_enable(s->dev);
-    qvirtio_reset(&s->dev->vdev);
-    qvirtio_set_acknowledge(&s->dev->vdev);
-    qvirtio_set_driver(&s->dev->vdev);
+    qvirtio_start_device(&s->dev->vdev);
 
     s->alloc = pc_alloc_init(qts);
 
tests/virtio-9p-test.c +1 -3
index a2b3108..d275c74 100644
@@ -65,9 +65,7 @@ static QVirtIO9P *qvirtio_9p_pci_start(void)
     v9p->dev = (QVirtioDevice *) dev;
 
     qvirtio_pci_device_enable(dev);
-    qvirtio_reset(v9p->dev);
-    qvirtio_set_acknowledge(v9p->dev);
-    qvirtio_set_driver(v9p->dev);
+    qvirtio_start_device(v9p->dev);
 
     v9p->vq = qvirtqueue_setup(v9p->dev, v9p->qs->alloc, 0);
 
tests/virtio-blk-test.c +3 -7
index 04c6087..f97c158 100644
@@ -116,10 +116,7 @@ static QVirtioPCIDevice *virtio_blk_pci_init(QPCIBus *bus, int slot)
     g_assert_cmphex(dev->pdev->devfn, ==, ((slot << 3) | PCI_FN));
 
     qvirtio_pci_device_enable(dev);
-    qvirtio_reset(&dev->vdev);
-    qvirtio_set_acknowledge(&dev->vdev);
-    qvirtio_set_driver(&dev->vdev);
-
+    qvirtio_start_device(&dev->vdev);
     return dev;
 }
 
@@ -333,6 +330,7 @@ static void pci_indirect(void)
     qvirtio_set_features(&dev->vdev, features);
 
     vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
+
     qvirtio_set_driver_ok(&dev->vdev);
 
     /* Write request */
@@ -720,9 +718,7 @@ static void mmio_basic(void)
     g_assert(dev != NULL);
     g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_BLOCK);
 
-    qvirtio_reset(&dev->vdev);
-    qvirtio_set_acknowledge(&dev->vdev);
-    qvirtio_set_driver(&dev->vdev);
+    qvirtio_start_device(&dev->vdev);
 
     alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE, MMIO_PAGE_SIZE);
     vq = qvirtqueue_setup(&dev->vdev, alloc, 0);
tests/virtio-net-test.c +1 -3
index e9783e6..d4a32a4 100644
@@ -44,9 +44,7 @@ static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
     g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
 
     qvirtio_pci_device_enable(dev);
-    qvirtio_reset(&dev->vdev);
-    qvirtio_set_acknowledge(&dev->vdev);
-    qvirtio_set_driver(&dev->vdev);
+    qvirtio_start_device(&dev->vdev);
 
     return dev;
 }
tests/virtio-scsi-test.c +1 -3
index 0d4f25d..961925c 100644
@@ -159,9 +159,7 @@ static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
     g_assert_cmphex(vs->dev->device_type, ==, VIRTIO_ID_SCSI);
 
     qvirtio_pci_device_enable(dev);
-    qvirtio_reset(vs->dev);
-    qvirtio_set_acknowledge(vs->dev);
-    qvirtio_set_driver(vs->dev);
+    qvirtio_start_device(vs->dev);
 
     vs->num_queues = qvirtio_config_readl(vs->dev, 0);
 
Signature · 1 line
1.8.3.1
#11 lore raw

PATCH 4/5 tests/libqos: embed allocators instead of malloc-ing them separately

In reply to Paolo Bonzini

qgraph will embed these objects instead of allocating them in a separate object. Expose a new API "generic_alloc_init" and "generic_alloc_destroy" for that, and rename the existing API with s/init/new/ and s/uninit/free/.

---
 tests/ahci-test.c             |  6 ++--
 tests/e1000e-test.c           | 22 +++++++-------
 tests/ide-test.c              | 17 +++++------
 tests/libqos/libqos-pc.c      |  3 +-
 tests/libqos/libqos-spapr.c   |  3 +-
 tests/libqos/libqos.c         | 13 ++++----
 tests/libqos/libqos.h         | 11 ++++---
 tests/libqos/malloc-generic.c | 21 ++-----------
 tests/libqos/malloc-generic.h |  7 ++---
 tests/libqos/malloc-pc.c      | 18 ++---------
 tests/libqos/malloc-pc.h      |  4 +--
 tests/libqos/malloc-spapr.c   | 19 ++----------
 tests/libqos/malloc-spapr.h   |  4 +--
 tests/libqos/malloc.c         | 41 ++++---------------------
 tests/libqos/malloc.h         | 21 +++++++++----
 tests/rtas-test.c             |  2 +-
 tests/vhost-user-test.c       | 10 +++----
 tests/virtio-9p-test.c        | 12 ++++----
 tests/virtio-blk-test.c       | 69 ++++++++++++++++++++++---------------------
 tests/virtio-net-test.c       | 10 +++----
 tests/virtio-scsi-test.c      | 14 ++++-----
 21 files changed, 123 insertions(+), 204 deletions(-)
tests/ahci-test.c +3 -3
index 5dd380e..9f07e6f 100644
@@ -162,7 +162,7 @@ static AHCIQState *ahci_vboot(const char *cli, va_list ap)
     s = g_new0(AHCIQState, 1);
     s->parent = qtest_pc_vboot(cli, ap);
     global_qtest = s->parent->qts;
-    alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);
+    alloc_set_flags(&s->parent->alloc, ALLOC_LEAK_ASSERT);
 
     /* Verify that we have an AHCI device present. */
     s->dev = get_ahci_device(s->parent->qts, &s->fingerprint);
@@ -1039,7 +1039,7 @@ static void test_dma_fragmented(void)
     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
 
     /* Create a DMA buffer in guest memory, and write our pattern to it. */
-    ptr = guest_alloc(ahci->parent->alloc, bufsize);
+    ptr = guest_alloc(&ahci->parent->alloc, bufsize);
     g_assert(ptr);
     bufwrite(ptr, tx, bufsize);
 
@@ -1059,7 +1059,7 @@ static void test_dma_fragmented(void)
 
     /* Read back the guest's receive buffer into local memory */
     bufread(ptr, rx, bufsize);
-    guest_free(ahci->parent->alloc, ptr);
+    guest_free(&ahci->parent->alloc, ptr);
 
     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
 
tests/e1000e-test.c +10 -12
index 5525589..17a12b8 100644
@@ -94,7 +94,7 @@ typedef struct e1000e_device {
 } e1000e_device;
 
 static int test_sockets[2];
-static QGuestAllocator *test_alloc;
+static QGuestAllocator test_alloc;
 static QPCIBus *test_bus;
 
 static void e1000e_pci_foreach_callback(QPCIDevice *dev, int devfn, void *data)
@@ -165,7 +165,7 @@ static void e1000e_device_init(QPCIBus *bus, e1000e_device *d)
         val | E1000E_CTRL_EXT_DRV_LOAD | E1000E_CTRL_EXT_TXLSFLOW);
 
     /* Allocate and setup TX ring */
-    d->tx_ring = guest_alloc(test_alloc, E1000E_RING_LEN);
+    d->tx_ring = guest_alloc(&test_alloc, E1000E_RING_LEN);
     g_assert(d->tx_ring != 0);
 
     e1000e_macreg_write(d, E1000E_TDBAL, (uint32_t) d->tx_ring);
@@ -178,7 +178,7 @@ static void e1000e_device_init(QPCIBus *bus, e1000e_device *d)
     e1000e_macreg_write(d, E1000E_TCTL, E1000E_TCTL_EN);
 
     /* Allocate and setup RX ring */
-    d->rx_ring = guest_alloc(test_alloc, E1000E_RING_LEN);
+    d->rx_ring = guest_alloc(&test_alloc, E1000E_RING_LEN);
     g_assert(d->rx_ring != 0);
 
     e1000e_macreg_write(d, E1000E_RDBAL, (uint32_t)d->rx_ring);
@@ -268,7 +268,7 @@ static void e1000e_send_verify(e1000e_device *d)
     uint32_t recv_len;
 
     /* Prepare test data buffer */
-    uint64_t data = guest_alloc(test_alloc, data_len);
+    uint64_t data = guest_alloc(&test_alloc, data_len);
     memwrite(data, "TEST", 5);
 
     /* Prepare TX descriptor */
@@ -296,7 +296,7 @@ static void e1000e_send_verify(e1000e_device *d)
     g_assert_cmpstr(buffer, == , "TEST");
 
     /* Free test data buffer */
-    guest_free(test_alloc, data);
+    guest_free(&test_alloc, data);
 }
 
 static void e1000e_receive_verify(e1000e_device *d)
@@ -348,7 +348,7 @@ static void e1000e_receive_verify(e1000e_device *d)
     g_assert_cmpint(ret, == , sizeof(test) + sizeof(len));
 
     /* Prepare test data buffer */
-    uint64_t data = guest_alloc(test_alloc, data_len);
+    uint64_t data = guest_alloc(&test_alloc, data_len);
 
     /* Prepare RX descriptor */
     memset(&descr, 0, sizeof(descr));
@@ -369,7 +369,7 @@ static void e1000e_receive_verify(e1000e_device *d)
     g_assert_cmpstr(buffer, == , "TEST");
 
     /* Free test data buffer */
-    guest_free(test_alloc, data);
+    guest_free(&test_alloc, data);
 }
 
 static void e1000e_device_clear(QPCIBus *bus, e1000e_device *d)
@@ -392,10 +392,8 @@ static void data_test_init(e1000e_device *d)
     qtest_start(cmdline);
     g_free(cmdline);
 
-    test_alloc = pc_alloc_init(global_qtest);
-    g_assert_nonnull(test_alloc);
-
-    test_bus = qpci_new_pc(global_qtest, test_alloc);
+    pc_alloc_init(&test_alloc, global_qtest, 0);
+    test_bus = qpci_new_pc(global_qtest, &test_alloc);
     g_assert_nonnull(test_bus);
 
     e1000e_device_init(test_bus, d);
@@ -405,7 +403,7 @@ static void data_test_clear(e1000e_device *d)
 {
     e1000e_device_clear(test_bus, d);
     close(test_sockets[0]);
-    pc_alloc_uninit(test_alloc);
+    alloc_destroy(&test_alloc);
     g_free(d->pci_dev);
     qpci_free_pc(test_bus);
     qtest_end();
tests/ide-test.c +8 -9
index 8f5adae..ef776fc 100644
@@ -120,7 +120,7 @@ enum {
 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
 
 static QPCIBus *pcibus = NULL;
-static QGuestAllocator *guest_malloc;
+static QGuestAllocator guest_malloc;
 
 static char tmp_path[] = "/tmp/qtest.XXXXXX";
 static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";
@@ -135,7 +135,7 @@ static void ide_test_start(const char *cmdline_fmt, ...)
     va_end(ap);
 
     qtest_start(cmdline);
-    guest_malloc = pc_alloc_init(global_qtest);
+    pc_alloc_init(&guest_malloc, global_qtest, 0);
 
     g_free(cmdline);
 }
@@ -146,8 +146,7 @@ static void ide_test_quit(void)
         qpci_free_pc(pcibus);
         pcibus = NULL;
     }
-    pc_alloc_uninit(guest_malloc);
-    guest_malloc = NULL;
+    alloc_destroy(&guest_malloc);
     qtest_end();
 }
 
@@ -246,7 +245,7 @@ static int send_dma_request(int cmd, uint64_t sector, int nb_sectors,
 
     /* Setup PRDT */
     len = sizeof(*prdt) * prdt_entries;
-    guest_prdt = guest_alloc(guest_malloc, len);
+    guest_prdt = guest_alloc(&guest_malloc, len);
     memwrite(guest_prdt, prdt, len);
     qpci_io_writel(dev, bmdma_bar, bmreg_prdt, guest_prdt);
 
@@ -311,7 +310,7 @@ static void test_bmdma_simple_rw(void)
     uint8_t *buf;
     uint8_t *cmpbuf;
     size_t len = 512;
-    uintptr_t guest_buf = guest_alloc(guest_malloc, len);
+    uintptr_t guest_buf = guest_alloc(&guest_malloc, len);
 
     PrdtEntry prdt[] = {
         {
@@ -381,7 +380,7 @@ static void test_bmdma_trim(void)
     const uint64_t bad_range = trim_range_le(TEST_IMAGE_SIZE / 512 - 1, 2);
     size_t len = 512;
     uint8_t *buf;
-    uintptr_t guest_buf = guest_alloc(guest_malloc, len);
+    uintptr_t guest_buf = guest_alloc(&guest_malloc, len);
 
     PrdtEntry prdt[] = {
         {
@@ -625,7 +624,7 @@ static void make_dirty(uint8_t device)
 
     dev = get_pci_device(&bmdma_bar, &ide_bar);
 
-    guest_buf = guest_alloc(guest_malloc, len);
+    guest_buf = guest_alloc(&guest_malloc, len);
     buf = g_malloc(len);
     memset(buf, rand() % 255 + 1, len);
     g_assert(guest_buf);
@@ -986,7 +985,7 @@ static void test_cdrom_dma(void)
                    "-device ide-cd,drive=sr0,bus=ide.0", tmp_path);
     qtest_irq_intercept_in(global_qtest, "ioapic");
 
-    guest_buf = guest_alloc(guest_malloc, len);
+    guest_buf = guest_alloc(&guest_malloc, len);
     prdt[0].addr = cpu_to_le32(guest_buf);
     prdt[0].size = cpu_to_le32(len | PRDT_EOT);
 
tests/libqos/libqos-pc.c +1 -2
index 293f9b6..d04abc5 100644
@@ -4,8 +4,7 @@
 #include "libqos/pci-pc.h"
 
 static QOSOps qos_ops = {
-    .init_allocator = pc_alloc_init_flags,
-    .uninit_allocator = pc_alloc_uninit,
+    .alloc_init = pc_alloc_init,
     .qpci_new = qpci_new_pc,
     .qpci_free = qpci_free_pc,
     .shutdown = qtest_pc_shutdown,
tests/libqos/libqos-spapr.c +1 -2
index 64addfe..8766d54 100644
@@ -4,8 +4,7 @@
 #include "libqos/pci-spapr.h"
 
 static QOSOps qos_ops = {
-    .init_allocator = spapr_alloc_init_flags,
-    .uninit_allocator = spapr_alloc_uninit,
+    .alloc_init = spapr_alloc_init,
     .qpci_new = qpci_new_spapr,
     .qpci_free = qpci_free_spapr,
     .shutdown = qtest_spapr_shutdown,
tests/libqos/libqos.c +5 -8
index 6c91371..636a111 100644
@@ -24,8 +24,8 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
     qs->qts = qtest_init(cmdline);
     qs->ops = ops;
     if (ops) {
-        qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
-        qs->pcibus = ops->qpci_new(qs->qts, qs->alloc);
+        ops->alloc_init(&qs->alloc, qs->qts, ALLOC_NO_FLAGS);
+        qs->pcibus = ops->qpci_new(qs->qts, &qs->alloc);
     }
 
     g_free(cmdline);
@@ -58,11 +58,8 @@ void qtest_common_shutdown(QOSState *qs)
             qs->ops->qpci_free(qs->pcibus);
             qs->pcibus = NULL;
         }
-        if (qs->alloc && qs->ops->uninit_allocator) {
-            qs->ops->uninit_allocator(qs->alloc);
-            qs->alloc = NULL;
-        }
     }
+    alloc_destroy(&qs->alloc);
     qtest_quit(qs->qts);
     g_free(qs);
 }
@@ -116,7 +113,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
 
     /* If we were running, we can wait for an event. */
     if (running) {
-        migrate_allocator(from->alloc, to->alloc);
+        migrate_allocator(&from->alloc, &to->alloc);
         set_context(to);
         qtest_qmp_eventwait(to->qts, "RESUME");
         return;
@@ -146,7 +143,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
         g_assert_not_reached();
     }
 
-    migrate_allocator(from->alloc, to->alloc);
+    migrate_allocator(&from->alloc, &to->alloc);
     set_context(to);
 }
 
tests/libqos/libqos.h +5 -6
index 1af6035..149b0be 100644
@@ -3,13 +3,12 @@
 
 #include "libqtest.h"
 #include "libqos/pci.h"
-#include "libqos/malloc-pc.h"
+#include "libqos/malloc.h"
 
 typedef struct QOSState QOSState;
 
 typedef struct QOSOps {
-    QGuestAllocator *(*init_allocator)(QTestState *qts, QAllocOpts);
-    void (*uninit_allocator)(QGuestAllocator *);
+    void (*alloc_init)(QGuestAllocator *, QTestState *, QAllocOpts);
     QPCIBus *(*qpci_new)(QTestState *qts, QGuestAllocator *alloc);
     void (*qpci_free)(QPCIBus *bus);
     void (*shutdown)(QOSState *);
@@ -17,7 +16,7 @@ typedef struct QOSOps {
 
 struct QOSState {
     QTestState *qts;
-    QGuestAllocator *alloc;
+    QGuestAllocator alloc;
     QPCIBus *pcibus;
     QOSOps *ops;
 };
@@ -36,12 +35,12 @@ void generate_pattern(void *buffer, size_t len, size_t cycle_len);
 
 static inline uint64_t qmalloc(QOSState *q, size_t bytes)
 {
-    return guest_alloc(q->alloc, bytes);
+    return guest_alloc(&q->alloc, bytes);
 }
 
 static inline void qfree(QOSState *q, uint64_t addr)
 {
-    guest_free(q->alloc, addr);
+    guest_free(&q->alloc, addr);
 }
 
 #endif
tests/libqos/malloc-generic.c +3 -18
index 33ce90b..766a308 100644
@@ -15,25 +15,10 @@
  * Mostly for valgrind happiness, but it does offer
  * a chokepoint for debugging guest memory leaks, too.
  */
-void generic_alloc_uninit(QGuestAllocator *allocator)
+void generic_alloc_init(QGuestAllocator *s, uint64_t base_addr,
+                        uint64_t size, uint32_t page_size)
 {
-    alloc_uninit(allocator);
-}
-
-QGuestAllocator *generic_alloc_init_flags(uint64_t base_addr, uint64_t size,
-                                        uint32_t page_size, QAllocOpts flags)
-{
-    QGuestAllocator *s;
     uint64_t start = base_addr + (1 << 20); /* Start at 1MB */
 
-    s = alloc_init_flags(flags, start, start + size);
-    alloc_set_page_size(s, page_size);
-
-    return s;
-}
-
-inline QGuestAllocator *generic_alloc_init(uint64_t base_addr, uint64_t size,
-                                                            uint32_t page_size)
-{
-    return generic_alloc_init_flags(base_addr, size, page_size, ALLOC_NO_FLAGS);
+    alloc_init(s, 0, start, start + size, page_size);
 }
tests/libqos/malloc-generic.h +2 -5
index 90104ec..40ea058 100644
@@ -12,10 +12,7 @@
 
 #include "libqos/malloc.h"
 
-QGuestAllocator *generic_alloc_init(uint64_t base_addr, uint64_t size,
-                                                            uint32_t page_size);
-QGuestAllocator *generic_alloc_init_flags(uint64_t base_addr, uint64_t size,
-                                        uint32_t page_size, QAllocOpts flags);
-void generic_alloc_uninit(QGuestAllocator *allocator);
+void generic_alloc_init(QGuestAllocator *s, uint64_t base_addr, uint64_t size,
+                        uint32_t page_size);
 
 #endif
tests/libqos/malloc-pc.c +2 -16
index b83cb8f..9aff807 100644
@@ -24,28 +24,14 @@
  * Mostly for valgrind happiness, but it does offer
  * a chokepoint for debugging guest memory leaks, too.
  */
-void pc_alloc_uninit(QGuestAllocator *allocator)
+void pc_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags)
 {
-    alloc_uninit(allocator);
-}
-
-QGuestAllocator *pc_alloc_init_flags(QTestState *qts, QAllocOpts flags)
-{
-    QGuestAllocator *s;
     uint64_t ram_size;
     QFWCFG *fw_cfg = pc_fw_cfg_init(qts);
 
     ram_size = qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE);
-    s = alloc_init_flags(flags, 1 << 20, MIN(ram_size, 0xE0000000));
-    alloc_set_page_size(s, PAGE_SIZE);
+    alloc_init(s, flags, 1 << 20, MIN(ram_size, 0xE0000000), PAGE_SIZE);
 
     /* clean-up */
     g_free(fw_cfg);
-
-    return s;
-}
-
-inline QGuestAllocator *pc_alloc_init(QTestState *qts)
-{
-    return pc_alloc_init_flags(qts, ALLOC_NO_FLAGS);
 }
tests/libqos/malloc-pc.h +1 -3
index 10f3da6..21e75ae 100644
@@ -15,8 +15,6 @@
 
 #include "libqos/malloc.h"
 
-QGuestAllocator *pc_alloc_init(QTestState *qts);
-QGuestAllocator *pc_alloc_init_flags(QTestState *qts, QAllocOpts flags);
-void pc_alloc_uninit(QGuestAllocator *allocator);
+void pc_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags);
 
 #endif
tests/libqos/malloc-spapr.c +2 -17
index 1c359ce..2a6b7e3 100644
@@ -17,22 +17,7 @@
  */
 #define SPAPR_MIN_SIZE 0x10000000
 
-void spapr_alloc_uninit(QGuestAllocator *allocator)
+void spapr_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags)
 {
-    alloc_uninit(allocator);
-}
-
-QGuestAllocator *spapr_alloc_init_flags(QTestState *qts, QAllocOpts flags)
-{
-    QGuestAllocator *s;
-
-    s = alloc_init_flags(flags, 1 << 20, SPAPR_MIN_SIZE);
-    alloc_set_page_size(s, PAGE_SIZE);
-
-    return s;
-}
-
-QGuestAllocator *spapr_alloc_init(void)
-{
-    return spapr_alloc_init_flags(NULL, ALLOC_NO_FLAGS);
+    alloc_init(s, flags, 1 << 20, SPAPR_MIN_SIZE, PAGE_SIZE);
 }
tests/libqos/malloc-spapr.h +1 -3
index 52a9346..e5fe9bf 100644
@@ -10,8 +10,6 @@
 
 #include "libqos/malloc.h"
 
-QGuestAllocator *spapr_alloc_init(void);
-QGuestAllocator *spapr_alloc_init_flags(QTestState *qts, QAllocOpts flags);
-void spapr_alloc_uninit(QGuestAllocator *allocator);
+void spapr_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags);
 
 #endif
tests/libqos/malloc.c +5 -36

78 lines not on this page · raw message

tests/libqos/malloc.h +15 -6
index 828fdda..4d1a2e2 100644
@@ -23,19 +23,28 @@ typedef enum {
     ALLOC_PARANOID    = 0x04
 } QAllocOpts;
 
-typedef struct QGuestAllocator QGuestAllocator;
+typedef QTAILQ_HEAD(MemList, MemBlock) MemList;
 
-void alloc_uninit(QGuestAllocator *allocator);
+typedef struct QGuestAllocator {
+    QAllocOpts opts;
+    uint64_t start;
+    uint64_t end;
+    uint32_t page_size;
+
+    MemList *used;
+    MemList *free;
+} QGuestAllocator;
 
 /* Always returns page aligned values */
 uint64_t guest_alloc(QGuestAllocator *allocator, size_t size);
 void guest_free(QGuestAllocator *allocator, uint64_t addr);
 void migrate_allocator(QGuestAllocator *src, QGuestAllocator *dst);
 
-QGuestAllocator *alloc_init(uint64_t start, uint64_t end);
-QGuestAllocator *alloc_init_flags(QAllocOpts flags,
-                                  uint64_t start, uint64_t end);
-void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size);
 void alloc_set_flags(QGuestAllocator *allocator, QAllocOpts opts);
 
+void alloc_init(QGuestAllocator *alloc, QAllocOpts flags,
+                uint64_t start, uint64_t end,
+                size_t page_size);
+void alloc_destroy(QGuestAllocator *allocator);
+
 #endif
tests/rtas-test.c +1 -1
index 009bda6..ee88867 100644
@@ -17,7 +17,7 @@ static void test_rtas_get_time_of_day(void)
     global_qtest = qs->qts;
 
     t1 = time(NULL);
-    ret = qrtas_get_time_of_day(qs->qts, qs->alloc, &tm, &ns);
+    ret = qrtas_get_time_of_day(qs->qts, &qs->alloc, &tm, &ns);
     g_assert_cmpint(ret, ==, 0);
     t2 = mktimegm(&tm);
     g_assert(t2 - t1 < 5); /* 5 sec max to run the test */
tests/vhost-user-test.c +5 -5
index 8d53482..54be25a 100644
@@ -157,7 +157,7 @@ typedef struct TestServer {
     bool test_fail;
     int test_flags;
     int queues;
-    QGuestAllocator *alloc;
+    QGuestAllocator alloc;
 } TestServer;
 
 static TestServer *test_server_new(const gchar *name);
@@ -205,10 +205,10 @@ static void init_virtio_dev(QTestState *qts, TestServer *s, uint32_t features_ma
     qvirtio_pci_device_enable(s->dev);
     qvirtio_start_device(&s->dev->vdev);
 
-    s->alloc = pc_alloc_init(qts);
+    pc_alloc_init(&s->alloc, qts, 0);
 
     for (i = 0; i < s->queues * 2; i++) {
-        s->vq[i] = qvirtqueue_setup(&s->dev->vdev, s->alloc, i);
+        s->vq[i] = qvirtqueue_setup(&s->dev->vdev, &s->alloc, i);
     }
 
     features = qvirtio_get_features(&s->dev->vdev);
@@ -223,9 +223,9 @@ static void uninit_virtio_dev(TestServer *s)
     int i;
 
     for (i = 0; i < s->queues * 2; i++) {
-        qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], s->alloc);
+        qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], &s->alloc);
     }
-    pc_alloc_uninit(s->alloc);
+    alloc_destroy(&s->alloc);
 
     qvirtio_pci_device_free(s->dev);
 }
tests/virtio-9p-test.c +6 -6
index d275c74..8fd74f6 100644
@@ -67,7 +67,7 @@ static QVirtIO9P *qvirtio_9p_pci_start(void)
     qvirtio_pci_device_enable(dev);
     qvirtio_start_device(v9p->dev);
 
-    v9p->vq = qvirtqueue_setup(v9p->dev, v9p->qs->alloc, 0);
+    v9p->vq = qvirtqueue_setup(v9p->dev, &v9p->qs->alloc, 0);
 
     qvirtio_set_driver_ok(v9p->dev);
 
@@ -76,7 +76,7 @@ static QVirtIO9P *qvirtio_9p_pci_start(void)
 
 static void qvirtio_9p_pci_stop(QVirtIO9P *v9p)
 {
-    qvirtqueue_cleanup(v9p->dev->bus, v9p->vq, v9p->qs->alloc);
+    qvirtqueue_cleanup(v9p->dev->bus, v9p->vq, &v9p->qs->alloc);
     qvirtio_pci_device_disable(container_of(v9p->dev, QVirtioPCIDevice, vdev));
     qvirtio_pci_device_free((QVirtioPCIDevice *)v9p->dev);
     qvirtio_9p_stop(v9p);
@@ -222,7 +222,7 @@ static P9Req *v9fs_req_init(QVirtIO9P *v9p, uint32_t size, uint8_t id,
 
     req->v9p = v9p;
     req->t_size = total_size;
-    req->t_msg = guest_alloc(v9p->qs->alloc, req->t_size);
+    req->t_msg = guest_alloc(&v9p->qs->alloc, req->t_size);
     v9fs_memwrite(req, &hdr, 7);
     req->tag = tag;
     return req;
@@ -232,7 +232,7 @@ static void v9fs_req_send(P9Req *req)
 {
     QVirtIO9P *v9p = req->v9p;
 
-    req->r_msg = guest_alloc(v9p->qs->alloc, P9_MAX_SIZE);
+    req->r_msg = guest_alloc(&v9p->qs->alloc, P9_MAX_SIZE);
     req->free_head = qvirtqueue_add(v9p->vq, req->t_msg, req->t_size, false,
                                     true);
     qvirtqueue_add(v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
@@ -290,8 +290,8 @@ static void v9fs_req_free(P9Req *req)
 {
     QVirtIO9P *v9p = req->v9p;
 
-    guest_free(v9p->qs->alloc, req->t_msg);
-    guest_free(v9p->qs->alloc, req->r_msg);
+    guest_free(&v9p->qs->alloc, req->t_msg);
+    guest_free(&v9p->qs->alloc, req->r_msg);
     g_free(req);
 }
 
tests/virtio-blk-test.c +35 -34

224 lines not on this page · raw message

tests/virtio-net-test.c +5 -5
index d4a32a4..653148e 100644
@@ -228,16 +228,16 @@ static void pci_basic(gconstpointer data)
                         "virtio-net-pci,netdev=hs0", sv[1]);
     dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
 
-    rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
-    tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
+    rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, &qs->alloc, 0);
+    tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, &qs->alloc, 1);
 
     driver_init(&dev->vdev);
-    func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
+    func(&dev->vdev, &qs->alloc, &rx->vq, &tx->vq, sv[0]);
 
     /* End test */
     close(sv[0]);
-    qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
-    qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
+    qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, &qs->alloc);
+    qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, &qs->alloc);
     qvirtio_pci_device_disable(dev);
     g_free(dev->pdev);
     g_free(dev);
tests/virtio-scsi-test.c +7 -7
index 961925c..9298aa8 100644
@@ -62,7 +62,7 @@ static void qvirtio_scsi_pci_free(QVirtIOSCSI *vs)
     int i;
 
     for (i = 0; i < vs->num_queues + 2; i++) {
-        qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], vs->qs->alloc);
+        qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], &vs->qs->alloc);
     }
     qvirtio_pci_device_disable(container_of(vs->dev, QVirtioPCIDevice, vdev));
     qvirtio_pci_device_free((QVirtioPCIDevice *)vs->dev);
@@ -75,7 +75,7 @@ static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,
 {
     uint64_t addr;
 
-    addr = guest_alloc(vs->qs->alloc, alloc_size);
+    addr = guest_alloc(&vs->qs->alloc, alloc_size);
     if (data) {
         memwrite(addr, data, alloc_size);
     }
@@ -133,10 +133,10 @@ static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
         memread(resp_addr, resp_out, sizeof(*resp_out));
     }
 
-    guest_free(vs->qs->alloc, req_addr);
-    guest_free(vs->qs->alloc, resp_addr);
-    guest_free(vs->qs->alloc, data_in_addr);
-    guest_free(vs->qs->alloc, data_out_addr);
+    guest_free(&vs->qs->alloc, req_addr);
+    guest_free(&vs->qs->alloc, resp_addr);
+    guest_free(&vs->qs->alloc, data_in_addr);
+    guest_free(&vs->qs->alloc, data_out_addr);
     return response;
 }
 
@@ -166,7 +166,7 @@ static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
     g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
 
     for (i = 0; i < vs->num_queues + 2; i++) {
-        vs->vq[i] = qvirtqueue_setup(vs->dev, vs->qs->alloc, i);
+        vs->vq[i] = qvirtqueue_setup(vs->dev, &vs->qs->alloc, i);
     }
 
     /* Clear the POWER ON OCCURRED unit attention */
Signature · 1 line
1.8.3.1
#15 lore raw

PATCH 5/5 tests: qgraph API for the qtest driver framework

In reply to Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

Add qgraph API that allows to add/remove nodes and edges from the graph, implementation of Depth First Search to discover the paths and basic unit test to check correctness of the API.
Included also a main executable that takes care of starting the framework, create the nodes, set the available drivers/machines, discover the path and run tests.

graph.h provides the public API to manage the graph nodes/edges graph_extra.h provides a more private API used successively by the gtest integration part qos-test.c provides the main executable

[Paolo's changes compared to the Google Summer of Code submission:
* added subprocess to test options
* refactored object creation to support live migration tests
* removed driver .before callback (unused)
* removed test .after callbacks (replaced by GTest destruction queue)]

---
 configure                      |   2 +-
 include/qemu/module.h          |   2 +
 tests/Makefile.include         |  13 +-
 tests/libqos/qgraph.c          | 756 +++++++++++++++++++++++++++++++++++++++++
 tests/libqos/qgraph.h          | 575 +++++++++++++++++++++++++++++++
 tests/libqos/qgraph_internal.h | 257 ++++++++++++++
 tests/libqtest.h               |   3 +
 tests/qos-test.c               | 450 ++++++++++++++++++++++++
 tests/test-qgraph.c            | 434 +++++++++++++++++++++++
 9 files changed, 2489 insertions(+), 3 deletions(-)
 create mode 100644 tests/libqos/qgraph.c
 create mode 100644 tests/libqos/qgraph.h
 create mode 100644 tests/libqos/qgraph_internal.h
 create mode 100644 tests/qos-test.c
 create mode 100644 tests/test-qgraph.c
configure +1 -1
index 3126e20..cf2c3f3 100755
@@ -7581,7 +7581,7 @@ fi
 # tests might fail. Prefer to keep the relevant files in their own
 # directory and symlink the directory instead.
 DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema tests/tcg/xtensa tests/qemu-iotests tests/vm"
-DIRS="$DIRS tests/fp"
+DIRS="$DIRS tests/fp tests/qgraph"
 DIRS="$DIRS docs docs/interop fsdev scsi"
 DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas pc-bios/s390-ccw"
 DIRS="$DIRS roms/seabios roms/vgabios"
include/qemu/module.h +2 -0
index 54300ab..1fcdca0 100644
@@ -44,6 +44,7 @@ typedef enum {
     MODULE_INIT_OPTS,
     MODULE_INIT_QOM,
     MODULE_INIT_TRACE,
+    MODULE_INIT_LIBQOS,
     MODULE_INIT_MAX
 } module_init_type;
 
@@ -51,6 +52,7 @@ typedef enum {
 #define opts_init(function) module_init(function, MODULE_INIT_OPTS)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
 #define trace_init(function) module_init(function, MODULE_INIT_TRACE)
+#define libqos_init(function) module_init(function, MODULE_INIT_LIBQOS)
 
 #define block_module_load_one(lib) module_load_one("block-", lib)
 #define ui_module_load_one(lib) module_load_one("ui-", lib)
tests/Makefile.include +11 -2
index 8b88cd5..4a66fda 100644
@@ -668,7 +668,10 @@ tests/test-crypto-ivgen$(EXESUF): tests/test-crypto-ivgen.o $(test-crypto-obj-y)
 tests/test-crypto-afsplit$(EXESUF): tests/test-crypto-afsplit.o $(test-crypto-obj-y)
 tests/test-crypto-block$(EXESUF): tests/test-crypto-block.o $(test-crypto-obj-y)
 
-libqos-obj-y = tests/libqos/pci.o tests/libqos/fw_cfg.o tests/libqos/malloc.o
+libqgraph-obj-y = tests/libqos/qgraph.o
+
+libqos-obj-y = $(libqgraph-obj-y) tests/libqos/pci.o tests/libqos/fw_cfg.o
+libqos-obj-y += tests/libqos/malloc.o tests/libqos/malloc-generic.o
 libqos-obj-y += tests/libqos/i2c.o tests/libqos/libqos.o
 libqos-spapr-obj-y = $(libqos-obj-y) tests/libqos/malloc-spapr.o
 libqos-spapr-obj-y += tests/libqos/libqos-spapr.o
@@ -680,7 +683,13 @@ libqos-pc-obj-y += tests/libqos/ahci.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
 libqos-imx-obj-y = $(libqos-obj-y) tests/libqos/i2c-imx.o
 libqos-usb-obj-y = $(libqos-spapr-obj-y) $(libqos-pc-obj-y) tests/libqos/usb.o
-libqos-virtio-obj-y = $(libqos-spapr-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o tests/libqos/virtio-mmio.o tests/libqos/malloc-generic.o
+libqos-virtio-obj-y = $(libqos-spapr-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o tests/libqos/virtio-mmio.o
+
+check-unit-y += tests/test-qgraph$(EXESUF)
+tests/test-qgraph$(EXESUF): tests/test-qgraph.o $(libqgraph-obj-y)
+
+check-qtest-generic-y += tests/qos-test$(EXESUF)
+tests/qos-test$(EXESUF): tests/qos-test.o $(libqgraph-obj-y)
 
 tests/qmp-test$(EXESUF): tests/qmp-test.o
 tests/qmp-cmd-test$(EXESUF): tests/qmp-cmd-test.o
tests/libqos/qgraph.c new file +756 -0

756 lines not on this page · raw message

tests/libqos/qgraph.h new file +575 -0

575 lines not on this page · raw message

tests/libqos/qgraph_internal.h new file +257 -0

257 lines not on this page · raw message

tests/libqtest.h +3 -0
index 7ea9413..cedf59a 100644
@@ -587,6 +587,9 @@ static inline QTestState *qtest_start(const char *args)
  */
 static inline void qtest_end(void)
 {
+    if (!global_qtest) {
+        return;
+    }
     qtest_quit(global_qtest);
     global_qtest = NULL;
 }
tests/qos-test.c new file +450 -0

450 lines not on this page · raw message

tests/test-qgraph.c new file +434 -0

434 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Reviewed-by on v1 3/5
#9 lore raw

Re: [Qemu-devel] [PATCH 3/5] tests: remove rule for nonexisting qdev-monitor-test

In reply to Paolo Bonzini
Quoted 13 lines · Paolo Bonzini

This test was merged into drive_del-test in 2014.

---
 tests/Makefile.include | 1 -
 1 file changed, 1 deletion(-)
tests/Makefile.include +0 -0
index 195af1f..8b88cd5 100644

tests/Makefile.include tests/qom-test$(EXESUF): tests/qom-test.o · line 742

Quoted 7 lines
tests/Makefile.include +0 -1
@@ -737,7 +737,6 @@ tests/qom-test$(EXESUF): tests/qom-test.o
 tests/test-hmp$(EXESUF): tests/test-hmp.o
 tests/machine-none-test$(EXESUF): tests/machine-none-test.o
 tests/drive_del-test$(EXESUF): tests/drive_del-test.o $(libqos-virtio-obj-y)
-tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y)
 tests/nvme-test$(EXESUF): tests/nvme-test.o $(libqos-pc-obj-y)
 tests/pvpanic-test$(EXESUF): tests/pvpanic-test.o
 tests/i82801b11-test$(EXESUF): tests/i82801b11-test.o
Fixes e2f3f221885a90de766ce9a38b87badeb658635a
Reviewed-by on v1 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] tests/libqos: introduce virtio_start_device

In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

This function is intended to group all the qvirtio_* functions that start the qvirtio devices.
Applied in all tests using this combination of functions.

Quoted 10 lines
---
  tests/libqos/virtio.c    |  7 +++++++
  tests/libqos/virtio.h    |  1 +
  tests/vhost-user-test.c  |  4 +---
  tests/virtio-9p-test.c   |  4 +---
  tests/virtio-blk-test.c  | 10 +++-------
  tests/virtio-net-test.c  |  4 +---
  tests/virtio-scsi-test.c |  4 +---
  7 files changed, 15 insertions(+), 19 deletions(-)
Reviewed-by on v1 2/5
#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests/libqos: rename qpci_init_pc and qpci_init_spapr functions

In reply to Paolo Bonzini
Quoted 20 lines · Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

Rename qpci_init_pc in qpci_pc_new and qpci_init_spapr in qpci_spapr_new, since theese function actually allocate a new pci struct and initialize it. Changed QOSOps field name from qpci_init to qpci_new.

---
  tests/e1000e-test.c         | 2 +-
  tests/i440fx-test.c         | 2 +-
  tests/ide-test.c            | 2 +-
  tests/libqos/ahci.c         | 2 +-
  tests/libqos/libqos-pc.c    | 2 +-
  tests/libqos/libqos-spapr.c | 2 +-
  tests/libqos/libqos.c       | 2 +-
  tests/libqos/libqos.h       | 2 +-
  tests/libqos/pci-pc.c       | 2 +-
  tests/libqos/pci-pc.h       | 9 ++++++++-
  tests/libqos/pci-spapr.c    | 2 +-
Quoted 8 lines
  tests/libqos/pci-spapr.h    | 2 +-
  tests/q35-test.c            | 4 ++--
  tests/rtl8139-test.c        | 2 +-
  tests/sdhci-test.c          | 2 +-
  tests/tco-test.c            | 2 +-
  tests/usb-hcd-ehci-test.c   | 2 +-
  tests/vhost-user-test.c     | 2 +-
  18 files changed, 26 insertions(+), 19 deletions(-)
Reviewed-by on v1 3/5
#10 lore raw

Re: [Qemu-devel] [PATCH 3/5] tests: remove rule for nonexisting qdev-monitor-test

In reply to Paolo Bonzini
Quoted 13 lines · Paolo Bonzini

This test was merged into drive_del-test in 2014.

---
 tests/Makefile.include | 1 -
 1 file changed, 1 deletion(-)
tests/Makefile.include +0 -0
index 195af1f..8b88cd5 100644

tests/Makefile.include tests/qom-test$(EXESUF): tests/qom-test.o · line 742

Quoted 7 lines
tests/Makefile.include +0 -1
@@ -737,7 +737,6 @@ tests/qom-test$(EXESUF): tests/qom-test.o
 tests/test-hmp$(EXESUF): tests/test-hmp.o
 tests/machine-none-test$(EXESUF): tests/machine-none-test.o
 tests/drive_del-test$(EXESUF): tests/drive_del-test.o $(libqos-virtio-obj-y)
-tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y)
 tests/nvme-test$(EXESUF): tests/nvme-test.o $(libqos-pc-obj-y)
 tests/pvpanic-test$(EXESUF): tests/pvpanic-test.o
 tests/i82801b11-test$(EXESUF): tests/i82801b11-test.o
Reviewed-by on v1 4/5
#12 lore raw

Re: [Qemu-devel] [PATCH 4/5] tests/libqos: embed allocators instead of malloc-ing them separately

In reply to Paolo Bonzini
Quoted 3 lines · Paolo Bonzini

qgraph will embed these objects instead of allocating them in a separate object. Expose a new API "generic_alloc_init" and "generic_alloc_destroy" for that, and rename the existing API with s/init/new/ and s/uninit/free/.

I'm not the "s/init/new/ and s/uninit/free/" are actually done in this patch.

Quoted 1 line
Reviewed-by on v1 1/5
#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] tests/libqos: introduce virtio_start_device

In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

This function is intended to group all the qvirtio_* functions that start the qvirtio devices.
Applied in all tests using this combination of functions.

---

[..]

Quoted 17 lines
tests/virtio-blk-test.c +1 -4
index 04c6087..f97c158 100644
@@ -116,10 +116,7 @@ static QVirtioPCIDevice *virtio_blk_pci_init(QPCIBus *bus, int slot)
     g_assert_cmphex(dev->pdev->devfn, ==, ((slot << 3) | PCI_FN));
 
     qvirtio_pci_device_enable(dev);
-    qvirtio_reset(&dev->vdev);
-    qvirtio_set_acknowledge(&dev->vdev);
-    qvirtio_set_driver(&dev->vdev);
-
+    qvirtio_start_device(&dev->vdev);
     return dev;
 }
 

tests/virtio-blk-test.c static void pci_indirect(void) · line 334

Quoted 5 lines
tests/virtio-blk-test.c +1 -0
@@ -333,6 +330,7 @@ static void pci_indirect(void)
     qvirtio_set_features(&dev->vdev, features);
 
     vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
+
     qvirtio_set_driver_ok(&dev->vdev);

Still unnecessary white-space change.

Apart from that nit, still:

#6 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests/libqos: rename qpci_init_pc and qpci_init_spapr functions

In reply to Paolo Bonzini
Quoted 151 lines · Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

Rename qpci_init_pc in qpci_pc_new and qpci_init_spapr in qpci_spapr_new, since theese function actually allocate a new pci struct and initialize it. Changed QOSOps field name from qpci_init to qpci_new.

---
 tests/e1000e-test.c         | 2 +-
 tests/i440fx-test.c         | 2 +-
 tests/ide-test.c            | 2 +-
 tests/libqos/ahci.c         | 2 +-
 tests/libqos/libqos-pc.c    | 2 +-
 tests/libqos/libqos-spapr.c | 2 +-
 tests/libqos/libqos.c       | 2 +-
 tests/libqos/libqos.h       | 2 +-
 tests/libqos/pci-pc.c       | 2 +-
 tests/libqos/pci-pc.h       | 9 ++++++++-
 tests/libqos/pci-spapr.c    | 2 +-
 tests/libqos/pci-spapr.h    | 2 +-
 tests/q35-test.c            | 4 ++--
 tests/rtl8139-test.c        | 2 +-
 tests/sdhci-test.c          | 2 +-
 tests/tco-test.c            | 2 +-
 tests/usb-hcd-ehci-test.c   | 2 +-
 tests/vhost-user-test.c     | 2 +-
 18 files changed, 26 insertions(+), 19 deletions(-)
tests/e1000e-test.c +1 -1
index c9408a5..5525589 100644
@@ -395,7 +395,7 @@ static void data_test_init(e1000e_device *d)
     test_alloc = pc_alloc_init(global_qtest);
     g_assert_nonnull(test_alloc);
 
-    test_bus = qpci_init_pc(global_qtest, test_alloc);
+    test_bus = qpci_new_pc(global_qtest, test_alloc);
     g_assert_nonnull(test_bus);
 
     e1000e_device_init(test_bus, d);
tests/i440fx-test.c +1 -1
index 4390e55..69205b5 100644
@@ -38,7 +38,7 @@ static QPCIBus *test_start_get_bus(const TestData *s)
     cmdline = g_strdup_printf("-smp %d", s->num_cpus);
     qtest_start(cmdline);
     g_free(cmdline);
-    return qpci_init_pc(global_qtest, NULL);
+    return qpci_new_pc(global_qtest, NULL);
 }
 
 static void test_i440fx_defaults(gconstpointer opaque)
tests/ide-test.c +1 -1
index f0280e6..8f5adae 100644
@@ -157,7 +157,7 @@ static QPCIDevice *get_pci_device(QPCIBar *bmdma_bar, QPCIBar *ide_bar)
     uint16_t vendor_id, device_id;
 
     if (!pcibus) {
-        pcibus = qpci_init_pc(global_qtest, NULL);
+        pcibus = qpci_new_pc(global_qtest, NULL);
     }
 
     /* Find PCI device and verify it's the right one */
tests/libqos/ahci.c +1 -1
index 63fbc9e..cc1b08e 100644
@@ -130,7 +130,7 @@ QPCIDevice *get_ahci_device(QTestState *qts, uint32_t *fingerprint)
     uint32_t ahci_fingerprint;
     QPCIBus *pcibus;
 
-    pcibus = qpci_init_pc(qts, NULL);
+    pcibus = qpci_new_pc(qts, NULL);
 
     /* Find the AHCI PCI device and verify it's the right one. */
     ahci = qpci_device_find(pcibus, QPCI_DEVFN(0x1F, 0x02));
tests/libqos/libqos-pc.c +1 -1
index a9c1ace..293f9b6 100644
@@ -6,7 +6,7 @@
 static QOSOps qos_ops = {
     .init_allocator = pc_alloc_init_flags,
     .uninit_allocator = pc_alloc_uninit,
-    .qpci_init = qpci_init_pc,
+    .qpci_new = qpci_new_pc,
     .qpci_free = qpci_free_pc,
     .shutdown = qtest_pc_shutdown,
 };
tests/libqos/libqos-spapr.c +1 -1
index a37791e..64addfe 100644
@@ -6,7 +6,7 @@
 static QOSOps qos_ops = {
     .init_allocator = spapr_alloc_init_flags,
     .uninit_allocator = spapr_alloc_uninit,
-    .qpci_init = qpci_init_spapr,
+    .qpci_new = qpci_new_spapr,
     .qpci_free = qpci_free_spapr,
     .shutdown = qtest_spapr_shutdown,
 };
tests/libqos/libqos.c +1 -1
index c514187..6c91371 100644
@@ -25,7 +25,7 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
     qs->ops = ops;
     if (ops) {
         qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
-        qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
+        qs->pcibus = ops->qpci_new(qs->qts, qs->alloc);
     }
 
     g_free(cmdline);
tests/libqos/libqos.h +1 -1
index 07d4b93..1af6035 100644
@@ -10,7 +10,7 @@ typedef struct QOSState QOSState;
 typedef struct QOSOps {
     QGuestAllocator *(*init_allocator)(QTestState *qts, QAllocOpts);
     void (*uninit_allocator)(QGuestAllocator *);
-    QPCIBus *(*qpci_init)(QTestState *qts, QGuestAllocator *alloc);
+    QPCIBus *(*qpci_new)(QTestState *qts, QGuestAllocator *alloc);
     void (*qpci_free)(QPCIBus *bus);
     void (*shutdown)(QOSState *);
 } QOSOps;
tests/libqos/pci-pc.c +1 -1
index a4fc02b..43d7082 100644
@@ -116,7 +116,7 @@ static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint3
     qtest_outl(bus->qts, 0xcfc, value);
 }
 
-QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc)
+QPCIBus *qpci_new_pc(QTestState *qts, QGuestAllocator *alloc)
 {
     QPCIBusPC *ret = g_new0(QPCIBusPC, 1);
 
tests/libqos/pci-pc.h +0 -0
index 491eeac..84cc300 100644

tests/libqos/pci-pc.h line 20

Quoted 6 lines
tests/libqos/pci-pc.h +2 -1
@@ -16,7 +16,14 @@
 #include "libqos/pci.h"
 #include "libqos/malloc.h"
 
-QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc);
+/* qpci_new_pc():
+* this function creates a new QPCIBusPC object,

Hmm, you did not notice my previous review, did you? See:

https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg01417.html

Thomas

#7 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests/libqos: rename qpci_init_pc and qpci_init_spapr functions

Quoted 8 lines · Thomas Huth
Quoted 8 lines
tests/libqos/pci-pc.h +0 -0
index 491eeac..84cc300 100644
@@ -16,7 +16,14 @@
 #include "libqos/pci.h"
 #include "libqos/malloc.h"
 
Quoted 7 lines

tests/libqos/pci-pc.h line 20

Quoted 3 lines
tests/libqos/pci-pc.h +2 -1
-QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc);
+/* qpci_new_pc():
+* this function creates a new QPCIBusPC object,

Hmm, you did not notice my previous review, did you? See:

https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg01417.html

No, I missed it.

Quoted 2 lines

I'd also be fine if we keep "init" instead
of "new"... it's quite a bit of code churn ...

It matches QOM's object_new vs. object_initialize.

Paolo

#13 lore raw

Re: [Qemu-devel] [PATCH 4/5] tests/libqos: embed allocators instead of malloc-ing them separately

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini

qgraph will embed these objects instead of allocating them in a separate object. Expose a new API "generic_alloc_init" and "generic_alloc_destroy" for that, and rename the existing API with s/init/new/ and s/uninit/free/.

---

[...]

Quoted 47 lines
tests/libqos/malloc.c +1 -14
index f7bae47..d44ae27 100644
@@ -15,24 +15,12 @@
 #include "qemu-common.h"
 #include "qemu/host-utils.h"
 
-typedef QTAILQ_HEAD(MemList, MemBlock) MemList;
-
 typedef struct MemBlock {
     QTAILQ_ENTRY(MemBlock) MLIST_ENTNAME;
     uint64_t size;
     uint64_t addr;
 } MemBlock;
 
-struct QGuestAllocator {
-    QAllocOpts opts;
-    uint64_t start;
-    uint64_t end;
-    uint32_t page_size;
-
-    MemList *used;
-    MemList *free;
-};
-
 #define DEFAULT_PAGE_SIZE 4096
 
 static void mlist_delete(MemList *list, MemBlock *node)
@@ -225,7 +213,7 @@ static void mlist_free(QGuestAllocator *s, uint64_t addr)
  * Mostly for valgrind happiness, but it does offer
  * a chokepoint for debugging guest memory leaks, too.
  */
-void alloc_uninit(QGuestAllocator *allocator)
+void alloc_destroy(QGuestAllocator *allocator)
 {
     MemBlock *node;
     MemBlock *tmp;
@@ -261,7 +249,6 @@ void alloc_uninit(QGuestAllocator *allocator)
 
     g_free(allocator->used);
     g_free(allocator->free);
-    g_free(allocator);
 }
 
 uint64_t guest_alloc(QGuestAllocator *allocator, size_t size)

tests/libqos/malloc.c void guest_free(QGuestAllocator *allocator, uint64_t addr) · line 287

Quoted 5 lines
tests/libqos/malloc.c +1 -1
@@ -297,9 +284,10 @@ void guest_free(QGuestAllocator *allocator, uint64_t addr)
     }
 }
 
-QGuestAllocator *alloc_init(uint64_t start, uint64_t end)
+void alloc_init(QGuestAllocator *s, QAllocOpts opts,

As far as I can see, you never use the "opts" parameter in this function anymore. Should you keep the "s->opts = opts" line below? Or remove this parameter completely?

Quoted 11 lines
tests/libqos/malloc.c +2 -1
+                uint64_t start, uint64_t end,
+                size_t page_size)
 {
-    QGuestAllocator *s = g_malloc0(sizeof(*s));
     MemBlock *node;
 
     s->start = start;
@@ -313,26 +301,7 @@ QGuestAllocator *alloc_init(uint64_t start, uint64_t end)
     node = mlist_new(s->start, s->end - s->start);
     QTAILQ_INSERT_HEAD(s->free, node, MLIST_ENTNAME);
 

tests/libqos/malloc.c QGuestAllocator *alloc_init(uint64_t start, uint64_t end) · line 325

Quoted 10 lines
tests/libqos/malloc.c +0 -10
-    s->page_size = DEFAULT_PAGE_SIZE;
-
-    return s;
-}
-
-QGuestAllocator *alloc_init_flags(QAllocOpts opts,
-                                  uint64_t start, uint64_t end)
-{
-    QGuestAllocator *s = alloc_init(start, end);
-    s->opts = opts;

i.e. keep this line ^ ?

tests/libqos/malloc.c QGuestAllocator *alloc_init(uint64_t start, uint64_t end) · line 305

Quoted 12 lines
tests/libqos/malloc.c +1 -10
-    return s;
-}
-
-void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size)
-{
-    /* Can't alter the page_size for an allocator in-use */
-    g_assert(QTAILQ_EMPTY(allocator->used));
-
-    g_assert(is_power_of_2(page_size));
-    allocator->page_size = page_size;
+    s->page_size = page_size;
 }

Thomas

#14 lore raw

Re: [Qemu-devel] [PATCH 4/5] tests/libqos: embed allocators instead of malloc-ing them separately

Quoted 12 lines · Thomas Huth
Quoted 12 lines
tests/libqos/malloc.c +2 -1
+                uint64_t start, uint64_t end,
+                size_t page_size)
 {
-    QGuestAllocator *s = g_malloc0(sizeof(*s));
     MemBlock *node;
 
     s->start = start;
@@ -313,26 +301,7 @@ QGuestAllocator *alloc_init(uint64_t start, uint64_t end)
     node = mlist_new(s->start, s->end - s->start);
     QTAILQ_INSERT_HEAD(s->free, node, MLIST_ENTNAME);
 
Quoted 12 lines

tests/libqos/malloc.c QGuestAllocator *alloc_init(uint64_t start, uint64_t end) · line 325

Quoted 10 lines
tests/libqos/malloc.c +0 -10
-    s->page_size = DEFAULT_PAGE_SIZE;
-
-    return s;
-}
-
-QGuestAllocator *alloc_init_flags(QAllocOpts opts,
-                                  uint64_t start, uint64_t end)
-{
-    QGuestAllocator *s = alloc_init(start, end);
-    s->opts = opts;

i.e. keep this line ^ ?

I'll add the line back. It's used by the legacy QOSOps.

Paolo

#16 lore raw

Re: [Qemu-devel] [PATCH 5/5] tests: qgraph API for the qtest driver framework

In reply to Paolo Bonzini
Quoted 22 lines · Paolo Bonzini

From: Emanuele Giuseppe Esposito <[email protected]>

Add qgraph API that allows to add/remove nodes and edges from the graph, implementation of Depth First Search to discover the paths and basic unit test to check correctness of the API.
Included also a main executable that takes care of starting the framework, create the nodes, set the available drivers/machines, discover the path and run tests.

graph.h provides the public API to manage the graph nodes/edges graph_extra.h provides a more private API used successively by the gtest integration part qos-test.c provides the main executable

[Paolo's changes compared to the Google Summer of Code submission:
* added subprocess to test options
* refactored object creation to support live migration tests
* removed driver .before callback (unused)
* removed test .after callbacks (replaced by GTest destruction queue)]

---

[...]

Quoted 4 lines
tests/libqos/qgraph.c new file +4 -0
+static void add_edge(const char *source, const char *dest,
+                     QOSEdgeType type, QOSGraphEdgeOptions *opts)
+{
+    char *key;

tests/libqos/qgraph.c line 88

Quoted 10 lines
tests/libqos/qgraph.c new file +10 -0
+    QOSGraphEdgeList *list = g_hash_table_lookup(edge_table, source);
+
+    if (!list) {
+        list = g_new0(QOSGraphEdgeList, 1);
+        key = g_strdup(source);
+        g_hash_table_insert(edge_table, key, list);
+    }
+
+    if (!opts) {
+        opts = &(QOSGraphEdgeOptions) { };

Not sure, but this part looks suspicious to me ... isn't that pointer going to be invalid as soon as the if-statement scope is over in the next line?

tests/libqos/qgraph.c line 94

Quoted 6 lines
tests/libqos/qgraph.c new file +6 -0
+    }
+
+    QOSGraphEdge *edge = g_new0(QOSGraphEdge, 1);
+    edge->type = type;
+    edge->dest = g_strdup(dest);
+    edge->edge_name = g_strdup(opts->edge_name ? : dest);

I think I'd write the elvis operator rather as "?:", without the space inbetween. (or does our checkpatch script dislike that?)

Quoted 11 lines
tests/libqos/qgraph.c new file +11 -0
+    edge->arg = g_memdup(opts->arg, opts->size_arg);
+
+    edge->before_cmd_line =
+        opts->before_cmd_line ? g_strconcat(" ", opts->before_cmd_line, NULL) : NULL;
+    edge->extra_device_opts =
+        opts->extra_device_opts ? g_strconcat(",", opts->extra_device_opts, NULL) : NULL;
+    edge->after_cmd_line =
+        opts->after_cmd_line ? g_strconcat(" ", opts->after_cmd_line, NULL) : NULL;
+
+    QSLIST_INSERT_HEAD(list, edge, edge_list);
+}

[...]

tests/libqos/qgraph.c line 535

Quoted 9 lines
tests/libqos/qgraph.c new file +9 -0
+bool qos_graph_has_edge(const char *start, const char *dest)
+{
+    QOSGraphEdgeList *list = get_edgelist(start);
+    QOSGraphEdge *e = search_list_edges(list, dest);
+    if (e) {
+        return TRUE;
+    }
+    return FALSE;
+}

I'm surprised that TRUE and FALSE are also still available with capital letters these days ... The spelling from stdbool.h is lowercase.

Anyway, maybe rather:

    return e != NULL;

?

[...]

tests/libqos/qgraph.c line 598

Quoted 8 lines
tests/libqos/qgraph.c new file +8 -0
+void qos_add_test(const char *name, const char *interface,
+                  QOSTestFunc test_func, QOSGraphTestOptions *opts)
+{
+    QOSGraphNode *node;
+    char *test_name = g_strdup_printf("%s-tests/%s", interface, name);;
+
+    if (!opts) {
+        opts = &(QOSGraphTestOptions) { };

Same as above ... is the pointer still valid after the next curly brace?

Quoted 13 lines
tests/libqos/qgraph.c new file +13 -0
+    }
+    node = create_node(test_name, QNODE_TEST);
+    node->u.test.function = test_func;
+    node->u.test.arg = opts->arg;
+    assert(!opts->edge.arg);
+    assert(!opts->edge.size_arg);
+
+    node->u.test.before = opts->before;
+    node->u.test.subprocess = opts->subprocess;
+    node->available = TRUE;
+    add_edge(interface, test_name, QEDGE_CONSUMED_BY, &opts->edge);
+    g_free(test_name);
+}

[...]

Quoted 6 lines

+ * There are three types of command line arguments:
+ * - in node : created from the node name. For example, machines will
+ * have "-M <machine>" to its command line, while devices
+ * "-device <device>". It is automatically done by the
+ * framework.
+ * - after node : added as additional argument to the node name.

Quoted 6 lines
+ *                  This argument is added optionally when creating edges,
+ *                  by setting the parameter @after_cmd_line and
+ *                  @extra_edge_opts in #QOSGraphEdgeOptions.
+ *                  The framework automatically adds
+ *                  a comma before @extra_edge_opts,
+ *                  because it is going to add attibutes

attributes

Quoted 19 lines
+ *                  after the destination node pointed by
+ *                  the edge containing these options, and automatically
+ *                  adds a space before @after_cmd_line, because it
+ *                  adds an additional device, not an attribute.
+ * - before node  : added as additional argument to the node name.
+ *                  This argument is added optionally when creating edges,
+ *                  by setting the parameter @before_cmd_line in
+ *                  #QOSGraphEdgeOptions. This attribute
+ *                  is going to add attibutes before the destination node
+ *                  pointed by the edge containing these options. It is
+ *                  helpful to commands that are not node-representable,
+ *                  such as "-fdsev" or "-netdev".

+ *
+ * While adding command line in edges is always used, not all nodes names are
+ * used in every path walk: this is because the contained or produced ones
+ * are already added by QEMU, so only nodes that "consumes" will be used to
+ * build the command line. Also, nodes that will have { "abstract" : true }
+ * as QMP attribute will loose their command line, since they are not proper
+ * devices to be added in QEMU.

[...]

Quoted 3 lines

+ * - destructor: Opposite to the node constructor, destroys the object.
+ * This function is called after the test has been executed, and performs
+ * a complete cleanup of each node allocated field. In case no constuctor

constructor

Quoted 16 lines

+ * is provided, no destructor will be called.
+ *
+ */

tests/libqos/qgraph.h new file +13 -0
+struct QOSGraphObject {
+    /* for produces edges, returns void * */
+    QOSGetDriver get_driver;
+    /* for contains edges, returns a QOSGraphObject * */
+    QOSGetDevice get_device;
+    /* start the hw, get ready for the test */
+    QOSStartFunct start_hw;
+    /* destroy this QOSGraphObject */
+    QOSDestructorFunc destructor;
+    /* free the memory associated to the QOSGraphObject and its contained
+     * children */
+    GDestroyNotify free;
+};

[...]

tests/qos-test.c line 40

Quoted 9 lines
tests/qos-test.c new file +9 -0
+static void apply_to_node(const char *name, bool is_machine, bool is_abstract)
+{
+    char *machine_name = NULL;
+    if (is_machine) {
+        const char *arch = qtest_get_arch();
+        machine_name = g_strconcat(arch, "/", name, NULL);
+        name = machine_name;
+    }
+    qos_graph_node_set_availability(name, TRUE);

true

Quoted 63 lines
tests/qos-test.c new file +63 -0
+    if (is_abstract) {
+        qos_delete_cmd_line(name);
+    }
+    g_free(machine_name);
+}
+
+/**
+ * apply_to_qlist(): using QMP queries QEMU for a list of
+ * machines and devices available, and sets the respective node
+ * as TRUE. If a node is found, also all its produced and contained
+ * child are marked available.
+ *
+ * See qos_graph_node_set_availability() for more info
+ */
+static void apply_to_qlist(QList *list, bool is_machine)
+{
+    const QListEntry *p;
+    const char *name;
+    bool abstract;
+    QDict *minfo;
+    QObject *qobj;
+    QString *qstr;
+    QBool *qbool;
+
+    for (p = qlist_first(list); p; p = qlist_next(p)) {
+        minfo = qobject_to(QDict, qlist_entry_obj(p));
+        qobj = qdict_get(minfo, "name");
+        qstr = qobject_to(QString, qobj);
+        name = qstring_get_str(qstr);
+
+        qobj = qdict_get(minfo, "abstract");
+        if (qobj) {
+            qbool = qobject_to(QBool, qobj);
+            abstract = qbool_get_bool(qbool);
+        } else {
+            abstract = false;
+        }
+
+        apply_to_node(name, is_machine, abstract);
+        qobj = qdict_get(minfo, "alias");
+        if (qobj) {
+            qstr = qobject_to(QString, qobj);
+            name = qstring_get_str(qstr);
+            apply_to_node(name, is_machine, abstract);
+        }
+    }
+}
+
+/**
+ * qos_set_machines_devices_available(): sets availability of qgraph
+ * machines and devices.
+ *
+ * This function firstly starts QEMU with "-machine none" option,
+ * and then executes the QMP protocol asking for the list of devices
+ * and machines available.
+ *
+ * for each of these items, it looks up the corresponding qgraph node,
+ * setting it as available. The list currently returns all devices that
+ * are either machines or QEDGE_CONSUMED_BY other nodes.
+ * Therefore, in order to mark all other nodes, it recursively sets
+ * all its QEDGE_CONTAINS and QEDGE_PRODUCES child as available too.
+ */
+static void qos_set_machines_devices_available(void)

tests/qos-test.c line 113

Quoted 10 lines
tests/qos-test.c new file +10 -0
+{
+    QDict *response;
+    QDict *args = qdict_new();
+    QList *list;
+
+    qtest_start("-machine none");
+    response = qmp("{ 'execute': 'query-machines' }");
+    list = qdict_get_qlist(response, "return");
+
+    apply_to_qlist(list, TRUE);

true

tests/qos-test.c line 117

Quoted 3 lines
tests/qos-test.c new file +3 -0
+    qobject_unref(response);
+
+    qdict_put_bool(args, "abstract", TRUE);

true

tests/qos-test.c line 125

Quoted 8 lines
tests/qos-test.c new file +8 -0
+    qdict_put_str(args, "implements", "device");
+
+    response = qmp("{'execute': 'qom-list-types',"
+                   " 'arguments': %p }", args);
+    g_assert(qdict_haskey(response, "return"));
+    list = qdict_get_qlist(response, "return");
+
+    apply_to_qlist(list, FALSE);

false

tests/qos-test.c line 129

Quoted 3 lines
tests/qos-test.c new file +3 -0
+    qtest_end();
+    qobject_unref(response);
+

Remove superfluous empty line?

tests/qos-test.c line 139

Quoted 10 lines
tests/qos-test.c new file +10 -0
+}
+
+static QGuestAllocator *get_machine_allocator(QOSGraphObject *obj)
+{
+    if (obj->get_driver) {
+        return obj->get_driver(obj, "memory");
+    } else {
+        return NULL;
+    }
+}

Maybe remove the "else" like this:

   if (obj->get_driver) {
       return obj->get_driver(obj, "memory");
   }
   return NULL;

?

Thomas

#17 lore raw

Re: [Qemu-devel] [PATCH 5/5] tests: qgraph API for the qtest driver framework

Quoted 7 lines · Thomas Huth

tests/libqos/qgraph.c line 94

Quoted 4 lines
tests/libqos/qgraph.c new file +4 -0
+    QOSGraphEdge *edge = g_new0(QOSGraphEdge, 1);
+    edge->type = type;
+    edge->dest = g_strdup(dest);
+    edge->edge_name = g_strdup(opts->edge_name ? : dest);

I think I'd write the elvis operator rather as "?:", without the space inbetween. (or does our checkpatch script dislike that?)

$ git grep '? :' | wc
     15     121    1059
$ git grep '?:' | wc
    270    1418   19542
Quoted 4 lines
Quoted 4 lines
tests/libqos/qgraph.c new file +4 -0
+bool qos_graph_has_edge(const char *start, const char *dest)
+{
+    QOSGraphEdgeList *list = get_edgelist(start);
+    QOSGraphEdge *e = search_list_edges(list, dest);
Quoted 8 lines

tests/libqos/qgraph.c line 535

Quoted 5 lines
tests/libqos/qgraph.c new file +5 -0
+    if (e) {
+        return TRUE;
+    }
+    return FALSE;
+}

I'm surprised that TRUE and FALSE are also still available with capital letters these days ... The spelling from stdbool.h is lowercase.

All caps versions are from glib's gboolean type (which is NOT a good substitute for C99 bool), and should be avoided for any use that does not specifically require gboolean due to type compatibility.

Quoted 4 lines

Anyway, maybe rather:

    return e != NULL;

Or return !!e

Quoted 10 lines

tests/libqos/qgraph.c line 598

Quoted 8 lines
tests/libqos/qgraph.c new file +8 -0
+void qos_add_test(const char *name, const char *interface,
+                  QOSTestFunc test_func, QOSGraphTestOptions *opts)
+{
+    QOSGraphNode *node;
+    char *test_name = g_strdup_printf("%s-tests/%s", interface, name);;
+
+    if (!opts) {
+        opts = &(QOSGraphTestOptions) { };

Same as above ... is the pointer still valid after the next curly brace?

https://stackoverflow.com/questions/47691857/lifetime-of-a-compound-literal

says it is not strict C99, but that

"A far more reasonable rule would indicate that the lifetime of a compound literal extends until code leaves the function in which it was used, or the expression creating it is re-executed, whichever happens first. Since the Standard allows compilers to extend the lifetime of automatic objects however they see fit, the fact that a compiler does so should not be considered a bug. On the other hand, a quality compiler which is deliberately going to be more useful than the Standard requires should probably explicitly document that fact. Otherwise future maintainers may declare that any programs that rely upon such sensible behavior are "defective", and that the compiler can be more "efficient" if it ceases to support them."

I'm surprised there is not a defect against the C standard to make the desired semantics explicit.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#18 lore raw

Re: [Qemu-devel] [PATCH 5/5] tests: qgraph API for the qtest driver framework

In reply to Thomas Huth
Quoted 1 line · Thomas Huth

[...]

Sounds all good. I'll make all the changes and send you a pull request for all of qgraph.

Thanks,

Paolo