上一篇
Linux如何获取内核时间戳
- Linux
- 2025-07-03
- 8
Linux获取 内核时间戳主要有两种方法:使用 clock_gettime()系统调用(指定 CLOCK_MONOTONIC等时钟源),或直接读取 /proc/uptime文件获取系统启动后的运行时间,前者精度更高,适用于程序内部;后者方便命令行查看。
用户空间程序获取内核时间
通过系统调用直接获取内核提供的时间数据:
-
clock_gettime() 函数
#include <time.h> #include <stdio.h> int main() { struct timespec ts; // 获取单调时间(系统启动后流逝的时间,不受时钟调整影响) clock_gettime(CLOCK_MONOTONIC, &ts); printf("Monotonic: %ld秒 %ld纳秒n", ts.tv_sec, ts.tv_nsec); // 获取实时时间(含时区调整) clock_gettime(CLOCK_REALTIME, &ts); printf("Realtime: %ld秒 %ld纳秒n", ts.tv_sec, ts.tv_nsec); return 0; }- 时钟类型:
- CLOCK_MONOTONIC:系统启动后的单调递增时间,适合性能测量。
- CLOCK_REALTIME:实际日期时间(可被NTP或用户修改)。
- CLOCK_BOOTTIME:包含系统休眠时间。
- 精度:通常达纳秒级。
- 时钟类型:
-
gettimeofday() 函数(旧版兼容)
#include <sys/time.h> struct timeval tv; gettimeofday(&tv, NULL); // 微秒精度 printf("%ld秒 %ld微秒n", tv.tv_sec, tv.tv_usec);- 注意:已被clock_gettime取代,精度较低(微秒级)。
内核模块中获取时间戳
在内核驱动或模块开发中直接调用时间函数:

-
ktime_get()
#include <linux/ktime.h> ktime_t now = ktime_get(); // 获取单调时间(纳秒精度) u64 nanoseconds = ktime_to_ns(now); // 转为64位纳秒值- 最佳实践:推荐用于新内核,精度高且无锁。
-
ktime_get_real()
ktime_t real_time = ktime_get_real(); // 获取实时时间(受系统时间修改影响)
-
旧版内核函数
#include <linux/time.h> struct timespec ts; getnstimeofday(&ts); // 实时时间(纳秒精度) ktime_get_ts(&ts); // 单调时间(纳秒精度)
命令行工具快速获取
-
date 命令
date +"%s.%N" # 当前实时时间(秒.纳秒) -
cat /proc/timer_list
查看系统时钟源和当前计数器值:
grep "now at" /proc/timer_list # 输出当前CPU的时钟计数器 -
dmesg 日志时间戳
dmesg -T # 显示转换后的实时时间 dmesg -t # 显示内核启动后的秒数
关键注意事项
-
精度与性能

- CLOCK_MONOTONIC 比 CLOCK_REALTIME 更快(无需锁)。
- ktime_get() 是内核中最安全的选择。
-
时钟源影响
通过 /sys/devices/system/clocksource/clocksource0/current_clocksource 查看当前时钟源(如tsc、hpet),不同时钟源精度差异显著。
-
实时性保障
用户空间程序需链接 -lrt 库(编译命令:gcc prog.c -o prog -lrt)。
-
版本兼容性
- 内核 ≥4.0:优先使用 ktime_get()。
- 旧内核:do_gettimeofday() 或 getnstimeofday()(已逐步弃用)。
- 网络延迟测量:用 CLOCK_MONOTONIC 记录数据包收发时间差。
- 内核驱动:使用 ktime_get() 为硬件事件打时间戳。
- 日志同步:dmesg -T 将内核日志关联到实际时间。
- Linux内核文档(kernel.org/doc)
- POSIX.1-2008标准(clock_gettime)
- 函数手册页(man 2 clock_gettime、man 3 ktime_get)
- 时钟源分析(Access Red Hat Clocksource)
典型应用场景
引用说明: