6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #if defined(__linux__)
26 #define _XOPEN_SOURCE 500
40 #if defined(__linux__)
41 # include <sys/types.h>
42 # include <sys/stat.h>
47 #include "app_mem_usage.h"
49 #define MAX_COMPONENTS 16
53 win32_get_total_mem_used_by_app(void)
56 PROCESS_MEMORY_COUNTERS pmc;
57 SIZE_T workingSize = 0;
59 pHandle = GetCurrentProcess();
61 if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
62 workingSize = pmc.WorkingSetSize;
70 return (int)workingSize;
74 #define get_total_mem_used_by_app win32_get_total_mem_used_by_app
78 #if defined(__linux__)
81 linux_get_memory(gsize *ptotal, gsize *prss)
84 static intptr_t pagesize = 0;
87 unsigned long total, rss;
91 pagesize = sysconf(_SC_PAGESIZE);
99 g_snprintf(path, sizeof(path), "/proc/%d/statm", getpid());
101 fd = open(path, O_RDONLY);
103 /* XXX, fallback to some other /proc file ? */
109 ret = pread(fd, buf, sizeof(buf)-1, 0);
115 if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
119 *ptotal = pagesize * (gsize) total;
121 *prss = pagesize * (gsize) rss;
127 linux_get_total_mem_used_by_app(void)
131 if (!linux_get_memory(&total, NULL))
138 linux_get_rss_mem_used_by_app(void)
142 if (!linux_get_memory(NULL, &rss))
148 #define get_total_mem_used_by_app linux_get_total_mem_used_by_app
150 #define get_rss_mem_used_by_app linux_get_rss_mem_used_by_app
154 /* XXX, BSD 4.3: getrusage() -> ru_ixrss ? */
156 #ifdef get_total_mem_used_by_app
157 static const ws_mem_usage_t total_usage = { "Total", get_total_mem_used_by_app, NULL };
160 #ifdef get_rss_mem_used_by_app
161 static const ws_mem_usage_t rss_usage = { "RSS", get_rss_mem_used_by_app, NULL };
164 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
165 #ifdef get_total_mem_used_by_app
168 #ifdef get_rss_mem_used_by_app
173 static guint memory_register_num = 0
174 #ifdef get_total_mem_used_by_app
177 #ifdef get_rss_mem_used_by_app
185 memory_usage_component_register(const ws_mem_usage_t *component)
187 if (memory_register_num >= MAX_COMPONENTS)
190 memory_components[memory_register_num++] = component;
194 memory_usage_get(guint index, gsize *value)
196 if (index >= memory_register_num)
200 *value = memory_components[index]->fetch();
202 return memory_components[index]->name;
206 memory_usage_gc(void)
210 for (i = 0; i < memory_register_num; i++) {
211 if (memory_components[i]->gc)
212 memory_components[i]->gc();