Skip to content

g1 region Num_alloc_rate_ms

wangkaiwu edited this page Feb 1, 2022 · 3 revisions

num_alloc_rate_ms

代码内容

以下是代码的内容,涉及两个问题。_alloc_rate_ms_seq 是什么?num() 返回了什么?

int G1Analytics::num_alloc_rate_ms() const {
    // 看完了后面的代码,_alloc_rate_ms_seq 实际上是一个队列
    // 队列每个元素实际上是两次 GC 的间隔时间内分配 eden 区 region 的速率
    return _alloc_rate_ms_seq->num();
}

_alloc_rate_ms_seq 是什么

以下 _alloc_rate_ms_seq 的赋值代码,它是 TruncatedSeq 类的对象.其中传参 TruncatedSeqLength 是个常量,值是 10.

G1Analytics::G1Analytics(const G1Predictions* predictor) :
    _alloc_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)){
    
};

num() 内容

int num() const { return _num; }

赋值代码

以下是对 _num 赋值的代码,其中 _length 就是 TruncatedSeqLength = 10

  1. 调用 add() 之后会让 _num 增加
void TruncatedSeq::add(double val) {
    if (_num < _length)
        ++_num;
}
  1. 通过下面的方法调用上面的 add() 方法
void G1Analytics::report_alloc_rate_ms(double alloc_rate) {
    _alloc_rate_ms_seq->add(alloc_rate);
}
  1. 下面的方法调用了 report_alloc_rate_ms,且是在每次 GC 执行结束的时候调用.
void G1Policy::record_collection_pause_end(double pause_time_ms, size_t cards_scanned, size_t heap_used_bytes_before_gc) {
    // 只要本次 GC 没有出现转移失败,则 update_stats 为 true
    bool update_stats = !_g1h->evacuation_failed();
    // cur_collection_start_sec 是本次 GC 的开始时间,prev_collection_pause_end_ms 是上一次 GC 结束的时间
    // 这里是在计算上一次 GC 结束,距离本次 GC 开始,中间过了多少 ms
    double app_time_ms = (phase_times()->cur_collection_start_sec() * 1000.0 - _analytics->prev_collection_pause_end_ms());
    // 假设为 ture
    if (update_stats) {
        // 这里需要解决两个问题,
        // 1._collection_set 是什么,看起来像是本次 GC 处理的 region 集合
        // 2. eden_region_length 看起来像是 eden 区的 region 个数
        uint regions_allocated = _collection_set->eden_region_length();
        // 平均每毫秒分配了几个 eden 区的 region
        double alloc_rate_ms = (double) regions_allocated / app_time_ms;
        // 记录一下本次的分配速率
        _analytics->report_alloc_rate_ms(alloc_rate_ms);    
    }
}

Clone this wiki locally