sync() 最多需要多少时间?
How much time sync() can take at max?
sync() causes all pending modifications to filesystem metadata and
cached file data to be written to the underlying filesystems.
sync() is always successful.
表示sync()会return只有同步所有数据后才会下划线
文件系统。我想知道同步()所有数据需要多少时间?
能不能几分钟,最差几个小时。
我不知道什么参数定义了 sync() 时间。
我已经使用 sync() 创建了一个测试程序
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
char buffer[536870912];
int main(void)
{
int fd;
loff_t nr = 536870912;
int ret;
remove("./dummy");
errno = 0;
fd = open("./dummy", O_CREAT | O_NOFOLLOW | O_LARGEFILE | O_RDWR, 0700);
perror("open");
buffer[10] = 'a';
buffer[1024] = 'b';
buffer[10000] = 'c';
buffer[536870912 - 1000] = 'd';
buffer[536870912 - 2000] = 'e';
buffer[536870912 - 1] = 'f';
int i = 3;
while (i-- > 0) {
ret = write(fd, buffer, 536870912);
if (ret <= 0) {
perror("write");
return 1;
}
nr -= ret;
printf("sync start\n");
}
sync();
printf("sync done\n");
return 0;
}
它显示 sync() 在多次迭代中花费的时间不均匀。
有时这个测试程序在 sync() 时甚至需要 30 分钟,即 sync() 不会 return 30 分钟。这是一个有效的行为吗?
我们将不胜感激。
永远最大。例如等待写入断开连接的 NFS 共享的数据。
POSIX 说:
The writing, although scheduled, is not necessarily complete upon
return from sync().
这意味着通常 sync()
只是安排刷新但不等待其完成。所以 sync()
几乎是异步的,它 returns 的事实并不意味着刷新已经完成。 sync()
调用底层刷新所消耗的时间之间没有关系。您也无法知道冲洗是否 successful/terminated。
sync()
执行的时间没有固定的上限。它的任务是将缓冲区缓存中的修改页面同步到底层文件系统。需要多少时间取决于(除其他因素外):有多少数据要同步,以及托管底层文件系统的设备有多慢(例如,想想写入速度非常低的 USB 记忆棒)。在极端情况下,这可能是几十分钟,甚至几个小时。
已接受的答案引用了 POSIX 中的详细信息:
The writing, although scheduled, is not necessarily complete upon
return from sync().
但随后将其解释为:
That means that in general sync()
just schedules the flush
but do not wait for its completion.
我认为这误解了 POSIX 以及现有实现的性质。在大多数现有(可能 all 现代)实现中,sync()
阻塞,直到所有数据都已同步到存储设备。因此,到 sync()
returns 时,应用程序知道数据已到达底层存储。
但是,POSIX 规范还允许另一种实现方式:内核仅安排同步操作以异步方式发生,returns 从 sync()
调用直接进行。这显然不如阻塞 sync()
实现有用,因为执行同步操作的应用程序通常想知道数据何时到达底层设备。在具有 "asynchronous" sync()
的系统上,应用程序无法确定数据何时到达存储设备。
POSIX 允许任何一种实现方式,大概是因为在 API 被标准化的时候,一些现有的实现提供了较弱的异步 sync()
实现,而通常的 POSIX 这种情况下的方法是标准化为最小公分母。
sync() causes all pending modifications to filesystem metadata and cached file data to be written to the underlying filesystems. sync() is always successful.
表示sync()会return只有同步所有数据后才会下划线 文件系统。我想知道同步()所有数据需要多少时间? 能不能几分钟,最差几个小时。
我不知道什么参数定义了 sync() 时间。
我已经使用 sync() 创建了一个测试程序
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
char buffer[536870912];
int main(void)
{
int fd;
loff_t nr = 536870912;
int ret;
remove("./dummy");
errno = 0;
fd = open("./dummy", O_CREAT | O_NOFOLLOW | O_LARGEFILE | O_RDWR, 0700);
perror("open");
buffer[10] = 'a';
buffer[1024] = 'b';
buffer[10000] = 'c';
buffer[536870912 - 1000] = 'd';
buffer[536870912 - 2000] = 'e';
buffer[536870912 - 1] = 'f';
int i = 3;
while (i-- > 0) {
ret = write(fd, buffer, 536870912);
if (ret <= 0) {
perror("write");
return 1;
}
nr -= ret;
printf("sync start\n");
}
sync();
printf("sync done\n");
return 0;
}
它显示 sync() 在多次迭代中花费的时间不均匀。
有时这个测试程序在 sync() 时甚至需要 30 分钟,即 sync() 不会 return 30 分钟。这是一个有效的行为吗?
我们将不胜感激。
永远最大。例如等待写入断开连接的 NFS 共享的数据。
POSIX 说:
The writing, although scheduled, is not necessarily complete upon return from sync().
这意味着通常 sync()
只是安排刷新但不等待其完成。所以 sync()
几乎是异步的,它 returns 的事实并不意味着刷新已经完成。 sync()
调用底层刷新所消耗的时间之间没有关系。您也无法知道冲洗是否 successful/terminated。
sync()
执行的时间没有固定的上限。它的任务是将缓冲区缓存中的修改页面同步到底层文件系统。需要多少时间取决于(除其他因素外):有多少数据要同步,以及托管底层文件系统的设备有多慢(例如,想想写入速度非常低的 USB 记忆棒)。在极端情况下,这可能是几十分钟,甚至几个小时。
已接受的答案引用了 POSIX 中的详细信息:
The writing, although scheduled, is not necessarily complete upon return from sync().
但随后将其解释为:
That means that in general
sync()
just schedules the flush but do not wait for its completion.
我认为这误解了 POSIX 以及现有实现的性质。在大多数现有(可能 all 现代)实现中,sync()
阻塞,直到所有数据都已同步到存储设备。因此,到 sync()
returns 时,应用程序知道数据已到达底层存储。
但是,POSIX 规范还允许另一种实现方式:内核仅安排同步操作以异步方式发生,returns 从 sync()
调用直接进行。这显然不如阻塞 sync()
实现有用,因为执行同步操作的应用程序通常想知道数据何时到达底层设备。在具有 "asynchronous" sync()
的系统上,应用程序无法确定数据何时到达存储设备。
POSIX 允许任何一种实现方式,大概是因为在 API 被标准化的时候,一些现有的实现提供了较弱的异步 sync()
实现,而通常的 POSIX 这种情况下的方法是标准化为最小公分母。