1 /**
2  * Copyright (c) 2021-2022 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 LIBPANDABASE_MEM_BASE_MEM_STATS_H
17 #define LIBPANDABASE_MEM_BASE_MEM_STATS_H
18 
19 #include "os/mutex.h"
20 #include <cstdio>
21 #include "macros.h"
22 #include "space.h"
23 
24 #include <array>
25 #include <atomic>
26 
27 WEAK_FOR_LTO_START
28 
29 namespace panda {
30 
31 class BaseMemStats {
32 public:
33     NO_COPY_SEMANTIC(BaseMemStats);
34     NO_MOVE_SEMANTIC(BaseMemStats);
35 
36     BaseMemStats() = default;
37     virtual ~BaseMemStats() = default;
38 
39     void RecordAllocateRaw(size_t size, SpaceType type_mem);
40 
41     // TODO(alovkov): call RecordFreeRaw when CodeAllocator supports deallocate
42     // TODO: call RecordFreeRaw when ArenaAllocator supports deallocate
43     void RecordFreeRaw(size_t size, SpaceType type_mem);
44 
45     // getters
46     [[nodiscard]] uint64_t GetAllocated(SpaceType type_mem) const;
47     [[nodiscard]] uint64_t GetFreed(SpaceType type_mem) const;
48     [[nodiscard]] uint64_t GetFootprint(SpaceType type_mem) const;
49 
50     [[nodiscard]] uint64_t GetAllocatedHeap() const;
51     [[nodiscard]] uint64_t GetFreedHeap() const;
52     [[nodiscard]] uint64_t GetFootprintHeap() const;
53     [[nodiscard]] uint64_t GetTotalFootprint() const;
54 
55 protected:
56     void RecordAllocate(size_t size, SpaceType type_mem);
57     void RecordMoved(size_t size, SpaceType type_mem);
58     void RecordFree(size_t size, SpaceType type_mem);
59 
60 private:
61     std::array<std::atomic_uint64_t, SPACE_TYPE_SIZE> allocated_ {0};
62     std::array<std::atomic_uint64_t, SPACE_TYPE_SIZE> freed_ {0};
63 };
64 
65 }  // namespace panda
66 
67 WEAK_FOR_LTO_END
68 
69 #endif  // LIBPANDABASE_MEM_BASE_MEM_STATS_H
70