Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

Commit fa5f991

Browse files
Gather and return real statistics in Stats() (#50)
1 parent e83ba10 commit fa5f991

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

pkg/v2/service.go

+71-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,77 @@ func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (*pt
565565
}
566566

567567
func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) {
568-
// TODO(random-liu): Use `runsc events --stats`.
569-
data, err := typeurl.MarshalAny(&cgroups.Metrics{})
568+
path, err := os.Getwd()
569+
if err != nil {
570+
return nil, err
571+
}
572+
ns, err := namespaces.NamespaceRequired(ctx)
573+
if err != nil {
574+
return nil, err
575+
}
576+
runtime, err := s.readRuntime(path)
577+
if err != nil {
578+
return nil, err
579+
}
580+
rs := proc.NewRunsc(proc.RunscRoot, path, ns, runtime, nil)
581+
stats, err := rs.Stats(ctx, s.id)
582+
if err != nil {
583+
return nil, err
584+
}
585+
586+
// gvisor currently (as of 2020-03-03) only returns the total memory
587+
// usage and current PID value[0]. However, we copy the common fields here
588+
// so that future updates will propagate correct information. We're
589+
// using the cgroups.Metrics structure so we're returning the same type
590+
// as runc.
591+
//
592+
// [0]: https://github.com/google/gvisor/blob/277a0d5a1fbe8272d4729c01ee4c6e374d047ebc/runsc/boot/events.go#L61-L81
593+
data, err := typeurl.MarshalAny(&cgroups.Metrics{
594+
CPU: &cgroups.CPUStat{
595+
Usage: &cgroups.CPUUsage{
596+
Total: stats.Cpu.Usage.Total,
597+
Kernel: stats.Cpu.Usage.Kernel,
598+
User: stats.Cpu.Usage.User,
599+
PerCPU: stats.Cpu.Usage.Percpu,
600+
},
601+
Throttling: &cgroups.Throttle{
602+
Periods: stats.Cpu.Throttling.Periods,
603+
ThrottledPeriods: stats.Cpu.Throttling.ThrottledPeriods,
604+
ThrottledTime: stats.Cpu.Throttling.ThrottledTime,
605+
},
606+
},
607+
Memory: &cgroups.MemoryStat{
608+
Cache: stats.Memory.Cache,
609+
Usage: &cgroups.MemoryEntry{
610+
Limit: stats.Memory.Usage.Limit,
611+
Usage: stats.Memory.Usage.Usage,
612+
Max: stats.Memory.Usage.Max,
613+
Failcnt: stats.Memory.Usage.Failcnt,
614+
},
615+
Swap: &cgroups.MemoryEntry{
616+
Limit: stats.Memory.Swap.Limit,
617+
Usage: stats.Memory.Swap.Usage,
618+
Max: stats.Memory.Swap.Max,
619+
Failcnt: stats.Memory.Swap.Failcnt,
620+
},
621+
Kernel: &cgroups.MemoryEntry{
622+
Limit: stats.Memory.Kernel.Limit,
623+
Usage: stats.Memory.Kernel.Usage,
624+
Max: stats.Memory.Kernel.Max,
625+
Failcnt: stats.Memory.Kernel.Failcnt,
626+
},
627+
KernelTCP: &cgroups.MemoryEntry{
628+
Limit: stats.Memory.KernelTCP.Limit,
629+
Usage: stats.Memory.KernelTCP.Usage,
630+
Max: stats.Memory.KernelTCP.Max,
631+
Failcnt: stats.Memory.KernelTCP.Failcnt,
632+
},
633+
},
634+
Pids: &cgroups.PidsStat{
635+
Current: stats.Pids.Current,
636+
Limit: stats.Pids.Limit,
637+
},
638+
})
570639
if err != nil {
571640
return nil, err
572641
}

0 commit comments

Comments
 (0)