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

Commit ed3bbc3a authored by Steve Kondik's avatar Steve Kondik
Browse files

core: Reduce mutex contention in ActivityManager

 * applyOomAdjLocked in ActivityManager will call into native
   layer to adjust tasks in the cpuctl and memory cgroups. The global AM
   lock is held while this happens, and on a busy system can result in
   slowdowns.
 * The cpuctl cgroup is already handled in a sane way. Adjust the memory
   cgroup code to cache file descriptors to reduce overhead by an order
   of magnitude.
 * These cgroups are actually exactly the same in their content, in the
   future when remounting a cgroup fs is supported we can combine them.

Change-Id: If0043e651eebdf73075a96e6de618badd8b5935b
parent b09f7797
Loading
Loading
Loading
Loading
+25 −12
Original line number Diff line number Diff line
@@ -370,27 +370,40 @@ jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz,
    return pri;
}

static bool memcgSupported = false;
static int memcgTasksFd = -1;
static int memcgSwapTasksFd = -1;
static pthread_once_t memcgInitOnce = PTHREAD_ONCE_INIT;

static void memcgInit(void) {
    if (!access("/sys/fs/cgroup/memory/tasks", F_OK)) {
        memcgTasksFd = open("/sys/fs/cgroup/memory/tasks", O_WRONLY);
        if (memcgTasksFd < 0) {
            return;
        }
        memcgSwapTasksFd = open("/sys/fs/cgroup/memory/sw/tasks", O_WRONLY);
        if (memcgSwapTasksFd < 0) {
            close(memcgTasksFd);
            return;
        }
        memcgSupported = true;
    }
}

jboolean android_os_Process_setSwappiness(JNIEnv *env, jobject clazz,
                                          jint pid, jboolean is_increased)
{
    char text[64];

    if (is_increased) {
        strcpy(text, "/sys/fs/cgroup/memory/sw/tasks");
    } else {
        strcpy(text, "/sys/fs/cgroup/memory/tasks");
    }

    struct stat st;
    if (stat(text, &st) || !S_ISREG(st.st_mode)) {
    pthread_once(&memcgInitOnce, memcgInit);
    if (!memcgSupported) {
        return false;
    }

    int fd = open(text, O_WRONLY);
    int fd = is_increased ? memcgSwapTasksFd : memcgTasksFd;
    char text[64];

    if (fd >= 0) {
        sprintf(text, "%" PRId32, pid);
        write(fd, text, strlen(text));
        close(fd);
    }

    return true;