Skip to content

sim: posix: add platform-specific timer handling for macOS and Linux#18331

Draft
aditya0yadav wants to merge 4 commits intoapache:masterfrom
aditya0yadav:master
Draft

sim: posix: add platform-specific timer handling for macOS and Linux#18331
aditya0yadav wants to merge 4 commits intoapache:masterfrom
aditya0yadav:master

Conversation

@aditya0yadav
Copy link


PLEASE DONT MERGE THIS PR

Summary

This change updates the POSIX simulator host timer implementation to
handle platform differences between macOS (Darwin) and Linux.

macOS does not provide full support for POSIX timers such as
timer_create() and timer_settime(). To address this, the simulator
now uses setitimer() on macOS while retaining POSIX timers with
absolute-time scheduling on Linux and other POSIX systems.

The platform selection is handled via the NUTTX_DARWIN macro, keeping
the simulator core logic unchanged while improving portability and
correctness on macOS.


Impact

  • Improves portability of the POSIX simulator on macOS hosts.
  • No functional or behavioral change for Linux or other POSIX platforms.
  • No impact on target hardware, board support, or NuttX core logic.
  • No documentation or security impact.
  • Build behavior remains unchanged on supported Linux systems.

Testing

  • Tested on macOS (Darwin):

    • Host: macOS (Apple Silicon)
    • Simulator: POSIX SIM
    • Verified successful build and runtime execution.
    • Confirmed timer interrupts are delivered via SIGALRM and simulator
      scheduling behaves as expected.
  • Linux testing:

    • Not yet tested on a Linux host.

Since this change affects host timer handling, additional testing on
Linux systems would be valuable before merging.
If anyone is able to test this change on a Linux PC and confirm that the
POSIX simulator builds and runs correctly, that feedback and any logs
would be greatly appreciated.

One more thing ,
For supporting macos, multiple things need to be done like docs and linker too .


@github-actions github-actions bot added Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small labels Feb 3, 2026
@aditya0yadav
Copy link
Author

For macos,
Please disable this
COVERAGE_TOOLCHAIN(=n) "Link toolchain gcov library to the OS"
SYSTEM_GCOV(=n) "gcov tool"

Screenshot 2026-02-03 at 12 46 49 PM

@simbit18 simbit18 marked this pull request as draft February 3, 2026 10:33
@simbit18
Copy link
Contributor

simbit18 commented Feb 3, 2026

Hi @aditya0yadav , I converted PR into draft.

@FelipeMdeO @lupyuen @aviralgarg05 What do you think?
#17818

it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 0;

return setitimer(ITIMER_REAL, &it, NULL);
Copy link
Contributor

@xiaoxiang781216 xiaoxiang781216 Feb 3, 2026

Choose a reason for hiding this comment

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

why not use setitimer on Linux too? https://linux.die.net/man/2/setitimer

Copy link
Author

Choose a reason for hiding this comment

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

Because linux has a better api : timer_create() + timer_settime()
in macos , we have to use that time_create exist but unreliable and uncompleted and time_settime() is not properly supported

Copy link
Contributor

Choose a reason for hiding this comment

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

it's better to go the same code path if we can, so don't need double the test effort.

Copy link
Author

Choose a reason for hiding this comment

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

@xiaoxiang781216 Ok i will do that
Any suggestion about linker
for to use sim in macos , we need to disable the gcov

Copy link
Author

Choose a reason for hiding this comment

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

@xiaoxiang781216 now set timer is being shared bet linux and macOS

@jerpelea
Copy link
Contributor

jerpelea commented Feb 3, 2026

please remove the merge commit !

@aviralgarg05
Copy link
Contributor

Hi @aditya0yadav , I converted PR into draft.

@FelipeMdeO @lupyuen @aviralgarg05 What do you think? #17818

Hi @simbit18, thanks for tagging me, the core approach of using setitimer() for macOS makes sense as a workaround for the missing POSIX timers but the addition of #define _POSIX_C_SOURCE 200809L in sim_hosttime.c has broken the Linux build. It causes an implicit declaration error for usleep() because usleep was removed in POSIX 2008. We might need to keep _POSIX_C_SOURCE at an older version or switch usleep to nanosleep.

and remove the use of #define _POSIX_C_SOURCE 200809L
@@ -1,5 +1,7 @@
/****************************************************************************
* arch/sim/src/sim/posix/sim_hosttime.c
* arch/sim/src/sim/win/sim_hosttime.c
Copy link
Contributor

Choose a reason for hiding this comment

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

@aditya0yadav incorrect path

Copy link
Author

@aditya0yadav aditya0yadav Feb 4, 2026

Choose a reason for hiding this comment

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

@simbit18
by mistake i copied license header from win folder

@aditya0yadav
Copy link
Author

@aviralgarg05 I’ve removed the #define _POSIX_C_SOURCE 200809L in macOS and Ubuntu-based code spaces and it’s working properly. Mind taking a look?

@aditya0yadav
Copy link
Author

@simbit18 One more thing, what should we do with the linker in macOS?
I know it will work perfectly with Linux but not with macOS.

Every time I have to disable the configuration toolchain related to Gcov.

Any suggestions?

@aviralgarg05
Copy link
Contributor

@aviralgarg05 I’ve removed the #define _POSIX_C_SOURCE 200809L in macOS and Ubuntu-based code spaces and it’s working properly. Mind taking a look?

Thanks for the update @aditya0yadav, I checked the latest changes, the approach looks correct. There might be an issue with the setitimer tick scheduling - the simulator isn't booting correctly. Could you check if SIGALRM is being delivered properly?

@aditya0yadav
Copy link
Author

aditya0yadav commented Feb 4, 2026 via email

@simbit18
Copy link
Contributor

simbit18 commented Feb 4, 2026

@simbit18 One more thing, what should we do with the linker in macOS? I know it will work perfectly with Linux but not with macOS.

Every time I have to disable the configuration toolchain related to Gcov.

Any suggestions?

A long time ago, it was possible to build sim:nsh for Linux, macOS, and Windows .
Currently, it does not work on macOS and Windows with MSYS2/Cygwin (the latest version I was able to build and test was 10.2), so I personally think it is right to restore the build by modifying the defconfig file.

Of course, this is just my opinion! :)

@acassis
Copy link
Contributor

acassis commented Feb 4, 2026

@simbit18 One more thing, what should we do with the linker in macOS? I know it will work perfectly with Linux but not with macOS.
Every time I have to disable the configuration toolchain related to Gcov.
Any suggestions?

A long time ago, it was possible to build sim:nsh for Linux, macOS, and Windows . Currently, it does not work on macOS and Windows with MSYS2/Cygwin (the latest version I was able to build and test was 10.2), so I personally think it is right to restore the build by modifying the defconfig file.

Of course, this is just my opinion! :)

I think is makes sense, maybe using git bisect it could be possible to identify which symbols cases the breaking and open an issue to track it.

@simbit18
Copy link
Contributor

simbit18 commented Feb 5, 2026

Hi @raiden00pl, The changes in this PR cause errors in sim:citest. Can you check it?

Traceback (most recent call last):
YAML config:
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
{'config': {'cwd': './', 'timeout_session': 6000},
 'product': {'cores': {'core0': {'conf_path': './.config',
                                 'device': 'sim',
                                 'elf_path': './nuttx',
                                 'name': 'main'}},
             'name': 'ntfc-sim'}}
JSON config:
{'args': {'kv': []},
 'module': {'exclude_module': ['Nuttx_System_Driver_Pm', 'Nuttx_System_Fs_Fs'],
            'include_module': [],
            'order': []}}
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/__main__.py", line 25, in <module>
    main()  # pylint: disable=no-value-for-parameter
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/core.py", line 1485, in __call__
    return self.main(*args, **kwargs)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/core.py", line 1416, in main
    ctx.exit()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/core.py", line 737, in exit
    self.close()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/core.py", line 621, in close
    self._close_with_exception_info(None, None, None)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/core.py", line 635, in _close_with_exception_info
    exit_result = self._exit_stack.__exit__(exc_type, exc_value, tb)
  File "/usr/lib/python3.10/contextlib.py", line 576, in __exit__
    raise exc_details[1]
  File "/usr/lib/python3.10/contextlib.py", line 561, in __exit__
    if cb(*exc_details):
  File "/usr/lib/python3.10/contextlib.py", line 449, in _exit_wrapper
    callback(*args, **kwds)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/decorators.py", line 93, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/click/core.py", line 824, in invoke
    return callback(*args, **kwargs)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/cli/main.py", line 181, in cli_on_close
    ret = test_run(pt, ctx)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/cli/main.py", line 123, in test_run
    return pt.runner(ctx.testpath, ctx.result, ctx.nologs)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/pytest/mypytest.py", line 270, in runner
    self._device_start()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/pytest/mypytest.py", line 222, in _device_start
    product.start()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/product.py", line 70, in start
    self._cores.start()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/cores.py", line 73, in start
    core.start()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/core.py", line 510, in start
    self._device.start()
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/device/sim.py", line 52, in start
    self.host_open(cmd, uptime)
  File "/github/workspace/nuttx-ntfc/venv/lib/python3.10/site-packages/ntfc/device/host.py", line 139, in host_open
    raise TimeoutError("device boot timeout")
TimeoutError: device boot timeout
+ ret=1
+ echo 1
+ deactivate

@simbit18 simbit18 requested a review from raiden00pl February 5, 2026 10:45
@raiden00pl
Copy link
Member

raiden00pl commented Feb 5, 2026

@simbit18 @aditya0yadav sim/citest is broken, NTFC detects that device is broken at init or doesn't boot at all.

When I run binary from this PR, NSH is not functional and it stucks on NSH prompt (not sure what is this init error and if it's realted):

image

probably not everyone knows about it, but you can download built binaries directly from CI:

image

@raiden00pl
Copy link
Member

with this PR sim/citest compiled on my host doesn't work either but sim/nsh works fine

@simbit18
Copy link
Contributor

simbit18 commented Feb 5, 2026

@raiden00pl, thank you very much.

@aditya0yadav
Copy link
Author

@simbit18 One more thing, what should we do with the linker in macOS? I know it will work perfectly with Linux but not with macOS.
Every time I have to disable the configuration toolchain related to Gcov.
Any suggestions?

A long time ago, it was possible to build sim:nsh for Linux, macOS, and Windows . Currently, it does not work on macOS and Windows with MSYS2/Cygwin (the latest version I was able to build and test was 10.2), so I personally think it is right to restore the build by modifying the defconfig file.
Of course, this is just my opinion! :)

I think is makes sense, maybe using git bisect it could be possible to identify which symbols cases the breaking and open an issue to track it.

I just want to check if I’m understanding correctly:
@simbit18 @acassis

The linker issue seems to happen only on macOS, not on Linux. On Linux, it works fine and uses Gcov as expected, but on macOS, I always have to disable the Gcov-related toolchain in the configuration.

If I wanted to find the specific symbol that is causing the linker to fail, doing a git bisect over 2–4 years of changes seems impractical because there’s been too much change in the repository.

One idea I had is to use Docker with Linux to build NuttX, since Linux doesn’t hit the macOS linker problem. But of course, that wouldn’t reproduce the macOS-specific issue.

Do you have any suggestions on how to approach this efficiently for macOS?

@FelipeMdeO
Copy link
Contributor

FelipeMdeO commented Feb 5, 2026 via email

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

Labels

Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants