From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41466) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VTdTW-00072K-Te for qemu-devel@nongnu.org; Tue, 08 Oct 2013 16:01:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VTdTN-0006AY-VD for qemu-devel@nongnu.org; Tue, 08 Oct 2013 16:01:46 -0400 Received: from e39.co.us.ibm.com ([32.97.110.160]:47483) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VTdTN-00068Z-NW for qemu-devel@nongnu.org; Tue, 08 Oct 2013 16:01:37 -0400 Received: from /spool/local by e39.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 8 Oct 2013 14:01:36 -0600 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 7DA9C1FF0021 for ; Tue, 8 Oct 2013 14:01:24 -0600 (MDT) Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r98K1Vve315384 for ; Tue, 8 Oct 2013 14:01:32 -0600 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r98K4dhA030750 for ; Tue, 8 Oct 2013 14:04:39 -0600 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Roth In-Reply-To: <1381213389-11440-1-git-send-email-wudxw@linux.vnet.ibm.com> References: <1381213389-11440-1-git-send-email-wudxw@linux.vnet.ibm.com> Message-ID: <20131008200130.28259.21647@loki> Date: Tue, 08 Oct 2013 15:01:30 -0500 Subject: Re: [Qemu-devel] [PATCH v2] 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-10-08 01:23:09) > 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). > = > Signed-off-by: Mark Wu > --- > Changes: > v2: > 1. Keep the signature of qmp_command_is_enabled (per Eric and Mic= hael) > 2. Remove the unnecessary pointer castings (per Eric) > = > include/qapi/qmp/dispatch.h | 5 ++-- > qapi/qmp-registry.c | 27 +++--------------- > qga/commands.c | 38 ++++++++++--------------- > qga/main.c | 68 +++++++++++++++++----------------------= ------ > 4 files changed, 48 insertions(+), 90 deletions(-) > = > diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h > index 1ce11f5..b6eb49e 100644 > --- a/include/qapi/qmp/dispatch.h > +++ b/include/qapi/qmp/dispatch.h > @@ -47,9 +47,10 @@ 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); > 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..3fcf10e 100644 > --- a/qapi/qmp-registry.c > +++ b/qapi/qmp-registry.c > @@ -66,35 +66,16 @@ 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) =3D=3D 0) { > - return cmd->enabled; > - } > - } > - > - return false; > + return cmd->enabled; > } > = > -char **qmp_get_command_list(void) > +void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque) > { > 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++; > + fn(cmd, opaque); > } > - > - return list_head; > } > diff --git a/qga/commands.c b/qga/commands.c > index 528b082..063b22b 100644 > --- a/qga/commands.c > +++ b/qga/commands.c > @@ -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 =3D g_malloc0(sizeof(GuestAgentInfo)); > + GuestAgentInfo *info =3D 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 =3D g_malloc0(sizeof(GuestAgentCommandInfo)); > + cmd_info->name =3D g_strdup(cmd->name); > + cmd_info->enabled =3D qmp_command_is_enabled(cmd); > = > - 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_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..ff2ee03 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 opaque; > + if (g_list_find_custom(blacklist, cmd->name, ga_strcmp) =3D=3D NULL = && > + !cmd->enabled) { qmp_command_is_enabled(cmd) I also agree with Eric's comments about using an accessor for cmd->name to we can continue treating QmpCommand as an opaque. Other than that, looks good.