X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fmono-proclib.c;h=9bf6ca52ae0188a5f93badde282e87aa21ef4510;hb=f54b3d4a8b684bded674e62f7b9e7bce084109c0;hp=749d98498717c2f59d1e4930f87a4924305a3481;hpb=e097a98f3bd9082c7d03fa580b604c0228b4ad67;p=mono.git diff --git a/mono/utils/mono-proclib.c b/mono/utils/mono-proclib.c index 749d9849871..9bf6ca52ae0 100644 --- a/mono/utils/mono-proclib.c +++ b/mono/utils/mono-proclib.c @@ -40,12 +40,16 @@ #include #endif #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC +# define kinfo_starttime_member kp_proc.p_starttime # define kinfo_pid_member kp_proc.p_pid # define kinfo_name_member kp_proc.p_comm #elif defined(__OpenBSD__) +// Can not figure out how to get the proc's start time on OpenBSD +# undef kinfo_starttime_member # define kinfo_pid_member p_pid # define kinfo_name_member p_comm #else +#define kinfo_starttime_member ki_start #define kinfo_pid_member ki_pid #define kinfo_name_member ki_comm #endif @@ -157,7 +161,7 @@ mono_process_list (int *size) count = 16; else count *= 2; - buf = g_realloc (buf, count * sizeof (void*)); + buf = (void **)g_realloc (buf, count * sizeof (void*)); } buf [i++] = GINT_TO_POINTER (pid); } @@ -183,7 +187,7 @@ get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoPr *error = MONO_PROCESS_ERROR_NOT_FOUND; return NULL; } - while ((s = fgets (buf, blen, f))) { + while ((s = fgets (buf, sizeof (buf), f))) { if (*item != *buf) continue; if (strncmp (buf, item, len)) @@ -304,12 +308,12 @@ mono_process_get_times (gpointer pid, gint64 *start_time, gint64 *user_time, gin if (start_time) { *start_time = 0; -#if USE_SYSCTL +#if USE_SYSCTL && defined(kinfo_starttime_member) { KINFO_PROC processi; if (sysctl_kinfo_proc (pid, &processi)) - *start_time = mono_100ns_datetime_from_timeval (processi.kp_proc.p_starttime); + *start_time = mono_100ns_datetime_from_timeval (processi.kinfo_starttime_member); } #endif @@ -363,7 +367,8 @@ get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error) } if (task_threads(task, &th_array, &th_count) != KERN_SUCCESS) { - mach_port_deallocate (mach_task_self (), task); + if (pid != getpid ()) + mach_port_deallocate (mach_task_self (), task); RET_ERROR (MONO_PROCESS_ERROR_OTHER); } @@ -386,7 +391,8 @@ get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error) for (i = 0; i < th_count; i++) mach_port_deallocate(task, th_array[i]); - mach_port_deallocate (mach_task_self (), task); + if (pid != getpid ()) + mach_port_deallocate (mach_task_self (), task); process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6; process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6; @@ -608,6 +614,11 @@ mono_process_current_pid () int mono_cpu_count (void) { +#ifdef HOST_WIN32 + SYSTEM_INFO info; + GetSystemInfo (&info); + return info.dwNumberOfProcessors; +#else int count = 0; #ifdef PLATFORM_ANDROID /* Android tries really hard to save power by powering off CPUs on SMP phones which @@ -643,12 +654,6 @@ mono_cpu_count (void) return count; } #endif -#ifdef HOST_WIN32 - { - SYSTEM_INFO info; - GetSystemInfo (&info); - return info.dwNumberOfProcessors; - } #endif /* FIXME: warn */ return 1; @@ -750,6 +755,15 @@ mono_atexit (void (*func)(void)) #endif } +/* + * This function returns the cpu usage in percentage, + * normalized on the number of cores. + * + * Warning : the percentage returned can be > 100%. This + * might happens on systems like Android which, for + * battery and performance reasons, shut down cores and + * lie about the number of active cores. + */ gint32 mono_cpu_usage (MonoCpuUsageState *prev) { @@ -803,8 +817,5 @@ mono_cpu_usage (MonoCpuUsageState *prev) if (cpu_total_time > 0 && cpu_busy_time > 0) cpu_usage = (gint32)(cpu_busy_time * 100 / cpu_total_time); - g_assert (cpu_usage >= 0); - g_assert (cpu_usage <= 100); - return cpu_usage; }