2010-06-15 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / utils / mono-proclib.c
index 445366bd4ef8fbbc0f4a635eb6b8a317d4091f57..a51e75ab552a36fb2414b63ce017ed2679b03e97 100644 (file)
@@ -8,11 +8,31 @@
 #include <unistd.h>
 #endif
 
+#ifdef HOST_WIN32
+#include <windows.h>
+#endif
+
 /* FIXME: bsds untested */
 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #include <sys/proc.h>
+#ifdef HAVE_SYS_USER_H
+#include <sys/user.h>
+#endif
+#ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
+#  ifdef KERN_PROC2
+#    define kinfo_pid_member p_pid
+#    define kinfo_name_member p_comm
+#  else
+#    define kinfo_pid_member kp_proc.p_pid
+#    define kinfo_name_member kp_proc.p_comm
+#  endif
+#else
+#define kinfo_pid_member ki_pid
+#define kinfo_name_member ki_comm
+#endif
 #define USE_SYSCTL 1
 #endif
 
@@ -27,10 +47,16 @@ gpointer*
 mono_process_list (int *size)
 {
 #if USE_SYSCTL
-       int mib [4];
        int res, i;
+#ifdef KERN_PROC2
+       int mib [6];
+       size_t data_len = sizeof (struct kinfo_proc2) * 400;
+       struct kinfo_proc2 *processes = malloc (data_len);
+#else
+       int mib [4];
        size_t data_len = sizeof (struct kinfo_proc) * 400;
        struct kinfo_proc *processes = malloc (data_len);
+#endif /* KERN_PROC2 */
        void **buf = NULL;
 
        if (size)
@@ -38,20 +64,36 @@ mono_process_list (int *size)
        if (!processes)
                return NULL;
 
+#ifdef KERN_PROC2
+       mib [0] = CTL_KERN;
+       mib [1] = KERN_PROC2;
+       mib [2] = KERN_PROC_ALL;
+       mib [3] = 0;
+       mib [4] = sizeof(struct kinfo_proc2);
+       mib [5] = 400; /* XXX */
+
+       res = sysctl (mib, 6, processes, &data_len, NULL, 0);
+#else
        mib [0] = CTL_KERN;
        mib [1] = KERN_PROC;
        mib [2] = KERN_PROC_ALL;
        mib [3] = 0;
        
        res = sysctl (mib, 4, processes, &data_len, NULL, 0);
+#endif /* KERN_PROC2 */
+
        if (res < 0) {
                free (processes);
                return NULL;
        }
+#ifdef KERN_PROC2
+       res = data_len/sizeof (struct kinfo_proc2);
+#else
        res = data_len/sizeof (struct kinfo_proc);
+#endif /* KERN_PROC2 */
        buf = g_realloc (buf, res * sizeof (void*));
        for (i = 0; i < res; ++i)
-               buf [i] = GINT_TO_POINTER (processes [i].kp_proc.p_pid);
+               buf [i] = GINT_TO_POINTER (processes [i].kinfo_pid_member);
        free (processes);
        if (size)
                *size = res;
@@ -89,6 +131,45 @@ mono_process_list (int *size)
 #endif
 }
 
+static char*
+get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
+{
+       char buf [256];
+       char *s;
+       FILE *f;
+       int len = strlen (item);
+
+       g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
+       f = fopen (buf, "r");
+       if (!f) {
+               if (error)
+                       *error = MONO_PROCESS_ERROR_NOT_FOUND;
+               return NULL;
+       }
+       while ((s = fgets (buf, blen, f))) {
+               if (*item != *buf)
+                       continue;
+               if (strncmp (buf, item, len))
+                       continue;
+               s = buf + len;
+               while (g_ascii_isspace (*s)) s++;
+               if (*s++ != ':')
+                       continue;
+               while (g_ascii_isspace (*s)) s++;
+               fclose (f);
+               len = strlen (s);
+               strncpy (rbuf, s, MIN (len, blen));
+               rbuf [MIN (len, blen) - 1] = 0;
+               if (error)
+                       *error = MONO_PROCESS_ERROR_NONE;
+               return rbuf;
+       }
+       fclose (f);
+       if (error)
+               *error = MONO_PROCESS_ERROR_OTHER;
+       return NULL;
+}
+
 /**
  * mono_process_get_name:
  * @pid: pid of the process
@@ -102,14 +183,33 @@ char*
 mono_process_get_name (gpointer pid, char *buf, int len)
 {
 #if USE_SYSCTL
-       int mib [4];
        int res;
-       char *p;
+#ifdef KERN_PROC2
+       int mib [6];
+       size_t data_len = sizeof (struct kinfo_proc2);
+       struct kinfo_proc2 processi;
+#else
+       int mib [4];
        size_t data_len = sizeof (struct kinfo_proc);
        struct kinfo_proc processi;
+#endif /* KERN_PROC2 */
 
        memset (buf, 0, len);
 
