Skip to content

Commit 7503a15

Browse files
authored
Implement all for FreeBSD (#12)
1 parent 168f0f9 commit 7503a15

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.20)
66
project(process)
77
include(GNUInstallDirs)
88

9-
set(TTLDTOR_PROCESS "1.3.0" CACHE STRING "The Process package version")
9+
set(TTLDTOR_PROCESS "2.0.0" CACHE STRING "The Process package version")
1010

1111
set(CMAKE_CXX_STANDARD 14)
1212
set(CMAKE_C_STANDARD 11)
@@ -32,6 +32,8 @@ add_library(process::process ALIAS process)
3232

3333
if (WIN32)
3434
target_link_libraries(process PRIVATE psapi)
35+
else ()
36+
message(STATUS "Platform: " ${CMAKE_SYSTEM_NAME})
3537
endif ()
3638

3739
if (TTLDTOR_PROCESS_SAMPLES)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Cross-platform library for obtaining metrics of the current process
1111
* [x] Windows
1212
* [x] Linux
1313
* [x] MacOS
14-
* [ ] FreeBSD
14+
* [x] FreeBSD
15+
* [ ] OpenBSD
1516
* [ ] AIX
1617
* [ ] ...
1718

src/process.cpp

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ std::uint64_t Process::getPrivateMemorySize() noexcept {
127127
#elif defined(__linux__)
128128

129129
# include <sys/resource.h>
130+
# include <unistd.h>
130131

131132
namespace ttldtor {
132133
namespace process {
@@ -339,29 +340,118 @@ std::uint64_t Process::getPrivateMemorySize() noexcept {
339340
}
340341
} // namespace process
341342
} // namespace ttldtor
342-
#else
343+
#elif defined(__FreeBSD__)
344+
345+
# include <errno.h>
346+
# include <sys/param.h>
347+
# include <sys/sysctl.h>
348+
# include <sys/types.h>
349+
# include <sys/user.h>
350+
# include <unistd.h>
351+
343352
namespace ttldtor {
344353
namespace process {
354+
355+
struct RUsageResult {
356+
std::chrono::milliseconds sysTime{};
357+
std::chrono::milliseconds userTime{};
358+
std::chrono::milliseconds totalTime{};
359+
360+
explicit RUsageResult(const rusage &ru)
361+
: sysTime{static_cast<std::uint64_t>(ru.ru_stime.tv_sec) * 1000ULL +
362+
static_cast<std::uint64_t>(ru.ru_stime.tv_usec) / 1000ULL},
363+
userTime{static_cast<std::uint64_t>(ru.ru_utime.tv_sec) * 1000ULL +
364+
static_cast<std::uint64_t>(ru.ru_utime.tv_usec) / 1000ULL},
365+
totalTime{sysTime + userTime} {
366+
}
367+
};
368+
369+
bool getProcInfo(int pid, kinfo_proc &info) noexcept {
370+
const std::size_t MIB_SIZE = 4; // 6 - OpenBSD
371+
// MIB - Management Information Base
372+
int mib[MIB_SIZE] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
373+
std::size_t length = sizeof(kinfo_proc);
374+
375+
if (sysctl(mib, MIB_SIZE, &info, &length, nullptr, 0) < 0) {
376+
return false;
377+
}
378+
379+
return true;
380+
}
381+
345382
std::chrono::milliseconds Process::getKernelProcessorTime() noexcept {
383+
kinfo_proc info{};
384+
385+
if (getProcInfo(getpid(), info)) {
386+
return RUsageResult{info.ki_rusage}.sysTime;
387+
}
388+
346389
return std::chrono::milliseconds(0);
347390
}
348391

349392
std::chrono::milliseconds Process::getUserProcessorTime() noexcept {
393+
kinfo_proc info{};
394+
395+
if (getProcInfo(getpid(), info)) {
396+
return RUsageResult{info.ki_rusage}.userTime;
397+
}
398+
350399
return std::chrono::milliseconds(0);
351400
}
352401

353402
std::chrono::milliseconds Process::getTotalProcessorTime() noexcept {
403+
kinfo_proc info{};
404+
405+
if (getProcInfo(getpid(), info)) {
406+
return RUsageResult{info.ki_rusage}.totalTime;
407+
}
408+
354409
return std::chrono::milliseconds(0);
355410
}
356411

357412
std::uint64_t Process::getWorkingSetSize() noexcept {
413+
kinfo_proc info{};
414+
415+
if (getProcInfo(getpid(), info)) {
416+
return static_cast<std::uint64_t>(info.ki_rssize) * getpagesize();
417+
}
418+
358419
return 0ULL;
359420
}
360421

361422
std::uint64_t Process::getPrivateMemorySize() noexcept {
423+
kinfo_proc info{};
424+
425+
if (getProcInfo(getpid(), info)) {
426+
return static_cast<std::uint64_t>(info.ki_size);
427+
}
428+
362429
return 0ULL;
363430
}
431+
} // namespace process
432+
} // namespace ttldtor
433+
#else
434+
namespace ttldtor {
435+
namespace process {
436+
std::chrono::milliseconds Process::getKernelProcessorTime() noexcept {
437+
return std::chrono::milliseconds(0);
438+
}
439+
440+
std::chrono::milliseconds Process::getUserProcessorTime() noexcept {
441+
return std::chrono::milliseconds(0);
442+
}
443+
444+
std::chrono::milliseconds Process::getTotalProcessorTime() noexcept {
445+
return std::chrono::milliseconds(0);
364446
}
447+
448+
std::uint64_t Process::getWorkingSetSize() noexcept {
449+
return 0ULL;
365450
}
451+
452+
std::uint64_t Process::getPrivateMemorySize() noexcept {
453+
return 0ULL;
366454
}
455+
} // namespace process
456+
} // namespace ttldtor
367457
#endif

0 commit comments

Comments
 (0)