genode/repos/os/src/app/trace_subject_reporter/main.cc

240 lines
5.5 KiB
C++
Raw Normal View History

2015-06-15 20:41:59 +02:00
/*
* \brief Report information about present trace subjects
* \author Norman Feske
* \date 2015-06-15
*/
/*
* Copyright (C) 2015-2017 Genode Labs GmbH
2015-06-15 20:41:59 +02: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.
2015-06-15 20:41:59 +02:00
*/
/* Genode includes */
#include <base/attached_rom_dataspace.h>
2015-06-15 20:41:59 +02:00
#include <trace_session/connection.h>
#include <timer_session/connection.h>
2016-11-25 16:54:49 +01:00
#include <base/component.h>
#include <base/attached_rom_dataspace.h>
#include <base/heap.h>
2015-06-15 20:41:59 +02:00
#include <os/reporter.h>
#include <util/retry.h>
2015-06-15 20:41:59 +02:00
struct Trace_subject_registry
{
private:
struct Entry : Genode::List<Entry>::Element
{
Genode::Trace::Subject_id const id;
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
Genode::Trace::Subject_info info { };
2015-06-15 20:41:59 +02:00
/**
* Execution time during the last period
*/
unsigned long long recent_execution_time = 0;
Entry(Genode::Trace::Subject_id id) : id(id) { }
void update(Genode::Trace::Subject_info const &new_info)
{
unsigned long long const last_execution_time = info.execution_time().thread_context;
2015-06-15 20:41:59 +02:00
info = new_info;
recent_execution_time = info.execution_time().thread_context - last_execution_time;
2015-06-15 20:41:59 +02:00
}
};
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
Genode::List<Entry> _entries { };
2015-06-15 20:41:59 +02:00
Entry *_lookup(Genode::Trace::Subject_id const id)
{
for (Entry *e = _entries.first(); e; e = e->next())
if (e->id == id)
return e;
return nullptr;
}
enum { MAX_SUBJECTS = 512 };
Genode::Trace::Subject_id _subjects[MAX_SUBJECTS];
void _sort_by_recent_execution_time()
{
Genode::List<Entry> sorted;
while (_entries.first()) {
/* find entry with lowest recent execution time */
Entry *lowest = _entries.first();
for (Entry *e = _entries.first(); e; e = e->next()) {
if (e->recent_execution_time < lowest->recent_execution_time)
lowest = e;
}
_entries.remove(lowest);
sorted.insert(lowest);
}
_entries = sorted;
}
unsigned update_subjects(Genode::Trace::Connection &trace)
{
return Genode::retry<Genode::Out_of_ram>(
[&] () { return trace.subjects(_subjects, MAX_SUBJECTS); },
[&] () { trace.upgrade_ram(4096); }
);
}
2015-06-15 20:41:59 +02:00
public:
void update(Genode::Trace::Connection &trace, Genode::Allocator &alloc)
{
unsigned const num_subjects = update_subjects(trace);
2015-06-15 20:41:59 +02:00
/* add and update existing entries */
for (unsigned i = 0; i < num_subjects; i++) {
Genode::Trace::Subject_id const id = _subjects[i];
Entry *e = _lookup(id);
if (!e) {
e = new (alloc) Entry(id);
_entries.insert(e);
}
e->update(trace.subject_info(id));
/* purge dead threads */
if (e->info.state() == Genode::Trace::Subject_info::DEAD) {
trace.free(e->id);
_entries.remove(e);
Genode::destroy(alloc, e);
}
}
_sort_by_recent_execution_time();
}
void report(Genode::Xml_generator &xml,
bool report_affinity, bool report_activity)
{
for (Entry const *e = _entries.first(); e; e = e->next()) {
xml.node("subject", [&] () {
xml.attribute("label", e->info.session_label().string());
xml.attribute("thread", e->info.thread_name().string());
xml.attribute("id", e->id.id);
typedef Genode::Trace::Subject_info Subject_info;
Subject_info::State const state = e->info.state();
xml.attribute("state", Subject_info::state_name(state));
if (report_activity)
xml.node("activity", [&] () {
xml.attribute("total", e->info.execution_time().thread_context);
2015-06-15 20:41:59 +02:00
xml.attribute("recent", e->recent_execution_time);
});
if (report_affinity)
xml.node("affinity", [&] () {
xml.attribute("xpos", e->info.affinity().xpos());
xml.attribute("ypos", e->info.affinity().ypos());
});
});
}
}
};
2016-11-25 16:54:49 +01:00
namespace App {
struct Main;
using namespace Genode;
}
struct App::Main
2015-06-15 20:41:59 +02:00
{
2016-11-25 16:54:49 +01:00
Env &_env;
Trace::Connection _trace { _env, 128*1024, 32*1024, 0 };
2015-06-15 20:41:59 +02:00
Reporter _reporter { _env, "trace_subjects", "trace_subjects", 64*1024 };
2015-06-15 20:41:59 +02:00
static uint64_t _default_period_ms() { return 5000; }
2015-06-15 20:41:59 +02:00
uint64_t _period_ms = _default_period_ms();
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
bool _report_affinity = false;
bool _report_activity = false;
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
Attached_rom_dataspace _config { _env, "config" };
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
bool _config_report_attribute_enabled(char const *attr) const
2015-06-15 20:41:59 +02:00
{
try {
2016-11-25 16:54:49 +01:00
return _config.xml().sub_node("report").attribute_value(attr, false);
2015-06-15 20:41:59 +02:00
} catch (...) { return false; }
}
2016-11-25 16:54:49 +01:00
Timer::Connection _timer { _env };
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
Heap _heap { _env.ram(), _env.rm() };
2015-06-15 20:41:59 +02:00
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
Trace_subject_registry _trace_subject_registry { };
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
void _handle_config();
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
Signal_handler<Main> _config_handler = {
_env.ep(), *this, &Main::_handle_config};
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
void _handle_period();
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
Signal_handler<Main> _periodic_handler = {
_env.ep(), *this, &Main::_handle_period};
Main(Env &env) : _env(env)
2015-06-15 20:41:59 +02:00
{
2016-11-25 16:54:49 +01:00
_config.sigh(_config_handler);
_handle_config();
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
_timer.sigh(_periodic_handler);
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
_reporter.enabled(true);
2015-06-15 20:41:59 +02:00
}
};
2016-11-25 16:54:49 +01:00
void App::Main::_handle_config()
2015-06-15 20:41:59 +02:00
{
2016-11-25 16:54:49 +01:00
_config.update();
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
_period_ms = _config.xml().attribute_value("period_ms", _default_period_ms());
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
_report_affinity = _config_report_attribute_enabled("affinity");
_report_activity = _config_report_attribute_enabled("activity");
2015-06-15 20:41:59 +02:00
2016-11-25 16:54:49 +01:00
_timer.trigger_periodic(1000*_period_ms);
2015-06-15 20:41:59 +02:00
}
2016-11-25 16:54:49 +01:00
void App::Main::_handle_period()
2015-06-15 20:41:59 +02:00
{
/* update subject information */
2016-11-25 16:54:49 +01:00
_trace_subject_registry.update(_trace, _heap);
2015-06-15 20:41:59 +02:00
/* generate report */
2016-11-25 16:54:49 +01:00
_reporter.clear();
Genode::Reporter::Xml_generator xml(_reporter, [&] ()
2015-06-15 20:41:59 +02:00
{
2016-11-25 16:54:49 +01:00
_trace_subject_registry.report(xml, _report_affinity, _report_activity);
2015-06-15 20:41:59 +02:00
});
}
2016-11-25 16:54:49 +01:00
void Component::construct(Genode::Env &env) { static App::Main main(env); }
2015-06-15 20:41:59 +02:00