1 /* 2 * Copyright (c) 2025 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 COMMON_COMPONENTS_HEAP_COLLECTOR_STATS_H 17 #define COMMON_COMPONENTS_HEAP_COLLECTOR_STATS_H 18 19 #include <algorithm> 20 #include <atomic> 21 #include <list> 22 #include <memory> 23 #include <mutex> 24 25 #include "common_components/base/immortal_wrapper.h" 26 #include "common_components/heap/collector/gc_request.h" 27 #include "common_components/log/log.h" 28 #include "common_interfaces/base_runtime.h" 29 30 namespace common { 31 // statistics for previous gc. 32 class GCStats { 33 public: 34 GCStats() = default; 35 ~GCStats() = default; 36 37 void Init(); 38 GetThreshold()39 size_t GetThreshold() const { return heapThreshold; } 40 TotalSTWTime()41 inline uint64_t TotalSTWTime() const { return totalSTWTime; } 42 MaxSTWTime()43 inline uint64_t MaxSTWTime() const { return maxSTWTime; } 44 recordSTWTime(uint64_t time)45 void recordSTWTime(uint64_t time) 46 { 47 totalSTWTime += time; 48 maxSTWTime = std::max(maxSTWTime, time); 49 } 50 51 void Dump() const; 52 GetPrevGCStartTime()53 static uint64_t GetPrevGCStartTime() { return prevGcStartTime; } 54 SetPrevGCStartTime(uint64_t timestamp)55 static void SetPrevGCStartTime(uint64_t timestamp) { prevGcStartTime = timestamp; } 56 GetPrevGCFinishTime()57 static uint64_t GetPrevGCFinishTime() { return prevGcFinishTime; } 58 SetPrevGCFinishTime(uint64_t timestamp)59 static void SetPrevGCFinishTime(uint64_t timestamp) { prevGcFinishTime = timestamp; } 60 GetAccumulatedFreeSize()61 size_t GetAccumulatedFreeSize() const { return accumulatedFreeSize; } 62 IncreaseAccumulatedFreeSize(size_t size)63 void IncreaseAccumulatedFreeSize(size_t size) { accumulatedFreeSize += size; } 64 65 static uint64_t prevGcStartTime; 66 static uint64_t prevGcFinishTime; 67 68 GCReason reason; 69 GCType gcType; 70 bool isConcurrentMark; 71 bool async; 72 73 uint64_t gcStartTime; 74 uint64_t gcEndTime; 75 76 uint64_t totalSTWTime; // total stw time(microseconds) 77 uint64_t maxSTWTime; // max stw time(microseconds) 78 79 size_t liveBytesBeforeGC; 80 size_t liveBytesAfterGC; 81 82 size_t fromSpaceSize; 83 size_t smallGarbageSize; 84 85 size_t pinnedSpaceSize; 86 size_t pinnedGarbageSize; 87 88 size_t largeSpaceSize; 89 size_t largeGarbageSize; 90 91 size_t collectedBytes; 92 size_t collectedObjects; 93 94 size_t accumulatedFreeSize; 95 96 double garbageRatio; 97 double collectionRate; // bytes per nano-second 98 99 size_t heapThreshold; 100 size_t targetFootprint; 101 102 // Use for heuristic request, set by last gc status. 103 bool shouldRequestYoung; 104 isYoungGC()105 bool isYoungGC() 106 { 107 return reason == GCReason::GC_REASON_YOUNG; 108 } 109 }; 110 extern size_t g_gcCount; 111 extern uint64_t g_gcTotalTimeUs; 112 extern size_t g_gcCollectedTotalBytes; 113 114 extern size_t g_fullGCCount; 115 extern double g_fullGCMeanRate; 116 } // namespace common 117 118 #endif // COMMON_COMPONENTS_HEAP_COLLECTOR_STATS_H 119