start using BufferedWriter (#1878)

* do the part up to actual usage

* borrow some memory for now
This commit is contained in:
Matthew Kennedy 2020-10-14 17:06:05 -07:00 committed by GitHub
parent 2a0205e3c4
commit 2855e04234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 52 deletions

View File

@ -61,6 +61,7 @@
#include "periodic_thread_controller.h" #include "periodic_thread_controller.h"
#include "cdm_ion_sense.h" #include "cdm_ion_sense.h"
#include "binary_logging.h" #include "binary_logging.h"
#include "buffered_writer.h"
extern bool main_loop_started; extern bool main_loop_started;
@ -142,7 +143,7 @@ static float getAirFlowGauge(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
return hasMafSensor() ? getRealMaf(PASS_ENGINE_PARAMETER_SIGNATURE) : engine->engineState.airFlow; return hasMafSensor() ? getRealMaf(PASS_ENGINE_PARAMETER_SIGNATURE) : engine->engineState.airFlow;
} }
void writeLogLine() { void writeLogLine(Writer& buffer) {
#if EFI_FILE_LOGGING #if EFI_FILE_LOGGING
if (!main_loop_started) if (!main_loop_started)
return; return;
@ -157,7 +158,7 @@ void writeLogLine() {
efiAssertVoid(OBD_PCM_Processor_Fault, length <= efi::size(sdLogBuffer), "SD log buffer overflow"); efiAssertVoid(OBD_PCM_Processor_Fault, length <= efi::size(sdLogBuffer), "SD log buffer overflow");
appendToLog(sdLogBuffer, length); buffer.write(sdLogBuffer, length);
binaryLogCount++; binaryLogCount++;
#endif /* EFI_FILE_LOGGING */ #endif /* EFI_FILE_LOGGING */

View File

@ -13,5 +13,7 @@ void updateDevConsoleState(void);
void prepareTunerStudioOutputs(void); void prepareTunerStudioOutputs(void);
void startStatusThreads(void); void startStatusThreads(void);
void initStatusLoop(void); void initStatusLoop(void);
void writeLogLine(void);
struct Writer;
void writeLogLine(Writer& buffer);
void printOverallStatus(systime_t nowSeconds); void printOverallStatus(systime_t nowSeconds);

View File

@ -707,7 +707,7 @@ void initEngineContoller(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX)
* UNUSED_SIZE constants. * UNUSED_SIZE constants.
*/ */
#ifndef RAM_UNUSED_SIZE #ifndef RAM_UNUSED_SIZE
#define RAM_UNUSED_SIZE 2850 #define RAM_UNUSED_SIZE 2350
#endif #endif
#ifndef CCM_UNUSED_SIZE #ifndef CCM_UNUSED_SIZE
#define CCM_UNUSED_SIZE 2900 #define CCM_UNUSED_SIZE 2900

View File

@ -25,6 +25,7 @@
#include "engine_configuration.h" #include "engine_configuration.h"
#include "status_loop.h" #include "status_loop.h"
#include "usb_msd_cfg.h" #include "usb_msd_cfg.h"
#include "buffered_writer.h"
#include "rtc_helper.h" #include "rtc_helper.h"
@ -304,49 +305,6 @@ static void listDirectory(const char *path) {
UNLOCK_SD_SPI; UNLOCK_SD_SPI;
} }
static int errorReported = FALSE; // this is used to report the error only once
#if 0
void readLogFileContent(char *buffer, short fileId, short offset, short length) {
}
#endif
/**
* @brief Appends specified line to the current log file
*/
void appendToLog(const char *line, size_t lineLength) {
UINT bytesWritten;
if (!isSdCardAlive()) {
if (!errorReported)
scheduleMsg(&logger, "appendToLog Error: No File system is mounted");
errorReported = TRUE;
return;
}
totalLoggedBytes += lineLength;
LOCK_SD_SPI;
FRESULT err = f_write(&FDLogFile, line, lineLength, &bytesWritten);
if (bytesWritten < lineLength) {
printError("write error or disk full", err); // error or disk full
mmcUnMount();
} else {
writeCounter++;
totalWritesCounter++;
if (writeCounter >= F_SYNC_FREQUENCY) {
/**
* Performance optimization: not f_sync after each line, f_sync is probably a heavy operation
* todo: one day someone should actually measure the relative cost of f_sync
*/
f_sync(&FDLogFile);
totalSyncCounter++;
writeCounter = 0;
}
}
UNLOCK_SD_SPI;
}
/* /*
* MMC card un-mount. * MMC card un-mount.
*/ */
@ -439,6 +397,39 @@ static void MMCmount(void) {
} }
} }
class SdLogBufferWriter final : public BufferedWriter<512> {
size_t writeInternal(const char* buffer, size_t count) override {
size_t bytesWritten;
totalLoggedBytes += count;
LOCK_SD_SPI;
FRESULT err = f_write(&FDLogFile, buffer, count, &bytesWritten);
if (bytesWritten != count) {
printError("write error or disk full", err); // error or disk full
mmcUnMount();
} else {
writeCounter++;
totalWritesCounter++;
if (writeCounter >= F_SYNC_FREQUENCY) {
/**
* Performance optimization: not f_sync after each line, f_sync is probably a heavy operation
* todo: one day someone should actually measure the relative cost of f_sync
*/
f_sync(&FDLogFile);
totalSyncCounter++;
writeCounter = 0;
}
}
UNLOCK_SD_SPI;
return bytesWritten;
}
};
static SdLogBufferWriter logBuffer MAIN_RAM;
static THD_FUNCTION(MMCmonThread, arg) { static THD_FUNCTION(MMCmonThread, arg) {
(void)arg; (void)arg;
chRegSetThreadName("MMC_Monitor"); chRegSetThreadName("MMC_Monitor");
@ -462,7 +453,7 @@ static THD_FUNCTION(MMCmonThread, arg) {
} }
if (isSdCardAlive()) { if (isSdCardAlive()) {
writeLogLine(); writeLogLine(logBuffer);
} else { } else {
chThdSleepMilliseconds(100); chThdSleepMilliseconds(100);
} }
@ -501,9 +492,6 @@ void initMmcCard(void) {
chThdCreateStatic(mmcThreadStack, sizeof(mmcThreadStack), LOWPRIO, (tfunc_t)(void*) MMCmonThread, NULL); chThdCreateStatic(mmcThreadStack, sizeof(mmcThreadStack), LOWPRIO, (tfunc_t)(void*) MMCmonThread, NULL);
addConsoleAction("mountsd", MMCmount); addConsoleAction("mountsd", MMCmount);
addConsoleActionS("appendtolog", [](const char* str) {
appendToLog(str, strlen(str));
});
addConsoleAction("umountsd", mmcUnMount); addConsoleAction("umountsd", mmcUnMount);
addConsoleActionS("ls", listDirectory); addConsoleActionS("ls", listDirectory);
addConsoleActionS("del", removeFile); addConsoleActionS("del", removeFile);

View File

@ -16,7 +16,6 @@
bool isLogFile(const char *fileName); bool isLogFile(const char *fileName);
void initMmcCard(void); void initMmcCard(void);
bool isSdCardAlive(void); bool isSdCardAlive(void);
void appendToLog(const char *line, size_t length);
void readLogFileContent(char *buffer, short fileId, short offset, short length); void readLogFileContent(char *buffer, short fileId, short offset, short length);