int do_settimeofday64(const struct timespec64 *ts)用于修改当前timekeeper的时间.
其源码分析如下:
int do_settimeofday64(const struct timespec64 *ts)
{
#得到当前timekeeper的指针
struct timekeeper *tk = &tk_core.timekeeper;
struct timespec64 ts_delta, xt;
unsigned long flags;
int ret = 0;
#形参ts是要设置到timekeeper中的时间,这里会检查这个时间是否是有效值
if (!timespec64_valid_strict(ts))
return -EINVAL;
#更新timekeeper的时间要禁止内核抢占
raw_spin_lock_irqsave(&timekeeper_lock, flags);
write_seqcount_begin(&tk_core.seq);
timekeeping_forward_now(tk);
#得带当前timekeeper的时间
xt = tk_xtime(tk);
#计算形参和当前timekeeper的时间的差值
ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
#比较这个差值是否比timekeeper的wall_to_monotonic 大,如果小于就不用修改timekeeper直接返回error
if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
ret = -EINVAL;
goto out;
}
#走到这一步说明前面的检查都合理,这里主要修改timekeeper的wall_to_monotonic的值
tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
#设置timekeeper的xtime_sec和xtime_nsec
tk_set_xtime(tk, ts);
out:
#设置完成后更新timekeeper
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
write_seqcount_end(&tk_core.seq);
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
/* signal hrtimers about time change */
#给高精度始终发送信号说明timekeeper的时间已经更新了
clock_was_set();
return ret;
}
其中tk_set_xtime的实现如下:
static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
{
tk->xtime_sec = ts->tv_sec;
tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
}
可以看出就是成员变量之间基本的赋值.
内核定时机制API之do_settimeofday64
最新推荐文章于 2023-12-24 16:46:34 发布