[runtime] Added helper function sysctl_kinfo_proc.
[mono.git] / mono / utils / mono-proclib.c
1 /*
2  * Copyright 2008-2011 Novell Inc
3  * Copyright 2011 Xamarin Inc
4  */
5
6 #include "config.h"
7 #include "utils/mono-proclib.h"
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16
17 #ifdef HOST_WIN32
18 #include <windows.h>
19 #include <process.h>
20 #endif
21
22 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
23 #include <sys/errno.h>
24 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/sysctl.h>
27 #include <sys/proc.h>
28 #if defined(__APPLE__)
29 #include <mach/mach.h>
30 #endif
31 #ifdef HAVE_SYS_USER_H
32 #include <sys/user.h>
33 #endif
34 #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
35 #    define kinfo_pid_member kp_proc.p_pid
36 #    define kinfo_name_member kp_proc.p_comm
37 #elif defined(__OpenBSD__)
38 #    define kinfo_pid_member p_pid
39 #    define kinfo_name_member p_comm
40 #else
41 #define kinfo_pid_member ki_pid
42 #define kinfo_name_member ki_comm
43 #endif
44 #define USE_SYSCTL 1
45 #endif
46
47 /**
48  * mono_process_list:
49  * @size: a pointer to a location where the size of the returned array is stored
50  *
51  * Return an array of pid values for the processes currently running on the system.
52  * The size of the array is stored in @size.
53  */
54 gpointer*
55 mono_process_list (int *size)
56 {
57 #if USE_SYSCTL
58         int res, i;
59 #ifdef KERN_PROC2
60         int mib [6];
61         size_t data_len = sizeof (struct kinfo_proc2) * 400;
62         struct kinfo_proc2 *processes = malloc (data_len);
63 #else
64         int mib [4];
65         size_t data_len = sizeof (struct kinfo_proc) * 16;
66         struct kinfo_proc *processes;
67         int limit = 8;
68 #endif /* KERN_PROC2 */
69         void **buf = NULL;
70
71         if (size)
72                 *size = 0;
73
74 #ifdef KERN_PROC2
75         if (!processes)
76                 return NULL;
77
78         mib [0] = CTL_KERN;
79         mib [1] = KERN_PROC2;
80         mib [2] = KERN_PROC_ALL;
81         mib [3] = 0;
82         mib [4] = sizeof(struct kinfo_proc2);
83         mib [5] = 400; /* XXX */
84
85         res = sysctl (mib, 6, processes, &data_len, NULL, 0);
86         if (res < 0) {
87                 free (processes);
88                 return NULL;
89         }
90 #else
91         processes = NULL;
92         while (limit) {
93                 mib [0] = CTL_KERN;
94                 mib [1] = KERN_PROC;
95                 mib [2] = KERN_PROC_ALL;
96                 mib [3] = 0;
97
98                 res = sysctl (mib, 4, NULL, &data_len, NULL, 0);
99                 if (res)
100                         return NULL;
101                 processes = malloc (data_len);
102                 res = sysctl (mib, 4, processes, &data_len, NULL, 0);
103                 if (res < 0) {
104                         free (processes);
105                         if (errno != ENOMEM)
106                                 return NULL;
107                         limit --;
108                 } else {
109                         break;
110                 }
111         }
112 #endif /* KERN_PROC2 */
113
114 #ifdef KERN_PROC2
115         res = data_len/sizeof (struct kinfo_proc2);
116 #else
117         res = data_len/sizeof (struct kinfo_proc);
118 #endif /* KERN_PROC2 */
119         buf = g_realloc (buf, res * sizeof (void*));
120         for (i = 0; i < res; ++i)
121                 buf [i] = GINT_TO_POINTER (processes [i].kinfo_pid_member);
122         free (processes);
123         if (size)
124                 *size = res;
125         return buf;
126 #elif defined(__HAIKU__)
127         /* FIXME: Add back the code from 9185fcc305e43428d0f40f3ee37c8a405d41c9ae */
128         g_assert_not_reached ();
129         return NULL;
130 #else
131         const char *name;
132         void **buf = NULL;
133         int count = 0;
134         int i = 0;
135         GDir *dir = g_dir_open ("/proc/", 0, NULL);
136         if (!dir) {
137                 if (size)
138                         *size = 0;
139                 return NULL;
140         }
141         while ((name = g_dir_read_name (dir))) {
142                 int pid;
143                 char *nend;
144                 pid = strtol (name, &nend, 10);
145                 if (pid <= 0 || nend == name || *nend)
146                         continue;
147                 if (i >= count) {
148                         if (!count)
149                                 count = 16;
150                         else
151                                 count *= 2;
152                         buf = g_realloc (buf, count * sizeof (void*));
153                 }
154                 buf [i++] = GINT_TO_POINTER (pid);
155         }
156         g_dir_close (dir);
157         if (size)
158                 *size = i;
159         return buf;
160 #endif
161 }
162
163 static G_GNUC_UNUSED char*
164 get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoProcessError *error)
165 {
166         char buf [256];
167         char *s;
168         FILE *f;
169         int len = strlen (item);
170
171         g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
172         f = fopen (buf, "r");
173         if (!f) {
174                 if (error)
175                         *error = MONO_PROCESS_ERROR_NOT_FOUND;
176                 return NULL;
177         }
178         while ((s = fgets (buf, blen, f))) {
179                 if (*item != *buf)
180                         continue;
181                 if (strncmp (buf, item, len))
182                         continue;
183                 s = buf + len;
184                 while (g_ascii_isspace (*s)) s++;
185                 if (*s++ != ':')
186                         continue;
187                 while (g_ascii_isspace (*s)) s++;
188                 fclose (f);
189                 len = strlen (s);
190                 strncpy (rbuf, s, MIN (len, blen));
191                 rbuf [MIN (len, blen) - 1] = 0;
192                 if (error)
193                         *error = MONO_PROCESS_ERROR_NONE;
194                 return rbuf;
195         }
196         fclose (f);
197         if (error)
198                 *error = MONO_PROCESS_ERROR_OTHER;
199         return NULL;
200 }
201
202 #if USE_SYSCTL
203
204 #ifdef KERN_PROC2
205 #define KINFO_PROC struct kinfo_proc2
206 #else
207 #define KINFO_PROC struct kinfo_proc
208 #endif
209
210 static gboolean
211 sysctl_kinfo_proc (gpointer pid, KINFO_PROC* processi)
212 {
213         int res;
214         size_t data_len = sizeof (KINFO_PROC);
215
216 #ifdef KERN_PROC2
217         int mib [6];
218         mib [0] = CTL_KERN;
219         mib [1] = KERN_PROC2;
220         mib [2] = KERN_PROC_PID;
221         mib [3] = GPOINTER_TO_UINT (pid);
222         mib [4] = sizeof(KINFO_PROC);
223         mib [5] = 400; /* XXX */
224
225         res = sysctl (mib, 6, processi, &data_len, NULL, 0);
226 #else
227         int mib [4];
228         mib [0] = CTL_KERN;
229         mib [1] = KERN_PROC;
230         mib [2] = KERN_PROC_PID;
231         mib [3] = GPOINTER_TO_UINT (pid);
232
233         res = sysctl (mib, 4, processi, &data_len, NULL, 0);
234 #endif /* KERN_PROC2 */
235
236         if (res < 0 || data_len != sizeof (KINFO_PROC))
237                 return FALSE;
238
239         return TRUE;
240 }
241 #endif /* USE_SYSCTL */
242
243 /**
244  * mono_process_get_name:
245  * @pid: pid of the process
246  * @buf: byte buffer where to store the name of the prcoess
247  * @len: size of the buffer @buf
248  *
249  * Return the name of the process identified by @pid, storing it
250  * inside @buf for a maximum of len bytes (including the terminating 0).
251  */
252 char*
253 mono_process_get_name (gpointer pid, char *buf, int len)
254 {
255 #if USE_SYSCTL
256         KINFO_PROC processi;
257
258         memset (buf, 0, len);
259
260         if (sysctl_kinfo_proc (pid, &processi))
261                 strncpy (buf, processi.kinfo_name_member, len - 1);
262
263         return buf;
264 #else
265         char fname [128];
266         FILE *file;
267         char *p;
268         int r;
269         sprintf (fname, "/proc/%d/cmdline", GPOINTER_TO_INT (pid));
270         buf [0] = 0;
271         file = fopen (fname, "r");
272         if (!file)
273                 return buf;
274         r = fread (buf, 1, len - 1, file);
275         fclose (file);
276         buf [r] = 0;
277         p = strrchr (buf, '/');
278         if (p)
279                 return p + 1;
280         if (r == 0) {
281                 return get_pid_status_item_buf (GPOINTER_TO_INT (pid), "Name", buf, len, NULL);
282         }
283         return buf;
284 #endif
285 }
286
287 /*
288  * /proc/pid/stat format:
289  * pid (cmdname) S 
290  *      [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
291  *      [10] utime stime cutime cstime prio nice threads 0 start_time vsize rss
292  *      [20] rss rsslim start_code end_code start_stack esp eip pending blocked sigign
293  *      [30] sigcatch wchan 0 0 exit_signal cpu rt_prio policy
294  */
295
296 #define RET_ERROR(err) do {     \
297                 if (error) *error = (err);      \
298                 return 0;                       \
299         } while (0)
300
301 static gint64
302 get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
303 {
304 #if defined(__APPLE__) 
305         double process_user_time = 0, process_system_time = 0;//, process_percent = 0;
306         task_t task;
307         struct task_basic_info t_info;
308         mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
309         thread_array_t th_array;
310         size_t i;
311
312         if (task_for_pid(mach_task_self(), pid, &task) != KERN_SUCCESS)
313                 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
314
315         if (task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count) != KERN_SUCCESS) {
316                 mach_port_deallocate (mach_task_self (), task);
317                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
318         }
319         
320         if (task_threads(task, &th_array, &th_count) != KERN_SUCCESS) {
321                 mach_port_deallocate (mach_task_self (), task);
322                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
323         }
324                 
325         for (i = 0; i < th_count; i++) {
326                 double thread_user_time, thread_system_time;//, thread_percent;
327                 
328                 struct thread_basic_info th_info;
329                 mach_msg_type_number_t th_info_count = THREAD_BASIC_INFO_COUNT;
330                 if (thread_info(th_array[i], THREAD_BASIC_INFO, (thread_info_t)&th_info, &th_info_count) == KERN_SUCCESS) {
331                         thread_user_time = th_info.user_time.seconds + th_info.user_time.microseconds / 1e6;
332                         thread_system_time = th_info.system_time.seconds + th_info.system_time.microseconds / 1e6;
333                         //thread_percent = (double)th_info.cpu_usage / TH_USAGE_SCALE;
334                         
335                         process_user_time += thread_user_time;
336                         process_system_time += thread_system_time;
337                         //process_percent += th_percent;
338                 }
339         }
340         
341         for (i = 0; i < th_count; i++)
342                 mach_port_deallocate(task, th_array[i]);
343
344         mach_port_deallocate (mach_task_self (), task);
345
346         process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
347         process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
348     
349         if (pos == 10 && sum == TRUE)
350                 return (gint64)((process_user_time + process_system_time) * 10000000);
351         else if (pos == 10)
352                 return (gint64)(process_user_time * 10000000);
353         else if (pos == 11)
354                 return (gint64)(process_system_time * 10000000);
355                 
356         return 0;
357 #else
358         char buf [512];
359         char *s, *end;
360         FILE *f;
361         int len, i;
362         gint64 value;
363
364         g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
365         f = fopen (buf, "r");
366         if (!f)
367                 RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
368         len = fread (buf, 1, sizeof (buf), f);
369         fclose (f);
370         if (len <= 0)
371                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
372         s = strchr (buf, ')');
373         if (!s)
374                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
375         s++;
376         while (g_ascii_isspace (*s)) s++;
377         if (!*s)
378                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
379         /* skip the status char */
380         while (*s && !g_ascii_isspace (*s)) s++;
381         if (!*s)
382                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
383         for (i = 0; i < pos; ++i) {
384                 while (g_ascii_isspace (*s)) s++;
385                 if (!*s)
386                         RET_ERROR (MONO_PROCESS_ERROR_OTHER);
387                 while (*s && !g_ascii_isspace (*s)) s++;
388                 if (!*s)
389                         RET_ERROR (MONO_PROCESS_ERROR_OTHER);
390         }
391         /* we are finally at the needed item */
392         value = strtoul (s, &end, 0);
393         /* add also the following value */
394         if (sum) {
395                 while (g_ascii_isspace (*s)) s++;
396                 if (!*s)
397                         RET_ERROR (MONO_PROCESS_ERROR_OTHER);
398                 value += strtoul (s, &end, 0);
399         }
400         if (error)
401                 *error = MONO_PROCESS_ERROR_NONE;
402         return value;
403 #endif
404 }
405
406 static int
407 get_user_hz (void)
408 {
409         static int user_hz = 0;
410         if (user_hz == 0) {
411 #ifdef _SC_CLK_TCK
412                 user_hz = sysconf (_SC_CLK_TCK);
413 #endif
414                 if (user_hz == 0)
415                         user_hz = 100;
416         }
417         return user_hz;
418 }
419
420 static gint64
421 get_process_stat_time (int pid, int pos, int sum, MonoProcessError *error)
422 {
423         gint64 val = get_process_stat_item (pid, pos, sum, error);
424 #if defined(__APPLE__)
425         return val;
426 #else
427         /* return 100ns ticks */
428         return (val * 10000000) / get_user_hz ();
429 #endif
430 }
431
432 static gint64
433 get_pid_status_item (int pid, const char *item, MonoProcessError *error, int multiplier)
434 {
435 #if defined(__APPLE__)
436         // ignore the multiplier
437         
438         gint64 ret;
439         task_t task;
440         struct task_basic_info t_info;
441         mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
442
443         if (pid == getpid ()) {
444                 /* task_for_pid () doesn't work on ios, even for the current process */
445                 task = mach_task_self ();
446         } else {
447                 if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
448                         RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
449         }
450         
451         if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count) != KERN_SUCCESS) {
452                 if (pid != getpid ())
453                         mach_port_deallocate (mach_task_self (), task);
454                 RET_ERROR (MONO_PROCESS_ERROR_OTHER);
455         }
456
457         if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0 || strcmp (item, "VmData") == 0)
458                 ret = t_info.resident_size;
459         else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
460                 ret = t_info.virtual_size;
461         else if (strcmp (item, "Threads") == 0)
462                 ret = th_count;
463         else
464                 ret = 0;
465
466         if (pid != getpid ())
467                 mach_port_deallocate (mach_task_self (), task);
468         
469         return ret;
470 #else
471         char buf [64];
472         char *s;
473
474         s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
475         if (s)
476                 return ((gint64) atol (s)) * multiplier;
477         return 0;
478 #endif
479 }
480
481 /**
482  * mono_process_get_data:
483  * @pid: pid of the process
484  * @data: description of data to return
485  *
486  * Return a data item of a process like user time, memory use etc,
487  * according to the @data argumet.
488  */
489 gint64
490 mono_process_get_data_with_error (gpointer pid, MonoProcessData data, MonoProcessError *error)
491 {
492         gint64 val;
493         int rpid = GPOINTER_TO_INT (pid);
494
495         if (error)
496                 *error = MONO_PROCESS_ERROR_OTHER;
497
498         switch (data) {
499         case MONO_PROCESS_NUM_THREADS:
500                 return get_pid_status_item (rpid, "Threads", error, 1);
501         case MONO_PROCESS_USER_TIME:
502                 return get_process_stat_time (rpid, 10, FALSE, error);
503         case MONO_PROCESS_SYSTEM_TIME:
504                 return get_process_stat_time (rpid, 11, FALSE, error);
505         case MONO_PROCESS_TOTAL_TIME:
506                 return get_process_stat_time (rpid, 10, TRUE, error);
507         case MONO_PROCESS_WORKING_SET:
508                 return get_pid_status_item (rpid, "VmRSS", error, 1024);
509         case MONO_PROCESS_WORKING_SET_PEAK:
510                 val = get_pid_status_item (rpid, "VmHWM", error, 1024);
511                 if (val == 0)
512                         val = get_pid_status_item (rpid, "VmRSS", error, 1024);
513                 return val;
514         case MONO_PROCESS_PRIVATE_BYTES:
515                 return get_pid_status_item (rpid, "VmData", error, 1024);
516         case MONO_PROCESS_VIRTUAL_BYTES:
517                 return get_pid_status_item (rpid, "VmSize", error, 1024);
518         case MONO_PROCESS_VIRTUAL_BYTES_PEAK:
519                 val = get_pid_status_item (rpid, "VmPeak", error, 1024);
520                 if (val == 0)
521                         val = get_pid_status_item (rpid, "VmSize", error, 1024);
522                 return val;
523         case MONO_PROCESS_FAULTS:
524                 return get_process_stat_item (rpid, 6, TRUE, error);
525         case MONO_PROCESS_ELAPSED:
526                 return get_process_stat_item (rpid, 18, FALSE, error) / get_user_hz ();
527         case MONO_PROCESS_PPID:
528                 return get_process_stat_time (rpid, 0, FALSE, error);
529         case MONO_PROCESS_PAGED_BYTES:
530                 return get_pid_status_item (rpid, "VmSwap", error, 1024);
531
532                 /* Nothing yet */
533         case MONO_PROCESS_END:
534                 return 0;
535         }
536         return 0;
537 }
538
539 gint64
540 mono_process_get_data (gpointer pid, MonoProcessData data)
541 {
542         MonoProcessError error;
543         return mono_process_get_data_with_error (pid, data, &error);
544 }
545
546 int
547 mono_process_current_pid ()
548 {
549 #if defined(HAVE_UNISTD_H)
550         return (int) getpid ();
551 #elif defined(HOST_WIN32)
552         return (int) GetCurrentProcessId ();
553 #else
554 #error getpid
555 #endif
556 }
557
558 /**
559  * mono_cpu_count:
560  *
561  * Return the number of processors on the system.
562  */
563 int
564 mono_cpu_count (void)
565 {
566         int count = 0;
567 #ifdef PLATFORM_ANDROID
568         /* Android tries really hard to save power by powering off CPUs on SMP phones which
569          * means the normal way to query cpu count returns a wrong value with userspace API.
570          * Instead we use /sys entries to query the actual hardware CPU count.
571          */
572         char buffer[8] = {'\0'};
573         int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
574         /* Format of the /sys entry is a cpulist of indexes which in the case
575          * of present is always of the form "0-(n-1)" when there is more than
576          * 1 core, n being the number of CPU cores in the system. Otherwise
577          * the value is simply 0
578          */
579         if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
580                 count = strtol (((char*)buffer) + 2, NULL, 10);
581         if (present != -1)
582                 close (present);
583         if (count > 0)
584                 return count + 1;
585 #endif
586 #ifdef _SC_NPROCESSORS_ONLN
587         count = sysconf (_SC_NPROCESSORS_ONLN);
588         if (count > 0)
589                 return count;
590 #endif
591 #ifdef USE_SYSCTL
592         {
593                 int mib [2];
594                 size_t len = sizeof (int);
595                 mib [0] = CTL_HW;
596                 mib [1] = HW_NCPU;
597                 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
598                         return count;
599         }
600 #endif
601 #ifdef HOST_WIN32
602         {
603                 SYSTEM_INFO info;
604                 GetSystemInfo (&info);
605                 return info.dwNumberOfProcessors;
606         }
607 #endif
608         /* FIXME: warn */
609         return 1;
610 }
611
612 static void
613 get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *sirq, gint64 *idle)
614 {
615         char buf [256];
616         char *s;
617         int hz = get_user_hz ();
618         guint64 user_ticks = 0, nice_ticks = 0, system_ticks = 0, idle_ticks = 0, irq_ticks = 0, sirq_ticks = 0;
619         FILE *f = fopen ("/proc/stat", "r");
620         if (!f)
621                 return;
622         if (cpu_id < 0)
623                 hz *= mono_cpu_count ();
624         while ((s = fgets (buf, sizeof (buf), f))) {
625                 char *data = NULL;
626                 if (cpu_id < 0 && strncmp (s, "cpu", 3) == 0 && g_ascii_isspace (s [3])) {
627                         data = s + 4;
628                 } else if (cpu_id >= 0 && strncmp (s, "cpu", 3) == 0 && strtol (s + 3, &data, 10) == cpu_id) {
629                         if (data == s + 3)
630                                 continue;
631                         data++;
632                 } else {
633                         continue;
634                 }
635                 
636                 user_ticks = strtoull (data, &data, 10);
637                 nice_ticks = strtoull (data, &data, 10);
638                 system_ticks = strtoull (data, &data, 10);
639                 idle_ticks = strtoull (data, &data, 10);
640                 /* iowait_ticks = strtoull (data, &data, 10); */
641                 irq_ticks = strtoull (data, &data, 10);
642                 sirq_ticks = strtoull (data, &data, 10);
643                 break;
644         }
645         fclose (f);
646
647         if (user)
648                 *user = (user_ticks + nice_ticks) * 10000000 / hz;
649         if (systemt)
650                 *systemt = (system_ticks) * 10000000 / hz;
651         if (irq)
652                 *irq = (irq_ticks) * 10000000 / hz;
653         if (sirq)
654                 *sirq = (sirq_ticks) * 10000000 / hz;
655         if (idle)
656                 *idle = (idle_ticks) * 10000000 / hz;
657 }
658
659 /**
660  * mono_cpu_get_data:
661  * @cpu_id: processor number or -1 to get a summary of all the processors
662  * @data: type of data to retrieve
663  *
664  * Get data about a processor on the system, like time spent in user space or idle time.
665  */
666 gint64
667 mono_cpu_get_data (int cpu_id, MonoCpuData data, MonoProcessError *error)
668 {
669         gint64 value = 0;
670
671         if (error)
672                 *error = MONO_PROCESS_ERROR_NONE;
673         switch (data) {
674         case MONO_CPU_USER_TIME:
675                 get_cpu_times (cpu_id, &value, NULL, NULL, NULL, NULL);
676                 break;
677         case MONO_CPU_PRIV_TIME:
678                 get_cpu_times (cpu_id, NULL, &value, NULL, NULL, NULL);
679                 break;
680         case MONO_CPU_INTR_TIME:
681                 get_cpu_times (cpu_id, NULL, NULL, &value, NULL, NULL);
682                 break;
683         case MONO_CPU_DCP_TIME:
684                 get_cpu_times (cpu_id, NULL, NULL, NULL, &value, NULL);
685                 break;
686         case MONO_CPU_IDLE_TIME:
687                 get_cpu_times (cpu_id, NULL, NULL, NULL, NULL, &value);
688                 break;
689
690         case MONO_CPU_END:
691                 /* Nothing yet */
692                 return 0;
693         }
694         return value;
695 }
696
697 int
698 mono_atexit (void (*func)(void))
699 {
700 #ifdef PLATFORM_ANDROID
701         /* Some versions of android libc doesn't define atexit () */
702         return 0;
703 #else
704         return atexit (func);
705 #endif
706 }