testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / gdbsupport / run-time-clock.cc
blobf754b03f0466cc5b93ebe94c870a12e9bd1a018f
1 /* User/system CPU time clocks that follow the std::chrono interface.
2 Copyright (C) 2016-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #include "run-time-clock.h"
20 #if defined HAVE_SYS_RESOURCE_H
21 #include <sys/resource.h>
22 #endif
24 using namespace std::chrono;
26 run_time_clock::time_point
27 run_time_clock::now () noexcept
29 return time_point (microseconds (get_run_time ()));
32 #ifdef HAVE_GETRUSAGE
33 static std::chrono::microseconds
34 timeval_to_microseconds (struct timeval *tv)
36 return (seconds (tv->tv_sec) + microseconds (tv->tv_usec));
38 #endif
40 void
41 run_time_clock::now (user_cpu_time_clock::time_point &user,
42 system_cpu_time_clock::time_point &system) noexcept
44 #ifdef HAVE_GETRUSAGE
45 struct rusage rusage;
47 getrusage (RUSAGE_SELF, &rusage);
49 microseconds utime = timeval_to_microseconds (&rusage.ru_utime);
50 microseconds stime = timeval_to_microseconds (&rusage.ru_stime);
51 user = user_cpu_time_clock::time_point (utime);
52 system = system_cpu_time_clock::time_point (stime);
53 #else
54 user = user_cpu_time_clock::time_point (microseconds (get_run_time ()));
55 system = system_cpu_time_clock::time_point (microseconds::zero ());
56 #endif