zvfs: 性能测试

This commit is contained in:
2026-02-24 16:01:29 +00:00
parent 6f8f2148c3
commit 8d1d9506cd
7 changed files with 779 additions and 48 deletions

View File

@@ -36,6 +36,7 @@ static ssize_t (*real_read_fn) (int, void*, size_t) = NULL;
static ssize_t (*real_write_fn)(int, const void*, size_t) = NULL;
static int (*real_close_fn)(int) = NULL;
static int (*real_unlink_fn)(const char *name) = NULL;
static off_t (*real_lseek_fn)(int fd, off_t offset, int whence) = NULL;
__attribute__((constructor))
static void zvfs_preload_init(void) {
@@ -44,6 +45,7 @@ static void zvfs_preload_init(void) {
real_write_fn = dlsym(RTLD_NEXT, "write");
real_close_fn = dlsym(RTLD_NEXT, "close");
real_unlink_fn= dlsym(RTLD_NEXT, "unlink");
real_lseek_fn = dlsym(RTLD_NEXT, "lseek");
}
/* 判断路径是否由我们接管 */
@@ -173,6 +175,7 @@ static int zvfs_ensure_mounted(void) {
}
if (!zvfs_mount(g_fs)) {
zvfs_umount(g_fs);
free(g_fs);
g_fs = NULL;
return -1;
@@ -255,7 +258,12 @@ static zvfs_file_t *fd_lookup(int pseudo_fd) {
/* ------------------------------------------------------------------ */
/* POSIX hook: open */
/* ------------------------------------------------------------------ */
/**
* O_RDONLY
* O_WRONLY
* O_RDWR
* O_CREAT
*/
int open(const char *path, int flags, ...) {
if (!is_zvfs_path(path)) {
mode_t mode = 0;
@@ -469,4 +477,49 @@ int unlink(const char *name) {
}
return 0;
}
/* ------------------------------------------------------------------ */
/* POSIX hook: unlink */
/* ------------------------------------------------------------------ */
/**
* SEEK_SET
* SEEK_CUR
* SEEK_END
*/
off_t lseek(int fd, off_t offset, int whence){
if (!is_zvfs_fd(fd)) {
return real_lseek_fn(fd, offset, whence);
}
zvfs_file_t *file = fd_lookup(fd);
if (!file) { errno = EBADF; return -1; }
off_t new_offset;
uint64_t file_size = file->dirent ? file->dirent->file_size : 0;
switch (whence)
{
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = (off_t)file->current_offset + offset;
break;
case SEEK_END:
new_offset = (off_t)file_size + offset;
break;
default:
errno = EINVAL;
return -1;
}
if (new_offset < 0) {
errno = EINVAL;
return -1;
}
file->current_offset = (uint64_t)new_offset;
return new_offset;
}