3 * Windows 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.
14 #include "mono/utils/mono-proclib.h"
17 mono_process_current_pid ()
19 return (int) GetCurrentProcessId ();
24 * \returns the number of processors on the system.
30 GetSystemInfo (&info
);
31 return info
.dwNumberOfProcessors
;
35 * This function returns the cpu usage in percentage,
36 * normalized on the number of cores.
38 * Warning : the percentage returned can be > 100%. This
39 * might happens on systems like Android which, for
40 * battery and performance reasons, shut down cores and
41 * lie about the number of active cores.
43 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
45 mono_cpu_usage (MonoCpuUsageState
*prev
)
48 gint64 cpu_total_time
;
54 if (!GetSystemTimes ((FILETIME
*) &idle_time
, (FILETIME
*) &kernel_time
, (FILETIME
*) &user_time
)) {
55 g_error ("GetSystemTimes() failed, error code is %d\n", GetLastError ());
59 cpu_total_time
= (gint64
)((user_time
- (prev
? prev
->user_time
: 0)) + (kernel_time
- (prev
? prev
->kernel_time
: 0)));
60 cpu_busy_time
= (gint64
)(cpu_total_time
- (idle_time
- (prev
? prev
->idle_time
: 0)));
63 prev
->idle_time
= idle_time
;
64 prev
->kernel_time
= kernel_time
;
65 prev
->user_time
= user_time
;
68 if (cpu_total_time
> 0 && cpu_busy_time
> 0)
69 cpu_usage
= (gint32
)(cpu_busy_time
* 100 / cpu_total_time
);
73 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
74 #endif /* HOST_WIN32*/