mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-30 09:33:33 +08:00
logger: remove _log_buffer from Logger, initialize it in the writer instead
it's not used in the logger, so don't store it there. It is accessed via LogWriter::write. This also makes sure the buffer size is >= _min_write_chunk and handles allocation failure properly.
This commit is contained in:
@@ -2,15 +2,15 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <mathlib/mathlib.h>
|
||||||
|
|
||||||
namespace px4
|
namespace px4
|
||||||
{
|
{
|
||||||
namespace logger
|
namespace logger
|
||||||
{
|
{
|
||||||
|
|
||||||
LogWriter::LogWriter(uint8_t *buffer, size_t buffer_size) :
|
LogWriter::LogWriter(size_t buffer_size) :
|
||||||
_buffer(buffer),
|
_buffer_size(math::max(buffer_size, _min_write_chunk))
|
||||||
_buffer_size(buffer_size),
|
|
||||||
_min_write_chunk(4096)
|
|
||||||
{
|
{
|
||||||
pthread_mutex_init(&_mtx, nullptr);
|
pthread_mutex_init(&_mtx, nullptr);
|
||||||
pthread_cond_init(&_cv, nullptr);
|
pthread_cond_init(&_cv, nullptr);
|
||||||
@@ -19,6 +19,24 @@ LogWriter::LogWriter(uint8_t *buffer, size_t buffer_size) :
|
|||||||
perf_fsync = perf_alloc(PC_ELAPSED, "sd fsync");
|
perf_fsync = perf_alloc(PC_ELAPSED, "sd fsync");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LogWriter::init()
|
||||||
|
{
|
||||||
|
if (_buffer) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
_buffer = new uint8_t[_buffer_size];
|
||||||
|
|
||||||
|
return _buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogWriter::~LogWriter()
|
||||||
|
{
|
||||||
|
if (_buffer) {
|
||||||
|
delete[] _buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LogWriter::start_log(const char *filename)
|
void LogWriter::start_log(const char *filename)
|
||||||
{
|
{
|
||||||
::strncpy(_filename, filename, sizeof(_filename));
|
::strncpy(_filename, filename, sizeof(_filename));
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ class LogWriter
|
|||||||
{
|
{
|
||||||
friend class Logger;
|
friend class Logger;
|
||||||
public:
|
public:
|
||||||
LogWriter(uint8_t *buffer, size_t buffer_size);
|
LogWriter(size_t buffer_size);
|
||||||
|
~LogWriter();
|
||||||
|
|
||||||
|
bool init();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start the thread
|
* start the thread
|
||||||
@@ -59,13 +62,15 @@ private:
|
|||||||
_count -= n;
|
_count -= n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 512 didn't seem to work properly, 4096 should match the FAT cluster size */
|
||||||
|
static const size_t _min_write_chunk = 4096;
|
||||||
|
|
||||||
char _filename[64];
|
char _filename[64];
|
||||||
int _fd;
|
int _fd;
|
||||||
uint8_t *_buffer;
|
uint8_t *_buffer = nullptr;
|
||||||
const size_t _buffer_size;
|
const size_t _buffer_size;
|
||||||
const size_t _min_write_chunk; /* 512 didn't seem to work properly, 4096 should match the FAT cluster size */
|
size_t _head = 0; ///< next position to write to
|
||||||
size_t _head = 0;
|
size_t _count = 0; ///< number of bytes in _buffer to be written
|
||||||
size_t _count = 0;
|
|
||||||
size_t _total_written = 0;
|
size_t _total_written = 0;
|
||||||
bool _should_run = false;
|
bool _should_run = false;
|
||||||
bool _running = false;
|
bool _running = false;
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ static constexpr size_t MAX_DATA_SIZE = 740;
|
|||||||
|
|
||||||
Logger::Logger(size_t buffer_size, unsigned log_interval, bool log_on_start) :
|
Logger::Logger(size_t buffer_size, unsigned log_interval, bool log_on_start) :
|
||||||
_log_on_start(log_on_start),
|
_log_on_start(log_on_start),
|
||||||
_writer((_log_buffer = new uint8_t[buffer_size]), buffer_size),
|
_writer(buffer_size),
|
||||||
_log_interval(log_interval)
|
_log_interval(log_interval)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -280,9 +280,6 @@ Logger::~Logger()
|
|||||||
}
|
}
|
||||||
} while (logger_task != -1);
|
} while (logger_task != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete [] _log_buffer;
|
|
||||||
logger_ptr = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Logger::add_topic(const orb_metadata *topic)
|
int Logger::add_topic(const orb_metadata *topic)
|
||||||
@@ -414,6 +411,11 @@ void Logger::run()
|
|||||||
// add_topic("estimator_status");
|
// add_topic("estimator_status");
|
||||||
add_topic("vehicle_status", 100);
|
add_topic("vehicle_status", 100);
|
||||||
|
|
||||||
|
if (!_writer.init()) {
|
||||||
|
PX4_ERR("logger: init of writer failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int ret = _writer.thread_start(_writer_thread);
|
int ret = _writer.thread_start(_writer_thread);
|
||||||
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
|||||||
@@ -80,13 +80,12 @@ private:
|
|||||||
|
|
||||||
bool copy_if_updated_multi(orb_id_t topic, int multi_instance, int *handle, void *buffer, uint64_t *time_last_checked);
|
bool copy_if_updated_multi(orb_id_t topic, int multi_instance, int *handle, void *buffer, uint64_t *time_last_checked);
|
||||||
|
|
||||||
static constexpr size_t MAX_TOPICS_NUM = 128;
|
static constexpr size_t MAX_TOPICS_NUM = 128; /**< Maximum number of logged topics */
|
||||||
static constexpr unsigned MAX_NO_LOGFOLDER = 999; /**< Maximum number of log dirs */
|
static constexpr unsigned MAX_NO_LOGFOLDER = 999; /**< Maximum number of log dirs */
|
||||||
static constexpr unsigned MAX_NO_LOGFILE = 999; /**< Maximum number of log files */
|
static constexpr unsigned MAX_NO_LOGFILE = 999; /**< Maximum number of log files */
|
||||||
static constexpr const char *LOG_ROOT = PX4_ROOTFSDIR"/fs/microsd/log";
|
static constexpr const char *LOG_ROOT = PX4_ROOTFSDIR"/fs/microsd/log";
|
||||||
|
|
||||||
bool _task_should_exit = true;
|
bool _task_should_exit = true;
|
||||||
uint8_t *_log_buffer;
|
|
||||||
char _log_dir[64];
|
char _log_dir[64];
|
||||||
uORB::Subscription<vehicle_status_s> _vehicle_status_sub {ORB_ID(vehicle_status)};
|
uORB::Subscription<vehicle_status_s> _vehicle_status_sub {ORB_ID(vehicle_status)};
|
||||||
uORB::Subscription<parameter_update_s> _parameter_update_sub {ORB_ID(parameter_update)};
|
uORB::Subscription<parameter_update_s> _parameter_update_sub {ORB_ID(parameter_update)};
|
||||||
|
|||||||
Reference in New Issue
Block a user