[lxc-devel] [PATCH 3/3] lxc-top: show kernel memory being used if available

Dwight Engen dwight.engen at gmail.com
Thu Oct 31 20:38:36 UTC 2013


- Also removed duplicate stats_clear lua function

Signed-off-by: Dwight Engen <dwight.engen at oracle.com>
---
 doc/lxc-top.sgml.in | 33 ++++++++++++++++++++++++++-------
 src/lua-lxc/lxc.lua | 17 ++++++-----------
 src/lxc/lxc-top     | 25 ++++++++++++++++++-------
 3 files changed, 50 insertions(+), 25 deletions(-)

diff --git a/doc/lxc-top.sgml.in b/doc/lxc-top.sgml.in
index 0021154..ba727c5 100644
--- a/doc/lxc-top.sgml.in
+++ b/doc/lxc-top.sgml.in
@@ -58,11 +58,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     <title>Description</title>
     <para>
       <command>lxc-top</command> displays container statistics. The output
-        is updated every <replaceable>delay</replaceable> seconds, and is
-        ordered according to the <replaceable>sortby</replaceable> value
-        given. Specifying <replaceable>count</replaceable> will limit the
-        number of containers displayed, otherwise <command>lxc-top</command>
-        will display as many containers as can fit in your terminal.
+      is updated every <replaceable>delay</replaceable> seconds, and is
+      ordered according to the <replaceable>sortby</replaceable> value
+      given. Specifying <replaceable>count</replaceable> will limit the
+      number of containers displayed, otherwise <command>lxc-top</command>
+      will display as many containers as can fit in your terminal.
     </para>
   </refsect1>
 
@@ -103,8 +103,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
           <para>
             Sort the containers by name, cpu use, or memory use. The
             <replaceable>sortby</replaceable> argument should be one of
-            the letters n,c,d,m to sort by name, cpu use, disk I/O, or
-            memory use respectively. The default is 'n'.
+            the letters n,c,d,m,k to sort by name, cpu use, disk I/O, memory,
+            or kernel memory use respectively. The default is 'n'.
           </para>
         </listitem>
       </varlistentry>
@@ -137,6 +137,25 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     </variablelist>
   </refsect1>
 
+  <refsect1>
+    <title>Notes</title>
+    <para>
+      For performance reasons the kernel does not account kernel memory use
+      unless a kernel memory limit is set. If a limit is not set, <command>
+      lxc-top</command> will display kernel memory use as 0. If no containers
+      are being accounted, the KMem column will not be displayed. A limit can
+      be set by specifying
+      <programlisting>
+      lxc.cgroup.memory.kmem.limit_in_bytes = <replaceable>number</replaceable>
+      </programlisting>
+      in your container configuration file, see
+      <citerefentry>
+        <refentrytitle>lxc.conf</refentrytitle>
+        <manvolnum>5</manvolnum>
+      </citerefentry>.
+    </para>
+  </refsect1>
+
   &seealso;
 
   <refsect1>
diff --git a/src/lua-lxc/lxc.lua b/src/lua-lxc/lxc.lua
index 5a3b49e..265e991 100755
--- a/src/lua-lxc/lxc.lua
+++ b/src/lua-lxc/lxc.lua
@@ -289,6 +289,8 @@ function container:stats_get(total)
     stat.mem_limit     = self:stat_get_int("memory.limit_in_bytes")
     stat.memsw_used    = self:stat_get_int("memory.memsw.usage_in_bytes")
     stat.memsw_limit   = self:stat_get_int("memory.memsw.limit_in_bytes")
+    stat.kmem_used     = self:stat_get_int("memory.kmem.usage_in_bytes")
+    stat.kmem_limit    = self:stat_get_int("memory.kmem.limit_in_bytes")
     stat.cpu_use_nanos = self:stat_get_int("cpuacct.usage")
     stat.cpu_use_user,
     stat.cpu_use_sys   = self:stat_get_ints("cpuacct.stat", {{1, 2}, {2, 2}})
@@ -299,6 +301,8 @@ function container:stats_get(total)
 	total.mem_limit     = total.mem_limit     + stat.mem_limit
 	total.memsw_used    = total.memsw_used    + stat.memsw_used
 	total.memsw_limit   = total.memsw_limit   + stat.memsw_limit
+	total.kmem_used     = total.kmem_used     + stat.kmem_used
+	total.kmem_limit    = total.kmem_limit    + stat.kmem_limit
 	total.cpu_use_nanos = total.cpu_use_nanos + stat.cpu_use_nanos
 	total.cpu_use_user  = total.cpu_use_user  + stat.cpu_use_user
 	total.cpu_use_sys   = total.cpu_use_sys   + stat.cpu_use_sys
