Page 2 of 6

Re: Threads are dying, I cannot find the issue :(

Posted: Fri Feb 27, 2015 6:56 pm
by russian
Ops, sorry - that's my helper wrapper over Chibi API:

Code: Select all

void chVTSetAny(virtual_timer_t *vtp, systime_t time, vtfunc_t vtfunc, void *par) {
   if (isIsrContext()) {
      bool wasLocked = lockAnyContext();

      /**
       * todo: this could be simplified once we migrate to ChibiOS 3.0
       * See http://www.chibios.org/dokuwiki/doku.php?id=chibios:howtos:porting_from_2_to_3
       */
      if (chVTIsArmedI(vtp))
         chVTResetI(vtp);

      chVTSetI(vtp, time, vtfunc, par);
      if (!wasLocked)
                    chSysUnlockFromIsr();
   } else {
      chSysLock();
      if (chVTIsArmedI(vtp))
         chVTResetI(vtp);
      chVTSetI(vtp, time, vtfunc, par);
      chSysUnlock();
   }

Re: Threads are dying, I cannot find the issue :(

Posted: Fri Feb 27, 2015 8:18 pm
by Giovanni
chSysLockFromIsr() is missing from that function.

Giovanni

Re: Threads are dying, I cannot find the issue :(

Posted: Fri Feb 27, 2015 8:23 pm
by russian
I think lockAnyContext takes care of that

Code: Select all

/**
 * @return TRUE if already in locked context
 */
bool lockAnyContext(void) {
   int alreadyLocked = isLocked();
   if (alreadyLocked)
      return true;
   if (isIsrContext()) {
      chSysLockFromIsr()
      ;
   } else {
      chSysLock()
      ;
   }
   return false;
}

Re: Threads are dying, I cannot find the issue :(

Posted: Fri Feb 27, 2015 8:26 pm
by Giovanni
I don't understand that code much, I am not sure if there is a problem there. Probably you are better off using the normal lock and unlock primitives.

Is the state checker active?

Giovanni

Re: Threads are dying, I cannot find the issue :(

Posted: Fri Feb 27, 2015 8:35 pm
by russian
russian wrote:Some details:

Code: Select all

__main_stack_size__     = 0x1000;
__process_stack_size__  = 0x0600;

#define PORT_IDLE_THREAD_STACK_SIZE     1024

#define PORT_INT_REQUIRED_STACK         32

#define CH_DBG_ENABLE_STACK_CHECK       TRUE
#define CH_DBG_ENABLE_CHECKS            TRUE
#define CH_DBG_ENABLE_ASSERTS           TRUE
#define CH_DBG_SYSTEM_STATE_CHECK       TRUE


Yes, all the state checks are enabled:

Code: Select all

#define CH_DBG_SYSTEM_STATE_CHECK       TRUE
#define CH_DBG_ENABLE_CHECKS            TRUE
#define CH_DBG_ENABLE_ASSERTS           TRUE
#define CH_DBG_ENABLE_STACK_CHECK       TRUE
#define CH_DBG_ENABLE_TRACE             TRUE


Re: Threads are dying, I cannot find the issue :(

Posted: Fri Feb 27, 2015 9:05 pm
by russian
Hopefully my helper method is more readable now:

Code: Select all

void chVTSetAny(virtual_timer_t *vtp, systime_t time, vtfunc_t vtfunc, void *par) {
   bool wasLocked = lockAnyContext();

   /**
    * todo: this could be simplified once we migrate to ChibiOS 3.0
    * See http://www.chibios.org/dokuwiki/doku.php?id=chibios:howtos:porting_from_2_to_3
    */
   if (chVTIsArmedI(vtp)) {
      chVTResetI(vtp);
   }

   chVTSetI(vtp, time, vtfunc, par);
   if (!wasLocked) {
      unlockAnyContext();
   }
}


To be honest the locking here is the thing I am least concerned, I am using the same

Code: Select all

   bool wasLocked = lockAnyContext();
...code to guard...
   if (!wasLocked) {
      unlockAnyContext();
   }
pattern a lot and in my understanding it's bulletproof

Re: Threads are dying, I cannot find the issue :(

Posted: Mon Mar 02, 2015 5:22 am
by russian
In order to get chibi state via the plugin I've installed ChibiStudio - and suddenly I was not able to reproduce the issue.

I know it might sound lame and wishful thinking, but it seems like using 4.9.3 AND fresh version of a makefile makes the issue either disappear or at least become much, much less often.

my older 4.8.3 with my makefile (based on older chibi makefile) fails after some iterations. my makefile with 4.9.3 fails after some iterations. it has to be 4.9.3 AND fresh chibi makefile

I wound understand if compiler would be creating faulty code, but how is it possible that compiler version or compiler options make a code which runs for 400 times and then fails? It's not like we have any JIT compilers here.

here's verbose of 'my' 'older' makefile:
arm-none-eabi-g++ -c -mcpu=cortex-m4 -std=c++11 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -Werror=write-strings -Werror=type-limits -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -Wall -Wextra -Wa,-alms=build/lst/main.lst -DST_STM32F4_DISCOVERY -DCORTEX_USE_FPU=TRUE -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/main.o.d -mthumb -DTHUMB -I. -Ichibios/os/ports/common/ARMCMx/CMSIS/include -Ichibios/os/ports/common/ARMCMx -Ichibios/os/ports/GCC/ARMCMx -Ichibios/os/ports/GCC/ARMCMx/STM32F4xx -Ichibios/os/kernel/include -Ichibios/os/hal/include -Ichibios/os/hal/platforms/STM32F4xx -Ichibios/os/hal/platforms/STM32 -Ichibios/os/hal/platforms/STM32/GPIOv2 -Ichibios/os/hal/platforms/STM32/I2Cv1 -Ichibios/os/hal/platforms/STM32/OTGv1 -Ichibios/os/hal/platforms/STM32/RTCv2 -Ichibios/os/hal/platforms/STM32/SPIv1 -Ichibios/os/hal/platforms/STM32/TIMv1 -Ichibios/os/hal/platforms/STM32/USARTv1 -Ichibios/boards/ST_STM32F4_DISCOVERY -Ichibios/os/various/cpp_wrappers -Ichibios/os/various -Iconfig/stm32f4ems -Iconfig/engines -Iconfig -Ichibios/os/various -Iext -Iext_algo -Iutil -Iconsole_util -Iconsole -Iconsole/tunerstudio -Ihw_layer -Ihw_layer/serial_over_usb -Ihw_layer/algo -Ihw_layer/lcd -Ihw_layer/stm32f4 -Idevelopment -Idevelopment/hw_layer -Idevelopment/test -Icontrollers -Icontrollers/sensors -Icontrollers/system -Icontrollers/algo -Icontrollers/core -Icontrollers/math -Icontrollers/trigger main.cpp -o build/obj/main.o


here's verbose of newer chibi makefile adopted for my project;
arm-none-eabi-g++ -c -mcpu=cortex-m4 -O2 -ggdb -std=gnu99 -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -fno-common -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -fsingle-precision-constant -std=c++11 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -Werror=write-strings -Werror=type-limits -Wall -Wextra -Wa,-alms=build/lst/main.lst -DCORTEX_USE_FPU=TRUE -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/main.o.d -mthumb -DTHUMB -I. -Ichibios/os/ports/common/ARMCMx/CMSIS/include -Ichibios/os/ports/common/ARMCMx -Ichibios/os/ports/GCC/ARMCMx -Ichibios/os/ports/GCC/ARMCMx/STM32F4xx -Ichibios/os/kernel/include -Ichibios/os/hal/include -Ichibios/os/hal/platforms/STM32F4xx -Ichibios/os/hal/platforms/STM32 -Ichibios/os/hal/platforms/STM32/GPIOv2 -Ichibios/os/hal/platforms/STM32/I2Cv1 -Ichibios/os/hal/platforms/STM32/OTGv1 -Ichibios/os/hal/platforms/STM32/RTCv2 -Ichibios/os/hal/platforms/STM32/SPIv1 -Ichibios/os/hal/platforms/STM32/TIMv1 -Ichibios/os/hal/platforms/STM32/USARTv1 -Ichibios/boards/ST_STM32F4_DISCOVERY -Ichibios/os/various -Iconfig/stm32f4ems -Iconfig/engines -Iconfig -Ichibios/os/various -Iext -Iext_algo -Iutil -Iconsole_util -Iconsole -Iconsole/tunerstudio -Ihw_layer -Ihw_layer/serial_over_usb -Ihw_layer/algo -Ihw_layer/lcd -Ihw_layer/stm32f4 -Idevelopment -Idevelopment/hw_layer -Idevelopment/test -Icontrollers -Icontrollers/sensors -Icontrollers/system -Icontrollers/algo -Icontrollers/core -Icontrollers/math -Icontrollers/trigger main.cpp -o build/obj/main.o


some of the differences between makefiles are
-fsingle-precision-constant
-falign-functions=16 -ffunction-sections -fdata-sections
-fno-common

I also have reports that 4.9.2 works with my 'older' style makefile. If the difference between 4.9.2 and 4.9.3 actually makes a difference it would be pretty upsetting.

I am logging the result of running same test with different makefiles and different compilers @ https://sourceforge.net/p/rusefi/tickets/155/

Re: Threads are dying, I cannot find the issue :(

Posted: Mon Mar 02, 2015 9:09 am
by Giovanni
It could be a compiler issue then.

About makefiles, you should always use those provided with the ChibiOS version you are using, same for all configuration files and scatter files.

Giovanni

Re: Threads are dying, I cannot find the issue :(

Posted: Mon Mar 02, 2015 1:23 pm
by russian
I've left it overnight to run a 12000 iteration test chibi config and newer GCC compiler embedded with chibistudio, that seems as my most reliable setup

it has failed in 4:30 hours at 3712th iteration, this makes me think the compiler is only affecting the chance of the issue occurring, but it is still there.

Re: Threads are dying, I cannot find the issue :(

Posted: Mon Mar 02, 2015 1:52 pm
by russian
IAR with the same exact source code has survived the night, it is now at iteration 7300ish.