Skip to contentSkip to conversation

new test infrastructure + qtest

PATCH Applied Paolo Bonzini 4 participants 21 messages Mar 28, 2012 – Mar 30, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

v4 cover new test infrastructure + qtest

This is a rebase of qtest. I split the gtester infrastructure into its own patch, and reorganized the tests by moving everything into tests/.

Also, libqtest now has bindings for the clock management commands, and I am using them in rtc-test. Finally, the accept is moved from qemu to libqtest; tests need not sleep anymore until QEMU connects.

This is on top of Luiz's recent pull request.

Anthony Liguori (4):
qtest: add test framework
qtest: add C version of test infrastructure
rtc: split out macros into a header file and use in test case
qtest: add rtc-test test-case

Paolo Bonzini (3):
test makefile overhaul
qtest: IRQ interception infrastructure
qtest: add clock management

 Makefile.objs                                      |    2 +
 cpu-exec.c                                         |    1 +
 cpus.c                                             |   82 ++++-
 cpus.h                                             |    2 +
 hw/irq.c                                           |   17 +
 hw/irq.h                                           |    5 +
 hw/mc146818rtc.c                                   |   33 --
 hw/mc146818rtc.h                                   |    3 +-
 hw/mc146818rtc_regs.h                              |   62 +++
 hw/pc_piix.c                                       |    5 +-
 osdep.h                                            |    2 +
 qemu-common.h                                      |    1 -
 qemu-options.hx                                    |    8 +
 qemu-timer.c                                       |    2 +-
 qemu-timer.h                                       |    1 +
 qtest.c                                            |  443 ++++++++++++++++++++
 qtest.h                                            |   35 ++
 rules.mak                                          |    2 +-
 scripts/gtester-cat                                |   26 ++
 scripts/qtest                                      |    5 +
 tests/Makefile                                     |  165 ++++++--
 check-qdict.c => tests/check-qdict.c               |    0
 check-qfloat.c => tests/check-qfloat.c             |    0
 check-qint.c => tests/check-qint.c                 |    0
 check-qjson.c => tests/check-qjson.c               |    0
 check-qlist.c => tests/check-qlist.c               |    0
 check-qstring.c => tests/check-qstring.c           |    0
 tests/libqtest.c                                   |  385 +++++++++++++++++
 tests/libqtest.h                                   |  333 +++++++++++++++
 tests/rtc-test.c                                   |  263 ++++++++++++
 test-coroutine.c => tests/test-coroutine.c         |    0
 test-qmp-commands.c => tests/test-qmp-commands.c   |    0
 .../test-qmp-input-strict.c                        |    0
 .../test-qmp-input-visitor.c                       |    0
 .../test-qmp-output-visitor.c                      |    0
 .../test-string-input-visitor.c                    |    0
 .../test-string-output-visitor.c                   |    0
 vl.c                                               |   10 +-
 38 files changed, 1806 insertions(+), 87 deletions(-)
 create mode 100644 hw/mc146818rtc_regs.h
 create mode 100644 qtest.c
 create mode 100644 qtest.h
 create mode 100755 scripts/gtester-cat
 create mode 100755 scripts/qtest
 rename check-qdict.c => tests/check-qdict.c (100%)
 rename check-qfloat.c => tests/check-qfloat.c (100%)
 rename check-qint.c => tests/check-qint.c (100%)
 rename check-qjson.c => tests/check-qjson.c (100%)
 rename check-qlist.c => tests/check-qlist.c (100%)
 rename check-qstring.c => tests/check-qstring.c (100%)
 create mode 100644 tests/libqtest.c
 create mode 100644 tests/libqtest.h
 create mode 100644 tests/rtc-test.c
 rename test-coroutine.c => tests/test-coroutine.c (100%)
 rename test-qmp-commands.c => tests/test-qmp-commands.c (100%)
 rename test-qmp-input-strict.c => tests/test-qmp-input-strict.c (100%)
 rename test-qmp-input-visitor.c => tests/test-qmp-input-visitor.c (100%)
 rename test-qmp-output-visitor.c => tests/test-qmp-output-visitor.c (100%)
 rename test-string-input-visitor.c => tests/test-string-input-visitor.c (100%)
 rename test-string-output-visitor.c => tests/test-string-output-visitor.c (100%)
Signature · 1 line
1.7.9.1
#1 lore raw

PATCH v4 1/7 test makefile overhaul

This introduces new test reporting infrastructure based on gtester and gtester-report.

Also, all existing tests are moved to tests/, and tests/Makefile is reorganized to factor out the commonalities in the rules.

---
 rules.mak                                          |    2 +-
 scripts/gtester-cat                                |   26 ++++
 tests/Makefile                                     |  140 +++++++++++++------
 check-qdict.c => tests/check-qdict.c               |    0
 check-qfloat.c => tests/check-qfloat.c             |    0
 check-qint.c => tests/check-qint.c                 |    0
 check-qjson.c => tests/check-qjson.c               |    0
 check-qlist.c => tests/check-qlist.c               |    0
 check-qstring.c => tests/check-qstring.c           |    0
 test-coroutine.c => tests/test-coroutine.c         |    0
 test-qmp-commands.c => tests/test-qmp-commands.c   |    0
 .../test-qmp-input-strict.c                        |    0
 .../test-qmp-input-visitor.c                       |    0
 .../test-qmp-output-visitor.c                      |    0
 .../test-string-input-visitor.c                    |    0
 .../test-string-output-visitor.c                   |    0
 16 files changed, 123 insertions(+), 45 deletions(-)
 create mode 100755 scripts/gtester-cat
 rename check-qdict.c => tests/check-qdict.c (100%)
 rename check-qfloat.c => tests/check-qfloat.c (100%)
 rename check-qint.c => tests/check-qint.c (100%)
 rename check-qjson.c => tests/check-qjson.c (100%)
 rename check-qlist.c => tests/check-qlist.c (100%)
 rename check-qstring.c => tests/check-qstring.c (100%)
 rename test-coroutine.c => tests/test-coroutine.c (100%)
 rename test-qmp-commands.c => tests/test-qmp-commands.c (100%)
 rename test-qmp-input-strict.c => tests/test-qmp-input-strict.c (100%)
 rename test-qmp-input-visitor.c => tests/test-qmp-input-visitor.c (100%)
 rename test-qmp-output-visitor.c => tests/test-qmp-output-visitor.c (100%)
 rename test-string-input-visitor.c => tests/test-string-input-visitor.c (100%)
 rename test-string-output-visitor.c => tests/test-string-output-visitor.c (100%)
rules.mak +1 -1
index 04a9198..c30093c 100644
@@ -47,7 +47,7 @@ quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
               >/dev/null 2>&1 && echo OK), $2, $3)
 
-VPATH_SUFFIXES = %.c %.h %.S %.m %.mak %.texi
+VPATH_SUFFIXES = %.c %.h %.S %.m %.mak %.texi %.sh
 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
 
 # find-in-path
scripts/gtester-cat new file +26 -0
new file mode 100755
index 0000000..5bcce50
@@ -0,0 +1,26 @@
+#!/bin/sh
+#
+# Copyright IBM, Corp. 2012
+#
+# Authors:
+#  Anthony Liguori <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPLv2 or later.
+# See the COPYING file in the top-level directory.
+
+cat <<EOF
+<?xml version="1.0"?>
+<gtester>
+ <info>
+  <package>qemu</package>
+  <version>0.0</version>
+  <revision>rev</revision>
+ </info>
+EOF
+
+sed \
+  -e '/<?xml/d' \
+  -e '/^<gtester>$/d' \
+  -e '/<info>/,/<\/info>/d' \
+  -e '$b' \
+  -e '/^<\/gtester>$/d' "$@"
tests/Makefile +96 -44

157 lines not on this page · raw message

tests/check-qdict.c renamed from check-qdict.c +0 -0
similarity index 100%
rename from check-qdict.c
rename to tests/check-qdict.c
tests/check-qfloat.c renamed from check-qfloat.c +0 -0
similarity index 100%
rename from check-qfloat.c
rename to tests/check-qfloat.c
tests/check-qint.c renamed from check-qint.c +0 -0
similarity index 100%
rename from check-qint.c
rename to tests/check-qint.c
tests/check-qjson.c renamed from check-qjson.c +0 -0
similarity index 100%
rename from check-qjson.c
rename to tests/check-qjson.c
tests/check-qlist.c renamed from check-qlist.c +0 -0
similarity index 100%
rename from check-qlist.c
rename to tests/check-qlist.c
tests/check-qstring.c renamed from check-qstring.c +0 -0
similarity index 100%
rename from check-qstring.c
rename to tests/check-qstring.c
tests/test-coroutine.c renamed from test-coroutine.c +0 -0
similarity index 100%
rename from test-coroutine.c
rename to tests/test-coroutine.c
tests/test-qmp-commands.c renamed from test-qmp-commands.c +0 -0
similarity index 100%
rename from test-qmp-commands.c
rename to tests/test-qmp-commands.c
tests/test-qmp-input-strict.c renamed from test-qmp-input-strict.c +0 -0
similarity index 100%
rename from test-qmp-input-strict.c
rename to tests/test-qmp-input-strict.c
tests/test-qmp-input-visitor.c renamed from test-qmp-input-visitor.c +0 -0
similarity index 100%
rename from test-qmp-input-visitor.c
rename to tests/test-qmp-input-visitor.c
tests/test-qmp-output-visitor.c renamed from test-qmp-output-visitor.c +0 -0
similarity index 100%
rename from test-qmp-output-visitor.c
rename to tests/test-qmp-output-visitor.c
tests/test-string-input-visitor.c renamed from test-string-input-visitor.c +0 -0
similarity index 100%
rename from test-string-input-visitor.c
rename to tests/test-string-input-visitor.c
tests/test-string-output-visitor.c renamed from test-string-output-visitor.c +0 -0
similarity index 100%
rename from test-string-output-visitor.c
rename to tests/test-string-output-visitor.c
Signature · 1 line
1.7.9.1
#2 lore raw

PATCH v4 2/7 qtest: add test framework

In reply to Paolo Bonzini

From: Anthony Liguori <[email protected]>

The idea behind qtest is pretty simple. Instead of executing a CPU via TCG or KVM, rely on an external process to send events to the device model that the CPU would normally generate.

qtest presents itself as an accelerator. In addition, a new option is added to establish a qtest server (-qtest) that takes a character device. This is what allows the external process to send CPU events to the device model.

qtest uses a simple line based protocol to send the events. Documentation of that protocol is in qtest.c.

I considered reusing the monitor for this job. Adding interrupts would be a bit difficult. In addition, logging would also be difficult.

qtest has extensive logging support. All protocol commands are logged with time stamps using a new command line option (-qtest-log). Logging is important since ultimately, this is a feature for debugging.

---
 Makefile.objs   |    2 +
 cpu-exec.c      |    1 +
 cpus.c          |   62 +++++++++-
 qemu-options.hx |    8 ++
 qtest.c         |  354 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qtest.h         |   35 ++++++
 scripts/qtest   |    5 +
 vl.c            |   10 ++-
 8 files changed, 473 insertions(+), 4 deletions(-)
 create mode 100644 qtest.c
 create mode 100644 qtest.h
 create mode 100755 scripts/qtest
Makefile.objs +2 -0
index 226b01d..e9842b0 100644
@@ -322,6 +322,8 @@ hw-obj-$(CONFIG_DP8393X) += dp8393x.o
 hw-obj-$(CONFIG_DS1225Y) += ds1225y.o
 hw-obj-$(CONFIG_MIPSNET) += mipsnet.o
 
+hw-obj-y += qtest.o
+
 # Sound
 sound-obj-y =
 sound-obj-$(CONFIG_SB16) += sb16.o
cpu-exec.c +1 -0
index 0fa8325..d153f97 100644
@@ -21,6 +21,7 @@
 #include "disas.h"
 #include "tcg.h"
 #include "qemu-barrier.h"
+#include "qtest.h"
 
 int tb_invalidated_flag;
 
cpus.c +59 -3

93 lines not on this page · raw message

qemu-options.hx +8 -0
index 662f571..fe88939 100644
@@ -2715,6 +2715,14 @@ the @var{simple} tracing backend.
 @end table
 ETEXI
 
+DEF("qtest", HAS_ARG, QEMU_OPTION_qtest,
+    "-qtest CHR      specify tracing options\n",
+    QEMU_ARCH_ALL)
+
+DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log,
+    "-qtest-log LOG  specify tracing options\n",
+    QEMU_ARCH_ALL)
+
 HXCOMM This is the last statement. Insert new options before this line!
 STEXI
 @end table
qtest.c new file +354 -0

354 lines not on this page · raw message

