A simple app to get run times from a thread

Just a quick app to test and demonstrate how to get run times from a
specific thread on FreeBSD.
This commit is contained in:
Harald Eilertsen 2025-06-20 13:21:43 +02:00
commit 45e6aebfbf
10 changed files with 366 additions and 0 deletions

53
procstat_cpu_time.cpp Normal file
View file

@ -0,0 +1,53 @@
/*
* SPDX-FileCopyrightText: 2025 Eilertsens Kodeknekkeri
* SPDX-FileCopyrightText: 2025 The FreeBSD Foundation
* SPDX-FileContributor: Harald Eilertsen <haraldei@anduin.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "procstat_cpu_time.hpp"
#include "helpers.hpp"
#include <iostream>
#include <unistd.h>
// For procstat funcs
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <libprocstat.h>
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);
}