]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/times.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / mach / hurd / times.c
1 /* Return CPU and real time used by process and its children. Hurd version.
2 Copyright (C) 2001-2019 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 <stddef.h>
21 #include <sys/resource.h>
22 #include <sys/times.h>
23 #include <sys/time.h>
24 #include <time.h>
25 #include <mach.h>
26 #include <mach/task_info.h>
27 #include <hurd.h>
28
29 static inline clock_t
30 clock_from_time_value (const time_value_t *t)
31 {
32 return t->seconds * 1000000 + t->microseconds;
33 }
34
35 /* Store the CPU time used by this process and all its
36 dead children (and their dead children) in BUFFER.
37 Return the elapsed real time, or (clock_t) -1 for errors.
38 All times are in CLK_TCKths of a second. */
39 clock_t
40 __times (struct tms *tms)
41 {
42 struct task_basic_info bi;
43 struct task_thread_times_info tti;
44 mach_msg_type_number_t count;
45 union { time_value_t tvt; struct timeval tv; } now;
46 error_t err;
47
48 count = TASK_BASIC_INFO_COUNT;
49 err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
50 (task_info_t) &bi, &count);
51 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 tms->tms_utime = (clock_from_time_value (&bi.user_time)
61 + clock_from_time_value (&tti.user_time));
62 tms->tms_stime = (clock_from_time_value (&bi.system_time)
63 + clock_from_time_value (&tti.system_time));
64
65 /* XXX This can't be implemented until getrusage(RUSAGE_CHILDREN) can be. */
66 tms->tms_cutime = tms->tms_cstime = 0;
67
68 if (__gettimeofday (&now.tv, NULL) < 0)
69 return -1;
70
71 return (clock_from_time_value (&now.tvt)
72 - clock_from_time_value (&bi.creation_time));
73 }
74 weak_alias (__times, times)