Skip to content

Conversation

centurysys
Copy link
Contributor

When I first ported NuttX, used "EPOLL" because "select" seemed to leak memory.
However, the number of fd allocated by default in newSelector() is 1024,
which is a lot for a small MCU, so I changed the implementation to select.

This implementation tested with Jester, on Cortex-A5 platform.

@centurysys centurysys force-pushed the feature/nuttx-switch-to-selectors-select branch from d812aab to 96a0d41 Compare February 17, 2023 03:54
@Araq
Copy link
Member

Araq commented Feb 17, 2023

Isn't epoll with a limit smaller than 1024 the better solution?

@centurysys
Copy link
Contributor Author

@Araq
NuttX config has a LIBC_OPEN_MAX entry, which is now returned by getrlimit(RLIMIT_NOFILE) as in other POSIX OS (merged into apache/nuttx master branch today).

apache/nuttx#8555

I guess it depends on the RAM capacity of the board, etc., but it seems to be 255 by default.

config LIBC_OPEN_MAX
        int "OPEN_MAX for this device"
        default 255
        ---help---
                The maximum number of files that a process can have open
                at any time.  Must not be less than _POSIX_OPEN_MAX.

If using this value instead of 1024 with "when defined(nuttx):", it may not interfere even with EPOLL.
BTW, is it recommended to use EPOLL over "select" in the Nim implementation for performance?

@centurysys
Copy link
Contributor Author

Would the following changes be acceptable?

--- a/lib/pure/ioselects/ioselectors_epoll.nim
+++ b/lib/pure/ioselects/ioselectors_epoll.nim
@@ -76,7 +76,7 @@ proc newSelector*[T](): Selector[T] =
   var maxFD = maxDescriptors()
   doAssert(maxFD > 0)
   # Start with a reasonable size, checkFd() will grow this on demand
-  const numFD = 1024
+  let numFD = min(maxFD, 1024)
 
   var epollFD = epoll_create1(O_CLOEXEC)

@centurysys
Copy link
Contributor Author

Even for MCUs as shown below, LIBC_OPEN_MAX was set to 255 unless config was explicitly changed.

https://www.st.com/en/microcontrollers-microprocessors/stm32l476rg.html

Up to 128 KB of SRAM including 32 KB with hardware parity check

It may be safer to set it to "select".

@centurysys centurysys force-pushed the feature/nuttx-switch-to-selectors-select branch from 96a0d41 to 3905702 Compare February 20, 2023 23:08
…s internal implementation.

When I first ported NuttX, used "EPOLL" because "select" seemed to leak memory.
However, the number of fd allocated by default in newSelector() is 1024,
which is a lot for a small MCU, so I changed the implementation to select.

Signed-off-by: Takeyoshi Kikuchi <[email protected]>
@centurysys centurysys force-pushed the feature/nuttx-switch-to-selectors-select branch from 3905702 to a6f8e44 Compare February 21, 2023 02:46
@Araq
Copy link
Member

Araq commented Feb 21, 2023

BTW, is it recommended to use EPOLL over "select" in the Nim implementation for performance?

Yes, exactly. Assuming that it is more efficient for the OS.

@centurysys
Copy link
Contributor Author

Yes, exactly. Assuming that it is more efficient for the OS.

I see.
Since NuttX targets range from MCUs with several hundred KiB of RAM to MPUs with several hundred MiB of RAM, it would be good if the internal implementation of select/EPOLL could be switched according to the memory capacity of the operating environment.

@centurysys
Copy link
Contributor Author

I would like to get some input from the NuttX OS internal implementation perspective, so I will discuss this with the NuttX community.

@centurysys
Copy link
Contributor Author

@Araq
I received an answer from the perspective of NuttX's internal implementation.

apache/nuttx#8604

They said that a patch was applied last year and that EPOLL is now the fastest.
It would be preferable to start with a smaller number of initial fd arrays and use EPOLL, but what do you think is the most preferable way to change the internals of the EPOLL implementation?

@Araq
Copy link
Member

Araq commented Feb 21, 2023

I don't understand the question, tbh. Add a when section to ioselectors_epoll.nim?

@centurysys
Copy link
Contributor Author

centurysys commented Feb 21, 2023

I found this value in NuttX config.

config FS_NEPOLL_DESCRIPTORS
	int "Maximum number of default epoll descriptors for epoll_create1(2)"
	default 8

How about the following changes instead of this pull request?

diff --git a/lib/posix/posix_other_consts.nim b/lib/posix/posix_other_consts.nim
index 506c92158..08069fe9a 100644
--- a/lib/posix/posix_other_consts.nim
+++ b/lib/posix/posix_other_consts.nim
@@ -750,3 +750,6 @@ var SEEK_SET* {.importc: "SEEK_SET", header: "<unistd.h>".}: cint
 var SEEK_CUR* {.importc: "SEEK_CUR", header: "<unistd.h>".}: cint
 var SEEK_END* {.importc: "SEEK_END", header: "<unistd.h>".}: cint
 
+# <nuttx/config.h>
+when defined(nuttx):
+  var NEPOLL_MAX* {.importc: "CONFIG_FS_NEPOLL_DESCRIPTORS", header: "<nuttx/config.h>".}: cint
diff --git a/lib/pure/ioselects/ioselectors_epoll.nim b/lib/pure/ioselects/ioselectors_epoll.nim
index 8526eb8a3..621fa9a9e 100644
--- a/lib/pure/ioselects/ioselectors_epoll.nim
+++ b/lib/pure/ioselects/ioselectors_epoll.nim
@@ -72,11 +72,16 @@ type
   SelectEvent* = ptr SelectEventImpl
 
 proc newSelector*[T](): Selector[T] =
+  proc initialNumFD(): int {.inline.} =
+    when defined(nuttx):
+      result = NEPOLL_MAX
+    else:
+      result = 1024
   # Retrieve the maximum fd count (for current OS) via getrlimit()
   var maxFD = maxDescriptors()
   doAssert(maxFD > 0)
   # Start with a reasonable size, checkFd() will grow this on demand
-  const numFD = 1024
+  let numFD = initialNumFD()
 
   var epollFD = epoll_create1(O_CLOEXEC)
   if epollFD < 0:

I am concerned that the initial fd numbers other than NuttX will change from const to let.

@Araq
Copy link
Member

Araq commented Feb 22, 2023

I am concerned that the initial fd numbers other than NuttX will change from const to let.

That should be fine.

@centurysys
Copy link
Contributor Author

@Araq
Thank you for your very kind review !
I will be submitting a new pull request.

@centurysys centurysys closed this Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants