Skip to content

sched/clock: Add clock_increase_sched_ticks() and clock_get_sched_ticks()#17192

Closed
wangchdo wants to merge 4 commits into
apache:masterfrom
wangchdo:add_sched_ticks
Closed

sched/clock: Add clock_increase_sched_ticks() and clock_get_sched_ticks()#17192
wangchdo wants to merge 4 commits into
apache:masterfrom
wangchdo:add_sched_ticks

Conversation

@wangchdo
Copy link
Copy Markdown
Contributor

@wangchdo wangchdo commented Oct 13, 2025

In the current NuttX kernel implementation, there is no function
to represent pure scheduler ticks.
The existing clock_systime_ticks() cannot serve this purpose,
because it keeps increasing even when the scheduler is not running
(when interrupt is blocked for a long time for example).
This happens since the returned value reflects real hardware time:
as long as the timer or alarm hardware is active,
clock_systime_ticks() will continuously increase.

This patch introduces two new functions that provide access to
scheduler ticks — a counter that only increases when the scheduler
itself is running.

Note: Please adhere to Contributing Guidelines.

Summary

add two new functions to maintain a pure scheduler related ticks in nuttx

Impact

new functions added, now impact to the existing nuttx functions

Testing

ostest passed on board a2g-tc397-5v-tft

image

ostest passed on board fvp-armv8r-aarch32

image

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Oct 13, 2025
@xiaoxiang781216
Copy link
Copy Markdown
Contributor

@wangchdo should we update CLOCK_MONOTONIC or CLOCK_BOOTTIME too?

@wangchdo
Copy link
Copy Markdown
Contributor Author

@wangchdo should we update CLOCK_MONOTONIC or CLOCK_BOOTTIME too?

I added update for clock_systime_timespec() and clock_resynchronize() please check.

@xiaoxiang781216
Copy link
Copy Markdown
Contributor

xiaoxiang781216 commented Oct 13, 2025

@wangchdo should we update CLOCK_MONOTONIC or CLOCK_BOOTTIME too?

I added update for clock_systime_timespec() and clock_resynchronize() please check.

from https://man7.org/linux/man-pages/man3/clock_gettime.3.html:

CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific)
              A nonsettable system-wide clock that is identical to
              CLOCK_MONOTONIC, except that it also includes any time that
              the system is suspended.  This allows applications to get a
              suspend-aware monotonic clock without having to deal with the
              complications of CLOCK_REALTIME, which may have discontinu‐
              ities if the time is changed using [settimeofday(2)](https://man7.org/linux/man-pages/man2/settimeofday.2.html) or similar.

CLOCK_BOOTTIME contain the suspend time, but CLOCK_MONOTONIC not. Since you add the new api to distinguish these two cases, we could implement CLOCK_MONOTONIC better now.

@wangchdo
Copy link
Copy Markdown
Contributor Author

@wangchdo should we update CLOCK_MONOTONIC or CLOCK_BOOTTIME too?

I added update for clock_systime_timespec() and clock_resynchronize() please check.

from https://man7.org/linux/man-pages/man3/clock_gettime.3.html:

CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific)
              A nonsettable system-wide clock that is identical to
              CLOCK_MONOTONIC, except that it also includes any time that
              the system is suspended.  This allows applications to get a
              suspend-aware monotonic clock without having to deal with the
              complications of CLOCK_REALTIME, which may have discontinu‐
              ities if the time is changed using [settimeofday(2)](https://man7.org/linux/man-pages/man2/settimeofday.2.html) or similar.

CLOCK_BOOTTIME contain the suspend time, but CLOCK_MONOTONIC not. Since you add the new api to distinguish these two cases, we could implement CLOCK_MONOTONIC better now.

Hi @xiaoxiang781216

I uploaded a new commit to improve CLOCK_MONOTONIC, please help to check.

@wangchdo wangchdo force-pushed the add_sched_ticks branch 3 times, most recently from 34ce476 to 892eeb5 Compare October 13, 2025 12:57
*
****************************************************************************/

clock_t clock_get_sched_ticks(void)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we worried only about the 64 and 32 bit architectures?
What about 16 and 8 bit supported architectures? It seems that this changes will affect all architectures if I am not mistaken.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! NuttX still needs to support 8, 16, 32 and 64-bit arch

Copy link
Copy Markdown
Contributor Author

@wangchdo wangchdo Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @acassis @hartmannathan

nuttx cloct_t now is uint32_t or uint64_t as defined in sys/types.h

image

Copy link
Copy Markdown
Contributor Author

@wangchdo wangchdo Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like nuttx clock module does not consider systems below 32-bit, the new function I added clock_get_sched_ticks() is in align with the current clock module implementation, below is the implementation before. @xiaoxiang781216 How do you think about this?

image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NuttX require the arch support at least 32bit int, and 64bit is optional, but the recent discuss suggesting to enforce 64bit for POSIX compliant. Please reference the discuss here: #14460

Copy link
Copy Markdown
Contributor

@hartmannathan hartmannathan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the CI test failing because of lines like this:

====================================================================================
Configuration/Tool: sam4l-xplained/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2025-10-13 16:53:05
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_BUILDROOT_OABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
  [1/1] Normalize sam4l-xplained/nsh
/usr/bin/bash: line 1: arm-nuttx-elf-gcc: command not found
/usr/bin/bash: line 1: arm-nuttx-elf-gcc: command not found

(Maybe the script should be updated to output an actual Error: line that will be highlighted in the GitHub CI web interface?)

…ks()

    In the current NuttX kernel implementation, there is no function
    to represent pure scheduler ticks.
    The existing clock_systime_ticks() cannot serve this purpose,
    because it keeps increasing even when the scheduler is not running.
    This happens since the returned value reflects real hardware time:
    as long as the timer or alarm hardware is active,
    clock_systime_ticks() will continuously increase.

    This patch introduces two new functions that provide access to
    scheduler ticks — a counter that only increases when the scheduler
    itself is running.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
   Replace direct usage of g_system_ticks with clock_increase_sched_ticks()
   and clock_get_sched_ticks()

Signed-off-by: Chengdong Wang wangchengdong@lixiang.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
@wangchdo
Copy link
Copy Markdown
Contributor Author

Is the CI test failing because of lines like this:

====================================================================================
Configuration/Tool: sam4l-xplained/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2025-10-13 16:53:05
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_BUILDROOT_OABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
  [1/1] Normalize sam4l-xplained/nsh
/usr/bin/bash: line 1: arm-nuttx-elf-gcc: command not found
/usr/bin/bash: line 1: arm-nuttx-elf-gcc: command not found

(Maybe the script should be updated to output an actual Error: line that will be highlighted in the GitHub CI web interface?)

Hi @hartmannathan,

Thank you for catching that.
I'm not very familiar with CI, but I think the build failure is unrelated to my PR.

@hartmannathan
Copy link
Copy Markdown
Contributor

Is the CI test failing because of lines like this:

====================================================================================

Configuration/Tool: sam4l-xplained/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI

2025-10-13 16:53:05


Cleaning...

Configuring...

Disabling CONFIG_ARM_TOOLCHAIN_BUILDROOT_OABI

Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI

Building NuttX...

[1/1] Normalize sam4l-xplained/nsh

/usr/bin/bash: line 1: arm-nuttx-elf-gcc: command not found

/usr/bin/bash: line 1: arm-nuttx-elf-gcc: command not found

(Maybe the script should be updated to output an actual Error: line that will be highlighted in the GitHub CI web interface?)

Hi @hartmannathan,

Thank you for catching that.

I'm not very familiar with CI, but I think the build failure is unrelated to my PR.

I think you are correct: it seems the CI is missing the compiler for some platforms, but I don't know how this can be. I am out of time for today but hopefully someone who understands the CI better can help.

    Enlarge kflash memory size for the sam3u-ek board
    to resolve the build error.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
@wangchdo wangchdo requested a review from anchao as a code owner October 14, 2025 04:48
Copy link
Copy Markdown
Contributor

@jerpelea jerpelea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to the datasheet the flash size is 256k
https://ww1.microchip.com/downloads/en/DeviceDoc/doc6478.pdf

@wangchdo
Copy link
Copy Markdown
Contributor Author

wangchdo commented Oct 14, 2025

according to the datasheet the flash size is 256k https://ww1.microchip.com/downloads/en/DeviceDoc/doc6478.pdf

Hi @jerpelea,

The flash memory is divided into three regions. In this update, I have enlarged the kflash region and reduced the uflash region accordingly to fix the build issue. The total flash size remains unchanged as 256k .

Before:
  kflash (rx)  : ORIGIN = 0x00080000, LENGTH = 64K
  uflash (rx)  : ORIGIN = 0x00090000, LENGTH = 64K
  xflash (rx)  : ORIGIN = 0x000a0000, LENGTH = 128K

After:
  kflash (rx)  : ORIGIN = 0x00080000, LENGTH = 65K
  uflash (rx)  : ORIGIN = 0x00090400, LENGTH = 63K
  xflash (rx)  : ORIGIN = 0x000a0000, LENGTH = 128K

@wangchdo wangchdo requested a review from jerpelea October 14, 2025 09:33
@wangchdo
Copy link
Copy Markdown
Contributor Author

wangchdo commented Oct 17, 2025

according to the datasheet the flash size is 256k https://ww1.microchip.com/downloads/en/DeviceDoc/doc6478.pdf

Hi @jerpelea,

The flash memory is divided into three regions. In this update, I have enlarged the kflash region and reduced the uflash region accordingly to fix the build issue. The total flash size remains unchanged as 256k .

Before:
  kflash (rx)  : ORIGIN = 0x00080000, LENGTH = 64K
  uflash (rx)  : ORIGIN = 0x00090000, LENGTH = 64K
  xflash (rx)  : ORIGIN = 0x000a0000, LENGTH = 128K

After:
  kflash (rx)  : ORIGIN = 0x00080000, LENGTH = 65K
  uflash (rx)  : ORIGIN = 0x00090400, LENGTH = 63K
  xflash (rx)  : ORIGIN = 0x000a0000, LENGTH = 128K

Hi @jerpelea

Do you have any further comments?

@wangchdo
Copy link
Copy Markdown
Contributor Author

This PR duplicates PR #17199
. Closing it now.

@wangchdo wangchdo closed this Oct 20, 2025
@wangchdo wangchdo deleted the add_sched_ticks branch October 20, 2025 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Board: arm Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants