Skip to content

Commit

Permalink
Increase open file limit to maximum possible value
Browse files Browse the repository at this point in the history
Opening a lot of modules for processing requires high open-file-limit
value. The default value is 1024 which is too low.

Try to increase process limit to infinity first. If it does not work
then try to increase soft limit to hard limit value.

Closes #76
  • Loading branch information
anatol committed Oct 24, 2021
1 parent f428a07 commit 29993f8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions generator/kmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
func TestModuleNames(t *testing.T) {
t.Parallel()

increaseOpenFileLimit()

ver, err := readKernelVersion()
require.NoError(t, err)

Expand Down
2 changes: 2 additions & 0 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func runGenerator() error {
defer pprof.StopCPUProfile()
}

increaseOpenFileLimit()

conf, err := readGeneratorConfig(*configFile)
if err != nil {
return err
Expand Down
38 changes: 37 additions & 1 deletion generator/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "regexp"
import (
"regexp"

"golang.org/x/sys/unix"
)

// parseProperties parses input in form of "PROP1=VAL1\nPROP2=VAL2\n..." into a map
func parseProperties(data string) map[string]string {
Expand All @@ -13,3 +17,35 @@ func parseProperties(data string) map[string]string {

return result
}

// Opening a lot of module files in parallel requires high limit of open file descriptors
func increaseOpenFileLimit() {
limit := unix.Rlimit{
Cur: unix.RLIM_INFINITY,
Max: unix.RLIM_INFINITY,
}

// first try to set the process limit to infinity
if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit); err == nil {
// it worked!
return
}

// if the current process unprivileged then the only thing we can do is to set soft limit to max limit value

if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil {
warning("unable to get open file limit: %v", err)
return
}

if limit.Cur >= limit.Max {
return // nothing to increase
}

debug("increasing open file limit %d->%d", limit.Cur, limit.Max)
limit.Cur = limit.Max

if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit); err != nil {
warning("unable to increase rlimit: %v", err)
}
}

0 comments on commit 29993f8

Please sign in to comment.