undo change in mini.c
[mono-project.git] / mono / utils / mono-proclib-windows-uwp.c
blob15a7cb2eca74fdb9648529fd4733a24bcff8405d
1 /**
2 * \file
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.
7 */
8 #include <config.h>
9 #include <glib.h>
10 #include "mono/utils/mono-compiler.h"
12 #if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
13 #include <windows.h>
14 #include <mono/utils/mono-proclib.h>
16 gint32
17 mono_cpu_usage (MonoCpuUsageState *prev)
19 gint32 cpu_usage = 0;
20 gint64 cpu_total_time;
21 gint64 cpu_busy_time;
22 guint64 idle_time;
23 guint64 kernel_time;
24 guint64 user_time;
25 guint64 current_time;
26 guint64 creation_time;
27 guint64 exit_time;
29 GetSystemTimeAsFileTime ((FILETIME*)&current_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 ());
32 return -1;
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)));
48 if (prev) {
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);
57 return cpu_usage;
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) */