我知道我的问题有一个答案:QFile seek performance。但我对答案并不完全满意。即使在查看了generic_file_llseek() for ext4的以下实现之后,我似乎也无法理解如何度量复杂性。
/**
* generic_file_llseek - generic llseek implementation for regular files
* @file: file structure to seek on
* @offset: file offset to seek to
* @origin: type of seek
*
* This is a generic implemenation of ->llseek useable for all normal local
* filesystems. It just updates the file offset to the value specified by
* @offset and @origin under i_mutex.
*/
loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
{
loff_t rval;
mutex_lock(&file->f_dentry->d_inode->i_mutex);
rval = generic_file_llseek_unlocked(file, offset, origin);
mutex_unlock(&file->f_dentry->d_inode->i_mutex);
return rval;
}
/**
* generic_file_llseek_unlocked - lockless generic llseek implementation
* @file: file structure to seek on
* @offset: file offset to seek to
* @origin: type of seek
*
* Updates the file offset to the value specified by @offset and @origin.
* Locking must be provided by the caller.
*/
loff_t
generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
{
struct inode *inode = file->f_mapping->host;
switch (origin) {
case SEEK_END:
offset += inode->i_size;
break;
case SEEK_CUR:
/*
* Here we special-case the lseek(fd, 0, SEEK_CUR)
* position-querying operation. Avoid rewriting the "same"
* f_pos value back to the file because a concurrent read(),
* write() or lseek() might have altered it
*/
if (offset == 0)
return file->f_pos;
break;
}
if (offset < 0 || offset > inode->i_sb->s_maxbytes)
return -EINVAL;
/* Special lock needed here? */
if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
return offset;
}例如,我有一个4GB文件,我知道文件中中间部分的偏移量,一个lseek()如何使我在不遍历整个文件的情况下到达那里?操作系统是否已经知道文件的每个字节驻留在哪里了?
发布于 2014-02-09 11:24:01
在lseek()中实现的ext4只会增加文件指针并执行一些验证检查。它不取决于文件大小,这意味着它是O(1)。
您还可以在代码中看到这一点,在代码中没有任何循环或可疑函数调用。
然而,虽然这在ext4上是正确的,但对于其他文件系统可能不是这样,因为POSIX并不保证这种行为。但是,除非文件系统是为了一个非常特殊的目的,否则很可能是这样。
发布于 2014-02-09 11:24:26
lseek的复杂性取决于文件在您的系统中的表示。在大多数现代系统中,文件是由某种聪明的树状数据结构组织的,导致seek在time O(logx(n))中执行,其中n是文件的大小,x是某个系统的依赖数。
发布于 2014-02-09 11:46:40
是的,操作系统已经知道如何在文件中找到任何特定的字节。
不,不能保证是O(1)。您发布的代码是O(1),但其他文件系统的代码可能不是。
是的,它将足够快,除非操作系统或文件系统是可怕的。
https://stackoverflow.com/questions/21658364
复制相似问题