[sheepdog] [PATCH] logger: make thread id more human readable

Liu Yuan namei.unix at gmail.com
Tue Jun 19 05:05:47 CEST 2012


On 06/19/2012 05:27 AM, MORITA Kazutaka wrote:
> Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
> ---
>  include/logger.h       |    2 +-
>  lib/logger.c           |   15 +++++++++++----
>  sheep/cluster/accord.c |    2 +-
>  sheep/sheep.c          |   12 ++++++------
>  sheep/work.c           |   28 +++++++++++++---------------
>  sheep/work.h           |    9 +++------
>  6 files changed, 35 insertions(+), 33 deletions(-)
> 
> diff --git a/include/logger.h b/include/logger.h
> index 7f7a5ec..7c8ddd2 100644
> --- a/include/logger.h
> +++ b/include/logger.h
> @@ -52,7 +52,7 @@ extern void log_close(void);
>  extern void dump_logmsg(void *);
>  extern void log_write(int prio, const char *func, int line, const char *fmt, ...)
>  	__attribute__ ((format (printf, 4, 5)));
> -extern void set_thread_id(int tid);
> +extern void set_thread_name(const char *name, int idx);
>  
>  /*
>  + * sheep log priorities, comliant with syslog spec
> diff --git a/lib/logger.c b/lib/logger.c
> index ef02305..e16ac8a 100644
> --- a/lib/logger.c
> +++ b/lib/logger.c
> @@ -47,7 +47,8 @@ static int log_enqueue(int prio, const char *func, int line, const char *fmt,
>  static void dolog(int prio, const char *func, int line, const char *fmt,
>  		  va_list ap) __attribute__ ((format (printf, 4, 0)));
>  
> -static __thread int thread_id;
> +static __thread const char *worker_name;
> +static __thread int worker_idx;
>  static struct logarea *la;
>  static char *log_name;
>  static char *log_nowname;
> @@ -201,7 +202,12 @@ static notrace int log_enqueue(int prio, const char *func, int line, const char
>  		strftime(p, MAX_MSG_SIZE, "%b %2d %H:%M:%S", tmp);
>  		p += strlen(p);
>  
> -		snprintf(p, MAX_MSG_SIZE - strlen(buff), "|%d|", thread_id);
> +		if (worker_name)
> +			snprintf(p, MAX_MSG_SIZE - strlen(buff), " [%s %d] ",
> +				 worker_name, worker_idx);
> +		else
> +			strncpy(p, " [main] ", MAX_MSG_SIZE - strlen(buff));
> +
>  		p += strlen(p);
>  	}
>  
> @@ -526,7 +532,8 @@ notrace void log_close(void)
>  	}
>  }
>  
> -notrace void set_thread_id(int tid)
> +notrace void set_thread_name(const char *name, int idx)
>  {
> -	thread_id = tid;
> +	worker_name = name;
> +	worker_idx = idx;
>  }
> diff --git a/sheep/cluster/accord.c b/sheep/cluster/accord.c
> index 3633a4c..ae75ced 100644
> --- a/sheep/cluster/accord.c
> +++ b/sheep/cluster/accord.c
> @@ -544,7 +544,7 @@ static int accord_init(const char *option)
>  		return -1;
>  	}
>  
> -	acrd_wq = init_work_queue(1);
> +	acrd_wq = init_work_queue("accord", 1);
>  	if (!acrd_wq) {
>  		eprintf("failed to create accord workqueue: %m\n");
>  		return -1;
> diff --git a/sheep/sheep.c b/sheep/sheep.c
> index 323419f..a2cd43e 100644
> --- a/sheep/sheep.c
> +++ b/sheep/sheep.c
> @@ -275,12 +275,12 @@ int main(int argc, char **argv)
>  		exit(1);
>  	}
>  
> -	sys->gateway_wqueue = init_work_queue(nr_gateway_worker);
> -	sys->io_wqueue = init_work_queue(nr_io_worker);
> -	sys->recovery_wqueue = init_work_queue(1);
> -	sys->deletion_wqueue = init_work_queue(1);
> -	sys->flush_wqueue = init_work_queue(1);
> -	sys->block_wqueue = init_work_queue(1);
> +	sys->gateway_wqueue = init_work_queue("gateway", nr_gateway_worker);
> +	sys->io_wqueue = init_work_queue("io", nr_io_worker);
> +	sys->recovery_wqueue = init_work_queue("recovery", 1);
> +	sys->deletion_wqueue = init_work_queue("deletion", 1);
> +	sys->flush_wqueue = init_work_queue("flush", 1);
> +	sys->block_wqueue = init_work_queue("block", 1);
>  	if (!sys->gateway_wqueue || !sys->io_wqueue ||
>  	    !sys->recovery_wqueue || !sys->deletion_wqueue ||
>  	    !sys->flush_wqueue || !sys->block_wqueue)
> diff --git a/sheep/work.c b/sheep/work.c
> index ae08525..6bebada 100644
> --- a/sheep/work.c
> +++ b/sheep/work.c
> @@ -78,13 +78,19 @@ static void bs_thread_request_done(int fd, int events, void *data)
>  
>  static void *worker_routine(void *arg)
>  {
> -	struct worker_info_ext *wie = arg;
> -	struct worker_info *wi = wie->wi;
> +	struct worker_info *wi = arg;
>  	struct work *work;
>  	eventfd_t value = 1;
> +	int i, idx;
>  
> -	set_thread_id(wie->thread_id);
> -	free(wie);
> +	for (i = 0; i < wi->nr_threads; i++) {
> +		if (wi->worker_thread[i] == pthread_self()) {
> +			idx = i;
> +			break;
> +		}
> +	}
> +
> +	set_thread_name(wi->name, idx);
>  
>  	pthread_mutex_lock(&wi->startup_lock);
>  	/* started this thread */
> @@ -145,12 +151,10 @@ static int init_eventfd(void)
>  	return 0;
>  }
>  
> -struct work_queue *init_work_queue(int nr)
> +struct work_queue *init_work_queue(const char *name, int nr)
>  {
>  	int i, ret;
>  	struct worker_info *wi;
> -	struct worker_info_ext *wie;
> -	static int thread_id;
>  
>  	ret = init_eventfd();
>  	if (ret)
> @@ -160,6 +164,7 @@ struct work_queue *init_work_queue(int nr)
>  	if (!wi)
>  		return NULL;
>  
> +	wi->name = name;
>  	wi->nr_threads = nr;
>  
>  	INIT_LIST_HEAD(&wi->q.pending_list);
> @@ -174,15 +179,8 @@ struct work_queue *init_work_queue(int nr)
>  
>  	pthread_mutex_lock(&wi->startup_lock);
>  	for (i = 0; i < wi->nr_threads; i++) {
> -		wie = zalloc(sizeof(*wie));
> -		if (!wie)
> -			return NULL;
> -
> -		wie->wi = wi;
> -		wie->thread_id = ++thread_id;
> -
>  		ret = pthread_create(&wi->worker_thread[i], NULL,
> -				     worker_routine, wie);
> +				     worker_routine, wi);
>  
>  		if (ret) {
>  			eprintf("failed to create worker thread #%d: %s\n",
> diff --git a/sheep/work.h b/sheep/work.h
> index 0a1727c..8e0beba 100644
> --- a/sheep/work.h
> +++ b/sheep/work.h
> @@ -20,6 +20,8 @@ struct work_queue {
>  };
>  
>  struct worker_info {
> +	const char *name;
> +
>  	struct list_head worker_info_siblings;
>  
>  	int nr_threads;
> @@ -39,15 +41,10 @@ struct worker_info {
>  	pthread_t worker_thread[0];
>  };
>  
> -struct worker_info_ext {
> -	int thread_id;
> -	struct worker_info *wi;
> -};
> -
>  extern struct list_head worker_info_list;
>  extern int total_nr_workers;
>  
> -struct work_queue *init_work_queue(int nr);
> +struct work_queue *init_work_queue(const char *name, int nr);
>  void queue_work(struct work_queue *q, struct work *work);
>  
>  #endif
> 

Applied, thanks.

Yuan




More information about the sheepdog mailing list