[Watchdog-devel] [PATCH] Add allocatable memory test
Brought to you by:
meskes,
paulcrawford
|
From: Eric B M. <em...@ak...> - 2014-10-17 14:20:08
|
Monitoring of free memory as reported in meminfo doesn't actually give
us a picture of what is allocatable. A better metric for this case is
to try and allocate an amount. This test attempts to mmap the specified
number of pages and fault them in.
Signed-off-by: Eric B Munson <em...@ak...>
---
include/extern.h | 2 ++
src/configfile.c | 5 +++++
src/memory.c | 25 +++++++++++++++++++++++++
src/watchdog.c | 3 +++
4 files changed, 35 insertions(+)
diff --git a/include/extern.h b/include/extern.h
index 04c0ccc..77bcb11 100644
--- a/include/extern.h
+++ b/include/extern.h
@@ -65,6 +65,7 @@ extern int maxload1;
extern int maxload5;
extern int maxload15;
extern int minpages;
+extern int minalloc;
extern int maxtemp;
extern int pingcount;
extern int temp_poweroff;
@@ -154,6 +155,7 @@ int check_iface(struct list *);
int open_memcheck(void);
int check_memory(void);
int close_memcheck(void);
+int check_allocatable(void);
/** shutdown.c **/
void do_shutdown(int errorcode);
diff --git a/src/configfile.c b/src/configfile.c
index 84af048..15891a0 100644
--- a/src/configfile.c
+++ b/src/configfile.c
@@ -35,6 +35,7 @@ static void add_test_binaries(const char *path);
#define MAXLOAD15 "max-load-15"
#define MAXTEMP "max-temperature"
#define MINMEM "min-memory"
+#define ALLOCMEM "allocatable-memory"
#define SERVERPIDFILE "pidfile"
#define PING "ping"
#define PINGCOUNT "ping-count"
@@ -66,6 +67,7 @@ int maxload1 = 0;
int maxload5 = 0;
int maxload15 = 0;
int minpages = 0;
+int minalloc = 0;
int maxtemp = 120;
int pingcount = 3;
int temp_poweroff = TRUE;
@@ -278,6 +280,9 @@ void read_config(char *configfile)
} else if (strncmp(line + i, MINMEM, strlen(MINMEM)) == 0) {
if (!spool(line, &i, strlen(MINMEM)))
minpages = atol(line + i);
+ } else if (strncmp(line + i, ALLOCMEM, strlen(ALLOCMEM)) == 0) {
+ if (!spool(line, &i, strlen(ALLOCMEM)))
+ minalloc = atol(line + i);
} else if (strncmp(line + i, LOGDIR, strlen(LOGDIR)) == 0) {
if (!spool(line, &i, strlen(LOGDIR)))
logdir = xstrdup(line + i);
diff --git a/src/memory.c b/src/memory.c
index d06c99d..9b6f7ef 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -20,6 +20,7 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/param.h>
+#include <sys/mman.h>
#include "extern.h"
#include "watch_err.h"
@@ -128,3 +129,27 @@ int close_memcheck(void)
mem_fd = -1;
return rv;
}
+
+int check_allocatable(void)
+{
+ int i;
+ char *mem;
+
+ if (minalloc <= 0)
+ return 0;
+
+ /*
+ * Map and fault in the pages
+ */
+ mem = mmap(NULL, EXEC_PAGESIZE * minalloc, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, 0, 0);
+ if (mem == MAP_FAILED) {
+ i = errno;
+ log_message(LOG_ALERT, "cannot allocate %d bytes (errno = %d)",
+ EXEC_PAGESIZE * minalloc, i);
+ return i;
+ }
+
+ munmap(mem, EXEC_PAGESIZE * minalloc);
+ return 0;
+}
diff --git a/src/watchdog.c b/src/watchdog.c
index bcc9954..babbece 100644
--- a/src/watchdog.c
+++ b/src/watchdog.c
@@ -415,6 +415,9 @@ int main(int argc, char *const argv[])
/* check free memory */
do_check(check_memory(), rbinary, NULL);
+ /* check allocatable memory */
+ do_check(check_allocatable(), rbinary, NULL);
+
/* check temperature */
for (act = temp_list; act != NULL; act = act->next)
do_check(check_temp(act), rbinary, NULL);
--
1.7.9.5
|