util: kernel: Add functions for getting process smaps file 80/275980/3
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 12 May 2022 05:44:33 +0000 (14:44 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 20 Jun 2022 11:26:51 +0000 (20:26 +0900)
Change-Id: Id8b9d677585ccc4381c33eb53373037c64540f59
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
include/util/kernel.h
src/util/kernel.c

index 5938d9f..c640bd5 100644 (file)
@@ -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
index 653b297..2101535 100644 (file)
@@ -27,6 +27,8 @@
 #include <util/log.h>
 #include <util/kernel.h>
 
+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);
+}