Bug 19739 - Process.GetCurrentProcess().TotalProcessorTime sometimes returns a negative value on OS X
Summary: Process.GetCurrentProcess().TotalProcessorTime sometimes returns a negative v...
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: 3.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2014-05-13 16:46 UTC by Jon Goldberger [MSFT]
Modified: 2014-05-14 13:18 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

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.