From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10540 invoked by alias); 23 Jun 2010 08:23:44 -0000 Received: (qmail 10487 invoked by uid 22791); 23 Jun 2010 08:23:30 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=BAYES_00,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from otisco.mckusick.com (HELO syracuse.mckusick.com) (64.81.247.46) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 23 Jun 2010 08:23:21 +0000 Received: from syracuse.mckusick.com (localhost [127.0.0.1]) by syracuse.mckusick.com (8.14.3/8.14.3/Debian-9.1ubuntu1) with ESMTP id o5N8N0oU000583; Wed, 23 Jun 2010 01:23:00 -0700 Received: (from hilfingr@localhost) by syracuse.mckusick.com (8.14.3/8.14.3/Submit) id o5N8N0sc000581; Wed, 23 Jun 2010 01:23:00 -0700 From: Paul Hilfinger To: gdb-patches@sourceware.org Cc: brobecker@adacore.com, Paul Hilfinger Subject: [RFA] Refactor 'maint time' command statistics. Date: Wed, 23 Jun 2010 08:23:00 -0000 Message-Id: <1277281366-548-1-git-send-email-Hilfinger@adacore.com> Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2010-06/txt/msg00494.txt.bz2 From: Paul Hilfinger Refactor 'maint time' code and make it work for errors, too. Consolidate code for displaying per-command time and space statistics to avoid duplication. Piggyback on cleanups so that statistics get printed even when commands terminate as a result of an error. Changelog * gdb/defs.h (make_command_stats_cleanup): Declare. (set_display_time): Declare. (set_display_space): Declare. * gdb/event-top.c (command_handler): Use make_command_stats_cleanup. * gdb/main.c (display_time, display_space): Move definitions to utils.c. (captured_main): Use make_command_stats_cleanup to get start-up statistics. Use set_display_time and set_display_space for processing OPT_STATISTICS case. * gdb/maint.c (maintenance_time_display): Use set_display_time. (maintenance_space_display): Use set_display_space. * gdb/top.c (execute_command): Remove obsolete 'maint time' code. (command_loop): Use make_command_stats_cleanup. * gdb/utils.c (struct cmd_stats): Structure for storing initial time and space usage. (display_time, display_space): Move definitions here from utils.c. (set_display_time): New function. (set_display_space): New function. (make_command_stats_cleanup): New function. (report_command_stats): New auxiliary function for make_command_stats_cleanup. --- gdb/defs.h | 6 +++ gdb/event-top.c | 40 ++--------------------- gdb/main.c | 34 ++----------------- gdb/maint.c | 8 +--- gdb/top.c | 59 +--------------------------------- gdb/utils.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 131 deletions(-) diff --git a/gdb/defs.h b/gdb/defs.h index 1270e85..4bd8abf 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -307,6 +307,10 @@ extern int subset_compare (char *, char *); extern char *safe_strerror (int); +extern void set_display_time (int); + +extern void set_display_space (int); + #define ALL_CLEANUPS ((struct cleanup *)0) extern void do_cleanups (struct cleanup *); @@ -369,6 +373,8 @@ extern void free_current_contents (void *); extern void null_cleanup (void *); +extern struct cleanup *make_command_stats_cleanup (int); + extern int myread (int, char *, int); extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2); diff --git a/gdb/event-top.c b/gdb/event-top.c index cf364ff..6b09bc4 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -476,12 +476,7 @@ static void command_handler (char *command) { int stdin_is_tty = ISATTY (stdin); - long time_at_cmd_start; -#ifdef HAVE_SBRK - long space_at_cmd_start = 0; -#endif - extern int display_time; - extern int display_space; + struct cleanup *stat_chain; quit_flag = 0; if (instream == stdin && stdin_is_tty) @@ -498,43 +493,14 @@ command_handler (char *command) execute_command ("quit", stdin == instream); } - time_at_cmd_start = get_run_time (); - - if (display_space) - { -#ifdef HAVE_SBRK - char *lim = (char *) sbrk (0); - - space_at_cmd_start = lim - lim_at_start; -#endif - } + stat_chain = make_command_stats_cleanup (1); execute_command (command, instream == stdin); /* Do any commands attached to breakpoint we stopped at. */ bpstat_do_actions (); - if (display_time) - { - long cmd_time = get_run_time () - time_at_cmd_start; - - printf_unfiltered (_("Command execution time: %ld.%06ld\n"), - cmd_time / 1000000, cmd_time % 1000000); - } - - if (display_space) - { -#ifdef HAVE_SBRK - char *lim = (char *) sbrk (0); - long space_now = lim - lim_at_start; - long space_diff = space_now - space_at_cmd_start; - - printf_unfiltered (_("Space used: %ld (%c%ld for this command)\n"), - space_now, - (space_diff >= 0 ? '+' : '-'), - space_diff); -#endif - } + do_cleanups (stat_chain); } /* Handle a complete line of input. This is called by the callback diff --git a/gdb/main.c b/gdb/main.c index c5c712a..bfd1213 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -43,14 +43,6 @@ #include "cli/cli-cmds.h" #include "python/python.h" -/* If nonzero, display time usage both at startup and for each command. */ - -int display_time; - -/* If nonzero, display space usage both at startup and for each command. */ - -int display_space; - /* The selected interpreter. This will be used as a set command variable, so it should always be malloc'ed - since do_setshow_command will free it. */ @@ -299,7 +291,7 @@ captured_main (void *data) int i; int save_auto_load; - long time_at_startup = get_run_time (); + struct cleanup *pre_stat_chain = make_command_stats_cleanup (0); #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) setlocale (LC_MESSAGES, ""); @@ -485,8 +477,8 @@ captured_main (void *data) break; case OPT_STATISTICS: /* Enable the display of both time and space usage. */ - display_time = 1; - display_space = 1; + set_display_time (1); + set_display_space (1); break; case OPT_TUI: /* --tui is equivalent to -i=tui. */ @@ -907,25 +899,7 @@ Can't attach to process and specify a core file at the same time.")); } /* Show time and/or space usage. */ - - if (display_time) - { - long init_time = get_run_time () - time_at_startup; - - printf_unfiltered (_("Startup time: %ld.%06ld\n"), - init_time / 1000000, init_time % 1000000); - } - - if (display_space) - { -#ifdef HAVE_SBRK - extern char **environ; - char *lim = (char *) sbrk (0); - - printf_unfiltered (_("Startup size: data size %ld\n"), - (long) (lim - (char *) &environ)); -#endif - } + do_cleanups (pre_stat_chain); /* NOTE: cagney/1999-11-07: There is probably no reason for not moving this loop and the code found in captured_command_loop() diff --git a/gdb/maint.c b/gdb/maint.c index 1673e93..bdcf900 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -171,23 +171,19 @@ maintenance_demangle (char *args, int from_tty) static void maintenance_time_display (char *args, int from_tty) { - extern int display_time; - if (args == NULL || *args == '\0') printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n")); else - display_time = strtol (args, NULL, 10); + set_display_time (strtol (args, NULL, 10)); } static void maintenance_space_display (char *args, int from_tty) { - extern int display_space; - if (args == NULL || *args == '\0') printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n"); else - display_space = strtol (args, NULL, 10); + set_display_space (strtol (args, NULL, 10)); } /* The "maintenance info" command is defined as a prefix, with diff --git a/gdb/top.c b/gdb/top.c index 0832c07..200181c 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -357,25 +357,6 @@ execute_command (char *p, int from_tty) enum language flang; static int warned = 0; char *line; - long time_at_cmd_start = 0; -#ifdef HAVE_SBRK - long space_at_cmd_start = 0; -#endif - extern int display_space; - - if (target_can_async_p ()) - { - time_at_cmd_start = get_run_time (); - - if (display_space) - { -#ifdef HAVE_SBRK - char *lim = (char *) sbrk (0); - - space_at_cmd_start = lim - lim_at_start; -#endif - } - } prepare_execute_command (); @@ -519,12 +500,6 @@ command_loop (void) struct cleanup *old_chain; char *command; int stdin_is_tty = ISATTY (stdin); - long time_at_cmd_start; -#ifdef HAVE_SBRK - long space_at_cmd_start = 0; -#endif - extern int display_time; - extern int display_space; while (instream && !feof (instream)) { @@ -543,16 +518,7 @@ command_loop (void) if (command == 0) return; - time_at_cmd_start = get_run_time (); - - if (display_space) - { -#ifdef HAVE_SBRK - char *lim = (char *) sbrk (0); - - space_at_cmd_start = lim - lim_at_start; -#endif - } + make_command_stats_cleanup (1); execute_command (command, instream == stdin); @@ -560,29 +526,6 @@ command_loop (void) bpstat_do_actions (); do_cleanups (old_chain); - - if (display_time) - { - long cmd_time = get_run_time () - time_at_cmd_start; - - printf_unfiltered (_("Command execution time: %ld.%06ld\n"), - cmd_time / 1000000, cmd_time % 1000000); - } - - if (display_space) - { -#ifdef HAVE_SBRK - char *lim = (char *) sbrk (0); - - long space_now = lim - lim_at_start; - long space_diff = space_now - space_at_cmd_start; - - printf_unfiltered (_("Space used: %ld (%s%ld for this command)\n"), - space_now, - (space_diff >= 0 ? "+" : ""), - space_diff); -#endif - } } } diff --git a/gdb/utils.c b/gdb/utils.c index 19bda4b..43ff8c2 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -513,6 +513,102 @@ null_cleanup (void *arg) { } +/* If nonzero, display time usage both at startup and for each command. */ + +static int display_time; + +/* If nonzero, display space usage both at startup and for each command. */ + +static int display_space; + +/* Records a run time and space usage to be used as a base for + reporting elapsed time or change in space. In addition, + the msg_type field indicates whether the saved time is from the + beginning of GDB execution (0) or the beginning of an individual + command execution (1). */ +struct cmd_stats +{ + int msg_type; + long start_time; + long start_space; +}; + +/* Set whether to display time statistics to NEW_VALUE (non-zero + means true). */ +void +set_display_time (int new_value) +{ + display_time = new_value; +} + +/* Set whether to display space statistics to NEW_VALUE (non-zero + means true). */ +void +set_display_space (int new_value) +{ + display_space = new_value; +} + +/* As indicated by display_time and display_space, report GDB's elapsed time + and space usage from the base time and space provided in ARG, which + must be a pointer to a struct cmd_stat. This function is intended + to be called as a cleanup. */ +static void +report_command_stats (void *arg) +{ + struct cmd_stats *start_stats = (struct cmd_stats *) arg; + int msg_type = start_stats->msg_type; + + if (display_time) + { + long cmd_time = get_run_time () - start_stats->start_time; + + printf_unfiltered (msg_type == 0 ? + _("Startup time: %ld.%06ld\n") : + _("Command execution time: %ld.%06ld\n"), + cmd_time / 1000000, cmd_time % 1000000); + } + + if (display_space) + { +#ifdef HAVE_SBRK + char *lim = (char *) sbrk (0); + + long space_now = lim - lim_at_start; + long space_diff = space_now - start_stats->start_space; + + printf_unfiltered (msg_type == 0 ? + _("Space used: %ld (%c%ld during startup)\n") : + _("Space used: %ld (%c%ld for this command)\n"), + space_now, + (space_diff >= 0 ? '+' : '-'), + space_diff); +#endif + } + + xfree (start_stats); +} + +/* Create a cleanup that reports time and space used since its + creation. Precise messages depend on MSG_TYPE: + 0: Initial time/space + 1: Individual command time/space. */ +struct cleanup * +make_command_stats_cleanup (int msg_type) +{ + struct cmd_stats *new_stat = XMALLOC (struct cmd_stats); + +#ifdef HAVE_SBRK + char *lim = (char *) sbrk (0); + new_stat->start_space = lim - lim_at_start; +#endif + + new_stat->msg_type = msg_type; + new_stat->start_time = get_run_time (); + + return make_cleanup (report_command_stats, new_stat); +} + /* Continuations are implemented as cleanups internally. Inherit from cleanups. */ struct continuation -- 1.7.0.4