Skip to content

Commit

Permalink
resource limit changes
Browse files Browse the repository at this point in the history
- make the stack size limit a Nim define and use it in "config.nims" for
  the Windows C compiler flag and in "beacon_node.nim" for the POSIX
  runtime setting.
- warn when the open file limit is bellow 1024 (macOS, by default)
- increase the macOS open file limit in Travis
- add "-Wstack-usage=..." C compiler flag. No GCC warnings visible at the
  stack limit we set, but any would be visible by defining "cwarnings"
  in Nim.
  • Loading branch information
stefantalpalaru committed Apr 26, 2020
1 parent 519787b commit a02e27f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ matrix:
- os: osx
before_install:
- HOMEBREW_NO_AUTO_UPDATE=1 HOMEBREW_NO_INSTALL_CLEANUP=1 brew install ccache
- ulimit -n 1024 # increase the open files limit from the default of 512
env:
- NPROC=2

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ ifneq ($(USE_LIBBACKTRACE), 0)
deps: | libbacktrace
endif

# maximum stack size (in bytes) mirroring Android limits
NIM_PARAMS := $(NIM_PARAMS) -d:stack_size=1000000

#- deletes and recreates "beacon_chain.nims" which on Windows is a copy instead of a proper symlink
update: | update-common
rm -f beacon_chain.nims && \
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ make -j$(nproc) NIMFLAGS="-d:release" USE_MULTITAIL=yes eth2_network_simulation
make USE_LIBBACKTRACE=0 # expect the resulting binaries to be 2-3 times slower
```

- show (some) C compiler warnings:

```bash
make NIMFLAGS="-d:cwarnings" beacon_node
```

## License

Licensed and distributed under either of
Expand Down
21 changes: 21 additions & 0 deletions beacon_chain/beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import
sync_protocol, request_manager, validator_keygen, interop, statusbar,
attestation_aggregation, sync_manager

when defined(posix):
import posix

const
genesisFile = "genesis.ssz"
hasPrompt = not defined(withoutPrompt)
Expand Down Expand Up @@ -1346,6 +1349,24 @@ programMain:
quit(QuitFailure)
setControlCHook(controlCHandler)

## Set the stack size limit on POSIX systems (the Windows one is set in
## config.nims).
# TODO: move this to nim-stew, if we need to use it elsewhere
when defined(posix):
const
RLIMIT_STACK = 3 # from "/usr/include/bits/resource.h"
stack_size {.intdefine.}: int = 0
var
rlimit: RLimit
when defined(stack_size):
if getrlimit(RLIMIT_STACK, rlimit) == -1:
error "getrlimit() error", msg = osErrorMsg(osLastError())
else:
rlimit.rlim_cur = stack_size
if setrlimit(RLIMIT_STACK, rlimit) == -1:
error "setrlimit() error", msg = osErrorMsg(osLastError())

## handle command line arguments
case config.cmd
of createTestnet:
var deposits: seq[Deposit]
Expand Down
25 changes: 21 additions & 4 deletions config.nims
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import strutils

if defined(release):
switch("nimcache", "nimcache/release/$projectName")
else:
switch("nimcache", "nimcache/debug/$projectName")

const stack_size {.intdefine.}: int = 0

# conservative compile-time estimation for single functions
when defined(stack_size) and defined(gcc):
switch("passC", "-Wstack-usage=" & $stack_size)

This comment has been minimized.

Copy link
@zah

zah Apr 26, 2020

Contributor

Can you test if instantiating BeaconState as a stack variable anywhere triggers this warning?


if defined(windows):
# disable timestamps in Windows PE headers - https://wiki.debian.org/ReproducibleBuilds/TimestampsInPEBinaries
switch("passL", "-Wl,--no-insert-timestamp")
# increase stack size
switch("passL", "-Wl,--stack,1000000")
# set stack size
when defined(stack_size):
switch("passL", "-Wl,--stack," & $stack_size)
# https://github.com/nim-lang/Nim/issues/4057
--tlsEmulation:off
if defined(i386):
Expand Down Expand Up @@ -43,9 +52,17 @@ else:

switch("import", "testutils/moduletests")

# the default open files limit is too low on macOS (512), breaking the
# The default open files limit is too low on macOS (512), breaking the
# "--debugger:native" build. It can be increased with `ulimit -n 1024`.
if not defined(macosx):
let openFilesLimitTarget = 1024
var openFilesLimit = 0
try:
openFilesLimit = staticExec("ulimit -n").parseInt()
except:
echo "ulimit error"
if openFilesLimit < openFilesLimitTarget:
echo "Open files limit too low. Increase it with \"ulimit -n " & $openFilesLimitTarget & "\""
else:
# add debugging symbols and original files and line numbers
--debugger:native
if not (defined(windows) and defined(i386)) and not defined(disable_libbacktrace):
Expand Down
9 changes: 9 additions & 0 deletions nim.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# can't do this in "config.nims", probably due to parsing order
@if cwarnings:
@if windows:
gcc.options.always = "-mno-ms-bitfields -Wno-all -Wno-discarded-qualifiers -Wno-incompatible-pointer-types"
@else:
gcc.options.always = "-Wno-all -Wno-discarded-qualifiers -Wno-incompatible-pointer-types"
@end
@end

0 comments on commit a02e27f

Please sign in to comment.