[lxc-devel] [lxd/master] Better handle errors in memory reporting

stgraber on Github lxc-bot at linuxcontainers.org
Sun Jul 2 22:15:10 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170702/8c5b2fe9/attachment.bin>
-------------- next part --------------
From dd8d99e68d22f2af58e06862e82c6e19e2f50653 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sun, 2 Jul 2017 18:14:10 -0400
Subject: [PATCH] Better handle errors in memory reporting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #3482

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 39 ++++++++++++++++++---------------------
 1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 7484a3319..c94bd3b1e 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -4998,39 +4998,36 @@ func (c *containerLXC) memoryState() api.ContainerStateMemory {
 
 	// Memory in bytes
 	value, err := c.CGroupGet("memory.usage_in_bytes")
-	valueInt, err := strconv.ParseInt(value, 10, 64)
-	if err != nil {
-		valueInt = -1
+	valueInt, err1 := strconv.ParseInt(value, 10, 64)
+	if err == nil && err1 == nil {
+		memory.Usage = valueInt
 	}
-	memory.Usage = valueInt
 
 	// Memory peak in bytes
 	value, err = c.CGroupGet("memory.max_usage_in_bytes")
-	valueInt, err = strconv.ParseInt(value, 10, 64)
-	if err != nil {
-		valueInt = -1
+	valueInt, err1 = strconv.ParseInt(value, 10, 64)
+	if err == nil && err1 == nil {
+		memory.UsagePeak = valueInt
 	}
 
-	memory.UsagePeak = valueInt
-
 	if cgSwapAccounting {
 		// Swap in bytes
-		value, err := c.CGroupGet("memory.memsw.usage_in_bytes")
-		valueInt, err := strconv.ParseInt(value, 10, 64)
-		if err != nil {
-			valueInt = -1
+		if memory.Usage > 0 {
+			value, err := c.CGroupGet("memory.memsw.usage_in_bytes")
+			valueInt, err1 := strconv.ParseInt(value, 10, 64)
+			if err == nil && err1 == nil {
+				memory.SwapUsage = valueInt - memory.Usage
+			}
 		}
 
-		memory.SwapUsage = valueInt - memory.Usage
-
 		// Swap peak in bytes
-		value, err = c.CGroupGet("memory.memsw.max_usage_in_bytes")
-		valueInt, err = strconv.ParseInt(value, 10, 64)
-		if err != nil {
-			valueInt = -1
+		if memory.UsagePeak > 0 {
+			value, err = c.CGroupGet("memory.memsw.max_usage_in_bytes")
+			valueInt, err1 = strconv.ParseInt(value, 10, 64)
+			if err == nil && err1 == nil {
+				memory.SwapUsagePeak = valueInt - memory.UsagePeak
+			}
 		}
-
-		memory.SwapUsagePeak = valueInt - memory.UsagePeak
 	}
 
 	return memory


More information about the lxc-devel mailing list