1 /* getrusage -- Get resource usage information about processes. Hurd version.
2 Copyright (C) 1999-2022 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, see
17 <https://www.gnu.org/licenses/>. */
18
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>
25
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
__getrusage(enum __rusage_who who,struct rusage * usage)29 __getrusage (enum __rusage_who who, struct rusage *usage)
30 {
31 struct task_basic_info bi;
32 struct task_events_info ei;
33 struct task_thread_times_info tti;
34 mach_msg_type_number_t count;
35 error_t err;
36
37 switch (who)
38 {
39 case RUSAGE_SELF:
40 count = TASK_BASIC_INFO_COUNT;
41 err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
42 (task_info_t) &bi, &count);
43 if (err)
44 return __hurd_fail (err);
45
46 count = TASK_EVENTS_INFO_COUNT;
47 err = __task_info (__mach_task_self (), TASK_EVENTS_INFO,
48 (task_info_t) &ei, &count);
49 if (err == KERN_INVALID_ARGUMENT) /* microkernel doesn't implement it */
50 memset (&ei, 0, sizeof ei);
51 else if (err)
52 return __hurd_fail (err);
53
54 count = TASK_THREAD_TIMES_INFO_COUNT;
55 err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
56 (task_info_t) &tti, &count);
57 if (err)
58 return __hurd_fail (err);
59
60 time_value_add (&bi.user_time, &tti.user_time);
61 time_value_add (&bi.system_time, &tti.system_time);
62
63 memset (usage, 0, sizeof (struct rusage));
64
65 usage->ru_utime.tv_sec = bi.user_time.seconds;
66 usage->ru_utime.tv_usec = bi.user_time.microseconds;
67 usage->ru_stime.tv_sec = bi.system_time.seconds;
68 usage->ru_stime.tv_usec = bi.system_time.microseconds;
69
70 /* These statistics map only approximately. */
71 usage->ru_majflt = ei.pageins;
72 usage->ru_minflt = ei.faults - ei.pageins;
73 usage->ru_msgsnd = ei.messages_sent; /* Mach IPC, not SysV IPC */
74 usage->ru_msgrcv = ei.messages_received; /* Mach IPC, not SysV IPC */
75 break;
76
77 case RUSAGE_CHILDREN:
78 /* XXX Not implemented yet. However, zero out USAGE to be
79 consistent with the wait3 and wait4 functions. */
80 memset (usage, 0, sizeof (struct rusage));
81
82 break;
83
84 default:
85 return EINVAL;
86 }
87
88 return 0;
89 }
90
91 weak_alias (__getrusage, getrusage)
92