crash-pipe: Print memory information 15/96915/2
authorŁukasz Stelmach <l.stelmach@samsung.com>
Fri, 21 Oct 2016 13:06:53 +0000 (15:06 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 8 Dec 2016 13:43:13 +0000 (14:43 +0100)
The code has been ported from sys-assert.

Change-Id: I64ae2fb0d2a12f7def29328b1ac46cb6c335e0a5

src/crash-pipe/crash-pipe.c

index a0a113adfa66cab77b0bc15112cc54dfebad90c2..90cf2d7f01a6a17873c21cde8b9fb3da484adfd1 100644 (file)
@@ -33,6 +33,7 @@
 #include <limits.h>
 
 #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"