qtest.h new file +35 -0
new file mode 100644
index 0000000..1478343
@@ -0,0 +1,35 @@
+/*
+ * Test Server
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QTEST_H
+#define QTEST_H
+
+#include "qemu-common.h"
+
+extern int qtest_allowed;
+extern const char *qtest_chrdev;
+extern const char *qtest_log;
+
+static inline bool qtest_enabled(void)
+{
+    return qtest_allowed;
+}
+
+static inline int qtest_available(void)
+{
+    return 1;
+}
+
+int qtest_init(void);
+
+#endif
scripts/qtest new file +5 -0
new file mode 100755
index 0000000..4ef6c1c
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+export QTEST_QEMU_BINARY=$1
+shift
+"$@"
vl.c +9 -1
index 0fccf50..e575401 100644
@@ -152,6 +152,7 @@ int main(int argc, char **argv)
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
 #endif
+#include "qtest.h"
 
 #include "disas.h"
 
@@ -1312,7 +1313,7 @@ int qemu_shutdown_requested(void)
 
 void qemu_kill_report(void)
 {
-    if (shutdown_signal != -1) {
+    if (!qtest_enabled() && shutdown_signal != -1) {
         fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
         if (shutdown_pid == 0) {
             /* This happens for eg ^C at the terminal, so it's worth
@@ -2098,6 +2099,7 @@ static struct {
     { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
     { "xen", "Xen", xen_available, xen_init, &xen_allowed },
     { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
+    { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
 };
 
 static int configure_accelerator(void)
@@ -3181,6 +3183,12 @@ int main(int argc, char **argv, char **envp)
                     fclose(fp);
                     break;
                 }
+            case QEMU_OPTION_qtest:
+                qtest_chrdev = optarg;
+                break;
+            case QEMU_OPTION_qtest_log:
+                qtest_log = optarg;
+                break;
             default:
                 os_parse_cmd_args(popt->index, optarg);
             }
Signature · 1 line
1.7.9.1
#3 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] qtest: add test framework

Hi

this commit breaks builds on Linux i386:

CC libhw64/qtest.o
cc1: warnings being treated as errors
/home/stefan/src/qemu/qemu.org/qemu/qtest.c: In function ‘qtest_send_prefix’:
/home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%lld’ expects type ‘long long int’, but argument 3 has type ‘__time_t’ /home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%06lld’ expects type ‘long long int’, but argument 4 has type ‘__suseconds_t’ (more follow)

The size of the timeval elements is only 32 bit on my Debian installation, Therefore this format string in qtest.c does not work:

+#define FMT_timeval "%" PRId64 ".%06" PRId64

Regards,
Stefan W.

#4 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] qtest: add test framework

Quoted 13 lines · Stefan Weil

Hi

this commit breaks builds on Linux i386:

CC libhw64/qtest.o
cc1: warnings being treated as errors /home/stefan/src/qemu/qemu.org/qemu/qtest.c: In function ‘qtest_send_prefix’:
/home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%lld’ expects type ‘long long int’, but argument 3 has type ‘__time_t’ /home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%06lld’ expects type ‘long long int’, but argument 4 has type ‘__suseconds_t’ (more follow)

Quoted 4 lines

The size of the timeval elements is only 32 bit on my Debian installation, Therefore this format string in qtest.c does not work:

+#define FMT_timeval "%" PRId64 ".%06" PRId64

Does %ld work instead of PRId64?

Regards,

Anthony Liguori

Quoted 5 lines

Regards,
Stefan W.

#5 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] qtest: add test framework

Am 30.03.2012 19:50, schrieb Anthony Liguori:

Quoted 14 lines · Stefan Weil
Quoted 13 lines

Hi

this commit breaks builds on Linux i386:

CC libhw64/qtest.o
cc1: warnings being treated as errors /home/stefan/src/qemu/qemu.org/qemu/qtest.c: In function ‘qtest_send_prefix’:
/home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%lld’ expects type ‘long long int’, but argument 3 has type ‘__time_t’ /home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%06lld’ expects type ‘long long int’, but argument 4 has type ‘__suseconds_t’ (more follow)

Quoted 6 lines
Quoted 4 lines

The size of the timeval elements is only 32 bit on my Debian installation, Therefore this format string in qtest.c does not work:

+#define FMT_timeval "%" PRId64 ".%06" PRId64

Does %ld work instead of PRId64?

That works for my 32 bit Debian, but it would fail on 64 bit Linux.

Quoted 12 lines

Regards,

Anthony Liguori

Quoted 5 lines

Regards,
Stefan W.

#6 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] qtest: add test framework

Quoted 15 lines · Stefan Weil

Am 30.03.2012 19:50, schrieb Anthony Liguori:

Quoted 14 lines · Stefan Weil
Quoted 13 lines

Hi

this commit breaks builds on Linux i386:

CC libhw64/qtest.o
cc1: warnings being treated as errors /home/stefan/src/qemu/qemu.org/qemu/qtest.c: In function ‘qtest_send_prefix’:
/home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%lld’ expects type ‘long long int’, but argument 3 has type ‘__time_t’ /home/stefan/src/qemu/qemu.org/qemu/qtest.c:156: error: format ‘%06lld’ expects type ‘long long int’, but argument 4 has type ‘__suseconds_t’ (more follow)

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

The size of the timeval elements is only 32 bit on my Debian installation, Therefore this format string in qtest.c does not work:

+#define FMT_timeval "%" PRId64 ".%06" PRId64

Does %ld work instead of PRId64?

That works for my 32 bit Debian, but it would fail on 64 bit Linux.

It works on my 64-bit Linux fwiw.

Regards,

Anthony Liguori

Quoted 14 lines
Quoted 12 lines

Regards,

Anthony Liguori

Quoted 5 lines

Regards,
Stefan W.

#7 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] qtest: add test framework

Il 30/03/2012 19:53, Anthony Liguori ha scritto:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

+#define FMT_timeval "%" PRId64 ".%06" PRId64

Does %ld work instead of PRId64?

That works for my 32 bit Debian, but it would fail on 64 bit Linux.

It works on my 64-bit Linux fwiw.

It would fail on x32, but it's a good start. I could only fix it up next Monday, so feel free to beat me to it.

Paolo

#8 lore raw

PATCH v4 3/7 qtest: IRQ interception infrastructure

In reply to Paolo Bonzini

Since /i440fx/piix3 is being removed from the composition tree, the IO-APIC is placed under /i440fx. This is wrong and should be changed as soon as the /i440fx/piix3 path is put back.

---
 hw/irq.c     |   17 +++++++++++
 hw/irq.h     |    5 +++
 hw/pc_piix.c |    5 ++-
 qtest.c      |   92 ++++++++++++++++++++++++++++++++++++++++++---------------
 4 files changed, 94 insertions(+), 25 deletions(-)
hw/irq.c +17 -0
index 62f766e..d413a0b 100644
@@ -104,3 +104,20 @@ qemu_irq *qemu_irq_proxy(qemu_irq **target, int n)
 {
     return qemu_allocate_irqs(proxy_irq_handler, target, n);
 }
+
+void qemu_irq_intercept_in(qemu_irq *gpio_in, qemu_irq_handler handler, int n)
+{
+    int i;
+    qemu_irq *old_irqs = qemu_allocate_irqs(NULL, NULL, n);
+    for (i = 0; i < n; i++) {
+        *old_irqs[i] = *gpio_in[i];
+        gpio_in[i]->handler = handler;
+        gpio_in[i]->opaque = old_irqs;
+    }
+}
+
+void qemu_irq_intercept_out(qemu_irq **gpio_out, qemu_irq_handler handler, int n)
+{
+    qemu_irq *old_irqs = *gpio_out;
+    *gpio_out = qemu_allocate_irqs(handler, old_irqs, n);
+}
hw/irq.h +5 -0
index 64da2fd..56c55f0 100644
@@ -38,4 +38,9 @@ qemu_irq qemu_irq_split(qemu_irq irq1, qemu_irq irq2);
  */
 qemu_irq *qemu_irq_proxy(qemu_irq **target, int n);
 
+/* For internal use in qtest.  Similar to qemu_irq_split, but operating
+   on an existing vector of qemu_irq.  */
+void qemu_irq_intercept_in(qemu_irq *gpio_in, qemu_irq_handler handler, int n);
+void qemu_irq_intercept_out(qemu_irq **gpio_out, qemu_irq_handler handler, int n);
+
 #endif
hw/pc_piix.c +3 -0
index 3f99f9a..a5f9551 100644
@@ -107,6 +107,9 @@ static void ioapic_init(GSIState *gsi_state)
     } else {
         dev = qdev_create(NULL, "ioapic");
     }
+    /* FIXME: this should be under the piix3.  */
+    object_property_add_child(object_resolve_path("i440fx", NULL),
+                              "ioapic", OBJECT(dev), NULL);
     qdev_init_nofail(dev);
     d = sysbus_from_qdev(dev);
     sysbus_mmio_map(d, 0, 0xfec00000);
qtest.c +68 -24

134 lines not on this page · raw message

Signature · 1 line
1.7.9.1
#9 lore raw

PATCH v4 4/7 qtest: add clock management

In reply to Paolo Bonzini

This patch combines qtest and -icount together to turn the vm_clock into a source that can be fully managed by the client. To this end new commands clock_step and clock_set are added. Hooking them with libqtest is left as an exercise to the reader.

---
 cpus.c       |   20 ++++++++++++++++++++
 cpus.h       |    2 ++
 qemu-timer.c |    2 +-
 qemu-timer.h |    1 +
 qtest.c      |   45 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 69 insertions(+), 1 deletions(-)
cpus.c +20 -0
index 010047e..107b2ca 100644
@@ -34,6 +34,7 @@
 
 #include "qemu-thread.h"
 #include "cpus.h"
+#include "qtest.h"
 #include "main-loop.h"
 
 #ifndef _WIN32
@@ -238,6 +239,20 @@ static void icount_warp_rt(void *opaque)
     vm_clock_warp_start = -1;
 }
 
+void qtest_clock_warp(int64_t dest)
+{
+    int64_t clock = qemu_get_clock_ns(vm_clock);
+    assert(qtest_enabled());
+    while (clock < dest) {
+        int64_t deadline = qemu_clock_deadline(vm_clock);
+        int64_t warp = MIN(dest - clock, deadline);
+        qemu_icount_bias += warp;
+        qemu_run_timers(vm_clock);
+        clock = qemu_get_clock_ns(vm_clock);
+    }
+    qemu_notify_event();
+}
+
 void qemu_clock_warp(QEMUClock *clock)
 {
     int64_t deadline;
@@ -264,6 +279,11 @@ void qemu_clock_warp(QEMUClock *clock)
         return;
     }
 
+    if (qtest_enabled()) {
+        /* When testing, qtest commands advance icount.  */
+	return;
+    }
+
     vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
     deadline = qemu_clock_deadline(vm_clock);
     if (deadline > 0) {
cpus.h +2 -0
index 4ea2fe2..81bd817 100644
@@ -11,6 +11,8 @@ void cpu_synchronize_all_states(void);
 void cpu_synchronize_all_post_reset(void);
 void cpu_synchronize_all_post_init(void);
 
+void qtest_clock_warp(int64_t dest);
+
 /* vl.c */
 extern int smp_cores;
 extern int smp_threads;
qemu-timer.c +1 -1
index d7f56e5..80bcc56 100644
@@ -397,7 +397,7 @@ int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
     return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
 }
 
-static void qemu_run_timers(QEMUClock *clock)
+void qemu_run_timers(QEMUClock *clock)
 {
     QEMUTimer **ptimer_head, *ts;
     int64_t current_time;
qemu-timer.h +1 -0
index de17f3b..661bbe7 100644
@@ -59,6 +59,7 @@ int qemu_timer_pending(QEMUTimer *ts);
 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
 uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);
 
