native: Add command line options to control real timeness

For the native_posix board,
added the command line options -rt and -no-rt to control
if the execution should be slowed down to real time or not.

CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME still works, but
now it just sets a default.

Signed-off-by: Alberto Escolar Piedras <alpi@oticon.com>
This commit is contained in:
Alberto Escolar Piedras 2018-05-26 15:20:21 +02:00 committed by Anas Nashif
parent 452cb61844
commit 477b5497a7
4 changed files with 68 additions and 21 deletions

View file

@ -9,6 +9,7 @@
#include "cmdline_common.h"
#include "zephyr/types.h"
#include "hw_models_top.h"
#include "timer_model.h"
#include "cmdline.h"
#include "toolchain.h"
#include "board.h"
@ -28,6 +29,20 @@ static void cmd_stop_at_found(char *argv, int offset)
hwm_set_end_of_time(args.stop_at*1e6);
}
static void cmd_realtime_found(char *argv, int offset)
{
ARG_UNUSED(argv);
ARG_UNUSED(offset);
hwtimer_set_real_time(true);
}
static void cmd_no_realtime_found(char *argv, int offset)
{
ARG_UNUSED(argv);
ARG_UNUSED(offset);
hwtimer_set_real_time(false);
}
#if defined(CONFIG_FAKE_ENTROPY_NATIVE_POSIX)
extern void entropy_native_posix_set_seed(unsigned int seed_i);
static void cmd_seed_found(char *argv, int offset)
@ -71,8 +86,20 @@ void native_handle_cmd_line(int argc, char *argv[])
* destination, callback,
* description
*/
{false, false, true,
"rt", "", 'b',
NULL, cmd_realtime_found,
"Slow down the execution to the host real time"},
{false, false, true,
"no-rt", "", 'b',
NULL, cmd_no_realtime_found,
"Do NOT slow down the execution to real time, but advance "
"Zephyr's time as fast as possible and decoupled from the host "
"time"},
{false, false, false,
"stop_at", "time", 'd',
"stop_at", "time", 'd',
(void *)&args.stop_at, cmd_stop_at_found,
"In simulated seconds, when to stop automatically"},

View file

@ -164,6 +164,10 @@ available options::
$ zephyr/zephyr.exe --help
[-h] [--h] [--help] [-?] :Display this help
[-rt] :Slow down the execution to the host real time
[-no-rt] :Do NOT slow down the execution to real time, but
advance Zephyr's time as fast as possible and
decoupled from the host time
[-stop_at=<time>] :In simulated seconds, when to stop automatically
[-seed=<r_seed>] :Seed for the entropy device
[-testargs <arg>...] :Any argument that follows will be ignored
@ -374,8 +378,7 @@ The following peripherals are currently provided with this board:
This peripheral driver also provides the needed functionality for this
architecture-specific :c:func:`k_busy_wait`.
This timer, is configured by default with
:option:`CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME`
This timer, may be configured
to slow down the execution to real host time.
This will provide the illusion that the simulated time is running at the same
speed as the real host time.
@ -384,6 +387,9 @@ The following peripherals are currently provided with this board:
the execution before raising its next timer interrupt.
Normally the Zephyr application and HW models run in very little time
on the host CPU, so this is a good enough approach.
To configure the timer to slow down the execution, either set the option
:option:`CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME`
or use the command line switch --rt.
**Entropy device**:
An entropy device based on the host :c:func:`random` API.

View file

@ -9,9 +9,12 @@
*
* If you want this timer model to slow down the execution to real time
* set (CONFIG_)NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME
* You can also control it with the --rt and --no-rt options from command line
*/
#include <stdint.h>
#include <time.h>
#include <stdbool.h>
#include "hw_models_top.h"
#include "irq_ctrl.h"
#include "board_soc.h"
@ -28,15 +31,23 @@ u64_t hw_timer_awake_timer;
static u64_t tick_p; /* Period of the ticker */
static s64_t silent_ticks;
static bool real_time =
#if (CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME)
#include <time.h>
true;
#else
false;
#endif
static u64_t Boot_time;
static struct timespec tv;
#endif
extern u64_t posix_get_hw_cycle(void);
void hwtimer_set_real_time(bool new_rt)
{
real_time = new_rt;
}
static void hwtimer_update_timer(void)
{
hw_timer_timer = min(hw_timer_tick_timer, hw_timer_awake_timer);
@ -48,10 +59,10 @@ void hwtimer_init(void)
hw_timer_tick_timer = NEVER;
hw_timer_awake_timer = NEVER;
hwtimer_update_timer();
#if (CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME)
clock_gettime(CLOCK_MONOTONIC, &tv);
Boot_time = tv.tv_sec*1e6 + tv.tv_nsec/1000;
#endif
if (real_time) {
clock_gettime(CLOCK_MONOTONIC, &tv);
Boot_time = tv.tv_sec*1e6 + tv.tv_nsec/1000;
}
}
void hwtimer_cleanup(void)
@ -72,24 +83,25 @@ void hwtimer_enable(u64_t period)
static void hwtimer_tick_timer_reached(void)
{
#if (CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME)
u64_t expected_realtime = Boot_time + hw_timer_tick_timer;
if (real_time) {
u64_t expected_realtime = Boot_time + hw_timer_tick_timer;
clock_gettime(CLOCK_MONOTONIC, &tv);
u64_t actual_real_time = tv.tv_sec*1e6 + tv.tv_nsec/1000;
clock_gettime(CLOCK_MONOTONIC, &tv);
u64_t actual_real_time = tv.tv_sec*1e6 + tv.tv_nsec/1000;
int64_t diff = expected_realtime - actual_real_time;
int64_t diff = expected_realtime - actual_real_time;
if (diff > 0) { /* we need to slow down */
struct timespec requested_time;
struct timespec remaining;
if (diff > 0) { /* we need to slow down */
struct timespec requested_time;
struct timespec remaining;
requested_time.tv_sec = diff / 1e6;
requested_time.tv_nsec = (diff - requested_time.tv_sec*1e6)*1e3;
requested_time.tv_sec = diff / 1e6;
requested_time.tv_nsec = (diff -
requested_time.tv_sec*1e6)*1e3;
nanosleep(&requested_time, &remaining);
nanosleep(&requested_time, &remaining);
}
}
#endif
hw_timer_tick_timer += tick_p;
hwtimer_update_timer();

View file

@ -8,6 +8,7 @@
#define _NATIVE_POSIX_TIMER_MODEL_H
#include "hw_models_top.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
@ -15,6 +16,7 @@ extern "C" {
void hwtimer_init(void);
void hwtimer_cleanup(void);
void hwtimer_set_real_time(bool new_rt);
void hwtimer_timer_reached(void);
void hwtimer_wake_in_time(u64_t time);
void hwtimer_set_silent_ticks(s64_t sys_ticks);