chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / mach / hurd / getrusage.c
1 /* getrusage -- Get resource usage information about processes.  Hurd version.
2    Copyright (C) 1999,2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <string.h>
22 #include <sys/resource.h>
23 #include <mach.h>
24 #include <mach/task_info.h>
25 #include <hurd.h>
26
27 /* Return resource usage information on process indicated by WHO
28    and put it in *USAGE.  Returns 0 for success, -1 for failure.  */
29 int
30 __getrusage (who, usage)
31      enum __rusage_who who;
32      struct rusage *usage;
33 {
34   struct task_basic_info bi;
35   struct task_events_info ei;
36   struct task_thread_times_info tti;
37   mach_msg_type_number_t count;
38   error_t err;
39
40   switch (who)
41     {
42     case RUSAGE_SELF:
43       count = TASK_BASIC_INFO_COUNT;
44       err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
45                          (task_info_t) &bi, &count);
46       if (err)
47         return __hurd_fail (err);
48
49       count = TASK_EVENTS_INFO_COUNT;
50       err = __task_info (__mach_task_self (), TASK_EVENTS_INFO,
51                          (task_info_t) &ei, &count);
52       if (err == KERN_INVALID_ARGUMENT) /* microkernel doesn't implement it */
53         memset (&ei, 0, sizeof ei);
54       else if (err)
55         return __hurd_fail (err);
56
57       count = TASK_THREAD_TIMES_INFO_COUNT;
58       err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
59                          (task_info_t) &tti, &count);
60       if (err)
61         return __hurd_fail (err);
62
63       time_value_add (&bi.user_time, &tti.user_time);
64       time_value_add (&bi.system_time, &tti.system_time);
65
66       memset (usage, 0, sizeof (struct rusage));
67
68       usage->ru_utime.tv_sec = bi.user_time.seconds;
69       usage->ru_utime.tv_usec = bi.user_time.microseconds;
70       usage->ru_stime.tv_sec = bi.system_time.seconds;
71       usage->ru_stime.tv_usec = bi.system_time.microseconds;
72
73       /* These statistics map only approximately.  */
74       usage->ru_majflt = ei.pageins;
75       usage->ru_minflt = ei.faults - ei.pageins;
76       usage->ru_msgsnd = ei.messages_sent; /* Mach IPC, not SysV IPC */
77       usage->ru_msgrcv = ei.messages_received; /* Mach IPC, not SysV IPC */
78       break;
79
80     case RUSAGE_CHILDREN:
81       /* XXX Not implemented yet.  However, zero out USAGE to be
82          consistent with the wait3 and wait4 functions.  */
83       memset (usage, 0, sizeof (struct rusage));
84
85       break;
86
87     default:
88       return EINVAL;
89     }
90
91   return 0;
92 }
93
94 weak_alias (__getrusage, getrusage)