| Index: trunk/extensions/PoolCounter/daemon/stats.c |
| — | — | @@ -1,5 +1,6 @@ |
| 2 | 2 | #include <stdio.h> |
| 3 | 3 | #include <string.h> |
| | 4 | +#include <math.h> |
| 4 | 5 | #include "stats.h" |
| 5 | 6 | |
| 6 | 7 | |
| — | — | @@ -7,11 +8,13 @@ |
| 8 | 9 | |
| 9 | 10 | #define COMMAND(item) + sizeof(#item) + 2 + MAX_COUNT_LEN |
| 10 | 11 | static char stats_buffer[ |
| 11 | | - sizeof("Uptime: 100000 days, 23h 59m 59s") + 2 |
| | 12 | + sizeof("Uptime: 100000 days, 23h 59m 59s") + 2 + 700 |
| 12 | 13 | #include "stats.list" |
| 13 | 14 | ]; |
| 14 | 15 | #undef COMMAND |
| 15 | 16 | |
| | 17 | +static size_t strtimeval(char* dst, size_t max, const char* title, const struct timeval* tv); |
| | 18 | + |
| 16 | 19 | const char* provide_stats(const char* type) |
| 17 | 20 | { |
| 18 | 21 | int seconds = time(NULL) - stats.start; |
| — | — | @@ -25,6 +28,14 @@ |
| 26 | 29 | n = sprintf( stats_buffer, "uptime: %u days, %dh %dm %ds\n", days, hours, minutes, seconds ); |
| 27 | 30 | |
| 28 | 31 | if ( !strcasecmp( type, "FULL" ) ) { |
| | 32 | + n += strtimeval( stats_buffer + n, 100, "total processing time", &stats.processing_time ); |
| | 33 | + n += strtimeval( stats_buffer + n, 100, "gained time", &stats.gained_time ); |
| | 34 | + n += strtimeval( stats_buffer + n, 100, "waiting time", &stats.waiting_time ); |
| | 35 | + n += strtimeval( stats_buffer + n, 100, "waiting time for me", &stats.waiting_time_for_me ); |
| | 36 | + n += strtimeval( stats_buffer + n, 100, "waiting time for anyone", &stats.waiting_time_for_anyone ); |
| | 37 | + n += strtimeval( stats_buffer + n, 100, "waiting time for good", &stats.waiting_time_for_good ); |
| | 38 | + n += strtimeval( stats_buffer + n, 100, "wasted timeout time", &stats.wasted_timeout_time ); |
| | 39 | + |
| 29 | 40 | #define COMMAND(item) n += sprintf( stats_buffer + n, #item ": %" PRcount "\n", stats.item ); |
| 30 | 41 | #include "stats.list" |
| 31 | 42 | #undef COMMAND |
| — | — | @@ -38,3 +49,40 @@ |
| 39 | 50 | } |
| 40 | 51 | return stats_buffer; |
| 41 | 52 | } |
| | 53 | + |
| | 54 | +/** |
| | 55 | + * Writes a timeval into the string. |
| | 56 | + * It is expected to get inlined. |
| | 57 | + * @return amount of bytes written |
| | 58 | + */ |
| | 59 | +static size_t strtimeval(char* dst, size_t max, const char* title, const struct timeval* tv) { |
| | 60 | + int n; |
| | 61 | + n = snprintf( dst, max, "%s: ", title ); |
| | 62 | + if ( max < n ) return 0; |
| | 63 | + |
| | 64 | + float seconds = tv->tv_sec + tv->tv_usec * 1.0e-6f; |
| | 65 | + if ( seconds >= 60 ) { |
| | 66 | + int minutes = seconds / 60; |
| | 67 | + seconds = fmodf( seconds, 60 ); |
| | 68 | + |
| | 69 | + if ( minutes >= 60 ) { |
| | 70 | + int hours = minutes / 60; |
| | 71 | + minutes %= 60; |
| | 72 | + |
| | 73 | + if ( hours >= 24 ) { |
| | 74 | + unsigned int days = hours / 24; |
| | 75 | + |
| | 76 | + n += snprintf( dst + n, max - n, "%u days ", days ); |
| | 77 | + if ( max < n ) return 0; |
| | 78 | + } |
| | 79 | + n += snprintf( dst + n, max - n, "%dh ", hours ); |
| | 80 | + if ( max < n ) return 0; |
| | 81 | + } |
| | 82 | + n += snprintf( dst + n, max - n, "%dm ", minutes ); |
| | 83 | + if ( max < n ) return 0; |
| | 84 | + } |
| | 85 | + n += snprintf( dst + n, max - n, "%fs\n", seconds ); |
| | 86 | + if ( max < n ) return 0; |
| | 87 | + |
| | 88 | + return n; |
| | 89 | +} |
| Index: trunk/extensions/PoolCounter/daemon/locks.h |
| — | — | @@ -2,6 +2,7 @@ |
| 3 | 3 | #define LOCKS_H |
| 4 | 4 | |
| 5 | 5 | #include <stdint.h> |
| | 6 | +#include <sys/time.h> |
| 6 | 7 | |
| 7 | 8 | /* This application uses several double linked lists. |
| 8 | 9 | * They are circular lists, new items are added on the end (ie. on prev) |
| — | — | @@ -34,6 +35,7 @@ |
| 35 | 36 | struct double_linked_list siblings; |
| 36 | 37 | struct PoolCounter* parent; |
| 37 | 38 | enum lock_state { UNLOCKED, WAITING, WAIT_ANY, PROCESSING } state; |
| | 39 | + struct timeval timeval; /* Stores the instante where it started waiting/processing */ |
| 38 | 40 | }; |
| 39 | 41 | |
| 40 | 42 | struct client_data; |
| Index: trunk/extensions/PoolCounter/daemon/stats.h |
| — | — | @@ -1,3 +1,7 @@ |
| | 2 | +#ifndef _BSD_SOURCE |
| | 3 | +#define _BSD_SOURCE |
| | 4 | +#endif |
| | 5 | +#include <sys/time.h> |
| 2 | 6 | |
| 3 | 7 | #include <time.h> |
| 4 | 8 | #include <stdint.h> |
| — | — | @@ -10,6 +14,14 @@ |
| 11 | 15 | struct stats { |
| 12 | 16 | time_t start; |
| 13 | 17 | |
| | 18 | + struct timeval processing_time; /* Total processing time */ |
| | 19 | + struct timeval gained_time; /* Processing time saved by waiting (pairs with waiting_time_for_good) */ |
| | 20 | + struct timeval waiting_time; /* Total time waiting until getting the lock (waiting_time_for_me + waiting_time_for_anyone) */ |
| | 21 | + struct timeval waiting_time_for_me; /* Total time waiting until getting a lock for that item */ |
| | 22 | + struct timeval waiting_time_for_anyone; /* Total time waiting until getting a lock for anyone */ |
| | 23 | + struct timeval waiting_time_for_good; /* Total time waiting until another worker did the work for us */ |
| | 24 | + struct timeval wasted_timeout_time; /* Waiting time of workers which finally hitted its timeout */ |
| | 25 | + |
| 14 | 26 | #define COMMAND(item) volatile count_t item; |
| 15 | 27 | #include "stats.list" |
| 16 | 28 | #undef COMMAND |
| — | — | @@ -21,3 +33,13 @@ |
| 22 | 34 | |
| 23 | 35 | #define incr_stats(item) stats.item++ |
| 24 | 36 | #define decr_stats(item) stats.item-- |
| | 37 | + |
| | 38 | +#define time_stats(lock,item) \ |
| | 39 | + do { \ |
| | 40 | + struct timeval tv; \ |
| | 41 | + if ( !timerisset( &now ) ) { \ |
| | 42 | + gettimeofday( &now, NULL ); \ |
| | 43 | + } \ |
| | 44 | + timersub( &now, &(lock)->timeval, &tv ); \ |
| | 45 | + timeradd( &tv, &stats.item, &stats.item ); \ |
| | 46 | + } while (0) |
| Index: trunk/extensions/PoolCounter/daemon/stats.list |
| — | — | @@ -10,4 +10,5 @@ |
| 11 | 11 | COMMAND(full_queues) /* Number of times locks were refused because the queue already had so many workers */ |
| 12 | 12 | COMMAND(lock_mismatch) /* Number of times a user tried to do a lock without a previous release */ |
| 13 | 13 | COMMAND(release_mismatch) /* Number of times a user tried to do a release without a previous lock */ |
| | 14 | +COMMAND(processed_count) /* Count of processing */ |
| 14 | 15 | |
| Index: trunk/extensions/PoolCounter/daemon/locks.c |
| — | — | @@ -1,12 +1,13 @@ |
| 2 | 2 | #define _XOPEN_SOURCE 500 |
| 3 | | -#include <string.h> |
| 4 | | -#include <stdlib.h> |
| 5 | | -#include <stdio.h> |
| | 3 | +#include "stats.h" |
| 6 | 4 | #include "locks.h" |
| 7 | 5 | #include "hash.h" |
| 8 | 6 | #include "client_data.h" |
| 9 | | -#include "stats.h" |
| 10 | 7 | |
| | 8 | +#include <string.h> |
| | 9 | +#include <stdlib.h> |
| | 10 | +#include <stdio.h> |
| | 11 | + |
| 11 | 12 | void init_lock(struct locks* l) { |
| 12 | 13 | l->state = UNLOCKED; |
| 13 | 14 | } |
| — | — | @@ -99,6 +100,7 @@ |
| 100 | 101 | |
| 101 | 102 | if ( pCounter->processing < workers ) { |
| 102 | 103 | l->state = PROCESSING; |
| | 104 | + gettimeofday( &l->timeval, NULL ); |
| 103 | 105 | pCounter->processing++; |
| 104 | 106 | incr_stats( processing_workers ); |
| 105 | 107 | DOUBLE_LLIST_ADD( &pCounter->working, &l->siblings ); |
| — | — | @@ -114,6 +116,7 @@ |
| 115 | 117 | DOUBLE_LLIST_ADD( &pCounter->for_them, &l->siblings ); |
| 116 | 118 | } |
| 117 | 119 | incr_stats( waiting_workers ); |
| | 120 | + gettimeofday( &l->timeval, NULL ); |
| 118 | 121 | |
| 119 | 122 | wait_time.tv_sec = timeout; |
| 120 | 123 | wait_time.tv_usec = 0; |
| — | — | @@ -139,6 +142,8 @@ |
| 140 | 143 | |
| 141 | 144 | void process_timeout(struct locks* l) { |
| 142 | 145 | if ( ( l->state == WAIT_ANY ) || ( l->state == WAITING ) ) { |
| | 146 | + struct timeval now = { 0 }; |
| | 147 | + time_stats( l, wasted_timeout_time ); |
| 143 | 148 | send_client( l, "TIMEOUT\n" ); |
| 144 | 149 | decr_stats( waiting_workers ); |
| 145 | 150 | remove_client_lock( l, 0 ); |
| — | — | @@ -146,18 +151,25 @@ |
| 147 | 152 | } |
| 148 | 153 | |
| 149 | 154 | void remove_client_lock(struct locks* l, int wakeup_anyones) { |
| | 155 | + struct timeval now = { 0 }; |
| | 156 | + |
| 150 | 157 | DOUBLE_LLIST_DEL(&l->siblings); |
| 151 | 158 | |
| 152 | 159 | if ( wakeup_anyones ) { |
| 153 | 160 | while ( l->parent->for_anyone.next != &l->parent->for_anyone ) { |
| | 161 | + time_stats( (struct locks*)l->parent->for_anyone.next, waiting_time_for_good ); |
| 154 | 162 | send_client( (void*)l->parent->for_anyone.next, "DONE\n" ); |
| 155 | 163 | remove_client_lock( (void*)l->parent->for_anyone.next, 0 ); |
| 156 | 164 | decr_stats( waiting_workers ); |
| | 165 | + time_stats( l, gained_time ); |
| 157 | 166 | } |
| 158 | 167 | } |
| 159 | 168 | |
| 160 | 169 | if ( l->state == PROCESSING ) { |
| 161 | 170 | /* One slot freed, wake up another worker */ |
| | 171 | + |
| | 172 | + time_stats( l, processing_time ); |
| | 173 | + incr_stats( processed_count ); |
| 162 | 174 | |
| 163 | 175 | /* Give priority to those which need to do it themselves, since |
| 164 | 176 | * the anyones will benefit from it, too. |
| — | — | @@ -167,17 +179,21 @@ |
| 168 | 180 | if ( l->parent->for_them.next != &l->parent->for_them ) { |
| 169 | 181 | /* The oldest waiting worker will be on next */ |
| 170 | 182 | new_owner = (struct locks*) l->parent->for_them.next; |
| | 183 | + time_stats( new_owner, waiting_time_for_me ); |
| 171 | 184 | } else if ( l->parent->for_anyone.next != &l->parent->for_anyone ) { |
| 172 | 185 | new_owner = (struct locks*) l->parent->for_anyone.next; |
| | 186 | + time_stats( new_owner, waiting_time_for_anyone ); |
| 173 | 187 | } |
| 174 | 188 | |
| 175 | 189 | if ( new_owner ) { |
| | 190 | + time_stats( new_owner, waiting_time ); |
| 176 | 191 | DOUBLE_LLIST_DEL( &new_owner->siblings ); |
| 177 | 192 | DOUBLE_LLIST_ADD( &l->parent->working, &new_owner->siblings ); |
| 178 | 193 | send_client( new_owner, "LOCKED\n" ); |
| 179 | 194 | new_owner->state = PROCESSING; |
| 180 | 195 | incr_stats( total_acquired ); |
| 181 | 196 | decr_stats( waiting_workers ); |
| | 197 | + gettimeofday( &l->timeval, NULL ); |
| 182 | 198 | } else { |
| 183 | 199 | l->parent->processing--; |
| 184 | 200 | decr_stats( processing_workers ); |
| Index: trunk/extensions/PoolCounter/daemon/Makefile |
| — | — | @@ -2,7 +2,7 @@ |
| 3 | 3 | DEFINES=-DENDIAN_BIG=0 -DENDIAN_LITTLE=1 -DHAVE_ACCEPT4=1 |
| 4 | 4 | CFLAGS=-Wall $(DEFINES) |
| 5 | 5 | OBJS=main.o client_data.o locks.o hash.o stats.o |
| 6 | | -LINK=-levent |
| | 6 | +LINK=-levent -lm |
| 7 | 7 | HEADERS=prototypes.h client_data.h stats.h stats.list |
| 8 | 8 | DESTDIR ?= |
| 9 | 9 | |