+void qemu_run_timers(QEMUClock *clock);
 void qemu_run_all_timers(void);
 int qemu_alarm_pending(void);
 void configure_alarms(char const *opt);
qtest.c +45 -0

69 lines not on this page · raw message

Signature · 1 line
1.7.9.1
#10 lore raw

PATCH v4 5/7 qtest: add C version of test infrastructure

In reply to Paolo Bonzini

From: Anthony Liguori <[email protected]>

This also includes a qtest wrapper script to make it easier to launch qtest tests directly.

---
 osdep.h          |    2 +
 qemu-common.h    |    1 -
 tests/Makefile   |   26 ++++-
 tests/libqtest.c |  385 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqtest.h |  333 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 743 insertions(+), 4 deletions(-)
 create mode 100644 tests/libqtest.c
 create mode 100644 tests/libqtest.h
osdep.h +2 -0
index 156666e..428285c 100644
@@ -140,4 +140,6 @@ static inline void qemu_timersub(const struct timeval *val1,
 #define qemu_timersub timersub
 #endif
 
+void qemu_set_cloexec(int fd);
+
 #endif
qemu-common.h +0 -1
index c9e96a8..4647dd9 100644
@@ -190,7 +190,6 @@ ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
     QEMU_WARN_UNUSED_RESULT;
 ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
     QEMU_WARN_UNUSED_RESULT;
-void qemu_set_cloexec(int fd);
 
 #ifndef _WIN32
 int qemu_eventfd(int pipefd[2]);
tests/Makefile +24 -3
index 249f972..99ca308 100644
@@ -58,11 +58,22 @@ tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qap
 tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y)
 tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y)
 
+# QTest rules
+
+TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
+QTEST_TARGETS=$(foreach TARGET,$(TARGETS), $(if $(check-qtest-$(TARGET)-y), $(TARGET),))
+check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET)-y))
+
+qtest-obj-y = tests/libqtest.o $(oslib-obj-y)
+$(check-qtest-y): $(qtest-obj-y)
+
 .PHONY: check-help
 check-help:
 	@echo "Regression testing targets:"
 	@echo
 	@echo " make check                Run all tests"
+	@echo " make check-qtest-TARGET   Run qtest tests for given target"
+	@echo " make check-qtest          Run qtest tests"
 	@echo " make check-unit           Run qobject tests"
 	@echo " make check-block          Run block tests"
 	@echo " make check-report.html    Generates an HTML test report"
@@ -81,18 +91,27 @@ GTESTER_OPTIONS = -k $(if $(V),--verbose,-q)
 
 # gtester tests, possibly with verbose output
 
+.PHONY: $(patsubst %, check-qtest-%, $(QTEST_TARGETS))
+$(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: $(check-qtest-y)
+	$(call quiet-command,QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \
+		gtester $(GTESTER_OPTIONS) -m=$(SPEED) $(check-qtest-$*-y),"GTESTER $@")
+
 .PHONY: $(patsubst %, check-%, $(check-unit-y))
 $(patsubst %, check-%, $(check-unit-y)): check-%: %
 	$(call quiet-command,gtester $(GTESTER_OPTIONS) -m=$(SPEED) $*,"GTESTER $*")
 
 # gtester tests with XML output
 
+$(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%.xml: $(check-qtest-y)
+	$(call quiet-command,QTEST_QEMU_BINARY=$*-softmmu/qemu-system-$* \
+	  gtester -q $(GTESTER_OPTIONS) -o $@ -m=$(SPEED) $(check-qtest-$*-y),"GTESTER $@")
+
 check-report-unit.xml: $(check-unit-y)
 	$(call quiet-command,gtester -q $(GTESTER_OPTIONS) -o $@ -m=$(SPEED) $^, "GTESTER $@")
 
 # Reports and overall runs
 
-check-report.xml: check-report-unit.xml
+check-report.xml: $(patsubst %,check-report-qtest-%.xml, $(QTEST_TARGETS)) check-report-unit.xml
 	$(call quiet-command,$(SRC_PATH)/scripts/gtester-cat $^ > $@, "  GEN    $@")
 
 check-report.html: check-report.xml
@@ -107,7 +126,8 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF)
 
 # Consolidated targets
 
-.PHONY: check-unit check
+.PHONY: check-qtest check-unit check
+check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
 check-unit: $(patsubst %,check-%, $(check-unit-y))
 check-block: $(patsubst %,check-%, $(check-block-y))
-check: check-unit
+check: check-unit check-qtest
tests/libqtest.c new file +387 -0

387 lines not on this page · raw message

tests/libqtest.h new file +335 -0

335 lines not on this page · raw message

Signature · 1 line
1.7.9.1
#11 lore raw

PATCH v4 6/7 rtc: split out macros into a header file and use in test case

In reply to Paolo Bonzini

From: Anthony Liguori <[email protected]>

---
 hw/mc146818rtc.c      |   33 --------------------------
 hw/mc146818rtc.h      |    3 +-
 hw/mc146818rtc_regs.h |   62 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 35 deletions(-)
 create mode 100644 hw/mc146818rtc_regs.h
hw/mc146818rtc.c +0 -33
index 2b59c36..9c64e0a 100644
@@ -47,39 +47,6 @@
 
 #define RTC_REINJECT_ON_ACK_COUNT 20
 
-#define RTC_SECONDS             0
-#define RTC_SECONDS_ALARM       1
-#define RTC_MINUTES             2
-#define RTC_MINUTES_ALARM       3
-#define RTC_HOURS               4
-#define RTC_HOURS_ALARM         5
-#define RTC_ALARM_DONT_CARE    0xC0
-
-#define RTC_DAY_OF_WEEK         6
-#define RTC_DAY_OF_MONTH        7
-#define RTC_MONTH               8
-#define RTC_YEAR                9
-
-#define RTC_REG_A               10
-#define RTC_REG_B               11
-#define RTC_REG_C               12
-#define RTC_REG_D               13
-
-#define REG_A_UIP 0x80
-
-#define REG_B_SET  0x80
-#define REG_B_PIE  0x40
-#define REG_B_AIE  0x20
-#define REG_B_UIE  0x10
-#define REG_B_SQWE 0x08
-#define REG_B_DM   0x04
-#define REG_B_24H  0x02
-
-#define REG_C_UF   0x10
-#define REG_C_IRQF 0x80
-#define REG_C_PF   0x40
-#define REG_C_AF   0x20
-
 typedef struct RTCState {
     ISADevice dev;
     MemoryRegion io;
hw/mc146818rtc.h +1 -2
index f119930..f286b6a 100644
@@ -2,8 +2,7 @@
 #define MC146818RTC_H
 
 #include "isa.h"
-
-#define RTC_ISA_IRQ 8
+#include "mc146818rtc_regs.h"
 
 ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq);
 void rtc_set_memory(ISADevice *dev, int addr, int val);
hw/mc146818rtc_regs.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.7.9.1
#12 lore raw

PATCH v4 7/7 qtest: add rtc-test test-case

In reply to Paolo Bonzini

From: Anthony Liguori <[email protected]>

---
 tests/Makefile   |    5 +
 tests/rtc-test.c |  263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+), 0 deletions(-)
 create mode 100644 tests/rtc-test.c
tests/Makefile +5 -0
index 99ca308..42ce2d7 100644
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)
+
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
 
 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
tests/rtc-test.c new file +263 -0

263 lines not on this page · raw message

Signature · 1 line
1.7.9.1
#13 lore raw

Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case

Quoted 18 lines · Paolo Bonzini

From: Anthony Liguori <[email protected]>

---
 tests/Makefile   |    5 +
 tests/rtc-test.c |  263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 268 insertions(+), 0 deletions(-)

 create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index 99ca308..42ce2d7 100644
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 

 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh

tests/Makefile check-unit-y += tests/test-coroutine$(EXESUF) · line 22

Quoted 4 lines
tests/Makefile +4 -0
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)

I get this error when trying 'make check':
GTESTER tests/test-string-output-visitor
GTESTER tests/test-coroutine
ccache gcc -O2 -g -Wl,--warn-common -m64 -g
/src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o
qemu-thread-posix.o -o tests/rtc-test
/src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No such file or directory
/src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory /src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside parameter list
/src/qemu/tests/rtc-test.c:46: warning: its scope is only this definition or declaration, which is probably not what you want

