]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/times.c
2065aac49aa87821a0a6e66f900b7a4d5d14c3c3
[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,02 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 <stddef.h>
22 #include <sys/resource.h>
23 #include <sys/times.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <mach.h>
27 #include <mach/task_info.h>
28 #include <hurd.h>
29
30 static inline clock_t
31 clock_from_time_value (const time_value_t *t)
32 {
33 return t->seconds * 1000000 + t->microseconds;
34 }
35
36 #if NO_CREATION_TIME
37 static time_value_t startup_time;
38 static void times_init (void) __attribute__ ((unused));
39 static void
40 times_init (void)
41 {
42 __gettimeofday ((struct timeval *) &startup_time, NULL);
43 }
44 text_set_element (__libc_subinit, times_init);
45 #endif
46
47 /* Store the CPU time used by this process and all its
48 dead children (and their dead children) in BUFFER.
49 Return the elapsed real time, or (clock_t) -1 for errors.
50 All times are in CLK_TCKths of a second. */
51 clock_t
52 __times (struct tms *tms)
53 {
54 struct task_basic_info bi;
55 struct task_thread_times_info tti;
56 mach_msg_type_number_t count;
57 time_value_t now;
58 error_t err;
59
60 count = TASK_BASIC_INFO_COUNT;
61 err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
62 (task_info_t) &bi, &count);
63 if (err)
64 return __hurd_fail (err);
65
66 count = TASK_THREAD_TIMES_INFO_COUNT;
67 err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
68 (task_info_t) &tti, &count);
69 if (err)
70 return __hurd_fail (err);
71
72 tms->tms_utime = (clock_from_time_value (&bi.user_time)
73 + clock_from_time_value (&tti.user_time));
74 tms->tms_stime = (clock_from_time_value (&bi.system_time)
75 + clock_from_time_value (&tti.system_time));
76
77 /* XXX This can't be implemented until getrusage(RUSAGE_CHILDREN) can be. */
78 tms->tms_cutime = tms->tms_cstime = 0;
79
80 if (__gettimeofday ((struct timeval *) &now, NULL) < 0)
81 return -1;
82
83 #if NO_CREATION_TIME
84 # define our_creation_time startup_time
85 #else
86 # define our_creation_time bi.startup_time
87 #endif
88 return (clock_from_time_value (&now)
89 - clock_from_time_value (&our_creation_time));
90 }
91 weak_alias (__times, times)