@@ -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
131132namespace ttldtor {
132133namespace 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+
343352namespace ttldtor {
344353namespace 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+
345382std::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
349392std::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
353402std::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
357412std::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
361422std::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