It looks like the build rules are not correct. This is also an out of tree build. Perhaps the patsubst rules in tests/Makefile do not match rtc-test because it doesn't start with "qtest-"?

Quoted 277 lines

+
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h

 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \

tests/rtc-test.c new file +263 -1
new file mode 100644
index 0000000..22f807c
@@ -0,0 +1,263 @@
+/*
+ * QTest testcase for the MC146818 real-time clock
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10) << 4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a < b) {
+        return -1;
+    } else if (a > b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B) & REG_B_24H) == 0) {
+        if (hour >= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts, &dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee that we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */
+
+    ts = time(NULL);
+    gmtime_r(&ts, &start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts, &end);
+
+    if (tm_cmp(&date[0], &date[1]) == 0) {
+        datep = &date[0];
+    } else if (tm_cmp(&date[1], &date[2]) == 0) {
+        datep = &date[1];
+    } else if (tm_cmp(&date[2], &date[3]) == 0) {
+        datep = &date[2];
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t < s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }
+
+        g_assert_cmpint(ABS(t - s), <=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM);
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts, &now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    cmos_read(RTC_REG_C);
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i < 2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        clock_step(1000000000);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
+    g_assert(cmos_read(RTC_REG_C) == 0);
+}
+
+int main(int argc, char **argv)
+{
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc, &argv, NULL);
+
+    s = qtest_start("-display none -rtc clock=vm");
+    qtest_irq_intercept_in(s, "ioapic");
+
+    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+    qtest_add_func("/rtc/dec/check-time", dec_check_time);
+    qtest_add_func("/rtc/alarm-time", alarm_time);
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
--

1.7.9.1

#14 lore raw

Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case

Quoted 38 lines · Blue Swirl
Quoted 18 lines · Paolo Bonzini

From: Anthony Liguori <[email protected]>

---
 tests/Makefile   |    5 +
 tests/rtc-test.c |  263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 268 insertions(+), 0 deletions(-)

 create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index 99ca308..42ce2d7 100644
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 

 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh

tests/Makefile

Quoted 4 lines
tests/Makefile +4 -0
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)

I get this error when trying 'make check':
GTESTER tests/test-string-output-visitor
GTESTER tests/test-coroutine
ccache gcc -O2 -g   -Wl,--warn-common -m64 -g /src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o qemu-thread-posix.o   -o tests/rtc-test /src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No such file or directory
/src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory /src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside parameter list
/src/qemu/tests/rtc-test.c:46: warning: its scope is only this definition or declaration, which is probably not what you want

Quoted 3 lines

It looks like the build rules are not correct. This is also an out of tree build. Perhaps the patsubst rules in tests/Makefile do not match rtc-test because it doesn't start with "qtest-"?

This fixes 'make check'. Trace objects are also needed if tracing is enabled.

tests/Makefile +0 -0
index 29c1e51..a98a848 100644
@@ -63,6 +63,8 @@ tests/test-qmp-input-visitor$(EXESUF):

tests/test-qmp-input-visitor.o $(test-qap
tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o
$(test-qapi-obj-y)
tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o
tests/test-qmp-marshal.o $(test-qapi-obj-y)

+tests/rtc-test$(EXESUF): tests/rtc-test.o $(trace-obj-y) +
# QTest rules

TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))

Quoted 277 lines
Quoted 277 lines

+
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h

 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \

tests/rtc-test.c new file +263 -1
new file mode 100644
index 0000000..22f807c
@@ -0,0 +1,263 @@
+/*
+ * QTest testcase for the MC146818 real-time clock
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10) << 4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a < b) {
+        return -1;
+    } else if (a > b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B) & REG_B_24H) == 0) {
+        if (hour >= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts, &dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee that we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */
+
+    ts = time(NULL);
+    gmtime_r(&ts, &start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts, &end);
+
+    if (tm_cmp(&date[0], &date[1]) == 0) {
+        datep = &date[0];
+    } else if (tm_cmp(&date[1], &date[2]) == 0) {
+        datep = &date[1];
+    } else if (tm_cmp(&date[2], &date[3]) == 0) {
+        datep = &date[2];
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t < s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }
+
+        g_assert_cmpint(ABS(t - s), <=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM);
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts, &now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    cmos_read(RTC_REG_C);
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i < 2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        clock_step(1000000000);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
+    g_assert(cmos_read(RTC_REG_C) == 0);
+}
+
+int main(int argc, char **argv)
+{
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc, &argv, NULL);
+
+    s = qtest_start("-display none -rtc clock=vm");
+    qtest_irq_intercept_in(s, "ioapic");
+
+    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+    qtest_add_func("/rtc/dec/check-time", dec_check_time);
+    qtest_add_func("/rtc/alarm-time", alarm_time);
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
--

1.7.9.1

Reviewed-by
#15 lore raw

Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case

Quoted 56 lines · Blue Swirl
Quoted 38 lines · Blue Swirl
Quoted 15 lines · Paolo Bonzini

From: Anthony Liguori<[email protected]>

---
  tests/Makefile   |    5 +
  tests/rtc-test.c |  263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 268 insertions(+), 0 deletions(-)

create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index 99ca308..42ce2d7 100644

tests/Makefile check-unit-y += tests/test-coroutine$(EXESUF) · line 22

Quoted 7 lines
tests/Makefile +4 -0
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 
  check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)

I get this error when trying 'make check':
GTESTER tests/test-string-output-visitor
GTESTER tests/test-coroutine
ccache gcc -O2 -g -Wl,--warn-common -m64 -g
/src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o
qemu-thread-posix.o -o tests/rtc-test
/src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No such file or directory
/src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory /src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside parameter list
/src/qemu/tests/rtc-test.c:46: warning: its scope is only this definition or declaration, which is probably not what you want

Quoted 3 lines

It looks like the build rules are not correct. This is also an out of tree build. Perhaps the patsubst rules in tests/Makefile do not match rtc-test because it doesn't start with "qtest-"?

This fixes 'make check'. Trace objects are also needed if tracing is enabled.

tests/Makefile +0 -0
index 29c1e51..a98a848 100644
@@ -63,6 +63,8 @@ tests/test-qmp-input-visitor$(EXESUF):

tests/test-qmp-input-visitor.o $(test-qap
tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o
$(test-qapi-obj-y)
tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o
tests/test-qmp-marshal.o $(test-qapi-obj-y)

tests/Makefile

Quoted 5 lines
tests/Makefile +2 -0
+tests/rtc-test$(EXESUF): tests/rtc-test.o $(trace-obj-y)
+
  # QTest rules
 
  TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))

Could you please add a SoB and apply Blue? Thanks!

Regards,

Anthony Liguori

Quoted 279 lines
Quoted 278 lines
Quoted 278 lines
tests/Makefile +1 -0
+
  GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
 
  test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
tests/rtc-test.c new file +263 -1
new file mode 100644
index 0000000..22f807c
@@ -0,0 +1,263 @@
+/*
+ * QTest testcase for the MC146818 real-time clock
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include<glib.h>
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+#include<unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value>>  4)&  0x0F) * 10) + (value&  0x0F);
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10)<<  4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a<  b) {
+        return -1;
+    } else if (a>  b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B)&  REG_B_24H) == 0) {
+        if (hour>= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts,&dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee that we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */
+
+    ts = time(NULL);
+    gmtime_r(&ts,&start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts,&end);
+
+    if (tm_cmp(&date[0],&date[1]) == 0) {
+        datep =&date[0];
+    } else if (tm_cmp(&date[1],&date[2]) == 0) {
+        datep =&date[1];
+    } else if (tm_cmp(&date[2],&date[3]) == 0) {
+        datep =&date[2];
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep)<= 0&&  tm_cmp(datep,&end)<= 0)) {
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t<  s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }
+
+        g_assert_cmpint(ABS(t - s),<=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B)&  ~REG_B_DM);
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts,&now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    cmos_read(RTC_REG_C);
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i<  2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        clock_step(1000000000);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert((cmos_read(RTC_REG_C)&  REG_C_AF) != 0);
+    g_assert(cmos_read(RTC_REG_C) == 0);
+}
+
+int main(int argc, char **argv)
+{
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc,&argv, NULL);
+
+    s = qtest_start("-display none -rtc clock=vm");
+    qtest_irq_intercept_in(s, "ioapic");
+
+    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+    qtest_add_func("/rtc/dec/check-time", dec_check_time);
+    qtest_add_func("/rtc/alarm-time", alarm_time);
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
--

