This patch implements POSIX-compliant CLOCKFD support, enabling user
applications to access dynamic PTP clocks through file descriptors
combined with clock_gettime() and clock_settime() system calls.
Key changes include:
1. CLOCKFD macro support:
- Added CLOCKFD() macro in include/nuttx/clock.h
- Allows encoding file descriptor into clockid_t: CLOCKFD(fd)
- Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts)
2. clock_gettime() enhancements:
- Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids
- Extracts file descriptor from clockid using CLOCKFD_TO_FD()
- Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME
- Falls back to standard clock handling for non-CLOCKFD clockids
3. clock_settime() enhancements:
- Modified sched/clock/clock_settime.c to support CLOCKFD clockids
- Extracts file descriptor and validates accessibility
- Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device
- Maintains backward compatibility with standard POSIX clocks
4. File descriptor validation:
- Checks file descriptor validity using fs_getfilep()
- Ensures proper reference counting with fs_putfilep()
- Returns appropriate error codes (EINVAL, EBADF) on failures
5. Integration with PTP clock framework:
- Seamlessly integrates with PTP clock driver's ioctl interface
- Enables standard POSIX clock API usage for dynamic PTP clocks
- No changes required to existing applications using standard clocks
Usage example:
int fd = open("/dev/ptp0", O_RDONLY);
struct timespec ts;
clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */
clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */
close(fd);
This implementation follows the Linux kernel's PTP clock model, providing
a familiar interface for developers working with PTP hardware.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
Improve nxclock_gettime(), to get the system time excluding
the time that the system is suspended, when the clockid is
CLOCK_MONOTONIC
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
There will be a large performance loss after SCHED_CRITMONITOR is enabled.
By isolating thread running time-related functions, CPU load can be run with less overhead.
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
Signed-off-by: buxiasen <buxiasen@xiaomi.com>
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.
Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
Change the type of task group member to single list chain to
avoid accessing the memory allocator to improve the performance
Signed-off-by: chao an <anchao@lixiang.com>
Calling syslog to print logs in clock_gettime will cause the system to have recursive output, i.e., clock_gettime->sinfo->syslog->clock_gettime, with the consequences of stack overflow or non-stop log output.
here is the reason:
1.clock_systime_timespec(core function) always exist regardless the setting
2.CLOCK_MONOTONIC is a foundamental clock type required by many places
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
lc823450 smp test
* sched/clock: Replace critical section APIs with spin lock APIs in clock_gettime.c
This change will improve performance for SMP systems but nothing
changes for non-SMP systems. (Pls see include/nuttx/irq.h)
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
* sched/sched: Remove unnecessary DEBUGASSERT in sched_removereadytorun.c
In SMP mode, rtrtcb is not always at the g_readytorun.head.
This change removes DEBUGASSERT() to avoid this condition.
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
Approved-by: Gregory Nutt <gnutt@nuttx.org>
Add clock_resynchronize for better synchronization of CLOCK_REALTIME and CLOCK_MONOTONIC to match RTC after resume from low-power state.
Add up_rtc_getdatetime_with_subseconds under CONFIG_ARCH_HAVE_RTC_SUBSECONDS to allow initializing (and resynchronizing) system clock with subseconds accuracy RTC.