1 /* Copyright 2014-2020 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>
34 #include <sys/utsname.h>
36 #define STR_SGX_PATH "/dev/pvrsrvkm"
37 #define STR_3D_PATH1 "/dev/mali"
38 #define STR_3D_PATH2 "/dev/kgsl-3d0"
39 #define STR_3D_UNIFIED_PATH "/usr/bin/gpu_mem_info"
40 #define STR_DRM_PATH1 "/drm mm object (deleted)"
41 #define STR_DRM_PATH2 "/dev/dri/card0"
42 #define MEMCG_PATH "/sys/fs/cgroup/memory"
43 #define ZRAM_USED_PATH "/sys/block/zram0/mem_used_total"
44 #define ZRAM_MM_STAT_PATH "/sys/block/zram0/mm_stat"
45 #define OOM_SCORE_ADJ_STR "oom_score_adj"
46 #define OOM_SCORE_STR "oom_score"
48 #define BUF_MAX (BUFSIZ) /* most optimal for libc::stdio */
49 #define BUF_INC_SIZE (512 * 1024) /* maximal SMAPS I saw 2 MB */
50 #define KB(bytes) ((bytes)/1024)
52 #define BYTE_TO_KBYTE(b) ((b) >> 10)
53 #define BYTE_TO_MBYTE(b) ((b) >> 20)
54 #define BYTE_TO_GBYTE(b) ((b) >> 30)
56 #define KBYTE_TO_BYTE(k) ((k) << 10)
57 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
58 #define KBYTE_TO_GBYTE(k) ((k) >> 20)
60 #define GBYTE_TO_BYTE(g) ((g) << 30)
61 #define GBYTE_TO_KBYTE(g) ((g) << 20)
62 #define GBYTE_TO_MBYTE(g) ((g) << 10)
64 typedef struct geminfo geminfo;
65 typedef struct mapinfo mapinfo;
66 typedef struct trib_mapinfo trib_mapinfo;
82 unsigned shared_clean;
83 unsigned shared_dirty;
84 unsigned private_clean;
85 unsigned private_dirty;
90 /* classify normal, graphic and other devices memory */
92 unsigned shared_clean;
93 unsigned shared_dirty;
94 unsigned private_clean;
95 unsigned private_dirty;
96 unsigned shared_clean_pss;
97 unsigned shared_dirty_pss;
106 unsigned other_devices;
120 static int use_gpu_mem_info = 0;
121 static int pid_max = 0;
123 /* reads file contents into memory */
124 static char* cread(const char* path)
126 /* once allocated area for reads */
127 static char* text = NULL;
128 static size_t size = 0;
133 int fd = open(path, O_RDONLY);
139 /* ensure we have enough space */
141 ptr = (char*)realloc(text, size + BUF_INC_SIZE);
150 size += BUF_INC_SIZE;
152 ret = read(fd, ptr, cap);
155 } else if (ret > 0) {
162 return (ret < 0 ? NULL : text);
165 /* like fgets/gets but adjusting contents pointer */
166 static inline char* cgets(char** contents)
168 if (contents && *contents && **contents) {
169 char* bos = *contents; /* begin of string */
170 char* eos = strchr(bos, '\n'); /* end of string */
184 /* get pid_max value */
185 static inline int get_pid_max(void)
187 static const char pid_max_path[] = "/proc/sys/kernel/pid_max";
190 line = cread(pid_max_path);
192 fprintf(stderr, "cannot open %s\n", pid_max_path);
196 return strtoul(line, NULL, 10);
200 static unsigned get_peak_rss(unsigned int pid)
202 static const char field[] = "VmHWM:";
207 snprintf(tmp, sizeof(tmp), "/proc/%d/status", pid);
210 fprintf(stderr, "cannot open %s\n", tmp);
214 value = strstr(line, field);
216 value += sizeof(field);
217 return strtoul(value, NULL, 10);
222 #define NUM_GEM_FIELD 6
224 static geminfo *read_geminfo(FILE *fp)
228 unsigned int pid, tgid, handle, refcount, hcount;
231 if (fgets(line, BUF_MAX, fp) != NULL) {
232 if (sscanf(line, "%d %d %d %d %d 0x%x",
233 &pid, &tgid, &handle, &refcount,
234 &hcount, &gem_size) != NUM_GEM_FIELD)
239 tgeminfo = malloc(sizeof(geminfo));
240 if (tgeminfo == NULL)
242 tgeminfo->tgid = tgid;
243 tgeminfo->hcount = hcount;
244 tgeminfo->rss_size = KB(gem_size);
245 tgeminfo->pss_size = KB(gem_size/tgeminfo->hcount);
253 static geminfo *find_geminfo(unsigned int tgid, geminfo *gilist)
256 for (gi = gilist; gi; ) {
257 if (gi->tgid == tgid)
265 static geminfo *load_geminfo(void)
268 geminfo *gilist = NULL;
269 geminfo *exist_ginfo = NULL;
274 drm_fp = fopen("/sys/kernel/debug/dri/0/gem_info", "r");
276 if (drm_fp == NULL) {
278 "cannot open /sys/kernel/debug/dri/0/gem_info\n");
282 if (fgets(line, BUF_MAX, drm_fp) == NULL) {
286 /* we should count a number of whitespace separated fields */
287 int in_field = (line[0] && !isblank(line[0]));
288 unsigned int size = (unsigned)in_field;
289 const char* ptr = &line[1];
291 /* sscanf() was used in original code, so number of fields */
292 /* in string is expected to be at least NUM_GEM_FIELD */
293 while (*ptr && size < NUM_GEM_FIELD) {
294 if (isblank(*ptr++)) {
301 /* next field started */
308 if (size != NUM_GEM_FIELD) {
314 while ((ginfo = read_geminfo(drm_fp)) != NULL) {
315 if (gilist && ginfo->tgid == gilist->tgid) {
316 gilist->pss_size += ginfo->pss_size;
317 gilist->rss_size += ginfo->rss_size;
320 } else if (gilist && ((exist_ginfo = find_geminfo(ginfo->tgid, gilist)) != NULL)) {
321 exist_ginfo->pss_size += ginfo->pss_size;
322 exist_ginfo->rss_size += ginfo->rss_size;
326 ginfo->next = gilist;
336 /* b6e82000-b6e83000 rw-p 00020000 b3:19 714 /usr/lib/ld-2.20-2014.11.so : TM1
337 * 7f9389d000-7f9389e000 rw-p 0001f000 b3:12 618 /usr/lib64/ld-2.20-2014.11.so : TM2
338 * 7fae2e4b2000-7fae2e4b3000 r--p 00021000 fe:01 603 /usr/lib64/ld-2.20-2014.11.so : x86-64 Emulator
339 * 01234567890123456789012345678901234567890123456789012345678901234567890123456789
342 mapinfo *read_mapinfo(char** smaps)
349 if ((line = cgets(smaps)) == 0)
356 mi = malloc(sizeof(mapinfo));
360 n = sscanf(line, "%lx-%lx %ms %*s %*s %*s %m[^\n]",
361 &mi->start, &mi->end, &mi->perm, &mi->name);
363 if (n == 3 && !mi->name)
364 mi->name = strndup("[anon]", strlen("[anon]"));
366 fprintf(stderr, "Fail to parse smaps\n");
371 while ((line = cgets(smaps))) {
372 if (sscanf(line, "Size: %d kB", &mi->size) == 1)
374 else if (sscanf(line, "Rss: %d kB", &mi->rss) == 1)
376 else if (sscanf(line, "Pss: %d kB", &mi->pss) == 1)
378 else if (sscanf(line, "Shared_Clean: %d kB", &mi->shared_clean) == 1)
380 else if (sscanf(line, "Shared_Dirty: %d kB", &mi->shared_dirty) == 1)
382 else if (sscanf(line, "Private_Clean: %d kB", &mi->private_clean) == 1)
384 else if (sscanf(line, "Private_Dirty: %d kB", &mi->private_dirty) == 1)
386 else if (sscanf(line, "Swap: %d kB", &mi->swap) == 1)
389 /* Drain lines until it meets next VMA address */
391 if ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f'))
399 static unsigned total_gem_memory(void)
402 unsigned total_gem_mem = 0;
403 unsigned name, size, handles, refcount;
406 gem_fp = fopen("/sys/kernel/debug/dri/0/gem_names", "r");
407 if (gem_fp == NULL) {
409 "cannot open /sys/kernel/debug/dri/0/gem_names\n");
413 if (fgets(line, BUF_MAX, gem_fp) == NULL) {
418 while (fgets(line, BUF_MAX, gem_fp) != NULL) {
419 if (sscanf(line, "%d %d %d %d\n",
420 &name, &size, &handles, &refcount) == 4) {
421 if (total_gem_mem <= UINT_MAX - size) {
422 total_gem_mem += size;
428 return total_gem_mem;
431 int get_zram_used(u_int32_t *zram_used)
434 char *fscanf_format = "%u";
437 f = fopen(ZRAM_USED_PATH, "r");
440 * ZRAM_USED_PATH is deprecated on latest kernel, but to support
441 * old kernel first try with that and if fails then use new node
444 f = fopen(ZRAM_MM_STAT_PATH, "r");
446 fprintf(stderr, "Fail to open zram stat file.\n");
449 /* only read 3rd value */
450 fscanf_format = "%*d %*d %d %*d %*d %*d %*d %*d";
453 ret = fscanf(f, fscanf_format, zram_used);
455 fprintf(stderr, "Fail to read file\n");
464 int fread_uint(const char *path, u_int32_t *number)
469 f = fopen(path, "r");
472 fprintf(stderr, "Fail to open %s file.\n", path);
476 ret = fscanf(f, "%u", number);
478 fprintf(stderr, "Fail to read file\n");
487 static int cgroup_read_node(const char *cgroup_name,
488 const char *file_name, unsigned int *value)
490 char buf[PATH_MAX + NAME_MAX];
492 snprintf(buf, sizeof(buf), "%s%s", cgroup_name, file_name);
493 ret = fread_uint(buf, value);
498 * @desc Provides usage in bytes for provided memory cgroup. Works
499 * with/without swap accounting.
501 * @param memcg_path[in] Full path to memory cgroup
502 * @param swap[in] Boolean value for deciding if account usage with swap
503 * @return current cgroup usage in bytes or 0 on error
505 static unsigned int get_memcg_usage(const char *memcg_path, bool swap)
511 ret = cgroup_read_node(memcg_path,
512 "/memory.memsw.usage_in_bytes", &usage);
514 ret = cgroup_read_node(memcg_path, "/memory.usage_in_bytes",
524 static void get_memcg_info(FILE *output_fp)
528 struct dirent *entry;
529 struct stat path_stat;
531 unsigned long usage, usage_with_swap;
533 fprintf(output_fp, "====================================================================\n");
534 fprintf(output_fp, "MEMORY CGROUPS USAGE INFO\n");
536 pdir = opendir(MEMCG_PATH);
538 fprintf(stderr, "cannot read directory %s", MEMCG_PATH);
543 while ((entry = readdir(pdir)) != NULL && !errno) {
544 snprintf(buf, sizeof(buf), "%s/%s", MEMCG_PATH, entry->d_name);
545 /* If can't stat then ignore */
546 if (stat(buf, &path_stat) != 0)
549 /* If it's not directory or it's parent path then ignore */
550 if (!(S_ISDIR(path_stat.st_mode) &&
551 strncmp(entry->d_name, "..", 3)))
554 usage = get_memcg_usage(buf, false);
555 usage_with_swap = get_memcg_usage(buf, true);
556 /* It is posible by rounding errors to get negative value */
557 usage_swap = usage_with_swap - usage;
561 /* Case of root cgroup in hierarchy */
562 if (!strncmp(entry->d_name, ".", 2))
563 fprintf(output_fp, "%13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB) \n\n",
564 MEMCG_PATH, BYTE_TO_MBYTE(usage),
565 BYTE_TO_KBYTE(usage),
566 BYTE_TO_MBYTE(usage_with_swap),
567 BYTE_TO_KBYTE(usage_with_swap),
568 BYTE_TO_MBYTE(usage_swap),
569 BYTE_TO_KBYTE(usage_swap));
571 fprintf(output_fp, "memcg: %13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB)\n",
572 entry->d_name, BYTE_TO_MBYTE(usage),
573 BYTE_TO_KBYTE(usage),
574 BYTE_TO_MBYTE(usage_with_swap),
575 BYTE_TO_KBYTE(usage_with_swap),
576 BYTE_TO_MBYTE(usage_swap),
577 BYTE_TO_KBYTE(usage_swap));
584 static void get_mem_info(FILE *output_fp)
589 unsigned int free = 0, cached = 0;
590 unsigned int total_mem = 0, available = 0, used;
591 unsigned int swap_total = 0, swap_free = 0, zram_used, swap_used;
592 unsigned int used_ratio;
594 if (output_fp == NULL)
597 fp = fopen("/proc/meminfo", "r");
600 fprintf(stderr, "/proc/meminfo open failed, %p", fp);
604 while (fgets(buf, PATH_MAX, fp) != NULL) {
605 if ((idx = strstr(buf, "MemTotal:"))) {
606 idx += strlen("Memtotal:");
607 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
609 total_mem = atoi(idx);
610 } else if ((idx = strstr(buf, "MemFree:"))) {
611 idx += strlen("MemFree:");
612 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
615 } else if ((idx = strstr(buf, "MemAvailable:"))) {
616 idx += strlen("MemAvailable:");
617 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
619 available = atoi(idx);
620 } else if ((idx = strstr(buf, "Cached:")) && !strstr(buf, "Swap")) {
621 idx += strlen("Cached:");
622 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
625 } else if ((idx = strstr(buf, "SwapTotal:"))) {
626 idx += strlen("SwapTotal:");
627 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
629 swap_total = atoi(idx);
630 } else if ((idx = strstr(buf, "SwapFree:"))) {
631 idx += strlen("SwapFree");
632 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
634 swap_free = atoi(idx);
639 if (total_mem == 0) {
640 fprintf(stderr, "cannot get total memory size\n");
646 available = free + cached;
647 used = total_mem - available;
648 used_ratio = used * 100 / total_mem;
649 swap_used = swap_total - swap_free;
651 if (get_zram_used(&zram_used) < 0)
655 "====================================================================\n");
658 fprintf(output_fp, "Total RAM size: \t%15d MB( %6d kB)\n",
659 total_mem >> 10, total_mem);
661 fprintf(output_fp, "Used (Mem+Reclaimable): %15d MB( %6d kB)\n",
662 (total_mem - free) >> 10, total_mem - free);
664 fprintf(output_fp, "Used (Mem+Swap): \t%15d MB( %6d kB)\n",
667 fprintf(output_fp, "Used (Mem): \t\t%15d MB( %6d kB)\n",
670 fprintf(output_fp, "Used (Swap): \t\t%15d MB( %6d kB)\n",
671 swap_used >> 10, swap_used);
673 fprintf(output_fp, "Used (Zram block device): %13d MB( %6d kB)\n",
674 BYTE_TO_MBYTE(zram_used), BYTE_TO_KBYTE(zram_used));
676 fprintf(output_fp, "Used Ratio: \t\t%15d %%\n", used_ratio);
678 fprintf(output_fp, "Mem Free:\t\t%15d MB( %6d kB)\n",
681 fprintf(output_fp, "Available (Free+Reclaimable):%10d MB( %6d kB)\n",
687 static int get_tmpfs_info(FILE *output_fp)
691 char *tmpfs_mp; /* tmpfs mount point */
692 struct statfs tmpfs_info;
694 if (output_fp == NULL)
697 fp = fopen("/etc/mtab", "r");
702 "====================================================================\n");
703 fprintf(output_fp, "TMPFS INFO\n");
705 while (fgets(line, BUF_MAX, fp) != NULL) {
706 if (sscanf(line, "tmpfs %ms tmpfs", &tmpfs_mp) == 1) {
707 if (statfs(tmpfs_mp, &tmpfs_info) == 0) {
709 #ifndef __USE_FILE_OFFSET64
710 "tmpfs %16s Total %8ld KB, Used %8ld, Avail %8ld\n",
712 "tmpfs %16s Total %8lld KB, Used %8lld, Avail %8lld\n",
715 /* 1 block is 4 KB */
716 tmpfs_info.f_blocks * 4,
717 (tmpfs_info.f_blocks - tmpfs_info.f_bfree) * 4,
718 tmpfs_info.f_bfree * 4);
728 mapinfo *load_maps(int pid)
735 snprintf(tmp, sizeof(tmp), "/proc/%d/smaps", pid);
740 while ((mi = read_mapinfo(&smaps)) != 0) {
742 if ((!strcmp(mi->name, milist->name)
743 && (mi->name[0] != '['))) {
744 milist->size += mi->size;
745 milist->swap += mi->swap;
746 milist->rss += mi->rss;
747 milist->pss += mi->pss;
748 milist->shared_clean += mi->shared_clean;
749 milist->shared_dirty += mi->shared_dirty;
750 milist->private_clean += mi->private_clean;
751 milist->private_dirty += mi->private_dirty;
753 milist->end = mi->end;
754 strncpy(milist->perm, mi->perm, 4);
768 static void init_trib_mapinfo(trib_mapinfo *tmi)
772 tmi->shared_clean = 0;
773 tmi->shared_dirty = 0;
774 tmi->private_clean = 0;
775 tmi->private_dirty = 0;
777 tmi->shared_clean_pss = 0;
778 tmi->shared_dirty_pss = 0;
786 tmi->other_devices = 0;
790 unsigned int get_graphic_3d_meminfo(unsigned int tgid)
792 char command[256], buf[256];
794 unsigned int size = 0;
798 snprintf(command, sizeof(command), "%s %d", STR_3D_UNIFIED_PATH, tgid);
799 p_gpu = popen(command, "r");
801 if (fgets(buf, 256, p_gpu)) {
802 ret = sscanf(buf, "%d %ms %d %ms", &tid, &tmp[0], &size, &tmp[1]);
814 get_trib_mapinfo(unsigned int tgid, mapinfo *milist,
815 geminfo *gilist, trib_mapinfo *result)
819 mapinfo *temp = NULL;
825 init_trib_mapinfo(result);
827 if (use_gpu_mem_info)
828 result->graphic_3d = get_graphic_3d_meminfo(tgid);
830 for (mi = milist; mi;) {
831 if (!use_gpu_mem_info && strstr(mi->name, STR_SGX_PATH)) {
832 result->graphic_3d += mi->pss;
833 } else if (!use_gpu_mem_info && (strstr(mi->name, STR_3D_PATH1) ||
834 strstr(mi->name, STR_3D_PATH2))) {
835 result->graphic_3d += mi->size;
836 } else if (mi->rss != 0 && mi->pss == 0
837 && mi->shared_clean == 0
838 && mi->shared_dirty == 0
839 && mi->private_clean == 0
840 && mi->private_dirty == 0
842 result->other_devices += mi->size;
843 } else if (!strncmp(mi->name, STR_DRM_PATH1,
844 sizeof(STR_DRM_PATH1)) ||
845 !strncmp(mi->name, STR_DRM_PATH2,
846 sizeof(STR_DRM_PATH2))) {
847 result->gem_mmap += mi->rss;
849 result->shared_clean += mi->shared_clean;
850 result->shared_dirty += mi->shared_dirty;
851 result->private_clean += mi->private_clean;
852 result->private_dirty += mi->private_dirty;
853 result->swap += mi->swap;
854 result->rss += mi->rss;
855 result->pss += mi->pss;
856 result->size += mi->size;
858 if (mi->shared_clean != 0)
859 result->shared_clean_pss += mi->pss;
860 else if (mi->shared_dirty != 0)
861 result->shared_dirty_pss += mi->pss;
872 result->peak_rss = get_peak_rss(tgid);
873 if (result->peak_rss < result->rss)
874 result->peak_rss = result->rss;
875 if (result->gem_mmap > 0)
876 result->peak_rss -= result->gem_mmap;
878 gi = find_geminfo(tgid, gilist);
880 result->gem_rss = gi->rss_size;
881 result->gem_pss = gi->pss_size;
887 static int get_cmdline(unsigned int pid, char *cmdline)
890 char buf[NAME_MAX] = {0, };
893 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
894 fp = fopen(buf, "r");
898 fprintf(stderr, "cannot file open %s\n", buf);
901 if ((ret = fscanf(fp, "%4095s", cmdline)) < 1) {
910 static int get_oomscoreadj(unsigned int pid, const char *oom_string)
916 snprintf(tmp, sizeof(tmp), "/proc/%d/%s", pid, oom_string);
917 fp = fopen(tmp, "r");
923 if (fgets(tmp, sizeof(tmp), fp) == NULL) {
929 oomadj_val = atoi(tmp);
935 static void get_rss(pid_t pid, unsigned long *result)
938 char proc_path[PATH_MAX];
939 unsigned long rss = 0;
943 snprintf(proc_path, sizeof(proc_path), "/proc/%d/statm", pid);
944 fp = fopen(proc_path, "r");
948 if (fscanf(fp, "%*s %ld", &rss) < 1) {
955 /* convert page to Kb */
960 static const char* get_readable_oom_path(void)
965 snprintf(tmp, sizeof(tmp), "/proc/1/%s", OOM_SCORE_ADJ_STR);
966 fp = fopen(tmp, "r");
969 return OOM_SCORE_ADJ_STR;
971 snprintf(tmp, sizeof(tmp), "/proc/1/%s", OOM_SCORE_STR);
972 fp = fopen(tmp, "r");
975 return OOM_SCORE_STR;
980 static void show_rss(int output_type, char *output_path)
983 struct dirent *curdir;
985 char cmdline[PATH_MAX];
986 FILE *output_file = NULL;
989 const char *oom_path = NULL;
991 oom_path = get_readable_oom_path();
993 fprintf(stderr, "there is no readable oom path\n");
997 pDir = opendir("/proc");
999 fprintf(stderr, "cannot read directory /proc.\n");
1003 if (output_type == OUTPUT_FILE && output_path) {
1004 output_file = fopen(output_path, "w+");
1006 fprintf(stderr, "cannot open output file(%s)\n",
1012 output_file = stdout;
1015 fprintf(output_file,
1016 " PID RSS %14s COMMAND\n", oom_path);
1019 while ((curdir = readdir(pDir)) != NULL && !errno) {
1020 pid = atoi(curdir->d_name);
1021 if (pid < 1 || pid > pid_max || pid == getpid())
1024 if (get_cmdline(pid, cmdline) < 0)
1027 oom_score_adj = get_oomscoreadj(pid, oom_path);
1029 fprintf(output_file,
1030 "%8d %8lu %8d %s\n",
1037 } /* end of while */
1039 get_tmpfs_info(output_file);
1040 get_mem_info(output_file);
1042 fclose(output_file);
1048 static int show_map_all_new(int output_type, char *output_path)
1051 struct dirent *curdir;
1055 unsigned total_pss = 0;
1056 unsigned total_private = 0;
1057 unsigned total_private_code = 0;
1058 unsigned total_private_data = 0;
1059 unsigned total_shared_code = 0;
1060 unsigned total_shared_data = 0;
1061 unsigned total_shared_code_pss = 0;
1062 unsigned total_shared_data_pss = 0;
1063 unsigned total_swap = 0;
1064 unsigned total_rss = 0;
1065 unsigned total_graphic_3d = 0;
1066 unsigned total_gem_rss = 0;
1067 unsigned total_gem_pss = 0;
1068 unsigned total_peak_rss = 0;
1069 unsigned total_allocated_gem = 0;
1071 char cmdline[PATH_MAX];
1072 FILE *output_file = NULL;
1074 const char *oom_path = NULL;
1076 oom_path = get_readable_oom_path();
1078 fprintf(stderr, "there is no readable oom path\n");
1082 use_gpu_mem_info = (!access(STR_3D_UNIFIED_PATH, F_OK | X_OK)) ? 1 : 0;
1085 pDir = opendir("/proc");
1087 fprintf(stderr, "cannot read directory /proc.\n");
1091 if (output_type == OUTPUT_FILE && output_path) {
1092 output_file = fopen(output_path, "w+");
1094 fprintf(stderr, "cannot open output file(%s)\n",
1100 output_file = stdout;
1102 glist = load_geminfo();
1106 fprintf(output_file,
1107 " PID S(CODE) S(DATA) P(CODE) P(DATA) "
1108 " PEAK PSS 3D GEM(PSS) GEM(RSS) "
1109 " SWAP %14s COMMAND\n", oom_path);
1111 fprintf(output_file,
1112 " PID CODE DATA PEAK PSS "
1113 " 3D GEM(PSS) SWAP COMMAND\n");
1117 while ((curdir = readdir(pDir)) != NULL && !errno) {
1118 pid = atoi(curdir->d_name);
1119 if (pid < 1 || pid > pid_max || pid == getpid())
1122 if (get_cmdline(pid, cmdline) < 0)
1125 milist = load_maps(pid);
1129 /* get classified map info */
1130 get_trib_mapinfo(pid, milist, glist, &tmi);
1131 oom_score_adj = get_oomscoreadj(pid, oom_path);
1135 fprintf(output_file,
1136 "%8d %8d %8d %8d %8d "
1137 "%8d %8d %8d %8d %8d "
1153 fprintf(output_file,
1154 "%8d %8d %8d %8d %8d "
1157 tmi.shared_clean + tmi.private_clean,
1158 tmi.shared_dirty + tmi.private_dirty,
1166 if (tmi.other_devices != 0)
1167 fprintf(output_file,
1168 "%s(%d) %d KB may mapped by device(s).\n",
1169 cmdline, pid, tmi.other_devices);
1172 total_private += (tmi.private_clean + tmi.private_dirty);
1173 total_pss += tmi.pss;
1174 total_rss += tmi.rss;
1175 total_graphic_3d += tmi.graphic_3d;
1176 total_gem_rss += tmi.gem_rss;
1177 total_gem_pss += tmi.gem_pss;
1178 total_private_code += tmi.private_clean;
1179 total_private_data += tmi.private_dirty;
1180 total_swap += tmi.swap;
1181 total_shared_code += tmi.shared_clean;
1182 total_shared_data += tmi.shared_dirty;
1183 total_peak_rss += tmi.peak_rss;
1185 total_shared_code_pss += tmi.shared_clean_pss;
1186 total_shared_data_pss += tmi.shared_dirty_pss;
1188 } /* end of while */
1190 total_allocated_gem = KB(total_gem_memory());
1191 fprintf(output_file,
1192 "==============================================="
1193 "===============================================\n");
1195 fprintf(output_file,
1196 "TOTAL: S(CODE) S(DATA) P(CODE) P(DATA) "
1197 " PEAK PSS 3D GEM(PSS) GEM(RSS) "
1198 "GEM(ALLOC) SWAP TOTAL(KB)\n");
1199 fprintf(output_file,
1201 "%8d %8d %8d %8d %8d "
1203 total_shared_code, total_shared_data,
1204 total_private_code, total_private_data,
1205 total_peak_rss, total_pss, total_graphic_3d,
1206 total_gem_pss, total_gem_rss,
1207 total_allocated_gem, total_swap,
1208 total_pss + total_graphic_3d +
1209 total_allocated_gem);
1211 fprintf(output_file,
1212 "TOTAL: CODE DATA PEAK PSS "
1213 " 3D GEM(PSS) GEM(ALLOC) SWAP TOTAL(KB)\n");
1214 fprintf(output_file,
1216 "%8d %8d %10d %8d %10d\n",
1217 total_shared_code + total_private_code,
1218 total_shared_data + total_private_data,
1219 total_peak_rss, total_pss,
1220 total_graphic_3d, total_gem_pss,
1221 total_allocated_gem, total_swap,
1222 total_pss + total_graphic_3d +
1223 total_allocated_gem);
1228 fprintf(output_file,
1229 "* S(CODE): shared clean memory, it includes"
1230 " duplicated memory\n"
1231 "* S(DATA): shared dirty memory, it includes"
1232 " duplicated memory\n"
1233 "* P(CODE): private clean memory\n"
1234 "* P(DATA): private dirty memory\n"
1235 "* PEAK: peak memory usage of S(CODE) + S(DATA)"
1236 " + P(CODE) + P(DATA)\n"
1237 "* PSS: Proportional Set Size\n"
1238 "* 3D: memory allocated by GPU driver\n"
1239 "* GEM(PSS): GEM memory divided by # of sharers\n"
1240 "* GEM(RSS): GEM memory including duplicated memory\n"
1241 "* GEM(ALLOC): sum of unique gem memory in the system\n"
1242 "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1244 fprintf(output_file,
1245 "* CODE: shared and private clean memory\n"
1246 "* DATA: shared and private dirty memory\n"
1247 "* PEAK: peak memory usage of CODE + DATA\n"
1248 "* PSS: Proportional Set Size\n"
1249 "* 3D: memory allocated by GPU driver\n"
1250 "* GEM(PSS): GEM memory divided by # of sharers\n"
1251 "* GEM(ALLOC): sum of unique GEM memory in the system\n"
1252 "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1254 get_tmpfs_info(output_file);
1255 get_memcg_info(output_file);
1256 get_mem_info(output_file);
1258 fclose(output_file);
1265 static int show_map_new(int pid)
1269 mapinfo *temp = NULL;
1270 unsigned shared_dirty = 0;
1271 unsigned shared_clean = 0;
1272 unsigned private_dirty = 0;
1273 unsigned private_clean = 0;
1275 unsigned long start = 0;
1276 unsigned long end = 0;
1277 unsigned private_clean_total = 0;
1278 unsigned private_dirty_total = 0;
1279 unsigned shared_clean_total = 0;
1280 unsigned shared_dirty_total = 0;
1281 int duplication = 0;
1283 milist = load_maps(pid);
1286 fprintf(stderr, "cannot get /proc/smaps for pid %d\n", pid);
1291 printf(" S(CODE) S(DATA) P(CODE) P(DATA) PSS "
1292 "ADDR(start-end) OBJECT NAME\n");
1293 printf("-------- -------- -------- -------- -------- "
1294 "----------------- ------------------------------\n");
1296 printf(" S(CODE) S(DATA) P(CODE) P(DATA) "
1298 printf("-------- -------- -------- -------- "
1299 "------------------\n");
1301 for (mi = milist; mi;) {
1302 shared_clean += mi->shared_clean;
1303 shared_dirty += mi->shared_dirty;
1304 private_clean += mi->private_clean;
1305 private_dirty += mi->private_dirty;
1308 shared_clean_total += mi->shared_clean;
1309 shared_dirty_total += mi->shared_dirty;
1310 private_clean_total += mi->private_clean;
1311 private_dirty_total += mi->private_dirty;
1316 if ((mi->next && !strcmp(mi->next->name, mi->name)) &&
1317 (mi->next->start == mi->end)) {
1332 printf("%8d %8d %8d %8d %8d %08lx-%08lx %s\n",
1333 shared_clean, shared_dirty, private_clean,
1334 private_dirty, mi->pss, start, end, mi->name);
1349 printf("%8d %8d %8d %8d %18d\n",
1352 private_clean_total,
1353 private_dirty_total,
1360 int main(int argc, char *argv[])
1365 pid_max = get_pid_max();
1368 if (!strncmp(argv[1], "-r", strlen("-r")+1)) {
1370 show_rss(OUTPUT_FILE, argv[2]);
1372 show_rss(OUTPUT_UART, NULL);
1374 } else if (!strncmp(argv[1], "-s", strlen("-s")+1)) {
1376 if (argc == 3 && atoi(argv[2]) > 0) {
1377 show_map_new(atoi(argv[2]));
1380 } else if (!strncmp(argv[1], "-a", strlen("-a")+1)) {
1382 show_map_all_new(OUTPUT_UART, NULL);
1384 } else if (!strncmp(argv[1], "-v", strlen("-v")+1)) {
1386 show_map_all_new(OUTPUT_UART, NULL);
1388 } else if (!strncmp(argv[1], "-f", strlen("-f")+1)) {
1391 show_map_all_new(OUTPUT_FILE, argv[2]);
1394 } else if (argc == 2 && atoi(argv[1]) > 0) {
1395 show_map_new(atoi(argv[1]));
1401 "memps [-a] | [-v] | [-r] | [-s] <pid> | <pid> | [-f] <output file full path>\n"
1402 " -s = sum (show only sum of each)\n"
1403 " -f = all (show all processes via output file)\n"
1404 " -a = all (show all processes)\n"
1405 " -r = all (show rss of all processes)\n"
1406 " -v = verbos (show all processes in detail)\n");