Skip to contentSkip to conversation

add direct support of event in qapi schema

RFC PATCH Superseded Wenchao Xia 2 participants 14 messages Mar 19, 2014 – Mar 24, 2014 Kbuild

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover add direct support of event in qapi schema

This series add support for tag/keyword 'event' in qapi-schema. A new file was created to store some helper functions in patch 2, patch 4 is the test case, patch 5 is a convert example.

The implemention is done by generate API and a batch of parameters for each event define, it doesn't generate a struture and visit function in the background for every event, so it doesn't support nested structure in the define to avoid trouble. A callback layer is added to control the behavior. More detail can be found in patch 3's message and incode comments.

v2:
Address Luiz's comments:
patch 3: rename *err to *local_err, do not initialize *qmp = NULL, create
a new function qmp_build_evet_dict().
Other change:
reorgnized script in patch 3, it have a clear three steps, see patch 3's
incode comments.

v3:
Address Luiz's comments:
2/5: use -1 when fail to get host timestamp.
3/5: typo fix and better incode comments. all unchanged functions are moved
into qmp-event.c, from generated file. To do so without include issue, 'int' was used intead of 'enum' in function prototype declaration, since the 'enum' define is a generated type.
Address Eric's comments:
2/5: use -1 when fail to get host timestamp.
3/5: typo fix, add docs/qapi-code-gen.txt.
4/5: typo fix, verify no other fields in member 'timestamp'. 5/5: add doc in qapi-schema.json.
fix license, all script using GPL2, all C code use LGPL, just as other
similar files in upstream.

Wenchao Xia (5):
1 os-posix: include sys/time.h
2 qapi: add event helper functions
3 qapi script: add event support
4 test: add test cases for qapi event
5 qapi event: convert RTC_CHANGE

 Makefile                                |    9 +-
 Makefile.objs                           |    2 +-
 docs/qapi-code-gen.txt                  |   18 ++
 include/qapi/qmp-event.h                |   25 ++
 include/sysemu/os-posix.h               |    2 +
 monitor.c                               |   15 ++
 qapi-schema.json                        |   13 +
 qapi/Makefile.objs                      |    1 +
 qapi/qmp-event.c                        |   71 ++++++
 scripts/qapi-event.py                   |  373 +++++++++++++++++++++++++++++++
 tests/Makefile                          |   14 +-
 tests/qapi-schema/qapi-schema-test.json |   12 +
 tests/qapi-schema/qapi-schema-test.out  |   10 +-
 tests/test-qmp-event.c                  |  258 +++++++++++++++++++++
 vl.c                                    |    7 +-
 15 files changed, 816 insertions(+), 14 deletions(-)
 create mode 100644 include/qapi/qmp-event.h
 create mode 100644 qapi/qmp-event.c
 create mode 100644 scripts/qapi-event.py
 create mode 100644 tests/test-qmp-event.c
#1 lore raw

RFC PATCH v3 1/5 os-posix: include sys/time.h

Since gettimeofday() is used in this header file as a macro define, include the function's define header file, to avoid compile warning when other file include os-posix.h.

---
 include/sysemu/os-posix.h |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
include/sysemu/os-posix.h +2 -0
index 25d0b2a..f131521 100644
@@ -26,6 +26,8 @@
 #ifndef QEMU_OS_POSIX_H
 #define QEMU_OS_POSIX_H
 
+#include <sys/time.h>
+
 void os_set_line_buffering(void);
 void os_set_proc_name(const char *s);
 void os_setup_signal_handling(void);
Signature · 1 line
1.7.1
#2 lore raw

RFC PATCH v3 2/5 qapi: add event helper functions

In reply to Wenchao Xia

This file hold some functions that do not need to be generated.

---
 include/qapi/qmp-event.h |   25 ++++++++++++++++
 qapi/Makefile.objs       |    1 +
 qapi/qmp-event.c         |   71 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 0 deletions(-)
 create mode 100644 include/qapi/qmp-event.h
 create mode 100644 qapi/qmp-event.c
