run-time-clock.cc 1.8 KB

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