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 2d544cf8ce
6 changed files with 29 additions and 76 deletions

View file

@ -23,6 +23,9 @@
extern "C" { extern "C" {
#endif #endif
#define NATIVE_HANDLE_MAX_FDS 1024
#define NATIVE_HANDLE_MAX_INTS 1024
/* Declare a char array for use with native_handle_init */ /* Declare a char array for use with native_handle_init */
#define NATIVE_HANDLE_DECLARE_STORAGE(name, maxFds, maxInts) \ #define NATIVE_HANDLE_DECLARE_STORAGE(name, maxFds, maxInts) \
alignas(native_handle_t) char (name)[ \ alignas(native_handle_t) char (name)[ \

View file

@ -22,9 +22,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static const int kMaxNativeFds = 1024;
static const int kMaxNativeInts = 1024;
native_handle_t* native_handle_init(char* storage, int numFds, int numInts) { native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
if ((uintptr_t) storage % alignof(native_handle_t)) { if ((uintptr_t) storage % alignof(native_handle_t)) {
errno = EINVAL; errno = EINVAL;
@ -39,7 +36,8 @@ native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
} }
native_handle_t* native_handle_create(int numFds, int numInts) { native_handle_t* native_handle_create(int numFds, int numInts) {
if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) { if (numFds < 0 || numInts < 0 || numFds > NATIVE_HANDLE_MAX_FDS ||
numInts > NATIVE_HANDLE_MAX_INTS) {
errno = EINVAL; errno = EINVAL;
return NULL; return NULL;
} }

View file

@ -984,7 +984,7 @@ static void ctrl_connect_handler(int data __unused, uint32_t events __unused) {
} }
#ifdef LMKD_LOG_STATS #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]; char key[LINE_MAX + 1];
int64_t value; 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; mem_st->swap_in_bytes = value;
} }
static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t uid) { static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) {
FILE *fp; FILE *fp;
char buf[PATH_MAX]; 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) { if (fp == NULL) {
ALOGE("%s open failed: %s", buf, strerror(errno)); ALOGE("%s open failed: %s", buf, strerror(errno));
return -1; return -1;
} }
while (fgets(buf, PAGE_SIZE, fp) != NULL) { while (fgets(buf, PAGE_SIZE, fp) != NULL ) {
memory_stat_parse_line(buf, mem_st); memory_stat_parse_line(buf, mem_st);
} }
fclose(fp); fclose(fp);
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 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;
} }
#endif #endif
@ -1354,11 +1316,7 @@ static int kill_one_process(struct proc* procp) {
#ifdef LMKD_LOG_STATS #ifdef LMKD_LOG_STATS
if (enable_stats_log) { if (enable_stats_log) {
if (per_app_memcg) { memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid);
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
@ -1385,10 +1343,7 @@ static int kill_one_process(struct proc* procp) {
if (memory_stat_parse_result == 0) { if (memory_stat_parse_result == 0) {
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.process_start_time_ns); 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, -1);
} }
#endif #endif
result = tasksize; result = tasksize;

View file

@ -65,7 +65,7 @@ int
stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid, 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, 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 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); assert(ctx != NULL);
int ret = -EINVAL; int ret = -EINVAL;
if (!ctx) { if (!ctx) {
@ -113,9 +113,5 @@ stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid
return ret; return ret;
} }
if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
return ret;
}
return write_to_logger(ctx, LOG_ID_STATS); return write_to_logger(ctx, LOG_ID_STATS);
} }

View file

@ -64,13 +64,9 @@ struct memory_stat {
int64_t rss_in_bytes; int64_t rss_in_bytes;
int64_t cache_in_bytes; int64_t cache_in_bytes;
int64_t swap_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 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
@ -88,7 +84,7 @@ int
stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid, 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, 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 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 __END_DECLS

View file

@ -22,15 +22,20 @@
#include <android-base/properties.h> #include <android-base/properties.h>
#include <android/hardware/usb/gadget/1.0/IUsbGadget.h> #include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
#include <hidl/HidlTransportSupport.h>
#define PERSISTENT_USB_CONFIG "persist.sys.usb.config" #define PERSISTENT_USB_CONFIG "persist.sys.usb.config"
using android::base::GetProperty; using android::base::GetProperty;
using android::base::SetProperty; using android::base::SetProperty;
using android::hardware::configureRpcThreadpool;
using android::hardware::usb::gadget::V1_0::GadgetFunction; using android::hardware::usb::gadget::V1_0::GadgetFunction;
using android::hardware::usb::gadget::V1_0::IUsbGadget; using android::hardware::usb::gadget::V1_0::IUsbGadget;
using android::hardware::Return; using android::hardware::Return;
int main(int /*argc*/, char** /*argv*/) { int main(int /*argc*/, char** /*argv*/) {
configureRpcThreadpool(1, true /*callerWillJoin*/);
android::sp<IUsbGadget> gadget = IUsbGadget::getService(); android::sp<IUsbGadget> gadget = IUsbGadget::getService();
Return<void> ret; Return<void> ret;