Bug 19739

Summary: Process.GetCurrentProcess().TotalProcessorTime sometimes returns a negative value on OS X
Product: [Mono] Runtime Reporter: Jon Goldberger [MSFT] <jon.goldberger>
Component: GeneralAssignee: Zoltan Varga <vargaz>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, mono-bugs+runtime
Priority: ---    
Version: 3.2.x   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Comment 1 Jon Goldberger [MSFT] 2014-05-13 16:47:55 UTC
On Mac OS X 10.9.2 Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds occasionally returns negative values, like -78, -37, -105, -117. We experience this problem on about 30% of client computers. Especially, when CPU usage is supposed to be high.


I've looked into mono code a little and it seems like TotalProcessorTime is calculated with the method ves_icall_System_Diagnostics_Process_Times below. And it also looks like it is converted to ticks differently in other methods in the process.c file. Could it be the bug in the conversion?


gint64
ves_icall_System_Diagnostics_Process_Times (HANDLE process, gint32 type)
{
FILETIME create_time, exit_time, kernel_time, user_time;

if (GetProcessTimes (process, &create_time, &exit_time, &kernel_time, &user_time)) {
if (type == 0)
return *(gint64*)&user_time;
else if (type == 1)
return *(gint64*)&kernel_time;
/* system + user time: FILETIME can be (memory) cast to a 64 bit int */
return *(gint64*)&kernel_time + *(gint64*)&user_time;
}
return 0;
}




gint64 ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process)

{

gboolean ret;

gint64 ticks;

FILETIME create_time, exit_time, kernel_time, user_time;



MONO_ARCH_SAVE_REGS;



ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,

&user_time);

if(ret==TRUE) {

ticks=((guint64)exit_time.dwHighDateTime << 32) +

exit_time.dwLowDateTime;



return(ticks);

} else {

return(0);

}

}



Xamarin and mono details:

=== Xamarin Studio ===

Version 4.2.3 (build 60)
Installation UUID: 9d9c0b6d-e584-426a-8602-6fc4fb754525
Runtime:
Mono 3.2.6 ((no/9b58377)
GTK+ 2.24.23 theme: Raleigh
GTK# (2.12.0.0)
Package version: 302060000

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.Android ===

Not Installed

=== Xamarin.Mac ===

Xamarin.Mac: 1.6.27

=== Xamarin.iOS ===

Not Installed

=== Build Information ===

Release ID: 402030060
Git revision: 30c4afc300c2a39ec5300851357ce02e49dd217e
Build date: 2014-03-05 22:09:33+0000
Xamarin addins: f8a9589b57c2bfab2ccd73c880e7ad81e3ecf044

=== Operating System ===

Mac OS X 10.9.2
Darwin [hostname redacted] Darwin Kernel Version 13.1.0
Wed Apr 2 23:52:02 PDT 2014
root:xnu-2422.92.1~2/RELEASE_X86_64 x86_64
Comment 2 Zoltan Varga 2014-05-14 13:18:25 UTC
Fixed in mono master abaceb24f42e0e6ffbdc3ee19c6df9b928bba42f.