ktime_get() and do_gettimeofday() APIs use

We have some APIs to measure time taken for a function or a piece of code in the driver.

1. do_gettimeofday()
2. ktime_get()

These APIs are used to measure the time (absolute timestamp) in the kernel.
do_gettimeofday() gives microsecond precision.
ktime_get() return ktime_t object, which has nano second precision.

ktime_get:

===========
Here is abstract code to show how ktime_get() API will be used in our driver to measure time taken for a function.

The prototype for ktime_get is:

 #include <linux/ktime.h>
 ktime_t ktime_get(void);

Sample-code:
--------------
ktime_t start, end;
s64 actual_time;

start = ktime_get();

function();
/* or piece of code */
....
....

end = ktime_get();

actual_time = ktime_to_ns(ktime_sub(end, start));

printk(KERN_INFO, "Time taken for function() execution: %lld\n",
(long long)actual_time);

/* Use below code for millisec precision */
actual_time = ktime_to_ms(ktime_sub(end, start));

printk(KERN_INFO, "Time taken for function() execution: %u\n",
(unsigned int)actual_time);


do_gettimeofday():

==================
Here is abstract code to show how do_gettimeofday API will be used in our driver to measure time taken for a function.

The prototype for do_gettimeofday is:

 #include <linux/time.h>
 void do_gettimeofday(struct timeval *tv);

When this function called, it fills the timestamp data in struct timeval.

Using struct timeval members, we can extract seconds and microseconds info.

Sample code:
-------------
do_gettimeofday(&tstart);

/* Function or code to measure time bound */

do_gettimeofday(&tend);

printk("time taken: %ld millisec\n",
1000 * (tend.tv_sec - tstart.tv_sec) +
(tend.tv_usec - tstart.tv_usec) / 1000);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值