+static void
+sample_mem_peak(void)
+{
+ /*
+ * We want to find out the peak memory usage of the process, which is
+ * maintained by the kernel, but unforunately it gets lost when the
+ * process exits (it is not reported in struct rusage). Therefore we
+ * have to sample it whenever we suspect that the process is about
+ * to exit.
+ */
+ char buf[PROC_BUF_SIZE], *x;
+ static int proc_status_fd;
+ read_proc_file(buf, "status", &proc_status_fd);
+
+ x = buf;
+ while (*x)
+ {
+ char *key = x;
+ while (*x && *x != ':' && *x != '\n')
+ x++;
+ if (!*x || *x == '\n')
+ break;
+ *x++ = 0;
+ while (*x == ' ' || *x == '\t')
+ x++;
+
+ char *val = x;
+ while (*x && *x != '\n')
+ x++;
+ if (!*x)
+ break;
+ *x++ = 0;
+
+ if (!strcmp(key, "VmPeak"))
+ {
+ int peak = atoi(val);
+ if (peak > mem_peak_kb)
+ mem_peak_kb = peak;
+ }
+ }
+
+ if (verbose > 1)
+ msg("[mem-peak: %u KB]\n", mem_peak_kb);
+}
+