From d4c8b657bf6489d42c77ff06173a58b1f4483c61 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Wed, 4 Mar 2009 18:36:45 +0100 Subject: [PATCH] Better "!stats"; can display peak and current bandwidth usage. Network stats are now updated every minute, so that we have them handy for printing in logs, or by issuing the !stats command. Note: this checkin disables the once-per-hour stats logging. I do not know wether or not that is useful functionality. --- ChangeLog | 3 ++- src/commands.c | 14 +++++++++++--- src/hub.h | 14 ++++++++++++++ src/main.c | 2 +- src/network.c | 16 +--------------- src/uhub.h | 2 +- src/usermanager.c | 30 ++++++++++++++++++++---------- src/usermanager.h | 3 ++- 8 files changed, 52 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6c62b63..b5166ac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 0.2.6: -- Better uptime command formatting. +- Better "!uptime" command formatting. +- Better "!stats"; can display peak and current bandwidth usage. 0.2.5-3487: diff --git a/src/commands.c b/src/commands.c index bcfc437..d976ee8 100644 --- a/src/commands.c +++ b/src/commands.c @@ -38,9 +38,17 @@ static int command_stats(struct user* user, const char* message) if (user->credentials < cred_super) return command_access_denied(user); - - char temp[64]; - snprintf(temp, 64, "*** Stats: %u users, peak %u", (unsigned int) user->hub->users->count, (unsigned int) user->hub->users->count_peak); + + char temp[128]; + + snprintf(temp, 128, "*** Stats: %zu users, peak: %zu. Network (up/down): %d/%d KB/s, peak: %d/%d KB/s", + user->hub->users->count, + user->hub->users->count_peak, + (int) user->hub->stats.net_tx / 1024, + (int) user->hub->stats.net_rx / 1024, + (int) user->hub->stats.net_tx_peak / 1024, + (int) user->hub->stats.net_rx_peak / 1024); + char* buffer = adc_msg_escape(temp); command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6); adc_msg_add_argument(command, buffer); diff --git a/src/hub.h b/src/hub.h index 319572c..5ef99f8 100644 --- a/src/hub.h +++ b/src/hub.h @@ -66,6 +66,19 @@ enum hub_state hub_status_disabled = 5, /**<<<"Hub is disabled (Running, but not accepting users) */ }; +/** + * Always updated each minute. + */ +struct hub_stats +{ + size_t net_tx; + size_t net_rx; + size_t net_tx_peak; + size_t net_rx_peak; + size_t net_tx_total; + size_t net_rx_total; +}; + struct hub_info { int fd_tcp; @@ -77,6 +90,7 @@ struct hub_info #ifdef ADC_UDP_OPERATION struct event ev_datagram; #endif + struct hub_stats stats; struct event_queue* queue; struct hub_config* config; struct user_manager* users; diff --git a/src/main.c b/src/main.c index 505fdd6..1745efc 100644 --- a/src/main.c +++ b/src/main.c @@ -70,7 +70,7 @@ void hub_handle_signal(int fd, short events, void* arg) case SIGUSR2: hub_log(log_trace, "hub_handle_signal(): caught SIGUSR2"); { - user_manager_stats(hub); + user_manager_print_stats(hub); } break; diff --git a/src/network.c b/src/network.c index 064f371..469e773 100644 --- a/src/network.c +++ b/src/network.c @@ -591,19 +591,6 @@ void net_stats_get(struct net_statistics** intermediate, struct net_statistics** } -void net_stats_report() -{ - int factor = (time(NULL) - stats.timestamp); - if (!factor) factor++; - - hub_log(log_info, "Statistics NET: tx=%d KB/s, rx=%d KB/s, (acc=%d/cls=%d/err=%d)", - (int) ((stats.tx / factor) / 1024), - (int) ((stats.rx / factor) / 1024), - (int) stats.accept, - (int) stats.closed, - (int) stats.errors); -} - void net_stats_reset() { stats_total.tx += stats.tx; @@ -619,8 +606,7 @@ void net_stats_reset() int net_stats_timeout() { - /* FIXME: Configurable time for dumping network statistics */ - return (time(NULL) - stats.timestamp > 60) ? 1 : 0; + return (difftime(time(NULL), stats.timestamp) > TIMEOUT_STATS) ? 1 : 0; } diff --git a/src/uhub.h b/src/uhub.h index d8c429e..459c78c 100644 --- a/src/uhub.h +++ b/src/uhub.h @@ -113,7 +113,7 @@ #define TIMEOUT_HANDSHAKE 30 #define TIMEOUT_SENDQ 120 #define TIMEOUT_IDLE 7200 -#define TIMEOUT_STATS 3600 +#define TIMEOUT_STATS 60 #define MAX_CLIENTS 512 #define MAX_CID_LEN 39 diff --git a/src/usermanager.c b/src/usermanager.c index 8fbdd93..362fcd9 100644 --- a/src/usermanager.c +++ b/src/usermanager.c @@ -39,30 +39,40 @@ static void clear_user_list_callback(void* ptr) } -void user_manager_stats(struct hub_info* hub) +void user_manager_update_stats(struct hub_info* hub) { - int factor = 0; + const int factor = TIMEOUT_STATS; struct net_statistics* total; struct net_statistics* intermediate; net_stats_get(&intermediate, &total); - - factor = (time(NULL) - intermediate->timestamp); - if (!factor) factor++; - hub_log(log_info, "Statistics users=%zu, net_tx=%d KB/s, net_rx=%d KB/s", - hub->users->count, - (int) ((intermediate->tx / factor) / 1024), - (int) ((intermediate->rx / factor) / 1024)); + hub->stats.net_tx = (intermediate->tx / factor); + hub->stats.net_rx = (intermediate->rx / factor); + hub->stats.net_tx_peak = MAX(hub->stats.net_tx, hub->stats.net_tx_peak); + hub->stats.net_tx_peak = MAX(hub->stats.net_rx, hub->stats.net_rx_peak); + hub->stats.net_tx_total = total->tx; + hub->stats.net_rx_total = total->rx; net_stats_reset(); } +void user_manager_print_stats(struct hub_info* hub) +{ + hub_log(log_info, "Statistics users=%zu (peak_users=%zu), net_tx=%d KB/s, net_rx=%d KB/s (peak_tx=%d KB/s, peak_rx=%d KB/s)", + hub->users->count, + hub->users->count_peak, + (int) hub->stats.net_tx / 1024, + (int) hub->stats.net_rx / 1024, + (int) hub->stats.net_tx_peak / 1024, + (int) hub->stats.net_rx_peak / 1024); +} + static void timer_statistics(int fd, short ev, void *arg) { struct hub_info* hub = (struct hub_info*) arg; struct timeval timeout = { TIMEOUT_STATS, 0 }; - user_manager_stats(hub); + user_manager_update_stats(hub); evtimer_set(&hub->ev_timer, timer_statistics, hub); evtimer_add(&hub->ev_timer, &timeout); } diff --git a/src/usermanager.h b/src/usermanager.h index 07207b6..348fc5e 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -45,7 +45,8 @@ extern void user_manager_shutdown(struct hub_info* hub); /** * Generate statistics for logfiles. */ -extern void user_manager_stats(struct hub_info* hub); +extern void user_manager_update_stats(struct hub_info* hub); +extern void user_manager_print_stats(struct hub_info* hub); /** * Add a new user to the user manager.