X-Simulator
Create: 17 September 2007;  Last review: 18 September 2007
Clock module
Introduction The clock model used in the X-Simulator is based on the Unix clock model, as reported also in the RFC-1305. Before introducing this model, we briefly explain the general model for a clock.
A clock is a very simple device, obtained by an oscillator and a counter.
The oscillator can be any physical device, such as a pendulum or a quartz crystal or it can be an electromagnetic signal. For instance an atomic clock uses an oscillation associates with a quantum transition between two energy level in an atom.
The counter keeps track of the number of oscillations that have occured. After being set initially, the clock can then provide its estimate of the correct time by adding up the number of oscillations, also called clock cycles.
In principle, if a clock was set perfectly and if its frequency (the frequency of the oscillator) remained perfect, it would keep the correct time indefinitely. However, this is not the case for different reasons. The quality of a clock depends on how well it is set, how accurate and stable its frequency is, and the degree of immunity the clock has to environmental changes.

With the variable t, called the epoch, it is indicated the reference time scale. It corresponds to the reading of a counter that runs at the standard unit of frequency and began counting at some initial epoch t0.
The standard unit of frequency corresponds to the inverse of the standard unit of time interval u, where the time interval is defined by the International Standard (SI) in terms of the standard second. A standard second corresponds to the duration of 9,192,631,770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the cesium-133 atom.
For the purpose of our work instead, the reference time scale, representing with the variable t, corresponds to the reading of a reference clock, which is a simply better clock, that usually does not correspond to an atomic clock!
In the following the reference clock is the master clock.

Now, we indicate with the variable C(t) the time displayed by our clock (which corresponds to a slave clock) at the time t relative to the master timescale. The relationship between these two timescales are:

C(t) =  1/2*D(t0)*[t-t0]2 +  R(t0)*[t-t0] +  C(t0) +  w(t)  .

  • The coefficient of the second-order term, D(t0), represents the fractional frequency drift per unit time and it is usually ignored.
  • The coefficient of the first-order term, R(t0), represents the frequency. In a stationary model this quantity can be assumed constant or changing slowly.
  • The constant term C(t0) gives the time measured by the our clock at some previous epoch t0.
  • Finally, the term x(t) is a random variable which represents the random noise (jitter); it is usually modelled by a normal distribution with a given autocorrelation function.

This holds to the following, simplified, relationship:

C(t) =  R*[t-t0] +  C(t0) +  w(t)  .

If the clock is perfectly synchronized with the reference time clock, its frequency is unitary, that is R=1. In the other cases, the clock frequency is different from the unitary value and the frequency can be represented by a model that takes into account the deviation from the unit:

R =  1 + DRIFT

Finally, we can obtain the model used in the simulator:

C(t) = (1 + DRIFT)*(t-t0) +  C(t0) +  w(t)  .


Unix clock model The Unix 4.3bsd clock model is based on two system calls, setimeofday and adjtime, and on two kernel variables, tick and tickadj.

The settimeoday call resets the kernel clock to the value given. Let value be the value passed with this system call. The effect of this system call is to initialize the variable C(t0)=value and the variable t0 to the time instant during which the system call was invoked.

The adjtime call instead, slews the kernel clock. The clock usually run at a rate that corresponds to the intrinsic frequency of the particular oscillator used as the clock source, representing by the tick value. The rate of the clock can be varied, such as the clock can actually run at three different rates, one correpsonding to tick, one to tick+tickadj and the last to tick-tickadj. These last two rates are used when the adjtime system call is invoked. The interval during which one or the other of the two rates are used is calculated by:

T =  value * tick / tickadj ,

where value is the parameter passed by the adjtime system call.
The effect is to slew the clock to a new value at a small, constant rate, rather than incorporate the adjustment all at once, which could cause the clock to be set backward.
One of the assumptions made in the Unix clock model is that the period T of adjustment computed in the adjtime call must be completed before the next call is made. If not, this results in an error message to the system log.



Implementation In the X-Simulator, the system clock is implementing by the clock module. This simple module include both the clock model and the system calls. It is a module of the coumpound slave module and furthermore, this module only exchanges messages with the node module.

The messages that the node module can send to the clock one are:
  • Timestamp request: it requires to the clock a timestamp value. When the clock receives this kind of message, it sends to the node module a packet containing the timestamp value. This value, indicated in the following by S[C(t)], is generated by adding at the previously described clock model C(t) a timestamp noise n(t):

    S[C(t)]=  C(t) +  n(t)  =
    = (1 + DRIFT)*(t-t0) +  C(t0) +  w(t) +  n(t) =
    = (1 + DRIFT)*(t-t0) +  C(t0) +  v(t)  ,
    where v(t) represents the sum of the noisy contributes. It is generated by a superposition of a white noise and a Bernoulli process, which models glitches that may occur in the timestamp mechanism.
  • Offset correction, that corresponds to the settimeofday system call. The clock sets the C(t0) value, called offset in the function, at the value:

    C(t0)=C(t) -  value  =
    =C(t0,previous) + (1+DRIFT)*(t -  t0,previous) -  value  .

    Then, the clock sets the t0 value, called in the function origine, at the current time.
  • Drift correction, that corresponds to the adjtime system call. The clock module sets its DRIFT to the new value given by:

    DRIFT  =  DRIFTprevious -  value  ,

    where value is the parameter of the message, which implements the system call. Usually value corresponds to an estimate of he drift parameter. This kind of correction is slightly different from the correction made by the adjtime system call. In fact, the drift adjustment is made all at once by the clock module. The entity of this adjustment, specified by value, is determined by the node module.
The clock module instead sends only one kind of message to the node module, a timestamp response message, containing a timestamp value.