Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / getrusage.c
blobc2d508769717fb83b8daba9dbd711b45743ad200
1 /* getrusage -- Get resource usage information about processes. Hurd version.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/resource.h>
22 #include <mach.h>
23 #include <mach/task_info.h>
24 #include <hurd.h>
26 /* Return resource usage information on process indicated by WHO
27 and put it in *USAGE. Returns 0 for success, -1 for failure. */
28 int
29 __getrusage (who, usage)
30 enum __rusage_who who;
31 struct rusage *usage;
33 struct task_basic_info bi;
34 struct task_events_info ei;
35 struct task_thread_times_info tti;
36 mach_msg_type_number_t count;
37 error_t err;
39 switch (who)
41 case RUSAGE_SELF:
42 count = TASK_BASIC_INFO_COUNT;
43 err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
44 (task_info_t) &bi, &count);
45 if (err)
46 return __hurd_fail (err);
48 count = TASK_EVENTS_INFO_COUNT;
49 err = __task_info (__mach_task_self (), TASK_EVENTS_INFO,
50 (task_info_t) &ei, &count);
51 if (err == KERN_INVALID_ARGUMENT) /* microkernel doesn't implement it */
52 memset (&ei, 0, sizeof ei);
53 else if (err)
54 return __hurd_fail (err);
56 count = TASK_THREAD_TIMES_INFO_COUNT;
57 err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
58 (task_info_t) &tti, &count);
59 if (err)
60 return __hurd_fail (err);
62 time_value_add (&bi.user_time, &tti.user_time);
63 time_value_add (&bi.system_time, &tti.system_time);
65 memset (usage, 0, sizeof (struct rusage));
67 usage->ru_utime.tv_sec = bi.user_time.seconds;
68 usage->ru_utime.tv_usec = bi.user_time.microseconds;
69 usage->ru_stime.tv_sec = bi.system_time.seconds;
70 usage->ru_stime.tv_usec = bi.system_time.microseconds;
72 /* These statistics map only approximately. */
73 usage->ru_majflt = ei.pageins;
74 usage->ru_minflt = ei.faults - ei.pageins;
75 usage->ru_msgsnd = ei.messages_sent; /* Mach IPC, not SysV IPC */
76 usage->ru_msgrcv = ei.messages_received; /* Mach IPC, not SysV IPC */
77 break;
79 case RUSAGE_CHILDREN:
80 /* XXX Not implemented yet. However, zero out USAGE to be
81 consistent with the wait3 and wait4 functions. */
82 memset (usage, 0, sizeof (struct rusage));
84 break;
86 default:
87 return EINVAL;
90 return 0;
93 weak_alias (__getrusage, getrusage)