Skip to content

g1 G1_Young_RemSet_Sampling

ceragon edited this page Dec 7, 2021 · 1 revision

G1 Young RemSet Sampling 线程

声明

  1. 声明位于 g1YoungRemSetSamplingThread.cpp 中
G1YoungRemSetSamplingThread::G1YoungRemSetSamplingThread() : ConcurrentGCThread(){
    set_name("G1 Young RemSet Sampling");
    create_and_start();
}
  1. 与 G1 Refine 和 G1 Main Marker 类似,都是 create_and_start 直接启动

run 方法

  1. 线程的run方法如下
void ConcurrentGCThread::run() {
  initialize_in_thread();
  wait_for_universe_init();
  run_service();
  terminate();
}
  1. run方法会调用 run_service() 方法,内容如下:
void G1YoungRemSetSamplingThread::run_service() {
      double vtime_start = os::elapsedVTime();
      while (!should_terminate()) {
        sample_young_list_rs_lengths();
        if (os::supports_vtime()) {
          _vtime_accum = (os::elapsedVTime() - vtime_start);
        } else {
          _vtime_accum = 0.0;
        }
        sleep_before_next_cycle();
      }
}

触发方式

  1. 上面的run_service() 方法会在 sleep_before_next_cycle() 处等待
void G1YoungRemSetSamplingThread::sleep_before_next_cycle() {
      MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
      if (!should_terminate()) {
        uintx waitms = G1ConcRefinementServiceIntervalMillis; // 300, really should be?
        _monitor.wait(Mutex::_no_safepoint_check_flag, waitms);
      }
}
  1. 可以看到 run_service 中的逻辑就是300ms 执行一次

主要逻辑

  1. run_service 主要的逻辑就是 sample_young_list_rs_lengths,大概的内容就是动态调整年轻代的young region 的大小
void G1YoungRemSetSamplingThread::sample_young_list_rs_lengths() {
  SuspendibleThreadSetJoiner sts;
  G1CollectedHeap* g1h = G1CollectedHeap::heap();
  G1Policy* g1p = g1h->g1_policy();

  if (g1p->adaptive_young_list_length()) {
    G1YoungRemSetSamplingClosure cl(&sts);

    G1CollectionSet* g1cs = g1h->collection_set();
    g1cs->iterate(&cl);

    if (cl.is_complete()) {
      g1p->revise_young_list_target_length_if_necessary(cl.sampled_rs_lengths());
    }
  }
}

Clone this wiki locally