1.7.9.1

#16 lore raw

Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case

Quoted 69 lines · Anthony Liguori
Quoted 68 lines · Blue Swirl
Quoted 43 lines · Blue Swirl
Quoted 20 lines · Paolo Bonzini

From: Anthony Liguori<[email protected]>

---
 tests/Makefile   |    5 +
 tests/rtc-test.c |  263
++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 268 insertions(+), 0 deletions(-)

 create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index 99ca308..42ce2d7 100644
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 

 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh

tests/Makefile

Quoted 4 lines
tests/Makefile +4 -0
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)

I get this error when trying 'make check':
GTESTER tests/test-string-output-visitor
GTESTER tests/test-coroutine
ccache gcc -O2 -g   -Wl,--warn-common -m64 -g /src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o qemu-thread-posix.o   -o tests/rtc-test /src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No such file or directory
/src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory
/src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside parameter list
/src/qemu/tests/rtc-test.c:46: warning: its scope is only this definition or declaration, which is probably not what you want

Quoted 3 lines

It looks like the build rules are not correct. This is also an out of tree build. Perhaps the patsubst rules in tests/Makefile do not match rtc-test because it doesn't start with "qtest-"?

This fixes 'make check'. Trace objects are also needed if tracing is enabled.

tests/Makefile +0 -0
index 29c1e51..a98a848 100644
@@ -63,6 +63,8 @@ tests/test-qmp-input-visitor$(EXESUF):

tests/test-qmp-input-visitor.o $(test-qap tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y)
 tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y)

tests/Makefile +2 -0
+tests/rtc-test$(EXESUF): tests/rtc-test.o $(trace-obj-y)
+

 # QTest rules

Quoted 6 lines
Quoted 1 line

 TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))

Could you please add a SoB and apply Blue?  Thanks!

OK, pushed.

Quoted 305 lines

Regards,

Anthony Liguori

Quoted 300 lines
Quoted 299 lines
Quoted 299 lines

+
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h

 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \

tests/rtc-test.c new file +9 -0
new file mode 100644
index 0000000..22f807c
@@ -0,0 +1,263 @@
+/*
+ * QTest testcase for the MC146818 real-time clock
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or

later.
+ * See the COPYING file in the top-level directory.
+ *
+ */

tests/rtc-test.c new file +118 -0
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include<glib.h>
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+#include<unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value>>  4)&  0x0F) * 10) + (value&  0x0F);
 
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10)<<  4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a<  b) {
+        return -1;
+    } else if (a>  b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
 
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B)&  REG_B_24H) == 0) {
 
+        if (hour>= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts,&dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee

that we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */

tests/rtc-test.c new file +31 -0
+
+    ts = time(NULL);
+    gmtime_r(&ts,&start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts,&end);
+
+    if (tm_cmp(&date[0],&date[1]) == 0) {
+        datep =&date[0];
+    } else if (tm_cmp(&date[1],&date[2]) == 0) {
+        datep =&date[1];
+    } else if (tm_cmp(&date[2],&date[3]) == 0) {
+        datep =&date[2];
 
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep)<= 0&&  tm_cmp(datep,&end)<= 0)) {
 
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t<  s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n",

(s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }

tests/rtc-test.c new file +89 -1
+
+        g_assert_cmpint(ABS(t - s),<=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B)&  ~REG_B_DM);
 
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
 
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts,&now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    cmos_read(RTC_REG_C);
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i<  2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        clock_step(1000000000);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert((cmos_read(RTC_REG_C)&  REG_C_AF) != 0);
 
+    g_assert(cmos_read(RTC_REG_C) == 0);
+}
+
+int main(int argc, char **argv)
+{
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc,&argv, NULL);
 
+
+    s = qtest_start("-display none -rtc clock=vm");
+    qtest_irq_intercept_in(s, "ioapic");
+
+    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+    qtest_add_func("/rtc/dec/check-time", dec_check_time);
+    qtest_add_func("/rtc/alarm-time", alarm_time);
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
--

1.7.9.1

#17 lore raw

Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case

In reply to Blue Swirl
Quoted 38 lines · Blue Swirl
Quoted 15 lines · Paolo Bonzini

From: Anthony Liguori<[email protected]>

---
  tests/Makefile   |    5 +
  tests/rtc-test.c |  263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 268 insertions(+), 0 deletions(-)

create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index 99ca308..42ce2d7 100644

tests/Makefile check-unit-y += tests/test-coroutine$(EXESUF) · line 22

Quoted 7 lines
tests/Makefile +4 -0
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 
  check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)

I get this error when trying 'make check':
GTESTER tests/test-string-output-visitor
GTESTER tests/test-coroutine
ccache gcc -O2 -g -Wl,--warn-common -m64 -g
/src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o
qemu-thread-posix.o -o tests/rtc-test
/src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No such file or directory
/src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory /src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside parameter list
/src/qemu/tests/rtc-test.c:46: warning: its scope is only this definition or declaration, which is probably not what you want

Quoted 3 lines

It looks like the build rules are not correct. This is also an out of tree build. Perhaps the patsubst rules in tests/Makefile do not match rtc-test because it doesn't start with "qtest-"?

It works for me with an out of tree build. I'm a bit surprised you don't have any LDFLAGS or LIBS set.

You are running make check at the top level, right? And make check previously worked for you?

What's your host?

Regards,

Anthony Liguori

Quoted 278 lines
Quoted 277 lines
tests/Makefile +1 -0
+
  GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
 
  test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
tests/rtc-test.c new file +263 -1
new file mode 100644
index 0000000..22f807c
@@ -0,0 +1,263 @@
+/*
+ * QTest testcase for the MC146818 real-time clock
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include<glib.h>
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+#include<unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value>>  4)&  0x0F) * 10) + (value&  0x0F);
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10)<<  4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a<  b) {
+        return -1;
+    } else if (a>  b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B)&  REG_B_24H) == 0) {
+        if (hour>= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts,&dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee that we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */
+
+    ts = time(NULL);
+    gmtime_r(&ts,&start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts,&end);
+
+    if (tm_cmp(&date[0],&date[1]) == 0) {
+        datep =&date[0];
+    } else if (tm_cmp(&date[1],&date[2]) == 0) {
+        datep =&date[1];
+    } else if (tm_cmp(&date[2],&date[3]) == 0) {
+        datep =&date[2];
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep)<= 0&&  tm_cmp(datep,&end)<= 0)) {
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t<  s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }
+
+        g_assert_cmpint(ABS(t - s),<=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B)&  ~REG_B_DM);
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts,&now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    cmos_read(RTC_REG_C);
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i<  2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        clock_step(1000000000);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert((cmos_read(RTC_REG_C)&  REG_C_AF) != 0);
+    g_assert(cmos_read(RTC_REG_C) == 0);
+}
+
+int main(int argc, char **argv)
+{
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc,&argv, NULL);
+
+    s = qtest_start("-display none -rtc clock=vm");
+    qtest_irq_intercept_in(s, "ioapic");
+
+    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+    qtest_add_func("/rtc/dec/check-time", dec_check_time);
+    qtest_add_func("/rtc/alarm-time", alarm_time);
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
--

1.7.9.1

#18 lore raw

Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case

Quoted 48 lines · Anthony Liguori
Quoted 42 lines · Blue Swirl
Quoted 20 lines · Paolo Bonzini

From: Anthony Liguori<[email protected]>

---
 tests/Makefile   |    5 +
 tests/rtc-test.c |  263
++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 268 insertions(+), 0 deletions(-)

 create mode 100644 tests/rtc-test.c

tests/Makefile +0 -0
index 99ca308..42ce2d7 100644
@@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
 

 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh

tests/Makefile check-unit-y += tests/test-coroutine$(EXESUF) · line 22

