genode/repos/base-nova/src/base/thread/thread_nova.cc
Martin Stein 8f9355b360 thread API & CPU session: accounting of CPU quota
In the init configuration one can configure the donation of CPU time via
'resource' tags that have the attribute 'name' set to "CPU" and the
attribute 'quantum' set to the percentage of CPU quota that init shall
donate. The pattern is the same as when donating RAM quota.

! <start name="test">
!   <resource name="CPU" quantum="75"/>
! </start>

This would cause init to try donating 75% of its CPU quota to the child
"test".  Init and core do not preserve CPU quota for their own
requirements by default as it is done with RAM quota.

The CPU quota that a process owns can be applied through the thread
constructor. The constructor has been enhanced by an argument that
indicates the percentage of the programs CPU quota that shall be granted
to the new thread. So 'Thread(33, "test")' would cause the backing CPU
session to try to grant 33% of the programs CPU quota to the thread
"test". By now, the CPU quota of a thread can't be altered after
construction. Constructing a thread with CPU quota 0 doesn't mean the
thread gets never scheduled but that the thread has no guaranty to receive
CPU time. Such threads have to live with excess CPU time.

Threads that already existed in the official repositories of Genode were
adapted in the way that they receive a quota of 0.

This commit also provides a run test 'cpu_quota' in base-hw (the only
kernel that applies the CPU-quota scheme currently). The test basically
runs three threads with different physical CPU quota. The threads simply
count for 30 seconds each and the test then checks wether the counter
values relate to the CPU-quota distribution.

fix #1275
2014-11-28 12:02:37 +01:00

209 lines
5.2 KiB
C++

/*
* \brief NOVA-specific implementation of the Thread API
* \author Norman Feske
* \author Sebastian Sumpf
* \author Alexander Boettcher
* \date 2010-01-19
*/
/*
* Copyright (C) 2010-2013 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/thread.h>
#include <base/printf.h>
#include <base/sleep.h>
#include <base/env.h>
#include <base/rpc_client.h>
#include <session/session.h>
/* NOVA includes */
#include <nova/syscalls.h>
#include <nova/util.h>
#include <nova_cpu_session/connection.h>
using namespace Genode;
/**
* Entry point entered by new threads
*/
void Thread_base::_thread_start()
{
using namespace Genode;
/* catch any exception at this point and try to print an error message */
try {
Thread_base::myself()->entry();
} catch (...) {
char thread_name[48];
Thread_base::myself()->name(thread_name, sizeof(thread_name));
try {
PERR("Thread '%s' died because of an uncaught exception", thread_name);
} catch (...) {
/* die in a noisy way */
nova_die();
}
throw;
}
Thread_base::myself()->_join_lock.unlock();
/* sleep silently */
Genode::sleep_forever();
}
/*****************
** Thread base **
*****************/
void Thread_base::_init_platform_thread(size_t, Type type)
{
using namespace Nova;
/*
* Allocate capability selectors for the thread's execution context,
* running semaphore and exception handler portals.
*/
_tid.ec_sel = Native_thread::INVALID_INDEX;
/* for main threads the member initialization differs */
if (type == MAIN || type == REINITIALIZED_MAIN) {
_thread_cap = env()->parent()->main_thread_cap();
Genode::Native_capability pager_cap(Nova::PT_SEL_MAIN_PAGER);
_pager_cap = reinterpret_cap_cast<Pager_object>(pager_cap);
_tid.exc_pt_sel = 0;
_tid.ec_sel = Nova::PT_SEL_MAIN_EC;
enum { DONT_MAP_PAGER_CAP = 0 };
request_native_ec_cap(_pager_cap, _tid.ec_sel, DONT_MAP_PAGER_CAP);
return;
}
_tid.exc_pt_sel = cap_map()->insert(NUM_INITIAL_PT_LOG2);
if (_tid.exc_pt_sel == Native_thread::INVALID_INDEX)
throw Cpu_session::Thread_creation_failed();
/* if no cpu session is given, use it from the environment */
if (!_cpu_session)
_cpu_session = env()->cpu_session();
/* create thread at core */
char buf[48];
name(buf, sizeof(buf));
_thread_cap = _cpu_session->create_thread(0, buf);
if (!_thread_cap.valid())
throw Cpu_session::Thread_creation_failed();
/* assign thread to protection domain */
if (env()->pd_session()->bind_thread(_thread_cap))
throw Cpu_session::Thread_creation_failed();
}
void Thread_base::_deinit_platform_thread()
{
using namespace Nova;
if (_tid.ec_sel != Native_thread::INVALID_INDEX) {
revoke(Obj_crd(_tid.ec_sel, 1));
cap_map()->remove(_tid.ec_sel, 1, false);
}
revoke(Obj_crd(_tid.exc_pt_sel, NUM_INITIAL_PT_LOG2));
cap_map()->remove(_tid.exc_pt_sel, NUM_INITIAL_PT_LOG2, false);
/* revoke utcb */
Rights rwx(true, true, true);
addr_t utcb = reinterpret_cast<addr_t>(&_context->utcb);
revoke(Mem_crd(utcb >> 12, 0, rwx));
/* de-announce thread */
if (_thread_cap.valid())
_cpu_session->kill_thread(_thread_cap);
if (_pager_cap.valid())
env()->rm_session()->remove_client(_pager_cap);
}
void Thread_base::start()
{
if (_tid.ec_sel < Native_thread::INVALID_INDEX - 1)
throw Cpu_session::Thread_creation_failed();
/*
* Default: create global thread - ec.sel == INVALID_INDEX
* create local thread - ec.sel == INVALID_INDEX - 1
*/
bool global = _tid.ec_sel == Native_thread::INVALID_INDEX;
using namespace Genode;
/* create new pager object and assign it to the new thread */
_pager_cap = env()->rm_session()->add_client(_thread_cap);
if (!_pager_cap.valid())
throw Cpu_session::Thread_creation_failed();
if (_cpu_session->set_pager(_thread_cap, _pager_cap))
throw Cpu_session::Thread_creation_failed();
/* create EC at core */
Thread_state state;
state.sel_exc_base = _tid.exc_pt_sel;
state.is_vcpu = _tid.is_vcpu;
/* local thread have no start instruction pointer - set via portal entry */
addr_t thread_ip = global ? reinterpret_cast<addr_t>(_thread_start) : 0;
try { _cpu_session->state(_thread_cap, state); }
catch (...) { throw Cpu_session::Thread_creation_failed(); }
if (_cpu_session->start(_thread_cap, thread_ip, _context->stack_top()))
throw Cpu_session::Thread_creation_failed();
/* request native EC thread cap */
_tid.ec_sel = cap_map()->insert(1);
if (_tid.ec_sel == Native_thread::INVALID_INDEX)
throw Cpu_session::Thread_creation_failed();
request_native_ec_cap(_pager_cap, _tid.ec_sel);
using namespace Nova;
/* request exception portals for normal threads */
if (!_tid.is_vcpu) {
request_event_portal(_pager_cap, _tid.exc_pt_sel, 0, NUM_INITIAL_PT_LOG2);
/* default: we don't accept any mappings or translations */
Utcb * utcb_obj = reinterpret_cast<Utcb *>(utcb());
utcb_obj->crd_rcv = Obj_crd();
utcb_obj->crd_xlt = Obj_crd();
}
if (global)
/* request creation of SC to let thread run*/
_cpu_session->resume(_thread_cap);
}
void Thread_base::cancel_blocking()
{
using namespace Nova;
if (sm_ctrl(_tid.exc_pt_sel + SM_SEL_EC, SEMAPHORE_UP))
nova_die();
}