2 #include "utils/mono-proclib.h"
15 /* FIXME: bsds untested */
16 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
17 #include <sys/types.h>
18 #include <sys/sysctl.h>
25 * @size: a pointer to a location where the size of the returned array is stored
27 * Return an array of pid values for the processes currently running on the system.
28 * The size of the array is stored in @size.
31 mono_process_list (int *size)
36 size_t data_len = sizeof (struct kinfo_proc) * 400;
37 struct kinfo_proc *processes = malloc (data_len);
47 mib [2] = KERN_PROC_ALL;
50 res = sysctl (mib, 4, processes, &data_len, NULL, 0);
55 res = data_len/sizeof (struct kinfo_proc);
56 buf = g_realloc (buf, res * sizeof (void*));
57 for (i = 0; i < res; ++i)
58 buf [i] = GINT_TO_POINTER (processes [i].kp_proc.p_pid);
68 GDir *dir = g_dir_open ("/proc/", 0, NULL);
74 while ((name = g_dir_read_name (dir))) {
77 pid = strtol (name, &nend, 10);
78 if (pid <= 0 || nend == name || *nend)
85 buf = g_realloc (buf, count * sizeof (void*));
87 buf [i++] = GINT_TO_POINTER (pid);
97 get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
102 int len = strlen (item);
104 g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
105 f = fopen (buf, "r");
108 *error = MONO_PROCESS_ERROR_NOT_FOUND;
111 while ((s = fgets (buf, blen, f))) {
114 if (strncmp (buf, item, len))
117 while (g_ascii_isspace (*s)) s++;
120 while (g_ascii_isspace (*s)) s++;
123 strncpy (rbuf, s, MIN (len, blen));
126 *error = MONO_PROCESS_ERROR_NONE;
131 *error = MONO_PROCESS_ERROR_OTHER;
136 * mono_process_get_name:
137 * @pid: pid of the process
138 * @buf: byte buffer where to store the name of the prcoess
139 * @len: size of the buffer @buf
141 * Return the name of the process identified by @pid, storing it
142 * inside @buf for a maximum of len bytes (including the terminating 0).
145 mono_process_get_name (gpointer pid, char *buf, int len)
151 size_t data_len = sizeof (struct kinfo_proc);
152 struct kinfo_proc processi;
154 memset (buf, 0, len);
158 mib [2] = KERN_PROC_PID;
159 mib [3] = GPOINTER_TO_UINT (pid);
161 res = sysctl (mib, 4, &processi, &data_len, NULL, 0);
162 if (res < 0 || data_len != sizeof (struct kinfo_proc)) {
165 strncpy (buf, processi.kp_proc.p_comm, len - 1);
172 sprintf (fname, "/proc/%d/cmdline", GPOINTER_TO_INT (pid));
174 file = fopen (fname, "r");
177 r = fread (buf, 1, len - 1, file);
180 p = strrchr (buf, '/');
184 return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
191 * /proc/pid/stat format:
193 * [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
194 * [10] utime stime cutime cstime prio nice threads start_time vsize rss
195 * [20] rsslim start_code end_code start_stack esp eip pending blocked sigign sigcatch
196 * [30] wchan 0 0 exit_signal cpu rt_prio policy
199 #define RET_ERROR(err) do { \
200 if (error) *error = (err); \
205 get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
213 g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
214 f = fopen (buf, "r");
216 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
217 len = fread (buf, 1, sizeof (buf), f);
220 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
221 s = strchr (buf, ')');
223 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
225 while (g_ascii_isspace (*s)) s++;
227 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
228 /* skip the status char */
229 while (*s && !g_ascii_isspace (*s)) s++;
231 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
232 for (i = 0; i < pos; ++i) {
233 while (g_ascii_isspace (*s)) s++;
235 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
236 while (*s && !g_ascii_isspace (*s)) s++;
238 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
240 /* we are finally at the needed item */
241 value = strtoul (s, &end, 0);
242 /* add also the following value */
244 while (g_ascii_isspace (*s)) s++;
246 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
247 value += strtoul (s, &end, 0);
250 *error = MONO_PROCESS_ERROR_NONE;
257 static int user_hz = 0;
260 user_hz = sysconf (_SC_CLK_TCK);
269 get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
271 gint64 val = get_process_stat_item (pid, pos, sum, error);
272 /* return milliseconds */
273 return (val * 1000) / get_user_hz ();
277 get_pid_status_item (int pid, const char *item, MonoProcessError *error)
282 s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
289 * mono_process_get_data:
290 * @pid: pid of the process
291 * @data: description of data to return
293 * Return a data item of a process like user time, memory use etc,
294 * according to the @data argumet.
297 mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProcessError *error)
300 int rpid = GPOINTER_TO_INT (pid);
303 *error = MONO_PROCESS_ERROR_OTHER;
306 case MONO_PROCESS_NUM_THREADS:
307 return get_pid_status_item (rpid, "Threads", error);
308 case MONO_PROCESS_USER_TIME:
309 return get_process_stat_time (rpid, 12, FALSE, error);
310 case MONO_PROCESS_SYSTEM_TIME:
311 return get_process_stat_time (rpid, 13, FALSE, error);
312 case MONO_PROCESS_TOTAL_TIME:
313 return get_process_stat_time (rpid, 12, TRUE, error);
314 case MONO_PROCESS_WORKING_SET:
315 return get_pid_status_item (rpid, "VmRSS", error) * 1024;
316 case MONO_PROCESS_WORKING_SET_PEAK:
317 val = get_pid_status_item (rpid, "VmHWM", error) * 1024;
319 val = get_pid_status_item (rpid, "VmRSS", error) * 1024;
321 case MONO_PROCESS_PRIVATE_BYTES:
322 return get_pid_status_item (rpid, "VmData", error) * 1024;
323 case MONO_PROCESS_VIRTUAL_BYTES:
324 return get_pid_status_item (rpid, "VmSize", error) * 1024;
325 case MONO_PROCESS_VIRTUAL_BYTES_PEAK:
326 val = get_pid_status_item (rpid, "VmPeak", error) * 1024;
328 val = get_pid_status_item (rpid, "VmSize", error) * 1024;
330 case MONO_PROCESS_FAULTS:
331 return get_process_stat_item (rpid, 6, TRUE, error);
337 mono_process_get_data (gpointer pid, MonoProcessData data)
339 MonoProcessError error;
340 return mono_process_get_data_with_error (pid, data, &error);
346 * Return the number of processors on the system.
349 mono_cpu_count (void)
352 #ifdef _SC_NPROCESSORS_ONLN
353 count = sysconf (_SC_NPROCESSORS_ONLN);
360 size_t len = sizeof (int);
363 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
367 #ifdef PLATFORM_WIN32
370 GetSystemInfo (&info);
371 return info.dwNumberOfProcessors;
379 get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
383 int hz = get_user_hz ();
384 long long unsigned int user_ticks, nice_ticks, system_ticks, idle_ticks, iowait_ticks, irq_ticks, sirq_ticks;
385 FILE *f = fopen ("/proc/stat", "r");
388 hz *= mono_cpu_count ();
389 while ((s = fgets (buf, sizeof (buf), f))) {
391 if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
393 } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
400 sscanf (data, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu", &user_ticks, &nice_ticks, &system_ticks, &idle_ticks, &iowait_ticks, &irq_ticks, &sirq_ticks);
405 *user = (user_ticks + nice_ticks) * 10000000 / hz;
407 *systemt = (system_ticks) * 10000000 / hz;
409 *irq = (irq_ticks) * 10000000 / hz;
411 *sirq = (sirq_ticks) * 10000000 / hz;
413 *idle = (idle_ticks) * 10000000 / hz;
418 * @cpu_id: processor number or -1 to get a summary of all the processors
419 * @data: type of data to retrieve
421 * Get data about a processor on the system, like time spent in user space or idle time.
424 mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
429 *error = MONO_PROCESS_ERROR_NONE;
431 case MONO_CPU_USER_TIME:
432 get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
434 case MONO_CPU_PRIV_TIME:
435 get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
437 case MONO_CPU_INTR_TIME:
438 get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
440 case MONO_CPU_DCP_TIME:
441 get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
443 case MONO_CPU_IDLE_TIME:
444 get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);