< prev index next >

src/os/linux/vm/os_linux.cpp

Print this page




  78 # include <signal.h>
  79 # include <errno.h>
  80 # include <dlfcn.h>
  81 # include <stdio.h>
  82 # include <unistd.h>
  83 # include <sys/resource.h>
  84 # include <pthread.h>
  85 # include <sys/stat.h>
  86 # include <sys/time.h>
  87 # include <sys/times.h>
  88 # include <sys/utsname.h>
  89 # include <sys/socket.h>
  90 # include <sys/wait.h>
  91 # include <pwd.h>
  92 # include <poll.h>
  93 # include <semaphore.h>
  94 # include <fcntl.h>
  95 # include <string.h>
  96 # include <syscall.h>
  97 # include <sys/sysinfo.h>

  98 # include <gnu/libc-version.h>

  99 # include <sys/ipc.h>
 100 # include <sys/shm.h>
 101 # include <link.h>
 102 # include <stdint.h>
 103 # include <inttypes.h>
 104 # include <sys/ioctl.h>
 105 
 106 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
 107 
 108 #ifndef _GNU_SOURCE
 109   #define _GNU_SOURCE
 110   #include <sched.h>
 111   #undef _GNU_SOURCE
 112 #else
 113   #include <sched.h>
 114 #endif
 115 
 116 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
 117 // getrusage() is prepared to handle the associated failure.
 118 #ifndef RUSAGE_THREAD


 581 // detecting pthread library
 582 
 583 void os::Linux::libpthread_init() {
 584   // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION
 585   // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a
 586   // generic name for earlier versions.
 587   // Define macros here so we can build HotSpot on old systems.
 588 # ifndef _CS_GNU_LIBC_VERSION
 589 # define _CS_GNU_LIBC_VERSION 2
 590 # endif
 591 # ifndef _CS_GNU_LIBPTHREAD_VERSION
 592 # define _CS_GNU_LIBPTHREAD_VERSION 3
 593 # endif
 594 
 595   size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
 596   if (n > 0) {
 597      char *str = (char *)malloc(n, mtInternal);
 598      confstr(_CS_GNU_LIBC_VERSION, str, n);
 599      os::Linux::set_glibc_version(str);
 600   } else {

 601      // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()
 602      static char _gnu_libc_version[32];
 603      jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),
 604               "glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());
 605      os::Linux::set_glibc_version(_gnu_libc_version);





 606   }
 607 
 608   n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
 609   if (n > 0) {
 610      char *str = (char *)malloc(n, mtInternal);
 611      confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);
 612      // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells
 613      // us "NPTL-0.29" even we are running with LinuxThreads. Check if this
 614      // is the case. LinuxThreads has a hard limit on max number of threads.
 615      // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.
 616      // On the other hand, NPTL does not have such a limit, sysconf()
 617      // will return -1 and errno is not changed. Check if it is really NPTL.
 618      if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&
 619          strstr(str, "NPTL") &&
 620          sysconf(_SC_THREAD_THREADS_MAX) > 0) {
 621        free(str);
 622        os::Linux::set_libpthread_version("linuxthreads");
 623      } else {
 624        os::Linux::set_libpthread_version(str);
 625      }


