-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vsivanandharao_expedia
committed
Jan 1, 2025
1 parent
f073125
commit 40759b0
Showing
7 changed files
with
235 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
from typing import Dict | ||
|
||
|
||
def get_memory_info(mem_lib: str | os.PathLike) -> Dict[str, int | str]: | ||
"""Get memory information on Linux systems. | ||
Args: | ||
mem_lib: Memory library path. | ||
Returns: | ||
Dict[str, int]: | ||
Returns the memory information as key-value pairs. | ||
""" | ||
memory_info = {} | ||
with open(mem_lib) as f: | ||
for line in f: | ||
if line.startswith( | ||
("MemTotal", "MemFree", "MemAvailable", "Buffers", "Cached") | ||
): | ||
parts = line.split() | ||
memory_info[parts[0][:-1]] = int( | ||
parts[1] | ||
) # Convert the memory value to int (in kB) | ||
|
||
# Convert values to bytes (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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import subprocess | ||
from typing import Dict | ||
|
||
from pyarchitecture import squire | ||
|
||
|
||
def byte_value(text: str, key: str) -> int: | ||
"""Converts the string value to bytes. | ||
Args: | ||
text: Text containing the value. | ||
key: Key to extract the value. | ||
Returns: | ||
int: | ||
Returns the value in bytes as an integer. | ||
""" | ||
return squire.convert_to_bytes(text.split(f"{key} = ")[1].split()[0]) | ||
|
||
|
||
def get_sysctl_value(mem_lib: str | os.PathLike, key: str): | ||
"""Get the value of the key from sysctl. | ||
Args: | ||
mem_lib: Memory library path. | ||
key: Key to extract the value. | ||
Returns: | ||
int: | ||
Returns the value of the key as an integer. | ||
""" | ||
result = subprocess.run([mem_lib, key], capture_output=True, text=True) | ||
if text := result.stdout.strip(): | ||
if key == "vm.swapusage": | ||
swap_usage = {} | ||
if "total" in text: | ||
swap_usage["swap_total"] = byte_value(text, "total") | ||
if "used" in text: | ||
swap_usage["swap_used"] = byte_value(text, "used") | ||
if "free" in text: | ||
swap_usage["swap_free"] = byte_value(text, "free") | ||
return swap_usage | ||
return int(text.split(":")[1].strip()) | ||
return 0 | ||
|
||
|
||
def get_memory_info(mem_lib: str | os.PathLike) -> Dict[str, int | str]: | ||
"""Get memory information on macOS systems. | ||
Args: | ||
mem_lib: Memory library path. | ||
Returns: | ||
Dict[str, int]: | ||
Returns the memory information as key-value pairs. | ||
""" | ||
# Physical memory information | ||
total = get_sysctl_value(mem_lib, "hw.memsize") | ||
free = get_sysctl_value(mem_lib, "vm.page_free_count") * get_sysctl_value( | ||
mem_lib, "hw.pagesize" | ||
) | ||
used = total - free | ||
|
||
# Virtual memory information | ||
swap_info = get_sysctl_value(mem_lib, "vm.swapusage") | ||
|
||
return { | ||
**{ | ||
"total": total, | ||
"free": free, | ||
"used": used, | ||
}, | ||
**swap_info, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import ctypes | ||
import logging | ||
from typing import Dict | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class MEMORYSTATUSEX(ctypes.Structure): | ||
"""Structure for the GlobalMemoryStatusEx function overriden by ctypes.Structure. | ||
>>> MEMORYSTATUSEX | ||
""" | ||
|
||
_fields_ = [ | ||
("dwLength", ctypes.c_uint), | ||
("dwMemoryLoad", ctypes.c_uint), | ||
("ullTotalPhys", ctypes.c_ulonglong), | ||
("ullAvailPhys", ctypes.c_ulonglong), | ||
("ullTotalPageFile", ctypes.c_ulonglong), | ||
("ullAvailPageFile", ctypes.c_ulonglong), | ||
("ullTotalVirtual", ctypes.c_ulonglong), | ||
("ullAvailVirtual", ctypes.c_ulonglong), | ||
("sullAvailExtendedVirtual", ctypes.c_ulonglong), | ||
] | ||
|
||
|
||
def get_memory_info(*args) -> Dict[str, int]: | ||
"""Get memory information for Windows OS. | ||
Returns: | ||
Dict[str, int]: | ||
Returns the memory information as key-value pairs. | ||
""" | ||
# Initialize the MEMORYSTATUSEX structure | ||
memory_status = MEMORYSTATUSEX() | ||
memory_status.dwLength = ctypes.sizeof(MEMORYSTATUSEX) | ||
|
||
# Load the kernel32 DLL and call GlobalMemoryStatusEx | ||
memory_info = ctypes.windll.kernel32.GlobalMemoryStatusEx | ||
|
||
# Call GlobalMemoryStatusEx to fill in the memory_status structure | ||
if memory_info(ctypes.byref(memory_status)) == 0: | ||
LOGGER.error("Failed to retrieve memory status") | ||
return {} | ||
|
||
# Extract the values from the structure | ||
total = memory_status.ullTotalPhys # Total physical memory (in bytes) | ||
available = memory_status.ullAvailPhys # Available physical memory (in bytes) | ||
used = total - available # Used memory (in bytes) | ||
|
||
# Optionally, you can also include virtual memory information | ||
virtual_total = memory_status.ullTotalVirtual | ||
virtual_available = memory_status.ullAvailVirtual | ||
|
||
return { | ||
"total": total, | ||
"available": available, | ||
"used": used, | ||
"virtual_total": virtual_total, | ||
"virtual_available": virtual_available, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters