The timing __linux in Linux

Source: Internet
Author: User
Tags knowledge base

This article describes some of the timing-related problems and solutions in the Linux system. Because in the course of study and research, we often need to count the time of program or program, evaluate their performance. So these problems are often encountered for us. Mastering multiple timing methods is a skill that must be mastered by developers or researchers. This article solves some of the technical problems that are often encountered in Linux timing and is believed to help others with their work.

In fact, there may be other ways to accomplish the tasks discussed in this article. I just talked about the methods I used, which doesn't mean that other methods are bad, but for me these methods are relatively simple and effective. time in Linux

Time plays a very important role in Linux systems, and it is almost ubiquitous. When powered on, the following information is displayed:

Last Login:tue Sep 22:12:50 from 192.168.6.100

When shutting down, we can use the shutdown command to specify when or how long the machine will be shut down at timed intervals. We might also be setting up a Linux time server to sync with a level or two time server on the Internet, and in short, we have to understand the time in the Linux system.

In fact, the Linux system has its own timer clock. You can experiment with the date and/sbin/clock (or Sbin/hwclock) commands separately, and the time is different.

[Grandiose@bygone grandiose]$ date
Sun Sep 21:11:02 EDT 2003
[Grandiose@bygone
grandiose]$ Sun SEP 2003 09:07:07 PM EDT  -0.466994 seconds

When you change the system time as root, remember to write the system time to the CMOS in Clock-w.

use the C language for timing

You can use the C language function Gettimeofday to get time in user space, and its invocation format is:

#include <sys/time.h> 
int gettimeofday (struct timeval *tv, struct timezone); 
int settimeofday (const struct Timeval *tv, const struct timezone);
	The structure timeval is defined as:
