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 "sched/task_manager.h"
17
18 #include "dfx/log/ffrt_log_api.h"
19
20 using namespace ffrt;
21
TaskStateCount(CPUEUTask * task)22 void TaskManager::TaskStateCount(CPUEUTask* task)
23 {
24 if (task->state.PreState() == task->state.CurState()) {
25 return;
26 }
27
28 DecreCount(taskCount[static_cast<size_t>(task->state.PreState())]);
29 IncreCount(taskCount[static_cast<size_t>(task->state.CurState())]);
30
31 CalcTaskTime(task);
32 }
33
CalcTaskTime(CPUEUTask * task)34 void TaskManager::CalcTaskTime(CPUEUTask* task)
35 {
36 auto calc = [](uint64_t time, uint64_t count, std::atomic_uint64_t& maxTime, std::atomic<double>& avgTime) {
37 if (time > maxTime) {
38 maxTime = time;
39 }
40
41 if (count <= 1) {
42 avgTime = time;
43 } else {
44 avgTime = avgTime + (time - avgTime) / count;
45 }
46 };
47
48 switch (task->state.CurState()) {
49 case TaskState::RUNNING:
50 calc(task->state.WaitingTime(), GetCount(TaskState::READY), maxWaitingTime, avgWaitingTime);
51 break;
52 case TaskState::EXITED: {
53 uint64_t count = GetCount(TaskState::EXITED);
54 calc(task->state.RunningTime(), count, maxRunningTime, avgRunningTime);
55 if ((count & 0x3FF) == 0) {
56 FFRT_LOGW(
57 "Task-done num: %lu; wait-time aver : %.2f us, max: %lu us; exec-time: aver %.2f us, max: %lu us",
58 count, avgWaitingTime.load(), maxWaitingTime.load(), avgRunningTime.load(), maxRunningTime.load());
59 }
60 break;
61 }
62 default:
63 break;
64 }
65 }