[patch]add the ksm infomation to linux pmda

"Wu, Liming" <[email protected]> Fri, 20 Jan 2017 09:41:57 +0000
Newsgroups gmane.comp.sysutils.pcp
Message-ID <[email protected]>
Hi:
   
1)   This patch add some information to linux pmda.
   Which is from /sys/kernel/mm/ksm 
   please refer to the attachment for the details.

2)  This pathc fix the uint error of atopsar.
When I use the pcp atopsar , I found that the swptotal  and swpfree is 
different with the output of atopsar. Output is as follows.
[root@server1 atop]# pcp atopsar -m 1
 
server1  3.10.0-327.el7.x86_64  #1 SMP Thu Oct 29 17:29:29 EDT 2015  x86_64  2017/01/20
 
-------------------------- analysis date: 2017/01/20 --------------------------
 
17:27:20  memtotal memfree buffers cached dirty slabmem  swptotal swpfree _mem_
17:27:21     3757M    147M      0M  1028M    0M    322M  4194300M 3224320M
17:27:22     3757M    146M      0M  1028M    0M    322M  4194300M 3224320M
^C
[root@server1 atop]# atopsar -m 1
 
server1  3.10.0-327.el7.x86_64  #1 SMP Thu Oct 29 17:29:29 EDT 2015  x86_64  2017/01/20
 
-------------------------- analysis date: 2017/01/20 --------------------------
 
17:27:27  memtotal memfree buffers cached dirty slabmem  swptotal swpfree _mem_
17:27:28     3757M    144M      0M  1028M    0M    322M     4095M   3148M
17:27:29     3757M    144M      0M  1028M    0M    322M     4095M   3148M

Thanks,

--
Liming Wu
--



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links:

You receive all messages sent to this group.