2919 # define __NR_vgetcpu 2
2920 # define VSYSCALL_START (-10UL << 20)
2921 # define VSYSCALL_SIZE 1024
2922 # define VSYSCALL_ADDR(vsyscall_nr) (VSYSCALL_START+VSYSCALL_SIZE*(vsyscall_nr))
2923   typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache);
2924   vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu);
2925   retval = vgetcpu(&cpu, NULL, NULL);
2926 #endif
2927 
2928   return (retval == -1) ? retval : cpu;
2929 }
2930 
2931 // Something to do with the numa-aware allocator needs these symbols
2932 extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }
2933 extern "C" JNIEXPORT void numa_error(char *where) { }
2934 extern "C" JNIEXPORT int fork1() { return fork(); }
2935 
2936 // Handle request to load libnuma symbol version 1.1 (API v1). If it fails
2937 // load symbol from base version instead.
2938 void* os::Linux::libnuma_dlsym(void* handle, const char *name) {

2939   void *f = dlvsym(handle, name, "libnuma_1.1");
2940   if (f == NULL) {
2941     f = dlsym(handle, name);
2942   }
2943   return f;



2944 }
2945 
2946 // Handle request to load libnuma symbol version 1.2 (API v2) only.
2947 // Return NULL if the symbol is not defined in this particular version.
2948 void* os::Linux::libnuma_v2_dlsym(void* handle, const char* name) {
2949   return dlvsym(handle, name, "libnuma_1.2");
2950 }
2951 
2952 bool os::Linux::libnuma_init() {
2953   // sched_getcpu() should be in libc.
2954   set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
2955                                   dlsym(RTLD_DEFAULT, "sched_getcpu")));
2956 
2957   // If it's not, try a direct syscall.
2958   if (sched_getcpu() == -1)
2959     set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, (void*)&sched_getcpu_syscall));
2960 
2961   if (sched_getcpu() != -1) { // Does it work?
2962     void *handle = dlopen("libnuma.so.1", RTLD_LAZY);
2963     if (handle != NULL) {


5823   info_ptr->may_skip_backward = false;     // elapsed time not wall time
5824   info_ptr->may_skip_forward = false;      // elapsed time not wall time
5825   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;  // user+system time is returned
5826 }
5827 
5828 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
5829   info_ptr->max_value = ALL_64_BITS;       // will not wrap in less than 64 bits
5830   info_ptr->may_skip_backward = false;     // elapsed time not wall time
5831   info_ptr->may_skip_forward = false;      // elapsed time not wall time
5832   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;  // user+system time is returned
5833 }
5834 
5835 bool os::is_thread_cpu_time_supported() {
5836   return true;
5837 }
5838 
5839 // System loadavg support.  Returns -1 if load average cannot be obtained.
5840 // Linux doesn't yet have a (official) notion of processor sets,
5841 // so just return the system wide load average.
5842 int os::loadavg(double loadavg[], int nelem) {

5843   return ::getloadavg(loadavg, nelem);



5844 }
5845 
5846 void os::pause() {
5847   char filename[MAX_PATH];
5848   if (PauseAtStartupFile && PauseAtStartupFile[0]) {
5849     jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
5850   } else {
5851     jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
5852   }
5853 
5854   int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
5855   if (fd != -1) {
5856     struct stat buf;
5857     ::close(fd);
5858     while (::stat(filename, &buf) == 0) {
5859       (void)::poll(NULL, 0, 100);
5860     }
5861   } else {
5862     jio_fprintf(stderr,
5863       "Could not open pause file '%s', continuing immediately.\n", filename);




  78 # include <signal.h>
  79 # include <errno.h>
  80 # include <dlfcn.h>
  81 # include <stdio.h>
  82 # include <unistd.h>
  83 # include <sys/resource.h>
  84 # include <pthread.h>
  85 # include <sys/stat.h>
  86 # include <sys/time.h>
  87 # include <sys/times.h>
  88 # include <sys/utsname.h>
  89 # include <sys/socket.h>
  90 # include <sys/wait.h>
  91 # include <pwd.h>
  92 # include <poll.h>
  93 # include <semaphore.h>
  94 # include <fcntl.h>
  95 # include <string.h>
  96 # include <syscall.h>
  97 # include <sys/sysinfo.h>
  98 #ifndef __UCLIBC__
  99 # include <gnu/libc-version.h>
 100 #endif
 101 # include <sys/ipc.h>
 102 # include <sys/shm.h>
 103 # include <link.h>
 104 # include <stdint.h>
 105 # include <inttypes.h>
 106 # include <sys/ioctl.h>
 107 
 108 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
 109 
 110 #ifndef _GNU_SOURCE
 111   #define _GNU_SOURCE
 112   #include <sched.h>
 113   #undef _GNU_SOURCE
 114 #else
 115   #include <sched.h>
 116 #endif
 117 
 118 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
 119 // getrusage() is prepared to handle the associated failure.
 120 #ifndef RUSAGE_THREAD


 583 // detecting pthread library
 584 
 585 void os::Linux::libpthread_init() {
 586   // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION
 587   // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a
 588   // generic name for earlier versions.
 589   // Define macros here so we can build HotSpot on old systems.
 590 # ifndef _CS_GNU_LIBC_VERSION
 591 # define _CS_GNU_LIBC_VERSION 2
 592 # endif
 593 # ifndef _CS_GNU_LIBPTHREAD_VERSION
 594 # define _CS_GNU_LIBPTHREAD_VERSION 3
 595 # endif
 596 
 597   size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
 598   if (n > 0) {
 599      char *str = (char *)malloc(n, mtInternal);
 600      confstr(_CS_GNU_LIBC_VERSION, str, n);
 601      os::Linux::set_glibc_version(str);
 602   } else {
 603 #ifndef __UCLIBC__
 604      // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()
 605      static char _gnu_libc_version[32];
 606      jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),
 607               "glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());
 608      os::Linux::set_glibc_version(_gnu_libc_version);
 609 #else
 610 #define STRFY(s) #s
 611      os::Linux::set_glibc_version("uclibc " STRFY(__UCLIB_MAJOR__) "." STRFY(__UCLIBC_MINOR__) " stable");
 612 #undef STRFY
 613 #endif
 614   }
 615 
 616   n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
 617   if (n > 0) {
 618      char *str = (char *)malloc(n, mtInternal);
 619      confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);
 620      // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells
 621      // us "NPTL-0.29" even we are running with LinuxThreads. Check if this
 622      // is the case. LinuxThreads has a hard limit on max number of threads.
 623      // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.
 624      // On the other hand, NPTL does not have such a limit, sysconf()
 625      // will return -1 and errno is not changed. Check if it is really NPTL.
 626      if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&
 627          strstr(str, "NPTL") &&
 628          sysconf(_SC_THREAD_THREADS_MAX) > 0) {
 629        free(str);
 630        os::Linux::set_libpthread_version("linuxthreads");
 631      } else {
 632        os::Linux::set_libpthread_version(str);
 633      }


2927 # define __NR_vgetcpu 2
2928 # define VSYSCALL_START (-10UL << 20)
2929 # define VSYSCALL_SIZE 1024
2930 # define VSYSCALL_ADDR(vsyscall_nr) (VSYSCALL_START+VSYSCALL_SIZE*(vsyscall_nr))
2931   typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache);
2932   vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu);
2933   retval = vgetcpu(&cpu, NULL, NULL);
2934 #endif
2935 
2936   return (retval == -1) ? retval : cpu;
2937 }
2938 
2939 // Something to do with the numa-aware allocator needs these symbols
2940 extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }
2941 extern "C" JNIEXPORT void numa_error(char *where) { }
2942 extern "C" JNIEXPORT int fork1() { return fork(); }
2943 
2944 // Handle request to load libnuma symbol version 1.1 (API v1). If it fails
2945 // load symbol from base version instead.
2946 void* os::Linux::libnuma_dlsym(void* handle, const char *name) {
2947 #ifndef __UCLIBC__
2948   void *f = dlvsym(handle, name, "libnuma_1.1");
2949   if (f == NULL) {
2950     f = dlsym(handle, name);
2951   }
2952   return f;
2953 #else
2954   return dlsym(handle, name);
2955 #endif
2956 }
2957 
2958 // Handle request to load libnuma symbol version 1.2 (API v2) only.
2959 // Return NULL if the symbol is not defined in this particular version.
2960 void* os::Linux::libnuma_v2_dlsym(void* handle, const char* name) {
2961   return dlvsym(handle, name, "libnuma_1.2");
2962 }
2963 
2964 bool os::Linux::libnuma_init() {
2965   // sched_getcpu() should be in libc.
2966   set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
2967                                   dlsym(RTLD_DEFAULT, "sched_getcpu")));
2968 
2969   // If it's not, try a direct syscall.
2970   if (sched_getcpu() == -1)
2971     set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, (void*)&sched_getcpu_syscall));
2972 
2973   if (sched_getcpu() != -1) { // Does it work?
2974     void *handle = dlopen("libnuma.so.1", RTLD_LAZY);
2975     if (handle != NULL) {


5835   info_ptr->may_skip_backward = false;     // elapsed time not wall time
5836   info_ptr->may_skip_forward = false;      // elapsed time not wall time
5837   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;  // user+system time is returned
5838 }
5839 
5840 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
5841   info_ptr->max_value = ALL_64_BITS;       // will not wrap in less than 64 bits
5842   info_ptr->may_skip_backward = false;     // elapsed time not wall time
5843   info_ptr->may_skip_forward = false;      // elapsed time not wall time
5844   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;  // user+system time is returned
5845 }
5846 
5847 bool os::is_thread_cpu_time_supported() {
5848   return true;
5849 }
5850 
5851 // System loadavg support.  Returns -1 if load average cannot be obtained.
5852 // Linux doesn't yet have a (official) notion of processor sets,
5853 // so just return the system wide load average.
5854 int os::loadavg(double loadavg[], int nelem) {
5855 #ifndef __UCLIBC__
5856   return ::getloadavg(loadavg, nelem);
5857 #else
5858   return -1;
5859 #endif
5860 }
5861 
5862 void os::pause() {
5863   char filename[MAX_PATH];
5864   if (PauseAtStartupFile && PauseAtStartupFile[0]) {
5865     jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
5866   } else {
5867     jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
5868   }
5869 
5870   int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
5871   if (fd != -1) {
5872     struct stat buf;
5873     ::close(fd);
5874     while (::stat(filename, &buf) == 0) {
5875       (void)::poll(NULL, 0, 100);
5876     }
5877   } else {
5878     jio_fprintf(stderr,
5879       "Could not open pause file '%s', continuing immediately.\n", filename);


< prev index next >