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.
This commit is contained in:
Alexander Krotov
2020-02-25 14:16:46 -08:00
committed by Peter Barnes
parent 4434c65c7a
commit 82b555582e
5 changed files with 187 additions and 4 deletions

View File

@@ -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 <krotov@iitp.ru>
*/
#include "priority-queue-scheduler.h"
#include "event-impl.h"
#include "assert.h"
#include "log.h"
#include <string>
/**
* \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<Scheduler> ()
.SetGroupName ("Core")
.AddConstructor<PriorityQueueScheduler> ()
;
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

View File

@@ -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 <mathieu.lacage@sophia.inria.fr>
*/
#ifndef PRIORITY_QUEUE_SCHEDULER_H
#define PRIORITY_QUEUE_SCHEDULER_H
#include "scheduler.h"
#include <stdint.h>
#include <queue>
#include <utility>
/**
* \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<Scheduler::Event,
std::vector<Scheduler::Event>,
std::greater<Scheduler::Event>> m_list;
};
} // namespace ns3
#endif /* PRIORITY_QUEUE_SCHEDULER_H */

View File

@@ -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;

View File

@@ -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',

View File

@@ -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));