os/timer: interpolate time via timestamps

Previously, the Genode::Timer::curr_time always used the
Timer_session::elapsed_ms RPC as back end.  Now, Genode::Timer reads
this remote time only in a periodic fashion independently from the calls
to Genode::Timer::curr_time. If now one calls Genode::Timer::curr_time,
the function takes the last read remote time value and adapts it using
the timestamp difference since the remote-time read. The conversion
factor from timestamps to time is estimated on every remote-time read
using the last read remote-time value and the timestamp difference since
the last remote time read.

This commit also re-works the timeout test. The test now has two stages.
In the first stage, it tests fast polling of the
Genode::Timer::curr_time. This stage checks the error between locally
interpolated and timer-driver time as well as wether the locally
interpolated time is monotone and sufficiently homogeneous. In the
second stage several periodic and one-shot timeouts are scheduled at
once. This stage checks if the timeouts trigger sufficiently precise.

This commit adds the new Kernel::time syscall to base-hw. The syscall is
solely used by the Genode::Timer on base-hw as substitute for the
timestamp. This is because on ARM, the timestamp function uses the ARM
performance counter that stops counting when the WFI (wait for
interrupt) instruction is active. This instruction, however is used by
the base-hw idle contexts that get active when no user thread needs to
be scheduled.  Thus, the ARM performance counter is not a good choice for
time interpolation and we use the kernel internal time instead.

With this commit, the timeout library becomes a basic library. That means
that it is linked against the LDSO which then provides it to the program it
serves. Furthermore, you can't use the timeout library anymore without the
LDSO because through the kernel-dependent LDSO make-files we can achieve a
kernel-dependent timeout implementation.

This commit introduces a structured Duration type that shall successively
replace the use of Microseconds, Milliseconds, and integer types for duration
values.

Open issues:

* The timeout test fails on Raspberry PI because of precision errors in the
  first stage. However, this does not render the framework unusable in general
  on the RPI but merely is an issue when speaking of microseconds precision.

* If we run on ARM with another Kernel than HW the timestamp speed may
  continuously vary from almost 0 up to CPU speed. The Timer, however,
  only uses interpolation if the timestamp speed remained stable (12.5%
  tolerance) for at least 3 observation periods. Currently, one period is
  100ms, so its 300ms. As long as this is not the case,
  Timer_session::elapsed_ms is called instead.

  Anyway, it might happen that the CPU load was stable for some time so
  interpolation becomes active and now the timestamp speed drops. In the
  worst case, we would now have 100ms of slowed down time. The bad thing
  about it would be, that this also affects the timeout of the period.
  Thus, it might "freeze" the local time for more than 100ms.

  On the other hand, if the timestamp speed suddenly raises after some
  stable time, interpolated time can get too fast. This would shorten the
  period but nonetheless may result in drifting away into the far future.
  Now we would have the problem that we can't deliver the real time
  anymore until it has caught up because the output of Timer::curr_time
  shall be monotone. So, effectively local time might "freeze" again for
  more than 100ms.

  It would be a solution to not use the Trace::timestamp on ARM w/o HW but
  a function whose return value causes the Timer to never use
  interpolation because of its stability policy.

Fixes #2400
This commit is contained in:
Martin Stein
2017-04-22 00:52:23 +02:00
committed by Christian Helmuth
parent dc566b7d52
commit c70fed29f7
74 changed files with 1640 additions and 536 deletions

View File

