【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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值