You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2015/02/01 05:09:37 UTC

trafficserver git commit: TS-1264: account for overhead in LRU cache. Add RamCache regression test. Fix hit accounting for history in CLFUS Ram cache.

Repository: trafficserver
Updated Branches:
  refs/heads/master 9d9670fdd -> 893d58d9f


TS-1264: account for overhead in LRU cache.
Add RamCache regression test.
Fix hit accounting for history in CLFUS Ram cache.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/893d58d9
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/893d58d9
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/893d58d9

Branch: refs/heads/master
Commit: 893d58d9f7b21689fb185f856f31c56d27636c60
Parents: 9d9670f
Author: John Plevyak <jp...@apache.org>
Authored: Sat Jan 31 16:44:52 2015 -0800
Committer: John Plevyak <jp...@acm.org>
Committed: Sat Jan 31 19:48:35 2015 -0800

----------------------------------------------------------------------
 iocore/cache/CacheTest.cc     | 52 ++++++++++++++++++++++++++++++++++++++
 iocore/cache/RamCacheCLFUS.cc | 18 ++++++-------
 iocore/cache/RamCacheLRU.cc   | 10 +++++---
 3 files changed, 67 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/893d58d9/iocore/cache/CacheTest.cc
----------------------------------------------------------------------
diff --git a/iocore/cache/CacheTest.cc b/iocore/cache/CacheTest.cc
index 6ca424c..9b5f4ff 100644
--- a/iocore/cache/CacheTest.cc
+++ b/iocore/cache/CacheTest.cc
@@ -25,6 +25,9 @@
 #include "P_Cache.h"
 #include "P_CacheTest.h"
 #include "api/ts/ts.h"
+#include <vector>
+
+using namespace std;
 
 CacheTestSM::CacheTestSM(RegressionTest *t) :
   RegressionSM(t),
@@ -487,3 +490,52 @@ REGRESSION_TEST(cache_disk_replacement_stability)(RegressionTest *t, int level,
   hr1.vols = 0;
   hr2.vols = 0;
 }
