libct/specconv: rm some init allocations
Eliminate some of these allocations when starting runc:
> init github.com/opencontainers/runc/libcontainer/specconv ms, 0.11 ms clock, 5408 bytes, 70 allocs
Most of this (4K) is the two regexes, which are left intact for now.
@@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"strings"
+ "sync"
"time"
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
@@ -24,7 +25,15 @@ import (
"golang.org/x/sys/unix"
)
-var namespaceMapping = map[specs.LinuxNamespaceType]configs.NamespaceType{
+var (
+ initMapsOnce sync.Once
+ namespaceMapping map[specs.LinuxNamespaceType]configs.NamespaceType
+ mountPropagationMapping map[string]int
+)
+
+func initMaps() {
+ initMapsOnce.Do(func() {
+ namespaceMapping = map[specs.LinuxNamespaceType]configs.NamespaceType{
specs.PIDNamespace: configs.NEWPID,
specs.NetworkNamespace: configs.NEWNET,
specs.MountNamespace: configs.NEWNS,
@@ -34,7 +43,7 @@ var namespaceMapping = map[specs.LinuxNamespaceType]configs.NamespaceType{
specs.CgroupNamespace: configs.NEWCGROUP,
}
-var mountPropagationMapping = map[string]int{
+ mountPropagationMapping = map[string]int{
"rprivate": unix.MS_PRIVATE | unix.MS_REC,
"private": unix.MS_PRIVATE,
"rslave": unix.MS_SLAVE | unix.MS_REC,
@@ -45,6 +54,8 @@ var mountPropagationMapping = map[string]int{
"unbindable": unix.MS_UNBINDABLE,
"": 0,
}
+ })
+}
// AllowedDevices is the set of devices which are automatically included for
// all containers.
@@ -256,6 +267,8 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
config.Cgroups = c
// set linux-specific config
if spec.Linux != nil {
+ initMaps()
+
var exists bool
if config.RootPropagation, exists = mountPropagationMapping[spec.Linux.RootfsPropagation]; !exists {
return nil, fmt.Errorf("rootfsPropagation=%v is not supported", spec.Linux.RootfsPropagation)