+#ifdef KERN_PROC2
+       mib [0] = CTL_KERN;
+       mib [1] = KERN_PROC2;
+       mib [2] = KERN_PROC_PID;
+       mib [3] = GPOINTER_TO_UINT (pid);
+       mib [4] = sizeof(struct kinfo_proc2);
+       mib [5] = 400; /* XXX */
+
+       res = sysctl (mib, 6, &processi, &data_len, NULL, 0);
+
+       if (res < 0 || data_len != sizeof (struct kinfo_proc2)) {
+               return buf;
+       }
+#else
        mib [0] = CTL_KERN;
        mib [1] = KERN_PROC;
        mib [2] = KERN_PROC_PID;
@@ -119,7 +219,8 @@ mono_process_get_name (gpointer pid, char *buf, int len)
        if (res < 0 || data_len != sizeof (struct kinfo_proc)) {
                return buf;
        }
-       strncpy (buf, processi.kp_proc.p_comm, len - 1);
+#endif /* KERN_PROC2 */
+       strncpy (buf, processi.kinfo_name_member, len - 1);
        return buf;
 #else
        char fname [128];
@@ -137,6 +238,9 @@ mono_process_get_name (gpointer pid, char *buf, int len)
        p = strrchr (buf, '/');
        if (p)
                return p + 1;
+       if (r == 0) {
+               return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
+       }
        return buf;
 #endif
 }
@@ -145,9 +249,9 @@ mono_process_get_name (gpointer pid, char *buf, int len)
  * /proc/pid/stat format:
  * pid (cmdname) S 
  *     [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
- *     [10] utime stime cutime cstime prio nice threads start_time vsize rss
- *     [20] rsslim start_code end_code start_stack esp eip pending blocked sigign sigcatch
- *     [30] wchan 0 0 exit_signal cpu rt_prio policy
+ *     [10] utime stime cutime cstime prio nice threads start_time vsize rss
+ *     [20] rss rsslim start_code end_code start_stack esp eip pending blocked sigign
+ *     [30] sigcatch wchan 0 0 exit_signal cpu rt_prio policy
  */
 
 #define RET_ERROR(err) do {    \
@@ -205,11 +309,10 @@ get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
        return value;
 }
 
-static gint64
-get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
+static int
+get_user_hz (void)
 {
        static int user_hz = 0;
-       gint64 val = get_process_stat_item (pid, pos, sum, error);
        if (user_hz == 0) {
 #ifdef _SC_CLK_TCK
                user_hz = sysconf (_SC_CLK_TCK);
@@ -217,36 +320,27 @@ get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
                if (user_hz == 0)
                        user_hz = 100;
        }
-       /* return milliseconds */
-       return (val * 1000) / user_hz;
+       return user_hz;
+}
+
+static gint64
+get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
+{
+       gint64 val = get_process_stat_item (pid, pos, sum, error);
+       /* return 100ns ticks */
+       return (val * 10000000) / get_user_hz ();
 }
 
 static gint64
 get_pid_status_item (int pid, const char *item, MonoProcessError *error)
 {
-       char buf [256];
+       char buf [64];
        char *s;
-       FILE *f;
-       int len = strlen (item);
 
-       g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
-       f = fopen (buf, "r");
-       if (!f)
-               RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
-       while ((s = fgets (buf, sizeof (buf), f))) {
-               if (*item != *buf)
-                       continue;
-               if (strncmp (buf, item, len))
-                       continue;
-               if (buf [len] != ':')
-                       continue;
-               fclose (f);
-               if (error)
-                       *error = MONO_PROCESS_ERROR_NONE;
-               return atoi (buf + len + 1);
-       }
-       fclose (f);
-       RET_ERROR (MONO_PROCESS_ERROR_OTHER);
+       s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
+       if (s)
+               return atoi (s);
+       return 0;
 }
 
 /**
@@ -270,11 +364,11 @@ mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProces
        case MONO_PROCESS_NUM_THREADS:
                return get_pid_status_item (rpid, "Threads", error);
        case MONO_PROCESS_USER_TIME:
-               return get_process_stat_time (rpid, 12, FALSE, error);
+               return get_process_stat_time (rpid, 10, FALSE, error);
        case MONO_PROCESS_SYSTEM_TIME:
-               return get_process_stat_time (rpid, 13, FALSE, error);
+               return get_process_stat_time (rpid, 11, FALSE, error);
        case MONO_PROCESS_TOTAL_TIME:
-               return get_process_stat_time (rpid, 12, TRUE, error);
+               return get_process_stat_time (rpid, 10, TRUE, error);
        case MONO_PROCESS_WORKING_SET:
                return get_pid_status_item (rpid, "VmRSS", error) * 1024;
        case MONO_PROCESS_WORKING_SET_PEAK:
@@ -293,6 +387,14 @@ mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProces
                return val;
        case MONO_PROCESS_FAULTS:
                return get_process_stat_item (rpid, 6, TRUE, error);
+       case MONO_PROCESS_ELAPSED:
+               return get_process_stat_item (rpid, 18, FALSE, error) / get_user_hz ();
+       case MONO_PROCESS_PPID:
+               return get_process_stat_time (rpid, 0, FALSE, error);
+
+               /* Nothing yet */
+       case MONO_PROCESS_END:
+               return 0;
        }
        return 0;
 }
