1 /** 2 * Copyright (c) 2021-2024 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 #ifndef PANDA_RUNTIME_MEM_STATS_H 16 #define PANDA_RUNTIME_MEM_STATS_H 17 18 #include <chrono> 19 #include <cstdint> 20 #include <cstring> 21 22 #include "libpandabase/macros.h" 23 #include "libpandabase/mem/base_mem_stats.h" 24 #include "libpandabase/os/mutex.h" 25 #include "runtime/include/mem/panda_string.h" 26 #include "runtime/mem/gc/gc_phase.h" 27 28 namespace ark { 29 class BaseClass; 30 } // namespace ark 31 32 namespace ark::mem { 33 34 class HeapManager; 35 36 /** 37 * Class for recording memory usage in the VM. Allocators use this class for both cases: object allocation in heap and 38 * raw memory for VM needs as well. This class uses CRTP for storing additional information in DEBUG mode. 39 */ 40 template <typename T> 41 class MemStats : public BaseMemStats { 42 public: 43 NO_COPY_SEMANTIC(MemStats); 44 NO_MOVE_SEMANTIC(MemStats); 45 46 MemStats() = default; 47 48 ~MemStats() override = default; 49 50 void RecordAllocateObject(size_t size, SpaceType typeMem); 51 52 void RecordAllocateObjects(size_t totalObjectNum, size_t totalObjectSize, SpaceType typeMem); 53 54 void RecordYoungMovedObjects(size_t youngObjectNum, size_t size, SpaceType typeMem); 55 56 void RecordTenuredMovedObjects(size_t tenuredObjectNum, size_t size, SpaceType typeMem); 57 58 void RecordFreeObject(size_t objectSize, SpaceType typeMem); 59 60 void RecordFreeObjects(size_t totalObjectNum, size_t totalObjectSize, SpaceType typeMem); 61 62 /// Number of allocated objects for all time 63 [[nodiscard]] uint64_t GetTotalObjectsAllocated() const; 64 65 /// Number of freed objects for all time 66 [[nodiscard]] uint64_t GetTotalObjectsFreed() const; 67 68 /// Number of allocated large and regular (size <= FREELIST_MAX_ALLOC_SIZE) objects for all time 69 [[nodiscard]] uint64_t GetTotalRegularObjectsAllocated() const; 70 71 /// Number of freed large and regular (size <= FREELIST_MAX_ALLOC_SIZE) objects for all time 72 [[nodiscard]] uint64_t GetTotalRegularObjectsFreed() const; 73 74 /// Number of allocated humongous (size > FREELIST_MAX_ALLOC_SIZE) objects for all time 75 [[nodiscard]] uint64_t GetTotalHumongousObjectsAllocated() const; 76 77 /// Number of freed humongous (size > FREELIST_MAX_ALLOC_SIZE) objects for all time 78 [[nodiscard]] uint64_t GetTotalHumongousObjectsFreed() const; 79 80 /// Number of alive objects now 81 [[nodiscard]] uint64_t GetObjectsCountAlive() const; 82 83 /// Number of alive large and regular (size <= FREELIST_MAX_ALLOC_SIZE) objects now 84 [[nodiscard]] uint64_t GetRegularObjectsCountAlive() const; 85 86 /// Number of alive humongous (size > FREELIST_MAX_ALLOC_SIZE) objects now 87 [[nodiscard]] uint64_t GetHumonguousObjectsCountAlive() const; 88 89 /// Number of moved bytes in last gc 90 [[nodiscard]] uint64_t GetLastYoungObjectsMovedBytes() const; 91 ClearLastYoungObjectsMovedBytes()92 void ClearLastYoungObjectsMovedBytes() 93 { 94 lastYoungObjectsMovedBytes_ = 0; 95 } 96 97 PandaString GetStatistics(); 98 99 private: 100 std::atomic_uint64_t lastYoungObjectsMovedBytes_ = 0; 101 102 // make groups of different parts of the VM (JIT, interpreter, etc) 103 std::atomic_uint64_t objectsAllocated_ = 0; 104 std::atomic_uint64_t objectsFreed_ = 0; 105 106 std::atomic_uint64_t humongousObjectsAllocated_ = 0; 107 std::atomic_uint64_t humongousObjectsFreed_ = 0; 108 }; 109 110 } // namespace ark::mem 111 #endif // PANDA_RUNTIME_MEM_STATS_H 112