include/qapi/qmp-event.h new file +25 -0
new file mode 100644
index 0000000..fdf1a7f
@@ -0,0 +1,25 @@
+/*
+ * QMP Event related
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef QMP_EVENT_H
+#define QMP_EVENT_H
+
+#include "qapi/error.h"
+#include "qapi/qmp/qdict.h"
+
+typedef void (*QMPEventFuncEmit)(int event_kind, QDict *dict, Error **errp);
+
+void qmp_event_set_func_emit(QMPEventFuncEmit emit);
+
+QMPEventFuncEmit qmp_event_get_func_emit(void);
+
+QDict *qmp_event_build_dict(const char *event_name);
+#endif
qapi/Makefile.objs +1 -0
index 1f9c973..d14b769 100644
@@ -3,3 +3,4 @@ util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
 
 util-obj-y += opts-visitor.o
+util-obj-y += qmp-event.o
qapi/qmp-event.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
1.7.1
#3 lore raw

Re: [Qemu-devel] [RFC PATCH V3 2/5] qapi: add event helper functions

Quoted 1 line · Wenchao Xia

This file hold some functions that do not need to be generated.

s/hold/holds/

Quoted 16 lines
---
 include/qapi/qmp-event.h |   25 ++++++++++++++++
 qapi/Makefile.objs       |    1 +
 qapi/qmp-event.c         |   71 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 0 deletions(-)
 create mode 100644 include/qapi/qmp-event.h
 create mode 100644 qapi/qmp-event.c
include/qapi/qmp-event.h new file +0 -0
new file mode 100644
index 0000000..fdf1a7f

include/qapi/qmp-event.h line 8

Quoted 8 lines
include/qapi/qmp-event.h new file +8 -0
@@ -0,0 +1,25 @@
+/*
+ * QMP Event related
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

For the [L]GPL to work, someone must assert copyright.

include/qapi/qmp-event.h line 8

Quoted 10 lines
include/qapi/qmp-event.h new file +9 -0
+++ b/qapi/qmp-event.c
@@ -0,0 +1,71 @@
+/*
+ * QMP Event related
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

Again, missing an actual use of the word "Copyright".

Quoted 6 lines
include/qapi/qmp-event.h new file +6 -0
+
+typedef struct QMPEventFunctions {
+    QMPEventFuncEmit emit;
+} QMPEventFunctions;
+
+QMPEventFunctions qmp_event_functions;

include/qapi/qmp-event.h line 24

Quoted 10 lines
include/qapi/qmp-event.h new file +10 -0
+
+void qmp_event_set_func_emit(QMPEventFuncEmit emit)
+{
+    qmp_event_functions.emit = emit;
+}
+
+QMPEventFuncEmit qmp_event_get_func_emit(void)
+{
+    return qmp_event_functions.emit;
+}

Is this struct a bit overkill, or do you extend it to include other fields later?

include/qapi/qmp-event.h line 28

Quoted 4 lines
include/qapi/qmp-event.h new file +4 -0
+    err = qemu_gettimeofday(&tv);
+    if (err < 0) {
+        /* Put -1 to indicate failure of getting host time */
+        tv.tv_sec = tv.tv_usec = -1;

Believe it or not, this is NOT portable. Let's consider what happens when tv_sec is int64_t and tv_usec is uint32_t. Assignments happen right to left, so tv_usec gets the unsigned value 0xffffffff, then since all uint32_t values fit in int64_t, integer promotion says that the value is 0-extended (not sign-extended), and tv_sec is NOT assigned -1.
Solution: break this into two separate statements:

tv.tv_sec = -1;
tv.tv_usec = -1;

include/qapi/qmp-event.h line 33

Quoted 5 lines
include/qapi/qmp-event.h new file +5 -0
+    }
+
+    obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
+                                "'microseconds': %" PRId64 " }",
+                                (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);

Indentation is odd, but that's cosmetic.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Re: [Qemu-devel] [RFC PATCH V3 2/5] qapi: add event helper functions

Quoted 13 lines · Eric Blake
Quoted 1 line · Wenchao Xia

This file hold some functions that do not need to be generated.

s/hold/holds/

Quoted 9 lines
---
  include/qapi/qmp-event.h |   25 ++++++++++++++++
  qapi/Makefile.objs       |    1 +
  qapi/qmp-event.c         |   71 ++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 97 insertions(+), 0 deletions(-)

create mode 100644 include/qapi/qmp-event.h
create mode 100644 qapi/qmp-event.c

Quoted 15 lines
Quoted 6 lines
include/qapi/qmp-event.h new file +0 -0
new file mode 100644
index 0000000..fdf1a7f

include/qapi/qmp-event.h line 8

Quoted 8 lines
include/qapi/qmp-event.h new file +8 -0
@@ -0,0 +1,25 @@
+/*
+ * QMP Event related
+ *
+ * Authors:
+ *  Wenchao Xia<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

For the [L]GPL to work, someone must assert copyright.

Will fix.

Quoted 22 lines

include/qapi/qmp-event.h line 8

Quoted 10 lines
include/qapi/qmp-event.h new file +9 -0
+++ b/qapi/qmp-event.c
@@ -0,0 +1,71 @@
+/*
+ * QMP Event related
+ *
+ * Authors:
+ *  Wenchao Xia<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

Again, missing an actual use of the word "Copyright".

Quoted 10 lines
include/qapi/qmp-event.h new file +10 -0
+
+typedef struct QMPEventFunctions {
+    QMPEventFuncEmit emit;
+} QMPEventFunctions;
+
+QMPEventFunctions qmp_event_functions;
+
+void qmp_event_set_func_emit(QMPEventFuncEmit emit)
+{
+    qmp_event_functions.emit = emit;
Quoted 8 lines

include/qapi/qmp-event.h line 24

Quoted 6 lines
include/qapi/qmp-event.h new file +6 -0
+}
+
+QMPEventFuncEmit qmp_event_get_func_emit(void)
+{
+    return qmp_event_functions.emit;
+}

Is this struct a bit overkill, or do you extend it to include other fields later?

No other fields will be added in this series, it allow different emit
function hooked.
Do you mean remove it and put it into generated qapi-event.c?

Quoted 12 lines

include/qapi/qmp-event.h line 28

Quoted 5 lines
include/qapi/qmp-event.h new file +4 -0
+    err = qemu_gettimeofday(&tv);
+    if (err<  0) {
+        /* Put -1 to indicate failure of getting host time */
+        tv.tv_sec = tv.tv_usec = -1;

