dcerpc-netlogon: improve NetrLogonGetCapabilities dissection
[wireshark-sm.git] / epan / app_mem_usage.c
blob41d8174ebe7ce90d4ea5161df197c84983ba1351
1 /*
2 * app_mem_usage.c
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #if defined(__linux__)
12 #define _XOPEN_SOURCE 500
13 #endif
15 #include "config.h"
17 #include <stdio.h>
19 #ifdef _WIN32
20 #include <windows.h>
21 #include <psapi.h>
22 #endif /* _WIN32 */
24 #if defined(__linux__)
25 # include <sys/types.h>
26 # include <sys/stat.h>
27 # include <unistd.h>
28 # include <fcntl.h>
29 #endif
31 #include "wsutil/file_util.h"
32 #include "app_mem_usage.h"
34 #define MAX_COMPONENTS 16
36 #if defined(_WIN32)
38 static size_t
39 win32_get_total_mem_used_by_app(void)
41 HANDLE pHandle;
42 PROCESS_MEMORY_COUNTERS pmc;
43 SIZE_T workingSize = 0;
45 pHandle = GetCurrentProcess();
47 if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
48 workingSize = pmc.WorkingSetSize;
51 CloseHandle(pHandle);
53 if(workingSize == 0){
54 return -1;
55 }else{
56 return (int)workingSize;
60 static const ws_mem_usage_t total_usage = { "Total", win32_get_total_mem_used_by_app, NULL };
62 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
63 &total_usage,
66 static unsigned memory_register_num = 1;
68 #elif defined(__linux__)
70 static bool
71 linux_get_memory(size_t *ptotal, size_t *prss)
73 static int fd = -1;
74 static intptr_t pagesize = 0;
76 char buf[128];
77 unsigned long total, rss;
78 ssize_t ret;
80 if (!pagesize)
81 pagesize = sysconf(_SC_PAGESIZE);
83 if (pagesize == -1)
84 return false;
86 if (fd < 0) {
87 char path[64];
89 snprintf(path, sizeof(path), "/proc/%d/statm", getpid());
91 fd = ws_open(path, O_RDONLY);
93 /* XXX, fallback to some other /proc file ? */
96 if (fd < 0)
97 return false;
99 ret = pread(fd, buf, sizeof(buf)-1, 0);
100 if (ret <= 0)
101 return false;
103 buf[ret] = '\0';
105 if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
106 return false;
108 if (ptotal)
109 *ptotal = pagesize * (size_t) total;
110 if (prss)
111 *prss = pagesize * (size_t) rss;
113 return true;
116 static size_t
117 linux_get_total_mem_used_by_app(void)
119 size_t total;
121 if (!linux_get_memory(&total, NULL))
122 total = 0;
124 return total;
127 static size_t
128 linux_get_rss_mem_used_by_app(void)
130 size_t rss;
132 if (!linux_get_memory(NULL, &rss))
133 rss = 0;
135 return rss;
138 static const ws_mem_usage_t total_usage = { "Total", linux_get_total_mem_used_by_app, NULL };
139 static const ws_mem_usage_t rss_usage = { "RSS", linux_get_rss_mem_used_by_app, NULL };
141 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
142 &total_usage,
143 &rss_usage,
146 static unsigned memory_register_num = 2;
148 #else
151 * macOS: task_info()?
153 * *BSD: getrusage() -> ru_ixrss ? Note that there are three
154 * current-RSS components in struct rusage, but those date
155 * back to the days when you had just text, data, and stack,
156 * and kernels might not even bother supplying them.
159 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS];
161 static unsigned memory_register_num;
163 #endif
165 /* public API */
167 void
168 memory_usage_component_register(const ws_mem_usage_t *component)
170 if (memory_register_num >= MAX_COMPONENTS)
171 return;
173 memory_components[memory_register_num++] = component;
176 const char *
177 memory_usage_get(unsigned idx, size_t *value)
179 if (idx >= memory_register_num)
180 return NULL;
182 if (value)
183 *value = memory_components[idx]->fetch();
185 return memory_components[idx]->name;
188 void
189 memory_usage_gc(void)
191 unsigned i;
193 for (i = 0; i < memory_register_num; i++) {
194 if (memory_components[i]->gc)
195 memory_components[i]->gc();
201 * Editor modelines - https://www.wireshark.org/tools/modelines.html
203 * Local variables:
204 * c-basic-offset: 8
205 * tab-width: 8
206 * indent-tabs-mode: t
207 * End:
209 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
210 * :indentSize=8:tabSize=8:noTabs=false: