genode/repos/base/include/cpu_session/cpu_session.h

261 lines
8.0 KiB
C
Raw Normal View History

2011-12-22 15:19:25 +00:00
/*
* \brief CPU (processing time) manager session interface
* \author Christian Helmuth
* \date 2006-06-27
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
2011-12-22 15:19:25 +00: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 15:19:25 +00:00
*/
#ifndef _INCLUDE__CPU_SESSION__CPU_SESSION_H_
#define _INCLUDE__CPU_SESSION__CPU_SESSION_H_
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-10-16 09:15:46 +00:00
#include <cpu_session/capability.h>
#include <cpu_thread/cpu_thread.h>
2011-12-22 15:19:25 +00:00
#include <base/stdint.h>
#include <base/rpc_args.h>
#include <session/session.h>
#include <dataspace/capability.h>
#include <pd_session/pd_session.h>
2011-12-22 15:19:25 +00:00
namespace Genode {
struct Cpu_session;
struct Cpu_session_client;
base/os: remove deprecated APIs This commit removes APIs that were previously marked as deprecated. This change has the following implications: - The use of the global 'env()' accessor is not possible anymore. - Boolean accessor methods are no longer prefixed with 'is_'. E.g., instead of 'is_valid()', use 'valid()'. - The last traces of 'Ram_session' are gone now. The 'Env::ram()' accessor returns the 'Ram_allocator' interface, which is a subset of the 'Pd_session' interface. - All connection constructors need the 'Env' as argument. - The 'Reporter' constructor needs an 'Env' argument now because the reporter creates a report connection. - The old overload 'Child_policy::resolve_session_request' that returned a 'Service' does not exist anymore. - The base/printf.h header has been removed, use base/log.h instead. - The old notion of 'Signal_dispatcher' is gone. Use 'Signal_handler'. - Transitional headers like os/server.h, cap_session/, volatile_object.h, os/attached*_dataspace.h, signal_rpc_dispatcher.h have been removed. - The distinction between 'Thread_state' and 'Thread_state_base' does not exist anymore. - The header cpu_thread/capability.h along with the type definition of 'Cpu_thread_capability' has been removed. Use the type 'Thread_capability' define in cpu_session/cpu_session.h instead. - Several XML utilities (i.e., at os/include/decorator) could be removed because their functionality is nowadays covered by util/xml_node.h. - The 'os/ram_session_guard.h' has been removed. Use 'Constrained_ram_allocator' provided by base/ram_allocator.h instead. Issue #1987
2019-01-30 16:27:46 +00:00
typedef Capability<Cpu_thread> Thread_capability;
}
struct Genode::Cpu_session : Session
{
2017-05-24 12:41:19 +00:00
/**
* \noapi
*/
static const char *service_name() { return "CPU"; }
/*
* A CPU session consumes a dataspace capability for the session-object
* allocation, its session capability, the capability of the 'Native_cpu'
* RPC interface, and a capability for the trace-control dataspace.
*/
enum { CAP_QUOTA = 6 };
typedef Cpu_session_client Client;
/*********************
** Exception types **
*********************/
class Thread_creation_failed : public Exception { };
class Quota_exceeded : public Thread_creation_failed { };
enum { THREAD_NAME_LEN = 32 };
enum { PRIORITY_LIMIT = 1 << 16 };
enum { QUOTA_LIMIT_LOG2 = 15 };
enum { QUOTA_LIMIT = 1 << QUOTA_LIMIT_LOG2 };
enum { DEFAULT_PRIORITY = 0 };
/**
* Thread weight argument type for 'create_thread'
*/
struct Weight
{
enum { DEFAULT_WEIGHT = 10 };
size_t value = DEFAULT_WEIGHT;
Weight() { }
explicit Weight(size_t value) : value(value) { }
};
typedef String<THREAD_NAME_LEN> Name;
/**
* Physical quota configuration
*/
struct Quota;
virtual ~Cpu_session() { }
/**
* Create a new thread
*
* \param pd protection domain where the thread will be executed
* \param name name for the thread
* \param affinity CPU affinity, referring to the session-local
* affinity space
* \param weight CPU quota that shall be granted to the thread
* \param utcb base of the UTCB that will be used by the thread
* \return capability representing the new thread
* \throw Thread_creation_failed
* \throw Out_of_ram
* \throw Out_of_caps
*/
virtual Thread_capability create_thread(Capability<Pd_session> pd,
Name const &name,
Affinity::Location affinity,
Weight weight,
addr_t utcb = 0) = 0;
/**
* Kill an existing thread
*
* \param thread capability of the thread to kill
*/
virtual void kill_thread(Thread_capability thread) = 0;
/**
* Register default signal handler for exceptions
*
* This handler is used for all threads that have no explicitly installed
* exception handler.
*
* On Linux, this exception is delivered when the process triggers
* a SIGCHLD. On other platforms, this exception is delivered on
* the occurrence of CPU exceptions such as division by zero.
*/
virtual void exception_sigh(Signal_context_capability) = 0;
/**
* Return affinity space of CPU nodes available to the CPU session
*
* The dimension of the affinity space as returned by this method
* represent the physical CPUs that are available.
*/
virtual Affinity::Space affinity_space() const = 0;
/**
* Translate generic priority value to kernel-specific priority levels
*
* \param pf_prio_limit maximum priority used for the kernel, must
* be power of 2
* \param prio generic priority value as used by the CPU
* session interface
* \param inverse order of platform priorities, if true
* 'pf_prio_limit' corresponds to the highest
* priority, otherwise it refers to the
* lowest priority.
* \return platform-specific priority value
*/
static unsigned scale_priority(unsigned pf_prio_limit, unsigned prio,
bool inverse = true)
{
/*
* Generic priority values are (0 is highest, 'PRIORITY_LIMIT'
* is lowest. On platforms where priority levels are defined
* the other way round, we have to invert the priority value.
*/
prio = inverse ? Cpu_session::PRIORITY_LIMIT - prio : prio;
/* scale value to platform priority range 0..pf_prio_limit */
return (prio*pf_prio_limit)/Cpu_session::PRIORITY_LIMIT;
}
/**
* Request trace control dataspace
*
* The trace-control dataspace is used to propagate tracing
* control information from core to the threads of a CPU session.
*
* The trace-control dataspace is accounted to the CPU session.
*/
virtual Dataspace_capability trace_control() = 0;
/**
* Define reference account for the CPU session
*
* \param cpu_session reference account
*
* \return 0 on success
*
* Each CPU session requires another CPU session as reference
* account to transfer quota to and from. The reference account can
* be defined only once.
*/
virtual int ref_account(Cpu_session_capability cpu_session) = 0;
/**
* Transfer quota to another CPU session
*
* \param cpu_session receiver of quota donation
* \param amount percentage of the session quota scaled up to
* the 'QUOTA_LIMIT' space
* \return 0 on success
*
* Quota can only be transfered if the specified CPU session is
* either the reference account for this session or vice versa.
*/
virtual int transfer_quota(Cpu_session_capability cpu_session,
size_t amount) = 0;
/**
* Return quota configuration of the session
*/
virtual Quota quota() = 0;
/**
* Scale up 'value' from its space with 'limit' to the 'QUOTA_LIMIT' space
*/
template<typename T = size_t>
static size_t quota_lim_upscale(size_t const value, size_t const limit) {
return ((T)value << Cpu_session::QUOTA_LIMIT_LOG2) / limit; }
/**
* Scale down 'value' from the 'QUOTA_LIMIT' space to a space with 'limit'
*/
template<typename T = size_t>
static size_t quota_lim_downscale(size_t const value, size_t const limit) {
return ((T)value * limit) >> Cpu_session::QUOTA_LIMIT_LOG2; }
/*****************************************
** Access to kernel-specific interface **
*****************************************/
/**
* Common base class of kernel-specific CPU interfaces
*/
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 14:42:15 +00:00
struct Native_cpu : Interface { };
/**
* Return capability to kernel-specific CPU operations
*/
virtual Capability<Native_cpu> native_cpu() = 0;
/*********************
** RPC declaration **
*********************/
GENODE_RPC_THROW(Rpc_create_thread, Thread_capability, create_thread,
GENODE_TYPE_LIST(Thread_creation_failed, Out_of_ram, Out_of_caps),
Capability<Pd_session>, Name const &, Affinity::Location,
Weight, addr_t);
GENODE_RPC(Rpc_kill_thread, void, kill_thread, Thread_capability);
GENODE_RPC(Rpc_exception_sigh, void, exception_sigh, Signal_context_capability);
GENODE_RPC(Rpc_affinity_space, Affinity::Space, affinity_space);
GENODE_RPC(Rpc_trace_control, Dataspace_capability, trace_control);
GENODE_RPC(Rpc_ref_account, int, ref_account, Cpu_session_capability);
GENODE_RPC(Rpc_transfer_quota, int, transfer_quota, Cpu_session_capability, size_t);
GENODE_RPC(Rpc_quota, Quota, quota);
GENODE_RPC(Rpc_native_cpu, Capability<Native_cpu>, native_cpu);
GENODE_RPC_INTERFACE(Rpc_create_thread, Rpc_kill_thread, Rpc_exception_sigh,
Rpc_affinity_space, Rpc_trace_control, Rpc_ref_account,
Rpc_transfer_quota, Rpc_quota, Rpc_native_cpu);
};
struct Genode::Cpu_session::Quota
{
size_t super_period_us;
size_t us;
};
2011-12-22 15:19:25 +00:00
#endif /* _INCLUDE__CPU_SESSION__CPU_SESSION_H_ */