• 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 #ifndef FFRT_TASK_MANAGER_H
17 #define FFRT_TASK_MANAGER_H
18 
19 #include <array>
20 #include <atomic>
21 
22 #include "core/task_ctx.h"
23 
24 namespace ffrt {
25 
26 class TaskManager {
27 public:
Instance()28     static TaskManager& Instance()
29     {
30         static TaskManager manager;
31         return manager;
32     }
33 
GetCount(TaskState::State state)34     uint64_t GetCount(TaskState::State state) const
35     {
36         return taskCount[static_cast<size_t>(state)];
37     }
38 
39     void TaskStateCount(TaskCtx* task);
40 
41 private:
42     TaskManager() = default;
43 
IncreCount(std::atomic_uint64_t & count)44     void IncreCount(std::atomic_uint64_t& count)
45     {
46         count.fetch_add(1, std::memory_order_relaxed);
47     }
48 
DecreCount(std::atomic_uint64_t & count)49     void DecreCount(std::atomic_uint64_t& count)
50     {
51         while (true) {
52             auto old = count.load();
53             if (old <= 0) {
54                 break;
55             }
56 
57             if (count.compare_exchange_weak(old, old - 1, std::memory_order_relaxed)) {
58                 break;
59             }
60         }
61     }
62 
63     inline void CalcTaskTime(TaskCtx* task);
64 
65     std::array<std::atomic_uint64_t, static_cast<size_t>(TaskState::MAX)> taskCount;
66 
67     std::atomic_uint64_t maxWaitingTime;
68     std::atomic<double> avgWaitingTime;
69 
70     std::atomic_uint64_t maxRunningTime;
71     std::atomic<double> avgRunningTime;
72 };
73 
74 } // namespace ffrt
75 
76 #endif