@@ -304,3 +406,115 @@ mono_process_get_data (gpointer pid, MonoProcessData data)
        return mono_process_get_data_with_error (pid, data, &error);
 }
 
+/**
+ * mono_cpu_count:
+ *
+ * Return the number of processors on the system.
+ */
+int
+mono_cpu_count (void)
+{
+       int count;
+#ifdef _SC_NPROCESSORS_ONLN
+       count = sysconf (_SC_NPROCESSORS_ONLN);
+       if (count > 0)
+               return count;
+#endif
+#ifdef USE_SYSCTL
+       {
+               int mib [2];
+               size_t len = sizeof (int);
+               mib [0] = CTL_HW;
+               mib [1] = HW_NCPU;
+               if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
+                       return count;
+       }
+#endif
+#ifdef HOST_WIN32
+       {
+               SYSTEM_INFO info;
+               GetSystemInfo (&info);
+               return info.dwNumberOfProcessors;
+       }
+#endif
+       /* FIXME: warn */
+       return 1;
+}
+
+static void
+get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
+{
+       char buf [256];
+       char *s;
+       int hz = get_user_hz ();
+       long long unsigned int user_ticks, nice_ticks, system_ticks, idle_ticks, iowait_ticks, irq_ticks, sirq_ticks;
+       FILE *f = fopen ("/proc/stat", "r");
+       if (!f)
+               return;
+       if (cpu_id < 0)
+               hz *= mono_cpu_count ();
+       while ((s = fgets (buf, sizeof (buf), f))) {
+               char *data = NULL;
+               if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
+                       data = s + 4;
+               } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
+                       if (data == s + 3)
+                               continue;
+                       data++;
+               } else {
+                       continue;
+               }
+               sscanf (data, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu", &user_ticks, &nice_ticks, &system_ticks, &idle_ticks, &iowait_ticks, &irq_ticks, &sirq_ticks);
+       }
+       fclose (f);
+
+       if (user)
+               *user = (user_ticks + nice_ticks) * 10000000 / hz;
+       if (systemt)
+               *systemt = (system_ticks) * 10000000 / hz;
+       if (irq)
+               *irq = (irq_ticks) * 10000000 / hz;
+       if (sirq)
+               *sirq = (sirq_ticks) * 10000000 / hz;
+       if (idle)
+               *idle = (idle_ticks) * 10000000 / hz;
+}
+
+/**
+ * mono_cpu_get_data:
+ * @cpu_id: processor number or -1 to get a summary of all the processors
+ * @data: type of data to retrieve
+ *
+ * Get data about a processor on the system, like time spent in user space or idle time.
+ */
+gint64
+mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
+{
+       gint64 value = 0;
+
+       if (error)
+               *error = MONO_PROCESS_ERROR_NONE;
+       switch (data) {
+       case MONO_CPU_USER_TIME:
+               get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
+               break;
+       case MONO_CPU_PRIV_TIME:
+               get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
+               break;
+       case MONO_CPU_INTR_TIME:
+               get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
+               break;
+       case MONO_CPU_DCP_TIME:
+               get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
+               break;
+       case MONO_CPU_IDLE_TIME:
+               get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);
+               break;
+
+       case MONO_CPU_END:
+               /* Nothing yet */
+               return 0;
+       }
+       return value;
+}
+