From eb4b3e8772d1aed68a574160a2f8f88ac7526e6f Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 26 Jul 2019 17:48:00 +0000 Subject: [PATCH] Fast path getting pid from task Signed-off-by: Michael Crosby --- runtime/task.go | 3 +++ runtime/v1/linux/task.go | 5 +++++ runtime/v2/shim.go | 5 +++++ services/tasks/local.go | 13 ++++++------- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/runtime/task.go b/runtime/task.go index 981e290c6..ab9017ba5 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -33,6 +33,7 @@ type TaskInfo struct { // Process is a runtime object for an executing process inside a container type Process interface { + // ID of the process ID() string // State returns the process state State(context.Context) (State, error) @@ -54,6 +55,8 @@ type Process interface { type Task interface { Process + // PID of the process + PID() uint32 // Namespace that the task exists in Namespace() string // Pause pauses the container process diff --git a/runtime/v1/linux/task.go b/runtime/v1/linux/task.go index 043f25b17..0970c3ea3 100644 --- a/runtime/v1/linux/task.go +++ b/runtime/v1/linux/task.go @@ -84,6 +84,11 @@ func (t *Task) Namespace() string { return t.namespace } +// PID of the task +func (t *Task) PID() uint32 { + return uint32(t.pid) +} + // Delete the task and return the exit status func (t *Task) Delete(ctx context.Context) (*runtime.Exit, error) { rsp, err := t.shim.Delete(ctx, empty) diff --git a/runtime/v2/shim.go b/runtime/v2/shim.go index 41f908c3e..38cf79899 100644 --- a/runtime/v2/shim.go +++ b/runtime/v2/shim.go @@ -195,6 +195,11 @@ func (s *shim) ID() string { return s.bundle.ID } +// PID of the task +func (s *shim) PID() uint32 { + return uint32(s.taskPid) +} + func (s *shim) Namespace() string { return s.bundle.Namespace } diff --git a/services/tasks/local.go b/services/tasks/local.go index 163ed0186..2f17b158c 100644 --- a/services/tasks/local.go +++ b/services/tasks/local.go @@ -182,24 +182,23 @@ func (l *local) Create(ctx context.Context, r *api.CreateTaskRequest, _ ...grpc. if err != nil { return nil, err } - if _, err := rtime.Get(ctx, r.ContainerID); err != runtime.ErrTaskNotExists { + _, err = rtime.Get(ctx, r.ContainerID) + if err != nil && err != runtime.ErrTaskNotExists { + return nil, errdefs.ToGRPC(err) + } + if err == nil { return nil, errdefs.ToGRPC(fmt.Errorf("task %s already exists", r.ContainerID)) } c, err := rtime.Create(ctx, r.ContainerID, opts) if err != nil { return nil, errdefs.ToGRPC(err) } - // TODO: fast path for getting pid on create if err := l.monitor.Monitor(c); err != nil { return nil, errors.Wrap(err, "monitor task") } - state, err := c.State(ctx) - if err != nil { - log.G(ctx).Error(err) - } return &api.CreateTaskResponse{ ContainerID: r.ContainerID, - Pid: state.Pid, + Pid: c.PID(), }, nil }