src/simulator/wall-clock-synchronizer.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 25 Feb 2010 14:17:21 +0100
changeset 6068 a2127017ecb4
parent 3642 00938a81ad10
child 6273 8d70de29d514
permissions -rw-r--r--
merge with HEAD
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 WALL_CLOCK_CLOCK_SYNCHRONIZER_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#define WALL_CLOCK_CLOCK_SYNCHRONIZER_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 "ns3/system-condition.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "synchronizer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
namespace ns3 {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
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
 * @brief Class used for synchronizing the simulation events to a real-time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
 * "wall clock" using Posix Clock functions.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
 * Enable this synchronizer using:
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
 *   DefaultValue::Bind ("Synchronizer", "WallClockSynchronizer");
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
 * before calling any simulator functions.
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
 * The simulation clock is maintained as a 64-bit integer in a unit specified
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
 * by the user through the TimeStepPrecision::Set function. This means that
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
 * it is not possible to specify event expiration times with anything better
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
 * than this user-specified accuracy.  
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
 * There are a couple of more issues at this level.  Posix clocks provide
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
 * access to several clocks we could use as a wall clock.  We don't care about
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
 * time in the sense of 0430 CEST, we care about some piece of hardware that
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
 * ticks at some regular period.  The most accurate posix clock in this
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
 * respect is the CLOCK_PROCESS_CPUTIME_ID clock.  This is a high-resolution
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
 * register in the CPU.  For example, on Intel machines this corresponds to
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
 * the timestamp counter (TSC) register.  The resolution of this counter will
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
 * be on the order of nanoseconds.
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
 * Now, just because we can measure time in nanoseconds doesn't mean we can
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
 * put our process to sleep to nanosecond resolution.  We are eventualy going
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
 * to use the function clock_nanosleep () to sleep until a simulation Time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
 * specified by the caller. 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
 * MORE ON JIFFIES, SLEEP, PROCESSES, etc., as required
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
 * Nanosleep takes a struct timespec as an input so we have to deal with
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
 * conversion between Time and struct timespec here.  They are both 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
 * interpreted as elapsed times.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
class WallClockSynchronizer : public Synchronizer
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
public:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  WallClockSynchronizer ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  virtual ~WallClockSynchronizer ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  static const uint64_t US_PER_NS = (uint64_t)1000;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  static const uint64_t US_PER_SEC = (uint64_t)1000000;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
  static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
protected:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
/**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
 * @brief Return true if this synchronizer is actually synchronizing to a
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
 * realtime clock.  The simulator sometimes needs to know this.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
 * @internal
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
 * Subclasses are expected to implement this method to tell the outside world
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
 * whether or not they are synchronizing to a realtime clock.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
 * @returns True if locked with realtime, false if not.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  virtual bool DoRealtime (void);
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
/**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
 * @brief Retrieve the value of the origin of the underlying normalized wall
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
 * clock time in nanosecond units.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
 * @internal
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
 * Subclasses are expected to implement this method to do the actual
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
 * real-time-clock-specific work of getting the current time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
 * @returns The normalized wall clock time (in nanosecond units).
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
 * @see TimeStepPrecision::Get
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
 * @see Synchronizer::SetOrigin
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
  virtual uint64_t DoGetCurrentRealtime (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
/**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
 * @brief Establish a correspondence between a simulation time and a 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
 * wall-clock (real) time.
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
 * @internal
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
 * There are three timelines involved here:  the simulation time, the 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
 * (absolute) wall-clock time and the (relative) synchronizer real time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
 * Calling this method makes a correspondence between the origin of the
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
 * synchronizer time and the current wall-clock time.
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
 * This method is expected to be called at the "instant" before simulation
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
 * begins.  At this point, simulation time = 0, and synchronizer time is
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
 * set = 0 in this method.  We then associate this time with the current
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
 * value of the real time clock that will be used to actually perform the
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
 * synchronization.
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
 * Subclasses are expected to implement this method to do the actual 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
 * real-time-clock-specific work of making the correspondence mentioned above.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
 * for example, this is where the differences between Time parameters and
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
 * parameters to clock_nanosleep would be dealt with. 
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
 * @param ns The simulation time we need to use as the origin (normalized to
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
 * nanosecond units).
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
  virtual void DoSetOrigin (uint64_t ns);
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
/**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
 * @brief Declaration of method used to retrieve drift between the real time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
 * clock used to synchronize the simulation and the current simulation time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
 * @internal
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
 * @param ns Simulation timestep from the simulator normalized to nanosecond 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
 * steps.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
 * @returns Drift in nanosecond units.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
 * @see TimeStepPrecision::Get
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
 * @see Synchronizer::SetOrigin
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
 * @see Synchronizer::GetDrift
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
  virtual int64_t DoGetDrift (uint64_t ns);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
/**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
 * @brief Wait until the real time is in sync with the specified simulation
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
 * time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
 * @internal
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
 * This is where the real work of synchronization is done.  The Time passed
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
 * in as a parameter is the simulation time.  The job of Synchronize is to
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
 * translate from simulation time to synchronizer time (in a perfect world
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
 * this is the same time) and then figure out how long in real-time it needs
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
 * to wait until that synchronizer / simulation time comes around.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
 * Subclasses are expected to implement this method to do the actual
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
 * real-time-clock-specific work of waiting (either busy-waiting or sleeping,
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
 * or some combination) until the requested simulation time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
 * @param ns The simulation time we need to wait for (normalized to nanosecond
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
 * units).
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
 * @see TimeStepPrecision::Get
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  virtual bool DoSynchronize (uint64_t nsCurrent, uint64_t nsDelay);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  virtual void DoSignal (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
  virtual void DoSetCondition (bool cond);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
  virtual void DoEventStart (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
  virtual uint64_t DoEventEnd (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
  bool SpinWait (uint64_t);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
  bool SleepWait (uint64_t);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
  uint64_t DriftCorrect (uint64_t nsNow, uint64_t nsDelay);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
  uint64_t GetRealtime (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
  uint64_t GetNormalizedRealtime (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
  void NsToTimeval (int64_t ns, struct timeval *tv);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
  uint64_t TimevalToNs (struct timeval *tv);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  void TimevalAdd (
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
    struct timeval *tv1, 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
    struct timeval *tv2,
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
    struct timeval *result);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
  uint64_t m_cpuTick;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
  uint64_t m_realtimeTick;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
  uint64_t m_jiffy;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
  uint64_t m_nsEventStart;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
  SystemCondition m_condition;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
};
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
}; // namespace ns3
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
#endif /* WALL_CLOCK_SYNCHRONIZER_H */