Linux kernel 墙上时间

前言

最近在研究 Linux 调度子系统,该子系统由时钟中断推动。每发生一次时钟中断,就会执行一次时钟中断服务程序,在时钟中断服务程序中,最终会调用 tick_periodic() 这个函数。该函数中有 update_wall_time() 这样一个函数,引起了我的兴趣。

环境

QEMU,Vexpress,Cortex-A9,ARMv7,Linux-6.1.44

更新墙上时间

/*
 * Periodic tick
 */
static void tick_periodic(int cpu)
{
	if (tick_do_timer_cpu == cpu) {
		raw_spin_lock(&jiffies_lock);
		write_seqcount_begin(&jiffies_seq);

		/* Keep track of the next tick event */
		tick_next_period = ktime_add_ns(tick_next_period, TICK_NSEC);

		do_timer(1);
		write_seqcount_end(&jiffies_seq);
		raw_spin_unlock(&jiffies_lock);
		update_wall_time();
	}

	update_process_times(user_mode(get_irq_regs()));
	profile_tick(CPU_PROFILING);
}

在 tick_periodic() 中调用了 update_wall_time(),用来更新墙上时间,所谓墙上时间,就是用户在系统中看到的时间,换句话说,就是在 shell 中使用 data 命令显示的时间。

时间静止

如果我注释掉 update_wall_time() 函数,墙上时间是不是就静止了?
实验一下

/*
 * Periodic tick
 */
static void tick_periodic(int cpu)
{
	if (tick_do_timer_cpu == cpu) {
		raw_spin_lock(&jiffies_lock);
		write_seqcount_begin(&jiffies_seq);

		/* Keep track of the next tick event */
		tick_next_period = ktime_add_ns(tick_next_period, TICK_NSEC);

		do_timer(1);
		write_seqcount_end(&jiffies_seq);
		raw_spin_unlock(&jiffies_lock);
		// update_wall_time();
	}

	update_process_times(user_mode(get_irq_regs()));
	profile_tick(CPU_PROFILING);
}

重新编译内核,启动

Welcome to Buildroot
buildroot login: root
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# date
Tue Feb 27 10:30:44 UTC 2024
# cat /proc/version 
Linux version 6.1.44 (liyongjun@Box) (arm-buildroot-linux-gnueabihf-gcc.br_real (Buildroot 2023.08-676-gdc81467e4b) 12.3.0, GNU ld (GNU Binutils) 2.40) #3 SMP Tue Feb 27 18:29:44 CST 2024

果然,Linux 系统时间不走了。

struct timekeeper

在 Linux kernel 中,除了墙上时间,还有单调时间、启动时间等,kernel 使用 struct timekeeper 结构体维护各种时间。

我们主要来看 xtime_sec、ktime_sec 这两个时间,它们分别表示实时时间当前的秒数,单调时间当前的秒数。

struct timekeeper {
	struct tk_read_base	tkr_mono;
	struct tk_read_base	tkr_raw;
	u64			xtime_sec;
	unsigned long		ktime_sec;
	struct timespec64	wall_to_monotonic;
。。。

};

update_wall_time() 调用 timekeeping_advance(),我们在该函数中打印 xtime_sec 和 ktime_sec。

void update_wall_time(void)
{
	if (timekeeping_advance(TK_ADV_TICK))
		clock_was_set_delayed();
}
static bool timekeeping_advance(enum timekeeping_adv_mode mode)
{
	struct timekeeper *real_tk = &tk_core.timekeeper;
	struct timekeeper *tk = &shadow_timekeeper;
	u64 offset;
	int shift = 0, maxshift;
	unsigned int clock_set = 0;
	unsigned long flags;

	raw_spin_lock_irqsave(&timekeeper_lock, flags);

printk("xtime_sec = %llu\n", real_tk->xtime_sec);
printk("ktime_sec = %lu\n", real_tk->ktime_sec);

。。。

	return !!clock_set;
}

重新编译内核,启动

L2C-310 enabling early BRESP for Cortex-A9
L2C-310 full line of zeros enabled for Cortex-A9
L2C-310 dynamic clock gating disabled, standby mode disabled
L2C-310 cache controller enabled, 8 ways, 128 kB
L2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
rcu: srcu_init: Setting srcu_struct sizes based on contention.
sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns
clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
smp_twd: clock not found -2
Console: colour dummy device 80x30
Calibrating local timer... 
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
75.67MHz.
Calibrating delay loop... 
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
ktime_sec = 0
761.03 BogoMIPS (lpj=3805184)
CPU: Testing write buffer coherency: ok
CPU0: Spectre v2: using BPIALL workaround
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
xtime_sec = 0
ktime_sec = 0
xtime_sec = 0
。。。

可以看到,在内核初始化 local timer 后,时钟中断便开始工作,一开始 xtime_sec 和 ktime_sec 均为 0,
随着 kernel 的运行,它们两个也开始同步增长

xtime_sec = 0
ktime_sec = 0
hw perfevents: enabled with armv7_cortex_a9 PMU driver, 7 counters available
xtime_sec = 1
ktime_sec = 1

当启动 rtc 后,kernel 便从 rtc 中读取时间,恢复到 xtime_sec,从此 xtime_sec 便开始表示墙上时间,而 ktime_sec 继续单调增长,表示系统启动时间。

xtime_sec = 1
ktime_sec = 1
rtc-pl031 10017000.rtc: registered as rtc0
rtc-pl031 10017000.rtc: setting system clock to 2024-02-27T13:08:30 UTC (1709039310)
xtime_sec = 1709039310
ktime_sec = 1
mmci-pl18x 10005000.mmci: Got CD GPIO
mmci-pl18x 10005000.mmci: Got WP GPIO
xtime_sec = 1709039310
ktime_sec = 1

相关推荐

  1. Linux kernel 墙上时间

    2024-04-15 07:30:04       30 阅读
  2. P1990 覆盖墙壁

    2024-04-15 07:30:04       12 阅读
  3. mysql根据时间段生成时间

    2024-04-15 07:30:04       19 阅读
  4. bash计算时间差 时间间隔

    2024-04-15 07:30:04       20 阅读
  5. Unity-时间

    2024-04-15 07:30:04       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-15 07:30:04       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-15 07:30:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-15 07:30:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-15 07:30:04       18 阅读

热门阅读

  1. Spark开窗函数之ROW

    2024-04-15 07:30:04       14 阅读
  2. C#去掉字符串中所有匹配的字符String.Replace方法

    2024-04-15 07:30:04       15 阅读
  3. 最短路计数

    2024-04-15 07:30:04       48 阅读
  4. MATLAB初学者入门(1)—— 基础知识和功能介绍

    2024-04-15 07:30:04       20 阅读
  5. MATLAB结合C+混编循环计算多孔结构的孔径分布

    2024-04-15 07:30:04       19 阅读
  6. 迁移强化学习论文笔记(一)(Successor Features)

    2024-04-15 07:30:04       46 阅读
  7. Promise实现

    2024-04-15 07:30:04       18 阅读