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.
This commit is contained in:
Augustin Cavalier 2019-07-18 20:47:44 -04:00
parent 1f39d6dd11
commit 62f06d8612
2 changed files with 14 additions and 4 deletions

View File

@ -20,6 +20,7 @@
#include <syscalls.h>
#include <syscall_restart.h>
#include <slab/Slab.h>
#include <util/AutoLock.h>
#include <vfs.h>
#include <wait_for_objects.h>
@ -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) {

View File

@ -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<MountHash> 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;