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 snprintf(proc_name, 64, "/proc/%d/stat", tid);
5300
5301 // The /proc/<tid>/stat aggregates per-process usage on
5302 // new Linux kernels 2.6+ where NPTL is supported.
5303 // The /proc/self/task/<tid>/stat still has the per-thread usage.
5304 // See bug 6328462.
5305 // There possibly can be cases where there is no directory
5306 // /proc/self/task, so we check its availability.
5307 if (proc_task_unchecked && os::Linux::is_NPTL()) {
5308 // This is executed only once
5309 proc_task_unchecked = false;
5310 fp = fopen("/proc/self/task", "r");
5311 if (fp != NULL) {
5312 snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);
5313 fclose(fp);
5314 }
5315 }
5316
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,
|