@ -333,28 +333,28 @@ class Input_filter::Chargen_source : public Source, Source::Sink
*/
struct Char_repeater
{
Source::Sink &_destination;
Genode::Timer &_timer;
Source::Sink &_destination;
Timer::Connection &_timer;
Time_source::Microseconds const _delay;
Time_source::Microseconds const _rate;
Microseconds const _delay;
Microseconds const _rate;
Input::Event::Utf8 _curr_character { 0 };
enum State { IDLE, REPEAT } _state;
void _handle_timeout(Time_source::Microseconds)
void _handle_timeout(Duration)
{
if (_state == REPEAT) {
_destination.submit_event(Input::Event(_curr_character));
_timeout.start(_rate);
_timeout.schedule(_rate);
}
}
One_shot_timeout<Char_repeater> _timeout {
Timer::One_shot_timeout<Char_repeater> _timeout {
_timer, *this, &Char_repeater::_handle_timeout };
Char_repeater(Source::Sink &destination, Genode::Timer &timer,
Char_repeater(Source::Sink &destination, Timer::Connection &timer,
Xml_node node)
:
_destination(destination), _timer(timer),
@ -367,7 +367,7 @@ class Input_filter::Chargen_source : public Source, Source::Sink
_curr_character = character;
_state = REPEAT;
_timeout.start(_delay);
_timeout.schedule(_delay);
}
void cancel()

View File

@ -52,10 +52,9 @@ struct Input_filter::Main : Input_connection::Avail_handler,
{
struct Lazy
{
Timer::Connection connection;
Genode::Timer timer;
Timer::Connection timer;
Lazy(Env &env) : connection(env), timer(connection, env.ep()) { }
Lazy(Env &env) : timer(env) { }
};
Env &_env;
@ -67,7 +66,7 @@ struct Input_filter::Main : Input_connection::Avail_handler,
/**
* Timer_accessor interface
*/
Genode::Timer &timer() override
Timer::Connection &timer() override
{
if (!lazy.constructed())
lazy.construct(_env);

View File

@ -1,4 +1,4 @@
TARGET = input_filter
SRC_CC = main.cc
LIBS = base timeout
LIBS = base
INC_DIR += $(PRG_DIR)

View File

@ -15,10 +15,10 @@
#define _INPUT_FILTER__TIMER_ACCESSOR_H_
/* Genode includes */
#include <os/timer.h>
#include <timer_session/connection.h>
namespace Input_filter { struct Timer_accessor; }
struct Input_filter::Timer_accessor { virtual Genode::Timer &timer() = 0; };
struct Input_filter::Timer_accessor { virtual Timer::Connection &timer() = 0; };
#endif /* _INPUT_FILTER__TIMER_ACCESSOR_H_ */

View File

@ -54,17 +54,17 @@ Session_component_base(Allocator &guarded_alloc_backing,
** Session_component **
***********************/
Net::Session_component::Session_component(Allocator &alloc,
size_t const amount,
Ram_session &buf_ram,
size_t const tx_buf_size,
size_t const rx_buf_size,
Region_map &region_map,
Uplink &uplink,
Xml_node config,
Genode::Timer &timer,
unsigned &curr_time,
Entrypoint &ep)
Net::Session_component::Session_component(Allocator &alloc,
size_t const amount,
Ram_session &buf_ram,
size_t const tx_buf_size,
size_t const rx_buf_size,
Region_map &region_map,
Uplink &uplink,
Xml_node config,
Timer::Connection &timer,
unsigned &curr_time,
Entrypoint &ep)
:
Session_component_base(alloc, amount, buf_ram, tx_buf_size, rx_buf_size),
Session_rpc_object(region_map, _tx_buf, _rx_buf, &_range_alloc, ep.rpc_ep()),
@ -99,14 +99,14 @@ void Session_component::link_state_sigh(Signal_context_capability sigh)
** Root **
**********/
Net::Root::Root(Entrypoint &ep,
Allocator &alloc,
Uplink &uplink,
Ram_session &buf_ram,
Xml_node config,
Genode::Timer &timer,
unsigned &curr_time,
Region_map &region_map)
Net::Root::Root(Entrypoint &ep,
Allocator &alloc,
Uplink &uplink,
Ram_session &buf_ram,
Xml_node config,
Timer::Connection &timer,
unsigned &curr_time,
Region_map &region_map)
:
Root_component<Session_component, Genode::Single_client>(&ep.rpc_ep(),
&alloc),

View File

@ -93,7 +93,7 @@ class Net::Session_component : public Session_component_base,
Genode::Region_map &region_map,
Uplink &uplink,
Genode::Xml_node config,
Genode::Timer &timer,
Timer::Connection &timer,
unsigned &curr_time,
Genode::Entrypoint &ep);
@ -118,7 +118,7 @@ class Net::Root : public Genode::Root_component<Session_component,
Genode::Ram_session &_buf_ram;
Genode::Region_map &_region_map;
Genode::Xml_node _config;
Genode::Timer &_timer;
Timer::Connection &_timer;
unsigned &_curr_time;
@ -135,7 +135,7 @@ class Net::Root : public Genode::Root_component<Session_component,
Uplink &uplink,
Genode::Ram_session &buf_ram,
Genode::Xml_node config,
Genode::Timer &timer,
Timer::Connection &timer,
unsigned &curr_time,
Genode::Region_map &region_map);
};

View File

@ -28,7 +28,7 @@ void Interface::_handle_eth(void *const eth_base,
try {
Ethernet_frame &eth = *new (eth_base) Ethernet_frame(eth_size);
Interface &remote = _remote.deref();
unsigned new_time = _timer.curr_time().value / 1000;
unsigned new_time = _timer.curr_time().trunc_to_plain_us().value / 1000;
if (_log_time) {
log("\033[33m(", remote._label, " <- ", _label, ")\033[0m ", eth,
" \033[33mtime ", new_time, " (", new_time - _curr_time,
@ -86,12 +86,12 @@ void Interface::_ready_to_ack()
}
Interface::Interface(Entrypoint &ep,
Interface_label label,
Genode::Timer &timer,
unsigned &curr_time,
bool log_time,
Allocator &alloc)
Interface::Interface(Entrypoint &ep,
Interface_label label,
Timer::Connection &timer,
unsigned &curr_time,
bool log_time,
Allocator &alloc)
:
_sink_ack (ep, *this, &Interface::_ack_avail),
_sink_submit (ep, *this, &Interface::_ready_to_submit),

View File

@ -20,7 +20,7 @@
/* Genode includes */
#include <nic_session/nic_session.h>
#include <util/string.h>
#include <os/timer.h>
#include <timer_session/connection.h>
namespace Net {
@ -49,7 +49,7 @@ class Net::Interface
Genode::Allocator &_alloc;
Pointer<Interface> _remote;
Interface_label _label;
Genode::Timer &_timer;
Timer::Connection &_timer;
unsigned &_curr_time;
bool _log_time;
@ -77,7 +77,7 @@ class Net::Interface
Interface(Genode::Entrypoint &ep,
Interface_label label,
Genode::Timer &timer,
Timer::Connection &timer,
unsigned &curr_time,
bool log_time,
Genode::Allocator &alloc);

View File

@ -30,8 +30,7 @@ class Main
private:
Genode::Attached_rom_dataspace _config;
Timer::Connection _timer_connection;
Genode::Timer _timer;
Timer::Connection _timer;
unsigned _curr_time { 0 };
Genode::Heap _heap;
Uplink _uplink;
@ -45,8 +44,7 @@ class Main
Main::Main(Env &env)
:
_config(env, "config"), _timer_connection(env),
_timer(_timer_connection, env.ep()), _heap(&env.ram(), &env.rm()),
_config(env, "config"), _timer(env), _heap(&env.ram(), &env.rm()),
_uplink(env, _config.xml(), _timer, _curr_time, _heap),
_root(env.ep(), _heap, _uplink, env.ram(), _config.xml(), _timer,
_curr_time, env.rm())

View File

@ -1,6 +1,6 @@
TARGET = nic_dump
LIBS += base net config timeout
LIBS += base net config
SRC_CC += component.cc main.cc uplink.cc interface.cc

View File

@ -22,11 +22,11 @@ using namespace Net;
using namespace Genode;
Net::Uplink::Uplink(Env &env,
Xml_node config,
Genode::Timer &timer,
unsigned &curr_time,
Allocator &alloc)
Net::Uplink::Uplink(Env &env,
Xml_node config,
Timer::Connection &timer,
unsigned &curr_time,
Allocator &alloc)
:
Nic::Packet_allocator(&alloc),
Nic::Connection(env, this, BUF_SIZE, BUF_SIZE),

View File

@ -46,7 +46,7 @@ class Net::Uplink : public Nic::Packet_allocator,
Uplink(Genode::Env &env,
Genode::Xml_node config,
Genode::Timer &timer,
Timer::Connection &timer,
unsigned &curr_time,
Genode::Allocator &alloc);
};

View File

@ -55,7 +55,7 @@ Session_component_base(Allocator &guarded_alloc_backing,
***********************/
Net::Session_component::Session_component(Allocator &alloc,
Genode::Timer &timer,
Timer::Connection &timer,
size_t const amount,
Ram_session &buf_ram,
size_t const tx_buf_size,
@ -95,7 +95,7 @@ void Session_component::link_state_sigh(Signal_context_capability sigh)
**********/
Net::Root::Root(Entrypoint &ep,
Genode::Timer &timer,
Timer::Connection &timer,
Allocator &alloc,
Mac_address const &router_mac,
Configuration &config,

View File

@ -84,7 +84,7 @@ class Net::Session_component : public Session_component_base,
public:
Session_component(Genode::Allocator &alloc,
Genode::Timer &timer,
Timer::Connection &timer,
Genode::size_t const amount,
Genode::Ram_session &buf_ram,
Genode::size_t const tx_buf_size,
@ -110,7 +110,7 @@ class Net::Root : public Genode::Root_component<Session_component>
{
private:
Genode::Timer &_timer;
Timer::Connection &_timer;
Mac_allocator _mac_alloc;
Genode::Entrypoint &_ep;
Mac_address const _router_mac;
@ -128,7 +128,7 @@ class Net::Root : public Genode::Root_component<Session_component>
public:
Root(Genode::Entrypoint &ep,
Genode::Timer &timer,
Timer::Connection &timer,
Genode::Allocator &alloc,
Mac_address const &router_mac,
Configuration &config,

View File

@ -570,7 +570,7 @@ void Interface::_send(Ethernet_frame &eth, Genode::size_t const size)
Interface::Interface(Entrypoint &ep,
Genode::Timer &timer,
Timer::Connection &timer,
Mac_address const router_mac,
Genode::Allocator &alloc,
Mac_address const mac,

View File

@ -52,7 +52,7 @@ class Net::Interface
private:
Genode::Timer &_timer;
Timer::Connection &_timer;
Genode::Allocator &_alloc;
Domain &_domain;
Arp_cache _arp_cache;
@ -150,7 +150,7 @@ class Net::Interface
struct Packet_postponed : Genode::Exception { };
Interface(Genode::Entrypoint &ep,
Genode::Timer &timer,
Timer::Connection &timer,
Mac_address const router_mac,
Genode::Allocator &alloc,
Mac_address const mac,

View File

@ -115,7 +115,7 @@ Link::Link(Interface &cln_interface,
Pointer<Port_allocator_guard> const srv_port_alloc,
Interface &srv_interface,
Link_side_id const &srv_id,
Genode::Timer &timer,
Timer::Connection &timer,
Configuration &config,
uint8_t const protocol)
:
@ -127,11 +127,11 @@ Link::Link(Interface &cln_interface,
_close_timeout_us(_config.rtt_sec() * 2 * 1000 * 1000),
_protocol(protocol)
{
_close_timeout.start(_close_timeout_us);
_close_timeout.schedule(_close_timeout_us);
}
void Link::_handle_close_timeout(Genode::Timer::Microseconds)
void Link::_handle_close_timeout(Duration)
{
dissolve();
_client._interface.link_closed(*this, _protocol);
@ -167,7 +167,7 @@ Tcp_link::Tcp_link(Interface &cln_interface,
Pointer<Port_allocator_guard> const srv_port_alloc,
Interface &srv_interface,
Link_side_id const &srv_id,
Genode::Timer &timer,
Timer::Connection &timer,
Configuration &config,
uint8_t const protocol)
:
@ -179,7 +179,7 @@ Tcp_link::Tcp_link(Interface &cln_interface,
void Tcp_link::_fin_acked()
{
if (_server_fin_acked && _client_fin_acked) {
_close_timeout.start(_close_timeout_us);
_close_timeout.schedule(_close_timeout_us);
_closed = true;
}
}
@ -228,7 +228,7 @@ Udp_link::Udp_link(Interface &cln_interface,
Pointer<Port_allocator_guard> const srv_port_alloc,
Interface &srv_interface,
Link_side_id const &srv_id,
Genode::Timer &timer,
Timer::Connection &timer,
Configuration &config,
uint8_t const protocol)
:

View File

@ -15,7 +15,7 @@
#define _LINK_H_
/* Genode includes */
#include <os/timer.h>
#include <timer_session/connection.h>
#include <util/avl_tree.h>
#include <util/list.h>
#include <net/ipv4.h>
@ -129,13 +129,13 @@ class Net::Link : public Link_list::Element
Link_side _client;
Pointer<Port_allocator_guard> const _server_port_alloc;
Link_side _server;
Genode::One_shot_timeout<Link> _close_timeout;
Genode::Timer::Microseconds const _close_timeout_us;
Timer::One_shot_timeout<Link> _close_timeout;
Genode::Microseconds const _close_timeout_us;
Genode::uint8_t const _protocol;
void _handle_close_timeout(Genode::Timer::Microseconds);
void _handle_close_timeout(Genode::Duration);
void _packet() { _close_timeout.start(_close_timeout_us); }
void _packet() { _close_timeout.schedule(_close_timeout_us); }
public:
@ -146,7 +146,7 @@ class Net::Link : public Link_list::Element
Pointer<Port_allocator_guard> const srv_port_alloc,
Interface &srv_interface,
Link_side_id const &srv_id,
Genode::Timer &timer,
Timer::Connection &timer,
Configuration &config,
Genode::uint8_t const protocol);
@ -188,7 +188,7 @@ class Net::Tcp_link : public Link
Pointer<Port_allocator_guard> const srv_port_alloc,
Interface &srv_interface,
Link_side_id const &srv_id,
Genode::Timer &timer,
Timer::Connection &timer,
Configuration &config,
Genode::uint8_t const protocol);
@ -205,7 +205,7 @@ struct Net::Udp_link : Link
Pointer<Port_allocator_guard> const srv_port_alloc,
Interface &srv_interface,
Link_side_id const &srv_id,
Genode::Timer &timer,
Timer::Connection &timer,
Configuration &config,
Genode::uint8_t const protocol);

View File

@ -15,7 +15,6 @@
#include <base/component.h>
#include <base/heap.h>
#include <base/attached_rom_dataspace.h>
#include <os/timer.h>
#include <nic/xml_node.h>
#include <timer_session/connection.h>
@ -32,8 +31,7 @@ class Main
{
private:
Timer::Connection _timer_connection;
Genode::Timer _timer;
Timer::Connection _timer;
Genode::Heap _heap;
Genode::Attached_rom_dataspace _config_rom;
Configuration _config;
@ -48,12 +46,10 @@ class Main
Main::Main(Env &env)
:
_timer_connection(env), _timer(_timer_connection, env.ep()),
_heap(&env.ram(), &env.rm()), _config_rom(env, "config"),
_config(_config_rom.xml(), _heap),
_uplink(env, _timer, _heap, _config),
_root(env.ep(), _timer, _heap, _uplink.router_mac(), _config, env.ram(),
env.rm())
_timer(env), _heap(&env.ram(), &env.rm()), _config_rom(env, "config"),
_config(_config_rom.xml(), _heap), _uplink(env, _timer, _heap, _config),
_root(env.ep(), _timer, _heap, _uplink.router_mac(), _config,
env.ram(), env.rm())
{
env.parent().announce(env.ep().manage(_root));
}

View File

@ -1,6 +1,6 @@
TARGET = nic_router
LIBS += base net config timeout
LIBS += base net config
SRC_CC += arp_waiter.cc ip_rule.cc
SRC_CC += component.cc port_allocator.cc forward_rule.cc

View File

@ -24,7 +24,7 @@ using namespace Genode;
Net::Uplink::Uplink(Env &env,
Genode::Timer &timer,
Timer::Connection &timer,
Genode::Allocator &alloc,
Configuration &config)
:

View File

@ -47,7 +47,7 @@ class Net::Uplink : public Nic::Packet_allocator,
public:
Uplink(Genode::Env &env,
Genode::Timer &timer,
Timer::Connection &timer,
Genode::Allocator &alloc,
Configuration &config);