Skip to content

[FEATURE] "portable" implementation for POSIX's uint64_t time. #18849

Open

Description

@cederom

Is your feature request related to a problem? Please describe.

  • We are moving to POSIX compliant time representation (The Open Group Base Specifications Issue 8 IEEE Std 1003.1-2024 [1][2][3]) with uint64_t.
  • POSIX sys/types.h [3] defines:

    time_t as "Used for time in seconds"
    time_t shall be an integer type with a width (see <stdint.h>) of at least 64 bits.

  • What it solves:
    • year 2038 problem.
    • 32-bit system clock wrapped in ~497 days and almost every active board already enabled it.
    • runtime time overflow calculation errors after some weeks.
  • What it breaks:
    • 8/16/32-bit platforms are most likely impacted by increased code size (i.e. ~2KB) when long long type is not natively supported.
    • time calculation will be slower on older platforms.
    • non-atomic time access (needs some additional protection).

This is a long standing challenge discussed on our mailing list [4][5].

Related PR: #18840, #14460, apache/nuttx-apps#3468.

[1] https://pubs.opengroup.org/onlinepubs/9799919799/
[2] https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/time.h.html
[3] https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html
[4] https://www.mail-archive.com/[email protected]/msg12060.html
[5] https://www.mail-archive.com/[email protected]/msg14534.html

Describe the solution you'd like

  1. We are moving towards POSIX standard compliant time representation with int64_t type as default.
  2. We need alternative portable implementation of the time representation on platforms that do not support native int64_t type that will solve size increase, slow down, and non-atomic time access protections, but still assure POSIX compliant interface.
  3. Form the OS perspective both implementations should provide exactly the same and one-to-one compatible / replaceable solution, but the "portable" should assure best fit for the small architectures.

Describe alternatives you've considered

No response

Verification

  • I have verified before submitting the report.

Activity

  1. added this to the v13.0.0 milestone on May 4, 2026
  2. added
    Area: Documentation
    on May 4, 2026
  3. added theissue type on May 4, 2026
  4. added
    Type: Enhancement
    Area: Modularity
    Area: Standards
    Arch: avr8
    Arch: avr32
    Arch: avr
    Arch: mips
    Arch: risc-v
    Arch: simulator
    Arch: x86
    Arch: xtensa
    Arch: z16
    Arch: z80
    Area: Kernel
    Area: Posix
    on May 4, 2026
  5. Brijesh-Thakkar commented on Oct 23, 2026

    @Brijesh-Thakkar
    Contributor

    Hi @cederom, I'd like to work on this.

    Current master: #18840 / apache/nuttx-apps#3468 are merged, time_t and clock_t are
    int64_t, CONFIG_SYSTEM_TIME64 is gone. No open PR addresses #18849.

    I measured the cost of the #18840 series (nuttx 9a84223 -> end of series, same
    defconfig, arm-none-eabi-gcc 14.2.1 / avr-gcc 14.3.0):

    config text delta total delta
    stm32vldiscovery:nsh (Cortex-M3) +1168 +1248
    nucleo-f072rb:nsh (Cortex-M0) +1504 +1520

    So the ~2 KB estimate in the issue looks about right for small parts.

    Side notes from testing: z80sim does not build with sdcc 4.5.0 even before #18840
    (sdcc rejects -isystem), and no z80/z16 config is in tools/ci/testlist, so I couldn't
    evaluate them. I'd suggest scoping this issue to AVR and small 32-bit parts first.
    Proposed staged plan:

    1. Add clock helpers in include/nuttx/clock.h (clock_add/sub/cmp, tick<->timespec)
      that default to today's int64_t operations, and convert sched/wdog, sched/timer and
      clock_systime_* to use them. Output should be byte-identical on existing configs.
    2. Let an arch override those helpers; prototype an AVR override to show a real size win.
    3. If still needed, a CONFIG option for a cheaper internal representation under the
      helpers, keeping the int64_t POSIX interface and using the existing seqlock for reads.
    4. Cheaper libc time conversions (gmtime/timegm/strftime) for small targets.
      Does this direction look right? Is a no-behavior-change helper PR OK as the first step?
  6. Brijesh-Thakkar commented on Oct 29, 2026

    @Brijesh-Thakkar
    Contributor

    Hi @cederom, just following up on this.
    Would it be okay to start with the helper refactoring PR?

  7. xiaoxiang781216 commented on Oct 29, 2026

    @xiaoxiang781216
    Contributor

    7. Hi @cederom, just following up on this.
    Would it be okay to start with the helper refactoring PR?

    so, the plan still keep 64bit time_t/clock_t, but optimize the math operation for each arch?

  8. Brijesh-Thakkar commented on Oct 30, 2026

    @Brijesh-Thakkar
    Contributor

    @xiaoxiang781216 yes, time_t/clock_t stay int64_t; no POSIX/API change.

    Before proposing code I'm measuring where the size actually goes. Early symbol diff on
    nucleo-f072rb:nsh (M0) shows ~80% of the +1.5 KB from #18840 is libgcc's generic 64-bit
    divide/multiply (__divdi3, __udivmoddi4, __aeabi_ldivmod...), pulled in by only four
    callers: clock_systime_timespec, uptime_read, clock_basetime and long long printf
    (__ultoa_invert). Three of them divide by constants.

    I'm checking whether avoiding those few divisions removes the libgcc code entirely, and
    will post numbers before suggesting a direction.

  9. xiaoxiang781216 commented on Oct 30, 2026

    @xiaoxiang781216
    Contributor

    9. @xiaoxiang781216 yes, time_t/clock_t stay int64_t; no POSIX/API change.
    Before proposing code I'm measuring where the size actually goes. Early symbol diff on
    nucleo-f072rb:nsh (M0) shows ~80% of the +1.5 KB from #18840 is libgcc's generic 64-bit
    divide/multiply (__divdi3, __udivmoddi4, __aeabi_ldivmod...), pulled in by only four
    callers: clock_systime_timespec, uptime_read, clock_basetime and long long printf
    (__ultoa_invert). Three of them divide by constants.
    I'm checking whether avoiding those few divisions removes the libgcc code entirely, and
    will post numbers before suggesting a direction.

    yes, it's right approach to avoid the kernel common link libgcc 64bit functions.

  10. xiaoxiang781216 commented on Oct 30, 2026

    @xiaoxiang781216
    Contributor

    __ultoa_invert is fixed by: #20996

  11. Brijesh-Thakkar commented on Oct 30, 2026

    @Brijesh-Thakkar
    Contributor

    Thanks @xiaoxiang781216, good to know about #20996. I'll drop __ultoa_invert from my side,
    rebase on top of #20996, and send a PR for the remaining kernel call sites
    (clock_systime_timespec, uptime_read, clock_basetime) with before/after sizes for
    nucleo-f072rb, stm32vldiscovery and breadxavr.

    Side finding: div_const in include/nuttx/lib/math32.h gates its reciprocal-multiply path
    on CONFIG_HAVE_EXPRESSION_STATEMENT, but nothing defines that symbol anywhere in the tree,
    so the fast path is dead code. Forcing it on made M0 slightly bigger (+116 B, it pulls in
    __aeabi_lmul instead), so I'm not relying on it. Should it be removed, or wired up?

  12. Brijesh-Thakkar commented on Oct 30, 2026

    @Brijesh-Thakkar
    Contributor

    Update after measuring on top of #20996:

    • Cortex-M: libs/libc: enable div64_const on 32bit targets #20996 removes all libgcc 64-bit helpers with DEBUG_FULLOPT (stm32vldiscovery -872 B),
      so no extra kernel helpers are needed there.
    • AVR: portable C replacements for the remaining 64-bit divisions were always larger than
      libgcc's routines (+306..+972 B on breadxavr), so I'm not proposing them. The one site that
      does pay off is clock_basetime's multiply: #XXXXX (-82 B on breadxavr).
    • Remaining AVR cost is libgcc 64-bit division, which is shared and already minimal. Reducing
      it further would need a different internal time representation.

    @cederom given these numbers, do you still want a "portable" representation for 8-bit targets,
    or is the remaining cost acceptable?

  13. Brijesh-Thakkar commented on Oct 30, 2026

    @Brijesh-Thakkar
    Contributor

    Summary of what I measured for this issue:

    @cederom given these numbers, is the remaining cost acceptable for v13, or do you want a
    "portable" representation for 8-bit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arch: avrArch: avr32Arch: avr8Arch: mipsArch: risc-vArch: simulatorArch: x86Arch: xtensaArch: z16Arch: z80Area: DocumentationArea: KernelArea: ModularityArea: PosixArea: StandardsBoard: allType: EnhancementType: Improvement

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions