1. 系统架构与核心组件

核心功能定位:子系统可感知并分发应用启动 / 退出、亮灭屏等系统事件,支持以插件形式接入资源调度服务,实现事件获取与资源调度;同时为性能、功耗均衡调度提供决策与执行能力。
关键子模块:智能分组模块 输入:应用前后台切换、用户焦点输入、后台任务执行状态等信息 核心作用:决策进程分组调度策略,支持通过配置映射到不同
CGROUP 分组;向资源调度框架转发应用 / 焦点 / 后台任务状态等事件,供插件订阅

接收事件、决策调度策略、执行调度机制的核心引擎,支持以动态链接插件形式扩展,不同产品可按需加载插件
四大组成部分:
事件管理器:通过对外接口直接感知、监听形式感知系统事件
应用智能分组:接收应用生命周期变更事件,决策应用分组优先级(全局资源调度根本依据)
插件管理器:加载产品对应资源调度插件,接收事件并按插件订阅情况分发
SOC 统一调频服务:从 SOC 统一调频插件接收调频事件,进行调频仲裁,通过内核接口设置 CPU 频率策略
已知插件:
内置服务插件:SOC 统一调频插件(服务包含于 resource_schedule_service)
外部仓服务插件:智能感知调度插件、设备状态管理插件(服务在其它仓,均按系统事件设调度策略至内核实施)

1.1 整体架构图

Data Sources
Plugin System
Resource Schedule Executor (1918)
Resource Schedule Service (1901)
System Events
App States
User Behavior
DeviceStandbyPlugin
FrameAwarePlugin
SocPerfPlugin
CGroup Sched Plugin
ResSchedExeService
PluginExecutorMgr
SocPerfExecutorPlugin
ResSchedService
PluginMgr
SceneRecognizerMgr
ResSchedSystemloadNotifierStub

1.2 核心组件关系

uses
uses
IPC communication
ResSchedService
+ReportData(resType, value, payload) : ErrCode
+ReportSyncEvent(resType, value, payload, reply, resultValue) : ErrCode
+KillProcess(payload, resultValue) : ErrCode
+RegisterSystemloadNotifier(notifier) : ErrCode
+GetSystemloadLevel(resultValue) : ErrCode
+Dump(fd, args) : int32_t
ResSchedClient
+GetInstance()
+ReportData(resType, value, mapPayload) : void
+ReportSyncEvent(resType, value, payload, reply) : int32_t
+KillProcess(mapPayload) : int32_t
+RegisterSystemloadNotifier(callbackObj) : void
+GetSystemloadLevel() : int32_t
PluginMgr
+Init() : bool
+DispatchResource(resData) : void
+Disable() : void
SceneRecognizerMgr
+DispatchResource(resData) : void
+SetListFlingTimeoutTime(value) : void

2. 核心数据结构

2.1 事件类型定义

文件: foundation/resourceschedule/resource_schedule_service/ressched/interfaces/innerkits/ressched_client/include/res_type.h

namespace OHOS::ResourceSchedule::ResType {
enum : uint32_t {
    RES_TYPE_SCREEN_STATUS = 0,           // 屏幕状态:0=关屏, 1=亮屏
    RES_TYPE_APP_STATE_CHANGE = 1,      // 应用状态变化:payload包含pid,uid,bundleName
    RES_TYPE_ABILITY_STATE_CHANGE = 2,  // Ability状态变化
    RES_TYPE_EXTENSION_STATE_CHANGE = 3,  // Extension状态变化
    RES_TYPE_PROCESS_STATE_CHANGE = 4,    // 进程状态:0=创建, 1=死亡
    RES_TYPE_WINDOW_FOCUS = 5,           // 窗口焦点:0=获得焦点, 1=失去焦点
    RES_TYPE_TRANSIENT_TASK = 6,         // 临时任务:0=开始, 1=结束
    RES_TYPE_CONTINUOUS_TASK = 7,        // 持续任务:0=开始, 1=结束
    RES_TYPE_CGROUP_ADJUSTER = 8,        // CGroup调整
    RES_TYPE_CLICK_RECOGNIZE = 9,        // 点击识别:1=触摸事件, 2=点击事件
    RES_TYPE_PUSH_PAGE = 10,             // 页面跳转:0=开始, 1=完成
    RES_TYPE_SLIDE_RECOGNIZE = 11,       // 滑动识别:3=开始, 4=结束
    // ... 更多类型,完整定义83个事件类型
};
}

