gopkg.in/dedis/onet.v2@v2.0.0-20181115163211-c8f3724038a7/simul/platform/fd_unix.go (about)

     1  // +build !windows
     2  
     3  package platform
     4  
     5  import (
     6  	"errors"
     7  	"syscall"
     8  
     9  	"gopkg.in/dedis/onet.v2/log"
    10  )
    11  
    12  // By default in simulation we update the per-process file descriptor limit
    13  // to the maximal limit.
    14  func init() {
    15  	var rLimit syscall.Rlimit
    16  	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    17  	if err != nil {
    18  		log.Fatal("Error Getting Rlimit ", err)
    19  	}
    20  
    21  	if rLimit.Cur < rLimit.Max {
    22  		rLimit.Cur = rLimit.Max
    23  		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    24  		if err != nil {
    25  			log.Warn("Error Setting Rlimit:", err)
    26  		}
    27  	}
    28  
    29  	err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    30  	if err != nil {
    31  		log.Error("Couldn't raise Rlimit: " + err.Error())
    32  	}
    33  }
    34  
    35  // CheckOutOfFileDescriptors tries to duplicate the stdout file descriptor
    36  // and throws an error if it cannot do it. This is a horrible hack mainly for
    37  // MacOSX where the file descriptor limit is quite low and we need to tell
    38  // people running simulations what they can do about it.
    39  func CheckOutOfFileDescriptors() error {
    40  	// Check if we're out of file descriptors
    41  	newFS, err := syscall.Dup(syscall.Stdout)
    42  	if err != nil {
    43  		return errors.New(`Out of file descriptors. You might want to do something like this for Mac OSX:
    44      sudo sysctl -w kern.maxfiles=122880
    45      sudo sysctl -w kern.maxfilesperproc=102400
    46      sudo sysctl -w kern.ipc.somaxconn=20480`)
    47  	}
    48  	if err = syscall.Close(newFS); err != nil {
    49  		return errors.New("Couldn't close new file descriptor: " + err.Error())
    50  	}
    51  	return nil
    52  }