ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
process_memory.cpp
Go to the documentation of this file.
1/**
2 * @file process_memory.cpp
3 * @brief Resident memory of a process, or of a process group, read from the
4 * operating system.
5 *
6 * See @c process_memory.h.
7 */
8#include "process_memory.h"
9
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <string>
14#include <vector>
15
16#if defined(__linux__)
17#include <dirent.h>
18#include <fcntl.h>
19#include <unistd.h>
20#elif defined(__APPLE__)
21#include <libproc.h>
22#include <sys/proc_info.h>
23#elif defined(__FreeBSD__)
24#include <sys/param.h>
25#include <sys/sysctl.h>
26#include <sys/user.h>
27#include <unistd.h>
28#endif
29
30#if defined(__linux__)
31/** @brief The contents of the small file @p path, or an empty string. */
32static std::string read_small_file(const char *path)
33{
34 char buf[512];
35 int fd = open(path, O_RDONLY);
36 ssize_t len;
37 if (fd < 0)
38 return std::string();
39 len = read(fd, buf, sizeof(buf) - 1);
40 close(fd);
41 if (len <= 0)
42 return std::string();
43 return std::string(buf, (std::size_t)len);
44}
45#endif
46
47std::size_t provsql_process_rss(pid_t pid)
48{
49#if defined(__linux__)
50 char path[64];
51 unsigned long size, resident;
52 snprintf(path, sizeof(path), "/proc/%d/statm", (int)pid);
53 std::string s = read_small_file(path);
54 if (s.empty() || sscanf(s.c_str(), "%lu %lu", &size, &resident) != 2)
55 return 0;
56 return (std::size_t)resident * (std::size_t)sysconf(_SC_PAGESIZE);
57#elif defined(__APPLE__)
58 struct proc_taskinfo ti;
59 if (proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &ti, sizeof(ti)) != sizeof(ti))
60 return 0;
61 return (std::size_t)ti.pti_resident_size;
62#elif defined(__FreeBSD__)
63 struct kinfo_proc kp;
64 std::size_t len = sizeof(kp);
65 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, (int)pid};
66 if (sysctl(mib, 4, &kp, &len, NULL, 0) != 0 || len == 0)
67 return 0;
68 return (std::size_t)kp.ki_rssize * (std::size_t)getpagesize();
69#else
70 (void)pid;
71 return 0;
72#endif
73}
74
75std::size_t provsql_process_group_rss(pid_t pgid)
76{
77#if defined(__linux__)
78 /* The group of each process is the fifth field of its stat, after the
79 * command, which is in parentheses and may contain anything */
80 std::size_t total = 0;
81 DIR *proc = opendir("/proc");
82 struct dirent *e;
83 if (proc == NULL)
84 return 0;
85 while ((e = readdir(proc)) != NULL) {
86 char path[300]; /* "/proc/" + a directory name + "/stat" */
87 const char *close_paren;
88 char state;
89 int ppid, pgrp;
90 if (e->d_name[0] < '0' || e->d_name[0] > '9')
91 continue;
92 snprintf(path, sizeof(path), "/proc/%s/stat", e->d_name);
93 std::string s = read_small_file(path);
94 close_paren = strrchr(s.c_str(), ')');
95 if (close_paren == NULL ||
96 sscanf(close_paren + 1, " %c %d %d", &state, &ppid, &pgrp) != 3)
97 continue;
98 if (pgrp == (int)pgid)
99 total += provsql_process_rss((pid_t)atoi(e->d_name));
100 }
101 closedir(proc);
102 return total;
103#elif defined(__APPLE__)
104 int bytes = proc_listpids(PROC_PGRP_ONLY, (uint32_t)pgid, NULL, 0);
105 std::size_t total = 0;
106 if (bytes <= 0)
107 return 0;
108 std::vector<pid_t> pids((std::size_t)bytes / sizeof(pid_t) + 16);
109 bytes = proc_listpids(PROC_PGRP_ONLY, (uint32_t)pgid, pids.data(),
110 (int)(pids.size() * sizeof(pid_t)));
111 if (bytes <= 0)
112 return 0;
113 for (std::size_t i = 0; i < (std::size_t)bytes / sizeof(pid_t); ++i)
114 if (pids[i] > 0)
115 total += provsql_process_rss(pids[i]);
116 return total;
117#elif defined(__FreeBSD__)
118 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PGRP, (int)pgid};
119 std::size_t len = 0, total = 0;
120 if (sysctl(mib, 4, NULL, &len, NULL, 0) != 0 || len == 0)
121 return 0;
122 std::vector<struct kinfo_proc> procs(len / sizeof(struct kinfo_proc) + 4);
123 len = procs.size() * sizeof(struct kinfo_proc);
124 if (sysctl(mib, 4, procs.data(), &len, NULL, 0) != 0)
125 return 0;
126 for (std::size_t i = 0; i < len / sizeof(struct kinfo_proc); ++i)
127 total += (std::size_t)procs[i].ki_rssize * (std::size_t)getpagesize();
128 return total;
129#else
130 (void)pgid;
131 return 0;
132#endif
133}
std::size_t provsql_process_rss(pid_t pid)
The resident memory of process pid, in bytes, or 0 where the platform does not say.
std::size_t provsql_process_group_rss(pid_t pgid)
The resident memory of the processes of group pgid, in bytes, or 0 where the platform does not say.
Resident memory of a process, or of a process group.