5739 } else {
5740 return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5741 }
5742 }
5743
5744 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5745 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5746 return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
5747 } else {
5748 return slow_thread_cpu_time(thread, user_sys_cpu_time);
5749 }
5750 }
5751
5752 //
5753 // -1 on error.
5754 //
5755
5756 PRAGMA_DIAG_PUSH
5757 PRAGMA_FORMAT_NONLITERAL_IGNORED
5758 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5759 static bool proc_task_unchecked = true;
5760 static const char *proc_stat_path = "/proc/%d/stat";
5761 pid_t tid = thread->osthread()->thread_id();
5762 char *s;
5763 char stat[2048];
5764 int statlen;
5765 char proc_name[64];
5766 int count;
5767 long sys_time, user_time;
5768 char cdummy;
5769 int idummy;
5770 long ldummy;
5771 FILE *fp;
5772
5773 // The /proc/<tid>/stat aggregates per-process usage on
5774 // new Linux kernels 2.6+ where NPTL is supported.
5775 // The /proc/self/task/<tid>/stat still has the per-thread usage.
5776 // See bug 6328462.
5777 // There possibly can be cases where there is no directory
5778 // /proc/self/task, so we check its availability.
5779 if (proc_task_unchecked && os::Linux::is_NPTL()) {
5780 // This is executed only once
5781 proc_task_unchecked = false;
5782 fp = fopen("/proc/self/task", "r");
5783 if (fp != NULL) {
5784 proc_stat_path = "/proc/self/task/%d/stat";
5785 fclose(fp);
5786 }
5787 }
5788
5789 sprintf(proc_name, proc_stat_path, tid);
5790 fp = fopen(proc_name, "r");
5791 if ( fp == NULL ) return -1;
5792 statlen = fread(stat, 1, 2047, fp);
5793 stat[statlen] = '\0';
5794 fclose(fp);
5795
5796 // Skip pid and the command string. Note that we could be dealing with
5797 // weird command names, e.g. user could decide to rename java launcher
5798 // to "java 1.4.2 :)", then the stat file would look like
5799 // 1234 (java 1.4.2 :)) R ... ...
5800 // We don't really need to know the command string, just find the last
5801 // occurrence of ")" and then start parsing from there. See bug 4726580.
5802 s = strrchr(stat, ')');
5803 if (s == NULL ) return -1;
5804
5805 // Skip blank chars
5806 do s++; while (isspace(*s));
5807
5808 count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5809 &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,
|
5739 } else {
5740 return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5741 }
5742 }
5743
5744 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5745 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5746 return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
5747 } else {
5748 return slow_thread_cpu_time(thread, user_sys_cpu_time);
5749 }
5750 }
5751
5752 //
5753 // -1 on error.
5754 //
5755
5756 PRAGMA_DIAG_PUSH
5757 PRAGMA_FORMAT_NONLITERAL_IGNORED
5758 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5759 pid_t tid = thread->osthread()->thread_id();
5760 char *s;
5761 char stat[2048];
5762 int statlen;
5763 char proc_name[64];
5764 int count;
5765 long sys_time, user_time;
5766 char cdummy;
5767 int idummy;
5768 long ldummy;
5769 FILE *fp;
5770
5771 snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);
5772 fp = fopen(proc_name, "r");
5773 if ( fp == NULL ) return -1;
5774 statlen = fread(stat, 1, 2047, fp);
5775 stat[statlen] = '\0';
5776 fclose(fp);
5777
5778 // Skip pid and the command string. Note that we could be dealing with
5779 // weird command names, e.g. user could decide to rename java launcher
5780 // to "java 1.4.2 :)", then the stat file would look like
5781 // 1234 (java 1.4.2 :)) R ... ...
5782 // We don't really need to know the command string, just find the last
5783 // occurrence of ")" and then start parsing from there. See bug 4726580.
5784 s = strrchr(stat, ')');
5785 if (s == NULL ) return -1;
5786
5787 // Skip blank chars
5788 do s++; while (isspace(*s));
5789
5790 count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5791 &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,
|