Skip to content
Permalink
Browse files

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.
  • Loading branch information...
waddlesplash committed Jul 19, 2019
1 parent 1f39d6d commit 62f06d86125fedd222617170f6df4890a5b84e7e
Showing with 14 additions and 4 deletions.
  1. +5 −2 src/system/kernel/fs/fd.cpp
  2. +9 −2 src/system/kernel/fs/vfs.cpp
@@ -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) {
@@ -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;

0 comments on commit 62f06d8

Please sign in to comment.
You can’t perform that action at this time.