summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJaremy Creechley <creechley@gmail.com>2021-11-16 11:30:07 -0800
committerGitHub <noreply@github.com>2021-11-16 20:30:07 +0100
commit92d6fb86c66ceda154972314ba695cea4aac9301 (patch)
tree9ecfc5922972e4818b35667e59e7c30e41c44f74
parent309ec7167e6ea17112901533539be0a1884c8b65 (diff)
downloadNim-92d6fb86c66ceda154972314ba695cea4aac9301.tar.gz
Enable customizing PageShift to set PageSize for embedded targets (#19129)
* Enable customizing PageSize (via PageShift).

This enables adjusting PageSize for embedded targets without abusing
cpu16.

* copy nimPageXYZ settings for mmpaptest

* add docs for Nim manual

* add docs for Nim manual

* docs tweaks

Co-authored-by: Jaremy Creechley <jaremy.creechley@panthalassa.com>
-rw-r--r--doc/nimc.rst24
-rw-r--r--lib/system/bitmasks.nim10
-rw-r--r--tests/mmaptest.nim7
3 files changed, 36 insertions, 5 deletions
diff --git a/doc/nimc.rst b/doc/nimc.rst
index 98b754729..d35dad6c2 100644
--- a/doc/nimc.rst
+++ b/doc/nimc.rst
@@ -656,6 +656,30 @@ is not available but C's `malloc` is. You can use the `nimAllocPagesViaMalloc`
 define to use `malloc` instead of `mmap`. `nimAllocPagesViaMalloc` is currently
 only supported with `--gc:arc` or `--gc:orc`. (Since version 1.6)
 
+nimPage256 / nimPage512 / nimPage1k
+===================================
+
+Adjust the page size for Nim's GC allocator. This enables using
+`nimAllocPagesViaMalloc` on devices with less RAM. The default
+page size requires too much RAM to work.
+
+Recommended settings:
+
+- < 32 kB of RAM use `nimPage256`
+
+- < 512 kB of RAM use `nimPage512`
+
+- < 2 MB of RAM use `nimPage1k`
+
+Initial testing hasn't shown much difference between 512B or 1kB page sizes
+in terms of performance or latency. Using `nimPages256` will limit the
+total amount of allocatable RAM.
+
+nimMemAlignTiny
+===============
+
+Sets `MemAlign` to `4` bytes which reduces the memory alignment
+to better match some embedded devices.
 
 Nim for realtime systems
 ========================
diff --git a/lib/system/bitmasks.nim b/lib/system/bitmasks.nim
index caf86568a..9cb57bc45 100644
--- a/lib/system/bitmasks.nim
+++ b/lib/system/bitmasks.nim
@@ -10,14 +10,18 @@
 # Page size of the system; in most cases 4096 bytes. For exotic OS or
 # CPU this needs to be changed:
 const
-  PageShift = when defined(cpu16): 8 else: 12 # \
-    # my tests showed no improvements for using larger page sizes.
+  PageShift = when defined(nimPage256) or defined(cpu16): 8
+              elif defined(nimPage512): 9
+              elif defined(nimPage1k): 10
+              else: 12 # \ # my tests showed no improvements for using larger page sizes.
+
   PageSize = 1 shl PageShift
   PageMask = PageSize-1
 
 
   MemAlign = # also minimal allocatable memory block
-    when defined(useMalloc):
+    when defined(nimMemAlignTiny): 4
+    elif defined(useMalloc):
       when defined(amd64): 16 
       else: 8
     else: 16
diff --git a/tests/mmaptest.nim b/tests/mmaptest.nim
index 7a93cdd45..33010606f 100644
--- a/tests/mmaptest.nim
+++ b/tests/mmaptest.nim
@@ -19,8 +19,11 @@ proc `+!!`(p: pointer, size: int): pointer {.inline.} =
   result = cast[pointer](cast[int](p) + size)
 
 const
-  PageShift = when defined(cpu16): 8 else: 12 # \
-    # my tests showed no improvements for using larger page sizes.
+  PageShift = when defined(nimPage256) or defined(cpu16): 8
+              elif defined(nimPage512): 9
+              elif defined(nimPage1k): 10
+              else: 12 # \ # my tests showed no improvements for using larger page sizes.
+
   PageSize = 1 shl PageShift
 
 var p = osAllocPages(3 * PageSize)