hotspot/src/os/linux/vm/os_linux.cpp
changeset 24352 9ba9e99b1ba7
parent 24351 61b33cc6d3cf
parent 24333 c7214442139d
child 24424 2658d7834c6e
equal deleted inserted replaced
24351:61b33cc6d3cf 24352:9ba9e99b1ba7
  5270 //
  5270 //
  5271 //  -1 on error.
  5271 //  -1 on error.
  5272 //
  5272 //
  5273 
  5273 
  5274 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
  5274 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
  5275   static bool proc_task_unchecked = true;
       
  5276   pid_t  tid = thread->osthread()->thread_id();
  5275   pid_t  tid = thread->osthread()->thread_id();
  5277   char *s;
  5276   char *s;
  5278   char stat[2048];
  5277   char stat[2048];
  5279   int statlen;
  5278   int statlen;
  5280   char proc_name[64];
  5279   char proc_name[64];
  5283   char cdummy;
  5282   char cdummy;
  5284   int idummy;
  5283   int idummy;
  5285   long ldummy;
  5284   long ldummy;
  5286   FILE *fp;
  5285   FILE *fp;
  5287 
  5286 
  5288   snprintf(proc_name, 64, "/proc/%d/stat", tid);
  5287   snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);
  5289 
       
  5290   // The /proc/<tid>/stat aggregates per-process usage on
       
  5291   // new Linux kernels 2.6+ where NPTL is supported.
       
  5292   // The /proc/self/task/<tid>/stat still has the per-thread usage.
       
  5293   // See bug 6328462.
       
  5294   // There possibly can be cases where there is no directory
       
  5295   // /proc/self/task, so we check its availability.
       
  5296   if (proc_task_unchecked && os::Linux::is_NPTL()) {
       
  5297     // This is executed only once
       
  5298     proc_task_unchecked = false;
       
  5299     fp = fopen("/proc/self/task", "r");
       
  5300     if (fp != NULL) {
       
  5301       snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);
       
  5302       fclose(fp);
       
  5303     }
       
  5304   }
       
  5305 
       
  5306   fp = fopen(proc_name, "r");
  5288   fp = fopen(proc_name, "r");
  5307   if ( fp == NULL ) return -1;
  5289   if ( fp == NULL ) return -1;
  5308   statlen = fread(stat, 1, 2047, fp);
  5290   statlen = fread(stat, 1, 2047, fp);
  5309   stat[statlen] = '\0';
  5291   stat[statlen] = '\0';
  5310   fclose(fp);
  5292   fclose(fp);