Move cputicks into osutils package
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
37260f75db
commit
97dbfe9e1f
3 changed files with 66 additions and 57 deletions
|
@ -1,12 +1,8 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -15,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/docker/containerd"
|
"github.com/docker/containerd"
|
||||||
"github.com/docker/containerd/api/grpc/types"
|
"github.com/docker/containerd/api/grpc/types"
|
||||||
|
"github.com/docker/containerd/osutils"
|
||||||
"github.com/docker/containerd/runtime"
|
"github.com/docker/containerd/runtime"
|
||||||
"github.com/docker/containerd/supervisor"
|
"github.com/docker/containerd/supervisor"
|
||||||
"github.com/golang/protobuf/ptypes"
|
"github.com/golang/protobuf/ptypes"
|
||||||
|
@ -351,7 +348,7 @@ func convertToPb(st *runtime.Stat) *types.StatsResponse {
|
||||||
Timestamp: tsp,
|
Timestamp: tsp,
|
||||||
CgroupStats: &types.CgroupStats{},
|
CgroupStats: &types.CgroupStats{},
|
||||||
}
|
}
|
||||||
systemUsage, _ := getSystemCPUUsage()
|
systemUsage, _ := osutils.GetSystemCPUUsage()
|
||||||
pbSt.CgroupStats.CpuStats = &types.CpuStats{
|
pbSt.CgroupStats.CpuStats = &types.CpuStats{
|
||||||
CpuUsage: &types.CpuUsage{
|
CpuUsage: &types.CpuUsage{
|
||||||
TotalUsage: st.CPU.Usage.Total,
|
TotalUsage: st.CPU.Usage.Total,
|
||||||
|
@ -426,55 +423,6 @@ func convertBlkioEntryToPb(b []runtime.BlkioEntry) []*types.BlkioStatsEntry {
|
||||||
return pbEs
|
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) {
|
func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.StatsResponse, error) {
|
||||||
e := &supervisor.StatsTask{}
|
e := &supervisor.StatsTask{}
|
||||||
e.ID = r.Id
|
e.ID = r.Id
|
||||||
|
|
|
@ -6,13 +6,10 @@ import (
|
||||||
"github.com/docker/containerd/api/grpc/types"
|
"github.com/docker/containerd/api/grpc/types"
|
||||||
"github.com/docker/containerd/specs"
|
"github.com/docker/containerd/specs"
|
||||||
"github.com/docker/containerd/supervisor"
|
"github.com/docker/containerd/supervisor"
|
||||||
"github.com/opencontainers/runc/libcontainer/system"
|
|
||||||
ocs "github.com/opencontainers/runtime-spec/specs-go"
|
ocs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var clockTicksPerSecond = uint64(system.GetClockTicks())
|
|
||||||
|
|
||||||
func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) {
|
func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) {
|
||||||
process := &specs.ProcessSpec{
|
process := &specs.ProcessSpec{
|
||||||
Terminal: r.Terminal,
|
Terminal: r.Terminal,
|
||||||
|
|
64
osutils/proc.go
Normal file
64
osutils/proc.go
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
// +build linux
|
||||||
|
|
||||||
|
package osutils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/opencontainers/runc/libcontainer/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
const nanoSecondsPerSecond = 1e9
|
||||||
|
|
||||||
|
var clockTicksPerSecond = uint64(system.GetClockTicks())
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue