genode/repos/os/src/server/trace_fs/node_handle_registry.h

182 lines
4.1 KiB
C
Raw Normal View History

os: initial version of trace_fs The trace_fs server provides access to a Trace_session by using a File_system_session as frontend. Each trace subject is represented by a directory ('thread_name.subject') that contains specific files ('active', 'cleanup', 'enable', 'events', 'buffer_size' and 'policy'), which are used to control the tracing process of the thread as well as storing the content of its trace buffer. The tracing of a thread is only activated if there is a valid policy installed and the intend to trace the subject was made clear by writing '1' to the 'enable' file. The tracing of a thread may be deactived by writing a '0' to the 'enable' file. A policy may be changed by overwriting the currently used one. In this case the old policy is replaced by the new policy and is automatically utilize. Writing a value to the 'buffer_size' file changes the appointed size of the trace buffer. This value is only evaluted by reactivating the tracing process. The content of the trace buffer may be accessed by reading from the 'events' file. Throughout all tracing session new trace events are appended to this file. Nodes of UNTRACED subjects are kept as long as they do not change their tracing state to DEAD. In this case all nodes are removed from the file system. Subjects that were traced before and are now UNTRACED will only be removed by writing '1' to the 'cleanup' file - even if they are DEAD by now. To use the trace_fs a config similar to the following may be used: ! <start name="trace_fs"> ! <resource name="RAM" quantum="128M"/> ! <provides><service name="File_system"/></provides> ! <config> ! <policy label="noux -> trace" interval="1000" subject_limit="512" trace_quota="64M" /> ! </config> ! </start> 'interval' sets the periode in which the Trace_session is polled. The time is given in milliseconds. 'subject_limit' speficies how many trace subject should by acquired at most when the Trace_session is polled. 'trace_quota' is the amount of quota the trace_fs should use for the Trace_session connection. The remaing amount of RAM quota will be used for the actual nodes of the file system and the 'policy' as well as the 'events' files. In addiition there are 'buffer_size' and 'buffer_size_limit' that define the initial and the upper limit of the size of a trace buffer. Tracing of parent processes or rather threads may be enabled by setting 'parent_levels' to a value greater than '0' (though this attribute is available, the trace session component within core still lacks support for it). A ready-to-use runscript can by found in 'ports/run/noux_trace_fs.run'. Fixes #1049.
2014-01-17 16:04:04 +01:00
/*
* \brief Facility for managing the session-local node-handle namespace
* \author Norman Feske
* \date 2012-04-11
*/
#ifndef _NODE_HANDLE_REGISTRY_H_
#define _NODE_HANDLE_REGISTRY_H_
namespace File_system {
class Node;
class Directory;
class File;
class Symlink;
/**
* Type trait for determining the node type for a given handle type
*/
template<typename T> struct Node_type;
template<> struct Node_type<Node_handle> { typedef Node Type; };
template<> struct Node_type<Dir_handle> { typedef Directory Type; };
template<> struct Node_type<File_handle> { typedef File Type; };
template<> struct Node_type<Symlink_handle> { typedef Symlink Type; };
/**
* Type trait for determining the handle type for a given node type
*/
template<typename T> struct Handle_type;
template<> struct Handle_type<Node> { typedef Node_handle Type; };
template<> struct Handle_type<Directory> { typedef Dir_handle Type; };
template<> struct Handle_type<File> { typedef File_handle Type; };
template<> struct Handle_type<Symlink> { typedef Symlink_handle Type; };
class Node_handle_registry
{
private:
/* maximum number of open nodes per session */
enum { MAX_NODE_HANDLES = 128U };
Lock mutable _lock;
Node *_nodes[MAX_NODE_HANDLES];
/**
* Each open node handle can act as a listener to be informed about
* node changes.
*/
Listener _listeners[MAX_NODE_HANDLES];
/**
* Allocate node handle
*
* \throw Out_of_node_handles
*/
int _alloc(Node *node)
{
Lock::Guard guard(_lock);
for (unsigned i = 0; i < MAX_NODE_HANDLES; i++)
if (!_nodes[i]) {
_nodes[i] = node;
return i;
}
throw Out_of_node_handles();
}
bool _in_range(int handle) const
{
return ((handle >= 0) && (handle < MAX_NODE_HANDLES));
}
public:
Node_handle_registry()
{
for (unsigned i = 0; i < MAX_NODE_HANDLES; i++)
_nodes[i] = 0;
}
template <typename NODE_TYPE>
typename Handle_type<NODE_TYPE>::Type alloc(NODE_TYPE *node)
{
typedef typename Handle_type<NODE_TYPE>::Type Handle;
return Handle(_alloc(node));
}
/**
* Release node handle
*/
void free(Node_handle handle)
{
if (!_in_range(handle.value))
return;
/*
* Notify listeners about the changed file.
*/
Node *node = dynamic_cast<Node *>(_nodes[handle.value]);
if (!node) { return; }
node->notify_listeners();
/*
* De-allocate handle
*/
Listener &listener = _listeners[handle.value];
if (listener.valid())
node->remove_listener(&listener);
_nodes[handle.value] = 0;
listener = Listener();
}
/**
* Lookup node using its handle as key
*
* \throw Invalid_handle
*/
template <typename HANDLE_TYPE>
typename Node_type<HANDLE_TYPE>::Type *lookup(HANDLE_TYPE handle)
{
if (!_in_range(handle.value))
throw Invalid_handle();
typedef typename Node_type<HANDLE_TYPE>::Type Node;
Node *node = dynamic_cast<Node *>(_nodes[handle.value]);
if (!node)
throw Invalid_handle();
return node;
}
bool refer_to_same_node(Node_handle h1, Node_handle h2) const
{
if (!_in_range(h1.value) || !_in_range(h2.value)) {
PDBG("refer_to_same_node -> Invalid_handle");
throw Invalid_handle();
}
return _nodes[h1.value] == _nodes[h2.value];
}
/**
* Register signal handler to be notified of node changes
*/
void sigh(Node_handle handle, Signal_context_capability sigh)
{
if (!_in_range(handle.value))
throw Invalid_handle();
Node *node = dynamic_cast<Node *>(_nodes[handle.value]);
if (!node) {
PDBG("Invalid_handle");
throw Invalid_handle();
}
Listener &listener = _listeners[handle.value];
/*
* If there was already a handler registered for the node,
* remove the old handler.
*/
if (listener.valid())
node->remove_listener(&listener);
/*
* Register new handler
*/
listener = Listener(sigh);
node->add_listener(&listener);
}
};
}
#endif /* _NODE_HANDLE_REGISTRY_H_ */