Skip to content

Commit

Permalink
Apply Go 1.12/Darwin file descriptor fix
Browse files Browse the repository at this point in the history
Also: Improve some error reporting.
  • Loading branch information
Jeff R. Allen committed May 8, 2019
1 parent 8eb7fa5 commit 03a8c57
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 31 deletions.
32 changes: 4 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
*.iml
*.cov

config/
build/
test_data/
test/
deploy/
.tags*
profile.tmp
*~
profile.tmp
*.cov
.DS_Store
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
go.dedis.ch/protobuf v1.0.6
go.etcd.io/bbolt v1.3.2
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 // indirect
golang.org/x/sys v0.0.0-20190124100055-b90733256f2e
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/satori/go.uuid.v1 v1.2.0
gopkg.in/tylerb/graceful.v1 v1.2.15
Expand Down
2 changes: 1 addition & 1 deletion simul/platform/deterlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (d *Deterlab) Cleanup() error {

err := SSHRunStdout(d.Login, d.Host, "test -f remote/users && ( cd remote; ./users -kill )")
if err != nil {
log.Lvl1("NOT-Normal error from cleanup")
log.Lvl1("NOT-Normal error from cleanup", err.Error())
sshKill <- "error"
}
sshKill <- "stopped"
Expand Down
62 changes: 62 additions & 0 deletions simul/platform/fd_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package platform

import (
"errors"
"syscall"

"go.dedis.ch/onet/v3/log"
"golang.org/x/sys/unix"
)

func init() {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("Error Getting Rlimit ", err)
}

// We observed that with Go 1.11.5, we were getting
// 24576, and then with Go 1.12, we started getting "invalid argument".
// See https://github.com/golang/go/issues/30401

// On Darwin, the real fd max is given by sysctl.
res, err := unix.Sysctl("kern.maxfilesperproc")
if err != nil || len(res) != 3 {
// In case of error, fall back to something reasonable.
res = "10240"
}
// res is type string, but according to sysctl(3), it should be interpreted
// as an int32. It seems to be little-endian. And for some reason, there are only
// 3 bytes.
rLimit.Max = uint64(res[0]) | uint64(res[1])<<8 | uint64(res[2])<<16

if rLimit.Cur < rLimit.Max {
rLimit.Cur = rLimit.Max
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Warn("Error Setting Rlimit:", err)
}
}

err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
log.Info("File descriptor limit is:", rLimit.Cur)
}

// CheckOutOfFileDescriptors tries to duplicate the stdout file descriptor
// and throws an error if it cannot do it. This is a horrible hack mainly for
// MacOSX where the file descriptor limit is quite low and we need to tell
// people running simulations what they can do about it.
func CheckOutOfFileDescriptors() error {
// Check if we're out of file descriptors
newFS, err := syscall.Dup(syscall.Stdout)
if err != nil {
return errors.New(`Out of file descriptors. You might want to do something like this for Mac OSX:
sudo sysctl -w kern.maxfiles=122880
sudo sysctl -w kern.maxfilesperproc=102400
sudo sysctl -w kern.ipc.somaxconn=20480`)
}
if err = syscall.Close(newFS); err != nil {
return errors.New("Couldn't close new file descriptor: " + err.Error())
}
return nil
}
2 changes: 1 addition & 1 deletion simul/platform/fd_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
// +build !windows,!darwin

package platform

Expand Down
4 changes: 3 additions & 1 deletion simul/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ func ReadRunFile(p Platform, filename string) []*RunConfig {
// Decoding of the first part of the run config file
// where the config wont change for the whole set of the simulation's tests
scanner := bufio.NewScanner(file)
line := 0
for scanner.Scan() {
text := scanner.Text()
line++
log.Lvl3("Decoding", text)
// end of the first part
if text == "" {
Expand All @@ -164,7 +166,7 @@ func ReadRunFile(p Platform, filename string) []*RunConfig {
// checking if format is good
vals := strings.Split(text, "=")
if len(vals) != 2 {
log.Fatal("Simulation file:", filename, " is not properly formatted ( key = value )")
log.Fatal("Simulation file:", filename, "line", line, "is not properly formatted ( key = value )")
}
// fill in the general config
masterConfig.Put(strings.TrimSpace(vals[0]), strings.TrimSpace(vals[1]))
Expand Down

0 comments on commit 03a8c57

Please sign in to comment.