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 4 return 1; 5 } 6 7 +#elif defined GPAC_CONFIG_FREEBSD 8 + 9 +#include <sys/types.h> 10 +#include <sys/sysctl.h> 11 +#include <sys/time.h> 12 +#include <sys/user.h> 13 + 14 +#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 15 +int getsysctl(const char *name, void *ptr, size_t len) { 16 + size_t nlen = 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))); 19 + return -1; 20 + } 21 + if (nlen != len) { 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)); 24 + return -1; 25 + } 26 + return 0; 27 +} 28 + 29 +static int stathz = 128; 30 + 31 +Bool gf_sys_get_rti_os(u32 refresh_time_ms, GF_SystemRTInfo *rti, u32 flags) 32 +{ 33 + size_t length; 34 + u32 entry_time; 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; 41 + 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)); 45 + return 0; 46 + } 47 + 48 + int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, the_rti.pid }; 49 + length = sizeof(kinfo); 50 + 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; 54 + } 55 + 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]; 59 + } 60 + 61 + the_rti.physical_memory = the_rti.physical_memory_avail = 0; 62 + if (GETSYSCTL("hw.physmem", result) == 0) { 63 + the_rti.physical_memory = result; 64 + } 65 + 66 + if (GETSYSCTL("hw.usermem", result) == 0) { 67 + the_rti.physical_memory_avail = result; 68 + } 69 + 70 + the_rti.sampling_instant = last_update_time; 71 + 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; 75 + 76 + /*oops, we have no choice but to assume 100% cpu usage during this period*/ 77 + if (!u_k_time) { 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); 84 + } else { 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; 88 + 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; 92 + 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 ); 101 + } 102 + } else { 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; 107 + } 108 + } 109 + } 110 + the_rti.process_memory = mem_at_startup - the_rti.physical_memory_avail; 111 + 112 +#ifdef GPAC_MEMORY_TRACKING 113 + the_rti.gpac_memory = gpac_allocated_memory; 114 +#endif 115 + 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)); 121 + return 1; 122 +} 123 + 124 //linux 125 #else 126 127