Tuesday, May 25, 2010

How to Measure the Duration of a RTAI Semaphore Operation

Today I was asked the question "How long does it take to signal a semaphore?".

This is important as I had to do it in an ISR [which serviced a quasi-timer IRQ] to signal an RT task to start doing a job (this is akin to Linux tasklets).

Here is how I measured it (interrupts are disable to keep the measurement accurate):

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/types.h>

#include <rtai_sem.h>
#include <rtai_sched.h>

SEM pollSem;

static int __init testsem_init_module(void)
{
rt_typed_sem_init(&pollSem, 0, BIN_SEM | FIFO_Q);

rt_global_cli();

volatile unsigned long long ticks_start;
__asm__("rdtsc\n\t"
"mov %%edx, %%ecx\n\t"
:"=A" (ticks_start));

rt_sem_signal(&pollSem);

volatile unsigned long long ticks_end;
__asm__("rdtsc\n\t"
"mov %%edx, %%ecx\n\t"
:"=A" (ticks_end));

rt_global_sti();

long long dT = ticks_end - ticks_start;

rt_sem_delete(&pollSem);

printk(KERN_DEBUG "rt_sem_signal took %lld ticks\n", dT);

return -EBUSY;
}

module_init(testsem_init_module);
MODULE_LICENSE("GPL");
The answer was (on a PIII/1.2GHz) about 300 cpu ticks which is ~0.25 uS.

-ulianov