From 0d13a2de172f5b828dd22e91f554cea26db60555 Mon Sep 17 00:00:00 2001 From: yuqing Date: Tue, 26 Jun 2018 10:56:34 +0800 Subject: [PATCH] sched_thread.h: add function sched_print_all_entries --- HISTORY | 3 +- src/sched_thread.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++ src/sched_thread.h | 15 ++++++++-- src/sockopt.c | 3 -- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/HISTORY b/HISTORY index 9922e3d..4b651e1 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.38 2018-06-11 +Version 1.38 2018-06-26 * connection_pool.c: set err_no to 0 when success * shared_func.h: add functions float2buff / buff2float, double2buff / buff2double * logger.h: add function log_get_level_caption @@ -9,6 +9,7 @@ Version 1.38 2018-06-11 * add skiplist_set.[hc] and skiplist bug fixed * correct CRC32 * shared_func.h: add functions int2str and long2str + * sched_thread.h: add function sched_print_all_entries Version 1.37 2018-02-24 * ini_file_reader.c function annotations LOCAL_IP_GET support index, such as: diff --git a/src/sched_thread.c b/src/sched_thread.c index a153a7b..635c336 100644 --- a/src/sched_thread.c +++ b/src/sched_thread.c @@ -24,8 +24,12 @@ static int waiting_del_id = -1; static ScheduleContext *schedule_context = NULL; static int timer_slot_count = 0; static int mblock_alloc_once = 0; +static uint32_t next_id = 0; +static bool print_all_entries = false; static void sched_deal_delay_tasks(ScheduleContext *pContext); +static int sched_dup_array(const ScheduleArray *pSrcArray, + ScheduleArray *pDestArray); static int sched_cmp_by_next_call_time(const void *p1, const void *p2) { @@ -58,6 +62,11 @@ static int sched_init_entries(ScheduleArray *pScheduleArray) pEnd = pScheduleArray->entries + pScheduleArray->count; for (pEntry=pScheduleArray->entries; pEntryid) + { + next_id = pEntry->id; + } + if (pEntry->interval <= 0) { logError("file: "__FILE__", line: %d, " \ @@ -296,6 +305,61 @@ static void *sched_call_func(void *args) return NULL; } +void sched_print_all_entries() +{ + print_all_entries = true; +} + +static int sched_cmp_by_id(const void *p1, const void *p2) +{ + return (int64_t)((ScheduleEntry *)p1)->id - + (int64_t)((ScheduleEntry *)p2)->id; +} + +static int print_all_sched_entries(ScheduleArray *pScheduleArray) +{ + ScheduleArray sortedByIdArray; + ScheduleEntry *pEntry; + ScheduleEntry *pEnd; + char timebase[32]; + int result; + + logInfo("schedule entry count: %d", pScheduleArray->count); + if (pScheduleArray->count == 0) + { + return 0; + } + + if ((result=sched_dup_array(pScheduleArray, &sortedByIdArray)) != 0) + { + return result; + } + + qsort(sortedByIdArray.entries, sortedByIdArray.count, + sizeof(ScheduleEntry), sched_cmp_by_id); + pEnd = sortedByIdArray.entries + sortedByIdArray.count; + for (pEntry=sortedByIdArray.entries; pEntrytime_base.hour == TIME_NONE) + { + strcpy(timebase, ""); + } + else + { + sprintf(timebase, "%02d:%02d:%02d", pEntry->time_base.hour, + pEntry->time_base.minute, pEntry->time_base.second); + } + logInfo("id: %u, time_base: %s, interval: %d, " + "new_thread: %s, task_func: %p, args: %p", + pEntry->id, timebase, pEntry->interval, + pEntry->new_thread ? "true" : "false", + pEntry->task_func, pEntry->func_args); + } + + free(sortedByIdArray.entries); + return 0; +} + static void *sched_thread_entrance(void *args) { ScheduleContext *pContext; @@ -322,6 +386,11 @@ static void *sched_thread_entrance(void *args) sched_deal_delay_tasks(pContext); sched_check_waiting(pContext); + if (print_all_entries) + { + print_all_sched_entries(&pContext->scheduleArray); + print_all_entries = false; + } if (pContext->scheduleArray.count == 0) //no schedule entry { sleep(1); @@ -848,3 +917,7 @@ static void sched_deal_delay_tasks(ScheduleContext *pContext) } } +uint32_t sched_generate_next_id() +{ + return ++next_id; +} diff --git a/src/sched_thread.h b/src/sched_thread.h index 1db1e33..2e93056 100644 --- a/src/sched_thread.h +++ b/src/sched_thread.h @@ -10,6 +10,7 @@ #define _SCHED_THREAD_H_ #include +#include #include #include "common_define.h" #include "fast_timer.h" @@ -19,7 +20,7 @@ typedef int (*TaskFunc) (void *args); typedef struct tagScheduleEntry { - int id; //the task id + uint32_t id; //the task id /* the time base to execute task, such as 00:00, interval is 3600, means execute the task every hour as 1:00, 2:00, 3:00 etc. */ @@ -108,6 +109,11 @@ extern volatile time_t g_current_time; //the current time #define get_current_time() (g_schedule_flag ? g_current_time: time(NULL)) +/** generate next id + * return: next id +*/ +uint32_t sched_generate_next_id(); + /** add schedule entries * parameters: * pScheduleArray: the schedule tasks @@ -165,9 +171,14 @@ int sched_start_ex(ScheduleArray *pScheduleArray, pthread_t *ptid, int sched_start(ScheduleArray *pScheduleArray, pthread_t *ptid, \ const int stack_size, bool * volatile pcontinue_flag); + +/** print all schedule entries for debug + * return: none +*/ +void sched_print_all_entries(); + #ifdef __cplusplus } #endif #endif - diff --git a/src/sockopt.c b/src/sockopt.c index 5ac6de9..696a446 100644 --- a/src/sockopt.c +++ b/src/sockopt.c @@ -533,9 +533,6 @@ int connectserverbyip_nb_ex(int sock, const char *server_ip, \ void *dest; int size; - memset(&addr, 0, sizeof(struct sockaddr_in)); - memset(&addr6, 0, sizeof(struct sockaddr_in6)); - if ((result=setsockaddrbyip(server_ip, server_port, &addr, &addr6, &dest, &size)) != 0) {