From 2a900c23aa14cc29e2c7d2101b181d508750e6de Mon Sep 17 00:00:00 2001 From: yuqing Date: Sat, 13 Sep 2014 22:10:04 +0800 Subject: [PATCH] add connection stats --- HISTORY | 1 + client/fdfs_monitor.c | 10 ++++++++-- storage/storage_global.c | 2 ++ storage/storage_global.h | 2 ++ storage/storage_nio.c | 1 + storage/storage_service.c | 19 +++++++++++++++++-- storage/tracker_client_thread.c | 9 +++++++++ tracker/tracker_global.c | 2 ++ tracker/tracker_global.h | 2 ++ tracker/tracker_nio.c | 1 + tracker/tracker_service.c | 25 +++++++++++++++++++++++-- tracker/tracker_types.h | 17 +++++++++++++++++ 12 files changed, 85 insertions(+), 6 deletions(-) diff --git a/HISTORY b/HISTORY index 7b05eef..197c06e 100644 --- a/HISTORY +++ b/HISTORY @@ -8,6 +8,7 @@ Version 5.04 2014-09-13 to the tracker server * fdfs_monitor support delete empty group * bug fixed: two tracker leaders occur in rare case + * add connection stats Version 5.03 2014-08-10 * network send and recv retry when error EINTR happen diff --git a/client/fdfs_monitor.c b/client/fdfs_monitor.c index 5afbe0e..1115587 100644 --- a/client/fdfs_monitor.c +++ b/client/fdfs_monitor.c @@ -417,8 +417,11 @@ static int list_storages(FDFSGroupStat *pGroupStat) "\t\tstorage_port = %d\n" \ "\t\tstorage_http_port = %d\n" \ "\t\tcurrent_write_path = %d\n" \ - "\t\tsource storage id= %s\n" \ - "\t\tif_trunk_server= %d\n" \ + "\t\tsource storage id = %s\n" \ + "\t\tif_trunk_server = %d\n" \ + "\t\tconnection.alloc_count = %d\n" \ + "\t\tconnection.current_count = %d\n" \ + "\t\tconnection.max_count = %d\n" \ "\t\ttotal_upload_count = %"PRId64"\n" \ "\t\tsuccess_upload_count = %"PRId64"\n" \ "\t\ttotal_append_count = %"PRId64"\n" \ @@ -478,6 +481,9 @@ static int list_storages(FDFSGroupStat *pGroupStat) pStorage->current_write_path, \ pStorage->src_id, \ pStorage->if_trunk_server, \ + pStorageStat->connection.alloc_count, \ + pStorageStat->connection.current_count, \ + pStorageStat->connection.max_count, \ pStorageStat->total_upload_count, \ pStorageStat->success_upload_count, \ pStorageStat->total_append_count, \ diff --git a/storage/storage_global.c b/storage/storage_global.c index 6a5954f..d0e6867 100644 --- a/storage/storage_global.c +++ b/storage/storage_global.c @@ -124,6 +124,8 @@ char g_exe_name[256] = {0}; struct storage_nio_thread_data *g_nio_thread_data = NULL; struct storage_dio_thread_data *g_dio_thread_data = NULL; +FDFSConnectionStat g_connection_stat = {0, 0}; + int storage_cmp_by_server_id(const void *p1, const void *p2) { return strcmp((*((FDFSStorageServer **)p1))->server.id, diff --git a/storage/storage_global.h b/storage/storage_global.h index 2c180eb..76be11b 100644 --- a/storage/storage_global.h +++ b/storage/storage_global.h @@ -171,6 +171,8 @@ extern char g_exe_name[256]; extern struct storage_nio_thread_data *g_nio_thread_data; //network io thread data extern struct storage_dio_thread_data *g_dio_thread_data; //disk io thread data +extern FDFSConnectionStat g_connection_stat; + int storage_cmp_by_server_id(const void *p1, const void *p2); #ifdef __cplusplus diff --git a/storage/storage_nio.c b/storage/storage_nio.c index 4f1daf6..15cd1de 100644 --- a/storage/storage_nio.c +++ b/storage/storage_nio.c @@ -68,6 +68,7 @@ void task_finish_clean_up(struct fast_task_info *pTask) memset(pTask->arg, 0, sizeof(StorageClientInfo)); free_queue_push(pTask); + __sync_fetch_and_sub(&g_connection_stat.current_count, 1); } static int set_recv_event(struct fast_task_info *pTask) diff --git a/storage/storage_service.c b/storage/storage_service.c index eb21fe2..5d0c3ff 100644 --- a/storage/storage_service.c +++ b/storage/storage_service.c @@ -1565,8 +1565,11 @@ static void storage_set_metadata_done_callback( \ int storage_service_init() { +#define ALLOC_CONNECTIONS_ONCE 256 + int result; int bytes; + int init_connections; struct storage_nio_thread_data *pThreadData; struct storage_nio_thread_data *pDataEnd; pthread_t tid; @@ -1594,8 +1597,11 @@ int storage_service_init() return result; } - if ((result=free_queue_init(g_max_connections, g_buff_size, \ - g_buff_size, sizeof(StorageClientInfo))) != 0) + init_connections = g_max_connections < ALLOC_CONNECTIONS_ONCE ? + g_max_connections : ALLOC_CONNECTIONS_ONCE; + if ((result=free_queue_init_ex(g_max_connections, init_connections, + ALLOC_CONNECTIONS_ONCE, g_buff_size, + g_buff_size, sizeof(StorageClientInfo))) != 0) { return result; } @@ -1843,6 +1849,15 @@ static void *accept_thread_entrance(void* arg) "errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); } + else + { + int current_connections; + current_connections = __sync_add_and_fetch(&g_connection_stat. + current_count, 1); + if (current_connections > g_connection_stat.max_count) { + g_connection_stat.max_count = current_connections; + } + } } return NULL; diff --git a/storage/tracker_client_thread.c b/storage/tracker_client_thread.c index 0eda313..2764167 100644 --- a/storage/tracker_client_thread.c +++ b/storage/tracker_client_thread.c @@ -25,6 +25,7 @@ #include "shared_func.h" #include "pthread_func.h" #include "sched_thread.h" +#include "fast_task_queue.h" #include "tracker_types.h" #include "tracker_proto.h" #include "tracker_client_thread.h" @@ -2131,6 +2132,14 @@ static int tracker_heart_beat(ConnectionInfo *pTrackerServer, \ { pStatBuff = (FDFSStorageStatBuff *)( \ out_buff + sizeof(TrackerHeader)); + + long2buff(free_queue_alloc_connections(), \ + pStatBuff->connection.sz_alloc_count); + long2buff(g_storage_stat.connection.current_count, \ + pStatBuff->connection.sz_current_count); + long2buff(g_storage_stat.connection.max_count, \ + pStatBuff->connection.sz_max_count); + long2buff(g_storage_stat.total_upload_count, \ pStatBuff->sz_total_upload_count); long2buff(g_storage_stat.success_upload_count, \ diff --git a/tracker/tracker_global.c b/tracker/tracker_global.c index 4caaa09..624aebe 100644 --- a/tracker/tracker_global.c +++ b/tracker/tracker_global.c @@ -73,3 +73,5 @@ bool g_http_servers_dirty = false; char g_exe_name[256] = {0}; #endif +FDFSConnectionStat g_connection_stat = {0, 0}; + diff --git a/tracker/tracker_global.h b/tracker/tracker_global.h index 4669c1e..d50786c 100644 --- a/tracker/tracker_global.h +++ b/tracker/tracker_global.h @@ -97,6 +97,8 @@ extern bool g_http_servers_dirty; extern char g_exe_name[256]; #endif +extern FDFSConnectionStat g_connection_stat; + #ifdef __cplusplus } #endif diff --git a/tracker/tracker_nio.c b/tracker/tracker_nio.c index 5072271..e48f6d0 100644 --- a/tracker/tracker_nio.c +++ b/tracker/tracker_nio.c @@ -72,6 +72,7 @@ void task_finish_clean_up(struct fast_task_info *pTask) memset(pTask->arg, 0, sizeof(TrackerClientInfo)); free_queue_push(pTask); + __sync_fetch_and_sub(&g_connection_stat.current_count, 1); } void recv_notify_read(int sock, short event, void *arg) diff --git a/tracker/tracker_service.c b/tracker/tracker_service.c index 252b826..3469c54 100644 --- a/tracker/tracker_service.c +++ b/tracker/tracker_service.c @@ -53,8 +53,10 @@ static void tracker_find_max_free_space_group(); int tracker_service_init() { +#define ALLOC_CONNECTIONS_ONCE 1024 int result; int bytes; + int init_connections; struct nio_thread_data *pThreadData; struct nio_thread_data *pDataEnd; pthread_t tid; @@ -77,8 +79,11 @@ int tracker_service_init() return result; } - if ((result=free_queue_init(g_max_connections, TRACKER_MAX_PACKAGE_SIZE,\ - TRACKER_MAX_PACKAGE_SIZE, sizeof(TrackerClientInfo))) != 0) + init_connections = g_max_connections < ALLOC_CONNECTIONS_ONCE ? + g_max_connections : ALLOC_CONNECTIONS_ONCE; + if ((result=free_queue_init_ex(g_max_connections, init_connections, + ALLOC_CONNECTIONS_ONCE, TRACKER_MAX_PACKAGE_SIZE, + TRACKER_MAX_PACKAGE_SIZE, sizeof(TrackerClientInfo))) != 0) { return result; } @@ -259,6 +264,15 @@ static void *accept_thread_entrance(void* arg) "errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); } + else + { + int current_connections; + current_connections = __sync_add_and_fetch(&g_connection_stat. + current_count, 1); + if (current_connections > g_connection_stat.max_count) { + g_connection_stat.max_count = current_connections; + } + } } return NULL; @@ -3506,6 +3520,13 @@ static int tracker_deal_storage_beat(struct fast_task_info *pTask) sizeof(TrackerHeader)); pStat = &(pClientInfo->pStorage->stat); + pStat->connection.alloc_count = \ + buff2long(pStatBuff->connection.sz_alloc_count); + pStat->connection.current_count = \ + buff2long(pStatBuff->connection.sz_current_count); + pStat->connection.max_count = \ + buff2long(pStatBuff->connection.sz_max_count); + pStat->total_upload_count = \ buff2long(pStatBuff->sz_total_upload_count); pStat->success_upload_count = \ diff --git a/tracker/tracker_types.h b/tracker/tracker_types.h index fdd116f..281991f 100644 --- a/tracker/tracker_types.h +++ b/tracker/tracker_types.h @@ -207,11 +207,23 @@ typedef struct /* last heart beat time */ time_t last_heart_beat_time; + + struct { + int alloc_count; + int current_count; + int max_count; + } connection; } FDFSStorageStat; /* struct for network transfering */ typedef struct { + struct { + char sz_alloc_count[4]; + char sz_current_count[4]; + char sz_max_count[4]; + } connection; + char sz_total_upload_count[8]; char sz_success_upload_count[8]; char sz_total_append_count[8]; @@ -440,5 +452,10 @@ typedef struct { bool if_leader; //if leader } TrackerRunningStatus; +typedef struct fdfs_connection_stat { + volatile int current_count; + volatile int max_count; +} FDFSConnectionStat; + #endif