自学内容网 自学内容网

【linux 获取时间】

linux 获取时间接口

我们在开发调试过程中,可能遇到一些和调用时序相关的问题,为了查看哪个步骤先调用,哪个步骤后调用,我们可以使用函数打印或者主动trace堆栈…但是有的时候我们需要排查2个接口调用的时间间隔,我们可以使用记录接口调用时间点的方式来查看接口见调用的时间间隔(比如网卡的power power 和 power down之间必须要有一定的时间间隔,才能正常的power down成功)。

1、系统时间获取接口

//include/linux/time64.h

//将时间按照s和ns记录
struct timespec64 {
    time64_t tv_sec; /* seconds */
    long tv_nsec     /* nanoseconds */
}
//kernel/time/timekeeping.c

/**
 * ktime_get_ts64 - get the monotonic clock in timespec64 format
 * @ts:pointer to timespec variable
 *
 * The function calculates the monotonic clock from the realtime
 * clock and the wall_to_monotonic offset and stores the result
 * in normalized timespec64 format in the variable pointed to by @ts.
 */
void ktime_get_ts64(struct timespec64 *ts)
{
struct timekeeper *tk = &tk_core.timekeeper;
struct timespec64 tomono;
unsigned int seq;
u64 nsec;

WARN_ON(timekeeping_suspended);

do {
seq = read_seqcount_begin(&tk_core.seq);
ts->tv_sec = tk->xtime_sec;
nsec = timekeeping_get_ns(&tk->tkr_mono);
tomono = tk->wall_to_monotonic;

} while (read_seqcount_retry(&tk_core.seq, seq));

ts->tv_sec += tomono.tv_sec;
ts->tv_nsec = 0;
timespec64_add_ns(ts, nsec + tomono.tv_nsec);
}

2、如何使用

    int main (void{
struct timespec64 callTimer = {0};
        
        ktime_get_ts64(&callTimer);
printf("tv_sec: %llu, tv_nsec: %llu\n", callTimer.tv_sec, callTimer.tv_nsec);
return 0;
    }

原文地址:https://blog.csdn.net/weixin_43300893/article/details/142385400

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!