Browse Source

[trac744] Messages for the hotspot cache

Michal 'vorner' Vaner 14 years ago
parent
commit
1151e49bce

+ 27 - 1
src/lib/datasrc/cache.cc

@@ -24,6 +24,7 @@
 #include <list>
 
 #include <datasrc/cache.h>
+#include <datasrc/logger.h>
 
 using namespace std;
 using namespace isc::dns;
@@ -204,16 +205,23 @@ public:
 // HotCacheImpl constructor
 HotCacheImpl::HotCacheImpl(int slots, bool enabled) :
     enabled_(enabled), slots_(slots), count_(0)
-{}
+{
+    logger.debug(DBG_TRACE_BASIC, DATASRC_CACHE_CREAT);
+}
 
 // Insert a cache node into the cache
 inline void
 HotCacheImpl::insert(const CacheNodePtr node) {
+    if (logger.isDebugEnabled(DBG_TRACE_DATA)) {
+        logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_INSERT,
+                       node->getRRset()->getName().toText().c_str());
+    }
     std::map<Question, CacheNodePtr>::const_iterator iter;
     iter = map_.find(node->question);
     if (iter != map_.end()) {
         CacheNodePtr old = iter->second;
         if (old && old->isValid()) {
+            logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_OLD_FOUND);
             remove(old);
         }
     }
@@ -225,6 +233,7 @@ HotCacheImpl::insert(const CacheNodePtr node) {
     ++count_;
 
     if (slots_ != 0 && count_ > slots_) {
+        logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_FULL);
         remove(lru_.back());
     }
 }
@@ -245,6 +254,10 @@ HotCacheImpl::promote(CacheNodePtr node) {
 // Remove a node from the LRU list and the map
 void
 HotCacheImpl::remove(ConstCacheNodePtr node) {
+    if (logger.isDebugEnabled(DBG_TRACE_DATA)) {
+        logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_REMOVE,
+                       node->getRRset()->getName().toText().c_str());
+    }
     lru_.erase(node->lru_entry_);
     map_.erase(node->question);
     --count_;
@@ -257,6 +270,7 @@ HotCache::HotCache(const int slots) {
 
 // HotCache destructor
 HotCache::~HotCache() {
+    logger.debug(DBG_TRACE_BASIC, DATASRC_CACHE_DESTROY);
     delete impl_;
 }
 
@@ -300,21 +314,26 @@ HotCache::retrieve(const Name& n, const RRClass& c, const RRType& t,
         return (false);
     }
 
+    logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_LOOKUP, n.toText().c_str());
+
     std::map<Question, CacheNodePtr>::const_iterator iter;
     iter = impl_->map_.find(Question(n, c, t));
     if (iter == impl_->map_.end()) {
+        logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_NOT_FOUND);
         return (false);
     }
 
     CacheNodePtr node = iter->second;
 
     if (node->isValid()) {
+        logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_FOUND);
         impl_->promote(node);
         rrset = node->getRRset();
         flags = node->getFlags();
         return (true);
     }
 
+    logger.debug(DBG_TRACE_DATA, DATASRC_CACHE_EXPIRED);
     impl_->remove(node);
     return (false);
 }
@@ -328,6 +347,8 @@ HotCache::setSlots(const int slots) {
         return;
     }
 
+    logger.info(DATASRC_CACHE_SLOTS, slots);
+
     while (impl_->slots_ != 0 && impl_->count_ > impl_->slots_) {
         impl_->remove(impl_->lru_.back());
     }
@@ -343,6 +364,11 @@ HotCache::getSlots() const {
 void
 HotCache::setEnabled(const bool e) {
     impl_->enabled_ = e;
+    if (e) {
+        logger.info(DATASRC_CACHE_ENABLE);
+    } else {
+        logger.info(DATASRC_CACHE_DISABLE);
+    }
 }
 
 /// Indicate whether the cache is enabled

+ 1 - 5
src/lib/datasrc/logger.cc

@@ -17,11 +17,7 @@
 namespace isc {
 namespace datasrc {
 
-isc::log::Logger &
-logger() {
-    static isc::log::Logger theLogger("datasrc");
-    return (theLogger);
-}
+isc::log::Logger logger("datasrc");
 
 }
 }

+ 9 - 3
src/lib/datasrc/logger.h

@@ -28,9 +28,15 @@
 namespace isc {
 namespace datasrc {
 
-/// \brief Get the logger
-isc::log::Logger&
-logger();
+/// \brief The logger for this library
+extern isc::log::Logger logger;
+
+enum {
+    /// \brief Trace basic operations
+    DBG_TRACE_BASIC = 10,
+    /// \brief Trace data changes and lookups as well
+    DBG_TRACE_DATA = 20
+};
 
 }
 }

+ 50 - 0
src/lib/datasrc/messagedef.mes

@@ -17,4 +17,54 @@ $NAMESPACE isc::datasrc
 
 # \brief Messages for the data source library
 
+CACHE_CREAT Creating the hotspot cache
++ Debug information that the hotspot cache was created at startup.
 
+CACHE_DESTROY Destroying the hotspot cache
++ Debug information. The hotspot cache is being destroyed.
+
+CACHE_INSERT Inserting item '%s' into the cache
++ Debug information. It means a new item is being inserted into the hotspot
++ cache.
+
+CACHE_OLD_FOUND Older instance of cache item found, replacing
++ Debug information. While inserting an item into the hotspot cache, an older
++ instance of an item with the same name was found. The old instance will be
++ removed. This should be directly followed by CACHE_REMOVE.
+
+CACHE_FULL Cache is full, dropping oldest
++ Debug information. After inserting an item into the hotspot cache, the
++ maximum number of items was exceeded, so the least recently used item will
++ be dropped. This should be directly followed by CACHE_REMOVE.
+
+CACHE_REMOVE Removing '%s' from the cache
++ Debug information. An item is being removed from the hotspot cache.
+
+CACHE_LOOKUP Looking up '%s' in the cache
++ Debug information. We are trying to look up an item in the hotspot cache.
++ Further progress and result will follow.
+
+CACHE_NOT_FOUND The item was not found
++ Debug information. The item we tried to look in the last CACHE_LOOKUP was
++ not found in the hotspot cache.
+
+CACHE_FOUND The item was found
++ Debug information. The last CACHE_LOOKUP was successful, eg. we have found
++ the requested item in the hotspot cache.
+
+CACHE_EXPIRED The item is expired
++ Debug information. The item requested in the last CACHE_LOOKUP was in the
++ hotspot cache, but it was old. We're going to remove it and report we don't
++ have it (the external result will be the same as with CACHE_NOT_FOUND).
+
+CACHE_SLOTS Setting the cache size to '%d'
++ The maximum allowed number of items of the hotspot cache is set to the given
++ number. If there are too many, we're going to drop them right now. The size
++ of 0 means no limit.
+
+CACHE_ENABLE Enabling the cache
++ The hotspot cache is enabled from now on.
+
+CACHE_DISABLE Disabling the cache
++ The hotspot cache is disabled from now on. It is not going to store
++ information or return anything.

+ 3 - 1
src/lib/datasrc/tests/logger_unittest.cc

@@ -23,7 +23,9 @@ namespace {
 TEST(CacheLogger, name) {
     // This does not check the name only, but the fact the logger is created
     // The dot is because of empty root logger
-    EXPECT_EQ(".datasrc", logger().getName());
+    std::string name(logger.getName());
+    EXPECT_EQ(name.size() - 8, name.rfind(".datasrc")) <<
+        "Wrong logger name: " << name;
 }
 
 }