master
c 368 lines 13.2 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "apps_plugin.h"
4
5 #if defined(OS_FREEBSD)
6
7 usec_t system_current_time_ut;
8 long global_block_size = 512;
9
10 static long get_fs_block_size(void) {
11 struct statvfs vfs;
12 static long block_size = 0;
13
14 if (block_size == 0) {
15 if (statvfs("/", &vfs) == 0) {
16 block_size = vfs.f_frsize ? vfs.f_frsize : vfs.f_bsize;
17 } else {
18 // If statvfs fails, fall back to the typical block size
19 block_size = 512;
20 }
21 }
22
23 return block_size;
24 }
25
26 void apps_os_init_freebsd(void) {
27 global_block_size = get_fs_block_size();
28 }
29
30 static inline void get_current_time(void) {
31 struct timeval current_time;
32 gettimeofday(&current_time, NULL);
33 system_current_time_ut = timeval_usec(&current_time);
34 }
35
36 uint64_t apps_os_get_total_memory_freebsd(void) {
37 uint64_t ret = 0;
38
39 int mib[2] = {CTL_HW, HW_PHYSMEM};
40 size_t size = sizeof(ret);
41 if (sysctl(mib, 2, &ret, &size, NULL, 0) == -1) {
42 netdata_log_error("Failed to get total memory using sysctl");
43 return 0;
44 }
45
46 return ret;
47 }
48
49 bool apps_os_read_pid_fds_freebsd(struct pid_stat *p, void *ptr) {
50 int mib[4];
51 size_t size;
52 struct kinfo_file *fds;
53 static char *fdsbuf;
54 char *bfdsbuf, *efdsbuf;
55 char fdsname[FILENAME_MAX + 1];
56 #define SHM_FORMAT_LEN 31 // format: 21 + size: 10
57 char shm_name[FILENAME_MAX - SHM_FORMAT_LEN + 1];
58
59 // we make all pid fds negative, so that
60 // we can detect unused file descriptors
61 // at the end, to free them
62 make_all_pid_fds_negative(p);
63
64 mib[0] = CTL_KERN;
65 mib[1] = KERN_PROC;
66 mib[2] = KERN_PROC_FILEDESC;
67 mib[3] = p->pid;
68
69 if (unlikely(sysctl(mib, 4, NULL, &size, NULL, 0))) {
70 netdata_log_error("sysctl error: Can't get file descriptors data size for pid %d", p->pid);
71 return false;
72 }
73 if (likely(size > 0))
74 fdsbuf = reallocz(fdsbuf, size);
75 if (unlikely(sysctl(mib, 4, fdsbuf, &size, NULL, 0))) {
76 netdata_log_error("sysctl error: Can't get file descriptors data for pid %d", p->pid);
77 return false;
78 }
79
80 bfdsbuf = fdsbuf;
81 efdsbuf = fdsbuf + size;
82 while (bfdsbuf < efdsbuf) {
83 fds = (struct kinfo_file *)(uintptr_t)bfdsbuf;
84 if (unlikely(fds->kf_structsize == 0))
85 break;
86
87 // do not process file descriptors for current working directory, root directory,
88 // jail directory, ktrace vnode, text vnode and controlling terminal
89 if (unlikely(fds->kf_fd < 0)) {
90 bfdsbuf += fds->kf_structsize;
91 continue;
92 }
93
94 // get file descriptors array index
95 size_t fdid = fds->kf_fd;
96
97 // check if the fds array is small
98 if (unlikely(fdid >= p->fds_size)) {
99 // it is small, extend it
100
101 uint32_t new_size = fds_new_size(p->fds_size, fdid);
102
103 debug_log("extending fd memory slots for %s from %u to %u",
104 pid_stat_comm(p), p->fds_size, new_size);
105
106 p->fds = reallocz(p->fds, new_size * sizeof(struct pid_fd));
107
108 // and initialize it
109 init_pid_fds(p, p->fds_size, new_size - p->fds_size);
110 p->fds_size = new_size;
111 }
112
113 if (unlikely(p->fds[fdid].fd == 0)) {
114 // we don't know this fd, get it
115
116 switch (fds->kf_type) {
117 case KF_TYPE_FIFO:
118 case KF_TYPE_VNODE:
119 if (unlikely(!fds->kf_path[0])) {
120 sprintf(fdsname, "other: inode: %lu", fds->kf_un.kf_file.kf_file_fileid);
121 break;
122 }
123 sprintf(fdsname, "%s", fds->kf_path);
124 break;
125 case KF_TYPE_SOCKET:
126 switch (fds->kf_sock_domain) {
127 case AF_INET:
128 case AF_INET6:
129 #if __FreeBSD_version < 1400074
130 if (fds->kf_sock_protocol == IPPROTO_TCP)
131 sprintf(fdsname, "socket: %d %lx", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sock_inpcb);
132 else
133 #endif
134 sprintf(fdsname, "socket: %d %lx", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sock_pcb);
135 break;
136 case AF_UNIX:
137 /* print address of pcb and connected pcb */
138 sprintf(fdsname, "socket: %lx %lx", fds->kf_un.kf_sock.kf_sock_pcb, fds->kf_un.kf_sock.kf_sock_unpconn);
139 break;
140 default:
141 /* print protocol number and socket address */
142 #if __FreeBSD_version < 1200031
143 sprintf(fdsname, "socket: other: %d %s %s", fds->kf_sock_protocol, fds->kf_sa_local.__ss_pad1, fds->kf_sa_local.__ss_pad2);
144 #else
145 sprintf(fdsname, "socket: other: %d %s %s", fds->kf_sock_protocol, fds->kf_un.kf_sock.kf_sa_local.__ss_pad1, fds->kf_un.kf_sock.kf_sa_local.__ss_pad2);
146 #endif
147 }
148 break;
149 case KF_TYPE_PIPE:
150 sprintf(fdsname, "pipe: %lu %lu", fds->kf_un.kf_pipe.kf_pipe_addr, fds->kf_un.kf_pipe.kf_pipe_peer);
151 break;
152 case KF_TYPE_PTS:
153 #if __FreeBSD_version < 1200031
154 sprintf(fdsname, "other: pts: %u", fds->kf_un.kf_pts.kf_pts_dev);
155 #else
156 sprintf(fdsname, "other: pts: %lu", fds->kf_un.kf_pts.kf_pts_dev);
157 #endif
158 break;
159 case KF_TYPE_SHM:
160 strncpyz(shm_name, fds->kf_path, FILENAME_MAX - SHM_FORMAT_LEN);
161 sprintf(fdsname, "other: shm: %s size: %lu", shm_name, fds->kf_un.kf_file.kf_file_size);
162 break;
163 case KF_TYPE_SEM:
164 sprintf(fdsname, "other: sem: %u", fds->kf_un.kf_sem.kf_sem_value);
165 break;
166 default:
167 sprintf(fdsname, "other: pid: %d fd: %d", fds->kf_un.kf_proc.kf_pid, fds->kf_fd);
168 }
169
170 // if another process already has this, we will get
171 // the same id
172 p->fds[fdid].fd = file_descriptor_find_or_add(fdsname, 0);
173 }
174
175 // else make it positive again, we need it
176 // of course, the actual file may have changed
177
178 else
179 p->fds[fdid].fd = -p->fds[fdid].fd;
180
181 bfdsbuf += fds->kf_structsize;
182 }
183
184 return true;
185 }
186
187 bool apps_os_get_pid_cmdline_freebsd(struct pid_stat *p, char *cmdline, size_t bytes) {
188 size_t i, b = bytes - 1;
189 int mib[4];
190
191 mib[0] = CTL_KERN;
192 mib[1] = KERN_PROC;
193 mib[2] = KERN_PROC_ARGS;
194 mib[3] = p->pid;
195 if (unlikely(sysctl(mib, 4, cmdline, &b, NULL, 0)))
196 return false;
197
198 cmdline[b] = '\0';
199 for(i = 0; i < b ; i++)
200 if(unlikely(!cmdline[i])) cmdline[i] = ' ';
201
202 return true;
203 }
204
205 bool apps_os_read_pid_io_freebsd(struct pid_stat *p, void *ptr) {
206 struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
207
208 pid_incremental_rate(io, PDF_LREAD, proc_info->ki_rusage.ru_inblock * global_block_size);
209 pid_incremental_rate(io, PDF_LWRITE, proc_info->ki_rusage.ru_oublock * global_block_size);
210
211 return true;
212 }
213
214 bool apps_os_read_pid_limits_freebsd(struct pid_stat *p __maybe_unused, void *ptr __maybe_unused) {
215 return false;
216 }
217
218 bool apps_os_read_pid_status_freebsd(struct pid_stat *p, void *ptr) {
219 struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
220
221 p->uid = proc_info->ki_uid;
222 p->gid = proc_info->ki_groups[0];
223 p->values[PDF_VMSIZE] = proc_info->ki_size;
224 p->values[PDF_VMRSS] = proc_info->ki_rssize * pagesize;
225 // TODO: what about shared and swap memory on FreeBSD?
226 return true;
227 }
228
229 //bool apps_os_read_global_cpu_utilization_freebsd(void) {
230 // static kernel_uint_t utime_raw = 0, stime_raw = 0, ntime_raw = 0;
231 // static usec_t collected_usec = 0, last_collected_usec = 0;
232 // long cp_time[CPUSTATES];
233 //
234 // if (unlikely(CPUSTATES != 5)) {
235 // goto cleanup;
236 // } else {
237 // static int mib[2] = {0, 0};
238 //
239 // if (unlikely(GETSYSCTL_SIMPLE("kern.cp_time", mib, cp_time))) {
240 // goto cleanup;
241 // }
242 // }
243 //
244 // last_collected_usec = collected_usec;
245 // collected_usec = now_monotonic_usec();
246 //
247 // calls_counter++;
248 //
249 // // temporary - it is added global_ntime;
250 // kernel_uint_t global_ntime = 0;
251 //
252 // incremental_rate(global_utime, utime_raw, cp_time[0], collected_usec, last_collected_usec, (NSEC_PER_SEC / system_hz));
253 // incremental_rate(global_ntime, ntime_raw, cp_time[1], collected_usec, last_collected_usec, (NSEC_PER_SEC / system_hz));
254 // incremental_rate(global_stime, stime_raw, cp_time[2], collected_usec, last_collected_usec, (NSEC_PER_SEC / system_hz));
255 //
256 // global_utime += global_ntime;
257 //
258 // if(unlikely(global_iterations_counter == 1)) {
259 // global_utime = 0;
260 // global_stime = 0;
261 // global_gtime = 0;
262 // }
263 //
264 // return 1;
265 //
266 //cleanup:
267 // global_utime = 0;
268 // global_stime = 0;
269 // global_gtime = 0;
270 // return 0;
271 //}
272
273 bool apps_os_read_pid_stat_freebsd(struct pid_stat *p, void *ptr) {
274 struct kinfo_proc *proc_info = (struct kinfo_proc *)ptr;
275 if (unlikely(proc_info->ki_tdflags & TDF_IDLETD))
276 goto cleanup;
277
278 char *comm = proc_info->ki_comm;
279 p->ppid = proc_info->ki_ppid;
280
281 update_pid_comm(p, comm);
282
283 pid_incremental_rate(stat, PDF_MINFLT, (kernel_uint_t)proc_info->ki_rusage.ru_minflt);
284 pid_incremental_rate(stat, PDF_CMINFLT, (kernel_uint_t)proc_info->ki_rusage_ch.ru_minflt);
285 pid_incremental_rate(stat, PDF_MAJFLT, (kernel_uint_t)proc_info->ki_rusage.ru_majflt);
286 pid_incremental_rate(stat, PDF_CMAJFLT, (kernel_uint_t)proc_info->ki_rusage_ch.ru_majflt);
287 pid_incremental_cpu(stat, PDF_UTIME, (kernel_uint_t)proc_info->ki_rusage.ru_utime.tv_sec * NSEC_PER_SEC + proc_info->ki_rusage.ru_utime.tv_usec * NSEC_PER_USEC);
288 pid_incremental_cpu(stat, PDF_STIME, (kernel_uint_t)proc_info->ki_rusage.ru_stime.tv_sec * NSEC_PER_SEC + proc_info->ki_rusage.ru_stime.tv_usec * NSEC_PER_USEC);
289 pid_incremental_cpu(stat, PDF_CUTIME, (kernel_uint_t)proc_info->ki_rusage_ch.ru_utime.tv_sec * NSEC_PER_SEC + proc_info->ki_rusage_ch.ru_utime.tv_usec * NSEC_PER_USEC);
290 pid_incremental_cpu(stat, PDF_CSTIME, (kernel_uint_t)proc_info->ki_rusage_ch.ru_stime.tv_sec * NSEC_PER_SEC + proc_info->ki_rusage_ch.ru_stime.tv_usec * NSEC_PER_USEC);
291
292 p->values[PDF_THREADS] = proc_info->ki_numthreads;
293
294 usec_t started_ut = timeval_usec(&proc_info->ki_start);
295 p->values[PDF_UPTIME] = (system_current_time_ut > started_ut) ? (system_current_time_ut - started_ut) / USEC_PER_SEC : 0;
296
297 if(unlikely(debug_enabled))
298 debug_log_int("READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d",
299 netdata_configured_host_prefix, p->pid, pid_stat_comm(p), (p->target)?string2str(p->target->name):"UNSET",
300 p->stat_collected_usec - p->last_stat_collected_usec,
301 p->values[PDF_UTIME],
302 p->values[PDF_STIME],
303 p->values[PDF_CUTIME],
304 p->values[PDF_CSTIME],
305 p->values[PDF_MINFLT],
306 p->values[PDF_MAJFLT],
307 p->values[PDF_CMINFLT],
308 p->values[PDF_CMAJFLT],
309 p->values[PDF_THREADS]);
310
311 return true;
312
313 cleanup:
314 return false;
315 }
316
317 bool apps_os_collect_all_pids_freebsd(void) {
318 // Mark all processes as unread before collecting new data
319 struct pid_stat *p = NULL;
320 int i, procnum;
321
322 static size_t procbase_size = 0;
323 static struct kinfo_proc *procbase = NULL;
324
325 size_t new_procbase_size;
326
327 int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC };
328 if (unlikely(sysctl(mib, 3, NULL, &new_procbase_size, NULL, 0))) {
329 netdata_log_error("sysctl error: Can't get processes data size");
330 return false;
331 }
332
333 // give it some air for processes that may be started
334 // during this little time.
335 new_procbase_size += 100 * sizeof(struct kinfo_proc);
336
337 // increase the buffer if needed
338 if(new_procbase_size > procbase_size) {
339 procbase_size = new_procbase_size;
340 procbase = reallocz(procbase, procbase_size);
341 }
342
343 // sysctl() gets from new_procbase_size the buffer size
344 // and also returns to it the amount of data filled in
345 new_procbase_size = procbase_size;
346
347 // get the processes from the system
348 if (unlikely(sysctl(mib, 3, procbase, &new_procbase_size, NULL, 0))) {
349 netdata_log_error("sysctl error: Can't get processes data");
350 return false;
351 }
352
353 // based on the amount of data filled in
354 // calculate the number of processes we got
355 procnum = new_procbase_size / sizeof(struct kinfo_proc);
356
357 get_current_time();
358
359 for (i = 0 ; i < procnum ; ++i) {
360 pid_t pid = procbase[i].ki_pid;
361 if (pid <= 0) continue;
362 incrementally_collect_data_for_pid(pid, &procbase[i]);
363 }
364
365 return true;
366 }
367
368 #endif