-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CPU information (model, vendor, frequency) and memory details (clock, size, type) to API #544
base: main
Are you sure you want to change the base?
Changes from all commits
d82570d
a4d2a5c
1a492ba
76c4982
7e54078
b7aaf47
6a3aff9
c4c2d0d
0e079a0
4795a85
b926497
e5c5a13
eac96f2
783e5ea
da65e94
c42e480
87122e8
befc521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import asyncio | ||
import json | ||
import re | ||
|
||
import psutil | ||
|
||
|
||
async def get_hardware_info(): | ||
lshw = await asyncio.create_subprocess_shell( | ||
"lshw -sanitize -json", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE | ||
) | ||
|
||
output, _ = await lshw.communicate() | ||
data = json.loads(output) | ||
|
||
hw_info = {"cpu": None, "memory": None} | ||
|
||
for hw in data["children"][0]["children"]: | ||
if hw["id"] == "cpu": | ||
hw_info["cpu"] = hw | ||
elif hw["class"] == "memory" and hw["id"] == "memory": | ||
hw_info["memory"] = hw | ||
|
||
return hw_info | ||
|
||
|
||
def get_cpu_info(hw): | ||
cpu_info = hw["cpu"] | ||
architecture = cpu_info["width"] | ||
|
||
if "x86_64" in cpu_info["capabilities"] or "x86-64" in cpu_info["capabilities"]: | ||
architecture = "x86_64" | ||
elif "arm64" in cpu_info["capabilities"] or "arm-64" in cpu_info["capabilities"]: | ||
architecture = "arm64" | ||
|
||
vendor = cpu_info["vendor"] | ||
# lshw vendor implementation => https://github.com/lyonel/lshw/blob/15e4ca64647ad119b69be63274e5de2696d3934f/src/core/cpuinfo.cc#L308 | ||
|
||
if "Intel Corp" in vendor: | ||
vendor = "GenuineIntel" | ||
elif "Advanced Micro Devices [AMD]" in vendor: | ||
vendor = "AuthenticAMD" | ||
|
||
return { | ||
"architecture": architecture, | ||
"vendor": vendor, | ||
"model": cpu_info["product"], | ||
"frequency": cpu_info["capacity"], | ||
"count": psutil.cpu_count(), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why return a dict and not instances of the Properties / Capabilities classes defined blow in the diff ? |
||
|
||
|
||
def get_memory_info(hw): | ||
mem_info = hw["memory"] | ||
|
||
memory_type = "" | ||
memory_clock = "" | ||
for bank in mem_info["children"]: | ||
memory_clock = bank.get("clock") | ||
if "description" in bank: | ||
matched = re.search("(DDR[2-6])", bank["description"]) | ||
if matched: | ||
memory_type = matched.group(0) | ||
break | ||
else: | ||
pass | ||
|
||
return { | ||
"size": mem_info["size"], | ||
"units": mem_info["units"], | ||
"type": memory_type, | ||
"clock": memory_clock, | ||
"clock_units": "Hz" if memory_clock is not None else "", | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not return an instance of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really the best approach to get the architecture ? Seems weird to look for it in the CPU capabilities.
What if a 32 bit system is running on a x86-64 capable system ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we want to know the architecture supported by the processor, not by the system
Obviously, 64-bit processors support 32-bit systems, but not vice versa.