Skip to content

Commit

Permalink
Add swap memory info for Linux machines
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Jan 2, 2025
1 parent d33dd4a commit 045dd92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyarchitecture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pyarchitecture import cpu, disks, gpu, memory

version = "0.1.0a3"
version = "0.1.0a4"


def all_components() -> Dict[str, Any]:
Expand Down
1 change: 1 addition & 0 deletions pyarchitecture/disks/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def reformat_windows(data: Dict[str, str | int | float]) -> Dict[str, str]:
data["size"] = squire.size_converter(data["Size"])
del data["Caption"]
del data["Model"]
del data["DeviceID"]
return data


Expand Down
33 changes: 25 additions & 8 deletions pyarchitecture/memory/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@ def get_memory_info(mem_lib: str | os.PathLike) -> Dict[str, int | str]:
Returns the memory information as key-value pairs.
"""
memory_info = {}
with open(mem_lib) as f:
for line in f:
with open(mem_lib) as mem_file:
for line in mem_file:
if line.startswith(
("MemTotal", "MemFree", "MemAvailable", "Buffers", "Cached")
(
"MemTotal",
"MemFree",
"MemAvailable",
"SwapTotal",
"SwapFree",
)
):
parts = line.split()
memory_info[parts[0][:-1]] = int(
parts[1]
) # Convert the memory value to int (in kB)
# Convert the memory value to int (in kB)
memory_info[parts[0][:-1]] = int(parts[1])

# Convert values to bytes (kB to bytes)
# Physical memory (kB to bytes)
total = memory_info.get("MemTotal", 0) * 1024
free = memory_info.get("MemFree", 0) * 1024
available = memory_info.get("MemAvailable", 0) * 1024
used = total - free - available

return {"total": total, "free": free, "used": used}
# Swap memory (kB bytes)
swap_total = memory_info.get("SwapTotal", 0) * 1024
swap_free = memory_info.get("SwapFree", 0) * 1024
swap_used = swap_total - swap_free

return {
"total": total,
"free": free,
"used": used,
"swap_total": swap_total,
"swap_used": swap_used,
"swap_free": swap_free,
}

0 comments on commit 045dd92

Please sign in to comment.