LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 12-19-2007, 06:51 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Rep: Reputation: 32
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.

With thanks in advance,
 
Old 12-20-2007, 03:34 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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;
}
 
Old 12-24-2007, 03:31 AM   #3
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
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.


#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void abc();

int main()
{
struct timeval t0,t1;
int ret;
double diff;
ret=gettimeofday(&t0,NULL);
abc();
ret=gettimeofday(&t1,NULL);
diff=(t1.tv_sec - t0.tv_sec)*1000 + (t1.tv_usec - t0.tv_usec)/1000.0;
printf("%10.5f msec",diff);

}

void abc()
{
long i=0;
while(i<100000)
i++;
}
 
Old 12-24-2007, 06:14 AM   #4
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
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++);
}

do_gettimeofday(&t1);

temp = (double) (t1.tv_sec - t0.tv_sec)*1000 + (t1.tv_usec - t0.tv_usec)/1000;
return 0;
}
 
Old 01-07-2008, 11:48 AM   #5
mat_the_w
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
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.

When I just tried to use the code

temp = (double) (t1.tv_sec - t0.tv_sec)*1000 + (t1.tv_usec - t0.tv_usec)/1000;

in a device driver, it complied just fine, but the linker(?) complained that:


*** Warning: "__adddf3" [drivers/matthew/test1/test1.ko] undefined!
*** Warning: "__muldf3" [drivers/matthew/test1/test1.ko] undefined!
*** Warning: "__floatsidf" [drivers/matthew/test1/test1.ko] undefined!

--mat_the_w
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to measure execution time in kernel space? didio Linux - Kernel 1 10-08-2007 10:01 PM
How To Measure Disk Space carlosinfl Linux - General 6 08-17-2007 02:30 PM
LXer: Measure Time in Linux with Time Base LXer Syndicated Linux News 0 04-11-2007 06:46 AM
How to measure the time to build a package? mullog Linux From Scratch 10 05-11-2004 11:23 AM
accurately measure time rasselin Programming 1 09-05-2003 05:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration