When doing Linux system operation, because the test program performance needs, the time must be accurate to the ns level, then how to achieve it? The following small series teaches you how to use the function to achieve the Linux user space ns level time acquisition, let's learn together.
I. Introduction
We often need to obtain precise time ns level of performance test procedures to measure performance when a program described below under linux user space to get ns Level time method
Second, user space to get ns level time
Using the clock_gettime function, the function prototype is as follows:
long sys_clock_gettime (clockid_t which_clock, struct timespec *tp);
1.which_clock parameter explanation
CLOCK_REALTIME: The system real-time time changes with the real-time time of the system, that is, starting from UTC1970-1-1 0:0:0, if the system is in the middle moment The time is changed by the user, and the corresponding time changes accordingly
CLOCK_MONOTONIC: Starts from the moment the system is started, and is not affected by the system time being changed by the user
CLOCK_PROCESS_CPUTIME_ID: This process is Time spent by the current code system CPU
CLOCK_THREAD_CPUTIME_ID: time spent by the thread to the current code system CPU
2.struct timespec structure
The code is as follows:
struct timespec
{
time_t tv_sec;
long int tv_nsec;
} ;
The sample code is as follows:
The code is as follows:
#include"stdio.h"
#include"stdlib.h"
#include《time.h》
int main(void)
{
struct timespec time_start={0, 0}, time_end={0, 0 };
clock_gettime(CLOCK_REALTIME, &time_start);
printf(“start time %llus,%llu ns\ ”, time_start.tv_sec, time_start.tv_nsec);
clock_gettime(CLOCK_REALTIME, &time_end);
printf(“endtime %llus,%llu ns\ ”, time_end.tv_sec, time_end.tv_nsec);
printf (&duquo;duration:%llus %lluns\ ”, time_end.tv_sec-time_start.tv_sec, time_end.tv_nsec-time_start.tv_nsec);
return 0;
}
Compile command:
The code is as follows:
gcc test.c -o test -lrt
Running result:
The code is as follows:
. /test
start time 1397395863s,973618673 ns
endtime 1397395863s,973633297 ns
duration:0s 14624ns
From the running results you can see that printf is called ( The function needs about 15us at a time.
The above is a method for obtaining ns-level time using functions under Linux. If you need to be accurate to ns-level time in the test program performance, you can try the method described in this article. Not very simple?
Ubuntu sometimes need to modify the command prompt PS1 when using the command, but many people do no
Swap is a swap partition in Ubuntu systems that functions somewhat like the virtual memory of a Wind
OpenWRT is an embedded Linux system. Friends who want to understand the embedded Linux system are bl
Many computers now have more than one network card, but one network card fails and the entire networ
What is optimized for Linux kernel 4.2?
How to modify the time zone of CentOS6 system?
Linux Mint Cinnamon uses vino-server to configure desktop sharing. Step
Learn to use the character set encoding of tool conversion files in Linux systems
Linux how to use vi /vim command
RedHat how to install log4cxx log library
How to view the vsftp database db file in Linux
Ubuntu install and switch Nvidia dual graphics driver method
The implementation principle of Linux system TSO
Detailed explanation of the functions and parameters of the Linux system ar command
How to make Win8 system U disk boot disk
Nagios latest version installation under CentOS5.5
Driving life perfectly fits Windows10 system
How to use Win7 system notebook fingerprint recognition?
Differences between Outlook and Outlook Express
Win8 system Chrome browser prompts "no registration class" how to deal with?
Win10 RTM official version of Build 10240 release log exposure
Some solutions to the problem of Win10 battery not charging
What operations will cause bad sectors on the hard drive or even hang up