-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
c2b4df5
commit 4868df5
Showing
3 changed files
with
74 additions
and
1 deletion.
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,50 @@ | ||
from pathlib import Path | ||
|
||
from linuxpy.util import try_numeric | ||
|
||
PROC_PATH = Path("/proc") | ||
|
||
CPU_INFO_PATH: Path = PROC_PATH / "cpuinfo" | ||
MEM_INFO_PATH: Path = PROC_PATH / "meminfo" | ||
MODULES_PATH: Path = PROC_PATH / "modules" | ||
|
||
|
||
def iter_cpu_info(): | ||
data = CPU_INFO_PATH.read_text() | ||
for cpu in data.split("\n\n"): | ||
info = {} | ||
for line in cpu.splitlines(): | ||
key, value = map(str.strip, line.split(":", 1)) | ||
if "flags" in key or key == "bugs": | ||
value = value.split() | ||
else: | ||
value = try_numeric(value) | ||
info[key] = value | ||
yield info | ||
|
||
|
||
def iter_mem_info(): | ||
data = MEM_INFO_PATH.read_text() | ||
for line in data.splitlines(): | ||
key, value = map(str.strip, line.split(":", 1)) | ||
if value.endswith(" kB"): | ||
value = try_numeric(value[:-3]) * 1024 | ||
else: | ||
value = try_numeric(value) | ||
yield key, value | ||
|
||
|
||
def iter_modules(): | ||
data = MODULES_PATH.read_text() | ||
for line in data.splitlines(): | ||
fields = line.split() | ||
mod = { | ||
"name": fields[0], | ||
"size": int(fields[1]), | ||
"use_count": int(fields[2]), | ||
"dependencies": [] if fields[3] == "-" else [dep for dep in fields[3].split(",") if dep], | ||
} | ||
if len(fields) > 5: | ||
mod["state"] = fields[4] | ||
mod["offset"] = int(fields[5], 16) | ||
yield mod |
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