• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "system_load.h"
16 
17 #include "battery_status.h"
18 #include "dfs_error.h"
19 #include "parameters.h"
20 #include "task_state_manager.h"
21 #include "utils_log.h"
22 #include "res_sched_client.h"
23 
24 namespace OHOS::FileManagement::CloudSync {
25 
SetDataSycner(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)26 void SystemLoadListener::SetDataSycner(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
27 {
28     dataSyncManager_ = dataSyncManager;
29 }
30 
RegisterSystemloadCallback(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)31 void SystemLoadStatus::RegisterSystemloadCallback(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
32 {
33     sptr<SystemLoadListener> loadListener = new (std::nothrow) SystemLoadListener();
34     if (loadListener == nullptr) {
35         return;
36     }
37     loadListener->SetDataSycner(dataSyncManager);
38     ResourceSchedule::ResSchedClient::GetInstance().RegisterSystemloadNotifier(loadListener);
39 }
40 
OnSystemloadLevel(int32_t level)41 void SystemLoadListener::OnSystemloadLevel(int32_t level)
42 {
43     SystemLoadStatus::Setload(level);
44     if (level >= SYSTEMLOADLEVEL_HOT) {
45         LOGI("OnSystemloadLevel over warm");
46     } else if (dataSyncManager_) {
47         std::string systemLoadSync = system::GetParameter(TEMPERATURE_SYSPARAM_SYNC, "");
48         std::string systemLoadThumb = system::GetParameter(TEMPERATURE_SYSPARAM_THUMB, "");
49         LOGI("OnSystemloadLevel is normal, level:%{public}d", level);
50         if (systemLoadSync == "true") {
51             LOGI("SetParameter TEMPERATURE_SYSPARAM_SYNC false");
52             system::SetParameter(TEMPERATURE_SYSPARAM_SYNC, "false");
53             TaskStateManager::GetInstance().StartTask();
54             dataSyncManager_->TriggerRecoverySync(SyncTriggerType::SYSTEM_LOAD_TRIGGER);
55         }
56         if (systemLoadThumb == "true") {
57             if (BatteryStatus::IsCharging() && level > SYSTEMLOADLEVEL_WARM) {
58                 return;
59             } else if (!BatteryStatus::IsCharging() && level > SYSTEMLOADLEVEL_NORMAL) {
60                 return;
61             }
62             LOGI("SetParameter TEMPERATURE_SYSPARAM_THUMB false");
63             system::SetParameter(TEMPERATURE_SYSPARAM_THUMB, "false");
64             TaskStateManager::GetInstance().StartTask();
65             dataSyncManager_->DownloadThumb();
66         }
67     }
68 }
69 
GetSystemloadLevel()70 void SystemLoadStatus::GetSystemloadLevel()
71 {
72     loadstatus_ = ResourceSchedule::ResSchedClient::GetInstance().GetSystemloadLevel();
73     LOGI("GetSystemloadLevel finish, loadstatus:%{public}d", loadstatus_);
74 }
75 
Setload(int32_t load)76 void SystemLoadStatus::Setload(int32_t load)
77 {
78     loadstatus_ = load;
79 }
80 
InitSystemload(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)81 void SystemLoadStatus::InitSystemload(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
82 {
83     GetSystemloadLevel();
84     RegisterSystemloadCallback(dataSyncManager);
85 }
86 
IsLoadStatusUnderNormal(STOPPED_TYPE process)87 bool SystemLoadStatus::IsLoadStatusUnderNormal(STOPPED_TYPE process)
88 {
89     if (loadstatus_ > SYSTEMLOADLEVEL_NORMAL && process == STOPPED_IN_SYNC) {
90         LOGI("SetParameter TEMPERATURE_SYSPARAM_SYNC true");
91         system::SetParameter(TEMPERATURE_SYSPARAM_SYNC, "true");
92         return false;
93     }
94     return true;
95 }
96 
IsLoadStatusUnderHot(STOPPED_TYPE process,int32_t level)97 bool SystemLoadStatus::IsLoadStatusUnderHot(STOPPED_TYPE process, int32_t level)
98 {
99     if (loadstatus_ > level) {
100         if (process == STOPPED_IN_THUMB) {
101             LOGI("SetParameter TEMPERATURE_SYSPARAM_THUMB true");
102             system::SetParameter(TEMPERATURE_SYSPARAM_THUMB, "true");
103         } else if (process == STOPPED_IN_SYNC) {
104             LOGI("SetParameter TEMPERATURE_SYSPARAM_SYNC true");
105             system::SetParameter(TEMPERATURE_SYSPARAM_SYNC, "true");
106         }
107         return false;
108     }
109     return true;
110 }
111 }