From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPaiI-0007XO-I5 for Qemu-devel@nongnu.org; Fri, 27 Sep 2013 12:16:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VPaiA-0003Cp-36 for Qemu-devel@nongnu.org; Fri, 27 Sep 2013 12:16:18 -0400 Received: from mail-ob0-x22d.google.com ([2607:f8b0:4003:c01::22d]:49683) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPai9-0003CU-Sg for Qemu-devel@nongnu.org; Fri, 27 Sep 2013 12:16:10 -0400 Received: by mail-ob0-f173.google.com with SMTP id vb8so3363422obc.32 for ; Fri, 27 Sep 2013 09:16:09 -0700 (PDT) Sender: fluxion Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Roth In-Reply-To: <1380160599-15659-1-git-send-email-wudxw@linux.vnet.ibm.com> References: <1380160599-15659-1-git-send-email-wudxw@linux.vnet.ibm.com> Message-ID: <20130927161606.20300.5935@loki> Date: Fri, 27 Sep 2013 11:16:06 -0500 Subject: Re: [Qemu-devel] [PATCH] Add interface to traverse the qmp command list by QmpCommand List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Mark Wu , Qemu-devel@nongnu.org Cc: Luiz Capitulino Quoting Mark Wu (2013-09-25 20:56:39) > 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) to O(n^2) > = > Signed-off-by: Mark Wu Thanks, the qmp_for_each_command stuff is a nice cleanup. Looks good other than previous comments. > --- > include/qapi/qmp/dispatch.h | 3 +- > qapi/qmp-registry.c | 28 ++----------------- > qga/commands.c | 39 ++++++++++---------------- > qga/main.c | 68 +++++++++++++++++----------------------= ------ > 4 files changed, 45 insertions(+), 93 deletions(-) > = > diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h > index 1ce11f5..fb174f6 100644 > --- a/include/qapi/qmp/dispatch.h > +++ b/include/qapi/qmp/dispatch.h > @@ -48,8 +48,9 @@ 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); > 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 > = > diff --git a/qapi/qmp-registry.c b/qapi/qmp-registry.c > index 28bbbe8..844d597 100644 > --- a/qapi/qmp-registry.c > +++ b/qapi/qmp-registry.c > @@ -66,35 +66,11 @@ void qmp_enable_command(const char *name) > qmp_toggle_command(name, true); > } > = > -bool qmp_command_is_enabled(const char *name) > +void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque) > { > QmpCommand *cmd; > = > QTAILQ_FOREACH(cmd, &qmp_commands, node) { > - if (strcmp(cmd->name, name) =3D=3D 0) { > - return cmd->enabled; > - } > + fn(cmd, opaque); > } > - > - return false; > -} > - > -char **qmp_get_command_list(void) > -{ > - QmpCommand *cmd; > - int count =3D 1; > - char **list_head, **list; > - > - QTAILQ_FOREACH(cmd, &qmp_commands, node) { > - count++; > - } > - > - list_head =3D list =3D g_malloc0(count * sizeof(char *)); > - > - QTAILQ_FOREACH(cmd, &qmp_commands, node) { > - *list =3D g_strdup(cmd->name); > - list++; > - } > - > - return list_head; > } > diff --git a/qga/commands.c b/qga/commands.c > index 528b082..602cd47 100644 > --- a/qga/commands.c > +++ b/qga/commands.c > @@ -45,35 +45,26 @@ 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 =3D g_malloc0(sizeof(GuestAgentInfo)); > + GuestAgentInfo *info =3D (GuestAgentInfo *)opaque; > GuestAgentCommandInfo *cmd_info; > GuestAgentCommandInfoList *cmd_info_list; > - char **cmd_list_head, **cmd_list; > - > - info->version =3D g_strdup(QEMU_VERSION); > - > - cmd_list_head =3D cmd_list =3D qmp_get_command_list(); > - if (*cmd_list_head =3D=3D NULL) { > - goto out; > - } > - > - while (*cmd_list) { > - cmd_info =3D g_malloc0(sizeof(GuestAgentCommandInfo)); > - cmd_info->name =3D g_strdup(*cmd_list); > - cmd_info->enabled =3D qmp_command_is_enabled(cmd_info->name); > = > - cmd_info_list =3D g_malloc0(sizeof(GuestAgentCommandInfoList)); > - cmd_info_list->value =3D cmd_info; > - cmd_info_list->next =3D info->supported_commands; > - info->supported_commands =3D cmd_info_list; > + cmd_info =3D g_malloc0(sizeof(GuestAgentCommandInfo)); > + cmd_info->name =3D g_strdup(cmd->name); > + cmd_info->enabled =3D cmd->enabled; > + cmd_info_list =3D g_malloc0(sizeof(GuestAgentCommandInfoList)); > + cmd_info_list->value =3D cmd_info; > + cmd_info_list->next =3D info->supported_commands; > + info->supported_commands =3D cmd_info_list; > +} > = > - g_free(*cmd_list); > - cmd_list++; > - } > +struct GuestAgentInfo *qmp_guest_info(Error **err) > +{ > + GuestAgentInfo *info =3D g_malloc0(sizeof(GuestAgentInfo)); > = > -out: > - g_free(cmd_list_head); > + info->version =3D g_strdup(QEMU_VERSION); > + qmp_for_each_command(qmp_command_info, info); > return info; > } > diff --git a/qga/main.c b/qga/main.c > index 6c746c8..1741d3f 100644 > --- a/qga/main.c > +++ b/qga/main.c > @@ -347,48 +347,34 @@ static gint ga_strcmp(gconstpointer str1, gconstpoi= nter str2) > } > = > /* disable commands that aren't safe for fsfreeze */ > -static void ga_disable_non_whitelisted(void) > +static void ga_disable_non_whitelisted(QmpCommand *cmd, void *opaque) > { > - char **list_head, **list; > bool whitelisted; > int i; > = > - list_head =3D list =3D qmp_get_command_list(); > - while (*list !=3D NULL) { > - whitelisted =3D false; > - i =3D 0; > - while (ga_freeze_whitelist[i] !=3D NULL) { > - if (strcmp(*list, ga_freeze_whitelist[i]) =3D=3D 0) { > - whitelisted =3D true; > - } > - i++; > - } > - if (!whitelisted) { > - g_debug("disabling command: %s", *list); > - qmp_disable_command(*list); > + whitelisted =3D false; > + i =3D 0; > + while (ga_freeze_whitelist[i] !=3D NULL) { > + if (strcmp(cmd->name, ga_freeze_whitelist[i]) =3D=3D 0) { > + whitelisted =3D true; > } > - g_free(*list); > - list++; > + i++; > + } > + if (!whitelisted) { > + g_debug("disabling command: %s", cmd->name); > + qmp_disable_command(cmd->name); > } > - g_free(list_head); > } > = > /* [re-]enable all commands, except those explicitly blacklisted by user= */ > -static void ga_enable_non_blacklisted(GList *blacklist) > +static void ga_enable_non_blacklisted(QmpCommand *cmd, void *opaque) > { > - char **list_head, **list; > - > - list_head =3D list =3D qmp_get_command_list(); > - while (*list !=3D NULL) { > - if (g_list_find_custom(blacklist, *list, ga_strcmp) =3D=3D NULL = && > - !qmp_command_is_enabled(*list)) { > - g_debug("enabling command: %s", *list); > - qmp_enable_command(*list); > - } > - g_free(*list); > - list++; > + GList *blacklist =3D (GList *)opaque; > + if (g_list_find_custom(blacklist, cmd->name, ga_strcmp) =3D=3D NULL = && > + !cmd->enabled) { > + g_debug("enabling command: %s", cmd->name); > + qmp_enable_command(cmd->name); > } > - g_free(list_head); > } > = > static bool ga_create_file(const char *path) > @@ -424,7 +410,7 @@ void ga_set_frozen(GAState *s) > return; > } > /* disable all non-whitelisted (for frozen state) commands */ > - ga_disable_non_whitelisted(); > + qmp_for_each_command(ga_disable_non_whitelisted, NULL); > g_warning("disabling logging due to filesystem freeze"); > ga_disable_logging(s); > s->frozen =3D true; > @@ -460,7 +446,7 @@ void ga_unset_frozen(GAState *s) > } > = > /* enable all disabled, non-blacklisted commands */ > - ga_enable_non_blacklisted(s->blacklist); > + qmp_for_each_command(ga_enable_non_blacklisted, s->blacklist); > s->frozen =3D false; > if (!ga_delete_file(s->state_filepath_isfrozen)) { > g_warning("unable to delete %s, fsfreeze may not function proper= ly", > @@ -920,6 +906,11 @@ int64_t ga_get_fd_handle(GAState *s, Error **errp) > return handle; > } > = > +static void ga_print_cmd(QmpCommand *cmd, void *opaque) > +{ > + printf("%s\n", cmd->name); > +} > + > int main(int argc, char **argv) > { > const char *sopt =3D "hVvdm:p:l:f:F::b:s:t:"; > @@ -996,15 +987,8 @@ int main(int argc, char **argv) > daemonize =3D 1; > break; > case 'b': { > - char **list_head, **list; > if (is_help_option(optarg)) { > - list_head =3D list =3D qmp_get_command_list(); > - while (*list !=3D NULL) { > - printf("%s\n", *list); > - g_free(*list); > - list++; > - } > - g_free(list_head); > + qmp_for_each_command(ga_print_cmd, NULL); > return 0; > } > for (j =3D 0, i =3D 0, len =3D strlen(optarg); i < len; i++)= { > @@ -1126,7 +1110,7 @@ int main(int argc, char **argv) > s->deferred_options.log_filepath =3D log_filepath; > } > ga_disable_logging(s); > - ga_disable_non_whitelisted(); > + qmp_for_each_command(ga_disable_non_whitelisted, NULL); > } else { > if (daemonize) { > become_daemon(pid_filepath); > -- = > 1.8.3.1