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 #include "common_components/heap/collector/gc_stats.h"
16
17 #include "common_interfaces/base_runtime.h"
18 #include "common_components/base/time_utils.h"
19 #include "common_components/heap/heap.h"
20 #include "common_components/log/log.h"
21
22 namespace common {
23 size_t g_gcCount = 0;
24 uint64_t g_gcTotalTimeUs = 0;
25 size_t g_gcCollectedTotalBytes = 0;
26
27 size_t g_fullGCCount = 0;
28 double g_fullGCMeanRate = 0.0;
29
30 uint64_t GCStats::prevGcStartTime = TimeUtil::NanoSeconds() - LONG_MIN_HEU_GC_INTERVAL_NS;
31 uint64_t GCStats::prevGcFinishTime = TimeUtil::NanoSeconds() - LONG_MIN_HEU_GC_INTERVAL_NS;
32
Init()33 void GCStats::Init()
34 {
35 isConcurrentMark = false;
36 async = false;
37 gcStartTime = TimeUtil::NanoSeconds();
38 gcEndTime = TimeUtil::NanoSeconds();
39
40 totalSTWTime = 0;
41 maxSTWTime = 0;
42
43 collectedObjects = 0;
44 collectedBytes = 0;
45
46 fromSpaceSize = 0;
47 smallGarbageSize = 0;
48
49 pinnedSpaceSize = 0;
50 pinnedGarbageSize = 0;
51
52 largeSpaceSize = 0;
53 largeGarbageSize = 0;
54
55 liveBytesBeforeGC = 0;
56 liveBytesAfterGC = 0;
57
58 garbageRatio = 0.0;
59 collectionRate = 0.0;
60
61 // 20 MB:set 20 MB as intial value
62 heapThreshold = std::min(BaseRuntime::GetInstance()->GetGCParam().gcThreshold, 20 * MB);
63 // 0.2:set 20% heap size as intial value
64 heapThreshold = std::min(static_cast<size_t>(Heap::GetHeap().GetMaxCapacity() * 0.2), heapThreshold);
65
66 targetFootprint = heapThreshold;
67 shouldRequestYoung = false;
68 }
69
Dump() const70 void GCStats::Dump() const
71 {
72 // Print a summary of the last GC.
73 size_t liveSize = Heap::GetHeap().GetAllocatedSize();
74 size_t heapSize = Heap::GetHeap().GetUsedPageSize();
75 double utilization = (heapSize == 0) ? 0 : (static_cast<double>(liveSize) / heapSize);
76 // Do not change this GC log format.
77 // Output one line statistic info after each gc task,
78 // include the gc type, collected objects and current heap utilization, etc.
79 // display to std-output. take care to modify.
80 std::string maxSTWTime = PrettyOrderMathNano(MaxSTWTime(), "s");
81 std::string totalSTWTime = PrettyOrderMathNano(TotalSTWTime(), "s");
82 std::string totalGCTime = PrettyOrderMathNano(gcEndTime - gcStartTime, "s");
83 std::ostringstream oss;
84 oss <<
85 "GC for " << g_gcRequests[reason].name << ": " << (async ? "async:" : "sync: ") <<
86 "gcType: " << GCTypeToString(gcType) << ", collected bytes: " <<
87 collectedBytes << "->" << PrettyOrderInfo(collectedBytes, "B") << ", " <<
88 "->" << PrettyOrderInfo(liveSize, "B") << "/" << heapSize << "->" <<
89 PrettyOrderInfo(heapSize, "B") << ", max pause: " << MaxSTWTime() <<
90 "->" << maxSTWTime << ", total pause: " << TotalSTWTime() << "->" << totalSTWTime <<
91 ", total GC time: " << (gcEndTime - gcStartTime) << "->" << totalGCTime;
92 VLOG(INFO, oss.str().c_str());
93 VLOG(DEBUG, "allocated size: %s, heap size: %s, heap utilization: %.2f%%", Pretty(liveSize).c_str(),
94 Pretty(heapSize).c_str(), utilization * 100); // 100 for percentage.
95
96 OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "CMCGC::GCStatsDump", (
97 "collectedObjects:" + std::to_string(collectedObjects) +
98 ";collectedBytes:" + std::to_string(collectedBytes) +
99 ";liveSize:" + std::to_string(liveSize) +
100 ";heapSize:" + std::to_string(heapSize) +
101 ";max pause:" + maxSTWTime +
102 ";total pause:" + totalSTWTime +
103 ";total GC time:" + totalGCTime
104 ).c_str());
105 }
106 } // namespace common
107