• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "aot/aot_loop_task.h"
17 
18 #include <chrono>
19 #include <string>
20 #include <thread>
21 
22 #include "aot/aot_handler.h"
23 #include "bundle_memory_guard.h"
24 #include "ffrt.h"
25 #include "parameters.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const std::string AOT_INTERVAL = "bms.aot.idle.interval";
31 constexpr uint32_t EIGHT_HOURS_MS = 8 * 60 * 60 * 1000;
32 const std::string AOT_TASK = "AOTTask";
33 }
34 
GetAOTIdleInterval()35 uint32_t AOTLoopTask::GetAOTIdleInterval()
36 {
37     uint32_t interval = EIGHT_HOURS_MS;
38     std::string str = system::GetParameter(AOT_INTERVAL, "");
39     if (!str.empty()) {
40         try {
41             interval = static_cast<uint32_t>(std::stoi(str));
42         } catch (...) {
43             APP_LOGE("convert AOT_INTERVAL failed");
44         }
45     }
46     APP_LOGI("aot idle interval ms : %{public}u", interval);
47     return interval;
48 }
49 
ScheduleLoopTask()50 void AOTLoopTask::ScheduleLoopTask()
51 {
52     APP_LOGI("ScheduleLoopTask begin");
53     std::weak_ptr<AOTLoopTask> weakPtr = shared_from_this();
54     auto task = [weakPtr]() {
55         BundleMemoryGuard memoryGuard;
56         while (true) {
57             auto sharedPtr = weakPtr.lock();
58             if (sharedPtr == nullptr) {
59                 APP_LOGD("stop AOT task");
60                 break;
61             }
62             AOTHandler::GetInstance().HandleIdle();
63             ffrt::this_task::sleep_for(std::chrono::milliseconds(AOTLoopTask::GetAOTIdleInterval()));
64         }
65         APP_LOGD("AOT task done");
66     };
67     serialQueue_->ScheduleDelayTask(AOT_TASK, AOTLoopTask::GetAOTIdleInterval(), task);
68 }
69 }  // namespace AppExecFwk
70 }  // namespace OHOS