Skip to content

Commit 29993f8

Browse files
committed
Increase open file limit to maximum possible value
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
1 parent f428a07 commit 29993f8

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Diff for: generator/kmod_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
func TestModuleNames(t *testing.T) {
2121
t.Parallel()
2222

23+
increaseOpenFileLimit()
24+
2325
ver, err := readKernelVersion()
2426
require.NoError(t, err)
2527

Diff for: generator/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func runGenerator() error {
6565
defer pprof.StopCPUProfile()
6666
}
6767

68+
increaseOpenFileLimit()
69+
6870
conf, err := readGeneratorConfig(*configFile)
6971
if err != nil {
7072
return err

Diff for: generator/util.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package main
22

3-
import "regexp"
3+
import (
4+
"regexp"
5+
6+
"golang.org/x/sys/unix"
7+
)
48

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

1418
return result
1519
}
20+
21+
// Opening a lot of module files in parallel requires high limit of open file descriptors
22+
func increaseOpenFileLimit() {
23+
limit := unix.Rlimit{
24+
Cur: unix.RLIM_INFINITY,
25+
Max: unix.RLIM_INFINITY,
26+
}
27+
28+
// first try to set the process limit to infinity
29+
if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit); err == nil {
30+
// it worked!
31+
return
32+
}
33+
34+
// if the current process unprivileged then the only thing we can do is to set soft limit to max limit value
35+
36+
if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil {
37+
warning("unable to get open file limit: %v", err)
38+
return
39+
}
40+
41+
if limit.Cur >= limit.Max {
42+
return // nothing to increase
43+
}
44+
45+
debug("increasing open file limit %d->%d", limit.Cur, limit.Max)
46+
limit.Cur = limit.Max
47+
48+
if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit); err != nil {
49+
warning("unable to increase rlimit: %v", err)
50+
}
51+
}

0 commit comments

Comments
 (0)