Believe it or not, this is NOT portable. Let's consider what happens when tv_sec is int64_t and tv_usec is uint32_t. Assignments happen right to left, so tv_usec gets the unsigned value 0xffffffff, then since all uint32_t values fit in int64_t, integer promotion says that the value is 0-extended (not sign-extended), and tv_sec is NOT assigned -1.
Solution: break this into two separate statements:

Quoted 2 lines

tv.tv_sec = -1;
tv.tv_usec = -1;

Good catch, thanks!

Quoted 6 lines

include/qapi/qmp-event.h line 33

Quoted 5 lines
include/qapi/qmp-event.h new file +5 -0
+    }
+
+    obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
+                                "'microseconds': %" PRId64 " }",
+                                (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);

Indentation is odd, but that's cosmetic.

Will fix.

#5 lore raw

Re: [Qemu-devel] [RFC PATCH V3 2/5] qapi: add event helper functions

Quoted 24 lines · Wenchao Xia
Quoted 15 lines · Eric Blake
Quoted 1 line · Wenchao Xia

This file hold some functions that do not need to be generated.

s/hold/holds/

Quoted 10 lines
qapi/qmp-event.c new file +10 -0
+
+typedef struct QMPEventFunctions {
+    QMPEventFuncEmit emit;
+} QMPEventFunctions;
+
+QMPEventFunctions qmp_event_functions;
+
+void qmp_event_set_func_emit(QMPEventFuncEmit emit)
+{
+    qmp_event_functions.emit = emit;
Quoted 8 lines

qapi/qmp-event.c line 41

Quoted 6 lines
qapi/qmp-event.c new file +6 -0
+}
+
+QMPEventFuncEmit qmp_event_get_func_emit(void)
+{
+    return qmp_event_functions.emit;
+}

Is this struct a bit overkill, or do you extend it to include other fields later?

Quoted 3 lines

No other fields will be added in this series, it allow different emit
function hooked.
Do you mean remove it and put it into generated qapi-event.c?

Keeping accessor functions is still a bit nicer than directly making a global variable; but my point is that your accessors can just directly access a static variable instead of wrapping things in a pointless struct:

static QMPEventFuncEmit qmp_emit;
void qmp_event_set_func_emit(QMPEventFuncEmit emit)

{
    qmp_emit = emit;
}
QMPEventFuncEmit qmp_event_get_func_emit(void)
{
    return qmp_emit;
}
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw

RFC PATCH v3 3/5 qapi script: add event support

In reply to Wenchao Xia

qapi-event.py will parse the schema and generate qapi-event.c, then the API in qapi-event.c can be used to handle event in qemu code. All API have prefix "qapi_event".

The script mainly include two parts: generate API for each event define, generate an enum type for all defined events.

Since in some case the real emit behavior may change, for example, qemu-img would not send a event, a callback layer is used to control the behavior. As a result, the stubs at compile time can be saved, the binding of block layer code and monitor code will become looser.

---
 Makefile               |    9 +-
 Makefile.objs          |    2 +-
 docs/qapi-code-gen.txt |   18 +++
 scripts/qapi-event.py  |  373 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 398 insertions(+), 4 deletions(-)
 create mode 100644 scripts/qapi-event.py
Makefile +6 -3
index ec74039..47cb661 100644
@@ -45,8 +45,8 @@ endif
 endif
 
 GENERATED_HEADERS = config-host.h qemu-options.def
-GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h
-GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c
+GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
+GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c
 
 GENERATED_HEADERS += trace/generated-events.h
 GENERATED_SOURCES += trace/generated-events.c
@@ -207,7 +207,7 @@ Makefile: $(version-obj-y) $(version-lobj-y)
 # Build libraries
 
 libqemustub.a: $(stub-obj-y)
-libqemuutil.a: $(util-obj-y) qapi-types.o qapi-visit.o
+libqemuutil.a: $(util-obj-y) qapi-types.o qapi-visit.o qapi-event.o
 
 block-modules = $(foreach o,$(block-obj-m),"$(basename $(subst /,-,$o))",) NULL
 util/module.o-cflags = -D'CONFIG_BLOCK_MODULES=$(block-modules)'
@@ -251,6 +251,9 @@ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
 qapi-visit.c qapi-visit.h :\
 $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o "." -b < $<, "  GEN   $@")
+qapi-event.c qapi-event.h :\
+$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-event.py $(qapi-py)
+	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py $(gen-out-type) -o "." -b < $<, "  GEN   $@")
 qmp-commands.h qmp-marshal.c :\
 $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -m -o "." < $<, "  GEN   $@")
