Skip to content

Commit

Permalink
Refactor get_hardware_info() to use async subprocess.run for improved…
Browse files Browse the repository at this point in the history
… concurrency
  • Loading branch information
aliel committed Apr 5, 2024
1 parent d82570d commit a483efd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
26 changes: 17 additions & 9 deletions src/aleph/vm/orchestrator/machine.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import asyncio
import json
import re
import subprocess
from functools import lru_cache

import psutil


@lru_cache
def get_hardware_info():
lshw = subprocess.Popen(["lshw", "-sanitize", "-json"], stdout=subprocess.PIPE, shell=False)
output, _ = lshw.communicate()
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 = {}
hw_info = {
"cpu": None,
"memory": None
}

for hw in data["children"][0]["children"]:
if hw["id"] == "cpu":
Expand All @@ -24,8 +32,8 @@ def get_hardware_info():


@lru_cache
def get_cpu_info():
hw = get_hardware_info()
async def get_cpu_info():
hw = await get_hardware_info()

cpu_info = hw["cpu"]
architecture = cpu_info["width"]
Expand Down Expand Up @@ -53,8 +61,8 @@ def get_cpu_info():


@lru_cache
def get_memory_info():
hw = get_hardware_info()
async def get_memory_info():
hw = await get_hardware_info()
mem_info = hw["memory"]

memory_type = ""
Expand Down
14 changes: 7 additions & 7 deletions src/aleph/vm/orchestrator/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ class MachineCapability(BaseModel):


@lru_cache
def get_machine_properties() -> MachineProperties:
async def get_machine_properties() -> MachineProperties:
"""Fetch machine properties such as architecture, CPU vendor, ...
These should not change while the supervisor is running.
In the future, some properties may have to be fetched from within a VM.
"""

cpu_info = get_cpu_info()
cpu_info = await get_cpu_info()
return MachineProperties(
cpu=CpuProperties(
architecture=cpu_info["architecture"],
Expand All @@ -117,9 +117,9 @@ def get_machine_properties() -> MachineProperties:


@lru_cache
def get_machine_capability() -> MachineCapability:
cpu_info = get_cpu_info()
mem_info = get_memory_info()
async def get_machine_capability() -> MachineCapability:
cpu_info = await get_cpu_info()
mem_info = await get_memory_info()

return MachineCapability(
cpu=ExtendedCpuProperties(
Expand Down Expand Up @@ -161,15 +161,15 @@ async def about_system_usage(_: web.Request):
start_timestamp=period_start,
duration_seconds=60,
),
properties=get_machine_properties(),
properties=await get_machine_properties(),
)
return web.json_response(text=usage.json(exclude_none=True), headers={"Access-Control-Allow-Origin:": "*"})


async def about_capability(_: web.Request):
"""Public endpoint to expose information about the CRN capability."""

capability: MachineCapability = get_machine_capability()
capability: MachineCapability = await get_machine_capability()
return web.json_response(text=capability.json(exclude_none=False), headers={"Access-Control-Allow-Origin:": "*"})


Expand Down

0 comments on commit a483efd

Please sign in to comment.