0 CPU time problem
Hi All,
I am trying to measure the CPU time consumed by a function foo(), and here is how I do it:
void foo(){
struct timespec tp1, tp2; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
//Computation goes here
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp2);
//The CPU time is given between the difference between tp2 and tp 1
}
The program is running on Ubuntu 10.10, and the kernel is 2.6.37 patched with Xenomai 2.5.6
Here is what I observed:
1) If foo() is running in the Linux domain (i.e., called by a regular Linux process), the CPU time obtained by the above method looks normal, and the mean CPU time of 25000 invocations of foo() is 10 ms.
2) If foo() is running in the primary domain (i.e., called by a Xenomai task spawned by rt_task_create() and rt_task_start()), then *most of the time*, the CPU time also looks as normal as the result in 1), and the mean CPU time is also about 10 ms. However, I also noticed that sometimes the CPU time obtained by the above method is 0. This zero value did not appear often, and out of 25000 invocations of foo(), I noticed that there is only 1 invocation of foo() where the CPU time is 0. BTW, the Xenomai task's priority is 99.
What causes this 0 CPU time in the primary domain? Is there something wrong with my method to measure the CPU time?
Thanks,
Tom
Quoted 37 lines · Tom Z
Hi All,
I am trying to measure the CPU time consumed by a function
foo(), and here is how I do it:void foo(){ struct timespec tp1, tp2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);//Computation goes here
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp2); //The CPU time is given between the difference between tp2 and tp 1}
The program is running on Ubuntu 10.10, and the kernel is 2.6.37 patched with Xenomai 2.5.6
Here is what I observed:
1) If
foo()is running in the Linux domain (i.e., called by a regular Linux process), the CPU time obtained by the above method looks normal, and the mean CPU time of 25000 invocations offoo()is 10 ms.2) If
foo()is running in the primary domain (i.e., called by a Xenomai task spawned byrt_task_create()andrt_task_start()), then *most of the time*, the CPU time also looks as normal as the result in 1), and the mean CPU time is also about 10 ms. However, I also noticed that sometimes the CPU time obtained by the above method is
0. This zero value did not appear often, and out of 25000 invocations offoo(), I noticed that there is only 1 invocation offoo()where the CPU time is 0. BTW, the Xenomai task's priority is 99.
Quoted 2 lines
What causes this 0 CPU time in the primary domain? Is there something wrong with my method to measure the CPU time?
We do not know what skin you use, we will assume that you use Xenomai posix skin, so, compile with the right flags.
CLOCK_PROCESS_CPUTIME_ID is not implemented for xenomai posix skin, so, clock_gettime probably returns an error, and the struct timespec is unchanged.
If somehow the glibc clock_gettime is used, and this call does not cause a system call, then all bets are off, and even a lockup could happen.
Signature · 1 line
Gilles.
"We do not know what skin you use, we will assume that you use Xenomaiposix skin, so, compile with the right flags."
Here is how I build my program, say, fooTest:
gcc -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__ -lnative -L/usr/xenomai/lib -lxenomai -lpthread -lnative -lrtdk fooTest.c -o fooTest
"CLOCK_PROCESS_CPUTIME_ID is not implemented for xenomai posix skin, so,clock_gettime probably returns an error, and the struct timespec is
unchanged.
If somehow the glibc clock_gettime is used, and this call does not cause
a system call, then all bets are off, and even a lockup could happen."
Thanks for explaining this. So can I trust the CPU time I obtained with my method when the function is running in the primary mode? It seems you explanation implies that the results I obtained are not reliable, if so, are there other reliable ways to obtain the CPU time?
With many thanks,
Tom
________________________________
From: Gilles Chanteperdrix <[email protected]>
To: Tom Z <[email protected]>
Cc: "[email protected]" <[email protected]>
Sent: Friday, 4 November 2011 2:21 PM
Subject: Re: [Xenomai-help] 0 CPU time problem
Quoted 37 lines · Tom Z
Hi All,
I am trying to measure the CPU time consumed by a function
foo(), and here is how I do it:void foo(){ struct timespec tp1, tp2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);//Computation goes here
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp2); //The CPU time is given between the difference between tp2 and tp 1}
The program is running on Ubuntu 10.10, and the kernel is 2.6.37 patched with Xenomai 2.5.6
Here is what I observed:
1) If
foo()is running in the Linux domain (i.e., called by a regular Linux process), the CPU time obtained by the above method looks normal, and the mean CPU time of 25000 invocations offoo()is 10 ms.2) If
foo()is running in the primary domain (i.e., called by a Xenomai task spawned byrt_task_create()andrt_task_start()), then *most of the time*, the CPU time also looks as normal as the result in 1), and the mean CPU time is also about 10 ms. However, I also noticed that sometimes the CPU time obtained by the above method is
0. This zero value did not appear often, and out of 25000 invocations offoo(), I noticed that there is only 1 invocation offoo()where the CPU time is 0. BTW, the Xenomai task's priority is 99.
Quoted 2 lines
What causes this 0 CPU time in the primary domain? Is there something wrong with my method to measure the CPU time?
We do not know what skin you use, we will assume that you use Xenomai posix skin, so, compile with the right flags.
CLOCK_PROCESS_CPUTIME_ID is not implemented for xenomai posix skin, so, clock_gettime probably returns an error, and the struct timespec is unchanged.
If somehow the glibc clock_gettime is used, and this call does not cause a system call, then all bets are off, and even a lockup could happen.
Signature · 1 line
Gilles.
Quoted 8 lines · Tom Z
Quoted 2 lines
We do not know what skin you use, we will assume that you use Xenomai posix skin, so, compile with the right flags.
Here is how I
build my program, say, fooTest: gcc -I/usr/xenomai/include(quoted diff) +0
-2-D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__ -lnative -L/usr/xenomai/lib -lxenomai -lpthread -lnative -lrtdk fooTest.c -ofooTest
You are using the native skin then.
Quoted 11 lines
Quoted 5 lines
CLOCK_PROCESS_CPUTIME_IDis not implemented for xenomai posix skin, so,clock_gettimeprobably returns an error, and the struct timespec is unchanged. If somehow the glibcclock_gettimeis used, and this call does not cause a system call, then all bets are off, and even a lockup could happen.Thanks for explaining this. So can I trust the
CPU time I obtained with my method when the function is running in the primary mode? It seems you explanation implies that the results I obtained are not reliable, if so, are there other reliable ways to obtain the CPU time?
No, CLOCK_PROCESS_CPUTIME is a clock implemented by the Linux kernel, so, it does not see the time spent in primary mode. If the foo function does not suspend, you can use rt_timer_tsc(), but the answer is no, there is currently no way to get the cpu time used when in primary mode.
Signature · 1 line
Gilles.
Hi Gilles,
"No, CLOCK_PROCESS_CPUTIME is a clock implemented by the Linux kernel,
so, it does not see the time spent in primary mode. If the foo function
does not suspend, you can use rt_timer_tsc(), but the answer is no,
there is currently no way to get the cpu time used when in primary mode."
Thanks a lot for the explanation. It seems my understanding on how Xenomai works was incorrect. I thought that if a task, say, myRTTask() is spawned by rt_task_create() and rt_task_start(), then myRTTask() will always run in the primary mode, so if myRTTask() calls foo(), then foo() will be running in the primary mode all the time. The correct fact is, myRTTask() could be running in either the primary mode or the secondary mode, right? This arises my another question, if foo() uses some third party libraries (with source code available), how can I tell what parts of foo() will be running in the primary mode, and what parts will be in the secondary mode? Are there principles for deciding whether the code is in the primary mode or the secondary mode?
Thanks again for your patient help.
Tom
________________________________
From: Gilles Chanteperdrix <[email protected]>
To: Tom Z <[email protected]>
Cc: "[email protected]" <[email protected]>
Sent: Friday, 4 November 2011 4:36 PM
Subject: Re: [Xenomai-help] 0 CPU time problem
Quoted 8 lines · Tom Z
Quoted 2 lines
We do not know what skin you use, we will assume that you use Xenomai posix skin, so, compile with the right flags.
Here is how I
build my program, say, fooTest: gcc -I/usr/xenomai/include(quoted diff) +0
-2-D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__ -lnative -L/usr/xenomai/lib -lxenomai -lpthread -lnative -lrtdk fooTest.c -ofooTest
You are using the native skin then.
Quoted 11 lines
Quoted 5 lines
CLOCK_PROCESS_CPUTIME_IDis not implemented for xenomai posix skin, so,clock_gettimeprobably returns an error, and the struct timespec is unchanged. If somehow the glibcclock_gettimeis used, and this call does not cause a system call, then all bets are off, and even a lockup could happen.Thanks for explaining this. So can I trust the
CPU time I obtained with my method when the function is running in the primary mode? It seems you explanation implies that the results I obtained are not reliable, if so, are there other reliable ways to obtain the CPU time?
No, CLOCK_PROCESS_CPUTIME is a clock implemented by the Linux kernel, so, it does not see the time spent in primary mode. If the foo function does not suspend, you can use rt_timer_tsc(), but the answer is no, there is currently no way to get the cpu time used when in primary mode.
Signature · 1 line
Gilles.
Quoted 8 lines · Tom Z
Hi Gilles,
Quoted 5 lines
No,
CLOCK_PROCESS_CPUTIMEis a clock implemented by the Linux kernel, so, it does not see the time spent in primary mode. If the foo function does not suspend, you can usert_timer_tsc(), but the answer is no, there is currently no way to get the cpu time used when in primary mode.
Quoted 12 lines
Thanks a lot for the explanation. It seems my understanding on how Xenomai works was incorrect. I thought that if a task, say,
myRTTask()is spawned byrt_task_create()andrt_task_start(), thenmyRTTask()will always run in the primary mode, so ifmyRTTask()callsfoo(), thenfoo()will be running in the primary mode all the time. The correct fact is,myRTTask()could be running in either the primary mode or the secondary mode, right? This arises my another question, iffoo()uses some third party libraries (with source code available), how can I tell what parts offoo()will be running in the primary mode, and what parts will be in the secondary mode? Are there principles for deciding whether the code is in the primary mode or the secondary mode?
Hi, it would be nice if you could quote and wrap mails as is usual on public mailing lists.
Every Linux system call causes a switch to secondary mode, as well as every call to a xenomai system call documented as such. For instance, if you look at rt_queue_create documentation, you see:
Environments:
This service can be called from:
* Kernel module initialization/cleanup code
* User-space task (switches to secondary mode)
If you want to detect unwanted mode switches to secondary mode, you can use rt_task_set_mode with the T_WARNSW bit, as is demonstrated in the example:examples/native/sigdebug.c
in xenomai sources tree.
Signature · 1 line
Gilles.
Quoted 24 lines · Gilles Chanteperdrix
Quoted 8 lines · Tom Z
Quoted 2 lines
We do not know what skin you use, we will assume that you use Xenomai posix skin, so, compile with the right flags.
Here is how I
build my program, say, fooTest: gcc -I/usr/xenomai/include(quoted diff) +0
-2-D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__ -lnative -L/usr/xenomai/lib -lxenomai -lpthread -lnative -lrtdk fooTest.c -ofooTest
You are using the native skin then.
Quoted 11 lines
Quoted 5 lines
CLOCK_PROCESS_CPUTIME_IDis not implemented for xenomai posix skin, so,clock_gettimeprobably returns an error, and the struct timespec is unchanged. If somehow the glibcclock_gettimeis used, and this call does not cause a system call, then all bets are off, and even a lockup could happen.Thanks for explaining this. So can I trust the
CPU time I obtained with my method when the function is running in the primary mode? It seems you explanation implies that the results I obtained are not reliable, if so, are there other reliable ways to obtain the CPU time?
Quoted 5 lines
No,
CLOCK_PROCESS_CPUTIMEis a clock implemented by the Linux kernel, so, it does not see the time spent in primary mode. If the foo function does not suspend, you can usert_timer_tsc(), but the answer is no, there is currently no way to get the cpu time used when in primary mode.
There is one, but deeply buried in the API basement, via rt_task_inquire():
RT_TASK_INFO info;
RTIME start;
ret = rt_task_inquire(NULL, &info); /* NULL means current task */ if (ret)
goto Houston_handles_the_problem;
start = info.exectime;
... do work ....
ret = rt_task_inquire(NULL, &info);
delta = info.exectime - start; /* elapsed CPU time in ns */
rt_task_inquire() will beget a syscall each time invoked though, so it does have some overhead. You need CONFIG_XENO_OPT_STATS enabled in your Kconfig for this feature (defaults to on).
Signature · 1 line
Philippe.