2 * Copyright 2008-2011 Novell Inc
3 * Copyright 2011 Xamarin Inc
4 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8 #include "utils/mono-proclib.h"
9 #include "utils/mono-time.h"
18 #ifdef HAVE_SCHED_GETAFFINITY
27 #if defined(_POSIX_VERSION)
28 #include <sys/errno.h>
29 #include <sys/param.h>
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
33 #ifdef HAVE_SYS_SYSCTL_H
34 #include <sys/sysctl.h>
36 #include <sys/resource.h>
38 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
40 #if defined(__APPLE__)
41 #include <mach/mach.h>
43 #ifdef HAVE_SYS_USER_H
46 #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
47 # define kinfo_starttime_member kp_proc.p_starttime
48 # define kinfo_pid_member kp_proc.p_pid
49 # define kinfo_name_member kp_proc.p_comm
50 #elif defined(__OpenBSD__)
51 // Can not figure out how to get the proc's start time on OpenBSD
52 # undef kinfo_starttime_member
53 # define kinfo_pid_member p_pid
54 # define kinfo_name_member p_comm
56 #define kinfo_starttime_member ki_start
57 #define kinfo_pid_member ki_pid
58 #define kinfo_name_member ki_comm
65 * @size: a pointer to a location where the size of the returned array is stored
67 * Return an array of pid values for the processes currently running on the system.
68 * The size of the array is stored in @size.
71 mono_process_list (int *size)
77 size_t data_len = sizeof (struct kinfo_proc2) * 400;
78 struct kinfo_proc2 *processes = malloc (data_len);
81 size_t data_len = sizeof (struct kinfo_proc) * 16;
82 struct kinfo_proc *processes;
84 #endif /* KERN_PROC2 */
96 mib [2] = KERN_PROC_ALL;
98 mib [4] = sizeof(struct kinfo_proc2);
99 mib [5] = 400; /* XXX */
101 res = sysctl (mib, 6, processes, &data_len, NULL, 0);
111 mib [2] = KERN_PROC_ALL;
114 res = sysctl (mib, 4, NULL, &data_len, NULL, 0);
117 processes = (struct kinfo_proc *) malloc (data_len);
118 res = sysctl (mib, 4, processes, &data_len, NULL, 0);
128 #endif /* KERN_PROC2 */
131 res = data_len/sizeof (struct kinfo_proc2);
133 res = data_len/sizeof (struct kinfo_proc);
134 #endif /* KERN_PROC2 */
135 buf = (void **) g_realloc (buf, res * sizeof (void*));
136 for (i = 0; i < res; ++i)
137 buf [i] = GINT_TO_POINTER (processes [i].kinfo_pid_member);
142 #elif defined(__HAIKU__)
143 /* FIXME: Add back the code from 9185fcc305e43428d0f40f3ee37c8a405d41c9ae */
144 g_assert_not_reached ();
151 GDir *dir = g_dir_open ("/proc/", 0, NULL);
157 while ((name = g_dir_read_name (dir))) {
160 pid = strtol (name, &nend, 10);
161 if (pid <= 0 || nend == name || *nend)
168 buf = (void **)g_realloc (buf, count * sizeof (void*));
170 buf [i++] = GINT_TO_POINTER (pid);
179 static G_GNUC_UNUSED char*
180 get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
185 int len = strlen (item);
187 g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
188 f = fopen (buf, "r");
191 *error = MONO_PROCESS_ERROR_NOT_FOUND;
194 while ((s = fgets (buf, sizeof (buf), f))) {
197 if (strncmp (buf, item, len))
200 while (g_ascii_isspace (*s)) s++;
203 while (g_ascii_isspace (*s)) s++;
206 strncpy (rbuf, s, MIN (len, blen));
207 rbuf [MIN (len, blen) - 1] = 0;
209 *error = MONO_PROCESS_ERROR_NONE;
214 *error = MONO_PROCESS_ERROR_OTHER;
221 #define KINFO_PROC struct kinfo_proc2
223 #define KINFO_PROC struct kinfo_proc
227 sysctl_kinfo_proc (gpointer pid, KINFO_PROC* processi)
230 size_t data_len = sizeof (KINFO_PROC);
235 mib [1] = KERN_PROC2;
236 mib [2] = KERN_PROC_PID;
237 mib [3] = GPOINTER_TO_UINT (pid);
238 mib [4] = sizeof(KINFO_PROC);
239 mib [5] = 400; /* XXX */
241 res = sysctl (mib, 6, processi, &data_len, NULL, 0);
246 mib [2] = KERN_PROC_PID;
247 mib [3] = GPOINTER_TO_UINT (pid);
249 res = sysctl (mib, 4, processi, &data_len, NULL, 0);
250 #endif /* KERN_PROC2 */
252 if (res < 0 || data_len != sizeof (KINFO_PROC))
257 #endif /* USE_SYSCTL */
260 * mono_process_get_name:
261 * @pid: pid of the process
262 * @buf: byte buffer where to store the name of the prcoess
263 * @len: size of the buffer @buf
265 * Return the name of the process identified by @pid, storing it
266 * inside @buf for a maximum of len bytes (including the terminating 0).
269 mono_process_get_name (gpointer pid, char *buf, int len)
274 memset (buf, 0, len);
276 if (sysctl_kinfo_proc (pid, &processi))
277 strncpy (buf, processi.kinfo_name_member, len - 1);
285 sprintf (fname, "/proc/%d/cmdline", GPOINTER_TO_INT (pid));
287 file = fopen (fname, "r");
290 r = fread (buf, 1, len - 1, file);
293 p = strrchr (buf, '/');
297 return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
304 mono_process_get_times (gpointer pid, gint64 *start_time, gint64 *user_time, gint64 *kernel_time)
307 *user_time = mono_process_get_data (pid, MONO_PROCESS_USER_TIME);
310 *kernel_time = mono_process_get_data (pid, MONO_PROCESS_SYSTEM_TIME);
315 #if USE_SYSCTL && defined(kinfo_starttime_member)
319 if (sysctl_kinfo_proc (pid, &processi))
320 *start_time = mono_100ns_datetime_from_timeval (processi.kinfo_starttime_member);
324 if (*start_time == 0) {
325 static guint64 boot_time = 0;
327 boot_time = mono_100ns_datetime () - ((guint64)mono_msec_ticks ()) * 10000;
329 *start_time = boot_time + mono_process_get_data (pid, MONO_PROCESS_ELAPSED);
335 * /proc/pid/stat format:
337 * [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
338 * [10] utime stime cutime cstime prio nice threads 0 start_time vsize
339 * [20] rss rsslim start_code end_code start_stack esp eip pending blocked sigign
340 * [30] sigcatch wchan 0 0 exit_signal cpu rt_prio policy
343 #define RET_ERROR(err) do { \
344 if (error) *error = (err); \
349 get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
351 #if defined(__APPLE__)
352 double process_user_time = 0, process_system_time = 0;//, process_percent = 0;
354 struct task_basic_info t_info;
355 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
356 thread_array_t th_array;
359 if (pid == getpid ()) {
360 /* task_for_pid () doesn't work on ios, even for the current process */
361 task = mach_task_self ();
363 if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
364 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
367 if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count) != KERN_SUCCESS) {
368 if (pid != getpid ())
369 mach_port_deallocate (mach_task_self (), task);
370 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
373 if (task_threads(task, &th_array, &th_count) != KERN_SUCCESS) {
374 if (pid != getpid ())
375 mach_port_deallocate (mach_task_self (), task);
376 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
379 for (i = 0; i < th_count; i++) {
380 double thread_user_time, thread_system_time;//, thread_percent;
382 struct thread_basic_info th_info;
383 mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT;
384 if (thread_info(th_array[i], THREAD_BASIC_INFO, (thread_info_t)&th_info, &th_info_count) == KERN_SUCCESS) {
385 thread_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6;
386 thread_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6;
387 //thread_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE;
389 process_user_time += thread_user_time;
390 process_system_time += thread_system_time;
391 //process_percent += th_percent;
395 for (i = 0; i < th_count; i++)
396 mach_port_deallocate(task, th_array[i]);
398 if (pid != getpid ())
399 mach_port_deallocate (mach_task_self (), task);
401 process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
402 process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
404 if (pos == 10 && sum == TRUE)
405 return (gint64)((process_user_time + process_system_time) * 10000000);
407 return (gint64)(process_user_time * 10000000);
409 return (gint64)(process_system_time * 10000000);
419 g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
420 f = fopen (buf, "r");
422 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
423 len = fread (buf, 1, sizeof (buf), f);
426 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
427 s = strchr (buf, ')');
429 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
431 while (g_ascii_isspace (*s)) s++;
433 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
434 /* skip the status char */
435 while (*s && !g_ascii_isspace (*s)) s++;
437 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
438 for (i = 0; i < pos; ++i) {
439 while (g_ascii_isspace (*s)) s++;
441 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
442 while (*s && !g_ascii_isspace (*s)) s++;
444 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
446 /* we are finally at the needed item */
447 value = strtoul (s, &end, 0);
448 /* add also the following value */
450 while (g_ascii_isspace (*s)) s++;
452 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
453 value += strtoul (s, &end, 0);
456 *error = MONO_PROCESS_ERROR_NONE;
464 static int user_hz = 0;
467 user_hz = sysconf (_SC_CLK_TCK);
476 get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
478 gint64 val = get_process_stat_item (pid, pos, sum, error);
479 #if defined(__APPLE__)
482 /* return 100ns ticks */
483 return (val * 10000000) / get_user_hz ();
488 get_pid_status_item (int pid, const char *item, MonoProcessError *error, int multiplier)
490 #if defined(__APPLE__)
491 // ignore the multiplier
495 struct task_basic_info t_info;
496 mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
498 if (pid == getpid ()) {
499 /* task_for_pid () doesn't work on ios, even for the current process */
500 task = mach_task_self ();
502 if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
503 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
506 if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count) != KERN_SUCCESS) {
507 if (pid != getpid ())
508 mach_port_deallocate (mach_task_self (), task);
509 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
512 if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0 || strcmp (item, "VmData") == 0)
513 ret = t_info.resident_size;
514 else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
515 ret = t_info.virtual_size;
516 else if (strcmp (item, "Threads") == 0)
521 if (pid != getpid ())
522 mach_port_deallocate (mach_task_self (), task);
529 s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
531 return ((gint64) atol (s)) * multiplier;
537 * mono_process_get_data:
538 * @pid: pid of the process
539 * @data: description of data to return
541 * Return a data item of a process like user time, memory use etc,
542 * according to the @data argumet.
545 mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProcessError *error)
548 int rpid = GPOINTER_TO_INT (pid);
551 *error = MONO_PROCESS_ERROR_OTHER;
554 case MONO_PROCESS_NUM_THREADS:
555 return get_pid_status_item (rpid, "Threads", error, 1);
556 case MONO_PROCESS_USER_TIME:
557 return get_process_stat_time (rpid, 10, FALSE, error);
558 case MONO_PROCESS_SYSTEM_TIME:
559 return get_process_stat_time (rpid, 11, FALSE, error);
560 case MONO_PROCESS_TOTAL_TIME:
561 return get_process_stat_time (rpid, 10, TRUE, error);
562 case MONO_PROCESS_WORKING_SET:
563 return get_pid_status_item (rpid, "VmRSS", error, 1024);
564 case MONO_PROCESS_WORKING_SET_PEAK:
565 val = get_pid_status_item (rpid, "VmHWM", error, 1024);
567 val = get_pid_status_item (rpid, "VmRSS", error, 1024);
569 case MONO_PROCESS_PRIVATE_BYTES:
570 return get_pid_status_item (rpid, "VmData", error, 1024);
571 case MONO_PROCESS_VIRTUAL_BYTES:
572 return get_pid_status_item (rpid, "VmSize", error, 1024);
573 case MONO_PROCESS_VIRTUAL_BYTES_PEAK:
574 val = get_pid_status_item (rpid, "VmPeak", error, 1024);
576 val = get_pid_status_item (rpid, "VmSize", error, 1024);
578 case MONO_PROCESS_FAULTS:
579 return get_process_stat_item (rpid, 6, TRUE, error);
580 case MONO_PROCESS_ELAPSED:
581 return get_process_stat_time (rpid, 18, FALSE, error);
582 case MONO_PROCESS_PPID:
583 return get_process_stat_time (rpid, 0, FALSE, error);
584 case MONO_PROCESS_PAGED_BYTES:
585 return get_pid_status_item (rpid, "VmSwap", error, 1024);
588 case MONO_PROCESS_END:
595 mono_process_get_data (gpointer pid, MonoProcessData data)
597 MonoProcessError error;
598 return mono_process_get_data_with_error (pid, data, &error);
602 mono_process_current_pid ()
604 #if defined(HAVE_UNISTD_H)
605 return (int) getpid ();
606 #elif defined(HOST_WIN32)
607 return (int) GetCurrentProcessId ();
616 * Return the number of processors on the system.
619 mono_cpu_count (void)
623 GetSystemInfo (&info);
624 return info.dwNumberOfProcessors;
626 #ifdef PLATFORM_ANDROID
627 /* Android tries really hard to save power by powering off CPUs on SMP phones which
628 * means the normal way to query cpu count returns a wrong value with userspace API.
629 * Instead we use /sys entries to query the actual hardware CPU count.
632 char buffer[8] = {'\0'};
633 int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
634 /* Format of the /sys entry is a cpulist of indexes which in the case
635 * of present is always of the form "0-(n-1)" when there is more than
636 * 1 core, n being the number of CPU cores in the system. Otherwise
637 * the value is simply 0
639 if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
640 count = strtol (((char*)buffer) + 2, NULL, 10);
647 #if defined(HOST_ARM) || defined (HOST_ARM64)
650 * Recap from Alexander Köplinger <alex.koeplinger@outlook.com>:
652 * When we merged the change from PR #2722, we started seeing random failures on ARM in
653 * the MonoTests.System.Threading.ThreadPoolTests.SetAndGetMaxThreads and
654 * MonoTests.System.Threading.ManualResetEventSlimTests.Constructor_Defaults tests. Both
655 * of those tests are dealing with Environment.ProcessorCount to verify some implementation
658 * It turns out that on the Jetson TK1 board we use on public Jenkins and on ARM kernels
659 * in general, the value returned by sched_getaffinity (or _SC_NPROCESSORS_ONLN) doesn't
660 * contain CPUs/cores that are powered off for power saving reasons. This is contrary to
661 * what happens on x86, where even cores in deep-sleep state are returned [1], [2]. This
662 * means that we would get a processor count of 1 at one point in time and a higher value
663 * when load increases later on as the system wakes CPUs.
665 * Various runtime pieces like the threadpool and also user code however relies on the
666 * value returned by Environment.ProcessorCount e.g. for deciding how many parallel tasks
667 * to start, thereby limiting the performance when that code thinks we only have one CPU.
669 * Talking to a few people, this was the reason why we changed to _SC_NPROCESSORS_CONF in
670 * mono#1688 and why we added a special case for Android in mono@de3addc to get the "real"
671 * number of processors in the system.
673 * Because of those issues Android/Dalvik also switched from _ONLN to _SC_NPROCESSORS_CONF
674 * for the Java API Runtime.availableProcessors() too [3], citing:
675 * > Traditionally this returned the number currently online, but many mobile devices are
676 * able to take unused cores offline to save power, so releases newer than Android 4.2 (Jelly
677 * Bean) return the maximum number of cores that could be made available if there were no
678 * power or heat constraints.
680 * The problem with sticking to _SC_NPROCESSORS_CONF however is that it breaks down in
681 * constrained environments like Docker or with an explicit CPU affinity set by the Linux
682 * `taskset` command, They'd get a higher CPU count than can be used, start more threads etc.
683 * which results in unnecessary context switches and overloaded systems. That's why we need
684 * to respect sched_getaffinity.
686 * So while in an ideal world we would be able to rely on sched_getaffinity/_SC_NPROCESSORS_ONLN
687 * to return the number of theoretically available CPUs regardless of power saving measures
688 * everywhere, we can't do this on ARM.
690 * I think the pragmatic solution is the following:
691 * * use sched_getaffinity (+ fallback to _SC_NPROCESSORS_ONLN in case of error) on x86. This
692 * ensures we're inline with what OpenJDK [4] and CoreCLR [5] do
693 * * use _SC_NPROCESSORS_CONF exclusively on ARM (I think we could eventually even get rid of
694 * the PLATFORM_ANDROID special case)
698 * [1] https://sourceware.org/ml/libc-alpha/2013-07/msg00383.html
699 * [2] https://lists.01.org/pipermail/powertop/2012-September/000433.html
700 * [3] https://android.googlesource.com/platform/libcore/+/750dc634e56c58d1d04f6a138734ac2b772900b5%5E1..750dc634e56c58d1d04f6a138734ac2b772900b5/
701 * [4] https://bugs.openjdk.java.net/browse/JDK-6515172
702 * [5] https://github.com/dotnet/coreclr/blob/7058273693db2555f127ce16e6b0c5b40fb04867/src/pal/src/misc/sysinfo.cpp#L148
705 #ifdef _SC_NPROCESSORS_CONF
707 int count = sysconf (_SC_NPROCESSORS_CONF);
715 #ifdef HAVE_SCHED_GETAFFINITY
718 if (sched_getaffinity (mono_process_current_pid (), sizeof (set), &set) == 0)
719 return CPU_COUNT (&set);
722 #ifdef _SC_NPROCESSORS_ONLN
724 int count = sysconf (_SC_NPROCESSORS_ONLN);
730 #endif /* defined(HOST_ARM) || defined (HOST_ARM64) */
736 size_t len = sizeof (int);
739 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
743 #endif /* HOST_WIN32 */
749 get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
753 int hz = get_user_hz ();
754 guint64 user_ticks = 0, nice_ticks = 0, system_ticks = 0, idle_ticks = 0, irq_ticks = 0, sirq_ticks = 0;
755 FILE *f = fopen ("/proc/stat", "r");
759 hz *= mono_cpu_count ();
760 while ((s = fgets (buf, sizeof (buf), f))) {
762 if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
764 } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
772 user_ticks = strtoull (data, &data, 10);
773 nice_ticks = strtoull (data, &data, 10);
774 system_ticks = strtoull (data, &data, 10);
775 idle_ticks = strtoull (data, &data, 10);
776 /* iowait_ticks = strtoull (data, &data, 10); */
777 irq_ticks = strtoull (data, &data, 10);
778 sirq_ticks = strtoull (data, &data, 10);
784 *user = (user_ticks + nice_ticks) * 10000000 / hz;
786 *systemt = (system_ticks) * 10000000 / hz;
788 *irq = (irq_ticks) * 10000000 / hz;
790 *sirq = (sirq_ticks) * 10000000 / hz;
792 *idle = (idle_ticks) * 10000000 / hz;
797 * @cpu_id: processor number or -1 to get a summary of all the processors
798 * @data: type of data to retrieve
800 * Get data about a processor on the system, like time spent in user space or idle time.
803 mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
808 *error = MONO_PROCESS_ERROR_NONE;
810 case MONO_CPU_USER_TIME:
811 get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
813 case MONO_CPU_PRIV_TIME:
814 get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
816 case MONO_CPU_INTR_TIME:
817 get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
819 case MONO_CPU_DCP_TIME:
820 get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
822 case MONO_CPU_IDLE_TIME:
823 get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);
834 mono_atexit (void (*func)(void))
836 #ifdef PLATFORM_ANDROID
837 /* Some versions of android libc doesn't define atexit () */
840 return atexit (func);
845 * This function returns the cpu usage in percentage,
846 * normalized on the number of cores.
848 * Warning : the percentage returned can be > 100%. This
849 * might happens on systems like Android which, for
850 * battery and performance reasons, shut down cores and
851 * lie about the number of active cores.
854 mono_cpu_usage (MonoCpuUsageState *prev)
856 gint32 cpu_usage = 0;
857 gint64 cpu_total_time;
858 gint64 cpu_busy_time;
861 struct rusage resource_usage;
866 if (getrusage (RUSAGE_SELF, &resource_usage) == -1) {
867 g_error ("getrusage() failed, errno is %d (%s)\n", errno, strerror (errno));
871 current_time = mono_100ns_ticks ();
872 kernel_time = resource_usage.ru_stime.tv_sec * 1000 * 1000 * 10 + resource_usage.ru_stime.tv_usec * 10;
873 user_time = resource_usage.ru_utime.tv_sec * 1000 * 1000 * 10 + resource_usage.ru_utime.tv_usec * 10;
875 cpu_busy_time = (user_time - (prev ? prev->user_time : 0)) + (kernel_time - (prev ? prev->kernel_time : 0));
876 cpu_total_time = (current_time - (prev ? prev->current_time : 0)) * mono_cpu_count ();
879 prev->kernel_time = kernel_time;
880 prev->user_time = user_time;
881 prev->current_time = current_time;
888 if (!GetSystemTimes ((FILETIME*) &idle_time, (FILETIME*) &kernel_time, (FILETIME*) &user_time)) {
889 g_error ("GetSystemTimes() failed, error code is %d\n", GetLastError ());
893 cpu_total_time = (gint64)((user_time - (prev ? prev->user_time : 0)) + (kernel_time - (prev ? prev->kernel_time : 0)));
894 cpu_busy_time = (gint64)(cpu_total_time - (idle_time - (prev ? prev->idle_time : 0)));
897 prev->idle_time = idle_time;
898 prev->kernel_time = kernel_time;
899 prev->user_time = user_time;
903 if (cpu_total_time > 0 && cpu_busy_time > 0)
904 cpu_usage = (gint32)(cpu_busy_time * 100 / cpu_total_time);