4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #if defined(__linux__)
24 #define _XOPEN_SOURCE 500
38 #if defined(__linux__)
39 # include <sys/types.h>
40 # include <sys/stat.h>
45 #include "app_mem_usage.h"
47 #define MAX_COMPONENTS 16
51 win32_get_total_mem_used_by_app(void)
54 PROCESS_MEMORY_COUNTERS pmc;
55 SIZE_T workingSize = 0;
57 pHandle = GetCurrentProcess();
59 if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
60 workingSize = pmc.WorkingSetSize;
68 return (int)workingSize;
72 #define get_total_mem_used_by_app win32_get_total_mem_used_by_app
76 #if defined(__linux__)
79 linux_get_memory(gsize *ptotal, gsize *prss)
82 static intptr_t pagesize = 0;
85 unsigned long total, rss;
89 pagesize = sysconf(_SC_PAGESIZE);
97 g_snprintf(path, sizeof(path), "/proc/%d/statm", getpid());
99 fd = open(path, O_RDONLY);
101 /* XXX, fallback to some other /proc file ? */
107 ret = pread(fd, buf, sizeof(buf)-1, 0);
113 if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
117 *ptotal = pagesize * (gsize) total;
119 *prss = pagesize * (gsize) rss;
125 linux_get_total_mem_used_by_app(void)
129 if (!linux_get_memory(&total, NULL))
136 linux_get_rss_mem_used_by_app(void)
140 if (!linux_get_memory(NULL, &rss))
146 #define get_total_mem_used_by_app linux_get_total_mem_used_by_app
148 #define get_rss_mem_used_by_app linux_get_rss_mem_used_by_app
152 /* XXX, BSD 4.3: getrusage() -> ru_ixrss ? */
154 #ifdef get_total_mem_used_by_app
155 static const ws_mem_usage_t total_usage = { "Total", get_total_mem_used_by_app, NULL };
158 #ifdef get_rss_mem_used_by_app
159 static const ws_mem_usage_t rss_usage = { "RSS", get_rss_mem_used_by_app, NULL };
162 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
163 #ifdef get_total_mem_used_by_app
166 #ifdef get_rss_mem_used_by_app
171 static guint memory_register_num = 0
172 #ifdef get_total_mem_used_by_app
175 #ifdef get_rss_mem_used_by_app
183 memory_usage_component_register(const ws_mem_usage_t *component)
185 if (memory_register_num >= MAX_COMPONENTS)
188 memory_components[memory_register_num++] = component;
192 memory_usage_get(guint index, gsize *value)
194 if (index >= memory_register_num)
198 *value = memory_components[index]->fetch();
200 return memory_components[index]->name;
204 memory_usage_gc(void)
208 for (i = 0; i < memory_register_num; i++) {
209 if (memory_components[i]->gc)
210 memory_components[i]->gc();
216 * Editor modelines - http://www.wireshark.org/tools/modelines.html
221 * indent-tabs-mode: t
224 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
225 * :indentSize=8:tabSize=8:noTabs=false: