|
3 | 3 | from functools import lru_cache
|
4 | 4 | from typing import Optional
|
5 | 5 |
|
6 |
| -import cpuinfo |
7 | 6 | import psutil
|
8 | 7 | from aiohttp import web
|
| 8 | +from aleph.vm.conf import settings |
| 9 | +from aleph.vm.orchestrator.machine import get_cpu_info, get_memory_info |
9 | 10 | from aleph_message.models import ItemHash
|
10 | 11 | from aleph_message.models.execution.environment import CpuProperties
|
11 | 12 | from pydantic import BaseModel, Field
|
12 | 13 |
|
13 |
| -from aleph.vm.conf import settings |
14 |
| - |
15 | 14 |
|
16 | 15 | class Period(BaseModel):
|
17 | 16 | datetime: datetime
|
@@ -76,18 +75,66 @@ class MachineUsage(BaseModel):
|
76 | 75 | active: bool = True
|
77 | 76 |
|
78 | 77 |
|
| 78 | +class ExtendedCpuProperties(CpuProperties): |
| 79 | + """CPU properties.""" |
| 80 | + |
| 81 | + model: Optional[str] = Field(default=None, description="CPU model") |
| 82 | + frequency: Optional[str] = Field(default=None, description="CPU frequency") |
| 83 | + count: Optional[str] = Field(default=None, description="CPU count") |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +class MemoryProperties(BaseModel): |
| 88 | + """MEMORY properties.""" |
| 89 | + |
| 90 | + size: Optional[str] = Field(default=None, description="Memory size") |
| 91 | + units: Optional[str] = Field(default=None, description="Memory size units") |
| 92 | + type: Optional[str] = Field(default=None, description="Memory type") |
| 93 | + clock: Optional[str] = Field(default=None, description="Memory clock") |
| 94 | + clock_units: Optional[str] = Field(default=None, description="Memory clock units") |
| 95 | + |
| 96 | + |
| 97 | +class MachineCapability(BaseModel): |
| 98 | + cpu: ExtendedCpuProperties |
| 99 | + memory: MemoryProperties |
| 100 | + |
| 101 | + |
79 | 102 | @lru_cache
|
80 | 103 | def get_machine_properties() -> MachineProperties:
|
81 | 104 | """Fetch machine properties such as architecture, CPU vendor, ...
|
82 | 105 | These should not change while the supervisor is running.
|
83 | 106 |
|
84 | 107 | In the future, some properties may have to be fetched from within a VM.
|
85 | 108 | """
|
86 |
| - cpu_info = cpuinfo.get_cpu_info() # Slow |
| 109 | + |
| 110 | + cpu_info = get_cpu_info() |
87 | 111 | return MachineProperties(
|
88 | 112 | cpu=CpuProperties(
|
89 |
| - architecture=cpu_info["raw_arch_string"], |
90 |
| - vendor=cpu_info["vendor_id"], |
| 113 | + architecture=cpu_info["architecture"], |
| 114 | + vendor=cpu_info["vendor"], |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@lru_cache |
| 120 | +def get_machine_capability() -> MachineCapability: |
| 121 | + cpu_info = get_cpu_info() |
| 122 | + mem_info = get_memory_info() |
| 123 | + |
| 124 | + return MachineCapability( |
| 125 | + cpu=ExtendedCpuProperties( |
| 126 | + architecture=cpu_info["architecture"], |
| 127 | + vendor=cpu_info["vendor"], |
| 128 | + model=cpu_info["model"], |
| 129 | + frequency=cpu_info["frequency"], |
| 130 | + count=cpu_info["count"], |
| 131 | + ), |
| 132 | + memory=MemoryProperties( |
| 133 | + size=mem_info["size"], |
| 134 | + units=mem_info["units"], |
| 135 | + type=mem_info["type"], |
| 136 | + clock=mem_info["clock"], |
| 137 | + clock_units=mem_info["clock_units"], |
91 | 138 | ),
|
92 | 139 | )
|
93 | 140 |
|
@@ -119,6 +166,13 @@ async def about_system_usage(_: web.Request):
|
119 | 166 | return web.json_response(text=usage.json(exclude_none=True), headers={"Access-Control-Allow-Origin:": "*"})
|
120 | 167 |
|
121 | 168 |
|
| 169 | +async def about_capability(_: web.Request): |
| 170 | + """Public endpoint to expose information about the CRN capability.""" |
| 171 | + |
| 172 | + capability: MachineCapability = get_machine_capability() |
| 173 | + return web.json_response(text=capability.json(exclude_none=False), headers={"Access-Control-Allow-Origin:": "*"}) |
| 174 | + |
| 175 | + |
122 | 176 | class Allocation(BaseModel):
|
123 | 177 | """An allocation is the set of resources that are currently allocated on this orchestrator.
|
124 | 178 | It contains the item_hashes of all persistent VMs, instances, on-demand VMs and jobs.
|
|
0 commit comments