Path: blob/main/databases/clickhouse/files/patch-src_Common_MemoryStatisticsOS.cpp
16124 views
--- src/Common/MemoryStatisticsOS.cpp.orig 2021-03-19 11:39:14 UTC1+++ src/Common/MemoryStatisticsOS.cpp2@@ -1,14 +1,20 @@3-#if defined(OS_LINUX)4-5#include <sys/types.h>6#include <sys/stat.h>7+8+#ifdef OS_FREEBSD9+#include <sys/param.h>10+#include <sys/sysctl.h>11+#include <sys/user.h>12+#include <libprocstat.h>13+#endif14+15#include <fcntl.h>16#include <unistd.h>17#include <cassert>1819#include "MemoryStatisticsOS.h"2021-#include <common/logger_useful.h>22+//#include <common/logger_useful.h>23#include <common/getPageSize.h>24#include <Common/Exception.h>25#include <IO/ReadBufferFromMemory.h>26@@ -24,20 +30,40 @@ namespace ErrorCodes27extern const int CANNOT_OPEN_FILE;28extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;29extern const int CANNOT_CLOSE_FILE;30+#ifdef OS_FREEBSD31+ extern const int CANNOT_ALLOCATE_MEMORY;32+#endif33}3435-static constexpr auto filename = "/proc/self/statm";36+#ifndef OS_FREEBSD37+ static constexpr auto filename = "/proc/self/statm";38+ static constexpr size_t PAGE_SIZE = 4096;39+#endif4041MemoryStatisticsOS::MemoryStatisticsOS()42{43+#ifdef OS_FREEBSD44+ pstat = ::procstat_open_sysctl();45+ if (NULL == pstat)46+ {47+ throwFromErrno("Cannot open sysctl", ErrorCodes::CANNOT_ALLOCATE_MEMORY);48+ }49+#else50fd = ::open(filename, O_RDONLY | O_CLOEXEC);5152if (-1 == fd)53throwFromErrno("Cannot open file " + std::string(filename), errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);54+#endif55}5657MemoryStatisticsOS::~MemoryStatisticsOS()58{59+#ifdef OS_FREEBSD60+ if (NULL != pstat)61+ {62+ ::procstat_close(pstat);63+ }64+#else65if (0 != ::close(fd))66{67try68@@ -51,12 +77,42 @@ MemoryStatisticsOS::~MemoryStatisticsOS()69DB::tryLogCurrentException(__PRETTY_FUNCTION__);70}71}72+#endif73}7475MemoryStatisticsOS::Data MemoryStatisticsOS::get() const76{77Data data;7879+#ifdef OS_FREEBSD80+ size_t pagesize = ::getpagesize();81+ unsigned int count = 0;82+83+ struct kinfo_proc *kp;84+ struct kinfo_vmentry *kve;85+86+ kp = ::procstat_getprocs(pstat, KERN_PROC_PID, ::getpid(), &count);87+ if (NULL == kp)88+ {89+ throwFromErrno("Cannot get proc info", ErrorCodes::CANNOT_ALLOCATE_MEMORY);90+ }91+92+ kve = ::procstat_getvmmap(pstat, kp, &count);93+ if (NULL == kve)94+ {95+ ::procstat_freeprocs(pstat, kp);96+ throwFromErrno("Cannot get vmmap info", ErrorCodes::CANNOT_ALLOCATE_MEMORY);97+ }98+99+ data.virt = kp->ki_size;100+ data.resident = kp->ki_rssize * pagesize;101+ data.shared = (kp->ki_rssize - kve->kve_private_resident) * pagesize;102+ data.code = kp->ki_tsize * pagesize;103+ data.data_and_stack = (kp->ki_dsize + kp->ki_ssize) * pagesize;104+105+ ::procstat_freevmmap(pstat, kve);106+ ::procstat_freeprocs(pstat, kp);107+#else108constexpr size_t buf_size = 1024;109char buf[buf_size];110111@@ -99,10 +155,8 @@ MemoryStatisticsOS::Data MemoryStatisticsOS::get() con112data.shared *= page_size;113data.code *= page_size;114data.data_and_stack *= page_size;115-116+#endif117return data;118}119120}121-122-#endif123124125