本系列前兩節討論了計時用的時鐘源 clocksource,以及內核內部的一些時間表示方法。但真實用戶感知的是真實世界的牆上時間,而 clocksource 只能提供一個按給定頻率不停遞增的周期計數——如何把兩者關聯起來?這正是本節的主題。
內核管理著多種時間:RTC 時間、wall time(牆上時間)、monotonic time、raw monotonic time、boot time(總啟動時間)。
RTC 時間:PC 上又稱 CMOS 時間,通常由專門的計時硬件實現,軟件讀取它可得到年月日、時分秒。嵌入式系統中,有的用專門的 RTC 芯片,有的把 RTC 集成到 SoC 裡,讀某個寄存器即可。RTC 一般可持續計時,不管系統是否上電都不會丟失,硬件上常用後備電池單獨供電。由於 RTC 硬件多樣,每種都需要相應驅動,內核與用戶空間通過驅動訪問它。
xtime:與 RTC 同為人們日常使用的牆上時間,但 RTC 精度通常較低(多為毫秒級),外部芯片訪問也慢。為此內核另維護了一個 wall time 變量 xtime,精度取決於所用 clocksource,可達納秒級;由於它是內存變量,訪問極快,內核絕大多數時間都用它獲取當前時間。xtime 記錄自 1970 年 1 月 1 日 24 時到當前時刻所經歷的納秒數。
monotonic time:自開機後單調遞增,不會因用戶改時間而跳變;但不計算系統休眠時間,休眠期間不遞增。
raw monotonic time:與 monotonic 類似也是單調遞增,唯一區別是它「更純淨」,不受 NTP 調整影響,代表系統獨立時鐘硬件對時間的統計。
boot time:與 monotonic 相同,但累加了休眠時間,代表系統上電後的總時間。
對比如下:RTC 精度低、訪問慢、累計休眠時間、受 NTP 調整影響;xtime 精度高、速度快、累計休眠、受 NTP 影響;monotonic 精度高、快、不累計休眠、受 NTP 影響;raw monotonic 精度高、快、不累計休眠、不受 NTP 影響;boot time 精度高、快、累計休眠、受 NTP 影響。
struct timekeeper {
struct clocksource *clock; /* Current clocksource used for timekeeping. */
u32 mult; /* NTP adjusted clock multiplier */
int shift; /* The shift value of the current clocksource. */
cycle_t cycle_interval; /* Number of clock cycles in one NTP interval. */
u64 xtime_interval; /* Number of clock shifted nano seconds in one NTP interval. */
s64 xtime_remainder; /* shifted nano seconds left over when rounding cycle_interval */
u32 raw_interval; /* Raw nano seconds accumulated per NTP interval. */
u64 xtime_nsec; /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
s64 ntp_error; /* Difference between accumulated time and NTP time. */
int ntp_error_shift; /* Shift conversion between clock and ntp shifted ns. */
struct timespec xtime; /* The current time */
struct timespec wall_to_monotonic;
struct timespec total_sleep_time; /* time spent in suspend */
struct timespec raw_time; /* The raw monotonic time for CLOCK_MONOTONIC_RAW */
ktime_t offs_real; /* Offset clock monotonic -> clock realtime */
ktime_t offs_boot; /* Offset clock monotonic -> clock boottime */
seqlock_t lock; /* Seqlock for all timekeeper values */
};
xtime 是 timespec 變量,記錄自 1970 年 1 月 1 日以來的時間,精度可達納秒(取決於硬件是否支持)。
內核還維護 monotonic time,可理解為自系統啟動以來的時間,只能單調遞增;xtime 正常情況下也遞增,但用戶可主動前後調整牆上時間從而修改它。奇怪的是,內核並沒有直接定義一個變量記錄 monotonic 時間,而是定義了 wall_to_monotonic 保存兩者的偏移量:需要 monotonic 時間時把 xtime 與它相加即可。由於默認啟動時 monotonic 為 0,wall_to_monotonic 實際是個負數,與 xtime 同時在 timekeeping_init 中初始化。
計算 monotonic 要扣除休眠時間,內核用 total_sleep_time 記錄,每次休眠醒來後重新累加並調整 wall_to_monotonic,使其在醒來後不發生跳變。因此想獲取 boot time,還需再加上該變量:
void get_monotonic_boottime(struct timespec *ts)
{
......
do {
seq = read_seqbegin(&timekeeper.lock);
*ts = timekeeper.xtime;
tomono = timekeeper.wall_to_monotonic;
sleep = timekeeper.total_sleep_time;
nsecs = timekeeping_get_ns();
} while (read_seqretry(&timekeeper.lock, seq));
set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
}
raw_time 表示真正的硬件時間,即 raw monotonic time,不受任何時間調整影響。monotonic 雖不受 settimeofday 影響,但會受 NTP 調整;raw_time 連 NTP 也不受,開機後就單調遞增。xtime、monotonic、raw_time 可通過用戶空間 clock_gettime 獲得,對應 CLOCK_REALTIME、CLOCK_MONOTONIC、CLOCK_MONOTONIC_RAW。clock 字段指向當前使用的時鐘源,三者都基於它計時;當有更高精度的 clocksource 被注册時,timekeeping_notify 會調用 change_clocksource 更新該字段。
早期內核中 xtime、wall_to_monotonic、raw_time 是全局靜態變量,到 V3.4.10 已移入 timekeeper 結構,現在只需維護一個全局變量:
static struct timekeeper timekeeper;
由 timekeeping_init 完成,它在 start_kernel 的初始化序列中被調用。先從 RTC 獲取當前時間:
void __init timekeeping_init(void)
{
struct clocksource *clock;
unsigned long flags;
struct timespec now, boot;
read_persistent_clock(&now);
read_boot_clock(&boot);
初始化鎖與 NTP:
seqlock_init(&timekeeper.lock); ntp_init();
取默認 clocksource(若平台未實現 clocksource_default_clock,則為基於 jiffies 的 clocksource_jiffies),並關聯:
write_seqlock_irqsave(&timekeeper.lock, flags);
clock = clocksource_default_clock();
if (clock->enable)
clock->enable(clock);
timekeeper_setup_internals(clock);
用 RTC 時間初始化 xtime、raw_time、wall_to_monotonic:
timekeeper.xtime.tv_sec = now.tv_sec;
timekeeper.xtime.tv_nsec = now.tv_nsec;
timekeeper.raw_time.tv_sec = 0;
timekeeper.raw_time.tv_nsec = 0;
if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
boot.tv_sec = timekeeper.xtime.tv_sec;
boot.tv_nsec = timekeeper.xtime.tv_nsec;
}
set_normalized_timespec(&timekeeper.wall_to_monotonic, -boot.tv_sec, -boot.tv_nsec);
最後初始化 offs_real,並把 total_sleep_time 清零:
update_rt_offset(); timekeeper.total_sleep_time.tv_sec = 0; timekeeper.total_sleep_time.tv_nsec = 0; write_sequnlock_irqrestore(&timekeeper.lock, flags); }
xtime 保存在內存中,掉電後無法保存,所以每次啟動都要通過 timekeeping_init 從 RTC 同步。read_persistent_clock 與 read_boot_clock 是平台級函數,分別獲取 RTC 硬件時間與啟動時的時間。值得注意的是,在 3.4 代碼樹中,ARM 體系只有 tegra 與 omap 平台實現了 read_persistent_clock,其他平台由內核提供默認(全 0)實現:
void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
{ ts->tv_sec = 0; ts->tv_nsec = 0; }
void __attribute__((weak)) read_boot_clock(struct timespec *ts)
{ ts->tv_sec = 0; ts->tv_nsec = 0; }
那其他 ARM 平台如何初始化 xtime?答案是 CONFIG_RTC_HCTOSYS。開啟後 driver/rtc/hctosys.c 會被編入,由 rtc_hctosys 在系統初始化時通過 do_settimeofday 完成:
static int __init rtc_hctosys(void)
{
......
err = rtc_read_time(rtc, &tm);
......
rtc_tm_to_time(&tm, &tv.tv_sec);
do_settimeofday(&tv);
......
return err;
}
late_initcall(rtc_hctosys);
xtime 初始化完成後,timekeeper 便獨立於 RTC,利用關聯的 clocksource 更新時間。更新頻度取決於配置:未配置 NO_HZ 時,通常每個 tick 定時中斷週期調用一次 do_timer;配置了 NO_HZ 時,可能好幾個 tick 才調用一次,傳入參數是本次與上次相隔的 tick 數,系統會保證在 clocksource 的 max_idle_ns 內調用 do_timer 以防溢出:
void do_timer(unsigned long ticks)
{
jiffies_64 += ticks;
update_wall_time();
calc_global_load(ticks);
}
jiffies_64 相應累加,然後在 update_wall_time 中完成 xtime 等時間的更新——核心動作就是讀取 clocksource 的計數值累加到 xtime 等字段,其中還包含 NTP 調整相關代碼,此處不再貼全。
void getboottime(struct timespec *ts); // 系統啟動時刻的實時時間 void get_monotonic_boottime(struct timespec *ts); // 啟動以來經過的時間,含休眠 ktime_t ktime_get_boottime(void); // 同上,返回 ktime ktime_t ktime_get(void); // 啟動以來的時間,不含休眠 void ktime_get_ts(struct timespec *ts); // 同上,返回 timespec unsigned long get_seconds(void); // xtime 的秒計數 struct timespec current_kernel_time(void); // 最後一次更新的 xtime,不累加其後的計數 void getnstimeofday(struct timespec *ts); // 當前時間,timespec void do_gettimeofday(struct timeval *tv); // 當前時間,timeval