DO NOT MERGE - Merge pi-dev@5234907 into stage-aosp-master

Bug: 120848293
Change-Id: I92822048a7862acaa669847b2fa9c8f708159952
This commit is contained in:
Xin Li 2019-02-21 11:28:44 -08:00
commit 32bc2d15ec
3 changed files with 19 additions and 72 deletions

79
lmkd.c
View File

@ -984,7 +984,7 @@ static void ctrl_connect_handler(int data __unused, uint32_t events __unused) {
}
#ifdef LMKD_LOG_STATS
static void memory_stat_parse_line(char* line, struct memory_stat* mem_st) {
static void memory_stat_parse_line(char *line, struct memory_stat *mem_st) {
char key[LINE_MAX + 1];
int64_t value;
@ -1006,63 +1006,25 @@ static void memory_stat_parse_line(char* line, struct memory_stat* mem_st) {
mem_st->swap_in_bytes = value;
}
static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t uid) {
FILE *fp;
char buf[PATH_MAX];
static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) {
FILE *fp;
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid);
snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid);
fp = fopen(buf, "r");
fp = fopen(buf, "r");
if (fp == NULL) {
ALOGE("%s open failed: %s", buf, strerror(errno));
return -1;
}
if (fp == NULL) {
ALOGE("%s open failed: %s", buf, strerror(errno));
return -1;
}
while (fgets(buf, PAGE_SIZE, fp) != NULL) {
memory_stat_parse_line(buf, mem_st);
}
fclose(fp);
while (fgets(buf, PAGE_SIZE, fp) != NULL ) {
memory_stat_parse_line(buf, mem_st);
}
fclose(fp);
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 22 is starttime
// field 24 is rss_in_pages
int64_t pgfault = 0, pgmajfault = 0, starttime = 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 "
"%" SCNd64 " %*d %" SCNd64 "",
&pgfault, &pgmajfault, &starttime, &rss_in_pages) != 4) {
return -1;
}
mem_st->pgfault = pgfault;
mem_st->pgmajfault = pgmajfault;
mem_st->rss_in_bytes = (rss_in_pages * PAGE_SIZE);
mem_st->process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK));
return 0;
return 0;
}
#endif
@ -1354,11 +1316,7 @@ static int kill_one_process(struct proc* procp) {
#ifdef LMKD_LOG_STATS
if (enable_stats_log) {
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);
}
memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid);
}
#endif
@ -1385,10 +1343,7 @@ static int kill_one_process(struct proc* procp) {
if (memory_stat_parse_result == 0) {
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,
mem_st.cache_in_bytes, mem_st.swap_in_bytes, mem_st.process_start_time_ns);
} 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, -1);
mem_st.cache_in_bytes, mem_st.swap_in_bytes);
}
#endif
result = tasksize;

View File

@ -65,7 +65,7 @@ int
stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
char const* process_name, int32_t oom_score, int64_t pgfault,
int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
int64_t swap_in_bytes, int64_t process_start_time_ns) {
int64_t swap_in_bytes) {
assert(ctx != NULL);
int ret = -EINVAL;
if (!ctx) {
@ -113,9 +113,5 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid
return ret;
}
if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
return ret;
}
return write_to_logger(ctx, LOG_ID_STATS);
}

View File

@ -64,13 +64,9 @@ struct memory_stat {
int64_t rss_in_bytes;
int64_t cache_in_bytes;
int64_t swap_in_bytes;
int64_t process_start_time_ns;
};
#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
@ -88,7 +84,7 @@ int
stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
char const* process_name, int32_t oom_score, int64_t pgfault,
int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
int64_t swap_in_bytes, int64_t process_start_time_ns);
int64_t swap_in_bytes);
__END_DECLS