• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "base_mem_stats.h"
17 
18 #include "os/mutex.h"
19 #include "utils/logger.h"
20 #include "utils/type_helpers.h"
21 
22 #include <numeric>
23 namespace panda {
24 
RecordAllocateRaw(size_t size,SpaceType type_mem)25 void BaseMemStats::RecordAllocateRaw(size_t size, SpaceType type_mem)
26 {
27     ASSERT(!IsHeapSpace(type_mem));
28     RecordAllocate(size, type_mem);
29 }
30 
RecordAllocate(size_t size,SpaceType type_mem)31 void BaseMemStats::RecordAllocate(size_t size, SpaceType type_mem)
32 {
33     auto index = helpers::ToUnderlying(type_mem);
34     allocated_[index].fetch_add(size, std::memory_order_acq_rel);
35 }
36 
RecordMoved(size_t size,SpaceType type_mem)37 void BaseMemStats::RecordMoved(size_t size, SpaceType type_mem)
38 {
39     auto index = helpers::ToUnderlying(type_mem);
40     uint64_t old_value = allocated_[index].fetch_sub(size, std::memory_order_acq_rel);
41     (void)old_value;
42     ASSERT(old_value >= size);
43 }
44 
RecordFreeRaw(size_t size,SpaceType type_mem)45 void BaseMemStats::RecordFreeRaw(size_t size, SpaceType type_mem)
46 {
47     ASSERT(!IsHeapSpace(type_mem));
48     RecordFree(size, type_mem);
49 }
50 
RecordFree(size_t size,SpaceType type_mem)51 void BaseMemStats::RecordFree(size_t size, SpaceType type_mem)
52 {
53     auto index = helpers::ToUnderlying(type_mem);
54     freed_[index].fetch_add(size, std::memory_order_acq_rel);
55 }
56 
GetAllocated(SpaceType type_mem) const57 uint64_t BaseMemStats::GetAllocated(SpaceType type_mem) const
58 {
59     return allocated_[helpers::ToUnderlying(type_mem)].load(std::memory_order_acquire);
60 }
61 
GetFreed(SpaceType type_mem) const62 uint64_t BaseMemStats::GetFreed(SpaceType type_mem) const
63 {
64     return freed_[helpers::ToUnderlying(type_mem)].load(std::memory_order_acquire);
65 }
66 
GetAllocatedHeap() const67 uint64_t BaseMemStats::GetAllocatedHeap() const
68 {
69     uint64_t result = 0;
70     for (size_t index = 0; index < SPACE_TYPE_SIZE; index++) {
71         SpaceType type = ToSpaceType(index);
72         if (IsHeapSpace(type)) {
73             result += allocated_[index].load(std::memory_order_acquire);
74         }
75     }
76     return result;
77 }
78 
GetFreedHeap() const79 uint64_t BaseMemStats::GetFreedHeap() const
80 {
81     uint64_t result = 0;
82     for (size_t index = 0; index < SPACE_TYPE_SIZE; index++) {
83         SpaceType type = ToSpaceType(index);
84         if (IsHeapSpace(type)) {
85             result += freed_[index].load(std::memory_order_acquire);
86         }
87     }
88     return result;
89 }
90 
GetFootprintHeap() const91 uint64_t BaseMemStats::GetFootprintHeap() const
92 {
93     return helpers::UnsignedDifferenceUint64(GetAllocatedHeap(), GetFreedHeap());
94 }
95 
GetFootprint(SpaceType type_mem) const96 uint64_t BaseMemStats::GetFootprint(SpaceType type_mem) const
97 {
98     auto index = helpers::ToUnderlying(type_mem);
99     LOG_IF(allocated_[index].load(std::memory_order_acquire) < freed_[index].load(std::memory_order_acquire), FATAL, GC)
100         << "Allocated < Freed (mem type = " << std::dec << static_cast<size_t>(index)
101         << "): " << allocated_[index].load(std::memory_order_acquire) << " < "
102         << freed_[index].load(std::memory_order_acquire);
103     return allocated_[index].load(std::memory_order_acquire) - freed_[index].load(std::memory_order_acquire);
104 }
105 
GetTotalFootprint() const106 uint64_t BaseMemStats::GetTotalFootprint() const
107 {
108     return std::accumulate(begin(allocated_), end(allocated_), 0UL) - std::accumulate(begin(freed_), end(freed_), 0UL);
109 }
110 
111 }  // namespace panda
112