mirror of
https://github.com/apache/nuttx.git
synced 2026-05-30 13:27:01 +08:00
fs: Add Kernel-level VFS Performance Profiler
Build Documentation / build-html (push) Has been cancelled
Build Documentation / build-html (push) Has been cancelled
This adds a kernel-level performance profiler for the VFS. By enabling CONFIG_FS_PROFILER, the core VFS system calls (file_read, file_write, file_open, and file_close) are instrumented to track high-resolution execution times using clock_systime_timespec() seamlessly. The collected statistics are exposed dynamically via a new procfs node at /proc/fs/profile, allowing CI regression testing without needing external debugging tools. Signed-off-by: Sumit6307 <sumitkesar6307@gmail.com>
This commit is contained in:
@@ -543,6 +543,7 @@ NuttX provides support for a variety of file systems out of the box.
|
||||
nxffs.rst
|
||||
partition.rst
|
||||
procfs.rst
|
||||
profiler.rst
|
||||
romfs.rst
|
||||
rpmsgfs.rst
|
||||
smartfs.rst
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
==============================
|
||||
VFS Performance Profiler
|
||||
==============================
|
||||
|
||||
The Virtual File System (VFS) Performance Profiler provides a simple, in-kernel
|
||||
mechanism to track execution times and invocation counts for core VFS operations
|
||||
(read, write, open, close) seamlessly. This is highly suitable for
|
||||
CI/CD automated regression testing and performance bottleneck identification.
|
||||
|
||||
Configuration
|
||||
=============
|
||||
|
||||
To enable the profiler, select ``CONFIG_FS_PROFILER`` in your Kconfig.
|
||||
To expose the metrics dynamically via procfs, ensure ``CONFIG_FS_PROCFS`` is enabled, and
|
||||
the profiler node is included via ``CONFIG_FS_PROCFS_PROFILER``.
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
When enabled, the profiler automatically intercepts calls to the underlying
|
||||
inode operations and records the execution elapsed times using ``perf_gettime()``.
|
||||
Since no blocking mutexes are used during updates (fast ``atomic.h`` operations
|
||||
are utilized instead), the overhead is extremely minimal and safely scales on SMP.
|
||||
|
||||
To view the current statistics collectively from the NuttShell (NSH), simply
|
||||
read the node:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
nsh> cat /proc/fs/profile
|
||||
VFS Performance Profile:
|
||||
Reads: 12 (Total time: 4500120 ns)
|
||||
Writes: 3 (Total time: 95050 ns)
|
||||
Opens: 15 (Total time: 1005000 ns)
|
||||
Closes: 15 (Total time: 45000 ns)
|
||||
|
||||
The reported times are in the raw ticks/units provided by ``perf_gettime()`` on
|
||||
your specific architecture.
|
||||
@@ -55,6 +55,8 @@ CONFIG_FS_FAT=y
|
||||
CONFIG_FS_HOSTFS=y
|
||||
CONFIG_FS_NAMED_SEMAPHORES=y
|
||||
CONFIG_FS_PROCFS=y
|
||||
CONFIG_FS_PROCFS_PROFILER=y
|
||||
CONFIG_FS_PROFILER=y
|
||||
CONFIG_FS_RAMMAP=y
|
||||
CONFIG_FS_ROMFS=y
|
||||
CONFIG_FS_SHMFS=y
|
||||
|
||||
@@ -5,6 +5,14 @@
|
||||
|
||||
comment "File system configuration"
|
||||
|
||||
config FS_PROFILER
|
||||
bool "VFS Performance Profiler"
|
||||
default n
|
||||
---help---
|
||||
Enable nanosecond/microsecond-level profiling for the Virtual File
|
||||
System (VFS) operations (open, close, read, write). The profile stats
|
||||
can be read via /proc/fs/profile if PROCFS is enabled.
|
||||
|
||||
config DISABLE_MOUNTPOINT
|
||||
bool "Disable support for mount points"
|
||||
default n
|
||||
|
||||
@@ -40,6 +40,10 @@ if(NOT CONFIG_DISABLE_MOUNTPOINT)
|
||||
list(APPEND SRCS fs_procfspressure.c)
|
||||
endif()
|
||||
|
||||
if(CONFIG_FS_PROCFS_PROFILER)
|
||||
list(APPEND SRCS fs_procfsprofile.c)
|
||||
endif()
|
||||
|
||||
target_sources(fs PRIVATE ${SRCS})
|
||||
|
||||
endif()
|
||||
|
||||
@@ -148,6 +148,16 @@ config FS_PROCFS_EXCLUDE_VERSION
|
||||
bool "Exclude version"
|
||||
default DEFAULT_SMALL
|
||||
|
||||
config FS_PROCFS_PROFILER
|
||||
bool "Include fs/profile information"
|
||||
depends on FS_PROFILER && !DEFAULT_SMALL
|
||||
default n
|
||||
---help---
|
||||
Enable the VFS performance profiler procfs node at /proc/fs/profile.
|
||||
This node provides real-time dynamic statistics (call counts, elapsed
|
||||
time) for core VFS operations (read, write, open, close) to help
|
||||
identify filesystem performance bottlenecks and regressions.
|
||||
|
||||
config FS_PROCFS_INCLUDE_PRESSURE
|
||||
bool "Include memory pressure notification"
|
||||
default n
|
||||
|
||||
@@ -32,6 +32,10 @@ ifeq ($(CONFIG_FS_PROCFS_INCLUDE_PRESSURE),y)
|
||||
CSRCS += fs_procfspressure.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FS_PROCFS_PROFILER),y)
|
||||
CSRCS += fs_procfsprofile.c
|
||||
endif
|
||||
|
||||
# Include procfs build support
|
||||
|
||||
DEPPATH += --dep-path procfs
|
||||
|
||||
@@ -74,6 +74,9 @@ extern const struct procfs_operations g_thermal_operations;
|
||||
extern const struct procfs_operations g_uptime_operations;
|
||||
extern const struct procfs_operations g_version_operations;
|
||||
extern const struct procfs_operations g_pressure_operations;
|
||||
#if defined(CONFIG_FS_PROFILER) && defined(CONFIG_FS_PROCFS_PROFILER)
|
||||
extern const struct procfs_operations g_fsprofile_operations;
|
||||
#endif
|
||||
|
||||
/* This is not good. These are implemented in other sub-systems. Having to
|
||||
* deal with them here is not a good coupling. What is really needed is a
|
||||
@@ -208,6 +211,9 @@ static const struct procfs_entry_s g_procfs_entries[] =
|
||||
#ifndef CONFIG_FS_PROCFS_EXCLUDE_VERSION
|
||||
{ "version", &g_version_operations, PROCFS_FILE_TYPE },
|
||||
#endif
|
||||
#if defined(CONFIG_FS_PROFILER) && defined(CONFIG_FS_PROCFS_PROFILER)
|
||||
{ "profile", &g_fsprofile_operations, PROCFS_FILE_TYPE },
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef CONFIG_FS_PROCFS_REGISTER
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/****************************************************************************
|
||||
* fs/procfs/fs_procfsprofile.c
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#if defined(CONFIG_FS_PROCFS) && defined(CONFIG_FS_PROFILER) && \
|
||||
defined(CONFIG_FS_PROCFS_PROFILER)
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
#include <nuttx/fs/procfs.h>
|
||||
#include "../vfs/vfs.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static int profile_open(FAR struct file *filep, FAR const char *relpath,
|
||||
int oflags, mode_t mode)
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int profile_close(FAR struct file *filep)
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
|
||||
static ssize_t profile_read(FAR struct file *filep, FAR char *buffer,
|
||||
size_t buflen)
|
||||
{
|
||||
char buf[256];
|
||||
size_t linesize;
|
||||
|
||||
procfs_snprintf(buf, sizeof(buf),
|
||||
"FS Performance Profile:\n"
|
||||
" Reads: %10" PRId32 " (Total time: %" PRId64 " ns)\n"
|
||||
" Writes: %10" PRId32 " (Total time: %" PRId64 " ns)\n"
|
||||
" Opens: %10" PRId32 " (Total time: %" PRId64 " ns)\n"
|
||||
" Closes: %10" PRId32 " (Total time: %" PRId64 " ns)\n",
|
||||
atomic_read(&g_fs_profile.reads),
|
||||
atomic64_read(&g_fs_profile.total_read_time),
|
||||
atomic_read(&g_fs_profile.writes),
|
||||
atomic64_read(&g_fs_profile.total_write_time),
|
||||
atomic_read(&g_fs_profile.opens),
|
||||
atomic64_read(&g_fs_profile.total_open_time),
|
||||
atomic_read(&g_fs_profile.closes),
|
||||
atomic64_read(&g_fs_profile.total_close_time));
|
||||
|
||||
linesize = strlen(buf);
|
||||
return procfs_memcpy(buf, linesize, buffer, buflen, &filep->f_pos);
|
||||
}
|
||||
|
||||
static int profile_dup(FAR const struct file *oldp, FAR struct file *newp)
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int profile_stat(FAR const char *relpath, FAR struct stat *buf)
|
||||
{
|
||||
memset(buf, 0, sizeof(struct stat));
|
||||
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
const struct procfs_operations g_fsprofile_operations =
|
||||
{
|
||||
profile_open, /* open */
|
||||
profile_close, /* close */
|
||||
profile_read, /* read */
|
||||
NULL, /* write */
|
||||
NULL, /* poll */
|
||||
profile_dup, /* dup */
|
||||
NULL, /* opendir */
|
||||
NULL, /* closedir */
|
||||
NULL, /* readdir */
|
||||
NULL, /* rewinddir */
|
||||
profile_stat /* stat */
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -93,4 +93,10 @@ if(CONFIG_SIGNAL_FD)
|
||||
list(APPEND SRCS fs_signalfd.c)
|
||||
endif()
|
||||
|
||||
# Support for profiler
|
||||
|
||||
if(CONFIG_FS_PROFILER)
|
||||
list(APPEND SRCS fs_profile.c)
|
||||
endif()
|
||||
|
||||
target_sources(fs PRIVATE ${SRCS})
|
||||
|
||||
@@ -65,6 +65,10 @@ ifeq ($(CONFIG_SIGNAL_FD),y)
|
||||
CSRCS += fs_signalfd.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FS_PROFILER),y)
|
||||
CSRCS += fs_profile.c
|
||||
endif
|
||||
|
||||
# Include vfs build support
|
||||
|
||||
DEPPATH += --dep-path vfs
|
||||
|
||||
@@ -119,9 +119,16 @@ int file_close(FAR struct file *filep)
|
||||
|
||||
if (inode->u.i_ops && inode->u.i_ops->close)
|
||||
{
|
||||
clock_t start_time;
|
||||
|
||||
FS_PROFILE_START(start_time);
|
||||
|
||||
/* Perform the close operation */
|
||||
|
||||
ret = inode->u.i_ops->close(filep);
|
||||
|
||||
FS_PROFILE_STOP(start_time, g_fs_profile.total_close_time,
|
||||
g_fs_profile.closes);
|
||||
}
|
||||
|
||||
/* And release the inode */
|
||||
|
||||
@@ -236,6 +236,10 @@ static int file_vopen(FAR struct file *filep, FAR const char *path,
|
||||
* because it may also be closed that many times.
|
||||
*/
|
||||
|
||||
clock_t start_time;
|
||||
|
||||
FS_PROFILE_START(start_time);
|
||||
|
||||
if (oflags & O_DIRECTORY)
|
||||
{
|
||||
ret = dir_allocate(filep, desc.relpath);
|
||||
@@ -261,6 +265,9 @@ static int file_vopen(FAR struct file *filep, FAR const char *path,
|
||||
ret = -ENXIO;
|
||||
}
|
||||
|
||||
FS_PROFILE_STOP(start_time, g_fs_profile.total_open_time,
|
||||
g_fs_profile.opens);
|
||||
|
||||
if (ret == -EISDIR && ((oflags & O_WRONLY) == 0))
|
||||
{
|
||||
ret = dir_allocate(filep, desc.relpath);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/****************************************************************************
|
||||
* fs/vfs/fs_profile.c
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/clock.h>
|
||||
#include <nuttx/atomic.h>
|
||||
#include "vfs.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
struct fs_profile_s g_fs_profile;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void fs_profile_start(FAR clock_t *start)
|
||||
{
|
||||
*start = perf_gettime();
|
||||
}
|
||||
|
||||
void fs_profile_stop(FAR clock_t *start, FAR atomic64_t *total,
|
||||
FAR atomic_t *count)
|
||||
{
|
||||
clock_t stop = perf_gettime();
|
||||
clock_t delta = stop - *start;
|
||||
|
||||
atomic64_fetch_add(total, delta);
|
||||
atomic_fetch_add(count, 1);
|
||||
}
|
||||
@@ -206,6 +206,10 @@ ssize_t file_readv(FAR struct file *filep,
|
||||
|
||||
else if (inode != NULL && inode->u.i_ops)
|
||||
{
|
||||
clock_t start_time;
|
||||
|
||||
FS_PROFILE_START(start_time);
|
||||
|
||||
if (inode->u.i_ops->readv)
|
||||
{
|
||||
struct uio uio;
|
||||
@@ -220,6 +224,9 @@ ssize_t file_readv(FAR struct file *filep,
|
||||
{
|
||||
ret = file_readv_compat(filep, iov, iovcnt);
|
||||
}
|
||||
|
||||
FS_PROFILE_STOP(start_time, g_fs_profile.total_read_time,
|
||||
g_fs_profile.reads);
|
||||
}
|
||||
|
||||
/* Return the number of bytes read (or possibly an error code) */
|
||||
|
||||
@@ -188,6 +188,10 @@ ssize_t file_writev(FAR struct file *filep,
|
||||
inode = filep->f_inode;
|
||||
if (inode != NULL && inode->u.i_ops)
|
||||
{
|
||||
clock_t start_time;
|
||||
|
||||
FS_PROFILE_START(start_time);
|
||||
|
||||
if (inode->u.i_ops->writev)
|
||||
{
|
||||
struct uio uio;
|
||||
@@ -202,6 +206,9 @@ ssize_t file_writev(FAR struct file *filep,
|
||||
{
|
||||
ret = file_writev_compat(filep, iov, iovcnt);
|
||||
}
|
||||
|
||||
FS_PROFILE_STOP(start_time, g_fs_profile.total_write_time,
|
||||
g_fs_profile.writes);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_FS_NOTIFY
|
||||
|
||||
@@ -30,6 +30,11 @@
|
||||
#include <nuttx/fs/fs.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifdef CONFIG_FS_PROFILER
|
||||
#include <nuttx/clock.h>
|
||||
#include <nuttx/atomic.h>
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
@@ -123,4 +128,35 @@ void notify_rename(FAR const char *oldpath, bool oldisdir,
|
||||
void notify_initialize(void);
|
||||
#endif /* CONFIG_FS_NOTIFY */
|
||||
|
||||
#ifdef CONFIG_FS_PROFILER
|
||||
|
||||
struct fs_profile_s
|
||||
{
|
||||
atomic_t reads;
|
||||
atomic_t writes;
|
||||
atomic_t opens;
|
||||
atomic_t closes;
|
||||
atomic64_t total_read_time;
|
||||
atomic64_t total_write_time;
|
||||
atomic64_t total_open_time;
|
||||
atomic64_t total_close_time;
|
||||
};
|
||||
|
||||
extern struct fs_profile_s g_fs_profile;
|
||||
|
||||
void fs_profile_start(FAR clock_t *start);
|
||||
void fs_profile_stop(FAR clock_t *start, FAR atomic64_t *total,
|
||||
FAR atomic_t *count);
|
||||
|
||||
#define FS_PROFILE_START(start_time) fs_profile_start(&start_time)
|
||||
#define FS_PROFILE_STOP(start_time, total_time, count) \
|
||||
fs_profile_stop(&start_time, &total_time, &count)
|
||||
|
||||
#else
|
||||
|
||||
#define FS_PROFILE_START(start_time) ((void)(start_time))
|
||||
#define FS_PROFILE_STOP(start_time, total_time, count) ((void)(start_time))
|
||||
|
||||
#endif /* CONFIG_FS_PROFILER */
|
||||
|
||||
#endif /* __FS_VFS_VFS_H */
|
||||
|
||||
Reference in New Issue
Block a user