summaryrefslogtreecommitdiff
path: root/src/procmemstat
diff options
context:
space:
mode:
Diffstat (limited to 'src/procmemstat')
-rw-r--r--src/procmemstat/GNUmakefile50
-rw-r--r--src/procmemstat/GNUmakefile.install39
-rw-r--r--src/procmemstat/README19
-rw-r--r--src/procmemstat/pmnsmap.spec21
-rw-r--r--src/procmemstat/procmemstat.c138
5 files changed, 267 insertions, 0 deletions
diff --git a/src/procmemstat/GNUmakefile b/src/procmemstat/GNUmakefile
new file mode 100644
index 0000000..e4911c7
--- /dev/null
+++ b/src/procmemstat/GNUmakefile
@@ -0,0 +1,50 @@
+#
+# Copyright (c) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
+#
+# 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.
+#
+
+TOPDIR = ../..
+include $(TOPDIR)/src/include/builddefs
+
+CFILES = procmemstat.c
+TARGETS =
+LLDLIBS = $(PCPLIB)
+LDIRT = pmnsmap.h mylog.* runme.sh $(TARGETS) pmnsmap.i GNUmakefile.install.xxx
+LSRCFILES = README GNUmakefile.install pmnsmap.spec
+
+DEMODIR=$(PCP_DEMOS_DIR)/procmemstat
+
+default: $(TARGETS) GNUmakefile.install.xxx
+
+include $(BUILDRULES)
+
+install: $(TARGETS)
+ $(INSTALL) -m 755 -d $(DEMODIR)
+ $(INSTALL) -m 644 GNUmakefile.install.xxx $(DEMODIR)/Makefile
+ $(INSTALL) -m 644 procmemstat.c README pmnsmap.spec $(DEMODIR)
+
+procmemstat.o : pmnsmap.h
+
+pmnsmap.h: pmnsmap.spec
+ PCP_CONF=$(TOPDIR)/src/include/pcp.conf; export PCP_CONF; \
+ sed -e "s;^\. \$PCP_DIR/etc/pcp.env;. $(TOPDIR)/src/include/pcp.env;" \
+ ../pmgenmap/pmgenmap.sh >runme.sh; \
+ $(RUN_IN_BUILD_ENV) $(TOPDIR)/src/pmcpp/pmcpp -D$(TARGET_OS) pmnsmap.spec \
+ | sed -e '/^#/d' -e '/^$$/d' >pmnsmap.i
+ sh ./runme.sh pmnsmap.i >pmnsmap.h
+
+GNUmakefile.install.xxx: GNUmakefile.install
+ sed -e "s;<CPP>;$(PCP_BINADM_DIR)/pmcpp -D$(TARGET_OS);" <GNUmakefile.install >GNUmakefile.install.xxx
+
+default_pcp : default
+
+install_pcp : install
diff --git a/src/procmemstat/GNUmakefile.install b/src/procmemstat/GNUmakefile.install
new file mode 100644
index 0000000..9b2a459
--- /dev/null
+++ b/src/procmemstat/GNUmakefile.install
@@ -0,0 +1,39 @@
+#
+# Copyright (c) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
+#
+# 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.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+CFILES = procmemstat.c
+CFLAGS = -I/usr/include/pcp -DPCP_DEBUG=1
+TARGETS = procmemstat
+LDOPTS =
+LDLIBS = -lpcp
+
+default: $(TARGETS)
+
+install:
+
+clobber:
+ rm -f $(TARGETS) *.o core a.out pmnsmap.h mylog.* pmnsmap.i
+
+procmemstat: procmemstat.c pmnsmap.h
+ rm -f $@
+ $(CC) $(CFLAGS) procmemstat.c -o $@ $(LDOPTS) $(LDLIBS)
+
+pmnsmap.h: pmnsmap.spec
+ <CPP> <pmnsmap.spec \
+ | sed -e '/^#/d' -e '/^$$/d' >pmnsmap.i
+ pmgenmap pmnsmap.i >pmnsmap.h
diff --git a/src/procmemstat/README b/src/procmemstat/README
new file mode 100644
index 0000000..b413fe5
--- /dev/null
+++ b/src/procmemstat/README
@@ -0,0 +1,19 @@
+procmemstat - a sample client using the PMAPI
+=============================================
+
+procmemstat is a sample client that uses the Performance Metrics
+Application Programming Interface (PMAPI) to report its own memory
+use.
+
+The source is shipped as part of pcp.sw.demo and is installed in
+/var/pcp/demos/procmemstat. If you have the C compiler installed, the
+source and Makefile in this directory may be used to create a
+functionally equivalent binary, simply by entering the command
+
+ % make
+
+The source in procmemstat.c demonstrates most of the PMAPI services, and
+may be used as a template and style guide when creating your own PMAPI
+clients. Note in particular, the use of ./pmnsmap.spec and the shipped
+tool pmgenmap to assist in the creation of arguments to the PMAPI
+routines, and the manipulation of PMAPI data structures.
diff --git a/src/procmemstat/pmnsmap.spec b/src/procmemstat/pmnsmap.spec
new file mode 100644
index 0000000..8df7190
--- /dev/null
+++ b/src/procmemstat/pmnsmap.spec
@@ -0,0 +1,21 @@
+metrics {
+#ifdef linux
+ proc.memory.rss RSS_TOTAL
+ proc.memory.textrss RSS_TEXT
+ proc.memory.librss RSS_LIB
+ proc.memory.datrss RSS_DATA
+#endif
+#ifdef sgi
+ /* IRIX */
+ proc.memory.virtual.txt V_TXT
+ proc.memory.virtual.dat V_DAT
+ proc.memory.virtual.bss V_BSS
+ proc.memory.virtual.stack V_STK
+ proc.memory.virtual.shm V_SHM
+ proc.memory.physical.txt P_TXT
+ proc.memory.physical.dat P_DAT
+ proc.memory.physical.bss P_BSS
+ proc.memory.physical.stack P_STK
+ proc.memory.physical.shm P_SHM
+#endif
+}
diff --git a/src/procmemstat/procmemstat.c b/src/procmemstat/procmemstat.c
new file mode 100644
index 0000000..f542455
--- /dev/null
+++ b/src/procmemstat/procmemstat.c
@@ -0,0 +1,138 @@
+/*
+ * procmemstat - sample, simple PMAPI client to report your own memory
+ * usage
+ *
+ * Copyright (c) 2013 Red Hat.
+ * Copyright (c) 2002 Silicon Graphics, Inc. All Rights Reserved.
+ *
+ * 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 "pmapi.h"
+#include "impl.h"
+#include "pmnsmap.h"
+
+static void
+get_sample(void)
+{
+ int first = 1;
+ static pmResult *rp;
+ static int numpmid;
+ static pmID *pmidlist;
+ static pmDesc *desclist;
+ pmUnits kbyte_scale;
+ int pid;
+ pmAtomValue tmp;
+ pmAtomValue atom;
+ int sts;
+ int i;
+
+ if (first) {
+ memset(&kbyte_scale, 0, sizeof(kbyte_scale));
+ kbyte_scale.dimSpace = 1;
+ kbyte_scale.scaleSpace = PM_SPACE_KBYTE;
+
+ numpmid = sizeof(metrics) / sizeof(char *);
+ if ((pmidlist = (pmID *)malloc(numpmid * sizeof(pmidlist[0]))) == NULL) {
+ fprintf(stderr, "%s: get_sample: malloc: %s\n", pmProgname, osstrerror());
+ exit(1);
+ }
+ if ((desclist = (pmDesc *)malloc(numpmid * sizeof(desclist[0]))) == NULL) {
+ fprintf(stderr, "%s: get_sample: malloc: %s\n", pmProgname, osstrerror());
+ exit(1);
+ }
+ if ((sts = pmLookupName(numpmid, metrics, pmidlist)) < 0) {
+ printf("%s: pmLookupName: %s\n", pmProgname, pmErrStr(sts));
+ for (i = 0; i < numpmid; i++) {
+ if (pmidlist[i] == PM_ID_NULL)
+ fprintf(stderr, "%s: metric \"%s\" not in name space\n", pmProgname, metrics[i]);
+ }
+ exit(1);
+ }
+ for (i = 0; i < numpmid; i++) {
+ if ((sts = pmLookupDesc(pmidlist[i], &desclist[i])) < 0) {
+ fprintf(stderr, "%s: cannot retrieve description for metric \"%s\" (PMID: %s)\nReason: %s\n",
+ pmProgname, metrics[i], pmIDStr(pmidlist[i]), pmErrStr(sts));
+ exit(1);
+ }
+ }
+ /*
+ * All metrics we care about share the same instance domain,
+ * and the instance of interest is _my_ PID
+ */
+ pmDelProfile(desclist[0].indom, 0, NULL); /* all off */
+ pid = (int)getpid();
+ pmAddProfile(desclist[0].indom, 1, &pid);
+
+ first = 0;
+ }
+
+ /* fetch the current metrics */
+ if ((sts = pmFetch(numpmid, pmidlist, &rp)) < 0) {
+ fprintf(stderr, "%s: pmFetch: %s\n", pmProgname, pmErrStr(sts));
+ exit(1);
+ }
+
+ printf("memory metrics for pid %" FMT_PID " (sizes in Kbytes)\n", pid);
+ for (i = 0; i < numpmid; i++) {
+ /* process metrics in turn */
+ pmExtractValue(rp->vset[i]->valfmt, rp->vset[i]->vlist,
+ desclist[i].type, &tmp, PM_TYPE_U32);
+ pmConvScale(PM_TYPE_U32, &tmp, &desclist[i].units,
+ &atom, &kbyte_scale);
+ printf("%8d %s\n", atom.l, metrics[i]);
+ }
+}
+
+pmLongOptions longopts[] = {
+ PMAPI_OPTIONS_HEADER("Options"),
+ PMOPT_DEBUG,
+ PMOPT_HELP,
+ PMAPI_OPTIONS_END
+};
+
+pmOptions opts = {
+ .short_options = "D:?",
+ .long_options = longopts,
+};
+
+int
+main(int argc, char **argv)
+{
+ int sts;
+ char *p;
+ char *q;
+
+ setlinebuf(stdout);
+ pmGetOptions(argc, argv, &opts);
+
+ if (opts.errors || opts.optind < argc - 1) {
+ pmUsageMessage(&opts);
+ exit(1);
+ }
+
+ if ((sts = pmNewContext(PM_CONTEXT_HOST, "local:")) < 0) {
+ fprintf(stderr, "%s: Cannot connect to PMCD on host \"local:\": %s\n",
+ pmProgname, pmErrStr(sts));
+ exit(1);
+ }
+
+ get_sample();
+
+#define ARRAY 1*1024*1024
+ p = (char *)malloc(ARRAY);
+ for (q = p; q < &p[ARRAY]; q += 1024)
+ *q = '\0';
+ printf("\nAfter malloc ...\n");
+ get_sample();
+
+ exit(0);
+}