From 19a9b3089b8e738e5e1864ee8f53e6a5353698ac Mon Sep 17 00:00:00 2001 From: rusEfi Date: Wed, 14 Jan 2015 17:04:00 -0600 Subject: [PATCH] auto-sync --- firmware/console/console_io.c | 2 +- firmware/controllers/engine_controller.cpp | 4 +- firmware/controllers/engine_controller.h | 2 +- firmware/controllers/sensors/thermistors.cpp | 2 +- firmware/development/rfi_perftest.cpp | 53 ++++++++++---------- firmware/development/rfi_perftest.h | 13 +---- firmware/development/wave_analyzer.cpp | 7 ++- firmware/development/wave_analyzer.h | 2 +- firmware/rusefi.cpp | 15 +++--- firmware/util/datalogging.cpp | 35 ++++++------- firmware/util/listener_array.cpp | 2 +- 11 files changed, 61 insertions(+), 76 deletions(-) diff --git a/firmware/console/console_io.c b/firmware/console/console_io.c index d195484c1d..8ef9ec3fd6 100644 --- a/firmware/console/console_io.c +++ b/firmware/console/console_io.c @@ -231,7 +231,7 @@ void startConsole(void (*console_line_callback_p)(char *)) { bool lockAnyContext(void) { int alreadyLocked = isLocked(); if (alreadyLocked) - return TRUE; + return true; if (isIsrContext()) { chSysLockFromIsr() ; diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index a075ee1b2e..1aa191ca1d 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -308,7 +308,7 @@ static void setFloat(const char *offsetStr, const char *valueStr) { scheduleMsg(&logger, "setting float @%d to %f", offset, value); } -void initEngineContoller(Engine *engine) { +void initEngineContoller(Logging *sharedLogger, Engine *engine) { if (hasFirmwareError()) { return; } @@ -326,7 +326,7 @@ void initEngineContoller(Engine *engine) { #if EFI_WAVE_ANALYZER if (engineConfiguration->isWaveAnalyzerEnabled) { - initWaveAnalyzer(); + initWaveAnalyzer(sharedLogger); } #endif /* EFI_WAVE_ANALYZER */ diff --git a/firmware/controllers/engine_controller.h b/firmware/controllers/engine_controller.h index 7425c80a11..ec1aae25f3 100644 --- a/firmware/controllers/engine_controller.h +++ b/firmware/controllers/engine_controller.h @@ -15,6 +15,6 @@ #include "engine.h" char * getPinNameByAdcChannel(adc_channel_e hwChannel, char *buffer); -void initEngineContoller(Engine *engine); +void initEngineContoller(Logging *sharedLogger, Engine *engine); #endif /* ENGINE_STATUS_H_ */ diff --git a/firmware/controllers/sensors/thermistors.cpp b/firmware/controllers/sensors/thermistors.cpp index 7b020d085f..c07c132a6e 100644 --- a/firmware/controllers/sensors/thermistors.cpp +++ b/firmware/controllers/sensors/thermistors.cpp @@ -189,5 +189,5 @@ void initThermistors(Engine *engine) { engine->engineConfiguration->cltAdcChannel); initThermistorCurve(&engine->iat, &engine->engineConfiguration->iatThermistorConf, engine->engineConfiguration->iatAdcChannel); - initialized = TRUE; + initialized = true; } diff --git a/firmware/development/rfi_perftest.cpp b/firmware/development/rfi_perftest.cpp index 53e002cf42..6ee7c939fc 100644 --- a/firmware/development/rfi_perftest.cpp +++ b/firmware/development/rfi_perftest.cpp @@ -20,7 +20,7 @@ #if EFI_PERF_METRICS || defined(__DOXYGEN__) -static LoggingWithStorage logger; +static Logging* logger; static void testSystemCalls(const int count) { time_t start, time; @@ -42,7 +42,7 @@ static void testSystemCalls(const int count) { time = currentTimeMillis() - start; if (result != 0) { // Finished 100000 iterations of 'chTimeNow()' in 33ms - scheduleMsg(&logger, "Finished %d iterations of 'chTimeNow()' in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of 'chTimeNow()' in %dms", count, time); } start = currentTimeMillis(); @@ -56,7 +56,7 @@ static void testSystemCalls(const int count) { time = currentTimeMillis() - start; if (result != 0) { // Finished 100000 iterations of 'chTimeNow()' with chSysLock in 144ms - scheduleMsg(&logger, "Finished %d iterations of 'chTimeNow()' with chSysLock in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of 'chTimeNow()' with chSysLock in %dms", count, time); } start = currentTimeMillis(); @@ -64,7 +64,7 @@ static void testSystemCalls(const int count) { result += currentTimeMillis(); time = currentTimeMillis() - start; if (result != 0) - scheduleMsg(&logger, "Finished %d iterations of 'currentTimeMillis' in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of 'currentTimeMillis' in %dms", count, time); } static Engine testEngine; @@ -79,14 +79,14 @@ static void testRusefiMethods(const int count) { tempi += getBaseTableFuel(testEngine.engineConfiguration, 4020, 2.21111); time = currentTimeMillis() - start; if (tempi != 0) - scheduleMsg(&logger, "Finished %d iterations of getBaseFuel in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of getBaseFuel in %dms", count, time); // start = currentTimeMillis(); // for (int i = 0; i < count; i++) // tempi += getFuelMs(1200, NULL); // todo // time = currentTimeMillis() - start; // if (tempi != 0) -// scheduleMsg(&logger, "Finished %d iterations of getFuelMs in %dms", count, time); +// scheduleMsg(logger, "Finished %d iterations of getFuelMs in %dms", count, time); start = currentTimeMillis(); for (int i = 0; i < count; i++) { @@ -95,7 +95,7 @@ static void testRusefiMethods(const int count) { } time = currentTimeMillis() - start; if (tempi != 0) - scheduleMsg(&logger, "Finished %d iterations of updateSlowSensors in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of updateSlowSensors in %dms", count, time); } static void testMath(const int count) { @@ -108,7 +108,7 @@ static void testMath(const int count) { } time = currentTimeMillis() - start; if (temp64 != 0) { - scheduleMsg(&logger, "Finished %d iterations of int64_t summation in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of int64_t summation in %dms", count, time); } temp64 = 1; @@ -118,14 +118,14 @@ static void testMath(const int count) { } time = currentTimeMillis() - start; if (temp64 == 0) { - scheduleMsg(&logger, "Finished %d iterations of int64_t multiplication in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of int64_t multiplication in %dms", count, time); } start = currentTimeMillis(); for (int i = 0; i < count; i++) ; time = currentTimeMillis() - start; - scheduleMsg(&logger, "Finished %d iterations of empty loop in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of empty loop in %dms", count, time); uint32_t tempi = 1; start = currentTimeMillis(); @@ -135,7 +135,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempi == 0) { // Finished 100000 iterations of uint32_t summation in 11ms - scheduleMsg(&logger, "Finished %d iterations of uint32_t summation in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of uint32_t summation in %dms", count, time); } start = currentTimeMillis(); @@ -146,7 +146,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempi != 0) { // Finished 100000 iterations of uint32_t division in 16ms - scheduleMsg(&logger, "Finished %d iterations of uint32_t division in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of uint32_t division in %dms", count, time); } start = currentTimeMillis(); @@ -157,7 +157,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (temp64 == 0) { // Finished 100000 iterations of int64_t summation in 21ms - scheduleMsg(&logger, "Finished %d iterations of int64_t summation in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of int64_t summation in %dms", count, time); } start = currentTimeMillis(); @@ -168,7 +168,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (temp64 != 0) { // Finished 100000 iterations of int64_t division in 181ms - scheduleMsg(&logger, "Finished %d iterations of int64_t division in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of int64_t division in %dms", count, time); } start = currentTimeMillis(); @@ -178,7 +178,7 @@ static void testMath(const int count) { } time = currentTimeMillis() - start; if (tempf != 0) { - scheduleMsg(&logger, "Finished %d iterations of float summation in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of float summation in %dms", count, time); } start = currentTimeMillis(); @@ -189,7 +189,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempf != 0) { // Finished 100000 iterations of float division in 65ms - scheduleMsg(&logger, "Finished %d iterations of float division in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of float division in %dms", count, time); } start = currentTimeMillis(); @@ -200,7 +200,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempf != 0) { // Finished 100000 iterations of float log in 191ms - scheduleMsg(&logger, "Finished %d iterations of float log in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of float log in %dms", count, time); } start = currentTimeMillis(); @@ -210,7 +210,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempd != 0) { // Finished 100000 iterations of double summation in 80ms - scheduleMsg(&logger, "Finished %d iterations of double summation in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of double summation in %dms", count, time); } start = currentTimeMillis(); @@ -220,7 +220,7 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempd != 0) { // Finished 100000 iterations of double division in 497ms - scheduleMsg(&logger, "Finished %d iterations of double division in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of double division in %dms", count, time); } start = currentTimeMillis(); @@ -231,12 +231,12 @@ static void testMath(const int count) { time = currentTimeMillis() - start; if (tempd != 0) { // Finished 100000 iterations of double log in 242ms - scheduleMsg(&logger, "Finished %d iterations of double log in %dms", count, time); + scheduleMsg(logger, "Finished %d iterations of double log in %dms", count, time); } } static void runTests(const int count) { - scheduleMsg(&logger, "Running tests: %d", count); + scheduleMsg(logger, "Running tests: %d", count); testRusefiMethods(count / 10); testSystemCalls(count); testMath(count); @@ -251,12 +251,12 @@ static int rtcStartTime; #include "chrtclib.h" static void timeInfo(void) { - scheduleMsg(&logger, "chTimeNow as seconds = %d", getTimeNowSeconds()); - scheduleMsg(&logger, "hal seconds = %d", halTime.get() / 168000000LL); + scheduleMsg(logger, "chTimeNow as seconds = %d", getTimeNowSeconds()); + scheduleMsg(logger, "hal seconds = %d", halTime.get() / 168000000LL); #if EFI_RTC int unix = rtcGetTimeUnixSec(&RTCD1) - rtcStartTime; - scheduleMsg(&logger, "unix seconds = %d", unix); + scheduleMsg(logger, "unix seconds = %d", unix); #endif } @@ -280,13 +280,14 @@ static void runChibioTest(void) { TestThread(getConsoleChannel()); } -void initTimePerfActions() { +void initTimePerfActions(Logging *sharedLogger) { + logger = sharedLogger; #if EFI_RTC rtcStartTime = rtcGetTimeUnixSec(&RTCD1); #endif - initLogging(&logger, "perftest"); + // initOutputPin("test pad", &testOutput, TEST_PORT, TEST_PIN); addConsoleActionI("perftest", runTests); diff --git a/firmware/development/rfi_perftest.h b/firmware/development/rfi_perftest.h index 9716e3a7eb..365c662771 100644 --- a/firmware/development/rfi_perftest.h +++ b/firmware/development/rfi_perftest.h @@ -8,17 +8,8 @@ #ifndef RFI_PERFTEST_H_ #define RFI_PERFTEST_H_ -#ifdef __cplusplus -extern "C" -{ -#endif /* __cplusplus */ +#include "global.h" -//void testMath(int count); -//void testSystemCalls(int count); -void initTimePerfActions(void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ +void initTimePerfActions(Logging *sharedLogger); #endif /* RFI_PERFTEST_H_ */ diff --git a/firmware/development/wave_analyzer.cpp b/firmware/development/wave_analyzer.cpp index 64331a9ec1..648ab483d8 100644 --- a/firmware/development/wave_analyzer.cpp +++ b/firmware/development/wave_analyzer.cpp @@ -45,7 +45,7 @@ static int waveReaderCount = 0; static WaveReader readers[MAX_ICU_COUNT]; static THD_WORKING_AREA(waThreadStack, UTILITY_THREAD_STACK_SIZE); -static LoggingWithStorage logger; +static Logging * logger; static void ensureInitialized(WaveReader *reader) { efiAssertVoid(reader->hw.started, "wave analyzer NOT INITIALIZED"); @@ -238,10 +238,9 @@ void printWave(Logging *logging) { reportWave(logging, 1); } -void initWaveAnalyzer(void) { +void initWaveAnalyzer(Logging *sharedLogger) { + logger = sharedLogger; #if EFI_WAVE_ANALYZER || defined(__DOXYGEN__) - initLogging(&logger, "wave"); - initWave(WA_CHANNEL_1, 0); initWave(WA_CHANNEL_2, 1); diff --git a/firmware/development/wave_analyzer.h b/firmware/development/wave_analyzer.h index f93d63a4a4..f987b1f663 100644 --- a/firmware/development/wave_analyzer.h +++ b/firmware/development/wave_analyzer.h @@ -57,7 +57,7 @@ public: volatile uint64_t last_wave_high_widthUs; // time period in systimer ticks }; -void initWaveAnalyzer(void); +void initWaveAnalyzer(Logging *sharedLogger); void printWave(Logging *logging); #endif diff --git a/firmware/rusefi.cpp b/firmware/rusefi.cpp index 0a0330637f..3c5f13d254 100644 --- a/firmware/rusefi.cpp +++ b/firmware/rusefi.cpp @@ -101,11 +101,8 @@ #include "efiGpio.h" #include "global.h" -extern "C" { - #include "rfi_perftest.h" #include "rusefi.h" -} #include "memstreams.h" #include "eficonsole.h" @@ -120,7 +117,7 @@ extern "C" { #include "engine_emulator.h" #endif /* EFI_ENGINE_EMULATOR */ -static LoggingWithStorage logging; +static LoggingWithStorage sharedLogger; int main_loop_started = FALSE; @@ -155,7 +152,7 @@ void runRusEfi(void) { * Next we should initialize serial port console, it's important to know what's going on */ initializeConsole(); - initLogging(&logging, "main"); + initLogging(&sharedLogger, "main"); engine->init(); @@ -164,17 +161,17 @@ void runRusEfi(void) { /** * Initialize hardware drivers */ - initHardware(&logging, engine); + initHardware(&sharedLogger, engine); initStatusLoop(engine); /** * Now let's initialize actual engine control logic * todo: should we initialize some? most? controllers before hardware? */ - initEngineContoller(engine); + initEngineContoller(&sharedLogger, engine); #if EFI_PERF_METRICS || defined(__DOXYGEN__) - initTimePerfActions(); + initTimePerfActions(&sharedLogger); #endif #if EFI_ENGINE_EMULATOR || defined(__DOXYGEN__) @@ -211,7 +208,7 @@ static void rebootNow(void) { * Once day we will write graceful shutdown, but that would be one day. */ void scheduleReset(void) { - scheduleMsg(&logging, "Rebooting in 5 seconds..."); + scheduleMsg(&sharedLogger, "Rebooting in 5 seconds..."); lockAnyContext(); chVTSetI(&resetTimer, 5 * CH_FREQUENCY, (vtfunc_t) rebootNow, NULL); unlockAnyContext(); diff --git a/firmware/util/datalogging.cpp b/firmware/util/datalogging.cpp index 8391fa4ee0..21b77b1edc 100644 --- a/firmware/util/datalogging.cpp +++ b/firmware/util/datalogging.cpp @@ -138,30 +138,19 @@ static void vappendPrintfI(Logging *logging, const char *fmt, va_list arg) { append(logging, (char *) intermediateLoggingBufferData); } +/** + * this method acquires system lock to guard the shared intermediateLoggingBuffer memory stream + */ void vappendPrintf(Logging *logging, const char *fmt, va_list arg) { efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#5b"); if (!intermediateLoggingBufferInited) { firmwareError("intermediateLoggingBufferInited not inited!"); return; } - int is_locked = isLocked(); - int icsr_vectactive = isIsrContext(); - if (is_locked) { - vappendPrintfI(logging, fmt, arg); - } else { - if (icsr_vectactive == 0) { - chSysLock() - ; - vappendPrintfI(logging, fmt, arg); - chSysUnlock() - ; - } else { - chSysLockFromIsr() - ; - vappendPrintfI(logging, fmt, arg); - chSysUnlockFromIsr() - ; - } + int wasLocked = lockAnyContext(); + vappendPrintfI(logging, fmt, arg); + if (wasLocked) { + unlockAnyContext(); } } @@ -274,7 +263,7 @@ void printLine(Logging *logging) { } void appendMsgPrefix(Logging *logging) { - appendPrintf(logging, "msg%s", DELIMETER); + append(logging, "msg" DELIMETER); } void appendMsgPostfix(Logging *logging) { @@ -309,7 +298,12 @@ void printMsg(Logging *logger, const char *fmt, ...) { printLine(logger); } +/** + * this whole method is executed under syslock so that we can have multiple threads use the same shared buffer + * in order to reduce memory usage + */ void scheduleMsg(Logging *logging, const char *fmt, ...) { + int wasLocked = lockAnyContext(); resetLogging(logging); // todo: is 'reset' really needed here? appendMsgPrefix(logging); @@ -320,6 +314,9 @@ void scheduleMsg(Logging *logging, const char *fmt, ...) { appendMsgPostfix(logging); scheduleLogging(logging); + if (wasLocked) { + unlockAnyContext(); + } } void scheduleLogging(Logging *logging) { diff --git a/firmware/util/listener_array.cpp b/firmware/util/listener_array.cpp index 6f10b9fc2b..ab9e182d39 100644 --- a/firmware/util/listener_array.cpp +++ b/firmware/util/listener_array.cpp @@ -16,7 +16,7 @@ void IntListenerArray::registerCallback(IntListener handler, void *arg) { } void IntListenerArray::registerCallback(Void listener) { - + registerCallback((IntListener)listener, NULL); } void invokeCallbacks(IntListenerArray *array, int value) {