From 65ed1c3a9275047ad67443491be23e6c7e887822 Mon Sep 17 00:00:00 2001 From: guo_peixu Date: Fri, 10 Jun 2022 16:13:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=8E=AF=E5=BD=A2=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA=E7=9B=B8=E5=85=B3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/test_handle_logger.c | 32 ++++++++++++++++---------------- src/MESA_handle_logger.c | 16 +++++++++++++++- src/Makefile | 6 +++--- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/demo/test_handle_logger.c b/demo/test_handle_logger.c index 2a3c845..f9cee4a 100644 --- a/demo/test_handle_logger.c +++ b/demo/test_handle_logger.c @@ -86,26 +86,26 @@ void call_logger(int log_num, int thread_num) void *thread_logger(void *arg) { - int thread_num = (int)(unsigned long long)arg; - printf("thread %d created! \n", thread_num); - call_logger(g_log_num, thread_num); - printf("thread %d finished! \n", thread_num); - return NULL; + int thread_num = (int)(unsigned long long)arg; + printf("thread %d created! \n", thread_num); + call_logger(g_log_num, thread_num); + printf("thread %d finished! \n", thread_num); + return NULL; } void sig_int_handler(int sig) { - printf("ctrl+c recviced!\n"); - MESA_destroy_runtime_log_handle(sample_handle); - MESA_destroy_runtime_log_handle(test_handle); - sample_handle = NULL; - test_handle = NULL; - if (g_mode == 2) - { - MESA_handle_runtime_log_destruction(); - } - printf("%d thread write %d log using %ld us, avg speed %f /s, %ld -> %ld\n", g_thread_num, g_thread_num * g_log_num, g_end_time - g_start_time, ((float)(g_thread_num * g_log_num) / (float)(g_end_time - g_start_time)) * 1000000, g_start_time, g_end_time); - exit(0); + printf("ctrl+c recviced!\n"); + MESA_destroy_runtime_log_handle(sample_handle); + MESA_destroy_runtime_log_handle(test_handle); + sample_handle = NULL; + test_handle = NULL; + if (g_mode == 2) + { + MESA_handle_runtime_log_destruction(); + } + printf("%d thread write %d log using %ld us, avg speed %f /s, %ld -> %ld\n", g_thread_num, g_thread_num * g_log_num, g_end_time - g_start_time, ((float)(g_thread_num * g_log_num) / (float)(g_end_time - g_start_time)) * 1000000, g_start_time, g_end_time); + exit(0); } void sig_hup_handler(int sig) diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index b4443e4..407a0fc 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -1,4 +1,5 @@ #include "MESA_handle_logger.h" +#include "MESA_shm_ring_queue.h" #include "snprintf.h" #include #include "zlog.h" @@ -30,6 +31,7 @@ struct MESA_pthread_private{ char *cache_buf; int fmt_buf_len; int cache_buf_len; + struct MESA_shm_queue_head *ring_queue_head; }; struct MESA_fmt_obj{ char sign; @@ -216,6 +218,12 @@ struct MESA_pthread_private *MESA_create_pthread_private(void *handle) if(pri->cache_buf == NULL){ goto error; } + pri->ring_queue_head = MESA_shm_get_ring_queue(); + /* + if(pri->queue_head == NULL){ + goto error; + } + */ return pri; error: @@ -237,6 +245,9 @@ void MESA_free_pthread_private(void *arg) if(pri == NULL){ return ; } + if(pri->ring_queue_head){ + MESA_shm_recycle_ring_queue(pri->ring_queue_head); + } if(pri->fmt_buf){ free(pri->fmt_buf); } @@ -252,6 +263,8 @@ void MESA_free_pthread_private(void *arg) void MESA_alloc_pthread_key() { pthread_key_create(&MESA_pthread_key, MESA_free_pthread_private); + MESA_shm_init_overview(); + MESA_shm_init_mutex(); return ; } @@ -334,9 +347,10 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const pri->cache_buf_len = n + 1; pri->cache_buf = p; va_copy(ap_bk, ap); - portable_vsnprintf(handle, pri->cache_buf, pri->cache_buf_len, fmt, ap_bk); + n = portable_vsnprintf(handle, pri->cache_buf, pri->cache_buf_len, fmt, ap_bk); } } + MESA_shm_copy_buf_to_ring_queue(pri->cache_buf, n, pri->ring_queue_head); va_end(ap); va_end(ap_bk); diff --git a/src/Makefile b/src/Makefile index 312a5df..653d7e6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -17,7 +17,7 @@ CFLAGS_+= -fsanitize=address -fno-omit-frame-pointer LIB+=-lasan endif -SRC=MESA_handle_logger.c +SRC=MESA_handle_logger.c MESA_shm_ring_queue.c OBJS=$(SRC:.c=.o) TARGET=libMESA_handle_logger.a libMESA_handle_logger.so @@ -26,10 +26,10 @@ all:$(TARGET) cp -f $(TARGET) ../lib libMESA_handle_logger.a:$(OBJS) - ar cqs $@ $< + ar cqs $@ $^ libMESA_handle_logger.so:$(OBJS) - $(CC) $(CFLAGS) $(LIB) -shared $< -o $@ + $(CC) $(CFLAGS) $(LIB) -shared $^ -o $@ .c.o: #$(OBJS):$(SRC)