Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get user space ns time in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to get ns-level time in user space in Linux, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

User space gets 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 interpretation

CLOCK_REALTIME: the real-time time of the system, which changes with the real-time time of the system, that is, starting from 0:0:0 in UTC1970-1-1. If the system time is supposed to be other by the user, the corresponding time will be changed accordingly.

CLOCK_MONOTONIC: timing starts from the moment the system starts, regardless of the change of the system time by the user

CLOCK_PROCESS_CPUTIME_ID: time spent from this process to the current code system CPU

CLOCK_THREAD_CPUTIME_ID: the time it takes from this 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 for usage 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\ n", time_start.tv_sec, time_start.tv_nsec)

Clock_gettime (CLOCK_REALTIME, & time_end)

Printf ("endtime% llus,%llu ns\ n", time_end.tv_sec, time_end.tv_nsec)

Printf ("duration:%llus% lluns\ n", 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, we can see that it takes about 15us to call the printf () function at one time.

This is the answer to the question about how to obtain user space ns time in Linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report