Add interface to traverse the qmp command list by QmpCommand
PATCH v3 1/1 Add interface to traverse the qmp command list by QmpCommand
In the original code, qmp_get_command_list is used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.
This patch adds an interface to traverse the qmp command list by QmpCommand to replace qmp_get_command_list. It can decrease the complexity from O(n^2) to O(n).
---
v3:
Add an accessor for cmd->name to avoid exposing internals of QmpCommand
v2:
1. Keep the signature of qmp_command_is_enabled (per Eric and Michael)
2. Remove the unnecessary pointer castings (per Eric)
include/qapi/qmp/dispatch.h | 6 ++-- qapi/qmp-registry.c | 30 +++++------------- qga/commands.c | 38 +++++++++-------------- qga/main.c | 75 ++++++++++++++++++--------------------------- 4 files changed, 57 insertions(+), 92 deletions(-)
include/qapi/qmp/dispatch.h +4 -2
@@ -47,9 +47,11 @@ QmpCommand *qmp_find_command(const char *name);QObject *qmp_dispatch (QObject *request);void qmp_disable_command (const char *name);void qmp_enable_command (const char *name); -bool qmp_command_is_enabled (const char *name); -char **qmp_get_command_list (void ); +bool qmp_command_is_enabled (const QmpCommand *cmd); +const char *qmp_command_name (const QmpCommand *cmd);QObject *qmp_build_error_object (Error *errp); +typedef void (*qmp_cmd_callback_fn )(QmpCommand *cmd,void *opaque); +void qmp_for_each_command (qmp_cmd_callback_fn fn,void *opaque);#endif
qapi/qmp-registry.c +8 -22
@@ -66,35 +66,21 @@ void qmp_enable_command(const char *name)qmp_toggle_command (name, true); } -bool qmp_command_is_enabled (const char *name) +bool qmp_command_is_enabled (const QmpCommand *cmd) { -QmpCommand *cmd; - - QTAILQ_FOREACH(cmd, &qmp_commands, node) { -if (strcmp (cmd->name, name) ==0 ) { -return cmd->enabled; - } - } +return cmd->enabled; +} -return false; +const char *qmp_command_name (const QmpCommand *cmd) +{ +return cmd->name; } -char **qmp_get_command_list (void ) +void qmp_for_each_command (qmp_cmd_callback_fn fn,void *opaque) {QmpCommand *cmd; -int count =1 ; -char **list_head, **list; - - QTAILQ_FOREACH(cmd, &qmp_commands, node) { - count++; - } - - list_head = list =g_malloc0 (count *sizeof (char *)); QTAILQ_FOREACH(cmd, &qmp_commands, node) { - *list =g_strdup (cmd->name); - list++; +fn (cmd, opaque); } - -return list_head; }
qga/commands.c +15 -23
@@ -45,35 +45,27 @@ void qmp_guest_ping(Error **err)slog ("guest-ping called" ); } -struct GuestAgentInfo *qmp_guest_info (Error **err) +static void qmp_command_info (QmpCommand *cmd,void *opaque) { -GuestAgentInfo *info =g_malloc0 (sizeof (GuestAgentInfo)); +GuestAgentInfo *info = opaque;GuestAgentCommandInfo *cmd_info;GuestAgentCommandInfoList *cmd_info_list; -char **cmd_list_head, **cmd_list; - - info->version =g_strdup (QEMU_VERSION ); - - cmd_list_head = cmd_list =qmp_get_command_list (); -if (*cmd_list_head ==NULL ) { - goto out; - } -while (*cmd_list) { - cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); - cmd_info->name =g_strdup (*cmd_list); - cmd_info->enabled =qmp_command_is_enabled (cmd_info->name); + cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); + cmd_info->name =g_strdup (cmd->name); + cmd_info->enabled =qmp_command_is_enabled (cmd); - cmd_info_list =g_malloc0 (sizeof (GuestAgentCommandInfoList)); - cmd_info_list->value = cmd_info; - cmd_info_list->next = info->supported_commands; - info->supported_commands = cmd_info_list; + cmd_info_list =g_malloc0 (sizeof (GuestAgentCommandInfoList)); + cmd_info_list->value = cmd_info; + cmd_info_list->next = info->supported_commands; + info->supported_commands = cmd_info_list; +} -g_free (*cmd_list); - cmd_list++; - } +struct GuestAgentInfo *qmp_guest_info (Error **err) +{ +GuestAgentInfo *info =g_malloc0 (sizeof (GuestAgentInfo)); -out: -g_free (cmd_list_head); + info->version =g_strdup (QEMU_VERSION ); +qmp_for_each_command (qmp_command_info, info);return info; }
qga/main.c +30 -45
120 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Quoted 5 lines · Mark Wu
In the original code,
qmp_get_command_listis used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.
Quoted 8 lines
This patch adds an interface to traverse the qmp command list by QmpCommand to replace
qmp_get_command_list. It can decrease the complexity from O(n^2) to O(n).Signed-off-by Mark Wu <[email protected]>---
v3:Add an accessor for cmd->name to avoid exposing internals of QmpCommand
As your two patches are related, I'd send them threaded under one cover letter if you have to respin. But maybe we don't need that...
Quoted 11 lines
v2:
1. Keep the signature ofqmp_command_is_enabled(per Eric and Michael)
2. Remove the unnecessary pointer castings (per Eric)include/qapi/qmp/dispatch.h | 6 ++-- qapi/qmp-registry.c | 30 +++++------------- qga/commands.c | 38 +++++++++-------------- qga/main.c | 75 ++++++++++++++++++--------------------------- 4 files changed, 57 insertions(+), 92 deletions(-)
qga/commands.c void qmp_guest_ping(Error **err) · line 55
Quoted 10 lines
qga/commands.c +3
-5-struct GuestAgentInfo *qmp_guest_info (Error **err) +static void qmp_command_info (QmpCommand *cmd,void *opaque) { -while (*cmd_list) { - cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); - cmd_info->name =g_strdup (*cmd_list); - cmd_info->enabled =qmp_command_is_enabled (cmd_info->name); + cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); + cmd_info->name =g_strdup (cmd->name);
Oops, not using qmp_command_name().
Seems minor enough that if it is the only change after this much churn, and the maintainer is willing to fix it on your behalf, I could live with:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Thanks a lot for your all kind and helpful comments. I would fix it by myself to save the time of maintainer :)
Then I have to beg your another review. BTW, I would like to ask if there's good tools or practice to perform
an incremental review in qemu patch works.
Thanks.
Mark.
Quoted 47 lines · Eric Blake
Quoted 5 lines · Mark Wu
In the original code,
qmp_get_command_listis used to construct a list of all commands' name. To get the information of all qga commands, it traverses the name list and search the command info with its name. So it can cause O(n^2) in the number of commands.Quoted 8 lines
This patch adds an interface to traverse the qmp command list by QmpCommand to replace
qmp_get_command_list. It can decrease the complexity from O(n^2) to O(n).Signed-off-by Mark Wu <[email protected]>---
v3:Add an accessor for cmd->name to avoid exposing internals of QmpCommandAs your two patches are related, I'd send them threaded under one cover letter if you have to respin. But maybe we don't need that...
Quoted 11 lines
v2:
1. Keep the signature ofqmp_command_is_enabled(per Eric and Michael)
2. Remove the unnecessary pointer castings (per Eric)include/qapi/qmp/dispatch.h | 6 ++-- qapi/qmp-registry.c | 30 +++++------------- qga/commands.c | 38 +++++++++-------------- qga/main.c | 75 ++++++++++++++++++--------------------------- 4 files changed, 57 insertions(+), 92 deletions(-)qga/commands.c void qmp_guest_ping(Error **err) · line 55
Quoted 10 lines
qga/commands.c +3
-5-struct GuestAgentInfo *qmp_guest_info (Error **err) +static void qmp_command_info (QmpCommand *cmd,void *opaque) { -while (*cmd_list) { - cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); - cmd_info->name =g_strdup (*cmd_list); - cmd_info->enabled =qmp_command_is_enabled (cmd_info->name); + cmd_info =g_malloc0 (sizeof (GuestAgentCommandInfo)); + cmd_info->name =g_strdup (cmd->name);Oops, not using
qmp_command_name().Seems minor enough that if it is the only change after this much churn, and the maintainer is willing to fix it on your behalf, I could live with:
Reviewed-by Eric Blake <[email protected]>