[Patch]add the metrics about share memory segment

"Wu, Liming" <[email protected]>
Newsgroups gmane.comp.sysutils.pcp
Message-ID <[email protected]>
Hi:
   
   This patch add the metrics about share memory segment. Output is like follow.
# pminfo -fdT ipc.shm
ipc.shm.key
    Data Type: string  InDom: 60.28 0xf00001c
    Semantics: discrete  Units: none
Help:
Key supplied to shmget (from shmctl(.., SHM_STAT, ..))
    inst [0 or "65536"] value "0x52030112"
    inst [1 or "32769"] value "0x000004d2"
 
ipc.shm.owner
    Data Type: string  InDom: 60.28 0xf00001c
    Semantics: discrete  Units: none
Help:
share memory segment owner (rom shmctl(.., SHM_STAT, ..))
    inst [0 or "65536"] value "root"
    inst [1 or "32769"] value "root"
 
ipc.shm.perms
    Data Type: 32-bit unsigned int  InDom: 60.28 0xf00001c
    Semantics: discrete  Units: none
Help:
operation perms (from shmctl(.., SHM_STAT, ..))
    inst [0 or "65536"] value 644
    inst [1 or "32769"] value 600
 
ipc.shm.segsz
    Data Type: 32-bit unsigned int  InDom: 60.28 0xf00001c
    Semantics: discrete  Units: none
Help:
size of segment (bytes) (from shmctl(.., SHM_STAT, ..))
    inst [0 or "65536"] value 1024
    inst [1 or "32769"] value 1024
 
ipc.shm.nattch
    Data Type: 32-bit unsigned int  InDom: 60.28 0xf00001c
    Semantics: discrete  Units: none
Help:
no. of current attaches (from shmctl(.., SHM_STAT, ..))
    inst [0 or "65536"] value 0
    inst [1 or "32769"] value 0
 
ipc.shm.status
    Data Type: string  InDom: 60.28 0xf00001c
    Semantics: discrete  Units: none
Help:
share memory segment status (from shmctl(.., SHM_STAT, ..))
    inst [0 or "65536"] value " "
    inst [1 or "32769"] value " "


Liming Wu
Best regards



-=-=-=-=-=-=-=-=-=-=-=-
pcp mailing list
[email protected]
https://groups.io/g/pcp/messages
-=-=-
Groups.io Links:

You receive all messages sent to this group.

