Skip to content

Commit d40572c

Browse files
committed
gh-150114: Log the memory usage in regrtest on Windows
1 parent 1d28f9a commit d40572c

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lib/test/libregrtest/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import tempfile
1414
import textwrap
1515
from collections.abc import Callable
16+
try:
17+
from _testcapi import get_process_memory_usage as _get_process_memory_usage
18+
except AttributeError:
19+
_get_process_memory_usage = None
1620

1721
from test import support
1822
from test.support import os_helper
@@ -756,8 +760,13 @@ def display_title(title):
756760

757761
def get_process_memory_usage(pid: int) -> int | None:
758762
"""
759-
Read the private memory in bytes from /proc/pid/smaps.
763+
Get process memory usage in bytes.
760764
"""
765+
if _get_process_memory_usage is not None:
766+
return _get_process_memory_usage(pid)
767+
768+
# Linux implementation: read the private memory in bytes from
769+
# /proc/pid/smaps.
761770
try:
762771
fp = open(f"/proc/{pid}/smaps", "rb")
763772
except OSError:

Modules/_testcapi/mem.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "parts.h"
22

3+
#ifdef MS_WINDOWS
4+
# include <windows.h>
5+
# include <psapi.h> // GetProcessMemoryInfo()
6+
#endif
37
#include <stddef.h>
48

59

@@ -684,6 +688,34 @@ tracemalloc_track_race(PyObject *self, PyObject *args)
684688
}
685689

686690

691+
#ifdef MS_WINDOWS
692+
// Get process memory usage in bytes.
693+
static PyObject *
694+
get_process_memory_usage(PyObject *self, PyObject *args)
695+
{
696+
int pid;
697+
if (!PyArg_ParseTuple(args, "i", &pid)) {
698+
return NULL;
699+
}
700+
701+
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
702+
if (handle == NULL) {
703+
return PyErr_SetFromWindowsErr(0);
704+
}
705+
706+
PROCESS_MEMORY_COUNTERS pmc;
707+
if (!GetProcessMemoryInfo(handle, &pmc, sizeof(pmc))) {
708+
CloseHandle(handle);
709+
return PyErr_SetFromWindowsErr(0);
710+
}
711+
CloseHandle(handle);
712+
713+
size_t size = (pmc.WorkingSetSize + pmc.PagefileUsage);
714+
return PyLong_FromSize_t(size);
715+
}
716+
#endif
717+
718+
687719
static PyMethodDef test_methods[] = {
688720
{"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
689721
{"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
@@ -698,6 +730,9 @@ static PyMethodDef test_methods[] = {
698730
{"test_pymem_setrawallocators", test_pymem_setrawallocators, METH_NOARGS},
699731
{"test_pyobject_new", test_pyobject_new, METH_NOARGS},
700732
{"test_pyobject_setallocators", test_pyobject_setallocators, METH_NOARGS},
733+
#ifdef MS_WINDOWS
734+
{"get_process_memory_usage", get_process_memory_usage, METH_VARARGS},
735+
#endif
701736

702737
// Tracemalloc tests
703738
{"tracemalloc_track", tracemalloc_track, METH_VARARGS},

0 commit comments

Comments
 (0)