trace: consolidate Trace_buffer implementations

Both, trace_logger and vfs_trace had their own trace_buffer.h. This
commit consolidates the existing implementations and provides the
resulting trace_buffer.h at 'include/trace/'. It thereby becomes part of
the trace api archive.

genodelabs/genode#4244
This commit is contained in:
Johannes Schlatow 2021-08-23 17:00:01 +02:00 committed by Christian Helmuth
parent ffbd26d63f
commit b5dd1dd01b
7 changed files with 16 additions and 101 deletions

View File

@ -3,3 +3,4 @@ os
so
vfs
gems
trace

View File

@ -20,9 +20,9 @@
#include <util/xml_generator.h>
#include <trace_session/connection.h>
#include <trace/trace_buffer.h>
#include "directory_tree.h"
#include "trace_buffer.h"
namespace Vfs_trace {

View File

@ -11,8 +11,8 @@
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _TRACE_BUFFER_H_
#define _TRACE_BUFFER_H_
#ifndef _TRACE__TRACE_BUFFER_H_
#define _TRACE__TRACE_BUFFER_H_
/* Genode includes */
#include <base/trace/buffer.h>
@ -42,8 +42,13 @@ class Trace_buffer
using namespace Genode;
bool wrapped = _buffer.wrapped() != _wrapped_count;
if (wrapped)
if (wrapped) {
if ((_buffer.wrapped() - 1) != _wrapped_count) {
warning("buffer wrapped multiple times; you might want to raise buffer size; curr_count=",
_buffer.wrapped(), " last_count=", _wrapped_count);
}
_wrapped_count = _buffer.wrapped();
}
Trace::Buffer::Entry new_curr { _curr };
Trace::Buffer::Entry entry { _curr };
@ -92,4 +97,4 @@ class Trace_buffer
};
#endif /* _TRACE_BUFFER_H_ */
#endif /* _TRACE__TRACE_BUFFER_H_ */

View File

@ -1,3 +1,4 @@
base
os
timer_session
trace

View File

@ -118,7 +118,7 @@ void Monitor::print(bool activity, bool affinity)
/* get readable data length and skip empty entries */
size_t length = min(entry.length(), (unsigned)MAX_ENTRY_LENGTH - 1);
if (!length)
return;
return true;
/* copy entry data from buffer and add terminating '0' */
memcpy(_curr_entry_data, entry.data(), length);
@ -134,6 +134,8 @@ void Monitor::print(bool activity, bool affinity)
printed_buf_entries = true;
}
log(Cstring(_curr_entry_data));
return true;
});
/* print end tags */
if (printed_buf_entries)

View File

@ -16,10 +16,10 @@
/* local includes */
#include <avl_tree.h>
#include <trace_buffer.h>
/* Genode includes */
#include <base/trace/types.h>
#include <trace/trace_buffer.h>
namespace Genode { namespace Trace { class Connection; } }

View File

@ -1,94 +0,0 @@
/*
* \brief Wrapper for Trace::Buffer that adds some convenient functionality
* \author Martin Stein
* \date 2018-01-12
*/
/*
* Copyright (C) 2018 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _TRACE_BUFFER_H_
#define _TRACE_BUFFER_H_
/* Genode includes */
#include <base/trace/buffer.h>
/**
* Wrapper for Trace::Buffer that adds some convenient functionality
*/
class Trace_buffer
{
private:
Genode::Trace::Buffer &_buffer;
Genode::Trace::Buffer::Entry _curr { _buffer.first() };
unsigned _wrapped_count { 0 };
public:
Trace_buffer(Genode::Trace::Buffer &buffer) : _buffer(buffer) { }
/**
* Call functor for each entry that wasn't yet processed
*/
template <typename FUNC>
void for_each_new_entry(FUNC && functor)
{
using namespace Genode;
bool wrapped = _buffer.wrapped() != _wrapped_count;
if (wrapped) {
if ((_buffer.wrapped() - 1) != _wrapped_count) {
warning("buffer wrapped multiple times; you might want to raise buffer size; curr_count=",
_buffer.wrapped(), " last_count=", _wrapped_count);
}
_wrapped_count = _buffer.wrapped();
}
Trace::Buffer::Entry entry { _curr };
/**
* If '_curr' is marked 'last' (i.e., the entry pointer it contains
* is invalid), either this method wasn't called before on this
* buffer or the buffer was empty on all previous calls (note that
* '_curr' is only updated with valid entry pointers by this
* method). In this case, we start with the first entry (if any).
*
* If '_curr' is not marked 'last' it points to the last processed
* entry and we proceed with the, so far unprocessed entry next to
* it (if any). Note that in this case, the entry behind '_curr'
* might have got overridden since the last call to this method
* because the buffer wrapped and oustripped the entry consumer.
* This problem is well known and should be avoided by choosing a
* large enough buffer.
*/
if (entry.last())
entry = _buffer.first();
else
entry = _buffer.next(entry);
/* iterate over all entries that were not processed yet */
for (; wrapped || !entry.last(); entry = _buffer.next(entry))
{
/* if buffer wrapped, we pass the last entry once and continue at first entry */
if (wrapped && entry.last()) {
wrapped = false;
entry = _buffer.first();
if (entry.last())
break;
}
/* remember the last processed entry in _curr */
_curr = entry;
functor(_curr);
}
}
};
#endif /* _TRACE_BUFFER_H_ */