[PATCH] MI: new timing command

Nick Roberts
2006-12-30T08:52:12Z

Here's a patch for timing MI commands e.g.

(gdb)
-enable-timings
^done
(gdb)
-break-insert main
^done,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x080484ed",func="main",file="myprog.c",fullname="/home/nickrob/myprog.c",line="73",times="0"},time={wallclock="0.05185",user="0.00800",system="0.00000"}
(gdb)
-enable-timings no
^done
(gdb)
-exec-run
^running
(gdb)
*stopped,reason="breakpoint-hit",bkptno="1",thread-id="0",frame={addr="0x080484ed",func="main",args=[{name="argc",value="1"},{name="argv",value="0xbfb60364"}],file="myprog.c",fullname="/home/nickrob/myprog.c",line="73"}
(gdb)

It's based on Apple's code. I also have a patch which works for asynchronous
operation but there's probably not much point in submitting that now.

I hope to submit further patches from Apple's code. Notably:

1) -stack-list-locals --create-varobjs (hopefully though Vladimir will
do this one).

2) Event notification.

I thought I'd start with this one as it's straightforward.

--
Nick http://www.inet.net.nz/~nickrob

2006-12-30 Nick Roberts <nickrob@snap.net.nz>

Based on work by Apple Computer, Inc.

* mi/mi-main.c: Include <sys/resource.h>.
(current_command_ts, do_timings): New static variables.
(timestamp, print_diff_now, print_diff, wallclock_diff)
(user_diff, system_diff): New static timing functions.
(mi_cmd_enable_timings): New function for new MI command.
(captured_mi_execute_command, mi_execute_async_cli_command):
Call timing functions.

* mi/mi-cmds.c (mi_cmds): Add entry for new MI command
-enable-timings.

* mi/mi-cmds.h (mi_cmd_enable_timings): New extern.

* mi/mi-parse.h: Include <sys/resource.h>
(mi_timestamp): New structure.
(mi_parse): Add mi_timestamp* member.

Index: mi-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-cmds.c,v
retrieving revision 1.21
diff -c -p -r1.21 mi-cmds.c
*** mi-cmds.c 5 Jul 2006 19:03:47 -0000 1.21
--- mi-cmds.c 30 Dec 2006 08:42:26 -0000
*************** struct mi_cmd mi_cmds[] =
*** 58,63 ****
--- 58,64 ----
{ "display-enable", { NULL, 0 }, NULL, NULL },
{ "display-insert", { NULL, 0 }, NULL, NULL },
{ "display-list", { NULL, 0 }, NULL, NULL },
+ { "enable-timings", { NULL, 0 }, 0, mi_cmd_enable_timings},
{ "environment-cd", { NULL, 0 }, 0, mi_cmd_env_cd},
{ "environment-directory", { NULL, 0 }, 0, mi_cmd_env_dir},
{ "environment-path", { NULL, 0 }, 0, mi_cmd_env_path},
Index: mi-cmds.h
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-cmds.h,v
retrieving revision 1.19
diff -c -p -r1.19 mi-cmds.h
*** mi-cmds.h 23 Dec 2005 18:57:46 -0000 1.19
--- mi-cmds.h 30 Dec 2006 08:42:26 -0000
*************** extern mi_cmd_argv_ftype mi_cmd_data_lis
*** 72,77 ****
--- 72,78 ----
extern mi_cmd_argv_ftype mi_cmd_data_read_memory;
extern mi_cmd_argv_ftype mi_cmd_data_write_memory;
extern mi_cmd_argv_ftype mi_cmd_data_write_register_values;
+ extern mi_cmd_argv_ftype mi_cmd_enable_timings;
extern mi_cmd_argv_ftype mi_cmd_env_cd;
extern mi_cmd_argv_ftype mi_cmd_env_dir;
extern mi_cmd_argv_ftype mi_cmd_env_path;
Index: mi-main.c
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-main.c,v
retrieving revision 1.86
diff -c -p -r1.86 mi-main.c
*** mi-main.c 17 Nov 2006 19:30:41 -0000 1.86
--- mi-main.c 30 Dec 2006 08:42:29 -0000
***************
*** 49,54 ****
--- 49,55 ----

