Linux User-State program timing method (1)

Source: Internet
Author: User
Tags integer numbers microsoft c

Linux User-State program timing method (1)

Preface

A good timer helps program developers determine program performance bottlenecks or compare performance of different algorithms. However, it is not easy to accurately measure the program running time, because process switching, interruptions, shared users, network traffic, high-speed cache access, transfer prediction, and other factors will affect the program timing.

This article will not consider these factors (for more information, see the book "understanding computer systems"), but only focus on the calculation of the execution time of user-state programs in Linux systems. In addition to the timing method described in this article, you can also use external tools to calculate time consumption, such as strace described in the Linux debugging analysis and diagnosis tool strace.

The running environment of the sample code in this article is as follows:

 

Basic concepts: 1.1 calendar time

Coordinated Universal Time (UTC): The world coordination Time (also known as the World Standard Time), the old Greenwich Mean Time (GMT ).

Calendar Time: the number of seconds that the Calendar Time passes from a standard Time point to the current Time. This standard time point varies with the compiler, but remains unchanged for the compilation system. The calendar time corresponding to the time in the compilation system is measured by the standard time point, so the calendar time is "relative time ". The UNIX/Linux time system starts from Epoch, which is specified as 00:00:00 GMT, January 1, January 1, 1970 ). In Microsoft C/C ++ 7.0, the standard time is set to 00:00:00, January 1, December 31, 1899, in Microsoft C/C ++ of other versions and Visual C ++ of all different versions, the standard time point is designated as 00:00:00, January 1, January 1, 1970. The calendar time is irrelevant to the time zone.

Epoch: time point. The time point in the Standard C/C ++ is an integer (time_t), which is expressed by the number of seconds (calendar time) between the current time and the standard time point. Currently, most UNIX systems use 32-bit record time. A positive value is expressed after January 1, 1970, and a negative value indicates before January 1, 1970. The time range can be simply estimated: 1970 ± (231-1)/3600/24/365) ≈ [] years. For a longer period of time, some compiler manufacturers introduce 64-bit or longer integer numbers to save the calendar time.

1.2 process time

Process time, also known as CPU time, is used to measure the CPU resources used by the process. The process time is calculated by clock ticking. Generally, three process time values are used, namely, the actual time (Real), the User CPU time (User), and the system CPU time (Sys ).

Actual time refers to the actual elapsed time; user time and system time refer to the CPU time used by a specific process. The specific differences are as follows:

  • Real is the wall clock time from process execution to completion, including the time slice used by other processes) this process takes time to block (such as waiting for the completion of I/O operations. This time corresponds to stopwatch for direct measurement.
  • The User is the CPU time consumed by the process to execute User-state code (outside the kernel). Only the actual CPU time used for executing the process is counted, it is not counted into the time slice used by other processes and the time when the process is blocked.
  • Sys is the CPU time consumed by the process to run in the kernel state, that is, the CPU time used by the kernel Execution System Call.

The total CPU time (User + Sys) is the sum of the time consumed by the CPU to execute User process operations and kernel (representing User process execution) system calls, that is, the process (including its threads and sub-processes) the actual CPU time used. If the program cyclically traverses the array, the user's CPU time is increased. If the program executes system calls such as exec or fork, the system CPU time is increased.

On a multi-core processor machine, if a process contains multiple threads or creates a sub-process by calling fork, the actual time may be less than the total CPU time-because different threads or processes can be executed in parallel, however, the time is the total CPU time of the master process. If the program is in the waiting state but not executed for a certain period of time, the actual time may be greater than the total CPU time. The numerical relationship is summarized as follows:

  • Real <CPU, indicating that the process is computing-intensive (CPU bound), taking advantage of parallel execution of multi-core processors;
  • Real ≈ CPU indicates that the process is computing-intensive (CPU bound) and is not executed in parallel;
  • Real> CPU, indicating that the process is I/O-intensive (I/O bound), the advantage of multi-core parallel execution is not obvious.

On a single-core processor, the difference between Real time and CPU time is that Real-(User + Sys) is the sum of all factors that delay program execution. It can be estimated that the CPU usage during the program running is CpuUsage = (User + Sys)/Real * 100 (% ).

On SMP (Symmetric Multi-processing system), the difference is approximately Real * ProcessorNum-(User + Sys ). These factors include:

  • I/O operations for transferring program text and data;
  • Obtain the I/O operations in which the program actually uses the memory;
  • CPU consumed by other programs;
  • CPU consumed by the operating system.
Second, timing

This section discusses and compares various timing methods based on the following functions:

1 #include <math.h>2 #define TIME_LOOP_NUM    1000000*203 void TimingFunc(void){4unsigned int i = 0;5     double y = 0.0;6     for(; i < TIME_LOOP_NUM; i++)7         y = sin((double)i);8 }
2.1 interval count

The operating system uses a timer (timer) to record the cumulative time used by each process. This time is only a rough measurement of the program execution time.

The operating system maintains the Count values of the user time and system time used by each process. When a timer is interrupted, the operating system searches for the active process in the current process list and increases the timer interval (usually 10 ms) for the Count value of the process ). If the process runs in kernel mode, the system time is increased; otherwise, the user time is increased.

