aboutsummaryrefslogtreecommitdiffstats
path: root/epan/app_mem_usage.c
blob: adcf13f3348f657b705d314169332d2192c69f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * app_mem_usage.c
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#if defined(__linux__)
 #define _XOPEN_SOURCE 500
#endif

#include "config.h"

#include <stdio.h>

#include <glib.h>

#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#endif /*  _WIN32 */

#if defined(__linux__)
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include <fcntl.h>
#endif

#include "wsutil/file_util.h"
#include "app_mem_usage.h"

#define MAX_COMPONENTS 16

#if defined(_WIN32)

static gsize
win32_get_total_mem_used_by_app(void)
{
	HANDLE pHandle;
	PROCESS_MEMORY_COUNTERS pmc;
	SIZE_T workingSize = 0;

	pHandle = GetCurrentProcess();

	if (GetProcessMemoryInfo(pHandle, &pmc, sizeof(pmc))){
		workingSize = pmc.WorkingSetSize;
	}

	CloseHandle(pHandle);

	if(workingSize == 0){
		return -1;
	}else{
		return (int)workingSize;
	}
}

static const ws_mem_usage_t total_usage = { "Total", win32_get_total_mem_used_by_app, NULL };

static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
	&total_usage,
};

static guint memory_register_num = 1;

#elif defined(__linux__)

static gboolean
linux_get_memory(gsize *ptotal, gsize *prss)
{
	static int fd = -1;
	static intptr_t pagesize = 0;

	char buf[128];
	unsigned long total, rss;
	ssize_t ret;

	if (!pagesize)
		pagesize = sysconf(_SC_PAGESIZE);

	if (pagesize == -1)
		return FALSE;

	if (fd < 0) {
		char path[64];

		g_snprintf(path, sizeof(path), "/proc/%d/statm", getpid());

		fd = ws_open(path, O_RDONLY);

		/* XXX, fallback to some other /proc file ? */
	}

	if (fd < 0)
		return FALSE;

	ret = pread(fd, buf, sizeof(buf)-1, 0);
	if (ret <= 0)
		return FALSE;

	buf[ret] = '\0';

	if (sscanf(buf, "%lu %lu", &total, &rss) != 2)
		return FALSE;

	if (ptotal)
		*ptotal = pagesize * (gsize) total;
	if (prss)
		*prss = pagesize * (gsize) rss;

	return TRUE;
}

static gsize
linux_get_total_mem_used_by_app(void)
{
	gsize total;

	if (!linux_get_memory(&total, NULL))
		total = 0;

	return total;
}

static gsize
linux_get_rss_mem_used_by_app(void)
{
	gsize rss;

	if (!linux_get_memory(NULL, &rss))
		rss = 0;

	return rss;
}

static const ws_mem_usage_t total_usage = { "Total", linux_get_total_mem_used_by_app, NULL };
static const ws_mem_usage_t rss_usage = { "RSS", linux_get_rss_mem_used_by_app, NULL };

static const ws_mem_usage_t *memory_components[MAX_COMPONENTS] = {
	&total_usage,
	&rss_usage,
};

static guint memory_register_num = 2;

#else

/*
 * macOS: task_info()?
 *
 * *BSD: getrusage() -> ru_ixrss ?  Note that there are three
 * current-RSS components in struct rusage, but those date
 * back to the days when you had just text, data, and stack,
 * and kernels might not even bother supplying them.
 */

static const ws_mem_usage_t *memory_components[MAX_COMPONENTS];

static guint memory_register_num = 0;

#endif

/* public API */

void
memory_usage_component_register(const ws_mem_usage_t *component)
{
	if (memory_register_num >= MAX_COMPONENTS)
		return;

	memory_components[memory_register_num++] = component;
}

const char *
memory_usage_get(guint idx, gsize *value)
{
	if (idx >= memory_register_num)
		return NULL;

	if (value)
		*value = memory_components[idx]->fetch();

	return memory_components[idx]->name;
}

void
memory_usage_gc(void)
{
	guint i;

	for (i = 0; i < memory_register_num; i++) {
		if (memory_components[i]->gc)
			memory_components[i]->gc();
	}
}


/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */