3 * UWP proclib support for Mono.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 #include "mono/utils/mono-compiler.h"
12 #if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
14 #include <mono/utils/mono-proclib.h>
17 mono_cpu_usage (MonoCpuUsageState
*prev
)
20 gint64 cpu_total_time
;
26 guint64 creation_time
;
29 GetSystemTimeAsFileTime ((FILETIME
*)¤t_time
);
30 if (!GetProcessTimes (GetCurrentProcess (), (FILETIME
*)&creation_time
, (FILETIME
*)&exit_time
, (FILETIME
*)&kernel_time
, (FILETIME
*)&user_time
)) {
31 g_error ("GetProcessTimes() failed, error code is %d\n", GetLastError ());
35 // GetProcessTimes user_time is a sum of user time spend by all threads in the process.
36 // This means that the total user time can be more than real time. In order to adjust for this
37 // the total available time that we can be scheduled depends on the number of available cores.
38 // For example, having 2 threads running 100% on a 2 core system for 100 ms will return a user_time of 200ms
39 // but the current_time - creation_time will only be 100ms but by adjusting the available time based on number of
40 // of availalbe cores will gives use the total load of the process.
41 guint64 total_available_time
= (current_time
- creation_time
) * mono_cpu_count ();
43 idle_time
= total_available_time
- (kernel_time
+ user_time
);
45 cpu_total_time
= (gint64
)((idle_time
- (prev
? prev
->idle_time
: 0)) + (user_time
- (prev
? prev
->user_time
: 0)) + (kernel_time
- (prev
? prev
->kernel_time
: 0)));
46 cpu_busy_time
= (gint64
)(cpu_total_time
- (idle_time
- (prev
? prev
->idle_time
: 0)));
49 prev
->idle_time
= idle_time
;
50 prev
->kernel_time
= kernel_time
;
51 prev
->user_time
= user_time
;
54 if (cpu_total_time
> 0 && cpu_busy_time
> 0)
55 cpu_usage
= (gint32
)(cpu_busy_time
* 100 / cpu_total_time
);
60 #else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
62 MONO_EMPTY_SOURCE_FILE (mono_proclib_windows_uwp
);
63 #endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */