From 62f06d86125fedd222617170f6df4890a5b84e7e Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 18 Jul 2019 20:47:44 -0400 Subject: [PATCH] kernel/fs: Use an object_cache for the file_descriptor structs. file_descriptor structs were (following the original packagefs changes) the 4th most allocated item during the boot, with 11903 instances. These are of course all rather ephemeral, as after the boot finished there were only 70-some-odd remaining (which is surprisingly low, I though.) During heavy system use, this will of course get hit much more often. So making them object_cached for both performance and memory reasons makes a lot of sense. --- src/system/kernel/fs/fd.cpp | 7 +++++-- src/system/kernel/fs/vfs.cpp | 11 +++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/system/kernel/fs/fd.cpp b/src/system/kernel/fs/fd.cpp index cc9d7f7fb4..61ae1099a5 100644 --- a/src/system/kernel/fs/fd.cpp +++ b/src/system/kernel/fs/fd.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,8 @@ static const size_t kMaxReadDirBufferSize = 64 * 1024; +extern object_cache* sFileDescriptorCache; + static struct file_descriptor* get_fd_locked(struct io_context* context, int fd); @@ -117,7 +120,7 @@ struct file_descriptor* alloc_fd(void) { file_descriptor* descriptor - = (file_descriptor*)malloc(sizeof(struct file_descriptor)); + = (file_descriptor*)object_cache_alloc(sFileDescriptorCache, 0); if (descriptor == NULL) return NULL; @@ -214,7 +217,7 @@ put_fd(struct file_descriptor* descriptor) if (descriptor->ops != NULL && descriptor->ops->fd_free != NULL) descriptor->ops->fd_free(descriptor); - free(descriptor); + object_cache_free(sFileDescriptorCache, descriptor, 0); } else if ((descriptor->open_mode & O_DISCONNECTED) != 0 && previous - 1 == descriptor->open_count && descriptor->ops != NULL) { diff --git a/src/system/kernel/fs/vfs.cpp b/src/system/kernel/fs/vfs.cpp index 5b0005c453..a26ae7775b 100644 --- a/src/system/kernel/fs/vfs.cpp +++ b/src/system/kernel/fs/vfs.cpp @@ -109,7 +109,6 @@ #endif -object_cache* sPathNameCache; const static size_t kMaxPathLength = 65536; // The absolute maximum path length (for getcwd() - this is not depending // on PATH_MAX @@ -326,6 +325,9 @@ typedef BOpenHashTable MountTable; } // namespace +object_cache* sPathNameCache; +object_cache* sFileDescriptorCache; + #define VNODE_HASH_TABLE_SIZE 1024 static VnodeTable* sVnodeTable; static struct vnode* sRoot; @@ -5344,11 +5346,16 @@ vfs_init(kernel_args* args) || sMountsTable->Init(MOUNTS_HASH_TABLE_SIZE) != B_OK) panic("vfs_init: error creating mounts hash table\n"); - sPathNameCache = create_object_cache("path names", + sPathNameCache = create_object_cache("vfs path names", B_PATH_NAME_LENGTH + 1, 8, NULL, NULL, NULL); if (sPathNameCache == NULL) panic("vfs_init: error creating path name object_cache\n"); + sFileDescriptorCache = create_object_cache("vfs fds", + sizeof(file_descriptor), 8, NULL, NULL, NULL); + if (sPathNameCache == NULL) + panic("vfs_init: error creating file descriptor object_cache\n"); + node_monitor_init(); sRoot = NULL;