18dffb485Schristos /* User/system CPU time clocks that follow the std::chrono interface. 2*5ba1f45fSchristos Copyright (C) 2016-2024 Free Software Foundation, Inc. 38dffb485Schristos 48dffb485Schristos This file is part of GDB. 58dffb485Schristos 68dffb485Schristos This program is free software; you can redistribute it and/or modify 78dffb485Schristos it under the terms of the GNU General Public License as published by 88dffb485Schristos the Free Software Foundation; either version 3 of the License, or 98dffb485Schristos (at your option) any later version. 108dffb485Schristos 118dffb485Schristos This program is distributed in the hope that it will be useful, 128dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 138dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148dffb485Schristos GNU General Public License for more details. 158dffb485Schristos 168dffb485Schristos You should have received a copy of the GNU General Public License 178dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 188dffb485Schristos 198dffb485Schristos #include "run-time-clock.h" 208dffb485Schristos #if defined HAVE_SYS_RESOURCE_H 218dffb485Schristos #include <sys/resource.h> 228dffb485Schristos #endif 238dffb485Schristos 248dffb485Schristos using namespace std::chrono; 258dffb485Schristos 268dffb485Schristos run_time_clock::time_point 278dffb485Schristos run_time_clock::now () noexcept 288dffb485Schristos { 298dffb485Schristos return time_point (microseconds (get_run_time ())); 308dffb485Schristos } 318dffb485Schristos 328dffb485Schristos #ifdef HAVE_GETRUSAGE 338dffb485Schristos static std::chrono::microseconds 348dffb485Schristos timeval_to_microseconds (struct timeval *tv) 358dffb485Schristos { 368dffb485Schristos return (seconds (tv->tv_sec) + microseconds (tv->tv_usec)); 378dffb485Schristos } 388dffb485Schristos #endif 398dffb485Schristos 408dffb485Schristos void 418dffb485Schristos run_time_clock::now (user_cpu_time_clock::time_point &user, 428dffb485Schristos system_cpu_time_clock::time_point &system) noexcept 438dffb485Schristos { 448dffb485Schristos #ifdef HAVE_GETRUSAGE 458dffb485Schristos struct rusage rusage; 468dffb485Schristos 478dffb485Schristos getrusage (RUSAGE_SELF, &rusage); 488dffb485Schristos 498dffb485Schristos microseconds utime = timeval_to_microseconds (&rusage.ru_utime); 508dffb485Schristos microseconds stime = timeval_to_microseconds (&rusage.ru_stime); 518dffb485Schristos user = user_cpu_time_clock::time_point (utime); 528dffb485Schristos system = system_cpu_time_clock::time_point (stime); 538dffb485Schristos #else 548dffb485Schristos user = user_cpu_time_clock::time_point (microseconds (get_run_time ())); 558dffb485Schristos system = system_cpu_time_clock::time_point (microseconds::zero ()); 568dffb485Schristos #endif 578dffb485Schristos } 58