Makefile.objs +1 -1
index a6e0e2a..93697a1 100644
@@ -12,7 +12,7 @@ block-obj-y += main-loop.o iohandler.o qemu-timer.o
 block-obj-$(CONFIG_POSIX) += aio-posix.o
 block-obj-$(CONFIG_WIN32) += aio-win32.o
 block-obj-y += block/
-block-obj-y += qapi-types.o qapi-visit.o
+block-obj-y += qapi-types.o qapi-visit.o qapi-event.o
 block-obj-y += qemu-io-cmds.o
 
 block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
docs/qapi-code-gen.txt +18 -0
index d78921f..d87b8a7 100644
@@ -180,6 +180,24 @@ An example command is:
    'data': { 'arg1': 'str', '*arg2': 'str' },
    'returns': 'str' }
 
+=== Events ===
+
+Events are defined with key workd 'event'.  When 'data' is also specified,
+additional info will be carried on.  Finally there will be C API generated
+in qapi-event.h, and when called by QEMU code, message with timestamp will
+be emit on the wire.  If timestamp is -1, it means failure in host time
+retrieving.
+
+An example event is:
+
+{ 'event': 'EVENT_C',
+  'data': { '*a': 'int', 'b': 'str' } }
+
+Resulting in this JSON object:
+
+{ "event": "EVENT_C",
+  "data": { "b": "test string" },
+  "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
 
 == Code generation ==
 
scripts/qapi-event.py new file +373 -0

373 lines not on this page · raw message

Signature · 1 line
1.7.1
#7 lore raw

Re: [Qemu-devel] [RFC PATCH V3 3/5] qapi script: add event support

Quoted 5 lines · Wenchao Xia

qapi-event.py will parse the schema and generate qapi-event.c, then the API in qapi-event.c can be used to handle event in qemu code. All API have prefix "qapi_event".

The script mainly include two parts: generate API for each event

s/include/includes/

Quoted 3 lines

define, generate an enum type for all defined events.

Since in some case the real emit behavior may change, for example,

s/case/cases/

Quoted 16 lines

qemu-img would not send a event, a callback layer is used to control the behavior. As a result, the stubs at compile time can be saved, the binding of block layer code and monitor code will become looser.

---
 Makefile               |    9 +-
 Makefile.objs          |    2 +-
 docs/qapi-code-gen.txt |   18 +++
 scripts/qapi-event.py  |  373 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 398 insertions(+), 4 deletions(-)
 create mode 100644 scripts/qapi-event.py

+++ b/docs/qapi-code-gen.txt

docs/qapi-code-gen.txt An example command is: · line 185

Quoted 6 lines
docs/qapi-code-gen.txt +3 -0
@@ -180,6 +180,24 @@ An example command is:
    'data': { 'arg1': 'str', '*arg2': 'str' },
    'returns': 'str' }
 
+=== Events ===
+
+Events are defined with key workd 'event'.  When 'data' is also specified,

s/workd/word/

docs/qapi-code-gen.txt An example command is: · line 187

Quoted 2 lines
docs/qapi-code-gen.txt +2 -0
+additional info will be carried on.  Finally there will be C API generated
+in qapi-event.h, and when called by QEMU code, message with timestamp will

s/message/a message/

docs/qapi-code-gen.txt An example command is: · line 188

Quoted 1 line
docs/qapi-code-gen.txt +1 -0
+be emit on the wire.  If timestamp is -1, it means failure in host time

s/emit/emitted/

docs/qapi-code-gen.txt An example command is: · line 189

Quoted 1 line
docs/qapi-code-gen.txt +1 -0
+retrieving.

s/in host time retrieving/to retrieve host time/

scripts/qapi-event.py line 8

Quoted 10 lines
scripts/qapi-event.py new file +9 -0
+++ b/scripts/qapi-event.py
@@ -0,0 +1,373 @@
+#
+# QAPI event generator
+#
+# Authors:
+#  Wenchao Xia <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.
+# See the COPYING file in the top-level directory.

Needs to use "Copyright".

scripts/qapi-event.py line 28

Quoted 9 lines
scripts/qapi-event.py new file +9 -0
+
+    if params:
+        for argname, argentry, optional, structured in parse_args(params):
+            if structured:
+                sys.stderr.write("Nested structure define in event is not "
+                                 "supported now, event '%s', argname '%s'\n" %
+                                 (event_name, argname))
+                sys.exit(1)
+                continue

Isn't this 'continue' dead code?

scripts/qapi-event.py line 174

Quoted 3 lines
scripts/qapi-event.py new file +3 -0
+
+# Following are the functions that generate an enum type for all defined
+# events, similar with qapi-types.py. Here we already have enum name and

s/with/to/

scripts/qapi-event.py line 175

Quoted 1 line
scripts/qapi-event.py new file +1 -0
+# values which is generated before and recorded in event_enum_*. It also

