Add imported to geminfo
[platform/core/system/memps.git] / memps.c
1 /* Copyright 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include <math.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/vfs.h>
27 #include <linux/limits.h>
28 #include <limits.h>
29
30 #include <ctype.h>
31 #include <stddef.h>
32 #include <inttypes.h>
33
34 #include <dirent.h>
35 #include <sys/utsname.h>
36
37 #define STR_SGX_PATH    "/dev/pvrsrvkm"
38 #define STR_3D_PATH1    "/dev/mali"
39 #define STR_3D_PATH2    "/dev/kgsl-3d0"
40 #define STR_3D_UNIFIED_PATH "/usr/bin/gpu_mem_info"
41 #define STR_DRM_PATH1   "/drm mm object (deleted)"
42 #define STR_DRM_PATH2   "/dev/dri/card"
43 #define STR_DRM_DBG_DIR "/sys/kernel/debug/dri/"
44 #define STR_DRM_RENDER_PATH     "/dev/dri/renderD"
45 #define MEMCG_PATH      "/sys/fs/cgroup/memory"
46 #define ZRAM_USED_PATH  "/sys/block/zram0/mem_used_total"
47 #define ZRAM_MM_STAT_PATH       "/sys/block/zram0/mm_stat"
48 #define OOM_SCORE_ADJ_STR       "oom_score_adj"
49 #define OOM_SCORE_STR   "oom_score"
50
51 #define BUF_MAX         (BUFSIZ)            /* most optimal for libc::stdio */
52 #define BUF_INC_SIZE    (512 * 1024)        /* maximal SMAPS I saw 2 MB     */
53 #define KB(bytes)       ((bytes)/1024)
54
55 #define BYTE_TO_KBYTE(b) ((b) >> 10)
56 #define BYTE_TO_MBYTE(b) ((b) >> 20)
57 #define BYTE_TO_GBYTE(b) ((b) >> 30)
58
59 #define KBYTE_TO_BYTE(k) ((k) << 10)
60 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
61 #define KBYTE_TO_GBYTE(k) ((k) >> 20)
62
63 #define GBYTE_TO_BYTE(g) ((g) << 30)
64 #define GBYTE_TO_KBYTE(g) ((g) << 20)
65 #define GBYTE_TO_MBYTE(g) ((g) << 10)
66
67 typedef struct geminfo geminfo;
68 typedef struct mapinfo mapinfo;
69 typedef struct trib_mapinfo trib_mapinfo;
70
71 enum {
72         OUTPUT_UART,
73         OUTPUT_FILE,
74         NUM_OUTPUT_TYPE
75 };
76
77 struct mapinfo {
78         mapinfo *next;
79         unsigned long start;
80         unsigned long end;
81         unsigned size;
82         unsigned swap;
83         unsigned rss;
84         unsigned pss;
85         unsigned shared_clean;
86         unsigned shared_dirty;
87         unsigned private_clean;
88         unsigned private_dirty;
89         char *perm;
90         char *name;
91 };
92
93 /* classify normal, graphic and other devices memory */
94 struct trib_mapinfo {
95         unsigned shared_clean;
96         unsigned shared_dirty;
97         unsigned private_clean;
98         unsigned private_dirty;
99         unsigned shared_clean_pss;
100         unsigned shared_dirty_pss;
101         unsigned swap;
102         unsigned rss;
103         unsigned pss;
104         unsigned size;
105         unsigned graphic_3d;
106         unsigned gem_rss;
107         unsigned gem_pss;
108         unsigned peak_rss;
109         unsigned other_devices;
110         unsigned gem_mmap;
111         unsigned render_gem_mmap;
112 };
113
114 struct geminfo {
115         geminfo *next;
116         unsigned int tgid;
117         unsigned rss_size;
118         unsigned pss_size;
119         unsigned hcount;
120         unsigned int imported;
121 };
122
123 static int sum;
124 static int verbos;
125 static int use_gpu_mem_info = 0;
126 static int pid_max = 0;
127 static int dri_card = 0;
128
129 /* reads file contents into memory */
130 static char* cread(const char* path)
131 {
132         /* once allocated area for reads */
133         static char*    text = NULL;
134         static size_t   size = 0;
135
136         ssize_t ret;
137         char*   ptr = text;
138         size_t  cap = size;
139         int     fd  = open(path, O_RDONLY);
140
141         if (fd < 0)
142                 return NULL;
143
144         do {
145                 /* ensure we have enough space */
146                 if (cap == 0) {
147                         ptr = (char*)realloc(text, size + BUF_INC_SIZE);
148                         if (ptr == NULL) {
149                                 ret = -1;
150                                 break;
151                         }
152
153                         text  = ptr;
154                         ptr   = text + size;
155                         cap   = BUF_INC_SIZE;
156                         size += BUF_INC_SIZE;
157                 }
158                 ret = read(fd, ptr, cap);
159                 if (ret == 0) {
160                         *ptr = 0;
161                 } else if (ret > 0) {
162                         cap -= ret;
163                         ptr += ret;
164                 }
165         } while (ret > 0);
166         close(fd);
167
168         return (ret < 0 ? NULL : text);
169 } /* cread */
170
171 /* like fgets/gets but adjusting contents pointer */
172 static inline char* cgets(char** contents)
173 {
174         if (contents && *contents && **contents) {
175                 char* bos = *contents;          /* begin of string */
176                 char* eos = strchr(bos, '\n');  /* end of string   */
177
178                 if (eos) {
179                         *contents = eos + 1;
180                         *eos      = 0;
181                 } else {
182                         *contents = NULL;
183                 }
184                 return bos;
185         }
186
187         return NULL;
188 } /* cgets */
189
190 /* get pid_max value */
191 static inline int get_pid_max(void)
192 {
193         static const char pid_max_path[] = "/proc/sys/kernel/pid_max";
194         char* line;
195
196         line = cread(pid_max_path);
197         if (line == NULL) {
198                 fprintf(stderr, "cannot open %s\n", pid_max_path);
199                 return 0;
200         }
201
202         return strtoul(line, NULL, 10);
203 }
204
205
206 static unsigned get_peak_rss(unsigned int pid)
207 {
208         static const char field[] = "VmHWM:";
209         char tmp[128];
210         char* line;
211         char* value;
212
213         snprintf(tmp, sizeof(tmp), "/proc/%d/status", pid);
214         line = cread(tmp);
215         if (line == NULL) {
216                 fprintf(stderr, "cannot open %s\n", tmp);
217                 return 0;
218         }
219
220         value = strstr(line, field);
221         if (value) {
222                 value += sizeof(field);
223                 return strtoul(value, NULL, 10);
224         }
225
226         return 0;
227 }
228 #define NUM_GEM_FIELD 7
229
230 static geminfo *read_geminfo(FILE *fp)
231 {
232         geminfo *tgeminfo;
233         char line[BUF_MAX];
234         unsigned int pid, tgid, handle, refcount, hcount;
235         unsigned gem_size;
236         unsigned int imported;
237
238         if (fgets(line, BUF_MAX, fp) != NULL) {
239                 if (sscanf(line, "%d %d %d %d %d 0x%x 0x%*x %*d %*d %d",
240                         &pid, &tgid, &handle, &refcount,
241                         &hcount, &gem_size, &imported) != NUM_GEM_FIELD)
242                         return NULL;
243
244                 if (hcount == 0)
245                         return NULL;
246                 tgeminfo = malloc(sizeof(geminfo));
247                 if (tgeminfo == NULL)
248                         return NULL;
249                 tgeminfo->tgid = tgid;
250                 tgeminfo->hcount = hcount;
251                 tgeminfo->rss_size = KB(gem_size);
252                 tgeminfo->pss_size = KB(gem_size/tgeminfo->hcount);
253                 tgeminfo->imported = imported;
254         } else
255                 return NULL;
256
257         return tgeminfo;
258 }
259
260
261 static geminfo *find_geminfo(unsigned int tgid, geminfo *gilist)
262 {
263         geminfo *gi;
264         for (gi = gilist; gi; ) {
265                 if (gi->tgid == tgid)
266                         return gi;
267
268                 gi = gi->next;
269         }
270         return NULL;
271 }
272
273 static geminfo *get_geminfo(FILE *drm_fp)
274 {
275         geminfo *ginfo;
276         geminfo *gilist = NULL;
277         geminfo *exist_ginfo = NULL;
278
279         char line[BUF_MAX];
280
281         if (fgets(line, BUF_MAX, drm_fp) == NULL) {
282                 return NULL;
283         } else {
284                 /* we should count a number of whitespace separated fields */
285                 int in_field = (line[0] && !isblank(line[0]));
286                 unsigned int size = (unsigned)in_field;
287                 const char*  ptr  = &line[1];
288
289                 /* sscanf() was used in original code, so number of fields */
290                 /* in string is expected to be at least NUM_GEM_FIELD      */
291                 while (*ptr && size < NUM_GEM_FIELD) {
292                         if (isblank(*ptr++)) {
293                                 if (in_field) {
294                                         /* end of field */
295                                         in_field = 0;
296                                 }
297                         } else {
298                                 if (!in_field) {
299                                         /* next field started */
300                                         in_field = 1;
301                                         size++;
302                                 }
303                         }
304                 } /* while */
305
306                 if (size != NUM_GEM_FIELD) {
307                         return NULL;
308                 }
309         }
310
311         while ((ginfo = read_geminfo(drm_fp)) != NULL) {
312                 if (gilist && ginfo->tgid == gilist->tgid) {
313                         gilist->pss_size += ginfo->pss_size;
314                         gilist->rss_size += ginfo->rss_size;
315                         free(ginfo);
316                         continue;
317                 } else if (gilist && ((exist_ginfo = find_geminfo(ginfo->tgid, gilist)) != NULL)) {
318                         exist_ginfo->pss_size += ginfo->pss_size;
319                         exist_ginfo->rss_size += ginfo->rss_size;
320                         free(ginfo);
321                         continue;
322                 }
323                 ginfo->next = gilist;
324                 gilist = ginfo;
325         }
326
327         return gilist;
328 }
329
330 static geminfo *load_geminfo(void)
331 {
332         geminfo *gilist;
333         FILE *drm_fp;
334
335         drm_fp = fopen(STR_DRM_DBG_DIR "0/gem_info", "r");
336         if (drm_fp == NULL) {
337                 drm_fp = fopen(STR_DRM_DBG_DIR "1/gem_info", "r");
338                 dri_card = 1;
339                 if (drm_fp == NULL) {
340                         fprintf(stderr,
341                                 "cannot open " STR_DRM_DBG_DIR "%d/gem_info\n",
342                                 dri_card);
343                         return NULL;
344                 }
345         }
346
347         gilist = get_geminfo(drm_fp);
348
349         fclose(drm_fp);
350
351         return gilist;
352 }
353
354 static geminfo *load_gpu_geminfo(void)
355 {
356         geminfo *gilist;
357         FILE *drm_fp;
358
359         /* Check the render dri card having gpu_gem_info */
360         drm_fp = fopen(STR_DRM_DBG_DIR "128/gpu_gem_info", "r");
361         if (drm_fp == NULL) {
362                 fprintf(stderr,
363                         "cannot open " STR_DRM_DBG_DIR "128/gpu_gem_info\n");
364                 return NULL;
365         }
366
367         gilist = get_geminfo(drm_fp);
368
369         fclose(drm_fp);
370
371         return gilist;
372 }
373
374 /* b6e82000-b6e83000 rw-p 00020000 b3:19 714        /usr/lib/ld-2.20-2014.11.so  : TM1
375  * 7f9389d000-7f9389e000 rw-p 0001f000 b3:12 618                            /usr/lib64/ld-2.20-2014.11.so  : TM2
376  * 7fae2e4b2000-7fae2e4b3000 r--p 00021000 fe:01 603                        /usr/lib64/ld-2.20-2014.11.so  : x86-64 Emulator
377  * 01234567890123456789012345678901234567890123456789012345678901234567890123456789
378  * 0         1         2         3         4         5         6         7
379  */
380 mapinfo *read_mapinfo(char** smaps)
381 {
382         char* line;
383         mapinfo *mi;
384         int len;
385         int n;
386
387         if ((line = cgets(smaps)) == 0)
388                 return 0;
389
390         len = strlen(line);
391         if (len < 1)
392                 return 0;
393
394         mi = malloc(sizeof(mapinfo));
395         if (mi == 0)
396                 return 0;
397
398         n = sscanf(line, "%lx-%lx %ms %*s %*s %*s %m[^\n]",
399                            &mi->start, &mi->end, &mi->perm, &mi->name);
400
401         if (n == 3 && !mi->name)
402                 mi->name = strndup("[anon]", strlen("[anon]"));
403         else if (n <= 3) {
404                 fprintf(stderr, "Fail to parse smaps\n");
405                 free(mi);
406                 return 0;
407         }
408
409         while ((line = cgets(smaps))) {
410                 if (sscanf(line, "Size: %d kB", &mi->size) == 1)
411                         ;
412                 else if (sscanf(line, "Rss: %d kB", &mi->rss) == 1)
413                         ;
414                 else if (sscanf(line, "Pss: %d kB", &mi->pss) == 1)
415                         ;
416                 else if (sscanf(line, "Shared_Clean: %d kB", &mi->shared_clean) == 1)
417                         ;
418                 else if (sscanf(line, "Shared_Dirty: %d kB", &mi->shared_dirty) == 1)
419                         ;
420                 else if (sscanf(line, "Private_Clean: %d kB", &mi->private_clean) == 1)
421                         ;
422                 else if (sscanf(line, "Private_Dirty: %d kB", &mi->private_dirty) == 1)
423                         ;
424                 else if (sscanf(line, "Swap: %d kB", &mi->swap) == 1)
425                         ;
426                 if (*smaps) {
427                         /* Drain lines until it meets next VMA address */
428                         char next = **smaps;
429                         if  ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f'))
430                                 break;
431                 }
432         }
433
434         return mi;
435 }
436
437 static unsigned total_gem_memory(void)
438 {
439         FILE *gem_fp;
440         unsigned total_gem_mem = 0;
441         unsigned name, size, handles, refcount;
442         char line[BUF_MAX];
443
444         if (!dri_card)
445                 gem_fp = fopen(STR_DRM_DBG_DIR "0/gem_names", "r");
446         else
447                 gem_fp = fopen(STR_DRM_DBG_DIR "1/gem_names", "r");
448         if (gem_fp == NULL) {
449                 fprintf(stderr,
450                 "cannot open " STR_DRM_DBG_DIR "%d/gem_names\n", dri_card);
451                 return 0;
452         }
453
454         if (fgets(line, BUF_MAX, gem_fp) == NULL) {
455                 fclose(gem_fp);
456                 return 0;
457         }
458
459         while (fgets(line, BUF_MAX, gem_fp) != NULL) {
460                 if (sscanf(line, "%d %d %d %d\n",
461                     &name, &size, &handles, &refcount) == 4) {
462                         if (total_gem_mem <= UINT_MAX - size) {
463                                 total_gem_mem += size;
464                         }
465                 }
466         }
467         fclose(gem_fp);
468
469         return total_gem_mem;
470 }
471
472 int get_zram_used(u_int32_t *zram_used)
473 {
474         FILE *f = NULL;
475         /* only read 3rd value */
476         char *fscanf_format = "%*d %*d %d %*d %*d %*d %*d %*d";
477         int ret;
478
479         f = fopen(ZRAM_MM_STAT_PATH, "r");
480         if (!f) {
481                 /*
482                  * ZRAM_USED_PATH is deprecated on latest kernel, but to support
483                  * old kernel try with that if fails new node 'mm_stat'.
484                  */
485                 f = fopen(ZRAM_USED_PATH, "r");
486                 if (!f) {
487                         fprintf(stderr, "Fail to open zram file.\n");
488                         return -1;
489                 }
490                 fscanf_format = "%u";
491         }
492
493         ret = fscanf(f, fscanf_format, zram_used);
494         if (ret == EOF) {
495                 fprintf(stderr, "Fail to read file\n");
496                 fclose(f);
497                 return -1;
498         }
499
500         fclose(f);
501         return 0;
502 }
503
504 int fread_uint(const char *path, u_int32_t *number)
505 {
506         FILE *f = NULL;
507         int ret;
508
509         f = fopen(path, "r");
510
511         if (!f) {
512                 fprintf(stderr, "Fail to open %s file.\n", path);
513                 return -1;
514         }
515
516         ret = fscanf(f, "%u", number);
517         if (ret == EOF) {
518                 fprintf(stderr, "Fail to read file\n");
519                 fclose(f);
520                 return -1;
521         }
522
523         fclose(f);
524         return 0;
525 }
526
527 static int cgroup_read_node(const char *cgroup_name,
528                 const char *file_name, unsigned int *value)
529 {
530         char buf[PATH_MAX + NAME_MAX];
531         int ret;
532         snprintf(buf, sizeof(buf), "%s%s", cgroup_name, file_name);
533         ret = fread_uint(buf, value);
534         return ret;
535 }
536
537 /**
538  * @desc Provides usage in bytes for provided memory cgroup. Works
539  * with/without swap accounting.
540  *
541  * @param memcg_path[in] Full path to memory cgroup
542  * @param swap[in] Boolean value for deciding if account usage with swap
543  * @return current cgroup usage in bytes or 0 on error
544  */
545 static unsigned int get_memcg_usage(const char *memcg_path, bool swap)
546 {
547         int ret;
548         unsigned int usage;
549
550         if (swap) {
551                 ret = cgroup_read_node(memcg_path,
552                                 "/memory.memsw.usage_in_bytes", &usage);
553         } else {
554                 ret = cgroup_read_node(memcg_path, "/memory.usage_in_bytes",
555                                 &usage);
556         }
557
558         if (ret != 0)
559                 usage = 0;
560
561         return usage;
562 }
563
564 static void get_memcg_info(FILE *output_fp)
565 {
566         char buf[PATH_MAX];
567         DIR *pdir = NULL;
568         struct dirent *entry;
569         struct stat path_stat;
570         long usage_swap;
571         unsigned long usage, usage_with_swap;
572
573         fprintf(output_fp, "====================================================================\n");
574         fprintf(output_fp, "MEMORY CGROUPS USAGE INFO\n");
575
576         pdir = opendir(MEMCG_PATH);
577         if (pdir == NULL) {
578                 fprintf(stderr, "cannot read directory %s", MEMCG_PATH);
579                 return;
580         }
581
582         errno = 0;
583         while ((entry = readdir(pdir)) != NULL && !errno) {
584                 snprintf(buf, sizeof(buf), "%s/%s", MEMCG_PATH, entry->d_name);
585                 /* If can't stat then ignore */
586                 if (stat(buf, &path_stat) != 0)
587                         continue;
588
589                 /* If it's not directory or it's parent path then ignore */
590                 if (!(S_ISDIR(path_stat.st_mode) &&
591                         strncmp(entry->d_name, "..", 3)))
592                         continue;
593
594                 usage = get_memcg_usage(buf, false);
595                 usage_with_swap = get_memcg_usage(buf, true);
596                 /* It is posible by rounding errors to get negative value */
597                 usage_swap = usage_with_swap - usage;
598                 if (usage_swap < 0)
599                         usage_swap = 0;
600
601                 /* Case of root cgroup in hierarchy */
602                 if (!strncmp(entry->d_name, ".", 2))
603                         fprintf(output_fp, "%13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB) \n\n",
604                                 MEMCG_PATH, BYTE_TO_MBYTE(usage),
605                                 BYTE_TO_KBYTE(usage),
606                                 BYTE_TO_MBYTE(usage_with_swap),
607                                 BYTE_TO_KBYTE(usage_with_swap),
608                                 BYTE_TO_MBYTE(usage_swap),
609                                 BYTE_TO_KBYTE(usage_swap));
610                 else
611                         fprintf(output_fp, "memcg: %13s  Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB)\n",
612                                 entry->d_name, BYTE_TO_MBYTE(usage),
613                                 BYTE_TO_KBYTE(usage),
614                                 BYTE_TO_MBYTE(usage_with_swap),
615                                 BYTE_TO_KBYTE(usage_with_swap),
616                                 BYTE_TO_MBYTE(usage_swap),
617                                 BYTE_TO_KBYTE(usage_swap));
618
619         }
620
621         closedir(pdir);
622 }
623
624 static void get_mem_info(FILE *output_fp)
625 {
626         char buf[PATH_MAX];
627         FILE *fp;
628         char *idx;
629         unsigned int free = 0, cached = 0;
630         unsigned int total_mem = 0, available = 0, used;
631         unsigned int swap_total = 0, swap_free = 0, zram_used, swap_used;
632         unsigned int used_ratio;
633
634         if (output_fp == NULL)
635                 return;
636
637         fp = fopen("/proc/meminfo", "r");
638
639         if (!fp) {
640                 fprintf(stderr, "/proc/meminfo open failed, %p", fp);
641                 return;
642         }
643
644         while (fgets(buf, PATH_MAX, fp) != NULL) {
645                 if ((idx = strstr(buf, "MemTotal:"))) {
646                         idx += strlen("Memtotal:");
647                         while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
648                                 idx++;
649                         total_mem = atoi(idx);
650                 } else if ((idx = strstr(buf, "MemFree:"))) {
651                         idx += strlen("MemFree:");
652                         while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
653                                 idx++;
654                         free = atoi(idx);
655                 } else if ((idx = strstr(buf, "MemAvailable:"))) {
656                         idx += strlen("MemAvailable:");
657                         while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
658                                 idx++;
659                         available = atoi(idx);
660                 } else if ((idx = strstr(buf, "Cached:")) && !strstr(buf, "Swap")) {
661                         idx += strlen("Cached:");
662                         while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
663                                 idx++;
664                         cached = atoi(idx);
665                 } else if ((idx = strstr(buf, "SwapTotal:"))) {
666                         idx += strlen("SwapTotal:");
667                         while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
668                                 idx++;
669                         swap_total = atoi(idx);
670                 } else if ((idx = strstr(buf, "SwapFree:"))) {
671                         idx += strlen("SwapFree");
672                         while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
673                                 idx++;
674                         swap_free = atoi(idx);
675                         break;
676                 }
677         }
678
679         if (total_mem == 0) {
680                 fprintf(stderr, "cannot get total memory size\n");
681                 fclose(fp);
682                 return;
683         }
684
685         if (available == 0)
686                 available = free + cached;
687         used = total_mem - available;
688         used_ratio = used * 100 / total_mem;
689         swap_used = swap_total - swap_free;
690
691         if (get_zram_used(&zram_used) < 0)
692                 zram_used = 0;
693
694         fprintf(output_fp,
695                 "====================================================================\n");
696
697
698         fprintf(output_fp, "Total RAM size: \t%15d MB( %6d kB)\n",
699                         total_mem >> 10, total_mem);
700
701         fprintf(output_fp, "Used (Mem+Reclaimable): %15d MB( %6d kB)\n",
702                         (total_mem - free) >> 10, total_mem - free);
703
704         fprintf(output_fp, "Used (Mem+Swap): \t%15d MB( %6d kB)\n",
705                         used >> 10, used);
706
707         fprintf(output_fp, "Used (Mem):  \t\t%15d MB( %6d kB)\n",
708                         used >> 10, used);
709
710         fprintf(output_fp, "Used (Swap): \t\t%15d MB( %6d kB)\n",
711                         swap_used >> 10, swap_used);
712
713         fprintf(output_fp, "Used (Zram block device): %13d MB( %6d kB)\n",
714             BYTE_TO_MBYTE(zram_used), BYTE_TO_KBYTE(zram_used));
715
716         fprintf(output_fp, "Used Ratio: \t\t%15d  %%\n", used_ratio);
717
718         fprintf(output_fp, "Mem Free:\t\t%15d MB( %6d kB)\n",
719                         free >> 10, free);
720
721         fprintf(output_fp, "Available (Free+Reclaimable):%10d MB( %6d kB)\n",
722                         available >> 10,
723                         available);
724         fclose(fp);
725 }
726
727 static int get_tmpfs_info(FILE *output_fp)
728 {
729         FILE *fp;
730         char line[BUF_MAX];
731         char *tmpfs_mp; /* tmpfs mount point */
732         struct statfs tmpfs_info;
733
734         if (output_fp == NULL)
735                 return -1;
736
737         fp = fopen("/etc/mtab", "r");
738         if (fp == NULL)
739                 return -1;
740
741         fprintf(output_fp,
742                 "====================================================================\n");
743         fprintf(output_fp, "TMPFS INFO\n");
744
745         while (fgets(line, BUF_MAX, fp) != NULL) {
746                 if (sscanf(line, "tmpfs %ms tmpfs", &tmpfs_mp) == 1) {
747                         if (statfs(tmpfs_mp, &tmpfs_info) == 0) {
748                                 fprintf(output_fp,
749 #ifndef __USE_FILE_OFFSET64
750                                         "tmpfs %16s  Total %8ld KB, Used %8ld, Avail %8ld\n",
751 #else
752                                         "tmpfs %16s  Total %8"PRId64" KB, Used %8"PRId64", Avail %8"PRId64"\n",
753 #endif
754                                         tmpfs_mp,
755                                         /* 1 block is 4 KB */
756                                         tmpfs_info.f_blocks * 4,
757                                         (tmpfs_info.f_blocks - tmpfs_info.f_bfree) * 4,
758                                         tmpfs_info.f_bfree * 4);
759                         }
760                         free(tmpfs_mp);
761                         tmpfs_mp = NULL;
762                 }
763         }
764         fclose(fp);
765         return 0;
766 }
767
768 mapinfo *load_maps(int pid)
769 {
770         char* smaps;
771         char tmp[128];
772         mapinfo *milist = 0;
773         mapinfo *mi;
774
775         snprintf(tmp, sizeof(tmp), "/proc/%d/smaps", pid);
776         smaps = cread(tmp);
777         if (smaps == NULL)
778                 return 0;
779
780         while ((mi = read_mapinfo(&smaps)) != 0) {
781                 if (milist) {
782                         if ((!strcmp(mi->name, milist->name)
783                              && (mi->name[0] != '['))) {
784                                 milist->size += mi->size;
785                                 milist->swap += mi->swap;
786                                 milist->rss += mi->rss;
787                                 milist->pss += mi->pss;
788                                 milist->shared_clean += mi->shared_clean;
789                                 milist->shared_dirty += mi->shared_dirty;
790                                 milist->private_clean += mi->private_clean;
791                                 milist->private_dirty += mi->private_dirty;
792
793                                 milist->end = mi->end;
794                                 strncpy(milist->perm, mi->perm, 4);
795                                 free(mi->perm);
796                                 free(mi->name);
797                                 free(mi);
798                                 continue;
799                         }
800                 }
801                 mi->next = milist;
802                 milist = mi;
803         }
804
805         return milist;
806 }
807
808 static void init_trib_mapinfo(trib_mapinfo *tmi)
809 {
810         if (!tmi)
811                 return;
812         tmi->shared_clean = 0;
813         tmi->shared_dirty = 0;
814         tmi->private_clean = 0;
815         tmi->private_dirty = 0;
816         tmi->swap = 0;
817         tmi->shared_clean_pss = 0;
818         tmi->shared_dirty_pss = 0;
819         tmi->rss = 0;
820         tmi->pss = 0;
821         tmi->size = 0;
822         tmi->graphic_3d = 0;
823         tmi->gem_rss = 0;
824         tmi->gem_pss = 0;
825         tmi->peak_rss = 0;
826         tmi->other_devices = 0;
827         tmi->gem_mmap = 0;
828         tmi->render_gem_mmap = 0;
829 }
830
831 unsigned int get_graphic_3d_meminfo(unsigned int tgid)
832 {
833         char command[256], buf[256];
834         char *tmp[2];
835         unsigned int size = 0;
836         int tid, ret;
837         FILE *p_gpu;
838
839         snprintf(command, sizeof(command), "%s %d", STR_3D_UNIFIED_PATH, tgid);
840         p_gpu = popen(command, "r");
841         if (p_gpu) {
842                 if (fgets(buf, 256, p_gpu)) {
843                         ret = sscanf(buf, "%d %ms %d %ms", &tid, &tmp[0], &size, &tmp[1]);
844                         if (ret == 4) {
845                                 free(tmp[0]);
846                                 free(tmp[1]);
847                         }
848                 }
849                 pclose(p_gpu);
850         }
851         return size;
852 }
853
854 static int
855 get_trib_mapinfo(unsigned int tgid, mapinfo *milist,
856                  geminfo *gilist, geminfo *gpu_glist,
857                  trib_mapinfo *result)
858
859 {
860         mapinfo *mi;
861         mapinfo *temp = NULL;
862         geminfo *gi;
863
864         if (!result)
865                 return -EINVAL;
866
867         init_trib_mapinfo(result);
868
869         if (use_gpu_mem_info)
870                 result->graphic_3d = get_graphic_3d_meminfo(tgid);
871
872         for (mi = milist; mi;) {
873                 if (!use_gpu_mem_info && strstr(mi->name, STR_SGX_PATH)) {
874                         result->graphic_3d += mi->pss;
875                 } else if (!use_gpu_mem_info && (strstr(mi->name, STR_3D_PATH1) ||
876                         strstr(mi->name, STR_3D_PATH2))) {
877                         result->graphic_3d += mi->size;
878                 } else if (mi->rss != 0 && mi->pss == 0
879                            && mi->shared_clean == 0
880                            && mi->shared_dirty == 0
881                            && mi->private_clean == 0
882                            && mi->private_dirty == 0
883                            && mi->swap == 0) {
884                         result->other_devices += mi->size;
885                 } else if (!strncmp(mi->name, STR_DRM_PATH1,
886                                 sizeof(STR_DRM_PATH1)) ||
887                                 !strncmp(mi->name, STR_DRM_PATH2,
888                                 sizeof(STR_DRM_PATH2))) {
889                         result->gem_mmap += mi->rss;
890                 } else if (!strncmp(mi->name, STR_DRM_RENDER_PATH,
891                                 sizeof(STR_DRM_RENDER_PATH))) {
892                         result->render_gem_mmap += mi->rss;
893                 } else {
894                         result->shared_clean += mi->shared_clean;
895                         result->shared_dirty += mi->shared_dirty;
896                         result->private_clean += mi->private_clean;
897                         result->private_dirty += mi->private_dirty;
898                         result->swap += mi->swap;
899                         result->rss += mi->rss;
900                         result->pss += mi->pss;
901                         result->size += mi->size;
902
903                         if (mi->shared_clean != 0)
904                                 result->shared_clean_pss += mi->pss;
905                         else if (mi->shared_dirty != 0)
906                                 result->shared_dirty_pss += mi->pss;
907                 }
908
909                 temp = mi;
910                 mi = mi->next;
911                 free(temp->perm);
912                 free(temp->name);
913                 free(temp);
914                 temp = NULL;
915         }
916
917         result->peak_rss = get_peak_rss(tgid);
918         if (result->peak_rss < result->rss)
919                 result->peak_rss = result->rss;
920         if (result->gem_mmap > 0)
921                 result->peak_rss -= result->gem_mmap;
922         if (result->render_gem_mmap > 0)
923                 result->peak_rss -= result->render_gem_mmap;
924
925         gi = find_geminfo(tgid, gilist);
926         if (gi != NULL) {
927                 result->gem_rss = gi->rss_size;
928                 result->gem_pss = gi->pss_size;
929         }
930
931         if (gpu_glist) {
932                 /*
933                  * drm render has gpu_gem_info, then use gpua gem pss
934                  * as graphic 3d memory
935                  */
936                 gi = find_geminfo(tgid, gpu_glist);
937                 if (gi != NULL)
938                         result->graphic_3d += gi->pss_size;
939         }
940
941         return 0;
942 }
943
944 static int get_cmdline(unsigned int pid, char *cmdline)
945 {
946         FILE *fp;
947         char buf[NAME_MAX] = {0, };
948         int ret = -1;
949
950         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
951         fp = fopen(buf, "r");
952         if (fp == 0) {
953                 if (errno == ENOENT)
954                         errno = 0;
955                 fprintf(stderr, "cannot file open %s\n", buf);
956                 return ret;
957         }
958         if ((ret = fscanf(fp, "%4095s", cmdline)) < 1) {
959                 fclose(fp);
960                 return ret;
961         }
962         fclose(fp);
963
964         return ret;
965 }
966
967 static int get_oomscoreadj(unsigned int pid, const char *oom_string)
968 {
969         FILE *fp;
970         char tmp[256];
971         int oomadj_val;
972
973         snprintf(tmp, sizeof(tmp), "/proc/%d/%s", pid, oom_string);
974         fp = fopen(tmp, "r");
975
976         if (fp == NULL) {
977                 oomadj_val = -50;
978                 return oomadj_val;
979         }
980         if (fgets(tmp, sizeof(tmp), fp) == NULL) {
981                 oomadj_val = -100;
982                 fclose(fp);
983                 return oomadj_val;
984         }
985
986         oomadj_val = atoi(tmp);
987
988         fclose(fp);
989         return oomadj_val;
990 }
991
992 static void get_rss(pid_t pid, unsigned long *result)
993 {
994         FILE *fp;
995         char proc_path[PATH_MAX];
996         unsigned long rss = 0;
997
998         *result = 0;
999
1000         snprintf(proc_path, sizeof(proc_path), "/proc/%d/statm", pid);
1001         fp = fopen(proc_path, "r");
1002         if (fp == NULL)
1003                 return;
1004
1005         if (fscanf(fp, "%*s %ld", &rss) < 1) {
1006                 fclose(fp);
1007                 return;
1008         }
1009
1010         fclose(fp);
1011
1012         /* convert page to Kb */
1013         *result = rss << 2;
1014         return;
1015 }
1016
1017 static const char* get_readable_oom_path(void)
1018 {
1019         FILE *fp = NULL;
1020         char tmp[256];
1021
1022         snprintf(tmp, sizeof(tmp), "/proc/1/%s", OOM_SCORE_ADJ_STR);
1023         fp = fopen(tmp, "r");
1024         if (fp) {
1025                 fclose(fp);
1026                 return OOM_SCORE_ADJ_STR;
1027         }
1028         snprintf(tmp, sizeof(tmp), "/proc/1/%s", OOM_SCORE_STR);
1029         fp = fopen(tmp, "r");
1030         if (fp) {
1031                 fclose(fp);
1032                 return OOM_SCORE_STR;
1033         }
1034         return NULL;
1035 }
1036
1037 static void show_rss(int output_type, char *output_path)
1038 {
1039         DIR *pDir = NULL;
1040         struct dirent *curdir;
1041         pid_t pid;
1042         char cmdline[PATH_MAX];
1043         FILE *output_file = NULL;
1044         int oom_score_adj;
1045         unsigned long rss;
1046         const char *oom_path = NULL;
1047
1048         oom_path = get_readable_oom_path();
1049         if (!oom_path) {
1050                 fprintf(stderr, "there is no readable oom path\n");
1051                 return;
1052         }
1053
1054         pDir = opendir("/proc");
1055         if (pDir == NULL) {
1056                 fprintf(stderr, "cannot read directory /proc.\n");
1057                 return;
1058         }
1059
1060         if (output_type == OUTPUT_FILE && output_path) {
1061                 output_file = fopen(output_path, "w+");
1062                 if (!output_file) {
1063                         fprintf(stderr, "cannot open output file(%s)\n",
1064                                 output_path);
1065                         closedir(pDir);
1066                         exit(1);
1067                 }
1068         } else
1069                 output_file = stdout;
1070
1071
1072         fprintf(output_file,
1073                         "     PID      RSS %14s    COMMAND\n", oom_path);
1074
1075         errno = 0;
1076         while ((curdir = readdir(pDir)) != NULL && !errno) {
1077                 pid = atoi(curdir->d_name);
1078                 if (pid < 1 || pid > pid_max || pid == getpid())
1079                         continue;
1080
1081                 if (get_cmdline(pid, cmdline) < 0)
1082                         continue;
1083                 get_rss(pid, &rss);
1084                 oom_score_adj = get_oomscoreadj(pid, oom_path);
1085
1086                 fprintf(output_file,
1087                                 "%8d %8lu       %8d    %s\n",
1088                                 pid,
1089                                 rss,
1090                                 oom_score_adj,
1091                                 cmdline);
1092
1093
1094         } /* end of while */
1095
1096         get_tmpfs_info(output_file);
1097         get_mem_info(output_file);
1098
1099         fclose(output_file);
1100         closedir(pDir);
1101
1102         return;
1103 }
1104
1105 static int show_map_all_new(int output_type, char *output_path)
1106 {
1107         DIR *pDir = NULL;
1108         struct dirent *curdir;
1109         unsigned int pid;
1110         mapinfo *milist;
1111         geminfo *glist;
1112         geminfo *gpu_glist;
1113         unsigned total_pss = 0;
1114         unsigned total_private = 0;
1115         unsigned total_private_code = 0;
1116         unsigned total_private_data = 0;
1117         unsigned total_shared_code = 0;
1118         unsigned total_shared_data = 0;
1119         unsigned total_shared_code_pss = 0;
1120         unsigned total_shared_data_pss = 0;
1121         unsigned total_swap = 0;
1122         unsigned total_rss = 0;
1123         unsigned total_graphic_3d = 0;
1124         unsigned total_gem_rss = 0;
1125         unsigned total_gem_pss = 0;
1126         unsigned total_peak_rss = 0;
1127         unsigned total_allocated_gem = 0;
1128         trib_mapinfo tmi;
1129         char cmdline[PATH_MAX];
1130         FILE *output_file = NULL;
1131         int oom_score_adj;
1132         const char *oom_path = NULL;
1133
1134         oom_path = get_readable_oom_path();
1135         if (!oom_path) {
1136                 fprintf(stderr, "there is no readable oom path\n");
1137                 return 0;
1138         }
1139
1140         use_gpu_mem_info = (!access(STR_3D_UNIFIED_PATH, F_OK | X_OK)) ? 1 : 0;
1141         errno = 0;
1142
1143         pDir = opendir("/proc");
1144         if (pDir == NULL) {
1145                 fprintf(stderr, "cannot read directory /proc.\n");
1146                 return 0;
1147         }
1148
1149         if (output_type == OUTPUT_FILE && output_path) {
1150                 output_file = fopen(output_path, "w+");
1151                 if (!output_file) {
1152                         fprintf(stderr, "cannot open output file(%s)\n",
1153                                 output_path);
1154                         closedir(pDir);
1155                         exit(1);
1156                 }
1157         } else
1158                 output_file = stdout;
1159
1160         glist = load_geminfo();
1161         gpu_glist = load_gpu_geminfo();
1162
1163         if (!sum) {
1164                 if (verbos)
1165                         fprintf(output_file,
1166                                         "     PID  S(CODE)  S(DATA)  P(CODE)  P(DATA) "
1167                                         "    PEAK      PSS       3D GEM(PSS) GEM(RSS) "
1168                                         "    SWAP %14s    COMMAND\n", oom_path);
1169                 else
1170                         fprintf(output_file,
1171                                         "     PID     CODE     DATA     PEAK      PSS "
1172                                         "      3D GEM(PSS)     SWAP    COMMAND\n");
1173         }
1174
1175         errno = 0;
1176         while ((curdir = readdir(pDir)) != NULL && !errno) {
1177                 pid = atoi(curdir->d_name);
1178                 if (pid < 1 || pid > pid_max || pid == getpid())
1179                         continue;
1180
1181                 if (get_cmdline(pid, cmdline) < 0)
1182                         continue;
1183
1184                 milist = load_maps(pid);
1185                 if (milist == 0)
1186                         continue;
1187
1188                 /* get classified map info */
1189                 get_trib_mapinfo(pid, milist, glist, gpu_glist, &tmi);
1190                 oom_score_adj = get_oomscoreadj(pid, oom_path);
1191
1192                 if (!sum) {
1193                         if (verbos)
1194                                 fprintf(output_file,
1195                                         "%8d %8d %8d %8d %8d "
1196                                         "%8d %8d %8d %8d %8d "
1197                                         "%8d       %8d    %s\n",
1198                                         pid,
1199                                         tmi.shared_clean,
1200                                         tmi.shared_dirty,
1201                                         tmi.private_clean,
1202                                         tmi.private_dirty,
1203                                         tmi.peak_rss,
1204                                         tmi.pss,
1205                                         tmi.graphic_3d,
1206                                         tmi.gem_pss,
1207                                         tmi.gem_rss,
1208                                         tmi.swap,
1209                                         oom_score_adj,
1210                                         cmdline);
1211                         else
1212                                 fprintf(output_file,
1213                                         "%8d %8d %8d %8d %8d "
1214                                         "%8d %8d %8d    %s\n",
1215                                         pid,
1216                                         tmi.shared_clean + tmi.private_clean,
1217                                         tmi.shared_dirty + tmi.private_dirty,
1218                                         tmi.peak_rss,
1219                                         tmi.pss,
1220                                         tmi.graphic_3d,
1221                                         tmi.gem_pss,
1222                                         tmi.swap,
1223                                         cmdline);
1224
1225                         if (tmi.other_devices != 0)
1226                                 fprintf(output_file,
1227                                         "%s(%d) %d KB may mapped by device(s).\n",
1228                                         cmdline, pid, tmi.other_devices);
1229                 }
1230
1231                 total_private += (tmi.private_clean + tmi.private_dirty);
1232                 total_pss += tmi.pss;
1233                 total_rss += tmi.rss;
1234                 total_graphic_3d += tmi.graphic_3d;
1235                 total_gem_rss += tmi.gem_rss;
1236                 total_gem_pss += tmi.gem_pss;
1237                 total_private_code += tmi.private_clean;
1238                 total_private_data += tmi.private_dirty;
1239                 total_swap += tmi.swap;
1240                 total_shared_code += tmi.shared_clean;
1241                 total_shared_data += tmi.shared_dirty;
1242                 total_peak_rss += tmi.peak_rss;
1243
1244                 total_shared_code_pss += tmi.shared_clean_pss;
1245                 total_shared_data_pss += tmi.shared_dirty_pss;
1246
1247         } /* end of while */
1248
1249         total_allocated_gem = KB(total_gem_memory());
1250         fprintf(output_file,
1251                         "==============================================="
1252                         "===============================================\n");
1253         if (verbos) {
1254                 fprintf(output_file,
1255                                 "TOTAL:    S(CODE)  S(DATA)  P(CODE)  P(DATA) "
1256                                 "    PEAK      PSS       3D GEM(PSS) GEM(RSS) "
1257                                 "GEM(ALLOC)     SWAP  TOTAL(KB)\n");
1258                 fprintf(output_file,
1259                         "         %8d %8d %8d %8d "
1260                         "%8d %8d %8d %8d %8d "
1261                         "%10d %8d %10d\n",
1262                         total_shared_code, total_shared_data,
1263                         total_private_code, total_private_data,
1264                         total_peak_rss, total_pss, total_graphic_3d,
1265                         total_gem_pss, total_gem_rss,
1266                         total_allocated_gem, total_swap,
1267                         total_pss + total_graphic_3d +
1268                         total_allocated_gem);
1269         } else {
1270                 fprintf(output_file,
1271                         "TOTAL:       CODE     DATA     PEAK      PSS "
1272                         "      3D GEM(PSS) GEM(ALLOC)     SWAP  TOTAL(KB)\n");
1273                 fprintf(output_file,
1274                         "         %8d %8d %8d %8d "
1275                         "%8d %8d %10d %8d %10d\n",
1276                         total_shared_code + total_private_code,
1277                         total_shared_data + total_private_data,
1278                         total_peak_rss, total_pss,
1279                         total_graphic_3d, total_gem_pss,
1280                         total_allocated_gem, total_swap,
1281                         total_pss + total_graphic_3d +
1282                         total_allocated_gem);
1283
1284         }
1285
1286         if (verbos)
1287                 fprintf(output_file,
1288                         "* S(CODE): shared clean memory, it includes"
1289                         " duplicated memory\n"
1290                         "* S(DATA): shared dirty memory, it includes"
1291                         " duplicated memory\n"
1292                         "* P(CODE): private clean memory\n"
1293                         "* P(DATA): private dirty memory\n"
1294                         "* PEAK: peak memory usage of S(CODE) + S(DATA)"
1295                         " + P(CODE) + P(DATA)\n"
1296                         "* PSS: Proportional Set Size\n"
1297                         "* 3D: memory allocated by GPU driver\n"
1298                         "* GEM(PSS): GEM memory divided by # of sharers\n"
1299                         "* GEM(RSS): GEM memory including duplicated memory\n"
1300                         "* GEM(ALLOC): sum of unique gem memory in the system\n"
1301                         "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1302         else
1303                 fprintf(output_file,
1304                         "* CODE: shared and private clean memory\n"
1305                         "* DATA: shared and private dirty memory\n"
1306                         "* PEAK: peak memory usage of CODE + DATA\n"
1307                         "* PSS: Proportional Set Size\n"
1308                         "* 3D: memory allocated by GPU driver\n"
1309                         "* GEM(PSS): GEM memory divided by # of sharers\n"
1310                         "* GEM(ALLOC): sum of unique GEM memory in the system\n"
1311                         "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1312
1313         get_tmpfs_info(output_file);
1314         get_memcg_info(output_file);
1315         get_mem_info(output_file);
1316
1317         fclose(output_file);
1318         if (glist)
1319                 free(glist);
1320         if (gpu_glist)
1321                 free(gpu_glist);
1322         closedir(pDir);
1323         return 1;
1324 }
1325
1326 static int show_map_new(int pid)
1327 {
1328         mapinfo *milist;
1329         mapinfo *mi;
1330         mapinfo *temp = NULL;
1331         unsigned shared_dirty = 0;
1332         unsigned shared_clean = 0;
1333         unsigned private_dirty = 0;
1334         unsigned private_clean = 0;
1335         unsigned pss = 0;
1336         unsigned long start = 0;
1337         unsigned long end = 0;
1338         unsigned private_clean_total = 0;
1339         unsigned private_dirty_total = 0;
1340         unsigned shared_clean_total = 0;
1341         unsigned shared_dirty_total = 0;
1342         int duplication = 0;
1343
1344         milist = load_maps(pid);
1345
1346         if (milist == 0) {
1347                 fprintf(stderr, "cannot get /proc/smaps for pid %d\n", pid);
1348                 return 1;
1349         }
1350
1351         if (!sum) {
1352                 if (sizeof(unsigned long) == 4) {
1353                         /* for 32-bit address */
1354                         printf(" S(CODE)  S(DATA)  P(CODE)  P(DATA)      PSS "
1355                                 "ADDR(start-end)   "
1356                                 "OBJECT NAME\n");
1357                         printf("-------- -------- -------- -------- -------- "
1358                                 "----------------- "
1359                                 "------------------------------\n");
1360                 } else {
1361                         /* for 64-bit address */
1362                         printf(" S(CODE)  S(DATA)  P(CODE)  P(DATA)      PSS "
1363                                 "ADDR(start-end)                   "
1364                                 "OBJECT NAME\n");
1365                         printf("-------- -------- -------- -------- -------- "
1366                                 "--------------------------------- "
1367                                 "------------------------------\n");
1368                 }
1369         } else {
1370                 printf(" S(CODE)  S(DATA)  P(CODE)  P(DATA)      PSS\n");
1371                 printf("-------- -------- -------- -------- --------\n");
1372         }
1373         for (mi = milist; mi;) {
1374                 shared_clean += mi->shared_clean;
1375                 shared_dirty += mi->shared_dirty;
1376                 private_clean += mi->private_clean;
1377                 private_dirty += mi->private_dirty;
1378                 pss += mi->pss;
1379
1380                 shared_clean_total += mi->shared_clean;
1381                 shared_dirty_total += mi->shared_dirty;
1382                 private_clean_total += mi->private_clean;
1383                 private_dirty_total += mi->private_dirty;
1384
1385                 if (!duplication)
1386                         start = mi->start;
1387
1388                 if ((mi->next && !strcmp(mi->next->name, mi->name)) &&
1389                     (mi->next->start == mi->end)) {
1390                         duplication = 1;
1391
1392                         temp = mi;
1393                         mi = mi->next;
1394                         free(temp->perm);
1395                         free(temp->name);
1396                         free(temp);
1397                         temp = NULL;
1398                         continue;
1399                 }
1400                 end = mi->end;
1401                 duplication = 0;
1402
1403                 if (!sum) {
1404                         if (sizeof(unsigned long) == 4) {
1405                                 /* for 32-bit address */
1406                                 printf("%8d %8d %8d %8d %8d %08lx-%08lx %s\n",
1407                                         shared_clean, shared_dirty,
1408                                         private_clean, private_dirty, mi->pss,
1409                                         start, end, mi->name);
1410                         } else {
1411                                 /* for 64-bit address */
1412                                 printf("%8d %8d %8d %8d %8d %016lx-%016lx %s\n",
1413                                         shared_clean, shared_dirty,
1414                                         private_clean, private_dirty, mi->pss,
1415                                         start, end, mi->name);
1416                         }
1417                 }
1418                 shared_clean = 0;
1419                 shared_dirty = 0;
1420                 private_clean = 0;
1421                 private_dirty = 0;
1422
1423                 temp = mi;
1424                 mi = mi->next;
1425                 free(temp->perm);
1426                 free(temp->name);
1427                 free(temp);
1428                 temp = NULL;
1429         }
1430         if (sum) {
1431                 printf("%8d %8d %8d %8d %8d\n",
1432                        shared_clean_total,
1433                        shared_dirty_total,
1434                        private_clean_total,
1435                        private_dirty_total,
1436                        pss);
1437         }
1438
1439         return 1;
1440 }
1441
1442 int main(int argc, char *argv[])
1443 {
1444         int usage = 1;
1445         sum = 0;
1446
1447         pid_max = get_pid_max();
1448
1449         if (argc > 1) {
1450                 if (!strncmp(argv[1], "-r", strlen("-r")+1)) {
1451                         if (argc >= 3)
1452                                 show_rss(OUTPUT_FILE, argv[2]);
1453                         else
1454                                 show_rss(OUTPUT_UART, NULL);
1455                         usage = 0;
1456                 } else if (!strncmp(argv[1], "-s", strlen("-s")+1)) {
1457                         sum = 1;
1458                         if (argc == 3 && atoi(argv[2]) > 0) {
1459                                 show_map_new(atoi(argv[2]));
1460                                 usage = 0;
1461                         }
1462                 } else if (!strncmp(argv[1], "-a", strlen("-a")+1)) {
1463                         verbos = 0;
1464                         show_map_all_new(OUTPUT_UART, NULL);
1465                         usage = 0;
1466                 } else if (!strncmp(argv[1], "-v", strlen("-v")+1)) {
1467                         verbos = 1;
1468                         show_map_all_new(OUTPUT_UART, NULL);
1469                         usage = 0;
1470                 } else if (!strncmp(argv[1], "-f", strlen("-f")+1)) {
1471                         if (argc >= 3) {
1472                                 verbos = 1;
1473                                 show_map_all_new(OUTPUT_FILE, argv[2]);
1474                                 usage = 0;
1475                         }
1476                 } else if (argc == 2 && atoi(argv[1]) > 0) {
1477                         show_map_new(atoi(argv[1]));
1478                         usage = 0;
1479                 }
1480         }
1481         if (usage) {
1482                 fprintf(stderr,
1483                         "memps [-a] | [-v] | [-r] | [-s] <pid> | <pid> | [-f] <output file full path>\n"
1484                         "        -s = sum (show only sum of each)\n"
1485                         "        -f = all (show all processes via output file)\n"
1486                         "        -a = all (show all processes)\n"
1487                         "        -r = all (show rss of all processes)\n"
1488                         "        -v = verbose (show all processes in detail)\n");
1489         }
1490
1491         return 0;
1492 }