1 /* User/system CPU time clocks that follow the std::chrono interface. 2 Copyright (C) 2016-2025 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 #ifndef GDBSUPPORT_RUN_TIME_CLOCK_H 20 #define GDBSUPPORT_RUN_TIME_CLOCK_H 21 22 #include <chrono> 23 24 /* Count the total amount of time spent executing in user mode. */ 25 26 struct user_cpu_time_clock 27 { 28 using duration = std::chrono::microseconds; 29 using rep = duration::rep; 30 using period = duration::period; 31 using time_point = std::chrono::time_point<user_cpu_time_clock>; 32 33 static constexpr bool is_steady = true; 34 35 /* Use run_time_clock::now instead. */ 36 static time_point now () noexcept = delete; 37 }; 38 39 /* Count the total amount of time spent executing in kernel mode. */ 40 41 struct system_cpu_time_clock 42 { 43 using duration = std::chrono::microseconds; 44 using rep = duration::rep; 45 using period = duration::period; 46 using time_point = std::chrono::time_point<system_cpu_time_clock>; 47 48 static constexpr bool is_steady = true; 49 50 /* Use run_time_clock::now instead. */ 51 static time_point now () noexcept = delete; 52 }; 53 54 /* Whether to measure time run time for the whole process or just one 55 thread. */ 56 57 enum class run_time_scope 58 { 59 process, 60 thread, 61 }; 62 63 /* Return the user/system time as separate time points, if 64 supported. If not supported, then the combined user+kernel time 65 is returned in USER and SYSTEM is set to zero. 66 67 SCOPE indicates whether to return the run time for the whole 68 process or just for the calling thread. If the latter isn't 69 supported, then returns the run time for the whole process even if 70 run_time_scope::thread is requested. */ 71 72 void get_run_time (user_cpu_time_clock::time_point &user, 73 system_cpu_time_clock::time_point &system, 74 run_time_scope scope) noexcept; 75 76 /* Returns true if is it possible for get_run_time above to return the 77 run time for just the calling thread. */ 78 79 bool get_run_time_thread_scope_available (); 80 81 /* Count the total amount of time spent executing in userspace+kernel 82 mode. */ 83 84 struct run_time_clock 85 { 86 using duration = std::chrono::microseconds; 87 using rep = duration::rep; 88 using period = duration::period; 89 using time_point = std::chrono::time_point<run_time_clock>; 90 91 static constexpr bool is_steady = true; 92 93 static time_point now () noexcept; 94 }; 95 96 #endif /* GDBSUPPORT_RUN_TIME_CLOCK_H */ 97