Linux内核中获取纳秒时间戳的方法
·
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
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐

所有评论(0)