Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2bc24f88 authored by Rajeev Kumar's avatar Rajeev Kumar Committed by Alistair Strachan
Browse files

Read memory stats from /proc/pid/stat file.

(cherry pick from commit 0301683e)
Bug: 117333340
Test: Manual testing using alloc-stress tool
Merged-In: Ie555933aafa6a6b7aa1dbf5518ebe804376e0afd

Change-Id: I8ab08606dba7de2f65711204453067dbfbdcbdd8
parent 14fea4f7
Loading
Loading
Loading
Loading
+59 −16
Original line number Original line Diff line number Diff line
@@ -1006,7 +1006,7 @@ static void memory_stat_parse_line(char *line, struct memory_stat *mem_st) {
        mem_st->swap_in_bytes = value;
        mem_st->swap_in_bytes = value;
}
}


static int memory_stat_parse(struct memory_stat *mem_st,  int pid, uid_t uid) {
static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t uid) {
    FILE *fp;
    FILE *fp;
    char buf[PATH_MAX];
    char buf[PATH_MAX];


@@ -1026,6 +1026,42 @@ static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) {


    return 0;
    return 0;
}
}

static int memory_stat_from_procfs(struct memory_stat* mem_st, int pid) {
    char path[PATH_MAX];
    char buffer[PROC_STAT_BUFFER_SIZE];
    int fd, ret;

    snprintf(path, sizeof(path), PROC_STAT_FILE_PATH, pid);
    if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0) {
        ALOGE("%s open failed: %s", path, strerror(errno));
        return -1;
    }

    ret = read(fd, buffer, sizeof(buffer));
    if (ret < 0) {
        ALOGE("%s read failed: %s", path, strerror(errno));
        close(fd);
        return -1;
    }
    close(fd);

    // field 10 is pgfault
    // field 12 is pgmajfault
    // field 24 is rss_in_pages
    int64_t pgfault = 0, pgmajfault = 0, rss_in_pages = 0;
    if (sscanf(buffer,
               "%*u %*s %*s %*d %*d %*d %*d %*d %*d %" SCNd64 " %*d "
               "%" SCNd64 " %*d %*u %*u %*d %*d %*d %*d %*d %*d "
               "%*d %*d %" SCNd64 "",
               &pgfault, &pgmajfault, &rss_in_pages) != 3) {
        return -1;
    }
    mem_st->pgfault = pgfault;
    mem_st->pgmajfault = pgmajfault;
    mem_st->rss_in_bytes = (rss_in_pages * PAGE_SIZE);
    return 0;
}
#endif
#endif


/* /prop/zoneinfo parsing routines */
/* /prop/zoneinfo parsing routines */
@@ -1316,7 +1352,11 @@ static int kill_one_process(struct proc* procp) {


#ifdef LMKD_LOG_STATS
#ifdef LMKD_LOG_STATS
    if (enable_stats_log) {
    if (enable_stats_log) {
        memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid);
        if (per_app_memcg) {
            memory_stat_parse_result = memory_stat_from_cgroup(&mem_st, pid, uid);
        } else {
            memory_stat_parse_result = memory_stat_from_procfs(&mem_st, pid);
        }
    }
    }
#endif
#endif


@@ -1344,6 +1384,9 @@ static int kill_one_process(struct proc* procp) {
            stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname,
            stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname,
                    procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes,
                    procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes,
                    mem_st.cache_in_bytes, mem_st.swap_in_bytes);
                    mem_st.cache_in_bytes, mem_st.swap_in_bytes);
        } else if (enable_stats_log) {
            stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, procp->oomadj,
                                          -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1);
        }
        }
#endif
#endif
        result = tasksize;
        result = tasksize;
+3 −0
Original line number Original line Diff line number Diff line
@@ -67,6 +67,9 @@ struct memory_stat {
};
};


#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
#define PROC_STAT_FILE_PATH "/proc/%d/stat"
#define PROC_STAT_BUFFER_SIZE 1024
#define BYTES_IN_KILOBYTE 1024


/**
/**
 * Logs the change in LMKD state which is used as start/stop boundaries for logging
 * Logs the change in LMKD state which is used as start/stop boundaries for logging