View/Reply Online (#14650): https://groups.io/g/pcp/message/14650
View All Messages In Topic (1): https://groups.io/g/pcp/topic/3041711
Mute This Topic: https://groups.io/mt/3041711?uid=174580
New Topic: https://groups.io/g/pcp/post

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-metrics-about-share-memory-segment.patch (application/octet-stream, 15.1 KB)
From 06c6f4e4d04ffa4adb6a9fa86d16d31f5219c411 Mon Sep 17 00:00:00 2001
From: Liming <[email protected]>
Date: Fri, 4 Nov 2016 16:44:27 +0800
Subject: [PATCH] Add the metrics about share memory segment

---
 qa/888                     |  7 +++-
 qa/888.out                 | 30 ++++++++++++++++
 qa/src/GNUlocaldefs        |  9 +++--
 qa/src/qa_shmctl_stat.c    | 26 ++++++++++++++
 src/pmdas/linux/clusters.h |  1 +
 src/pmdas/linux/help       |  6 ++++
 src/pmdas/linux/indom.h    |  1 +
 src/pmdas/linux/ipc.c      | 88 +++++++++++++++++++++++++++++++++++++++++++++-
 src/pmdas/linux/ipc.h      | 23 ++++++++++++
 src/pmdas/linux/pmda.c     | 79 +++++++++++++++++++++++++++++++++++++++++
 src/pmdas/linux/root_linux |  6 ++++
 11 files changed, 272 insertions(+), 4 deletions(-)
 create mode 100644 qa/src/qa_shmctl_stat.c

diff --git a/qa/888 b/qa/888
index 2c8ef6c..bbccfe8 100755
--- a/qa/888
+++ b/qa/888
@@ -32,7 +32,12 @@ LD_PRELOAD=$here/src/qa_shmctl.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.
 LD_PRELOAD=$here/src/qa_shmctl.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.used_ids
 LD_PRELOAD=$here/src/qa_shmctl.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.swap_attempts
 LD_PRELOAD=$here/src/qa_shmctl.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.swap_successes
-
+LD_PRELOAD=$here/src/qa_shmctl_stat.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.key
+LD_PRELOAD=$here/src/qa_shmctl_stat.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.owner
+LD_PRELOAD=$here/src/qa_shmctl_stat.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.perms
+LD_PRELOAD=$here/src/qa_shmctl_stat.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.segsz
+LD_PRELOAD=$here/src/qa_shmctl_stat.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.nattch
+LD_PRELOAD=$here/src/qa_shmctl_stat.so  pminfo -L -K clear -K add,60,$pmda -d -f ipc.shm.status
 # success, all done
 status=0
 exit
diff --git a/qa/888.out b/qa/888.out
index 073b112..f3e6185 100644
--- a/qa/888.out
+++ b/qa/888.out
@@ -29,3 +29,33 @@ ipc.shm.swap_successes
     Data Type: 32-bit unsigned int  InDom: PM_INDOM_NULL 0xffffffff
     Semantics: instant  Units: none
     value 3
+
+ipc.shm.key
+    Data Type: string  InDom: 60.28 0xf00001c
+    Semantics: discrete  Units: none
+    inst [0 or "0"] value "0x00000457"
+
+ipc.shm.owner
+    Data Type: string  InDom: 60.28 0xf00001c
+    Semantics: discrete  Units: none
+    inst [0 or "0"] value "root"
+
+ipc.shm.perms
+    Data Type: 32-bit unsigned int  InDom: 60.28 0xf00001c
+    Semantics: discrete  Units: none
+    inst [0 or "0"] value 570
+
+ipc.shm.segsz
+    Data Type: 32-bit unsigned int  InDom: 60.28 0xf00001c
+    Semantics: discrete  Units: none
+    inst [0 or "0"] value 666
+
+ipc.shm.nattch
+    Data Type: 32-bit unsigned int  InDom: 60.28 0xf00001c
+    Semantics: discrete  Units: none
+    inst [0 or "0"] value 777
+
+ipc.shm.status
+    Data Type: string  InDom: 60.28 0xf00001c
+    Semantics: discrete  Units: none
+    inst [0 or "0"] value "dest"
diff --git a/qa/src/GNUlocaldefs b/qa/src/GNUlocaldefs
index b6fe50e..6a87c8f 100644
--- a/qa/src/GNUlocaldefs
+++ b/qa/src/GNUlocaldefs
@@ -150,7 +150,7 @@ MYFILES += \
 	root_irix root_pmns tiny.pmns sgi.bf versiondefs \
 	pthread_barrier.h pv.c qa_test.c qa_timezone.c \
 	permslist \
-	qa_shmctl.c \
+	qa_shmctl.c  qa_shmctl_stat.c \
 	qa_sem_msg_ctl.c 
 
 MYSCRIPTS = grind-tools ipcs_clear show-args fixhosts mkpermslist \
@@ -165,7 +165,8 @@ TARGETS = $(subst broken_pmda,,$(basename $(CFILES_TARGETS))) \
 	fetchrate_lite pv
 
 ifeq "$(TARGET_OS)" "linux"
-TARGETS += qa_shmctl.$(DSOSUFFIX) qa_sem_msg_ctl.$(DSOSUFFIX)
+TARGETS += qa_shmctl.$(DSOSUFFIX) qa_sem_msg_ctl.$(DSOSUFFIX) \
+	qa_shmctl_stat.$(DSOSUFFIX)
 endif
 
 ifeq ($(HAVE_64), 1)
@@ -589,6 +590,10 @@ qa_shmctl.$(DSOSUFFIX):	 qa_shmctl.c
 	$(CCF) $(LDFLAGS) -shared -o $@ qa_shmctl.c
 	@rm -f qa_shmctl.o
 
+qa_shmctl_stat.$(DSOSUFFIX):	 qa_shmctl_stat.c
+	$(CCF) $(LDFLAGS) -shared -o $@ qa_shmctl_stat.c
+	@rm -f qa_shmctl_stat.o
+
 qa_sem_msg_ctl.$(DSOSUFFIX):	 qa_sem_msg_ctl.c
 	$(CCF) $(LDFLAGS) -shared -o $@ qa_sem_msg_ctl.c
 	@rm -f qa_sem_msg_ctl.o
diff --git a/qa/src/qa_shmctl_stat.c b/qa/src/qa_shmctl_stat.c
new file mode 100644
index 0000000..02f2a43
--- /dev/null
+++ b/qa/src/qa_shmctl_stat.c
@@ -0,0 +1,26 @@
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include <sys/shm.h>
+#include <sys/msg.h>
+
+#if defined (__GLIBC__) && __GLIBC__ >= 2                                                           
+# define KEY __key                                                                                  
+#else
+# define KEY key                                                                                    
+#endif
+
+int shmctl(int shmid, int cmd, struct shmid_ds *buf)
+{
+    struct shmid_ds *j = (struct shmid_ds *)buf;   
+    memset(j, 0, sizeof(*j));
+    struct ipc_perm  *ipcp = (struct ipc_perm *)(&(j->shm_perm));
+    memset(ipcp, 0, sizeof(*ipcp));
+    ipcp->KEY = 1111;
+    ipcp->uid = 0;
+    ipcp->mode = 888;
+    j->shm_segsz = 666;
+    j->shm_nattch = 777;
+    
+    
+    return 0;
+}
diff --git a/src/pmdas/linux/clusters.h b/src/pmdas/linux/clusters.h
index 666967d..9b4a3ef 100644
--- a/src/pmdas/linux/clusters.h
+++ b/src/pmdas/linux/clusters.h
@@ -84,6 +84,7 @@ enum {
 	CLUSTER_SEM_INFO,	/* 61 shmctl(SEM_INFO) system call */
 	CLUSTER_MSG_INFO,	/* 62 msgctl(MSG_INFO) system call */
 	CLUSTER_SOFTIRQS,	/* 63 /proc/softirqs percpu counters */
+	CLUSTER_SHM_STAT,	/* 64 shmctl(SHM_STAT) system call */
 
 	NUM_CLUSTERS		/* one more than highest numbered cluster */
 };
diff --git a/src/pmdas/linux/help b/src/pmdas/linux/help
index 200b410..c888179 100644
--- a/src/pmdas/linux/help
+++ b/src/pmdas/linux/help
@@ -1510,6 +1510,12 @@ See also the kernel.uname.* metrics
 @ ipc.shm.used_ids number of currently existing segments (from shmctl(..,SHM_INFO,..))
 @ ipc.shm.swap_attempts number of swap attempts (from shmctl(..,SHM_INFO,..))
 @ ipc.shm.swap_successes number of swap successes (from shmctl(..,SHM_INFO,..))
+@ ipc.shm.key Key supplied to shmget (from shmctl(.., SHM_STAT, ..))
+@ ipc.shm.owner share memory segment owner (rom shmctl(.., SHM_STAT, ..))
+@ ipc.shm.perms operation perms (from shmctl(.., SHM_STAT, ..))
+@ ipc.shm.segsz size of segment (bytes) (from shmctl(.., SHM_STAT, ..))
+@ ipc.shm.nattch no. of current attaches (from shmctl(.., SHM_STAT, ..))
+@ ipc.shm.status share memory segment status (from shmctl(.., SHM_STAT, ..))
 
 @ vfs.files.count number of in-use file structures
 @ vfs.files.free number of available file structures
diff --git a/src/pmdas/linux/indom.h b/src/pmdas/linux/indom.h
index 4b1f0d5..aa46d27 100644
--- a/src/pmdas/linux/indom.h
+++ b/src/pmdas/linux/indom.h
@@ -46,6 +46,7 @@ enum {
 	MD_INDOM,		/* 25 - multi-device devices */
 	INTERRUPT_NAMES_INDOM,	/* 26 - persistent percpu interrupts IDs */
 	SOFTIRQS_NAMES_INDOM,	/* 27 - persistent percpu softirqs IDs */
+	IPC_STAT_INDOM,	        /* 28 - ipc shm_stat shmid */
 
 	NUM_INDOMS		/* one more than highest numbered cluster */
 };
diff --git a/src/pmdas/linux/ipc.c b/src/pmdas/linux/ipc.c
index 3c38f2f..18e8a67 100644
--- a/src/pmdas/linux/ipc.c
+++ b/src/pmdas/linux/ipc.c
@@ -12,14 +12,18 @@
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
  */
-
 #include "pmapi.h"
 #define __USE_GNU 1
 #include <sys/ipc.h>
 #include <sys/sem.h>
 #include <sys/shm.h>
 #include <sys/msg.h>
+#include <pwd.h>
 #include "ipc.h"
+#include "pmapi.h"
+#include "impl.h"
+#include "pmda.h"
+#include "indom.h"
 
 int
 refresh_shm_info(shm_info_t *_shm_info)
@@ -125,3 +129,85 @@ refresh_msg_limits(msg_limits_t *msg_limits)
     msg_limits->msgseg = msginfo.msgseg;
     return 0;
 }