View/Reply Online (#14997): https://groups.io/g/pcp/message/14997
View All Messages In Topic (1): https://groups.io/g/pcp/topic/4218245
Mute This Topic: https://groups.io/mt/4218245?uid=174580
New Topic: https://groups.io/g/pcp/post
-=-=-
pcp mailing list
[email protected]
https://groups.io/g/pcp/messages
-=-=-
Change Your Subscription: https://groups.io/g/pcp/editsub?uid=174580
Group Home: https://groups.io/g/pcp
Contact Group Owner: [email protected]
Terms of Service: https://groups.io/static/tos
Unsubscribe: https://groups.io/g/pcp/leave/354243/563757577/xyzzy
-=-=-=-=-=-=-=-=-=-=-=-
Add-the-ksm-info-to-linux-pmda.patch (application/octet-stream, 12.2 KB)
From 452d8cb1c7971c8e0ba65c60aaa593b7a04dcc26 Mon Sep 17 00:00:00 2001
From: Liming <[email protected]>
Date: Fri, 20 Jan 2017 17:04:57 +0800
Subject: [PATCH] Add the ksm info to linux pmda

---
 src/pmdas/linux/GNUmakefile |  6 ++--
 src/pmdas/linux/help        |  9 +++++
 src/pmdas/linux/ksm.c       | 86 ++++++++++++++++++++++++++++++++++++++++++++
 src/pmdas/linux/ksm.h       | 37 +++++++++++++++++++
 src/pmdas/linux/linux.h     |  1 +
 src/pmdas/linux/pmda.c      | 87 +++++++++++++++++++++++++++++++++++++++++++++
 src/pmdas/linux/root_linux  | 12 +++++++
 7 files changed, 236 insertions(+), 2 deletions(-)
 create mode 100644 src/pmdas/linux/ksm.c
 create mode 100644 src/pmdas/linux/ksm.h

diff --git a/src/pmdas/linux/GNUmakefile b/src/pmdas/linux/GNUmakefile
index 4d31f63..ebf67ed 100644
--- a/src/pmdas/linux/GNUmakefile
+++ b/src/pmdas/linux/GNUmakefile
@@ -36,7 +36,7 @@ CFILES		= pmda.c \
 		  sysfs_kernel.c linux_table.c numa_meminfo.c \
 		  proc_net_netstat.c namespaces.c proc_net_softnet.c \
 		  proc_net_snmp6.c mem_bandwidth.c proc_buddyinfo.c \
-                  proc_zoneinfo.c
+                  proc_zoneinfo.c ksm.c
 
 HFILES		= linux.h convert.h \
 		  proc_stat.h proc_meminfo.h proc_loadavg.h \
@@ -47,7 +47,8 @@ HFILES		= linux.h convert.h \
 		  proc_slabinfo.h proc_sys_fs.h proc_vmstat.h \
 		  sysfs_kernel.h linux_table.h numa_meminfo.h \
 		  proc_net_netstat.h namespaces.h proc_net_softnet.h \
-		  proc_net_snmp6.h proc_buddyinfo.h proc_zoneinfo.h
+		  proc_net_snmp6.h proc_buddyinfo.h proc_zoneinfo.h \
+                  ksm.h
 
 VERSION_SCRIPT	= exports
 HELPTARGETS	= help.dir help.pag
@@ -135,4 +136,5 @@ pmda.o swapdev.o:	swapdev.h
 pmda.o sysfs_kernel.o:	sysfs_kernel.h
 pmda.o proc_buddyinfo.o:	proc_buddyinfo.h
 pmda.o proc_zoneinfo.o:	proc_zoneinfo.h
+pmda.o ksm.o:	ksm.h
 pmda.o:	$(VERSION_SCRIPT)
diff --git a/src/pmdas/linux/help b/src/pmdas/linux/help
index f560c07..a79fc2d 100644
--- a/src/pmdas/linux/help
+++ b/src/pmdas/linux/help
@@ -931,6 +931,15 @@ Count of huge TLB page buddy allocation successes, from /proc/vmstat
 @ mem.zoneinfo.spanned spanned space in each zone for each NUMA node
 @ mem.zoneinfo.present present space in each zone for each NUMA node
 @ mem.zoneinfo.managed managed space in each zone for each NUMA node
+@ mem.ksm.full_scans Number of times that KSM has scanned for duplicated content
+@ mem.ksm.merge_across_nodes Allows merging across NUMA nodes
+@ mem.ksm.pages_shared The number of nodes in the stable tree
+@ mem.ksm.pages_sharing The number of virtual pages that are sharing a single page
+@ mem.ksm.pages_to_scan Number of pages to scan at a time
+@ mem.ksm.pages_unshared The number of nodes in the unstable tree
+@ mem.ksm.pages_volatile Number of pages that are candidate to be shared
+@ mem.ksm.run Wheter the KSM is run
+@ mem.ksm.sleep_millisecs Milliseconds ksmd should sleep between batches
 
 @ swap.length total swap available metric from /proc/meminfo
 @ swap.used swap used metric from /proc/meminfo
diff --git a/src/pmdas/linux/ksm.c b/src/pmdas/linux/ksm.c
new file mode 100644
index 0000000..2edb86b
--- /dev/null
+++ b/src/pmdas/linux/ksm.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2016 Fujitsu.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+#include <dirent.h>
+#include "linux.h"
+#include "ksm.h"
+
+int
+refresh_ksm_info(ksm_info_t *ksm_info)
+{
+    DIR *FD;
+    struct dirent* in_file;
+    FILE    *fp;
+    char buffer[BUFSIZ];
+    char path[100];
+    if(NULL == (FD = opendir("/sys/kernel/mm/ksm")))
+	return -oserror();
+
+    while ((in_file = readdir(FD))) 
+    {   
+        if (!strncmp (in_file->d_name, ".", 1))
+            continue;
+        if (!strncmp (in_file->d_name, "..", 2))    
+            continue;
+        
+        sprintf(path, "/sys/kernel/mm/ksm/%s", in_file->d_name);
+        path[sizeof(path)-1] = '\0';
+
+        if ((fp = linux_statsfile(path, buffer, sizeof(buffer))) == NULL)
+	    return -oserror();
+
+        while (fgets(buffer, BUFSIZ, fp) != NULL)
+        {
+            if (!strncmp(in_file->d_name, "full_scans", 10)) {
+               ksm_info->full_scans = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "merge_across_nodes", 18)) {
+               ksm_info->merge_across_nodes = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "pages_shared", 12)) {
+               ksm_info->pages_shared = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "pages_sharing", 13)) {
+               ksm_info->pages_sharing = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "pages_to_scan", 13)) {
+               ksm_info->pages_to_scan = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "pages_unshared", 14)) {
+               ksm_info->pages_unshared = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "pages_volatile", 14)) {
+               ksm_info->pages_volatile = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "run", 3)) {
+               ksm_info->run = atol(buffer);
+               break;
+            }
+            else if (!strncmp(in_file->d_name, "sleep_millisecs", 15)) {
+               ksm_info->sleep_millisecs = atol(buffer);
+               break;
+            }
+        }
+    } 
+    free(in_file);
+    closedir(FD);
+    fclose(fp);
+    return 0;
+}
diff --git a/src/pmdas/linux/ksm.h b/src/pmdas/linux/ksm.h
new file mode 100644
index 0000000..0b12e9a
--- /dev/null
+++ b/src/pmdas/linux/ksm.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2016 Fujitsu.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+typedef struct {
+        /* Number of times that KSM has scanned for duplicated content. */
+	unsigned long full_scans;
+        /* Allows merging across NUMA nodes. */
+	unsigned long merge_across_nodes;
+        /* The number of nodes in the stable tree */
+        unsigned long pages_shared;
+        /* The number of virtual pages that are sharing a single page. */ 
+        unsigned long pages_sharing;
+        /* Number of pages to scan at a time. */ 
+        unsigned int pages_to_scan;
+        /* The number of nodes in the unstable tree */ 
+        unsigned long pages_unshared;
+        /* Number of pages that are candidate to be shared */
+        unsigned long pages_volatile;
+        /* Wheter the KSM is run */
+        int run;
+        /* Milliseconds ksmd should sleep between batches */
+        unsigned int sleep_millisecs;
+} ksm_info_t;
+
+extern int refresh_ksm_info(ksm_info_t *ksm_info);
+
diff --git a/src/pmdas/linux/linux.h b/src/pmdas/linux/linux.h
index 6a18fa2..2869ce0 100644
--- a/src/pmdas/linux/linux.h
+++ b/src/pmdas/linux/linux.h
@@ -91,6 +91,7 @@ enum {
 	CLUSTER_SEM_STAT,	/* 66 msgctl(SEM_STAT) system call */
 	CLUSTER_BUDDYINFO,	/* 67 /proc/buddyinfo */
 	CLUSTER_ZONEINFO,	/* 68 /proc/zoneinfo */
+	CLUSTER_KSM_INFO,	/* 69 /sys/kernel/mm/ksm */
 
 	NUM_CLUSTERS		/* one more than highest numbered cluster */
 };
