798 lines
23 KiB
C
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Representation used for children of the init process
* \author Norman Feske
* \date 2010-05-04
*/
/*
* Copyright (C) 2010-2016 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 General Public License version 2.
*/
#ifndef _INCLUDE__INIT__CHILD_H_
#define _INCLUDE__INIT__CHILD_H_
/* Genode includes */
#include <base/log.h>
2011-12-22 16:19:25 +01:00
#include <base/child.h>
#include <os/session_requester.h>
#include <os/session_policy.h>
2011-12-22 16:19:25 +01:00
/* init includes */
2017-01-03 18:12:53 +01:00
#include <init/verbose.h>
2011-12-22 16:19:25 +01:00
#include <init/child_config.h>
#include <init/child_policy.h>
namespace Init {
class Routed_service;
class Name_registry;
class Child_registry;
class Child;
2017-01-03 18:12:53 +01:00
using namespace Genode;
using Genode::size_t;
using Genode::strlen;
using Genode::strncpy;
typedef Genode::Registered<Genode::Parent_service> Parent_service;
}
2011-12-22 16:19:25 +01:00
/***************
** Utilities **
***************/
namespace Init {
2017-01-03 18:12:53 +01:00
static void warn_insuff_quota(size_t const avail)
{
2017-01-03 18:12:53 +01:00
warning("specified quota exceeds available quota, "
"proceeding with a quota of ", avail);
}
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
inline long read_priority(Xml_node start_node, long prio_levels)
2011-12-22 16:19:25 +01:00
{
2017-01-03 18:12:53 +01:00
long priority = Cpu_session::DEFAULT_PRIORITY;
2011-12-22 16:19:25 +01:00
try { start_node.attribute("priority").value(&priority); }
catch (...) { }
/*
* All priority declarations in the config file are
* negative because child priorities can never be higher
* than parent priorities. To simplify priority
* calculations, we use inverted values. Lower values
* correspond to higher priorities.
*/
priority = -priority;
if (priority && (priority >= prio_levels)) {
long new_prio = prio_levels ? prio_levels-1 : 0;
2017-01-03 18:12:53 +01:00
char name[Service::Name::capacity()];
start_node.attribute("name").value(name, sizeof(name));
2017-01-03 18:12:53 +01:00
warning(Cstring(name), ": invalid priority, upgrading "
"from ", -priority, " to ", -new_prio);
return new_prio;
}
return priority;
2011-12-22 16:19:25 +01:00
}
2017-01-03 18:12:53 +01:00
inline Affinity::Location
read_affinity_location(Affinity::Space const &space,
Xml_node start_node)
{
2017-01-03 18:12:53 +01:00
typedef Affinity::Location Location;
try {
2017-01-03 18:12:53 +01:00
Xml_node node = start_node.sub_node("affinity");
/* if no position value is specified, select the whole row/column */
unsigned long const
default_width = node.has_attribute("xpos") ? 1 : space.width(),
default_height = node.has_attribute("ypos") ? 1 : space.height();
unsigned long const
width = node.attribute_value<unsigned long>("width", default_width),
height = node.attribute_value<unsigned long>("height", default_height);
long const x1 = node.attribute_value<long>("xpos", 0),
y1 = node.attribute_value<long>("ypos", 0),
x2 = x1 + width - 1,
y2 = y1 + height - 1;
/* clip location to space boundary */
2017-01-03 18:12:53 +01:00
return Location(max(x1, 0L), max(y1, 0L),
min((unsigned)(x2 - x1 + 1), space.width()),
min((unsigned)(y2 - y1 + 1), space.height()));
}
catch (...) { return Location(0, 0, space.width(), space.height()); }
}
/**
* Return amount of RAM that is currently unused
*/
2017-01-03 18:12:53 +01:00
static inline size_t avail_slack_ram_quota(size_t ram_avail)
{
2017-01-03 18:12:53 +01:00
size_t const preserve = 148*1024;
2017-01-03 18:12:53 +01:00
return ram_avail > preserve ? ram_avail - preserve : 0;
}
/**
* Return sub string of label with the leading child name stripped out
*
* \return character pointer to the scoped part of the label,
* or nullptr if the label is not correctly prefixed with the child's
* name
*/
inline char const *skip_label_prefix(char const *child_name, char const *label)
{
2017-01-03 18:12:53 +01:00
size_t const child_name_len = strlen(child_name);
if (strcmp(child_name, label, child_name_len) != 0)
return nullptr;
label += child_name_len;
/*
* Skip label separator. This condition should be always satisfied.
*/
if (strcmp(" -> ", label, 4) != 0)
return nullptr;
return label + 4;
}
/**
* Return true if service XML node matches service request
*
* \param args session arguments, inspected for the session label
* \param child_name name of the originator of the session request
* \param service_name name of the requested service
*/
inline bool service_node_matches(Xml_node const service_node,
Session_label const &label,
2017-01-03 18:12:53 +01:00
Child_policy::Name const &child_name,
Service::Name const &service_name)
{
bool const service_matches =
service_node.has_type("any-service") ||
(service_node.has_type("service") &&
service_node.attribute("name").has_value(service_name.string()));
if (!service_matches)
return false;
bool const route_depends_on_child_provided_label =
service_node.has_attribute("label") ||
service_node.has_attribute("label_prefix") ||
service_node.has_attribute("label_suffix");
char const *unscoped_attr = "unscoped_label";
if (service_node.has_attribute(unscoped_attr)) {
/*
* If an 'unscoped_label' attribute is provided, don't consider any
* scoped label attribute.
*/
if (route_depends_on_child_provided_label)
warning("service node contains both scoped and unscoped label attributes");
typedef String<Session_label::capacity()> Label;
return label == service_node.attribute_value(unscoped_attr, Label());
}
if (!route_depends_on_child_provided_label)
return true;
2011-12-22 16:19:25 +01:00
char const * const scoped_label = skip_label_prefix(
child_name.string(), label.string());
if (!scoped_label)
return false;
Session_label const session_label(scoped_label);
2011-12-22 16:19:25 +01:00
return !Xml_node_label_score(service_node, session_label).conflict();
2011-12-22 16:19:25 +01:00
}
/**
* Check if service name is ambiguous
*
* \return true if the same service is provided multiple
* times
*
* \deprecated
*/
template <typename T>
2017-01-03 18:12:53 +01:00
inline bool is_ambiguous(Registry<T> const &services,
Service::Name const &name)
{
/* count number of services with the specified name */
unsigned cnt = 0;
services.for_each([&] (T const &service) {
cnt += (service.name() == name); });
return cnt > 1;
}
template <typename T>
2017-01-03 18:12:53 +01:00
inline Service *find_service(Registry<T> &services,
Service::Name const &name)
{
T *service = nullptr;
services.for_each([&] (T &s) {
if (!service && (s.name() == name))
service = &s; });
return service;
}
}
2011-12-22 16:19:25 +01:00
/**
* Init-specific representation of a child service
*/
2017-01-03 18:12:53 +01:00
class Init::Routed_service : public Child_service
{
public:
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
typedef Child_policy::Name Child_name;
2011-12-22 16:19:25 +01:00
private:
2011-12-22 16:19:25 +01:00
Child_name _child_name;
2017-01-03 18:12:53 +01:00
Registry<Routed_service>::Element _registry_element;
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
/**
* Constructor
*
* \param services registry of all services provides by children
* \param child_name child name of server, used for session routing
*
* The other arguments correspond to the arguments of 'Child_service'.
*/
2017-01-03 18:12:53 +01:00
Routed_service(Registry<Routed_service> &services,
Child_name const &child_name,
Id_space<Parent::Server> &server_id_space,
Session_state::Factory &factory,
Service::Name const &name,
2017-01-03 18:12:53 +01:00
Ram_session_capability ram,
Child_service::Wakeup &wakeup)
:
Child_service(server_id_space, factory, name, ram, wakeup),
_child_name(child_name), _registry_element(services, *this)
{ }
Child_name const &child_name() const { return _child_name; }
};
2011-12-22 16:19:25 +01:00
/**
* Interface for name database
*/
struct Init::Name_registry
{
virtual ~Name_registry() { }
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
typedef Child_policy::Name Name;
2011-12-22 16:19:25 +01:00
/**
* Check if specified name is unique
*
* \return false if name already exists
2011-12-22 16:19:25 +01:00
*/
virtual bool unique(const char *name) const = 0;
2011-12-22 16:19:25 +01:00
/**
* Return child name for a given alias name
*
* If there is no alias, the function returns the original name.
*/
virtual Name deref_alias(Name const &) = 0;
};
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
class Init::Child : Child_policy, Child_service::Wakeup
{
public:
2011-12-22 16:19:25 +01:00
/**
* Exception type
*/
class Child_name_is_not_unique { };
2011-12-22 16:19:25 +01:00
private:
friend class Child_registry;
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Env &_env;
Verbose const &_verbose;
2017-01-03 18:12:53 +01:00
List_element<Child> _list_element;
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Xml_node _start_node;
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Xml_node _default_route_node;
2011-12-22 16:19:25 +01:00
Name_registry &_name_registry;
2011-12-22 16:19:25 +01:00
/**
* Unique child name and file name of ELF binary
*/
struct Name
{
enum { MAX_NAME_LEN = 64 };
char file[MAX_NAME_LEN];
char unique[MAX_NAME_LEN];
2011-12-22 16:19:25 +01:00
/**
* Constructor
*
* Obtains file name and unique process name from XML node
*
* \param start_node XML start node
* \param registry registry tracking unique names
2011-12-22 16:19:25 +01:00
*/
2017-01-03 18:12:53 +01:00
Name(Xml_node start_node, Name_registry const &registry) {
try {
start_node.attribute("name").value(unique, sizeof(unique)); }
2017-01-03 18:12:53 +01:00
catch (Xml_node::Nonexistent_attribute) {
warning("missing 'name' attribute in '<start>' entry");
throw; }
/* check for a name confict with the other children */
if (!registry.unique(unique)) {
2017-01-03 18:12:53 +01:00
error("child name \"", Cstring(unique), "\" is not unique");
throw Child_name_is_not_unique();
}
2011-12-22 16:19:25 +01:00
/* use name as default file name if not declared otherwise */
2017-01-03 18:12:53 +01:00
strncpy(file, unique, sizeof(file));
2011-12-22 16:19:25 +01:00
/* check for a binary declaration */
try {
2017-01-03 18:12:53 +01:00
Xml_node binary = start_node.sub_node("binary");
binary.attribute("name").value(file, sizeof(file));
} catch (...) { }
}
} _name;
2011-12-22 16:19:25 +01:00
struct Read_quota
{
2017-01-03 18:12:53 +01:00
Read_quota(Xml_node start_node,
size_t &ram_quota,
size_t &cpu_quota_pc,
bool &constrain_phys,
size_t const ram_avail,
Verbose const &verbose)
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 11:15:46 +02:00
{
cpu_quota_pc = 0;
constrain_phys = false;
2017-01-03 18:12:53 +01:00
Number_of_bytes ram_bytes = 0;
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 11:15:46 +02:00
try {
2017-01-03 18:12:53 +01:00
Xml_node rsc = start_node.sub_node("resource");
for (;; rsc = rsc.next("resource")) {
try {
if (rsc.attribute("name").has_value("RAM")) {
rsc.attribute("quantum").value(&ram_bytes);
constrain_phys = rsc.attribute_value("constrain_phys", false);
} else if (rsc.attribute("name").has_value("CPU")) {
rsc.attribute("quantum").value(&cpu_quota_pc); }
} catch (...) { }
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 11:15:46 +02:00
}
} catch (...) { }
ram_quota = ram_bytes;
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 11:15:46 +02:00
/*
* If the configured RAM quota exceeds our own quota, we donate
* all remaining quota to the child but we need to count in
* our allocation of the child meta data from the heap.
* Hence, we preserve some of our own quota.
*/
if (ram_quota > ram_avail) {
ram_quota = ram_avail;
2017-01-03 18:12:53 +01:00
if (verbose.enabled())
warn_insuff_quota(ram_avail);
2011-12-22 16:19:25 +01:00
}
}
};
2011-12-22 16:19:25 +01:00
/**
* Resources assigned to the child
*/
struct Resources : Read_quota
{
2017-01-03 18:12:53 +01:00
long prio_levels_log2;
long priority;
Affinity affinity;
size_t ram_quota;
size_t cpu_quota_pc;
bool constrain_phys;
Resources(Xml_node start_node, const char *label,
long prio_levels,
2017-01-03 18:12:53 +01:00
Affinity::Space const &affinity_space,
size_t ram_avail,
Verbose const &verbose)
:
2017-01-03 18:12:53 +01:00
Read_quota(start_node, ram_quota, cpu_quota_pc,
constrain_phys, ram_avail, verbose),
prio_levels_log2(log2(prio_levels)),
priority(read_priority(start_node, prio_levels)),
affinity(affinity_space,
read_affinity_location(affinity_space, start_node))
{
/* deduce session costs from usable ram quota */
ram_quota = Genode::Child::effective_ram_quota(ram_quota);
}
} _resources;
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Genode::Parent_service _env_ram_service { _env, Ram_session::service_name() };
Genode::Parent_service _env_cpu_service { _env, Cpu_session::service_name() };
Genode::Parent_service _env_pd_service { _env, Pd_session::service_name() };
Genode::Parent_service _env_log_service { _env, Log_session::service_name() };
Genode::Parent_service _env_rom_service { _env, Rom_session::service_name() };
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Registry<Parent_service> &_parent_services;
Registry<Routed_service> &_child_services;
/**
* Private child configuration
*/
Init::Child_config _config;
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Session_requester _session_requester;
2011-12-22 16:19:25 +01:00
/**
* Policy helpers
*/
Init::Child_policy_handle_cpu_priorities _priority_policy;
Init::Child_policy_provide_rom_file _config_policy;
Init::Child_policy_redirect_rom_file _configfile_policy;
Init::Child_policy_ram_phys _ram_session_policy;
2017-01-03 18:12:53 +01:00
Genode::Child _child { _env.rm(), _env.ep().rpc_ep(), *this };
/**
* Child_service::Wakeup callback
*/
void wakeup_child_service() override
{
_session_requester.trigger_update();
}
public:
/**
* Constructor
*
* \throw Ram_session::Alloc_failed allocation of config buffer failed
* \throw Region_map::Attach_failed failed to temporarily attach
* config dataspace to local address
* space
*/
2017-01-03 18:12:53 +01:00
Child(Env &env,
Verbose const &verbose,
Xml_node start_node,
Xml_node default_route_node,
Name_registry &name_registry,
long prio_levels,
Affinity::Space const &affinity_space,
Registry<Parent_service> &parent_services,
Registry<Routed_service> &child_services)
:
2017-01-03 18:12:53 +01:00
_env(env), _verbose(verbose),
_list_element(this),
_start_node(start_node),
_default_route_node(default_route_node),
_name_registry(name_registry),
_name(start_node, name_registry),
_resources(start_node, _name.unique, prio_levels,
2017-01-03 18:12:53 +01:00
affinity_space, avail_slack_ram_quota(_env.ram().avail()),
_verbose),
_parent_services(parent_services),
_child_services(child_services),
_config(_env.ram(), _env.rm(), start_node),
2017-01-03 18:12:53 +01:00
_session_requester(_env.ep().rpc_ep(), _env.ram(), _env.rm()),
_priority_policy(_resources.prio_levels_log2, _resources.priority),
2017-01-03 18:12:53 +01:00
_config_policy("config", _config.dataspace(), &_env.ep().rpc_ep()),
_configfile_policy("config", _config.filename()),
_ram_session_policy(_resources.constrain_phys)
{
if (_resources.ram_quota == 0)
2017-01-03 18:12:53 +01:00
warning("no valid RAM resource for child "
"\"", Cstring(_name.unique), "\"");
2017-01-03 18:12:53 +01:00
if (_verbose.enabled()) {
log("child \"", Cstring(_name.unique), "\"");
log(" RAM quota: ", _resources.ram_quota);
log(" ELF binary: ", Cstring(_name.file));
log(" priority: ", _resources.priority);
}
/*
* Determine services provided by the child
*/
try {
Xml_node service_node = start_node.sub_node("provides").sub_node("service");
2011-12-22 16:19:25 +01:00
for (; ; service_node = service_node.next("service")) {
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
char name[Service::Name::capacity()];
service_node.attribute("name").value(name, sizeof(name));
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
if (_verbose.enabled())
log(" provides service ", Cstring(name));
2011-12-22 16:19:25 +01:00
new (_child.heap())
Routed_service(child_services, this->name(),
_session_requester.id_space(),
_child.session_factory(),
name, _child.ram_session_cap(), *this);
2011-12-22 16:19:25 +01:00
}
}
catch (Xml_node::Nonexistent_sub_node) { }
catch (Genode::Child::Inactive) {
error(this->name(), ": incomplete environment at construction time"); }
}
virtual ~Child()
{
_child_services.for_each([&] (Routed_service &service) {
if (service.has_id_space(_session_requester.id_space()))
destroy(_child.heap(), &service); });
}
2011-12-22 16:19:25 +01:00
/**
* Return true if the child has the specified name
*/
bool has_name(Child_policy::Name const &str) const { return str == name(); }
2011-12-22 16:19:25 +01:00
/****************************
** Child-policy interface **
****************************/
2011-12-22 16:19:25 +01:00
Child_policy::Name name() const override { return _name.unique; }
Binary_name binary_name() const override { return _name.file; }
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Ram_session &ref_ram() override { return _env.ram(); }
Ram_session_capability ref_ram_cap() const override { return _env.ram_session_cap(); }
2017-01-03 18:12:53 +01:00
void init(Ram_session &session, Ram_session_capability cap) override
{
2017-01-03 18:12:53 +01:00
session.ref_account(_env.ram_session_cap());
_env.ram().transfer_quota(cap, _resources.ram_quota);
}
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
void init(Cpu_session &session, Cpu_session_capability cap) override
{
static size_t avail = Cpu_session::quota_lim_upscale( 100, 100);
size_t const need = Cpu_session::quota_lim_upscale(_resources.cpu_quota_pc, 100);
size_t need_adj = 0;
if (need > avail || avail == 0) {
warn_insuff_quota(Cpu_session::quota_lim_downscale(avail, 100));
need_adj = Cpu_session::quota_lim_upscale(100, 100);
avail = 0;
} else {
need_adj = Cpu_session::quota_lim_upscale(need, avail);
avail -= need;
}
session.ref_account(_env.cpu_session_cap());
_env.cpu().transfer_quota(cap, need_adj);
}
2017-01-03 18:12:53 +01:00
Id_space<Parent::Server> &server_id_space() override {
return _session_requester.id_space(); }
Route resolve_session_request(Service::Name const &service_name,
Session_label const &label) override
{
2017-01-03 18:12:53 +01:00
Service *service = nullptr;
/* check for "config" ROM request */
if ((service = _config_policy.resolve_session_request_with_label(service_name, label)))
return Route { *service, label };
/* check for "session_requests" ROM request */
2017-01-03 18:12:53 +01:00
if (service_name == Rom_session::service_name()
&& label.last_element() == Session_requester::rom_name())
return Route { _session_requester.service() };
2011-12-22 16:19:25 +01:00
try {
2017-01-03 18:12:53 +01:00
Xml_node route_node = _default_route_node;
2011-12-22 16:19:25 +01:00
try {
route_node = _start_node.sub_node("route"); }
catch (...) { }
2017-01-03 18:12:53 +01:00
Xml_node service_node = route_node.sub_node();
2011-12-22 16:19:25 +01:00
for (; ; service_node = service_node.next()) {
2011-12-22 16:19:25 +01:00
bool service_wildcard = service_node.has_type("any-service");
2011-12-22 16:19:25 +01:00
if (!service_node_matches(service_node, label, name(), service_name))
continue;
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
Xml_node target = service_node.sub_node();
for (; ; target = target.next()) {
2011-12-22 16:19:25 +01:00
/*
* Determine session label to be provided to the server.
*
* By default, the client's identity (accompanied with
* the a client-provided label) is presented as session
* label to the server. However, the target node can
* explicitly override the client's identity by a
* custom label via the 'label' attribute.
*/
typedef String<Session_label::capacity()> Label;
Label const target_label =
target.attribute_value("label", Label(label.string()));
if (target.has_type("parent")) {
if ((service = find_service(_parent_services, service_name)))
return Route { *service, target_label };
2011-12-22 16:19:25 +01:00
if (!service_wildcard) {
warning(name(), ": service lookup for "
"\"", service_name, "\" at parent failed");
2017-01-03 18:12:53 +01:00
throw Parent::Service_denied();
2011-12-22 16:19:25 +01:00
}
}
2011-12-22 16:19:25 +01:00
if (target.has_type("child")) {
2011-12-22 16:19:25 +01:00
typedef Name_registry::Name Name;
Name server_name = target.attribute_value("name", Name());
server_name = _name_registry.deref_alias(server_name);
_child_services.for_each([&] (Routed_service &s) {
if (s.name() == Service::Name(service_name)
&& s.child_name() == server_name)
service = &s; });
if (service)
return Route { *service, target_label };
2011-12-22 16:19:25 +01:00
if (!service_wildcard) {
2017-01-03 18:12:53 +01:00
warning(name(), ": lookup to child "
"server \"", server_name, "\" failed");
throw Parent::Service_denied();
2011-12-22 16:19:25 +01:00
}
}
2011-12-22 16:19:25 +01:00
if (target.has_type("any-child")) {
if (is_ambiguous(_child_services, service_name)) {
error(name(), ": ambiguous routes to "
"service \"", service_name, "\"");
2017-01-03 18:12:53 +01:00
throw Parent::Service_denied();
2011-12-22 16:19:25 +01:00
}
if ((service = find_service(_child_services, service_name)))
return Route { *service, target_label };
2011-12-22 16:19:25 +01:00
if (!service_wildcard) {
warning(name(), ": lookup for service "
"\"", service_name, "\" failed");
2017-01-03 18:12:53 +01:00
throw Parent::Service_denied();
}
2011-12-22 16:19:25 +01:00
}
if (target.last())
break;
2011-12-22 16:19:25 +01:00
}
}
} catch (...) {
warning(name(), ": no route to service \"", service_name, "\"");
2011-12-22 16:19:25 +01:00
}
if (!service)
2017-01-03 18:12:53 +01:00
throw Parent::Service_denied();
return Route { *service };
}
2011-12-22 16:19:25 +01:00
void filter_session_args(Service::Name const &service,
2017-01-03 18:12:53 +01:00
char *args, size_t args_len) override
{
_priority_policy. filter_session_args(service.string(), args, args_len);
_configfile_policy. filter_session_args(service.string(), args, args_len);
_ram_session_policy.filter_session_args(service.string(), args, args_len);
}
2017-01-03 18:12:53 +01:00
Affinity filter_session_affinity(Affinity const &session_affinity) override
{
Affinity::Space const &child_space = _resources.affinity.space();
Affinity::Location const &child_location = _resources.affinity.location();
/* check if no valid affinity space was specified */
if (session_affinity.space().total() == 0)
return Affinity(child_space, child_location);
Affinity::Space const &session_space = session_affinity.space();
Affinity::Location const &session_location = session_affinity.location();
/* scale resolution of resulting space */
Affinity::Space space(child_space.multiply(session_space));
/* subordinate session affinity to child affinity subspace */
Affinity::Location location(child_location
.multiply_position(session_space)
.transpose(session_location.xpos(),
session_location.ypos()));
return Affinity(space, location);
}
2011-12-22 16:19:25 +01:00
2017-01-03 18:12:53 +01:00
void announce_service(Service::Name const &service_name) override
{
log("child \"", name(), "\" announces service \"", service_name, "\"");
bool found = false;
_child_services.for_each([&] (Routed_service &service) {
if (service.has_id_space(_session_requester.id_space())
&& service.name() == service_name)
found = true; });
if (!found)
2017-01-03 18:12:53 +01:00
error(name(), ": illegal announcement of "
"service \"", service_name, "\"");
}
2017-01-03 18:12:53 +01:00
void resource_request(Parent::Resource_args const &args) override
{
log("child \"", name(), "\" requests resources: ", args.string());
2017-01-03 18:12:53 +01:00
size_t const requested_ram_quota =
Arg_string::find_arg(args.string(), "ram_quota")
.ulong_value(0);
2017-01-03 18:12:53 +01:00
if (avail_slack_ram_quota(_env.ram().avail()) < requested_ram_quota) {
warning("cannot respond to resource request - out of memory");
return;
}
2017-01-03 18:12:53 +01:00
_env.ram().transfer_quota(_child.ram_session_cap(),
requested_ram_quota);
/* wake up child that was starved for resources */
_child.notify_resource_avail();
}
void exit(int exit_value) override
{
try {
if (_start_node.sub_node("exit").attribute_value("propagate", false)) {
2017-01-03 18:12:53 +01:00
_env.parent().exit(exit_value);
return;
}
} catch (...) { }
/*
* Print a message as the exit is not handled otherwise. There are
* a number of automated tests that rely on this message. It is
* printed by the default implementation of 'Child_policy::exit'.
*/
Child_policy::exit(exit_value);
}
};
2011-12-22 16:19:25 +01:00
#endif /* _INCLUDE__INIT__CHILD_H_ */