model: An experimental support for LFU replacement policy (need further adjustments)
diff --git a/model/cs/content-store-impl.cc b/model/cs/content-store-impl.cc
index aa7a234..39eab27 100644
--- a/model/cs/content-store-impl.cc
+++ b/model/cs/content-store-impl.cc
@@ -23,6 +23,7 @@
#include "../../utils/trie/random-policy.h"
#include "../../utils/trie/lru-policy.h"
#include "../../utils/trie/fifo-policy.h"
+#include "../../utils/trie/lfu-policy.h"
#define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ) \
static struct X ## type ## templ ## RegistrationClass \
@@ -56,10 +57,17 @@
**/
template class ContentStoreImpl<fifo_policy_traits>;
+/**
+ * @brief ContentStore with Least Frequently Used (LFU) cache replacement policy
+ **/
+template class ContentStoreImpl<lfu_policy_traits>;
+
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, lru_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, random_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, fifo_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, lfu_policy_traits);
+
#ifdef DOXYGEN
// /**
// * \brief Content Store implementing LRU cache replacement policy
@@ -75,6 +83,11 @@
* \brief Content Store implementing Random cache replacement policy
*/
class Random : public ContentStoreImpl<random_policy_traits> { };
+
+/**
+ * \brief Content Store implementing Least Frequently Used cache replacement policy
+ */
+class Lfu : public ContentStoreImpl<lfu_policy_traits> { };
#endif
diff --git a/model/cs/content-store-with-freshness.cc b/model/cs/content-store-with-freshness.cc
index 67030d1..3691891 100644
--- a/model/cs/content-store-with-freshness.cc
+++ b/model/cs/content-store-with-freshness.cc
@@ -23,6 +23,7 @@
#include "../../utils/trie/random-policy.h"
#include "../../utils/trie/lru-policy.h"
#include "../../utils/trie/fifo-policy.h"
+#include "../../utils/trie/lfu-policy.h"
#define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ) \
static struct X ## type ## templ ## RegistrationClass \
@@ -56,10 +57,17 @@
**/
template class ContentStoreWithFreshness<fifo_policy_traits>;
+/**
+ * @brief ContentStore with freshness and Least Frequently Used (LFU) cache replacement policy
+ **/
+template class ContentStoreWithFreshness<lfu_policy_traits>;
+
+
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, lru_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, random_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, fifo_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, lfu_policy_traits);
#ifdef DOXYGEN
// /**
@@ -77,6 +85,11 @@
*/
class Freshness::Random : public ContentStoreWithFreshness<random_policy_traits> { };
+/**
+ * \brief Content Store with freshness implementing Least Frequently Used cache replacement policy
+ */
+class Freshness::Lfu : public ContentStoreWithFreshness<lfu_policy_traits> { };
+
#endif
diff --git a/model/cs/content-store-with-stats.cc b/model/cs/content-store-with-stats.cc
index 828cbb1..ad179e4 100644
--- a/model/cs/content-store-with-stats.cc
+++ b/model/cs/content-store-with-stats.cc
@@ -23,6 +23,7 @@
#include "../../utils/trie/random-policy.h"
#include "../../utils/trie/lru-policy.h"
#include "../../utils/trie/fifo-policy.h"
+#include "../../utils/trie/lfu-policy.h"
#define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ) \
static struct X ## type ## templ ## RegistrationClass \
@@ -56,10 +57,17 @@
**/
template class ContentStoreWithStats<fifo_policy_traits>;
+/**
+ * @brief ContentStore with stats and Least Frequently Used (LFU) cache replacement policy
+ **/
+template class ContentStoreWithStats<lfu_policy_traits>;
+
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithStats, lru_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithStats, random_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithStats, fifo_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithStats, lfu_policy_traits);
+
#ifdef DOXYGEN
// /**
@@ -77,6 +85,11 @@
*/
class Stats::Random : public ContentStoreWithStats<random_policy_traits> { };
+/**
+ * \brief Content Store with stats implementing Least Frequently Used cache replacement policy
+ */
+class Stats::Lfu : public ContentStoreWithStats<lfu_policy_traits> { };
+
#endif
diff --git a/utils/trie/freshness-policy.h b/utils/trie/freshness-policy.h
index e123d73..d22b624 100644
--- a/utils/trie/freshness-policy.h
+++ b/utils/trie/freshness-policy.h
@@ -53,14 +53,14 @@
template<class Base,
class Container,
class Hook>
- struct policy
+ struct policy
{
static Time& get_freshness (typename Container::iterator item)
{
return static_cast<typename policy_container::value_traits::hook_type*>
(policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
}
-
+
static const Time& get_freshness (typename Container::const_iterator item)
{
return static_cast<const typename policy_container::value_traits::hook_type*>
@@ -80,7 +80,7 @@
boost::intrusive::compare< MemberHookLess< Container > >,
Hook > policy_container;
-
+
class type : public policy_container
{
public:
@@ -96,9 +96,9 @@
inline void
update (typename parent_trie::iterator item)
{
- // do nothing. it's random policy
+ // do nothing
}
-
+
inline bool
insert (typename parent_trie::iterator item)
{
@@ -115,13 +115,13 @@
return true;
}
-
+
inline void
lookup (typename parent_trie::iterator item)
{
// do nothing. it's random policy
}
-
+
inline void
erase (typename parent_trie::iterator item)
{
@@ -152,7 +152,7 @@
private:
type () : base_(*((Base*)0)) { };
-
+
private:
Base &base_;
size_t max_size_;
diff --git a/utils/trie/lfu-policy.h b/utils/trie/lfu-policy.h
new file mode 100644
index 0000000..e453813
--- /dev/null
+++ b/utils/trie/lfu-policy.h
@@ -0,0 +1,161 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef LFU_POLICY_H_
+#define LFU_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/set.hpp>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+
+/**
+ * @brief Traits for LFU replacement policy
+ */
+struct lfu_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
+ static std::string GetName () { return "Lfu"; }
+
+ struct policy_hook_type : public boost::intrusive::set_member_hook<> { double frequency; };
+
+ template<class Container>
+ struct container_hook
+ {
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ static double& get_order (typename Container::iterator item)
+ {
+ return static_cast<typename policy_container::value_traits::hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->frequency;
+ }
+
+ static const double& get_order (typename Container::const_iterator item)
+ {
+ return static_cast<const typename policy_container::value_traits::hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->frequency;
+ }
+
+ template<class Key>
+ struct MemberHookLess
+ {
+ bool operator () (const Key &a, const Key &b) const
+ {
+ return get_order (&a) < get_order (&b);
+ }
+ };
+
+ typedef boost::intrusive::multiset< Container,
+ boost::intrusive::compare< MemberHookLess< Container > >,
+ Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef policy policy_base; // to get access to get_order methods from outside
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ policy_container::erase (*item);
+ get_order (item) += 1;
+ policy_container::insert (*item);
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ get_order (item) = 0;
+
+ if (max_size_ != 0 && policy_container::size () >= max_size_)
+ {
+ // this erases the "least frequently used item" from cache
+ base_.erase (&(*policy_container::begin ()));
+ }
+
+ policy_container::insert (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ policy_container::erase (*item);
+ get_order (item) += 1;
+ policy_container::insert (*item);
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ size_t max_size_;
+ };
+ };
+};
+
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif // LFU_POLICY_H