Just a quick app to test and demonstrate how to get run times from a specific thread on FreeBSD.
53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
/*
|
|
* 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);
|
|
}
|