qemu-error: advanced report_once handling
cover qemu-error: advanced report_once handling
Based on previous discussions, I wanted to enhance the recently merged report_once infrastucture with a way to print a message once based on a variable instead of globally-once, similar to what vfio-ccw uses today.
Not really tested, mainly wanted to get this out before my vacation to get the ball rolling.
Cornelia Huck (3):
qemu-error: add {error,warn}_report_once_cond
qemu-error: make use of {error,warn}_report_once_cond
vfio-ccw: switch to warn_report_once_cond()
hw/vfio/ccw.c | 18 +++--------------- include/qemu/error-report.h | 15 +++++++-------- util/qemu-error.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 23 deletions(-)
Signature · 1 line
2.14.4
PATCH 1/3 qemu-error: add {error, warn}_report_once_cond
Add two functions to print an error/warning report once depending on a passed-in condition variable and flip it if printed. This is useful if you want to print a message not once-globally, but e.g. once-per-device.
Inspired by warn_once() in hw/vfio/ccw.c.
--- include/qemu/error-report.h | 5 +++++ util/qemu-error.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)
include/qemu/error-report.h +5 -0
@@ -44,6 +44,11 @@ void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); void warn_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); void info_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +void error_report_once_cond(bool *printed, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +void warn_report_once_cond(bool *printed, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +/* * Similar to error_report(), except it prints the message just once. * Return true when it prints, false otherwise.
util/qemu-error.c +44 -0
47 lines not on this page · raw message
Signature · 1 line
2.14.4
PATCH 3/3 vfio-ccw: switch to warn_report_once_cond()
In reply to Cornelia HuckWe can replace the local function with the new common interface.
--- hw/vfio/ccw.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-)
hw/vfio/ccw.c +3 -15
@@ -37,24 +37,12 @@ typedef struct VFIOCCWDevice {bool warned_orb_pfch; } VFIOCCWDevice; -static inline void warn_once (bool *warned,const char *fmt, ...) -{ -va_list ap; - -if (!warned || *warned) { -return ; - } - *warned = true; -va_start (ap, fmt); -warn_vreport (fmt, ap); -va_end (ap); -} -static inline void warn_once_pfch (VFIOCCWDevice *vcdev,SubchDev *sch,const char *msg) { -warn_once (&vcdev->warned_orb_pfch,"vfio-ccw (devno %x.%x.%04x): %s" , - sch->cssid, sch->ssid, sch->devno, msg); +warn_report_once_cond (&vcdev->warned_orb_pfch, +"vfio-ccw (devno %x.%x.%04x): %s" , + sch->cssid, sch->ssid, sch->devno, msg); }static void vfio_ccw_compute_needs_reset (VFIODevice *vdev)
Signature · 1 line
2.14.4
PATCH 2/3 qemu-error: make use of {error, warn}_report_once_cond
In reply to Cornelia Huck{error,warn}_report_once() are a special case of the new functions and can simply switch to them.
--- include/qemu/error-report.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
include/qemu/error-report.h +2 -8
@@ -58,10 +58,7 @@ void warn_report_once_cond(bool *printed, const char *fmt, ...)static bool print_once_; \bool ret_print_once_ = !print_once_; \ \ -if (!print_once_) { \ - print_once_ = true; \ - error_report(fmt, ##__VA_ARGS__); \ - } \ + error_report_once_cond(&print_once_, fmt, ##__VA_ARGS__); \unlikely (ret_print_once_); \ })
@@ -74,10 +71,7 @@ void warn_report_once_cond(bool *printed, const char *fmt, ...)static bool print_once_; \bool ret_print_once_ = !print_once_; \ \ -if (!print_once_) { \ - print_once_ = true; \ - warn_report(fmt, ##__VA_ARGS__); \ - } \ + warn_report_once_cond(&print_once_, fmt, ##__VA_ARGS__); \unlikely (ret_print_once_); \ })
Signature · 1 line
2.14.4
Re: [Qemu-devel] [PATCH 1/3] qemu-error: add {error, warn}_report_once_cond
In reply to Cornelia HuckQuoted 6 lines · Cornelia Huck
Add two functions to print an error/warning report once depending on a passed-in condition variable and flip it if printed. This is useful if you want to print a message not once-globally, but e.g. once-per-device.
Inspired by
warn_once()inhw/vfio/ccw.c.
This leaves the reader wondering why you don't replace that function. You do, in PATCH 3. I'd either announce that here, or squash the two patches.
Quoted 28 lines
Signed-off-by Cornelia Huck <[email protected]>--- include/qemu/error-report.h | 5 +++++ util/qemu-error.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)include/qemu/error-report.h +5
-0@@ -44,6 +44,11 @@ void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); void warn_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); void info_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +void error_report_once_cond(bool *printed, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +void warn_report_once_cond(bool *printed, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +/* * Similar to error_report(), except it prints the message just once. * Return true when it prints, false otherwise. util/qemu-error.c +0
-0
util/qemu-error.c void info_report(const char *fmt, ...) · line 317
Quoted 8 lines
util/qemu-error.c +5
-0@@ -310,3 +310,47 @@ void info_report(const char *fmt, ...)vreport (REPORT_TYPE_INFO , fmt, ap);va_end (ap); } + +/* +* If *printed is false, print an error message to current monitor if we +* have one, else to stderr, and flip *printed to true. +* If printed is NULL, do not print anything.
Any particular reason for supporting null @printed?
For what it's worth, warn_once() supports it, but its caller doesn't use it.
Quoted 39 lines
util/qemu-error.c +39
-0+* Format arguments like sprintf(). The resulting message should be +* a single phrase, with no newline or trailing punctuation. +* Prepend the current location and append a newline. +* It's wrong to call this in a QMP monitor. Use error_setg() there. +*/ +void error_report_once_cond (bool *printed,const char *fmt, ...) +{ +va_list ap; + +if (!printed || *printed) { +return ; + } + *printed = true; +va_start (ap, fmt); +vreport (REPORT_TYPE_ERROR , fmt, ap); +va_end (ap); +} + +/* +* If *printed is false, print a warning message to current monitor if we +* have one, else to stderr, and flip *printed to true. +* If printed is NULL, do not print anything. +* Format arguments like sprintf(). The resulting message should be +* a single phrase, with no newline or trailing punctuation. +* Prepend the current location and append a newline. +* It's wrong to call this in a QMP monitor. Use error_setg() there. +*/ +void warn_report_once_cond (bool *printed,const char *fmt, ...) +{ +va_list ap; + +if (!printed || *printed) { +return ; + } + *printed = true; +va_start (ap, fmt); +vreport (REPORT_TYPE_WARNING , fmt, ap); +va_end (ap); +}
Re: [Qemu-devel] [PATCH 2/3] qemu-error: make use of {error, warn}_report_once_cond
In reply to Cornelia HuckQuoted 25 lines · Cornelia Huck
{error,warn}_report_once() are a special case of the new functions and can simply switch to them.
Signed-off-by Cornelia Huck <[email protected]>--- include/qemu/error-report.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)include/qemu/error-report.h +1
-4@@ -58,10 +58,7 @@ void warn_report_once_cond(bool *printed, const char *fmt, ...)static bool print_once_; \bool ret_print_once_ = !print_once_; \ \ -if (!print_once_) { \ - print_once_ = true; \ - error_report(fmt, ##__VA_ARGS__); \ - } \ + error_report_once_cond(&print_once_, fmt, ##__VA_ARGS__); \unlikely (ret_print_once_); \ })
include/qemu/error-report.h void warn_report_once_cond(bool *printed, const char *fmt, ...) · line 76
Quoted 10 lines
include/qemu/error-report.h +1
-4@@ -74,10 +71,7 @@ void warn_report_once_cond(bool *printed, const char *fmt, ...)static bool print_once_; \bool ret_print_once_ = !print_once_; \ \ -if (!print_once_) { \ - print_once_ = true; \ - warn_report(fmt, ##__VA_ARGS__); \ - } \ + warn_report_once_cond(&print_once_, fmt, ##__VA_ARGS__); \unlikely (ret_print_once_); \ })
Hmm. The macros return a value, while the functions do not. Doesn't this contradict the commit message's claim the macros are ä special case of the new functions"?
If you make the functions return the value, too, the macros become simpler. Moving complexity from macros to functions feels like a good deal, even when it's just a little bit of complexity like here.
Re: [Qemu-devel] [PATCH 1/3] qemu-error: add {error, warn}_report_once_cond
In reply to Markus ArmbrusterQuoted 7 lines · On Wed, 29 Aug 2018 18:07:46 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 5 lines · Cornelia Huck
Add two functions to print an error/warning report once depending on a passed-in condition variable and flip it if printed. This is useful if you want to print a message not once-globally, but e.g. once-per-device.
Quoted 5 lines
Quoted 1 line
Inspired by
warn_once()inhw/vfio/ccw.c.This leaves the reader wondering why you don't replace that function. You do, in PATCH 3. I'd either announce that here, or squash the two patches.
Squashing probably makes the most sense.
Quoted 40 lines
Quoted 29 lines
Signed-off-by Cornelia Huck <[email protected]>--- include/qemu/error-report.h | 5 +++++ util/qemu-error.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)include/qemu/error-report.h +5
-0@@ -44,6 +44,11 @@ void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); void warn_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); void info_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +void error_report_once_cond(bool *printed, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +void warn_report_once_cond(bool *printed, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +/* * Similar to error_report(), except it prints the message just once. * Return true when it prints, false otherwise. util/qemu-error.c +0
-0util/qemu-error.c void info_report(const char *fmt, ...) · line 317
Quoted 8 lines
util/qemu-error.c +5
-0@@ -310,3 +310,47 @@ void info_report(const char *fmt, ...)vreport (REPORT_TYPE_INFO , fmt, ap);va_end (ap); } + +/* +* If *printed is false, print an error message to current monitor if we +* have one, else to stderr, and flip *printed to true. +* If printed is NULL, do not print anything. Any particular reason for supporting null @printed?
Quoted 2 lines
For what it's worth,
warn_once()supports it, but its caller doesn't use it.
I can change this to asserting on NULL @printed. If you call this with NULL, you probably have messed up somewhere already.
Quoted 40 lines
Quoted 40 lines
util/qemu-error.c +39
-0+* Format arguments like sprintf(). The resulting message should be +* a single phrase, with no newline or trailing punctuation. +* Prepend the current location and append a newline. +* It's wrong to call this in a QMP monitor. Use error_setg() there. +*/ +void error_report_once_cond (bool *printed,const char *fmt, ...) +{ +va_list ap; + +if (!printed || *printed) { +return ; + } + *printed = true; +va_start (ap, fmt); +vreport (REPORT_TYPE_ERROR , fmt, ap); +va_end (ap); +} + +/* +* If *printed is false, print a warning message to current monitor if we +* have one, else to stderr, and flip *printed to true. +* If printed is NULL, do not print anything. +* Format arguments like sprintf(). The resulting message should be +* a single phrase, with no newline or trailing punctuation. +* Prepend the current location and append a newline. +* It's wrong to call this in a QMP monitor. Use error_setg() there. +*/ +void warn_report_once_cond (bool *printed,const char *fmt, ...) +{ +va_list ap; + +if (!printed || *printed) { +return ; + } + *printed = true; +va_start (ap, fmt); +vreport (REPORT_TYPE_WARNING , fmt, ap); +va_end (ap); +}
Re: [Qemu-devel] [PATCH 2/3] qemu-error: make use of {error, warn}_report_once_cond
In reply to Markus ArmbrusterQuoted 42 lines · On Wed, 29 Aug 2018 18:12:32 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 25 lines · Cornelia Huck
{error,warn}_report_once() are a special case of the new functions and can simply switch to them.
Signed-off-by Cornelia Huck <[email protected]>--- include/qemu/error-report.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)include/qemu/error-report.h +1
-4@@ -58,10 +58,7 @@ void warn_report_once_cond(bool *printed, const char *fmt, ...)static bool print_once_; \bool ret_print_once_ = !print_once_; \ \ -if (!print_once_) { \ - print_once_ = true; \ - error_report(fmt, ##__VA_ARGS__); \ - } \ + error_report_once_cond(&print_once_, fmt, ##__VA_ARGS__); \unlikely (ret_print_once_); \ })include/qemu/error-report.h void warn_report_once_cond(bool *printed, const char *fmt, ...) · line 76
Quoted 10 lines
include/qemu/error-report.h +1
-4@@ -74,10 +71,7 @@ void warn_report_once_cond(bool *printed, const char *fmt, ...)static bool print_once_; \bool ret_print_once_ = !print_once_; \ \ -if (!print_once_) { \ - print_once_ = true; \ - warn_report(fmt, ##__VA_ARGS__); \ - } \ + warn_report_once_cond(&print_once_, fmt, ##__VA_ARGS__); \unlikely (ret_print_once_); \ })Hmm. The macros return a value, while the functions do not. Doesn't this contradict the commit message's claim the macros are ä special case of the new functions"?
Quoted 3 lines
If you make the functions return the value, too, the macros become simpler. Moving complexity from macros to functions feels like a good deal, even when it's just a little bit of complexity like here.
Reducing macro complexity is a good idea, agreed.
Thanks for looking!
Quoted 7 lines · Cornelia Huck
Based on previous discussions, I wanted to enhance the recently merged
report_onceinfrastucture with a way to print a message once based on a variable instead of globally-once, similar to what vfio-ccw uses today.Not really tested, mainly wanted to get this out before my vacation to get the ball rolling.
I had a few comments, but found nothing serious enough to withhold my