use ns as internal time and export time as ns.

This commit is contained in:
Mathieu Lacage
2006-09-04 13:11:34 +02:00
parent b77c4b2bdd
commit a222d1b8da
11 changed files with 112 additions and 65 deletions

View File

@@ -15,20 +15,22 @@ private:
void
MyModel::start (void)
{
Simulator::schedule (RelTimeS (10.0),
Simulator::schedule (Time::rel_s (10.0),
&MyModel::deal_with_event,
this, Simulator::now ().s ());
}
void
MyModel::deal_with_event (double value)
{
std::cout << "Member method received event at " << Simulator::now ().s () << " started at " << value << std::endl;
std::cout << "Member method received event at " << Simulator::now ().s () <<
"s started at " << value << "s" << std::endl;
}
static void
random_function (MyModel *model)
{
std::cout << "random function received event at " << Simulator::now ().s () << std::endl;
std::cout << "random function received event at " <<
Simulator::now ().s () << "s" << std::endl;
model->start ();
}
@@ -37,7 +39,7 @@ int main (int argc, char *argv[])
{
MyModel model;
Simulator::schedule (AbsTimeS (10.0), &random_function, &model);
Simulator::schedule (Time::abs_s (10.0), &random_function, &model);
Simulator::run ();

View File

@@ -25,13 +25,13 @@ namespace ns3 {
EventId::EventId ()
: m_event_impl (0),
m_time (0),
m_ns (0),
m_uid (0)
{}
EventId::EventId (EventImpl *impl, uint64_t time, uint32_t uid)
EventId::EventId (EventImpl *impl, uint64_t ns, uint32_t uid)
: m_event_impl (impl),
m_time (time),
m_ns (ns),
m_uid (uid)
{}
void
@@ -50,9 +50,9 @@ EventId::get_event_impl (void) const
return m_event_impl;
}
uint64_t
EventId::get_time (void) const
EventId::get_ns (void) const
{
return m_time;
return m_ns;
}
uint32_t
EventId::get_uid (void) const

View File

@@ -30,7 +30,7 @@ class EventImpl;
class EventId {
public:
EventId ();
EventId (EventImpl *impl, uint64_t time, uint32_t uid);
EventId (EventImpl *impl, uint64_t ns, uint32_t uid);
void cancel (void);
bool is_expired (void);
public:
@@ -39,11 +39,11 @@ public:
* subclasses of the Scheduler base class.
*/
EventImpl *get_event_impl (void) const;
uint64_t get_time (void) const;
uint64_t get_ns (void) const;
uint32_t get_uid (void) const;
private:
EventImpl *m_event_impl;
uint64_t m_time;
uint64_t m_ns;
uint32_t m_uid;
};

View File

@@ -197,7 +197,7 @@ SchedulerHeap::insert (EventImpl *event, Scheduler::EventKey key)
m_heap.push_back (std::make_pair (event, key));
bottom_up ();
store_in_event (event, last ());
return EventId (event, key.m_time, key.m_uid);
return EventId (event, key.m_ns, key.m_uid);
}
EventImpl *
@@ -240,7 +240,7 @@ SchedulerHeap::is_valid (EventId id)
EventImpl *ev = id.get_event_impl ();
uint32_t i = get_from_event (ev);
Scheduler::EventKey key = m_heap[i].second;
return (key.m_time == id.get_time () &&
return (key.m_ns == id.get_ns () &&
key.m_uid == id.get_uid ());
}
}; // namespace ns3

View File

