[sheepdog] [PATCH 10/10] sheep: add event_loop checker

Liu Yuan namei.unix at gmail.com
Wed Aug 7 20:02:25 CEST 2013


On Thu, Aug 08, 2013 at 12:49:08AM +0900, MORITA Kazutaka wrote:
> From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
> 
> We shouldn't spend too much time in the main thread.  This checker
> prints a warning message when sheep uses more than 1 ms in the main
> thread.
> 
> Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
> ---
>  sheep/thread_check.c |   42 ++++++++++++++++++++++++++++++++++++++++++
>  sheep/trace/graph.c  |    8 --------
>  sheep/trace/trace.h  |    8 ++++++++
>  3 files changed, 50 insertions(+), 8 deletions(-)
> 
> diff --git a/sheep/thread_check.c b/sheep/thread_check.c
> index 8a63b0d..39ad25b 100644
> --- a/sheep/thread_check.c
> +++ b/sheep/thread_check.c
> @@ -12,6 +12,47 @@
>  #include "sheep_priv.h"
>  #include "trace/trace.h"
>  
> +#define MAX_EVENT_DURATION 1000 /* us */
> +static int event_handler_depth = -1;
> +static uint64_t start_time;
> +
> +static void event_handler_enter(const struct caller *this_fn, int depth)
> +{
> +	if (!is_main_thread())
> +		return;
> +
> +	if (event_handler_depth < 0) {
> +		if (strcmp(this_fn->name, "do_event_loop") == 0)
> +			event_handler_depth = depth + 1;
> +	}
> +
> +	if (depth == event_handler_depth)
> +		start_time = clock_get_time();
> +}
> +
> +static void event_handler_exit(const struct caller *this_fn, int depth)
> +{
> +	if (!is_main_thread())
> +		return;
> +
> +	if (depth == event_handler_depth) {
> +		uint64_t duration = clock_get_time() - start_time;
> +		unsigned quot = duration / 1000, rem = duration % 1000;
> +
> +		if (quot > MAX_EVENT_DURATION)
> +			sd_printf(SDOG_WARNING,
> +				  "%s wastes too much time in event loop: "
> +				  "%u.%-3u us", this_fn->name, quot, rem);
> +	}
> +}
> +
> +static struct tracer event_handler_tracer = {
> +	.name = "event handler",
> +
> +	.enter = event_handler_enter,
> +	.exit = event_handler_exit,
> +};
> +
>  static void thread_check_enter(const struct caller *this_fn, int depth)
>  {
>  	if (strcmp(this_fn->section, MAIN_FN_SECTION) == 0) {
> @@ -33,5 +74,6 @@ static struct tracer thread_check_tracer = {
>  
>  void register_thread_checker(void)
>  {
> +	trace_register(&event_handler_tracer);
>  	trace_register(&thread_check_tracer);
>  }

This abuse register_thread_checker(). Simply register tracer in the trace_init
is pretty enough.

Also I think thread_check.c should be renamed & moved to
sheep/trace/checkers.c

Thanks
Yuan



More information about the sheepdog mailing list