strut timeval {
long tv_sec;/* seconds/
long tv_usec;//* microsecond number *
/};

It can be seen that this way of timing, precision can reach microseconds, that is, 10-6 seconds. When timing, we need to call two times before and after the Gettimeofday, and then calculate the median difference:

Gettimeofday (&start, NULL);
Foo (); 
Gettimeofday (&end, NULL);
Timeuse = 1000000 * (end.tv_sec-start.tv_sec) + end.tv_usec-start.tv_usec; 
Timeuse/= 1000000;
Shell Timing

Under the Linux shell, we often use the shell's built-in time command and the GNU version of the timing command to test how long the program is running.

The built-in time provides a relatively small number of parameter options, while GNU provides rich parameter options, including the ability to specify output files.

[Grandiose@bygone grandiose]$/usr/bin/time--output=foo.txt foo

The previous sentence only has time information exported to the Foo.txt file, and if you want to include the results of Foo execution, you need to use the following sentence:

[Grandiose@bygone grandiose]$/usr/bin/time--output=foo.txt--append foo >foo.txt

If you want to control the format of the output time, you can use the-f switch to format:

[Grandiose@bygone grandiose]$/usr/bin/time 
   --output=foo.txt-f "\\t%E real,\\t%u user,\\t%s sys" Foo

If you still need to use the Shell's built-in time command, you can output the results to a file in the following sentence:

[Grandiose@bygone grandiose]$ (time foo) 2>foo.txt

This is written because the output of the built-in command time is to the standard error, and the file descriptor 2 indicates the standard error stderr. If you also want to include the results of Foo execution, you need to do this:

[Grandiose@bygone grandiose]$ (Time foo) >foo.txt 2>&1

Where the meaning of 2>&1 is 2 and 1 the same, together into the foo.txt.

The Nohup command guarantees that a process can still run after it exits the system, which is its general usage. We can also use nohup this way:

[Grandiose@bygone grandiose]$ nohup time foo

The results are all output to nohup.out, as well as time information for the program to run. You can use the following statement to output time information to the file foo.txt.

[Grandiose@bygone grandiose]$ tail-2 nohup.out > Foo.txt

To ensure consistency with POSIX, the time format for the output is (except for the content in Nohup.out):

Real    0m0.007s
user    0m0.002s
sys     0m0.004s

We can use Linux some of the following filter commands such as awk, SED, grep, tr and so on to filter out what we want to get, for example, want to get real segment corresponding time:

[Grandiose@bygone grandiose]$ grep real Foo.txt | CUT-F2, or
[grandiose@bygone grandiose]$ sed-n ' 2p ' foo.txt | cut-f2

Under the shell, the time precision of the output is millisecond, and if microsecond timing is required, it should be handled in the program. timing in kernel space

If you want to customize your device driver, you may use the timing function in the kernel. Timing in the Linux kernel space is not the same as the timing of user space. In kernel space, there is a global variable jiffies maintaining the current time. The call related to the system clock has (the new timer service):

#include <asm/param.h> 
#include <linux/timer.h> 
void Add_timer (struct timer_list * timer); 
int Del_timer (struct timer_list * timer); 
inline void Init_timer (struct timer_list * timer);

The definition of structure struct timer_list is:

struct Timer_list { 
	struct timer_list *next; 
	struct timer_list *prev; 
	unsigned long expires; 
	unsigned long data; 
	void (*function) (unsigned long d); 

Where the expiration time expires is the time to perform the function. Typically jiffies = jiffies + num when calling Add_timer, which indicates that the function is executed after the minimum time interval of the NUM system. The minimum time interval of the system is related to the hardware platform used, in which the constant Hz is defined as the number of minimum intervals within a second, then num*hz represents num seconds. The system calls the function at the scheduled time and deletes the subroutine from the timed queue, so if you want to perform it once at intervals, you must call Add_timer again in the function. The function's parameter d is the data item in the timer.

Jiffies's timing accuracy is 1% seconds, and if more precise timing is required in the kernel, functions in time_calls.h are used, which can be used for high-precision time calculations. Supplements

Sometimes, we need to get the time of the measured target more accurately, then we usually need to run the mean value to eliminate the error.

Gettimeofday (&start, NULL);
for (int i = 0; i< i++) foo (); 
Gettimeofday (&end, NULL);
Timeuse = 1000000 * (end.tv_sec-start.tv_sec) + end.tv_usec-start.tv_usec; 
Timeuse/= 10000000;

The statistics above actually introduce a new error, which is acceptable when the execution time of the cyclic instruction is negligible compared to Foo (), otherwise we will have to remove the execution time of the loop instruction before we get the correct statistic timing.

Under the Linux shell, if statistics are less frequent, you can:

For I at 1 2 3 4 5 6 7 8 9 do
	(time foo) 2>foo.tmp
	grep real foo.tmp | cut-f2 >> foo.txt
Done

If you have more times, you will need:

I=1 while
[$i-le] does
	(time foo) 2>foo.tmp grep real
	foo.tmp | cut-f2 >> foo.txt i=
	' ex PR $i + 1 ' done

Writing into the foo.txt if you manually calculate the average, it will be more time-consuming, we can write a shell script or in C language to read the file, calculate its mean value.

/* Time-consuming Division sum * *
cut-d ' m '-f1 foo.txt > Foo.tmp
sum=0 while
read
-do
	sum=$ (echo "$sum + $line" | BC -L
< foo.tmp
echo $sum/
* time consuming seconds sum * * *
cut-d ' m '-f2 foo.txt | tr-d ' s ' >
foo.tmp
sum=0 While the read line does
	sum=$ (echo "$sum + $line" | bc-l) Done
< foo.tmp
Echo $sum

It is much easier to calculate the sum of divisions and seconds and then manually calculate the average. Note that expr is not used for calculation because expr can only support integer values. Under the Linux shell, if you want to calculate floating-point numbers, you need to use BC or GEXPR. Concluding remarks

In fact, we can also use multiple languages, such as Perl and Python, to clock in a Linux system. Choose which tool or language to timing, which is related to the type of program or program being tested and the language in which they are written. Considering the requirement of precision, running time and running times, the running time of the program can be obtained reasonably and reliably. reference Please read runtime:high-performance programming techniques on Linux and Windows 2000, there are some discussions about the timing performance overhead of Linux and Windows platforms. Please refer to the website Chinese Linux Knowledge Base. For information about GEXPR, see the website gexpr. Matt Welsh & Lar kaufman,running linux,usa:o ' Reilly & associates,1995 Find more for Linux developers in the DeveloperWorks Linux zone of reference materials. The man page in Linux is also an important source of information.

http://www.ibm.com/developerworks/cn/linux/l-time/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.