Timers: add timer debugging through -timer-debug-log
cover Timers: add timer debugging through -timer-debug-log
This patch set adds facilities for debugging timers using the additional command line option -timer-debug-log=FILE. If this option is selected, a debugging file will be written showing information about the current state of timers in the system, which the author feels will be useful for debugging in the field.
Note that the option is a command line option rather than a configure time option. This is because users in the field having issues are unlikely to have a compile time option enabled.
It would be useful to get this feature in prior to 1.7 as it has little impact other than making a major change to a subsystem more debuggable. This patch has been lightly test.
Impact of changes whether or not -timer-debug-log is specified:
1. QEMUTimer is expanded to hold additional debugging information. Some of this is unused when the command line option is unspecified.
2. The file and line number of the caller that allocated the timer are recorded. This is useful for debugging in gdb.
It is felt these are minimal in nature.
Additional impact of changes only when -timer-debug-log is specified:
1. On every timer modification, the current clock time for that timer is read, and the additional debug information filled in.
2. Every second (roughly) a file is written (atomically) containing the timer debug information.
The debug information includes information on the number of timer expiries since the timer was created, the average expiry time (in nanoseconds), and the number of short expiries, being the number of times the timer was asked to expire in less than one microsecond (these usually but not always indicate a bug).
The file format is designed to be useful both to a mailing list and to a user armed with gdb. An example of the output follows:
Timer list at 0x7f4d6cf0d6e0 clock 0:
Address Expiries AvgLength NumShort Source
Timer list at 0x7f4d6cf0cbc0 clock 0:
Address Expiries AvgLength NumShort Source
Timer list at 0x7f4d6cf0d750 clock 1:
Address Expiries AvgLength NumShort Source
Timer list at 0x7f4d6cf0cc30 clock 1:
Address Expiries AvgLength NumShort Source
0x7f4d6cf51550 1 27462700 0 i8254.c:333
Timer list at 0x7f4d6cf0d7c0 clock 2:
Address Expiries AvgLength NumShort Source
Timer list at 0x7f4d6cf0cca0 clock 2:
Address Expiries AvgLength NumShort Source
0x7f4d6cf6eed0 1 999997000 0 mc146818rtc.c:858
Note that the somewhat strange choice to output to a file has been taken because the tracing infrastructure is unlikely to be enabled in a distro environment.
Alex Bligh (4):
Timers: add debugging macros wrapping timer functions and debug
structures
Timers: add command line option -timer-debug-log
Timers: Instrument timer_mod
Timers: produce timer-debug-log file
include/block/aio.h | 20 ++++++--- include/qemu/timer.h | 70 ++++++++++++++++++++++-------- qemu-options.hx | 11 +++++ qemu-timer.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++-- vl.c | 3 ++ 5 files changed, 194 insertions(+), 28 deletions(-)
Signature · 1 line
1.7.9.5
PATCH 1/4 Timers: add debugging macros wrapping timer functions and debug structures
Add debugging versions of functions creating timers to record the file and line number that they were called from. Add macros to call these transparently. Add fields to timer struct to store debugging information.
Note this patch contains one checkpatch.pl warning (space before parenthesis) and a rather arcane double stringify macro. These are copied from audio_int.h and I believe are to work around compiler incompatibilities.
--- include/block/aio.h | 20 ++++++++++----- include/qemu/timer.h | 69 ++++++++++++++++++++++++++++++++++++-------------- qemu-timer.c | 8 +++--- 3 files changed, 69 insertions(+), 28 deletions(-)
include/block/aio.h +14 -6
@@ -262,13 +262,17 @@ void qemu_aio_set_fd_handler(int fd,* * Returns: a pointer to the new timer */ -static inline QEMUTimer *aio_timer_new (AioContext *ctx,QEMUClockType type, -int scale, -QEMUTimerCB *cb,void *opaque) +static inline QEMUTimer *aio_timer_new_dbg (AioContext *ctx,QEMUClockType type, +int scale, +QEMUTimerCB *cb,void *opaque, +const char *dbg) { -return timer_new_tl (ctx->tlg.tl[type], scale, cb, opaque); +return timer_new_tl_dbg (ctx->tlg.tl[type], scale, cb, opaque, dbg); } +#define aio_timer_new (ctx, type, scale, opaque) \ + aio_timer_new_dbg(ctx, type, scale, opaque, TIMER_DBG) +/** * aio_timer_init: * @ctx: the aio context
@@ -284,9 +288,13 @@ static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,static inline void aio_timer_init (AioContext *ctx,QEMUTimer *ts,QEMUClockType type,int scale, -QEMUTimerCB *cb,void *opaque) +QEMUTimerCB *cb,void *opaque, +const char *dbg) { -timer_init (ts, ctx->tlg.tl[type], scale, cb, opaque); +timer_init_dbg (ts, ctx->tlg.tl[type], scale, cb, opaque, dbg); } +#define aio_timer_init (ctx, ts, type, scale, cb, opaque) \ + aio_timer_init(ctx, ts, type, scale, cb, opaque, TIMER_DBG) +#endif
include/qemu/timer.h +50 -19
133 lines not on this page · raw message
qemu-timer.c +5 -3
@@ -321,15 +321,17 @@ int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout) } -void timer_init (QEMUTimer *ts, -QEMUTimerList *timer_list,int scale, -QEMUTimerCB *cb,void *opaque) +void timer_init_dbg (QEMUTimer *ts, +QEMUTimerList *timer_list,int scale, +QEMUTimerCB *cb,void *opaque, +const char *dbg) { ts->timer_list = timer_list; ts->cb = cb; ts->opaque = opaque; ts->scale = scale; ts->expire_time =-1 ; + ts->dbg = dbg; }void timer_free (QEMUTimer *ts)
Signature · 1 line
1.7.9.5
PATCH 2/4 Timers: add command line option -timer-debug-log
In reply to Alex BlighAdd a command line option -timer-debug-log which takes the name of a file to which periodic timer debugging information will be written.
--- include/qemu/timer.h | 1 + qemu-options.hx | 11 +++++++++++ qemu-timer.c | 1 + vl.c | 3 +++ 4 files changed, 16 insertions(+)
include/qemu/timer.h +1 -0
@@ -75,6 +75,7 @@ struct QEMUTimer { };extern QEMUTimerListGroup main_loop_tlg; +extern const char *timer_debug_log;/* * QEMUClockType
qemu-options.hx +11 -0
@@ -3116,6 +3116,17 @@ STEXI prepend a timestamp to each log message.(default:on) ETEXI +DEF("timer-debug-log", HAS_ARG, QEMU_OPTION_timer_debug_log, + "-timer-debug-log FILE\n" + " write timer debug log to FILE (default: don't write)", + QEMU_ARCH_ALL) +STEXI +@item -timer-debug-log @var{file} +@findex -timer-debug-log +Write timer debug output periodically to file @var{file}. By default, +no timer debug logging is written. +ETEXI + HXCOMM This is the last statement. Insert new options before this line! STEXI @end table
qemu-timer.c +1 -0
@@ -57,6 +57,7 @@ typedef struct QEMUClock {QEMUTimerListGroup main_loop_tlg;QEMUClock qemu_clocks[QEMU_CLOCK_MAX ]; +const char *timer_debug_log;/* A QEMUTimerList is a list of timers attached to a clock. More * than one QEMUTimerList can be attached to each clock, for instance
vl.c +3 -0
@@ -3794,6 +3794,9 @@ int main(int argc, char **argv, char **envp) }configure_msg (opts);break ; +case QEMU_OPTION_timer_debug_log: + timer_debug_log = optarg; +break ;default :os_parse_cmd_args (popt->index, optarg); }
Signature · 1 line
1.7.9.5
PATCH 3/4 Timers: Instrument timer_mod
In reply to Alex BlighAdd instrumentation for timer_mod to allow measurement of the average time delta to expiry plus the number of short delta periods. This is only run when logging to a file because getting the clock value may add appreciable expense.
--- qemu-timer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
qemu-timer.c +17 -0
@@ -376,6 +376,23 @@ static bool timer_mod_ns_locked(QEMUTimerList *timer_list, ts->next = *pt; *pt = ts; +if (timer_debug_log) { +int64_t delta; + + delta = ts->expire_time - +qemu_clock_get_ns (ts->timer_list->clock->type); +if (delta <=0 ) { + delta =0 ; + } + + ts->tot_deltas += delta; + ts->num_deltas++; + +if (delta <SCALE_US ) { + ts->num_short++; + } + } +return pt == &timer_list->active_timers; }
Signature · 1 line
1.7.9.5
PATCH 4/4 Timers: produce timer-debug-log file
In reply to Alex BlighWrite a timer-debug-log file if enabled containing data about the currently existing timers.
--- qemu-timer.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)
qemu-timer.c +92 -0
@@ -58,6 +58,7 @@ typedef struct QEMUClock {QEMUTimerListGroup main_loop_tlg;QEMUClock qemu_clocks[QEMU_CLOCK_MAX ];const char *timer_debug_log; +static int64_t timer_last_debug;/* A QEMUTimerList is a list of timers attached to a clock. More * than one QEMUTimerList can be attached to each clock, for instance
@@ -396,6 +397,93 @@ static bool timer_mod_ns_locked(QEMUTimerList *timer_list,return pt == &timer_list->active_timers; } +static void timer_debug (void ) +{ +GString *debug_text; +GString *tmpfile; +QEMUClockType type; +FILE *f; +uint64_t now; + +if (!timer_debug_log) { +return ; + } + +/* In order not to avoid influencing the output, we don't use a timer +* here, but use this disappointingly manual method. +*/ + now =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ); +if ((now - timer_last_debug) <1000 *SCALE_MS ) { +return ; + } + timer_last_debug = now; + + debug_text =g_string_new ("" ); + tmpfile =g_string_new (timer_debug_log); +g_string_append (tmpfile,".tmp" ); + +for (type =0 ; type <QEMU_CLOCK_MAX ; type++) { +QEMUTimerList *timer_list; +QEMUClock *clock =qemu_clock_ptr (type); + +/* Iteration through timerlists means we need the BQL held to +* call this safely. +*/ + QLIST_FOREACH(timer_list, &clock->timerlists, list) { +QEMUTimer *ts; + +g_string_append_printf (debug_text,"\nTimer list at %p clock %d:\n" , + timer_list, (int ) type); +g_string_append_printf (debug_text,"%18s %14s %14s %14s %s\n" , +"Address" , +"Expiries" , +"AvgLength" , +"NumShort" , +"Source" ); +qemu_mutex_lock (&timer_list->active_timers_lock); + ts = timer_list->active_timers; +for (ts = timer_list->active_timers; ts; ts = ts->next) { +int64_t avg =-1 ; +if (ts->num_deltas) { + avg = (ts->tot_deltas + (ts->num_deltas/2 )) / + ts->num_deltas; + } +const char *src ="unknown" ; +if (ts->dbg) { +const char *slash; + src = ts->dbg; + slash =strrchr (src,'/' ); +if (!slash) { + slash =strrchr (src,'\\' ); + } +if (slash) { + src = slash+1 ; + } + } + +g_string_append_printf (debug_text,"%18p %14" PRId64" %14" + PRId64" %14" PRId64" %s\n" , + ts, + ts->num_deltas, + avg, + ts->num_short, + src); + } +qemu_mutex_unlock (&timer_list->active_timers_lock); + } + } + + f =fopen (tmpfile->str,"w" ); +if (f) { +fprintf (f,"%s" , debug_text->str); +fclose (f); +rename (tmpfile->str, timer_debug_log); + } + +g_string_free (tmpfile, true); +g_string_free (debug_text, true); +} +static void timerlist_rearm (QEMUTimerList *timer_list) {/* Interrupt execution to force deadline recalculation. */
@@ -621,6 +709,10 @@ bool qemu_clock_run_all_timers(void)bool progress = false;QEMUClockType type; +if (timer_debug_log) { +timer_debug (); + } +for (type =0 ; type <QEMU_CLOCK_MAX ; type++) { progress |=qemu_clock_run_timers (type); }
Signature · 1 line
1.7.9.5
Il 25/10/2013 23:30, Alex Bligh ha scritto:
Quoted 57 lines
This patch set adds facilities for debugging timers using the additional command line option -timer-debug-log=FILE. If this option is selected, a debugging file will be written showing information about the current state of timers in the system, which the author feels will be useful for debugging in the field.
Note that the option is a command line option rather than a configure time option. This is because users in the field having issues are unlikely to have a compile time option enabled.
It would be useful to get this feature in prior to 1.7 as it has little impact other than making a major change to a subsystem more debuggable. This patch has been lightly test.
Impact of changes whether or not -timer-debug-log is specified:
1. QEMUTimer is expanded to hold additional debugging information. Some of this is unused when the command line option is unspecified.
2. The file and line number of the caller that allocated the timer are recorded. This is useful for debugging in gdb.
It is felt these are minimal in nature.
Additional impact of changes only when -timer-debug-log is specified:
1. On every timer modification, the current clock time for that timer is read, and the additional debug information filled in.
2. Every second (roughly) a file is written (atomically) containing the timer debug information.
The debug information includes information on the number of timer expiries since the timer was created, the average expiry time (in nanoseconds), and the number of short expiries, being the number of times the timer was asked to expire in less than one microsecond (these usually but not always indicate a bug).
The file format is designed to be useful both to a mailing list and to a user armed with gdb. An example of the output follows:
Timer list at
0x7f4d6cf0d6e0clock 0:
Address Expiries AvgLength NumShort SourceTimer list at
0x7f4d6cf0cbc0clock 0:
Address Expiries AvgLength NumShort SourceTimer list at
0x7f4d6cf0d750clock 1:
Address Expiries AvgLength NumShort SourceTimer list at
0x7f4d6cf0cc30clock 1:
Address Expiries AvgLength NumShort Source
0x7f4d6cf515501 27462700 0 i8254.c:333Timer list at
0x7f4d6cf0d7c0clock 2:
Address Expiries AvgLength NumShort Source
Quoted 7 lines
Timer list at
0x7f4d6cf0cca0clock 2:
Address Expiries AvgLength NumShort Source
0x7f4d6cf6eed01 999997000 0 mc146818rtc.c:858Note that the somewhat strange choice to output to a file has been taken because the tracing infrastructure is unlikely to be enabled in a distro environment.
This is a bug in the distro, if it is Linux. There is no reason not to enable the stap trace format when running on Linux (Fedora does for other packages than QEMU, too---most notably glib and glibc).
If it is useful, adding debugging information to timer_new_ns (please make file and line two separate arguments, though) can definitely be done unconditionally and added to the traces. I think adding a tracepoint in timerlist_run_timers would provide very similar information to that in your file.
Paolo
Quoted 9 lines · Paolo Bonzini
his is a bug in the distro, if it is Linux. There is no reason not to enable the stap trace format when running on Linux (Fedora does for other packages than QEMU, too---most notably glib and glibc).
If it is useful, adding debugging information to
timer_new_ns(please make file and line two separate arguments, though) can definitely be done unconditionally and added to the traces. I think adding a tracepoint intimerlist_run_timerswould provide very similar information to that in your file.
I read the tracepoint information. Doesn't that require the end user to have far more skills (and far more stuff installed) to get things like "average expiry delta"? Especially as that's not something we'd normally calculate as we don't get the clock value when setting a timer normally.
Signature · 1 line
Alex Bligh
Il 26/10/2013 06:52, Alex Bligh ha scritto:
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines
his is a bug in the distro, if it is Linux. There is no reason not to enable the stap trace format when running on Linux (Fedora does for other packages than QEMU, too---most notably glib and glibc).
If it is useful, adding debugging information to
timer_new_ns(please make file and line two separate arguments, though) can definitely be done unconditionally and added to the traces. I think adding a tracepoint intimerlist_run_timerswould provide very similar information to that in your file.
Quoted 5 lines
I read the tracepoint information. Doesn't that require the end user to have far more skills (and far more stuff installed) to get things like "average expiry delta"? Especially as that's not something we'd normally calculate as we don't get the clock value when setting a timer normally.
The simple trace backend includes a nanosecond value that is the same as rt_clock, so you can correlate the last timer_mod with the next iteration in timerlist_run_timers (I would put the tracepoint inside the loop, just before the callback is done). Similarly, in systemtap you can just use the builtin gettimeofday_ns() function
All clocks are basically in nanoseconds (the only difference between vm_clock and rt_clock is that the former stops when the VM is stopped), so you can get the average expiry delta from there.
Of course it requires some postprocessing or a systemtap script, but we can put that into scripts/ or give it to the user. See
https://sourceware.org/ml/systemtap/2011-q3/txt00003.txt for an example, I suppose the "expiry time" computation would look like this:
global modtime, expirytot, firedcnt
probe qemu.system.x86_64.timer_mod {
loc = sprintf("%s:%d", timer_file, timer_line) modtime[pid(), loc] = gettimeofday_ns()
}
probe qemu.system.x86_64.timer_run {
loc = sprintf("%s:%d", timer_file, timer_line) expirytot[pid(), loc] += gettimeofday_ns() - modtime[pid(), loc] firedcnt[pid(), loc]++
}
probe end
{
printf("\n%8s %28s %8s %8s\n", "pid", "loc", "avg expiry", "cnt") foreach([p+, loc] in firedcnt) {
printf("%8d %28s %8d %8d\n", p, loc,
expirytot[p, loc] / firedcnt[p, loc], firedcnt[p, loc])
}
}
which I wrote based on the example at
http://cygwin.com/ml/systemtap/2011-q3/msg00316.html
I understand your usecase, but it's not really feasible to add a logging option for each tracing need; it's much better to add new tracepoints and distribute sample systemtap scripts. And please "lobby" your distro for enabling systemtap support and distributing the .stp file!
Paolo
Paolo,
Quoted 6 lines · Paolo Bonzini
The simple trace backend includes a nanosecond value that is the same as
rt_clock, so you can correlate the lasttimer_modwith the next iteration intimerlist_run_timers(I would put the tracepoint inside the loop, just before the callback is done). Similarly, in systemtap you can just use the builtingettimeofday_ns()function
Quoted 8 lines
All clocks are basically in nanoseconds (the only difference between
vm_clockandrt_clockis that the former stops when the VM is stopped), so you can get the average expiry delta from there.Of course it requires some postprocessing or a systemtap script, but we can put that into
scripts/or give it to the user. See
https://sourceware.org/ml/systemtap/2011-q3/txt00003.txt for an example, I suppose the "expiry time" computation would look like this:
OK I need to look at the tracepoint stuff a bit more it seems :-)
If we can do this without a patch (save to put some tracepoints in), that's obviously better than doing it with a patch. I don't like the file particularly either.
Quoted 4 lines
global modtime, expirytot, firedcnt
probe qemu.system.x86_64.timer_mod {
loc =sprintf("%s:%d", timer_file, timer_line)
How do timer_file and timer_line get filled in? If those still need to be put in through code changes, how about taking patch 1 of the series (that fills in timer->dbg with this information), save for the unnecessary additions to the structure, plus whatever is necessary to put the tracepoints in?
Alex
Quoted 17 lines
modtime[pid(), loc] =
gettimeofday_ns()
}
probe qemu.system.x86_64.timer_run {
loc =sprintf("%s:%d", timer_file, timer_line)expirytot[pid(), loc] +=gettimeofday_ns()- modtime[pid(), loc] firedcnt[pid(), loc]++
}probe end
{
printf("\n%8s %28s %8s %8s\n", "pid", "loc", "avg expiry", "cnt")foreach([p+, loc] in firedcnt){
printf("%8d %28s %8d %8d\n", p, loc,
expirytot[p, loc] / firedcnt[p, loc], firedcnt[p, loc])
}
}
Signature · 1 line
Alex Bligh
Il 26/10/2013 10:24, Alex Bligh ha scritto:
Quoted 5 lines
How do
timer_fileandtimer_lineget filled in? If those still need to be put in through code changes, how about taking patch 1 of the series (that fills intimer->dbgwith this information), save for the unnecessary additions to the structure, plus whatever is necessary to put the tracepoints in?
Sure!
Paolo