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