Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/databases/clickhouse/files/patch-src_Common_MemoryStatisticsOS.cpp
16460 views
1
--- src/Common/MemoryStatisticsOS.cpp.orig 2021-12-26 09:29:33 UTC
2
+++ src/Common/MemoryStatisticsOS.cpp
3
@@ -1,4 +1,4 @@
4
-#if defined(OS_LINUX)
5
+#if defined(OS_LINUX) || defined(OS_FREEBSD)
6
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
@@ -6,6 +6,13 @@
10
#include <unistd.h>
11
#include <cassert>
12
13
+#ifdef OS_FREEBSD
14
+#include <sys/param.h>
15
+#include <sys/sysctl.h>
16
+#include <sys/user.h>
17
+#include <libprocstat.h>
18
+#endif
19
+
20
#include "MemoryStatisticsOS.h"
21
22
#include <base/logger_useful.h>
23
@@ -24,20 +31,40 @@ namespace ErrorCodes
24
extern const int CANNOT_OPEN_FILE;
25
extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
26
extern const int CANNOT_CLOSE_FILE;
27
+#ifdef OS_FREEBSD
28
+ extern const int CANNOT_ALLOCATE_MEMORY;
29
+#endif
30
}
31
32
-static constexpr auto filename = "/proc/self/statm";
33
+#ifndef OS_FREEBSD
34
+ static constexpr auto filename = "/proc/self/statm";
35
+ static constexpr size_t PAGE_SIZE = 4096;
36
+#endif
37
38
MemoryStatisticsOS::MemoryStatisticsOS()
39
{
40
+#ifdef OS_FREEBSD
41
+ pstat = ::procstat_open_sysctl();
42
+ if (NULL == pstat)
43
+ {
44
+ throwFromErrno("Cannot open sysctl", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
45
+ }
46
+#else
47
fd = ::open(filename, O_RDONLY | O_CLOEXEC);
48
49
if (-1 == fd)
50
throwFromErrno("Cannot open file " + std::string(filename), errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
51
+#endif
52
}
53
54
MemoryStatisticsOS::~MemoryStatisticsOS()
55
{
56
+#ifdef OS_FREEBSD
57
+ if (NULL != pstat)
58
+ {
59
+ ::procstat_close(pstat);
60
+ }
61
+#else
62
if (0 != ::close(fd))
63
{
64
try
65
@@ -51,12 +78,42 @@ MemoryStatisticsOS::~MemoryStatisticsOS()
66
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
67
}
68
}
69
+#endif
70
}
71
72
MemoryStatisticsOS::Data MemoryStatisticsOS::get() const
73
{
74
Data data;
75
76
+#ifdef OS_FREEBSD
77
+ size_t pagesize = ::getpagesize();
78
+ unsigned int count = 0;
79
+
80
+ struct kinfo_proc *kp;
81
+ struct kinfo_vmentry *kve;
82
+
83
+ kp = ::procstat_getprocs(pstat, KERN_PROC_PID, ::getpid(), &count);
84
+ if (NULL == kp)
85
+ {
86
+ throwFromErrno("Cannot get proc info", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
87
+ }
88
+
89
+ kve = ::procstat_getvmmap(pstat, kp, &count);
90
+ if (NULL == kve)
91
+ {
92
+ ::procstat_freeprocs(pstat, kp);
93
+ throwFromErrno("Cannot get vmmap info", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
94
+ }
95
+
96
+ data.virt = kp->ki_size;
97
+ data.resident = kp->ki_rssize * pagesize;
98
+ data.shared = (kp->ki_rssize - kve->kve_private_resident) * pagesize;
99
+ data.code = kp->ki_tsize * pagesize;
100
+ data.data_and_stack = (kp->ki_dsize + kp->ki_ssize) * pagesize;
101
+
102
+ ::procstat_freevmmap(pstat, kve);
103
+ ::procstat_freeprocs(pstat, kp);
104
+#else
105
constexpr size_t buf_size = 1024;
106
char buf[buf_size];
107
108
@@ -99,7 +156,7 @@ MemoryStatisticsOS::Data MemoryStatisticsOS::get() con
109
data.shared *= page_size;
110
data.code *= page_size;
111
data.data_and_stack *= page_size;
112
-
113
+#endif
114
return data;
115
}
116
117
118