From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= <anders.f.bjorklund@gmail.com>
Date: Sat, 3 Feb 2018 12:51:54 +0100
Git-commit: 91b5b4c58d1aeedf99a601ae16b796125ce3da07
References: bsc#1087487
Subject: [PATCH] Add regular Slab memory to linux memory metrics

Previously only reporting reclaimable slabs, but not total slab...
There are also unreclaim slabs, can view them all with slabtop(1):

    Slab %lu
        In-kernel data structures cache.

    SReclaimable %lu (since Linux 2.6.19)
        Part of Slab, that might be reclaimed, such as caches.

    SUnreclaim %lu (since Linux 2.6.19)
        Part of Slab, that cannot be reclaimed on memory pressure.
Acked-by: John Jolly <jjolly@suse.de>
---
 ganglia.pod                    |  1 +
 gmetad/server.c                |  5 ++++-
 gmond/modules/memory/mod_mem.c |  3 +++
 libmetrics/libmetrics.h        |  2 ++
 libmetrics/linux/metrics.c     | 34 ++++++++++++++++++++++++++++++++++
 5 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/ganglia.pod b/ganglia.pod
index 6149ca1a..542c5ddf 100644
--- a/ganglia.pod
+++ b/ganglia.pod
@@ -609,6 +609,7 @@ is only partially complete).
   mem_cached    Amount of cached memory                  l,f
   mem_free      Amount of available memory               l,f
   mem_shared    Amount of shared memory                  l,f
+  mem_slab      Amount of in-kernel data struct cache    l
   mem_sreclaimable    Amount of slab reclaimable memory  l (kernel >= 2.6.19)
   mem_total     Amount of available memory               l,f
   mtu           Network maximum transmission unit        l,f
diff --git a/gmetad/server.c b/gmetad/server.c
index a2cd0ac8..9cd03403 100644
--- a/gmetad/server.c
+++ b/gmetad/server.c
@@ -119,6 +119,8 @@ static const struct metricinfo
   #ifdef LINUX
   "mem_sreclaimable", mem_sreclaimable_func, g_float},
   {
+  "mem_slab", mem_slab_func, g_float},
+  {
   #endif
   #ifdef SOLARIS
   "bread_sec", bread_sec_func, g_float},
@@ -894,7 +896,8 @@ status_report( client_t *client , char *callback)
          systemOffset += snprintf (systemBuf + systemOffset, METRICSBUFSIZE > systemOffset ? METRICSBUFSIZE - systemOffset : 0, "\"%s\":%u,", metrics[i].name, (unsigned) val.uint32);
       }
 #ifdef LINUX
-      else if(strcmp(metrics[i].name, "mem_sreclaimable") == 0){
+      else if(strcmp(metrics[i].name, "mem_slab") == 0 ||
+              strcmp(metrics[i].name, "mem_sreclaimable") == 0){
          memoryOffset += snprintf (memoryBuf + memoryOffset, METRICSBUFSIZE > memoryOffset ? METRICSBUFSIZE - memoryOffset : 0, "\"%s\":%f,", metrics[i].name, val.f);
       }
 #endif
diff --git a/gmond/modules/memory/mod_mem.c b/gmond/modules/memory/mod_mem.c
index feb8d99c..deef9615 100644
--- a/gmond/modules/memory/mod_mem.c
+++ b/gmond/modules/memory/mod_mem.c
@@ -50,6 +50,8 @@ static g_val_t mem_metric_handler ( int metric_index )
 #ifdef LINUX
     case 7:
         return mem_sreclaimable_func();
+    case 8:
+        return mem_slab_func();
 #endif
 #if HPUX
     case 7:
@@ -79,6 +81,7 @@ static Ganglia_25metric mem_metric_info[] =
     {0, "swap_total", 1200, GANGLIA_VALUE_FLOAT, "KB", "zero", "%.0f", UDP_HEADER_SIZE+8, "Total amount of swap space displayed in KBs"},
 #ifdef LINUX
     {0, "mem_sreclaimable", 180, GANGLIA_VALUE_FLOAT, "KB", "both", "%.0f", UDP_HEADER_SIZE+8, "Amount of reclaimable slab memory"},
+    {0, "mem_slab",    180, GANGLIA_VALUE_FLOAT, "KB", "both", "%.0f", UDP_HEADER_SIZE+8, "Amount of in-kernel data structures cache"},
 #endif
 #if HPUX
     {0, "mem_arm",     180, GANGLIA_VALUE_FLOAT, "KB", "both", "%.0f", UDP_HEADER_SIZE+8, "mem_arm"},
diff --git a/libmetrics/libmetrics.h b/libmetrics/libmetrics.h
index d487f542..f74a6f5e 100644
--- a/libmetrics/libmetrics.h
+++ b/libmetrics/libmetrics.h
@@ -79,7 +79,9 @@ void libmetrics_init( void );
  g_val_t location_func(void);
 
 #ifdef LINUX
+ g_val_t mem_slab_func (void);
  g_val_t mem_sreclaimable_func (void);
+ g_val_t mem_sunreclaim_func (void);
 #endif
 
 /* the following are additional internal metrics added by swagner
diff --git a/libmetrics/linux/metrics.c b/libmetrics/linux/metrics.c
index a13cc1cd..38d882e4 100644
--- a/libmetrics/linux/metrics.c
+++ b/libmetrics/linux/metrics.c
@@ -1294,6 +1294,23 @@ mem_buffers_func ( void )
 }
 
 g_val_t
+mem_slab_func ( void )
+{
+   char *p;
+   g_val_t val;
+
+   p = strstr( update_file(&proc_meminfo), "Slab:" );
+   if(p) {
+     p = skip_token(p);
+     val.f = atof( p );
+   } else {
+     val.f = 0;
+   }
+
+   return val;
+}
+
+g_val_t
 mem_sreclaimable_func ( void )
 {
    char *p;
@@ -1311,6 +1328,23 @@ mem_sreclaimable_func ( void )
 }
 
 g_val_t
+mem_sunreclaim_func ( void )
+{
+   char *p;
+   g_val_t val;
+
+   p = strstr( update_file(&proc_meminfo), "SUnreclaim:" );
+   if(p) {
+     p = skip_token(p);
+     val.f = atof( p );
+   } else {
+     val.f = 0;
+   }
+
+   return val;
+}
+
+g_val_t
 mem_cached_func ( void )
 {
    char *p;
-- 
2.13.6

