src/os/linux/vm/os_linux.cpp

Print this page




5267     return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5268   } else {
5269     return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5270   }
5271 }
5272 
5273 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5274   if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5275     return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
5276   } else {
5277     return slow_thread_cpu_time(thread, user_sys_cpu_time);
5278   }
5279 }
5280 
5281 //
5282 //  -1 on error.
5283 //
5284 
5285 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5286   static bool proc_task_unchecked = true;
5287   static const char *proc_stat_path = "/proc/%d/stat";
5288   pid_t  tid = thread->osthread()->thread_id();
5289   char *s;
5290   char stat[2048];
5291   int statlen;
5292   char proc_name[64];
5293   int count;
5294   long sys_time, user_time;
5295   char cdummy;
5296   int idummy;
5297   long ldummy;
5298   FILE *fp;
5299 



5300   // The /proc/<tid>/stat aggregates per-process usage on
5301   // new Linux kernels 2.6+ where NPTL is supported.
5302   // The /proc/self/task/<tid>/stat still has the per-thread usage.
5303   // See bug 6328462.
5304   // There possibly can be cases where there is no directory
5305   // /proc/self/task, so we check its availability.
5306   if (proc_task_unchecked && os::Linux::is_NPTL()) {
5307     // This is executed only once
5308     proc_task_unchecked = false;
5309     fp = fopen("/proc/self/task", "r");
5310     if (fp != NULL) {
5311       proc_stat_path = "/proc/self/task/%d/stat";
5312       fclose(fp);
5313     }
5314   }
5315 
5316   sprintf(proc_name, proc_stat_path, tid);
5317   fp = fopen(proc_name, "r");
5318   if ( fp == NULL ) return -1;
5319   statlen = fread(stat, 1, 2047, fp);
5320   stat[statlen] = '\0';
5321   fclose(fp);
5322 
5323   // Skip pid and the command string. Note that we could be dealing with
5324   // weird command names, e.g. user could decide to rename java launcher
5325   // to "java 1.4.2 :)", then the stat file would look like
5326   //                1234 (java 1.4.2 :)) R ... ...
5327   // We don't really need to know the command string, just find the last
5328   // occurrence of ")" and then start parsing from there. See bug 4726580.
5329   s = strrchr(stat, ')');
5330   if (s == NULL ) return -1;
5331 
5332   // Skip blank chars
5333   do s++; while (isspace(*s));
5334 
5335   count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5336                  &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,




5267     return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5268   } else {
5269     return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5270   }
5271 }
5272 
5273 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5274   if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5275     return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
5276   } else {
5277     return slow_thread_cpu_time(thread, user_sys_cpu_time);
5278   }
5279 }
5280 
5281 //
5282 //  -1 on error.
5283 //
5284 
5285 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5286   static bool proc_task_unchecked = true;

5287   pid_t  tid = thread->osthread()->thread_id();
5288   char *s;
5289   char stat[2048];
5290   int statlen;
5291   char proc_name[64];
5292   int count;
5293   long sys_time, user_time;
5294   char cdummy;
5295   int idummy;
5296   long ldummy;
5297   FILE *fp;
5298 
5299 
5300   sprintf(proc_name, "/proc/%d/stat", tid);
5301   
5302   // The /proc/<tid>/stat aggregates per-process usage on
5303   // new Linux kernels 2.6+ where NPTL is supported.
5304   // The /proc/self/task/<tid>/stat still has the per-thread usage.
5305   // See bug 6328462.
5306   // There possibly can be cases where there is no directory
5307   // /proc/self/task, so we check its availability.
5308   if (proc_task_unchecked && os::Linux::is_NPTL()) {
5309     // This is executed only once
5310     proc_task_unchecked = false;
5311     fp = fopen("/proc/self/task", "r");
5312     if (fp != NULL) {
5313       sprintf(proc_name, "/proc/self/task/%d/stat", tid);
5314       fclose(fp);
5315     }
5316   }
5317 

5318   fp = fopen(proc_name, "r");
5319   if ( fp == NULL ) return -1;
5320   statlen = fread(stat, 1, 2047, fp);
5321   stat[statlen] = '\0';
5322   fclose(fp);
5323 
5324   // Skip pid and the command string. Note that we could be dealing with
5325   // weird command names, e.g. user could decide to rename java launcher
5326   // to "java 1.4.2 :)", then the stat file would look like
5327   //                1234 (java 1.4.2 :)) R ... ...
5328   // We don't really need to know the command string, just find the last
5329   // occurrence of ")" and then start parsing from there. See bug 4726580.
5330   s = strrchr(stat, ')');
5331   if (s == NULL ) return -1;
5332 
5333   // Skip blank chars
5334   do s++; while (isspace(*s));
5335 
5336   count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5337                  &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,