GDB (xrefs)

run-time-clock.c
Go to the documentation of this file.
1 /* User/system CPU time clocks that follow the std::chrono interface.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "common-defs.h"
20 #include "run-time-clock.h"
21 #if defined HAVE_SYS_RESOURCE_H
22 #include <sys/resource.h>
23 #endif
24
25 using namespace std::chrono;
26
27 run_time_clock::time_point
28 run_time_clock::now () noexcept
29 {
30 return time_point (microseconds (get_run_time ()));
31 }
32
33 #ifdef HAVE_GETRUSAGE
34 static std::chrono::microseconds
35 timeval_to_microseconds (struct timeval *tv)
36 {
37 return (seconds (tv->tv_sec) + microseconds (tv->tv_usec));
38 }
39 #endif
40
41 void
42 run_time_clock::now (user_cpu_time_clock::time_point &user,
43 system_cpu_time_clock::time_point &system) noexcept
44 {
45 #ifdef HAVE_GETRUSAGE
46 struct rusage rusage;
47
48 getrusage (RUSAGE_SELF, &rusage);
49
50 microseconds utime = timeval_to_microseconds (&rusage.ru_utime);
51 microseconds stime = timeval_to_microseconds (&rusage.ru_stime);
52 user = user_cpu_time_clock::time_point (utime);
53 system = system_cpu_time_clock::time_point (stime);
54 #else
55 user = user_cpu_time_clock::time_point (microseconds (get_run_time ()));
56 system = system_cpu_time_clock::time_point (microseconds::zero ());
57 #endif
58 }
rusage
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t rusage
Definition: gnu-nat.c:1822
system_cpu_time_clock::time_point
std::chrono::time_point< system_cpu_time_clock > time_point
Definition: run-time-clock.h:46
timeval_to_microseconds
static std::chrono::microseconds timeval_to_microseconds(struct timeval *tv)
Definition: run-time-clock.c:35
chrono
common-defs.h
run-time-clock.h
user_cpu_time_clock::time_point
std::chrono::time_point< user_cpu_time_clock > time_point
Definition: run-time-clock.h:31
run_time_clock::time_point
std::chrono::time_point< run_time_clock > time_point
Definition: run-time-clock.h:62
run_time_clock::now
static time_point now() noexcept
Definition: run-time-clock.c:28