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>
35 #include <sys/utsname.h>
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 MEMCG_PATH "/sys/fs/cgroup/memory"
45 #define ZRAM_USED_PATH "/sys/block/zram0/mem_used_total"
46 #define ZRAM_MM_STAT_PATH "/sys/block/zram0/mm_stat"
47 #define OOM_SCORE_ADJ_STR "oom_score_adj"
48 #define OOM_SCORE_STR "oom_score"
50 #define BUF_MAX (BUFSIZ) /* most optimal for libc::stdio */
51 #define BUF_INC_SIZE (512 * 1024) /* maximal SMAPS I saw 2 MB */
52 #define KB(bytes) ((bytes)/1024)
54 #define BYTE_TO_KBYTE(b) ((b) >> 10)
55 #define BYTE_TO_MBYTE(b) ((b) >> 20)
56 #define BYTE_TO_GBYTE(b) ((b) >> 30)
58 #define KBYTE_TO_BYTE(k) ((k) << 10)
59 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
60 #define KBYTE_TO_GBYTE(k) ((k) >> 20)
62 #define GBYTE_TO_BYTE(g) ((g) << 30)
63 #define GBYTE_TO_KBYTE(g) ((g) << 20)
64 #define GBYTE_TO_MBYTE(g) ((g) << 10)
66 typedef struct geminfo geminfo;
67 typedef struct mapinfo mapinfo;
68 typedef struct trib_mapinfo trib_mapinfo;
84 unsigned shared_clean;
85 unsigned shared_dirty;
86 unsigned private_clean;
87 unsigned private_dirty;
92 /* classify normal, graphic and other devices memory */
94 unsigned shared_clean;
95 unsigned shared_dirty;
96 unsigned private_clean;
97 unsigned private_dirty;
98 unsigned shared_clean_pss;
99 unsigned shared_dirty_pss;
108 unsigned other_devices;
122 static int use_gpu_mem_info = 0;
123 static int pid_max = 0;
124 static int dri_card = 0;
126 /* reads file contents into memory */
127 static char* cread(const char* path)
129 /* once allocated area for reads */
130 static char* text = NULL;
131 static size_t size = 0;
136 int fd = open(path, O_RDONLY);
142 /* ensure we have enough space */
144 ptr = (char*)realloc(text, size + BUF_INC_SIZE);
153 size += BUF_INC_SIZE;
155 ret = read(fd, ptr, cap);
158 } else if (ret > 0) {
165 return (ret < 0 ? NULL : text);
168 /* like fgets/gets but adjusting contents pointer */
169 static inline char* cgets(char** contents)
171 if (contents && *contents && **contents) {
172 char* bos = *contents; /* begin of string */
173 char* eos = strchr(bos, '\n'); /* end of string */
187 /* get pid_max value */
188 static inline int get_pid_max(void)
190 static const char pid_max_path[] = "/proc/sys/kernel/pid_max";
193 line = cread(pid_max_path);
195 fprintf(stderr, "cannot open %s\n", pid_max_path);
199 return strtoul(line, NULL, 10);
203 static unsigned get_peak_rss(unsigned int pid)
205 static const char field[] = "VmHWM:";
210 snprintf(tmp, sizeof(tmp), "/proc/%d/status", pid);
213 fprintf(stderr, "cannot open %s\n", tmp);
217 value = strstr(line, field);
219 value += sizeof(field);
220 return strtoul(value, NULL, 10);
225 #define NUM_GEM_FIELD 6
227 static geminfo *read_geminfo(FILE *fp)
231 unsigned int pid, tgid, handle, refcount, hcount;
234 if (fgets(line, BUF_MAX, fp) != NULL) {
235 if (sscanf(line, "%d %d %d %d %d 0x%x",
236 &pid, &tgid, &handle, &refcount,
237 &hcount, &gem_size) != NUM_GEM_FIELD)
242 tgeminfo = malloc(sizeof(geminfo));
243 if (tgeminfo == NULL)
245 tgeminfo->tgid = tgid;
246 tgeminfo->hcount = hcount;
247 tgeminfo->rss_size = KB(gem_size);
248 tgeminfo->pss_size = KB(gem_size/tgeminfo->hcount);
256 static geminfo *find_geminfo(unsigned int tgid, geminfo *gilist)
259 for (gi = gilist; gi; ) {
260 if (gi->tgid == tgid)
268 static geminfo *load_geminfo(void)
271 geminfo *gilist = NULL;
272 geminfo *exist_ginfo = NULL;
277 drm_fp = fopen(STR_DRM_DBG_DIR "0/gem_info", "r");
278 if (drm_fp == NULL) {
279 drm_fp = fopen(STR_DRM_DBG_DIR "1/gem_info", "r");
281 if (drm_fp == NULL) {
283 "cannot open " STR_DRM_DBG_DIR "%d/gem_info\n",
289 if (fgets(line, BUF_MAX, drm_fp) == NULL) {
293 /* we should count a number of whitespace separated fields */
294 int in_field = (line[0] && !isblank(line[0]));
295 unsigned int size = (unsigned)in_field;
296 const char* ptr = &line[1];
298 /* sscanf() was used in original code, so number of fields */
299 /* in string is expected to be at least NUM_GEM_FIELD */
300 while (*ptr && size < NUM_GEM_FIELD) {
301 if (isblank(*ptr++)) {
308 /* next field started */
315 if (size != NUM_GEM_FIELD) {
321 while ((ginfo = read_geminfo(drm_fp)) != NULL) {
322 if (gilist && ginfo->tgid == gilist->tgid) {
323 gilist->pss_size += ginfo->pss_size;
324 gilist->rss_size += ginfo->rss_size;
327 } else if (gilist && ((exist_ginfo = find_geminfo(ginfo->tgid, gilist)) != NULL)) {
328 exist_ginfo->pss_size += ginfo->pss_size;
329 exist_ginfo->rss_size += ginfo->rss_size;
333 ginfo->next = gilist;
343 /* b6e82000-b6e83000 rw-p 00020000 b3:19 714 /usr/lib/ld-2.20-2014.11.so : TM1
344 * 7f9389d000-7f9389e000 rw-p 0001f000 b3:12 618 /usr/lib64/ld-2.20-2014.11.so : TM2
345 * 7fae2e4b2000-7fae2e4b3000 r--p 00021000 fe:01 603 /usr/lib64/ld-2.20-2014.11.so : x86-64 Emulator
346 * 01234567890123456789012345678901234567890123456789012345678901234567890123456789
349 mapinfo *read_mapinfo(char** smaps)
356 if ((line = cgets(smaps)) == 0)
363 mi = malloc(sizeof(mapinfo));
367 n = sscanf(line, "%lx-%lx %ms %*s %*s %*s %m[^\n]",
368 &mi->start, &mi->end, &mi->perm, &mi->name);
370 if (n == 3 && !mi->name)
371 mi->name = strndup("[anon]", strlen("[anon]"));
373 fprintf(stderr, "Fail to parse smaps\n");
378 while ((line = cgets(smaps))) {
379 if (sscanf(line, "Size: %d kB", &mi->size) == 1)
381 else if (sscanf(line, "Rss: %d kB", &mi->rss) == 1)
383 else if (sscanf(line, "Pss: %d kB", &mi->pss) == 1)
385 else if (sscanf(line, "Shared_Clean: %d kB", &mi->shared_clean) == 1)
387 else if (sscanf(line, "Shared_Dirty: %d kB", &mi->shared_dirty) == 1)
389 else if (sscanf(line, "Private_Clean: %d kB", &mi->private_clean) == 1)
391 else if (sscanf(line, "Private_Dirty: %d kB", &mi->private_dirty) == 1)
393 else if (sscanf(line, "Swap: %d kB", &mi->swap) == 1)
396 /* Drain lines until it meets next VMA address */
398 if ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f'))
406 static unsigned total_gem_memory(void)
409 unsigned total_gem_mem = 0;
410 unsigned name, size, handles, refcount;
414 gem_fp = fopen(STR_DRM_DBG_DIR "0/gem_names", "r");
416 gem_fp = fopen(STR_DRM_DBG_DIR "1/gem_names", "r");
417 if (gem_fp == NULL) {
419 "cannot open " STR_DRM_DBG_DIR "%d/gem_names\n", dri_card);
423 if (fgets(line, BUF_MAX, gem_fp) == NULL) {
428 while (fgets(line, BUF_MAX, gem_fp) != NULL) {
429 if (sscanf(line, "%d %d %d %d\n",
430 &name, &size, &handles, &refcount) == 4) {
431 if (total_gem_mem <= UINT_MAX - size) {
432 total_gem_mem += size;
438 return total_gem_mem;
441 int get_zram_used(u_int32_t *zram_used)
444 /* only read 3rd value */
445 char *fscanf_format = "%*d %*d %d %*d %*d %*d %*d %*d";
448 f = fopen(ZRAM_MM_STAT_PATH, "r");
451 * ZRAM_USED_PATH is deprecated on latest kernel, but to support
452 * old kernel try with that if fails new node 'mm_stat'.
454 f = fopen(ZRAM_USED_PATH, "r");
456 fprintf(stderr, "Fail to open zram file.\n");
459 fscanf_format = "%u";
462 ret = fscanf(f, fscanf_format, zram_used);
464 fprintf(stderr, "Fail to read file\n");
473 int fread_uint(const char *path, u_int32_t *number)
478 f = fopen(path, "r");
481 fprintf(stderr, "Fail to open %s file.\n", path);
485 ret = fscanf(f, "%u", number);
487 fprintf(stderr, "Fail to read file\n");
496 static int cgroup_read_node(const char *cgroup_name,
497 const char *file_name, unsigned int *value)
499 char buf[PATH_MAX + NAME_MAX];
501 snprintf(buf, sizeof(buf), "%s%s", cgroup_name, file_name);
502 ret = fread_uint(buf, value);
507 * @desc Provides usage in bytes for provided memory cgroup. Works
508 * with/without swap accounting.
510 * @param memcg_path[in] Full path to memory cgroup
511 * @param swap[in] Boolean value for deciding if account usage with swap
512 * @return current cgroup usage in bytes or 0 on error
514 static unsigned int get_memcg_usage(const char *memcg_path, bool swap)
520 ret = cgroup_read_node(memcg_path,
521 "/memory.memsw.usage_in_bytes", &usage);
523 ret = cgroup_read_node(memcg_path, "/memory.usage_in_bytes",
533 static void get_memcg_info(FILE *output_fp)
537 struct dirent *entry;
538 struct stat path_stat;
540 unsigned long usage, usage_with_swap;
542 fprintf(output_fp, "====================================================================\n");
543 fprintf(output_fp, "MEMORY CGROUPS USAGE INFO\n");
545 pdir = opendir(MEMCG_PATH);
547 fprintf(stderr, "cannot read directory %s", MEMCG_PATH);
552 while ((entry = readdir(pdir)) != NULL && !errno) {
553 snprintf(buf, sizeof(buf), "%s/%s", MEMCG_PATH, entry->d_name);
554 /* If can't stat then ignore */
555 if (stat(buf, &path_stat) != 0)
558 /* If it's not directory or it's parent path then ignore */
559 if (!(S_ISDIR(path_stat.st_mode) &&
560 strncmp(entry->d_name, "..", 3)))
563 usage = get_memcg_usage(buf, false);
564 usage_with_swap = get_memcg_usage(buf, true);
565 /* It is posible by rounding errors to get negative value */
566 usage_swap = usage_with_swap - usage;
570 /* Case of root cgroup in hierarchy */
571 if (!strncmp(entry->d_name, ".", 2))
572 fprintf(output_fp, "%13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB) \n\n",
573 MEMCG_PATH, BYTE_TO_MBYTE(usage),
574 BYTE_TO_KBYTE(usage),
575 BYTE_TO_MBYTE(usage_with_swap),
576 BYTE_TO_KBYTE(usage_with_swap),
577 BYTE_TO_MBYTE(usage_swap),
578 BYTE_TO_KBYTE(usage_swap));
580 fprintf(output_fp, "memcg: %13s Mem %3ld MB (%6ld kB), Mem+Swap %3ld MB (%6ld kB), Swap %3ld MB (%6ld kB)\n",
581 entry->d_name, BYTE_TO_MBYTE(usage),
582 BYTE_TO_KBYTE(usage),
583 BYTE_TO_MBYTE(usage_with_swap),
584 BYTE_TO_KBYTE(usage_with_swap),
585 BYTE_TO_MBYTE(usage_swap),
586 BYTE_TO_KBYTE(usage_swap));
593 static void get_mem_info(FILE *output_fp)
598 unsigned int free = 0, cached = 0;
599 unsigned int total_mem = 0, available = 0, used;
600 unsigned int swap_total = 0, swap_free = 0, zram_used, swap_used;
601 unsigned int used_ratio;
603 if (output_fp == NULL)
606 fp = fopen("/proc/meminfo", "r");
609 fprintf(stderr, "/proc/meminfo open failed, %p", fp);
613 while (fgets(buf, PATH_MAX, fp) != NULL) {
614 if ((idx = strstr(buf, "MemTotal:"))) {
615 idx += strlen("Memtotal:");
616 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
618 total_mem = atoi(idx);
619 } else if ((idx = strstr(buf, "MemFree:"))) {
620 idx += strlen("MemFree:");
621 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
624 } else if ((idx = strstr(buf, "MemAvailable:"))) {
625 idx += strlen("MemAvailable:");
626 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
628 available = atoi(idx);
629 } else if ((idx = strstr(buf, "Cached:")) && !strstr(buf, "Swap")) {
630 idx += strlen("Cached:");
631 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
634 } else if ((idx = strstr(buf, "SwapTotal:"))) {
635 idx += strlen("SwapTotal:");
636 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
638 swap_total = atoi(idx);
639 } else if ((idx = strstr(buf, "SwapFree:"))) {
640 idx += strlen("SwapFree");
641 while ((idx < buf + PATH_MAX) && (*idx < '0' || *idx > '9'))
643 swap_free = atoi(idx);
648 if (total_mem == 0) {
649 fprintf(stderr, "cannot get total memory size\n");
655 available = free + cached;
656 used = total_mem - available;
657 used_ratio = used * 100 / total_mem;
658 swap_used = swap_total - swap_free;
660 if (get_zram_used(&zram_used) < 0)
664 "====================================================================\n");
667 fprintf(output_fp, "Total RAM size: \t%15d MB( %6d kB)\n",
668 total_mem >> 10, total_mem);
670 fprintf(output_fp, "Used (Mem+Reclaimable): %15d MB( %6d kB)\n",
671 (total_mem - free) >> 10, total_mem - free);
673 fprintf(output_fp, "Used (Mem+Swap): \t%15d MB( %6d kB)\n",
676 fprintf(output_fp, "Used (Mem): \t\t%15d MB( %6d kB)\n",
679 fprintf(output_fp, "Used (Swap): \t\t%15d MB( %6d kB)\n",
680 swap_used >> 10, swap_used);
682 fprintf(output_fp, "Used (Zram block device): %13d MB( %6d kB)\n",
683 BYTE_TO_MBYTE(zram_used), BYTE_TO_KBYTE(zram_used));
685 fprintf(output_fp, "Used Ratio: \t\t%15d %%\n", used_ratio);
687 fprintf(output_fp, "Mem Free:\t\t%15d MB( %6d kB)\n",
690 fprintf(output_fp, "Available (Free+Reclaimable):%10d MB( %6d kB)\n",
696 static int get_tmpfs_info(FILE *output_fp)
700 char *tmpfs_mp; /* tmpfs mount point */
701 struct statfs tmpfs_info;
703 if (output_fp == NULL)
706 fp = fopen("/etc/mtab", "r");
711 "====================================================================\n");
712 fprintf(output_fp, "TMPFS INFO\n");
714 while (fgets(line, BUF_MAX, fp) != NULL) {
715 if (sscanf(line, "tmpfs %ms tmpfs", &tmpfs_mp) == 1) {
716 if (statfs(tmpfs_mp, &tmpfs_info) == 0) {
718 #ifndef __USE_FILE_OFFSET64
719 "tmpfs %16s Total %8ld KB, Used %8ld, Avail %8ld\n",
721 "tmpfs %16s Total %8"PRId64" KB, Used %8"PRId64", Avail %8"PRId64"\n",
724 /* 1 block is 4 KB */
725 tmpfs_info.f_blocks * 4,
726 (tmpfs_info.f_blocks - tmpfs_info.f_bfree) * 4,
727 tmpfs_info.f_bfree * 4);
737 mapinfo *load_maps(int pid)
744 snprintf(tmp, sizeof(tmp), "/proc/%d/smaps", pid);
749 while ((mi = read_mapinfo(&smaps)) != 0) {
751 if ((!strcmp(mi->name, milist->name)
752 && (mi->name[0] != '['))) {
753 milist->size += mi->size;
754 milist->swap += mi->swap;
755 milist->rss += mi->rss;
756 milist->pss += mi->pss;
757 milist->shared_clean += mi->shared_clean;
758 milist->shared_dirty += mi->shared_dirty;
759 milist->private_clean += mi->private_clean;
760 milist->private_dirty += mi->private_dirty;
762 milist->end = mi->end;
763 strncpy(milist->perm, mi->perm, 4);
777 static void init_trib_mapinfo(trib_mapinfo *tmi)
781 tmi->shared_clean = 0;
782 tmi->shared_dirty = 0;
783 tmi->private_clean = 0;
784 tmi->private_dirty = 0;
786 tmi->shared_clean_pss = 0;
787 tmi->shared_dirty_pss = 0;
795 tmi->other_devices = 0;
799 unsigned int get_graphic_3d_meminfo(unsigned int tgid)
801 char command[256], buf[256];
803 unsigned int size = 0;
807 snprintf(command, sizeof(command), "%s %d", STR_3D_UNIFIED_PATH, tgid);
808 p_gpu = popen(command, "r");
810 if (fgets(buf, 256, p_gpu)) {
811 ret = sscanf(buf, "%d %ms %d %ms", &tid, &tmp[0], &size, &tmp[1]);
823 get_trib_mapinfo(unsigned int tgid, mapinfo *milist,
824 geminfo *gilist, trib_mapinfo *result)
828 mapinfo *temp = NULL;
834 init_trib_mapinfo(result);
836 if (use_gpu_mem_info)
837 result->graphic_3d = get_graphic_3d_meminfo(tgid);
839 for (mi = milist; mi;) {
840 if (!use_gpu_mem_info && strstr(mi->name, STR_SGX_PATH)) {
841 result->graphic_3d += mi->pss;
842 } else if (!use_gpu_mem_info && (strstr(mi->name, STR_3D_PATH1) ||
843 strstr(mi->name, STR_3D_PATH2))) {
844 result->graphic_3d += mi->size;
845 } else if (mi->rss != 0 && mi->pss == 0
846 && mi->shared_clean == 0
847 && mi->shared_dirty == 0
848 && mi->private_clean == 0
849 && mi->private_dirty == 0
851 result->other_devices += mi->size;
852 } else if (!strncmp(mi->name, STR_DRM_PATH1,
853 sizeof(STR_DRM_PATH1)) ||
854 !strncmp(mi->name, STR_DRM_PATH2,
855 sizeof(STR_DRM_PATH2))) {
856 result->gem_mmap += mi->rss;
858 result->shared_clean += mi->shared_clean;
859 result->shared_dirty += mi->shared_dirty;
860 result->private_clean += mi->private_clean;
861 result->private_dirty += mi->private_dirty;
862 result->swap += mi->swap;
863 result->rss += mi->rss;
864 result->pss += mi->pss;
865 result->size += mi->size;
867 if (mi->shared_clean != 0)
868 result->shared_clean_pss += mi->pss;
869 else if (mi->shared_dirty != 0)
870 result->shared_dirty_pss += mi->pss;
881 result->peak_rss = get_peak_rss(tgid);
882 if (result->peak_rss < result->rss)
883 result->peak_rss = result->rss;
884 if (result->gem_mmap > 0)
885 result->peak_rss -= result->gem_mmap;
887 gi = find_geminfo(tgid, gilist);
889 result->gem_rss = gi->rss_size;
890 result->gem_pss = gi->pss_size;
896 static int get_cmdline(unsigned int pid, char *cmdline)
899 char buf[NAME_MAX] = {0, };
902 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
903 fp = fopen(buf, "r");
907 fprintf(stderr, "cannot file open %s\n", buf);
910 if ((ret = fscanf(fp, "%4095s", cmdline)) < 1) {
919 static int get_oomscoreadj(unsigned int pid, const char *oom_string)
925 snprintf(tmp, sizeof(tmp), "/proc/%d/%s", pid, oom_string);
926 fp = fopen(tmp, "r");
932 if (fgets(tmp, sizeof(tmp), fp) == NULL) {
938 oomadj_val = atoi(tmp);
944 static void get_rss(pid_t pid, unsigned long *result)
947 char proc_path[PATH_MAX];
948 unsigned long rss = 0;
952 snprintf(proc_path, sizeof(proc_path), "/proc/%d/statm", pid);
953 fp = fopen(proc_path, "r");
957 if (fscanf(fp, "%*s %ld", &rss) < 1) {
964 /* convert page to Kb */
969 static const char* get_readable_oom_path(void)
974 snprintf(tmp, sizeof(tmp), "/proc/1/%s", OOM_SCORE_ADJ_STR);
975 fp = fopen(tmp, "r");
978 return OOM_SCORE_ADJ_STR;
980 snprintf(tmp, sizeof(tmp), "/proc/1/%s", OOM_SCORE_STR);
981 fp = fopen(tmp, "r");
984 return OOM_SCORE_STR;
989 static void show_rss(int output_type, char *output_path)
992 struct dirent *curdir;
994 char cmdline[PATH_MAX];
995 FILE *output_file = NULL;
998 const char *oom_path = NULL;
1000 oom_path = get_readable_oom_path();
1002 fprintf(stderr, "there is no readable oom path\n");
1006 pDir = opendir("/proc");
1008 fprintf(stderr, "cannot read directory /proc.\n");
1012 if (output_type == OUTPUT_FILE && output_path) {
1013 output_file = fopen(output_path, "w+");
1015 fprintf(stderr, "cannot open output file(%s)\n",
1021 output_file = stdout;
1024 fprintf(output_file,
1025 " PID RSS %14s COMMAND\n", oom_path);
1028 while ((curdir = readdir(pDir)) != NULL && !errno) {
1029 pid = atoi(curdir->d_name);
1030 if (pid < 1 || pid > pid_max || pid == getpid())
1033 if (get_cmdline(pid, cmdline) < 0)
1036 oom_score_adj = get_oomscoreadj(pid, oom_path);
1038 fprintf(output_file,
1039 "%8d %8lu %8d %s\n",
1046 } /* end of while */
1048 get_tmpfs_info(output_file);
1049 get_mem_info(output_file);
1051 fclose(output_file);
1057 static int show_map_all_new(int output_type, char *output_path)
1060 struct dirent *curdir;
1064 unsigned total_pss = 0;
1065 unsigned total_private = 0;
1066 unsigned total_private_code = 0;
1067 unsigned total_private_data = 0;
1068 unsigned total_shared_code = 0;
1069 unsigned total_shared_data = 0;
1070 unsigned total_shared_code_pss = 0;
1071 unsigned total_shared_data_pss = 0;
1072 unsigned total_swap = 0;
1073 unsigned total_rss = 0;
1074 unsigned total_graphic_3d = 0;
1075 unsigned total_gem_rss = 0;
1076 unsigned total_gem_pss = 0;
1077 unsigned total_peak_rss = 0;
1078 unsigned total_allocated_gem = 0;
1080 char cmdline[PATH_MAX];
1081 FILE *output_file = NULL;
1083 const char *oom_path = NULL;
1085 oom_path = get_readable_oom_path();
1087 fprintf(stderr, "there is no readable oom path\n");
1091 use_gpu_mem_info = (!access(STR_3D_UNIFIED_PATH, F_OK | X_OK)) ? 1 : 0;
1094 pDir = opendir("/proc");
1096 fprintf(stderr, "cannot read directory /proc.\n");
1100 if (output_type == OUTPUT_FILE && output_path) {
1101 output_file = fopen(output_path, "w+");
1103 fprintf(stderr, "cannot open output file(%s)\n",
1109 output_file = stdout;
1111 glist = load_geminfo();
1115 fprintf(output_file,
1116 " PID S(CODE) S(DATA) P(CODE) P(DATA) "
1117 " PEAK PSS 3D GEM(PSS) GEM(RSS) "
1118 " SWAP %14s COMMAND\n", oom_path);
1120 fprintf(output_file,
1121 " PID CODE DATA PEAK PSS "
1122 " 3D GEM(PSS) SWAP COMMAND\n");
1126 while ((curdir = readdir(pDir)) != NULL && !errno) {
1127 pid = atoi(curdir->d_name);
1128 if (pid < 1 || pid > pid_max || pid == getpid())
1131 if (get_cmdline(pid, cmdline) < 0)
1134 milist = load_maps(pid);
1138 /* get classified map info */
1139 get_trib_mapinfo(pid, milist, glist, &tmi);
1140 oom_score_adj = get_oomscoreadj(pid, oom_path);
1144 fprintf(output_file,
1145 "%8d %8d %8d %8d %8d "
1146 "%8d %8d %8d %8d %8d "
1162 fprintf(output_file,
1163 "%8d %8d %8d %8d %8d "
1166 tmi.shared_clean + tmi.private_clean,
1167 tmi.shared_dirty + tmi.private_dirty,
1175 if (tmi.other_devices != 0)
1176 fprintf(output_file,
1177 "%s(%d) %d KB may mapped by device(s).\n",
1178 cmdline, pid, tmi.other_devices);
1181 total_private += (tmi.private_clean + tmi.private_dirty);
1182 total_pss += tmi.pss;
1183 total_rss += tmi.rss;
1184 total_graphic_3d += tmi.graphic_3d;
1185 total_gem_rss += tmi.gem_rss;
1186 total_gem_pss += tmi.gem_pss;
1187 total_private_code += tmi.private_clean;
1188 total_private_data += tmi.private_dirty;
1189 total_swap += tmi.swap;
1190 total_shared_code += tmi.shared_clean;
1191 total_shared_data += tmi.shared_dirty;
1192 total_peak_rss += tmi.peak_rss;
1194 total_shared_code_pss += tmi.shared_clean_pss;
1195 total_shared_data_pss += tmi.shared_dirty_pss;
1197 } /* end of while */
1199 total_allocated_gem = KB(total_gem_memory());
1200 fprintf(output_file,
1201 "==============================================="
1202 "===============================================\n");
1204 fprintf(output_file,
1205 "TOTAL: S(CODE) S(DATA) P(CODE) P(DATA) "
1206 " PEAK PSS 3D GEM(PSS) GEM(RSS) "
1207 "GEM(ALLOC) SWAP TOTAL(KB)\n");
1208 fprintf(output_file,
1210 "%8d %8d %8d %8d %8d "
1212 total_shared_code, total_shared_data,
1213 total_private_code, total_private_data,
1214 total_peak_rss, total_pss, total_graphic_3d,
1215 total_gem_pss, total_gem_rss,
1216 total_allocated_gem, total_swap,
1217 total_pss + total_graphic_3d +
1218 total_allocated_gem);
1220 fprintf(output_file,
1221 "TOTAL: CODE DATA PEAK PSS "
1222 " 3D GEM(PSS) GEM(ALLOC) SWAP TOTAL(KB)\n");
1223 fprintf(output_file,
1225 "%8d %8d %10d %8d %10d\n",
1226 total_shared_code + total_private_code,
1227 total_shared_data + total_private_data,
1228 total_peak_rss, total_pss,
1229 total_graphic_3d, total_gem_pss,
1230 total_allocated_gem, total_swap,
1231 total_pss + total_graphic_3d +
1232 total_allocated_gem);
1237 fprintf(output_file,
1238 "* S(CODE): shared clean memory, it includes"
1239 " duplicated memory\n"
1240 "* S(DATA): shared dirty memory, it includes"
1241 " duplicated memory\n"
1242 "* P(CODE): private clean memory\n"
1243 "* P(DATA): private dirty memory\n"
1244 "* PEAK: peak memory usage of S(CODE) + S(DATA)"
1245 " + P(CODE) + P(DATA)\n"
1246 "* PSS: Proportional Set Size\n"
1247 "* 3D: memory allocated by GPU driver\n"
1248 "* GEM(PSS): GEM memory divided by # of sharers\n"
1249 "* GEM(RSS): GEM memory including duplicated memory\n"
1250 "* GEM(ALLOC): sum of unique gem memory in the system\n"
1251 "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1253 fprintf(output_file,
1254 "* CODE: shared and private clean memory\n"
1255 "* DATA: shared and private dirty memory\n"
1256 "* PEAK: peak memory usage of CODE + DATA\n"
1257 "* PSS: Proportional Set Size\n"
1258 "* 3D: memory allocated by GPU driver\n"
1259 "* GEM(PSS): GEM memory divided by # of sharers\n"
1260 "* GEM(ALLOC): sum of unique GEM memory in the system\n"
1261 "* TOTAL: PSS + 3D + GEM(ALLOC)\n");
1263 get_tmpfs_info(output_file);
1264 get_memcg_info(output_file);
1265 get_mem_info(output_file);
1267 fclose(output_file);
1274 static int show_map_new(int pid)
1278 mapinfo *temp = NULL;
1279 unsigned shared_dirty = 0;
1280 unsigned shared_clean = 0;
1281 unsigned private_dirty = 0;
1282 unsigned private_clean = 0;
1284 unsigned long start = 0;
1285 unsigned long end = 0;
1286 unsigned private_clean_total = 0;
1287 unsigned private_dirty_total = 0;
1288 unsigned shared_clean_total = 0;
1289 unsigned shared_dirty_total = 0;
1290 int duplication = 0;
1292 milist = load_maps(pid);
1295 fprintf(stderr, "cannot get /proc/smaps for pid %d\n", pid);
1300 if (sizeof(unsigned long) == 4) {
1301 /* for 32-bit address */
1302 printf(" S(CODE) S(DATA) P(CODE) P(DATA) PSS "
1305 printf("-------- -------- -------- -------- -------- "
1306 "----------------- "
1307 "------------------------------\n");
1309 /* for 64-bit address */
1310 printf(" S(CODE) S(DATA) P(CODE) P(DATA) PSS "
1313 printf("-------- -------- -------- -------- -------- "
1314 "--------------------------------- "
1315 "------------------------------\n");
1318 printf(" S(CODE) S(DATA) P(CODE) P(DATA) PSS\n");
1319 printf("-------- -------- -------- -------- --------\n");
1321 for (mi = milist; mi;) {
1322 shared_clean += mi->shared_clean;
1323 shared_dirty += mi->shared_dirty;
1324 private_clean += mi->private_clean;
1325 private_dirty += mi->private_dirty;
1328 shared_clean_total += mi->shared_clean;
1329 shared_dirty_total += mi->shared_dirty;
1330 private_clean_total += mi->private_clean;
1331 private_dirty_total += mi->private_dirty;
1336 if ((mi->next && !strcmp(mi->next->name, mi->name)) &&
1337 (mi->next->start == mi->end)) {
1352 if (sizeof(unsigned long) == 4) {
1353 /* for 32-bit address */
1354 printf("%8d %8d %8d %8d %8d %08lx-%08lx %s\n",
1355 shared_clean, shared_dirty,
1356 private_clean, private_dirty, mi->pss,
1357 start, end, mi->name);
1359 /* for 64-bit address */
1360 printf("%8d %8d %8d %8d %8d %016lx-%016lx %s\n",
1361 shared_clean, shared_dirty,
1362 private_clean, private_dirty, mi->pss,
1363 start, end, mi->name);
1379 printf("%8d %8d %8d %8d %8d\n",
1382 private_clean_total,
1383 private_dirty_total,
1390 int main(int argc, char *argv[])
1395 pid_max = get_pid_max();
1398 if (!strncmp(argv[1], "-r", strlen("-r")+1)) {
1400 show_rss(OUTPUT_FILE, argv[2]);
1402 show_rss(OUTPUT_UART, NULL);
1404 } else if (!strncmp(argv[1], "-s", strlen("-s")+1)) {
1406 if (argc == 3 && atoi(argv[2]) > 0) {
1407 show_map_new(atoi(argv[2]));
1410 } else if (!strncmp(argv[1], "-a", strlen("-a")+1)) {
1412 show_map_all_new(OUTPUT_UART, NULL);
1414 } else if (!strncmp(argv[1], "-v", strlen("-v")+1)) {
1416 show_map_all_new(OUTPUT_UART, NULL);
1418 } else if (!strncmp(argv[1], "-f", strlen("-f")+1)) {
1421 show_map_all_new(OUTPUT_FILE, argv[2]);
1424 } else if (argc == 2 && atoi(argv[1]) > 0) {
1425 show_map_new(atoi(argv[1]));
1431 "memps [-a] | [-v] | [-r] | [-s] <pid> | <pid> | [-f] <output file full path>\n"
1432 " -s = sum (show only sum of each)\n"
1433 " -f = all (show all processes via output file)\n"
1434 " -a = all (show all processes)\n"
1435 " -r = all (show rss of all processes)\n"
1436 " -v = verbose (show all processes in detail)\n");