13c3173acSopenharmony_ci/*
23c3173acSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
33c3173acSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43c3173acSopenharmony_ci * you may not use this file except in compliance with the License.
53c3173acSopenharmony_ci * You may obtain a copy of the License at
63c3173acSopenharmony_ci *
73c3173acSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83c3173acSopenharmony_ci *
93c3173acSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103c3173acSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113c3173acSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123c3173acSopenharmony_ci * See the License for the specific language governing permissions and
133c3173acSopenharmony_ci * limitations under the License.
143c3173acSopenharmony_ci */
153c3173acSopenharmony_ci
163c3173acSopenharmony_ci#include <fcntl.h>
173c3173acSopenharmony_ci#include <securec.h>
183c3173acSopenharmony_ci#include <sys/ioctl.h>
193c3173acSopenharmony_ci#include <sys/resource.h>
203c3173acSopenharmony_ci#include <unistd.h>
213c3173acSopenharmony_ci#include <hitrace_meter.h>
223c3173acSopenharmony_ci#include <linux/sched.h>
233c3173acSopenharmony_ci#include "accesstoken_kit.h"
243c3173acSopenharmony_ci#include "concurrent_task_log.h"
253c3173acSopenharmony_ci#include "rtg_interface.h"
263c3173acSopenharmony_ci#include "ipc_skeleton.h"
273c3173acSopenharmony_ci#include "parameters.h"
283c3173acSopenharmony_ci#include "concurrent_task_controller.h"
293c3173acSopenharmony_ci
303c3173acSopenharmony_ciusing namespace OHOS::RME;
313c3173acSopenharmony_ciusing namespace OHOS::Security::AccessToken;
323c3173acSopenharmony_ci
333c3173acSopenharmony_cinamespace OHOS {
343c3173acSopenharmony_cinamespace ConcurrentTask {
353c3173acSopenharmony_cinamespace {
363c3173acSopenharmony_ci    const std::string INTERVAL_DDL = "persist.ffrt.interval.renderthread";
373c3173acSopenharmony_ci    const std::string INTERVAL_APP_RATE = "persist.ffrt.interval.appRate";
383c3173acSopenharmony_ci    const std::string INTERVAL_RS_RATE = "persist.ffrt.interval.rsRate";
393c3173acSopenharmony_ci    const std::string CONFIG_FILE_NAME = "etc/qos_manager/qos_manager_config.xml";
403c3173acSopenharmony_ci    constexpr int CURRENT_RATE = 120;
413c3173acSopenharmony_ci    constexpr int PARAM_TYPE = 1;
423c3173acSopenharmony_ci    constexpr int UNI_APP_RATE_ID = -1;
433c3173acSopenharmony_ci    const char RTG_SCHED_IPC_MAGIC = 0xAB;
443c3173acSopenharmony_ci    constexpr int RTG_TYPE_MAX = 3;
453c3173acSopenharmony_ci    constexpr int RS_UID = 1003;
463c3173acSopenharmony_ci    constexpr int EXECUTOR_LIMIT_NUM = 3;
473c3173acSopenharmony_ci}
483c3173acSopenharmony_ci
493c3173acSopenharmony_ci#define CMD_ID_SET_RTG \
503c3173acSopenharmony_ci    _IOWR(RTG_SCHED_IPC_MAGIC, SET_RTG, struct rtg_str_data)
513c3173acSopenharmony_ci
523c3173acSopenharmony_ciTaskController& TaskController::GetInstance()
533c3173acSopenharmony_ci{
543c3173acSopenharmony_ci    static TaskController instance;
553c3173acSopenharmony_ci    return instance;
563c3173acSopenharmony_ci}
573c3173acSopenharmony_ci
583c3173acSopenharmony_civoid TaskController::RequestAuth(const Json::Value& payload)
593c3173acSopenharmony_ci{
603c3173acSopenharmony_ci    {
613c3173acSopenharmony_ci        std::lock_guard<std::mutex> autolock(configReaderMutex_);
623c3173acSopenharmony_ci        if (!configEnable_ && !ConfigReaderInit()) {
633c3173acSopenharmony_ci            return;
643c3173acSopenharmony_ci        }
653c3173acSopenharmony_ci    }
663c3173acSopenharmony_ci    pid_t uid = IPCSkeleton::GetInstance().GetCallingUid();
673c3173acSopenharmony_ci    auto bundleName = GetProcessNameByToken();
683c3173acSopenharmony_ci    if (configReader_->IsBundleNameAuth(bundleName) || configReader_->IsUidAuth(uid)) {
693c3173acSopenharmony_ci        pid_t pid = IPCSkeleton::GetInstance().GetCallingPid();
703c3173acSopenharmony_ci        AuthSystemProcess(pid);
713c3173acSopenharmony_ci        return;
723c3173acSopenharmony_ci    }
733c3173acSopenharmony_ci    CONCUR_LOGE("Invalid uid %{public}d, can't call RequestAuth", uid);
743c3173acSopenharmony_ci}
753c3173acSopenharmony_ci
763c3173acSopenharmony_civoid TaskController::ReportData(uint32_t resType, int64_t value, const Json::Value& payload)
773c3173acSopenharmony_ci{
783c3173acSopenharmony_ci    pid_t uid = IPCSkeleton::GetInstance().GetCallingUid();
793c3173acSopenharmony_ci    if (GetProcessNameByToken() != RESOURCE_SCHEDULE_PROCESS_NAME) {
803c3173acSopenharmony_ci        CONCUR_LOGE("Invalid uid %{public}d, only RSS can call ReportData", uid);
813c3173acSopenharmony_ci        return;
823c3173acSopenharmony_ci    }
833c3173acSopenharmony_ci    if (!CheckJsonValid(payload)) {
843c3173acSopenharmony_ci        return;
853c3173acSopenharmony_ci    }
863c3173acSopenharmony_ci    std::string strRequstType = "";
873c3173acSopenharmony_ci    try {
883c3173acSopenharmony_ci        strRequstType = payload["type"].asString();
893c3173acSopenharmony_ci    } catch (...) {
903c3173acSopenharmony_ci        CONCUR_LOGE("Unexpected type format");
913c3173acSopenharmony_ci        return;
923c3173acSopenharmony_ci    }
933c3173acSopenharmony_ci    if (strRequstType.length() == 0) {
943c3173acSopenharmony_ci        CONCUR_LOGE("Get payload type err");
953c3173acSopenharmony_ci        return;
963c3173acSopenharmony_ci    }
973c3173acSopenharmony_ci    int requstType = GetRequestType(strRequstType);
983c3173acSopenharmony_ci    DealSystemRequest(requstType, payload);
993c3173acSopenharmony_ci    PrintInfo();
1003c3173acSopenharmony_ci}
1013c3173acSopenharmony_ci
1023c3173acSopenharmony_civoid TaskController::QueryInterval(int queryItem, IntervalReply& queryRs)
1033c3173acSopenharmony_ci{
1043c3173acSopenharmony_ci    pid_t uid = IPCSkeleton::GetInstance().GetCallingUid();
1053c3173acSopenharmony_ci    pid_t pid = IPCSkeleton::GetInstance().GetCallingPid();
1063c3173acSopenharmony_ci    switch (queryItem) {
1073c3173acSopenharmony_ci        case QUERY_UI:
1083c3173acSopenharmony_ci            QueryUi(uid, queryRs);
1093c3173acSopenharmony_ci            break;
1103c3173acSopenharmony_ci        case QUERY_RENDER:
1113c3173acSopenharmony_ci            QueryRender(uid, queryRs);
1123c3173acSopenharmony_ci            break;
1133c3173acSopenharmony_ci        case QUERY_RENDER_SERVICE:
1143c3173acSopenharmony_ci            QueryRenderService(uid, queryRs);
1153c3173acSopenharmony_ci            break;
1163c3173acSopenharmony_ci        case QUERY_RENDER_SERVICE_MAIN:
1173c3173acSopenharmony_ci            QueryRenderServiceMain(uid, pid, queryRs);
1183c3173acSopenharmony_ci            break;
1193c3173acSopenharmony_ci        case QUERY_RENDER_SERVICE_RENDER:
1203c3173acSopenharmony_ci            QueryRenderServiceRender(uid, pid, queryRs);
1213c3173acSopenharmony_ci            break;
1223c3173acSopenharmony_ci        case QUERY_COMPOSER:
1233c3173acSopenharmony_ci            QueryHwc(uid, queryRs);
1243c3173acSopenharmony_ci            break;
1253c3173acSopenharmony_ci        case QUERY_HARDWARE:
1263c3173acSopenharmony_ci            QueryHardware(uid, pid, queryRs);
1273c3173acSopenharmony_ci            break;
1283c3173acSopenharmony_ci        case QUERY_EXECUTOR_START:
1293c3173acSopenharmony_ci            QueryExecutorStart(uid, pid, queryRs);
1303c3173acSopenharmony_ci            break;
1313c3173acSopenharmony_ci        default:
1323c3173acSopenharmony_ci            break;
1333c3173acSopenharmony_ci    }
1343c3173acSopenharmony_ci}
1353c3173acSopenharmony_ci
1363c3173acSopenharmony_cistd::string TaskController::GetProcessNameByToken()
1373c3173acSopenharmony_ci{
1383c3173acSopenharmony_ci    AccessTokenID tokenID = IPCSkeleton::GetInstance().GetCallingTokenID();
1393c3173acSopenharmony_ci    NativeTokenInfo tokenInfo;
1403c3173acSopenharmony_ci    if (AccessTokenKit::GetNativeTokenInfo(tokenID, tokenInfo) != AccessTokenKitRet::RET_SUCCESS) {
1413c3173acSopenharmony_ci        return "";
1423c3173acSopenharmony_ci    }
1433c3173acSopenharmony_ci    return tokenInfo.processName;
1443c3173acSopenharmony_ci}
1453c3173acSopenharmony_ci
1463c3173acSopenharmony_civoid TaskController::QueryUi(int uid, IntervalReply& queryRs)
1473c3173acSopenharmony_ci{
1483c3173acSopenharmony_ci    pid_t pid = IPCSkeleton::GetInstance().GetCallingPid();
1493c3173acSopenharmony_ci    ForegroundAppRecord* record = GetRecordOfPid(pid);
1503c3173acSopenharmony_ci    if (!record) {
1513c3173acSopenharmony_ci        CONCUR_LOGD("Query ui with pid %{public}d failed", pid);
1523c3173acSopenharmony_ci        return;
1533c3173acSopenharmony_ci    }
1543c3173acSopenharmony_ci    int grpId = record->GetGrpId();
1553c3173acSopenharmony_ci    if (grpId <= 0) {
1563c3173acSopenharmony_ci        CONCUR_LOGI("%{public}d Query ui with none grpid", pid);
1573c3173acSopenharmony_ci        queryRs.rtgId = -1;
1583c3173acSopenharmony_ci    } else {
1593c3173acSopenharmony_ci        queryRs.rtgId = grpId;
1603c3173acSopenharmony_ci    }
1613c3173acSopenharmony_ci    queryRs.bundleName = appBundleName[pid];
1623c3173acSopenharmony_ci}
1633c3173acSopenharmony_ci
1643c3173acSopenharmony_civoid TaskController::QueryRender(int uid, IntervalReply& queryRs)
1653c3173acSopenharmony_ci{
1663c3173acSopenharmony_ci    pid_t pid = IPCSkeleton::GetInstance().GetCallingPid();
1673c3173acSopenharmony_ci    ForegroundAppRecord* record = GetRecordOfPid(pid);
1683c3173acSopenharmony_ci    if (!record) {
1693c3173acSopenharmony_ci        CONCUR_LOGD("Query render with pid %{public}d failed", pid);
1703c3173acSopenharmony_ci        return;
1713c3173acSopenharmony_ci    }
1723c3173acSopenharmony_ci    int grpId = record->GetGrpId();
1733c3173acSopenharmony_ci    if (grpId <= 0) {
1743c3173acSopenharmony_ci        CONCUR_LOGI("%{public}d Query render with none grpid", pid);
1753c3173acSopenharmony_ci        queryRs.rtgId = -1;
1763c3173acSopenharmony_ci    } else {
1773c3173acSopenharmony_ci        queryRs.rtgId = grpId;
1783c3173acSopenharmony_ci    }
1793c3173acSopenharmony_ci}
1803c3173acSopenharmony_ci
1813c3173acSopenharmony_civoid TaskController::QueryRenderService(int uid, IntervalReply& queryRs)
1823c3173acSopenharmony_ci{
1833c3173acSopenharmony_ci    if (uid != RS_UID) {
1843c3173acSopenharmony_ci        return;
1853c3173acSopenharmony_ci    }
1863c3173acSopenharmony_ci    int queryTid = queryRs.tid;
1873c3173acSopenharmony_ci    if (renderServiceMainGrpId_ <= 0) {
1883c3173acSopenharmony_ci        TryCreateRSMainGrp();
1893c3173acSopenharmony_ci        CONCUR_LOGI("uid %{public}d query rs group failed and create %{public}d.", uid, renderServiceMainGrpId_);
1903c3173acSopenharmony_ci        if (renderServiceMainGrpId_ <= 0) {
1913c3173acSopenharmony_ci            CONCUR_LOGE("uid %{public}d create rs group failed", uid);
1923c3173acSopenharmony_ci            return;
1933c3173acSopenharmony_ci        }
1943c3173acSopenharmony_ci    }
1953c3173acSopenharmony_ci
1963c3173acSopenharmony_ci    queryRs.rtgId = renderServiceMainGrpId_;
1973c3173acSopenharmony_ci    if (queryTid <= 0) {
1983c3173acSopenharmony_ci        return;
1993c3173acSopenharmony_ci    }
2003c3173acSopenharmony_ci    list<int>::iterator iter = find(rsThreads_.begin(), rsThreads_.end(), queryTid);
2013c3173acSopenharmony_ci    if (iter != rsThreads_.end()) {
2023c3173acSopenharmony_ci        return;
2033c3173acSopenharmony_ci    }
2043c3173acSopenharmony_ci    queryRs.rtgId = renderServiceMainGrpId_;
2053c3173acSopenharmony_ci    int ret = AddThreadToRtg(queryTid, renderServiceMainGrpId_, PRIO_RT);
2063c3173acSopenharmony_ci    if (ret < 0) {
2073c3173acSopenharmony_ci        CONCUR_LOGE("uid %{public}d tid %{public}d join rs group failed", uid, queryTid);
2083c3173acSopenharmony_ci        return;
2093c3173acSopenharmony_ci    }
2103c3173acSopenharmony_ci    CONCUR_LOGI("uid %{public}d tid %{public}d join rs group success in Query", uid, queryTid);
2113c3173acSopenharmony_ci    SetFrameRateAndPrioType(renderServiceMainGrpId_, CURRENT_RATE, PARAM_TYPE);
2123c3173acSopenharmony_ci}
2133c3173acSopenharmony_ci
2143c3173acSopenharmony_civoid TaskController::QueryRenderServiceMain(int uid, int pid, IntervalReply& queryRs)
2153c3173acSopenharmony_ci{
2163c3173acSopenharmony_ci    if (uid != RS_UID) {
2173c3173acSopenharmony_ci        return;
2183c3173acSopenharmony_ci    }
2193c3173acSopenharmony_ci    if (authedRSPid_ != pid) {
2203c3173acSopenharmony_ci        if (AuthSystemProcess(pid) != 0) {
2213c3173acSopenharmony_ci            return;
2223c3173acSopenharmony_ci        }
2233c3173acSopenharmony_ci        authedRSPid_ = pid;
2243c3173acSopenharmony_ci    }
2253c3173acSopenharmony_ci    if (renderServiceMainGrpId_ <= 0) {
2263c3173acSopenharmony_ci        TryCreateRSMainGrp();
2273c3173acSopenharmony_ci        CONCUR_LOGI("uid %{public}d query rs group failed and create %{public}d.", uid, renderServiceMainGrpId_);
2283c3173acSopenharmony_ci        if (renderServiceMainGrpId_ <= 0) {
2293c3173acSopenharmony_ci            CONCUR_LOGE("uid %{public}d create rs group failed", uid);
2303c3173acSopenharmony_ci            return;
2313c3173acSopenharmony_ci        }
2323c3173acSopenharmony_ci    }
2333c3173acSopenharmony_ci    queryRs.rtgId = renderServiceMainGrpId_;
2343c3173acSopenharmony_ci    if (renderServiceMainTid_ <= 0) {
2353c3173acSopenharmony_ci        renderServiceMainTid_ = queryRs.tid;
2363c3173acSopenharmony_ci        int ret = AddThreadToRtg(renderServiceMainTid_, renderServiceMainGrpId_, PRIO_RT);
2373c3173acSopenharmony_ci        if (ret < 0) {
2383c3173acSopenharmony_ci            CONCUR_LOGE("uid %{public}d tid %{public}d join rs group failed.", uid, renderServiceMainTid_);
2393c3173acSopenharmony_ci        }
2403c3173acSopenharmony_ci    }
2413c3173acSopenharmony_ci    SetFrameRateAndPrioType(renderServiceMainGrpId_, CURRENT_RATE, PARAM_TYPE);
2423c3173acSopenharmony_ci}
2433c3173acSopenharmony_ci
2443c3173acSopenharmony_civoid TaskController::QueryRenderServiceRender(int uid, int pid, IntervalReply& queryRs)
2453c3173acSopenharmony_ci{
2463c3173acSopenharmony_ci    if (uid != RS_UID) {
2473c3173acSopenharmony_ci        return;
2483c3173acSopenharmony_ci    }
2493c3173acSopenharmony_ci    if (renderServiceRenderGrpId_ <= 0) {
2503c3173acSopenharmony_ci        TryCreateRSRenderGrp();
2513c3173acSopenharmony_ci        if (renderServiceRenderGrpId_ <= 0) {
2523c3173acSopenharmony_ci            CONCUR_LOGE("uid %{public}d create rs group failed", uid);
2533c3173acSopenharmony_ci            return;
2543c3173acSopenharmony_ci        }
2553c3173acSopenharmony_ci    }
2563c3173acSopenharmony_ci    queryRs.rtgId = renderServiceRenderGrpId_;
2573c3173acSopenharmony_ci    if (renderServiceRenderTid_ <= 0 || renderServiceRenderTid_ != queryRs.tid) {
2583c3173acSopenharmony_ci        renderServiceRenderTid_ = queryRs.tid;
2593c3173acSopenharmony_ci        int ret = AddThreadToRtg(renderServiceRenderTid_, renderServiceRenderGrpId_, PRIO_RT);
2603c3173acSopenharmony_ci        if (ret < 0) {
2613c3173acSopenharmony_ci            CONCUR_LOGE("uid %{public}d tid %{public}d join rs group failed.", uid, renderServiceMainGrpId_);
2623c3173acSopenharmony_ci        }
2633c3173acSopenharmony_ci    }
2643c3173acSopenharmony_ci    SetFrameRateAndPrioType(renderServiceRenderGrpId_, CURRENT_RATE, PARAM_TYPE);
2653c3173acSopenharmony_ci}
2663c3173acSopenharmony_ci
2673c3173acSopenharmony_civoid TaskController::QueryHardware(int uid, int pid, IntervalReply& queryRs)
2683c3173acSopenharmony_ci{
2693c3173acSopenharmony_ci    if (uid != RS_UID) {
2703c3173acSopenharmony_ci        return;
2713c3173acSopenharmony_ci    }
2723c3173acSopenharmony_ci    if (hardwareGrpId_ < 0) {
2733c3173acSopenharmony_ci        return;
2743c3173acSopenharmony_ci    }
2753c3173acSopenharmony_ci    hardwareTid_ = queryRs.tid;
2763c3173acSopenharmony_ci    TryCreateRSMainGrp();
2773c3173acSopenharmony_ci    int ret = AddThreadToRtg(hardwareTid_, renderServiceMainGrpId_, PRIO_RT);
2783c3173acSopenharmony_ci    if (ret < 0) {
2793c3173acSopenharmony_ci        CONCUR_LOGE("uid %{public}d tid %{public}d join hardware group failed.", uid, hardwareTid_);
2803c3173acSopenharmony_ci        return;
2813c3173acSopenharmony_ci    }
2823c3173acSopenharmony_ci    queryRs.rtgId = hardwareGrpId_;
2833c3173acSopenharmony_ci}
2843c3173acSopenharmony_ci
2853c3173acSopenharmony_civoid TaskController::QueryExecutorStart(int uid, int pid, IntervalReply& queryRs)
2863c3173acSopenharmony_ci{
2873c3173acSopenharmony_ci    if (uid != RS_UID) {
2883c3173acSopenharmony_ci        return;
2893c3173acSopenharmony_ci    }
2903c3173acSopenharmony_ci    if (renderServiceMainGrpId_ < 0) {
2913c3173acSopenharmony_ci        return;
2923c3173acSopenharmony_ci    }
2933c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(executorStartLock_);
2943c3173acSopenharmony_ci    if (executorNum_ >= EXECUTOR_LIMIT_NUM) {
2953c3173acSopenharmony_ci        return;
2963c3173acSopenharmony_ci    }
2973c3173acSopenharmony_ci    if (queryRs.tid <= 0) {
2983c3173acSopenharmony_ci        return;
2993c3173acSopenharmony_ci    }
3003c3173acSopenharmony_ci    int ret = AddThreadToRtg(queryRs.tid, renderServiceMainGrpId_, PRIO_RT);
3013c3173acSopenharmony_ci    if (ret < 0) {
3023c3173acSopenharmony_ci        CONCUR_LOGE("uid %{public}d tid %{public}d join executor group failed.", uid, renderServiceMainTid_);
3033c3173acSopenharmony_ci        return;
3043c3173acSopenharmony_ci    }
3053c3173acSopenharmony_ci    executorNum_++;
3063c3173acSopenharmony_ci    queryRs.rtgId = renderServiceMainGrpId_;
3073c3173acSopenharmony_ci}
3083c3173acSopenharmony_ci
3093c3173acSopenharmony_civoid TaskController::QueryHwc(int uid, IntervalReply& queryRs)
3103c3173acSopenharmony_ci{
3113c3173acSopenharmony_ci    pid_t pid = IPCSkeleton::GetInstance().GetCallingPid();
3123c3173acSopenharmony_ci    ForegroundAppRecord* record = GetRecordOfPid(pid);
3133c3173acSopenharmony_ci    if (!record) {
3143c3173acSopenharmony_ci        CONCUR_LOGD("Query ipc thread with pid %{public}d failed", pid);
3153c3173acSopenharmony_ci        return;
3163c3173acSopenharmony_ci    }
3173c3173acSopenharmony_ci    int grpId = record->GetGrpId();
3183c3173acSopenharmony_ci    if (grpId <= 0) {
3193c3173acSopenharmony_ci        CONCUR_LOGI("%{public}d Query ipc thread with none grpid", pid);
3203c3173acSopenharmony_ci        queryRs.rtgId = -1;
3213c3173acSopenharmony_ci    } else {
3223c3173acSopenharmony_ci        queryRs.rtgId = grpId;
3233c3173acSopenharmony_ci    }
3243c3173acSopenharmony_ci}
3253c3173acSopenharmony_ci
3263c3173acSopenharmony_civoid TaskController::Init()
3273c3173acSopenharmony_ci{
3283c3173acSopenharmony_ci    TypeMapInit();
3293c3173acSopenharmony_ci    qosPolicy_.Init();
3303c3173acSopenharmony_ci    TryCreateRsGroup();
3313c3173acSopenharmony_ci
3323c3173acSopenharmony_ci    std::lock_guard<std::mutex> autolock(configReaderMutex_);
3333c3173acSopenharmony_ci    if (!configEnable_) {
3343c3173acSopenharmony_ci        ConfigReaderInit();
3353c3173acSopenharmony_ci    }
3363c3173acSopenharmony_ci}
3373c3173acSopenharmony_ci
3383c3173acSopenharmony_cibool TaskController::ConfigReaderInit()
3393c3173acSopenharmony_ci{
3403c3173acSopenharmony_ci    configReader_ = make_unique<ConfigReader>();
3413c3173acSopenharmony_ci    if (!configReader_) {
3423c3173acSopenharmony_ci        CONCUR_LOGE("configReader_ initialize error!");
3433c3173acSopenharmony_ci        return configEnable_;
3443c3173acSopenharmony_ci    }
3453c3173acSopenharmony_ci
3463c3173acSopenharmony_ci    std::string realPath;
3473c3173acSopenharmony_ci    configReader_->GetRealConfigPath(CONFIG_FILE_NAME.c_str(), realPath);
3483c3173acSopenharmony_ci    if (realPath.empty() || !configReader_->LoadFromConfigFile(realPath)) {
3493c3173acSopenharmony_ci        CONCUR_LOGE("config load failed!");
3503c3173acSopenharmony_ci        return configEnable_;
3513c3173acSopenharmony_ci    }
3523c3173acSopenharmony_ci    configEnable_ = true;
3533c3173acSopenharmony_ci    ddlSceneSchedSwitch_ = configReader_->GetPowerModeSchedSwitch();
3543c3173acSopenharmony_ci    CONCUR_LOGI("deadline scene sched switch = %{public}d", ddlSceneSchedSwitch_);
3553c3173acSopenharmony_ci    return configEnable_;
3563c3173acSopenharmony_ci}
3573c3173acSopenharmony_ci
3583c3173acSopenharmony_civoid TaskController::Release()
3593c3173acSopenharmony_ci{
3603c3173acSopenharmony_ci    msgType_.clear();
3613c3173acSopenharmony_ci    if (renderServiceMainGrpId_ > 0) {
3623c3173acSopenharmony_ci        DestroyRtgGrp(renderServiceMainGrpId_);
3633c3173acSopenharmony_ci        renderServiceMainGrpId_ = -1;
3643c3173acSopenharmony_ci    }
3653c3173acSopenharmony_ci    if (renderServiceRenderGrpId_ > 0) {
3663c3173acSopenharmony_ci        DestroyRtgGrp(renderServiceRenderGrpId_);
3673c3173acSopenharmony_ci        renderServiceRenderGrpId_ = -1;
3683c3173acSopenharmony_ci    }
3693c3173acSopenharmony_ci    ddlSceneSchedSwitch_ = false;
3703c3173acSopenharmony_ci
3713c3173acSopenharmony_ci    std::lock_guard<std::mutex> autolock(configReaderMutex_);
3723c3173acSopenharmony_ci    configReader_ = nullptr;
3733c3173acSopenharmony_ci}
3743c3173acSopenharmony_ci
3753c3173acSopenharmony_civoid TaskController::TypeMapInit()
3763c3173acSopenharmony_ci{
3773c3173acSopenharmony_ci    msgType_.clear();
3783c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("foreground", MSG_FOREGROUND));
3793c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("background", MSG_BACKGROUND));
3803c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("appStart", MSG_APP_START));
3813c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("appKilled", MSG_APP_KILLED));
3823c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("continuousStart", MSG_CONTINUOUS_TASK_START));
3833c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("continuousEnd", MSG_CONTINUOUS_TASK_END));
3843c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("getFocus", MSG_GET_FOCUS));
3853c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("loseFocus", MSG_LOSE_FOCUS));
3863c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("enterInteractionScene", MSG_ENTER_INTERACTION_SCENE));
3873c3173acSopenharmony_ci    msgType_.insert(pair<std::string, int>("exitInteractionScene", MSG_EXIT_INTERACTION_SCENE));
3883c3173acSopenharmony_ci}
3893c3173acSopenharmony_ci
3903c3173acSopenharmony_civoid TaskController::TryCreateRSMainGrp()
3913c3173acSopenharmony_ci{
3923c3173acSopenharmony_ci    if (renderServiceMainGrpId_ == -1) {
3933c3173acSopenharmony_ci        renderServiceMainGrpId_ = TryCreateSystemGroup();
3943c3173acSopenharmony_ci        hardwareGrpId_ = renderServiceMainGrpId_;
3953c3173acSopenharmony_ci    }
3963c3173acSopenharmony_ci}
3973c3173acSopenharmony_ci
3983c3173acSopenharmony_civoid TaskController::TryCreateRSRenderGrp()
3993c3173acSopenharmony_ci{
4003c3173acSopenharmony_ci    if (renderServiceRenderGrpId_ == -1) {
4013c3173acSopenharmony_ci        renderServiceRenderGrpId_ = TryCreateSystemGroup();
4023c3173acSopenharmony_ci    }
4033c3173acSopenharmony_ci}
4043c3173acSopenharmony_ci
4053c3173acSopenharmony_civoid TaskController::TryCreateRsGroup()
4063c3173acSopenharmony_ci{
4073c3173acSopenharmony_ci    TryCreateRSMainGrp();
4083c3173acSopenharmony_ci    TryCreateRSRenderGrp();
4093c3173acSopenharmony_ci}
4103c3173acSopenharmony_ci
4113c3173acSopenharmony_ciint TaskController::TryCreateSystemGroup()
4123c3173acSopenharmony_ci{
4133c3173acSopenharmony_ci    if (!rtgEnabled_) {
4143c3173acSopenharmony_ci        rtgEnabled_ = EnableRtg(true) < 0 ? false : true;
4153c3173acSopenharmony_ci        if (!rtgEnabled_) {
4163c3173acSopenharmony_ci            CONCUR_LOGE("Rtg enable failed");
4173c3173acSopenharmony_ci            return -1;
4183c3173acSopenharmony_ci        }
4193c3173acSopenharmony_ci        CONCUR_LOGI("Enable Rtg");
4203c3173acSopenharmony_ci    }
4213c3173acSopenharmony_ci    int grpId = CreateNewRtgGrp(PRIO_RT, MAX_KEY_THREADS);
4223c3173acSopenharmony_ci    if (grpId <= 0) {
4233c3173acSopenharmony_ci        CONCUR_LOGI("CreateRsRtgGroup with RT failed, try change to normal type.");
4243c3173acSopenharmony_ci        grpId = CreateNewRtgGrp(PRIO_NORMAL, MAX_KEY_THREADS);
4253c3173acSopenharmony_ci    }
4263c3173acSopenharmony_ci    if (grpId <= 0) {
4273c3173acSopenharmony_ci        CONCUR_LOGI("CreateRsRtgGroup failed! rtGrp:%{public}d", grpId);
4283c3173acSopenharmony_ci        return -1;
4293c3173acSopenharmony_ci    }
4303c3173acSopenharmony_ci    return grpId;
4313c3173acSopenharmony_ci}
4323c3173acSopenharmony_ci
4333c3173acSopenharmony_ciint TaskController::GetRequestType(std::string strRequstType)
4343c3173acSopenharmony_ci{
4353c3173acSopenharmony_ci    auto iter = msgType_.find(strRequstType);
4363c3173acSopenharmony_ci    if (iter == msgType_.end()) {
4373c3173acSopenharmony_ci        return MSG_TYPE_MAX;
4383c3173acSopenharmony_ci    }
4393c3173acSopenharmony_ci    return msgType_[strRequstType];
4403c3173acSopenharmony_ci}
4413c3173acSopenharmony_ci
4423c3173acSopenharmony_cibool TaskController::ParsePayload(const Json::Value& payload, int& uid, int& pid, std::string& bundleName)
4433c3173acSopenharmony_ci{
4443c3173acSopenharmony_ci    try {
4453c3173acSopenharmony_ci        uid = stoi(payload["uid"].asString());
4463c3173acSopenharmony_ci        pid = stoi(payload["pid"].asString());
4473c3173acSopenharmony_ci        bundleName = payload["bundleName"].asString();
4483c3173acSopenharmony_ci    } catch(...) {
4493c3173acSopenharmony_ci        CONCUR_LOGE("Unexpected uid or pid format");
4503c3173acSopenharmony_ci        return false;
4513c3173acSopenharmony_ci    }
4523c3173acSopenharmony_ci    if (uid > 0 && pid > 0) {
4533c3173acSopenharmony_ci        return true;
4543c3173acSopenharmony_ci    }
4553c3173acSopenharmony_ci    return false;
4563c3173acSopenharmony_ci}
4573c3173acSopenharmony_ci
4583c3173acSopenharmony_civoid TaskController::DealSystemRequest(int requestType, const Json::Value& payload)
4593c3173acSopenharmony_ci{
4603c3173acSopenharmony_ci    int uid = -1;
4613c3173acSopenharmony_ci    int pid = -1;
4623c3173acSopenharmony_ci    std::string bundleName = "";
4633c3173acSopenharmony_ci    if (!ParsePayload(payload, uid, pid, bundleName)) {
4643c3173acSopenharmony_ci        return;
4653c3173acSopenharmony_ci    }
4663c3173acSopenharmony_ci    switch (requestType) {
4673c3173acSopenharmony_ci        case MSG_FOREGROUND:
4683c3173acSopenharmony_ci            NewForeground(uid, pid);
4693c3173acSopenharmony_ci            break;
4703c3173acSopenharmony_ci        case MSG_BACKGROUND:
4713c3173acSopenharmony_ci            NewBackground(uid, pid);
4723c3173acSopenharmony_ci            break;
4733c3173acSopenharmony_ci        case MSG_APP_START:
4743c3173acSopenharmony_ci            NewAppStart(uid, pid, bundleName);
4753c3173acSopenharmony_ci            break;
4763c3173acSopenharmony_ci        case MSG_APP_KILLED:
4773c3173acSopenharmony_ci            AppKilled(uid, pid);
4783c3173acSopenharmony_ci            break;
4793c3173acSopenharmony_ci        case MSG_CONTINUOUS_TASK_START:
4803c3173acSopenharmony_ci        case MSG_CONTINUOUS_TASK_END:
4813c3173acSopenharmony_ci            ContinuousTaskProcess(uid, pid, requestType);
4823c3173acSopenharmony_ci            break;
4833c3173acSopenharmony_ci        case MSG_GET_FOCUS:
4843c3173acSopenharmony_ci        case MSG_LOSE_FOCUS:
4853c3173acSopenharmony_ci            FocusStatusProcess(uid, pid, requestType);
4863c3173acSopenharmony_ci            break;
4873c3173acSopenharmony_ci        case MSG_ENTER_INTERACTION_SCENE:
4883c3173acSopenharmony_ci        case MSG_EXIT_INTERACTION_SCENE:
4893c3173acSopenharmony_ci            InteractionSceneProcess(requestType);
4903c3173acSopenharmony_ci            break;
4913c3173acSopenharmony_ci        default:
4923c3173acSopenharmony_ci            CONCUR_LOGE("Unknown system request");
4933c3173acSopenharmony_ci            break;
4943c3173acSopenharmony_ci    }
4953c3173acSopenharmony_ci}
4963c3173acSopenharmony_ci
4973c3173acSopenharmony_ciForegroundAppRecord* TaskController::GetRecordOfPid(int pid)
4983c3173acSopenharmony_ci{
4993c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
5003c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
5013c3173acSopenharmony_ci        if (iter->GetPid() == pid) {
5023c3173acSopenharmony_ci            return &*iter;
5033c3173acSopenharmony_ci        }
5043c3173acSopenharmony_ci    }
5053c3173acSopenharmony_ci    return nullptr;
5063c3173acSopenharmony_ci}
5073c3173acSopenharmony_ci
5083c3173acSopenharmony_civoid TaskController::NewForeground(int uid, int pid)
5093c3173acSopenharmony_ci{
5103c3173acSopenharmony_ci    int uiTid = pid;
5113c3173acSopenharmony_ci    auto it = find(authApps_.begin(), authApps_.end(), pid);
5123c3173acSopenharmony_ci    if (it == authApps_.end()) {
5133c3173acSopenharmony_ci        CONCUR_LOGI("un-authed pid %{public}d", pid);
5143c3173acSopenharmony_ci        return;
5153c3173acSopenharmony_ci    }
5163c3173acSopenharmony_ci    int ret = AuthGet(pid);
5173c3173acSopenharmony_ci    if (ret != static_cast<int>(AuthStatus::AUTH_STATUS_FOCUS)) {
5183c3173acSopenharmony_ci        CONCUR_LOGI("pid %{public}d change to foreground.", pid);
5193c3173acSopenharmony_ci        unsigned int pidParam = static_cast<unsigned int>(pid);
5203c3173acSopenharmony_ci        unsigned int uaFlag = AF_RTG_APP;
5213c3173acSopenharmony_ci        unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_FOREGROUND);
5223c3173acSopenharmony_ci        int ret = AuthEnable(pidParam, uaFlag, status);
5233c3173acSopenharmony_ci        if (ret == 0) {
5243c3173acSopenharmony_ci            CONCUR_LOGI("auth_enable %{public}d success", pid);
5253c3173acSopenharmony_ci        } else {
5263c3173acSopenharmony_ci            CONCUR_LOGE("auth_enable %{public}d fail with ret %{public}d", pid, ret);
5273c3173acSopenharmony_ci        }
5283c3173acSopenharmony_ci    } else {
5293c3173acSopenharmony_ci        CONCUR_LOGI("pid %{public}d is already focus", pid);
5303c3173acSopenharmony_ci    }
5313c3173acSopenharmony_ci    bool found = false;
5323c3173acSopenharmony_ci    bool ddlEnabled = OHOS::system::GetBoolParameter(INTERVAL_DDL, false);
5333c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
5343c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
5353c3173acSopenharmony_ci        if (iter->GetPid() == pid) {
5363c3173acSopenharmony_ci            found = true;
5373c3173acSopenharmony_ci            if (ddlEnabled && pid != curGamePid_) {
5383c3173acSopenharmony_ci                iter->AddKeyThread(uiTid, PRIO_RT);
5393c3173acSopenharmony_ci            }
5403c3173acSopenharmony_ci            iter->BeginScene();
5413c3173acSopenharmony_ci            break;
5423c3173acSopenharmony_ci        }
5433c3173acSopenharmony_ci    }
5443c3173acSopenharmony_ci    if (!found) {
5453c3173acSopenharmony_ci        NewForegroundAppRecord(pid, uiTid, ddlEnabled);
5463c3173acSopenharmony_ci    }
5473c3173acSopenharmony_ci}
5483c3173acSopenharmony_ci
5493c3173acSopenharmony_civoid TaskController::NewForegroundAppRecord(int pid, int uiTid, bool ddlEnabled)
5503c3173acSopenharmony_ci{
5513c3173acSopenharmony_ci    ForegroundAppRecord& appRecord = foregroundApp_.emplace_back(pid, uiTid, pid != curGamePid_);
5523c3173acSopenharmony_ci    if (foregroundApp_.size() <= 0 || appRecord.GetPid() != pid) {
5533c3173acSopenharmony_ci        CONCUR_LOGE("pid %{public}d create app record failed", pid);
5543c3173acSopenharmony_ci        return;
5553c3173acSopenharmony_ci    }
5563c3173acSopenharmony_ci    if (appRecord.IsValid()) {
5573c3173acSopenharmony_ci        if (ddlEnabled && pid != curGamePid_) {
5583c3173acSopenharmony_ci            appRecord.AddKeyThread(uiTid, PRIO_RT);
5593c3173acSopenharmony_ci        }
5603c3173acSopenharmony_ci        appRecord.BeginScene();
5613c3173acSopenharmony_ci    }
5623c3173acSopenharmony_ci}
5633c3173acSopenharmony_ci
5643c3173acSopenharmony_civoid TaskController::NewBackground(int uid, int pid)
5653c3173acSopenharmony_ci{
5663c3173acSopenharmony_ci    auto it = find(authApps_.begin(), authApps_.end(), pid);
5673c3173acSopenharmony_ci    if (it == authApps_.end()) {
5683c3173acSopenharmony_ci        CONCUR_LOGI("un-authed pid %{public}d", pid);
5693c3173acSopenharmony_ci        return;
5703c3173acSopenharmony_ci    }
5713c3173acSopenharmony_ci    CONCUR_LOGI("pid %{public}d change to background.", pid);
5723c3173acSopenharmony_ci    unsigned int pidParam = static_cast<unsigned int>(pid);
5733c3173acSopenharmony_ci
5743c3173acSopenharmony_ci    int ret = AuthPause(pidParam);
5753c3173acSopenharmony_ci    if (ret == 0) {
5763c3173acSopenharmony_ci        CONCUR_LOGI("auth_pause %{public}d success", pid);
5773c3173acSopenharmony_ci    } else {
5783c3173acSopenharmony_ci        CONCUR_LOGI("auth_pause %{public}d fail with %{public}d", pid, ret);
5793c3173acSopenharmony_ci    }
5803c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
5813c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
5823c3173acSopenharmony_ci        if (iter->GetPid() == pid) {
5833c3173acSopenharmony_ci            iter->EndScene();
5843c3173acSopenharmony_ci            return;
5853c3173acSopenharmony_ci        }
5863c3173acSopenharmony_ci    }
5873c3173acSopenharmony_ci}
5883c3173acSopenharmony_ci
5893c3173acSopenharmony_civoid TaskController::NewAppStart(int uid, int pid, const std::string& bundleName)
5903c3173acSopenharmony_ci{
5913c3173acSopenharmony_ci    CONCUR_LOGI("pid %{public}d start.", pid);
5923c3173acSopenharmony_ci    unsigned int pidParam = static_cast<unsigned int>(pid);
5933c3173acSopenharmony_ci    unsigned int uaFlag = AF_RTG_APP;
5943c3173acSopenharmony_ci    unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_DEFAULT);
5953c3173acSopenharmony_ci
5963c3173acSopenharmony_ci    int ret = AuthEnable(pidParam, uaFlag, status);
5973c3173acSopenharmony_ci    if (ret == 0) {
5983c3173acSopenharmony_ci        CONCUR_LOGI("auth_enable %{public}d success", pid);
5993c3173acSopenharmony_ci    } else {
6003c3173acSopenharmony_ci        CONCUR_LOGE("auth_enable %{public}d fail with ret %{public}d", pid, ret);
6013c3173acSopenharmony_ci        return;
6023c3173acSopenharmony_ci    }
6033c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
6043c3173acSopenharmony_ci    authApps_.push_back(pid);
6053c3173acSopenharmony_ci    appBundleName[pid] = bundleName;
6063c3173acSopenharmony_ci}
6073c3173acSopenharmony_ci
6083c3173acSopenharmony_civoid TaskController::AppKilled(int uid, int pid)
6093c3173acSopenharmony_ci{
6103c3173acSopenharmony_ci    CONCUR_LOGI("pid %{public}d killed.", pid);
6113c3173acSopenharmony_ci    unsigned int pidParam = static_cast<unsigned int>(pid);
6123c3173acSopenharmony_ci    int ret = AuthDelete(pidParam);
6133c3173acSopenharmony_ci    if (ret == 0) {
6143c3173acSopenharmony_ci        CONCUR_LOGI("auth_delete %{public}d success", pid);
6153c3173acSopenharmony_ci    } else {
6163c3173acSopenharmony_ci        CONCUR_LOGE("auth_delete %{public}d fail with %{public}d", pid, ret);
6173c3173acSopenharmony_ci    }
6183c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
6193c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
6203c3173acSopenharmony_ci        if (iter->GetPid() == pid) {
6213c3173acSopenharmony_ci            foregroundApp_.erase(iter);
6223c3173acSopenharmony_ci            break;
6233c3173acSopenharmony_ci        }
6243c3173acSopenharmony_ci    }
6253c3173acSopenharmony_ci    for (auto iter = authApps_.begin(); iter != authApps_.end(); iter++) {
6263c3173acSopenharmony_ci        if (*iter == pid) {
6273c3173acSopenharmony_ci            authApps_.erase(iter);
6283c3173acSopenharmony_ci            break;
6293c3173acSopenharmony_ci        }
6303c3173acSopenharmony_ci    }
6313c3173acSopenharmony_ci    appBundleName.erase(pid);
6323c3173acSopenharmony_ci}
6333c3173acSopenharmony_ci
6343c3173acSopenharmony_ciint TaskController::AuthSystemProcess(int pid)
6353c3173acSopenharmony_ci{
6363c3173acSopenharmony_ci    unsigned int uaFlag = AF_RTG_ALL;
6373c3173acSopenharmony_ci    unsigned int status = static_cast<unsigned int>(AuthStatus::AUTH_STATUS_SYSTEM_SERVER);
6383c3173acSopenharmony_ci    int ret = AuthEnable(pid, uaFlag, status);
6393c3173acSopenharmony_ci    if (ret == 0) {
6403c3173acSopenharmony_ci        CONCUR_LOGI("auth process %{public}d success", pid);
6413c3173acSopenharmony_ci    } else {
6423c3173acSopenharmony_ci        CONCUR_LOGI("auth process %{public}d failed, ret %{public}d", pid, ret);
6433c3173acSopenharmony_ci    }
6443c3173acSopenharmony_ci    return ret;
6453c3173acSopenharmony_ci}
6463c3173acSopenharmony_ci
6473c3173acSopenharmony_civoid TaskController::ContinuousTaskProcess(int uid, int pid, int status)
6483c3173acSopenharmony_ci{
6493c3173acSopenharmony_ci    int ret = -1;
6503c3173acSopenharmony_ci    if (status == static_cast<int>(MSG_CONTINUOUS_TASK_START)) {
6513c3173acSopenharmony_ci        ret = AuthEnhance(pid, true);
6523c3173acSopenharmony_ci        CONCUR_LOGI("auth_enhance pid %{public}d start, ret %{public}d", pid, ret);
6533c3173acSopenharmony_ci    } else if (status == static_cast<int>(MSG_CONTINUOUS_TASK_END)) {
6543c3173acSopenharmony_ci        ret = AuthEnhance(pid, false);
6553c3173acSopenharmony_ci        CONCUR_LOGI("auth_enhance pid %{public}d end, ret %{public}d", pid, ret);
6563c3173acSopenharmony_ci    } else {
6573c3173acSopenharmony_ci        CONCUR_LOGE("Invalid auth_enhance status %{public}d", status);
6583c3173acSopenharmony_ci    }
6593c3173acSopenharmony_ci}
6603c3173acSopenharmony_ci
6613c3173acSopenharmony_civoid TaskController::FocusStatusProcess(int uid, int pid, int status)
6623c3173acSopenharmony_ci{
6633c3173acSopenharmony_ci    int ret = -1;
6643c3173acSopenharmony_ci    unsigned int rtgFlag = AF_RTG_APP;
6653c3173acSopenharmony_ci    unsigned int qosFlag = AF_QOS_DELEGATED;
6663c3173acSopenharmony_ci    if (status == static_cast<int>(MSG_GET_FOCUS)) {
6673c3173acSopenharmony_ci        ret = AuthSwitch(pid, rtgFlag, qosFlag, static_cast<unsigned int>(AuthStatus::AUTH_STATUS_FOCUS));
6683c3173acSopenharmony_ci        CONCUR_LOGI("pid %{public}d get focus. ret %{public}d", pid, ret);
6693c3173acSopenharmony_ci    } else if (status == static_cast<int>(MSG_LOSE_FOCUS)) {
6703c3173acSopenharmony_ci        ret = AuthSwitch(pid, rtgFlag, qosFlag, static_cast<unsigned int>(AuthStatus::AUTH_STATUS_FOREGROUND));
6713c3173acSopenharmony_ci        CONCUR_LOGI("pid %{public}d lose focus. ret %{public}d", pid, ret);
6723c3173acSopenharmony_ci    } else {
6733c3173acSopenharmony_ci        CONCUR_LOGE("Invalid focus status %{public}d", status);
6743c3173acSopenharmony_ci    }
6753c3173acSopenharmony_ci}
6763c3173acSopenharmony_ci
6773c3173acSopenharmony_civoid TaskController::InteractionSceneProcess(int status)
6783c3173acSopenharmony_ci{
6793c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(ddlPowerModeLock_);
6803c3173acSopenharmony_ci    if (ddlSceneSchedSwitch_) {
6813c3173acSopenharmony_ci        if (status ==  MSG_ENTER_INTERACTION_SCENE) {
6823c3173acSopenharmony_ci            DeadlinePerfMode();
6833c3173acSopenharmony_ci        } else if (status == MSG_EXIT_INTERACTION_SCENE) {
6843c3173acSopenharmony_ci            DeadlinePowerMode();
6853c3173acSopenharmony_ci        }
6863c3173acSopenharmony_ci    }
6873c3173acSopenharmony_ci}
6883c3173acSopenharmony_ci
6893c3173acSopenharmony_civoid TaskController::DeadlinePerfMode()
6903c3173acSopenharmony_ci{
6913c3173acSopenharmony_ci    if (ddlPowerModeEnable_) {
6923c3173acSopenharmony_ci        StartTrace(HITRACE_TAG_ACE, "Deadline perf mode");
6933c3173acSopenharmony_ci        SetAppAndRenderServiceRate(uniAppRate_, systemRate_);
6943c3173acSopenharmony_ci        ddlPowerModeEnable_ = false;
6953c3173acSopenharmony_ci        CONCUR_LOGI("Deadline switch to perf mode");
6963c3173acSopenharmony_ci        FinishTrace(HITRACE_TAG_ACE);
6973c3173acSopenharmony_ci    }
6983c3173acSopenharmony_ci}
6993c3173acSopenharmony_ci
7003c3173acSopenharmony_civoid TaskController::DeadlinePowerMode()
7013c3173acSopenharmony_ci{
7023c3173acSopenharmony_ci    if (!ddlPowerModeEnable_) {
7033c3173acSopenharmony_ci        StartTrace(HITRACE_TAG_ACE, "Deadline power mode");
7043c3173acSopenharmony_ci        int appRate = uniAppRate_;
7053c3173acSopenharmony_ci        int rsRate = systemRate_;
7063c3173acSopenharmony_ci        if (configEnable_ && configReader_) {
7073c3173acSopenharmony_ci            appRate = configReader_->GetDegratationFps(appRate);
7083c3173acSopenharmony_ci            rsRate = configReader_->GetDegratationFps(rsRate);
7093c3173acSopenharmony_ci        }
7103c3173acSopenharmony_ci        SetAppAndRenderServiceRate(appRate, rsRate);
7113c3173acSopenharmony_ci        ddlPowerModeEnable_ = true;
7123c3173acSopenharmony_ci        CONCUR_LOGI("Deadline switch to power mode");
7133c3173acSopenharmony_ci        FinishTrace(HITRACE_TAG_ACE);
7143c3173acSopenharmony_ci    }
7153c3173acSopenharmony_ci}
7163c3173acSopenharmony_civoid TaskController::QueryDeadline(int queryItem, DeadlineReply& ddlReply, const Json::Value& payload)
7173c3173acSopenharmony_ci{
7183c3173acSopenharmony_ci    pid_t uid = IPCSkeleton::GetInstance().GetCallingUid();
7193c3173acSopenharmony_ci    std::string processName = GetProcessNameByToken();
7203c3173acSopenharmony_ci    if (processName != RENDER_SERVICE_PROCESS_NAME && processName != GAME_ACCELERATE_SCHED_PROCESS_NAME) {
7213c3173acSopenharmony_ci        CONCUR_LOGE("Invalid uid %{public}d, only RS or RSS can call QueryDeadline", uid);
7223c3173acSopenharmony_ci        return;
7233c3173acSopenharmony_ci    }
7243c3173acSopenharmony_ci    switch (queryItem) {
7253c3173acSopenharmony_ci        case DDL_RATE: {
7263c3173acSopenharmony_ci            ModifySystemRate(payload);
7273c3173acSopenharmony_ci            break;
7283c3173acSopenharmony_ci        }
7293c3173acSopenharmony_ci        case MSG_GAME: {
7303c3173acSopenharmony_ci            ModifyGameState(payload);
7313c3173acSopenharmony_ci            break;
7323c3173acSopenharmony_ci        }
7333c3173acSopenharmony_ci        default: {
7343c3173acSopenharmony_ci            break;
7353c3173acSopenharmony_ci        }
7363c3173acSopenharmony_ci    }
7373c3173acSopenharmony_ci}
7383c3173acSopenharmony_ci
7393c3173acSopenharmony_civoid TaskController::ModifyGameState(const Json::Value& payload)
7403c3173acSopenharmony_ci{
7413c3173acSopenharmony_ci    if (!CheckJsonValid(payload)) {
7423c3173acSopenharmony_ci        CONCUR_LOGE("[MSG_GAME]receive json invalid");
7433c3173acSopenharmony_ci        return;
7443c3173acSopenharmony_ci    }
7453c3173acSopenharmony_ci    if (payload["gameMsg"].isNull()) {
7463c3173acSopenharmony_ci        CONCUR_LOGE("[MSG_GAME]message is null");
7473c3173acSopenharmony_ci        return;
7483c3173acSopenharmony_ci    }
7493c3173acSopenharmony_ci    std::string gameMsg = payload["gameMsg"].asString();
7503c3173acSopenharmony_ci    int oldGamePid = curGamePid_;
7513c3173acSopenharmony_ci    int newGamePid = GetGamePid(gameMsg);
7523c3173acSopenharmony_ci    curGamePid_ = newGamePid;
7533c3173acSopenharmony_ci    CONCUR_LOGI("[MSG_GAME]current game pid is %{public}d, old game pid is %{public}d",
7543c3173acSopenharmony_ci                newGamePid, oldGamePid);
7553c3173acSopenharmony_ci    if (curGamePid_ == -1) {
7563c3173acSopenharmony_ci        return;
7573c3173acSopenharmony_ci    }
7583c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
7593c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
7603c3173acSopenharmony_ci        if (iter->GetPid() == curGamePid_ && iter->GetGrpId() >= 0) {
7613c3173acSopenharmony_ci            CONCUR_LOGI("[MSG_GAME]destroy rtg grp, pid is %{public}d grpId is %{public}d",
7623c3173acSopenharmony_ci                        iter->GetPid(), iter->GetGrpId());
7633c3173acSopenharmony_ci            DestroyRtgGrp(iter->GetGrpId());
7643c3173acSopenharmony_ci            iter->SetGrpId(-1);
7653c3173acSopenharmony_ci            break;
7663c3173acSopenharmony_ci        }
7673c3173acSopenharmony_ci    }
7683c3173acSopenharmony_ci    return;
7693c3173acSopenharmony_ci}
7703c3173acSopenharmony_ci
7713c3173acSopenharmony_ciint TaskController::GetGamePid(const std::string &gameMsg) const
7723c3173acSopenharmony_ci{
7733c3173acSopenharmony_ci    GameStatus status = GetGameScene(gameMsg);
7743c3173acSopenharmony_ci    CONCUR_LOGI("[MSG_GAME]gamescene status %{public}d", status);
7753c3173acSopenharmony_ci    int gamePid = -1;
7763c3173acSopenharmony_ci    if (status == GAME_ENTRY_MSG) {
7773c3173acSopenharmony_ci        size_t pos = gameMsg.find(",");
7783c3173acSopenharmony_ci        if (pos == string::npos) {
7793c3173acSopenharmony_ci            return -1;
7803c3173acSopenharmony_ci        }
7813c3173acSopenharmony_ci        int ret = sscanf_s(gameMsg.substr(0, pos).c_str(), "{\"gamePid\":\"%d\"", &gamePid);
7823c3173acSopenharmony_ci        if (ret <= 0) {
7833c3173acSopenharmony_ci            CONCUR_LOGE("[MSG_GAME]message parsing failed, ret is %{public}d", ret);
7843c3173acSopenharmony_ci        } else {
7853c3173acSopenharmony_ci            CONCUR_LOGI("[MSG_GAME]message parsing success");
7863c3173acSopenharmony_ci        }
7873c3173acSopenharmony_ci    }
7883c3173acSopenharmony_ci    return gamePid;
7893c3173acSopenharmony_ci}
7903c3173acSopenharmony_ci
7913c3173acSopenharmony_ciGameStatus TaskController::GetGameScene(const std::string &gameMsg) const
7923c3173acSopenharmony_ci{
7933c3173acSopenharmony_ci    if (gameMsg.find("gameScene\":\"1") != std::string::npos) {
7943c3173acSopenharmony_ci        return GAME_ENTRY_MSG;
7953c3173acSopenharmony_ci    }
7963c3173acSopenharmony_ci    if (gameMsg.find("gameScene\":\"0") != std::string::npos) {
7973c3173acSopenharmony_ci        return GAME_EXIT_MSG;
7983c3173acSopenharmony_ci    }
7993c3173acSopenharmony_ci    if (gameMsg.find("cameraScene\":\"1") != std::string::npos) {
8003c3173acSopenharmony_ci        return CAMERA_ENTRY_MSG;
8013c3173acSopenharmony_ci    }
8023c3173acSopenharmony_ci    if (gameMsg.find("cameraScene\":\"0") != std::string::npos) {
8033c3173acSopenharmony_ci        return CAMERA_EXIT_MSG;
8043c3173acSopenharmony_ci    }
8053c3173acSopenharmony_ci    if (gameMsg.find("GTXGamePid\":") != std::string::npos) {
8063c3173acSopenharmony_ci        return GAME_GTX_MSG;
8073c3173acSopenharmony_ci    }
8083c3173acSopenharmony_ci    return STATUS_MSG_MAX;
8093c3173acSopenharmony_ci}
8103c3173acSopenharmony_ci
8113c3173acSopenharmony_cibool TaskController::ModifySystemRate(const Json::Value& payload)
8123c3173acSopenharmony_ci{
8133c3173acSopenharmony_ci    if (!CheckJsonValid(payload)) {
8143c3173acSopenharmony_ci        CONCUR_LOGI("service receive json invalid");
8153c3173acSopenharmony_ci        return false;
8163c3173acSopenharmony_ci    }
8173c3173acSopenharmony_ci    SetAppRate(payload);
8183c3173acSopenharmony_ci    SetRenderServiceRate(payload);
8193c3173acSopenharmony_ci    return true;
8203c3173acSopenharmony_ci}
8213c3173acSopenharmony_ci
8223c3173acSopenharmony_civoid TaskController::SetAppRate(const Json::Value& payload)
8233c3173acSopenharmony_ci{
8243c3173acSopenharmony_ci    int rtgId = 0;
8253c3173acSopenharmony_ci    int uiTid = 0;
8263c3173acSopenharmony_ci    int appRate = FindRateFromInfo(UNI_APP_RATE_ID, payload);
8273c3173acSopenharmony_ci    if (appRate > 0 && appRate != uniAppRate_) {
8283c3173acSopenharmony_ci        CONCUR_LOGD("set unified app rate %{public}d", appRate);
8293c3173acSopenharmony_ci        uniAppRate_ = appRate;
8303c3173acSopenharmony_ci        if (ddlSceneSchedSwitch_ && ddlPowerModeEnable_ && configEnable_ && configReader_) {
8313c3173acSopenharmony_ci            appRate = configReader_->GetDegratationFps(appRate);
8323c3173acSopenharmony_ci        }
8333c3173acSopenharmony_ci        bool ret = OHOS::system::SetParameter(INTERVAL_APP_RATE, std::to_string(appRate));
8343c3173acSopenharmony_ci        if (ret == false) {
8353c3173acSopenharmony_ci            CONCUR_LOGI("set app rate param failed");
8363c3173acSopenharmony_ci        }
8373c3173acSopenharmony_ci        StartTrace(HITRACE_TAG_ACE,
8383c3173acSopenharmony_ci            "SetAppRate:" + std::to_string(appRate) + " ret:" + std::to_string(ret));
8393c3173acSopenharmony_ci        FinishTrace(HITRACE_TAG_ACE);
8403c3173acSopenharmony_ci        return;
8413c3173acSopenharmony_ci    }
8423c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
8433c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
8443c3173acSopenharmony_ci        uiTid = iter->GetUiTid();
8453c3173acSopenharmony_ci        rtgId = iter->GetGrpId();
8463c3173acSopenharmony_ci        if (uiTid <= 0 || rtgId <= 0) {
8473c3173acSopenharmony_ci            continue;
8483c3173acSopenharmony_ci        }
8493c3173acSopenharmony_ci        appRate = FindRateFromInfo(uiTid, payload);
8503c3173acSopenharmony_ci        if (appRate > 0 && appRate != iter->GetRate()) {
8513c3173acSopenharmony_ci            CONCUR_LOGI("set app rate %{public}d rtgId is %{public}d, old rate is %{public}d",
8523c3173acSopenharmony_ci                        appRate, rtgId, iter->GetRate());
8533c3173acSopenharmony_ci            SetFrameRate(rtgId, appRate);
8543c3173acSopenharmony_ci            iter->SetRate(appRate);
8553c3173acSopenharmony_ci        }
8563c3173acSopenharmony_ci    }
8573c3173acSopenharmony_ci    return;
8583c3173acSopenharmony_ci}
8593c3173acSopenharmony_ci
8603c3173acSopenharmony_ciint TaskController::FindRateFromInfo(int uiTid, const Json::Value& payload)
8613c3173acSopenharmony_ci{
8623c3173acSopenharmony_ci    int appRate = 0;
8633c3173acSopenharmony_ci    if (payload[std::to_string(uiTid)].isNull()) {
8643c3173acSopenharmony_ci        CONCUR_LOGD("FindRateFromInfo tid %{public}d is null", uiTid);
8653c3173acSopenharmony_ci        return appRate;
8663c3173acSopenharmony_ci    }
8673c3173acSopenharmony_ci    try {
8683c3173acSopenharmony_ci        appRate = stoi(payload[std::to_string(uiTid)].asString());
8693c3173acSopenharmony_ci    } catch (...) {
8703c3173acSopenharmony_ci        CONCUR_LOGI("application %{public}d is not in rtg_group", uiTid);
8713c3173acSopenharmony_ci    }
8723c3173acSopenharmony_ci    return appRate;
8733c3173acSopenharmony_ci}
8743c3173acSopenharmony_ci
8753c3173acSopenharmony_civoid TaskController::SetRenderServiceRate(const Json::Value& payload)
8763c3173acSopenharmony_ci{
8773c3173acSopenharmony_ci    int rsRate = FindRateFromInfo(renderServiceMainTid_, payload);
8783c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(rateInfoLock_);
8793c3173acSopenharmony_ci    if (renderServiceMainGrpId_ > 0 && rsRate > 0 && rsRate != systemRate_) {
8803c3173acSopenharmony_ci        CONCUR_LOGD("set rs rate %{public}d rtgId is %{public}d, old rate is %{public}d",
8813c3173acSopenharmony_ci                    rsRate, renderServiceMainGrpId_, systemRate_);
8823c3173acSopenharmony_ci        SetFrameRate(renderServiceMainGrpId_, rsRate);
8833c3173acSopenharmony_ci        systemRate_ = rsRate;
8843c3173acSopenharmony_ci        if (ddlSceneSchedSwitch_ && ddlPowerModeEnable_ && configEnable_ && configReader_) {
8853c3173acSopenharmony_ci            rsRate = configReader_->GetDegratationFps(rsRate);
8863c3173acSopenharmony_ci        }
8873c3173acSopenharmony_ci        bool ret = OHOS::system::SetParameter(INTERVAL_RS_RATE, std::to_string(rsRate));
8883c3173acSopenharmony_ci        if (ret == false) {
8893c3173acSopenharmony_ci            CONCUR_LOGI("set rs rate param failed");
8903c3173acSopenharmony_ci        }
8913c3173acSopenharmony_ci        StartTrace(HITRACE_TAG_ACE,
8923c3173acSopenharmony_ci            "SetRSRate:" + std::to_string(rsRate) + " ret:" + std::to_string(ret));
8933c3173acSopenharmony_ci        FinishTrace(HITRACE_TAG_ACE);
8943c3173acSopenharmony_ci    }
8953c3173acSopenharmony_ci}
8963c3173acSopenharmony_ci
8973c3173acSopenharmony_civoid TaskController::SetAppAndRenderServiceRate(int appRate, int rsRate)
8983c3173acSopenharmony_ci{
8993c3173acSopenharmony_ci    bool ret = OHOS::system::SetParameter(INTERVAL_APP_RATE, std::to_string(appRate));
9003c3173acSopenharmony_ci    if (ret == false) {
9013c3173acSopenharmony_ci        CONCUR_LOGI("set app rate param failed");
9023c3173acSopenharmony_ci    }
9033c3173acSopenharmony_ci    StartTrace(HITRACE_TAG_ACE,
9043c3173acSopenharmony_ci        "SetAppRate:" + std::to_string(appRate) + " ret:" + std::to_string(ret));
9053c3173acSopenharmony_ci    FinishTrace(HITRACE_TAG_ACE);
9063c3173acSopenharmony_ci
9073c3173acSopenharmony_ci    ret = OHOS::system::SetParameter(INTERVAL_RS_RATE, std::to_string(rsRate));
9083c3173acSopenharmony_ci    if (ret == false) {
9093c3173acSopenharmony_ci        CONCUR_LOGI("set rs rate param failed");
9103c3173acSopenharmony_ci    }
9113c3173acSopenharmony_ci    StartTrace(HITRACE_TAG_ACE,
9123c3173acSopenharmony_ci        "SetRSRate:" + std::to_string(rsRate) + " ret:" + std::to_string(ret));
9133c3173acSopenharmony_ci    FinishTrace(HITRACE_TAG_ACE);
9143c3173acSopenharmony_ci}
9153c3173acSopenharmony_ci
9163c3173acSopenharmony_cibool TaskController::CheckJsonValid(const Json::Value& payload)
9173c3173acSopenharmony_ci{
9183c3173acSopenharmony_ci    Json::ValueType type = payload.type();
9193c3173acSopenharmony_ci    if (type != Json::objectValue) {
9203c3173acSopenharmony_ci        CONCUR_LOGE("error payload");
9213c3173acSopenharmony_ci        return false;
9223c3173acSopenharmony_ci    }
9233c3173acSopenharmony_ci    if (payload.empty()) {
9243c3173acSopenharmony_ci        CONCUR_LOGI("payload empty");
9253c3173acSopenharmony_ci        return false;
9263c3173acSopenharmony_ci    }
9273c3173acSopenharmony_ci    return true;
9283c3173acSopenharmony_ci}
9293c3173acSopenharmony_ci
9303c3173acSopenharmony_civoid TaskController::SetFrameRate(int rtgId, int rate)
9313c3173acSopenharmony_ci{
9323c3173acSopenharmony_ci    if (rtgId > 0) {
9333c3173acSopenharmony_ci        SetFrameRateAndPrioType(rtgId, rate, PARAM_TYPE);
9343c3173acSopenharmony_ci    }
9353c3173acSopenharmony_ci}
9363c3173acSopenharmony_ci
9373c3173acSopenharmony_civoid TaskController::PrintInfo()
9383c3173acSopenharmony_ci{
9393c3173acSopenharmony_ci    std::lock_guard<std::mutex> lock(appInfoLock_);
9403c3173acSopenharmony_ci    for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) {
9413c3173acSopenharmony_ci        iter->PrintKeyThreads();
9423c3173acSopenharmony_ci    }
9433c3173acSopenharmony_ci}
9443c3173acSopenharmony_ci
9453c3173acSopenharmony_ciint TaskController::CreateNewRtgGrp(int prioType, int rtNum)
9463c3173acSopenharmony_ci{
9473c3173acSopenharmony_ci    struct rtg_grp_data grp_data;
9483c3173acSopenharmony_ci    int ret;
9493c3173acSopenharmony_ci    char fileName[] = "/proc/self/sched_rtg_ctrl";
9503c3173acSopenharmony_ci    int fd = open(fileName, O_RDWR);
9513c3173acSopenharmony_ci    if (fd < 0) {
9523c3173acSopenharmony_ci        CONCUR_LOGE("Open file /proc/self/sched_rth_ctrl, errno = %{public}d", errno);
9533c3173acSopenharmony_ci        return fd;
9543c3173acSopenharmony_ci    }
9553c3173acSopenharmony_ci    (void)memset_s(&grp_data, sizeof(struct rtg_grp_data), 0, sizeof(struct rtg_grp_data));
9563c3173acSopenharmony_ci    if ((prioType > 0) && (prioType < RTG_TYPE_MAX)) {
9573c3173acSopenharmony_ci        grp_data.prio_type = prioType;
9583c3173acSopenharmony_ci    }
9593c3173acSopenharmony_ci    if (rtNum > 0) {
9603c3173acSopenharmony_ci        grp_data.rt_cnt = rtNum;
9613c3173acSopenharmony_ci    }
9623c3173acSopenharmony_ci    grp_data.rtg_cmd = CMD_CREATE_RTG_GRP;
9633c3173acSopenharmony_ci    ret = ioctl(fd, CMD_ID_SET_RTG, &grp_data);
9643c3173acSopenharmony_ci    if (ret <= 0) {
9653c3173acSopenharmony_ci        CONCUR_LOGE("create rtg grp failed, errno = %{public}d (%{public}s)", errno, strerror(errno));
9663c3173acSopenharmony_ci    } else {
9673c3173acSopenharmony_ci        CONCUR_LOGI("create rtg grp success, get rtg id %{public}d.", ret);
9683c3173acSopenharmony_ci    }
9693c3173acSopenharmony_ci    close(fd);
9703c3173acSopenharmony_ci    return ret;
9713c3173acSopenharmony_ci}
9723c3173acSopenharmony_ci
9733c3173acSopenharmony_ciForegroundAppRecord::ForegroundAppRecord(int pid, int uiTid, bool createGrp)
9743c3173acSopenharmony_ci{
9753c3173acSopenharmony_ci    pid_ = pid;
9763c3173acSopenharmony_ci    uiTid_ = uiTid;
9773c3173acSopenharmony_ci    if (OHOS::system::GetBoolParameter(INTERVAL_DDL, false) && createGrp) {
9783c3173acSopenharmony_ci        grpId_ = TaskController::GetInstance().CreateNewRtgGrp(PRIO_RT, MAX_KEY_THREADS);
9793c3173acSopenharmony_ci    } else {
9803c3173acSopenharmony_ci        grpId_ = -1;
9813c3173acSopenharmony_ci    }
9823c3173acSopenharmony_ci}
9833c3173acSopenharmony_ci
9843c3173acSopenharmony_ciForegroundAppRecord::~ForegroundAppRecord()
9853c3173acSopenharmony_ci{
9863c3173acSopenharmony_ci    if (grpId_ > 0) {
9873c3173acSopenharmony_ci        DestroyRtgGrp(grpId_);
9883c3173acSopenharmony_ci    }
9893c3173acSopenharmony_ci}
9903c3173acSopenharmony_ci
9913c3173acSopenharmony_civoid ForegroundAppRecord::AddKeyThread(int tid, int prio)
9923c3173acSopenharmony_ci{
9933c3173acSopenharmony_ci    int rtgPrio = (prio >= PRIO_NORMAL) ? PRIO_NORMAL : PRIO_RT;
9943c3173acSopenharmony_ci    if (keyThreads_.find(tid) != keyThreads_.end()) {
9953c3173acSopenharmony_ci        return;
9963c3173acSopenharmony_ci    }
9973c3173acSopenharmony_ci    if (grpId_ <= 0) {
9983c3173acSopenharmony_ci        CONCUR_LOGI("Add key thread fail: Grp id not been created success, tid is %{public}d", tid);
9993c3173acSopenharmony_ci        return;
10003c3173acSopenharmony_ci    }
10013c3173acSopenharmony_ci    if (keyThreads_.size() >= MAX_KEY_THREADS) {
10023c3173acSopenharmony_ci        CONCUR_LOGI("Add key thread fail: Key threads num limit.");
10033c3173acSopenharmony_ci        return;
10043c3173acSopenharmony_ci    }
10053c3173acSopenharmony_ci    if (prio == RPIO_IN) {
10063c3173acSopenharmony_ci        setpriority(PRIO_PROCESS, tid, -13); // -13 represent spcial nice in qos
10073c3173acSopenharmony_ci    } else {
10083c3173acSopenharmony_ci        int ret = AddThreadToRtg(tid, grpId_, rtgPrio);
10093c3173acSopenharmony_ci        if (ret != 0) {
10103c3173acSopenharmony_ci            CONCUR_LOGI("Add key thread fail: Kernel err report. ret is %{public}d", ret);
10113c3173acSopenharmony_ci        } else {
10123c3173acSopenharmony_ci            CONCUR_LOGI("Add key thread %{public}d", tid);
10133c3173acSopenharmony_ci        }
10143c3173acSopenharmony_ci        keyThreads_.insert(tid);
10153c3173acSopenharmony_ci    }
10163c3173acSopenharmony_ci}
10173c3173acSopenharmony_ci
10183c3173acSopenharmony_cibool ForegroundAppRecord::BeginScene()
10193c3173acSopenharmony_ci{
10203c3173acSopenharmony_ci    if (grpId_ <= 0) {
10213c3173acSopenharmony_ci        CONCUR_LOGI("Error begin scene in pid %{public}d", pid_);
10223c3173acSopenharmony_ci        return false;
10233c3173acSopenharmony_ci    }
10243c3173acSopenharmony_ci    OHOS::RME::BeginFrameFreq(0);
10253c3173acSopenharmony_ci    OHOS::RME::EndFrameFreq(0);
10263c3173acSopenharmony_ci    return true;
10273c3173acSopenharmony_ci}
10283c3173acSopenharmony_ci
10293c3173acSopenharmony_cibool ForegroundAppRecord::EndScene()
10303c3173acSopenharmony_ci{
10313c3173acSopenharmony_ci    if (grpId_ <= 0) {
10323c3173acSopenharmony_ci        CONCUR_LOGI("Error end scene loss grpId_ in pid %{public}d", pid_);
10333c3173acSopenharmony_ci        return false;
10343c3173acSopenharmony_ci    }
10353c3173acSopenharmony_ci    OHOS::RME::EndScene(grpId_);
10363c3173acSopenharmony_ci    return true;
10373c3173acSopenharmony_ci}
10383c3173acSopenharmony_ci
10393c3173acSopenharmony_ciint ForegroundAppRecord::GetPid() const
10403c3173acSopenharmony_ci{
10413c3173acSopenharmony_ci    return pid_;
10423c3173acSopenharmony_ci}
10433c3173acSopenharmony_ci
10443c3173acSopenharmony_ciint ForegroundAppRecord::GetGrpId() const
10453c3173acSopenharmony_ci{
10463c3173acSopenharmony_ci    return grpId_;
10473c3173acSopenharmony_ci}
10483c3173acSopenharmony_ci
10493c3173acSopenharmony_civoid ForegroundAppRecord::SetGrpId(int grpId)
10503c3173acSopenharmony_ci{
10513c3173acSopenharmony_ci    grpId_ = grpId;
10523c3173acSopenharmony_ci}
10533c3173acSopenharmony_ci
10543c3173acSopenharmony_civoid ForegroundAppRecord::SetRate(int appRate)
10553c3173acSopenharmony_ci{
10563c3173acSopenharmony_ci    rate_ = appRate;
10573c3173acSopenharmony_ci}
10583c3173acSopenharmony_ci
10593c3173acSopenharmony_ciint ForegroundAppRecord::GetRate() const
10603c3173acSopenharmony_ci{
10613c3173acSopenharmony_ci    return rate_;
10623c3173acSopenharmony_ci}
10633c3173acSopenharmony_ci
10643c3173acSopenharmony_civoid ForegroundAppRecord::SetUiTid(int uiTid)
10653c3173acSopenharmony_ci{
10663c3173acSopenharmony_ci    uiTid_ = uiTid;
10673c3173acSopenharmony_ci}
10683c3173acSopenharmony_ci
10693c3173acSopenharmony_ciint ForegroundAppRecord::GetUiTid() const
10703c3173acSopenharmony_ci{
10713c3173acSopenharmony_ci    return uiTid_;
10723c3173acSopenharmony_ci}
10733c3173acSopenharmony_ci
10743c3173acSopenharmony_cibool ForegroundAppRecord::IsValid()
10753c3173acSopenharmony_ci{
10763c3173acSopenharmony_ci    if (pid_ > 0) {
10773c3173acSopenharmony_ci        return true;
10783c3173acSopenharmony_ci    }
10793c3173acSopenharmony_ci    return false;
10803c3173acSopenharmony_ci}
10813c3173acSopenharmony_ci
10823c3173acSopenharmony_civoid ForegroundAppRecord::PrintKeyThreads()
10833c3173acSopenharmony_ci{
10843c3173acSopenharmony_ci    std::string strLog = "pid ";
10853c3173acSopenharmony_ci    strLog.append(std::to_string(pid_));
10863c3173acSopenharmony_ci    strLog.append(" has key threads: ");
10873c3173acSopenharmony_ci    for (auto iter = keyThreads_.begin(); iter != keyThreads_.end(); iter++) {
10883c3173acSopenharmony_ci        std::string temp = std::to_string(*iter);
10893c3173acSopenharmony_ci        strLog.append(temp);
10903c3173acSopenharmony_ci        strLog.append(", ");
10913c3173acSopenharmony_ci    }
10923c3173acSopenharmony_ci    CONCUR_LOGD("%{public}s", strLog.c_str());
10933c3173acSopenharmony_ci}
10943c3173acSopenharmony_ci} // namespace ConcurrentTask
10953c3173acSopenharmony_ci} // namespace OHOS
1096