genode/repos/base/src/test/timer/main.cc

221 lines
5.6 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Test for timer service
* \author Norman Feske
* \author Martin Stein
2011-12-22 16:19:25 +01:00
* \date 2009-06-22
*/
/*
* Copyright (C) 2009-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
/* Genode includes */
#include <base/component.h>
#include <base/heap.h>
2011-12-22 16:19:25 +01:00
#include <timer_session/connection.h>
#include <base/registry.h>
using namespace Genode;
2011-12-22 16:19:25 +01:00
struct Lazy_test
2011-12-22 16:19:25 +01:00
{
struct Faster_timer_too_slow : Exception { };
Env &env;
Signal_transmitter done;
Timer::Connection slow_timer { env };
Signal_handler<Lazy_test> slow_handler { env.ep(), *this,
&Lazy_test::handle_slow_timer };
Timer::Connection fast_timer { env };
Signal_handler<Lazy_test> fast_handler { env.ep(), *this,
&Lazy_test::handle_fast_timer };
Timer::Connection faster_timer { env };
Signal_handler<Lazy_test> faster_handler { env.ep(), *this,
&Lazy_test::handle_faster_timer };
enum { RUN_TIME_US = 2 * 1000 * 1000, TIMEOUT_US = 50*1000, FACTOR = 2 };
unsigned fast = 0;
unsigned faster = 0;
void handle_slow_timer()
{
log("timeout fired - ", fast, "/", faster, "/",
RUN_TIME_US / TIMEOUT_US * FACTOR);
if (fast)
throw Faster_timer_too_slow();
done.submit();
}
2011-12-22 16:19:25 +01:00
void handle_fast_timer() {
fast ++;
if (faster <= fast)
throw Faster_timer_too_slow();
}
void handle_faster_timer() { set_fast_timers(); }
2011-12-22 16:19:25 +01:00
void set_fast_timers()
{
fast_timer.trigger_once(TIMEOUT_US);
faster_timer.trigger_once(TIMEOUT_US/FACTOR);
faster ++;
}
2011-12-22 16:19:25 +01:00
Lazy_test(Env &env, Signal_context_capability done) : env(env), done(done)
{
slow_timer.sigh(slow_handler);
fast_timer.sigh(fast_handler);
faster_timer.sigh(faster_handler);
2011-12-22 16:19:25 +01:00
log("register two-seconds timeout...");
slow_timer.trigger_once(RUN_TIME_US);
set_fast_timers();
}
};
struct Stress_test
{
enum { DURATION_SEC = 10 };
enum { MAX_SLV_PERIOD_US = 33000 };
struct Starvation : Exception { };
struct Violation_of_timer_rate_limit : Exception { };
struct Slave
{
enum { DURATION_US = DURATION_SEC * 1000 * 1000 };
enum { MIN_TIMER_PERIOD_US = 1000 };
enum { MAX_CNT_BASE = DURATION_US / MIN_TIMER_PERIOD_US };
enum { MAX_CNT_TOLERANCE = MAX_CNT_BASE / 9 };
enum { MAX_CNT = MAX_CNT_BASE + MAX_CNT_TOLERANCE };
enum { MIN_CNT = DURATION_US / MAX_SLV_PERIOD_US / 2 };
Signal_handler<Slave> timer_handler;
Timer::Connection timer;
uint64_t us;
unsigned count { 0 };
2011-12-22 16:19:25 +01:00
Slave(Env &env, uint64_t us)
: timer_handler(env.ep(), *this, &Slave::handle_timer),
timer(env), us(us) { timer.sigh(timer_handler); }
2011-12-22 16:19:25 +01:00
virtual ~Slave() { }
void handle_timer()
2011-12-22 16:19:25 +01:00
{
count++;
timer.trigger_once(us);
2011-12-22 16:19:25 +01:00
}
void dump(unsigned &starvation, unsigned &rate_violation)
{
log("timer (period ", us, " us) triggered ", count,
" times (min ", (unsigned)MIN_CNT,
" max ", (unsigned)MAX_CNT, ") -> slept ",
((uint64_t)us * count) / 1000, " ms");
/* detect starvation of timeouts */
if (count < MIN_CNT) {
error("triggered less than ", (unsigned)MIN_CNT,
" times");
starvation ++;
}
/* detect violation of timer rate limitation */
if (count > MAX_CNT) {
error("triggered more than ", (unsigned)MAX_CNT,
" times");
rate_violation ++;
}
}
2011-12-22 16:19:25 +01:00
void start() { timer.trigger_once(us); }
void stop() { timer.sigh(Signal_context_capability()); }
};
2011-12-22 16:19:25 +01:00
Env &env;
Signal_transmitter done;
Heap heap { &env.ram(), &env.rm() };
Timer::Connection timer { env };
unsigned count { 0 };
Signal_handler<Stress_test> handler { env.ep(), *this, &Stress_test::handle };
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Registry<Registered<Slave> > slaves { };
2011-12-22 16:19:25 +01:00
void handle()
{
if (count < DURATION_SEC) {
count++;
log("wait ", count, "/", (uint64_t)DURATION_SEC);
timer.trigger_once((uint64_t)1000 * 1000);
} else {
unsigned starvation = 0;
unsigned rate_violation = 0;
slaves.for_each([&] (Slave &timer) { timer.stop(); });
slaves.for_each([&] (Slave &timer) {
timer.dump(starvation, rate_violation);
});
if (starvation)
throw Starvation();
if (rate_violation)
throw Violation_of_timer_rate_limit();
done.submit();
}
}
Stress_test(Env &env, Signal_context_capability done) : env(env), done(done)
{
timer.sigh(handler);
for (uint64_t us_1 = 1; us_1 < MAX_SLV_PERIOD_US; us_1 *= 2) {
new (heap) Registered<Slave>(slaves, env, us_1 - us_1 / 3);
new (heap) Registered<Slave>(slaves, env, us_1);
}
slaves.for_each([&] (Slave &slv) { slv.start(); });
timer.trigger_once((uint64_t)1000 * 1000);
}
2011-12-22 16:19:25 +01:00
~Stress_test() {
slaves.for_each([&] (Registered<Slave> &slv) { destroy(heap, &slv); }); }
};
2011-12-22 16:19:25 +01:00
struct Main
2011-12-22 16:19:25 +01:00
{
Env &env;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Constructible<Lazy_test> test_1 { };
Signal_handler<Main> test_1_done { env.ep(), *this, &Main::handle_test_1_done };
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Constructible<Stress_test> test_2 { };
Signal_handler<Main> test_2_done { env.ep(), *this, &Main::handle_test_2_done };
2011-12-22 16:19:25 +01:00
void handle_test_1_done()
{
test_1.destruct();
test_2.construct(env, test_2_done);
}
void handle_test_2_done()
{
log("--- timer test finished ---");
env.parent().exit(0);
2011-12-22 16:19:25 +01:00
}
Main(Env &env) : env(env)
{
log("--- timer test ---");
test_1.construct(env, test_1_done);
2011-12-22 16:19:25 +01:00
}
};
2011-12-22 16:19:25 +01:00
void Component::construct(Env &env) { static Main main(env); }