From e96abae130e53d118bd7491f98ebd6d56b0b1c85 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Mon, 12 Dec 2005 12:18:14 +0000 Subject: [PATCH] * fhandler_proc.cc (format_proc_uptime): Drop usage of GetSystemTimes. Use NtQuerySystemInformation to evaluate uptime and idle_time from all CPU's processor times. Fallback to GetTickCount. --- winsup/cygwin/ChangeLog | 6 +++++ winsup/cygwin/fhandler_proc.cc | 43 ++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index af716dc92..94ac5711c 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,9 @@ +2005-12-12 Corinna Vinschen + + * fhandler_proc.cc (format_proc_uptime): Drop usage of GetSystemTimes. + Use NtQuerySystemInformation to evaluate uptime and idle_time from + all CPU's processor times. Fallback to GetTickCount. + 2005-12-12 Corinna Vinschen * mmap.cc (gen_create_protect): Always generate WRITECOPY protection diff --git a/winsup/cygwin/fhandler_proc.cc b/winsup/cygwin/fhandler_proc.cc index 0ecfc0d5d..996444a66 100644 --- a/winsup/cygwin/fhandler_proc.cc +++ b/winsup/cygwin/fhandler_proc.cc @@ -468,30 +468,37 @@ static _off64_t format_proc_uptime (char *destbuf, size_t maxsize) { unsigned long long uptime = 0ULL, idle_time = 0ULL; - SYSTEM_PROCESSOR_TIMES spt; - if (!GetSystemTimes ((FILETIME *) &spt.IdleTime, (FILETIME *) &spt.KernelTime, - (FILETIME *) &spt.UserTime) - && GetLastError () == ERROR_PROC_NOT_FOUND) + if (wincap.is_winnt ()) { - NTSTATUS ret = NtQuerySystemInformation (SystemProcessorTimes, (PVOID) &spt, - sizeof spt, NULL); - if (!ret && GetLastError () == ERROR_PROC_NOT_FOUND) - { - uptime = GetTickCount () / 10; - goto out; - } - else if (ret != STATUS_SUCCESS) + NTSTATUS ret; + SYSTEM_BASIC_INFORMATION sbi; + + ret = NtQuerySystemInformation (SystemBasicInformation, (PVOID) &sbi, + sizeof sbi, NULL); + if (!NT_SUCCESS (ret)) { __seterrno_from_nt_status (ret); - debug_printf("NtQuerySystemInformation: ret %d, Dos(ret) %E", ret); - return 0; + debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %E", ret); + sbi.NumberProcessors = 1; } + + SYSTEM_PROCESSOR_TIMES spt[sbi.NumberProcessors]; + ret = NtQuerySystemInformation (SystemProcessorTimes, (PVOID) spt, + sizeof spt[0] * sbi.NumberProcessors, + NULL); + if (NT_SUCCESS (ret)) + for (int i = 0; i < sbi.NumberProcessors; i++) + { + uptime += (spt[i].KernelTime.QuadPart + spt[i].UserTime.QuadPart) + / 100000ULL; + idle_time += spt[i].IdleTime.QuadPart / 100000ULL; + } + uptime /= sbi.NumberProcessors; + idle_time /= sbi.NumberProcessors; } - idle_time = spt.IdleTime.QuadPart / 100000ULL; - uptime = (spt.KernelTime.QuadPart + - spt.UserTime.QuadPart) / 100000ULL; -out: + if (!uptime) + uptime = GetTickCount () / 10; return __small_sprintf (destbuf, "%U.%02u %U.%02u\n", uptime / 100, long (uptime % 100),