Implementing new cache replacement strategies: Random and FIFO
diff --git a/examples/ccnx-grid.cc b/examples/ccnx-grid.cc
index 4bab1f8..07d3bbf 100644
--- a/examples/ccnx-grid.cc
+++ b/examples/ccnx-grid.cc
@@ -64,9 +64,6 @@
Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
-
- // Set maximum number of packets that will be cached (default 100)
- Config::SetDefault ("ns3::CcnxContentStoreLru::Size", StringValue ("1000"));
uint32_t nGrid = 3;
Time finishTime = Seconds (20.0);
@@ -84,6 +81,8 @@
// Install CCNx stack on all nodes
NS_LOG_INFO ("Installing CCNx stack on all nodes");
CcnxStackHelper ccnxHelper;
+ ccnxHelper.SetContentStore ("ns3::CcnxContentStoreRandom",
+ "Size", "10");
ccnxHelper.InstallAll ();
CcnxGlobalRoutingHelper ccnxGlobalRoutingHelper;
@@ -118,6 +117,16 @@
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
+
+ for (NodeList::Iterator node = NodeList::Begin ();
+ node != NodeList::End ();
+ node ++)
+ {
+ std::cout << "Node #" << (*node)->GetId () << std::endl;
+ (*node)->GetObject<CcnxContentStore> ()->Print (std::cout);
+ std::cout << std::endl;
+ }
+
Simulator::Destroy ();
NS_LOG_INFO ("Done!");
diff --git a/examples/trie.cc b/examples/trie.cc
index 4d82ba6..3e77a4f 100644
--- a/examples/trie.cc
+++ b/examples/trie.cc
@@ -20,7 +20,7 @@
#include "ns3/core-module.h"
#include "ns3/ndnSIM-module.h"
-#include "../utils/trie.h"
+#include "../utils/trie-with-policy.h"
using namespace ns3;
@@ -46,9 +46,15 @@
int
main (int argc, char *argv[])
{
- typedef indexed_trie<ns3::CcnxNameComponents, Integer, smart_pointer_payload_traits<Integer> > trie;
+ typedef trie_with_policy<ns3::CcnxNameComponents,
+ Integer,
+ smart_pointer_payload_traits<Integer>,
+ lru_policy_traits<CcnxNameComponents,
+ Integer,
+ smart_pointer_payload_traits<Integer> >
+ > trie;
trie x;
- x.getPolicy ().set_max_size (2);
+ x.getPolicy ().set_max_size (100);
// x.getTrie ().PrintStat (std::cout);
@@ -61,22 +67,23 @@
ns3::Ptr<Integer> i = ns3::Create<Integer> (1);
x.insert (n4, ns3::Create<Integer> (4));
-
-
x.insert (n3, ns3::Create<Integer> (3));
std::pair< trie::iterator, bool > item =
x.insert (n2, ns3::Create<Integer> (2));
- x.erase (item.first);
+ // x.erase (item.first);
x.insert (n1, ns3::Create<Integer> (1));
x.insert (n4, ns3::Create<Integer> (4));
- // std::cout << x.getTrie ();
- BOOST_FOREACH (const trie::parent_trie &item, x.getPolicy ())
- {
- std::cout << *item.payload () << " " << std::endl;
- }
+ std::cout << "digraph trie {\n";
+ std::cout << x.getTrie ();
+ std::cout << "}\n";
+
+ // BOOST_FOREACH (const trie::parent_trie &item, x.getPolicy ())
+ // {
+ // std::cout << *item.payload () << " " << std::endl;
+ // }
// ns3::CcnxNameComponents n4;
// n4("a")("c");
diff --git a/model/ccnx-content-store-impl.h b/model/ccnx-content-store-impl.h
new file mode 100644
index 0000000..e2ee632
--- /dev/null
+++ b/model/ccnx-content-store-impl.h
@@ -0,0 +1,104 @@
+/* -*- 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 CCNX_CONTENT_STORE_IMPL_H_
+#define CCNX_CONTENT_STORE_IMPL_H_
+
+#include "ccnx-content-store.h"
+#include "ns3/packet.h"
+#include "ns3/ccnx-interest-header.h"
+#include "ns3/ccnx-content-object-header.h"
+#include <boost/foreach.hpp>
+
+namespace ns3
+{
+
+template<class Container>
+class CcnxContentStoreImpl : public CcnxContentStore,
+ protected Container
+{
+public:
+ // from CcnxContentStore
+
+ virtual inline boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
+ Lookup (Ptr<const CcnxInterestHeader> interest);
+
+ virtual inline bool
+ Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
+
+ // virtual bool
+ // Remove (Ptr<CcnxInterestHeader> header);
+
+ virtual inline void
+ Print (std::ostream &os) const;
+};
+
+
+template<class Container>
+boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
+CcnxContentStoreImpl<Container>::Lookup (Ptr<const CcnxInterestHeader> interest)
+{
+ // NS_LOG_FUNCTION (this << interest->GetName ());
+
+ /// @todo Change to search with predicate
+ typename Container::iterator node = this->deepest_prefix_match (interest->GetName ());
+
+ if (node != this->end ())
+ {
+ this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
+
+ // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
+ return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (),
+ node->payload ()->GetHeader (),
+ node->payload ()->GetPacket ());
+ }
+ else
+ {
+ // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
+ this->m_cacheMissesTrace (interest);
+ return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
+ }
+}
+
+template<class Container>
+bool
+CcnxContentStoreImpl<Container>::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
+{
+ // NS_LOG_FUNCTION (this << header->GetName ());
+
+ return
+ this->insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet))
+ .second;
+}
+
+template<class Container>
+void
+CcnxContentStoreImpl<Container>::Print (std::ostream &os) const
+{
+ BOOST_FOREACH (const typename Container::parent_trie &item, this->getPolicy ())
+ {
+ os << item.payload ()->GetName () << std::endl;
+ }
+}
+
+
+} // namespace ns3
+
+#endif // CCNX_CONTENT_STORE_IMPL_H_
diff --git a/model/ccnx-content-store-lru.cc b/model/ccnx-content-store-lru.cc
index 5546d84..a8d00b6 100644
--- a/model/ccnx-content-store-lru.cc
+++ b/model/ccnx-content-store-lru.cc
@@ -15,22 +15,24 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
- * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
- * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#include "ccnx-content-store-lru.h"
#include "ns3/log.h"
-#include "ns3/packet.h"
-#include "ns3/ccnx-interest-header.h"
-#include "ns3/ccnx-content-object-header.h"
#include "ns3/uinteger.h"
-NS_LOG_COMPONENT_DEFINE ("CcnxContentStoreLru");
+NS_LOG_COMPONENT_DEFINE ("CcnxContentStorePolicies");
namespace ns3
{
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+// LRU policy
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+
NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreLru);
TypeId
@@ -38,7 +40,7 @@
{
static TypeId tid = TypeId ("ns3::CcnxContentStoreLru")
.SetGroupName ("Ccnx")
- .SetParent<CcnxContentStore> ()
+ .SetParent< CcnxContentStore > ()
.AddConstructor<CcnxContentStoreLru> ()
.AddAttribute ("Size",
"Maximum number of packets that content storage can hold",
@@ -54,77 +56,118 @@
void
CcnxContentStoreLru::SetMaxSize (uint32_t maxSize)
{
- m_maxSize = maxSize;
- m_contentStore.getPolicy ().set_max_size (maxSize);
+ getPolicy ().set_max_size (maxSize);
}
uint32_t
CcnxContentStoreLru::GetMaxSize () const
{
- return m_maxSize;
+ return getPolicy ().get_max_size ();
}
-
-//////////////////////////////////////////////////////////////////////
-// Helper classes
-//////////////////////////////////////////////////////////////////////
-/**
- * \ingroup ccnx
- * \brief Typedef for hash index of content store container
- */
-
CcnxContentStoreLru::CcnxContentStoreLru ()
- : m_maxSize (100)
-{ } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier
+{
+}
CcnxContentStoreLru::~CcnxContentStoreLru ()
-{ }
-
-
-boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
-CcnxContentStoreLru::Lookup (Ptr<const CcnxInterestHeader> interest)
{
- NS_LOG_FUNCTION (this << interest->GetName ());
-
- /// @todo Change to search with predicate
- CcnxContentStoreLruContainer::iterator node =
- m_contentStore.deepest_prefix_match (interest->GetName ());
-
- if (node != m_contentStore.end ())
- {
- m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
-
- NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
- return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (),
- node->payload ()->GetHeader (),
- node->payload ()->GetPacket ());
- }
- else
- {
- NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
- m_cacheMissesTrace (interest);
- return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
- }
-}
-
-bool
-CcnxContentStoreLru::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
-{
- NS_LOG_FUNCTION (this << header->GetName ());
-
- return
- m_contentStore
- .insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet))
- .second;
}
-
-void
-CcnxContentStoreLru::Print () const
+
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+// RANDOM policy
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+
+NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreRandom);
+
+TypeId
+CcnxContentStoreRandom::GetTypeId (void)
{
- BOOST_FOREACH (const CcnxContentStoreLruContainer::parent_trie &item, m_contentStore.getPolicy ())
- {
- NS_LOG_INFO (item.payload ()->GetName ());
- }
+ static TypeId tid = TypeId ("ns3::CcnxContentStoreRandom")
+ .SetGroupName ("Ccnx")
+ .SetParent< CcnxContentStore > ()
+ .AddConstructor<CcnxContentStoreRandom> ()
+
+ .AddAttribute ("Size",
+ "Maximum number of packets that content storage can hold",
+ UintegerValue (100),
+ MakeUintegerAccessor (&CcnxContentStoreRandom::SetMaxSize,
+ &CcnxContentStoreRandom::GetMaxSize),
+ MakeUintegerChecker<uint32_t> ())
+ ;
+
+ return tid;
}
+void
+CcnxContentStoreRandom::SetMaxSize (uint32_t maxSize)
+{
+ getPolicy ().set_max_size (maxSize);
+}
+
+uint32_t
+CcnxContentStoreRandom::GetMaxSize () const
+{
+ return getPolicy ().get_max_size ();
+}
+
+CcnxContentStoreRandom::CcnxContentStoreRandom ()
+{
+}
+
+CcnxContentStoreRandom::~CcnxContentStoreRandom ()
+{
+}
+
+
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+// FIFO policy
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+
+NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreFifo);
+
+TypeId
+CcnxContentStoreFifo::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("ns3::CcnxContentStoreFifo")
+ .SetGroupName ("Ccnx")
+ .SetParent< CcnxContentStore > ()
+ .AddConstructor<CcnxContentStoreFifo> ()
+
+ .AddAttribute ("Size",
+ "Maximum number of packets that content storage can hold",
+ UintegerValue (100),
+ MakeUintegerAccessor (&CcnxContentStoreFifo::SetMaxSize,
+ &CcnxContentStoreFifo::GetMaxSize),
+ MakeUintegerChecker<uint32_t> ())
+ ;
+
+ return tid;
+}
+
+void
+CcnxContentStoreFifo::SetMaxSize (uint32_t maxSize)
+{
+ getPolicy ().set_max_size (maxSize);
+}
+
+uint32_t
+CcnxContentStoreFifo::GetMaxSize () const
+{
+ return getPolicy ().get_max_size ();
+}
+
+CcnxContentStoreFifo::CcnxContentStoreFifo ()
+{
+}
+
+CcnxContentStoreFifo::~CcnxContentStoreFifo ()
+{
+}
+
+
} // namespace ns3
+
diff --git a/model/ccnx-content-store-lru.h b/model/ccnx-content-store-lru.h
index 277290a..f863aae 100644
--- a/model/ccnx-content-store-lru.h
+++ b/model/ccnx-content-store-lru.h
@@ -15,36 +15,41 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
- * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Ilya Moiseenko <iliamo@cs.ucla.edu>
*/
#ifndef CCNX_CONTENT_STORE_LRU_H
#define CCNX_CONTENT_STORE_LRU_H
-#include "ccnx-content-store.h"
+#include "ccnx-content-store-impl.h"
#include "ccnx.h"
#include "ccnx-name-components-hash-helper.h"
#include "../utils/trie.h"
+#include "../utils/trie-with-policy.h"
+
+#include "../utils/lru-policy.h"
+#include "../utils/random-policy.h"
+#include "../utils/fifo-policy.h"
namespace ns3
{
-/**
- * \ingroup ccnx
- * \brief Typedef for content store container implemented as trie with configurable replacement policy
- */
-typedef indexed_trie<CcnxNameComponents,
- CcnxContentStoreEntry, smart_pointer_payload_traits<CcnxContentStoreEntry>,
- lru_policy_traits<CcnxNameComponents,
- CcnxContentStoreEntry,
- smart_pointer_payload_traits<CcnxContentStoreEntry> > > CcnxContentStoreLruContainer;
/**
* \ingroup ccnx
- * \brief NDN content store entry
+ * \brief Content Store with LRU replacement policy
*/
-class CcnxContentStoreLru : public CcnxContentStore
+class CcnxContentStoreLru :
+ public CcnxContentStoreImpl<
+ trie_with_policy<CcnxNameComponents,
+ CcnxContentStoreEntry,
+ smart_pointer_payload_traits<CcnxContentStoreEntry>,
+ lru_policy_traits<CcnxNameComponents,
+ CcnxContentStoreEntry,
+ smart_pointer_payload_traits<CcnxContentStoreEntry> > >
+ >
{
public:
/**
@@ -60,42 +65,88 @@
CcnxContentStoreLru ();
virtual ~CcnxContentStoreLru ();
- /**
- * \brief Set maximum size of content store
- *
- * \param size size in packets
- */
+private:
void
SetMaxSize (uint32_t maxSize);
- /**
- * \brief Get maximum size of content store
- *
- * \returns size in packets
- */
uint32_t
GetMaxSize () const;
-
- // from CcnxContentStore
- virtual boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
- Lookup (Ptr<const CcnxInterestHeader> interest);
-
- virtual bool
- Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
+};
- virtual void
- Print () const;
-
-private:
- size_t m_maxSize; ///< \brief maximum number of entries in cache
+
+/**
+ * \ingroup ccnx
+ * \brief Content Store with RANDOM replacement policy
+ */
+class CcnxContentStoreRandom :
+ public CcnxContentStoreImpl<
+ trie_with_policy<CcnxNameComponents,
+ CcnxContentStoreEntry,
+ smart_pointer_payload_traits<CcnxContentStoreEntry>,
+ random_policy_traits<CcnxNameComponents,
+ CcnxContentStoreEntry,
+ smart_pointer_payload_traits<CcnxContentStoreEntry> > >
+ >
+{
+public:
+ /**
+ * \brief Interface ID
+ *
+ * \return interface ID
+ */
+ static TypeId GetTypeId ();
/**
- * \brief Content store implemented as a Boost.MultiIndex container
- * \internal
+ * Default constructor
*/
- CcnxContentStoreLruContainer m_contentStore;
+ CcnxContentStoreRandom ();
+ virtual ~CcnxContentStoreRandom ();
+
+private:
+ void
+ SetMaxSize (uint32_t maxSize);
+
+ uint32_t
+ GetMaxSize () const;
};
+/**
+ * \ingroup ccnx
+ * \brief Content Store with FIFO replacement policy
+ */
+class CcnxContentStoreFifo :
+ public CcnxContentStoreImpl<
+ trie_with_policy<CcnxNameComponents,
+ CcnxContentStoreEntry,
+ smart_pointer_payload_traits<CcnxContentStoreEntry>,
+ fifo_policy_traits<CcnxNameComponents,
+ CcnxContentStoreEntry,
+ smart_pointer_payload_traits<CcnxContentStoreEntry> > >
+ >
+{
+public:
+ /**
+ * \brief Interface ID
+ *
+ * \return interface ID
+ */
+ static TypeId GetTypeId ();
+
+ /**
+ * Default constructor
+ */
+ CcnxContentStoreFifo ();
+ virtual ~CcnxContentStoreFifo ();
+
+private:
+ void
+ SetMaxSize (uint32_t maxSize);
+
+ uint32_t
+ GetMaxSize () const;
+};
+
+
} //namespace ns3
#endif // CCNX_CONTENT_STORE_LRU_H
diff --git a/model/ccnx-content-store.h b/model/ccnx-content-store.h
index 27bcda4..34d209d 100644
--- a/model/ccnx-content-store.h
+++ b/model/ccnx-content-store.h
@@ -153,7 +153,7 @@
* \brief Print out content store entries
*/
virtual void
- Print () const = 0;
+ Print (std::ostream &os) const = 0;
protected:
TracedCallback<Ptr<const CcnxInterestHeader>,
@@ -165,7 +165,7 @@
inline std::ostream&
operator<< (std::ostream &os, const CcnxContentStore &cs)
{
- cs.Print ();
+ cs.Print (os);
return os;
}
diff --git a/model/ccnx-pit-entry.h b/model/ccnx-pit-entry.h
index bc81c89..1ab4f65 100644
--- a/model/ccnx-pit-entry.h
+++ b/model/ccnx-pit-entry.h
@@ -30,14 +30,12 @@
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/tag.hpp>
#include <boost/multi_index/ordered_index.hpp>
-#include <boost/multi_index/composite_key.hpp>
-#include <boost/multi_index/hashed_index.hpp>
+// #include <boost/multi_index/composite_key.hpp>
+// #include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
-#include <boost/multi_index/mem_fun.hpp>
+// #include <boost/multi_index/mem_fun.hpp>
#include <set>
-#include <iostream>
-
namespace ns3 {
class CcnxFace;
diff --git a/model/ccnx-pit.h b/model/ccnx-pit.h
index be9c80f..8bb746e 100644
--- a/model/ccnx-pit.h
+++ b/model/ccnx-pit.h
@@ -120,7 +120,7 @@
* \brief Destructor
*/
virtual ~CcnxPit ();
-
+
/**
* \brief Find corresponding PIT entry for the given content name
* \param prefix Prefix for which to lookup the entry
@@ -167,15 +167,16 @@
*/
void
MarkErased (iterator entry);
-
+
protected:
// inherited from Object class
virtual void NotifyNewAggregate (); ///< @brief Even when object is aggregated to another Object
virtual void DoDispose (); ///< @brief Do cleanup
-
+
private:
/** \brief Remove expired records from PIT */
- void CleanExpired ();
+ void
+ CleanExpired ();
/**
* \brief Set cleanup timeout
@@ -184,16 +185,19 @@
*
* \param timeout cleanup timeout
*/
- void SetCleanupTimeout (const Time &timeout);
+ void
+ SetCleanupTimeout (const Time &timeout);
/**
* \brief Get cleanup timeout
*
* \returns cleanup timeout
*/
- Time GetCleanupTimeout () const;
+ Time
+ GetCleanupTimeout () const;
- friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
+ friend std::ostream&
+ operator<< (std::ostream& os, const CcnxPit &fib);
private:
Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working
diff --git a/utils/fifo-policy.h b/utils/fifo-policy.h
new file mode 100644
index 0000000..db6bd9f
--- /dev/null
+++ b/utils/fifo-policy.h
@@ -0,0 +1,94 @@
+/* -*- 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 FIFO_POLICY_H_
+#define FIFO_POLICY_H_
+
+template<typename FullKey,
+ typename Payload,
+ typename PayloadTraits
+ >
+struct fifo_policy_traits
+{
+ typedef bi::list_member_hook<> policy_hook_type;
+ typedef trie< FullKey, Payload, PayloadTraits, policy_hook_type> parent_trie;
+ typedef typename bi::list< parent_trie,
+ bi::member_hook< parent_trie,
+ policy_hook_type,
+ &parent_trie::policy_hook_ > > policy_container;
+
+
+ class policy : public policy_container
+ {
+ public:
+ policy ()
+ : max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline void
+ insert (typename parent_trie::iterator item)
+ {
+ if (policy_container::size () >= max_size_)
+ {
+ typename parent_trie::iterator oldItem = &(*policy_container::begin ());
+ policy_container::pop_front ();
+ oldItem->erase ();
+ }
+
+ policy_container::push_back (*item);
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ size_t max_size_;
+ };
+};
+
+#endif
diff --git a/utils/lru-policy.h b/utils/lru-policy.h
new file mode 100644
index 0000000..2a9187d
--- /dev/null
+++ b/utils/lru-policy.h
@@ -0,0 +1,100 @@
+/* -*- 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 LRU_POLICY_H_
+#define LRU_POLICY_H_
+
+template<typename FullKey,
+ typename Payload,
+ typename PayloadTraits
+ >
+struct lru_policy_traits
+{
+ typedef bi::list_member_hook<> policy_hook_type;
+ typedef trie< FullKey, Payload, PayloadTraits, policy_hook_type> parent_trie;
+ typedef typename bi::list< parent_trie,
+ bi::member_hook< parent_trie,
+ policy_hook_type,
+ &parent_trie::policy_hook_ > > policy_container;
+
+
+ class policy : public policy_container
+ {
+ public:
+ policy ()
+ : max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do relocation
+ policy_container::splice (policy_container::end (),
+ *this,
+ policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ insert (typename parent_trie::iterator item)
+ {
+ if (policy_container::size () >= max_size_)
+ {
+ typename parent_trie::iterator oldItem = &(*policy_container::begin ());
+ policy_container::pop_front ();
+ oldItem->erase ();
+ }
+
+ policy_container::push_back (*item);
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do relocation
+ policy_container::splice (policy_container::end (),
+ *this,
+ policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ size_t max_size_;
+ };
+};
+
+#endif
diff --git a/utils/random-policy.h b/utils/random-policy.h
new file mode 100644
index 0000000..1f47f00
--- /dev/null
+++ b/utils/random-policy.h
@@ -0,0 +1,112 @@
+/* -*- 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 RANDOM_POLICY_H_
+#define RANDOM_POLICY_H_
+
+#include "ns3/random-variable.h"
+
+struct bla : public bi::set_member_hook<>
+{
+ uint32_t randomOrder;
+};
+
+template<class Key>
+struct MemberHookLess
+{
+ bool operator () (const Key &a, const Key &b) const
+ {
+ return a.policy_hook_.randomOrder < b.policy_hook_.randomOrder;
+ }
+};
+
+template<typename FullKey,
+ typename Payload, typename PayloadTraits
+ >
+struct random_policy_traits
+{
+ typedef bla policy_hook_type;
+ typedef trie< FullKey, Payload, PayloadTraits, policy_hook_type > parent_trie;
+ typedef typename bi::set< parent_trie,
+ bi::compare< MemberHookLess< parent_trie > >,
+ bi::member_hook< parent_trie,
+ policy_hook_type,
+ &parent_trie::policy_hook_ > > policy_container;
+
+ class policy : public policy_container
+ {
+ public:
+ policy ()
+ : u_rand (0, std::numeric_limits<uint32_t>::max ())
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
+
+ inline void
+ insert (typename parent_trie::iterator item)
+ {
+ item->policy_hook_.randomOrder = u_rand.GetValue ();
+ if (policy_container::size () >= max_size_)
+ {
+ typename parent_trie::iterator oldItem = &(*policy_container::begin ());
+ policy_container::erase (policy_container::begin ());
+ oldItem->erase ();
+ }
+
+ policy_container::insert (*item);
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ ns3::UniformVariable u_rand;
+ size_t max_size_;
+ };
+};
+
+#endif // RANDOM_POLICY_H
diff --git a/utils/trie-with-policy.h b/utils/trie-with-policy.h
new file mode 100644
index 0000000..a977285
--- /dev/null
+++ b/utils/trie-with-policy.h
@@ -0,0 +1,230 @@
+/* -*- 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 TRIE_WITH_POLICY_H_
+#define TRIE_WITH_POLICY_H_
+
+#include "trie.h"
+#include "lru-policy.h"
+
+template<typename FullKey,
+ typename Payload,
+ typename PayloadTraits,
+ typename PolicyTraits
+ >
+class trie_with_policy
+{
+public:
+ typedef trie< FullKey, Payload, PayloadTraits, typename PolicyTraits::policy_hook_type > parent_trie;
+ typedef typename parent_trie::iterator iterator;
+
+ inline
+ trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10)
+ : trie_ ("", bucketSize, bucketIncrement)
+ {
+ }
+
+ inline std::pair< iterator, bool >
+ insert (const FullKey &key, typename PayloadTraits::const_pointer_type payload)
+ {
+ std::pair<iterator, bool> item =
+ trie_.insert (key, payload);
+
+ if (item.second) // real insert
+ {
+ policy_.insert (s_iterator_to (item.first));
+ }
+ else
+ {
+ item.first->set_payload (payload);
+ policy_.update (s_iterator_to (item.first));
+ }
+
+ return item;
+ }
+
+ inline void
+ erase (iterator node)
+ {
+ if (node == end ()) return;
+
+ policy_.erase (s_iterator_to (node));
+ node->erase (); // will do cleanup here
+ }
+
+ /**
+ * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
+ */
+ inline iterator
+ longest_prefix_match (const FullKey &key)
+ {
+ boost::tuple< iterator, bool, iterator > item = trie_.find (key);
+ if (item.template get<0> () != trie_.end ())
+ {
+ policy_.lookup (s_iterator_to (item.template get<0> ()));
+ }
+ return item.template get<0> ();
+ }
+
+ /**
+ * @brief Find a node that has prefix at least as the key (cache lookup)
+ */
+ inline iterator
+ deepest_prefix_match (const FullKey &key)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ // guard in case we don't have anything in the trie
+ if (lastItem == trie_.end ())
+ return trie_.end ();
+
+ if (reachLast)
+ {
+ if (foundItem == trie_.end ())
+ {
+ foundItem = lastItem->find (); // should be something
+ }
+ policy_.lookup (s_iterator_to (foundItem));
+ return foundItem;
+ }
+ else
+ { // couldn't find a node that has prefix at least as key
+ return trie_.end ();
+ }
+ }
+
+ /**
+ * @brief Find a node that has prefix at least as the key
+ */
+ template<class Predicate>
+ inline iterator
+ deepest_prefix_match (const FullKey &key, Predicate pred)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ // guard in case we don't have anything in the trie
+ if (lastItem == trie_.end ())
+ return trie_.end ();
+
+ if (reachLast)
+ {
+ foundItem = lastItem->find_if (pred); // may or may not find something
+ if (foundItem == trie_.end ())
+ {
+ return trie_.end ();
+ }
+ policy_.lookup (s_iterator_to (foundItem));
+ return foundItem;
+ }
+ else
+ { // couldn't find a node that has prefix at least as key
+ return trie_.end ();
+ }
+ }
+
+ // /**
+ // * @brief Perform the longest prefix match
+ // * @param key the key for which to perform the longest prefix match
+ // *
+ // * @return ->second is true if prefix in ->first is longer than key
+ // * ->third is always last node searched
+ // */
+ // inline boost::tuple< iterator, bool, iterator >
+ // find (const FullKey &key)
+ // {
+ // boost::tuple< iterator, bool, iterator > item = trie_.find (key);
+ // if (item.template get<0> () != trie_.end ())
+ // {
+ // policy_.lookup (s_iterator_to (item.template get<0> ()));
+ // }
+ // return boost::make_tuple (s_iterator_to (item.template get<0> ()),
+ // item.template get<1> (),
+ // s_iterator_to (item.template get<2> ()));
+ // }
+
+ // /**
+ // * @brief Find next payload of the sub-trie
+ // * @param start Start for the search (root for the sub-trie)
+ // * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
+ // */
+ // inline iterator
+ // find (iterator start)
+ // {
+ // iterator item = start->find ();
+ // if (item != trie_.end ())
+ // {
+ // policy_.lookup (s_iterator_to (item));
+ // }
+ // return item;
+ // }
+
+ // /**
+ // * @brief Find next payload of the sub-trie satisfying the predicate
+ // * @param start Start for the search (root for the sub-trie)
+ // * @param pred predicate
+ // * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
+ // */
+ // template<class Predicate>
+ // inline iterator
+ // find_if (iterator start, Predicate pred)
+ // {
+ // iterator item = start->find (pred);
+ // if (item != trie_.end ())
+ // {
+ // policy_.lookup (s_iterator_to (item));
+ // }
+ // return item;
+ // }
+
+ iterator end ()
+ {
+ return 0;
+ }
+
+ const parent_trie &
+ getTrie () const { return trie_; }
+
+ const typename PolicyTraits::policy &
+ getPolicy () const { return policy_; }
+
+ typename PolicyTraits::policy &
+ getPolicy () { return policy_; }
+
+ static inline iterator
+ s_iterator_to (typename parent_trie::iterator item)
+ {
+ if (item == 0)
+ return 0;
+ else
+ return &(*item);
+ }
+
+private:
+ parent_trie trie_;
+ typename PolicyTraits::policy policy_;
+};
+
+
+#endif // TRIE_WITH_POLICY_H_
+
diff --git a/utils/trie.h b/utils/trie.h
index 21bc362..5d440d2 100644
--- a/utils/trie.h
+++ b/utils/trie.h
@@ -18,11 +18,14 @@
* Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
-#include "ns3/core-module.h"
-#include "ns3/ndnSIM-module.h"
+#ifndef TRIE_H_
+#define TRIE_H_
+
+#include "ns3/ptr.h"
#include <boost/intrusive/unordered_set.hpp>
#include <boost/intrusive/list.hpp>
+#include <boost/intrusive/set.hpp>
#include <boost/functional/hash.hpp>
#include <boost/interprocess/smart_ptr/unique_ptr.hpp>
#include <boost/tuple/tuple.hpp>
@@ -64,8 +67,8 @@
//
template<typename FullKey,
typename Payload,
- typename PayloadTraits = pointer_payload_traits<Payload>,
- typename PolicyHook = bi::list_member_hook<> >
+ typename PayloadTraits,
+ typename PolicyHook >
class trie;
template<typename FullKey, typename Payload, typename PayloadTraits, typename PolicyHook>
@@ -339,7 +342,7 @@
if (payload_ != PayloadTraits::empty_payload)
return this;
- typedef trie<FullKey, Payload, PayloadTraits> trie;
+ typedef trie<FullKey, Payload, PayloadTraits, PolicyHook> trie;
BOOST_FOREACH (trie &subnode, children_)
{
iterator value = subnode.find ();
@@ -451,274 +454,4 @@
return boost::hash_value (trie_node.key_);
}
-
-
-template<typename FullKey,
- typename Payload, typename PayloadTraits
- >
-struct lru_policy_traits
-{
- typedef trie< FullKey, Payload, PayloadTraits, bi::list_member_hook<> > parent_trie;
- typedef typename bi::list< parent_trie,
- bi::member_hook< parent_trie,
- bi::list_member_hook<>,
- &parent_trie::policy_hook_ > > policy_container;
-
- class policy : public policy_container
- {
- public:
- policy ()
- : max_size_ (100)
- {
- }
-
- inline void
- update (typename parent_trie::iterator item)
- {
- // do relocation
- policy_container::splice (policy_container::end (),
- *this,
- policy_container::s_iterator_to (*item));
- }
-
- inline void
- insert (typename parent_trie::iterator item)
- {
- if (policy_container::size () >= max_size_)
- {
- typename parent_trie::iterator oldItem = &(*policy_container::begin ());
- policy_container::pop_front ();
- oldItem->erase ();
- }
-
- policy_container::push_back (*item);
- }
-
- inline void
- lookup (typename parent_trie::iterator item)
- {
- // do relocation
- policy_container::splice (policy_container::end (),
- *this,
- policy_container::s_iterator_to (*item));
- }
-
- inline void
- erase (typename parent_trie::iterator item)
- {
- policy_container::erase (policy_container::s_iterator_to (*item));
- }
-
- inline void
- set_max_size (size_t max_size)
- {
- max_size_ = max_size;
- }
-
- private:
- size_t max_size_;
- };
-};
-
-
-
-template<typename FullKey,
- typename Payload, typename PayloadTraits = pointer_payload_traits<Payload>,
- typename policy_traits = lru_policy_traits<FullKey, Payload, PayloadTraits>
- >
-class indexed_trie
-{
-public:
- typedef trie< FullKey, Payload, PayloadTraits > parent_trie;
- typedef typename parent_trie::iterator iterator;
-
- inline
- indexed_trie (size_t bucketSize = 10, size_t bucketIncrement = 10)
- : trie_ ("", bucketSize, bucketIncrement)
- {
- }
-
- inline std::pair< iterator, bool >
- insert (const FullKey &key, typename PayloadTraits::const_pointer_type payload)
- {
- std::pair<iterator, bool> item =
- trie_.insert (key, payload);
-
- if (item.second) // real insert
- {
- policy_.insert (s_iterator_to (item.first));
- }
- else
- {
- item.first->set_payload (payload);
- policy_.update (s_iterator_to (item.first));
- }
-
- return item;
- }
-
- inline void
- erase (iterator node)
- {
- if (node == end ()) return;
-
- policy_.erase (s_iterator_to (node));
- node->erase (); // will do cleanup here
- }
-
- /**
- * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
- */
- inline iterator
- longest_prefix_match (const FullKey &key)
- {
- boost::tuple< iterator, bool, iterator > item = trie_.find (key);
- if (item.template get<0> () != trie_.end ())
- {
- policy_.lookup (s_iterator_to (item.template get<0> ()));
- }
- return item.template get<0> ();
- }
-
- /**
- * @brief Find a node that has prefix at least as the key (cache lookup)
- */
- inline iterator
- deepest_prefix_match (const FullKey &key)
- {
- iterator foundItem, lastItem;
- bool reachLast;
- boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
-
- // guard in case we don't have anything in the trie
- if (lastItem == trie_.end ())
- return trie_.end ();
-
- if (reachLast)
- {
- if (foundItem == trie_.end ())
- {
- foundItem = lastItem->find (); // should be something
- }
- policy_.lookup (s_iterator_to (foundItem));
- return foundItem;
- }
- else
- { // couldn't find a node that has prefix at least as key
- return trie_.end ();
- }
- }
-
- /**
- * @brief Find a node that has prefix at least as the key
- */
- template<class Predicate>
- inline iterator
- deepest_prefix_match (const FullKey &key, Predicate pred)
- {
- iterator foundItem, lastItem;
- bool reachLast;
- boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
-
- // guard in case we don't have anything in the trie
- if (lastItem == trie_.end ())
- return trie_.end ();
-
- if (reachLast)
- {
- foundItem = lastItem->find_if (pred); // may or may not find something
- if (foundItem == trie_.end ())
- {
- return trie_.end ();
- }
- policy_.lookup (s_iterator_to (foundItem));
- return foundItem;
- }
- else
- { // couldn't find a node that has prefix at least as key
- return trie_.end ();
- }
- }
-
- // /**
- // * @brief Perform the longest prefix match
- // * @param key the key for which to perform the longest prefix match
- // *
- // * @return ->second is true if prefix in ->first is longer than key
- // * ->third is always last node searched
- // */
- // inline boost::tuple< iterator, bool, iterator >
- // find (const FullKey &key)
- // {
- // boost::tuple< iterator, bool, iterator > item = trie_.find (key);
- // if (item.template get<0> () != trie_.end ())
- // {
- // policy_.lookup (s_iterator_to (item.template get<0> ()));
- // }
- // return boost::make_tuple (s_iterator_to (item.template get<0> ()),
- // item.template get<1> (),
- // s_iterator_to (item.template get<2> ()));
- // }
-
- // /**
- // * @brief Find next payload of the sub-trie
- // * @param start Start for the search (root for the sub-trie)
- // * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
- // */
- // inline iterator
- // find (iterator start)
- // {
- // iterator item = start->find ();
- // if (item != trie_.end ())
- // {
- // policy_.lookup (s_iterator_to (item));
- // }
- // return item;
- // }
-
- // /**
- // * @brief Find next payload of the sub-trie satisfying the predicate
- // * @param start Start for the search (root for the sub-trie)
- // * @param pred predicate
- // * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
- // */
- // template<class Predicate>
- // inline iterator
- // find_if (iterator start, Predicate pred)
- // {
- // iterator item = start->find (pred);
- // if (item != trie_.end ())
- // {
- // policy_.lookup (s_iterator_to (item));
- // }
- // return item;
- // }
-
- iterator end ()
- {
- return 0;
- }
-
- const parent_trie &
- getTrie () const { return trie_; }
-
- const typename policy_traits::policy &
- getPolicy () const { return policy_; }
-
- typename policy_traits::policy &
- getPolicy () { return policy_; }
-
- static inline iterator
- s_iterator_to (typename parent_trie::iterator item)
- {
- if (item == 0)
- return 0;
- else
- return &(*item);
- }
-
-private:
- parent_trie trie_;
- typename policy_traits::policy policy_;
-};
-
+#endif // TRIE_H_
diff --git a/wscript b/wscript
index bb6f0a8..07dbcc7 100644
--- a/wscript
+++ b/wscript
@@ -86,6 +86,7 @@
"model/ccnx-pit-entry.h",
"model/ccnx-pit-entry-incoming-face.h",
"model/ccnx-pit-entry-outgoing-face.h",
+ "model/ccnx-content-store.h",
"model/ccnx-fib.h",
"model/ccnx-face.h",
"model/ccnx-app-face.h",