[sheepdog] [PATCH 2/2] sheep: print thread id in logfile which could help debuging
Yunkai Zhang
yunkai.me at gmail.com
Sat Jun 16 21:00:28 CEST 2012
From: Yunkai Zhang <qiushu.zyk at taobao.com>
Main thread's id is 0, other thread's id will be [1..nr_thread].
The output format looks like these:
Jun 17 02:30:31|0|queue_request(387) 3
Jun 17 02:30:31|1|do_gateway_request(280) 3, 80a5d05d00000000 , 1
^
|
--- Here is thread id
Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
include/logger.h | 1 +
lib/logger.c | 11 ++++++++++-
sheep/work.c | 17 +++++++++++++++--
sheep/work.h | 5 +++++
4 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/include/logger.h b/include/logger.h
index c2ebece..7f7a5ec 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -52,6 +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);
/*
+ * sheep log priorities, comliant with syslog spec
diff --git a/lib/logger.c b/lib/logger.c
index c91bf3a..ef02305 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -47,6 +47,7 @@ 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 struct logarea *la;
static char *log_name;
static char *log_nowname;
@@ -197,7 +198,10 @@ static notrace int log_enqueue(int prio, const char *func, int line, const char
t = time(NULL);
tmp = localtime(&t);
- strftime(p, MAX_MSG_SIZE, "%b %2d %H:%M:%S ", tmp);
+ strftime(p, MAX_MSG_SIZE, "%b %2d %H:%M:%S", tmp);
+ p += strlen(p);
+
+ snprintf(p, MAX_MSG_SIZE - strlen(buff), "|%d|", thread_id);
p += strlen(p);
}
@@ -521,3 +525,8 @@ notrace void log_close(void)
free_logarea();
}
}
+
+notrace void set_thread_id(int tid)
+{
+ thread_id = tid;
+}
diff --git a/sheep/work.c b/sheep/work.c
index 8564cb2..84561b5 100644
--- a/sheep/work.c
+++ b/sheep/work.c
@@ -164,10 +164,14 @@ static void bs_thread_request_done(int fd, int events, void *data)
static void *worker_routine(void *arg)
{
- struct worker_info *wi = arg;
+ struct worker_info_ext *wie = arg;
+ struct worker_info *wi = wie->wi;
struct work *work;
eventfd_t value = 1;
+ set_thread_id(wie->thread_id);
+ free(wie);
+
pthread_mutex_lock(&wi->startup_lock);
/* started this thread */
pthread_mutex_unlock(&wi->startup_lock);
@@ -231,6 +235,8 @@ struct work_queue *init_work_queue(int nr)
{
int i, ret;
struct worker_info *wi;
+ struct worker_info_ext *wie;
+ static int thread_id;
ret = init_eventfd();
if (ret)
@@ -254,8 +260,15 @@ 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, wi);
+ worker_routine, wie);
if (ret) {
eprintf("failed to create worker thread #%d: %s\n",
diff --git a/sheep/work.h b/sheep/work.h
index 418b850..a0a5214 100644
--- a/sheep/work.h
+++ b/sheep/work.h
@@ -45,6 +45,11 @@ 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;
--
1.7.10.2
More information about the sheepdog
mailing list