sched/clock: support using CLOCKFD to call clock_getres

This patch extends CLOCKFD support to clock_getres(), allowing
applications to query the resolution of PTP clocks through file
descriptors using the standard POSIX clock API.

Key changes include:

1. Move clock_getres() from libc to kernel:
   - Relocated from libs/libc/sched/clock_getres.c to sched/clock/
   - Enables direct kernel-level file descriptor handling
   - Maintains POSIX compliance while adding CLOCKFD support

2. CLOCKFD support in clock_getres():
   - Detects CLOCKFD-encoded clockids using CLOCKFD_TO_FD() check
   - Extracts file descriptor from clockid parameter
   - Validates file descriptor through fs_getfilep()
   - Issues PTP_CLOCK_GETRES ioctl to query clock resolution

3. PTP clock resolution query:
   - Retrieves resolution from PTP clock device via ioctl
   - Returns nanosecond-precision timing resolution
   - Enables applications to determine clock accuracy capabilities
   - Falls back to standard clock resolution for non-CLOCKFD clockids

4. Error handling:
   - Returns EINVAL for invalid CLOCKFD file descriptors
   - Properly manages file reference counting with fs_putfilep()
   - Maintains backward compatibility with existing clock types

5. Build system updates:
   - Updated libs/libc/sched/CMakeLists.txt and Make.defs
   - Updated sched/clock/CMakeLists.txt to include clock_getres.c
   - Ensures proper compilation in kernel context

Usage example:
  int fd = open("/dev/ptp0", O_RDONLY);
  struct timespec res;
  clock_getres(CLOCKFD(fd), &res);  /* Query PTP clock resolution */
  printf("Resolution: %ld.%09ld sec\n", res.tv_sec, res.tv_nsec);
  close(fd);

This completes the basic POSIX clock API support for dynamic PTP clocks,
providing applications with comprehensive clock information access.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1
2025-03-20 22:46:44 +08:00
committed by Alan C. Assis
parent 1c643b5cc3
commit 6802d3510b
5 changed files with 23 additions and 6 deletions
-1
View File
@@ -24,7 +24,6 @@ set(SRCS
sched_getprioritymax.c
sched_getprioritymin.c
clock_getcpuclockid.c
clock_getres.c
task_cancelpt.c
task_setcancelstate.c
task_setcanceltype.c
+1 -1
View File
@@ -23,9 +23,9 @@
# Add the sched C files to the build
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
CSRCS += clock_getcpuclockid.c clock_getres.c
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
CSRCS += task_testcancel.c task_gettid.c
CSRCS += clock_getcpuclockid.c
ifeq ($(CONFIG_SMP),y)
CSRCS += sched_cpucount.c
+1
View File
@@ -23,6 +23,7 @@
set(SRCS
clock.c
clock_initialize.c
clock_getres.c
clock_settime.c
clock_gettime.c
clock_realtime2absticks.c
+1 -1
View File
@@ -22,7 +22,7 @@
CSRCS += clock.c clock_initialize.c clock_settime.c clock_gettime.c
CSRCS += clock_systime_ticks.c clock_systime_timespec.c clock_sched_ticks.c
CSRCS += clock_perf.c clock_realtime2absticks.c
CSRCS += clock_perf.c clock_realtime2absticks.c clock_getres.c
# Unless a driver with a more accurate version of up_*delay is enabled, always
# include the base delay definition (busy-loop) as a minimum-viable product. We
@@ -1,5 +1,5 @@
/****************************************************************************
* libs/libc/sched/clock_getres.c
* sched/clock/clock_getres.c
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -24,14 +24,14 @@
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/clock.h>
#include <nuttx/fs/fs.h>
#include <nuttx/timers/ptp_clock.h>
/****************************************************************************
* Public Functions
@@ -74,6 +74,23 @@ int clock_getres(clockid_t clock_id, struct timespec *res)
sinfo("Returning res=(%d,%d)\n", (int)res->tv_sec,
(int)res->tv_nsec);
break;
#ifdef CONFIG_PTP_CLOCK
case CLOCK_FD:
{
FAR struct file *filep;
ret = ptp_clockid_to_filep(clock_id, &filep);
if (ret < 0)
{
return ret;
}
ret = file_ioctl(filep, PTP_CLOCK_GETRES, res);
fs_putfilep(filep);
}
break;
#endif
}
return ret;