/* * SPDX-FileCopyrightText: 2025 Eilertsens Kodeknekkeri * SPDX-FileCopyrightText: 2025 The FreeBSD Foundation * SPDX-FileContributor: Harald Eilertsen * * SPDX-License-Identifier: BSD-2-Clause */ #include "procstat_cpu_time.hpp" #include "helpers.hpp" #include #include // For procstat funcs #include #include #include #include #include #include void procstat_cpu_time(pthread_t thread, struct timeval * utime, struct timeval * stime) { auto ps = procstat_open_sysctl(); unsigned int count = 0; auto procinfo = procstat_getprocs(ps, KERN_PROC_PID|KERN_PROC_INC_THREAD, getpid(), &count); #ifndef NDEBUG std::cout << "[*] Received procinfo @ " << procinfo << ", with " << count << " entries" << std::endl; #endif for (auto i = 0U; i < count; ++i) { auto tinfo = procinfo + i; #ifndef NDEBUG std::cout << "[*] " << i << ": tid = " << tinfo->ki_tid << ", runtime = " << tinfo->ki_runtime << ", user time = " << timeval_to_long(tinfo->ki_rusage.ru_utime) << ", sys time = " << timeval_to_long(tinfo->ki_rusage.ru_stime) << std::endl; #endif if (tinfo->ki_tid == ktid) { *utime = tinfo->ki_rusage.ru_utime; *stime = tinfo->ki_rusage.ru_stime; break; } } procstat_freeprocs(ps, procinfo); }