2.2 资源值定义

文件: foundation/resourceschedule/resource_schedule_service/ressched/interfaces/innerkits/ressched_client/include/res_value.h

namespace OHOS::ResourceSchedule::ResType {
// 屏幕状态
enum ScreenStatus : int64_t {
    SCREEN_OFF = 0,
    SCREEN_ON = 1,
};

// 应用安装状态
enum AppInstallStatus : int64_t {
    APP_UNINSTALL = 0,
    APP_INSTALL_END = 1,
    APP_CHANGED = 2,
    APP_REPLACED = 3,
    APP_FULLY_REMOVED = 4,
    BUNDLE_REMOVED = 5,
    APP_INSTALL_START = 6
};

// 进程状态
enum ProcessStatus : int64_t {
    PROCESS_CREATED = 0,
    PROCESS_READY,
    PROCESS_FOREGROUND,
    PROCESS_FOCOUS,
    PROCESS_BACKGROUND,
    PROCESS_DIED,
};

// 滑动事件状态
enum SlideEventStatus : int64_t {
    SLIDE_EVENT_OFF = 0,
    SLIDE_EVENT_ON = 1,
    SLIDE_EVENT_DETECTING = 2,
    SLIDE_NORMAL_BEGIN = 3,
    SLIDE_NORMAL_END = 4,
    AUTO_PLAY_ON = 5,
    AUTO_PLAY_OFF = 6,
    MOVE_EVENT_ON = 7,
};
}

2.3 客户端接口

文件: foundation/resourceschedule/resource_schedule_service/ressched/interfaces/innerkits/ressched_client/include/res_sched_client.h

class ResSchedClient {
public:
    static ResSchedClient& GetInstance();
    
    // 异步上报数据
    void ReportData(uint32_t resType, int64_t value, 
                   const std::unordered_map<std::string, std::string>& mapPayload);
    
    // 同步上报事件
    int32_t ReportSyncEvent(uint32_t resType, int64_t value, 
                           const nlohmann::json& payload, nlohmann::json& reply);
    
    // 进程管理
    int32_t KillProcess(const std::unordered_map<std::string, std::string>& mapPayload);
    
    // 系统负载监听
    void RegisterSystemloadNotifier(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
    int32_t GetSystemloadLevel();
    
    // 事件监听
    void RegisterEventListener(const sptr<ResSchedEventListener>& eventListener, 
                              uint32_t eventType, uint32_t listenerGroup = 1);
};

3. 插件系统实现

3.1 插件基类定义

文件: foundation/resourceschedule/resource_schedule_service/ressched/services/resschedmgr/pluginbase/plugin.h

class Plugin {
public:
    virtual ~Plugin() = default;
    virtual void Init() = 0;
    virtual void Disable() = 0;
    virtual void DispatchResource(const std::shared_ptr<ResData>& resData) = 0;
};

3.2 设备待机插件

文件: foundation/resourceschedule/resource_schedule_service/ressched/plugins/device_standby_plugin/include/device_standby_plugin.h

class DeviceStandbyPlugin : public Plugin {
    DECLARE_SINGLE_INSTANCE(DeviceStandbyPlugin)

public:
    void Init() override;
    void Disable() override;
    void DispatchResource(const std::shared_ptr<ResData>& resData) override;

private:
    std::set<uint32_t> resTypes_;  // 监听的事件类型集合
};

3.3 SoC性能插件

文件: foundation/resourceschedule/resource_schedule_service/ressched/plugins/socperf_plugin/include/socperf_plugin.h

class SocPerfPlugin : public Plugin {
    DECLARE_SINGLE_INSTANCE(SocPerfPlugin)

public:
    void Init() override;
    void Disable() override;
    void DispatchResource(const std::shared_ptr<ResData>& resData) override;

private:
    // 应用管理
    std::set<int32_t> focusAppUids_;           // 焦点应用UID集合
    std::set<std::string> keyAppName_;         // 关键应用名称集合
    std::map<int32_t, int32_t> pidToAppTypeMap_; // PID到应用类型映射
    
