From 830407e88f9d40d954356c3754f2647f91d5c06a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:26:00 +0200 Subject: Adding upstream version 5.6.0. Signed-off-by: Daniel Baumann --- modules/detect_time_jump/detect_time_jump.lua | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/detect_time_jump/detect_time_jump.lua (limited to 'modules/detect_time_jump/detect_time_jump.lua') diff --git a/modules/detect_time_jump/detect_time_jump.lua b/modules/detect_time_jump/detect_time_jump.lua new file mode 100644 index 0000000..6c5b63f --- /dev/null +++ b/modules/detect_time_jump/detect_time_jump.lua @@ -0,0 +1,45 @@ +-- SPDX-License-Identifier: GPL-3.0-or-later +-- Module interface +local ffi = require('ffi') + +local mod = {} +mod.threshold = 10 * min +local event_id = nil + +-- Get time of last cache clear. Compute difference between realtime +-- and monotonic time. Compute difference of actual realtime and monotonic +-- time. In ideal case these differences should be almost same. +-- If they differ more than mod.threshold value then clear cache. +local function check_time() + local checkpoint = cache.checkpoint() + local cache_timeshift = checkpoint.walltime.sec * 1000 - checkpoint.monotime + local actual_timeshift = os.time() * 1000 - tonumber(ffi.C.kr_now()) + local jump_backward = cache_timeshift - actual_timeshift + if jump_backward > mod.threshold then + log_info(ffi.C.LOG_GRP_DETECTTIMEJUMP, "Detected backwards time jump, clearing cache.\n" .. + "But what does that mean? It means your future hasn't been written yet." + ) + cache.clear() + elseif -jump_backward > mod.threshold then + -- On Linux 4.17+ this shouldn't happen anymore: https://lwn.net/Articles/751482/ + log_info(ffi.C.LOG_GRP_DETECTTIMEJUMP, "Detected forward time jump. (Suspend-resume, possibly.)") + cache.checkpoint(true) + end +end + +function mod.init() + if event_id then + error("Module is already loaded.") + else + event_id = event.recurrent(1 * min , check_time) + end +end + +function mod.deinit() + if event_id then + event.cancel(event_id) + event_id = nil + end +end + +return mod -- cgit v1.2.3