Quoted 4 lines
tests/Makefile +4 -0
+# All QTests for now are POSIX-only, but the dependencies are
+# really in libqtest, not in the testcases themselves.
+check-qtest-i386-y = tests/rtc-test
+check-qtest-x86_64-y = $(check-qtest-i386-y)

I get this error when trying 'make check':
GTESTER tests/test-string-output-visitor
GTESTER tests/test-coroutine
ccache gcc -O2 -g   -Wl,--warn-common -m64 -g /src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o qemu-thread-posix.o   -o tests/rtc-test /src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No such file or directory
/src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory /src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside parameter list
/src/qemu/tests/rtc-test.c:46: warning: its scope is only this definition or declaration, which is probably not what you want

Quoted 3 lines

It looks like the build rules are not correct. This is also an out of tree build. Perhaps the patsubst rules in tests/Makefile do not match rtc-test because it doesn't start with "qtest-"?

Quoted 5 lines

It works for me with an out of tree build.  I'm a bit surprised you don't have any LDFLAGS or LIBS set.

You are running make check at the top level, right?  And make check previously worked for you?

Top of build tree, not source tree. I hadn't tested before.

Quoted 1 line

What's your host?

Debian stable x86_64.

Quoted 304 lines

Regards,

Anthony Liguori

Quoted 299 lines
Quoted 298 lines

+
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h

 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \

tests/rtc-test.c new file +9 -0
new file mode 100644
index 0000000..22f807c
@@ -0,0 +1,263 @@
+/*
+ * QTest testcase for the MC146818 real-time clock
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Anthony Liguori<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or

later.
+ * See the COPYING file in the top-level directory.
+ *
+ */

tests/rtc-test.c new file +118 -0
+#include "libqtest.h"
+#include "hw/mc146818rtc_regs.h"
+
+#include<glib.h>
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+#include<unistd.h>
+
+static uint8_t base = 0x70;
+
+static int bcd2dec(int value)
+{
+    return (((value>>  4)&  0x0F) * 10) + (value&  0x0F);
 
+}
+
+static int dec2bcd(int value)
+{
+    return ((value / 10)<<  4) | (value % 10);
+}
+
+static uint8_t cmos_read(uint8_t reg)
+{
+    outb(base + 0, reg);
+    return inb(base + 1);
+}
+
+static void cmos_write(uint8_t reg, uint8_t val)
+{
+    outb(base + 0, reg);
+    outb(base + 1, val);
+}
+
+static int tm_cmp(struct tm *lhs, struct tm *rhs)
+{
+    time_t a, b;
+    struct tm d1, d2;
+
+    memcpy(&d1, lhs, sizeof(d1));
+    memcpy(&d2, rhs, sizeof(d2));
+
+    a = mktime(&d1);
+    b = mktime(&d2);
+
+    if (a<  b) {
+        return -1;
+    } else if (a>  b) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#if 0
+static void print_tm(struct tm *tm)
+{
+    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
+}
+#endif
+
+static void cmos_get_date_time(struct tm *date)
+{
+    int base_year = 2000, hour_offset;
+    int sec, min, hour, mday, mon, year;
+    time_t ts;
+    struct tm dummy;
+
+    sec = cmos_read(RTC_SECONDS);
+    min = cmos_read(RTC_MINUTES);
+    hour = cmos_read(RTC_HOURS);
+    mday = cmos_read(RTC_DAY_OF_MONTH);
+    mon = cmos_read(RTC_MONTH);
+    year = cmos_read(RTC_YEAR);
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
 
+        sec = bcd2dec(sec);
+        min = bcd2dec(min);
+        hour = bcd2dec(hour);
+        mday = bcd2dec(mday);
+        mon = bcd2dec(mon);
+        year = bcd2dec(year);
+        hour_offset = 80;
+    } else {
+        hour_offset = 0x80;
+    }
+
+    if ((cmos_read(0x0B)&  REG_B_24H) == 0) {
 
+        if (hour>= hour_offset) {
+            hour -= hour_offset;
+            hour += 12;
+        }
+    }
+
+    ts = time(NULL);
+    localtime_r(&ts,&dummy);
+
+    date->tm_isdst = dummy.tm_isdst;
+    date->tm_sec = sec;
+    date->tm_min = min;
+    date->tm_hour = hour;
+    date->tm_mday = mday;
+    date->tm_mon = mon - 1;
+    date->tm_year = base_year + year - 1900;
+    date->tm_gmtoff = 0;
+
+    ts = mktime(date);
+}
+
+static void check_time(int wiggle)
+{
+    struct tm start, date[4], end;
+    struct tm *datep;
+    time_t ts;
+
+    /*
+     * This check assumes a few things.  First, we cannot guarantee that

we get
+     * a consistent reading from the wall clock because we may hit an edge of
+     * the clock while reading.  To work around this, we read four clock readings
+     * such that at least two of them should match.  We need to assume that one
+     * reading is corrupt so we need four readings to ensure that we have at
+     * least two consecutive identical readings
+     *
+     * It's also possible that we'll cross an edge reading the host clock so
+     * simply check to make sure that the clock reading is within the period of
+     * when we expect it to be.
+     */

tests/rtc-test.c new file +31 -0
+
+    ts = time(NULL);
+    gmtime_r(&ts,&start);
+
+    cmos_get_date_time(&date[0]);
+    cmos_get_date_time(&date[1]);
+    cmos_get_date_time(&date[2]);
+    cmos_get_date_time(&date[3]);
+
+    ts = time(NULL);
+    gmtime_r(&ts,&end);
+
+    if (tm_cmp(&date[0],&date[1]) == 0) {
+        datep =&date[0];
+    } else if (tm_cmp(&date[1],&date[2]) == 0) {
+        datep =&date[1];
+    } else if (tm_cmp(&date[2],&date[3]) == 0) {
+        datep =&date[2];
 
+    } else {
+        g_assert_not_reached();
+    }
+
+    if (!(tm_cmp(&start, datep)<= 0&&  tm_cmp(datep,&end)<= 0)) {
 
+        time_t t, s;
+
+        start.tm_isdst = datep->tm_isdst;
+
+        t = mktime(datep);
+        s = mktime(&start);
+        if (t<  s) {
+            g_test_message("RTC is %ld second(s) behind wall-clock\n",

(s - t));
+        } else {
+            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
+        }

tests/rtc-test.c new file +89 -1
+
+        g_assert_cmpint(ABS(t - s),<=, wiggle);
+    }
+}
+
+static int wiggle = 2;
+
+static void bcd_check_time(void)
+{
+    /* Set BCD mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B)&  ~REG_B_DM);
 
+    check_time(wiggle);
+}
+
+static void dec_check_time(void)
+{
+    /* Set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+    check_time(wiggle);
+}
+
+static void set_alarm_time(struct tm *tm)
+{
+    int sec;
+
+    sec = tm->tm_sec;
+
+    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
 
+        sec = dec2bcd(sec);
+    }
+
+    cmos_write(RTC_SECONDS_ALARM, sec);
+    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
+    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
+}
+
+static void alarm_time(void)
+{
+    struct tm now;
+    time_t ts;
+    int i;
+
+    ts = time(NULL);
+    gmtime_r(&ts,&now);
+
+    /* set DEC mode */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
+
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    cmos_read(RTC_REG_C);
+
+    now.tm_sec = (now.tm_sec + 2) % 60;
+    set_alarm_time(&now);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
+
+    for (i = 0; i<  2 + wiggle; i++) {
+        if (get_irq(RTC_ISA_IRQ)) {
+            break;
+        }
+
+        clock_step(1000000000);
+    }
+
+    g_assert(get_irq(RTC_ISA_IRQ));
+    g_assert((cmos_read(RTC_REG_C)&  REG_C_AF) != 0);
 
+    g_assert(cmos_read(RTC_REG_C) == 0);
+}
+
+int main(int argc, char **argv)
+{
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc,&argv, NULL);
 
+
+    s = qtest_start("-display none -rtc clock=vm");
+    qtest_irq_intercept_in(s, "ioapic");
+
+    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
+    qtest_add_func("/rtc/dec/check-time", dec_check_time);
+    qtest_add_func("/rtc/alarm-time", alarm_time);
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
--

1.7.9.1

#19 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

