Mercurial > gnulib
annotate lib/times.c @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
10735
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
1 /* Get process times |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
2 |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
3 Copyright (C) 2008-2019 Free Software Foundation, Inc. |
10735
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
4 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
5 This program is free software; you can redistribute it and/or modify |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
7 the Free Software Foundation; either version 2, or (at your option) |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
8 any later version. |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
9 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
13 GNU General Public License for more details. |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
14 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
19190 | 16 along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
10735
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
17 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
18 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */ |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
19 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
20 #include <config.h> |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
21 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
22 /* Get times prototype. */ |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
23 #include <sys/times.h> |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
24 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
25 /* Get round. */ |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
26 #include <math.h> |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
27 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
28 /* Get GetProcessTimes etc. */ |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
29 #include <windows.h> |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
30 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
31 static clock_t |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
32 filetime2clock (FILETIME time) |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
33 { |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
34 float f; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
35 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
36 /* We have a 64-bit value, in the form of two DWORDS aka unsigned |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
37 int, counting the number of 100-nanosecond intervals. We need to |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
38 convert these to clock ticks. Older POSIX uses CLK_TCK to |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
39 indicate the number of clock ticks per second while modern POSIX |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
40 uses sysconf(_SC_CLK_TCK). Mingw32 does not appear to have |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
41 sysconf(_SC_CLK_TCK), but appears to have CLK_TCK = 1000 so we |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
42 use it. Note that CLOCKS_PER_SEC constant does not apply here, |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
43 it is for use with the clock function. */ |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
44 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
45 f = (unsigned long long) time.dwHighDateTime << 32; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
46 f += time.dwLowDateTime; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
47 f = f * CLK_TCK / 10000000; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
48 return (clock_t) round (f); |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
49 } |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
50 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
51 clock_t |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
52 times (struct tms * buffer) |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
53 { |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
54 FILETIME creation_time, exit_time, kernel_time, user_time; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
55 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
56 if (GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10735
diff
changeset
|
57 &kernel_time, &user_time) == 0) |
10735
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
58 return (clock_t) -1; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
59 |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
60 buffer->tms_utime = filetime2clock (user_time); |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
61 buffer->tms_stime = filetime2clock (kernel_time); |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
62 buffer->tms_cutime = 0; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
63 buffer->tms_cstime = 0; |
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
64 |
17700
a2f13d7d07b3
times: fix to return non constant value on MS-Windows
Eli Zaretskii <eliz@gnu.org>
parents:
17576
diff
changeset
|
65 return clock (); |
10735
d3ffeb7d9a34
Add modules for sys/times.h header and times function.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
66 } |