1 /*
2 * Copyright (c) 2023-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
16 #include "libpandabase/taskmanager/task.h"
17 #include "libpandabase/taskmanager/task_scheduler.h"
18
19 namespace ark::taskmanager {
20
GetAndStoreTimeOfTaskAddingToQueue()21 void TaskLifeTimeAggregator::GetAndStoreTimeOfTaskAddingToQueue()
22 {
23 addingToQueueTime_ = time::GetCurrentTimeInMicros(false);
24 }
25
GetAndStoreTimeOfTaskExecutionStart()26 void TaskLifeTimeAggregator::GetAndStoreTimeOfTaskExecutionStart()
27 {
28 startExecutionTime_ = time::GetCurrentTimeInMicros(false);
29 }
30
GetTimeOfTaskExecutionFinishAndStoreTimeStats(TaskProperties prop)31 void TaskLifeTimeAggregator::GetTimeOfTaskExecutionFinishAndStoreTimeStats(TaskProperties prop)
32 {
33 auto *scheduler = TaskScheduler::GetTaskScheduler();
34 ASSERT(scheduler != nullptr);
35 auto *taskTimeStats = scheduler->GetTaskTimeStats();
36 ASSERT(taskTimeStats != nullptr);
37 auto endExecutionTime = time::GetCurrentTimeInMicros(false);
38 taskTimeStats->CollectLifeAndExecutionTimes(prop, endExecutionTime - addingToQueueTime_,
39 endExecutionTime - startExecutionTime_);
40 }
41
42 /* static */
Create(TaskProperties properties,RunnerCallback runner)43 Task Task::Create(TaskProperties properties, RunnerCallback runner)
44 {
45 Task task(properties, std::move(runner));
46 return task;
47 }
48
GetTaskProperties() const49 TaskProperties Task::GetTaskProperties() const
50 {
51 return properties_;
52 }
53
RunTask()54 void Task::RunTask()
55 {
56 ASSERT(!IsInvalid());
57 EventOnStartExecution();
58 runner_();
59 EventOnEndExecution();
60 }
61
MakeInvalid()62 void Task::MakeInvalid()
63 {
64 properties_ = INVALID_TASK_PROPERTIES;
65 runner_ = nullptr;
66 }
67
IsInvalid() const68 bool Task::IsInvalid() const
69 {
70 return properties_ == INVALID_TASK_PROPERTIES;
71 }
72
EventOnTaskAdding()73 void Task::EventOnTaskAdding()
74 {
75 auto *scheduler = TaskScheduler::GetTaskScheduler();
76 if UNLIKELY (scheduler == nullptr) {
77 return;
78 }
79 if (scheduler->IsTaskLifetimeStatisticsUsed()) {
80 lifeTimeStorage_.GetAndStoreTimeOfTaskAddingToQueue();
81 }
82 }
83
EventOnStartExecution()84 void Task::EventOnStartExecution()
85 {
86 auto *scheduler = TaskScheduler::GetTaskScheduler();
87 if UNLIKELY (scheduler == nullptr) {
88 return;
89 }
90 if (scheduler->IsTaskLifetimeStatisticsUsed()) {
91 lifeTimeStorage_.GetAndStoreTimeOfTaskExecutionStart();
92 }
93 }
94
EventOnEndExecution()95 void Task::EventOnEndExecution()
96 {
97 auto *scheduler = TaskScheduler::GetTaskScheduler();
98 if UNLIKELY (scheduler == nullptr) {
99 return;
100 }
101 if (scheduler->IsTaskLifetimeStatisticsUsed()) {
102 lifeTimeStorage_.GetTimeOfTaskExecutionFinishAndStoreTimeStats(properties_);
103 }
104 }
105
operator <<(std::ostream & os,TaskType type)106 std::ostream &operator<<(std::ostream &os, TaskType type)
107 {
108 switch (type) {
109 case TaskType::GC:
110 os << "TaskType::GC";
111 break;
112 case TaskType::JIT:
113 os << "TaskType::JIT";
114 break;
115 default:
116 UNREACHABLE();
117 break;
118 }
119 return os;
120 }
121
operator <<(std::ostream & os,VMType type)122 std::ostream &operator<<(std::ostream &os, VMType type)
123 {
124 switch (type) {
125 case VMType::DYNAMIC_VM:
126 os << "VMType::DYNAMIC_VM";
127 break;
128 case VMType::STATIC_VM:
129 os << "VMType::STATIC_VM";
130 break;
131 default:
132 UNREACHABLE();
133 break;
134 }
135 return os;
136 }
137
operator <<(std::ostream & os,TaskExecutionMode mode)138 std::ostream &operator<<(std::ostream &os, TaskExecutionMode mode)
139 {
140 switch (mode) {
141 case TaskExecutionMode::FOREGROUND:
142 os << "TaskExecutionMode::FOREGROUND";
143 break;
144 case TaskExecutionMode::BACKGROUND:
145 os << "TaskExecutionMode::BACKGROUND";
146 break;
147 default:
148 UNREACHABLE();
149 break;
150 }
151 return os;
152 }
153
operator <<(std::ostream & os,TaskProperties prop)154 std::ostream &operator<<(std::ostream &os, TaskProperties prop)
155 {
156 os << "{" << prop.GetTaskType() << "," << prop.GetVMType() << "," << prop.GetTaskExecutionMode() << "}";
157 return os;
158 }
159
160 } // namespace ark::taskmanager
161