-
Notifications
You must be signed in to change notification settings - Fork 19
New capability endpoint to add CPU information (model, vendor, frequency) and memory details (clock, size, type) to API #801
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5554e92
Update CPU and memory details by switching to lshw method instead of …
aliel 98a19a5
Refactor get_hardware_info() to use async subprocess.run for improved…
aliel eec6835
Fix code style
aliel eaaa827
Remove duplicate cors headers
olethanh e3473ed
Problem: async funcs cannot be lru_cached
olethanh e40ac8f
fix lshw parsing for memory info
olethanh 58acf97
Move async_cache decorator to utils module
olethanh 156ef90
Change router order
olethanh 252873f
Adapt the tests
olethanh 7b8bdf4
add test for /about/capability
olethanh c681817
Fix dependencies in ci
olethanh bfdd26a
Fix tests and parsing
olethanh 03a1943
mod: uniformize coding style
olethanh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
import asyncio | ||
import json | ||
import re | ||
import shutil | ||
|
||
import psutil | ||
|
||
from aleph.vm.utils import run_in_subprocess | ||
|
||
|
||
async def get_hardware_info(): | ||
lshw_path = shutil.which("lshw") | ||
assert lshw_path, "lshw not found in PATH. apt install lshw." | ||
lshw_output = await run_in_subprocess([lshw_path, "-sanitize", "-json"]) | ||
data = json.loads(lshw_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"] | ||
|
||
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" | ||
else: | ||
architecture = None | ||
|
||
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(), | ||
} | ||
|
||
|
||
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 "", | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.