From 09bf314bfd77a981b9b2e81aece880c7c13f7803 Mon Sep 17 00:00:00 2001 From: "Justin Terry (VM)" Date: Tue, 27 Nov 2018 15:11:04 -0800 Subject: [PATCH] Cache shim v2 exec.LookPath results Signed-off-by: Justin Terry (VM) --- runtime/v2/shim/util.go | 32 +++++++++++++------ vendor.conf | 2 +- .../Microsoft/hcsshim/pkg/go-runhcs/runhcs.go | 32 +++++++++++++++++-- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/runtime/v2/shim/util.go b/runtime/v2/shim/util.go index b7034ce50..77afaa1f3 100644 --- a/runtime/v2/shim/util.go +++ b/runtime/v2/shim/util.go @@ -24,6 +24,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync" "time" "github.com/containerd/containerd/namespaces" @@ -32,6 +33,8 @@ import ( const shimBinaryFormat = "containerd-shim-%s-%s" +var runtimePaths sync.Map + // Command returns the shim command with the provided args and configuration func Command(ctx context.Context, runtime, containerdAddress, path string, cmdArgs ...string) (*exec.Cmd, error) { ns, err := namespaces.NamespaceRequired(ctx) @@ -52,19 +55,30 @@ func Command(ctx context.Context, runtime, containerdAddress, path string, cmdAr if name == "" { return nil, fmt.Errorf("invalid runtime name %s, correct runtime name should format like io.containerd.runc.v1", runtime) } + var cmdPath string - var lerr error - if cmdPath, lerr = exec.LookPath(name); lerr != nil { - if eerr, ok := lerr.(*exec.Error); ok { - if eerr.Err == exec.ErrNotFound { - return nil, errors.Wrapf(os.ErrNotExist, "runtime %q binary not installed %q", runtime, name) + cmdPathI, cmdPathFound := runtimePaths.Load(name) + if cmdPathFound { + cmdPath = cmdPathI.(string) + } else { + var lerr error + if cmdPath, lerr = exec.LookPath(name); lerr != nil { + if eerr, ok := lerr.(*exec.Error); ok { + if eerr.Err == exec.ErrNotFound { + return nil, errors.Wrapf(os.ErrNotExist, "runtime %q binary not installed %q", runtime, name) + } } } + cmdPath, err = filepath.Abs(cmdPath) + if err != nil { + return nil, err + } + if cmdPathI, cmdPathFound = runtimePaths.LoadOrStore(name, cmdPath); cmdPathFound { + // We didn't store cmdPath we loaded an already cached value. Use it. + cmdPath = cmdPathI.(string) + } } - cmdPath, err = filepath.Abs(cmdPath) - if err != nil { - return nil, err - } + cmd := exec.Command(cmdPath, args...) cmd.Dir = path cmd.Env = append(os.Environ(), "GOMAXPROCS=2") diff --git a/vendor.conf b/vendor.conf index efba4b6bf..9e29a7fbc 100644 --- a/vendor.conf +++ b/vendor.conf @@ -33,7 +33,7 @@ golang.org/x/sync 450f422ab23cf9881c94e2db30cac0eb1b7cf80c github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895 github.com/grpc-ecosystem/go-grpc-prometheus 6b7015e65d366bf3f19b2b2a000a831940f0f7e0 github.com/Microsoft/go-winio v0.4.11 -github.com/Microsoft/hcsshim v0.8.1 +github.com/Microsoft/hcsshim v0.8.2 google.golang.org/genproto d80a6e20e776b0b17a324d0ba1ab50a39c8e8944 golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4 github.com/containerd/ttrpc 2a805f71863501300ae1976d29f0454ae003e85a diff --git a/vendor/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs.go b/vendor/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs.go index 24dc467e4..5d5205abd 100644 --- a/vendor/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs.go +++ b/vendor/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strings" "sync" + "sync/atomic" irunhcs "github.com/Microsoft/hcsshim/internal/runhcs" "github.com/containerd/go-runc" @@ -23,10 +24,35 @@ const ( Text Format = "text" // JSON is the JSON formatted log output. JSON Format = "json" - - command = "runhcs" ) +var runhcsPath atomic.Value + +func getCommandPath() string { + const command = "runhcs.exe" + + pathi := runhcsPath.Load() + if pathi == nil { + path, err := exec.LookPath(command) + if err != nil { + // Failed to look up command just use it directly and let the + // Windows loader find it. + path = command + runhcsPath.Store(path) + return path + } + apath, err := filepath.Abs(path) + if err != nil { + // We couldnt make `path` an `AbsPath`. Just use `path` directly and + // let the Windows loader find it. + apath = path + } + runhcsPath.Store(apath) + return apath + } + return pathi.(string) +} + var bytesBufferPool = sync.Pool{ New: func() interface{} { return bytes.NewBuffer(nil) @@ -84,7 +110,7 @@ func (r *Runhcs) args() []string { } func (r *Runhcs) command(context context.Context, args ...string) *exec.Cmd { - cmd := exec.CommandContext(context, command, append(r.args(), args...)...) + cmd := exec.CommandContext(context, getCommandPath(), append(r.args(), args...)...) cmd.Env = os.Environ() return cmd }