@@ -314,17 +318,8 @@ function M.stats_clear(stat)
     stat.mem_limit     = 0
     stat.memsw_used    = 0
     stat.memsw_limit   = 0
-    stat.cpu_use_nanos = 0
-    stat.cpu_use_user  = 0
-    stat.cpu_use_sys   = 0
-    stat.blkio         = 0
-end
-
-function M.stats_clear(stat)
-    stat.mem_used      = 0
-    stat.mem_limit     = 0
-    stat.memsw_used    = 0
-    stat.memsw_limit   = 0
+    stat.kmem_used     = 0
+    stat.kmem_limit    = 0
     stat.cpu_use_nanos = 0
     stat.cpu_use_user  = 0
     stat.cpu_use_sys   = 0
diff --git a/src/lxc/lxc-top b/src/lxc/lxc-top
index 35d7e02..a1f0250 100755
--- a/src/lxc/lxc-top
+++ b/src/lxc/lxc-top
@@ -117,12 +117,14 @@ function container_sort(a, b)
 	elseif (optarg["s"] == "c") then return (stats[a].cpu_use_nanos < stats[b].cpu_use_nanos)
 	elseif (optarg["s"] == "d") then return (stats[a].blkio < stats[b].blkio)
 	elseif (optarg["s"] == "m") then return (stats[a].mem_used < stats[b].mem_used)
+	elseif (optarg["s"] == "k") then return (stats[a].kmem_used < stats[b].kmem_used)
 	end
     else
 	if     (optarg["s"] == "n") then return (a < b)
 	elseif (optarg["s"] == "c") then return (stats[a].cpu_use_nanos > stats[b].cpu_use_nanos)
 	elseif (optarg["s"] == "d") then return (stats[a].blkio > stats[b].blkio)
 	elseif (optarg["s"] == "m") then return (stats[a].mem_used > stats[b].mem_used)
+	elseif (optarg["s"] == "k") then return (stats[a].kmem_used > stats[b].kmem_used)
 	end
     end
 end
@@ -163,14 +165,19 @@ function container_list_update()
     table.sort(containers, container_sort)
 end
 
-function stats_print_header()
+function stats_print_header(stats_total)
     printf(TERMRVRS .. TERMBOLD)
-    printf("%-15s %8s %8s %8s %10s %10s\n", "Container", "CPU",  "CPU",  "CPU",  "BlkIO", "Mem")
-    printf("%-15s %8s %8s %8s %10s %10s\n", "Name",      "Used", "Sys",  "User", "Total", "Used")
+    printf("%-15s %8s %8s %8s %10s %10s", "Container", "CPU",  "CPU",  "CPU",  "BlkIO", "Mem")
+    if (stats_total.kmem_used > 0) then printf(" %10s", "KMem") end
+    printf("\n")
+
+    printf("%-15s %8s %8s %8s %10s %10s", "Name",      "Used", "Sys",  "User", "Total", "Used")
+    if (stats_total.kmem_used > 0) then printf(" %10s", "Used") end
+    printf("\n")
     printf(TERMNORM)
 end
 
-function stats_print(name, stats)
+function stats_print(name, stats, stats_total)
     printf("%-15s %8.2f %8.2f %8.2f %10s %10s",
 	   name,
 	   stats.cpu_use_nanos / 1000000000,
@@ -178,6 +185,9 @@ function stats_print(name, stats)
 	   stats.cpu_use_user / USER_HZ,
 	   strsisize(stats.blkio),
 	   strsisize(stats.mem_used))
+    if (stats_total.kmem_used > 0) then
+	printf(" %10s", strsisize(stats.kmem_used))
+    end
 end
 
 function usage()
@@ -190,6 +200,7 @@ function usage()
 	"                 c = CPU use\n" ..
 	"                 d = Disk I/O use\n" ..
 	"                 m = Memory use\n" ..
+	"                 k = Kernel memory use\n" ..
 	"  -r|--reverse   sort in reverse (descending) order\n"
     )
     os.exit(1)
@@ -219,15 +230,15 @@ do
     -- may fall back to this, or ncurses. ug.
     --os.execute("tput clear")
     printf(TERMCLEAR)
-    stats_print_header()
+    stats_print_header(stats_total)
     for index,ctname in ipairs(containers) do
-	stats_print(ctname, stats[ctname])
+	stats_print(ctname, stats[ctname], stats_total)
 	printf("\n")
 	if (index >= optarg["m"]) then
 	    break
 	end
     end
-    stats_print(string.format("TOTAL (%-2d)", #containers), stats_total)
+    stats_print(string.format("TOTAL (%-2d)", #containers), stats_total, stats_total)
     io.flush()
     core.usleep(optarg["d"] * 1000000)
 end
-- 
1.8.3.1





More information about the lxc-devel mailing list