Linux内核中获取纳秒时间戳的方法
1 方法1:使用getnstimeofday64方法
此处以获取内核中mm/page_alloc.c的__alloc_pages_direct_reclaim函数中的__perform_reclaim函数的执行之间为例(内核版本4.14.216),代码如下:
/* The really slow allocator path where we enter direct reclaim */
static inline struct page *
__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
unsigned int alloc_flags, const struct alloc_context *ac,
unsigned long *did_some_progress)
{
struct page *page = NULL;
bool drained = false;
struct timespec64 start, end;
getnstimeofday64(&start);
*did_some_progress = __perform_reclaim(gfp_mask, order, ac);
getnstimeofday64(&end);
printk("func : %s-> %s, time spent = %ld s, %ld ns\n", \
__func__, "__perform_reclaim", (end.tv_sec - start.tv_sec),\
(end.tv_nsec - start.tv_nsec));
//... the rest of the code
}
2 方法2:使用ktime_get_real_ns方法
此处仍是以获取内核中mm/page_alloc.c的__alloc_pages_direct_reclaim函数中的__perform_reclaim函数的执行之间为例(内核版本4.14.216),代码如下:
/* The really slow allocator path where we enter direct reclaim */
static inline struct page *
__alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
unsigned int alloc_flags, const struct alloc_context *ac,
unsigned long *did_some_progress)
{
struct page *page = NULL;
bool drained = false;
u64 start_time, end_time;
start_time = ktime_get_real_ns();
*did_some_progress = __perform_reclaim(gfp_mask, order, ac);
end_time = ktime_get_real_ns();
printk("func : %s-> %s, time spent = %ld ns\n", \
__func__, "__perform_reclaim", (end_time - start_time));
//... the rest of the code
}
相关参考:
Linux latest timekeeping
本文介绍了两种在Linux内核4.14.216版本中获取纳秒时间戳的方法:方法1是使用getnstimeofday64,方法2则是利用ktime_get_real_ns。这两种方法都适用于衡量内核函数执行时间。
591

被折叠的 条评论
为什么被折叠?



