• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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 
16 #ifndef PANDA_MEM_GC_G1_G1_ANALYTICS_H
17 #define PANDA_MEM_GC_G1_G1_ANALYTICS_H
18 
19 #include <atomic>
20 #include "runtime/include/gc_task.h"
21 #include "runtime/mem/gc/g1/g1_predictions.h"
22 #include "runtime/mem/gc/g1/collection_set.h"
23 
24 namespace ark::mem {
25 class G1Analytics {
26 public:
27     explicit G1Analytics(uint64_t now);
28 
29     void ReportCollectionStart(uint64_t time);
30     void ReportCollectionEnd(GCTaskCause cause, uint64_t endTime, const CollectionSet &collectionSet,
31                              bool singlePassCompactionEnabled, bool dump = false);
32 
33     void ReportEvacuatedBytes(size_t bytes);
34     void ReportRemsetSize(size_t remsetSize, size_t remsetRefsCount);
35     void ReportScanDirtyCardsStart(uint64_t time);
36     void ReportScanDirtyCardsEnd(uint64_t time, size_t dirtyCardsCount);
37     void ReportMarkingStart(uint64_t time);
38     void ReportMarkingEnd(uint64_t time, size_t remsetRefsCount);
39     void ReportEvacuationStart(uint64_t time);
40     void ReportEvacuationEnd(uint64_t time);
41     void ReportUpdateRefsStart(uint64_t time);
42     void ReportUpdateRefsEnd(uint64_t time);
43     void ReportPromotedRegion();
44     void ReportLiveObjects(size_t num);
45     void ReportSurvivedBytesRatio(const CollectionSet &collectionSet);
46     void ReportScanRemsetTime(size_t remsetSize, uint64_t time);
47     double PredictAllocationRate() const;
48     uint64_t PredictYoungCollectionTimeInMicros(size_t edenLength) const;
49     uint64_t PredictYoungCollectionTimeInMicros(const CollectionSet &collectionSet) const;
50     uint64_t PredictOldCollectionTimeInMicros(size_t remsetSize, size_t liveBytes, size_t liveObjects) const;
51     uint64_t PredictOldCollectionTimeInMicros(Region *region) const;
52     uint64_t PredictScanDirtyCardsTime(size_t dirtyCardsCount) const;
53     double PredictSurvivedBytesRatio() const;
54 
ReportPredictedMixedPause(uint64_t time)55     void ReportPredictedMixedPause(uint64_t time)
56     {
57         predictedMixedPause_ = time;
58     }
59 
60 private:
61     enum StatType { MARKING_COLLECTION = 0, SINGLE_PASS_COMPACTION, COUNT };
62     size_t GetPromotedRegions() const;
63     size_t GetEvacuatedBytes() const;
64     double PredictPromotedRegions(size_t edenLength) const;
65     uint64_t EstimatePromotionTimeInMicros(size_t promotedRegions) const;
66     uint64_t PredictUpdateRefsTimeInMicros(size_t liveObjects, size_t remsetRefsCount) const;
67     uint64_t PredictMarkingTimeInMicros(size_t liveObjects, size_t remsetRefsCount) const;
68     uint64_t PredictCopyingTimeInMicros(size_t copiedBytes) const;
69     size_t PredictRemsetRefsCount(size_t remsetSize) const;
70     uint64_t PredictRemsetScanTimeInMicros(size_t edenLength) const;
71     uint64_t PredictYoungSinglePassCompactionTimeInMicros(size_t edenLength) const;
72     uint64_t PredictYoungMarkingCollectionTimeInMicros(size_t edenLength, size_t expectedRemsetRefsCount) const;
73     uint64_t PredictOtherTime(StatType type) const;
74 
PredictTime(size_t volume,ark::Sequence rateSeq)75     uint64_t PredictTime(size_t volume, ark::Sequence rateSeq) const
76     {
77         auto ratePrediction = predictor_.Predict(rateSeq);
78         return ratePrediction == 0 ? 0 : volume / ratePrediction;
79     }
80 
81     void DumpMetrics(const CollectionSet &collectionSet, uint64_t pauseTime, double allocationRate) const;
82     void DumpSinglePassCompactionMetrics(size_t edenLength, uint64_t pauseTime) const;
83     void DumpMarkingCollectionMetrics(size_t edenLength, uint64_t pauseTime) const;
84     void ReportMarkingCollectionEnd(GCTaskCause gcCause, uint64_t pauseTime, size_t edenLength);
85     void ReportSinglePassCompactionEnd(GCTaskCause gcCause, uint64_t pauseTime, size_t edenLength);
86     void UpdateCopiedBytesStat(size_t compactedRegions);
87     void UpdateCopiedBytesRateStat(uint64_t compactionTime);
88 
89     static constexpr uint64_t DEFAULT_PROMOTION_COST = 50;
90     const uint64_t promotionCost_ {DEFAULT_PROMOTION_COST};
91     uint64_t previousYoungCollectionEnd_;
92     uint64_t currentYoungCollectionStart_ {0};
93     size_t remsetSize_ {0};
94     size_t remsetRefsCount_ {0};
95     size_t totalRemsetRefsCount_ {0};
96     size_t dirtyCardsCount_ {0};
97     uint64_t markingStart_ {0};
98     uint64_t markingEnd_ {0};
99     uint64_t evacuationStart_ {0};
100     uint64_t evacuationEnd_ {0};
101     uint64_t updateRefsStart_ {0};
102     uint64_t updateRefsEnd_ {0};
103     uint64_t scanDirtyCardsStart_ {0};
104     uint64_t scanDirtyCardsEnd_ {0};
105     uint64_t predictedMixedPause_ {0};
106     uint64_t scanRemsetTime_ {0};
107     ark::Sequence allocationRateSeq_;
108     ark::Sequence copiedBytesSeq_;
109     ark::Sequence copyingBytesRateSeq_;
110     ark::Sequence remsetRefsSeq_;
111     ark::Sequence remsetRefsPerChunkSeq_;
112     ark::Sequence liveObjecstSeq_;
113     ark::Sequence markingRateSeq_;
114     ark::Sequence updateRefsRateSeq_;
115     ark::Sequence promotionSeq_;
116     std::array<ark::Sequence, StatType::COUNT> otherSeq_;
117     ark::Sequence liveObjectsSeq_;
118     ark::Sequence scanDirtyCardsRateSeq_;
119     ark::Sequence survivedBytesRatioSeq_;
120     ark::Sequence remsetSizeSeq_;
121     ark::Sequence remsetScanRateSeq_;
122     static constexpr double DEFAULT_CONFIDENCE_FACTOR = 0.5;
123     G1Predictor predictor_ {DEFAULT_CONFIDENCE_FACTOR};
124     std::atomic<size_t> copiedBytes_ {0};
125     std::atomic<size_t> promotedRegions_ {0};
126     std::atomic<size_t> liveObjects_ {0};
127     bool previousWasSinglePassCompaction_ {false};
128 };
129 }  // namespace ark::mem
130 #endif
131