1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/types.h>
27 #include <linux/limits.h>
33 #include <sys/utsname.h>
35 #define STR_SGX_PATH "/dev/pvrsrvkm"
36 #define STR_3D_PATH1 "/dev/mali"
37 #define STR_3D_PATH2 "/dev/kgsl-3d0"
38 #define STR_DRM_PATH1 "/drm mm object (deleted)"
39 #define STR_DRM_PATH2 "/dev/dri/card0"
40 #define MEMCG_PATH "/sys/fs/cgroup/memory"
41 #define ZRAM_USED_PATH "/sys/block/zram0/mem_used_total"
43 #define BUF_MAX (BUFSIZ) /* most optimal for libc::stdio */
44 #define BUF_INC_SIZE (512 * 1024) /* maximal SMAPS I saw 2 MB */
45 #define KB(bytes) ((bytes)/1024)
47 #define BYTE_TO_KBYTE(b) ((b) >> 10)
48 #define BYTE_TO_MBYTE(b) ((b) >> 20)
49 #define BYTE_TO_GBYTE(b) ((b) >> 30)
51 #define KBYTE_TO_BYTE(k) ((k) << 10)
52 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
53 #define KBYTE_TO_GBYTE(k) ((k) >> 20)
55 #define GBYTE_TO_BYTE(g) ((g) << 30)
56 #define GBYTE_TO_KBYTE(g) ((g) << 20)
57 #define GBYTE_TO_MBYTE(g) ((g) << 10)
59 typedef struct geminfo geminfo;
60 typedef struct mapinfo mapinfo;
61 typedef struct trib_mapinfo trib_mapinfo;
77 unsigned shared_clean;
78 unsigned shared_dirty;
79 unsigned private_clean;
80 unsigned private_dirty;
85 /* classify normal, graphic and other devices memory */
87 unsigned shared_clean;
88 unsigned shared_dirty;
89 unsigned private_clean;
90 unsigned private_dirty;
91 unsigned shared_clean_pss;
92 unsigned shared_dirty_pss;
101 unsigned other_devices;
116 /* reads file contents into memory */
117 static char* cread(const char* path)
119 /* once allocated area for reads */
120 static char* text = NULL;
121 static size_t size = 0;
126 int fd = open(path, O_RDONLY);
133 /* ensure we have enough space */
135 ptr = (char*)realloc(text, size + BUF_INC_SIZE);
144 size += BUF_INC_SIZE;
146 ret = read(fd, ptr, cap);
149 } else if (ret > 0) {
156 return (ret < 0 ? NULL : text);
159 /* like fgets/gets but adjusting contents pointer */
160 static inline char* cgets(char** contents)
162 if (contents && *contents && **contents) {
163 char* bos = *contents; /* begin of string */
164 char* eos = strchr(bos, '\n'); /* end of string */
179 static unsigned get_peak_rss(unsigned int pid)
181 static const char field[] = "VmHWM:";
186 snprintf(tmp, sizeof(tmp), "/proc/%d/status", pid);
189 fprintf(stderr, "cannot open %s\n", tmp);
193 value = strstr(line, field);
195 value += sizeof(field);
196 return strtoul(value, NULL, 10);
201 #define NUM_GEM_FIELD 6
203 static geminfo *read_geminfo(FILE *fp)
207 unsigned int pid, tgid, handle, refcount, hcount;
210 if (fgets(line, BUF_MAX, fp) != NULL) {
211 if (sscanf(line, "%d %d %d %d %d 0x%x",
212 &pid, &tgid, &handle, &refcount,
213 &hcount, &gem_size) != NUM_GEM_FIELD)
218 tgeminfo = malloc(sizeof(geminfo));
219 if (tgeminfo == NULL)
221 tgeminfo->tgid = tgid;
222 tgeminfo->hcount = hcount;
223 tgeminfo->rss_size = KB(gem_size);
224 tgeminfo->pss_size = KB(gem_size/tgeminfo->hcount);
232 static geminfo *find_geminfo(unsigned int tgid, geminfo *gilist)
235 for (gi = gilist; gi; ) {
236 if (gi->tgid == tgid)
244 static geminfo *load_geminfo(void)
247 geminfo *gilist = NULL;
248 geminfo *exist_ginfo = NULL;
253 drm_fp = fopen("/sys/kernel/debug/dri/0/gem_info", "r");
255 if (drm_fp == NULL) {
257 "cannot open /sys/kernel/debug/dri/0/gem_info\n");
261 if (fgets(line, BUF_MAX, drm_fp) == NULL) {
265 /* we should count a number of whitespace separated fields */
266 int in_field = (line[0] && !isblank(line[0]));
267 unsigned int size = (unsigned)in_field;
268 const char* ptr = &line[1];
270 /* sscanf() was used in original code, so number of fields */
271 /* in string is expected to be at least NUM_GEM_FIELD */
272 while (*ptr && size < NUM_GEM_FIELD) {
273 if (isblank(*ptr++)) {
280 /* next field started */
287 if (size != NUM_GEM_FIELD) {
293 while ((ginfo = read_geminfo(drm_fp)) != NULL) {
294 if (gilist && ginfo->tgid == gilist->tgid) {
295 gilist->pss_size += ginfo->pss_size;
296 gilist->rss_size += ginfo->rss_size;
299 } else if(gilist && ((exist_ginfo = find_geminfo(ginfo->tgid, gilist)) != NULL)) {
300 exist_ginfo->pss_size += ginfo->pss_size;
301 exist_ginfo->rss_size += ginfo->rss_size;
305 ginfo->next = gilist;
315 /* b6e82000-b6e83000 rw-p 00020000 b3:19 714 /usr/lib/ld-2.20-2014.11.so : TM1
316 * 7f9389d000-7f9389e000 rw-p 0001f000 b3:12 618 /usr/lib64/ld-2.20-2014.11.so : TM2
317 * 7fae2e4b2000-7fae2e4b3000 r--p 00021000 fe:01 603 /usr/lib64/ld-2.20-2014.11.so : x86-64 Emulator
318 * 01234567890123456789012345678901234567890123456789012345678901234567890123456789
321 mapinfo *read_mapinfo(char** smaps)
328 if ((line = cgets(smaps)) == 0)
335 mi = malloc(sizeof(mapinfo));
339 n = sscanf(line, "%lx-%lx %ms %*s %*s %*s %m[^\n]",
340 &mi->start, &mi->end, &mi->perm, &mi->name);
342 if (n == 3 && !mi->name)
343 mi->name = strndup("[anon]", strlen("[anon]"));
345 fprintf(stderr,"Fail to parse smaps\n");
350 while ((line = cgets(smaps))) {
351 if (sscanf(line, "Size: %d kB", &mi->size) == 1) {}
352 else if (sscanf(line, "Rss: %d kB", &mi->rss) == 1) {}
353 else if (sscanf(line, "Pss: %d kB", &mi->pss) == 1) {}
354 else if (sscanf(line, "Shared_Clean: %d kB", &mi->shared_clean) == 1) {}
355 else if (sscanf(line, "Shared_Dirty: %d kB", &mi->shared_dirty) == 1) {}
356 else if (sscanf(line, "Private_Clean: %d kB", &mi->private_clean) == 1) {}
357 else if (sscanf(line, "Private_Dirty: %d kB", &mi->private_dirty) == 1) {}
358 else if (sscanf(line, "Swap: %d kB", &mi->swap) == 1) {}
360 /* Drain lines until it meets next VMA address */
362 if ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f'))
370 static unsigned total_gem_memory(void)
373 unsigned total_gem_mem = 0;
374 unsigned name, size, handles, refcount;
377 gem_fp = fopen("/proc/dri/0/gem_names", "r");
380 "cannot open /proc/dir/0/gem_names\n");
384 if (fgets(line, BUF_MAX, gem_fp) == NULL) {
389 while (fgets(line, BUF_MAX, gem_fp) != NULL)
390 if (sscanf(line, "%d %d %d %d\n",
391 &name, &size, &handles, &refcount) == 4)
392 total_gem_mem += size;
395 return total_gem_mem;
399 int fread_uint(const char *path, u_int32_t *number)
404 f = fopen(path, "r");
407 fprintf(stderr,"Fail to open %s file.\n", path);
411 ret = fscanf(f, "%u", number);
413 fprintf(stderr,"Fail to read file\n");
422 #define MAX_PATH_LENGTH 512
423 static int cgroup_read_node(const char *cgroup_name,
424 const char *file_name, unsigned int *value)
426 char buf[MAX_PATH_LENGTH];
428 snprintf(buf, sizeof(buf), "%s%s", cgroup_name, file_name);
429 ret = fread_uint(buf, value);
434 * @desc Provides usage in bytes for provided memory cgroup. Works
435 * with/without swap accounting.
437 * @param memcg_path[in] Full path to memory cgroup
438 * @param swap[in] Boolean value for deciding if account usage with swap
439 * @return current cgroup usage in bytes or 0 on error
441 static unsigned int get_memcg_usage(const char *memcg_path, bool swap)
447 ret = cgroup_read_node(memcg_path,
448 "/memory.memsw.usage_in_bytes", &usage);
450 ret = cgroup_read_node(memcg_path, "/memory.usage_in_bytes",
460 static void get_memcg_info(FILE *output_fp)
464 struct dirent *entry;
465 struct stat path_stat;
467 unsigned long usage, usage_with_swap;
469 fprintf(output_fp,"====================================================================\n");
470 fprintf(output_fp,"MEMORY CGROUPS USAGE INFO\n");
472 pdir = opendir(MEMCG_PATH);
474 fprintf(stderr,"cannot read directory %s", MEMCG_PATH);
479 while ((entry = readdir(pdir)) != NULL && !errno) {
480 snprintf(buf, sizeof(buf), "%s/%s", MEMCG_PATH, entry->d_name);
481 /* If can't stat then ignore */
482 if (stat(buf, &path_stat) != 0)
485 /* If it's not directory or it's parent path then ignore */
486 if (!(S_ISDIR(path_stat.st_mode) &&
487 strncmp(entry->d_name, "..", 3)))
490 usage = get_memcg_usage(buf, false);
491 usage_with_swap = get_memcg_usage(buf, true);
492 /* It is posible by rounding errors to get negative value */
493 usage_swap = usage_with_swap - usage;
497 /* Case of root cgroup in hierarchy */
498 if (!strncmp(entry->d_name, ".", 2))
499 fprintf(output_fp, "%13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB) \n\n",
500 MEMCG_PATH, BYTE_TO_MBYTE(usage),
501 BYTE_TO_KBYTE(usage),
502 BYTE_TO_MBYTE(usage_with_swap),
503 BYTE_TO_KBYTE(usage_with_swap),
504 BYTE_TO_MBYTE(usage_swap),
505 BYTE_TO_KBYTE(usage_swap));
507 fprintf(output_fp, "memcg: %13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB)\n",
508 entry->d_name, BYTE_TO_MBYTE(usage),
509 BYTE_TO_KBYTE(usage),
510 BYTE_TO_MBYTE(usage_with_swap),
511 BYTE_TO_KBYTE(usage_with_swap),
512 BYTE_TO_MBYTE(usage_swap),
513 BYTE_TO_KBYTE(usage_swap));
520 static void get_mem_info(FILE *output_fp)
525 unsigned int free = 0, cached = 0;
526 unsigned int total_mem = 0, available = 0, used;
527 unsigned int swap_total = 0, swap_free = 0, zram_used, swap_used;
528 unsigned int used_ratio;
530 if (output_fp == NULL)
533 fp = fopen("/proc/meminfo", "r");
536 fprintf(stderr, "%s open failed, %p", buf, fp);
540 while (fgets(buf, PATH_MAX, fp) != NULL) {
541 if ((idx = strstr(buf, "MemTotal:"))) {
542 idx += strlen("Memtotal:");
543 while (*idx < '0' || *idx > '9')
545 total_mem = atoi(idx);
546 } else if ((idx = strstr(buf, "MemFree:"))) {
547 idx += strlen("MemFree:");
548 while (*idx < '0' || *idx > '9')
551 } else if ((idx = strstr(buf, "MemAvailable:"))) {
552 idx += strlen("MemAvailable:");
553 while (*idx < '0' || *idx > '9')
555 available = atoi(idx);
556 } else if((idx = strstr(buf, "Cached:")) && !strstr(buf, "Swap")) {
557 idx += strlen("Cached:");
558 while (*idx < '0' || *idx > '9')
561 } else if((idx = strstr(buf, "SwapTotal:"))) {
562 idx += strlen("SwapTotal:");
563 while (*idx < '0' || *idx > '9')
565 swap_total = atoi(idx);
566 } else if((idx = strstr(buf, "SwapFree:"))) {
567 idx += strlen("SwapFree");
568 while (*idx < '0' || *idx > '9')
570 swap_free = atoi(idx);
575 if (total_mem == 0) {
576 fprintf(stderr, "cannot get total memory size\n");
582 available = free + cached;
583 used = total_mem - available;
584 used_ratio = used * 100 / total_mem;
585 swap_used = swap_total - swap_free;
587 if (fread_uint(ZRAM_USED_PATH, &zram_used) != 0)
591 "====================================================================\n");
594 fprintf(output_fp, "Total RAM size: \t%15d MB( %6d kB)\n",
595 total_mem >> 10, total_mem);
597 fprintf(output_fp, "Used (Mem+Reclaimable): %15d MB( %6d kB)\n",
598 (total_mem - free) >> 10, total_mem - free);
600 fprintf(output_fp, "Used (Mem+Swap): \t%15d MB( %6d kB)\n",
603 fprintf(output_fp, "Used (Mem): \t\t%15d MB( %6d kB)\n",
606 fprintf(output_fp, "Used (Swap): \t\t%15d MB( %6d kB)\n",
607 swap_used >> 10, swap_used);
609 fprintf(output_fp, "Used (Zram block device): %13d MB( %6d kB)\n",
610 BYTE_TO_MBYTE(zram_used), BYTE_TO_KBYTE(zram_used));
612 fprintf(output_fp, "Used Ratio: \t\t%15d %%\n", used_ratio);
614 fprintf(output_fp, "Mem Free:\t\t%15d MB( %6d kB)\n",
617 fprintf(output_fp, "Available (Free+Reclaimable):%10d MB( %6d kB)\n",
623 static int get_tmpfs_info(FILE *output_fp)
627 char *tmpfs_mp; /* tmpfs mount point */
628 struct statfs tmpfs_info;
630 if (output_fp == NULL)
633 fp = fopen("/etc/mtab", "r");
638 "====================================================================\n");
639 fprintf(output_fp, "TMPFS INFO\n");
641 while (fgets(line, BUF_MAX, fp) != NULL) {
642 if (sscanf(line, "tmpfs %ms tmpfs", &tmpfs_mp) == 1) {
643 statfs(tmpfs_mp, &tmpfs_info);
645 "tmpfs %16s Total %8ld KB, Used %8ld, Avail %8ld\n",
647 /* 1 block is 4 KB */
648 tmpfs_info.f_blocks * 4,
649 (tmpfs_info.f_blocks - tmpfs_info.f_bfree) * 4,
650 tmpfs_info.f_bfree * 4);
658 mapinfo *load_maps(int pid)
665 snprintf(tmp, sizeof(tmp), "/proc/%d/smaps", pid);
670 while ((mi = read_mapinfo(&smaps)) != 0) {
672 if ((!strcmp(mi->name, milist->name)
673 && (mi->name[0] != '['))) {
674 milist->size += mi->size;
675 milist->swap += mi->swap;
676 milist->rss += mi->rss;
677 milist->pss += mi->pss;
678 milist->shared_clean += mi->shared_clean;
679 milist->shared_dirty += mi->shared_dirty;
680 milist->private_clean += mi->private_clean;
681 milist->private_dirty += mi->private_dirty;
683 milist->end = mi->end;
684 strncpy(milist->perm, mi->perm, 4);
698 static void init_trib_mapinfo(trib_mapinfo *tmi)
702 tmi->shared_clean = 0;
703 tmi->shared_dirty = 0;
704 tmi->private_clean = 0;
705 tmi->private_dirty = 0;
707 tmi->shared_clean_pss = 0;
708 tmi->shared_dirty_pss = 0;
716 tmi->other_devices = 0;
721 get_trib_mapinfo(unsigned int tgid, mapinfo *milist,
722 geminfo *gilist, trib_mapinfo *result)
726 mapinfo *temp = NULL;
732 init_trib_mapinfo(result);
733 for (mi = milist; mi;) {
734 if (strstr(mi->name, STR_SGX_PATH)) {
735 result->graphic_3d += mi->pss;
736 } else if (strstr(mi->name, STR_3D_PATH1) ||
737 strstr(mi->name, STR_3D_PATH2)) {
738 result->graphic_3d += mi->size;
739 } else if (mi->rss != 0 && mi->pss == 0
740 && mi->shared_clean == 0
741 && mi->shared_dirty == 0
742 && mi->private_clean == 0
743 && mi->private_dirty == 0
745 result->other_devices += mi->size;
746 } else if (!strncmp(mi->name, STR_DRM_PATH1,
747 sizeof(STR_DRM_PATH1)) ||
748 !strncmp(mi->name, STR_DRM_PATH2,
749 sizeof(STR_DRM_PATH2))) {
750 result->gem_mmap += mi->rss;
752 result->shared_clean += mi->shared_clean;
753 result->shared_dirty += mi->shared_dirty;
754 result->private_clean += mi->private_clean;
755 result->private_dirty += mi->private_dirty;
756 result->swap += mi->swap;
757 result->rss += mi->rss;
758 result->pss += mi->pss;
759 result->size += mi->size;
761 if(mi->shared_clean != 0)
762 result->shared_clean_pss += mi->pss;
763 else if (mi->shared_dirty != 0)
764 result->shared_dirty_pss += mi->pss;
775 result->peak_rss = get_peak_rss(tgid);
776 if (result->peak_rss < result->rss)
777 result->peak_rss = result->rss;
778 if (result->gem_mmap > 0)
779 result->peak_rss -= result->gem_mmap;
781 gi = find_geminfo(tgid, gilist);
783 result->gem_rss = gi->rss_size;
784 result->gem_pss = gi->pss_size;
790 static int get_cmdline(unsigned int pid, char *cmdline)
793 char buf[NAME_MAX] = {0, };
796 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
797 fp = fopen(buf, "r");
799 fprintf(stderr, "cannot file open %s\n", buf);
802 if ((ret = fscanf(fp, "%4095s", cmdline)) < 1) {
811 static int get_oomscoreadj(unsigned int pid)
817 snprintf(tmp, sizeof(tmp), "/proc/%d/oom_score_adj", pid);
818 fp = fopen(tmp, "r");
824 if (fgets(tmp, sizeof(tmp), fp) == NULL) {
830 oomadj_val = atoi(tmp);
836 static void get_rss(pid_t pid, unsigned long *result)
839 char proc_path[PATH_MAX];
840 unsigned long rss = 0;
844 snprintf(proc_path, sizeof(proc_path), "/proc/%d/statm", pid);
845 fp = fopen(proc_path, "r");
849 if (fscanf(fp, "%*s %ld", &rss) < 1) {
856 /* convert page to Kb */
861 static void show_rss(int output_type, char *output_path)
864 struct dirent *curdir;
866 char cmdline[PATH_MAX];
867 FILE *output_file = NULL;
871 pDir = opendir("/proc");
873 fprintf(stderr, "cannot read directory /proc.\n");
877 if (output_type == OUTPUT_FILE && output_path) {
878 output_file = fopen(output_path, "w+");
880 fprintf(stderr, "cannot open output file(%s)\n",
886 output_file = stdout;
890 " PID RSS OOM_SCORE COMMAND\n");
893 while ((curdir = readdir(pDir)) != NULL && !errno) {
894 pid = atoi(curdir->d_name);
895 if (pid < 1 || pid > 32768 || pid == getpid())
898 if (get_cmdline(pid, cmdline) < 0)
901 oom_score_adj = get_oomscoreadj(pid);
913 get_tmpfs_info(output_file);
914 get_mem_info(output_file);
922 static int show_map_all_new(int output_type, char *output_path)
925 struct dirent *curdir;
929 unsigned total_pss = 0;
930 unsigned total_private = 0;
931 unsigned total_private_code = 0;
932 unsigned total_private_data = 0;
933 unsigned total_shared_code = 0;
934 unsigned total_shared_data = 0;
935 unsigned total_shared_code_pss = 0;
936 unsigned total_shared_data_pss = 0;
937 unsigned total_swap = 0;
938 unsigned total_rss = 0;
939 unsigned total_graphic_3d = 0;
940 unsigned total_gem_rss = 0;
941 unsigned total_gem_pss = 0;
942 unsigned total_peak_rss = 0;
943 unsigned total_allocated_gem = 0;
945 char cmdline[PATH_MAX];
946 FILE *output_file = NULL;
949 pDir = opendir("/proc");
951 fprintf(stderr, "cannot read directory /proc.\n");
955 if (output_type == OUTPUT_FILE && output_path) {
956 output_file = fopen(output_path, "w+");
958 fprintf(stderr, "cannot open output file(%s)\n",
964 output_file = stdout;
966 glist = load_geminfo();
971 " PID S(CODE) S(DATA) P(CODE) P(DATA)"
973 " GEM(PSS) GEM(RSS) SWAP"
974 " OOM_SCORE_ADJ COMMAND\n");
977 " PID CODE DATA PEAK PSS"
978 " 3D GEM(PSS) SWAP COMMAND\n");
982 while ((curdir = readdir(pDir)) != NULL && !errno) {
983 pid = atoi(curdir->d_name);
984 if (pid < 1 || pid > 32768 || pid == getpid())
987 if (get_cmdline(pid, cmdline) < 0)
990 milist = load_maps(pid);
994 /* get classified map info */
995 get_trib_mapinfo(pid, milist, glist, &tmi);
996 oom_score_adj = get_oomscoreadj(pid);
1000 fprintf(output_file,
1001 "%8d %8d %8d %8d %8d %8d %8d %8d %8d %8d %8d"
1004 tmi.shared_clean, tmi.shared_dirty,
1005 tmi.private_clean, tmi.private_dirty,
1006 tmi.peak_rss, tmi.pss, tmi.graphic_3d,
1007 tmi.gem_pss, tmi.gem_rss, tmi.swap,
1008 oom_score_adj, cmdline);
1010 fprintf(output_file,
1011 "%8d %8d %8d %8d %8d %8d %8d %8d %s\n",
1015 tmi.shared_dirty + tmi.private_dirty,
1022 if (tmi.other_devices != 0)
1023 fprintf(output_file,
1024 "%s(%d) %d KB may mapped by device(s).\n",
1025 cmdline, pid, tmi.other_devices);
1028 total_private += (tmi.private_clean + tmi.private_dirty);
1029 total_pss += tmi.pss;
1030 total_rss += tmi.rss;
1031 total_graphic_3d += tmi.graphic_3d;
1032 total_gem_rss += tmi.gem_rss;
1033 total_gem_pss += tmi.gem_pss;
1034 total_private_code += tmi.private_clean;
1035 total_private_data += tmi.private_dirty;
1036 total_swap += tmi.swap;
1037 total_shared_code += tmi.shared_clean;
1038 total_shared_data += tmi.shared_dirty;
1039 total_peak_rss += tmi.peak_rss;
1041 total_shared_code_pss += tmi.shared_clean_pss;
1042 total_shared_data_pss += tmi.shared_dirty_pss;
1044 } /* end of while */
1046 total_allocated_gem = KB(total_gem_memory());
1047 fprintf(output_file,
1048 "==============================================="
1049 "===============================================\n");
1051 fprintf(output_file,
1052 "TOTAL: S(CODE) S(DATA) P(CODE) P(DATA)"
1054 "GEM(PSS) GEM(RSS) GEM(ALLOC) SWAP TOTAL(KB)\n");
1055 fprintf(output_file,
1056 " %8d %8d %8d %8d %8d %8d %8d"
1057 " %8d %8d %8d %8d %8d\n",
1058 total_shared_code, total_shared_data,
1059 total_private_code, total_private_data,
1060 total_peak_rss, total_pss, total_graphic_3d,
1061 total_gem_pss, total_gem_rss,
1062 total_allocated_gem, total_swap,
1063 total_pss + total_graphic_3d +
1064 total_allocated_gem);
1066 fprintf(output_file,
1067 "TOTAL: CODE DATA PEAK PSS "
1068 "3D GEM(PSS) GEM(ALLOC) TOTAL(KB)\n");
1069 fprintf(output_file, " %8d %8d %8d %8d %8d %8d %7d %8d %8d\n",
1070 total_shared_code + total_private_code,
1071 total_shared_data + total_private_data,
1072 total_peak_rss, total_pss,
1073 total_graphic_3d, total_gem_pss,
1074 total_allocated_gem, total_swap,
1075 total_pss + total_graphic_3d +
1076 total_allocated_gem);
1081 fprintf(output_file,
1082 "* S(CODE): shared clean memory, it includes"
1083 " duplicated memory\n"
1084 "* S(DATA): shared dirty memory, it includes"
1085 " duplicated memory\n"
1086 "* P(CODE): private clean memory\n"
1087 "* P(DATA): private dirty memory\n"
1088 "* PEAK: peak memory usage of S(CODE) + S(DATA)"
1089 " + P(CODE) + P(DATA)\n"
1090 "* PSS: Proportional Set Size\n"
1091 "* 3D: memory allocated by GPU driver\n"
1092 "* GEM(PSS): GEM memory divided by # of sharers\n"
1093 "* GEM(RSS): GEM memory including duplicated memory\n"
1094 "* GEM(ALLOC): sum of unique gem memory in the system\n"
1095 "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1097 fprintf(output_file,
1098 "* CODE: shared and private clean memory\n"
1099 "* DATA: shared and private dirty memory\n"
1100 "* PEAK: peak memory usage of CODE + DATA\n"
1101 "* PSS: Proportional Set Size\n"
1102 "* 3D: memory allocated by GPU driver\n"
1103 "* GEM(PSS): GEM memory divided by # of sharers\n"
1104 "* GEM(ALLOC): sum of unique GEM memory in the system\n"
1105 "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1107 get_tmpfs_info(output_file);
1108 get_memcg_info(output_file);
1109 get_mem_info(output_file);
1111 fclose(output_file);
1117 static int show_map_new(int pid)
1121 unsigned shared_dirty = 0;
1122 unsigned shared_clean = 0;
1123 unsigned private_dirty = 0;
1124 unsigned private_clean = 0;
1126 unsigned long start = 0;
1127 unsigned long end = 0;
1128 unsigned private_clean_total = 0;
1129 unsigned private_dirty_total = 0;
1130 unsigned shared_clean_total = 0;
1131 unsigned shared_dirty_total = 0;
1132 int duplication = 0;
1134 milist = load_maps(pid);
1137 fprintf(stderr, "cannot get /proc/smaps for pid %d\n", pid);
1142 printf(" S(CODE) S(DATA) P(CODE) P(DATA) ADDR(start-end)"
1144 printf("-------- -------- -------- -------- -----------------"
1145 "------------------------------\n");
1147 printf(" S(CODE) S(DATA) P(CODE) P(DATA) PSS\n");
1148 printf("-------- -------- -------------------"
1149 "------------------\n");
1151 for (mi = milist; mi; mi = mi->next) {
1152 shared_clean += mi->shared_clean;
1153 shared_dirty += mi->shared_dirty;
1154 private_clean += mi->private_clean;
1155 private_dirty += mi->private_dirty;
1158 shared_clean_total += mi->shared_clean;
1159 shared_dirty_total += mi->shared_dirty;
1160 private_clean_total += mi->private_clean;
1161 private_dirty_total += mi->private_dirty;
1166 if ((mi->next && !strcmp(mi->next->name, mi->name)) &&
1167 (mi->next->start == mi->end)) {
1175 printf("%8d %8d %8d %8d %08lx-%08lx %s\n",
1176 shared_clean, shared_dirty, private_clean,
1177 private_dirty, start, end, mi->name);
1185 printf("%8d %8d %8d %8d %18d\n",
1188 private_clean_total,
1189 private_dirty_total,
1196 int main(int argc, char *argv[])
1202 if (!strncmp(argv[1], "-r", strlen("-r")+1)) {
1204 show_rss(OUTPUT_FILE, argv[2]);
1206 show_rss(OUTPUT_UART, NULL);
1208 } else if (!strncmp(argv[1], "-s", strlen("-s")+1)) {
1210 if (argc == 3 && atoi(argv[2]) > 0) {
1211 show_map_new(atoi(argv[2]));
1214 } else if (!strncmp(argv[1], "-a", strlen("-a")+1)) {
1216 show_map_all_new(OUTPUT_UART, NULL);
1218 } else if (!strncmp(argv[1], "-v", strlen("-v")+1)) {
1220 show_map_all_new(OUTPUT_UART, NULL);
1222 } else if (!strncmp(argv[1], "-f", strlen("-f")+1)) {
1225 show_map_all_new(OUTPUT_FILE, argv[2]);
1228 } else if (argc == 2 && atoi(argv[1]) > 0) {
1229 show_map_new(atoi(argv[1]));
1235 "memps [-a] | [-v] | [-s] <pid> | [-f] <output file full path>\n"
1236 " -s = sum (show only sum of each)\n"
1237 " -f = all (show all processes via output file)\n"
1238 " -a = all (show all processes)\n"
1239 " -v = verbos (show all processes in detail)\n");