Add entry hit stats
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 28 Aug 2012 15:26:10 +0000 (16:26 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 28 Aug 2012 16:07:26 +0000 (17:07 +0100)
raddb/modules/cache
share/dictionary.freeradius.internal
src/include/radius.h
src/modules/rlm_cache/rlm_cache.c

index 902ae35..6508140 100644 (file)
@@ -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.
index 62a239a..19266de 100644 (file)
@@ -219,8 +219,9 @@ ATTRIBUTE   MS-CHAP-Password                        1133    string
 ATTRIBUTE      Packet-Transmit-Counter                 1134    integer
 ATTRIBUTE      Cached-Session-Policy                   1135    string
 
-\ATTRIBUTE     Cache-TTL                               1136    integer
-ATTRIBUTE      Cache-Status-Only                       1137    integer
+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
index d143f3c..3e06aed 100644 (file)
 
 #define PW_CACHE_TTL                   1140
 #define PW_CACHE_STATUS_ONLY           1141
+#define PW_CACHE_ENTRY_HITS            1142
 
 /*
  *     Integer Translations
index fce9a79..2fce9fe 100644 (file)
@@ -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;
 }
@@ -414,6 +427,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 */
 };
@@ -562,14 +577,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;
 }