diff --git a/src/pmdas/linux/pmda.c b/src/pmdas/linux/pmda.c
index fa2dd55..462d1f6 100644
--- a/src/pmdas/linux/pmda.c
+++ b/src/pmdas/linux/pmda.c
@@ -58,6 +58,7 @@
 #include "proc_buddyinfo.h"
 #include "proc_zoneinfo.h"
 #include "numa_meminfo.h"
+#include "ksm.h"
 
 static proc_stat_t		proc_stat;
 static proc_meminfo_t		proc_meminfo;
@@ -79,6 +80,7 @@ static sem_info_t              _sem_info;
 static msg_info_t              _msg_info;
 static proc_net_softnet_t	proc_net_softnet;
 static proc_buddyinfo_t		proc_buddyinfo;
+static ksm_info_t               ksm_info;
 
 static int		_isDSO = 1;	/* =0 I am a daemon */
 static int		rootfd = -1;	/* af_unix pmdaroot */
@@ -3934,6 +3936,54 @@ static pmdaMetric metrictab[] = {
     PMDA_PMUNITS(0,0,0,0,0,0) }, },
 
 /*
+ * ksm info cluster
+ */
+/* msm.ksm.full_scans */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 0), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.merge_across_nodes */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 1), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.pages_shared */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 2), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.pages_sharing */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 3), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.pages_to_scan */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 4), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.pages_unshared */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 5), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.pages_volatile */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 6), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.run */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 7), PM_TYPE_32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/* msm.ksm.sleep_millisecs */
+  { NULL,
+    { PMDA_PMID(CLUSTER_KSM_INFO, 8), PM_TYPE_U32, PM_INDOM_NULL, PM_SEM_INSTANT,
+    PMDA_PMUNITS(0,0,0,0,0,0)}},
+
+/*
  * number of users cluster
  */
 