@@ -45,7 +45,7 @@ SchedulerList::get_event_id (Scheduler::EventKey key, EventsI i)
memcpy ((char *)&(internal_iterator), (char *)&i, sizeof (void *));
EventImpl *ev = i->first;
ev->set_internal_iterator (internal_iterator);
return EventId (ev, key.m_time, key.m_uid);
return EventId (ev, key.m_ns, key.m_uid);
}
SchedulerList::EventsI
SchedulerList::get_iterator (EventId id)
@@ -101,7 +101,7 @@ SchedulerList::remove (EventId id, Scheduler::EventKey *key)
{
EventsI i = get_iterator (id);
*key = i->second;
assert (key->m_time == id.get_time () &&
assert (key->m_ns == id.get_ns () &&
key->m_uid == id.get_uid ());
EventImpl *ev = i->first;
m_events.erase (i);
@@ -113,7 +113,7 @@ SchedulerList::is_valid (EventId id)
{
EventsI i = get_iterator (id);
Scheduler::EventKey key = i->second;
return (key.m_time == id.get_time () &&
return (key.m_ns == id.get_ns () &&
key.m_uid == id.get_uid ());
}

View File

@@ -66,7 +66,7 @@ SchedulerMap::insert (EventImpl *event, Scheduler::EventKey key)
std::pair<EventMapI,bool> result = m_list.insert (std::make_pair (key, event));
assert (result.second);
store_in_event (event, result.first);
return EventId (event, key.m_time, key.m_uid);
return EventId (event, key.m_ns, key.m_uid);
}
bool
@@ -113,7 +113,7 @@ SchedulerMap::is_valid (EventId id)
{
EventMapI i = get_from_event (id.get_event_impl ());
Scheduler::EventKey key = i->first;
return (key.m_time == id.get_time () &&
return (key.m_ns == id.get_ns () &&
key.m_uid == id.get_uid ());
}

View File

@@ -35,9 +35,9 @@ bool
ns3::Scheduler::EventKeyCompare::operator () (struct EventKey a, struct EventKey b)
{
assert (a.m_uid != b.m_uid);
if (a.m_time < b.m_time) {
if (a.m_ns < b.m_ns) {
return true;
} else if (a.m_time == b.m_time && a.m_uid < b.m_uid) {
} else if (a.m_ns == b.m_ns && a.m_uid < b.m_uid) {
return true;
} else {
return false;

View File

@@ -32,7 +32,7 @@ class EventImpl;
class Scheduler {
public:
struct EventKey {
uint64_t m_time;
uint64_t m_ns;
uint32_t m_uid;
};
class EventKeyCompare {

View File

@@ -75,7 +75,7 @@ private:
Scheduler *m_events;
uint32_t m_uid;
uint32_t m_current_uid;
uint64_t m_current_us;
uint64_t m_current_ns;
std::ofstream m_log;
std::ifstream m_input_log;
bool m_log_enable;
@@ -91,7 +91,7 @@ SimulatorPrivate::SimulatorPrivate (Scheduler *events)
m_events = events;
m_uid = 0;
m_log_enable = false;
m_current_us = 0;
m_current_ns = 0;
}
SimulatorPrivate::~SimulatorPrivate ()
@@ -122,10 +122,10 @@ SimulatorPrivate::process_one_event (void)
Scheduler::EventKey next_key = m_events->peek_next_key ();
m_events->remove_next ();
TRACE ("handle " << next_ev);
m_current_us = next_key.m_time;
m_current_ns = next_key.m_ns;
m_current_uid = next_key.m_uid;
if (m_log_enable) {
m_log << "e "<<next_key.m_uid << " " << next_key.m_time << std::endl;
m_log << "e "<<next_key.m_uid << " " << next_key.m_ns << std::endl;
}
next_ev->invoke ();
delete next_ev;
@@ -141,7 +141,7 @@ SimulatorPrivate::next (void) const
{
assert (!m_events->is_empty ());
Scheduler::EventKey next_key = m_events->peek_next_key ();
return AbsTimeUs (next_key.m_time);
return AbsTimeUs (next_key.m_ns);
}
@@ -149,7 +149,7 @@ void
SimulatorPrivate::run (void)
{
while (!m_events->is_empty () && !m_stop &&
(m_stop_at == 0 || m_stop_at > next ().us ())) {
(m_stop_at == 0 || m_stop_at > next ().ns ())) {
process_one_event ();
}
m_log.close ();
@@ -164,7 +164,7 @@ SimulatorPrivate::stop (void)
void
SimulatorPrivate::stop_at (Time at)
{
m_stop_at = at.us ();
m_stop_at = at.ns ();
}
EventId
SimulatorPrivate::schedule (Time time, EventImpl *event)
@@ -172,18 +172,18 @@ SimulatorPrivate::schedule (Time time, EventImpl *event)
if (time.is_destroy ()) {
m_destroy.push_back (std::make_pair (event, m_uid));
if (m_log_enable) {
m_log << "id " << m_current_uid << " " << now ().us () << " "
m_log << "id " << m_current_uid << " " << now ().ns () << " "
<< m_uid << std::endl;
}
m_uid++;
//XXX
return EventId ();
}
assert (time.us () >= now ().us ());
Scheduler::EventKey key = {time.us (), m_uid};
assert (time.ns () >= now ().ns ());
Scheduler::EventKey key = {time.ns (), m_uid};
if (m_log_enable) {
m_log << "i "<<m_current_uid<<" "<<now ().us ()<<" "
<<m_uid<<" "<<time.us () << std::endl;
m_log << "i "<<m_current_uid<<" "<<now ().ns ()<<" "
<<m_uid<<" "<<time.ns () << std::endl;
}
m_uid++;
return m_events->insert (event, key);
@@ -191,7 +191,7 @@ SimulatorPrivate::schedule (Time time, EventImpl *event)
Time
SimulatorPrivate::now (void) const
{
return AbsTimeUs (m_current_us);
return Time::abs_ns (m_current_ns);
}
void
@@ -201,8 +201,8 @@ SimulatorPrivate::remove (EventId ev)
EventImpl *impl = m_events->remove (ev, &key);
delete impl;
if (m_log_enable) {
m_log << "r " << m_current_uid << " " << now ().us () << " "
<< key.m_uid << " " << key.m_time << std::endl;
m_log << "r " << m_current_uid << " " << now ().ns () << " "
<< key.m_uid << " " << key.m_ns << std::endl;
}
}
@@ -218,7 +218,7 @@ bool
SimulatorPrivate::is_expired (EventId ev)
{
if (ev.get_event_impl () != 0 &&
ev.get_time () <= now ().us () &&
ev.get_ns () <= now ().ns () &&
ev.get_uid () < m_current_uid) {
return false;
}

View File

@@ -24,36 +24,43 @@
namespace ns3 {
Time::Time ()
: m_us (0),
: m_ns (0),
m_is_destroy (true)
{}
Time::Time (Time const &o)
: m_us (o.m_us),
: m_ns (o.m_ns),
m_is_destroy (o.m_is_destroy)
{}
Time &
Time::operator = (Time const &o)
{
m_us = o.m_us;
m_ns = o.m_ns;
m_is_destroy = o.m_is_destroy;
return *this;
}
Time::Time (uint64_t us)
: m_us (us),
Time::Time (uint64_t ns)
: m_ns (ns),
m_is_destroy (false)
{}
double
Time::s (void) const
{
double us = m_us;
us /= 1000000;
return us;
double ns = m_ns;
ns /= 1000000000;
return ns;
}
uint64_t
Time::us (void) const
{
return m_us;
uint64_t us = m_ns / 1000;
return us;
}
uint64_t
Time::ns (void) const
{
return m_ns;
}
bool
@@ -62,34 +69,67 @@ Time::is_destroy (void) const
return m_is_destroy;
}
Time
operator + (Time const &lhs, uint64_t delta_us)
Time
Time::abs_s (double s)
{
return AbsTimeUs (lhs.us () + delta_us);
int64_t ns = (int64_t)(s * 1000000000.0);
return Time (ns);
}
Time
operator + (Time const &lhs, double delta_s)
Time
Time::abs_us (uint64_t us)
{
uint64_t delta_us = (uint64_t)(int64_t)(delta_s * 1000000.0);
return AbsTimeUs (lhs.us () + delta_us);
int64_t ns = us * 1000;
return Time (ns);
}
Time
Time::abs_ns (uint64_t ns)
{
return Time (ns);
}
Time
Time::rel_s (double s)
{
int64_t ns = (int64_t)(s * 1000000000.0);
return Time (Simulator::now ().ns () + ns);
}
Time
Time::rel_us (uint64_t us)
{
return Time (Simulator::now ().ns () + us * 1000);
}
Time
Time::rel_ns (uint64_t ns)
{
return Time (Simulator::now ().ns () + ns);
}
Time
Time::now (void)
{
return Time (Simulator::now ().ns ());
}
Time
Time::destroy (void)
{
return Time ();
}
AbsTimeS::AbsTimeS (double s)
: Time ((uint64_t)(int64_t)(s * 1000000.0))
: Time ((uint64_t)(int64_t)(s * 1000000000.0))
{}
AbsTimeUs::AbsTimeUs (uint64_t us)
: Time (us)
: Time (us*1000)
{}
RelTimeS::RelTimeS (double s)
: Time (Simulator::now () + s)
: Time (Simulator::now ().ns () + (int64_t)(s * 1000000000.0))
{}
RelTimeUs::RelTimeUs (uint64_t us)
: Time (Simulator::now () + us)
: Time (Simulator::now ().ns () + us*1000)
{}
NowTime::NowTime ()
: Time (Simulator::now ().us ())
: Time (Simulator::now ().ns ())
{}
DestroyTime::DestroyTime ()

View File

@@ -31,19 +31,24 @@ public:
Time &operator = (Time const &o);
double s (void) const;
uint64_t us (void) const;
uint64_t ns (void) const;
bool is_destroy (void) const;
static Time abs_s (double s);
static Time abs_us (uint64_t us);
static Time abs_ns (uint64_t ns);
static Time rel_s (double s);
static Time rel_us (uint64_t us);
static Time rel_ns (uint64_t ns);
static Time now (void);
static Time destroy (void);
protected:
Time (uint64_t us);
Time (uint64_t ns);
Time ();
private:
uint64_t m_us;
uint64_t m_ns;
bool m_is_destroy;
};
Time operator + (Time const &lhs, uint64_t delta);
Time operator + (Time const &lhs, double delta);
class AbsTimeS : public Time {
public:
AbsTimeS (double s);