From dab9b5c03d94dcf53baef987b2f6e67a7c3ef95a Mon Sep 17 00:00:00 2001 From: guo_peixu Date: Tue, 28 Jun 2022 18:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=BF=9B=E7=A8=8B=E9=97=B4?= =?UTF-8?q?=E5=85=B1=E4=BA=AB=E7=9A=84=E4=BF=A1=E5=8F=B7=E9=87=8F=EF=BC=8C?= =?UTF-8?q?=E6=A0=87=E8=AE=B0=E6=B6=88=E8=B4=B9=E8=80=85=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=AD=A3=E5=9C=A8=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/MESA_shm_ring_queue.h | 7 +++++++ shm_consumer/MESA_shm_consumer.c | 34 ++++++++++++++++++++++++++++++- src/MESA_handle_logger.c | 21 +++++++++++++++++-- src/MESA_shm_ring_queue.c | 35 ++++++++++++++++++++++++++++++++ src/version.map | 2 +- 5 files changed, 95 insertions(+), 4 deletions(-) diff --git a/inc/MESA_shm_ring_queue.h b/inc/MESA_shm_ring_queue.h index 05dcaa3..ff7c180 100644 --- a/inc/MESA_shm_ring_queue.h +++ b/inc/MESA_shm_ring_queue.h @@ -16,6 +16,9 @@ #define MESA_SHM_RING_QUEUE_HALF_IDLE 2 #define MESA_SHM_RING_QUEUE_USED 3 +#define MESA_SEM_KEY 35719 + + struct MESA_shm_overview *MESA_shm_alloc_overview(); struct MESA_shm_queue_head *MESA_shm_get_ring_queue(); void MESA_shm_init_mutex(); @@ -25,6 +28,10 @@ int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head); int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head); void MESA_shm_ring_queue_set_empty(struct MESA_shm_queue_head *head); +int MESA_shm_alloc_sem(); +int MESA_shm_consumer_is_running(); + + diff --git a/shm_consumer/MESA_shm_consumer.c b/shm_consumer/MESA_shm_consumer.c index 373e98f..c21fa49 100644 --- a/shm_consumer/MESA_shm_consumer.c +++ b/shm_consumer/MESA_shm_consumer.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,8 @@ struct log_file_list g_log_file_list; struct care_pid_list g_care_pid_list; int g_output_mode = CONSUMER_OUTPUT_MODE_FILE; int g_care_pid = CONSUMER_CARE_PID_ALL; +int g_semid = -1; + void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head) { int i = 0; @@ -52,7 +55,10 @@ void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_ if(tmp_ovw->shmid == -1){ break; } - ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0); + ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0); + if(ring_queue_head[i] == (struct MESA_shm_queue_head *)-1){ + ring_queue_head[i] = NULL; + } } return ; } @@ -507,6 +513,30 @@ error: pclose(fp); return -1; } +void MESA_shm_sem_p(int semid) +{ + if(g_semid == -1){ + return ; + } + struct sembuf sem_b; + sem_b.sem_num = 0; + sem_b.sem_op = -1; + sem_b.sem_flg = SEM_UNDO; + semop(semid, &sem_b, 1); + return ; +} +void MESA_shm_sem_v(int semid) +{ + if(g_semid == -1){ + return ; + } + struct sembuf sem_b; + sem_b.sem_num = 0; + sem_b.sem_op = 1; + sem_b.sem_flg = SEM_UNDO; + semop(g_semid, &sem_b, 1); + return ; +} void print_help(char *exe_name) { @@ -537,6 +567,8 @@ int main(int argc, char **argv) }else{ g_cur_tty_fd = get_cur_tty_fd(); } + g_semid = MESA_shm_alloc_sem(); + MESA_shm_sem_p(g_semid); int i = 0; struct MESA_shm_overview *shm_overview = NULL; struct MESA_shm_overview *tmp_ovw = NULL; diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index b4497d8..bf1f0c4 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -26,6 +26,11 @@ static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = ""; pthread_key_t MESA_pthread_key; pthread_once_t MESA_pthread_key_once = PTHREAD_ONCE_INIT; +#define MESA_CONSUMER_RUNNING 1 +#define MESA_CONSUMER_NOT_RUNNING 0 + +int MESA_consumer_status = MESA_CONSUMER_NOT_RUNNING; + struct MESA_pthread_private{ char *fmt_buf; char *cache_buf; @@ -260,11 +265,21 @@ void MESA_free_pthread_private(void *arg) } return ; } - +void *MESA_check_consumer_status(void *arg) +{ + while(1){ + MESA_consumer_status = MESA_shm_consumer_is_running(); + sleep(1); + }; + return NULL; +} void MESA_alloc_pthread_key() { pthread_key_create(&MESA_pthread_key, MESA_free_pthread_private); MESA_shm_init_overview(); + pthread_t tid; + pthread_create(&tid, NULL, MESA_check_consumer_status, NULL); + pthread_detach(tid); return ; } @@ -329,7 +344,9 @@ void MESA_destroy_runtime_log_handle(void *handle) void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...) { - + if(MESA_consumer_status == MESA_CONSUMER_NOT_RUNNING){ + return ; + } log_handle_t *p_handle = (log_handle_t *)handle; if(p_handle == NULL || p_handle->runtime_log_file == NULL)return; diff --git a/src/MESA_shm_ring_queue.c b/src/MESA_shm_ring_queue.c index aaaafd2..8e85916 100644 --- a/src/MESA_shm_ring_queue.c +++ b/src/MESA_shm_ring_queue.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,14 @@ struct MESA_shm_overview *MESA_shm_ovw = NULL; +int MESA_shm_semid = -1; +union semun { + int val; + struct semid_ds *buf; + unsigned short *array; + struct seminfo *__buf; +}; + void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw) { @@ -24,6 +33,31 @@ void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw) return ; } +int MESA_shm_alloc_sem() +{ + union semun semopt; + int semid = semget(MESA_SEM_KEY, 0 , 0); + if(semid == -1){ + semid = semget(MESA_SEM_KEY, 1 ,IPC_CREAT | 0666); + if(semid == -1){ + return -1; + } + semopt.val = 1; + semctl(semid, 0, SETVAL, semopt); + } + return semid; +} + +int MESA_shm_consumer_is_running() +{ + if(MESA_shm_semid == -1){ + return 0; + } + int semval = semctl(MESA_shm_semid, 0 ,GETVAL, 0); + return !semval ; +} + + struct MESA_shm_overview *MESA_shm_alloc_overview() { int shmsize = sizeof(struct MESA_shm_overview) * MESA_SHM_RING_QUEUE_NUM; @@ -62,6 +96,7 @@ void MESA_shm_init_overview() if(MESA_shm_ovw == NULL){ MESA_shm_ovw = MESA_shm_alloc_overview(); } + MESA_shm_semid = MESA_shm_alloc_sem(); return ; } diff --git a/src/version.map b/src/version.map index a4c52a9..c815f8a 100644 --- a/src/version.map +++ b/src/version.map @@ -1,4 +1,4 @@ { - global: MESA*runtime_log*;GIT_VERSION_*;MESA_shm_alloc_overview;MESA_handle_fmt_rule_register;MESA_shm_ring_queue_is_empty;MESA_shm_ring_queue_is_full;MESA_shm_ring_queue_set_empty; + global: MESA*runtime_log*;GIT_VERSION_*;MESA_shm_alloc*;MESA_handle_fmt_rule_register;MESA_shm_ring_queue*; local: *; };