• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "res_sa_init.h"
17 
18 #include "ffrt.h"
19 #include "res_sched_client.h"
20 #include "res_sched_log.h"
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
24 
GetInstance()25 ResSchedSaInit& ResSchedSaInit::GetInstance()
26 {
27     static ResSchedSaInit instance;
28     return instance;
29 }
30 
GetInstance()31 ResSchedIpcThread& ResSchedIpcThread::GetInstance()
32 {
33     static ResSchedIpcThread instance;
34     return instance;
35 }
36 
SetQos(int32_t clientPid)37 void ResSchedIpcThread::SetQos(int32_t clientPid)
38 {
39     if (!isInit_.load()) {
40         return;
41     }
42 
43     if (clientPid == selfPid_) {
44         return;
45     }
46 
47     int32_t tid = gettid();
48     int32_t pid = selfPid_;
49     std::unique_lock<std::mutex> lock(ipcThreadSetQosMutex_);
50     if (ipcThreadTids_.find(tid) != ipcThreadTids_.end()) {
51         lock.unlock();
52         return;
53     }
54     if (ipcThreadTids_.size() > MAX_IPC_THREAD_NUM) {
55         RESSCHED_LOGI("ResSchedIpcThread SetQos Thread size EXCEEDS LIMIT.");
56         ipcThreadTids_.clear();
57     }
58     ipcThreadTids_.insert(tid);
59     lock.unlock();
60 
61     // Throw out an ffrt task to prevent deadlocks
62     ffrt::submit([this, tid, pid]() {
63         std::string strTid = std::to_string(tid);
64         std::unordered_map<std::string, std::string> mapPayload;
65         mapPayload["pid"] = std::to_string(pid);
66         mapPayload[strTid] = std::to_string(RSS_IPC_QOS_LEVEL);
67         mapPayload["bundleName"] = RSS_BUNDLE_NAME;
68         RESSCHED_LOGI("ResSchedIpcThread SetQos Thread tid=%{public}u.", tid);
69         OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
70             OHOS::ResourceSchedule::ResType::RES_TYPE_THREAD_QOS_CHANGE, 0, mapPayload);
71     });
72 }
73 
SetInitFlag(bool flag)74 void ResSchedIpcThread::SetInitFlag(bool flag)
75 {
76     selfPid_ = getpid();
77     isInit_ = flag;
78 }
79 
80 } // namespace ResourceSchedule
81 } // namespace OHOS
82