#include <ctype.h>
#include <sys/time.h>
+ #include <sys/resource.h>

enum
{
*************** struct captured_mi_execute_command_args
*** 81,86 ****
--- 82,93 ----
int mi_debug_p;
struct ui_file *raw_stdout;

+ /* This is used to pass the current command timestamp
+ down to continuation routines. */
+ static struct mi_timestamp *current_command_ts;
+
+ static int do_timings = 0;
+
/* The token of the last asynchronous command */
static char *last_async_command;
static char *previous_async_command;
*************** static int get_register (int regnum, int
*** 103,108 ****
--- 110,125 ----
layer that calls libgdb. Any operation used in the below should be
formalized. */

+ static void timestamp (struct mi_timestamp *tv);
+
+ static void print_diff_now (struct mi_timestamp *start);
+
+ static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
+ static long wallclock_diff (struct mi_timestamp *start,
+ struct mi_timestamp *end);
+ static long user_diff (struct mi_timestamp *start, struct mi_timestamp *end);
+ static long system_diff (struct mi_timestamp *start, struct mi_timestamp *end);
+
enum mi_cmd_result
mi_cmd_gdb_exit (char *command, char **argv, int argc)
{
*************** mi_cmd_exec_continue (char *args, int fr
*** 194,200 ****
}

/* Interrupt the execution of the target. Note how we must play around
! with the token varialbes, in order to display the current token in
the result of the interrupt command, and the previous execution
token when the target finally stops. See comments in
mi_cmd_execute. */
--- 211,217 ----
}

/* Interrupt the execution of the target. Note how we must play around
! with the token variables, in order to display the current token in
the result of the interrupt command, and the previous execution
token when the target finally stops. See comments in
mi_cmd_execute. */
*************** mi_cmd_data_write_memory (char *command,
*** 1024,1029 ****
--- 1041,1070 ----
return MI_CMD_DONE;
}

+ enum mi_cmd_result
+ mi_cmd_enable_timings (char *command, char **argv, int argc)
+ {
+ if (argc == 0)
+ do_timings = 1;
+ else if (argc == 1)
+ {
+ if (strcmp (argv[0], "yes") == 0)
+ do_timings = 1;
+ else if (strcmp (argv[0], "no") == 0)
+ do_timings = 0;
+ else
+ goto usage_error;
+ }
+ else
+ goto usage_error;
+
+ return MI_CMD_DONE;
+
+ usage_error:
+ error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
+ return MI_CMD_ERROR;
+ }
+
/* Execute a command within a safe environment.
Return <0 for error; >=0 for ok.

*************** captured_mi_execute_command (struct ui_o
*** 1038,1043 ****
--- 1079,1086 ----
(struct captured_mi_execute_command_args *) data;
struct mi_parse *context = args->command;

+ struct mi_timestamp cmd_finished;
+
switch (context->op)
{

*************** captured_mi_execute_command (struct ui_o
*** 1052,1059 ****
--- 1095,1109 ----
indication of what action is required and then switch on
that. */
args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
+
+ if (do_timings)
+ current_command_ts = context->cmd_start;
+
args->rc = mi_cmd_execute (context);

+ if (do_timings)
+ timestamp (&cmd_finished);
+
if (!target_can_async_p () || !target_executing)
{
/* print the result if there were no errors
*************** captured_mi_execute_command (struct ui_o
*** 1068,1073 ****
--- 1118,1127 ----
fputs_unfiltered ("^done", raw_stdout);
mi_out_put (uiout, raw_stdout);
mi_out_rewind (uiout);
+ /* Have to check cmd_start, since the command could be
+ "mi-enable-timings". */
+ if (do_timings && context->cmd_start)
+ print_diff (context->cmd_start, &cmd_finished);
fputs_unfiltered ("\n", raw_stdout);
}
else if (args->rc == MI_CMD_ERROR)
*************** mi_execute_command (char *cmd, int from_
*** 1163,1168 ****
--- 1217,1230 ----
if (command != NULL)
{
struct gdb_exception result;
+
+ if (do_timings)
+ {
+ command->cmd_start = (struct mi_timestamp *)
+ xmalloc (sizeof (struct mi_timestamp));
+ timestamp (command->cmd_start);
+ }
+
/* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
be pushed even further down or even eliminated? */
args.command = command;
*************** mi_execute_async_cli_command (char *mi,
*** 1350,1355 ****
--- 1412,1419 ----
fputs_unfiltered ("*stopped", raw_stdout);
mi_out_put (uiout, raw_stdout);
mi_out_rewind (uiout);
+ if (do_timings)
+ print_diff_now (current_command_ts);
fputs_unfiltered ("\n", raw_stdout);
return MI_CMD_QUIET;
}
*************** _initialize_mi_main (void)
*** 1466,1468 ****
--- 1530,1581 ----
DEPRECATED_REGISTER_GDBARCH_SWAP (old_regs);
deprecated_register_gdbarch_swap (NULL, 0, mi_setup_architecture_data);
}
+
+ static void
+ timestamp (struct mi_timestamp *tv)
+ {
+ gettimeofday (&tv->wallclock, NULL);
+ getrusage (RUSAGE_SELF, &tv->rusage);
+ }
+
+ static void
+ print_diff_now (struct mi_timestamp *start)
+ {
+ struct mi_timestamp now;
+ timestamp (&now);
+ print_diff (start, &now);
+ }
+
+ static void
+ print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
+ {
+ fprintf_unfiltered
+ (raw_stdout,
+ ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
+ wallclock_diff (start, end) / 1000000.0,
+ user_diff (start, end) / 1000000.0,
+ system_diff (start, end) / 1000000.0);
+ }
+
+ static long
+ wallclock_diff (struct mi_timestamp *start, struct mi_timestamp *end)
+ {
+ return ((end->wallclock.tv_sec - start->wallclock.tv_sec) * 1000000) +
+ (end->wallclock.tv_usec - start->wallclock.tv_usec);
+ }
+
+ static long
+ user_diff (struct mi_timestamp *start, struct mi_timestamp *end)
+ {
+ return
+ ((end->rusage.ru_utime.tv_sec - start->rusage.ru_utime.tv_sec) * 1000000) +
+ (end->rusage.ru_utime.tv_usec - start->rusage.ru_utime.tv_usec);
+ }
+
+ static long
+ system_diff (struct mi_timestamp *start, struct mi_timestamp *end)
+ {
+ return
+ ((end->rusage.ru_stime.tv_sec - start->rusage.ru_stime.tv_sec) * 1000000) +
+ (end->rusage.ru_stime.tv_usec - start->rusage.ru_stime.tv_usec);
+ }
Index: mi-parse.h
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-parse.h,v
retrieving revision 1.5
diff -c -p -r1.5 mi-parse.h
*** mi-parse.h 23 Dec 2005 18:57:46 -0000 1.5
--- mi-parse.h 30 Dec 2006 08:42:29 -0000
***************
*** 24,29 ****
--- 24,37 ----

/* MI parser */

+ #include <sys/resource.h>
+
+ /* Timestamps for current command and last asynchronous command */
+ struct mi_timestamp {
+ struct timeval wallclock;
+ struct rusage rusage;
+ };
+
enum mi_command_type
{
MI_COMMAND, CLI_COMMAND
*************** struct mi_parse
*** 35,40 ****
--- 43,49 ----
char *command;
char *token;
const struct mi_cmd *cmd;
+ struct mi_timestamp *cmd_start;
char *args;
char **argv;
int argc;