s/is/were/

scripts/qapi-event.py line 176

Quoted 1 line
scripts/qapi-event.py new file +1 -0
+# walk around the issue that "import qapi-types" can't work.

s/walk around/works around/

scripts/qapi-event.py line 296

Quoted 12 lines
scripts/qapi-event.py new file +12 -0
+
+fdef.write(mcgen('''
+/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+
+/*
+ * schema-defined QAPI event functions
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

Also needs "Copyright"

scripts/qapi-event.py line 313

Quoted 5 lines
scripts/qapi-event.py new file +5 -0
+fdecl.write(mcgen('''
+/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+
+/*
+ * schema-defined QAPI event function

s/function/functions/

scripts/qapi-event.py line 296

Quoted 6 lines
scripts/qapi-event.py new file +6 -0
+ *
+ * Authors:
+ *  Wenchao Xia  <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

Needs "Copyright"

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#8 lore raw

Re: [Qemu-devel] [RFC PATCH V3 3/5] qapi script: add event support

Quoted 69 lines · Eric Blake
Quoted 5 lines · Wenchao Xia

qapi-event.py will parse the schema and generate qapi-event.c, then the API in qapi-event.c can be used to handle event in qemu code. All API have prefix "qapi_event".

The script mainly include two parts: generate API for each event

s/include/includes/

Quoted 3 lines

define, generate an enum type for all defined events.

Since in some case the real emit behavior may change, for example,

s/case/cases/

Quoted 16 lines

qemu-img would not send a event, a callback layer is used to control the behavior. As a result, the stubs at compile time can be saved, the binding of block layer code and monitor code will become looser.

---
  Makefile               |    9 +-
  Makefile.objs          |    2 +-
  docs/qapi-code-gen.txt |   18 +++
  scripts/qapi-event.py  |  373 ++++++++++++++++++++++++++++++++++++++++++++++++
  4 files changed, 398 insertions(+), 4 deletions(-)

create mode 100644 scripts/qapi-event.py

+++ b/docs/qapi-code-gen.txt

docs/qapi-code-gen.txt An example command is: · line 185

Quoted 6 lines
docs/qapi-code-gen.txt +3 -0
@@ -180,6 +180,24 @@ An example command is:
     'data': { 'arg1': 'str', '*arg2': 'str' },
     'returns': 'str' }
 
+=== Events ===
+
+Events are defined with key workd 'event'.  When 'data' is also specified,

s/workd/word/

docs/qapi-code-gen.txt An example command is: · line 187

Quoted 2 lines
docs/qapi-code-gen.txt +2 -0
+additional info will be carried on.  Finally there will be C API generated
+in qapi-event.h, and when called by QEMU code, message with timestamp will

s/message/a message/

docs/qapi-code-gen.txt An example command is: · line 188

Quoted 1 line
docs/qapi-code-gen.txt +1 -0
+be emit on the wire.  If timestamp is -1, it means failure in host time

s/emit/emitted/

docs/qapi-code-gen.txt An example command is: · line 189

Quoted 1 line
docs/qapi-code-gen.txt +1 -0
+retrieving.

s/in host time retrieving/to retrieve host time/

scripts/qapi-event.py line 8

Quoted 10 lines
scripts/qapi-event.py new file +9 -0
+++ b/scripts/qapi-event.py
@@ -0,0 +1,373 @@
+#
+# QAPI event generator
+#
+# Authors:
+#  Wenchao Xia<[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.
+# See the COPYING file in the top-level directory.

Needs to use "Copyright".

scripts/qapi-event.py line 28

Quoted 9 lines
scripts/qapi-event.py new file +9 -0
+
+    if params:
+        for argname, argentry, optional, structured in parse_args(params):
+            if structured:
+                sys.stderr.write("Nested structure define in event is not "
+                                 "supported now, event '%s', argname '%s'\n" %
+                                 (event_name, argname))
+                sys.exit(1)
+                continue
Quoted 1 line

Isn't this 'continue' dead code?

Yes, I missed it. I'd like to respin a rfc v4, with better error check
function moved into qapi.py, just like the series which check error for other kind of schema error.

Quoted 42 lines

scripts/qapi-event.py line 174

Quoted 4 lines
scripts/qapi-event.py new file +3 -0
+
+# Following are the functions that generate an enum type for all defined
+# events, similar with qapi-types.py. Here we already have enum name and

s/with/to/

scripts/qapi-event.py line 175

Quoted 1 line
scripts/qapi-event.py new file +1 -0
+# values which is generated before and recorded in event_enum_*. It also

s/is/were/

scripts/qapi-event.py line 176

Quoted 1 line
scripts/qapi-event.py new file +1 -0
+# walk around the issue that "import qapi-types" can't work.

s/walk around/works around/

scripts/qapi-event.py line 296

Quoted 12 lines
scripts/qapi-event.py new file +12 -0
+
+fdef.write(mcgen('''
+/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+
+/*
+ * schema-defined QAPI event functions
+ *
+ * Authors:
+ *  Wenchao Xia<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

Also needs "Copyright"

scripts/qapi-event.py line 313

Quoted 5 lines
scripts/qapi-event.py new file +5 -0
+fdecl.write(mcgen('''
+/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+
+/*
+ * schema-defined QAPI event function

s/function/functions/

scripts/qapi-event.py line 296

Quoted 6 lines
scripts/qapi-event.py new file +6 -0
+ *
+ * Authors:
+ *  Wenchao Xia<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.

Needs "Copyright"

#9 lore raw

RFC PATCH v3 4/5 test: add test cases for qapi event

In reply to Wenchao Xia

These cases will verify whether the expected qdict is built.

---
 tests/Makefile                          |   14 ++-
 tests/qapi-schema/qapi-schema-test.json |   12 ++
 tests/qapi-schema/qapi-schema-test.out  |   10 +-
 tests/test-qmp-event.c                  |  258 +++++++++++++++++++++++++++++++
 4 files changed, 289 insertions(+), 5 deletions(-)
 create mode 100644 tests/test-qmp-event.c
tests/Makefile +11 -3
index 471b4c8..5e26542 100644
@@ -27,6 +27,8 @@ check-unit-y += tests/test-string-input-visitor$(EXESUF)
 gcov-files-test-string-input-visitor-y = qapi/string-input-visitor.c
 check-unit-y += tests/test-string-output-visitor$(EXESUF)
 gcov-files-test-string-output-visitor-y = qapi/string-output-visitor.c
+check-unit-y += tests/test-qmp-event$(EXESUF)
+gcov-files-test-qmp-event-y += qapi/qmp-event.c
 check-unit-y += tests/test-opts-visitor$(EXESUF)
 gcov-files-test-opts-visitor-y = qapi/opts-visitor.c
 check-unit-y += tests/test-coroutine$(EXESUF)
@@ -166,7 +168,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
         flat-union-invalid-branch-key.json flat-union-reverse-define.json \
         flat-union-string-discriminator.json)
 
-GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
+GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h \
+                     tests/test-qapi-event.h tests/test-qmp-commands.h
 
 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/check-qlist.o tests/check-qfloat.o tests/check-qjson.o \
@@ -175,9 +178,10 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \
 	tests/test-qmp-commands.o tests/test-visitor-serialization.o \
 	tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \
-	tests/test-opts-visitor.o
+	tests/test-opts-visitor.o tests/test-qmp-event.o
 
-test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o
+test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o \
+                  tests/test-qapi-event.o
 
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
@@ -219,12 +223,16 @@ $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-typ
 tests/test-qapi-visit.c tests/test-qapi-visit.h :\
 $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-visit.py
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o tests -p "test-" < $<, "  GEN   $@")
+tests/test-qapi-event.c tests/test-qapi-event.h :\
+$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-event.py
+	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py $(gen-out-type) -o tests -p "test-" < $<, "  GEN   $@")
 tests/test-qmp-commands.h tests/test-qmp-marshal.c :\
 $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-commands.py
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -o tests -p "test-" < $<, "  GEN   $@")
 
 tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
+tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-output-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
 tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a
tests/qapi-schema/qapi-schema-test.json +12 -0
index 818c06d..ab4d3d9 100644
@@ -89,3 +89,15 @@
     '*u16' : [ 'uint16' ],
     '*i64x':   'int'     ,
     '*u64x':   'uint64'  } }
+
+# testing event
+{ 'type': 'EventStructOne',
+  'data': { 'struct1': 'UserDefOne', 'string': 'str', '*enum2': 'EnumOne' } }
+
+{ 'event': 'EVENT_A' }
+{ 'event': 'EVENT_B',
+  'data': { } }
+{ 'event': 'EVENT_C',
+  'data': { '*a': 'int', '*b': 'UserDefOne', 'c': 'str' } }
+{ 'event': 'EVENT_D',
+  'data': { 'a' : 'EventStructOne', 'b' : 'str', '*c': 'str', '*enum3': 'EnumOne' } }
tests/qapi-schema/qapi-schema-test.out +8 -2
index 6cd03f3..95e9899 100644
@@ -15,7 +15,12 @@
  OrderedDict([('command', 'user_def_cmd1'), ('data', OrderedDict([('ud1a', 'UserDefOne')]))]),
  OrderedDict([('command', 'user_def_cmd2'), ('data', OrderedDict([('ud1a', 'UserDefOne'), ('*ud1b', 'UserDefOne')])), ('returns', 'UserDefTwo')]),
  OrderedDict([('command', 'user_def_cmd3'), ('data', OrderedDict([('a', 'int'), ('*b', 'int')])), ('returns', 'int')]),
- OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])]
+ OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))]),
+ OrderedDict([('type', 'EventStructOne'), ('data', OrderedDict([('struct1', 'UserDefOne'), ('string', 'str'), ('*enum2', 'EnumOne')]))]),
+ OrderedDict([('event', 'EVENT_A')]),
+ OrderedDict([('event', 'EVENT_B'), ('data', OrderedDict())]),
+ OrderedDict([('event', 'EVENT_C'), ('data', OrderedDict([('*a', 'int'), ('*b', 'UserDefOne'), ('c', 'str')]))]),
+ OrderedDict([('event', 'EVENT_D'), ('data', OrderedDict([('a', 'EventStructOne'), ('b', 'str'), ('*c', 'str'), ('*enum3', 'EnumOne')]))])]
 [{'enum_name': 'EnumOne', 'enum_values': ['value1', 'value2', 'value3']},
  {'enum_name': 'UserDefUnionKind', 'enum_values': None},
  {'enum_name': 'UserDefAnonUnionKind', 'enum_values': None},
@@ -28,4 +33,5 @@
  OrderedDict([('type', 'UserDefA'), ('data', OrderedDict([('boolean', 'bool')]))]),
  OrderedDict([('type', 'UserDefB'), ('data', OrderedDict([('integer', 'int')]))]),
  OrderedDict([('type', 'UserDefUnionBase'), ('data', OrderedDict([('string', 'str'), ('enum1', 'EnumOne')]))]),
- OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))])]
+ OrderedDict([('type', 'UserDefOptions'), ('data', OrderedDict([('*i64', ['int']), ('*u64', ['uint64']), ('*u16', ['uint16']), ('*i64x', 'int'), ('*u64x', 'uint64')]))]),
+ OrderedDict([('type', 'EventStructOne'), ('data', OrderedDict([('struct1', 'UserDefOne'), ('string', 'str'), ('*enum2', 'EnumOne')]))])]
tests/test-qmp-event.c new file +258 -0

258 lines not on this page · raw message

Signature · 1 line
1.7.1
#10 lore raw

Re: [Qemu-devel] [RFC PATCH V3 4/5] test: add test cases for qapi event

Quoted 15 lines · Wenchao Xia

These cases will verify whether the expected qdict is built.

---
 tests/Makefile                          |   14 ++-
 tests/qapi-schema/qapi-schema-test.json |   12 ++
 tests/qapi-schema/qapi-schema-test.out  |   10 +-
 tests/test-qmp-event.c                  |  258 +++++++++++++++++++++++++++++++
 4 files changed, 289 insertions(+), 5 deletions(-)
 create mode 100644 tests/test-qmp-event.c

+++ b/tests/test-qmp-event.c

tests/test-qmp-event.c line 9

Quoted 9 lines
tests/test-qmp-event.c new file +9 -0
@@ -0,0 +1,258 @@
+/*
+ * qapi event unit-tests.
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *

Missing "Copyright"

tests/test-qmp-event.c line 73

Quoted 11 lines
tests/test-qmp-event.c new file +11 -0
+    case QTYPE_QINT:
+        d->result = (qint_get_int(qobject_to_qint(obj1)) ==
+                     qint_get_int(qobject_to_qint(obj2)));
+        return;
+    case QTYPE_QSTRING:
+        if (!g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
+                       qstring_get_str(qobject_to_qstring(obj2)))) {
+            d->result = true;
+        } else {
+            d->result = false;
+        }

Could also be written without 'if':
d->result = g_strcmp0(...) == 0;

tests/test-qmp-event.c line 111

Quoted 4 lines
tests/test-qmp-event.c new file +4 -0
+    obj = qdict_get(t, "seconds");
+    g_assert(obj && qobject_type(obj) == QTYPE_QINT);
+    obj = qdict_get(t, "microseconds");
+    g_assert(obj && qobject_type(obj) == QTYPE_QINT);

Might be worth asserting that microseconds is within the range [0,999999] (or -1 if seconds is -1)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#11 lore raw

Re: [Qemu-devel] [RFC PATCH V3 4/5] test: add test cases for qapi event

Quoted 38 lines · Eric Blake
Quoted 14 lines · Wenchao Xia

These cases will verify whether the expected qdict is built.

---
  tests/Makefile                          |   14 ++-
  tests/qapi-schema/qapi-schema-test.json |   12 ++
  tests/qapi-schema/qapi-schema-test.out  |   10 +-
  tests/test-qmp-event.c                  |  258 +++++++++++++++++++++++++++++++
  4 files changed, 289 insertions(+), 5 deletions(-)

create mode 100644 tests/test-qmp-event.c

+++ b/tests/test-qmp-event.c

tests/test-qmp-event.c line 9

Quoted 9 lines
tests/test-qmp-event.c new file +9 -0
@@ -0,0 +1,258 @@
+/*
+ * qapi event unit-tests.
+ *
+ * Authors:
+ *  Wenchao Xia<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *

Missing "Copyright"

tests/test-qmp-event.c line 73

Quoted 11 lines
tests/test-qmp-event.c new file +11 -0
+    case QTYPE_QINT:
+        d->result = (qint_get_int(qobject_to_qint(obj1)) ==
+                     qint_get_int(qobject_to_qint(obj2)));
+        return;
+    case QTYPE_QSTRING:
+        if (!g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
+                       qstring_get_str(qobject_to_qstring(obj2)))) {
+            d->result = true;
+        } else {
+            d->result = false;
+        }

Could also be written without 'if':

Quoted 9 lines
   d->result = g_strcmp0(...) == 0;

tests/test-qmp-event.c

Quoted 4 lines
tests/test-qmp-event.c new file +4 -0
+    obj = qdict_get(t, "seconds");
+    g_assert(obj&&  qobject_type(obj) == QTYPE_QINT);
+    obj = qdict_get(t, "microseconds");
+    g_assert(obj&&  qobject_type(obj) == QTYPE_QINT);

Might be worth asserting that microseconds is within the range [0,999999] (or -1 if seconds is -1)

will add those test.

#12 lore raw

RFC PATCH v3 5/5 qapi event: convert RTC_CHANGE

In reply to Wenchao Xia

This is just an example of how to use qapi event API, and it bypassed the event throttle queue. A complete convert should be first define all events in qapi-schema.json, use qapi event types in monitor functions, then change caller one by one.

---
 monitor.c        |   15 +++++++++++++++
 qapi-schema.json |   13 +++++++++++++
 vl.c             |    7 ++-----
 3 files changed, 30 insertions(+), 5 deletions(-)
monitor.c +15 -0
index 342e83b..163f654 100644
@@ -57,6 +57,7 @@
 #include "qapi/qmp/qjson.h"
 #include "qapi/qmp/json-streamer.h"
 #include "qapi/qmp/json-parser.h"
+#include "qapi/qmp-event.h"
 #include <qom/object_interfaces.h>
 #include "qemu/osdep.h"
 #include "cpu.h"
@@ -76,6 +77,8 @@
 #endif
 #include "hw/lm32/lm32_pic.h"
 
+#include "qapi-event.h"
+
 //#define DEBUG
 //#define DEBUG_COMPLETION
 
@@ -632,6 +635,16 @@ monitor_protocol_event_throttle(MonitorEvent event,
     evstate->data = NULL;
 }
 
+static void monitor_event_emit(int event_kind, QDict *d, Error **errp)
+{
+    Monitor *mon;
+
+    QLIST_FOREACH(mon, &mon_list, entry) {
+        if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
+            monitor_json_emitter(mon, QOBJECT(d));
+        }
+    }
+}
 
 /* Global, one-time initializer to configure the rate limiting
  * and initialize state */
@@ -644,6 +657,8 @@ static void monitor_protocol_event_init(void)
     /* limit the rate of quorum events to avoid hammering the management */
     monitor_protocol_event_throttle(QEVENT_QUORUM_REPORT_BAD, 1000);
     monitor_protocol_event_throttle(QEVENT_QUORUM_FAILURE, 1000);
+
+    qmp_event_set_func_emit(monitor_event_emit);
 }
 
 /**
qapi-schema.json +13 -0
index b68cd44..62918c5 100644
@@ -4686,3 +4686,16 @@
               'btn'     : 'InputBtnEvent',
               'rel'     : 'InputMoveEvent',
               'abs'     : 'InputMoveEvent' } }
+
+##
+# @RTC_CHANGE
+#
+# Emitted when the guest changes the RTC time.
+#
+# @offset: Offset between base RTC clock (as specified by -rtc base), and
+#          new RTC clock value
+#
+# Since: 2.1
+##
+{ 'event': 'RTC_CHANGE',
+  'data': { 'offset' : 'int' } }
vl.c +2 -5
index f0fe48b..08a0e56 100644
@@ -117,6 +117,7 @@ int main(int argc, char **argv)
 #include "ui/qemu-spice.h"
 #include "qapi/string-input-visitor.h"
 #include "qom/object_interfaces.h"
+#include "qapi-event.h"
 
 #define DEFAULT_RAM_SIZE 128
 
@@ -723,11 +724,7 @@ int qemu_timedate_diff(struct tm *tm)
 
 void rtc_change_mon_event(struct tm *tm)
 {
-    QObject *data;
-
-    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
-    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
-    qobject_decref(data);
+    qapi_event_send_rtc_change(qemu_timedate_diff(tm), NULL);
 }
 
 static void configure_rtc_date_offset(const char *startdate, int legacy)
Signature · 1 line
1.7.1
#13 lore raw

Re: [Qemu-devel] [RFC PATCH V3 5/5] qapi event: convert RTC_CHANGE

Quoted 5 lines · Wenchao Xia

This is just an example of how to use qapi event API, and it bypassed the event throttle queue. A complete convert should be first define all events in qapi-schema.json, use qapi event types in monitor functions, then change caller one by one.

This commit message will need to be adjusted when you change this to be a non-RFC series.

Quoted 8 lines
---
 monitor.c        |   15 +++++++++++++++
 qapi-schema.json |   13 +++++++++++++
 vl.c             |    7 ++-----
 3 files changed, 30 insertions(+), 5 deletions(-)

I'm liking where this is headed, and looking forward to the full conversion in 2.1

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org