Skip to content

Commit

Permalink
kernel/fs: Use an object_cache for the file_descriptor structs.
Browse files Browse the repository at this point in the history
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 62f06d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/system/kernel/fs/fd.cpp
Expand Up @@ -20,6 +20,7 @@


#include <syscalls.h> #include <syscalls.h>
#include <syscall_restart.h> #include <syscall_restart.h>
#include <slab/Slab.h>
#include <util/AutoLock.h> #include <util/AutoLock.h>
#include <vfs.h> #include <vfs.h>
#include <wait_for_objects.h> #include <wait_for_objects.h>
Expand All @@ -37,6 +38,8 @@


static const size_t kMaxReadDirBufferSize = 64 * 1024; static const size_t kMaxReadDirBufferSize = 64 * 1024;


extern object_cache* sFileDescriptorCache;



static struct file_descriptor* get_fd_locked(struct io_context* context, static struct file_descriptor* get_fd_locked(struct io_context* context,
int fd); int fd);
Expand Down Expand Up @@ -117,7 +120,7 @@ struct file_descriptor*
alloc_fd(void) alloc_fd(void)
{ {
file_descriptor* descriptor file_descriptor* descriptor
= (file_descriptor*)malloc(sizeof(struct file_descriptor)); = (file_descriptor*)object_cache_alloc(sFileDescriptorCache, 0);
if (descriptor == NULL) if (descriptor == NULL)
return NULL; return NULL;


Expand Down Expand Up @@ -214,7 +217,7 @@ put_fd(struct file_descriptor* descriptor)
if (descriptor->ops != NULL && descriptor->ops->fd_free != NULL) if (descriptor->ops != NULL && descriptor->ops->fd_free != NULL)
descriptor->ops->fd_free(descriptor); descriptor->ops->fd_free(descriptor);


free(descriptor); object_cache_free(sFileDescriptorCache, descriptor, 0);
} else if ((descriptor->open_mode & O_DISCONNECTED) != 0 } else if ((descriptor->open_mode & O_DISCONNECTED) != 0
&& previous - 1 == descriptor->open_count && previous - 1 == descriptor->open_count
&& descriptor->ops != NULL) { && descriptor->ops != NULL) {
Expand Down
11 changes: 9 additions & 2 deletions src/system/kernel/fs/vfs.cpp
Expand Up @@ -109,7 +109,6 @@
#endif #endif




object_cache* sPathNameCache;
const static size_t kMaxPathLength = 65536; const static size_t kMaxPathLength = 65536;
// The absolute maximum path length (for getcwd() - this is not depending // The absolute maximum path length (for getcwd() - this is not depending
// on PATH_MAX // on PATH_MAX
Expand Down Expand Up @@ -326,6 +325,9 @@ typedef BOpenHashTable<MountHash> MountTable;
} // namespace } // namespace




object_cache* sPathNameCache;
object_cache* sFileDescriptorCache;

#define VNODE_HASH_TABLE_SIZE 1024 #define VNODE_HASH_TABLE_SIZE 1024
static VnodeTable* sVnodeTable; static VnodeTable* sVnodeTable;
static struct vnode* sRoot; static struct vnode* sRoot;
Expand Down Expand Up @@ -5344,11 +5346,16 @@ vfs_init(kernel_args* args)
|| sMountsTable->Init(MOUNTS_HASH_TABLE_SIZE) != B_OK) || sMountsTable->Init(MOUNTS_HASH_TABLE_SIZE) != B_OK)
panic("vfs_init: error creating mounts hash table\n"); 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); B_PATH_NAME_LENGTH + 1, 8, NULL, NULL, NULL);
if (sPathNameCache == NULL) if (sPathNameCache == NULL)
panic("vfs_init: error creating path name object_cache\n"); 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(); node_monitor_init();


sRoot = NULL; sRoot = NULL;
Expand Down

0 comments on commit 62f06d8

Please sign in to comment.