tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[PATCH] a character device counting kernel pages
Hi,
the attached patch is implementing a simple counter of memory pages used by
the kernel.
This kernel memory consumption measuring device (/dev/kp0, major=239, minor=0)
is reporting (in decimal form, useful for shell scripts) the number of
physical memory pages used by the kernel itself. It is tested for ARM only,
but will probably work with some other architectures too. To enable it add the
line "pseudo-device pagecounter" to the config file and create the device node
(mknod /dev/kp0 c 239 0).
Usage in a shell script: KERNEL_MEM=$(cat /dev/kp0)
Any comment and suggestions are welcome.
Greetings,
Ilya Dogolazky
---
sys/conf/files | 3 +
sys/conf/majors | 1 +
sys/dev/pagecounter.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 123 insertions(+), 0 deletions(-)
create mode 100644 sys/dev/pagecounter.c
diff --git a/sys/conf/files b/sys/conf/files
index bd5d919..4b5213c 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -1621,3 +1621,6 @@ include "lib/libx86emu/files.x86emu"
# "tprof" profiler.
#
include "dev/tprof/files.tprof"
+
+defpseudo pagecounter
+file dev/pagecounter.c pagecounter needs-flag
diff --git a/sys/conf/majors b/sys/conf/majors
index c0af446..af04480 100644
--- a/sys/conf/majors
+++ b/sys/conf/majors
@@ -37,3 +37,4 @@ device-major pad char 189 pad
device-major zfs char 190 zfs
device-major tprof char 191 tprof
device-major isv char 192 isv
+device-major pagecounter char 239
diff --git a/sys/dev/pagecounter.c b/sys/dev/pagecounter.c
new file mode 100644
index 0000000..1c5d732
--- /dev/null
+++ b/sys/dev/pagecounter.c
@@ -0,0 +1,119 @@
+/* $NetBSD$ */
+
+/*
+ * Copyright (c) 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This software was developed by the Computer Systems Engineering group
+ * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
+ * contributed to Berkeley.
+ *
+ * All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Lawrence Berkeley Laboratory.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)ms.c 8.1 (Berkeley) 6/11/93
+ */
+
+/*
+ * Device counting memory pages used by the kernel (/dev/kp0)
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD$");
+
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/vnode.h>
+
+#define PAGECOUNTER_BUFSIZE 21
+
+struct pagecounter_softc {
+ struct device sc_dev;
+};
+
+void pagecounterattach(struct device *parent, struct device *self,
void *aux);
+int pagecounterread(dev_t device, struct uio *io, int flags);
+int pagecounteropen(dev_t device, int flags, int fmt, struct lwp
*process);
+static int count_pages(void);
+
+const struct cdevsw pagecounter_cdevsw = {
+ pagecounteropen,
+ noclose,
+ pagecounterread,
+ nowrite,
+ noioctl,
+ nostop,
+ notty,
+ nopoll,
+ nommap,
+ nokqfilter,
+};
+
+void
+pagecounterattach(struct device *parent, struct device *self, void *aux)
+{
+ printf("Initializing kernel page counter /dev/kp0\n");
+}
+
+int
+pagecounteropen(dev_t device, int flags, int fmt, struct lwp *process)
+{
+ return minor(device)==0 ? 0 : ENXIO ;
+}
+
+int
+pagecounterread(dev_t device, struct uio *io, int flags)
+{
+ char buf[PAGECOUNTER_BUFSIZE];
+ int len;
+
+ if (io->uio_offset > 0) /* second read call, return EOF */
+ return 0;
+
+ sprintf(buf, "%d", count_pages());
+ len = strlen(buf);
+
+ if (len > io->uio_resid) /* partial read not supported */
+ return ENODEV;
+
+ uiomove((void*)(buf), len, io);
+ return 0;
+}
+
+static int count_pages(void)
+{
+ struct vm_page *pg;
+ unsigned i, km_pages=0;
+
+ for (i=0; i < vm_nphysseg; i++)
+ for (pg = vm_physmem[i].pgs; pg <= vm_physmem[i].lastpg; pg++)
+ if(pg->mdpage.pvh_attrs & PVF_KMPAGE)
+ ++ km_pages;
+ return km_pages;
+}
--
1.5.6.3
Home |
Main Index |
Thread Index |
Old Index