// SetLimits raises some process limits to values which allow dcrd and // associated utilities to run. func SetLimits() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } if rLimit.Cur > fileLimitWant { return nil } if rLimit.Max < fileLimitMin { err = fmt.Errorf("need at least %v file descriptors", fileLimitMin) return err } if rLimit.Max < fileLimitWant { rLimit.Cur = rLimit.Max } else { rLimit.Cur = fileLimitWant } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { // try min value rLimit.Cur = fileLimitMin err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } } return nil }
func TestRlimit(t *testing.T) { var rlimit, zero syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit) if err != nil { t.Fatalf("Getrlimit: save failed: %v", err) } if zero == rlimit { t.Fatalf("Getrlimit: save failed: got zero value %#v", rlimit) } set := rlimit set.Cur = set.Max - 1 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &set) if err != nil { t.Fatalf("Setrlimit: set failed: %#v %v", set, err) } var get syscall.Rlimit err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &get) if err != nil { t.Fatalf("Getrlimit: get failed: %v", err) } set = rlimit set.Cur = set.Max - 1 if set != get { t.Fatalf("Rlimit: change failed: wanted %#v got %#v", set, get) } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit) if err != nil { t.Fatalf("Setrlimit: restore failed: %#v %v", rlimit, err) } }
func setResourceLimits() error { const maximumTimeInSeconds = 5 limit := syscall.Rlimit{maximumTimeInSeconds, maximumTimeInSeconds} if err := syscall.Setrlimit(syscall.RLIMIT_CPU, &limit); err != nil { return err } const maximumMemoryInBytes = 1 << 28 limit = syscall.Rlimit{maximumMemoryInBytes, maximumMemoryInBytes} return syscall.Setrlimit(syscall.RLIMIT_AS, &limit) }
func registerSync(container, local, remote string) error { syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{ Max: 999999, Cur: 999999, }) abs, err := filepath.Abs(local) if err != nil { return err } sym, err := filepath.EvalSymlinks(abs) if err != nil { return err } syncs = append(syncs, Sync{ Container: container, Local: sym, Remote: remote, }) return nil }
func ctor(cfg *vm.Config, index int) (vm.Instance, error) { p := new(params) if err := json.Unmarshal(cfg.Params, p); err != nil { return nil, fmt.Errorf("failed to unmarshal local params: %v", err) } if _, err := os.Stat(p.Fuzzer); err != nil { return nil, fmt.Errorf("fuzzer binary '%v' does not exist: %v", p.Fuzzer, err) } if _, err := os.Stat(p.Executor); err != nil { return nil, fmt.Errorf("executor binary '%v' does not exist: %v", p.Executor, err) } os.MkdirAll(cfg.Workdir, 0770) // Disable annoying segfault dmesg messages, fuzzer is going to crash a lot. etrace, err := os.Open("/proc/sys/debug/exception-trace") if err == nil { etrace.Write([]byte{'0'}) etrace.Close() } // Don't write executor core files. syscall.Setrlimit(syscall.RLIMIT_CORE, &syscall.Rlimit{0, 0}) loc := &local{ params: *p, workdir: cfg.Workdir, syscalls: cfg.EnabledSyscalls, nocover: cfg.NoCover, id: index, mgrPort: cfg.ManagerPort, } return loc, nil }
func main() { var rlim syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Errorf("获取Rlimit报错 %s", err) os.Exit(1) } fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim) rlim.Max = 65535 rlim.Cur = 65535 //err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Errorf("设置Rlimit报错 %s", err) os.Exit(1) } //fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim) err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Errorf("获取Rlimit报错 %s", err) os.Exit(1) } fmt.Printf("ENV RLIMIT_NOFILE : %+v\n", rlim) }
func main() { lim := &syscall.Rlimit{} syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim) lim.Cur = 15 syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim) fmt.Println(lim) var s []*os.File for i := 0; i < int(lim.Max+10); i++ { f, err := os.Open("/tmp/whj.txt") fmt.Println(f, err) s = append(s, f) err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim) fmt.Println(lim, err) } err := syscall.Getrlimit(syscall.RLIMIT_CORE, lim) fmt.Println(lim, err) rusage := &syscall.Rusage{} err = syscall.Getrusage(0, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(1, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(2, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(3, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(4, rusage) fmt.Println(rusage, err) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) os.Setenv("GOTRACEBACK", "crash") lim := syscall.Rlimit{} syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim) if lim.Cur < _MaxOpenfile || lim.Max < _MaxOpenfile { lim.Cur = _MaxOpenfile lim.Max = _MaxOpenfile syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim) } ln, err := net.Listen("tcp", ":1153") if err != nil { log.Fatal(err) } go TCPServer(ln, queryTypeDNS) ln, err = net.Listen("tcp", ":1154") if err != nil { log.Fatal(err) } go TCPServer(ln, queryTypeMYIP) http.HandleFunc("/myip", HTTPServerMYIP) http.HandleFunc("/dns", HTTPServerDNS) http.HandleFunc("/health", HTTPServerHealth) log.Fatal(http.ListenAndServe(":1053", nil)) }
func Init(logPrefix string) { if logPrefix != "" { logPrefix += " " } logPrefix += fmt.Sprintf("[%v]", os.Getpid()) f, err := logfile.Open(*logfileName, *logFrequency, *logMaxSize, *logMaxFiles) if err != nil { panic(fmt.Sprintf("unable to open logfile %s: %v", *logfileName, err)) } logger := relog.New(f, logPrefix+" ", log.Ldate|log.Lmicroseconds|log.Lshortfile, relog.LogNameToLogLevel(*logLevel)) relog.SetLogger(logger) if *gomaxprocs != 0 { runtime.GOMAXPROCS(*gomaxprocs) relog.Info("set GOMAXPROCS = %v", *gomaxprocs) } fdLimit := &syscall.Rlimit{*maxOpenFds, *maxOpenFds} if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, fdLimit); err != nil { relog.Fatal("can't Setrlimit %#v: err %v", *fdLimit, err) } else { relog.Info("set max-open-fds = %v", *maxOpenFds) } }
func testRlimit(t *testing.T, userns bool) { if testing.Short() { return } rootfs, err := newRootfs() ok(t, err) defer remove(rootfs) config := newTemplateConfig(rootfs) if userns { config.UidMappings = []configs.IDMap{{0, 0, 1000}} config.GidMappings = []configs.IDMap{{0, 0, 1000}} config.Namespaces = append(config.Namespaces, configs.Namespace{Type: configs.NEWUSER}) } // ensure limit is lower than what the config requests to test that in a user namespace // the Setrlimit call happens early enough that we still have permissions to raise the limit. ok(t, syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{ Max: 1024, Cur: 1024, })) out, _, err := runContainer(config, "", "/bin/sh", "-c", "ulimit -n") ok(t, err) if limit := strings.TrimSpace(out.Stdout.String()); limit != "1025" { t.Fatalf("expected rlimit to be 1025, got %s", limit) } }
// setOpenFilesLimit sets the open file limit in the kernel // cur is the soft limit, max is the ceiling (or hard limit) for that limit func (pm *ProcessManager) setOpenFilesLimit(cur, max uint64) error { var rLimit syscall.Rlimit // First check if the limits are already what we want err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } // If the current values are less than we want, set them if rLimit.Cur < cur || rLimit.Max < max { if cur > rLimit.Cur { rLimit.Cur = cur } if max > rLimit.Max { rLimit.Max = max } log.Infof("Setting open files limit (soft, hard) to (%v, %v)", rLimit.Cur, rLimit.Max) err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } } return nil }
// Set Max File Descriptor limits // // Background information: // // - SG docs // http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/sync-gateway/os-level-tuning/max-file-descriptors/index.html // - Related SG issues // https://github.com/couchbase/sync_gateway/issues/1083 // - Hard limit vs Soft limit // http://unix.stackexchange.com/questions/29577/ulimit-difference-between-hard-and-soft-limits func SetMaxFileDescriptors(requestedSoftFDLimit uint64) (uint64, error) { var limits syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil { return 0, err } requiresUpdate, recommendedSoftFDLimit := getSoftFDLimit( requestedSoftFDLimit, limits, ) // No call to Setrlimit required, because the requested soft limit is lower than current soft limit if !requiresUpdate { return 0, nil } // Update the soft limit (but don't bother updating the hard limit, since only root can do that, // and it's assumed that this process is not running as root) limits.Cur = recommendedSoftFDLimit err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits) if err == nil { Logf("Configured process to allow %d open file descriptors", recommendedSoftFDLimit) } return recommendedSoftFDLimit, err }
func TestMain(m *testing.M) { var lim syscall.Rlimit syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim) lim.Cur = 1024000 lim.Max = 1024000 syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim) }
func checkAndSetUlimit() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("error getting rlimit: %s", err) } ipfsFileDescNum := int64(ipfsFileDescNum) var setting bool if rLimit.Cur < ipfsFileDescNum { if rLimit.Max < ipfsFileDescNum { log.Error("adjusting max") rLimit.Max = ipfsFileDescNum } fmt.Printf("Adjusting current ulimit to %d...\n", ipfsFileDescNum) rLimit.Cur = ipfsFileDescNum setting = true } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("error setting ulimit: %s", err) } if setting { fmt.Printf("Successfully raised file descriptor limit to %d.\n", ipfsFileDescNum) } return nil }
// MaximizeOpenFileLimit tries to set the resource limit RLIMIT_NOFILE (number // of open file descriptors) to the max (hard limit), if the current (soft // limit) is below the max. Returns the new (though possibly unchanged) limit, // or an error if it could not be changed. func MaximizeOpenFileLimit() (int, error) { // Get the current limit on number of open files. var lim syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { return 0, err } // If we're already at max, there's no need to try to raise the limit. if lim.Cur >= lim.Max { return int(lim.Cur), nil } // Try to increase the limit to the max. oldLimit := lim.Cur lim.Cur = lim.Max if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { return int(oldLimit), err } // If the set succeeded, perform a new get to see what happened. We might // have gotten a value lower than the one in lim.Max, if lim.Max was // something that indicated "unlimited" (i.e. intmax). if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { // We don't really know the correct value here since Getrlimit // mysteriously failed after working once... Shouldn't ever happen, I // think. return 0, err } return int(lim.Cur), nil }
func setRlimit(resource int, value int) { rlimit := &syscall.Rlimit{Cur: uint64(value), Max: uint64(value)} err := syscall.Setrlimit(resource, rlimit) if err != nil { log.Fatalln("Error Setting Rlimit ", err) } }
func RestoreLimits(limits []*Limit) error { for _, v := range limits { if err := syscall.Setrlimit(v.Resource, v.Rlimit); err != nil { return err } } return nil }
// upLimitToHard ups a resource limit identified // by the resource argument to it's hard limit // the returned error is either from syscall.(Get|Set)rlimit func upLimitToHard(resource int) error { var resLimit = new(syscall.Rlimit) if err := syscall.Getrlimit(resource, resLimit); err != nil { return err } resLimit.Cur = resLimit.Max return syscall.Setrlimit(resource, resLimit) }
func initLimit() { var rLimit syscall.Rlimit rLimit.Max = 10000 rLimit.Cur = 10000 err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { fmt.Println("Error Setting Rlimit ", err) } }
func setupRlimits(config *configs.Config) error { for _, rlimit := range config.Rlimits { l := &syscall.Rlimit{Max: rlimit.Hard, Cur: rlimit.Soft} if err := syscall.Setrlimit(rlimit.Type, l); err != nil { return fmt.Errorf("error setting rlimit type %v: %v", rlimit.Type, err) } } return nil }
func main() { log.Println("livesyncd running") log.Println("Stop with [CTRL] + [c]") log.Println("Ignore: ", config.Ignore) rlimit := new(syscall.Rlimit) syscall.Getrlimit(syscall.RLIMIT_NOFILE, rlimit) rlimit.Cur = rlimit.Max err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, rlimit) if err != nil { log.Panicf("Could not change Rlimit: %q", err) } watcher, err := fsnotify.NewWatcher() if err != nil { log.Panicln(err) } events := make(chan *SyncEvent) quitSync := make(chan bool) quitWatcher := make(chan bool) sigInt := make(chan os.Signal) signal.Notify(sigInt, os.Interrupt) StartSFTPSync(events, quitSync, config) startWatchLoop(events, quitWatcher, watcher) watched := addWatchesRecursive(root, watcher) log.Printf("Found %d directories to watch\n", watched) select { case <-sigInt: log.Println("Stopping to watch...") // Wait until the watcher has finished quitting quitWatcher <- true log.Println("Done") // Close all file handles, opened by the watcher watcher.Close() log.Println("Stopping Sync Backend...") quitSync <- true <-quitSync log.Println("Done") return } }
func init() { var rLimit syscall.Rlimit rLimit.Max = 4096 rLimit.Cur = 4096 err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Println("Warning: setrlimit:", err) } }
// 提升程序可打开的文件描述符上限 func init() { var rlim syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil { panic(err.Error()) } rlim.Cur = 1000000 rlim.Max = 1000000 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil { panic(err.Error()) } }
func SetCurrentMaxFileNum(limit uint64) (err error) { rLimit := syscall.Rlimit{ Max: limit, Cur: limit, } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return fmt.Errorf("syscall.SetrLimit: %s", err) } return }
func executeProgram(name string, args []string) { cpuLim := new(syscall.Rlimit) (*cpuLim).Max = 28 (*cpuLim).Cur = 28 if err := syscall.Setrlimit(6, cpuLim); err != nil { fmt.Println("Error here") panic(err) } cmd := exec.Command(name, args[1:]...) out, _ := cmd.CombinedOutput() fmt.Printf("%s", out) }
// For all unixes we need to bump allowed number of open files to a // higher value than its usual default of '1024'. The reasoning is // that this value is too small for a server. func setMaxOpenFiles() error { var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { return err } // Set the current limit to Max, it is usually around 4096. // TO increase this limit further user has to manually edit // `/etc/security/limits.conf` rLimit.Cur = rLimit.Max return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) }
func NewServer() *Server { s := &Server{ port: common.GetConfInt("server", "port", 8080), maxClients: common.GetConfInt("server", "max_clients", 100000), clientNum: 0, acceptTimeout: common.GetConfSecond("server", "accept_timeout", 60*5), connTimeout: common.GetConfSecond("server", "connection_timeout", 60*3), headerLen: common.GetConfInt("server", "header_length", 10), maxBodyLen: common.GetConfInt("server", "max_body_length", 102400), running: false, heartbeat: time.Now().Unix(), workerNum: common.WorkerNum, currentWorker: -1, slowread: common.GetConfSecond("server", "slow_read", 0), logger: common.NewLogger("server"), } if s == nil { fmt.Println("new server failed") return nil } if s.logger == nil { fmt.Println("new server logger failed") return nil } ip, err := common.Conf.String("server", "bind") if err != nil { ip = "" } s.ip = ip max_clients := uint64(s.maxClients) if max_clients > 1024 { var rlim syscall.Rlimit err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Println("server get rlimit error: " + err.Error()) return nil } rlim.Cur = max_clients rlim.Max = max_clients err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) if err != nil { fmt.Println("server set rlimit error: " + err.Error()) return nil } s.logger.Info("set fd limit to %d", s.maxClients) } return s }
func main() { logging.SetFormatter(logging.MustStringFormatter(format)) logging.SetLevel(logging.INFO, "gogorobot") //logging.SetLevel(logging.DEBUG, "gogorobot") // // Set the file descriptor limit higher if we've permission var rLimit syscall.Rlimit err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Info(fmt.Sprintf("Error geting rlimit: %s", err)) } rLimit.Max = 65536 rLimit.Cur = 65536 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) if err != nil { log.Info(fmt.Sprintf("Error setting rlimit: %s", err)) } // fetchPipeline := make(chan FetchRequest) savePipeline := make(chan RobotResponse) // NOTE: Important to pass via pointer // Otherwise, a new WaitGroup is created var fetchGroup sync.WaitGroup var saveGroup sync.WaitGroup saveGroup.Add(1) go saveRobots(savePipeline, &saveGroup) for i := 0; i < 50; i++ { fetchGroup.Add(1) go fetchRobot(fetchPipeline, savePipeline, &fetchGroup) } // Insert entries reader := bufio.NewReader(os.Stdin) for { domain, err := reader.ReadString('\n') if err != nil { break } domain = strings.TrimRight(domain, "\r\n") log.Debug(fmt.Sprintf("MAIN: Providing %s to fetch pipeline", domain)) fetchPipeline <- FetchRequest{domain, 0} } // TODO: Temporary fix to allow time for the pipeline to clear (see TODO in fetcher's error area) time.Sleep(60 * time.Second) close(fetchPipeline) log.Notice("Fetching pipeline closed -- waiting for pending fetches to complete") // fetchGroup.Wait() close(savePipeline) saveGroup.Wait() }
func getMaxOpenFiles() int { var rlim syscall.Rlimit var err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim) if err == nil && rlim.Cur < rlim.Max { rlim.Cur = rlim.Max syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim) } var maxOpenFiles = 1024 if maxOpenFiles < int(rlim.Cur) { maxOpenFiles = int(rlim.Cur) } return maxOpenFiles }
func setUlimit(cfg *Config) error { var rLimit syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { return err } if cfg.NoFiles == 0 { rLimit.Max = 1000000 } else { rLimit.Max = cfg.NoFiles } rLimit.Cur = rLimit.Max return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) }