From d579693f3d406e1fc0b11b1d1a54b64f49907fde Mon Sep 17 00:00:00 2001 From: gardners Date: Mon, 25 Jun 2012 14:35:55 +0930 Subject: [PATCH] added stats generation and summarisation to take place of older excessively chatty timing gathering. --- fdqueue.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++-------- overlay.c | 3 ++ serval.h | 2 + 3 files changed, 129 insertions(+), 22 deletions(-) diff --git a/fdqueue.c b/fdqueue.c index 637dfee5..6e6fdbca 100644 --- a/fdqueue.c +++ b/fdqueue.c @@ -20,11 +20,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "serval.h" #include +struct callback_stats { + long long max_time; + long long total_time; + int calls; +}; + #define MAX_ALARMS 128 typedef struct callback_alarm { void (*func)(); long long next_alarm; long long repeat_every; + + struct callback_stats stats; } callback_alarm; callback_alarm alarms[MAX_ALARMS]; @@ -34,6 +42,9 @@ int alarmcount=0; struct pollfd fds[MAX_WATCHED_FDS]; int fdcount=0; void(*fd_functions[MAX_WATCHED_FDS])(int fd); +struct callback_stats fd_stats[MAX_WATCHED_FDS]; + +struct callback_stats poll_stats={0,0,0}; /* @PGS/20120615 */ int last_valid=0; @@ -79,6 +90,11 @@ int fd_watch(int fd,void (*func)(int fd),int events) fds[fdcount].fd=fd; fds[fdcount++].events=events; + if (func!=fd_functions[fd]) { + fd_stats[fd].max_time=0; + fd_stats[fd].total_time=0; + fd_stats[fd].calls=0; + } fd_functions[fd]=func; return 0; @@ -126,6 +142,11 @@ int fd_setalarm(void (*func),long long first_alarm_in,int repeat_every) return 0; } else { /* Create new alarm, or update existing one */ + if (alarms[i].func!=func) { + alarms[i].stats.calls=0; + alarms[i].stats.max_time=0; + alarms[i].stats.total_time=0; + } alarms[i].func=func; alarms[i].next_alarm=overlay_gettime_ms()+first_alarm_in; alarms[i].repeat_every=repeat_every; @@ -134,6 +155,13 @@ int fd_setalarm(void (*func),long long first_alarm_in,int repeat_every) } } +void fd_update_stats(struct callback_stats *s,long long elapsed) +{ + s->total_time+=elapsed; + if (elapsed>s->max_time) s->max_time=elapsed; + s->calls++; +} + int fd_checkalarms() { long long now=overlay_gettime_ms(); @@ -144,12 +172,12 @@ int fd_checkalarms() TIMING_PAUSE(); for(i=0;i0) { for(i=0;itotal_time+=a->total_time; + total->calls+=a->calls; + if (a->max_time>total->max_time) total->max_time=a->max_time; + return 0; +} + +int fd_showstat(struct callback_stats *total, struct callback_stats *a, char *msg) +{ + WHYF("%lldms (%2.1f%%) in %d calls (max %lldms, avg %.1fms) : %s", + a->total_time,a->total_time*100.0/total->total_time, + a->calls, + a->max_time,a->total_time*1.00/a->calls, + msg); + return 0; +} + +int fd_clearstat(struct callback_stats *s) +{ + s->calls=0; + s->max_time=0; + s->total_time=0; + return 0; +} + +int fd_clearstats() +{ + int i; + fd_clearstat(&poll_stats); + for(i=0;i