Null performance enhancement
authorSteve Smith <smith84@llnl.gov
Fri, 30 Jan 2015 15:04:08 -0500
changeset 11187 597e36a6eb28
parent 11186 9922f3df84ae
child 11188 e010083f6592
Null performance enhancement
src/nix-vector-routing/model/ipv4-nix-vector-routing.cc
src/nix-vector-routing/model/ipv4-nix-vector-routing.h
--- a/src/nix-vector-routing/model/ipv4-nix-vector-routing.cc	Fri Jan 30 08:12:06 2015 +0100
+++ b/src/nix-vector-routing/model/ipv4-nix-vector-routing.cc	Fri Jan 30 15:04:08 2015 -0500
@@ -34,6 +34,8 @@
 
 NS_OBJECT_ENSURE_REGISTERED (Ipv4NixVectorRouting);
 
+bool Ipv4NixVectorRouting::g_isCacheDirty = false;
+
 TypeId 
 Ipv4NixVectorRouting::GetTypeId (void)
 {
@@ -86,7 +88,7 @@
 }
 
 void
-Ipv4NixVectorRouting::FlushGlobalNixRoutingCache ()
+Ipv4NixVectorRouting::FlushGlobalNixRoutingCache (void) const
 {
   NS_LOG_FUNCTION_NOARGS ();
   NodeList::Iterator listEnd = NodeList::End ();
@@ -105,14 +107,14 @@
 }
 
 void
-Ipv4NixVectorRouting::FlushNixCache ()
+Ipv4NixVectorRouting::FlushNixCache (void) const
 {
   NS_LOG_FUNCTION_NOARGS ();
   m_nixCache.clear ();
 }
 
 void
-Ipv4NixVectorRouting::FlushIpv4RouteCache ()
+Ipv4NixVectorRouting::FlushIpv4RouteCache (void) const
 {
   NS_LOG_FUNCTION_NOARGS ();
   m_ipv4RouteCache.clear ();
@@ -168,6 +170,8 @@
 {
   NS_LOG_FUNCTION_NOARGS ();
 
+  CheckCacheStateAndFlush ();
+
   NixMap_t::iterator iter = m_nixCache.find (address);
   if (iter != m_nixCache.end ())
     {
@@ -184,6 +188,8 @@
 {
   NS_LOG_FUNCTION_NOARGS ();
 
+  CheckCacheStateAndFlush ();
+
   Ipv4RouteMap_t::iterator iter = m_ipv4RouteCache.find (address);
   if (iter != m_ipv4RouteCache.end ())
     {
@@ -364,7 +370,7 @@
 }
 
 uint32_t
-Ipv4NixVectorRouting::FindTotalNeighbors ()
+Ipv4NixVectorRouting::FindTotalNeighbors (void)
 {
   uint32_t numberOfDevices = m_node->GetNDevices ();
   uint32_t totalNeighbors = 0;
@@ -488,6 +494,8 @@
   Ptr<NixVector> nixVectorInCache;
   Ptr<NixVector> nixVectorForPacket;
 
+  CheckCacheStateAndFlush ();
+
   NS_LOG_DEBUG ("Dest IP from header: " << header.GetDestination ());
   // check if cache
   nixVectorInCache = GetNixVectorInCache (header.GetDestination ());
@@ -608,6 +616,8 @@
 {
   NS_LOG_FUNCTION_NOARGS ();
 
+  CheckCacheStateAndFlush ();
+
   Ptr<Ipv4Route> rtentry;
 
   // Get the nix-vector from the packet
@@ -663,6 +673,8 @@
 Ipv4NixVectorRouting::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const
 {
 
+  CheckCacheStateAndFlush ();
+
   std::ostream* os = stream->GetStream ();
   *os << "NixCache:" << std::endl;
   if (m_nixCache.size () > 0)
@@ -707,22 +719,22 @@
 void
 Ipv4NixVectorRouting::NotifyInterfaceUp (uint32_t i)
 {
-  FlushGlobalNixRoutingCache ();
+  g_isCacheDirty = true;
 }
 void
 Ipv4NixVectorRouting::NotifyInterfaceDown (uint32_t i)
 {
-  FlushGlobalNixRoutingCache ();
+  g_isCacheDirty = true;
 }
 void
 Ipv4NixVectorRouting::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
 {
-  FlushGlobalNixRoutingCache ();
+  g_isCacheDirty = true;
 }
 void
 Ipv4NixVectorRouting::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
 {
-  FlushGlobalNixRoutingCache ();
+  g_isCacheDirty = true;
 }
 
 bool
@@ -873,4 +885,14 @@
   return false;
 }
 
+void 
+Ipv4NixVectorRouting::CheckCacheStateAndFlush (void) const
+{
+  if (g_isCacheDirty)
+    {
+      FlushGlobalNixRoutingCache ();
+      g_isCacheDirty = false;
+    }
+}
+
 } // namespace ns3
--- a/src/nix-vector-routing/model/ipv4-nix-vector-routing.h	Fri Jan 30 08:12:06 2015 +0100
+++ b/src/nix-vector-routing/model/ipv4-nix-vector-routing.h	Fri Jan 30 15:04:08 2015 -0500
@@ -73,17 +73,22 @@
    * which iterates through the node list and flushes any
    * nix vector caches
    *
+   * \internal
+   * \c const is used here due to need to potentially flush the cache
+   * in const methods such as PrintRoutingTable.  Caches are stored in
+   * mutable variables and flushed in const methods.
    */
-  void FlushGlobalNixRoutingCache (void);
+  void FlushGlobalNixRoutingCache (void) const;
 
 private:
+
   /* flushes the cache which stores nix-vector based on
    * destination IP */
-  void FlushNixCache (void);
+  void FlushNixCache (void) const;
 
   /* flushes the cache which stores the Ipv4 route
    * based on the destination IP */
-  void FlushIpv4RouteCache (void);
+  void FlushIpv4RouteCache (void) const;
 
   /* upon a run-time topology change caches are
    * flushed and the total number of neighbors is
@@ -155,17 +160,27 @@
   virtual void SetIpv4 (Ptr<Ipv4> ipv4);
   virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;
 
+  /* 
+   * Flushes routing caches if required.
+   */
+  void CheckCacheStateAndFlush (void) const;
 
-  /* cache stores nix-vectors based on destination ip */
-  NixMap_t m_nixCache;
+  /* 
+   * Flag to mark when caches are dirty and need to be flushed.  
+   * Used for lazy cleanup of caches when there are many topology changes.
+   */
+  static bool g_isCacheDirty;
 
-  /* cache stores Ipv4Routes based on destination ip */
-  Ipv4RouteMap_t m_ipv4RouteCache;
+  /* Cache stores nix-vectors based on destination ip */
+  mutable NixMap_t m_nixCache;
+
+  /* Cache stores Ipv4Routes based on destination ip */
+  mutable Ipv4RouteMap_t m_ipv4RouteCache;
 
   Ptr<Ipv4> m_ipv4;
   Ptr<Node> m_node;
 
-  /* total neighbors used for nix-vector to determine
+  /* Total neighbors used for nix-vector to determine
    * number of bits */
   uint32_t m_totalNeighbors;
 };