From: Dongwoo Lee Date: Thu, 12 May 2022 05:44:33 +0000 (+0900) Subject: util: kernel: Add functions for getting process smaps file X-Git-Tag: submit/tizen/20220623.085905~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b9f08a7f088b274b8018d7ac34b2f74a45dcad9e;p=platform%2Fcore%2Fsystem%2Fpass.git util: kernel: Add functions for getting process smaps file Change-Id: Id8b9d677585ccc4381c33eb53373037c64540f59 Signed-off-by: Dongwoo Lee --- diff --git a/include/util/kernel.h b/include/util/kernel.h index 5938d9f..c640bd5 100644 --- a/include/util/kernel.h +++ b/include/util/kernel.h @@ -35,6 +35,13 @@ struct cpu_stat { int64_t soft_irq; }; +struct proc_map_info { + uint32_t rss; + uint32_t pss; + uint32_t swap; + uint32_t swap_pss; +}; + enum { PROCESS_STAT_FIELD_PID, PROCESS_STAT_FIELD_COMM, @@ -58,4 +65,5 @@ int kernel_get_process_stat_fields(pid_t pid, char *buffer, int buf_len, char *stat_fields[PROCESS_STAT_FIELD_MAX]); int kernel_get_process_taskstats(struct taskstats *stats, int cmd_type, pid_t pid); int kernel_get_thread_group_taskstats(struct taskstats *stats, pid_t tgid, bool get_proc_info); +int kernel_get_thread_group_map_info(struct proc_map_info *map_info, pid_t tgid); #endif diff --git a/src/util/kernel.c b/src/util/kernel.c index 653b297..2101535 100644 --- a/src/util/kernel.c +++ b/src/util/kernel.c @@ -27,6 +27,8 @@ #include #include +static int have_smaps_rollup; + static int __get_cpu_num(char *path) { FILE *fp; @@ -407,3 +409,44 @@ int kernel_get_thread_group_taskstats(struct taskstats *stats, pid_t tgid, bool return 0; } + +int kernel_get_thread_group_map_info(struct proc_map_info *map_info, pid_t tgid) +{ + char smap_file_path[BUFF_MAX]; + char buffer[BUFF_MAX]; + FILE *smaps_file; + + sprintf(smap_file_path, "/proc/%d/%s", tgid, have_smaps_rollup ? "smaps_rollup" : "smaps"); + + smaps_file = fopen(smap_file_path, "r"); + if (!smaps_file) + return -EIO; + + map_info->rss = 0; + map_info->pss = 0; + map_info->swap = 0; + map_info->swap_pss = 0; + + while (fgets(buffer, sizeof(buffer), smaps_file)) { + if (strchr(buffer, '\n') == 0) + continue; + + if (strstr(buffer, "Rss:")) + map_info->rss += strtol(buffer + 4, NULL, 10); + else if (strstr(buffer, "Pss:")) + map_info->pss += strtol(buffer + 4, NULL, 10); + else if (strstr(buffer, "Swap:")) + map_info->swap += strtol(buffer + 5, NULL, 10); + else if (strstr(buffer, "SwapPss:")) + map_info->swap_pss += strtol(buffer + 8, NULL, 10); + } + + fclose(smaps_file); + + return 0; +} + +static void __CONSTRUCTOR__ kernel_init(void) +{ + have_smaps_rollup = !access("/proc/self/smaps_rollup", R_OK); +}