JVM OS::init()源码分析

来源:互联网 发布:点云数据 编辑:程序博客网 时间:2024/06/06 05:05
void os::init(void) {  char dummy;   /* used to get a guess on initial stack address *///  first_hrtime = gethrtime();  // With LinuxThreads the JavaMain thread pid (primordial thread)  // is different than the pid of the java launcher thread.  // So, on Linux, the launcher thread pid is passed to the VM  // via the sun.java.launcher.pid property.  // Use this property instead of getpid() if it was correctly passed.  // See bug 6351349.  //优先获取当前启动启动进程的进程号,因为启动进程可能和当前的线程不是同一个进程  //JavaMain是通过创建新线程的方式启动的  pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();  _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();  //获取每秒时钟的滴答数  clock_tics_per_sec = sysconf(_SC_CLK_TCK);  //初始化随机种子  init_random(1234567);  ThreadCritical::initialize();  //设置页的大小  Linux::set_page_size(sysconf(_SC_PAGESIZE));  if (Linux::page_size() == -1) {    fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)",                  strerror(errno)));  }  //设置页的大小  init_page_sizes((size_t) Linux::page_size());  //获取cpu的数量  //获取物理内存的大小:sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES)  Linux::initialize_system_info();  // main_thread points to the aboriginal thread  //获取当前的线程pid  Linux::_main_thread = pthread_self();  Linux::clock_init();  initial_time_count = os::elapsed_counter();  //初始化互斥量  pthread_mutex_init(&dl_mutex, NULL);}void os::Linux::initialize_system_info() {  //设置处理器的数量  set_processor_count(sysconf(_SC_NPROCESSORS_CONF));  if (processor_count() == 1) {    //打开/prod/$pid,应该是在检测什么?    pid_t pid = os::Linux::gettid();    char fname[32];    jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);    FILE *fp = fopen(fname, "r");    if (fp == NULL) {      unsafe_chroot_detected = true;    } else {      fclose(fp);    }  }  //计算物理内存的大小=物理内存页数*物理内存的页的大小  _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);  assert(processor_count() > 0, "linux error");}

0 0