Merge pull request #7599 from lambdageek/abort-cctor-no-protection
[mono-project.git] / mono / utils / mono-proclib.c
blob191ee1dc1f049d370b8b26c876ad1c45ad436120
1 /**
2 * \file
3 * Copyright 2008-2011 Novell Inc
4 * Copyright 2011 Xamarin Inc
5 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 */
8 #include "config.h"
9 #include "utils/mono-proclib.h"
10 #include "utils/mono-time.h"
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #ifdef HAVE_SCHED_GETAFFINITY
20 #include <sched.h>
21 #endif
23 #if defined(_POSIX_VERSION)
24 #ifdef HAVE_SYS_ERRNO_H
25 #include <sys/errno.h>
26 #endif
27 #include <sys/param.h>
28 #include <errno.h>
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_SYSCTL_H
33 #include <sys/sysctl.h>
34 #endif
35 #ifdef HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
37 #endif
38 #endif
39 #if defined(__HAIKU__)
40 #include <os/kernel/OS.h>
41 #endif
42 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
43 #include <sys/proc.h>
44 #if defined(__APPLE__)
45 #include <mach/mach.h>
46 #endif
47 #ifdef HAVE_SYS_USER_H
48 #include <sys/user.h>
49 #endif
50 #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
51 # define kinfo_starttime_member kp_proc.p_starttime
52 # define kinfo_pid_member kp_proc.p_pid
53 # define kinfo_name_member kp_proc.p_comm
54 #elif defined(__NetBSD__)
55 # define kinfo_starttime_member p_ustart_sec
56 # define kinfo_pid_member p_pid
57 # define kinfo_name_member p_comm
58 #elif defined(__OpenBSD__)
59 // Can not figure out how to get the proc's start time on OpenBSD
60 # undef kinfo_starttime_member
61 # define kinfo_pid_member p_pid
62 # define kinfo_name_member p_comm
63 #else
64 #define kinfo_starttime_member ki_start
65 #define kinfo_pid_member ki_pid
66 #define kinfo_name_member ki_comm
67 #endif
68 #define USE_SYSCTL 1
69 #endif
71 #ifdef HAVE_SCHED_GETAFFINITY
72 # ifndef GLIBC_HAS_CPU_COUNT
73 static int
74 CPU_COUNT(cpu_set_t *set)
76 int i, count = 0;
78 for (int i = 0; i < CPU_SETSIZE; i++)
79 if (CPU_ISSET(i, set))
80 count++;
81 return count;
83 # endif
84 #endif
86 /**
87 * mono_process_list:
88 * \param size a pointer to a location where the size of the returned array is stored
89 * \returns an array of pid values for the processes currently running on the system.
90 * The size of the array is stored in \p size.
92 gpointer*
93 mono_process_list (int *size)
95 #if USE_SYSCTL
96 int res, i;
97 #ifdef KERN_PROC2
98 int mib [6];
99 size_t data_len = sizeof (struct kinfo_proc2) * 400;
100 struct kinfo_proc2 *processes = g_malloc (data_len);
101 #else
102 int mib [4];
103 size_t data_len = sizeof (struct kinfo_proc) * 16;
104 struct kinfo_proc *processes;
105 int limit = 8;
106 #endif /* KERN_PROC2 */
107 void **buf = NULL;
109 if (size)
110 *size = 0;
112 #ifdef KERN_PROC2
113 if (!processes)
114 return NULL;
116 mib [0] = CTL_KERN;
117 mib [1] = KERN_PROC2;
118 mib [2] = KERN_PROC_ALL;
119 mib [3] = 0;
120 mib [4] = sizeof(struct kinfo_proc2);
121 mib [5] = 400; /* XXX */
123 res = sysctl (mib, 6, processes, &data_len, NULL, 0);
124 if (res < 0) {
125 g_free (processes);
126 return NULL;
128 #else
129 processes = NULL;
130 while (limit) {
131 mib [0] = CTL_KERN;
132 mib [1] = KERN_PROC;
133 mib [2] = KERN_PROC_ALL;
134 mib [3] = 0;
136 res = sysctl (mib, 3, NULL, &data_len, NULL, 0);
137 if (res)
138 return NULL;
139 processes = (struct kinfo_proc *) g_malloc (data_len);
140 res = sysctl (mib, 3, processes, &data_len, NULL, 0);
141 if (res < 0) {
142 g_free (processes);
143 if (errno != ENOMEM)
144 return NULL;
145 limit --;
146 } else {
147 break;
150 #endif /* KERN_PROC2 */
152 #ifdef KERN_PROC2
153 res = data_len/sizeof (struct kinfo_proc2);
154 #else
155 res = data_len/sizeof (struct kinfo_proc);
156 #endif /* KERN_PROC2 */
157 buf = (void **) g_realloc (buf, res * sizeof (void*));
158 for (i = 0; i < res; ++i)
159 buf [i] = GINT_TO_POINTER (processes [i].kinfo_pid_member);
160 g_free (processes);
161 if (size)
162 *size = res;
163 return buf;
164 #elif defined(__HAIKU__)
165 int32 cookie = 0;
166 int32 i = 0;
167 team_info ti;
168 system_info si;
170 get_system_info(&si);
171 void **buf = g_calloc(si.used_teams, sizeof(void*));
173 while (get_next_team_info(&cookie, &ti) == B_OK && i < si.used_teams) {
174 buf[i++] = GINT_TO_POINTER (ti.team);
176 *size = i;
178 return buf;
179 #else
180 const char *name;
181 void **buf = NULL;
182 int count = 0;
183 int i = 0;
184 GDir *dir = g_dir_open ("/proc/", 0, NULL);
185 if (!dir) {
186 if (size)
187 *size = 0;
188 return NULL;
190 while ((name = g_dir_read_name (dir))) {
191 int pid;
192 char *nend;
193 pid = strtol (name, &nend, 10);
194 if (pid <= 0 || nend == name || *nend)
195 continue;
196 if (i >= count) {
197 if (!count)
198 count = 16;
199 else
200 count *= 2;
201 buf = (void **)g_realloc (buf, count * sizeof (void*));
203 buf [i++] = GINT_TO_POINTER (pid);
205 g_dir_close (dir);
206 if (size)
207 *size = i;
208 return buf;
209 #endif
212 static G_GNUC_UNUSED char*
213 get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
215 char buf [256];
216 char *s;
217 FILE *f;
218 size_t len = strlen (item);
220 g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
221 f = fopen (buf, "r");
222 if (!f) {
223 if (error)
224 *error = MONO_PROCESS_ERROR_NOT_FOUND;
225 return NULL;
227 while ((s = fgets (buf, sizeof (buf), f))) {
228 if (*item != *buf)
229 continue;
230 if (strncmp (buf, item, len))
231 continue;
232 s = buf + len;
233 while (g_ascii_isspace (*s)) s++;
234 if (*s++ != ':')
235 continue;
236 while (g_ascii_isspace (*s)) s++;
237 fclose (f);
238 len = strlen (s);
239 memcpy (rbuf, s, MIN (len, blen));
240 rbuf [MIN (len, blen) - 1] = 0;
241 if (error)
242 *error = MONO_PROCESS_ERROR_NONE;
243 return rbuf;
245 fclose (f);
246 if (error)
247 *error = MONO_PROCESS_ERROR_OTHER;
248 return NULL;
251 #if USE_SYSCTL
253 #ifdef KERN_PROC2
254 #define KINFO_PROC struct kinfo_proc2
255 #else
256 #define KINFO_PROC struct kinfo_proc
257 #endif
259 static gboolean
260 sysctl_kinfo_proc (gpointer pid, KINFO_PROC* processi)
262 int res;
263 size_t data_len = sizeof (KINFO_PROC);
265 #ifdef KERN_PROC2
266 int mib [6];
267 mib [0] = CTL_KERN;
268 mib [1] = KERN_PROC2;
269 mib [2] = KERN_PROC_PID;
270 mib [3] = GPOINTER_TO_UINT (pid);
271 mib [4] = sizeof(KINFO_PROC);
272 mib [5] = 400; /* XXX */
274 res = sysctl (mib, 6, processi, &data_len, NULL, 0);
275 #else
276 int mib [4];
277 mib [0] = CTL_KERN;
278 mib [1] = KERN_PROC;
279 mib [2] = KERN_PROC_PID;
280 mib [3] = GPOINTER_TO_UINT (pid);
282 res = sysctl (mib, 4, processi, &data_len, NULL, 0);
283 #endif /* KERN_PROC2 */
285 if (res < 0 || data_len != sizeof (KINFO_PROC))
286 return FALSE;
288 return TRUE;
290 #endif /* USE_SYSCTL */
293 * mono_process_get_name:
294 * \param pid pid of the process
295 * \param buf byte buffer where to store the name of the prcoess
296 * \param len size of the buffer \p buf
297 * \returns the name of the process identified by \p pid, storing it
298 * inside \p buf for a maximum of len bytes (including the terminating 0).
300 char*
301 mono_process_get_name (gpointer pid, char *buf, int len)
303 #if USE_SYSCTL
304 KINFO_PROC processi;
306 memset (buf, 0, len);
308 if (sysctl_kinfo_proc (pid, &processi))
309 memcpy (buf, processi.kinfo_name_member, len - 1);
311 return buf;
312 #else
313 char fname [128];
314 FILE *file;
315 char *p;
316 size_t r;
317 sprintf (fname, "/proc/%d/cmdline", GPOINTER_TO_INT (pid));
318 buf [0] = 0;
319 file = fopen (fname, "r");
320 if (!file)
321 return buf;
322 r = fread (buf, 1, len - 1, file);
323 fclose (file);
324 buf [r] = 0;
325 p = strrchr (buf, '/');
326 if (p)
327 return p + 1;
328 if (r == 0) {
329 return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
331 return buf;
332 #endif
335 void
336 mono_process_get_times (gpointer pid, gint64 *start_time, gint64 *user_time, gint64 *kernel_time)
338 if (user_time)
339 *user_time = mono_process_get_data (pid, MONO_PROCESS_USER_TIME);
341 if (kernel_time)
342 *kernel_time = mono_process_get_data (pid, MONO_PROCESS_SYSTEM_TIME);
344 if (start_time) {
345 *start_time = 0;
347 #if USE_SYSCTL && defined(kinfo_starttime_member)
349 KINFO_PROC processi;
351 if (sysctl_kinfo_proc (pid, &processi)) {
352 #if defined(__NetBSD__)
353 struct timeval tv;
354 tv.tv_sec = processi.kinfo_starttime_member;
355 tv.tv_usec = processi.p_ustart_usec;
356 *start_time = mono_100ns_datetime_from_timeval(tv);
357 #else
358 *start_time = mono_100ns_datetime_from_timeval (processi.kinfo_starttime_member);
359 #endif
362 #endif
364 if (*start_time == 0) {
365 static guint64 boot_time = 0;
366 if (!boot_time)
367 boot_time = mono_100ns_datetime () - mono_msec_boottime () * 10000;
369 *start_time = boot_time + mono_process_get_data (pid, MONO_PROCESS_ELAPSED);
375 * /proc/pid/stat format:
376 * pid (cmdname) S
377 * [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
378 * [10] utime stime cutime cstime prio nice threads 0 start_time vsize
379 * [20] rss rsslim start_code end_code start_stack esp eip pending blocked sigign
380 * [30] sigcatch wchan 0 0 exit_signal cpu rt_prio policy
383 #define RET_ERROR(err) do { \
384 if (error) *error = (err); \
385 return 0; \
386 } while (0)
388 static gint64
389 get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
391 #if defined(__APPLE__)
392 double process_user_time = 0, process_system_time = 0;//, process_percent = 0;
393 task_t task;
394 struct task_basic_info t_info;
395 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
396 thread_array_t th_array;
397 size_t i;
398 kern_return_t ret;
400 if (pid == getpid ()) {
401 /* task_for_pid () doesn't work on ios, even for the current process */
402 task = mach_task_self ();
403 } else {
404 do {
405 ret = task_for_pid (mach_task_self (), pid, &task);
406 } while (ret == KERN_ABORTED);
408 if (ret != KERN_SUCCESS)
409 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
412 do {
413 ret = task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
414 } while (ret == KERN_ABORTED);
416 if (ret != KERN_SUCCESS) {
417 if (pid != getpid ())
418 mach_port_deallocate (mach_task_self (), task);
419 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
422 do {
423 ret = task_threads (task, &th_array, &th_count);
424 } while (ret == KERN_ABORTED);
426 if (ret != KERN_SUCCESS) {
427 if (pid != getpid ())
428 mach_port_deallocate (mach_task_self (), task);
429 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
432 for (i = 0; i < th_count; i++) {
433 double thread_user_time, thread_system_time;//, thread_percent;
435 struct thread_basic_info th_info;
436 mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT;
437 do {
438 ret = thread_info(th_array[i], THREAD_BASIC_INFO, (thread_info_t)&th_info, &th_info_count);
439 } while (ret == KERN_ABORTED);
441 if (ret == KERN_SUCCESS) {
442 thread_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6;
443 thread_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6;
444 //thread_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE;
446 process_user_time += thread_user_time;
447 process_system_time += thread_system_time;
448 //process_percent += th_percent;
452 for (i = 0; i < th_count; i++)
453 mach_port_deallocate(task, th_array[i]);
455 if (pid != getpid ())
456 mach_port_deallocate (mach_task_self (), task);
458 process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
459 process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
461 if (pos == 10 && sum == TRUE)
462 return (gint64)((process_user_time + process_system_time) * 10000000);
463 else if (pos == 10)
464 return (gint64)(process_user_time * 10000000);
465 else if (pos == 11)
466 return (gint64)(process_system_time * 10000000);
468 return 0;
469 #else
470 char buf [512];
471 char *s, *end;
472 FILE *f;
473 size_t len;
474 int i;
475 gint64 value;
477 g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
478 f = fopen (buf, "r");
479 if (!f)
480 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
481 len = fread (buf, 1, sizeof (buf), f);
482 fclose (f);
483 if (len <= 0)
484 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
485 s = strchr (buf, ')');
486 if (!s)
487 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
488 s++;
489 while (g_ascii_isspace (*s)) s++;
490 if (!*s)
491 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
492 /* skip the status char */
493 while (*s && !g_ascii_isspace (*s)) s++;
494 if (!*s)
495 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
496 for (i = 0; i < pos; ++i) {
497 while (g_ascii_isspace (*s)) s++;
498 if (!*s)
499 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
500 while (*s && !g_ascii_isspace (*s)) s++;
501 if (!*s)
502 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
504 /* we are finally at the needed item */
505 value = strtoul (s, &end, 0);
506 /* add also the following value */
507 if (sum) {
508 while (g_ascii_isspace (*s)) s++;
509 if (!*s)
510 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
511 value += strtoul (s, &end, 0);
513 if (error)
514 *error = MONO_PROCESS_ERROR_NONE;
515 return value;
516 #endif
519 static int
520 get_user_hz (void)
522 static int user_hz = 0;
523 if (user_hz == 0) {
524 #if defined (_SC_CLK_TCK) && defined (HAVE_SYSCONF)
525 user_hz = sysconf (_SC_CLK_TCK);
526 #endif
527 if (user_hz == 0)
528 user_hz = 100;
530 return user_hz;
533 static gint64
534 get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
536 gint64 val = get_process_stat_item (pid, pos, sum, error);
537 #if defined(__APPLE__)
538 return val;
539 #else
540 /* return 100ns ticks */
541 return (val * 10000000) / get_user_hz ();
542 #endif
545 static gint64
546 get_pid_status_item (int pid, const char *item, MonoProcessError *error, int multiplier)
548 #if defined(__APPLE__)
549 // ignore the multiplier
551 gint64 ret;
552 task_t task;
553 task_vm_info_data_t t_info;
554 mach_msg_type_number_t info_count = TASK_VM_INFO_COUNT;
555 kern_return_t mach_ret;
557 if (pid == getpid ()) {
558 /* task_for_pid () doesn't work on ios, even for the current process */
559 task = mach_task_self ();
560 } else {
561 do {
562 mach_ret = task_for_pid (mach_task_self (), pid, &task);
563 } while (mach_ret == KERN_ABORTED);
565 if (mach_ret != KERN_SUCCESS)
566 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
569 do {
570 mach_ret = task_info (task, TASK_VM_INFO, (task_info_t)&t_info, &info_count);
571 } while (mach_ret == KERN_ABORTED);
573 if (mach_ret != KERN_SUCCESS) {
574 if (pid != getpid ())
575 mach_port_deallocate (mach_task_self (), task);
576 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
579 if(strcmp (item, "VmData") == 0)
580 ret = t_info.internal + t_info.compressed;
581 else if (strcmp (item, "VmRSS") == 0)
582 ret = t_info.resident_size;
583 else if(strcmp (item, "VmHWM") == 0)
584 ret = t_info.resident_size_peak;
585 else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
586 ret = t_info.virtual_size;
587 else if (strcmp (item, "Threads") == 0) {
588 struct task_basic_info t_info;
589 mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
590 do {
591 mach_ret = task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count);
592 } while (mach_ret == KERN_ABORTED);
594 if (mach_ret != KERN_SUCCESS) {
595 if (pid != getpid ())
596 mach_port_deallocate (mach_task_self (), task);
597 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
599 ret = th_count;
600 } else if (strcmp (item, "VmSwap") == 0)
601 ret = t_info.compressed;
602 else
603 ret = 0;
605 if (pid != getpid ())
606 mach_port_deallocate (mach_task_self (), task);
608 return ret;
609 #else
610 char buf [64];
611 char *s;
613 s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
614 if (s)
615 return ((gint64) atol (s)) * multiplier;
616 return 0;
617 #endif
621 * mono_process_get_data:
622 * \param pid pid of the process
623 * \param data description of data to return
624 * \returns a data item of a process like user time, memory use etc,
625 * according to the \p data argumet.
627 gint64
628 mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProcessError *error)
630 gint64 val;
631 int rpid = GPOINTER_TO_INT (pid);
633 if (error)
634 *error = MONO_PROCESS_ERROR_OTHER;
636 switch (data) {
637 case MONO_PROCESS_NUM_THREADS:
638 return get_pid_status_item (rpid, "Threads", error, 1);
639 case MONO_PROCESS_USER_TIME:
640 return get_process_stat_time (rpid, 10, FALSE, error);
641 case MONO_PROCESS_SYSTEM_TIME:
642 return get_process_stat_time (rpid, 11, FALSE, error);
643 case MONO_PROCESS_TOTAL_TIME:
644 return get_process_stat_time (rpid, 10, TRUE, error);
645 case MONO_PROCESS_WORKING_SET:
646 return get_pid_status_item (rpid, "VmRSS", error, 1024);
647 case MONO_PROCESS_WORKING_SET_PEAK:
648 val = get_pid_status_item (rpid, "VmHWM", error, 1024);
649 if (val == 0)
650 val = get_pid_status_item (rpid, "VmRSS", error, 1024);
651 return val;
652 case MONO_PROCESS_PRIVATE_BYTES:
653 return get_pid_status_item (rpid, "VmData", error, 1024);
654 case MONO_PROCESS_VIRTUAL_BYTES:
655 return get_pid_status_item (rpid, "VmSize", error, 1024);
656 case MONO_PROCESS_VIRTUAL_BYTES_PEAK:
657 val = get_pid_status_item (rpid, "VmPeak", error, 1024);
658 if (val == 0)
659 val = get_pid_status_item (rpid, "VmSize", error, 1024);
660 return val;
661 case MONO_PROCESS_FAULTS:
662 return get_process_stat_item (rpid, 6, TRUE, error);
663 case MONO_PROCESS_ELAPSED:
664 return get_process_stat_time (rpid, 18, FALSE, error);
665 case MONO_PROCESS_PPID:
666 return get_process_stat_time (rpid, 0, FALSE, error);
667 case MONO_PROCESS_PAGED_BYTES:
668 return get_pid_status_item (rpid, "VmSwap", error, 1024);
670 /* Nothing yet */
671 case MONO_PROCESS_END:
672 return 0;
674 return 0;
677 gint64
678 mono_process_get_data (gpointer pid, MonoProcessData data)
680 MonoProcessError error;
681 return mono_process_get_data_with_error (pid, data, &error);
684 #ifndef HOST_WIN32
686 mono_process_current_pid ()
688 #if defined(HAVE_UNISTD_H)
689 return (int) getpid ();
690 #else
691 #error getpid
692 #endif
694 #endif /* !HOST_WIN32 */
697 * mono_cpu_count:
698 * \returns the number of processors on the system.
700 #ifndef HOST_WIN32
702 mono_cpu_count (void)
704 #ifdef HOST_ANDROID
705 /* Android tries really hard to save power by powering off CPUs on SMP phones which
706 * means the normal way to query cpu count returns a wrong value with userspace API.
707 * Instead we use /sys entries to query the actual hardware CPU count.
709 int count = 0;
710 char buffer[8] = {'\0'};
711 int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
712 /* Format of the /sys entry is a cpulist of indexes which in the case
713 * of present is always of the form "0-(n-1)" when there is more than
714 * 1 core, n being the number of CPU cores in the system. Otherwise
715 * the value is simply 0
717 if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
718 count = strtol (((char*)buffer) + 2, NULL, 10);
719 if (present != -1)
720 close (present);
721 if (count > 0)
722 return count + 1;
723 #endif
725 #if defined(HOST_ARM) || defined (HOST_ARM64)
728 * Recap from Alexander Köplinger <alex.koeplinger@outlook.com>:
730 * When we merged the change from PR #2722, we started seeing random failures on ARM in
731 * the MonoTests.System.Threading.ThreadPoolTests.SetAndGetMaxThreads and
732 * MonoTests.System.Threading.ManualResetEventSlimTests.Constructor_Defaults tests. Both
733 * of those tests are dealing with Environment.ProcessorCount to verify some implementation
734 * details.
736 * It turns out that on the Jetson TK1 board we use on public Jenkins and on ARM kernels
737 * in general, the value returned by sched_getaffinity (or _SC_NPROCESSORS_ONLN) doesn't
738 * contain CPUs/cores that are powered off for power saving reasons. This is contrary to
739 * what happens on x86, where even cores in deep-sleep state are returned [1], [2]. This
740 * means that we would get a processor count of 1 at one point in time and a higher value
741 * when load increases later on as the system wakes CPUs.
743 * Various runtime pieces like the threadpool and also user code however relies on the
744 * value returned by Environment.ProcessorCount e.g. for deciding how many parallel tasks
745 * to start, thereby limiting the performance when that code thinks we only have one CPU.
747 * Talking to a few people, this was the reason why we changed to _SC_NPROCESSORS_CONF in
748 * mono#1688 and why we added a special case for Android in mono@de3addc to get the "real"
749 * number of processors in the system.
751 * Because of those issues Android/Dalvik also switched from _ONLN to _SC_NPROCESSORS_CONF
752 * for the Java API Runtime.availableProcessors() too [3], citing:
753 * > Traditionally this returned the number currently online, but many mobile devices are
754 * able to take unused cores offline to save power, so releases newer than Android 4.2 (Jelly
755 * Bean) return the maximum number of cores that could be made available if there were no
756 * power or heat constraints.
758 * The problem with sticking to _SC_NPROCESSORS_CONF however is that it breaks down in
759 * constrained environments like Docker or with an explicit CPU affinity set by the Linux
760 * `taskset` command, They'd get a higher CPU count than can be used, start more threads etc.
761 * which results in unnecessary context switches and overloaded systems. That's why we need
762 * to respect sched_getaffinity.
764 * So while in an ideal world we would be able to rely on sched_getaffinity/_SC_NPROCESSORS_ONLN
765 * to return the number of theoretically available CPUs regardless of power saving measures
766 * everywhere, we can't do this on ARM.
768 * I think the pragmatic solution is the following:
769 * * use sched_getaffinity (+ fallback to _SC_NPROCESSORS_ONLN in case of error) on x86. This
770 * ensures we're inline with what OpenJDK [4] and CoreCLR [5] do
771 * * use _SC_NPROCESSORS_CONF exclusively on ARM (I think we could eventually even get rid of
772 * the HOST_ANDROID special case)
774 * Helpful links:
776 * [1] https://sourceware.org/ml/libc-alpha/2013-07/msg00383.html
777 * [2] https://lists.01.org/pipermail/powertop/2012-September/000433.html
778 * [3] https://android.googlesource.com/platform/libcore/+/750dc634e56c58d1d04f6a138734ac2b772900b5%5E1..750dc634e56c58d1d04f6a138734ac2b772900b5/
779 * [4] https://bugs.openjdk.java.net/browse/JDK-6515172
780 * [5] https://github.com/dotnet/coreclr/blob/7058273693db2555f127ce16e6b0c5b40fb04867/src/pal/src/misc/sysinfo.cpp#L148
783 #if defined (_SC_NPROCESSORS_CONF) && defined (HAVE_SYSCONF)
785 int count = sysconf (_SC_NPROCESSORS_CONF);
786 if (count > 0)
787 return count;
789 #endif
791 #else
793 #ifdef HAVE_SCHED_GETAFFINITY
795 cpu_set_t set;
796 if (sched_getaffinity (mono_process_current_pid (), sizeof (set), &set) == 0)
797 return CPU_COUNT (&set);
799 #endif
800 #if defined (_SC_NPROCESSORS_ONLN) && defined (HAVE_SYSCONF)
802 int count = sysconf (_SC_NPROCESSORS_ONLN);
803 if (count > 0)
804 return count;
806 #endif
808 #endif /* defined(HOST_ARM) || defined (HOST_ARM64) */
810 #ifdef USE_SYSCTL
812 int count;
813 int mib [2];
814 size_t len = sizeof (int);
815 mib [0] = CTL_HW;
816 mib [1] = HW_NCPU;
817 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
818 return count;
820 #endif
821 /* FIXME: warn */
822 return 1;
824 #endif /* !HOST_WIN32 */
826 static void
827 get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
829 char buf [256];
830 char *s;
831 int uhz = get_user_hz ();
832 guint64 user_ticks = 0, nice_ticks = 0, system_ticks = 0, idle_ticks = 0, irq_ticks = 0, sirq_ticks = 0;
833 FILE *f = fopen ("/proc/stat", "r");
834 if (!f)
835 return;
836 if (cpu_id < 0)
837 uhz *= mono_cpu_count ();
838 while ((s = fgets (buf, sizeof (buf), f))) {
839 char *data = NULL;
840 if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
841 data = s + 4;
842 } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
843 if (data == s + 3)
844 continue;
845 data++;
846 } else {
847 continue;
850 user_ticks = strtoull (data, &data, 10);
851 nice_ticks = strtoull (data, &data, 10);
852 system_ticks = strtoull (data, &data, 10);
853 idle_ticks = strtoull (data, &data, 10);
854 /* iowait_ticks = strtoull (data, &data, 10); */
855 irq_ticks = strtoull (data, &data, 10);
856 sirq_ticks = strtoull (data, &data, 10);
857 break;
859 fclose (f);
861 if (user)
862 *user = (user_ticks + nice_ticks) * 10000000 / uhz;
863 if (systemt)
864 *systemt = (system_ticks) * 10000000 / uhz;
865 if (irq)
866 *irq = (irq_ticks) * 10000000 / uhz;
867 if (sirq)
868 *sirq = (sirq_ticks) * 10000000 / uhz;
869 if (idle)
870 *idle = (idle_ticks) * 10000000 / uhz;
874 * mono_cpu_get_data:
875 * \param cpu_id processor number or -1 to get a summary of all the processors
876 * \param data type of data to retrieve
877 * Get data about a processor on the system, like time spent in user space or idle time.
879 gint64
880 mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
882 gint64 value = 0;
884 if (error)
885 *error = MONO_PROCESS_ERROR_NONE;
886 switch (data) {
887 case MONO_CPU_USER_TIME:
888 get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
889 break;
890 case MONO_CPU_PRIV_TIME:
891 get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
892 break;
893 case MONO_CPU_INTR_TIME:
894 get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
895 break;
896 case MONO_CPU_DCP_TIME:
897 get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
898 break;
899 case MONO_CPU_IDLE_TIME:
900 get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);
901 break;
903 case MONO_CPU_END:
904 /* Nothing yet */
905 return 0;
907 return value;
911 mono_atexit (void (*func)(void))
913 #ifdef HOST_ANDROID
914 /* Some versions of android libc doesn't define atexit () */
915 return 0;
916 #else
917 return atexit (func);
918 #endif
922 * This function returns the cpu usage in percentage,
923 * normalized on the number of cores.
925 * Warning : the percentage returned can be > 100%. This
926 * might happens on systems like Android which, for
927 * battery and performance reasons, shut down cores and
928 * lie about the number of active cores.
930 #ifndef HOST_WIN32
931 gint32
932 mono_cpu_usage (MonoCpuUsageState *prev)
934 gint32 cpu_usage = 0;
935 #ifdef HAVE_GETRUSAGE
936 gint64 cpu_total_time;
937 gint64 cpu_busy_time;
938 struct rusage resource_usage;
939 gint64 current_time;
940 gint64 kernel_time;
941 gint64 user_time;
943 if (getrusage (RUSAGE_SELF, &resource_usage) == -1) {
944 g_error ("getrusage() failed, errno is %d (%s)\n", errno, strerror (errno));
945 return -1;
948 current_time = mono_100ns_ticks ();
949 kernel_time = resource_usage.ru_stime.tv_sec * 1000 * 1000 * 10 + resource_usage.ru_stime.tv_usec * 10;
950 user_time = resource_usage.ru_utime.tv_sec * 1000 * 1000 * 10 + resource_usage.ru_utime.tv_usec * 10;
952 cpu_busy_time = (user_time - (prev ? prev->user_time : 0)) + (kernel_time - (prev ? prev->kernel_time : 0));
953 cpu_total_time = (current_time - (prev ? prev->current_time : 0)) * mono_cpu_count ();
955 if (prev) {
956 prev->kernel_time = kernel_time;
957 prev->user_time = user_time;
958 prev->current_time = current_time;
961 if (cpu_total_time > 0 && cpu_busy_time > 0)
962 cpu_usage = (gint32)(cpu_busy_time * 100 / cpu_total_time);
963 #endif
964 return cpu_usage;
966 #endif /* !HOST_WIN32 */