    // 性能配置
    std::map<int32_t, Frequencies> socperfBatteryConfig_; // 电池容量配置
    
    // 事件处理映射
    std::unordered_map<uint32_t, std::function<void(const std::shared_ptr<ResData>&)>> functionMap;
    
    // 事件处理函数
    void HandleWindowFocus(const std::shared_ptr<ResData>& data);
    void HandleEventClick(const std::shared_ptr<ResData>& data);
    void HandleEventSlide(const std::shared_ptr<ResData>& data);
    void HandleAppAbilityStart(const std::shared_ptr<ResData>& data);
    bool HandleGameBoost(const std::shared_ptr<ResData>& data);
};

3.4 帧感知插件

文件: foundation/resourceschedule/resource_schedule_service/ressched/plugins/frame_aware_plugin/include/frame_aware_plugin.h

class FrameAwarePlugin : public Plugin {
    DECLARE_SINGLE_INSTANCE(FrameAwarePlugin)

public:
    void Init() override;
    void Disable() override;
    void DispatchResource(const std::shared_ptr<ResData>& resData) override;

private:
    std::set<uint32_t> resTypes_;  // 监听的事件类型
    std::unordered_map<uint32_t, std::function<void(const std::shared_ptr<ResData>&)>> functionMap;
};

4. 场景识别系统

4.1 场景识别管理器

文件: foundation/resourceschedule/resource_schedule_service/ressched/scene_recognize/include/scene_recognizer_mgr.h

class SceneRecognizerMgr {
    DECLARE_SINGLE_INSTANCE_BASE(SceneRecognizerMgr)

public:
    SceneRecognizerMgr();
    ~SceneRecognizerMgr();
    void DispatchResource(const std::shared_ptr<ResData>& resData);
    
