2 #include "utils/mono-proclib.h"
15 /* FIXME: bsds untested */
16 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
17 #include <sys/param.h>
18 #include <sys/types.h>
19 #include <sys/sysctl.h>
21 #include <mach/mach.h>
22 #ifdef HAVE_SYS_USER_H
25 #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
27 # define kinfo_pid_member p_pid
28 # define kinfo_name_member p_comm
30 # define kinfo_pid_member kp_proc.p_pid
31 # define kinfo_name_member kp_proc.p_comm
34 #define kinfo_pid_member ki_pid
35 #define kinfo_name_member ki_comm
42 * @size: a pointer to a location where the size of the returned array is stored
44 * Return an array of pid values for the processes currently running on the system.
45 * The size of the array is stored in @size.
48 mono_process_list (int *size)
54 size_t data_len = sizeof (struct kinfo_proc2) * 400;
55 struct kinfo_proc2 *processes = malloc (data_len);
58 size_t data_len = sizeof (struct kinfo_proc) * 400;
59 struct kinfo_proc *processes = malloc (data_len);
60 #endif /* KERN_PROC2 */
71 mib [2] = KERN_PROC_ALL;
73 mib [4] = sizeof(struct kinfo_proc2);
74 mib [5] = 400; /* XXX */
76 res = sysctl (mib, 6, processes, &data_len, NULL, 0);
80 mib [2] = KERN_PROC_ALL;
83 res = sysctl (mib, 4, processes, &data_len, NULL, 0);
84 #endif /* KERN_PROC2 */
91 res = data_len/sizeof (struct kinfo_proc2);
93 res = data_len/sizeof (struct kinfo_proc);
94 #endif /* KERN_PROC2 */
95 buf = g_realloc (buf, res * sizeof (void*));
96 for (i = 0; i < res; ++i)
97 buf [i] = GINT_TO_POINTER (processes [i].kinfo_pid_member);
107 GDir *dir = g_dir_open ("/proc/", 0, NULL);
113 while ((name = g_dir_read_name (dir))) {
116 pid = strtol (name, &nend, 10);
117 if (pid <= 0 || nend == name || *nend)
124 buf = g_realloc (buf, count * sizeof (void*));
126 buf [i++] = GINT_TO_POINTER (pid);
136 get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
141 int len = strlen (item);
143 g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
144 f = fopen (buf, "r");
147 *error = MONO_PROCESS_ERROR_NOT_FOUND;
150 while ((s = fgets (buf, blen, f))) {
153 if (strncmp (buf, item, len))
156 while (g_ascii_isspace (*s)) s++;
159 while (g_ascii_isspace (*s)) s++;
162 strncpy (rbuf, s, MIN (len, blen));
163 rbuf [MIN (len, blen) - 1] = 0;
165 *error = MONO_PROCESS_ERROR_NONE;
170 *error = MONO_PROCESS_ERROR_OTHER;
175 * mono_process_get_name:
176 * @pid: pid of the process
177 * @buf: byte buffer where to store the name of the prcoess
178 * @len: size of the buffer @buf
180 * Return the name of the process identified by @pid, storing it
181 * inside @buf for a maximum of len bytes (including the terminating 0).
184 mono_process_get_name (gpointer pid, char *buf, int len)
190 size_t data_len = sizeof (struct kinfo_proc2);
191 struct kinfo_proc2 processi;
194 size_t data_len = sizeof (struct kinfo_proc);
195 struct kinfo_proc processi;
196 #endif /* KERN_PROC2 */
198 memset (buf, 0, len);
202 mib [1] = KERN_PROC2;
203 mib [2] = KERN_PROC_PID;
204 mib [3] = GPOINTER_TO_UINT (pid);
205 mib [4] = sizeof(struct kinfo_proc2);
206 mib [5] = 400; /* XXX */
208 res = sysctl (mib, 6, &processi, &data_len, NULL, 0);
210 if (res < 0 || data_len != sizeof (struct kinfo_proc2)) {
216 mib [2] = KERN_PROC_PID;
217 mib [3] = GPOINTER_TO_UINT (pid);
219 res = sysctl (mib, 4, &processi, &data_len, NULL, 0);
220 if (res < 0 || data_len != sizeof (struct kinfo_proc)) {
223 #endif /* KERN_PROC2 */
224 strncpy (buf, processi.kinfo_name_member, len - 1);
231 sprintf (fname, "/proc/%d/cmdline", GPOINTER_TO_INT (pid));
233 file = fopen (fname, "r");
236 r = fread (buf, 1, len - 1, file);
239 p = strrchr (buf, '/');
243 return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
250 * /proc/pid/stat format:
252 * [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
253 * [10] utime stime cutime cstime prio nice threads 0 start_time vsize rss
254 * [20] rss rsslim start_code end_code start_stack esp eip pending blocked sigign
255 * [30] sigcatch wchan 0 0 exit_signal cpu rt_prio policy
258 #define RET_ERROR(err) do { \
259 if (error) *error = (err); \
264 get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
266 #if defined(__APPLE__)
267 double process_user_time = 0, process_system_time = 0;//, process_percent = 0;
270 if (task_for_pid(mach_task_self(), pid, &task) != KERN_SUCCESS)
271 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
273 struct task_basic_info t_info;
274 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
277 (task_info_t)&t_info,
278 &t_info_count) != KERN_SUCCESS)
279 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
281 thread_array_t th_array;
282 if (task_threads(task, &th_array, &th_count) != KERN_SUCCESS)
283 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
287 for (i = 0; i < th_count; i++) {
288 double thread_user_time, thread_system_time;//, thread_percent;
290 struct thread_basic_info th_info;
291 mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT;
292 if (thread_info(th_array[i], THREAD_BASIC_INFO, (thread_info_t)&th_info, &th_info_count) == KERN_SUCCESS) {
293 thread_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6;
294 thread_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6;
295 //thread_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE;
297 process_user_time += thread_user_time;
298 process_system_time += thread_system_time;
299 //process_percent += th_percent;
303 for (i = 0; i < th_count; i++)
304 mach_port_deallocate(task, th_array[i]);
306 process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
307 process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
309 if (pos == 10 && sum == TRUE)
310 return (gint64)((process_user_time + process_system_time) * 10000000);
312 return (gint64)(process_user_time * 10000000);
314 return (gint64)(process_system_time * 10000000);
324 g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
325 f = fopen (buf, "r");
327 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
328 len = fread (buf, 1, sizeof (buf), f);
331 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
332 s = strchr (buf, ')');
334 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
336 while (g_ascii_isspace (*s)) s++;
338 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
339 /* skip the status char */
340 while (*s && !g_ascii_isspace (*s)) s++;
342 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
343 for (i = 0; i < pos; ++i) {
344 while (g_ascii_isspace (*s)) s++;
346 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
347 while (*s && !g_ascii_isspace (*s)) s++;
349 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
351 /* we are finally at the needed item */
352 value = strtoul (s, &end, 0);
353 /* add also the following value */
355 while (g_ascii_isspace (*s)) s++;
357 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
358 value += strtoul (s, &end, 0);
361 *error = MONO_PROCESS_ERROR_NONE;
369 static int user_hz = 0;
372 user_hz = sysconf (_SC_CLK_TCK);
381 get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
383 gint64 val = get_process_stat_item (pid, pos, sum, error);
384 #if defined(__APPLE__)
387 /* return 100ns ticks */
388 return (val * 10000000) / get_user_hz ();
393 get_pid_status_item (int pid, const char *item, MonoProcessError *error, int multiplier)
395 #if defined(__APPLE__)
396 // ignore the multiplier
399 if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
400 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
402 struct task_basic_info t_info;
403 mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
405 if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count) != KERN_SUCCESS)
406 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
408 if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0)
409 return t_info.resident_size;
410 else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
411 return t_info.virtual_size;
412 else if (strcmp (item, "Threads") == 0)
419 s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
421 return atoi (s) * multiplier;
427 * mono_process_get_data:
428 * @pid: pid of the process
429 * @data: description of data to return
431 * Return a data item of a process like user time, memory use etc,
432 * according to the @data argumet.
435 mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProcessError *error)
438 int rpid = GPOINTER_TO_INT (pid);
441 *error = MONO_PROCESS_ERROR_OTHER;
444 case MONO_PROCESS_NUM_THREADS:
445 return get_pid_status_item (rpid, "Threads", error, 1);
446 case MONO_PROCESS_USER_TIME:
447 return get_process_stat_time (rpid, 10, FALSE, error);
448 case MONO_PROCESS_SYSTEM_TIME:
449 return get_process_stat_time (rpid, 11, FALSE, error);
450 case MONO_PROCESS_TOTAL_TIME:
451 return get_process_stat_time (rpid, 10, TRUE, error);
452 case MONO_PROCESS_WORKING_SET:
453 return get_pid_status_item (rpid, "VmRSS", error, 1024);
454 case MONO_PROCESS_WORKING_SET_PEAK:
455 val = get_pid_status_item (rpid, "VmHWM", error, 1024);
457 val = get_pid_status_item (rpid, "VmRSS", error, 1024);
459 case MONO_PROCESS_PRIVATE_BYTES:
460 return get_pid_status_item (rpid, "VmData", error, 1024);
461 case MONO_PROCESS_VIRTUAL_BYTES:
462 return get_pid_status_item (rpid, "VmSize", error, 1024);
463 case MONO_PROCESS_VIRTUAL_BYTES_PEAK:
464 val = get_pid_status_item (rpid, "VmPeak", error, 1024);
466 val = get_pid_status_item (rpid, "VmSize", error, 1024);
468 case MONO_PROCESS_FAULTS:
469 return get_process_stat_item (rpid, 6, TRUE, error);
470 case MONO_PROCESS_ELAPSED:
471 return get_process_stat_item (rpid, 18, FALSE, error) / get_user_hz ();
472 case MONO_PROCESS_PPID:
473 return get_process_stat_time (rpid, 0, FALSE, error);
476 case MONO_PROCESS_END:
483 mono_process_get_data (gpointer pid, MonoProcessData data)
485 MonoProcessError error;
486 return mono_process_get_data_with_error (pid, data, &error);
492 * Return the number of processors on the system.
495 mono_cpu_count (void)
498 #ifdef _SC_NPROCESSORS_ONLN
499 count = sysconf (_SC_NPROCESSORS_ONLN);
506 size_t len = sizeof (int);
509 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
516 GetSystemInfo (&info);
517 return info.dwNumberOfProcessors;
525 get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
529 int hz = get_user_hz ();
530 long long unsigned int user_ticks, nice_ticks, system_ticks, idle_ticks, iowait_ticks, irq_ticks, sirq_ticks;
531 FILE *f = fopen ("/proc/stat", "r");
535 hz *= mono_cpu_count ();
536 while ((s = fgets (buf, sizeof (buf), f))) {
538 if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
540 } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
547 sscanf (data, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu", &user_ticks, &nice_ticks, &system_ticks, &idle_ticks, &iowait_ticks, &irq_ticks, &sirq_ticks);
552 *user = (user_ticks + nice_ticks) * 10000000 / hz;
554 *systemt = (system_ticks) * 10000000 / hz;
556 *irq = (irq_ticks) * 10000000 / hz;
558 *sirq = (sirq_ticks) * 10000000 / hz;
560 *idle = (idle_ticks) * 10000000 / hz;
565 * @cpu_id: processor number or -1 to get a summary of all the processors
566 * @data: type of data to retrieve
568 * Get data about a processor on the system, like time spent in user space or idle time.
571 mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
576 *error = MONO_PROCESS_ERROR_NONE;
578 case MONO_CPU_USER_TIME:
579 get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
581 case MONO_CPU_PRIV_TIME:
582 get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
584 case MONO_CPU_INTR_TIME:
585 get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
587 case MONO_CPU_DCP_TIME:
588 get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
590 case MONO_CPU_IDLE_TIME:
591 get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);