[lxc-devel] [lxd/master] lxc info: show cpu usage

brauner on Github lxc-bot at linuxcontainers.org
Thu Sep 8 20:14:25 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 313 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160908/45f25e8f/attachment.bin>
-------------- next part --------------
From a12d4c141d576041c393eb2df571cb500cd0e0a8 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Thu, 8 Sep 2016 16:12:05 +0200
Subject: [PATCH 1/3] shared: add CPU info

Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
 shared/container.go | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/shared/container.go b/shared/container.go
index 721aa19..348ba6b 100644
--- a/shared/container.go
+++ b/shared/container.go
@@ -10,6 +10,7 @@ import (
 type ContainerState struct {
 	Status     string                           `json:"status"`
 	StatusCode StatusCode                       `json:"status_code"`
+	CPU        ContainerStateCPU                `json:"cpu"`
 	Disk       map[string]ContainerStateDisk    `json:"disk"`
 	Memory     ContainerStateMemory             `json:"memory"`
 	Network    map[string]ContainerStateNetwork `json:"network"`
@@ -21,6 +22,10 @@ type ContainerStateDisk struct {
 	Usage int64 `json:"usage"`
 }
 
+type ContainerStateCPU struct {
+	Usage int64 `json:"usage"`
+}
+
 type ContainerStateMemory struct {
 	Usage         int64 `json:"usage"`
 	UsagePeak     int64 `json:"usage_peak"`

From f6bcc7ba7b563dbe67d7cf2bfd20c983b9ab7c12 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Thu, 8 Sep 2016 16:30:37 +0200
Subject: [PATCH 2/3] container_lxc: add cpuState() to retrieve CPU info

Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
 lxd/container_lxc.go | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index aeac665..1a1c37f 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1826,6 +1826,7 @@ func (c *containerLXC) RenderState() (*shared.ContainerState, error) {
 
 	if c.IsRunning() {
 		pid := c.InitPID()
+		status.CPU = c.cpuState()
 		status.Disk = c.diskState()
 		status.Memory = c.memoryState()
 		status.Network = c.networkState()
@@ -3384,6 +3385,25 @@ func (c *containerLXC) Exec(command []string, env map[string]string, stdin *os.F
 	return 0, nil
 }
 
+func (c *containerLXC) cpuState() shared.ContainerStateCPU {
+	cpu := shared.ContainerStateCPU{}
+
+	if !cgCpuController {
+		return cpu
+	}
+
+	// CPU usage in seconds
+	value, err := c.CGroupGet("cpuacct.usage")
+	valueInt, err := strconv.ParseInt(value, 10, 64)
+	if err != nil {
+		valueInt = -1
+	}
+
+	cpu.Usage = valueInt
+
+	return cpu
+}
+
 func (c *containerLXC) diskState() map[string]shared.ContainerStateDisk {
 	disk := map[string]shared.ContainerStateDisk{}
 

From f9ee700cd739ab8aaf595a08b2e8a4c4bc1a9706 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at canonical.com>
Date: Thu, 8 Sep 2016 17:21:33 +0200
Subject: [PATCH 3/3] info: add cpu usage

Signed-off-by: Christian Brauner <christian.brauner at canonical.com>
---
 lxc/info.go | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lxc/info.go b/lxc/info.go
index 6cb617b..22f65a6 100644
--- a/lxc/info.go
+++ b/lxc/info.go
@@ -141,6 +141,17 @@ func (c *infoCmd) containerInfo(d *lxd.Client, name string, showLog bool) error
 			fmt.Printf(diskInfo)
 		}
 
+		// CPU usage
+		cpuInfo := ""
+		if cs.CPU.Usage != 0 {
+			cpuInfo += fmt.Sprintf("    %s: %v\n", i18n.G("CPU usage (in seconds)"), cs.CPU.Usage / 1000000000)
+		}
+
+		if cpuInfo != "" {
+			fmt.Println(i18n.G("  CPU usage:"))
+			fmt.Printf(cpuInfo)
+		}
+
 		// Memory usage
 		memoryInfo := ""
 		if cs.Memory.Usage != 0 {


More information about the lxc-devel mailing list