Merge pull request #2338 from BogdanovKirill/httpwritefix3
[mono.git] / mono / utils / mono-proclib.c
index dea168417927c9934afc12fa1439459c8aaa4faa..c4e20e4ed42400bad8e38c6ddb3a8ff100d13b87 100644 (file)
 #include <sys/user.h>
 #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);
        }
@@ -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
 
@@ -751,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)
 {
@@ -804,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;
 }