How to Measure Time to execute a function in Kernel space
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303
Rep:
How to Measure Time to execute a function in Kernel space
Hi,
I have written one kernel level program and
I want to measure the execution time of my kernel function inside this program. I am using linux kernel 2.6.
Can you suggest me some function to measure the start time and end time so that I can find the difference between both.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
You probably want to use the clock() function -- be sure to read the manual page and determine that the value of CLOCKS_PER_SEC for your system is appropriate for your use.
Here is an example program that shows usage:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void foo (void);
void main (void)
{
clock_t t0, t1;
t0 = clock ();
foo ();
t1 = clock ();
(void) fprintf (stdout, "foo took %f secs.\n",
(float) (t1 - t0) / CLOCKS_PER_SEC);
exit (EXIT_SUCCESS);
}
void foo (void)
{
long i;
for (i = 0; i < 10e6L; i++)
;
return;
}
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303
Original Poster
Rep:
I need to measure time in kernel space not in user space.
ANd more over to measure acurate time in user space, you should always use "struct timeval". Because clock() can't give accracy in msec.
The code in user space is as below.
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303
Original Poster
Rep:
For kernel space following code will work ....
#include <linux/time.h>
int init_module (void) /* Loads a module in the kernel */
{
struct timeval t0,t1;
long i=0;
float diff;
double temp;
do_gettimeofday(&t0);
//Your code goes here
while(i<1000)
{
//printk("%ld",i++);
}
I'm pretty sure that you aren't supposed to use floating point calculations in kernel code, as the kernel doesn't keep track of the FPU's state when the kernel is running code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.