+
+int 
+refresh_shm_stat(pmInDom shm_indom){
+    struct passwd *pw = NULL;
+    char shmid[16]; 
+    char perms_temp[16];
+    int i = 0, maxid = 0;
+    int sts = 0;
+    shm_stat_t *shm_stat = NULL;
+
+    pmdaCacheOp(shm_indom, PMDA_CACHE_INACTIVE);
+    struct shm_info dummy;
+
+    maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) &dummy);
+    if (maxid < 0)
+        return -1;
+ 
+    while (i <= maxid) {
+        int shmid_o;
+        struct shmid_ds shmseg;
+        struct ipc_perm *ipcp = &shmseg.shm_perm; 
+        shmid_o = shmctl(i, SHM_STAT, &shmseg);
+        if (shmid_o < 0) {
+            i++;
+            continue;
+        }
+	else {
+	    i ++;
+	}
+
+	sprintf(shmid, "%d", shmid_o);
+        sts = pmdaCacheLookupName(shm_indom, shmid, NULL, (void **)&shm_stat);
+
+        if (sts == PMDA_CACHE_ACTIVE){
+	    continue;
+	}
+	if (sts == PMDA_CACHE_INACTIVE){
+            pmdaCacheStore(shm_indom, PMDA_CACHE_ADD, shmid, shm_stat);
+	}
+	else {
+	    if ((shm_stat =(shm_stat_t*)malloc(sizeof(shm_stat_t))) == NULL) {
+	        continue;
+            }
+	    memset(shm_stat, 0, sizeof(shm_stat_t));
+
+	    sprintf(shm_stat->shm_key, "0x%08x", ipcp->KEY); 
+            pw = getpwuid(ipcp->uid);
+            if(pw){
+                strcpy(shm_stat->shm_owner, pw->pw_name);
+            } 
+            else
+            {
+                strcpy(shm_stat->shm_owner, ipcp->uid);
+            }  
+            /* convert to octal number */
+            sprintf(perms_temp, "%o", ipcp->mode & 0777);
+            shm_stat->shm_perms      = atoi(perms_temp);
+            shm_stat->shm_bytes      = shmseg.shm_segsz;
+            shm_stat->shm_nattch     = shmseg.shm_nattch;
+            if(ipcp->mode & SHM_DEST){
+                shm_stat->shm_status = "dest";
+            }
+            else if(ipcp->mode & SHM_LOCKED){
+                shm_stat->shm_status = "locked";
+            }
+            else{
+                shm_stat->shm_status = " ";
+            }  
+            sts = pmdaCacheStore(shm_indom, PMDA_CACHE_ADD, shmid, (void *)shm_stat);
+            if (sts < 0) {
+                fprintf(stderr, "Warning: refresh_shm_stat: pmdaCacheOp(%s, ADD, \"%s\", (%s)): %s\n",
+                pmInDomStr(shm_indom), shmid, shm_stat->shm_key, pmErrStr(sts));
+                free(shm_stat->shm_key);
+                free(shm_stat->shm_owner);
+                free(shm_stat->shm_status);
+                free(shm_stat);
+           }	
+	}
+    }
+    pmdaCacheOp(shm_indom, PMDA_CACHE_SAVE);
+    return 0;
+}
diff --git a/src/pmdas/linux/ipc.h b/src/pmdas/linux/ipc.h
index 04f33ca..61b38a6 100644
--- a/src/pmdas/linux/ipc.h
+++ b/src/pmdas/linux/ipc.h
@@ -12,6 +12,19 @@
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
  */
