src/simulator/realtime-simulator-impl.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 15 Oct 2008 14:51:16 +0200
changeset 3811 4eb8e926c6d7
parent 3810 ca47c7015ea4
child 3812 6cca59a0fca6
permissions -rw-r--r--
rename RealtimeSimulator to WallclockSimulator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * Copyright (c) 2008 University of Washington
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * GNU General Public License for more details.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#ifndef REALTIME_SIMULATOR_IMPL_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#define REALTIME_SIMULATOR_IMPL_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "simulator-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "scheduler.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include "synchronizer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "event-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include "ns3/ptr.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include "ns3/assert.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
#include "ns3/log.h"
3650
516c4f22d251 Missing include
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3566
diff changeset
    31
#include "ns3/system-mutex.h"
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
#include <list>
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
namespace ns3 {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
class RealtimeEventLock : public EventLock
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
public:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
  void Lock (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
  void Unlock (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
private:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
  SystemMutex m_eventMutex;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
};
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
class RealtimeSimulatorImpl : public SimulatorImpl
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
public:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
  static TypeId GetTypeId (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
   * Enumeration of the types of packets supported in the class.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
   *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
  enum SynchronizationMode {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
    SYNC_BEST_EFFORT, /** Make a best effort to keep synced to real-time */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
    SYNC_HARD_LIMIT, /** Keep to real-time within a tolerance or die trying */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
  };
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
  RealtimeSimulatorImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
  ~RealtimeSimulatorImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    63
  virtual void Destroy ();
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    64
  virtual bool IsFinished (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    65
  virtual Time Next (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    66
  virtual void Stop (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    67
  virtual void Stop (Time const &time);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    68
  virtual EventId Schedule (Time const &time, EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    69
  virtual EventId ScheduleNow (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    70
  virtual EventId ScheduleDestroy (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    71
  virtual void Remove (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    72
  virtual void Cancel (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    73
  virtual bool IsExpired (const EventId &ev) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    74
  virtual void Run (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    75
  virtual void RunOneEvent (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    76
  virtual Time Now (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    77
  virtual Time GetDelayLeft (const EventId &id) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    78
  virtual Time GetMaximumSimulationTime (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    79
  virtual void SetScheduler (Ptr<Scheduler> scheduler);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    80
  virtual Ptr<Scheduler> GetScheduler (void) const;
3811
4eb8e926c6d7 rename RealtimeSimulator to WallclockSimulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3810
diff changeset
    81
  virtual void ScheduleWallclock (Time const &time, EventImpl *event);
4eb8e926c6d7 rename RealtimeSimulator to WallclockSimulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3810
diff changeset
    82
  virtual void ScheduleWallclockNow (EventImpl *event);
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
    83
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
    84
  Time RealtimeNow (void) const;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  void SetSynchronizationMode (RealtimeSimulatorImpl::SynchronizationMode mode);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  void SetHardLimit (Time limit);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
  Time GetHardLimit (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
private:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  bool Running (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
  bool Realtime (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  void ProcessOneEvent (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  uint64_t NextTs (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
  typedef std::list<EventId> DestroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
  DestroyEvents m_destroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
  uint64_t m_stopAt;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  bool m_stop;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  bool m_running;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  // The following variables are protected using the m_mutex
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
  Ptr<Scheduler> m_events;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  int m_unscheduledEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  uint32_t m_uid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  uint32_t m_currentUid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  uint64_t m_currentTs;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  mutable SystemMutex m_mutex;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  RealtimeEventLock m_eventLock;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  Ptr<Synchronizer> m_synchronizer;
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   116
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
   * The policy to use if the simulation cannot keep synchronized to real-time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  SynchronizationMode m_synchronizationMode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
   * The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
  Time m_hardLimit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
};
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
} // namespace ns3
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
#endif /* REALTIME_SIMULATOR_IMPL_H */