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>
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
34 #ifdef HAVE_SYS_SYSCTL_H
35 #include <sys/sysctl.h>
37 #include <sys/resource.h>
39 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
41 #if defined(__APPLE__)
42 #include <mach/mach.h>
44 #ifdef HAVE_SYS_USER_H
47 #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
48 # define kinfo_starttime_member kp_proc.p_starttime
49 # define kinfo_pid_member kp_proc.p_pid
50 # define kinfo_name_member kp_proc.p_comm
51 #elif defined(__OpenBSD__)
52 // Can not figure out how to get the proc's start time on OpenBSD
53 # undef kinfo_starttime_member
54 # define kinfo_pid_member p_pid
55 # define kinfo_name_member p_comm
57 #define kinfo_starttime_member ki_start
58 #define kinfo_pid_member ki_pid
59 #define kinfo_name_member ki_comm
66 * @size: a pointer to a location where the size of the returned array is stored
68 * Return an array of pid values for the processes currently running on the system.
69 * The size of the array is stored in @size.
72 mono_process_list (int *size)
78 size_t data_len = sizeof (struct kinfo_proc2) * 400;
79 struct kinfo_proc2 *processes = malloc (data_len);
82 size_t data_len = sizeof (struct kinfo_proc) * 16;
83 struct kinfo_proc *processes;
85 #endif /* KERN_PROC2 */
97 mib [2] = KERN_PROC_ALL;
99 mib [4] = sizeof(struct kinfo_proc2);
100 mib [5] = 400; /* XXX */
102 res = sysctl (mib, 6, processes, &data_len, NULL, 0);
112 mib [2] = KERN_PROC_ALL;
115 res = sysctl (mib, 4, NULL, &data_len, NULL, 0);
118 processes = (struct kinfo_proc *) malloc (data_len);
119 res = sysctl (mib, 4, processes, &data_len, NULL, 0);
129 #endif /* KERN_PROC2 */
132 res = data_len/sizeof (struct kinfo_proc2);
134 res = data_len/sizeof (struct kinfo_proc);
135 #endif /* KERN_PROC2 */
136 buf = (void **) g_realloc (buf, res * sizeof (void*));
137 for (i = 0; i < res; ++i)
138 buf [i] = GINT_TO_POINTER (processes [i].kinfo_pid_member);
143 #elif defined(__HAIKU__)
144 /* FIXME: Add back the code from 9185fcc305e43428d0f40f3ee37c8a405d41c9ae */
145 g_assert_not_reached ();
152 GDir *dir = g_dir_open ("/proc/", 0, NULL);
158 while ((name = g_dir_read_name (dir))) {
161 pid = strtol (name, &nend, 10);
162 if (pid <= 0 || nend == name || *nend)
169 buf = (void **)g_realloc (buf, count * sizeof (void*));
171 buf [i++] = GINT_TO_POINTER (pid);
180 static G_GNUC_UNUSED char*
181 get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
186 int len = strlen (item);
188 g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
189 f = fopen (buf, "r");
192 *error = MONO_PROCESS_ERROR_NOT_FOUND;
195 while ((s = fgets (buf, sizeof (buf), f))) {
198 if (strncmp (buf, item, len))
201 while (g_ascii_isspace (*s)) s++;
204 while (g_ascii_isspace (*s)) s++;
207 strncpy (rbuf, s, MIN (len, blen));
208 rbuf [MIN (len, blen) - 1] = 0;
210 *error = MONO_PROCESS_ERROR_NONE;
215 *error = MONO_PROCESS_ERROR_OTHER;
222 #define KINFO_PROC struct kinfo_proc2
224 #define KINFO_PROC struct kinfo_proc
228 sysctl_kinfo_proc (gpointer pid, KINFO_PROC* processi)
231 size_t data_len = sizeof (KINFO_PROC);
236 mib [1] = KERN_PROC2;
237 mib [2] = KERN_PROC_PID;
238 mib [3] = GPOINTER_TO_UINT (pid);
239 mib [4] = sizeof(KINFO_PROC);
240 mib [5] = 400; /* XXX */
242 res = sysctl (mib, 6, processi, &data_len, NULL, 0);
247 mib [2] = KERN_PROC_PID;
248 mib [3] = GPOINTER_TO_UINT (pid);
250 res = sysctl (mib, 4, processi, &data_len, NULL, 0);
251 #endif /* KERN_PROC2 */
253 if (res < 0 || data_len != sizeof (KINFO_PROC))
258 #endif /* USE_SYSCTL */
261 * mono_process_get_name:
262 * @pid: pid of the process
263 * @buf: byte buffer where to store the name of the prcoess
264 * @len: size of the buffer @buf
266 * Return the name of the process identified by @pid, storing it
267 * inside @buf for a maximum of len bytes (including the terminating 0).
270 mono_process_get_name (gpointer pid, char *buf, int len)
275 memset (buf, 0, len);
277 if (sysctl_kinfo_proc (pid, &processi))
278 strncpy (buf, processi.kinfo_name_member, len - 1);
286 sprintf (fname, "/proc/%d/cmdline", GPOINTER_TO_INT (pid));
288 file = fopen (fname, "r");
291 r = fread (buf, 1, len - 1, file);
294 p = strrchr (buf, '/');
298 return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
305 mono_process_get_times (gpointer pid, gint64 *start_time, gint64 *user_time, gint64 *kernel_time)
308 *user_time = mono_process_get_data (pid, MONO_PROCESS_USER_TIME);
311 *kernel_time = mono_process_get_data (pid, MONO_PROCESS_SYSTEM_TIME);
316 #if USE_SYSCTL && defined(kinfo_starttime_member)
320 if (sysctl_kinfo_proc (pid, &processi))
321 *start_time = mono_100ns_datetime_from_timeval (processi.kinfo_starttime_member);
325 if (*start_time == 0) {
326 static guint64 boot_time = 0;
328 boot_time = mono_100ns_datetime () - mono_msec_boottime () * 10000;
330 *start_time = boot_time + mono_process_get_data (pid, MONO_PROCESS_ELAPSED);
336 * /proc/pid/stat format:
338 * [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
339 * [10] utime stime cutime cstime prio nice threads 0 start_time vsize
340 * [20] rss rsslim start_code end_code start_stack esp eip pending blocked sigign
341 * [30] sigcatch wchan 0 0 exit_signal cpu rt_prio policy
344 #define RET_ERROR(err) do { \
345 if (error) *error = (err); \
350 get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
352 #if defined(__APPLE__)
353 double process_user_time = 0, process_system_time = 0;//, process_percent = 0;
355 struct task_basic_info t_info;
356 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
357 thread_array_t th_array;
361 if (pid == getpid ()) {
362 /* task_for_pid () doesn't work on ios, even for the current process */
363 task = mach_task_self ();
366 ret = task_for_pid (mach_task_self (), pid, &task);
367 } while (ret == KERN_ABORTED);
369 if (ret != KERN_SUCCESS)
370 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
374 ret = task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
375 } while (ret == KERN_ABORTED);
377 if (ret != KERN_SUCCESS) {
378 if (pid != getpid ())
379 mach_port_deallocate (mach_task_self (), task);
380 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
384 ret = task_threads (task, &th_array, &th_count);
385 } while (ret == KERN_ABORTED);
387 if (ret != KERN_SUCCESS) {
388 if (pid != getpid ())
389 mach_port_deallocate (mach_task_self (), task);
390 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
393 for (i = 0; i < th_count; i++) {
394 double thread_user_time, thread_system_time;//, thread_percent;
396 struct thread_basic_info th_info;
397 mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT;
399 ret = thread_info(th_array[i], THREAD_BASIC_INFO, (thread_info_t)&th_info, &th_info_count);
400 } while (ret == KERN_ABORTED);
402 if (ret == KERN_SUCCESS) {
403 thread_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6;
404 thread_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6;
405 //thread_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE;
407 process_user_time += thread_user_time;
408 process_system_time += thread_system_time;
409 //process_percent += th_percent;
413 for (i = 0; i < th_count; i++)
414 mach_port_deallocate(task, th_array[i]);
416 if (pid != getpid ())
417 mach_port_deallocate (mach_task_self (), task);
419 process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
420 process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
422 if (pos == 10 && sum == TRUE)
423 return (gint64)((process_user_time + process_system_time) * 10000000);
425 return (gint64)(process_user_time * 10000000);
427 return (gint64)(process_system_time * 10000000);
437 g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
438 f = fopen (buf, "r");
440 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
441 len = fread (buf, 1, sizeof (buf), f);
444 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
445 s = strchr (buf, ')');
447 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
449 while (g_ascii_isspace (*s)) s++;
451 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
452 /* skip the status char */
453 while (*s && !g_ascii_isspace (*s)) s++;
455 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
456 for (i = 0; i < pos; ++i) {
457 while (g_ascii_isspace (*s)) s++;
459 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
460 while (*s && !g_ascii_isspace (*s)) s++;
462 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
464 /* we are finally at the needed item */
465 value = strtoul (s, &end, 0);
466 /* add also the following value */
468 while (g_ascii_isspace (*s)) s++;
470 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
471 value += strtoul (s, &end, 0);
474 *error = MONO_PROCESS_ERROR_NONE;
482 static int user_hz = 0;
485 user_hz = sysconf (_SC_CLK_TCK);
494 get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
496 gint64 val = get_process_stat_item (pid, pos, sum, error);
497 #if defined(__APPLE__)
500 /* return 100ns ticks */
501 return (val * 10000000) / get_user_hz ();
506 get_pid_status_item (int pid, const char *item, MonoProcessError *error, int multiplier)
508 #if defined(__APPLE__)
509 // ignore the multiplier
513 struct task_basic_info t_info;
514 mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
515 kern_return_t mach_ret;
517 if (pid == getpid ()) {
518 /* task_for_pid () doesn't work on ios, even for the current process */
519 task = mach_task_self ();
522 mach_ret = task_for_pid (mach_task_self (), pid, &task);
523 } while (mach_ret == KERN_ABORTED);
525 if (mach_ret != KERN_SUCCESS)
526 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
530 mach_ret = task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count);
531 } while (mach_ret == KERN_ABORTED);
533 if (mach_ret != KERN_SUCCESS) {
534 if (pid != getpid ())
535 mach_port_deallocate (mach_task_self (), task);
536 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
539 if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0 || strcmp (item, "VmData") == 0)
540 ret = t_info.resident_size;
541 else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
542 ret = t_info.virtual_size;
543 else if (strcmp (item, "Threads") == 0)
548 if (pid != getpid ())
549 mach_port_deallocate (mach_task_self (), task);
556 s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
558 return ((gint64) atol (s)) * multiplier;
564 * mono_process_get_data:
565 * @pid: pid of the process
566 * @data: description of data to return
568 * Return a data item of a process like user time, memory use etc,
569 * according to the @data argumet.
572 mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProcessError *error)
575 int rpid = GPOINTER_TO_INT (pid);
578 *error = MONO_PROCESS_ERROR_OTHER;
581 case MONO_PROCESS_NUM_THREADS:
582 return get_pid_status_item (rpid, "Threads", error, 1);
583 case MONO_PROCESS_USER_TIME:
584 return get_process_stat_time (rpid, 10, FALSE, error);
585 case MONO_PROCESS_SYSTEM_TIME:
586 return get_process_stat_time (rpid, 11, FALSE, error);
587 case MONO_PROCESS_TOTAL_TIME:
588 return get_process_stat_time (rpid, 10, TRUE, error);
589 case MONO_PROCESS_WORKING_SET:
590 return get_pid_status_item (rpid, "VmRSS", error, 1024);
591 case MONO_PROCESS_WORKING_SET_PEAK:
592 val = get_pid_status_item (rpid, "VmHWM", error, 1024);
594 val = get_pid_status_item (rpid, "VmRSS", error, 1024);
596 case MONO_PROCESS_PRIVATE_BYTES:
597 return get_pid_status_item (rpid, "VmData", error, 1024);
598 case MONO_PROCESS_VIRTUAL_BYTES:
599 return get_pid_status_item (rpid, "VmSize", error, 1024);
600 case MONO_PROCESS_VIRTUAL_BYTES_PEAK:
601 val = get_pid_status_item (rpid, "VmPeak", error, 1024);
603 val = get_pid_status_item (rpid, "VmSize", error, 1024);
605 case MONO_PROCESS_FAULTS:
606 return get_process_stat_item (rpid, 6, TRUE, error);
607 case MONO_PROCESS_ELAPSED:
608 return get_process_stat_time (rpid, 18, FALSE, error);
609 case MONO_PROCESS_PPID:
610 return get_process_stat_time (rpid, 0, FALSE, error);
611 case MONO_PROCESS_PAGED_BYTES:
612 return get_pid_status_item (rpid, "VmSwap", error, 1024);
615 case MONO_PROCESS_END:
622 mono_process_get_data (gpointer pid, MonoProcessData data)
624 MonoProcessError error;
625 return mono_process_get_data_with_error (pid, data, &error);
629 mono_process_current_pid ()
631 #if defined(HAVE_UNISTD_H)
632 return (int) getpid ();
633 #elif defined(HOST_WIN32)
634 return (int) GetCurrentProcessId ();
643 * Return the number of processors on the system.
646 mono_cpu_count (void)
650 GetSystemInfo (&info);
651 return info.dwNumberOfProcessors;
653 #ifdef PLATFORM_ANDROID
654 /* Android tries really hard to save power by powering off CPUs on SMP phones which
655 * means the normal way to query cpu count returns a wrong value with userspace API.
656 * Instead we use /sys entries to query the actual hardware CPU count.
659 char buffer[8] = {'\0'};
660 int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
661 /* Format of the /sys entry is a cpulist of indexes which in the case
662 * of present is always of the form "0-(n-1)" when there is more than
663 * 1 core, n being the number of CPU cores in the system. Otherwise
664 * the value is simply 0
666 if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
667 count = strtol (((char*)buffer) + 2, NULL, 10);
674 #if defined(HOST_ARM) || defined (HOST_ARM64)
677 * Recap from Alexander Köplinger <alex.koeplinger@outlook.com>:
679 * When we merged the change from PR #2722, we started seeing random failures on ARM in
680 * the MonoTests.System.Threading.ThreadPoolTests.SetAndGetMaxThreads and
681 * MonoTests.System.Threading.ManualResetEventSlimTests.Constructor_Defaults tests. Both
682 * of those tests are dealing with Environment.ProcessorCount to verify some implementation
685 * It turns out that on the Jetson TK1 board we use on public Jenkins and on ARM kernels
686 * in general, the value returned by sched_getaffinity (or _SC_NPROCESSORS_ONLN) doesn't
687 * contain CPUs/cores that are powered off for power saving reasons. This is contrary to
688 * what happens on x86, where even cores in deep-sleep state are returned [1], [2]. This
689 * means that we would get a processor count of 1 at one point in time and a higher value
690 * when load increases later on as the system wakes CPUs.
692 * Various runtime pieces like the threadpool and also user code however relies on the
693 * value returned by Environment.ProcessorCount e.g. for deciding how many parallel tasks
694 * to start, thereby limiting the performance when that code thinks we only have one CPU.
696 * Talking to a few people, this was the reason why we changed to _SC_NPROCESSORS_CONF in
697 * mono#1688 and why we added a special case for Android in mono@de3addc to get the "real"
698 * number of processors in the system.
700 * Because of those issues Android/Dalvik also switched from _ONLN to _SC_NPROCESSORS_CONF
701 * for the Java API Runtime.availableProcessors() too [3], citing:
702 * > Traditionally this returned the number currently online, but many mobile devices are
703 * able to take unused cores offline to save power, so releases newer than Android 4.2 (Jelly
704 * Bean) return the maximum number of cores that could be made available if there were no
705 * power or heat constraints.
707 * The problem with sticking to _SC_NPROCESSORS_CONF however is that it breaks down in
708 * constrained environments like Docker or with an explicit CPU affinity set by the Linux
709 * `taskset` command, They'd get a higher CPU count than can be used, start more threads etc.
710 * which results in unnecessary context switches and overloaded systems. That's why we need
711 * to respect sched_getaffinity.
713 * So while in an ideal world we would be able to rely on sched_getaffinity/_SC_NPROCESSORS_ONLN
714 * to return the number of theoretically available CPUs regardless of power saving measures
715 * everywhere, we can't do this on ARM.
717 * I think the pragmatic solution is the following:
718 * * use sched_getaffinity (+ fallback to _SC_NPROCESSORS_ONLN in case of error) on x86. This
719 * ensures we're inline with what OpenJDK [4] and CoreCLR [5] do
720 * * use _SC_NPROCESSORS_CONF exclusively on ARM (I think we could eventually even get rid of
721 * the PLATFORM_ANDROID special case)
725 * [1] https://sourceware.org/ml/libc-alpha/2013-07/msg00383.html
726 * [2] https://lists.01.org/pipermail/powertop/2012-September/000433.html
727 * [3] https://android.googlesource.com/platform/libcore/+/750dc634e56c58d1d04f6a138734ac2b772900b5%5E1..750dc634e56c58d1d04f6a138734ac2b772900b5/
728 * [4] https://bugs.openjdk.java.net/browse/JDK-6515172
729 * [5] https://github.com/dotnet/coreclr/blob/7058273693db2555f127ce16e6b0c5b40fb04867/src/pal/src/misc/sysinfo.cpp#L148
732 #ifdef _SC_NPROCESSORS_CONF
734 int count = sysconf (_SC_NPROCESSORS_CONF);
742 #ifdef HAVE_SCHED_GETAFFINITY
745 if (sched_getaffinity (mono_process_current_pid (), sizeof (set), &set) == 0)
746 return CPU_COUNT (&set);
749 #ifdef _SC_NPROCESSORS_ONLN
751 int count = sysconf (_SC_NPROCESSORS_ONLN);
757 #endif /* defined(HOST_ARM) || defined (HOST_ARM64) */
763 size_t len = sizeof (int);
766 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
770 #endif /* HOST_WIN32 */
776 get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
780 int hz = get_user_hz ();
781 guint64 user_ticks = 0, nice_ticks = 0, system_ticks = 0, idle_ticks = 0, irq_ticks = 0, sirq_ticks = 0;
782 FILE *f = fopen ("/proc/stat", "r");
786 hz *= mono_cpu_count ();
787 while ((s = fgets (buf, sizeof (buf), f))) {
789 if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
791 } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
799 user_ticks = strtoull (data, &data, 10);
800 nice_ticks = strtoull (data, &data, 10);
801 system_ticks = strtoull (data, &data, 10);
802 idle_ticks = strtoull (data, &data, 10);
803 /* iowait_ticks = strtoull (data, &data, 10); */
804 irq_ticks = strtoull (data, &data, 10);
805 sirq_ticks = strtoull (data, &data, 10);
811 *user = (user_ticks + nice_ticks) * 10000000 / hz;
813 *systemt = (system_ticks) * 10000000 / hz;
815 *irq = (irq_ticks) * 10000000 / hz;
817 *sirq = (sirq_ticks) * 10000000 / hz;
819 *idle = (idle_ticks) * 10000000 / hz;
824 * @cpu_id: processor number or -1 to get a summary of all the processors
825 * @data: type of data to retrieve
827 * Get data about a processor on the system, like time spent in user space or idle time.
830 mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
835 *error = MONO_PROCESS_ERROR_NONE;
837 case MONO_CPU_USER_TIME:
838 get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
840 case MONO_CPU_PRIV_TIME:
841 get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
843 case MONO_CPU_INTR_TIME:
844 get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
846 case MONO_CPU_DCP_TIME:
847 get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
849 case MONO_CPU_IDLE_TIME:
850 get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);
861 mono_atexit (void (*func)(void))
863 #ifdef PLATFORM_ANDROID
864 /* Some versions of android libc doesn't define atexit () */
867 return atexit (func);
872 * This function returns the cpu usage in percentage,
873 * normalized on the number of cores.
875 * Warning : the percentage returned can be > 100%. This
876 * might happens on systems like Android which, for
877 * battery and performance reasons, shut down cores and
878 * lie about the number of active cores.
881 mono_cpu_usage (MonoCpuUsageState *prev)
883 gint32 cpu_usage = 0;
884 gint64 cpu_total_time;
885 gint64 cpu_busy_time;
888 struct rusage resource_usage;
893 if (getrusage (RUSAGE_SELF, &resource_usage) == -1) {
894 g_error ("getrusage() failed, errno is %d (%s)\n", errno, strerror (errno));
898 current_time = mono_100ns_ticks ();
899 kernel_time = resource_usage.ru_stime.tv_sec * 1000 * 1000 * 10 + resource_usage.ru_stime.tv_usec * 10;
900 user_time = resource_usage.ru_utime.tv_sec * 1000 * 1000 * 10 + resource_usage.ru_utime.tv_usec * 10;
902 cpu_busy_time = (user_time - (prev ? prev->user_time : 0)) + (kernel_time - (prev ? prev->kernel_time : 0));
903 cpu_total_time = (current_time - (prev ? prev->current_time : 0)) * mono_cpu_count ();
906 prev->kernel_time = kernel_time;
907 prev->user_time = user_time;
908 prev->current_time = current_time;
915 if (!GetSystemTimes ((FILETIME*) &idle_time, (FILETIME*) &kernel_time, (FILETIME*) &user_time)) {
916 g_error ("GetSystemTimes() failed, error code is %d\n", GetLastError ());
920 cpu_total_time = (gint64)((user_time - (prev ? prev->user_time : 0)) + (kernel_time - (prev ? prev->kernel_time : 0)));
921 cpu_busy_time = (gint64)(cpu_total_time - (idle_time - (prev ? prev->idle_time : 0)));
924 prev->idle_time = idle_time;
925 prev->kernel_time = kernel_time;
926 prev->user_time = user_time;
930 if (cpu_total_time > 0 && cpu_busy_time > 0)
931 cpu_usage = (gint32)(cpu_busy_time * 100 / cpu_total_time);