|
@@ -33,6 +33,16 @@
|
|
|
#if !defined(__APPLE__)
|
|
|
#include <sys/times.h>
|
|
|
#include <sys/wait.h>
|
|
|
+#ifdef __FreeBSD__
|
|
|
+#include <sys/param.h>
|
|
|
+#include <sys/queue.h>
|
|
|
+#include <sys/socket.h>
|
|
|
+#include <sys/user.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <libprocstat.h>
|
|
|
+#else
|
|
|
+#include <sys/resource.h>
|
|
|
+#endif
|
|
|
#include <spawn.h>
|
|
|
#endif
|
|
|
|
|
@@ -678,6 +688,120 @@ int os_get_logical_cores(void)
|
|
|
os_get_cores_internal();
|
|
|
return logical_cores;
|
|
|
}
|
|
|
+
|
|
|
+#ifdef __FreeBSD__
|
|
|
+uint64_t os_get_sys_free_size(void)
|
|
|
+{
|
|
|
+ uint64_t mem_free = 0;
|
|
|
+ size_t length = sizeof(mem_free);
|
|
|
+ if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length,
|
|
|
+ NULL, 0) < 0)
|
|
|
+ return 0;
|
|
|
+ return mem_free;
|
|
|
+}
|
|
|
+
|
|
|
+static inline bool os_get_proc_memory_usage_internal(struct kinfo_proc *kinfo)
|
|
|
+{
|
|
|
+ int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
|
|
|
+ size_t length = sizeof(*kinfo);
|
|
|
+ if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), kinfo, &length,
|
|
|
+ NULL, 0) < 0)
|
|
|
+ return false;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|
|
+{
|
|
|
+ struct kinfo_proc kinfo;
|
|
|
+ if (!os_get_proc_memory_usage_internal(&kinfo))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ usage->resident_size =
|
|
|
+ (uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
|
|
|
+ usage->virtual_size = (uint64_t)kinfo.ki_size;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+uint64_t os_get_proc_resident_size(void)
|
|
|
+{
|
|
|
+ struct kinfo_proc kinfo;
|
|
|
+ if (!os_get_proc_memory_usage_internal(&kinfo))
|
|
|
+ return 0;
|
|
|
+ return (uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
|
|
|
+}
|
|
|
+
|
|
|
+uint64_t os_get_proc_virtual_size(void)
|
|
|
+{
|
|
|
+ struct kinfo_proc kinfo;
|
|
|
+ if (!os_get_proc_memory_usage_internal(&kinfo))
|
|
|
+ return 0;
|
|
|
+ return (uint64_t)kinfo.ki_size;
|
|
|
+}
|
|
|
+#else
|
|
|
+uint64_t os_get_sys_free_size(void) {return 0;}
|
|
|
+
|
|
|
+typedef struct
|
|
|
+{
|
|
|
+ unsigned long virtual_size;
|
|
|
+ unsigned long resident_size;
|
|
|
+ unsigned long share_pages;
|
|
|
+ unsigned long text;
|
|
|
+ unsigned long library;
|
|
|
+ unsigned long data;
|
|
|
+ unsigned long dirty_pages;
|
|
|
+} statm_t;
|
|
|
+
|
|
|
+static inline bool os_get_proc_memory_usage_internal(statm_t *statm)
|
|
|
+{
|
|
|
+ const char *statm_path = "/proc/self/statm";
|
|
|
+
|
|
|
+ FILE *f = fopen(statm_path, "r");
|
|
|
+ if (!f)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (fscanf(f, "%ld %ld %ld %ld %ld %ld %ld",
|
|
|
+ &statm->virtual_size,
|
|
|
+ &statm->resident_size,
|
|
|
+ &statm->share_pages,
|
|
|
+ &statm->text,
|
|
|
+ &statm->library,
|
|
|
+ &statm->data,
|
|
|
+ &statm->dirty_pages) != 7) {
|
|
|
+ fclose(f);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ fclose(f);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|
|
+{
|
|
|
+ statm_t statm = {};
|
|
|
+ if (!os_get_proc_memory_usage_internal(&statm))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ usage->resident_size = statm.resident_size;
|
|
|
+ usage->virtual_size = statm.virtual_size;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+uint64_t os_get_proc_resident_size(void)
|
|
|
+{
|
|
|
+ statm_t statm = {};
|
|
|
+ if (!os_get_proc_memory_usage_internal(&statm))
|
|
|
+ return 0;
|
|
|
+ return (uint64_t)statm.resident_size;
|
|
|
+}
|
|
|
+
|
|
|
+uint64_t os_get_proc_virtual_size(void)
|
|
|
+{
|
|
|
+ statm_t statm = {};
|
|
|
+ if (!os_get_proc_memory_usage_internal(&statm))
|
|
|
+ return 0;
|
|
|
+ return (uint64_t)statm.virtual_size;
|
|
|
+}
|
|
|
+#endif
|
|
|
#endif
|
|
|
|
|
|
uint64_t os_get_free_disk_space(const char *dir)
|