AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 759880
Accepted
Alberto Pianon
Alberto Pianon
Asked: 2023-10-26 17:06:44 +0800 CST2023-10-26 17:06:44 +0800 CST 2023-10-26 17:06:44 +0800 CST

为什么 Linux 中的文件系统时间总是比系统时间晚一些毫秒?

  • 772

在Linux中,文件系统时间似乎总是落后系统时间几毫秒,如果您想检查文件是否在给定时间之前或之后在非常窄的时间范围(毫秒)内被修改,则会导致不一致。

在任何具有支持纳秒分辨率的文件系统的 Linux 系统中(我尝试使用具有 256 字节 inode 和 ZFS 的 ext4),如果您尝试执行以下操作:

date +%H:%M:%S.%N; echo "hello" > test1; stat -c %y test1 | cut -d" " -f 2

第二个输出值(文件修改时间)始终比第一个输出值(系统时间)晚几毫秒,例如:

17:26:42.400823099
17:26:42.395348462

而它应该是相反的,因为文件在调用命令后被test1修改。date

你可以在 python 中得到相同的结果:

import os, time

def test():
    print(time.time())
    with open("test1", "w") as f:
        f.write("hello")
        print(os.stat("test1").st_mtime)

test()
1698255477.3125281
1698255477.3070245

为什么会这样呢?有没有办法避免它,使系统时间与文件系统时间一致?到目前为止,我发现的唯一解决方法是通过创建一个虚拟临时文件并获取其修改时间来获取文件系统“时间”(无论这在实践中意味着什么),如下所示:

def get_filesystem_time():
    """
    get the current filesystem time by creating a temporary file and getting
    its modification time.
    """
    with tempfile.NamedTemporaryFile() as f:
        return os.stat(f.name).st_mtime

但我想知道是否有更清洁的解决方案。

linux
  • 2 2 个回答
  • 1084 Views

2 个回答

  • Voted
  1. Best Answer
    Stephen Kitt
    2023-10-26T18:49:34+08:002023-10-26T18:49:34+08:00

    文件时间戳使用的时间是最后一次计时器计时的时间,该时间总是稍微过去一些。调用中的函数current_timeinode.cktime_get_coarse_real_ts64:

    /**
     * current_time - Return FS time
     * @inode: inode.
     *
     * Return the current time truncated to the time granularity supported by
     * the fs.
     *
     * Note that inode and inode->sb cannot be NULL.
     * Otherwise, the function warns and returns time without truncation.
     */
    struct timespec64 current_time(struct inode *inode)
    {
        struct timespec64 now;
    
        ktime_get_coarse_real_ts64(&now);
    
        if (unlikely(!inode->i_sb)) {
            WARN(1, "current_time() called with uninitialized super_block in the inode");
            return now;
        }
    
        return timestamp_truncate(now, inode);
    }
    

    后者记录如下:

    这里返回的时间对应于最后一个计时器滴答声,可能是过去 10 毫秒(对于 CONFIG_HZ=100),与读取“jiffies”变量相同。这些[函数]仅在快速路径中调用时才有用,并且人们仍然期望优于秒的精度,但不能轻易使用“jiffies”,例如用于 inode 时间戳。在具有可靠周期计数器的大多数现代机器上,跳过硬件时钟访问可节省大约 100 个 CPU 周期,但在具有外部时钟源的旧硬件上最多可节省几微秒。

    请注意 inode 时间戳的具体提及。

    除了修改内核之外,我不知道有什么方法可以完全避免这种情况。您可以通过增加 来减少影响CONFIG_HZ。最近有一项改进建议,目前仍在研究中。

    • 21
  2. Marcus Müller
    2023-10-26T19:25:50+08:002023-10-26T19:25:50+08:00

    斯蒂芬·基特的回答似乎很准确。

    我们可以通过实际获得文件系统使用的相同“粗略”时钟来很好地重现这一点,至少在我的内核配置上是如此;在访问文件之前始终获取粗略实时时钟的C程序要么采用文件的时间戳,要么(很少)采用早一个系统时钟周期的时间戳:

    // excerpt from the program linked above, not a relicensing
    // …
        clock_gettime(CLOCK_REALTIME_COARSE, &now_coarse);
        clock_gettime(CLOCK_REALTIME, &now);
        int fd = open("temp", O_WRONLY | O_CREAT);
        write(fd, data, length);
        close(fd);
        clock_gettime(CLOCK_REALTIME, &now_after);
    
        stat("temp", &props);
    
        printf("Differences relative to coarse clock before:\n"
               "Fine Realtime before:   %+8jd ns\n"
               "File Modification Time: %+8jd ns\n"
               "Realtime clock after:   %+8jd ns\n",
               ns_difference(&now_coarse, &now),
               ns_difference(&now_coarse, &props.st_mtim),
               ns_difference(&now_coarse, &now_after));
    

    产生类似的东西

    Differences relative to coarse clock before:
    Fine Realtime before:   +1551810 ns
    File Modification Time:       +0 ns
    Realtime clock after:   +1626199 ns
    

    now_coarse前面提到的大约tick-duration delta的情况很少发生,当系统tick正好落在文件的获取和修改之间时,就会发生这种情况:

    Differences relative to coarse clock before:
    Fine Realtime before:   +1497562 ns
    File Modification Time:  +999992 ns
    Realtime clock after:   +1609943 ns
    

    顺便说一句,统计数据表明,如果我们执行上述操作,直到收集到 10,000 次发生这种“滴答跳”的事件,则可能的延迟范围非常小:

    Total processed:     777618
    Observed tick progressions:      10000
    Percentage:                 0.013
    Minimum tick delta:     999992
    Maximum tick delta:     999993
    Average tick delta: 999992.905900
    

    换句话说,当涉及到报价之间的增量时,我们的时间安排非常接近。

    • 14

相关问题

  • 有没有办法让 ls 只显示某些目录的隐藏文件?

  • 使用键盘快捷键启动/停止 systemd 服务 [关闭]

  • 需要一些系统调用

  • astyle 不会更改源文件格式

  • 通过标签将根文件系统传递给linux内核

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve