1 --- src/utils/os_divers.c.orig 2020-06-16 16:17:17 UTC
2 +++ src/utils/os_divers.c
3 @@ -1680,6 +1680,123 @@ Bool gf_sys_get_rti_os(u32 refresh_time_ms, GF_SystemR
7 +#elif defined GPAC_CONFIG_FREEBSD
9 +#include <sys/types.h>
10 +#include <sys/sysctl.h>
11 +#include <sys/time.h>
12 +#include <sys/user.h>
14 +#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
15 +int getsysctl(const char *name, void *ptr, size_t len) {
17 + if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
18 + GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] sysctl(%s...) failed: %s\n", name, strerror(errno)));
22 + GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("[RTI] sysctl(%s...) expected %lu, got %lu\n",
23 + name, (unsigned long)len, (unsigned long)nlen));
29 +static int stathz = 128;
31 +Bool gf_sys_get_rti_os(u32 refresh_time_ms, GF_SystemRTInfo *rti, u32 flags)
35 + struct kinfo_proc kinfo;
36 + unsigned long result;
37 + u32 u_k_time = 0, idle_time = 0;
38 + u64 process_u_k_time = 0;
39 + long cp_time[CPUSTATES];
40 + struct clockinfo clockinfo;
42 + entry_time = gf_sys_clock();
43 + if (last_update_time && (entry_time - last_update_time < refresh_time_ms)) {
44 + memcpy(rti, &the_rti, sizeof(GF_SystemRTInfo));
48 + int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, the_rti.pid };
49 + length = sizeof(kinfo);
51 + if (sysctl(mib, 4, &kinfo, &length, NULL, 0) == 0) {
52 + process_u_k_time = (u64)(kinfo.ki_rusage.ru_utime.tv_usec + kinfo.ki_rusage.ru_stime.tv_usec) +
53 + (u64)(kinfo.ki_rusage.ru_utime.tv_sec + kinfo.ki_rusage.ru_stime.tv_sec) * (u64)1000000;
56 + if (GETSYSCTL("kern.cp_time", cp_time) == 0) {
57 + u_k_time = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
58 + idle_time = cp_time[CP_IDLE];
61 + the_rti.physical_memory = the_rti.physical_memory_avail = 0;
62 + if (GETSYSCTL("hw.physmem", result) == 0) {
63 + the_rti.physical_memory = result;
66 + if (GETSYSCTL("hw.usermem", result) == 0) {
67 + the_rti.physical_memory_avail = result;
70 + the_rti.sampling_instant = last_update_time;
72 + if (last_update_time) {
73 + the_rti.sampling_period_duration = (entry_time - last_update_time);
74 + the_rti.process_cpu_time_diff = (process_u_k_time - last_process_k_u_time) / 1000;
76 + /*oops, we have no choice but to assume 100% cpu usage during this period*/
78 + the_rti.total_cpu_time_diff = the_rti.sampling_period_duration;
79 + u_k_time = last_cpu_u_k_time + the_rti.sampling_period_duration;
80 + the_rti.cpu_idle_time = 0;
81 + the_rti.total_cpu_usage = 100;
82 + if (!the_rti.process_cpu_time_diff) the_rti.process_cpu_time_diff = the_rti.total_cpu_time_diff;
83 + the_rti.process_cpu_usage = (u32) ( 100 * the_rti.process_cpu_time_diff / the_rti.sampling_period_duration);
85 + u64 samp_sys_time, cpu_idle_time;
86 + /*move to ms (kern.cp_time gives times in 1/stathz unit*/
87 + the_rti.total_cpu_time_diff = (u_k_time - last_cpu_u_k_time) * 1000 / stathz;
89 + /*we're not that accurate....*/
90 + if (the_rti.total_cpu_time_diff > the_rti.sampling_period_duration)
91 + the_rti.sampling_period_duration = the_rti.total_cpu_time_diff;
93 + if (!idle_time) idle_time = (the_rti.sampling_period_duration - the_rti.total_cpu_time_diff) * stathz / 1000;
94 + samp_sys_time = u_k_time - last_cpu_u_k_time;
95 + cpu_idle_time = idle_time - last_cpu_idle_time;
96 + the_rti.total_cpu_usage = (u32) ( 100 * samp_sys_time / (cpu_idle_time + samp_sys_time ) );
97 + /*move to ms (kern.cp_time gives times in 1/stathz unit*/
98 + the_rti.cpu_idle_time = cpu_idle_time * 1000 / stathz;
99 + if (!the_rti.process_cpu_time_diff) the_rti.process_cpu_time_diff = the_rti.total_cpu_time_diff;
100 + the_rti.process_cpu_usage = (u32) ( stathz * the_rti.process_cpu_time_diff / (cpu_idle_time + samp_sys_time) / 10 );
103 + mem_at_startup = the_rti.physical_memory_avail;
104 + if (GETSYSCTL("kern.clockrate", clockinfo) == 0) {
105 + if (clockinfo.stathz > 0) {
106 + stathz = clockinfo.stathz;
110 + the_rti.process_memory = mem_at_startup - the_rti.physical_memory_avail;
112 +#ifdef GPAC_MEMORY_TRACKING
113 + the_rti.gpac_memory = gpac_allocated_memory;
116 + last_process_k_u_time = process_u_k_time;
117 + last_cpu_idle_time = idle_time;
118 + last_cpu_u_k_time = u_k_time;
119 + last_update_time = entry_time;
120 + memcpy(rti, &the_rti, sizeof(GF_SystemRTInfo));