Skip to content

Commit

Permalink
Determine total memory without psutil (mne-tools#12787)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrnr authored Sep 11, 2024
1 parent 5cd5299 commit 828953e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/12787.other.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use custom code in :func:`mne.sys_info` to get the amount of physical memory and a more informative CPU name instead of using the ``psutil`` package, by `Clemens Brunner`_.
60 changes: 55 additions & 5 deletions mne/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
_temp_home_dir = None


class UnknownPlatformError(Exception):
"""Exception raised for unknown platforms."""


def set_cache_dir(cache_dir):
"""Set the directory to be used for temporary file storage.
Expand Down Expand Up @@ -605,6 +609,47 @@ def _get_gpu_info():
return out


def _get_total_memory():
"""Return the total memory of the system in bytes."""
if platform.system() == "Windows":
o = subprocess.check_output(
[
"powershell.exe",
"(Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory",
]
).decode()
total_memory = int(o)
elif platform.system() == "Linux":
o = subprocess.check_output(["free", "-b"]).decode()
total_memory = int(o.splitlines()[1].split()[1])
elif platform.system() == "Darwin":
o = subprocess.check_output(["sysctl", "hw.memsize"]).decode()
total_memory = int(o.split(":")[1].strip())
else:
raise UnknownPlatformError("Could not determine total memory")

return total_memory


def _get_cpu_brand():
"""Return the CPU brand string."""
if platform.system() == "Windows":
o = subprocess.check_output(
["powershell.exe", "(Get-CimInstance Win32_Processor).Name"]
).decode()
cpu_brand = o.strip().splitlines()[-1]
elif platform.system() == "Linux":
o = subprocess.check_output(["grep", "model name", "/proc/cpuinfo"]).decode()
cpu_brand = o.splitlines()[0].split(": ")[1]
elif platform.system() == "Darwin":
o = subprocess.check_output(["sysctl", "machdep.cpu"]).decode()
cpu_brand = o.split("brand_string: ")[1].strip()
else:
cpu_brand = "?"

return cpu_brand


def sys_info(
fid=None,
show_paths=False,
Expand Down Expand Up @@ -656,15 +701,20 @@ def sys_info(
out("Platform".ljust(ljust) + platform_str + "\n")
out("Python".ljust(ljust) + str(sys.version).replace("\n", " ") + "\n")
out("Executable".ljust(ljust) + sys.executable + "\n")
out("CPU".ljust(ljust) + f"{platform.processor()} ")
try:
cpu_brand = _get_cpu_brand()
except Exception:
cpu_brand = "?"
out("CPU".ljust(ljust) + f"{cpu_brand} ")
out(f"({multiprocessing.cpu_count()} cores)\n")
out("Memory".ljust(ljust))
try:
import psutil
except ImportError:
out('Unavailable (requires "psutil" package)')
total_memory = _get_total_memory()
except UnknownPlatformError:
total_memory = "?"
else:
out(f"{psutil.virtual_memory().total / float(2 ** 30):0.1f} GB\n")
total_memory = f"{total_memory / 1024**3:.1f}" # convert to GiB
out(f"{total_memory} GiB\n")
out("\n")
ljust -= 3 # account for +/- symbols
libs = _get_numpy_libs()
Expand Down
2 changes: 1 addition & 1 deletion mne/utils/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_sys_info_basic():
assert "numpy" in out
# replace all in-line whitespace with single space
out = "\n".join(" ".join(o.split()) for o in out.splitlines())

assert "? GiB" not in out
if platform.system() == "Darwin":
assert "Platform macOS-" in out
elif platform.system() == "Linux":
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ full-no-qt = [
"jupyter",
"python-picard",
"joblib",
"psutil",
"dipy",
"vtk",
"nilearn",
Expand Down Expand Up @@ -175,6 +174,7 @@ doc = [
"intersphinx_registry>=0.2405.27",
# https://github.com/sphinx-contrib/sphinxcontrib-towncrier/issues/92
"towncrier<24.7",
"psutil",
]
dev = ["mne[test,doc]", "rcssmin"]

Expand Down

0 comments on commit 828953e

Please sign in to comment.