From 178ccdeb4d11a2c7ebb8fb9a809a6aa3cd79e378 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 23 Nov 2021 08:42:45 -0800 Subject: [PATCH] simulator writes binary log (#3598) * simulator writes log * replace the file * check that the sim actually fails * put it back * did the sim really not fail? * good, asan does catch it --- .gitignore | 3 + firmware/console/status_loop.cpp | 36 +++++------ firmware/hw_layer/mmc_card.cpp | 63 ++++++++++++++++---- simulator/simulator/efifeatures.h | 2 +- simulator/simulator/rusEfiFunctionalTest.cpp | 8 ++- 5 files changed, 77 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index dd62ca4310..13a012f48f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ log.txt # Eclipse .metadata/ + +# rusEFI simulator makes logs, ignore those +rusefi_simulator_log.mlg diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index fd992e1a56..536291aa76 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -129,6 +129,22 @@ static void setWarningEnabled(int value) { static NO_CACHE char sdLogBuffer[220]; static uint64_t binaryLogCount = 0; +void writeLogLine(Writer& buffer) { + if (!main_loop_started) + return; + + if (binaryLogCount == 0) { + writeHeader(buffer); + } else { + updateTunerStudioState(&tsOutputChannels); + size_t length = writeBlock(sdLogBuffer); + efiAssertVoid(OBD_PCM_Processor_Fault, length <= efi::size(sdLogBuffer), "SD log buffer overflow"); + buffer.write(sdLogBuffer, length); + } + + binaryLogCount++; +} + #endif /* EFI_FILE_LOGGING */ /** @@ -145,26 +161,6 @@ static float getAirFlowGauge() { return Sensor::get(SensorType::Maf).value_or(engine->engineState.airFlow); } -void writeLogLine(Writer& buffer) { -#if EFI_FILE_LOGGING - if (!main_loop_started) - return; - - if (binaryLogCount == 0) { - writeHeader(buffer); - } else { - updateTunerStudioState(&tsOutputChannels); - size_t length = writeBlock(sdLogBuffer); - efiAssertVoid(OBD_PCM_Processor_Fault, length <= efi::size(sdLogBuffer), "SD log buffer overflow"); - buffer.write(sdLogBuffer, length); - } - - binaryLogCount++; -#else - (void)buffer; -#endif /* EFI_FILE_LOGGING */ -} - static int prevCkpEventCounter = -1; /** diff --git a/firmware/hw_layer/mmc_card.cpp b/firmware/hw_layer/mmc_card.cpp index 6b8550af7a..032be918ce 100644 --- a/firmware/hw_layer/mmc_card.cpp +++ b/firmware/hw_layer/mmc_card.cpp @@ -16,14 +16,23 @@ #if EFI_FILE_LOGGING -#include "ch.hpp" +#include "buffered_writer.h" +#include "status_loop.h" + +static bool fs_ready = false; + +int totalLoggedBytes = 0; +static int fileCreatedCounter = 0; +static int writeCounter = 0; +static int totalWritesCounter = 0; +static int totalSyncCounter = 0; + +#if EFI_PROD_CODE + #include #include #include "mmc_card.h" #include "ff.h" -#include "hardware.h" -#include "status_loop.h" -#include "buffered_writer.h" #include "mass_storage_init.h" #include "rtc_helper.h" @@ -40,17 +49,10 @@ // todo: shall we migrate to enum with enum2string for consistency? maybe not until we start reading sdStatus? static const char *sdStatus = SD_STATE_INIT; -static bool fs_ready = false; // at about 20Hz we write about 2Kb per second, looks like we flush once every ~2 seconds #define F_SYNC_FREQUENCY 10 -int totalLoggedBytes = 0; -static int fileCreatedCounter = 0; -static int writeCounter = 0; -static int totalWritesCounter = 0; -static int totalSyncCounter = 0; - /** * on't re-read SD card spi device after boot - it could change mid transaction (TS thread could preempt), * which will cause disaster (usually multiple-unlock of the same mutex in UNLOCK_SD_SPI) @@ -66,8 +68,6 @@ spi_device_e mmcSpiDevice = SPI_NONE; #define LS_RESPONSE "ls_result" #define FILE_LIST_MAX_COUNT 20 -static THD_WORKING_AREA(mmcThreadStack, 3 * UTILITY_THREAD_STACK_SIZE); // MMC monitor thread - #if HAL_USE_MMC_SPI /** * MMC driver instance. @@ -468,8 +468,41 @@ struct SdLogBufferWriter final : public BufferedWriter<512> { } }; +#else // not EFI_PROD_CODE (simulator) + +#include + +bool mountMmc() { + // Stub so the loop thinks the MMC mounted OK + return true; +} + +class SdLogBufferWriter final : public BufferedWriter<512> { +public: + bool failed = false; + + SdLogBufferWriter() + : m_stream("rusefi_simulator_log.mlg", std::ios::binary | std::ios::trunc) + { + fs_ready = true; + } + + size_t writeInternal(const char* buffer, size_t count) override { + m_stream.write(buffer, count); + m_stream.flush(); + return count; + } + +private: + std::ofstream m_stream; +}; + +#endif // EFI_PROD_CODE + static NO_CACHE SdLogBufferWriter logBuffer; + +static THD_WORKING_AREA(mmcThreadStack, 3 * UTILITY_THREAD_STACK_SIZE); // MMC monitor thread static THD_FUNCTION(MMCmonThread, arg) { (void)arg; chRegSetThreadName("MMC Card Logger"); @@ -485,9 +518,11 @@ static THD_FUNCTION(MMCmonThread, arg) { while (true) { // if the SPI device got un-picked somehow, cancel SD card +#if EFI_PROD_CODE if (engineConfiguration->sdCardSpiDevice == SPI_NONE) { return; } +#endif if (engineConfiguration->debugMode == DBG_SD_CARD) { tsOutputChannels.debugIntField1 = totalLoggedBytes; @@ -516,12 +551,14 @@ bool isSdCardAlive(void) { // Pre-config load init void initEarlyMmcCard() { +#if EFI_PROD_CODE logName[0] = 0; addConsoleAction("sdinfo", sdStatistics); addConsoleActionS("ls", listDirectory); addConsoleActionS("del", removeFile); addConsoleAction("incfilename", incLogFileName); +#endif // EFI_PROD_CODE } void initMmcCard() { diff --git a/simulator/simulator/efifeatures.h b/simulator/simulator/efifeatures.h index b1870097c9..8187555960 100644 --- a/simulator/simulator/efifeatures.h +++ b/simulator/simulator/efifeatures.h @@ -134,7 +134,7 @@ #define DEBUG_PWM FALSE #define EFI_SIGNAL_EXECUTOR_ONE_TIMER FALSE #define EFI_TUNER_STUDIO_VERBOSE FALSE -#define EFI_FILE_LOGGING FALSE +#define EFI_FILE_LOGGING TRUE #define EFI_WARNING_LED FALSE #define EFI_VEHICLE_SPEED FALSE diff --git a/simulator/simulator/rusEfiFunctionalTest.cpp b/simulator/simulator/rusEfiFunctionalTest.cpp index 3d5ceabef5..1b9ab2483a 100644 --- a/simulator/simulator/rusEfiFunctionalTest.cpp +++ b/simulator/simulator/rusEfiFunctionalTest.cpp @@ -30,7 +30,7 @@ extern WaveChart waveChart; -int getRemainingStack(thread_t *otp) { +int getRemainingStack(thread_t*) { return 99999; } @@ -103,6 +103,9 @@ void rusEfiFunctionalTest(void) { startLoggingProcessor(); + void initMmcCard(); + initMmcCard(); + runChprintfTest(); initPeriodicEvents(); @@ -113,6 +116,9 @@ void rusEfiFunctionalTest(void) { startSerialChannels(); startLua(); + + extern bool main_loop_started; + main_loop_started = true; } void printPendingMessages(void) {