|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Advanced Micro Devices, Inc. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +############################################################################### |
| 26 | +from typing import Optional |
| 27 | + |
| 28 | +from nodescraper.base import InBandDataCollector |
| 29 | +from nodescraper.connection.inband.inband import CommandArtifact |
| 30 | +from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily |
| 31 | +from nodescraper.models import TaskResult |
| 32 | + |
| 33 | +from .deviceenumdata import DeviceEnumerationDataModel |
| 34 | + |
| 35 | + |
| 36 | +class DeviceEnumerationCollector(InBandDataCollector[DeviceEnumerationDataModel, None]): |
| 37 | + """Collect CPU and GPU count""" |
| 38 | + |
| 39 | + DATA_MODEL = DeviceEnumerationDataModel |
| 40 | + |
| 41 | + CMD_CPU_COUNT_LINUX = "lscpu | grep Socket | awk '{ print $2 }'" |
| 42 | + CMD_GPU_COUNT_LINUX = "lspci -d {vendorid_ep}: | grep -i 'VGA\\|Display\\|3D' | wc -l" |
| 43 | + CMD_VF_COUNT_LINUX = "lspci -d {vendorid_ep}: | grep -i 'Virtual Function' | wc -l" |
| 44 | + |
| 45 | + CMD_CPU_COUNT_WINDOWS = ( |
| 46 | + 'powershell -Command "(Get-WmiObject -Class Win32_Processor | Measure-Object).Count"' |
| 47 | + ) |
| 48 | + CMD_GPU_COUNT_WINDOWS = 'powershell -Command "(wmic path win32_VideoController get name | findstr AMD | Measure-Object).Count"' |
| 49 | + CMD_VF_COUNT_WINDOWS = ( |
| 50 | + 'powershell -Command "(Get-VMHostPartitionableGpu | Measure-Object).Count"' |
| 51 | + ) |
| 52 | + |
| 53 | + def _warning( |
| 54 | + self, |
| 55 | + description: str, |
| 56 | + command: CommandArtifact, |
| 57 | + category: EventCategory = EventCategory.PLATFORM, |
| 58 | + ): |
| 59 | + self._log_event( |
| 60 | + category=category, |
| 61 | + description=description, |
| 62 | + data={ |
| 63 | + "command": command.command, |
| 64 | + "stdout": command.stdout, |
| 65 | + "stderr": command.stderr, |
| 66 | + "exit_code": command.exit_code, |
| 67 | + }, |
| 68 | + priority=EventPriority.WARNING, |
| 69 | + ) |
| 70 | + |
| 71 | + def collect_data(self, args=None) -> tuple[TaskResult, Optional[DeviceEnumerationDataModel]]: |
| 72 | + """ |
| 73 | + Read CPU and GPU count |
| 74 | + On Linux, use lscpu and lspci |
| 75 | + On Windows, use WMI and hyper-v cmdlets |
| 76 | + """ |
| 77 | + if self.system_info.os_family == OSFamily.LINUX: |
| 78 | + # Count CPU sockets |
| 79 | + cpu_count_res = self._run_sut_cmd(self.CMD_CPU_COUNT_LINUX) |
| 80 | + |
| 81 | + # Count all AMD GPUs |
| 82 | + vendor_id = format(self.system_info.vendorid_ep, "x") |
| 83 | + gpu_count_res = self._run_sut_cmd( |
| 84 | + self.CMD_GPU_COUNT_LINUX.format(vendorid_ep=vendor_id) |
| 85 | + ) |
| 86 | + |
| 87 | + # Count AMD Virtual Functions |
| 88 | + vf_count_res = self._run_sut_cmd(self.CMD_VF_COUNT_LINUX.format(vendorid_ep=vendor_id)) |
| 89 | + else: |
| 90 | + cpu_count_res = self._run_sut_cmd(self.CMD_CPU_COUNT_WINDOWS) |
| 91 | + gpu_count_res = self._run_sut_cmd(self.CMD_GPU_COUNT_WINDOWS) |
| 92 | + vf_count_res = self._run_sut_cmd(self.CMD_VF_COUNT_WINDOWS) |
| 93 | + |
| 94 | + device_enum = DeviceEnumerationDataModel() |
| 95 | + |
| 96 | + if cpu_count_res.exit_code == 0: |
| 97 | + device_enum.cpu_count = int(cpu_count_res.stdout) |
| 98 | + else: |
| 99 | + self._warning(description="Cannot determine CPU count", command=cpu_count_res) |
| 100 | + |
| 101 | + if gpu_count_res.exit_code == 0: |
| 102 | + device_enum.gpu_count = int(gpu_count_res.stdout) |
| 103 | + else: |
| 104 | + self._warning(description="Cannot determine GPU count", command=gpu_count_res) |
| 105 | + |
| 106 | + if vf_count_res.exit_code == 0: |
| 107 | + device_enum.vf_count = int(vf_count_res.stdout) |
| 108 | + else: |
| 109 | + self._warning( |
| 110 | + description="Cannot determine VF count", |
| 111 | + command=vf_count_res, |
| 112 | + category=EventCategory.SW_DRIVER, |
| 113 | + ) |
| 114 | + |
| 115 | + if device_enum.cpu_count or device_enum.gpu_count or device_enum.vf_count: |
| 116 | + self._log_event( |
| 117 | + category=EventCategory.PLATFORM, |
| 118 | + description=f"Counted {device_enum.cpu_count} CPUs, {device_enum.gpu_count} GPUs, {device_enum.vf_count} VFs", |
| 119 | + data=device_enum.model_dump(exclude_none=True), |
| 120 | + priority=EventPriority.INFO, |
| 121 | + ) |
| 122 | + self.result.message = f"Device Enumeration: {device_enum.model_dump(exclude_none=True)}" |
| 123 | + self.result.status = ExecutionStatus.OK |
| 124 | + return self.result, device_enum |
| 125 | + else: |
| 126 | + self.result.message = "Device Enumeration info not found" |
| 127 | + self.result.status = ExecutionStatus.EXECUTION_FAILURE |
| 128 | + self._log_event( |
| 129 | + category=EventCategory.SW_DRIVER, |
| 130 | + description=self.result.message, |
| 131 | + priority=EventPriority.CRITICAL, |
| 132 | + ) |
| 133 | + return self.result, None |
0 commit comments