@@ -4964,6 +5014,9 @@ linux_refresh(pmdaExt *pmda, int *need_refresh, int context)
     if (need_refresh[CLUSTER_ZONEINFO])
 	refresh_proc_zoneinfo(INDOM(ZONEINFO_INDOM));
 
+    if (need_refresh[CLUSTER_KSM_INFO])
+	refresh_ksm_info(&ksm_info);
+
 done:
     if (need_refresh_mtab)
 	pmdaDynamicMetricTable(pmda);
@@ -6347,6 +6400,40 @@ linux_fetchCallBack(pmdaMetric *mdesc, unsigned int inst, pmAtomValue *atom)
 	break;
     }
 
+    case CLUSTER_KSM_INFO:
+	switch (idp->item) {
+	case 0: /* mem.ksm.full_scans */
+	    atom->ul = ksm_info.full_scans;
+	    break;
+	case 1: /* mem.ksm.merge_across_nodes */
+	    atom->ul = ksm_info.merge_across_nodes;
+	    break;
+	case 2: /* mem.ksm.pages_shared */
+	    atom->ul = ksm_info.pages_shared;
+	    break;
+	case 3: /* mem.ksm.pages_sharing */
+	    atom->ul = ksm_info.pages_sharing;
+	    break;
+	case 4: /* mem.ksm.pages_to_scan */
+	    atom->ul = ksm_info.pages_to_scan;
+	    break;
+	case 5: /* mem.ksm.pages_unshared */
+	    atom->ul = ksm_info.pages_unshared;
+	    break;
+	case 6: /* mem.ksm.pages_volatile */
+	    atom->ul = ksm_info.pages_volatile;
+	    break;
+	case 7: /* mem.ksm.run */
+	    atom->d = ksm_info.run;
+	    break;
+	case 8: /* mem.ksm.sleep_millisecs */
+	    atom->ul = ksm_info.sleep_millisecs;
+	    break;
+	default:
+	    return PM_ERR_PMID;
+        }
+	break;
+
     case CLUSTER_SEM_INFO:
 	switch (idp->item) {
 	case 0:	/* ipc.sem.used_sem */
diff --git a/src/pmdas/linux/root_linux b/src/pmdas/linux/root_linux
index 0879fe3..e8f5d9b 100644
--- a/src/pmdas/linux/root_linux
+++ b/src/pmdas/linux/root_linux
@@ -379,6 +379,18 @@ mem {
     buddyinfo
     slabinfo
     zoneinfo
+    ksm
+}
+mem.ksm {
+    full_scans          60:69:0
+    merge_across_nodes  60:69:1
+    pages_shared        60:69:2
+    pages_sharing       60:69:3
+    pages_to_scan       60:69:4
+    pages_unshared      60:69:5
+    pages_volatile      60:69:6
+    run                 60:69:7
+    sleep_millisecs     60:69:8
 }
 
 mem.buddyinfo {
-- 
1.8.3.1
fix-the-unit-error-of-atopsar.patch (application/octet-stream, 822 B)
From c792173095127ee23e38308b4aab36d734a06b75 Mon Sep 17 00:00:00 2001
From: Liming <[email protected]>
Date: Fri, 20 Jan 2017 17:31:01 +0800
Subject: [PATCH] fix the unit error of atopsar

---
 src/pcp/atop/atopsar.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/pcp/atop/atopsar.c b/src/pcp/atop/atopsar.c
index ecaab1c..05f1fae 100644
--- a/src/pcp/atop/atopsar.c
+++ b/src/pcp/atop/atopsar.c
@@ -1235,8 +1235,8 @@ memline(struct sstat *ss, struct tstat *ts, struct tstat **ps, int nactproc,
 		ss->mem.cachemem  /1024,
 		ss->mem.cachedrt  /1024,
 		ss->mem.slabmem   /1024,
-		ss->mem.totswap   /1024,
-		ss->mem.freeswap  /1024);
+		ss->mem.totswap   /(1024 * 1024),
+		ss->mem.freeswap  /(1024 * 1024));
 
 	postprint(mbadness >= sbadness ? mbadness : sbadness);
 
-- 
1.8.3.1