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

Commit 07a554bf authored by San Mehat's avatar San Mehat Committed by Android Git Automerger
Browse files

am 493dad96: libcutils: Add abstracted support for setting scheduler policies...

am 493dad96: libcutils: Add abstracted support for setting scheduler policies Also changes the background policy to use SCHED_BATCH. IDLEPRIO can cause 100% starvation.

Merge commit '493dad96' into eclair-plus-aosp

* commit '493dad96':
  libcutils: Add abstracted support for setting scheduler policies
parents 6088a9ad 493dad96
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __CUTILS_SCHED_POLICY_H
#define __CUTILS_SCHED_POLICY_H

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
    SP_BACKGROUND = 0,
    SP_FOREGROUND = 1,
} SchedPolicy;

extern int set_sched_policy(int tid, SchedPolicy policy);

#ifdef __cplusplus
}
#endif

#endif /* __CUTILS_SCHED_POLICY_H */ 
+2 −1
Original line number Diff line number Diff line
@@ -36,7 +36,8 @@ commonSources := \
	record_stream.c \
	process_name.c \
	properties.c \
	threads.c
	threads.c \
	sched_policy.c

commonHostSources := \
        ashmem-host.c
+84 −0
Original line number Diff line number Diff line
/* libs/cutils/sched_policy.c
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <linux/sched.h>

#include <cutils/sched_policy.h>

static int add_tid_to_cgroup(int tid, const char *grp_name)
{
    int fd;
    char path[255];
    char text[64];

    sprintf(path, "/dev/cpuctl/%s/tasks", grp_name);

    if ((fd = open(path, O_WRONLY)) < 0)
        return -1;

    sprintf(text, "%d", tid);
    if (write(fd, text, strlen(text)) < 0) {
        close(fd);
        return -1;
    }

    close(fd);
    return 0;
}

int set_sched_policy(int tid, SchedPolicy policy)
{
    static int __sys_supports_schedgroups = -1;

    if (__sys_supports_schedgroups < 0) {
        if (!access("/dev/cpuctl/tasks", F_OK)) {
            __sys_supports_schedgroups = 1;
        } else {
            __sys_supports_schedgroups = 0;
        }
    }

    if (__sys_supports_schedgroups) {
        const char *grp = NULL;

        if (policy == SP_BACKGROUND) {
            grp = "bg_non_interactive";
        }

        if (add_tid_to_cgroup(tid, grp)) {
            if (errno != ESRCH && errno != ENOENT)
                return -errno;
        }
    } else {
        struct sched_param param;

        param.sched_priority = 0;
        sched_setscheduler(tid,
                           (policy == SP_BACKGROUND) ?
                            SCHED_BATCH : SCHED_NORMAL,
                           &param);
    }

    return 0;
}