From 9617bd2e4d028a7e24b9681d9ed9b3ac14effe8f Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Sat, 24 Jul 2021 22:39:44 -0300 Subject: [PATCH] wifi: reuse Simulator::Now in WifiMacQueue --- src/wifi/model/wifi-mac-queue.cc | 66 +++++++++++++++++++++----------- src/wifi/model/wifi-mac-queue.h | 3 +- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/wifi/model/wifi-mac-queue.cc b/src/wifi/model/wifi-mac-queue.cc index af4e8ae7c..e116da06a 100644 --- a/src/wifi/model/wifi-mac-queue.cc +++ b/src/wifi/model/wifi-mac-queue.cc @@ -93,12 +93,12 @@ WifiMacQueue::GetMaxDelay (void) const } bool -WifiMacQueue::TtlExceeded (ConstIterator &it) +WifiMacQueue::TtlExceeded (ConstIterator &it, const Time& now) { - if (Simulator::Now () > (*it)->GetTimeStamp () + m_maxDelay) + if (now > (*it)->GetTimeStamp () + m_maxDelay) { NS_LOG_DEBUG ("Removing packet that stayed in the queue for too long (" << - Simulator::Now () - (*it)->GetTimeStamp () << ")"); + now - (*it)->GetTimeStamp () << ")"); m_traceExpired (*it); auto curr = it++; DoRemove (curr); @@ -138,13 +138,14 @@ WifiMacQueue::Insert (ConstIterator pos, Ptr item) // the queue is full; scan the list in the attempt to remove stale packets ConstIterator it = begin (); + const Time now = Simulator::Now (); while (it != end ()) { - if (it == pos && TtlExceeded (it)) + if (it == pos && TtlExceeded (it, now)) { return DoEnqueue (it, item); } - if (TtlExceeded (it)) + if (TtlExceeded (it, now)) { return DoEnqueue (pos, item); } @@ -174,9 +175,10 @@ Ptr WifiMacQueue::Dequeue (void) { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { return DoDequeue (it); } @@ -242,9 +244,10 @@ WifiMacQueue::Dequeue (ConstIterator pos) { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); if (!m_expiredPacketsPresent) { - if (TtlExceeded (pos)) + if (TtlExceeded (pos, now)) { NS_LOG_DEBUG ("Packet lifetime expired"); return 0; @@ -261,13 +264,13 @@ WifiMacQueue::Dequeue (ConstIterator pos) // reset the flag signaling the presence of expired packets before returning m_expiredPacketsPresent = false; - if (TtlExceeded (it)) + if (TtlExceeded (it, now)) { return 0; } return DoDequeue (it); } - else if (!TtlExceeded (it)) + else if (!TtlExceeded (it, now)) { it++; } @@ -280,11 +283,12 @@ Ptr WifiMacQueue::Peek (void) const { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); for (auto it = begin (); it != end (); it++) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method - if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay) + if (now <= (*it)->GetTimeStamp () + m_maxDelay) { return DoPeek (it); } @@ -300,11 +304,12 @@ WifiMacQueue::PeekByAddress (Mac48Address dest, ConstIterator pos) const { NS_LOG_FUNCTION (this << dest); ConstIterator it = (pos != EMPTY ? pos : begin ()); + const Time now = Simulator::Now (); while (it != end ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method - if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay) + if (now <= (*it)->GetTimeStamp () + m_maxDelay) { if (((*it)->GetHeader ().IsData () || (*it)->GetHeader ().IsQosData ()) && (*it)->GetDestinationAddress () == dest) @@ -328,11 +333,12 @@ WifiMacQueue::PeekByTid (uint8_t tid, ConstIterator pos) const { NS_LOG_FUNCTION (this << +tid); ConstIterator it = (pos != EMPTY ? pos : begin ()); + const Time now = Simulator::Now (); while (it != end ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method - if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay) + if (now <= (*it)->GetTimeStamp () + m_maxDelay) { if ((*it)->GetHeader ().IsQosData () && (*it)->GetHeader ().GetQosTid () == tid) { @@ -355,11 +361,12 @@ WifiMacQueue::PeekByTidAndAddress (uint8_t tid, Mac48Address dest, ConstIterator { NS_LOG_FUNCTION (this << +tid << dest); ConstIterator it = (pos != EMPTY ? pos : begin ()); + const Time now = Simulator::Now (); while (it != end ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method - if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay) + if (now <= (*it)->GetTimeStamp () + m_maxDelay) { if ((*it)->GetHeader ().IsQosData () && (*it)->GetDestinationAddress () == dest && (*it)->GetHeader ().GetQosTid () == tid) @@ -383,11 +390,12 @@ WifiMacQueue::PeekFirstAvailable (const Ptr blockedPacke { NS_LOG_FUNCTION (this); ConstIterator it = (pos != EMPTY ? pos : begin ()); + const Time now = Simulator::Now (); while (it != end ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method - if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay) + if (now <= (*it)->GetTimeStamp () + m_maxDelay) { if (!(*it)->GetHeader ().IsQosData () || !blockedPackets || !blockedPackets->IsBlocked ((*it)->GetHeader ().GetAddr1 (), (*it)->GetHeader ().GetQosTid ())) @@ -411,9 +419,10 @@ WifiMacQueue::Remove (void) { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { return DoRemove (it); } @@ -426,9 +435,11 @@ bool WifiMacQueue::Remove (Ptr packet) { NS_LOG_FUNCTION (this << packet); + + const Time now = Simulator::Now (); for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { if ((*it)->GetPacket () == packet) { @@ -455,6 +466,8 @@ WifiMacQueue::Remove (ConstIterator pos, bool removeExpired) return pos; } + const Time now = Simulator::Now (); + // remove stale items queued before the given position ConstIterator it = begin (); while (it != end ()) @@ -468,7 +481,7 @@ WifiMacQueue::Remove (ConstIterator pos, bool removeExpired) DoRemove (curr); return pos; } - else if (!TtlExceeded (it)) + else if (!TtlExceeded (it, now)) { it++; } @@ -483,10 +496,11 @@ WifiMacQueue::GetNPacketsByAddress (Mac48Address dest) NS_LOG_FUNCTION (this << dest); uint32_t nPackets = 0; + const Time now = Simulator::Now (); for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { if ((*it)->GetHeader ().IsData () && (*it)->GetDestinationAddress () == dest) { @@ -505,9 +519,11 @@ WifiMacQueue::GetNPacketsByTidAndAddress (uint8_t tid, Mac48Address dest) { NS_LOG_FUNCTION (this << dest); uint32_t nPackets = 0; + const Time now = Simulator::Now (); + for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { if ((*it)->GetHeader ().IsQosData () && (*it)->GetDestinationAddress () == dest && (*it)->GetHeader ().GetQosTid () == tid) @@ -526,9 +542,11 @@ bool WifiMacQueue::IsEmpty (void) { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); + for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { NS_LOG_DEBUG ("returns false"); return false; @@ -542,10 +560,12 @@ uint32_t WifiMacQueue::GetNPackets (void) { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); + // remove packets that stayed in the queue for too long for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { it++; } @@ -557,10 +577,12 @@ uint32_t WifiMacQueue::GetNBytes (void) { NS_LOG_FUNCTION (this); + const Time now = Simulator::Now (); + // remove packets that stayed in the queue for too long for (ConstIterator it = begin (); it != end (); ) { - if (!TtlExceeded (it)) + if (!TtlExceeded (it, now)) { it++; } diff --git a/src/wifi/model/wifi-mac-queue.h b/src/wifi/model/wifi-mac-queue.h index 45d8a999b..ebf0b649c 100644 --- a/src/wifi/model/wifi-mac-queue.h +++ b/src/wifi/model/wifi-mac-queue.h @@ -337,9 +337,10 @@ private: * point to the item that followed the erased one. * * \param it an iterator pointing to the item + * \param now a copy of Simulator::Now() * \return true if the item is removed, false otherwise */ - bool TtlExceeded (ConstIterator &it); + bool TtlExceeded (ConstIterator &it, const Time& now); /** * Wrapper for the DoEnqueue method provided by the base class that additionally * sets the iterator field of the item and updates internal statistics, if