This is a rebase of qtest. I split the gtester infrastructure into its own patch, and reorganized the tests by moving everything into tests/.

Also, libqtest now has bindings for the clock management commands, and I am using them in rtc-test. Finally, the accept is moved from qemu to libqtest; tests need not sleep anymore until QEMU connects.

This is on top of Luiz's recent pull request.

Thanks, I'll process and apply this ASAP.

Regards,

Anthony Liguori

Quoted 73 lines

Anthony Liguori (4):
qtest: add test framework
qtest: add C version of test infrastructure
rtc: split out macros into a header file and use in test case
qtest: add rtc-test test-case

Paolo Bonzini (3):
test makefile overhaul
qtest: IRQ interception infrastructure
qtest: add clock management

  Makefile.objs                                      |    2 +
  cpu-exec.c                                         |    1 +
  cpus.c                                             |   82 ++++-
  cpus.h                                             |    2 +
  hw/irq.c                                           |   17 +
  hw/irq.h                                           |    5 +
  hw/mc146818rtc.c                                   |   33 --
  hw/mc146818rtc.h                                   |    3 +-
  hw/mc146818rtc_regs.h                              |   62 +++
  hw/pc_piix.c                                       |    5 +-
  osdep.h                                            |    2 +
  qemu-common.h                                      |    1 -
  qemu-options.hx                                    |    8 +
  qemu-timer.c                                       |    2 +-
  qemu-timer.h                                       |    1 +
  qtest.c                                            |  443 ++++++++++++++++++++
  qtest.h                                            |   35 ++
  rules.mak                                          |    2 +-
  scripts/gtester-cat                                |   26 ++
  scripts/qtest                                      |    5 +
  tests/Makefile                                     |  165 ++++++--
  check-qdict.c =>  tests/check-qdict.c               |    0
  check-qfloat.c =>  tests/check-qfloat.c             |    0
  check-qint.c =>  tests/check-qint.c                 |    0
  check-qjson.c =>  tests/check-qjson.c               |    0
  check-qlist.c =>  tests/check-qlist.c               |    0
  check-qstring.c =>  tests/check-qstring.c           |    0
  tests/libqtest.c                                   |  385 +++++++++++++++++
  tests/libqtest.h                                   |  333 +++++++++++++++
  tests/rtc-test.c                                   |  263 ++++++++++++
  test-coroutine.c =>  tests/test-coroutine.c         |    0
  test-qmp-commands.c =>  tests/test-qmp-commands.c   |    0
  .../test-qmp-input-strict.c                        |    0
  .../test-qmp-input-visitor.c                       |    0
  .../test-qmp-output-visitor.c                      |    0
  .../test-string-input-visitor.c                    |    0
  .../test-string-output-visitor.c                   |    0
  vl.c                                               |   10 +-
  38 files changed, 1806 insertions(+), 87 deletions(-)

create mode 100644 hw/mc146818rtc_regs.h
create mode 100644 qtest.c
create mode 100644 qtest.h
create mode 100755 scripts/gtester-cat
create mode 100755 scripts/qtest
rename check-qdict.c => tests/check-qdict.c (100%)
rename check-qfloat.c => tests/check-qfloat.c (100%)
rename check-qint.c => tests/check-qint.c (100%)
rename check-qjson.c => tests/check-qjson.c (100%)
rename check-qlist.c => tests/check-qlist.c (100%)
rename check-qstring.c => tests/check-qstring.c (100%)
create mode 100644 tests/libqtest.c
create mode 100644 tests/libqtest.h
create mode 100644 tests/rtc-test.c
rename test-coroutine.c => tests/test-coroutine.c (100%)
rename test-qmp-commands.c => tests/test-qmp-commands.c (100%) rename test-qmp-input-strict.c => tests/test-qmp-input-strict.c (100%) rename test-qmp-input-visitor.c => tests/test-qmp-input-visitor.c (100%) rename test-qmp-output-visitor.c => tests/test-qmp-output-visitor.c (100%) rename test-string-input-visitor.c => tests/test-string-input-visitor.c (100%) rename test-string-output-visitor.c => tests/test-string-output-visitor.c (100%)

#20 lore raw
In reply to Paolo Bonzini
Quoted 16 lines · Paolo Bonzini

This is a rebase of qtest. I split the gtester infrastructure into its own patch, and reorganized the tests by moving everything into tests/.

Also, libqtest now has bindings for the clock management commands, and I am using them in rtc-test. Finally, the accept is moved from qemu to libqtest; tests need not sleep anymore until QEMU connects.

This is on top of Luiz's recent pull request.

Anthony Liguori (4):
qtest: add test framework
qtest: add C version of test infrastructure
rtc: split out macros into a header file and use in test case
qtest: add rtc-test test-case

Quoted 4 lines

Paolo Bonzini (3):
test makefile overhaul
qtest: IRQ interception infrastructure
qtest: add clock management

Applied. Thanks!

Regards,

Anthony Liguori

Quoted 62 lines
  Makefile.objs                                      |    2 +
  cpu-exec.c                                         |    1 +
  cpus.c                                             |   82 ++++-
  cpus.h                                             |    2 +
  hw/irq.c                                           |   17 +
  hw/irq.h                                           |    5 +
  hw/mc146818rtc.c                                   |   33 --
  hw/mc146818rtc.h                                   |    3 +-
  hw/mc146818rtc_regs.h                              |   62 +++
  hw/pc_piix.c                                       |    5 +-
  osdep.h                                            |    2 +
  qemu-common.h                                      |    1 -
  qemu-options.hx                                    |    8 +
  qemu-timer.c                                       |    2 +-
  qemu-timer.h                                       |    1 +
  qtest.c                                            |  443 ++++++++++++++++++++
  qtest.h                                            |   35 ++
  rules.mak                                          |    2 +-
  scripts/gtester-cat                                |   26 ++
  scripts/qtest                                      |    5 +
  tests/Makefile                                     |  165 ++++++--
  check-qdict.c =>  tests/check-qdict.c               |    0
  check-qfloat.c =>  tests/check-qfloat.c             |    0
  check-qint.c =>  tests/check-qint.c                 |    0
  check-qjson.c =>  tests/check-qjson.c               |    0
  check-qlist.c =>  tests/check-qlist.c               |    0
  check-qstring.c =>  tests/check-qstring.c           |    0
  tests/libqtest.c                                   |  385 +++++++++++++++++
  tests/libqtest.h                                   |  333 +++++++++++++++
  tests/rtc-test.c                                   |  263 ++++++++++++
  test-coroutine.c =>  tests/test-coroutine.c         |    0
  test-qmp-commands.c =>  tests/test-qmp-commands.c   |    0
  .../test-qmp-input-strict.c                        |    0
  .../test-qmp-input-visitor.c                       |    0
  .../test-qmp-output-visitor.c                      |    0
  .../test-string-input-visitor.c                    |    0
  .../test-string-output-visitor.c                   |    0
  vl.c                                               |   10 +-
  38 files changed, 1806 insertions(+), 87 deletions(-)

create mode 100644 hw/mc146818rtc_regs.h
create mode 100644 qtest.c
create mode 100644 qtest.h
create mode 100755 scripts/gtester-cat
create mode 100755 scripts/qtest
rename check-qdict.c => tests/check-qdict.c (100%)
rename check-qfloat.c => tests/check-qfloat.c (100%)
rename check-qint.c => tests/check-qint.c (100%)
rename check-qjson.c => tests/check-qjson.c (100%)
rename check-qlist.c => tests/check-qlist.c (100%)
rename check-qstring.c => tests/check-qstring.c (100%)
create mode 100644 tests/libqtest.c
create mode 100644 tests/libqtest.h
create mode 100644 tests/rtc-test.c
rename test-coroutine.c => tests/test-coroutine.c (100%)
rename test-qmp-commands.c => tests/test-qmp-commands.c (100%) rename test-qmp-input-strict.c => tests/test-qmp-input-strict.c (100%) rename test-qmp-input-visitor.c => tests/test-qmp-input-visitor.c (100%) rename test-qmp-output-visitor.c => tests/test-qmp-output-visitor.c (100%) rename test-string-input-visitor.c => tests/test-string-input-visitor.c (100%) rename test-string-output-visitor.c => tests/test-string-output-visitor.c (100%)