CPMConnect: unify version handling
[wireshark-wip.git] / epan / app_mem_usage.c
blob55093ae84785e4f88c40a1e74944acffa63dc057
1 /*
2 * app_mem_usage.c
4 * $Id$
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
27 #endif
29 #include "config.h"
31 #include <stdio.h>
33 #include <glib.h>
35 #ifdef _WIN32
36 #include <windows.h>
37 #include <psapi.h>
38 #endif /* _WIN32 */
40 #if defined(__linux__)
41 # include <sys/types.h>
42 # include <sys/stat.h>
43 # include <unistd.h>
44 # include <fcntl.h>
45 #endif
47 #include "app_mem_usage.h"
49 #define MAX_COMPONENTS 16
51 #if defined(_WIN32)
52 static gsize
53 win32_get_total_mem_used_by_app(void)
55 HANDLE pHandle;
56 PROCESS_MEMORY_COUNTERS pmc;
57 SIZE_T workingSize = 0;
59 pHandle = GetCurrentProcess();
61 if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
62 workingSize = pmc.WorkingSetSize;
65 CloseHandle(pHandle);
67 if(workingSize == 0){
68 return -1;
69 }else{
70 return (int)workingSize;
74 #define get_total_mem_used_by_app win32_get_total_mem_used_by_app
76 #endif /* (_WIN32) */
78 #if defined(__linux__)
80 static gboolean
81 linux_get_memory(gsize *ptotal, gsize *prss)
83 static int fd = -1;
84 static intptr_t pagesize = 0;
86 char buf[128];
87 unsigned long total, rss;
88 ssize_t ret;
90 if (!pagesize)
91 pagesize = sysconf(_SC_PAGESIZE);
93 if (pagesize == -1)
94 return FALSE;
96 if (fd < 0) {
97 char path[64];
99 g_snprintf(path, sizeof(path), "/proc/%d/statm", getpid());
101 fd = open(path, O_RDONLY);
103 /* XXX, fallback to some other /proc file ? */
106 if (fd < 0)
107 return FALSE;
109 ret = pread(fd, buf, sizeof(buf)-1, 0);
110 if (ret <= 0)
111 return FALSE;
113 buf[ret] = '\0';
115 if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
116 return FALSE;
118 if (ptotal)
119 *ptotal = pagesize * (gsize) total;
120 if (prss)
121 *prss = pagesize * (gsize) rss;
123 return TRUE;
126 static gsize
127 linux_get_total_mem_used_by_app(void)
129 gsize total;
131 if (!linux_get_memory(&total, NULL))
132 total = 0;
134 return total;
137 static gsize
138 linux_get_rss_mem_used_by_app(void)
140 gsize rss;
142 if (!linux_get_memory(NULL, &rss))
143 rss = 0;
145 return 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
152 #endif
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 };
158 #endif
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 };
162 #endif
164 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
165 #ifdef get_total_mem_used_by_app
166 &total_usage,
167 #endif
168 #ifdef get_rss_mem_used_by_app
169 &rss_usage,
170 #endif
173 static guint memory_register_num = 0
174 #ifdef get_total_mem_used_by_app
176 #endif
177 #ifdef get_rss_mem_used_by_app
179 #endif
182 /* public API */
184 void
185 memory_usage_component_register(const ws_mem_usage_t *component)
187 if (memory_register_num >= MAX_COMPONENTS)
188 return;
190 memory_components[memory_register_num++] = component;
193 const char *
194 memory_usage_get(guint index, gsize *value)
196 if (index >= memory_register_num)
197 return NULL;
199 if (value)
200 *value = memory_components[index]->fetch();
202 return memory_components[index]->name;
205 void
206 memory_usage_gc(void)
208 guint i;
210 for (i = 0; i < memory_register_num; i++) {
211 if (memory_components[i]->gc)
212 memory_components[i]->gc();