• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "libpandabase/taskmanager/task.h"
17 #include "libpandabase/utils/time.h"
18 #include "libpandabase/taskmanager/schedulable_task_queue_interface.h"
19 
20 namespace ark::taskmanager::internal {
21 
GetAndStoreTimeOfTaskAddingToQueue()22 void TaskLifeTimeAggregator::GetAndStoreTimeOfTaskAddingToQueue()
23 {
24     addingToQueueTime_ = time::GetCurrentTimeInMicros(false);
25 }
26 
GetAndStoreTimeOfTaskExecutionStart()27 void TaskLifeTimeAggregator::GetAndStoreTimeOfTaskExecutionStart()
28 {
29     startExecutionTime_ = time::GetCurrentTimeInMicros(false);
30 }
31 
GetTimeOfTaskExecutionFinishAndStoreTimeStats(TaskQueueInterface * queue)32 void TaskLifeTimeAggregator::GetTimeOfTaskExecutionFinishAndStoreTimeStats(TaskQueueInterface *queue)
33 {
34     auto *taskTimeStats = static_cast<SchedulableTaskQueueInterface *>(queue)->GetTaskTimeStats();
35     ASSERT(taskTimeStats != nullptr);
36     auto endExecutionTime = time::GetCurrentTimeInMicros(false);
37     taskTimeStats->CollectLifeAndExecutionTimes(queue->GetQueueId(), endExecutionTime - addingToQueueTime_,
38                                                 endExecutionTime - startExecutionTime_);
39 }
40 
41 /* static */
Create(RunnerCallback runner,TaskQueueInterface * queue,OnDestructionCallback callback)42 Task::Ptr Task::Create(RunnerCallback runner, TaskQueueInterface *queue, OnDestructionCallback callback)
43 {
44     // Task can be created only by TaskQueue so callback should be controled from outside.
45     ASSERT(callback != nullptr);
46     // CC-OFFNXT(G.RES.09-CPP): constructor of Task class is private
47     return std::unique_ptr<Task>(new Task(std::move(runner), queue, callback));
48 }
49 
~Task()50 Task::~Task()
51 {
52     onDestructionCallback_(parentQueue_);
53 }
54 
RunTask()55 void Task::RunTask()
56 {
57     ASSERT(!IsInvalid());
58     EventOnStartExecution();
59     runner_();
60     EventOnEndExecution();
61 }
62 
MakeInvalid()63 void Task::MakeInvalid()
64 {
65     runner_ = nullptr;
66 }
67 
IsInvalid() const68 bool Task::IsInvalid() const
69 {
70     return runner_ == nullptr;
71 }
72 
EventOnTaskAdding()73 void Task::EventOnTaskAdding()
74 {
75     if (static_cast<SchedulableTaskQueueInterface *>(parentQueue_)->GetTaskTimeStats() != nullptr) {
76         lifeTimeStorage_.GetAndStoreTimeOfTaskAddingToQueue();
77     }
78 }
79 
EventOnStartExecution()80 void Task::EventOnStartExecution()
81 {
82     if (static_cast<SchedulableTaskQueueInterface *>(parentQueue_)->GetTaskTimeStats() != nullptr) {
83         lifeTimeStorage_.GetAndStoreTimeOfTaskExecutionStart();
84     }
85 }
86 
EventOnEndExecution()87 void Task::EventOnEndExecution()
88 {
89     if (static_cast<SchedulableTaskQueueInterface *>(parentQueue_)->GetTaskTimeStats() != nullptr) {
90         lifeTimeStorage_.GetTimeOfTaskExecutionFinishAndStoreTimeStats(parentQueue_);
91     }
92 }
93 
94 }  // namespace ark::taskmanager::internal
95