From: Arran Cudbard-Bell Date: Tue, 28 Aug 2012 15:26:10 +0000 (+0100) Subject: Add entry hit stats X-Git-Tag: release_3_0_0_beta0~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d24c41f0921ad5ca449cd9a1aec3a45a6db5457;p=thirdparty%2Ffreeradius-server.git Add entry hit stats --- diff --git a/raddb/mods-available/cache b/raddb/mods-available/cache index d6271ecf743..07c0722ebff 100644 --- a/raddb/mods-available/cache +++ b/raddb/mods-available/cache @@ -54,6 +54,11 @@ cache { # # Note: expired entries will still be removed. + # If yes the following attributes will be added to the request list: + # * Cache-Entry-Hits - The number of times this entry has been + # retrieved. + add-stats = no + # The list of attributes to cache for a particular key. # Each key gets the same set of cached attributes. # The attributes are dynamically expanded at run time. diff --git a/share/dictionary.freeradius.internal b/share/dictionary.freeradius.internal index b67c3da859d..292a8d25710 100644 --- a/share/dictionary.freeradius.internal +++ b/share/dictionary.freeradius.internal @@ -223,6 +223,7 @@ ATTRIBUTE Stripped-User-Domain 1138 string ATTRIBUTE Called-Station-SSID 1139 string ATTRIBUTE Cache-TTL 1140 integer ATTRIBUTE Cache-Status-Only 1141 integer +ATTRIBUTE Cache-Entry-Hits 1142 integer VALUE Cache-Status-Only no 0 VALUE Cache-Status-Only yes 1 diff --git a/src/include/radius.h b/src/include/radius.h index bc3931ba7d1..47cd17156c4 100644 --- a/src/include/radius.h +++ b/src/include/radius.h @@ -240,6 +240,7 @@ #define PW_CACHED_SESSION_POLICY 1135 #define PW_CACHE_TTL 1140 #define PW_CACHE_STATUS_ONLY 1141 +#define PW_CACHE_ENTRY_HITS 1142 /* * Integer Translations diff --git a/src/modules/rlm_cache/rlm_cache.c b/src/modules/rlm_cache/rlm_cache.c index 614c3dd4b84..72d4189c5a6 100644 --- a/src/modules/rlm_cache/rlm_cache.c +++ b/src/modules/rlm_cache/rlm_cache.c @@ -40,6 +40,7 @@ typedef struct rlm_cache_t { char *key; int ttl; int epoch; + int stats; CONF_SECTION *cs; rbtree_t *cache; fr_heap_t *heap; @@ -48,6 +49,7 @@ typedef struct rlm_cache_t { typedef struct rlm_cache_entry_t { const char *key; int offset; + long long int hits; time_t created; time_t expires; VALUE_PAIR *control; @@ -98,7 +100,8 @@ static int cache_heap_cmp(const void *one, const void *two) /* * Merge a cached entry into a REQUEST. */ -static void cache_merge(REQUEST *request, rlm_cache_entry_t *c) +static void cache_merge(rlm_cache_t *inst, REQUEST *request, + rlm_cache_entry_t *c) { VALUE_PAIR *vp; @@ -122,6 +125,15 @@ static void cache_merge(REQUEST *request, rlm_cache_entry_t *c) pairmove(&request->reply->vps, &vp); pairfree(&vp); } + + if (inst->stats) { + vp = paircreate(PW_CACHE_ENTRY_HITS, 0, PW_TYPE_INTEGER); + rad_assert(vp != NULL); + + vp->vp_integer = c->hits; + + pairadd(&request->packet->vps, vp); + } } @@ -188,6 +200,7 @@ static rlm_cache_entry_t *cache_find(rlm_cache_t *inst, REQUEST *request, c->expires = request->timestamp + ttl; DEBUG("rlm_cache: Adding %d to the TTL", ttl); } + c->hits++; return c; } @@ -428,6 +441,8 @@ static const CONF_PARSER module_config[] = { offsetof(rlm_cache_t, ttl), NULL, "500" }, { "epoch", PW_TYPE_INTEGER, offsetof(rlm_cache_t, epoch), NULL, "0" }, + { "add-stats", PW_TYPE_BOOLEAN, + offsetof(rlm_cache_t, stats), NULL, "no" }, { NULL, -1, 0, NULL, NULL } /* end the list */ }; @@ -576,14 +591,14 @@ static int cache_it(void *instance, REQUEST *request) } if (c) { - cache_merge(request, c); + cache_merge(inst, request, c); return RLM_MODULE_OK; } c = cache_add(inst, request, buffer); if (!c) return RLM_MODULE_NOOP; - cache_merge(request, c); + cache_merge(inst, request, c); return RLM_MODULE_UPDATED; }