【ResourceManagerService 分析】第三篇:SystemCallbackImpl 电量管理 BatteryNotifier 的代理+ CpuSet 提升等级的入口

SystemCallbackInterface 接口定义在头文件中,定义了四个public的函数
在这里插入图片描述

frameworks/av/services/mediaresourcemanager/ResourceManagerService.h

class ResourceManagerService : public BnResourceManagerService {
public:
    struct SystemCallbackInterface : public RefBase {
        // 通知 uid 启动视频播放
        virtual void noteStartVideo(int uid) = 0;
        // 通知 uid 停止视频播放
        virtual void noteStopVideo(int uid) = 0;
        // 通知 uid 重置视频播放
        virtual void noteResetVideo() = 0;
        // 请求将 media.codec 所在进程的cpu等级移动到 SP_FOREGROUND 和 SP_FOREGROUND中间的级别
        virtual bool requestCpusetBoost(bool enable) = 0;
    };
}

frameworks/av/services/mediaresourcemanager/ResourceManagerService.cpp

struct SystemCallbackImpl :
        public ResourceManagerService::SystemCallbackInterface {
    SystemCallbackImpl() : mClientToken(new BBinder()) {}

    virtual void noteStartVideo(int uid) override {
        BatteryNotifier::getInstance().noteStartVideo(uid);
    }
    virtual void noteStopVideo(int uid) override {
        BatteryNotifier::getInstance().noteStopVideo(uid);
    }
    virtual void noteResetVideo() override {
        BatteryNotifier::getInstance().noteResetVideo();
    }
    virtual bool requestCpusetBoost(bool enable) override {
        return android::requestCpusetBoost(enable, mClientToken);
    }

protected:
    virtual ~SystemCallbackImpl() {}

private:
    DISALLOW_EVIL_CONSTRUCTORS(SystemCallbackImpl);// 定义拷贝构造函数和赋值拷贝构造函数为私有,防止对象赋值
    sp<IBinder> mClientToken;
};

从以上实现来看,主要是 BatteryNotifier 这个类的单例代理完成了功能。BatteryNotifier 是 media 模块的工具类, 主要功能是在 mediaserver 进程中记录电池寿命事件的类。详细分析见《BatteryNotifier 分析》

有个特别的函数需要说明一下 requestCpusetBoost() 实现在

frameworks/av/media/utils/SchedulingPolicyService.cpp

static sp<ISchedulingPolicyService> sSchedulingPolicyService;
static const String16 _scheduling_policy("scheduling_policy");
static Mutex sMutex;

int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous)
{
    // FIXME merge duplicated code related to service lookup, caching, and error recovery
    int ret;
    for (;;) {
        sMutex.lock();
        sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
        sMutex.unlock();
        if (sps == 0) {
            sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
            if (binder == 0) {
                sleep(1);
                continue;
            }
            sps = interface_cast<ISchedulingPolicyService>(binder);
            sMutex.lock();
            sSchedulingPolicyService = sps;
            sMutex.unlock();
        }
        ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous);
        if (ret != DEAD_OBJECT) {
            break;
        }
        ALOGW("SchedulingPolicyService died");
        sMutex.lock();
        sSchedulingPolicyService.clear();
        sMutex.unlock();
    }
    return ret;
}

int requestCpusetBoost(bool enable, const sp<IBinder> &client)
{
    int ret;
    sMutex.lock();
    sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
    sMutex.unlock();
    if (sps == 0) {
        sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
        if (binder == 0) {
            return DEAD_OBJECT;
        }
        sps = interface_cast<ISchedulingPolicyService>(binder);
        sMutex.lock();
        sSchedulingPolicyService = sps;
        sMutex.unlock();
    }
    // 调用 SchedulingPolicyService 服务的 requestCpusetBoost 函数完成。
    ret = sps->requestCpusetBoost(enable, client);
    if (ret != DEAD_OBJECT) {
        return ret;
    }
    ALOGW("SchedulingPolicyService died");
    sMutex.lock();
    sSchedulingPolicyService.clear();
    sMutex.unlock();
    return ret;
}

关于 SchedulingPolicyService 的详细分析,见《SchedulingPolicyService 分析》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智者向内寻求力量

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值