From 82b555582e484ce1b9ecc0bddacb4f9bdade60d7 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Tue, 25 Feb 2020 14:16:46 -0800 Subject: [PATCH] core: PriorityQueueScheduler based on std::priority_queue This has a stub for Remove, which just cancels the event. In turn this causes the simulator test case to fail, because of the sanity check at the end of DefaultSimulatorImpl::Run. --- src/core/model/priority-queue-scheduler.cc | 96 ++++++++++++++++++++++ src/core/model/priority-queue-scheduler.h | 75 +++++++++++++++++ src/core/test/simulator-test-suite.cc | 3 + src/core/wscript | 2 + utils/bench-simulator.cc | 15 +++- 5 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 src/core/model/priority-queue-scheduler.cc create mode 100644 src/core/model/priority-queue-scheduler.h diff --git a/src/core/model/priority-queue-scheduler.cc b/src/core/model/priority-queue-scheduler.cc new file mode 100644 index 000000000..64bf76906 --- /dev/null +++ b/src/core/model/priority-queue-scheduler.cc @@ -0,0 +1,96 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2016 IITP + * + * 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 Krotov + */ + +#include "priority-queue-scheduler.h" +#include "event-impl.h" +#include "assert.h" +#include "log.h" +#include + +/** + * \file + * \ingroup scheduler + * Implementation of ns3::PriorityQueueScheduler class. + */ + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("PriorityQueueScheduler"); + +NS_OBJECT_ENSURE_REGISTERED (PriorityQueueScheduler); + +TypeId +PriorityQueueScheduler::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::PriorityQueueScheduler") + .SetParent () + .SetGroupName ("Core") + .AddConstructor () + ; + return tid; +} + +PriorityQueueScheduler::PriorityQueueScheduler () +{ + NS_LOG_FUNCTION (this); +} +PriorityQueueScheduler::~PriorityQueueScheduler () +{ + NS_LOG_FUNCTION (this); +} + +void +PriorityQueueScheduler::Insert (const Event &ev) +{ + NS_LOG_FUNCTION (this << ev.impl << ev.key.m_ts << ev.key.m_uid); + m_list.push (ev); +} + +bool +PriorityQueueScheduler::IsEmpty (void) const +{ + NS_LOG_FUNCTION (this); + return m_list.empty (); +} + +Scheduler::Event +PriorityQueueScheduler::PeekNext (void) const +{ + NS_LOG_FUNCTION (this); + return m_list.top (); +} + +Scheduler::Event +PriorityQueueScheduler::RemoveNext (void) +{ + NS_LOG_FUNCTION (this); + Scheduler::Event ev = m_list.top (); + m_list.pop (); + return ev; +} + +void +PriorityQueueScheduler::Remove (const Scheduler::Event &ev) +{ + NS_LOG_FUNCTION (this); + ev.impl->Cancel (); +} + +} // namespace ns3 diff --git a/src/core/model/priority-queue-scheduler.h b/src/core/model/priority-queue-scheduler.h new file mode 100644 index 000000000..54692c1ed --- /dev/null +++ b/src/core/model/priority-queue-scheduler.h @@ -0,0 +1,75 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * + * 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: Mathieu Lacage + */ + +#ifndef PRIORITY_QUEUE_SCHEDULER_H +#define PRIORITY_QUEUE_SCHEDULER_H + +#include "scheduler.h" +#include +#include +#include + +/** + * \file + * \ingroup scheduler + * Declaration of ns3::PriorityQueueScheduler class. + */ + +namespace ns3 { + +/** + * \ingroup scheduler + * \brief a std::map event scheduler + * + * This class implements the an event scheduler using an std::map + * data structure. + */ +class PriorityQueueScheduler : public Scheduler +{ +public: + /** + * Register this type. + * \return The object TypeId. + */ + static TypeId GetTypeId (void); + + /** Constructor. */ + PriorityQueueScheduler (); + /** Destructor. */ + virtual ~PriorityQueueScheduler (); + + // Inherited + virtual void Insert (const Scheduler::Event &ev); + virtual bool IsEmpty (void) const; + virtual Scheduler::Event PeekNext (void) const; + virtual Scheduler::Event RemoveNext (void); + virtual void Remove (const Event &ev); + +private: + + /** The event list. */ + std::priority_queue, + std::greater> m_list; +}; + +} // namespace ns3 + +#endif /* PRIORITY_QUEUE_SCHEDULER_H */ diff --git a/src/core/test/simulator-test-suite.cc b/src/core/test/simulator-test-suite.cc index 79ce7302f..6b47df7d4 100644 --- a/src/core/test/simulator-test-suite.cc +++ b/src/core/test/simulator-test-suite.cc @@ -23,6 +23,7 @@ #include "ns3/heap-scheduler.h" #include "ns3/map-scheduler.h" #include "ns3/calendar-scheduler.h" +#include "ns3/priority-queue-scheduler.h" using namespace ns3; @@ -480,5 +481,7 @@ public: AddTestCase (new SimulatorEventsTestCase (factory), TestCase::QUICK); factory.SetTypeId (CalendarScheduler::GetTypeId ()); AddTestCase (new SimulatorEventsTestCase (factory), TestCase::QUICK); + factory.SetTypeId (PriorityQueueScheduler::GetTypeId ()); + AddTestCase (new SimulatorEventsTestCase (factory), TestCase::QUICK); } } g_simulatorTestSuite; diff --git a/src/core/wscript b/src/core/wscript index 5b757fe70..cc5476b55 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -161,6 +161,7 @@ def build(bld): 'model/map-scheduler.cc', 'model/heap-scheduler.cc', 'model/calendar-scheduler.cc', + 'model/priority-queue-scheduler.cc', 'model/event-impl.cc', 'model/simulator.cc', 'model/simulator-impl.cc', @@ -253,6 +254,7 @@ def build(bld): 'model/map-scheduler.h', 'model/heap-scheduler.h', 'model/calendar-scheduler.h', + 'model/priority-queue-scheduler.h', 'model/simulation-singleton.h', 'model/singleton.h', 'model/timer.h', diff --git a/utils/bench-simulator.cc b/utils/bench-simulator.cc index a905adcc0..9727562b1 100644 --- a/utils/bench-simulator.cc +++ b/utils/bench-simulator.cc @@ -203,10 +203,11 @@ GetRandomStream (std::string filename) int main (int argc, char *argv[]) { - bool schedCal = false; - bool schedHeap = false; - bool schedList = false; - bool schedMap = true; + bool schedCal = false; + bool schedHeap = false; + bool schedList = false; + bool schedMap = true; + bool schedPriorityQueue = false; uint32_t pop = 100000; uint32_t total = 1000000; @@ -226,6 +227,7 @@ int main (int argc, char *argv[]) cmd.AddValue ("heap", "use HeapScheduler", schedHeap); cmd.AddValue ("list", "use ListSheduler", schedList); cmd.AddValue ("map", "use MapScheduler (default)", schedMap); + cmd.AddValue ("pri", "use PriorityQueue", schedPriorityQueue); cmd.AddValue ("debug", "enable debugging output", g_debug); cmd.AddValue ("pop", "event population size (default 1E5)", pop); cmd.AddValue ("total", "total number of events to run (default 1E6)", total); @@ -249,6 +251,11 @@ int main (int argc, char *argv[]) { factory.SetTypeId ("ns3::ListScheduler"); } + if (schedPriorityQueue) + { + factory.SetTypeId ("ns3::PriorityQueueScheduler"); + } + Simulator::SetScheduler (factory); LOGME (std::setprecision (g_fwidth - 6));