Cache shim v2 exec.LookPath results

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
Justin Terry (VM) 2018-11-27 15:11:04 -08:00
parent 3eae8b9c3f
commit 09bf314bfd
3 changed files with 53 additions and 13 deletions

View File

@ -24,6 +24,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/namespaces"
@ -32,6 +33,8 @@ import (
const shimBinaryFormat = "containerd-shim-%s-%s" const shimBinaryFormat = "containerd-shim-%s-%s"
var runtimePaths sync.Map
// Command returns the shim command with the provided args and configuration // 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) { func Command(ctx context.Context, runtime, containerdAddress, path string, cmdArgs ...string) (*exec.Cmd, error) {
ns, err := namespaces.NamespaceRequired(ctx) ns, err := namespaces.NamespaceRequired(ctx)
@ -52,19 +55,30 @@ func Command(ctx context.Context, runtime, containerdAddress, path string, cmdAr
if name == "" { if name == "" {
return nil, fmt.Errorf("invalid runtime name %s, correct runtime name should format like io.containerd.runc.v1", runtime) return nil, fmt.Errorf("invalid runtime name %s, correct runtime name should format like io.containerd.runc.v1", runtime)
} }
var cmdPath string var cmdPath string
var lerr error cmdPathI, cmdPathFound := runtimePaths.Load(name)
if cmdPath, lerr = exec.LookPath(name); lerr != nil { if cmdPathFound {
if eerr, ok := lerr.(*exec.Error); ok { cmdPath = cmdPathI.(string)
if eerr.Err == exec.ErrNotFound { } else {
return nil, errors.Wrapf(os.ErrNotExist, "runtime %q binary not installed %q", runtime, name) 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 := exec.Command(cmdPath, args...)
cmd.Dir = path cmd.Dir = path
cmd.Env = append(os.Environ(), "GOMAXPROCS=2") cmd.Env = append(os.Environ(), "GOMAXPROCS=2")

View File

@ -33,7 +33,7 @@ golang.org/x/sync 450f422ab23cf9881c94e2db30cac0eb1b7cf80c
github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895 github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895
github.com/grpc-ecosystem/go-grpc-prometheus 6b7015e65d366bf3f19b2b2a000a831940f0f7e0 github.com/grpc-ecosystem/go-grpc-prometheus 6b7015e65d366bf3f19b2b2a000a831940f0f7e0
github.com/Microsoft/go-winio v0.4.11 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 google.golang.org/genproto d80a6e20e776b0b17a324d0ba1ab50a39c8e8944
golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4 golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4
github.com/containerd/ttrpc 2a805f71863501300ae1976d29f0454ae003e85a github.com/containerd/ttrpc 2a805f71863501300ae1976d29f0454ae003e85a

View File

@ -9,6 +9,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"sync/atomic"
irunhcs "github.com/Microsoft/hcsshim/internal/runhcs" irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
"github.com/containerd/go-runc" "github.com/containerd/go-runc"
@ -23,10 +24,35 @@ const (
Text Format = "text" Text Format = "text"
// JSON is the JSON formatted log output. // JSON is the JSON formatted log output.
JSON Format = "json" 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{ var bytesBufferPool = sync.Pool{
New: func() interface{} { New: func() interface{} {
return bytes.NewBuffer(nil) return bytes.NewBuffer(nil)
@ -84,7 +110,7 @@ func (r *Runhcs) args() []string {
} }
func (r *Runhcs) command(context context.Context, args ...string) *exec.Cmd { 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() cmd.Env = os.Environ()
return cmd return cmd
} }