src/os/linux/vm/os_linux.cpp

Print this page
rev 6352 : imported patch 8031126-threadusertime


5254   if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5255     return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5256   } else {
5257     return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5258   }
5259 }
5260 
5261 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5262   if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5263     return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
5264   } else {
5265     return slow_thread_cpu_time(thread, user_sys_cpu_time);
5266   }
5267 }
5268 
5269 //
5270 //  -1 on error.
5271 //
5272 
5273 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5274   static bool proc_task_unchecked = true;
5275   pid_t  tid = thread->osthread()->thread_id();
5276   char *s;
5277   char stat[2048];
5278   int statlen;
5279   char proc_name[64];
5280   int count;
5281   long sys_time, user_time;
5282   char cdummy;
5283   int idummy;
5284   long ldummy;
5285   FILE *fp;
5286 
5287   snprintf(proc_name, 64, "/proc/%d/stat", tid);
5288 
5289   // The /proc/<tid>/stat aggregates per-process usage on
5290   // new Linux kernels 2.6+ where NPTL is supported.
5291   // The /proc/self/task/<tid>/stat still has the per-thread usage.
5292   // See bug 6328462.
5293   // There possibly can be cases where there is no directory
5294   // /proc/self/task, so we check its availability.
5295   if (proc_task_unchecked && os::Linux::is_NPTL()) {
5296     // This is executed only once
5297     proc_task_unchecked = false;
5298     fp = fopen("/proc/self/task", "r");
5299     if (fp != NULL) {
5300       snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);
5301       fclose(fp);
5302     }
5303   }
5304 
5305   fp = fopen(proc_name, "r");
5306   if ( fp == NULL ) return -1;
5307   statlen = fread(stat, 1, 2047, fp);
5308   stat[statlen] = '\0';
5309   fclose(fp);
5310 
5311   // Skip pid and the command string. Note that we could be dealing with
5312   // weird command names, e.g. user could decide to rename java launcher
5313   // to "java 1.4.2 :)", then the stat file would look like
5314   //                1234 (java 1.4.2 :)) R ... ...
5315   // We don't really need to know the command string, just find the last
5316   // occurrence of ")" and then start parsing from there. See bug 4726580.
5317   s = strrchr(stat, ')');
5318   if (s == NULL ) return -1;
5319 
5320   // Skip blank chars
5321   do s++; while (isspace(*s));
5322 
5323   count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5324                  &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,




5254   if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5255     return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5256   } else {
5257     return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5258   }
5259 }
5260 
5261 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5262   if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5263     return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
5264   } else {
5265     return slow_thread_cpu_time(thread, user_sys_cpu_time);
5266   }
5267 }
5268 
5269 //
5270 //  -1 on error.
5271 //
5272 
5273 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {

5274   pid_t  tid = thread->osthread()->thread_id();
5275   char *s;
5276   char stat[2048];
5277   int statlen;
5278   char proc_name[64];
5279   int count;
5280   long sys_time, user_time;
5281   char cdummy;
5282   int idummy;
5283   long ldummy;
5284   FILE *fp;
5285 













5286   snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);




5287   fp = fopen(proc_name, "r");
5288   if ( fp == NULL ) return -1;
5289   statlen = fread(stat, 1, 2047, fp);
5290   stat[statlen] = '\0';
5291   fclose(fp);
5292 
5293   // Skip pid and the command string. Note that we could be dealing with
5294   // weird command names, e.g. user could decide to rename java launcher
5295   // to "java 1.4.2 :)", then the stat file would look like
5296   //                1234 (java 1.4.2 :)) R ... ...
5297   // We don't really need to know the command string, just find the last
5298   // occurrence of ")" and then start parsing from there. See bug 4726580.
5299   s = strrchr(stat, ')');
5300   if (s == NULL ) return -1;
5301 
5302   // Skip blank chars
5303   do s++; while (isspace(*s));
5304 
5305   count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5306                  &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,