Keeping track of the CPU time for a thread

Mohammad A Haque mhaque4 at
Tue Jun 26 20:52:44 CEST 2012

Thank you for your reply.

Just to test the functionality, I tried a very simple thread. It computes a factorial for some number of iterations.

void entry_point(void * param) {
    int num_run = 1000000;
    int number = 15;
    int fact;

    thread_t self = env()->self();
    for(int i=0;i<num_run;i++) {
      fact = 1;
      for(int j = 2; j<= number; j++) {
        fact = fact * j;
    printf("Factorial of %d is : %d\n", number, fact);
    printf("Consumed time: %llu\n",l4_utcb_mr()->mr[0]);

What I found is - the consumed time does not depend on the number of iterations (num_run) - I tried from 1 to a very large number. It does vary with the number. Am I missing something?

Thanks again.

Date: Sun, 24 Jun 2012 23:29:25 +0200
From: Adam Lackorzynski <adam at>
To: l4-hackers at
Subject: Re: Keeping track of the CPU time for a thread
Message-ID: <20120624212925.GC1246 at>
Content-Type: text/plain; charset=iso-8859-1


On Thu Jun 21, 2012 at 17:44:25 +0000, Mohammad A Haque wrote:
> I want to measure how much CPU time a thread has consumed. When the
> CPU time consumption exceeds a certain limit (specific to that
> thread), I want to raise an interrupt. Please suggest me how can I do
> it efficiently?

The L4::Thread interface has the stats_time function to get the consumed
time of a thread (FINE_GRAINED_CPUTIME in the kernel might be of
interest here). Triggering an interrupt goes with its trigger function.
Then it's just about plugging this together.

Adam                 adam at

More information about the l4-hackers mailing list