github.com/ilhicas/nomad@v0.11.8/drivers/docker/util/stats_posix.go (about)

     1  // +build !windows
     2  
     3  package util
     4  
     5  import (
     6  	"runtime"
     7  
     8  	docker "github.com/fsouza/go-dockerclient"
     9  	cstructs "github.com/hashicorp/nomad/client/structs"
    10  	"github.com/hashicorp/nomad/helper/stats"
    11  )
    12  
    13  var (
    14  	DockerMeasuredCPUStats = []string{"Throttled Periods", "Throttled Time", "Percent"}
    15  	DockerMeasuredMemStats = []string{"RSS", "Cache", "Swap", "Usage", "Max Usage"}
    16  )
    17  
    18  func DockerStatsToTaskResourceUsage(s *docker.Stats) *cstructs.TaskResourceUsage {
    19  	ms := &cstructs.MemoryStats{
    20  		RSS:      s.MemoryStats.Stats.Rss,
    21  		Cache:    s.MemoryStats.Stats.Cache,
    22  		Swap:     s.MemoryStats.Stats.Swap,
    23  		Usage:    s.MemoryStats.Usage,
    24  		MaxUsage: s.MemoryStats.MaxUsage,
    25  		Measured: DockerMeasuredMemStats,
    26  	}
    27  
    28  	cs := &cstructs.CpuStats{
    29  		ThrottledPeriods: s.CPUStats.ThrottlingData.ThrottledPeriods,
    30  		ThrottledTime:    s.CPUStats.ThrottlingData.ThrottledTime,
    31  		Measured:         DockerMeasuredCPUStats,
    32  	}
    33  
    34  	// Calculate percentage
    35  	cs.Percent = CalculateCPUPercent(
    36  		s.CPUStats.CPUUsage.TotalUsage, s.PreCPUStats.CPUUsage.TotalUsage,
    37  		s.CPUStats.SystemCPUUsage, s.PreCPUStats.SystemCPUUsage, runtime.NumCPU())
    38  	cs.SystemMode = CalculateCPUPercent(
    39  		s.CPUStats.CPUUsage.UsageInKernelmode, s.PreCPUStats.CPUUsage.UsageInKernelmode,
    40  		s.CPUStats.CPUUsage.TotalUsage, s.PreCPUStats.CPUUsage.TotalUsage, runtime.NumCPU())
    41  	cs.UserMode = CalculateCPUPercent(
    42  		s.CPUStats.CPUUsage.UsageInUsermode, s.PreCPUStats.CPUUsage.UsageInUsermode,
    43  		s.CPUStats.CPUUsage.TotalUsage, s.PreCPUStats.CPUUsage.TotalUsage, runtime.NumCPU())
    44  	cs.TotalTicks = (cs.Percent / 100) * stats.TotalTicksAvailable() / float64(runtime.NumCPU())
    45  
    46  	return &cstructs.TaskResourceUsage{
    47  		ResourceUsage: &cstructs.ResourceUsage{
    48  			MemoryStats: ms,
    49  			CpuStats:    cs,
    50  		},
    51  		Timestamp: s.Read.UTC().UnixNano(),
    52  	}
    53  }