Page 1 of 1

RFC on a couple of new features

Posted: Wed Nov 07, 2012 3:36 am
by iggarpe
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:

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.

Re: RFC on a couple of new features

Posted: Wed Nov 07, 2012 1:38 pm
by Giovanni
The problem with the stack limit is that the main() thread is created in a different way and it is not possible to know the physical end of the C-runtime stack from chSysInit() unless I add a mandatory dependency on an external symbol that, being that portable code, would have to be provided by all ports. This extra field could be associated to the stack checking option, in that case there is already such an external dependency for the lower limit.

The DWT counter is that not all architectures have that and even in the M3 it can be not present (it is a configurable part). This why the functionality ended up in the HAL, initially I wanted to add it to the Kernel.

Giovanni

Re: RFC on a couple of new features

Posted: Wed Nov 07, 2012 8:13 pm
by barthess
If we speaking about performance measurement how about adding interrupt counters in drivers?

Re: RFC on a couple of new features

Posted: Wed Nov 07, 2012 8:36 pm
by Giovanni
I think it is be a good idea, the statistics side is a bit lacking, I think the kernel needs a generic statistics module that could be queried by the debug plugin or, may be, just add this functionality to the registry.

Giovanni

Re: RFC on a couple of new features

Posted: Wed Nov 07, 2012 9:56 pm
by jscott
YES! I would vote for statistics module. I would like something that I can get to from a shell command to print more information about what the processor is doing.

Of course, it would be nice to have it optional so it can be removed for smaller / tighter builds...

Don't know enough about how thing go together yet to help on the coding side, but if there is anything else you need help with I would be happy to help!!

-John Scott
atl123.com

Re: RFC on a couple of new features

Posted: Fri Nov 09, 2012 12:35 am
by iggarpe
Yes, I noticed my hack doesn't work for the main thread, but just noticed it and excluded it from the listing of the stack usage. If you can come up with a portable method to know the main thread size, good, but if not, just making super clear that this information is not valid for just that specific thread may be a worthy fallback.

Regarding the DWT, in principle it totally makes sense to put it in the hal (where else?), however it makes it impossible to use it in a portable way from chconf.h.

Instead, I propose a configuration variable that:

a) Tells if a freerunning cycle counter is available.
b) Provides a procedure to read its value.

(a) can be used to conditionally add the required fields to Thread and the required code to the context switch code, which would make use of (b) to read the abstract counter value (let's say it needs to be at least 32 bit wide). This way, the high precision cycle counting would be available only in certain architectures, and (a) can also be used by the programmers to conditionally implement whatever code uses it.

Rgds.

Re: RFC on a couple of new features

Posted: Fri Nov 09, 2012 9:06 am
by Giovanni
A variables that tells if a counter is available is already exported by the HAL driver low level. The problem is that I don't want to make the kernel dependent on HAL, this is why it has to be included as a kernel feature (a port feature to be exact) if the kernel needs it.

It is feasible but not a small change.

Giovanni