ssl,dtls: use ProtocolVersion from Server Hello
[metze/wireshark/wip.git] / epan / app_mem_usage.c
1 /*
2  * app_mem_usage.c
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #if defined(__linux__)
24  #define _XOPEN_SOURCE 500
25 #endif
26
27 #include "config.h"
28
29 #include <stdio.h>
30
31 #include <glib.h>
32
33 #ifdef _WIN32
34 #include <windows.h>
35 #include <psapi.h>
36 #endif /*  _WIN32 */
37
38 #if defined(__linux__)
39 # include <sys/types.h>
40 # include <sys/stat.h>
41 # include <unistd.h>
42 # include <fcntl.h>
43 #endif
44
45 #include "app_mem_usage.h"
46
47 #define MAX_COMPONENTS 16
48
49 #if defined(_WIN32)
50 static gsize
51 win32_get_total_mem_used_by_app(void)
52 {
53         HANDLE pHandle;
54         PROCESS_MEMORY_COUNTERS pmc;
55         SIZE_T workingSize = 0;
56
57         pHandle = GetCurrentProcess();
58
59         if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
60                 workingSize = pmc.WorkingSetSize;
61         }
62
63         CloseHandle(pHandle);
64
65         if(workingSize == 0){
66                 return -1;
67         }else{
68                 return (int)workingSize;
69         }
70 }
71
72 #define get_total_mem_used_by_app win32_get_total_mem_used_by_app
73
74 #endif /* (_WIN32) */
75
76 #if defined(__linux__)
77
78 static gboolean
79 linux_get_memory(gsize *ptotal, gsize *prss)
80 {
81         static int fd = -1;
82         static intptr_t pagesize = 0;
83
84         char buf[128];
85         unsigned long total, rss;
86         ssize_t ret;
87
88         if (!pagesize)
89                 pagesize = sysconf(_SC_PAGESIZE);
90
91         if (pagesize == -1)
92                 return FALSE;
93
94         if (fd < 0) {
95                 char path[64];
96
97                 g_snprintf(path, sizeof(path), "/proc/%d/statm", getpid());
98
99                 fd = open(path, O_RDONLY);
100
101                 /* XXX, fallback to some other /proc file ? */
102         }
103
104         if (fd < 0)
105                 return FALSE;
106
107         ret = pread(fd, buf, sizeof(buf)-1, 0);
108         if (ret <= 0)
109                 return FALSE;
110
111         buf[ret] = '\0';
112
113         if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
114                 return FALSE;
115
116         if (ptotal)
117                 *ptotal = pagesize * (gsize) total;
118         if (prss)
119                 *prss = pagesize * (gsize) rss;
120
121         return TRUE;
122 }
123
124 static gsize
125 linux_get_total_mem_used_by_app(void)
126 {
127         gsize total;
128
129         if (!linux_get_memory(&total, NULL))
130                 total = 0;
131
132         return total;
133 }
134
135 static gsize
136 linux_get_rss_mem_used_by_app(void)
137 {
138         gsize rss;
139
140         if (!linux_get_memory(NULL, &rss))
141                 rss = 0;
142
143         return rss;
144 }
145
146 #define get_total_mem_used_by_app linux_get_total_mem_used_by_app
147
148 #define get_rss_mem_used_by_app linux_get_rss_mem_used_by_app
149
150 #endif
151
152 /* XXX, BSD 4.3: getrusage() -> ru_ixrss ? */
153
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 };
156 #endif
157
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 };
160 #endif
161
162 static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
163 #ifdef get_total_mem_used_by_app
164         &total_usage,
165 #endif
166 #ifdef get_rss_mem_used_by_app
167         &rss_usage,
168 #endif
169 };
170
171 static guint memory_register_num = 0
172 #ifdef get_total_mem_used_by_app
173         + 1
174 #endif
175 #ifdef get_rss_mem_used_by_app
176         + 1
177 #endif
178         ;
179
180 /* public API */
181
182 void
183 memory_usage_component_register(const ws_mem_usage_t *component)
184 {
185         if (memory_register_num >= MAX_COMPONENTS)
186                 return;
187
188         memory_components[memory_register_num++] = component;
189 }
190
191 const char *
192 memory_usage_get(guint index, gsize *value)
193 {
194         if (index >= memory_register_num)
195                 return NULL;
196
197         if (value)
198                 *value = memory_components[index]->fetch();
199
200         return memory_components[index]->name;
201 }
202
203 void
204 memory_usage_gc(void)
205 {
206         guint i;
207
208         for (i = 0; i < memory_register_num; i++) {
209                 if (memory_components[i]->gc)
210                         memory_components[i]->gc();
211         }
212 }
213
214
215 /*
216  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
217  *
218  * Local variables:
219  * c-basic-offset: 8
220  * tab-width: 8
221  * indent-tabs-mode: t
222  * End:
223  *
224  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
225  * :indentSize=8:tabSize=8:noTabs=false:
226  */