• 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 #ifndef PANDA_RUNTIME_MEM_MEM_STATS_H_
17 #define PANDA_RUNTIME_MEM_MEM_STATS_H_
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <cstring>
22 
23 #include "libpandabase/macros.h"
24 #include "libpandabase/mem/base_mem_stats.h"
25 #include "libpandabase/os/mutex.h"
26 #include "runtime/include/mem/panda_string.h"
27 #include "runtime/mem/gc/gc_phase.h"
28 
29 namespace panda {
30 class BaseClass;
31 }  // namespace panda
32 
33 namespace panda::mem {
34 
35 class HeapManager;
36 
37 /**
38  * Class for recording memory usage in the VM. Allocators use this class for both cases: object allocation in heap and
39  * raw memory for VM needs as well. This class uses CRTP for storing additional information in DEBUG mode.
40  */
41 template <typename T>
42 class MemStats : public BaseMemStats {
43 public:
44     NO_COPY_SEMANTIC(MemStats);
45     NO_MOVE_SEMANTIC(MemStats);
46 
47     MemStats() = default;
48 
49     ~MemStats() override = default;
50 
51     void RecordAllocateObject(size_t size, SpaceType type_mem);
52 
53     void RecordMovedObjects(size_t total_object_num, size_t size, SpaceType type_mem);
54 
55     void RecordFreeObject(size_t object_size, SpaceType type_mem);
56 
57     void RecordFreeObjects(size_t total_object_num, size_t total_object_size, SpaceType type_mem);
58 
59     void RecordGCPauseStart();
60     void RecordGCPauseEnd();
61 
62     /**
63      *  Number of allocated objects for all time
64      */
65     [[nodiscard]] uint64_t GetTotalObjectsAllocated() const;
66 
67     /**
68      *  Number of freed objects for all time
69      */
70     [[nodiscard]] uint64_t GetTotalObjectsFreed() const;
71 
72     /**
73      *  Number of allocated large and regular (size <= FREELIST_MAX_ALLOC_SIZE) objects for all time
74      */
75     [[nodiscard]] uint64_t GetTotalRegularObjectsAllocated() const;
76 
77     /**
78      *  Number of freed large and regular (size <= FREELIST_MAX_ALLOC_SIZE) objects for all time
79      */
80     [[nodiscard]] uint64_t GetTotalRegularObjectsFreed() const;
81 
82     /**
83      *  Number of allocated humongous (size > FREELIST_MAX_ALLOC_SIZE) objects for all time
84      */
85     [[nodiscard]] uint64_t GetTotalHumongousObjectsAllocated() const;
86 
87     /**
88      *  Number of freed humongous (size > FREELIST_MAX_ALLOC_SIZE) objects for all time
89      */
90     [[nodiscard]] uint64_t GetTotalHumongousObjectsFreed() const;
91 
92     /**
93      *  Number of alive objects now
94      */
95     [[nodiscard]] uint64_t GetObjectsCountAlive() const;
96 
97     /**
98      *  Number of alive large and regular (size <= FREELIST_MAX_ALLOC_SIZE) objects now
99      */
100     [[nodiscard]] uint64_t GetRegularObjectsCountAlive() const;
101 
102     /**
103      *  Number of alive humongous (size > FREELIST_MAX_ALLOC_SIZE) objects now
104      */
105     [[nodiscard]] uint64_t GetHumonguousObjectsCountAlive() const;
106 
107     PandaString GetStatistics(HeapManager *heap_manager);
108 
109     uint64_t GetMinGCPause() const;
110     uint64_t GetMaxGCPause() const;
111     uint64_t GetAverageGCPause() const;
112     uint64_t GetTotalGCPause() const;
113 
114 protected:
115     using clock = std::chrono::high_resolution_clock;
116     using duration = std::chrono::duration<uint64_t, std::nano>;
117 
118 private:
119     clock::time_point pause_start_time_ = clock::now();
120     duration min_pause_ = duration(0);
121     duration max_pause_ = duration(0);
122     duration sum_pause_ = duration(0);
123     uint pause_count_ = 0;
124 
125     // make groups of different parts of the VM (JIT, interpreter, etc)
126     std::atomic_uint64_t objects_allocated_ = 0;
127     std::atomic_uint64_t objects_freed_ = 0;
128 
129     std::atomic_uint64_t humongous_objects_allocated_ = 0;
130     std::atomic_uint64_t humongous_objects_freed_ = 0;
131 };
132 
133 }  // namespace panda::mem
134 
135 #endif  // PANDA_RUNTIME_MEM_MEM_STATS_H_
136