Skip to content

Commit 3292ea5

Browse files
committed
pkg/seccomp: use sync.Once to speed up IsEnabled
It does not make sense to check if seccomp is supported by the kernel more than once per runtime, so let's use sync.Once to speed it up. A quick benchmark (old implementation, before this commit, after): BenchmarkIsEnabledOld-4 37183 27971 ns/op BenchmarkIsEnabled-4 1252161 947 ns/op BenchmarkIsEnabledOnce-4 666274008 2.14 ns/op Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 00b5c99 commit 3292ea5

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

pkg/seccomp/seccomp_linux.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@
3333
package seccomp
3434

3535
import (
36+
"sync"
37+
3638
"golang.org/x/sys/unix"
3739
)
3840

41+
var (
42+
enabled bool
43+
enabledOnce sync.Once
44+
)
45+
3946
// isEnabled returns whether the kernel has been configured to support seccomp
4047
// (including the check for CONFIG_SECCOMP_FILTER kernel option).
4148
func isEnabled() bool {
@@ -65,5 +72,9 @@ func isEnabled() bool {
6572
// EFAULT). IOW, EINVAL means "seccomp not supported", any other error
6673
// means it is supported.
6774

68-
return unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0) != unix.EINVAL
75+
enabledOnce.Do(func() {
76+
enabled = unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0) != unix.EINVAL
77+
})
78+
79+
return enabled
6980
}

0 commit comments

Comments
 (0)