1 /* Return CPU and real time used by process and its children.  Hurd version.
2    Copyright (C) 2001-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 <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
clock_from_time_value(const time_value_t * 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
__times(struct tms * tms)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   time_value_t 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   __host_get_time (__mach_host_self (), &now);
69 
70   return (clock_from_time_value (&now)
71 	  - clock_from_time_value (&bi.creation_time));
72 }
73 weak_alias (__times, times)
74