2015-12-09 22:18:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-04-26 20:29:35 +00:00
|
|
|
"bufio"
|
2015-12-09 22:18:31 +00:00
|
|
|
"errors"
|
2016-02-01 19:02:41 +00:00
|
|
|
"fmt"
|
2016-04-26 20:29:35 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-12-09 22:18:31 +00:00
|
|
|
"syscall"
|
2016-02-12 18:17:59 +00:00
|
|
|
"time"
|
2015-12-09 22:18:31 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
2016-05-09 18:38:28 +00:00
|
|
|
"github.com/docker/containerd"
|
2015-12-09 22:18:31 +00:00
|
|
|
"github.com/docker/containerd/api/grpc/types"
|
|
|
|
"github.com/docker/containerd/runtime"
|
2015-12-18 00:07:04 +00:00
|
|
|
"github.com/docker/containerd/supervisor"
|
2015-12-09 22:18:31 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apiServer struct {
|
2015-12-18 00:07:04 +00:00
|
|
|
sv *supervisor.Supervisor
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns grpc server instance
|
2015-12-18 00:07:04 +00:00
|
|
|
func NewServer(sv *supervisor.Supervisor) types.APIServer {
|
2015-12-09 22:18:31 +00:00
|
|
|
return &apiServer{
|
|
|
|
sv: sv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 18:38:28 +00:00
|
|
|
func (s *apiServer) GetServerVersion(ctx context.Context, c *types.GetServerVersionRequest) (*types.GetServerVersionResponse, error) {
|
|
|
|
return &types.GetServerVersionResponse{
|
|
|
|
Major: containerd.VersionMajor,
|
|
|
|
Minor: containerd.VersionMinor,
|
|
|
|
Patch: containerd.VersionPatch,
|
|
|
|
Revision: containerd.GitCommit,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
func (s *apiServer) CreateContainer(ctx context.Context, c *types.CreateContainerRequest) (*types.CreateContainerResponse, error) {
|
|
|
|
if c.BundlePath == "" {
|
|
|
|
return nil, errors.New("empty bundle path")
|
|
|
|
}
|
2016-02-17 18:55:54 +00:00
|
|
|
e := &supervisor.StartTask{}
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = c.Id
|
|
|
|
e.BundlePath = c.BundlePath
|
2016-02-03 21:56:15 +00:00
|
|
|
e.Stdin = c.Stdin
|
|
|
|
e.Stdout = c.Stdout
|
|
|
|
e.Stderr = c.Stderr
|
2016-02-11 21:44:25 +00:00
|
|
|
e.Labels = c.Labels
|
2016-03-30 21:25:42 +00:00
|
|
|
e.NoPivotRoot = c.NoPivotRoot
|
2016-05-23 22:43:47 +00:00
|
|
|
e.Runtime = c.Runtime
|
|
|
|
e.RuntimeArgs = c.RuntimeArgs
|
2015-12-18 00:07:04 +00:00
|
|
|
e.StartResponse = make(chan supervisor.StartResponse, 1)
|
2016-04-26 20:29:35 +00:00
|
|
|
if c.Checkpoint != "" {
|
2016-05-26 00:42:37 +00:00
|
|
|
e.CheckpointDir = c.CheckpointDir
|
2016-04-26 20:29:35 +00:00
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: c.Checkpoint,
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 01:26:24 +00:00
|
|
|
s.sv.SendTask(e)
|
2016-02-17 18:55:54 +00:00
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
2015-12-09 22:18:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-11 19:56:11 +00:00
|
|
|
r := <-e.StartResponse
|
2016-02-12 22:01:24 +00:00
|
|
|
apiC, err := createAPIContainer(r.Container, false)
|
2016-02-11 19:56:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.CreateContainerResponse{
|
|
|
|
Container: apiC,
|
|
|
|
}, nil
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 20:29:35 +00:00
|
|
|
func (s *apiServer) CreateCheckpoint(ctx context.Context, r *types.CreateCheckpointRequest) (*types.CreateCheckpointResponse, error) {
|
|
|
|
e := &supervisor.CreateCheckpointTask{}
|
|
|
|
e.ID = r.Id
|
2016-05-26 00:42:37 +00:00
|
|
|
e.CheckpointDir = r.CheckpointDir
|
2016-04-26 20:29:35 +00:00
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: r.Checkpoint.Name,
|
|
|
|
Exit: r.Checkpoint.Exit,
|
2016-06-03 22:00:49 +00:00
|
|
|
TCP: r.Checkpoint.Tcp,
|
2016-04-26 20:29:35 +00:00
|
|
|
UnixSockets: r.Checkpoint.UnixSockets,
|
|
|
|
Shell: r.Checkpoint.Shell,
|
|
|
|
}
|
2016-05-26 00:42:37 +00:00
|
|
|
|
2016-04-26 20:29:35 +00:00
|
|
|
s.sv.SendTask(e)
|
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.CreateCheckpointResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) DeleteCheckpoint(ctx context.Context, r *types.DeleteCheckpointRequest) (*types.DeleteCheckpointResponse, error) {
|
|
|
|
if r.Name == "" {
|
|
|
|
return nil, errors.New("checkpoint name cannot be empty")
|
|
|
|
}
|
|
|
|
e := &supervisor.DeleteCheckpointTask{}
|
|
|
|
e.ID = r.Id
|
2016-05-26 00:42:37 +00:00
|
|
|
e.CheckpointDir = r.CheckpointDir
|
2016-04-26 20:29:35 +00:00
|
|
|
e.Checkpoint = &runtime.Checkpoint{
|
|
|
|
Name: r.Name,
|
|
|
|
}
|
|
|
|
s.sv.SendTask(e)
|
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.DeleteCheckpointResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) ListCheckpoint(ctx context.Context, r *types.ListCheckpointRequest) (*types.ListCheckpointResponse, error) {
|
|
|
|
e := &supervisor.GetContainersTask{}
|
|
|
|
s.sv.SendTask(e)
|
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var container runtime.Container
|
|
|
|
for _, c := range e.Containers {
|
|
|
|
if c.ID() == r.Id {
|
|
|
|
container = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if container == nil {
|
|
|
|
return nil, grpc.Errorf(codes.NotFound, "no such containers")
|
|
|
|
}
|
|
|
|
var out []*types.Checkpoint
|
2016-05-26 00:42:37 +00:00
|
|
|
checkpoints, err := container.Checkpoints(r.CheckpointDir)
|
2016-04-26 20:29:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, c := range checkpoints {
|
|
|
|
out = append(out, &types.Checkpoint{
|
|
|
|
Name: c.Name,
|
2016-06-03 22:00:49 +00:00
|
|
|
Tcp: c.TCP,
|
2016-04-26 20:29:35 +00:00
|
|
|
Shell: c.Shell,
|
|
|
|
UnixSockets: c.UnixSockets,
|
|
|
|
// TODO: figure out timestamp
|
|
|
|
//Timestamp: c.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &types.ListCheckpointResponse{Checkpoints: out}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
func (s *apiServer) Signal(ctx context.Context, r *types.SignalRequest) (*types.SignalResponse, error) {
|
2016-02-17 18:55:54 +00:00
|
|
|
e := &supervisor.SignalTask{}
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
2016-02-17 18:55:54 +00:00
|
|
|
e.PID = r.Pid
|
2015-12-09 22:18:31 +00:00
|
|
|
e.Signal = syscall.Signal(int(r.Signal))
|
2016-02-12 01:26:24 +00:00
|
|
|
s.sv.SendTask(e)
|
2016-02-17 18:55:54 +00:00
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
2015-12-09 22:18:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.SignalResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) State(ctx context.Context, r *types.StateRequest) (*types.StateResponse, error) {
|
2016-02-17 18:55:54 +00:00
|
|
|
e := &supervisor.GetContainersTask{}
|
2016-02-11 20:20:29 +00:00
|
|
|
e.ID = r.Id
|
2016-02-12 01:26:24 +00:00
|
|
|
s.sv.SendTask(e)
|
2016-02-17 18:55:54 +00:00
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
2015-12-09 22:18:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := s.sv.Machine()
|
|
|
|
state := &types.StateResponse{
|
|
|
|
Machine: &types.Machine{
|
|
|
|
Cpus: uint32(m.Cpus),
|
2016-02-11 23:55:19 +00:00
|
|
|
Memory: uint64(m.Memory),
|
2015-12-09 22:18:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range e.Containers {
|
2016-02-12 22:01:24 +00:00
|
|
|
apiC, err := createAPIContainer(c, true)
|
2015-12-09 22:18:31 +00:00
|
|
|
if err != nil {
|
2016-02-11 19:56:11 +00:00
|
|
|
return nil, err
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
2016-02-11 19:56:11 +00:00
|
|
|
state.Containers = append(state.Containers, apiC)
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 22:01:24 +00:00
|
|
|
func createAPIContainer(c runtime.Container, getPids bool) (*types.Container, error) {
|
2016-02-11 19:56:11 +00:00
|
|
|
processes, err := c.Processes()
|
|
|
|
if err != nil {
|
2016-03-25 04:20:56 +00:00
|
|
|
return nil, grpc.Errorf(codes.Internal, "get processes for container: "+err.Error())
|
2016-02-11 19:56:11 +00:00
|
|
|
}
|
|
|
|
var procs []*types.Process
|
|
|
|
for _, p := range processes {
|
|
|
|
oldProc := p.Spec()
|
|
|
|
stdio := p.Stdio()
|
2016-04-26 20:29:35 +00:00
|
|
|
proc := &types.Process{
|
2016-02-11 19:56:11 +00:00
|
|
|
Pid: p.ID(),
|
|
|
|
SystemPid: uint32(p.SystemPid()),
|
|
|
|
Terminal: oldProc.Terminal,
|
|
|
|
Args: oldProc.Args,
|
|
|
|
Env: oldProc.Env,
|
|
|
|
Cwd: oldProc.Cwd,
|
|
|
|
Stdin: stdio.Stdin,
|
|
|
|
Stdout: stdio.Stdout,
|
|
|
|
Stderr: stdio.Stderr,
|
2016-02-26 03:42:37 +00:00
|
|
|
}
|
2016-04-26 20:29:35 +00:00
|
|
|
proc.User = &types.User{
|
|
|
|
Uid: oldProc.User.UID,
|
|
|
|
Gid: oldProc.User.GID,
|
|
|
|
AdditionalGids: oldProc.User.AdditionalGids,
|
|
|
|
}
|
|
|
|
proc.Capabilities = oldProc.Capabilities
|
|
|
|
proc.ApparmorProfile = oldProc.ApparmorProfile
|
|
|
|
proc.SelinuxLabel = oldProc.SelinuxLabel
|
|
|
|
proc.NoNewPrivileges = oldProc.NoNewPrivileges
|
|
|
|
for _, rl := range oldProc.Rlimits {
|
|
|
|
proc.Rlimits = append(proc.Rlimits, &types.Rlimit{
|
|
|
|
Type: rl.Type,
|
|
|
|
Soft: rl.Soft,
|
|
|
|
Hard: rl.Hard,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
procs = append(procs, proc)
|
2016-02-11 19:56:11 +00:00
|
|
|
}
|
2016-02-12 22:01:24 +00:00
|
|
|
var pids []int
|
2016-05-09 20:29:14 +00:00
|
|
|
state := c.State()
|
2016-02-25 20:59:34 +00:00
|
|
|
if getPids && (state == runtime.Running || state == runtime.Paused) {
|
2016-02-12 22:01:24 +00:00
|
|
|
if pids, err = c.Pids(); err != nil {
|
2016-03-25 04:20:56 +00:00
|
|
|
return nil, grpc.Errorf(codes.Internal, "get all pids for container: "+err.Error())
|
2016-02-12 22:01:24 +00:00
|
|
|
}
|
2016-02-11 22:07:34 +00:00
|
|
|
}
|
2016-02-11 19:56:11 +00:00
|
|
|
return &types.Container{
|
|
|
|
Id: c.ID(),
|
|
|
|
BundlePath: c.Path(),
|
|
|
|
Processes: procs,
|
2016-02-11 21:44:25 +00:00
|
|
|
Labels: c.Labels(),
|
2016-02-25 20:59:34 +00:00
|
|
|
Status: string(state),
|
2016-02-11 22:07:34 +00:00
|
|
|
Pids: toUint32(pids),
|
2016-02-25 20:59:34 +00:00
|
|
|
Runtime: c.Runtime(),
|
2016-02-11 19:56:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-11 22:07:34 +00:00
|
|
|
func toUint32(its []int) []uint32 {
|
|
|
|
o := []uint32{}
|
|
|
|
for _, i := range its {
|
|
|
|
o = append(o, uint32(i))
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
func (s *apiServer) UpdateContainer(ctx context.Context, r *types.UpdateContainerRequest) (*types.UpdateContainerResponse, error) {
|
2016-02-17 18:55:54 +00:00
|
|
|
e := &supervisor.UpdateTask{}
|
2015-12-09 22:18:31 +00:00
|
|
|
e.ID = r.Id
|
2016-01-27 22:19:10 +00:00
|
|
|
e.State = runtime.State(r.Status)
|
2016-03-07 23:23:52 +00:00
|
|
|
if r.Resources != nil {
|
|
|
|
rs := r.Resources
|
|
|
|
e.Resources = &runtime.Resource{}
|
|
|
|
if rs.CpuShares != 0 {
|
|
|
|
e.Resources.CPUShares = int64(rs.CpuShares)
|
|
|
|
}
|
|
|
|
if rs.BlkioWeight != 0 {
|
|
|
|
e.Resources.BlkioWeight = uint16(rs.BlkioWeight)
|
|
|
|
}
|
|
|
|
if rs.CpuPeriod != 0 {
|
|
|
|
e.Resources.CPUPeriod = int64(rs.CpuPeriod)
|
|
|
|
}
|
|
|
|
if rs.CpuQuota != 0 {
|
|
|
|
e.Resources.CPUQuota = int64(rs.CpuQuota)
|
|
|
|
}
|
|
|
|
if rs.CpusetCpus != "" {
|
|
|
|
e.Resources.CpusetCpus = rs.CpusetCpus
|
|
|
|
}
|
|
|
|
if rs.CpusetMems != "" {
|
|
|
|
e.Resources.CpusetMems = rs.CpusetMems
|
|
|
|
}
|
|
|
|
if rs.KernelMemoryLimit != 0 {
|
|
|
|
e.Resources.KernelMemory = int64(rs.KernelMemoryLimit)
|
|
|
|
}
|
2016-06-06 18:26:07 +00:00
|
|
|
if rs.KernelTCPMemoryLimit != 0 {
|
|
|
|
e.Resources.KernelTCPMemory = int64(rs.KernelTCPMemoryLimit)
|
|
|
|
}
|
2016-03-07 23:23:52 +00:00
|
|
|
if rs.MemoryLimit != 0 {
|
|
|
|
e.Resources.Memory = int64(rs.MemoryLimit)
|
|
|
|
}
|
|
|
|
if rs.MemoryReservation != 0 {
|
|
|
|
e.Resources.MemoryReservation = int64(rs.MemoryReservation)
|
|
|
|
}
|
|
|
|
if rs.MemorySwap != 0 {
|
|
|
|
e.Resources.MemorySwap = int64(rs.MemorySwap)
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 01:26:24 +00:00
|
|
|
s.sv.SendTask(e)
|
2016-02-17 18:55:54 +00:00
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
2015-12-09 22:18:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.UpdateContainerResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:21:25 +00:00
|
|
|
func (s *apiServer) UpdateProcess(ctx context.Context, r *types.UpdateProcessRequest) (*types.UpdateProcessResponse, error) {
|
2016-02-17 18:55:54 +00:00
|
|
|
e := &supervisor.UpdateProcessTask{}
|
2016-02-02 22:21:25 +00:00
|
|
|
e.ID = r.Id
|
2016-02-17 18:55:54 +00:00
|
|
|
e.PID = r.Pid
|
2016-02-02 22:21:25 +00:00
|
|
|
e.Height = int(r.Height)
|
|
|
|
e.Width = int(r.Width)
|
|
|
|
e.CloseStdin = r.CloseStdin
|
2016-02-12 01:26:24 +00:00
|
|
|
s.sv.SendTask(e)
|
2016-02-17 18:55:54 +00:00
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
2016-02-02 22:21:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.UpdateProcessResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:18:31 +00:00
|
|
|
func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer) error {
|
2016-02-12 21:29:53 +00:00
|
|
|
t := time.Time{}
|
|
|
|
if r.Timestamp != 0 {
|
|
|
|
t = time.Unix(int64(r.Timestamp), 0)
|
|
|
|
}
|
|
|
|
events := s.sv.Events(t)
|
2015-12-10 20:30:04 +00:00
|
|
|
defer s.sv.Unsubscribe(events)
|
2016-02-12 01:26:24 +00:00
|
|
|
for e := range events {
|
|
|
|
if err := stream.Send(&types.Event{
|
|
|
|
Id: e.ID,
|
|
|
|
Type: e.Type,
|
|
|
|
Timestamp: uint64(e.Timestamp.Unix()),
|
2016-02-17 18:55:54 +00:00
|
|
|
Pid: e.PID,
|
2016-02-12 01:26:24 +00:00
|
|
|
Status: uint32(e.Status),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2015-12-16 00:22:53 +00:00
|
|
|
}
|
2015-12-09 22:18:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-26 20:29:35 +00:00
|
|
|
|
|
|
|
func convertToPb(st *runtime.Stat) *types.StatsResponse {
|
|
|
|
pbSt := &types.StatsResponse{
|
|
|
|
Timestamp: uint64(st.Timestamp.Unix()),
|
|
|
|
CgroupStats: &types.CgroupStats{},
|
|
|
|
}
|
|
|
|
systemUsage, _ := getSystemCPUUsage()
|
|
|
|
pbSt.CgroupStats.CpuStats = &types.CpuStats{
|
|
|
|
CpuUsage: &types.CpuUsage{
|
2016-06-03 22:00:49 +00:00
|
|
|
TotalUsage: st.CPU.Usage.Total,
|
|
|
|
PercpuUsage: st.CPU.Usage.Percpu,
|
|
|
|
UsageInKernelmode: st.CPU.Usage.Kernel,
|
|
|
|
UsageInUsermode: st.CPU.Usage.User,
|
2016-04-26 20:29:35 +00:00
|
|
|
},
|
|
|
|
ThrottlingData: &types.ThrottlingData{
|
2016-06-03 22:00:49 +00:00
|
|
|
Periods: st.CPU.Throttling.Periods,
|
|
|
|
ThrottledPeriods: st.CPU.Throttling.ThrottledPeriods,
|
|
|
|
ThrottledTime: st.CPU.Throttling.ThrottledTime,
|
2016-04-26 20:29:35 +00:00
|
|
|
},
|
|
|
|
SystemUsage: systemUsage,
|
|
|
|
}
|
|
|
|
pbSt.CgroupStats.MemoryStats = &types.MemoryStats{
|
2016-04-26 21:57:30 +00:00
|
|
|
Cache: st.Memory.Cache,
|
2016-04-26 20:29:35 +00:00
|
|
|
Usage: &types.MemoryData{
|
2016-04-26 21:57:30 +00:00
|
|
|
Usage: st.Memory.Usage.Usage,
|
|
|
|
MaxUsage: st.Memory.Usage.Max,
|
|
|
|
Failcnt: st.Memory.Usage.Failcnt,
|
|
|
|
Limit: st.Memory.Usage.Limit,
|
2016-04-26 20:29:35 +00:00
|
|
|
},
|
|
|
|
SwapUsage: &types.MemoryData{
|
2016-04-26 21:57:30 +00:00
|
|
|
Usage: st.Memory.Swap.Usage,
|
|
|
|
MaxUsage: st.Memory.Swap.Max,
|
|
|
|
Failcnt: st.Memory.Swap.Failcnt,
|
|
|
|
Limit: st.Memory.Swap.Limit,
|
2016-04-26 20:29:35 +00:00
|
|
|
},
|
|
|
|
KernelUsage: &types.MemoryData{
|
2016-04-26 21:57:30 +00:00
|
|
|
Usage: st.Memory.Kernel.Usage,
|
|
|
|
MaxUsage: st.Memory.Kernel.Max,
|
|
|
|
Failcnt: st.Memory.Kernel.Failcnt,
|
|
|
|
Limit: st.Memory.Kernel.Limit,
|
2016-04-26 20:29:35 +00:00
|
|
|
},
|
2016-04-28 01:42:16 +00:00
|
|
|
Stats: st.Memory.Raw,
|
2016-04-26 20:29:35 +00:00
|
|
|
}
|
|
|
|
pbSt.CgroupStats.BlkioStats = &types.BlkioStats{
|
2016-04-26 21:57:30 +00:00
|
|
|
IoServiceBytesRecursive: convertBlkioEntryToPb(st.Blkio.IoServiceBytesRecursive),
|
|
|
|
IoServicedRecursive: convertBlkioEntryToPb(st.Blkio.IoServicedRecursive),
|
|
|
|
IoQueuedRecursive: convertBlkioEntryToPb(st.Blkio.IoQueuedRecursive),
|
|
|
|
IoServiceTimeRecursive: convertBlkioEntryToPb(st.Blkio.IoServiceTimeRecursive),
|
|
|
|
IoWaitTimeRecursive: convertBlkioEntryToPb(st.Blkio.IoWaitTimeRecursive),
|
|
|
|
IoMergedRecursive: convertBlkioEntryToPb(st.Blkio.IoMergedRecursive),
|
|
|
|
IoTimeRecursive: convertBlkioEntryToPb(st.Blkio.IoTimeRecursive),
|
|
|
|
SectorsRecursive: convertBlkioEntryToPb(st.Blkio.SectorsRecursive),
|
2016-04-26 20:29:35 +00:00
|
|
|
}
|
|
|
|
pbSt.CgroupStats.HugetlbStats = make(map[string]*types.HugetlbStats)
|
2016-04-26 21:57:30 +00:00
|
|
|
for k, st := range st.Hugetlb {
|
2016-04-26 20:29:35 +00:00
|
|
|
pbSt.CgroupStats.HugetlbStats[k] = &types.HugetlbStats{
|
|
|
|
Usage: st.Usage,
|
2016-04-26 21:57:30 +00:00
|
|
|
MaxUsage: st.Max,
|
2016-04-26 20:29:35 +00:00
|
|
|
Failcnt: st.Failcnt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pbSt.CgroupStats.PidsStats = &types.PidsStats{
|
2016-04-26 21:57:30 +00:00
|
|
|
Current: st.Pids.Current,
|
|
|
|
Limit: st.Pids.Limit,
|
2016-04-26 20:29:35 +00:00
|
|
|
}
|
|
|
|
return pbSt
|
|
|
|
}
|
|
|
|
|
2016-04-26 21:57:30 +00:00
|
|
|
func convertBlkioEntryToPb(b []runtime.BlkioEntry) []*types.BlkioStatsEntry {
|
2016-04-26 20:29:35 +00:00
|
|
|
var pbEs []*types.BlkioStatsEntry
|
|
|
|
for _, e := range b {
|
|
|
|
pbEs = append(pbEs, &types.BlkioStatsEntry{
|
|
|
|
Major: e.Major,
|
|
|
|
Minor: e.Minor,
|
|
|
|
Op: e.Op,
|
|
|
|
Value: e.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return pbEs
|
|
|
|
}
|
|
|
|
|
|
|
|
const nanoSecondsPerSecond = 1e9
|
|
|
|
|
|
|
|
// getSystemCPUUsage returns the host system's cpu usage in
|
|
|
|
// nanoseconds. An error is returned if the format of the underlying
|
|
|
|
// file does not match.
|
|
|
|
//
|
|
|
|
// Uses /proc/stat defined by POSIX. Looks for the cpu
|
|
|
|
// statistics line and then sums up the first seven fields
|
|
|
|
// provided. See `man 5 proc` for details on specific field
|
|
|
|
// information.
|
|
|
|
func getSystemCPUUsage() (uint64, error) {
|
|
|
|
var line string
|
|
|
|
f, err := os.Open("/proc/stat")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
bufReader := bufio.NewReaderSize(nil, 128)
|
|
|
|
defer func() {
|
|
|
|
bufReader.Reset(nil)
|
|
|
|
f.Close()
|
|
|
|
}()
|
|
|
|
bufReader.Reset(f)
|
|
|
|
err = nil
|
|
|
|
for err == nil {
|
|
|
|
line, err = bufReader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
switch parts[0] {
|
|
|
|
case "cpu":
|
|
|
|
if len(parts) < 8 {
|
|
|
|
return 0, fmt.Errorf("bad format of cpu stats")
|
|
|
|
}
|
|
|
|
var totalClockTicks uint64
|
|
|
|
for _, i := range parts[1:8] {
|
|
|
|
v, err := strconv.ParseUint(i, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("error parsing cpu stats")
|
|
|
|
}
|
|
|
|
totalClockTicks += v
|
|
|
|
}
|
|
|
|
return (totalClockTicks * nanoSecondsPerSecond) /
|
|
|
|
clockTicksPerSecond, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("bad stats format")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.StatsResponse, error) {
|
|
|
|
e := &supervisor.StatsTask{}
|
|
|
|
e.ID = r.Id
|
|
|
|
e.Stat = make(chan *runtime.Stat, 1)
|
|
|
|
s.sv.SendTask(e)
|
|
|
|
if err := <-e.ErrorCh(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stats := <-e.Stat
|
|
|
|
t := convertToPb(stats)
|
|
|
|
return t, nil
|
|
|
|
}
|