From 72365a6bac1f282d183e51b61156c5078ccb8a05 Mon Sep 17 00:00:00 2001
From: "Peter W. Draper" <p.w.draper@durham.ac.uk>
Date: Wed, 2 Aug 2017 13:21:35 +0100
Subject: [PATCH] Add function to report the memory use of the process, same
 output as 'top'

---
 src/debug.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/debug.h |  3 +++
 2 files changed, 66 insertions(+)

diff --git a/src/debug.c b/src/debug.c
index 601f63d6e1..2069c042e2 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -26,6 +26,7 @@
 /* Some standard headers. */
 #include <float.h>
 #include <stdio.h>
+#include <unistd.h>
 
 /* This object's header. */
 #include "debug.h"
@@ -450,3 +451,65 @@ void dumpCellRanks(const char *prefix, struct cell *cells_top, int nr_cells) {
 }
 
 #endif /* HAVE_MPI */
+
+/**
+ * @brief parse the process /proc/self/statm file to get the process
+ *        memory use (in KB). Top field in ().
+ *
+ * @param size     total virtual memory (VIRT)
+ * @param resident resident non-swapped memory (RES)
+ * @param share    shared (mmap'd) memory  (SHR)
+ * @param trs      text (exe) resident set (CODE)
+ * @param lrs      library resident set
+ * @param drs      data+stack resident set (DATA)
+ * @param dt       dirty pages (nDRT)
+ */
+void getProcMemUse(long *size, long *resident, long *share, long *trs,
+                   long *lrs, long *drs, long *dt) {
+
+  /* Open the file. */
+  FILE *file = fopen("/proc/self/statm", "r");
+  if (file != NULL) {
+    fscanf(file, "%ld %ld %ld %ld %ld %ld %ld", size, resident, share,
+           trs, lrs, drs, dt);
+
+    /* Convert pages into bytes. Usually 4096, but could be 512 on some
+     * systems so take care in conversion to KB. */
+    long sz = sysconf(_SC_PAGESIZE);
+    *size *= sz;
+    *resident *= sz;
+    *share *= sz;
+    *trs *= sz;
+    *lrs *= sz;
+    *drs *= sz;
+    *dt *= sz;
+
+    *size /= 1024;
+    *resident /= 1024;
+    *share /= 1024;
+    *trs /= 1024;
+    *lrs /= 1024;
+    *drs /= 1024;
+    *dt /= 1024;
+    fclose(file);
+  } else {
+    error("Failed to open /proc/self/statm");
+  }
+}
+
+/**
+ * @brief Print the current memory use of the process. A la "top".
+ */
+void printProcMemUse() {
+  long size;
+  long resident;
+  long share;
+  long trs;
+  long lrs;
+  long drs;
+  long dt;
+  getProcMemUse(&size, &resident, &share, &trs, &lrs, &drs, &dt);
+  printf("## VIRT = %ld , RES = %ld , SHR = %ld , CODE = %ld, DATA = %ld\n",
+          size, resident, share, trs, drs);
+  fflush(stdout);
+}
diff --git a/src/debug.h b/src/debug.h
index 7422a6f7f9..7dca848b6b 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -44,4 +44,7 @@ void dumpMETISGraph(const char *prefix, idx_t nvtxs, idx_t ncon, idx_t *xadj,
 void dumpCellRanks(const char *prefix, struct cell *cells_top, int nr_cells);
 #endif
 
+void getProcMemUse(long *size, long *resident, long *share, long *trs,
+                   long *lrs, long *drs, long *dt);
+void printProcMemUse();
 #endif /* SWIFT_DEBUG_H */
-- 
GitLab