The principle of this interval counting ("accounting") method is simple but not accurate. If a process runs for a short time (the same order of magnitude as the system timer) and the process is running when the timer is interrupted, the timer interval is added for the counter no matter whether the process has been running for a period of time or 1 millisecond before the interruption. The process has been switched when the interruption occurs. Therefore, there are errors at the beginning and end of the interval count. However, if the program runs for a long enough time (at least a few seconds), the inaccuracy of the interval count may make up for each other (the mean post-error of the overestimated and undervalued measurements is close to 0 ). Theoretically, it is difficult to analyze the error value. Therefore, it is meaningful to use the interval counting method only when the running time reaches the second level. In addition, the main advantage of this method is that its accuracy is not very dependent on the system load.

In Linux, the time command and times library function measure the command or program execution time by using the interval counting method.

2.1.1 time Command

The time command can measure the time consumed by the command or script execution and the usage of system resources. The statistical results include the following time (in seconds ):

  • Actual execution time (real time): the time consumed from the command line execution to the end of the operation;
  • User CPU time: the CPU time consumed by the command execution in the user State, that is, the time used by the program itself and the library functions it calls;
  • System CPU time: the CPU time consumed by the command execution in the kernel state, that is, the time when the system invocation is called directly or indirectly by the program.

In Linux, you can use the Shell built-in command time or the GNU General Command time (/usr/bin/time) to test the program running time. The former is only responsible for timing, with a precision of up to 10 milliseconds. The latter is slightly less accurate, but can access the information called by the getrusage system, and provides a wide range of Parameter options, including specifying output files and other functions.

The time command cannot be used to measure the execution time of a function or code segment in a program.

2.1.1.1 Shell command

The time format of the built-in Shell command is

Time <command> [<arguments...>]

After the command line is executed, the time statistics for executing the command line are printed in the standard output. For example:

 

Real> (User + Sys) indicates that the processor may execute other processes at the same time, or the process may be congested or sleep ). Sleep time is not counted into user time and system time. Blocking may be caused by system call errors or slow devices in the system.

Another example is to count the time spent in searching for the file hello. c in the current directory:

 

It can be seen that Real is much larger than (User + Sys), because the find Command performs a large number of disk I/O operations when traversing each directory, these operations are relatively time-consuming, therefore, most of the time the find process is waiting for disk I/O to complete. In addition, file-related system calls consume system time.

When you run the find command again, the real time will be significantly reduced:

 

This benefits from the System File Cache, which significantly reduces the number of disk I/O operations.

The following two methods can redirect the time information output by the time command to a file:

{Time find. -name "hello. c ";} 2> hello.txt // code block (the space character inside the curly braces is indispensable) (time find. -name "hello. c ") 2> hello.txt // sub-Shell (more resources)

Note that curly braces and parentheses in the preceding example are indispensable. Otherwise, Shell will process the command line following the time keyword as a whole, and the output of the time Command itself will not be redirected. The built-in command time is output to the standard error. file descriptor 2 indicates the standard error stderr. If you want to include the execution result of the find command, it is available:

(time find . -name "hello.c") 2>hello.txt 2>&1


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.