• 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 "libpandabase/taskmanager/task_statistics/lock_free_task_statistics_impl.h"
17 #include <atomic>
18 
19 namespace panda::taskmanager {
20 
LockFreeTaskStatisticsImpl()21 LockFreeTaskStatisticsImpl::LockFreeTaskStatisticsImpl()
22 {
23     for (const auto &properties : allTaskProperties_) {
24         taskInSystemCounterMap_[properties] = 0;
25         for (const auto &status : ALL_TASK_STATUSES) {
26             taskPropertiesCounterMap_[status][properties] = 0;
27         }
28     }
29 }
30 
IncrementCount(TaskStatus status,TaskProperties properties,size_t count)31 void LockFreeTaskStatisticsImpl::IncrementCount(TaskStatus status, TaskProperties properties, size_t count)
32 {
33     // Atomic with acq_rel order reason: sync for counter
34     taskPropertiesCounterMap_.at(status).at(properties).fetch_add(count, std::memory_order_acq_rel);
35 
36     switch (status) {
37         case TaskStatus::ADDED:
38             // Atomic with acq_rel order reason: sync for counter
39             taskInSystemCounterMap_.at(properties).fetch_add(count, std::memory_order_acq_rel);
40             break;
41         case TaskStatus::EXECUTED:
42             // Atomic with acquire order reason: sync for counter
43             ASSERT(taskInSystemCounterMap_.at(properties).load(std::memory_order_acquire) >= count);
44             // Atomic with acq_rel order reason: sync for counter
45             taskInSystemCounterMap_.at(properties).fetch_sub(count, std::memory_order_acq_rel);
46             break;
47         case TaskStatus::POPPED:
48             // Atomic with acquire order reason: sync for counter
49             ASSERT(taskInSystemCounterMap_.at(properties).load(std::memory_order_acquire) >= count);
50             // Atomic with acq_rel order reason: sync for counter
51             taskInSystemCounterMap_.at(properties).fetch_sub(count, std::memory_order_acq_rel);
52             break;
53         default:
54             UNREACHABLE();
55     }
56 }
57 
GetCount(TaskStatus status,TaskProperties properties) const58 size_t LockFreeTaskStatisticsImpl::GetCount(TaskStatus status, TaskProperties properties) const
59 {
60     // Atomic with acquire order reason: sync for counter
61     return taskPropertiesCounterMap_.at(status).at(properties).load(std::memory_order_acquire);
62 }
63 
GetCountOfTaskInSystem() const64 size_t LockFreeTaskStatisticsImpl::GetCountOfTaskInSystem() const
65 {
66     size_t taskInSystemCount = 0;
67     for (const auto &properties : allTaskProperties_) {
68         taskInSystemCount += GetCountOfTasksInSystemWithTaskProperties(properties);
69     }
70     return taskInSystemCount;
71 }
72 
GetCountOfTasksInSystemWithTaskProperties(TaskProperties properties) const73 size_t LockFreeTaskStatisticsImpl::GetCountOfTasksInSystemWithTaskProperties(TaskProperties properties) const
74 {
75     // Atomic with acquire order reason: sync for counter
76     return taskInSystemCounterMap_.at(properties).load(std::memory_order_acquire);
77 }
78 
ResetAllCounters()79 void LockFreeTaskStatisticsImpl::ResetAllCounters()
80 {
81     for (const auto &properties : allTaskProperties_) {
82         ResetCountersWithTaskProperties(properties);
83     }
84 }
85 
ResetCountersWithTaskProperties(TaskProperties properties)86 void LockFreeTaskStatisticsImpl::ResetCountersWithTaskProperties(TaskProperties properties)
87 {
88     for (const auto &state : ALL_TASK_STATUSES) {
89         // Atomic with release order reason: sync for counter
90         taskPropertiesCounterMap_.at(state).at(properties).store(0, std::memory_order_release);
91     }
92 }
93 
94 }  // namespace panda::taskmanager
95