    // 配置参数
    void SetListFlingTimeoutTime(int64_t value);
    void SetListFlingEndTime(int64_t value);
    void SetListFlingSpeedLimit(float value);

private:
    std::unordered_map<uint32_t, std::shared_ptr<SceneRecognizerBase>> sceneRecognizers_;
    std::shared_ptr<ffrt::queue> ffrtQueue_;  // 异步任务队列
};

4.2 场景识别类型

文件: foundation/resourceschedule/resource_schedule_service/ressched/scene_recognize/include/scene_recognizer_base.h

enum RecognizeType : uint32_t {
    CONTINUOUS_APP_INSTALL_RECOGNIZER,           // 连续应用安装识别
    SYSTEM_UPGRADE_SCENE_RECOGNIZER,            // 系统升级场景识别
    BACKGROUND_SENSITIVE_TASK_OVERLAPPING_SCENE_RECOGNIZER,  // 后台敏感任务重叠识别
    SLIDE_RECOGNIZER,                           // 滑动识别
};

5. 配置系统

5.1 SA配置文件

文件: foundation/resourceschedule/resource_schedule_service/ressched/sa_profile/1901.json

{
    "process": "resource_schedule_service",
    "systemability": [
        {
            "name": 1901,
            "libpath": "libresschedsvc.z.so",
            "run-on-create": true,
            "distributed": false,
            "dump_level": 1
        }
    ]
}

5.2 执行器SA配置

文件: foundation/resourceschedule/resource_schedule_service/ressched_executor/sa_profile/1918.json

{
    "process": "resource_schedule_executor",
    "systemability": [
        {
            "name": 1918,
            "libpath": "libresschedexeservice.z.so",
            "run-on-create": true,
            "distributed": false,
            "dump_level": 1
        }
    ]
}

5.3 系统初始化配置

文件: foundation/resourceschedule/resource_schedule_service/ressched/etc/init/resource_schedule_service.cfg

{
    "services" : [{
        "name" : "resource_schedule_service",
        "path" : "/system/bin/sa_main",
        "uid" : "system",
        "gid" : ["system", "shell"],
        "secon" : "u:r:resource_schedule_service:s0",
        "permission" : ["ohos.permission.RESOURCE_SCHEDULE"],
        "importance" : 1,
        "critical" : 0,
        "cpulevel" : 0,
        "d-cpu" : 0,
        "iolevel" : 0,
        "d-io" : 0,
        "start-mode" : "condition",
        "wait" : 1
    }]
}

6. 函数调用流程

6.1 事件上报流程

App ResSchedClient ResSchedService PluginMgr Plugin ReportData(resType, value, payload) IPC调用 DispatchResource(resData) DispatchResource(resData) 处理对应事件 App ResSchedClient ResSchedService PluginMgr Plugin

6.2 场景识别流程

EventSource SceneRecognizerMgr SceneRecognizer ResSchedService DispatchResource(resData) 提交异步任务 识别场景 回调处理结果 EventSource SceneRecognizerMgr SceneRecognizer ResSchedService

7. 使用示例

7.1 客户端事件上报

// 正确的客户端使用方式
#include "res_sched_client.h"
#include "res_type.h"

void ReportAppState() {
    OHOS::ResourceSchedule::ResSchedClient& client = 
        OHOS::ResourceSchedule::ResSchedClient::GetInstance();
    
    std::unordered_map<std::string, std::string> payload;
    payload["pid"] = std::to_string(getpid());
    payload["uid"] = std::to_string(getuid());
    payload["bundleName"] = "com.example.app";
    
    // 上报应用状态变化
    client.ReportData(
        OHOS::ResourceSchedule::ResType::RES_TYPE_APP_STATE_CHANGE,
        OHOS::ResourceSchedule::ResType::PROCESS_FOREGROUND,
        payload
    );
}

void ReportClickEvent() {
    OHOS::ResourceSchedule::ResSchedClient& client = 
        OHOS::ResourceSchedule::ResSchedClient::GetInstance();
    
    std::unordered_map<std::string, std::string> payload;
    payload["pid"] = std::to_string(getpid());
    payload["uid"] = std::to_string(getuid());
    payload["bundleName"] = "com.example.app";
    
    // 上报点击事件
    client.ReportData(
        OHOS::ResourceSchedule::ResType::RES_TYPE_CLICK_RECOGNIZE,
        OHOS::ResourceSchedule::ResType::CLICK_EVENT,
        payload
    );
}

7.2 同步事件处理

void ReportSyncEventWithReply() {
    OHOS::ResourceSchedule::ResSchedClient& client = 
        OHOS::ResourceSchedule::ResSchedClient::GetInstance();
    
    nlohmann::json payload;
    payload["pid"] = getpid();
    payload["uid"] = getuid();
    payload["bundleName"] = "com.example.app";
    
    nlohmann::json reply;
    int32_t result = client.ReportSyncEvent(
        OHOS::ResourceSchedule::ResType::RES_TYPE_APP_STATE_CHANGE,
        1,  // value
        payload,
        reply
    );
    
    if (result == 0) {
        // 处理返回结果
        std::cout << "Sync event processed successfully" << std::endl;
    }
}

7.3 系统负载监听

class MySystemloadObserver : public OHOS::ResourceSchedule::ResSchedSystemloadNotifierClient {
public:
    void OnSystemloadLevel(int32_t level) override {
        switch (level) {
            case 0:
                // 系统负载低
                EnableHighPerformance();
                break;
            case 1:
                // 系统负载中等
                EnableBalancedMode();
                break;
            case 2:
                // 系统负载高
                EnablePowerSaveMode();
                break;
        }
    }
};

void RegisterSystemloadObserver() {
    OHOS::ResourceSchedule::ResSchedClient& client = 
        OHOS::ResourceSchedule::ResSchedClient::GetInstance();
    
    sptr<MySystemloadObserver> observer = new MySystemloadObserver();
    client.RegisterSystemloadNotifier(observer);
}

8. 调试与诊断

8.1 使用hidumper查看状态

# 查看资源调度服务状态
hidumper -s 1901 -a -s

# 查看系统负载等级
hidumper -s 1901 -a -l

# 查看插件信息
hidumper -s 1901 -a -p

# 查看事件统计
hidumper -s 1901 -a -e

8.2 日志分析

# 查看资源调度服务日志
hilog | grep resource_schedule_service

# 查看场景识别日志
hilog | grep SceneRecognizer

# 查看插件处理日志
hilog | grep Plugin

8.3 调试接口

文件: ressched/services/resschedservice/src/res_sched_service.cpp

// 服务提供的调试接口
void ResSchedService::DumpSystemLoadInfo(std::string &result);
void ResSchedService::DumpAllPluginConfig(std::string &result);
void ResSchedService::DumpAllInfo(std::string &result);

9. 错误码与处理

9.1 系统错误码

enum ErrCode {
    ERR_OK = 0,                    // 成功
    ERR_NO_MEMORY = -1,          // 内存不足
    ERR_INVALID_PARAM = -2,      // 参数无效
    ERR_INVALID_OPERATION = -3,  // 操作无效
    ERR_PERMISSION_DENIED = -4,  // 权限不足
};

9.2 常见错误处理

int32_t result = client.ReportSyncEvent(...);
switch (result) {
    case OHOS::ResourceSchedule::ERR_OK:
        // 处理成功
        break;
    case OHOS::ResourceSchedule::ERR_INVALID_PARAM:
        // 参数错误,检查输入
        break;
    case OHOS::ResourceSchedule::ERR_PERMISSION_DENIED:
        // 权限不足,检查权限配置
        break;
    default:
        // 其他错误
        break;
}

10. 性能优化建议

10.1 事件上报优化

// 批量上报优化
class BatchEventReporter {
private:
    static constexpr int MAX_BATCH_SIZE = 10;
    std::vector<std::pair<uint32_t, int64_t>> eventBuffer;
    std::mutex bufferMutex;
    
public:
    void ReportEvent(uint32_t resType, int64_t value) {
        std::lock_guard<std::mutex> lock(bufferMutex);
        eventBuffer.emplace_back(resType, value);
        
        if (eventBuffer.size() >= MAX_BATCH_SIZE) {
            FlushEvents();
        }
    }
    
    void FlushEvents() {
        auto& client = OHOS::ResourceSchedule::ResSchedClient::GetInstance();
        for (const auto& event : eventBuffer) {
            client.ReportData(event.first, event.second, {});
        }
        eventBuffer.clear();
    }
};

10.2 资源监听优化

// 智能监听器管理
class SmartEventListener : public OHOS::ResourceSchedule::ResSchedEventListener {
public:
    void OnReceiveEvent(uint32_t eventType, uint32_t eventValue, 
                       uint32_t listenerGroup, const std::string& extInfo) override {
        // 根据事件类型分发处理
        switch (eventType) {
            case OHOS::ResourceSchedule::ResType::RES_TYPE_APP_STATE_CHANGE:
                HandleAppStateChange(eventValue, extInfo);
                break;
            case OHOS::ResourceSchedule::ResType::RES_TYPE_CLICK_RECOGNIZE:
                HandleClickEvent(eventValue, extInfo);
                break;
        }
    }
};
Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