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
11 #if defined(__linux__)
12 #define _XOPEN_SOURCE 500
26 #if defined(__linux__)
27 # include <sys/types.h>
28 # include <sys/stat.h>
33 #include "wsutil/file_util.h"
34 #include "app_mem_usage.h"
36 #define MAX_COMPONENTS 16
41 win32_get_total_mem_used_by_app(void)
44 PROCESS_MEMORY_COUNTERS pmc;
45 SIZE_T workingSize = 0;
47 pHandle = GetCurrentProcess();
49 if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
50 workingSize = pmc.WorkingSetSize;
58 return (int)workingSize;
62 static const ws_mem_usage_t total_usage = { "Total", win32_get_total_mem_used_by_app, NULL };
64 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
68 static guint memory_register_num = 1;
70 #elif defined(__linux__)
73 linux_get_memory(gsize *ptotal, gsize *prss)
76 static intptr_t pagesize = 0;
79 unsigned long total, rss;
83 pagesize = sysconf(_SC_PAGESIZE);
91 g_snprintf(path, sizeof(path), "/proc/%d/statm", getpid());
93 fd = ws_open(path, O_RDONLY);
95 /* XXX, fallback to some other /proc file ? */
101 ret = pread(fd, buf, sizeof(buf)-1, 0);
107 if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
111 *ptotal = pagesize * (gsize) total;
113 *prss = pagesize * (gsize) rss;
119 linux_get_total_mem_used_by_app(void)
123 if (!linux_get_memory(&total, NULL))
130 linux_get_rss_mem_used_by_app(void)
134 if (!linux_get_memory(NULL, &rss))
140 static const ws_mem_usage_t total_usage = { "Total", linux_get_total_mem_used_by_app, NULL };
141 static const ws_mem_usage_t rss_usage = { "RSS", linux_get_rss_mem_used_by_app, NULL };
143 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
148 static guint memory_register_num = 2;
153 * macOS: task_info()?
155 * *BSD: getrusage() -> ru_ixrss ? Note that there are three
156 * current-RSS components in struct rusage, but those date
157 * back to the days when you had just text, data, and stack,
158 * and kernels might not even bother supplying them.
161 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS];
163 static guint memory_register_num = 0;
170 memory_usage_component_register(const ws_mem_usage_t *component)
172 if (memory_register_num >= MAX_COMPONENTS)
175 memory_components[memory_register_num++] = component;
179 memory_usage_get(guint idx, gsize *value)
181 if (idx >= memory_register_num)
185 *value = memory_components[idx]->fetch();
187 return memory_components[idx]->name;
191 memory_usage_gc(void)
195 for (i = 0; i < memory_register_num; i++) {
196 if (memory_components[i]->gc)
197 memory_components[i]->gc();
203 * Editor modelines - http://www.wireshark.org/tools/modelines.html
208 * indent-tabs-mode: t
211 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
212 * :indentSize=8:tabSize=8:noTabs=false: