* [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object @ 2018-11-19 13:59 Daniel P. Berrangé 2018-11-20 5:45 ` Marc-André Lureau 2018-11-20 18:16 ` Markus Armbruster 0 siblings, 2 replies; 5+ messages in thread From: Daniel P. Berrangé @ 2018-11-19 13:59 UTC (permalink / raw) To: qemu-devel; +Cc: Andreas Färber, Daniel P. Berrangé When debugging QEMU it is often useful to put a breakpoint on the error_setg_internal method impl. Unfortunately the object_property_add / object_class_property_add methods call object_property_find / object_class_property_find methods to check if a property exists already before adding the new property. As a result there are a huge number of calls to error_setg_internal on startup of most QEMU commands, making it very painful to set a breakpoint on this method. This puts a minor optimization on the code so that we avoid calling error_setg() when errp is NULL. Functionally there's no difference since error_setg() is a no-op when errp is NULL, but this lets us use breakpoints in GDB in a practical way. Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> --- qom/object.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/qom/object.c b/qom/object.c index 547dcf97c3..ddd5e7a30e 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1087,7 +1087,12 @@ ObjectProperty *object_property_find(Object *obj, const char *name, return prop; } - error_setg(errp, "Property '.%s' not found", name); + /* Optimized to avoid calling error_setg if errp == NULL + * otherwise every property add call hits error_setg + * making it impratical to set breakpoints in GDB */ + if (errp) { + error_setg(errp, "Property '.%s' not found", name); + } return NULL; } @@ -1133,7 +1138,10 @@ ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, } prop = g_hash_table_lookup(klass->properties, name); - if (!prop) { + /* Optimized to avoid calling error_setg if errp == NULL + * otherwise every property add call hits error_setg + * making it impratical to set breakpoints in GDB */ + if (!prop && errp) { error_setg(errp, "Property '.%s' not found", name); } return prop; -- 2.19.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object 2018-11-19 13:59 [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object Daniel P. Berrangé @ 2018-11-20 5:45 ` Marc-André Lureau 2018-11-20 9:45 ` Daniel P. Berrangé 2018-11-20 18:16 ` Markus Armbruster 1 sibling, 1 reply; 5+ messages in thread From: Marc-André Lureau @ 2018-11-20 5:45 UTC (permalink / raw) To: Daniel P. Berrange; +Cc: QEMU, Andreas Färber Hi On Mon, Nov 19, 2018 at 5:59 PM Daniel P. Berrangé <berrange@redhat.com> wrote: > > When debugging QEMU it is often useful to put a breakpoint on the > error_setg_internal method impl. > > Unfortunately the object_property_add / object_class_property_add > methods call object_property_find / object_class_property_find methods > to check if a property exists already before adding the new property. > > As a result there are a huge number of calls to error_setg_internal > on startup of most QEMU commands, making it very painful to set a > breakpoint on this method. > > This puts a minor optimization on the code so that we avoid calling > error_setg() when errp is NULL. Functionally there's no difference > since error_setg() is a no-op when errp is NULL, but this lets us > use breakpoints in GDB in a practical way. > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> Or maybe change the error_setg* macros? After all, if errp is NULL, it's an error qemu can handle. > --- > qom/object.c | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/qom/object.c b/qom/object.c > index 547dcf97c3..ddd5e7a30e 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -1087,7 +1087,12 @@ ObjectProperty *object_property_find(Object *obj, const char *name, > return prop; > } > > - error_setg(errp, "Property '.%s' not found", name); > + /* Optimized to avoid calling error_setg if errp == NULL > + * otherwise every property add call hits error_setg > + * making it impratical to set breakpoints in GDB */ > + if (errp) { > + error_setg(errp, "Property '.%s' not found", name); > + } > return NULL; > } > > @@ -1133,7 +1138,10 @@ ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, > } > > prop = g_hash_table_lookup(klass->properties, name); > - if (!prop) { > + /* Optimized to avoid calling error_setg if errp == NULL > + * otherwise every property add call hits error_setg > + * making it impratical to set breakpoints in GDB */ > + if (!prop && errp) { > error_setg(errp, "Property '.%s' not found", name); > } > return prop; > -- > 2.19.1 > > -- Marc-André Lureau ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object 2018-11-20 5:45 ` Marc-André Lureau @ 2018-11-20 9:45 ` Daniel P. Berrangé 0 siblings, 0 replies; 5+ messages in thread From: Daniel P. Berrangé @ 2018-11-20 9:45 UTC (permalink / raw) To: Marc-André Lureau; +Cc: QEMU, Andreas Färber On Tue, Nov 20, 2018 at 09:45:23AM +0400, Marc-André Lureau wrote: > Hi > > On Mon, Nov 19, 2018 at 5:59 PM Daniel P. Berrangé <berrange@redhat.com> wrote: > > > > When debugging QEMU it is often useful to put a breakpoint on the > > error_setg_internal method impl. > > > > Unfortunately the object_property_add / object_class_property_add > > methods call object_property_find / object_class_property_find methods > > to check if a property exists already before adding the new property. > > > > As a result there are a huge number of calls to error_setg_internal > > on startup of most QEMU commands, making it very painful to set a > > breakpoint on this method. > > > > This puts a minor optimization on the code so that we avoid calling > > error_setg() when errp is NULL. Functionally there's no difference > > since error_setg() is a no-op when errp is NULL, but this lets us > > use breakpoints in GDB in a practical way. > > > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> > > Or maybe change the error_setg* macros? After all, if errp is NULL, > it's an error qemu can handle. That would mean that all callers would get short circuited which I don't think it is desirable in general. This QOM usage is special in that the calling code is using the error scenario as a means to trigger different code flow. IOW it doesn't consider it an error scenario at all. > > --- > > qom/object.c | 12 ++++++++++-- > > 1 file changed, 10 insertions(+), 2 deletions(-) > > > > diff --git a/qom/object.c b/qom/object.c > > index 547dcf97c3..ddd5e7a30e 100644 > > --- a/qom/object.c > > +++ b/qom/object.c > > @@ -1087,7 +1087,12 @@ ObjectProperty *object_property_find(Object *obj, const char *name, > > return prop; > > } > > > > - error_setg(errp, "Property '.%s' not found", name); > > + /* Optimized to avoid calling error_setg if errp == NULL > > + * otherwise every property add call hits error_setg > > + * making it impratical to set breakpoints in GDB */ > > + if (errp) { > > + error_setg(errp, "Property '.%s' not found", name); > > + } > > return NULL; > > } > > > > @@ -1133,7 +1138,10 @@ ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, > > } > > > > prop = g_hash_table_lookup(klass->properties, name); > > - if (!prop) { > > + /* Optimized to avoid calling error_setg if errp == NULL > > + * otherwise every property add call hits error_setg > > + * making it impratical to set breakpoints in GDB */ > > + if (!prop && errp) { > > error_setg(errp, "Property '.%s' not found", name); > > } > > return prop; > > -- > > 2.19.1 > > > > > > > -- > Marc-André Lureau Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :| ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object 2018-11-19 13:59 [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object Daniel P. Berrangé 2018-11-20 5:45 ` Marc-André Lureau @ 2018-11-20 18:16 ` Markus Armbruster 2018-11-21 9:31 ` Daniel P. Berrangé 1 sibling, 1 reply; 5+ messages in thread From: Markus Armbruster @ 2018-11-20 18:16 UTC (permalink / raw) To: Daniel P. Berrangé; +Cc: qemu-devel, Andreas Färber Daniel P. Berrangé <berrange@redhat.com> writes: > When debugging QEMU it is often useful to put a breakpoint on the > error_setg_internal method impl. > > Unfortunately the object_property_add / object_class_property_add > methods call object_property_find / object_class_property_find methods > to check if a property exists already before adding the new property. > > As a result there are a huge number of calls to error_setg_internal > on startup of most QEMU commands, making it very painful to set a > breakpoint on this method. > > This puts a minor optimization on the code so that we avoid calling > error_setg() when errp is NULL. Functionally there's no difference > since error_setg() is a no-op when errp is NULL, but this lets us > use breakpoints in GDB in a practical way. > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> > --- > qom/object.c | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/qom/object.c b/qom/object.c > index 547dcf97c3..ddd5e7a30e 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -1087,7 +1087,12 @@ ObjectProperty *object_property_find(Object *obj, const char *name, > return prop; > } > > - error_setg(errp, "Property '.%s' not found", name); > + /* Optimized to avoid calling error_setg if errp == NULL > + * otherwise every property add call hits error_setg > + * making it impratical to set breakpoints in GDB */ > + if (errp) { > + error_setg(errp, "Property '.%s' not found", name); > + } > return NULL; > } > In my opinion, this function's design is awkward. Stress on *opinion*. On success, it returns a (non-null) pointer. On failure, it sets an error and returns null. Note that it has just one failure mode: "Property '.%s' not found". Setting an error is just a convenience for those callers that want to propagate exactly this error to their callers. I count 30 callers. Only six pass a non-NULL argument to @errp. I'd rather have a pair of functions similar to how Python has both .get() and .__getitem__(): the former doesn't fail, but returns None instead, and the latter does fail, throwing KeyError. In QEMU, we can't throw, so we set an error. Here's the obvious code: ObjectProperty *object_property_find(Object *obj, const char *name) { ObjectProperty *prop; ObjectClass *klass = object_get_class(obj); prop = object_class_property_find(klass, name, NULL); if (prop) { return prop; } prop = g_hash_table_lookup(obj->properties, name); if (prop) { return prop; } return NULL; } ObjectProperty *object_property_find_err(Object *obj, const char *name, Error **errp) { ObjectProperty *prop = object_property_find(obj, name); if (!prop) { error_setg(errp, "Property '.%s' not found", name); } return prop; } > @@ -1133,7 +1138,10 @@ ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, > } > > prop = g_hash_table_lookup(klass->properties, name); > - if (!prop) { > + /* Optimized to avoid calling error_setg if errp == NULL > + * otherwise every property add call hits error_setg > + * making it impratical to set breakpoints in GDB */ > + if (!prop && errp) { > error_setg(errp, "Property '.%s' not found", name); > } > return prop; Likewise, just more so: callers passing non-NULL do not exist. ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object 2018-11-20 18:16 ` Markus Armbruster @ 2018-11-21 9:31 ` Daniel P. Berrangé 0 siblings, 0 replies; 5+ messages in thread From: Daniel P. Berrangé @ 2018-11-21 9:31 UTC (permalink / raw) To: Markus Armbruster; +Cc: qemu-devel, Andreas Färber On Tue, Nov 20, 2018 at 07:16:31PM +0100, Markus Armbruster wrote: > Daniel P. Berrangé <berrange@redhat.com> writes: > > > When debugging QEMU it is often useful to put a breakpoint on the > > error_setg_internal method impl. > > > > Unfortunately the object_property_add / object_class_property_add > > methods call object_property_find / object_class_property_find methods > > to check if a property exists already before adding the new property. > > > > As a result there are a huge number of calls to error_setg_internal > > on startup of most QEMU commands, making it very painful to set a > > breakpoint on this method. > > > > This puts a minor optimization on the code so that we avoid calling > > error_setg() when errp is NULL. Functionally there's no difference > > since error_setg() is a no-op when errp is NULL, but this lets us > > use breakpoints in GDB in a practical way. > > > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> > > --- > > qom/object.c | 12 ++++++++++-- > > 1 file changed, 10 insertions(+), 2 deletions(-) > > > > diff --git a/qom/object.c b/qom/object.c > > index 547dcf97c3..ddd5e7a30e 100644 > > --- a/qom/object.c > > +++ b/qom/object.c > > @@ -1087,7 +1087,12 @@ ObjectProperty *object_property_find(Object *obj, const char *name, > > return prop; > > } > > > > - error_setg(errp, "Property '.%s' not found", name); > > + /* Optimized to avoid calling error_setg if errp == NULL > > + * otherwise every property add call hits error_setg > > + * making it impratical to set breakpoints in GDB */ > > + if (errp) { > > + error_setg(errp, "Property '.%s' not found", name); > > + } > > return NULL; > > } > > > > In my opinion, this function's design is awkward. Stress on *opinion*. > > On success, it returns a (non-null) pointer. > > On failure, it sets an error and returns null. Note that it has just > one failure mode: "Property '.%s' not found". Setting an error is just > a convenience for those callers that want to propagate exactly this > error to their callers. > > I count 30 callers. Only six pass a non-NULL argument to @errp. > > I'd rather have a pair of functions similar to how Python has both > .get() and .__getitem__(): the former doesn't fail, but returns None > instead, and the latter does fail, throwing KeyError. In QEMU, we can't > throw, so we set an error. Here's the obvious code: > > ObjectProperty *object_property_find(Object *obj, const char *name) > { > ObjectProperty *prop; > ObjectClass *klass = object_get_class(obj); > > prop = object_class_property_find(klass, name, NULL); > if (prop) { > return prop; > } > > prop = g_hash_table_lookup(obj->properties, name); > if (prop) { > return prop; > } > > return NULL; > } > > ObjectProperty *object_property_find_err(Object *obj, const char *name, > Error **errp) > { > ObjectProperty *prop = object_property_find(obj, name); > > if (!prop) { > error_setg(errp, "Property '.%s' not found", name); > } > return prop; > } Yes, I'd be happy with this approach as I think it is more useful in general Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :| ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-11-21 9:31 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-11-19 13:59 [Qemu-devel] [PATCH] qom: avoid reporting errors for NULL error object Daniel P. Berrangé 2018-11-20 5:45 ` Marc-André Lureau 2018-11-20 9:45 ` Daniel P. Berrangé 2018-11-20 18:16 ` Markus Armbruster 2018-11-21 9:31 ` Daniel P. Berrangé
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.