+
+bool test_RamCache(RegressionTest *t, RamCache *cache) {
+  bool pass = true;
+  CacheKey key;
+  Vol *vol = theCache->key_to_vol(&key, "example.com", sizeof("example.com")-1);
+  cache->init(1 << 20, vol);
+  vector<Ptr<IOBufferData>> data;
+  for (int l = 0; l < 10; l++) {
+    for (int i = 0; i < 200; i++) {
+      IOBufferData *d = new IOBufferData;
+      d->alloc(BUFFER_SIZE_INDEX_16K);
+      data.emplace_back(d);
+      INK_MD5 md5;
+      md5.u64[0] = ((uint64_t)i << 32) + i;
+      md5.u64[1] = ((uint64_t)i << 32) + i;
+      cache->put(&md5, data[i], 1 << 15);
+      // More hits for the first 10.
+      for (int j = 0; j <= i && j < 10; j++) {
+        INK_MD5 md5;
+        md5.u64[0] = ((uint64_t)j << 32) + j;
+        md5.u64[1] = ((uint64_t)j << 32) + j;
+        Ptr<IOBufferData> data;
+        cache->get(&md5, &data);
+      }
+    }
+  }
+  for (int i = 0; i < 10; i++) {
+    INK_MD5 md5;
+    md5.u64[0] = ((uint64_t)i << 32) + i;
+    md5.u64[1] = ((uint64_t)i << 32) + i;
+    Ptr<IOBufferData> data;
+    if (!cache->get(&md5, &data)) pass = false;
+  }
+  rprintf(t, "RamCache Test Done");
+  return pass;
+}
+
+REGRESSION_TEST(ram_cache)(RegressionTest *t, int /* level ATS_UNUSED */, int *pstatus) {
+  if (cacheProcessor.IsCacheEnabled() != CACHE_INITIALIZED) {
+    rprintf(t, "cache not initialized");
+    *pstatus = REGRESSION_TEST_FAILED;
+    return;
+  }
+  if (!test_RamCache(t, new_RamCacheLRU()) ||
+      !test_RamCache(t, new_RamCacheCLFUS()))
+    *pstatus = REGRESSION_TEST_FAILED;
+  else
+    *pstatus = REGRESSION_TEST_PASSED;
+}

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/893d58d9/iocore/cache/RamCacheCLFUS.cc
----------------------------------------------------------------------
diff --git a/iocore/cache/RamCacheCLFUS.cc b/iocore/cache/RamCacheCLFUS.cc
index ce4f332..8130255 100644
--- a/iocore/cache/RamCacheCLFUS.cc
+++ b/iocore/cache/RamCacheCLFUS.cc
@@ -93,7 +93,7 @@ struct RamCacheCLFUS : public RamCache {
   void victimize(RamCacheCLFUSEntry *e);
   void move_compressed(RamCacheCLFUSEntry *e);
   RamCacheCLFUSEntry *destroy(RamCacheCLFUSEntry *e);
-  void requeue_victims(RamCacheCLFUS *c, Que(RamCacheCLFUSEntry, lru_link) &victims);
+  void requeue_victims(Que(RamCacheCLFUSEntry, lru_link) &victims);
   void tick(); // move CLOCK on history
   RamCacheCLFUS(): max_bytes(0), bytes(0), objects(0), vol(0), history(0), ibuckets(0), nbuckets(0), bucket(0),
               seen(0), ncompressed(0), compressed(0) { }
@@ -290,7 +290,7 @@ void RamCacheCLFUS::tick() {
   RamCacheCLFUSEntry *e = lru[1].dequeue();
   if (!e)
     return;
-  e->hits <<= 1;
+  e->hits >>= 1;
   if (e->hits) {
     e->hits = REQUEUE_HITS(e->hits);
     lru[1].enqueue(e);
@@ -485,14 +485,14 @@ RamCacheCLFUS::compress_entries(EThread *thread, int do_at_most)
 }
 
 void
-RamCacheCLFUS::requeue_victims(RamCacheCLFUS *c, Que(RamCacheCLFUSEntry, lru_link) &victims)
+RamCacheCLFUS::requeue_victims(Que(RamCacheCLFUSEntry, lru_link) &victims)
 {
   RamCacheCLFUSEntry *victim = 0;
   while ((victim = victims.dequeue())) {
-    c->bytes += victim->size + ENTRY_OVERHEAD;
+    bytes += victim->size + ENTRY_OVERHEAD;
     CACHE_SUM_DYN_STAT_THREAD(cache_ram_cache_bytes_stat, victim->size);
     victim->hits = REQUEUE_HITS(victim->hits);
-    c->lru[0].enqueue(victim);
+    lru[0].enqueue(victim);
   }
 }
 
@@ -564,7 +564,7 @@ RamCacheCLFUS::put(INK_MD5 *key, IOBufferData *data, uint32_t len, bool copy, ui
         goto Linsert;
       if (e)
         lru[1].enqueue(e);
-      requeue_victims(this, victims);
+      requeue_victims(victims);
       DDebug("ram_cache", "put %X %d %d NO VICTIM", key->slice32(3), auxkey1, auxkey2);
       return 0;
     }
@@ -575,14 +575,14 @@ RamCacheCLFUS::put(INK_MD5 *key, IOBufferData *data, uint32_t len, bool copy, ui
       compressed = 0;
     else
       ncompressed--;
-    victim->hits <<= 1;
+    victim->hits >>= 1;
     tick();
     if (!e)
       goto Lhistory;
     else { // e from history
       DDebug("ram_cache_compare", "put %f %f", CACHE_VALUE(victim), CACHE_VALUE(e));
       if (bytes + victim->size + size > max_bytes && CACHE_VALUE(victim) > CACHE_VALUE(e)) {
-        requeue_victims(this, victims);
+        requeue_victims(victims);
         lru[1].enqueue(e);
         DDebug("ram_cache", "put %X %d %d size %d INC %" PRId64" HISTORY",
                key->slice32(3), auxkey1, auxkey2, e->size, e->hits);
@@ -637,7 +637,7 @@ Linsert:
   DDebug("ram_cache", "put %X %d %d size %d INSERTED", key->slice32(3), auxkey1, auxkey2, e->size);
   return 1;
 Lhistory:
-  requeue_victims(this, victims);
+  requeue_victims(victims);
   check_accounting(this);
   e = THREAD_ALLOC(ramCacheCLFUSEntryAllocator, this_ethread());
   e->key = *key;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/893d58d9/iocore/cache/RamCacheLRU.cc
----------------------------------------------------------------------
diff --git a/iocore/cache/RamCacheLRU.cc b/iocore/cache/RamCacheLRU.cc
index c85df38..fc3c949 100644
--- a/iocore/cache/RamCacheLRU.cc
+++ b/iocore/cache/RamCacheLRU.cc
@@ -32,6 +32,8 @@ struct RamCacheLRUEntry {
   Ptr<IOBufferData> data;
 };
 
+#define ENTRY_OVERHEAD 128 // per-entry overhead to consider when computing sizes
+
 struct RamCacheLRU: public RamCache {
   int64_t max_bytes;
   int64_t bytes;
@@ -127,8 +129,8 @@ RamCacheLRUEntry * RamCacheLRU::remove(RamCacheLRUEntry *e) {
   uint32_t b = e->key.slice32(3) % nbuckets;
   bucket[b].remove(e);
   lru.remove(e);
-  bytes -= e->data->block_size();
-  CACHE_SUM_DYN_STAT_THREAD(cache_ram_cache_bytes_stat, -e->data->block_size());
+  bytes -= ENTRY_OVERHEAD + e->data->block_size();
+  CACHE_SUM_DYN_STAT_THREAD(cache_ram_cache_bytes_stat, -(ENTRY_OVERHEAD + e->data->block_size()));
   DDebug("ram_cache", "put %X %d %d FREED", e->key.slice32(3), e->auxkey1, e->auxkey2);
   e->data = NULL;
   THREAD_FREE(e, ramCacheLRUEntryAllocator, this_thread());
@@ -171,9 +173,9 @@ int RamCacheLRU::put(INK_MD5 *key, IOBufferData *data, uint32_t len, bool, uint3
   e->data = data;
   bucket[i].push(e);
   lru.enqueue(e);
-  bytes += data->block_size();
+  bytes += ENTRY_OVERHEAD + data->block_size();
   objects++;
-  CACHE_SUM_DYN_STAT_THREAD(cache_ram_cache_bytes_stat, data->block_size());
+  CACHE_SUM_DYN_STAT_THREAD(cache_ram_cache_bytes_stat, ENTRY_OVERHEAD + data->block_size());
   while (bytes > max_bytes) {
     RamCacheLRUEntry *ee = lru.dequeue();
     if (ee)