+#define SHM_DEST	01000	/* segment will be destroyed on last detach */
+#define SHM_LOCKED	02000	/* segment will not be swapped */
+
+/*
+ *  * X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;                            
+ *   *      glibc-1.09 has no support for sysv ipc.                                                     
+ *    *      glibc 2 uses __key, __seq
+ *     */
+#if defined (__GLIBC__) && __GLIBC__ >= 2
+# define KEY __key                                                                 
+#else
+# define KEY key
+#endif
 
 typedef struct {
     unsigned int shm_tot; /* total allocated shm */
@@ -94,3 +107,13 @@ typedef struct {
 
 extern int refresh_msg_limits(msg_limits_t *);
 
+typedef struct {
+        char                shm_key[16];
+        char                shm_owner[128];
+        unsigned int        shm_perms;
+        unsigned int        shm_bytes;
+        unsigned int        shm_nattch;
+        char                *shm_status;
+} shm_stat_t;
+
+extern int refresh_shm_stat(pmInDom shm_indom);
diff --git a/src/pmdas/linux/pmda.c b/src/pmdas/linux/pmda.c
index ddbf94f..5c4e0ce 100644
--- a/src/pmdas/linux/pmda.c
+++ b/src/pmdas/linux/pmda.c
@@ -312,6 +312,7 @@ static pmdaIndom indomtab[] = {
     { MD_INDOM, 0, NULL }, /* cached */
     { INTERRUPT_NAMES_INDOM, 0, NULL },
     { SOFTIRQS_NAMES_INDOM, 0, NULL },
+    { IPC_STAT_INDOM, 0, NULL },
 };
 
 
@@ -3773,6 +3774,41 @@ static pmdaMetric metrictab[] = {
     PMDA_PMUNITS(0,0,0,0,0,0)}},
 
 /*
+ * shared memory stat cluster
+ * Cluster added by wu liming <[email protected]>
+ */
+
+/* ipc.shm.key */
+  { NULL,
+    { PMDA_PMID(CLUSTER_SHM_STAT,0), PM_TYPE_STRING, IPC_STAT_INDOM, PM_SEM_DISCRETE, 
+    PMDA_PMUNITS(0,0,0,0,0,0) }, },
+
+/* ipc.shm.owner */
+  { NULL,
+    { PMDA_PMID(CLUSTER_SHM_STAT,1), PM_TYPE_STRING, IPC_STAT_INDOM, PM_SEM_DISCRETE, 
+    PMDA_PMUNITS(0,0,0,0,0,0) }, },
+
+/* ipc.shm.perms */
+  { NULL,
+    { PMDA_PMID(CLUSTER_SHM_STAT,2), PM_TYPE_U32, IPC_STAT_INDOM, PM_SEM_DISCRETE, 
+    PMDA_PMUNITS(0,0,0,0,0,0) }, },
+
+/* ipc.shm.segsz */
+  { NULL,
+    { PMDA_PMID(CLUSTER_SHM_STAT,3), PM_TYPE_U32, IPC_STAT_INDOM, PM_SEM_DISCRETE, 
+    PMDA_PMUNITS(0,0,0,0,0,0) }, },
+
+/* ipc.shm.nattch */
+  { NULL,
+    { PMDA_PMID(CLUSTER_SHM_STAT,4), PM_TYPE_U32, IPC_STAT_INDOM, PM_SEM_DISCRETE, 
+    PMDA_PMUNITS(0,0,0,0,0,0) }, },
+
+/* ipc.shm.status */
+  { NULL,
+    { PMDA_PMID(CLUSTER_SHM_STAT,5), PM_TYPE_STRING, IPC_STAT_INDOM, PM_SEM_DISCRETE, 
+    PMDA_PMUNITS(0,0,0,0,0,0) }, },
+
+/*
  * number of users cluster
  */
 
@@ -4787,6 +4823,9 @@ linux_refresh(pmdaExt *pmda, int *need_refresh, int context)
     if (need_refresh[CLUSTER_NET_SOFTNET])
 	refresh_proc_net_softnet(&proc_net_softnet);
 
+    if (need_refresh[CLUSTER_SHM_STAT])
+	refresh_shm_stat(INDOM(IPC_STAT_INDOM));
+
 done:
     if (need_refresh_mtab)
 	pmdaDynamicMetricTable(pmda);
@@ -4850,6 +4889,9 @@ linux_instance(pmInDom indom, int inst, char *name, __pmInResult **result, pmdaE
     case ICMPMSG_INDOM:
 	need_refresh[CLUSTER_NET_SNMP]++;
 	break;
+    case IPC_STAT_INDOM:
+	need_refresh[CLUSTER_SHM_STAT]++;
+	break;
     /* no default label : pmdaInstance will pick up errors */
     }
 
@@ -6130,6 +6172,43 @@ linux_fetchCallBack(pmdaMetric *mdesc, unsigned int inst, pmAtomValue *atom)
 	break;
 
     /*
+     * Cluster added by Wu Liming <[email protected]>
+     */
+    case CLUSTER_SHM_STAT: {
+	
+        shm_stat_t *shm_stat;
+
+	sts = pmdaCacheLookup(INDOM(IPC_STAT_INDOM), inst, NULL, (void **)&shm_stat);
+	if (sts < 0)
+	    return sts;
+	if (sts != PMDA_CACHE_ACTIVE)
+	    return PM_ERR_INST;
+	switch (idp->item) {
+	case 0:	/* ipc.shm.key */
+	    atom->cp = (char *)shm_stat->shm_key;
+	    break;
+	case 1:	/* ipc.shm.owner */
+	    atom->cp = (char *)shm_stat->shm_owner;
+	    break;
+	case 2:	/* ipc.shm.perms */
+	    atom->ul = shm_stat->shm_perms;
+	    break;
+	case 3:	/* ipc.shm.segsz */
+	    atom->ul = shm_stat->shm_bytes;
+	    break;
+	case 4:	/* ipc.shm.nattch */
+	    atom->ul = shm_stat->shm_nattch;
+	    break;
+	case 5:	/* ipc.shm.status */
+	    atom->cp = (char *)shm_stat->shm_status;
+	    break;
+	default:
+	    return PM_ERR_PMID;
+	}
+	break;
+     }
+     
+    /*
      * Cluster added by Mike Mason <[email protected]>
      */
     case CLUSTER_SEM_LIMITS:
diff --git a/src/pmdas/linux/root_linux b/src/pmdas/linux/root_linux
index aa16862..1f3b92d 100644
--- a/src/pmdas/linux/root_linux
+++ b/src/pmdas/linux/root_linux
@@ -243,6 +243,12 @@ ipc.shm {
     used_ids		60:56:3
     swap_attempts	60:56:4
     swap_successes	60:56:5
+    key 		60:64:0
+    owner 		60:64:1
+    perms 		60:64:2
+    segsz 		60:64:3
+    nattch 		60:64:4
+    status 		60:64:5
 }
 
 pmda {
-- 
1.8.3.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.