diff options
author | Paul N. Hilfinger <hilfinger@adacore.com> | 2010-06-26 06:44:47 +0000 |
---|---|---|
committer | Paul N. Hilfinger <hilfinger@adacore.com> | 2010-06-26 06:44:47 +0000 |
commit | 0f3bb72eb70f61f3154fd51eaac173bd6486f3d6 (patch) | |
tree | 6e84fe05cdf4fa6017e2b838db95d5f8aae34a80 /gdb/utils.c | |
parent | 3c1b3731d34376550c72a8593f9489bb78d2e445 (diff) | |
download | gdb-0f3bb72eb70f61f3154fd51eaac173bd6486f3d6.zip gdb-0f3bb72eb70f61f3154fd51eaac173bd6486f3d6.tar.gz gdb-0f3bb72eb70f61f3154fd51eaac173bd6486f3d6.tar.bz2 |
Refactor 'maint time' command statistics.
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/testsuite/gdb.gdb/selftest.exp: Adjust expected message for
capturing start-up runtime.
Diffstat (limited to 'gdb/utils.c')
-rw-r--r-- | gdb/utils.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/gdb/utils.c b/gdb/utils.c index 472cf47..a8beb37 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -513,6 +513,100 @@ 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 + } +} + +/* 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_dtor (report_command_stats, new_stat, xfree); +} + /* Continuations are implemented as cleanups internally. Inherit from cleanups. */ struct continuation |