4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #if defined(__linux__)
12 #define _XOPEN_SOURCE 500
24 #if defined(__linux__)
25 # include <sys/types.h>
26 # include <sys/stat.h>
31 #include "wsutil/file_util.h"
32 #include "app_mem_usage.h"
34 #define MAX_COMPONENTS 16
39 win32_get_total_mem_used_by_app(void)
42 PROCESS_MEMORY_COUNTERS pmc
;
43 SIZE_T workingSize
= 0;
45 pHandle
= GetCurrentProcess();
47 if (GetProcessMemoryInfo(pHandle
, &pmc
, sizeof(pmc
))){
48 workingSize
= pmc
.WorkingSetSize
;
56 return (int)workingSize
;
60 static const ws_mem_usage_t total_usage
= { "Total", win32_get_total_mem_used_by_app
, NULL
};
62 static const ws_mem_usage_t
*memory_components
[MAX_COMPONENTS
] = {
66 static unsigned memory_register_num
= 1;
68 #elif defined(__linux__)
71 linux_get_memory(size_t *ptotal
, size_t *prss
)
74 static intptr_t pagesize
= 0;
77 unsigned long total
, rss
;
81 pagesize
= sysconf(_SC_PAGESIZE
);
89 snprintf(path
, sizeof(path
), "/proc/%d/statm", getpid());
91 fd
= ws_open(path
, O_RDONLY
);
93 /* XXX, fallback to some other /proc file ? */
99 ret
= pread(fd
, buf
, sizeof(buf
)-1, 0);
105 if (sscanf(buf
, "%lu %lu", &total
, &rss
) != 2)
109 *ptotal
= pagesize
* (size_t) total
;
111 *prss
= pagesize
* (size_t) rss
;
117 linux_get_total_mem_used_by_app(void)
121 if (!linux_get_memory(&total
, NULL
))
128 linux_get_rss_mem_used_by_app(void)
132 if (!linux_get_memory(NULL
, &rss
))
138 static const ws_mem_usage_t total_usage
= { "Total", linux_get_total_mem_used_by_app
, NULL
};
139 static const ws_mem_usage_t rss_usage
= { "RSS", linux_get_rss_mem_used_by_app
, NULL
};
141 static const ws_mem_usage_t
*memory_components
[MAX_COMPONENTS
] = {
146 static unsigned memory_register_num
= 2;
151 * macOS: task_info()?
153 * *BSD: getrusage() -> ru_ixrss ? Note that there are three
154 * current-RSS components in struct rusage, but those date
155 * back to the days when you had just text, data, and stack,
156 * and kernels might not even bother supplying them.
159 static const ws_mem_usage_t
*memory_components
[MAX_COMPONENTS
];
161 static unsigned memory_register_num
;
168 memory_usage_component_register(const ws_mem_usage_t
*component
)
170 if (memory_register_num
>= MAX_COMPONENTS
)
173 memory_components
[memory_register_num
++] = component
;
177 memory_usage_get(unsigned idx
, size_t *value
)
179 if (idx
>= memory_register_num
)
183 *value
= memory_components
[idx
]->fetch();
185 return memory_components
[idx
]->name
;
189 memory_usage_gc(void)
193 for (i
= 0; i
< memory_register_num
; i
++) {
194 if (memory_components
[i
]->gc
)
195 memory_components
[i
]->gc();
201 * Editor modelines - https://www.wireshark.org/tools/modelines.html
206 * indent-tabs-mode: t
209 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
210 * :indentSize=8:tabSize=8:noTabs=false: