Hotspot源码解析-第十六章-代码缓存空间初始化

第十六章-代码缓存空间初始化

16.1 codeCache.cpp

16.1.1 codeCache_init

void codeCache_init() {
  CodeCache::initialize();
}

void CodeCache::initialize() {
  // 判断操作
  assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
#ifdef COMPILER2
  assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment,  "CodeCacheSegmentSize must be large enough to align inner loops");
#endif
  assert(CodeCacheSegmentSize >= sizeof(jdouble),    "CodeCacheSegmentSize must be large enough to align constants");
  /*
  * 下面三步都是将各自大小调整到按页面大小四舍五入,CodeCache分段的好处:
  * 1.提升运行时程序性能,特别是GC的性能,GC扫描根只需要遍历一个区域而不需要遍历所有区域
  * 2.提升代码局部性
  */
  CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size());
  InitialCodeCacheSize = round_to(InitialCodeCacheSize, os::vm_page_size());
  ReservedCodeCacheSize = round_to(ReservedCodeCacheSize, os::vm_page_size());
  // 按ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize三块空间创建一个C堆空间,并用CodeHeap对象持有
  if (!_heap->reserve(ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize)) {
    vm_exit_during_initialization("Could not reserve enough space for code cache");
  }
  // 将新创建的C堆空间的封装到code heap内存池CodeHeapPool中,然后通过MemoryManager内存管理器对象通过数组来管理这些池对象,最后再分别将CodeHeapPool池对象和MemoryManager内存管理对象,添加到GrowableArray可增长的数组对象中,总结就是,这里预创建了内存区域,并用句柄管理起来
  MemoryService::add_code_heap_memory_pool(_heap);

  // 这个里面实际上没做什么,忽略
  icache_init();

  // 将生成的CodeCache区域注册到操作系统中,使得当在动态生成的代码中发生异常时,会将异常信息发送到topLevelExceptionFilter(顶级异常过滤器)
  os::register_code_area(_heap->low_boundary(), _heap->high_boundary());
}

16.2 VM版本初始化

16.2.1 vm_version.cpp

16.2.1.1 VM_Version_init
void VM_Version_init() {
  VM_Version::initialize();

#ifndef PRODUCT
  if (PrintMiscellaneous && Verbose) {
    os::print_cpu_info(tty);
  }
#endif
}

void VM_Version::initialize() {
  ResourceMark rm;
  // Making this stub must be FIRST use of assembler
  // 创建BufferBlob对象,stub_size 默认是600
  stub_blob = BufferBlob::create("get_cpu_info_stub", stub_size);
  if (stub_blob == NULL) { // 创建失败,打印日志并退出vm
    vm_exit_during_initialization("Unable to allocate get_cpu_info_stub");
  }
  CodeBuffer c(stub_blob); // 创建CodeBuffer
  VM_Version_StubGenerator g(&c); // 创建VM_Version_StubGenerator
  // 得到获取cpu信息的函数
  get_cpu_info_stub = CAST_TO_FN_PTR(get_cpu_info_stub_t,
                                     g.generate_get_cpu_info());
  // 调用函数获取cpu特征信息,比如cpu支持的各种指令集
  get_processor_features();
}

你可能感兴趣的:(Java虚拟机,java)