From 42771be625cdc4a3eada6fd42239261455e08c60 Mon Sep 17 00:00:00 2001 From: Soo-Hyun Yoo Date: Wed, 17 Jun 2015 14:27:56 -0700 Subject: [PATCH] refs #45 Tweak buffer sizes and things. --- src/filesystem/filesystem.cpp | 14 +++++++++----- src/filesystem/filesystem.hpp | 5 +++++ src/filesystem/fs_writer_thread.cpp | 19 +++++++------------ src/filesystem/fs_writer_thread.hpp | 10 ++++++---- src/filesystem/fs_writer_thread.tpp | 10 ++++------ src/filesystem/logger.cpp | 2 +- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/filesystem/filesystem.cpp b/src/filesystem/filesystem.cpp index bdbcb90..2fa4d86 100644 --- a/src/filesystem/filesystem.cpp +++ b/src/filesystem/filesystem.cpp @@ -137,18 +137,22 @@ void FileSystem::write(uint8_t *buf, uint16_t len) { // TODO(yoos): set FS error flag //chprintf(chp, "write failed\r\n"); } - err = f_sync(&FileObject); // TODO(yoos): This ensures data gets written, but bad for performance... - if (err != FR_OK) { - // TODO(yoos): set FS error flag - //chprintf(chp, "sync failed\r\n"); - } if (bytes_written != len) { // TODO(yoos): set FS error flag //chprintf(chp, "write incomplete\r\n"); } + //sync(); // TODO(yoos): This ensures data gets written, but bad for performance... // TODO(yoos): On failure, reset logger to wait state } +void FileSystem::sync(void) { + err = f_sync(&FileObject); + if (err != FR_OK) { + // TODO(yoos): set FS error flag + //chprintf(chp, "sync failed\r\n"); + } +} + void FileSystem::getFn(char *buf) { std::strcpy(buf, fname); } diff --git a/src/filesystem/filesystem.hpp b/src/filesystem/filesystem.hpp index 1018c45..b08b580 100644 --- a/src/filesystem/filesystem.hpp +++ b/src/filesystem/filesystem.hpp @@ -65,6 +65,11 @@ class FileSystem { */ void write(uint8_t *c, uint16_t len); + /** + * Sync filesystem. + */ + void sync(void); + /** * Get short filename */ diff --git a/src/filesystem/fs_writer_thread.cpp b/src/filesystem/fs_writer_thread.cpp index 52080bd..ffacca0 100644 --- a/src/filesystem/fs_writer_thread.cpp +++ b/src/filesystem/fs_writer_thread.cpp @@ -8,6 +8,7 @@ FsWriterThread::FsWriterThread(SDCDriver& sdcd, Communicator& communicator) : fs(sdcd), fsInfoMessageStream(communicator, 1) { + rb_init(&buf, sizeof(_buf), _buf); } msg_t FsWriterThread::main() { @@ -17,19 +18,14 @@ msg_t FsWriterThread::main() { while (!fs.connect()) chThdSleepMilliseconds(10); while (!fs.mount()) chThdSleepMilliseconds(10); while (!fs.openNew()) chThdSleepMilliseconds(10); + fsReady = true; while(true) { - // Check if there is data in the buffer that has not yet been written. - if(bottom != top) { - size_t numbytes = (top > bottom) ? top-bottom : buffer.size()-bottom; - fs.write(&buffer[bottom], numbytes); - bottom += numbytes; - - // Wrap if the end of the buffer is reached. - if(bottom >= buffer.size()) { - bottom = 0; - } - } + uint32_t count = buf.count; + if (count > 3000) count = 3000 + 0.0625*(count-3000); + rb_remove(&buf, count, writebuf); + fs.write(writebuf, count); + fs.sync(); if (fsInfoMessageStream.ready()) { protocol::message::fs_info_message_t m; @@ -40,7 +36,6 @@ msg_t FsWriterThread::main() { fsInfoMessageStream.publish(m); } - // TODO(kyle): Just yield, or sleep? yield(); } diff --git a/src/filesystem/fs_writer_thread.hpp b/src/filesystem/fs_writer_thread.hpp index 262b9b0..ae1a0f3 100644 --- a/src/filesystem/fs_writer_thread.hpp +++ b/src/filesystem/fs_writer_thread.hpp @@ -9,11 +9,12 @@ #include "communication/communicator.hpp" #include "communication/rate_limited_stream.hpp" #include "filesystem/filesystem.hpp" +#include "filesystem/ringbuffer.hpp" /** * Thread to write to storage media without blocking control thread. */ -class FsWriterThread : public chibios_rt::BaseStaticThread<2048> { +class FsWriterThread : public chibios_rt::BaseStaticThread<1024> { public: FsWriterThread(SDCDriver& sdcd, Communicator& communicator); @@ -27,11 +28,12 @@ class FsWriterThread : public chibios_rt::BaseStaticThread<2048> { private: FileSystem fs; + bool fsReady; RateLimitedStream fsInfoMessageStream; - std::array buffer; - std::size_t bottom; - std::size_t top; + rb_t buf; + std::uint8_t _buf[83000]; + std::uint8_t writebuf[8000]; }; #include "filesystem/fs_writer_thread.tpp" diff --git a/src/filesystem/fs_writer_thread.tpp b/src/filesystem/fs_writer_thread.tpp index 2f02b57..d8d299c 100644 --- a/src/filesystem/fs_writer_thread.tpp +++ b/src/filesystem/fs_writer_thread.tpp @@ -1,10 +1,8 @@ +#include + template void FsWriterThread::append(std::array ap, std::size_t len) { - for(std::size_t i = 0; i < len; i++) { - buffer[top++] = ap[i]; - - if(top >= buffer.size()) { - top = 0; - } + if (fsReady) { + rb_add(&buf, len, ap.data()); } } diff --git a/src/filesystem/logger.cpp b/src/filesystem/logger.cpp index b23a517..f2ae83a 100644 --- a/src/filesystem/logger.cpp +++ b/src/filesystem/logger.cpp @@ -9,7 +9,7 @@ void Logger::start(void) { // TODO(yoos): Rethink this, as in the rocket, we really want to log data // before we transmit to ground, but this is not the case in radio-controlled // vehicles. Maybe priorities should be user-configurable. - writer.start(LOWPRIO + 2); + writer.start(HIGHPRIO-1); } bool Logger::ready(void) {