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_request.h"
16
17 #include "common_components/base/time_utils.h"
18 #include "common_components/heap/collector/gc_stats.h"
19 namespace common {
20 namespace {
21 // Set a safe initial value so that the first GC is able to trigger.
22 uint64_t g_initHeuTriggerTimestamp = TimeUtil::NanoSeconds() - LONG_MIN_HEU_GC_INTERVAL_NS;
23 uint64_t g_initNativeTriggerTimestamp = TimeUtil::NanoSeconds() - MIN_ASYNC_GC_INTERVAL_NS;
24 } // namespace
25
IsFrequentGC() const26 inline bool GCRequest::IsFrequentGC() const
27 {
28 if (minIntervelNs == 0) {
29 return false;
30 }
31 int64_t now = static_cast<int64_t>(TimeUtil::NanoSeconds());
32 return (now - prevRequestTime < minIntervelNs);
33 }
34
IsFrequentAsyncGC() const35 inline bool GCRequest::IsFrequentAsyncGC() const
36 {
37 uint64_t now = TimeUtil::NanoSeconds();
38 return (now - GCStats::GetPrevGCFinishTime()) < minIntervelNs;
39 }
40
41 // heuristic gc is triggered by object allocation,
42 // the heap stats should take into consideration.
IsFrequentHeuristicGC() const43 inline bool GCRequest::IsFrequentHeuristicGC() const { return IsFrequentAsyncGC(); }
44
ShouldBeIgnored() const45 bool GCRequest::ShouldBeIgnored() const
46 {
47 switch (reason) {
48 case GC_REASON_HEU:
49 case GC_REASON_YOUNG:
50 case GC_REASON_BACKGROUND:
51 case GC_REASON_HINT:
52 return IsFrequentHeuristicGC();
53 case GC_REASON_NATIVE:
54 return IsFrequentAsyncGC();
55 case GC_REASON_OOM:
56 case GC_REASON_FORCE:
57 return IsFrequentGC();
58 default:
59 return false;
60 }
61 }
62
63 GCRequest g_gcRequests[] = {
64 { GC_REASON_USER, "user", false, true, 0, 0 },
65 { GC_REASON_OOM, "oom", true, false, 0, 0 },
66 { GC_REASON_BACKUP, "backup", true, false, 0, 0 },
67 { GC_REASON_HEU, "heuristic", false, true, LONG_MIN_HEU_GC_INTERVAL_NS, g_initHeuTriggerTimestamp },
68 { GC_REASON_YOUNG, "young", false, true, LONG_MIN_HEU_GC_INTERVAL_NS, g_initHeuTriggerTimestamp },
69 { GC_REASON_NATIVE, "native_alloc", false, true, MIN_ASYNC_GC_INTERVAL_NS, g_initNativeTriggerTimestamp },
70 { GC_REASON_HEU_SYNC, "heuristic_sync", true, true, 0, 0 },
71 { GC_REASON_NATIVE_SYNC, "native_alloc_sync", true, true, 0, 0 },
72 { GC_REASON_FORCE, "force", true, false, 0, 0 },
73 { GC_REASON_APPSPAWN, "appspawn", true, false, 0, 0 },
74 { GC_REASON_BACKGROUND, "backgound", false, true, LONG_MIN_HEU_GC_INTERVAL_NS, g_initHeuTriggerTimestamp },
75 { GC_REASON_HINT, "hint", false, true, LONG_MIN_HEU_GC_INTERVAL_NS, g_initHeuTriggerTimestamp },
76 { GC_REASON_IDLE, "idle", false, true, LONG_MIN_HEU_GC_INTERVAL_NS, g_initHeuTriggerTimestamp }
77 };
78 } // namespace common
79