RFC on a couple of new features
Posted: Wed Nov 07, 2012 3:36 am
I've found myself in need of a couple of OS features not currently available, and though I've been able to implement then thanks to the available hook macros, it feels a bit dirty and is not much portable. Since I think these are useful features, maybe it would be a good idea to make them part of the core code.
First one is simple: I use the CH_DBG_FILL_THREADS and will most likely even leave it enabled in production. I don't create and destroy threads frequently so the overhead is minimal and it helps a lot diagnosing stack problems. However, it is a pain in the ass to use the debugger to examine the working area of each thread, so set out to implement a shell command that would walk the stack area upwards looking for the fill value and telling how much of the stack is still intact.
Problem is the working area size is stored nowhere. Sure, I can just walk upwards from (uint8_t *)tp + sizeof(Thread) and surely I'll hit a non fill character sometime, but as most programmers, I like my loops bound.
I implemented this by adding an extra "void *p_tos" field to the Thread structure (using THREAD_EXT_FIELDS) and saving there the address of the next byte after the working area (using THREAD_EXT_INIT_HOOK). Since the working area size is not available to THREAD_EXT_INIT_HOOK, I had to resort to the following ugly hack:
Which is not only ugly but also totally non portable.
The second feature is an accurate thread cpu time counter using the freerunning CPU cycle counter available in the STM32F4 platform:
I had to add two fields to the thread structure using THREAD_EXT_FIELDS:
The first one is the total thread cycles, the second one is the cycle counter value when the thread was las scheduled for execution. I also need a global uint64_t value called ctime that will hold the total number of cycles (which I will need for CPU percentage calculation). That is because the freerunning CPU cycle counter is 32 bits wide and thus I need to make partial measurements and accumulate.
The in the THREAD_CONTEXT_SWITCH_HOOK (ntp is the new thread, otp is the old thread):
Works, but is ugly for a number of reasons, one of them being the explicit access to the STM32F4 cycle counter instead of using halGetCounterValue(), which I haven't been able to make available to that code without causing header havoc.
Suggestions and comments highly welcome. Thanks.
First one is simple: I use the CH_DBG_FILL_THREADS and will most likely even leave it enabled in production. I don't create and destroy threads frequently so the overhead is minimal and it helps a lot diagnosing stack problems. However, it is a pain in the ass to use the debugger to examine the working area of each thread, so set out to implement a shell command that would walk the stack area upwards looking for the fill value and telling how much of the stack is still intact.
Problem is the working area size is stored nowhere. Sure, I can just walk upwards from (uint8_t *)tp + sizeof(Thread) and surely I'll hit a non fill character sometime, but as most programmers, I like my loops bound.
I implemented this by adding an extra "void *p_tos" field to the Thread structure (using THREAD_EXT_FIELDS) and saving there the address of the next byte after the working area (using THREAD_EXT_INIT_HOOK). Since the working area size is not available to THREAD_EXT_INIT_HOOK, I had to resort to the following ugly hack:
Code: Select all
tp->p_tos = (uint8_t *)tp->p_ctx.r13 + sizeof(struct intctx);
Which is not only ugly but also totally non portable.
The second feature is an accurate thread cpu time counter using the freerunning CPU cycle counter available in the STM32F4 platform:
I had to add two fields to the thread structure using THREAD_EXT_FIELDS:
Code: Select all
uint64_t p_ctime;
uint32_t p_cref;
The first one is the total thread cycles, the second one is the cycle counter value when the thread was las scheduled for execution. I also need a global uint64_t value called ctime that will hold the total number of cycles (which I will need for CPU percentage calculation). That is because the freerunning CPU cycle counter is 32 bits wide and thus I need to make partial measurements and accumulate.
The in the THREAD_CONTEXT_SWITCH_HOOK (ntp is the new thread, otp is the old thread):
Code: Select all
ntp->p_cref = DWT_CYCCNT;
if (otp) {
uint32_t cnt = ntp->p_cref - otp->p_cref;
otp->p_ctime += cnt;
ctime += cnt;
}
Works, but is ugly for a number of reasons, one of them being the explicit access to the STM32F4 cycle counter instead of using halGetCounterValue(), which I haven't been able to make available to that code without causing header havoc.
Suggestions and comments highly welcome. Thanks.