From 203c4afe1c2bc889b97659c5415f27440afa66d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Stelmach?= Date: Fri, 21 Oct 2016 15:06:53 +0200 Subject: [PATCH] crash-pipe: Print memory information The code has been ported from sys-assert. Change-Id: I64ae2fb0d2a12f7def29328b1ac46cb6c335e0a5 --- src/crash-pipe/crash-pipe.c | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/crash-pipe/crash-pipe.c b/src/crash-pipe/crash-pipe.c index a0a113a..90cf2d7 100644 --- a/src/crash-pipe/crash-pipe.c +++ b/src/crash-pipe/crash-pipe.c @@ -33,6 +33,7 @@ #include #define NELEMS(arr) (sizeof(arr)/sizeof(arr[0])) +#define BUF_SIZE (BUFSIZ) enum { OPT_HELP, @@ -115,6 +116,98 @@ void print_multiline(char *buf, int buf_size) printf("%21d: %s\n", i, buf + pos); } +static char *fgets_fd(char *str, int len, int fd) +{ + char ch; + register char *cs; + int num = 0; + + cs = str; + while (--len > 0 && (num = read(fd, &ch, 1) > 0)) { + if ((*cs++ = ch) == '\n') + break; + } + *cs = '\0'; + return (num == 0 && cs == str) ? NULL : str; +} + +static void meminfo_report(const char *pidstr){ + char infoname[BUF_SIZE]; + char memsize[BUF_SIZE]; + char linebuf[BUF_SIZE]; + char file_path[PATH_MAX]; + int fd; + + printf("\nMemory information\n"); + + if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) { + fprintf(stderr, "[crash-pipe] can't open %s\n", file_path); + } else { + while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) { + sscanf(linebuf, "%s %s %*s", infoname, memsize); + if (strcmp("MemTotal:", infoname) == 0) { + printf("%s %8s KB\n", infoname, memsize); + } else if (strcmp("MemFree:", infoname) == 0) { + printf("%s %8s KB\n", infoname, memsize); + } else if (strcmp("Buffers:", infoname) == 0) { + printf("%s %8s KB\n", infoname, memsize); + } else if (strcmp("Cached:", infoname) == 0) { + printf("%s %8s KB\n", infoname, memsize); + break; + } + } + close(fd); + } + + snprintf(file_path, PATH_MAX, "/proc/%s/status", pidstr); + if ((fd = open(file_path, O_RDONLY)) < 0) { + fprintf(stderr, "[crash-pipe]can't open /proc/meminfo\n"); + } else { + while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) { + sscanf(linebuf, "%s %s %*s", infoname, memsize); + if (strcmp("VmPeak:", infoname) == 0) { + printf("%s %8s KB\n", infoname, + memsize); + } else if (strcmp("VmSize:", infoname) == 0) { + printf("%s %8s KB\n", infoname, + memsize); + } else if (strcmp("VmLck:", infoname) == 0) { + printf("%s %8s KB\n", infoname, + memsize); + } else if (strcmp("VmPin:", infoname) == 0) { + printf("%s %8s KB\n", infoname, + memsize); + } else if (strcmp("VmHWM:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmRSS:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmData:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmStk:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmExe:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmLib:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmPTE:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + } else if (strcmp("VmSwap:", infoname) == 0) { + printf("%s %8s KB\n", + infoname, memsize); + break; + } + } + close(fd); + } +} + static void report(int argc, char *argv[]) { const char *pidstr = argv[0]; @@ -150,6 +243,8 @@ static void report(int argc, char *argv[]) printf("Executable File Path: %s\n", exe_file); } + meminfo_report(pidstr); + printf(" - passed from kernel -\n" "%16s: %s\n" "%16s: %s\n" -- 2.7.4